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

Commit 256bdce

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

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

package.json

Lines changed: 3 additions & 3 deletions
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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const pushable = require('pull-pushable')
1212
const EOL = require('os').EOL
1313
const toStream = require('pull-stream-to-stream')
1414
const Joi = require('joi')
15+
const ndjson = require('pull-ndjson')
1516

1617
exports = module.exports
1718

@@ -104,7 +105,7 @@ exports.get = {
104105
pull(
105106
stream,
106107
pull.asyncMap((file, cb) => {
107-
const header = {name: file.path}
108+
const header = { name: file.path }
108109
if (!file.content) {
109110
header.type = 'directory'
110111
pack.entry(header)
@@ -207,10 +208,35 @@ exports.add = {
207208
fileAdder.end()
208209
})
209210

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

215241
pull(
216242
fileAdder,
@@ -221,7 +247,6 @@ exports.add = {
221247
Hash: file.hash
222248
}
223249
}),
224-
pull.map((file) => JSON.stringify(file) + EOL),
225250
pull.collect((err, files) => {
226251
if (err) {
227252
return reply({
@@ -237,9 +262,8 @@ exports.add = {
237262
}).code(500)
238263
}
239264

240-
reply(files.join('\n'))
241-
.header('x-chunked-output', '1')
242-
.header('content-type', 'application/json')
265+
files.forEach((f) => replyStream.push(f))
266+
replyStream.end()
243267
})
244268
)
245269
}

test/cli/files.js

Lines changed: 8 additions & 0 deletions
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)