-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathconnection.ts
226 lines (193 loc) · 6.48 KB
/
connection.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { CodeError } from '@libp2p/interface/errors'
import { logger } from '@libp2p/logger'
import * as mss from '@libp2p/multistream-select'
import { peerIdFromString } from '@libp2p/peer-id'
import { duplexPair } from 'it-pair/duplex'
import { pipe } from 'it-pipe'
import { mockMultiaddrConnection } from './multiaddr-connection.js'
import { mockMuxer } from './muxer.js'
import { mockRegistrar } from './registrar.js'
import type { AbortOptions } from '@libp2p/interface'
import type { MultiaddrConnection, Connection, Stream, Direction, ConnectionTimeline, ConnectionStatus } from '@libp2p/interface/connection'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
import type { Registrar } from '@libp2p/interface-internal/registrar'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Duplex, Source } from 'it-stream-types'
import type { Uint8ArrayList } from 'uint8arraylist'
const log = logger('libp2p:mock-connection')
export interface MockConnectionOptions {
direction?: Direction
registrar?: Registrar
muxerFactory?: StreamMuxerFactory
}
interface MockConnectionInit {
remoteAddr: Multiaddr
remotePeer: PeerId
direction: Direction
maConn: MultiaddrConnection
muxer: StreamMuxer
}
class MockConnection implements Connection {
public id: string
public remoteAddr: Multiaddr
public remotePeer: PeerId
public direction: Direction
public timeline: ConnectionTimeline
public multiplexer?: string
public encryption?: string
public status: ConnectionStatus
public streams: Stream[]
public tags: string[]
public transient: boolean
private readonly muxer: StreamMuxer
private readonly maConn: MultiaddrConnection
constructor (init: MockConnectionInit) {
const { remoteAddr, remotePeer, direction, maConn, muxer } = init
this.id = `mock-connection-${Math.random()}`
this.remoteAddr = remoteAddr
this.remotePeer = remotePeer
this.direction = direction
this.status = 'open'
this.direction = direction
this.timeline = maConn.timeline
this.multiplexer = 'test-multiplexer'
this.encryption = 'yes-yes-very-secure'
this.streams = []
this.tags = []
this.muxer = muxer
this.maConn = maConn
this.transient = false
}
async newStream (protocols: string | string[], options?: AbortOptions): Promise<Stream> {
if (!Array.isArray(protocols)) {
protocols = [protocols]
}
if (protocols.length === 0) {
throw new Error('protocols must have a length')
}
if (this.status !== 'open') {
throw new CodeError('connection must be open to create streams', 'ERR_CONNECTION_CLOSED')
}
const id = `${Math.random()}`
const stream = await this.muxer.newStream(id)
const result = await mss.select(stream, protocols, options)
stream.protocol = result.protocol
stream.direction = 'outbound'
stream.sink = result.stream.sink
stream.source = result.stream.source
this.streams.push(stream)
return stream
}
addStream (stream: Stream): void {
this.streams.push(stream)
}
removeStream (id: string): void {
this.streams = this.streams.filter(stream => stream.id !== id)
}
async close (options?: AbortOptions): Promise<void> {
this.status = 'closing'
await Promise.all(
this.streams.map(async s => s.close(options))
)
await this.maConn.close()
this.status = 'closed'
this.timeline.close = Date.now()
}
abort (err: Error): void {
this.status = 'closing'
this.streams.forEach(s => {
s.abort(err)
})
this.maConn.abort(err)
this.status = 'closed'
this.timeline.close = Date.now()
}
}
export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectionOptions = {}): Connection {
const remoteAddr = maConn.remoteAddr
const remotePeerIdStr = remoteAddr.getPeerId() ?? '12D3KooWCrhmFM1BCPGBkNzbPfDk4cjYmtAYSpZwUBC69Qg2kZyq'
if (remotePeerIdStr == null) {
throw new Error('Remote multiaddr must contain a peer id')
}
const remotePeer = peerIdFromString(remotePeerIdStr)
const direction = opts.direction ?? 'inbound'
const registrar = opts.registrar ?? mockRegistrar()
const muxerFactory = opts.muxerFactory ?? mockMuxer()
const muxer = muxerFactory.createStreamMuxer({
direction,
onIncomingStream: (muxedStream) => {
try {
mss.handle(muxedStream, registrar.getProtocols())
.then(({ stream, protocol }) => {
log('%s: incoming stream opened on %s', direction, protocol)
muxedStream.protocol = protocol
muxedStream.sink = stream.sink
muxedStream.source = stream.source
connection.addStream(muxedStream)
const { handler } = registrar.getHandler(protocol)
handler({ connection, stream: muxedStream })
}).catch(err => {
log.error(err)
})
} catch (err: any) {
log.error(err)
}
},
onStreamEnd: (muxedStream) => {
connection.removeStream(muxedStream.id)
}
})
void pipe(
maConn, muxer, maConn
)
const connection = new MockConnection({
remoteAddr,
remotePeer,
direction,
maConn,
muxer
})
return connection
}
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>): Stream {
return {
...stream,
close: async () => {},
closeRead: async () => {},
closeWrite: async () => {},
abort: () => {},
direction: 'outbound',
protocol: '/foo/1.0.0',
timeline: {
open: Date.now()
},
metadata: {},
id: `stream-${Date.now()}`,
status: 'open',
readStatus: 'ready',
writeStatus: 'ready'
}
}
export interface Peer {
peerId: PeerId
registrar: Registrar
}
export function multiaddrConnectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ MultiaddrConnection, MultiaddrConnection ] {
const [peerBtoPeerA, peerAtoPeerB] = duplexPair<Uint8Array>()
return [
mockMultiaddrConnection(peerAtoPeerB, b.peerId),
mockMultiaddrConnection(peerBtoPeerA, a.peerId)
]
}
export function connectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ Connection, Connection ] {
const [peerBtoPeerA, peerAtoPeerB] = multiaddrConnectionPair(a, b)
return [
mockConnection(peerBtoPeerA, {
registrar: a.registrar
}),
mockConnection(peerAtoPeerB, {
registrar: b.registrar
})
]
}