2
2
3
3
const { Buffer } = require ( 'buffer' )
4
4
const CID = require ( 'cids' )
5
+ const ndjson = require ( 'iterable-ndjson' )
6
+ const toIterable = require ( 'stream-to-it/source' )
5
7
const configure = require ( './lib/configure' )
6
8
7
9
module . exports = configure ( ( { ky } ) => {
@@ -10,18 +12,11 @@ module.exports = configure(({ ky }) => {
10
12
11
13
const searchParams = new URLSearchParams ( )
12
14
searchParams . set ( 'arg' , `${ Buffer . isBuffer ( path ) ? new CID ( path ) : path } ` )
15
+ searchParams . set ( 'stream' , options . stream == null ? true : options . stream )
13
16
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 )
25
20
26
21
const res = await ky . post ( 'ls' , {
27
22
timeout : options . timeout ,
@@ -30,31 +25,32 @@ module.exports = configure(({ ky }) => {
30
25
searchParams
31
26
} )
32
27
33
- let result = await res . json ( )
28
+ for await ( let result of ndjson ( toIterable ( res . body ) ) ) {
29
+ result = result . Objects
34
30
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
+ }
39
34
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
+ }
44
39
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
+ }
49
44
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
+ }
58
54
}
59
55
}
60
56
}
0 commit comments