|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const promisify = require('promisify-es6') |
| 3 | +const ndjson = require('iterable-ndjson') |
| 4 | +const toIterable = require('../lib/stream-to-iterable') |
| 5 | +const configure = require('../lib/configure') |
| 6 | +const toCamel = require('../lib/object-to-camel') |
4 | 7 |
|
5 |
| -const transform = function (res, callback) { |
6 |
| - const entries = res.Entries || [] |
| 8 | +module.exports = configure(({ ky }) => { |
| 9 | + return async function * ls (path, options) { |
| 10 | + options = options || {} |
7 | 11 |
|
8 |
| - callback(null, entries.map((entry) => { |
9 |
| - return { |
10 |
| - name: entry.Name, |
11 |
| - type: entry.Type, |
12 |
| - size: entry.Size, |
13 |
| - hash: entry.Hash |
14 |
| - } |
15 |
| - })) |
16 |
| -} |
| 12 | + const searchParams = new URLSearchParams(options.searchParams) |
| 13 | + searchParams.set('arg', `${path}`) |
| 14 | + searchParams.set('stream', true) |
| 15 | + if (options.cidBase) searchParams.set('cid-base', options.cidBase) |
| 16 | + if (options.long != null) searchParams.set('long', options.long) |
17 | 17 |
|
18 |
| -module.exports = (send) => { |
19 |
| - return promisify((args, opts, callback) => { |
20 |
| - if (typeof (opts) === 'function') { |
21 |
| - callback = opts |
22 |
| - opts = {} |
23 |
| - } |
| 18 | + const res = await ky.post('files/ls', { |
| 19 | + timeout: options.timeout, |
| 20 | + signal: options.signal, |
| 21 | + headers: options.headers, |
| 22 | + searchParams |
| 23 | + }) |
24 | 24 |
|
25 |
| - if (typeof (args) === 'function') { |
26 |
| - callback = args |
27 |
| - opts = {} |
28 |
| - args = null |
| 25 | + for await (const result of ndjson(toIterable(res.body))) { |
| 26 | + // go-ipfs does not yet support the "stream" option |
| 27 | + if ('Entries' in result) { |
| 28 | + for (const entry of result.Entries || []) { |
| 29 | + yield toCamel(entry) |
| 30 | + } |
| 31 | + return |
| 32 | + } |
| 33 | + yield toCamel(result) |
29 | 34 | }
|
30 |
| - |
31 |
| - return send.andTransform({ |
32 |
| - path: 'files/ls', |
33 |
| - args: args, |
34 |
| - qs: opts |
35 |
| - }, transform, callback) |
36 |
| - }) |
37 |
| -} |
| 35 | + } |
| 36 | +}) |
0 commit comments