|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const promisify = require('promisify-es6') |
4 |
| -const concatStream = require('concat-stream') |
5 |
| -const once = require('once') |
6 |
| -const SendFilesStream = require('../utils/send-files-stream') |
7 |
| - |
8 |
| -module.exports = (send) => { |
9 |
| - const sendFilesStream = SendFilesStream(send, 'files/write') |
10 |
| - |
11 |
| - return promisify((pathDst, _files, opts, _callback) => { |
12 |
| - if (typeof opts === 'function' && |
13 |
| - !_callback) { |
14 |
| - _callback = opts |
15 |
| - opts = {} |
16 |
| - } |
17 |
| - |
18 |
| - // opts is the real callback -- |
19 |
| - // 'callback' is being injected by promisify |
20 |
| - if (typeof opts === 'function' && |
21 |
| - typeof _callback === 'function') { |
22 |
| - _callback = opts |
23 |
| - opts = {} |
24 |
| - } |
25 |
| - |
26 |
| - const files = [].concat(_files) |
27 |
| - const callback = once(_callback) |
28 |
| - |
29 |
| - const options = { |
30 |
| - args: pathDst, |
31 |
| - qs: opts |
32 |
| - } |
33 |
| - |
34 |
| - const stream = sendFilesStream({ qs: options }) |
35 |
| - const concat = concatStream((result) => callback(null, result)) |
36 |
| - stream.once('error', callback) |
37 |
| - stream.pipe(concat) |
38 |
| - |
39 |
| - files.forEach((file) => stream.write(file)) |
40 |
| - stream.end() |
41 |
| - }) |
42 |
| -} |
| 3 | +const configure = require('../lib/configure') |
| 4 | +const toFormData = require('../lib/buffer-to-form-data') |
| 5 | + |
| 6 | +module.exports = configure(({ ky }) => { |
| 7 | + return async (path, input, options) => { |
| 8 | + options = options || {} |
| 9 | + |
| 10 | + const searchParams = new URLSearchParams(options.searchParams) |
| 11 | + searchParams.set('arg', path) |
| 12 | + searchParams.set('stream-channels', true) |
| 13 | + if (options.cidVersion) searchParams.set('cid-version', options.cidVersion) |
| 14 | + if (options.create != null) searchParams.set('create', options.create) |
| 15 | + if (options.hashAlg) searchParams.set('hash', options.hashAlg) |
| 16 | + if (options.length != null) searchParams.set('length', options.length) |
| 17 | + if (options.offset != null) searchParams.set('offset', options.offset) |
| 18 | + if (options.parents != null) searchParams.set('parents', options.parents) |
| 19 | + if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves) |
| 20 | + if (options.truncate != null) searchParams.set('truncate', options.truncate) |
| 21 | + |
| 22 | + const res = await ky.post('files/write', { |
| 23 | + timeout: options.timeout, |
| 24 | + signal: options.signal, |
| 25 | + headers: options.headers, |
| 26 | + searchParams, |
| 27 | + body: toFormData(input) // TODO: support inputs other than buffer as per spec |
| 28 | + }) |
| 29 | + |
| 30 | + return res.text() |
| 31 | + } |
| 32 | +}) |
0 commit comments