Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0e601a7

Browse files
alanshawdaviddias
authored andcommitted
fix: files.add with pull streams
1 parent 3019fc0 commit 0e601a7

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"ipfs-unixfs-engine": "~0.29.0",
117117
"ipld": "^0.17.0",
118118
"is-ipfs": "^0.3.2",
119+
"is-pull-stream": "0.0.0",
119120
"is-stream": "^1.1.0",
120121
"joi": "^13.2.0",
121122
"joi-browser": "^13.0.1",

src/core/components/files.js

+51-30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const toPull = require('stream-to-pull-stream')
1212
const deferred = require('pull-defer')
1313
const waterfall = require('async/waterfall')
1414
const isStream = require('is-stream')
15+
const isSource = require('is-pull-stream').isSource
1516
const Duplex = require('readable-stream').Duplex
1617
const OtherBuffer = require('buffer').Buffer
1718
const CID = require('cids')
@@ -60,6 +61,10 @@ function normalizeContent (opts, content) {
6061
data = { path: '', content: toPull.source(data) }
6162
}
6263

64+
if (isSource(data)) {
65+
data = { path: '', content: data }
66+
}
67+
6368
if (data && data.content && typeof data.content !== 'function') {
6469
if (Buffer.isBuffer(data.content)) {
6570
data.content = pull.values([data.content])
@@ -196,40 +201,56 @@ module.exports = function files (self) {
196201
}
197202

198203
return {
199-
add: promisify((data, options = {}, callback) => {
200-
if (typeof options === 'function') {
201-
callback = options
202-
options = {}
203-
} else if (!callback || typeof callback !== 'function') {
204-
callback = noop
205-
}
204+
add: (() => {
205+
const add = promisify((data, options = {}, callback) => {
206+
if (typeof options === 'function') {
207+
callback = options
208+
options = {}
209+
} else if (!callback || typeof callback !== 'function') {
210+
callback = noop
211+
}
206212

207-
const ok = Buffer.isBuffer(data) ||
208-
isStream.readable(data) ||
209-
Array.isArray(data) ||
210-
OtherBuffer.isBuffer(data) ||
211-
typeof data === 'object'
213+
const ok = Buffer.isBuffer(data) ||
214+
isStream.readable(data) ||
215+
Array.isArray(data) ||
216+
OtherBuffer.isBuffer(data) ||
217+
typeof data === 'object' ||
218+
isSource(data)
212219

213-
if (!ok) {
214-
return callback(new Error('first arg must be a buffer, readable stream, an object or array of objects'))
215-
}
220+
if (!ok) {
221+
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
222+
}
216223

217-
// CID v0 is for multihashes encoded with sha2-256
218-
if (options.hashAlg && options.cidVersion !== 1) {
219-
options.cidVersion = 1
220-
}
224+
// CID v0 is for multihashes encoded with sha2-256
225+
if (options.hashAlg && options.cidVersion !== 1) {
226+
options.cidVersion = 1
227+
}
221228

222-
pull(
223-
pull.values([data]),
224-
_addPullStream(options),
225-
sort((a, b) => {
226-
if (a.path < b.path) return 1
227-
if (a.path > b.path) return -1
228-
return 0
229-
}),
230-
pull.collect(callback)
231-
)
232-
}),
229+
pull(
230+
pull.values([data]),
231+
_addPullStream(options),
232+
sort((a, b) => {
233+
if (a.path < b.path) return 1
234+
if (a.path > b.path) return -1
235+
return 0
236+
}),
237+
pull.collect(callback)
238+
)
239+
})
240+
241+
return function () {
242+
const args = Array.from(arguments)
243+
244+
// If we files.add(<pull stream>), then promisify thinks the pull stream
245+
// is a callback! Add an empty options object in this case so that a
246+
// promise is returned.
247+
if (args.length === 1 && isSource(args[0])) {
248+
args.push({})
249+
}
250+
251+
return add.apply(null, args)
252+
}
253+
})(),
233254

234255
addReadableStream: (options) => {
235256
options = options || {}

0 commit comments

Comments
 (0)