Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit d832277

Browse files
committed
feat: add streaming option to http
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent f993514 commit d832277

File tree

3 files changed

+305
-182
lines changed

3 files changed

+305
-182
lines changed

src/core/ls-pull-stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module.exports = (context) => {
3030
path = FILE_SEPARATOR
3131
}
3232

33+
if (path === undefined) {
34+
path = FILE_SEPARATOR
35+
}
36+
3337
options = Object.assign({}, defaultOptions, options)
3438

3539
options.long = options.l || options.long

src/http/ls.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
'use strict'
22

33
const Joi = require('joi')
4+
const {
5+
PassThrough
6+
} = require('stream')
7+
8+
const mapEntry = (entry) => {
9+
return {
10+
Name: entry.name,
11+
Type: entry.type,
12+
Size: entry.size,
13+
Hash: entry.hash
14+
}
15+
}
416

517
const mfsLs = (api) => {
618
api.route({
@@ -14,21 +26,59 @@ const mfsLs = (api) => {
1426
const {
1527
arg,
1628
long,
17-
cidBase
29+
cidBase,
30+
stream
1831
} = request.query
1932

33+
if (stream) {
34+
const readableStream = ipfs.files.lsReadableStream(arg, {
35+
long,
36+
cidBase
37+
})
38+
39+
if (!readableStream._read) {
40+
// make the stream look like a Streams2 to appease Hapi
41+
readableStream._read = () => {}
42+
readableStream._readableState = {}
43+
}
44+
45+
let passThrough
46+
47+
readableStream.on('data', (entry) => {
48+
if (!passThrough) {
49+
passThrough = new PassThrough()
50+
51+
reply(passThrough)
52+
.header('X-Stream-Output', '1')
53+
}
54+
55+
passThrough.write(JSON.stringify(mapEntry(entry)) + '\n')
56+
})
57+
58+
readableStream.once('end', (entry) => {
59+
if (passThrough) {
60+
passThrough.end(entry ? JSON.stringify(mapEntry(entry)) + '\n' : undefined)
61+
}
62+
})
63+
64+
readableStream.once('error', (error) => {
65+
reply({
66+
Message: error.message,
67+
Code: error.code || 0,
68+
Type: 'error'
69+
}).code(500).takeover()
70+
})
71+
72+
return
73+
}
74+
2075
return ipfs.files.ls(arg, {
2176
long,
2277
cidBase
2378
})
2479
.then(files => {
2580
reply({
26-
Entries: files.map(file => ({
27-
Name: file.name,
28-
Type: file.type,
29-
Size: file.size,
30-
Hash: file.hash
31-
}))
81+
Entries: files.map(mapEntry)
3282
})
3383
})
3484
.catch(error => {
@@ -47,12 +97,17 @@ const mfsLs = (api) => {
4797
query: Joi.object().keys({
4898
arg: Joi.string().default('/'),
4999
long: Joi.boolean().default(false),
50-
cidBase: Joi.string().default('base58btc')
100+
cidBase: Joi.string().default('base58btc'),
101+
stream: Joi.boolean().default(false)
51102
})
52103
.rename('l', 'long', {
53104
override: true,
54105
ignoreUndefined: true
55106
})
107+
.rename('s', 'stream', {
108+
override: true,
109+
ignoreUndefined: true
110+
})
56111
}
57112
}
58113
})

0 commit comments

Comments
 (0)