1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
- var expect = require ( 'chai' ) . expect
5
- var bs58 = require ( 'bs58' )
6
- var Readable = require ( 'readable-stream' )
7
- var path = require ( 'path' )
8
- var isNode = require ( 'detect-node' )
9
- var fs = require ( 'fs' )
10
- var bl = require ( 'bl' )
11
-
12
- module . exports = function ( common ) {
13
- describe ( '.files' , function ( ) {
14
- var smallFile = void 0
15
- var bigFile = void 0
16
- var ipfs = void 0
17
-
18
- before ( function ( done ) {
19
- smallFile = fs . readFileSync ( path . join ( __dirname , './data/testfile.txt' ) )
20
- bigFile = fs . readFileSync ( path . join ( __dirname , './data/15mb.random' ) )
21
-
22
- common . setup ( function ( err , _ipfs ) {
4
+ const expect = require ( 'chai' ) . expect
5
+ const bs58 = require ( 'bs58' )
6
+ const Readable = require ( 'readable-stream' )
7
+ const path = require ( 'path' )
8
+ const fs = require ( 'fs' )
9
+ const isNode = require ( 'detect-node' )
10
+ const bl = require ( 'bl' )
11
+
12
+ module . exports = ( common ) => {
13
+ describe ( '.files' , ( ) => {
14
+ let smallFile
15
+ let bigFile
16
+ let ipfs
17
+
18
+ before ( ( done ) => {
19
+ smallFile = fs . readFileSync ( path . join ( __dirname , './data/testfile.txt' )
20
+ )
21
+ bigFile = fs . readFileSync ( path . join ( __dirname , './data/15mb.random' )
22
+ )
23
+
24
+ common . setup ( ( err , _ipfs ) => {
23
25
expect ( err ) . to . not . exist
24
26
ipfs = _ipfs
25
27
done ( )
26
28
} )
27
29
} )
28
30
29
- after ( function ( done ) {
31
+ after ( ( done ) => {
30
32
common . teardown ( done )
31
33
} )
32
34
33
- describe ( '.add' , function ( ) {
34
- it ( 'stream' , function ( done ) {
35
- var buffered = new Buffer ( 'some data' )
36
- var rs = new Readable ( )
35
+ describe ( '.add' , ( ) => {
36
+ it ( 'stream' , ( done ) => {
37
+ const buffered = new Buffer ( 'some data' )
38
+ const rs = new Readable ( )
37
39
rs . push ( buffered )
38
40
rs . push ( null )
39
41
40
- var arr = [ ]
41
- var filePair = { path : 'data.txt' , content : rs }
42
+ const arr = [ ]
43
+ const filePair = { path : 'data.txt' , content : rs }
42
44
arr . push ( filePair )
43
45
44
- ipfs . files . add ( arr , function ( err , res ) {
46
+ ipfs . files . add ( arr , ( err , res ) => {
45
47
expect ( err ) . to . not . exist
46
48
expect ( res ) . to . be . length ( 1 )
47
49
expect ( res [ 0 ] . path ) . to . equal ( 'data.txt' )
48
50
expect ( res [ 0 ] . node . size ( ) ) . to . equal ( 17 )
49
- var mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
51
+ const mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
50
52
expect ( bs58 . encode ( res [ 0 ] . node . multihash ( ) ) . toString ( ) ) . to . equal ( mh )
51
53
done ( )
52
54
} )
53
55
} )
54
56
55
- it ( 'buffer as tuple' , function ( done ) {
56
- if ( ! isNode ) return done ( )
57
-
58
- var file = {
57
+ it ( 'buffer as tuple' , ( done ) => {
58
+ const file = {
59
59
path : 'testfile.txt' ,
60
60
content : smallFile
61
61
}
62
62
63
- ipfs . files . add ( [ file ] , function ( err , res ) {
63
+ ipfs . files . add ( [ file ] , ( err , res ) => {
64
64
expect ( err ) . to . not . exist
65
65
66
- var added = res [ 0 ] != null ? res [ 0 ] : res
67
- var mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
66
+ const added = res [ 0 ] != null ? res [ 0 ] : res
67
+ const mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
68
68
expect ( mh ) . to . equal ( 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' )
69
69
expect ( added . path ) . to . equal ( 'testfile.txt' )
70
70
expect ( added . node . links ) . to . have . length ( 0 )
71
71
done ( )
72
72
} )
73
73
} )
74
74
75
- it ( 'buffer' , function ( done ) {
76
- ipfs . files . add ( smallFile , function ( err , res ) {
75
+ it ( 'buffer' , ( done ) => {
76
+ ipfs . files . add ( smallFile , ( err , res ) => {
77
77
expect ( err ) . to . not . exist
78
78
79
79
expect ( res ) . to . have . length ( 1 )
80
- var mh = bs58 . encode ( res [ 0 ] . node . multihash ( ) ) . toString ( )
80
+ const mh = bs58 . encode ( res [ 0 ] . node . multihash ( ) ) . toString ( )
81
81
expect ( mh ) . to . equal ( 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' )
82
82
expect ( res [ 0 ] . path ) . to . equal ( mh )
83
83
expect ( res [ 0 ] . node . links ) . to . have . length ( 0 )
84
84
done ( )
85
85
} )
86
86
} )
87
87
88
- it ( 'BIG buffer' , function ( done ) {
89
- ipfs . files . add ( bigFile , function ( err , res ) {
88
+ it ( 'BIG buffer' , ( done ) => {
89
+ ipfs . files . add ( bigFile , ( err , res ) => {
90
90
expect ( err ) . to . not . exist
91
91
92
92
expect ( res ) . to . have . length ( 1 )
93
93
expect ( res [ 0 ] . node . links ) . to . have . length ( 58 )
94
- var mh = bs58 . encode ( res [ 0 ] . node . multihash ( ) ) . toString ( )
94
+ const mh = bs58 . encode ( res [ 0 ] . node . multihash ( ) ) . toString ( )
95
95
expect ( res [ 0 ] . path ) . to . equal ( mh )
96
96
expect ( mh ) . to . equal ( 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq' )
97
97
done ( )
98
98
} )
99
99
} )
100
100
101
- it ( 'add a nested dir as array' , function ( done ) {
101
+ it ( 'add a nested dir as array' , ( done ) => {
102
102
if ( ! isNode ) {
103
103
return done ( )
104
+ // can't run this test cause browserify
105
+ // can't shim readFileSync in runtime
104
106
}
105
- var base = path . join ( __dirname , 'data/test-folder' )
106
- var content = function content ( name ) {
107
- return {
108
- path : 'test-folder/' + name ,
109
- content : fs . readFileSync ( path . join ( base , name ) )
110
- }
111
- }
112
- var emptyDir = function emptyDir ( name ) {
113
- return {
114
- path : 'test-folder/' + name ,
115
- dir : true
116
- }
117
- }
118
- var dirs = [ content ( 'pp.txt' ) , content ( 'holmes.txt' ) , content ( 'jungle.txt' ) , content ( 'alice.txt' ) , emptyDir ( 'empty-folder' ) , content ( 'files/hello.txt' ) , content ( 'files/ipfs.txt' ) , emptyDir ( 'files/empty' ) ]
107
+ const base = path . join ( __dirname , 'data/test-folder' )
108
+ const content = ( name ) => ( {
109
+ path : `test-folder/${ name } ` ,
110
+ content : fs . readFileSync ( path . join ( base , name ) )
111
+ } )
112
+ const emptyDir = ( name ) => ( {
113
+ path : `test-folder/${ name } `
114
+ } )
115
+ const dirs = [
116
+ content ( 'pp.txt' ) ,
117
+ content ( 'holmes.txt' ) ,
118
+ content ( 'jungle.txt' ) ,
119
+ content ( 'alice.txt' ) ,
120
+ emptyDir ( 'empty-folder' ) ,
121
+ content ( 'files/hello.txt' ) ,
122
+ content ( 'files/ipfs.txt' ) ,
123
+ emptyDir ( 'files/empty' )
124
+ ]
119
125
120
- ipfs . files . add ( dirs , function ( err , res ) {
126
+ ipfs . files . add ( dirs , ( err , res ) => {
121
127
expect ( err ) . to . not . exist
122
128
123
- var added = res [ res . length - 1 ]
124
- var mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
129
+ const added = res [ res . length - 1 ]
130
+ const mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
125
131
expect ( mh ) . to . equal ( 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP' )
126
132
expect ( added . path ) . to . equal ( 'test-folder' )
127
133
expect ( added . node . links ) . to . have . length ( 6 )
128
134
done ( )
129
135
} )
130
136
} )
131
137
132
- describe ( 'promise' , function ( ) {
133
- it ( 'buffer' , function ( ) {
134
- return ipfs . files . add ( smallFile ) . then ( function ( res ) {
135
- var added = res [ 0 ] != null ? res [ 0 ] : res
136
- var mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
137
- expect ( mh ) . to . equal ( 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' )
138
- expect ( added . path ) . to . equal ( mh )
139
- expect ( added . node . links ) . to . have . length ( 0 )
140
- } ) . catch ( function ( err ) {
141
- expect ( err ) . to . not . exist
142
- } )
138
+ describe ( 'promise' , ( ) => {
139
+ it ( 'buffer' , ( ) => {
140
+ return ipfs . files . add ( smallFile )
141
+ . then ( ( res ) => {
142
+ const added = res [ 0 ] != null ? res [ 0 ] : res
143
+ const mh = bs58 . encode ( added . node . multihash ( ) ) . toString ( )
144
+ expect ( mh ) . to . equal ( 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' )
145
+ expect ( added . path ) . to . equal ( mh )
146
+ expect ( added . node . links ) . to . have . length ( 0 )
147
+ } )
148
+ . catch ( ( err ) => {
149
+ expect ( err ) . to . not . exist
150
+ } )
143
151
} )
144
152
} )
145
153
} )
146
154
147
- describe ( '.cat' , function ( ) {
148
- it ( 'returns file stream' , function ( done ) {
149
- var hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
150
- ipfs . cat ( hash , function ( err , file ) {
155
+ describe ( '.cat' , ( ) => {
156
+ it ( 'returns file stream' , ( done ) => {
157
+ const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
158
+ ipfs . cat ( hash , ( err , file ) => {
151
159
expect ( err ) . to . not . exist
152
- file . pipe ( bl ( function ( err , bldata ) {
160
+ file . pipe ( bl ( ( err , bldata ) => {
153
161
expect ( err ) . to . not . exist
154
162
expect ( bldata . toString ( ) ) . to . contain ( 'Check out some of the other files in this directory:' )
155
163
done ( )
@@ -158,11 +166,11 @@ module.exports = function (common) {
158
166
} )
159
167
160
168
// This fails on js-ipfs-api
161
- it ( 'takes a buffer input' , function ( done ) {
162
- var mhBuf = new Buffer ( bs58 . decode ( 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' ) )
163
- ipfs . cat ( mhBuf , function ( err , file ) {
169
+ it ( 'takes a buffer input' , ( done ) => {
170
+ const mhBuf = new Buffer ( bs58 . decode ( 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' ) )
171
+ ipfs . cat ( mhBuf , ( err , file ) => {
164
172
expect ( err ) . to . not . exist
165
- file . pipe ( bl ( function ( err , bldata ) {
173
+ file . pipe ( bl ( ( err , bldata ) => {
166
174
expect ( err ) . to . not . exist
167
175
expect ( bldata . toString ( ) ) . to . contain ( 'Check out some of the other files in this directory:' )
168
176
done ( )
@@ -171,23 +179,23 @@ module.exports = function (common) {
171
179
} )
172
180
173
181
// You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs
174
- it ( 'returns a large file' , function ( done ) {
175
- var hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
176
- ipfs . cat ( hash , function ( err , file ) {
182
+ it ( 'returns a large file' , ( done ) => {
183
+ const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
184
+ ipfs . cat ( hash , ( err , file ) => {
177
185
expect ( err ) . to . not . exist
178
- file . pipe ( bl ( function ( err , bldata ) {
186
+ file . pipe ( bl ( ( err , bldata ) => {
179
187
expect ( err ) . to . not . exist
180
188
expect ( bldata ) . to . deep . equal ( bigFile )
181
189
done ( )
182
190
} ) )
183
191
} )
184
192
} )
185
193
186
- it ( 'returns error on invalid key' , function ( done ) {
187
- var hash = 'somethingNotMultihash'
188
- ipfs . cat ( hash , function ( err , file ) {
194
+ it ( 'returns error on invalid key' , ( done ) => {
195
+ const hash = 'somethingNotMultihash'
196
+ ipfs . cat ( hash , ( err , file ) => {
189
197
expect ( err ) . to . exist
190
- var errString = err . toString ( )
198
+ const errString = err . toString ( )
191
199
if ( errString === 'Error: invalid ipfs ref path' ) {
192
200
expect ( err . toString ( ) ) . to . contain ( 'Error: invalid ipfs ref path' )
193
201
}
@@ -198,25 +206,29 @@ module.exports = function (common) {
198
206
} )
199
207
} )
200
208
201
- describe ( 'promise' , function ( ) {
202
- it ( 'files.cat' , function ( done ) {
203
- var hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
204
- ipfs . cat ( hash ) . then ( function ( stream ) {
205
- stream . pipe ( bl ( function ( err , bldata ) {
209
+ describe ( 'promise' , ( ) => {
210
+ it ( 'files.cat' , ( done ) => {
211
+ const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
212
+ ipfs . cat ( hash )
213
+ . then ( ( stream ) => {
214
+ stream . pipe ( bl ( ( err , bldata ) => {
206
215
expect ( err ) . to . not . exist
207
216
expect ( bldata . toString ( ) ) . to . contain ( 'Check out some of the other files in this directory:' )
208
217
done ( )
209
218
} ) )
210
- } ) . catch ( function ( err ) {
219
+ } )
220
+ . catch ( ( err ) => {
211
221
expect ( err ) . to . not . exist
212
222
} )
213
223
} )
214
224
215
- it ( 'returns error on invalid key' , function ( done ) {
216
- var hash = 'somethingNotMultihash'
217
- ipfs . cat ( hash ) . then ( function ( stream ) { } ) . catch ( function ( err ) {
225
+ it ( 'returns error on invalid key' , ( done ) => {
226
+ const hash = 'somethingNotMultihash'
227
+ ipfs . cat ( hash )
228
+ . then ( ( stream ) => { } )
229
+ . catch ( ( err ) => {
218
230
expect ( err ) . to . exist
219
- var errString = err . toString ( )
231
+ const errString = err . toString ( )
220
232
if ( errString === 'Error: invalid ipfs ref path' ) {
221
233
expect ( err . toString ( ) ) . to . contain ( 'Error: invalid ipfs ref path' )
222
234
}
@@ -227,15 +239,17 @@ module.exports = function (common) {
227
239
} )
228
240
} )
229
241
230
- it ( 'takes a buffer input' , function ( done ) {
231
- var hash = new Buffer ( bs58 . decode ( 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' ) )
232
- ipfs . cat ( hash ) . then ( function ( stream ) {
233
- stream . pipe ( bl ( function ( err , bldata ) {
242
+ it ( 'takes a buffer input' , ( done ) => {
243
+ const hash = new Buffer ( bs58 . decode ( 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' ) )
244
+ ipfs . cat ( hash )
245
+ . then ( ( stream ) => {
246
+ stream . pipe ( bl ( ( err , bldata ) => {
234
247
expect ( err ) . to . not . exist
235
248
expect ( bldata . toString ( ) ) . to . contain ( 'Check out some of the other files in this directory:' )
236
249
done ( )
237
250
} ) )
238
- } ) . catch ( function ( err ) {
251
+ } )
252
+ . catch ( ( err ) => {
239
253
expect ( err ) . to . not . exist
240
254
} )
241
255
} )
0 commit comments