-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoke.js
67 lines (54 loc) · 1.8 KB
/
smoke.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
import { CID } from 'multiformats/cid'
import * as helper from './helper/index.js'
import { Connection } from './lib/networking.js'
import { BITSWAP_V_120, Entry, Message, WantList } from './lib/protocol.js'
const TARGET_ENV = process.env.TARGET_ENV ?? 'local'
const MUXERS = process.env.MUXERS ? process.env.MUXERS.split(',') : ['mplex', 'yamux']
const protocol = BITSWAP_V_120
async function test (cid, type) {
cid = CID.parse(cid)
type = type === 'data' ? Entry.WantType.Block : Entry.WantType.Have
console.log('creating peer client ...')
console.log(`target: ${helper.targets[TARGET_ENV]}\n protocol: ${protocol}`)
console.time('connected')
const client = await helper.createClient({
target: helper.targets[TARGET_ENV],
protocol,
muxers: MUXERS
})
console.log('client connected.')
console.timeEnd('connected')
const handlerOptions = {
maxInboundStreams: Infinity,
maxOutboundStreams: Infinity
}
client.node.handle(protocol, async ({ stream }) => {
const connection = new Connection(stream)
connection.on('data', async data => {
console.log('\n\n>>> response')
const message = Message.decode(data)
console.log('***')
console.info(helper.printResponse(message))
console.log('***')
console.timeEnd('response')
end(client)
})
}, handlerOptions)
console.log(`sending request ${cid.toString()} ${type === Entry.WantType.Block ? 'WantType.Block' : 'WantType.Have'} ...`)
console.time('response')
client.link.send(
new Message(
new WantList(
[new Entry(cid, 1, false, type, true)], true
)
).encode(protocol)
)
}
function end (client) {
client.node.stop()
client.connection.close()
client.link.close()
client.stream.close()
console.log('--- done')
}
test(process.argv[2], process.argv[3])