forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
91 lines (71 loc) · 2.24 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import path from 'path'
import { fileURLToPath } from 'url'
import { execa } from 'execa'
import pDefer from 'p-defer'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function startProcess (name, args = []) {
return execa('node', [path.join(__dirname, name), ...args], {
cwd: path.resolve(__dirname),
all: true
})
}
let output1 = ''
let output2 = ''
let output3 = ''
let relayAddr
let autoRelayAddr
const proc1Ready = pDefer()
const proc2Ready = pDefer()
// Step 1 process
process.stdout.write('relay.js\n')
const proc1 = startProcess('relay.js')
proc1.all.on('data', async (data) => {
process.stdout.write(data)
output1 += uint8ArrayToString(data)
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
proc1Ready.resolve()
}
})
await proc1Ready.promise
process.stdout.write('==================================================================\n')
// Step 2 process
process.stdout.write('listener.js\n')
const proc2 = startProcess('listener.js', [relayAddr])
proc2.all.on('data', async (data) => {
process.stdout.write(data)
output2 += uint8ArrayToString(data)
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1].trim()
proc2Ready.resolve()
}
})
await proc2Ready.promise
process.stdout.write('==================================================================\n')
// Step 3 process
process.stdout.write('dialer.js\n')
const proc3 = startProcess('dialer.js', [autoRelayAddr])
proc3.all.on('data', async (data) => {
process.stdout.write(data)
output3 += uint8ArrayToString(data)
if (output3.includes('Connected to the auto relay node via')) {
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
if (remoteAddr === autoRelayAddr) {
proc3.kill()
proc2.kill()
proc1.kill()
} else {
throw new Error('dialer did not dial through the relay')
}
}
})
await Promise.all([
proc1,
proc2,
proc3
]).catch((err) => {
if (err.signal !== 'SIGTERM') {
throw err
}
})