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 pathcp.js
476 lines (376 loc) · 16.2 KB
/
cp.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* eslint-env mocha */
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { nanoid } from 'nanoid'
import all from 'it-all'
import { fixtures } from '../utils/index.js'
import { expect } from 'aegir/utils/chai.js'
import { getDescribe, getIt } from '../utils/mocha.js'
import { identity } from 'multiformats/hashes/identity'
import { CID } from 'multiformats/cid'
import { randomBytes } from 'iso-random-stream'
import { createShardedDirectory } from '../utils/create-sharded-directory.js'
import isShardAtPath from '../utils/is-shard-at-path.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
export function testCp (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.cp', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('refuses to copy files without a source', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.cp()).to.eventually.be.rejected.with('Please supply at least one source')
})
it('refuses to copy files without a source, even with options', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.cp({})).to.eventually.be.rejected.with('Please supply at least one source')
})
it('refuses to copy files without a destination', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.cp('/source')).to.eventually.be.rejected.with('Please supply at least one source')
})
it('refuses to copy files without a destination, even with options', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.cp('/source', {})).to.eventually.be.rejected.with('Please supply at least one source')
})
it('refuses to copy a non-existent file', async () => {
await expect(ipfs.files.cp('/i-do-not-exist', '/destination', {})).to.eventually.be.rejected.with('does not exist')
})
it('refuses to copy multiple files to a non-existent child directory', async () => {
const src1 = `/src1-${Math.random()}`
const src2 = `/src2-${Math.random()}`
const parent = `/output-${Math.random()}`
await ipfs.files.write(src1, [], {
create: true
})
await ipfs.files.write(src2, [], {
create: true
})
await ipfs.files.mkdir(parent)
await expect(ipfs.files.cp([src1, src2], `${parent}/child`)).to.eventually.be.rejectedWith(Error)
.that.has.property('message').that.matches(/destination did not exist/)
})
it('refuses to copy files to an unreadable node', async () => {
const src1 = `/src2-${Math.random()}`
const parent = `/output-${Math.random()}`
const hash = await identity.digest(uint8ArrayFromString('derp'))
const cid = CID.createV1(identity.code, hash)
await ipfs.block.put(uint8ArrayFromString('derp'), {
mhtype: 'identity'
})
await ipfs.files.cp(`/ipfs/${cid}`, parent)
await ipfs.files.write(src1, [], {
create: true
})
await expect(ipfs.files.cp(src1, `${parent}/child`)).to.eventually.be.rejectedWith(Error)
.that.has.property('message').that.matches(/unsupported codec/i)
})
it('refuses to copy files to an exsting file', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true
})
await ipfs.files.write(destination, randomBytes(100), {
create: true
})
try {
await ipfs.files.cp(source, destination)
throw new Error('No error was thrown when trying to overwrite a file')
} catch (/** @type {any} */ err) {
expect(err.message).to.contain('directory already has entry by that name')
}
})
it('refuses to copy a file to itself', async () => {
const source = `/source-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true
})
try {
await ipfs.files.cp(source, source)
throw new Error('No error was thrown for a non-existent file')
} catch (/** @type {any} */ err) {
expect(err.message).to.contain('directory already has entry by that name')
}
})
it('copies a file to new location', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`
const data = randomBytes(500)
await ipfs.files.write(source, data, {
create: true
})
await ipfs.files.cp(source, destination)
const bytes = uint8ArrayConcat(await all(ipfs.files.read(destination)))
expect(bytes).to.deep.equal(data)
})
it('copies a file to a pre-existing directory', async () => {
const source = `/source-file-${Math.random()}.txt`
const directory = `/dest-directory-${Math.random()}`
const destination = `${directory}${source}`
await ipfs.files.write(source, randomBytes(500), {
create: true
})
await ipfs.files.mkdir(directory)
await ipfs.files.cp(source, directory)
const stats = await ipfs.files.stat(destination)
expect(stats.size).to.equal(500)
})
it('copies directories', async () => {
const source = `/source-directory-${Math.random()}`
const destination = `/dest-directory-${Math.random()}`
await ipfs.files.mkdir(source)
await ipfs.files.cp(source, destination)
const stats = await ipfs.files.stat(destination)
expect(stats.type).to.equal('directory')
})
it('copies directories recursively', async () => {
const directory = `/source-directory-${Math.random()}`
const subDirectory = `/source-directory-${Math.random()}`
const source = `${directory}${subDirectory}`
const destination = `/dest-directory-${Math.random()}`
await ipfs.files.mkdir(source, {
parents: true
})
await ipfs.files.cp(directory, destination)
const stats = await ipfs.files.stat(destination)
expect(stats.type).to.equal('directory')
const subDirStats = await ipfs.files.stat(`${destination}/${subDirectory}`)
expect(subDirStats.type).to.equal('directory')
})
it('copies multiple files to new location', async () => {
const sources = [{
path: `/source-file-${Math.random()}.txt`,
data: randomBytes(500)
}, {
path: `/source-file-${Math.random()}.txt`,
data: randomBytes(500)
}]
const destination = `/dest-dir-${Math.random()}`
for (const source of sources) {
await ipfs.files.write(source.path, source.data, {
create: true
})
}
await ipfs.files.cp([sources[0].path, sources[1].path], destination, {
parents: true
})
for (const source of sources) {
const bytes = uint8ArrayConcat(await all(ipfs.files.read(`${destination}${source.path}`)))
expect(bytes).to.deep.equal(source.data)
}
})
it('copies files from ipfs paths', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true
})
const stats = await ipfs.files.stat(source)
await ipfs.files.cp(`/ipfs/${stats.cid}`, destination)
const destinationStats = await ipfs.files.stat(destination)
expect(destinationStats.size).to.equal(100)
})
it('copies files from deep ipfs paths', async () => {
const dir = `dir-${Math.random()}`
const file = `source-file-${Math.random()}.txt`
const source = `/${dir}/${file}`
const destination = `/dest-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true,
parents: true
})
const stats = await ipfs.files.stat(`/${dir}`)
await ipfs.files.cp(`/ipfs/${stats.cid}/${file}`, destination)
const destinationStats = await ipfs.files.stat(destination)
expect(destinationStats.size).to.equal(100)
})
it('copies files to deep mfs paths and creates intermediate directories', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/really/deep/path/to/dest-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true
})
await ipfs.files.cp(source, destination, {
parents: true
})
const destinationStats = await ipfs.files.stat(destination)
expect(destinationStats.size).to.equal(100)
})
it('fails to copy files to deep mfs paths when intermediate directories do not exist', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/really/deep/path-${Math.random()}/to-${Math.random()}/dest-file-${Math.random()}.txt`
await ipfs.files.write(source, randomBytes(100), {
create: true
})
await expect(ipfs.files.cp(source, destination)).to.eventually.be.rejected()
})
it('should respect metadata when copying files', async function () {
const testSrcPath = `/test-${nanoid()}`
const testDestPath = `/test-${nanoid()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime.getTime() - (seconds * 1000)) * 1000
}
await ipfs.files.write(testSrcPath, uint8ArrayFromString('TEST'), {
create: true,
mode,
mtime
})
await ipfs.files.cp(testSrcPath, testDestPath)
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
it('should respect metadata when copying directories', async function () {
const testSrcPath = `/test-${nanoid()}`
const testDestPath = `/test-${nanoid()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime.getTime() - (seconds * 1000)) * 1000
}
await ipfs.files.mkdir(testSrcPath, {
mode,
mtime
})
await ipfs.files.cp(testSrcPath, testDestPath, {
recursive: true
})
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
it('should respect metadata when copying from outside of mfs', async function () {
const testDestPath = `/test-${nanoid()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime.getTime() - (seconds * 1000)) * 1000
}
const {
cid
} = await ipfs.add({
content: fixtures.smallFile.data,
mode,
mtime
})
await ipfs.files.cp(`/ipfs/${cid}`, testDestPath)
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
describe('with sharding', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async function () {
const ipfsd = await factory.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go with automatic threshold dropped to the minimum so it shards everything
Internal: {
UnixFSShardingSizeThreshold: '1B'
}
}
}
})
ipfs = ipfsd.api
})
it('copies a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const normalDir = `dir-${Math.random()}`
const normalDirPath = `/${normalDir}`
await ipfs.files.mkdir(normalDirPath)
await ipfs.files.cp(shardedDirPath, normalDirPath)
const finalShardedDirPath = `${normalDirPath}${shardedDirPath}`
// should still be a sharded directory
await expect(isShardAtPath(finalShardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(finalShardedDirPath)).type).to.equal('directory')
const files = await all(ipfs.files.ls(finalShardedDirPath))
expect(files.length).to.be.ok()
})
it('copies a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const normalDir = `dir-${Math.random()}`
const normalDirPath = `/${normalDir}`
await ipfs.files.mkdir(normalDirPath)
await ipfs.files.cp(normalDirPath, shardedDirPath)
const finalDirPath = `${shardedDirPath}${normalDirPath}`
// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalDirPath)).type).to.equal('directory')
})
it('copies a file from a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const file = `file-${Math.random()}.txt`
const filePath = `/${file}`
const finalFilePath = `${shardedDirPath}/${file}`
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})
await ipfs.files.cp(filePath, finalFilePath)
// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})
it('copies a file from a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const othershardedDirPath = await createShardedDirectory(ipfs)
const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${othershardedDirPath}/${file}`
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})
await ipfs.files.cp(filePath, finalFilePath)
// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
await expect(isShardAtPath(othershardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(othershardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})
it('copies a file from a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dir = `dir-${Math.random()}`
const dirPath = `/${dir}`
const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${dirPath}/${file}`
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3]), {
create: true
})
await ipfs.files.mkdir(dirPath)
await ipfs.files.cp(filePath, finalFilePath)
// should still be a sharded directory
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(dirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
})
})
})
}