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

Commit 29e013a

Browse files
author
Alan Shaw
committed
feat: support streaming in ls
1 parent 2d43c18 commit 29e013a

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/ls.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const { Buffer } = require('buffer')
44
const CID = require('cids')
5+
const ndjson = require('iterable-ndjson')
6+
const toIterable = require('stream-to-it/source')
57
const configure = require('./lib/configure')
68

79
module.exports = configure(({ ky }) => {
@@ -10,18 +12,11 @@ module.exports = configure(({ ky }) => {
1012

1113
const searchParams = new URLSearchParams()
1214
searchParams.set('arg', `${Buffer.isBuffer(path) ? new CID(path) : path}`)
15+
searchParams.set('stream', options.stream == null ? true : options.stream)
1316

14-
if (options.long !== undefined) {
15-
searchParams.set('long', options.long)
16-
}
17-
18-
if (options.unsorted !== undefined) {
19-
searchParams.set('unsorted', options.unsorted)
20-
}
21-
22-
if (options.recursive !== undefined) {
23-
searchParams.set('recursive', options.recursive)
24-
}
17+
if (options.long != null) searchParams.set('long', options.long)
18+
if (options.unsorted != null) searchParams.set('unsorted', options.unsorted)
19+
if (options.recursive != null) searchParams.set('recursive', options.recursive)
2520

2621
const res = await ky.post('ls', {
2722
timeout: options.timeout,
@@ -30,31 +25,32 @@ module.exports = configure(({ ky }) => {
3025
searchParams
3126
})
3227

33-
let result = await res.json()
28+
for await (let result of ndjson(toIterable(res.body))) {
29+
result = result.Objects
3430

35-
result = result.Objects
36-
if (!result) {
37-
throw new Error('expected .Objects in results')
38-
}
31+
if (!result) {
32+
throw new Error('expected .Objects in results')
33+
}
3934

40-
result = result[0]
41-
if (!result) {
42-
throw new Error('expected one array in results.Objects')
43-
}
35+
result = result[0]
36+
if (!result) {
37+
throw new Error('expected one array in results.Objects')
38+
}
4439

45-
result = result.Links
46-
if (!Array.isArray(result)) {
47-
throw new Error('expected one array in results.Objects[0].Links')
48-
}
40+
result = result.Links
41+
if (!Array.isArray(result)) {
42+
throw new Error('expected one array in results.Objects[0].Links')
43+
}
4944

50-
for (const link of result) {
51-
yield {
52-
name: link.Name,
53-
path: path + '/' + link.Name,
54-
size: link.Size,
55-
cid: new CID(link.Hash),
56-
type: typeOf(link),
57-
depth: link.Depth || 1
45+
for (const link of result) {
46+
yield {
47+
name: link.Name,
48+
path: path + '/' + link.Name,
49+
size: link.Size,
50+
cid: new CID(link.Hash),
51+
type: typeOf(link),
52+
depth: link.Depth || 1
53+
}
5854
}
5955
}
6056
}

0 commit comments

Comments
 (0)