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 pathtest.js
68 lines (56 loc) · 1.49 KB
/
test.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
'use strict'
const path = require('path')
const execa = require('execa')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const PeerId = require('peer-id')
const uint8ArrayToString = require('uint8arrays/to-string')
async function test () {
let output = ''
const proc = execa('node', [path.join(__dirname, 'index.js')], {
cwd: path.resolve(__dirname),
all: true
})
proc.all.on('data', async (data) => {
process.stdout.write(data)
output += uint8ArrayToString(data)
if (output.includes('The node now has')) {
// the node has started up, try to dial it
const address = output.trim().match(/Swarm listening on (.*)\n/)[1]
console.info('Dialling', address)
const peerId = await PeerId.create()
const libp2p = new Libp2p({
peerId,
addresses: {
listen: ['/ip4/127.0.0.1/tcp/0']
},
modules: {
transport: [
TCP
],
streamMuxer: [
MPLEX
],
connEncryption: [
NOISE
]
}
})
await libp2p.start()
await libp2p.dial(address)
console.info('Dialled', address)
proc.kill()
await libp2p.stop()
}
})
await proc.catch(() => {
// throw new Error('libp2p should have been killed')
}, (err) => {
if (err.exitSignal !== 'SIGTERM') {
throw err
}
})
}
module.exports = test