-
Notifications
You must be signed in to change notification settings - Fork 443
/
registrar.ts
86 lines (67 loc) · 2.24 KB
/
registrar.ts
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
import merge from 'merge-options'
import type { Connection, PeerId, Topology } from '@libp2p/interface'
import type { IncomingStreamData, Registrar, StreamHandler, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface-internal'
export class MockRegistrar implements Registrar {
private readonly topologies = new Map<string, Array<{ id: string, topology: Topology }>>()
private readonly handlers = new Map<string, StreamHandlerRecord>()
getProtocols (): string[] {
return Array.from(this.handlers.keys()).sort()
}
async handle (protocol: string, handler: StreamHandler, opts?: StreamHandlerOptions): Promise<void> {
const options = merge.bind({ ignoreUndefined: true })({
maxInboundStreams: 1,
maxOutboundStreams: 1
}, opts)
if (this.handlers.has(protocol)) {
throw new Error(`Handler already registered for protocol ${protocol}`)
}
this.handlers.set(protocol, {
handler,
options
})
}
async unhandle (protocol: string): Promise<void> {
this.handlers.delete(protocol)
}
getHandler (protocol: string): StreamHandlerRecord {
const handler = this.handlers.get(protocol)
if (handler == null) {
throw new Error(`No handler registered for protocol ${protocol}`)
}
return handler
}
async register (protocol: string, topology: Topology): Promise<string> {
const id = `topology-id-${Math.random()}`
let topologies = this.topologies.get(protocol)
if (topologies == null) {
topologies = []
}
topologies.push({
id,
topology
})
this.topologies.set(protocol, topologies)
return id
}
unregister (id: string | string[]): void {
if (!Array.isArray(id)) {
id = [id]
}
id.forEach(id => this.topologies.delete(id))
}
getTopologies (protocol: string): Topology[] {
return (this.topologies.get(protocol) ?? []).map(t => t.topology)
}
}
export function mockRegistrar (): Registrar {
return new MockRegistrar()
}
export async function mockIncomingStreamEvent (protocol: string, conn: Connection, remotePeer: PeerId): Promise<IncomingStreamData> {
return {
...await conn.newStream([protocol]),
// @ts-expect-error incomplete implementation
connection: {
remotePeer
}
}
}