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

Commit b177a08

Browse files
committed
Support content as a Buffer.
1 parent b5acb24 commit b177a08

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@
5656
"debug": "^2.2.0",
5757
"ipfs-merkle-dag": "^0.5.0",
5858
"ipfs-unixfs": "^0.1.0",
59+
"isstream": "^0.1.2",
5960
"readable-stream": "^1.1.13",
6061
"run-series": "^1.1.4",
62+
"streamifier": "^0.1.1",
6163
"through2": "^2.0.0"
6264
},
6365
"contributors": [

src/importer.js

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const UnixFS = require('ipfs-unixfs')
1010
const util = require('util')
1111
const bs58 = require('bs58')
1212
const Duplex = require('readable-stream').Duplex
13+
const isStream = require('isstream')
14+
const streamifier = require('streamifier')
1315

1416
exports = module.exports = Importer
1517

@@ -63,6 +65,18 @@ function Importer (dagService, options) {
6365
return
6466
}
6567

68+
// Convert a buffer to a readable stream
69+
if (Buffer.isBuffer(fl.content)) {
70+
const r = streamifier.createReadStream(fl.content)
71+
fl.content = r
72+
}
73+
74+
// Bail if 'content' is not readable
75+
if (!isStream.isReadable(fl.content)) {
76+
this.emit('error', new Error('"content" is not a Buffer nor Readable stream'))
77+
return
78+
}
79+
6680
const leaves = []
6781
fl.content
6882
.pipe(fsc(CHUNK_SIZE))

test/test-importer.js

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ module.exports = function (repo) {
2929
done()
3030
})
3131

32+
it('bad input', (done) => {
33+
const r = 'banana'
34+
const i = new Importer(ds)
35+
i.on('error', (err) => {
36+
expect(err).to.exist
37+
done()
38+
})
39+
i.write({path: '200Bytes.txt', content: r})
40+
i.end()
41+
})
42+
3243
it('small file (smaller than a chunk)', (done) => {
3344
const buffered = smallFile
3445
const r = streamifier.createReadStream(buffered)
@@ -45,6 +56,21 @@ module.exports = function (repo) {
4556
})
4657
})
4758

59+
it('small file as buffer (smaller than a chunk)', (done) => {
60+
const buffered = smallFile
61+
const i = new Importer(ds)
62+
i.on('data', (obj) => {
63+
expect(obj.path).to.equal('200Bytes.txt')
64+
expect(bs58.encode(obj.multihash)).to.equal('QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8')
65+
expect(obj.size).to.equal(211)
66+
})
67+
i.write({path: '200Bytes.txt', content: buffered})
68+
i.end()
69+
i.on('end', () => {
70+
done()
71+
})
72+
})
73+
4874
it('small file (smaller than a chunk) inside a dir', (done) => {
4975
const buffered = smallFile
5076
const r = streamifier.createReadStream(buffered)

0 commit comments

Comments
 (0)