Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit d8be8bb

Browse files
author
Alan Shaw
authored
feat: use it-tar (#2758)
`it-tar` now supports packing \o/
1 parent 8425168 commit d8be8bb

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
"it-glob": "0.0.7",
124124
"it-last": "^1.0.1",
125125
"it-pipe": "^1.1.0",
126+
"it-tar": "^1.2.1",
126127
"it-to-stream": "^0.1.1",
127128
"iterable-ndjson": "^1.1.0",
128129
"jsondiffpatch": "~0.3.11",
@@ -165,7 +166,6 @@
165166
"semver": "^7.1.2",
166167
"stream-to-it": "^0.2.0",
167168
"streaming-iterables": "^4.1.1",
168-
"tar-stream": "^2.0.0",
169169
"temp": "~0.9.0",
170170
"timeout-abort-controller": "^1.1.0",
171171
"update-notifier": "^4.0.0",

src/http/api/resources/files-regular.js

+20-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const multipart = require('ipfs-multipart')
44
const debug = require('debug')
5-
const tar = require('tar-stream')
5+
const tar = require('it-tar')
66
const log = debug('ipfs:http-api:files')
77
log.error = debug('ipfs:http-api:files:error')
88
const toIterable = require('stream-to-it')
@@ -11,16 +11,20 @@ const Boom = require('@hapi/boom')
1111
const { PassThrough } = require('stream')
1212
const multibase = require('multibase')
1313
const isIpfs = require('is-ipfs')
14-
const { promisify } = require('util')
1514
const { cidToString } = require('../../../utils/cid')
1615
const { Format } = require('../../../core/components/refs')
1716
const pipe = require('it-pipe')
1817
const all = require('it-all')
19-
const concat = require('it-concat')
2018
const ndjson = require('iterable-ndjson')
2119
const { map } = require('streaming-iterables')
2220
const streamResponse = require('../../utils/stream-response')
2321

22+
const toBuffer = async function * (source) {
23+
for await (const chunk of source) {
24+
yield chunk.slice()
25+
}
26+
}
27+
2428
function numberFromQuery (query, key) {
2529
if (query && query[key] !== undefined) {
2630
const value = parseInt(query[key], 10)
@@ -86,32 +90,24 @@ exports.get = {
8690
const { ipfs } = request.server.app
8791
const { key } = request.pre.args
8892

89-
const pack = tar.pack()
90-
pack.entry = promisify(pack.entry.bind(pack))
93+
return streamResponse(request, h, () => pipe(
94+
ipfs.get(key),
95+
async function * (source) {
96+
for await (const file of source) {
97+
const header = {
98+
name: file.path
99+
}
91100

92-
const streamFiles = async () => {
93-
try {
94-
for await (const file of ipfs.get(key)) {
95101
if (file.content) {
96-
const content = await concat(file.content)
97-
pack.entry({ name: file.path, size: file.size }, content.slice())
102+
yield { header: { ...header, size: file.size }, body: toBuffer(file.content) }
98103
} else {
99-
pack.entry({ name: file.path, type: 'directory' })
104+
yield { header: { ...header, type: 'directory' } }
100105
}
101106
}
102-
pack.finalize()
103-
} catch (err) {
104-
log.error(err)
105-
pack.emit('error', err)
106-
pack.destroy()
107-
}
108-
}
109-
110-
streamFiles()
111-
112-
// reply must be called right away so that tar-stream offloads its content
113-
// otherwise it will block in large files
114-
return h.response(pack).header('X-Stream-Output', '1')
107+
},
108+
tar.pack(),
109+
toBuffer
110+
))
115111
}
116112
}
117113

0 commit comments

Comments
 (0)