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 pathtransfer.js
88 lines (75 loc) · 3.25 KB
/
transfer.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
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { isWebWorker } from 'ipfs-utils/src/env.js'
import { randomBytes } from 'iso-random-stream'
import concat from 'it-concat'
import { nanoid } from 'nanoid'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import pmap from 'p-map'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
* @typedef {import('multiformats').CID} CID
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testTransfer (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('transfer blocks', function () {
this.timeout(540 * 1000)
afterEach(() => factory.clean())
describe('transfer a block between', () => {
it('2 peers', async function () {
// webworkers are not dialable because webrtc is not available
const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const remoteId = await remote.id()
const local = (await factory.spawn({ type: 'proc' })).api
await local.swarm.connect(remoteId.addresses[0])
const data = uint8ArrayFromString(`IPFS is awesome ${nanoid()}`)
const cid = await local.block.put(data)
const b = await remote.block.get(cid)
expect(b).to.equalBytes(data)
})
it('3 peers', async () => {
const blocks = Array(6).fill(0).map(() => uint8ArrayFromString(`IPFS is awesome ${nanoid()}`))
const remote1 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const remote1Id = await remote1.id()
const remote2 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const remote2Id = await remote2.id()
const local = (await factory.spawn({ type: 'proc' })).api
await local.swarm.connect(remote1Id.addresses[0])
await local.swarm.connect(remote2Id.addresses[0])
await remote1.swarm.connect(remote2Id.addresses[0])
// order is important
/** @type {CID[]} */
const cids = []
cids.push(await remote1.block.put(blocks[0]))
cids.push(await remote1.block.put(blocks[1]))
cids.push(await remote2.block.put(blocks[2]))
cids.push(await remote2.block.put(blocks[3]))
cids.push(await local.block.put(blocks[4]))
cids.push(await local.block.put(blocks[5]))
await pmap(blocks, async (block, i) => {
expect(await remote1.block.get(cids[i])).to.eql(block)
expect(await remote2.block.get(cids[i])).to.eql(block)
expect(await local.block.get(cids[i])).to.eql(block)
}, { concurrency: 3 })
})
})
describe('transfer a file between', () => {
it('2 peers', async () => {
const content = randomBytes(1024)
const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const remoteId = await remote.id()
const local = (await factory.spawn({ type: 'proc' })).api
local.swarm.connect(remoteId.addresses[0])
const file = await remote.add({ path: 'awesome.txt', content })
const data = await concat(local.cat(file.cid))
expect(data.slice()).to.eql(content)
})
})
})
}