This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathadd-all.js
402 lines (322 loc) · 12.7 KB
/
add-all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* eslint-env mocha, browser */
'use strict'
const { fixtures } = require('./utils')
const { Readable } = require('readable-stream')
const all = require('it-all')
const last = require('it-last')
const drain = require('it-drain')
const fs = require('fs')
const os = require('os')
const path = require('path')
const { supportsFileReader } = require('ipfs-utils/src/supports')
const globSource = require('ipfs-utils/src/files/glob-source')
const { isNode } = require('ipfs-utils/src/env')
const { getDescribe, getIt, expect } = require('./utils/mocha')
const testTimeout = require('./utils/test-timeout')
const uint8ArrayFromString = require('uint8arrays/from-string')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.addAll', function () {
this.timeout(120 * 1000)
let ipfs
async function testMode (mode, expectedMode) {
const content = String(Math.random() + Date.now())
const files = await all(ipfs.addAll([{
content: uint8ArrayFromString(content),
mode
}]))
expect(files).to.have.length(1)
expect(files).to.have.nested.property('[0].mode', expectedMode)
const stats = await ipfs.files.stat(`/ipfs/${files[0].cid}`)
expect(stats).to.have.property('mode', expectedMode)
}
async function testMtime (mtime, expectedMtime) {
const content = String(Math.random() + Date.now())
const files = await all(ipfs.addAll({
content: uint8ArrayFromString(content),
mtime
}))
expect(files).to.have.length(1)
expect(files).to.have.deep.nested.property('[0].mtime', expectedMtime)
const stats = await ipfs.files.stat(`/ipfs/${files[0].cid}`)
expect(stats).to.have.deep.property('mtime', expectedMtime)
}
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
it('should respect timeout option when adding files', () => {
return testTimeout(() => drain(ipfs.addAll(uint8ArrayFromString('Hello'), {
timeout: 1
})))
})
it('should add a File as array of tuples', async function () {
if (!supportsFileReader) return this.skip('skip in node')
const tuple = {
path: 'filename.txt',
content: new self.File(['should add a File'], 'filename.txt', { type: 'text/plain' })
}
const filesAdded = await all(ipfs.addAll([tuple]))
expect(filesAdded[0].cid.toString()).to.be.eq('QmTVfLxf3qXiJgr4KwG6UBckcNvTqBp93Rwy5f7h3mHsVC')
})
it('should add a Uint8Array as array of tuples', async () => {
const tuple = { path: 'testfile.txt', content: fixtures.smallFile.data }
const filesAdded = await all(ipfs.addAll([tuple]))
expect(filesAdded).to.have.length(1)
const file = filesAdded[0]
expect(file.cid.toString()).to.equal(fixtures.smallFile.cid)
expect(file.path).to.equal('testfile.txt')
})
it('should add array of objects with readable stream content', async function () {
if (!isNode) this.skip()
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
const rs = new Readable()
rs.push(uint8ArrayFromString('some data'))
rs.push(null)
const tuple = { path: 'data.txt', content: rs }
const filesAdded = await all(ipfs.addAll([tuple]))
expect(filesAdded).to.be.length(1)
const file = filesAdded[0]
expect(file.path).to.equal('data.txt')
expect(file.size).to.equal(17)
expect(file.cid.toString()).to.equal(expectedCid)
})
it('should add a nested directory as array of tupples', async function () {
const content = (name) => ({
path: `test-folder/${name}`,
content: fixtures.directory.files[name]
})
const emptyDir = (name) => ({ path: `test-folder/${name}` })
const 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')
]
const root = await last(ipfs.addAll(dirs))
expect(root.path).to.equal('test-folder')
expect(root.cid.toString()).to.equal(fixtures.directory.cid)
})
it('should add a nested directory as array of tupples with progress', async function () {
const content = (name) => ({
path: `test-folder/${name}`,
content: fixtures.directory.files[name]
})
const emptyDir = (name) => ({ path: `test-folder/${name}` })
const progressSizes = {}
const 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')
]
const total = dirs.reduce((acc, curr) => {
if (curr.content) {
acc[curr.path] = curr.content.length
}
return acc
}, {})
const handler = (bytes, path) => {
progressSizes[path] = bytes
}
const root = await last(ipfs.addAll(dirs, { progress: handler }))
expect(progressSizes).to.deep.equal(total)
expect(root.path).to.equal('test-folder')
expect(root.cid.toString()).to.equal(fixtures.directory.cid)
})
it('should receive file name from progress event', async () => {
const receivedNames = []
function handler (p, name) {
receivedNames.push(name)
}
await drain(ipfs.addAll([{
content: 'hello',
path: 'foo.txt'
}, {
content: 'world',
path: 'bar.txt'
}], {
progress: handler,
wrapWithDirectory: true
}))
expect(receivedNames).to.deep.equal(['foo.txt', 'bar.txt'])
})
it('should add files to a directory non sequentially', async function () {
const content = path => ({
path: `test-dir/${path}`,
content: fixtures.directory.files[path.split('/').pop()]
})
const input = [
content('a/pp.txt'),
content('a/holmes.txt'),
content('b/jungle.txt'),
content('a/alice.txt')
]
const filesAdded = await all(ipfs.addAll(input))
const toPath = ({ path }) => path
const nonSeqDirFilePaths = input.map(toPath).filter(p => p.includes('/a/'))
const filesAddedPaths = filesAdded.map(toPath)
expect(nonSeqDirFilePaths.every(p => filesAddedPaths.includes(p))).to.be.true()
})
it('should fail when passed invalid input', async () => {
const nonValid = 138
await expect(all(ipfs.addAll(nonValid))).to.eventually.be.rejected()
})
it('should wrap content in a directory', async () => {
const data = { path: 'testfile.txt', content: fixtures.smallFile.data }
const filesAdded = await all(ipfs.addAll(data, { wrapWithDirectory: true }))
expect(filesAdded).to.have.length(2)
const file = filesAdded[0]
const wrapped = filesAdded[1]
expect(file.cid.toString()).to.equal(fixtures.smallFile.cid)
expect(file.path).to.equal('testfile.txt')
expect(wrapped.path).to.equal('')
})
it('should add a directory with only-hash=true', async function () {
this.slow(10 * 1000)
const content = String(Math.random() + Date.now())
const files = await all(ipfs.addAll([{
path: '/foo/bar.txt',
content: uint8ArrayFromString(content)
}, {
path: '/foo/baz.txt',
content: uint8ArrayFromString(content)
}], { onlyHash: true }))
expect(files).to.have.length(3)
await Promise.all(
files.map(file => expect(ipfs.object.get(file.cid, { timeout: 4000 }))
.to.eventually.be.rejected()
.and.to.have.property('name').that.equals('TimeoutError')
)
)
})
it('should add with mode as string', async function () {
this.slow(10 * 1000)
const mode = '0777'
await testMode(mode, parseInt(mode, 8))
})
it('should add with mode as number', async function () {
this.slow(10 * 1000)
const mode = parseInt('0777', 8)
await testMode(mode, mode)
})
it('should add with mtime as Date', async function () {
this.slow(10 * 1000)
const mtime = new Date(5000)
await testMtime(mtime, {
secs: 5,
nsecs: 0
})
})
it('should add with mtime as { nsecs, secs }', async function () {
this.slow(10 * 1000)
const mtime = {
secs: 5,
nsecs: 0
}
await testMtime(mtime, mtime)
})
it('should add with mtime as timespec', async function () {
this.slow(10 * 1000)
await testMtime({
Seconds: 5,
FractionalNanoseconds: 0
}, {
secs: 5,
nsecs: 0
})
})
it('should add with mtime as hrtime', async function () {
this.slow(10 * 1000)
const mtime = process.hrtime()
await testMtime(mtime, {
secs: mtime[0],
nsecs: mtime[1]
})
})
it('should add a directory from the file system', async function () {
if (!isNode) this.skip()
const filesPath = path.join(__dirname, '..', 'test', 'fixtures', 'test-folder')
const result = await all(ipfs.addAll(globSource(filesPath, { recursive: true })))
expect(result.length).to.be.above(8)
})
it('should add a directory from the file system with an odd name', async function () {
if (!isNode) this.skip()
const filesPath = path.join(__dirname, '..', 'test', 'fixtures', 'weird name folder [v0]')
const result = await all(ipfs.addAll(globSource(filesPath, { recursive: true })))
expect(result.length).to.be.above(8)
})
it('should ignore a directory from the file system', async function () {
if (!isNode) this.skip()
const filesPath = path.join(__dirname, '..', 'test', 'fixtures', 'test-folder')
const result = await all(ipfs.addAll(globSource(filesPath, { recursive: true, ignore: ['files/**'] })))
expect(result.length).to.be.below(9)
})
it('should add a file from the file system', async function () {
if (!isNode) this.skip()
const filePath = path.join(__dirname, '..', 'test', 'fixtures', 'testfile.txt')
const result = await all(ipfs.addAll(globSource(filePath)))
expect(result.length).to.equal(1)
expect(result[0].path).to.equal('testfile.txt')
})
it('should add a hidden file in a directory from the file system', async function () {
if (!isNode) this.skip()
const filesPath = path.join(__dirname, '..', 'test', 'fixtures', 'hidden-files-folder')
const result = await all(ipfs.addAll(globSource(filesPath, { recursive: true, hidden: true })))
expect(result.length).to.be.above(10)
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
expect(result.map(object => object.cid.toString())).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
})
it('should add a file from the file system with only-hash=true', async function () {
if (!isNode) this.skip()
this.slow(10 * 1000)
const content = String(Math.random() + Date.now())
const filepath = path.join(os.tmpdir(), `${content}.txt`)
fs.writeFileSync(filepath, content)
const out = await all(ipfs.addAll(globSource(filepath), { onlyHash: true }))
fs.unlinkSync(filepath)
await expect(ipfs.object.get(out[0].cid, { timeout: 500 }))
.to.eventually.be.rejected()
.and.to.have.property('name').that.equals('TimeoutError')
})
it('should respect raw leaves when file is smaller than one block and no metadata is present', async () => {
const files = await all(ipfs.addAll(Uint8Array.from([0, 1, 2]), {
cidVersion: 1,
rawLeaves: true
}))
expect(files.length).to.equal(1)
expect(files[0].cid.toString()).to.equal('bafkreifojmzibzlof6xyh5auu3r5vpu5l67brf3fitaf73isdlglqw2t7q')
expect(files[0].cid.codec).to.equal('raw')
expect(files[0].size).to.equal(3)
})
it('should override raw leaves when file is smaller than one block and metadata is present', async () => {
const files = await all(ipfs.addAll({
content: Uint8Array.from([0, 1, 2]),
mode: 0o123,
mtime: {
secs: 1000,
nsecs: 0
}
}, {
cidVersion: 1,
rawLeaves: true
}))
expect(files.length).to.equal(1)
expect(files[0].cid.toString()).to.equal('bafybeifmayxiu375ftlgydntjtffy5cssptjvxqw6vyuvtymntm37mpvua')
expect(files[0].cid.codec).to.equal('dag-pb')
expect(files[0].size).to.equal(18)
})
})
}