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

Commit 8ea66f9

Browse files
committed
feat: add missing progress bar support for http api
1 parent d311dc5 commit 8ea66f9

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@
9494
"bl": "^1.2.1",
9595
"boom": "^5.2.0",
9696
"byteman": "^1.3.5",
97-
"cids": "~0.5.1",
98-
"debug": "^3.0.1",
9997
"cids": "^0.5.1",
98+
"debug": "^3.0.1",
10099
"file-type": "^6.1.0",
101100
"filesize": "^3.5.10",
102101
"fsm-event": "^2.1.0",
@@ -144,6 +143,7 @@
144143
"progress": "^2.0.0",
145144
"promisify-es6": "^1.0.3",
146145
"pull-file": "^1.0.0",
146+
"pull-ndjson": "^0.1.1",
147147
"pull-paramap": "^1.2.2",
148148
"pull-pushable": "^2.1.1",
149149
"pull-sort": "^1.0.1",
@@ -220,4 +220,4 @@
220220
"Łukasz Magiera <[email protected]>",
221221
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
222222
]
223-
}
223+
}

src/http/api/resources/files.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ log.error = debug('jsipfs:http-api:files:error')
99
const pull = require('pull-stream')
1010
const toPull = require('stream-to-pull-stream')
1111
const pushable = require('pull-pushable')
12-
const EOL = require('os').EOL
1312
const toStream = require('pull-stream-to-stream')
1413
const Joi = require('joi')
14+
const ndjson = require('pull-ndjson')
1515

1616
exports = module.exports
1717

@@ -104,7 +104,7 @@ exports.get = {
104104
pull(
105105
stream,
106106
pull.asyncMap((file, cb) => {
107-
const header = {name: file.path}
107+
const header = { name: file.path }
108108
if (!file.content) {
109109
header.type = 'directory'
110110
pack.entry(header)
@@ -207,10 +207,34 @@ exports.add = {
207207
fileAdder.end()
208208
})
209209

210+
const replyStream = pushable()
211+
const progressHandler = (bytes) => {
212+
replyStream.push({ Bytes: bytes })
213+
}
214+
210215
const options = {
211216
'cid-version': request.query['cid-version'],
212-
'raw-leaves': request.query['raw-leaves']
217+
'raw-leaves': request.query['raw-leaves'],
218+
progress: request.query['progress'] ? progressHandler : null
219+
}
220+
221+
const stream = toStream.source(pull(
222+
replyStream,
223+
ndjson.serialize()
224+
))
225+
226+
// const stream = toStream.source(replyStream.source)
227+
// hapi is not very clever and throws if no
228+
// - _read method
229+
// - _readableState object
230+
// are there :(
231+
if (!stream._read) {
232+
stream._read = () => {}
233+
stream._readableState = {}
213234
}
235+
reply(stream)
236+
.header('x-chunked-output', '1')
237+
.header('content-type', 'application/json')
214238

215239
pull(
216240
fileAdder,
@@ -221,7 +245,6 @@ exports.add = {
221245
Hash: file.hash
222246
}
223247
}),
224-
pull.map((file) => JSON.stringify(file) + EOL),
225248
pull.collect((err, files) => {
226249
if (err) {
227250
return reply({
@@ -237,9 +260,8 @@ exports.add = {
237260
}).code(500)
238261
}
239262

240-
reply(files.join('\n'))
241-
.header('x-chunked-output', '1')
242-
.header('content-type', 'application/json')
263+
files.forEach((f) => replyStream.push(f))
264+
replyStream.end()
243265
})
244266
)
245267
}

test/cli/files.js

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ describe('files', () => runOnAndOff((thing) => {
106106
ipfs = thing.ipfs
107107
})
108108

109+
it('add with progress', () => {
110+
return ipfs('files add -p src/init-files/init-docs/readme')
111+
.then((out) => {
112+
expect(out)
113+
.to.eql('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme\n')
114+
})
115+
})
116+
109117
it('add', () => {
110118
return ipfs('files add src/init-files/init-docs/readme')
111119
.then((out) => {

0 commit comments

Comments
 (0)