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

Commit 659bb5c

Browse files
committed
feat: add refs.localPullStream && refs.localReadableStream
1 parent 2dc9265 commit 659bb5c

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

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

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ exports.refs = {
346346
parseArgs: exports.parseKey,
347347

348348
// main route handler which is called after the above `parseArgs`, but only if the args were valid
349-
async handler (request, h) {
349+
handler (request, h) {
350350
const { ipfs } = request.server.app
351351
const { key } = request.pre.args
352352
const recursive = request.query.r === 'true' || request.query.recursive === 'true'
@@ -358,31 +358,65 @@ exports.refs = {
358358
maxDepth = parseInt(maxDepth)
359359
}
360360

361-
let refs
362-
try {
363-
refs = await ipfs.refs(key, { recursive, format, e, u, maxDepth })
364-
} catch (err) {
365-
throw Boom.boomify(err, { message: 'Failed to get refs for path' })
366-
}
367-
368-
return h.response(refs)
361+
const source = ipfs.refsPullStream(key, { recursive, format, e, u, maxDepth })
362+
return sendRefsReplyStream(request, h, `refs for ${key}`, source)
369363
}
370364
}
371365

372366
exports.refs.local = {
373367
// main route handler
374-
async handler (request, h) {
368+
handler (request, h) {
375369
const { ipfs } = request.server.app
370+
const source = ipfs.refs.localPullStream()
371+
return sendRefsReplyStream(request, h, 'local refs', source)
372+
}
373+
}
376374

377-
let refs
378-
try {
379-
refs = await ipfs.refs.local()
380-
} catch (err) {
381-
throw Boom.boomify(err, { message: 'Failed to get local refs' })
382-
}
375+
function sendRefsReplyStream (request, h, desc, source) {
376+
const replyStream = pushable()
377+
const aborter = abortable()
378+
379+
const stream = toStream.source(pull(
380+
replyStream,
381+
aborter,
382+
ndjson.serialize()
383+
))
383384

384-
return h.response(refs)
385+
// const stream = toStream.source(replyStream.source)
386+
// hapi is not very clever and throws if no
387+
// - _read method
388+
// - _readableState object
389+
// are there :(
390+
if (!stream._read) {
391+
stream._read = () => {}
392+
stream._readableState = {}
393+
stream.unpipe = () => {}
385394
}
395+
396+
pull(
397+
source,
398+
pull.drain(
399+
(ref) => replyStream.push(ref),
400+
(err) => {
401+
if (err) {
402+
request.raw.res.addTrailers({
403+
'X-Stream-Error': JSON.stringify({
404+
Message: `Failed to get ${desc}: ${err.message || ''}`,
405+
Code: 0
406+
})
407+
})
408+
return aborter.abort()
409+
}
410+
411+
replyStream.end()
412+
}
413+
)
414+
)
415+
416+
return h.response(stream)
417+
.header('x-chunked-output', '1')
418+
.header('content-type', 'application/json')
419+
.header('Trailer', 'X-Stream-Error')
386420
}
387421

388422
exports.refs = {

0 commit comments

Comments
 (0)