This repository was archived by the owner on Aug 12, 2020. It is now read-only.
File tree 3 files changed +42
-0
lines changed
3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 56
56
"debug" : " ^2.2.0" ,
57
57
"ipfs-merkle-dag" : " ^0.5.0" ,
58
58
"ipfs-unixfs" : " ^0.1.0" ,
59
+ "isstream" : " ^0.1.2" ,
59
60
"readable-stream" : " ^1.1.13" ,
60
61
"run-series" : " ^1.1.4" ,
62
+ "streamifier" : " ^0.1.1" ,
61
63
"through2" : " ^2.0.0"
62
64
},
63
65
"contributors" : [
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ const UnixFS = require('ipfs-unixfs')
10
10
const util = require ( 'util' )
11
11
const bs58 = require ( 'bs58' )
12
12
const Duplex = require ( 'readable-stream' ) . Duplex
13
+ const isStream = require ( 'isstream' )
14
+ const streamifier = require ( 'streamifier' )
13
15
14
16
exports = module . exports = Importer
15
17
@@ -63,6 +65,18 @@ function Importer (dagService, options) {
63
65
return
64
66
}
65
67
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
+
66
80
const leaves = [ ]
67
81
fl . content
68
82
. pipe ( fsc ( CHUNK_SIZE ) )
Original file line number Diff line number Diff line change @@ -29,6 +29,17 @@ module.exports = function (repo) {
29
29
done ( )
30
30
} )
31
31
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
+
32
43
it ( 'small file (smaller than a chunk)' , ( done ) => {
33
44
const buffered = smallFile
34
45
const r = streamifier . createReadStream ( buffered )
@@ -45,6 +56,21 @@ module.exports = function (repo) {
45
56
} )
46
57
} )
47
58
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
+
48
74
it ( 'small file (smaller than a chunk) inside a dir' , ( done ) => {
49
75
const buffered = smallFile
50
76
const r = streamifier . createReadStream ( buffered )
You can’t perform that action at this time.
0 commit comments