|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const ndjson = require('iterable-ndjson') |
4 | 3 | const CID = require('cids')
|
5 |
| -const configure = require('../lib/configure') |
6 |
| -const toIterable = require('stream-to-it/source') |
| 4 | +const merge = require('merge-options') |
7 | 5 | const { toFormData } = require('./form-data')
|
8 | 6 | const toCamel = require('../lib/object-to-camel')
|
| 7 | +const configure = require('../lib/configure') |
9 | 8 |
|
10 |
| -module.exports = configure(({ ky }) => { |
11 |
| - return async function * add (input, options) { |
12 |
| - options = options || {} |
13 |
| - |
14 |
| - const searchParams = new URLSearchParams(options.searchParams) |
15 |
| - |
16 |
| - searchParams.set('stream-channels', true) |
17 |
| - if (options.chunker) searchParams.set('chunker', options.chunker) |
18 |
| - if (options.cidVersion) searchParams.set('cid-version', options.cidVersion) |
19 |
| - if (options.cidBase) searchParams.set('cid-base', options.cidBase) |
20 |
| - if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment) |
21 |
| - if (options.hashAlg) searchParams.set('hash', options.hashAlg) |
22 |
| - if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash) |
23 |
| - if (options.pin != null) searchParams.set('pin', options.pin) |
24 |
| - if (options.progress) searchParams.set('progress', true) |
25 |
| - if (options.quiet != null) searchParams.set('quiet', options.quiet) |
26 |
| - if (options.quieter != null) searchParams.set('quieter', options.quieter) |
27 |
| - if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves) |
28 |
| - if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold) |
29 |
| - if (options.silent) searchParams.set('silent', options.silent) |
30 |
| - if (options.trickle != null) searchParams.set('trickle', options.trickle) |
31 |
| - if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory) |
32 |
| - if (options.preload != null) searchParams.set('preload', options.preload) |
33 |
| - if (options.fileImportConcurrency != null) searchParams.set('file-import-concurrency', options.fileImportConcurrency) |
34 |
| - if (options.blockWriteConcurrency != null) searchParams.set('block-write-concurrency', options.blockWriteConcurrency) |
| 9 | +module.exports = configure((api) => { |
| 10 | + return async function * add (input, options = {}) { |
| 11 | + const progressFn = options.progress |
| 12 | + options = merge( |
| 13 | + options, |
| 14 | + { |
| 15 | + 'stream-channels': true, |
| 16 | + progress: Boolean(progressFn), |
| 17 | + hash: options.hashAlg // TODO fix this either is hash or hashAlg |
| 18 | + } |
| 19 | + ) |
35 | 20 |
|
36 |
| - const res = await ky.post('add', { |
| 21 | + const res = await api.ndjson('add', { |
| 22 | + method: 'POST', |
| 23 | + searchParams: options, |
| 24 | + body: await toFormData(input), |
37 | 25 | timeout: options.timeout,
|
38 |
| - signal: options.signal, |
39 |
| - headers: options.headers, |
40 |
| - searchParams, |
41 |
| - body: await toFormData(input) |
| 26 | + signal: options.signal |
42 | 27 | })
|
43 | 28 |
|
44 |
| - for await (let file of ndjson(toIterable(res.body))) { |
| 29 | + for await (let file of res) { |
45 | 30 | file = toCamel(file)
|
46 | 31 |
|
47 |
| - if (options.progress && file.bytes) { |
48 |
| - options.progress(file.bytes) |
| 32 | + if (progressFn && file.bytes) { |
| 33 | + progressFn(file.bytes) |
49 | 34 | } else {
|
50 | 35 | yield toCoreInterface(file)
|
51 | 36 | }
|
|
0 commit comments