Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 2fa16c5

Browse files
alanshawdaviddias
authored andcommitted
fix: adding files by pull stream
1 parent 7c5cea5 commit 2fa16c5

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"ipld-dag-cbor": "^0.12.0",
3939
"ipld-dag-pb": "^0.14.2",
4040
"is-ipfs": "^0.3.2",
41+
"is-pull-stream": "0.0.0",
4142
"is-stream": "^1.1.0",
4243
"libp2p-crypto": "^0.13.0",
4344
"lru-cache": "^4.1.2",
@@ -51,6 +52,7 @@
5152
"promisify-es6": "^1.0.3",
5253
"pull-defer": "^0.2.2",
5354
"pull-pushable": "^2.2.0",
55+
"pull-stream-to-stream": "^1.3.4",
5456
"pump": "^3.0.0",
5557
"qs": "^6.5.1",
5658
"readable-stream": "^2.3.6",

Diff for: src/files/add.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const ConcatStream = require('concat-stream')
55
const once = require('once')
66
const isStream = require('is-stream')
77
const OtherBuffer = require('buffer').Buffer
8+
const isSource = require('is-pull-stream').isSource
89
const FileResultStreamConverter = require('../utils/file-result-stream-converter')
910
const SendFilesStream = require('../utils/send-files-stream')
1011

1112
module.exports = (send) => {
1213
const createAddStream = SendFilesStream(send, 'add')
1314

14-
return promisify((_files, options, _callback) => {
15+
const add = promisify((_files, options, _callback) => {
1516
if (typeof options === 'function') {
1617
_callback = options
1718
options = null
@@ -28,10 +29,11 @@ module.exports = (send) => {
2829
isStream.readable(_files) ||
2930
Array.isArray(_files) ||
3031
OtherBuffer.isBuffer(_files) ||
31-
typeof _files === 'object'
32+
typeof _files === 'object' ||
33+
isSource(_files)
3234

3335
if (!ok) {
34-
return callback(new Error('first arg must be a buffer, readable stream, an object or array of objects'))
36+
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
3537
}
3638

3739
const files = [].concat(_files)
@@ -44,4 +46,17 @@ module.exports = (send) => {
4446
files.forEach((file) => stream.write(file))
4547
stream.end()
4648
})
49+
50+
return function () {
51+
const args = Array.from(arguments)
52+
53+
// If we files.add(<pull stream>), then promisify thinks the pull stream is
54+
// a callback! Add an empty options object in this case so that a promise
55+
// is returned.
56+
if (args.length === 1 && isSource(args[0])) {
57+
args.push({})
58+
}
59+
60+
return add.apply(null, args)
61+
}
4762
}

Diff for: src/utils/multipart.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const Transform = require('stream').Transform
44
const isNode = require('detect-node')
5+
const isSource = require('is-pull-stream').isSource
6+
const toStream = require('pull-stream-to-stream')
57

68
const PADDING = '--'
79
const NEW_LINE = '\r\n'
@@ -75,6 +77,10 @@ class Multipart extends Transform {
7577
return callback() // early
7678
}
7779

80+
if (isSource(content)) {
81+
content = toStream.source(content)
82+
}
83+
7884
// From now on we assume content is a stream
7985

8086
content.once('error', this.emit.bind(this, 'error'))

Diff for: test/files.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const isNode = require('detect-node')
1010
const loadFixture = require('aegir/fixtures')
1111
const mh = require('multihashes')
1212
const CID = require('cids')
13+
const pull = require('pull-stream')
1314

1415
const IPFSApi = require('../src')
1516
const f = require('./utils/factory')
@@ -272,6 +273,52 @@ describe('.files (the MFS API part)', function () {
272273
})
273274
})
274275

276+
it('files.addPullStream with object chunks and pull stream content', (done) => {
277+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
278+
279+
pull(
280+
pull.values([{ content: pull.values([Buffer.from('test')]) }]),
281+
ipfs.files.addPullStream(),
282+
pull.collect((err, res) => {
283+
if (err) return done(err)
284+
expect(res).to.have.length(1)
285+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
286+
done()
287+
})
288+
)
289+
})
290+
291+
it('files.add with pull stream (callback)', (done) => {
292+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
293+
294+
ipfs.files.add(pull.values([Buffer.from('test')]), (err, res) => {
295+
if (err) return done(err)
296+
expect(res).to.have.length(1)
297+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
298+
done()
299+
})
300+
})
301+
302+
it('files.add with pull stream (promise)', () => {
303+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
304+
305+
return ipfs.files.add(pull.values([Buffer.from('test')]))
306+
.then((res) => {
307+
expect(res).to.have.length(1)
308+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
309+
})
310+
})
311+
312+
it('files.add with array of objects with pull stream content', () => {
313+
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
314+
315+
return ipfs.files.add([{ content: pull.values([Buffer.from('test')]) }])
316+
.then((res) => {
317+
expect(res).to.have.length(1)
318+
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
319+
})
320+
})
321+
275322
it('files.mkdir', (done) => {
276323
ipfs.files.mkdir('/test-folder', done)
277324
})

0 commit comments

Comments
 (0)