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