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

Commit

Permalink
feat: add streaming option to http
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
achingbrain committed Dec 4, 2018
1 parent f993514 commit d832277
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 182 deletions.
4 changes: 4 additions & 0 deletions src/core/ls-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = (context) => {
path = FILE_SEPARATOR
}

if (path === undefined) {
path = FILE_SEPARATOR
}

options = Object.assign({}, defaultOptions, options)

options.long = options.l || options.long
Expand Down
71 changes: 63 additions & 8 deletions src/http/ls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
'use strict'

const Joi = require('joi')
const {
PassThrough
} = require('stream')

const mapEntry = (entry) => {
return {
Name: entry.name,
Type: entry.type,
Size: entry.size,
Hash: entry.hash
}
}

const mfsLs = (api) => {
api.route({
Expand All @@ -14,21 +26,59 @@ const mfsLs = (api) => {
const {
arg,
long,
cidBase
cidBase,
stream
} = request.query

if (stream) {
const readableStream = ipfs.files.lsReadableStream(arg, {
long,
cidBase
})

if (!readableStream._read) {
// make the stream look like a Streams2 to appease Hapi
readableStream._read = () => {}
readableStream._readableState = {}
}

let passThrough

readableStream.on('data', (entry) => {
if (!passThrough) {
passThrough = new PassThrough()

reply(passThrough)
.header('X-Stream-Output', '1')
}

passThrough.write(JSON.stringify(mapEntry(entry)) + '\n')
})

readableStream.once('end', (entry) => {
if (passThrough) {
passThrough.end(entry ? JSON.stringify(mapEntry(entry)) + '\n' : undefined)
}
})

readableStream.once('error', (error) => {
reply({
Message: error.message,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})

return
}

return ipfs.files.ls(arg, {
long,
cidBase
})
.then(files => {
reply({
Entries: files.map(file => ({
Name: file.name,
Type: file.type,
Size: file.size,
Hash: file.hash
}))
Entries: files.map(mapEntry)
})
})
.catch(error => {
Expand All @@ -47,12 +97,17 @@ const mfsLs = (api) => {
query: Joi.object().keys({
arg: Joi.string().default('/'),
long: Joi.boolean().default(false),
cidBase: Joi.string().default('base58btc')
cidBase: Joi.string().default('base58btc'),
stream: Joi.boolean().default(false)
})
.rename('l', 'long', {
override: true,
ignoreUndefined: true
})
.rename('s', 'stream', {
override: true,
ignoreUndefined: true
})
}
}
})
Expand Down
Loading

0 comments on commit d832277

Please sign in to comment.