This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathadd.js
69 lines (56 loc) · 2.31 KB
/
add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'
const promisify = require('promisify-es6')
const ConcatStream = require('concat-stream')
const once = require('once')
const isStream = require('is-stream')
const isSource = require('is-pull-stream').isSource
const FileResultStreamConverter = require('../utils/file-result-stream-converter')
const SendFilesStream = require('../utils/send-files-stream')
module.exports = (send) => {
const createAddStream = SendFilesStream(send, 'add')
const add = promisify((_files, options, _callback) => {
if (typeof options === 'function') {
_callback = options
options = null
}
const callback = once(_callback)
if (!options) {
options = {}
}
options.converter = FileResultStreamConverter
// Buffer, pull stream or Node.js stream
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
// An object like { content?, path? }, where content isBufferOrStream and path isString
const isContentObject = obj => {
if (typeof obj !== 'object') return false
// path is optional if content is present
if (obj.content) return isBufferOrStream(obj.content)
// path must be a non-empty string if no content
return Boolean(obj.path) && typeof obj.path === 'string'
}
// An input atom: a buffer, stream or content object
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
// All is ok if data isInput or data is an array of isInput
const ok = isInput(_files) || (Array.isArray(_files) && _files.every(isInput))
if (!ok) {
return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects'))
}
const files = [].concat(_files)
const stream = createAddStream({ qs: options })
const concat = ConcatStream((result) => callback(null, result))
stream.once('error', callback)
stream.pipe(concat)
files.forEach((file) => stream.write(file))
stream.end()
})
return function () {
const args = Array.from(arguments)
// If we files.add(<pull stream>), then promisify thinks the pull stream is
// a callback! Add an empty options object in this case so that a promise
// is returned.
if (args.length === 1 && isSource(args[0])) {
args.push({})
}
return add.apply(null, args)
}
}