@libp2p/interface-compliance-tests 6.5.0 → 7.0.0-049bfa0fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/connection-encryption/index.d.ts.map +1 -1
- package/dist/src/connection-encryption/index.js +24 -15
- package/dist/src/connection-encryption/index.js.map +1 -1
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/base-test.js +62 -345
- package/dist/src/stream-muxer/base-test.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +254 -320
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/dist/src/stream-muxer/index.js +2 -2
- package/dist/src/stream-muxer/index.js.map +1 -1
- package/dist/src/stream-muxer/{mega-stress-test.d.ts → stream-test.d.ts} +2 -2
- package/dist/src/stream-muxer/stream-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/stream-test.js +290 -0
- package/dist/src/stream-muxer/stream-test.js.map +1 -0
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/stress-test.js +70 -16
- package/dist/src/stream-muxer/stress-test.js.map +1 -1
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/src/transport/index.js +235 -205
- package/dist/src/transport/index.js.map +1 -1
- package/dist/src/transport/utils.d.ts.map +1 -1
- package/dist/src/transport/utils.js +3 -2
- package/dist/src/transport/utils.js.map +1 -1
- package/package.json +23 -51
- package/src/connection-encryption/index.ts +27 -20
- package/src/stream-muxer/base-test.ts +75 -413
- package/src/stream-muxer/close-test.ts +305 -343
- package/src/stream-muxer/index.ts +2 -2
- package/src/stream-muxer/stream-test.ts +381 -0
- package/src/stream-muxer/stress-test.ts +92 -18
- package/src/transport/index.ts +281 -241
- package/src/transport/utils.ts +3 -2
- package/dist/src/connection-encryption/utils/index.d.ts +0 -3
- package/dist/src/connection-encryption/utils/index.d.ts.map +0 -1
- package/dist/src/connection-encryption/utils/index.js +0 -21
- package/dist/src/connection-encryption/utils/index.js.map +0 -1
- package/dist/src/matchers.d.ts +0 -12
- package/dist/src/matchers.d.ts.map +0 -1
- package/dist/src/matchers.js +0 -14
- package/dist/src/matchers.js.map +0 -1
- package/dist/src/mocks/connection-manager.d.ts +0 -27
- package/dist/src/mocks/connection-manager.d.ts.map +0 -1
- package/dist/src/mocks/connection-manager.js +0 -147
- package/dist/src/mocks/connection-manager.js.map +0 -1
- package/dist/src/mocks/connection.d.ts +0 -41
- package/dist/src/mocks/connection.d.ts.map +0 -1
- package/dist/src/mocks/connection.js +0 -236
- package/dist/src/mocks/connection.js.map +0 -1
- package/dist/src/mocks/duplex.d.ts +0 -4
- package/dist/src/mocks/duplex.d.ts.map +0 -1
- package/dist/src/mocks/duplex.js +0 -9
- package/dist/src/mocks/duplex.js.map +0 -1
- package/dist/src/mocks/index.d.ts +0 -12
- package/dist/src/mocks/index.d.ts.map +0 -1
- package/dist/src/mocks/index.js +0 -8
- package/dist/src/mocks/index.js.map +0 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
- package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
- package/dist/src/mocks/multiaddr-connection.js +0 -64
- package/dist/src/mocks/multiaddr-connection.js.map +0 -1
- package/dist/src/mocks/muxer.d.ts +0 -36
- package/dist/src/mocks/muxer.d.ts.map +0 -1
- package/dist/src/mocks/muxer.js +0 -234
- package/dist/src/mocks/muxer.js.map +0 -1
- package/dist/src/mocks/registrar.d.ts +0 -16
- package/dist/src/mocks/registrar.d.ts.map +0 -1
- package/dist/src/mocks/registrar.js +0 -66
- package/dist/src/mocks/registrar.js.map +0 -1
- package/dist/src/mocks/upgrader.d.ts +0 -9
- package/dist/src/mocks/upgrader.d.ts.map +0 -1
- package/dist/src/mocks/upgrader.js +0 -46
- package/dist/src/mocks/upgrader.js.map +0 -1
- package/dist/src/pubsub/api.d.ts +0 -6
- package/dist/src/pubsub/api.d.ts.map +0 -1
- package/dist/src/pubsub/api.js +0 -88
- package/dist/src/pubsub/api.js.map +0 -1
- package/dist/src/pubsub/connection-handlers.d.ts +0 -6
- package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
- package/dist/src/pubsub/connection-handlers.js +0 -329
- package/dist/src/pubsub/connection-handlers.js.map +0 -1
- package/dist/src/pubsub/emit-self.d.ts +0 -6
- package/dist/src/pubsub/emit-self.d.ts.map +0 -1
- package/dist/src/pubsub/emit-self.js +0 -80
- package/dist/src/pubsub/emit-self.js.map +0 -1
- package/dist/src/pubsub/index.d.ts +0 -18
- package/dist/src/pubsub/index.d.ts.map +0 -1
- package/dist/src/pubsub/index.js +0 -17
- package/dist/src/pubsub/index.js.map +0 -1
- package/dist/src/pubsub/messages.d.ts +0 -6
- package/dist/src/pubsub/messages.d.ts.map +0 -1
- package/dist/src/pubsub/messages.js +0 -48
- package/dist/src/pubsub/messages.js.map +0 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
- package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/multiple-nodes.js +0 -350
- package/dist/src/pubsub/multiple-nodes.js.map +0 -1
- package/dist/src/pubsub/two-nodes.d.ts +0 -6
- package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/two-nodes.js +0 -216
- package/dist/src/pubsub/two-nodes.js.map +0 -1
- package/dist/src/pubsub/utils.d.ts +0 -5
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -27
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.js +0 -11
- package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
- package/dist/src/stream-muxer/spawner.d.ts +0 -4
- package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
- package/dist/src/stream-muxer/spawner.js +0 -39
- package/dist/src/stream-muxer/spawner.js.map +0 -1
- package/dist/typedoc-urls.json +0 -44
- package/src/connection-encryption/utils/index.ts +0 -27
- package/src/matchers.ts +0 -18
- package/src/mocks/connection-manager.ts +0 -216
- package/src/mocks/connection.ts +0 -309
- package/src/mocks/duplex.ts +0 -11
- package/src/mocks/index.ts +0 -11
- package/src/mocks/multiaddr-connection.ts +0 -80
- package/src/mocks/muxer.ts +0 -331
- package/src/mocks/registrar.ts +0 -86
- package/src/mocks/upgrader.ts +0 -65
- package/src/pubsub/api.ts +0 -116
- package/src/pubsub/connection-handlers.ts +0 -413
- package/src/pubsub/emit-self.ts +0 -99
- package/src/pubsub/index.ts +0 -34
- package/src/pubsub/messages.ts +0 -59
- package/src/pubsub/multiple-nodes.ts +0 -440
- package/src/pubsub/two-nodes.ts +0 -272
- package/src/pubsub/utils.ts +0 -34
- package/src/stream-muxer/mega-stress-test.ts +0 -14
- package/src/stream-muxer/spawner.ts +0 -57
package/src/mocks/connection.ts
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
import { ConnectionClosedError } from '@libp2p/interface'
|
|
2
|
-
import { defaultLogger, logger } from '@libp2p/logger'
|
|
3
|
-
import * as mss from '@libp2p/multistream-select'
|
|
4
|
-
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
|
-
import { closeSource } from '@libp2p/utils/close-source'
|
|
6
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
7
|
-
import { pipe } from 'it-pipe'
|
|
8
|
-
import { Uint8ArrayList } from 'uint8arraylist'
|
|
9
|
-
import { mockMultiaddrConnection } from './multiaddr-connection.js'
|
|
10
|
-
import { mockMuxer } from './muxer.js'
|
|
11
|
-
import { mockRegistrar } from './registrar.js'
|
|
12
|
-
import type { AbortOptions, ComponentLogger, Logger, MultiaddrConnection, Connection, Stream, Direction, ConnectionTimeline, ConnectionStatus, PeerId, StreamMuxer, StreamMuxerFactory, NewStreamOptions, ConnectionLimits } from '@libp2p/interface'
|
|
13
|
-
import type { Registrar } from '@libp2p/interface-internal'
|
|
14
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
15
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
16
|
-
|
|
17
|
-
export interface MockConnectionOptions {
|
|
18
|
-
direction?: Direction
|
|
19
|
-
registrar?: Registrar
|
|
20
|
-
muxerFactory?: StreamMuxerFactory
|
|
21
|
-
logger?: ComponentLogger
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface MockConnectionInit {
|
|
25
|
-
remoteAddr: Multiaddr
|
|
26
|
-
remotePeer: PeerId
|
|
27
|
-
direction: Direction
|
|
28
|
-
maConn: MultiaddrConnection
|
|
29
|
-
muxer: StreamMuxer
|
|
30
|
-
logger: ComponentLogger
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
class MockConnection implements Connection {
|
|
34
|
-
public id: string
|
|
35
|
-
public remoteAddr: Multiaddr
|
|
36
|
-
public remotePeer: PeerId
|
|
37
|
-
public direction: Direction
|
|
38
|
-
public timeline: ConnectionTimeline
|
|
39
|
-
public multiplexer?: string
|
|
40
|
-
public encryption?: string
|
|
41
|
-
public status: ConnectionStatus
|
|
42
|
-
public streams: Stream[]
|
|
43
|
-
public tags: string[]
|
|
44
|
-
public limits?: ConnectionLimits
|
|
45
|
-
public log: Logger
|
|
46
|
-
|
|
47
|
-
private readonly muxer: StreamMuxer
|
|
48
|
-
private readonly maConn: MultiaddrConnection
|
|
49
|
-
private readonly logger: ComponentLogger
|
|
50
|
-
|
|
51
|
-
constructor (init: MockConnectionInit) {
|
|
52
|
-
const { remoteAddr, remotePeer, direction, maConn, muxer, logger } = init
|
|
53
|
-
|
|
54
|
-
this.id = `mock-connection-${Math.random()}`
|
|
55
|
-
this.remoteAddr = remoteAddr
|
|
56
|
-
this.remotePeer = remotePeer
|
|
57
|
-
this.direction = direction
|
|
58
|
-
this.status = 'open'
|
|
59
|
-
this.direction = direction
|
|
60
|
-
this.timeline = maConn.timeline
|
|
61
|
-
this.multiplexer = 'test-multiplexer'
|
|
62
|
-
this.encryption = 'yes-yes-very-secure'
|
|
63
|
-
this.streams = []
|
|
64
|
-
this.tags = []
|
|
65
|
-
this.muxer = muxer
|
|
66
|
-
this.maConn = maConn
|
|
67
|
-
this.logger = logger
|
|
68
|
-
this.log = logger.forComponent(this.id)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async newStream (protocols: string | string[], options?: NewStreamOptions): Promise<Stream> {
|
|
72
|
-
if (!Array.isArray(protocols)) {
|
|
73
|
-
protocols = [protocols]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (protocols.length === 0) {
|
|
77
|
-
throw new Error('protocols must have a length')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (this.status !== 'open') {
|
|
81
|
-
throw new ConnectionClosedError('connection must be open to create streams')
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
options?.signal?.throwIfAborted()
|
|
85
|
-
|
|
86
|
-
const id = `${Math.random()}`
|
|
87
|
-
const stream = await this.muxer.newStream(id)
|
|
88
|
-
const result = await mss.select(stream, protocols, {
|
|
89
|
-
...options,
|
|
90
|
-
log: this.logger.forComponent('libp2p:mock-connection:stream:mss:select')
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
stream.protocol = result.protocol
|
|
94
|
-
stream.direction = 'outbound'
|
|
95
|
-
stream.sink = result.stream.sink
|
|
96
|
-
stream.source = result.stream.source
|
|
97
|
-
|
|
98
|
-
this.streams.push(stream)
|
|
99
|
-
|
|
100
|
-
return stream
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async close (options?: AbortOptions): Promise<void> {
|
|
104
|
-
this.status = 'closing'
|
|
105
|
-
await Promise.all(
|
|
106
|
-
this.streams.map(async s => s.close(options))
|
|
107
|
-
)
|
|
108
|
-
await this.maConn.close()
|
|
109
|
-
this.status = 'closed'
|
|
110
|
-
this.timeline.close = Date.now()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
abort (err: Error): void {
|
|
114
|
-
this.status = 'closing'
|
|
115
|
-
this.streams.forEach(s => {
|
|
116
|
-
s.abort(err)
|
|
117
|
-
})
|
|
118
|
-
this.maConn.abort(err)
|
|
119
|
-
this.status = 'closed'
|
|
120
|
-
this.timeline.close = Date.now()
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectionOptions = {}): Connection {
|
|
125
|
-
const remoteAddr = maConn.remoteAddr
|
|
126
|
-
const remotePeerIdStr = remoteAddr.getPeerId() ?? '12D3KooWCrhmFM1BCPGBkNzbPfDk4cjYmtAYSpZwUBC69Qg2kZyq'
|
|
127
|
-
const logger = opts.logger ?? defaultLogger()
|
|
128
|
-
|
|
129
|
-
if (remotePeerIdStr == null) {
|
|
130
|
-
throw new Error('Remote multiaddr must contain a peer id')
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const remotePeer = peerIdFromString(remotePeerIdStr)
|
|
134
|
-
const direction = opts.direction ?? 'inbound'
|
|
135
|
-
const registrar = opts.registrar ?? mockRegistrar()
|
|
136
|
-
const muxerFactory = opts.muxerFactory ?? mockMuxer()
|
|
137
|
-
const log = logger.forComponent('libp2p:mock-muxer')
|
|
138
|
-
|
|
139
|
-
const muxer = muxerFactory.createStreamMuxer({
|
|
140
|
-
log,
|
|
141
|
-
direction,
|
|
142
|
-
onIncomingStream: (muxedStream) => {
|
|
143
|
-
try {
|
|
144
|
-
mss.handle(muxedStream, registrar.getProtocols(), {
|
|
145
|
-
log
|
|
146
|
-
})
|
|
147
|
-
.then(async ({ stream, protocol }) => {
|
|
148
|
-
log('%s: incoming stream opened on %s', direction, protocol)
|
|
149
|
-
muxedStream.protocol = protocol
|
|
150
|
-
muxedStream.sink = stream.sink
|
|
151
|
-
muxedStream.source = stream.source
|
|
152
|
-
|
|
153
|
-
connection.streams.push(muxedStream)
|
|
154
|
-
const { handler } = registrar.getHandler(protocol)
|
|
155
|
-
|
|
156
|
-
await handler({ connection, stream: muxedStream })
|
|
157
|
-
}).catch(err => {
|
|
158
|
-
log.error(err)
|
|
159
|
-
muxedStream.abort(err)
|
|
160
|
-
})
|
|
161
|
-
} catch (err: any) {
|
|
162
|
-
log.error(err)
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
onStreamEnd: (muxedStream) => {
|
|
166
|
-
connection.streams = connection.streams.filter(stream => stream.id !== muxedStream.id)
|
|
167
|
-
}
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
void pipe(
|
|
171
|
-
maConn, muxer, maConn
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
const connection = new MockConnection({
|
|
175
|
-
remoteAddr,
|
|
176
|
-
remotePeer,
|
|
177
|
-
direction,
|
|
178
|
-
maConn,
|
|
179
|
-
muxer,
|
|
180
|
-
logger
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
return connection
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export interface StreamInit {
|
|
187
|
-
direction?: Direction
|
|
188
|
-
protocol?: string
|
|
189
|
-
id?: string
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init: StreamInit = {}): Stream {
|
|
193
|
-
const id = `stream-${Date.now()}`
|
|
194
|
-
const log = logger(`libp2p:mock-stream:${id}`)
|
|
195
|
-
|
|
196
|
-
// ensure stream output is `Uint8ArrayList` as it would be from an actual
|
|
197
|
-
// Stream where everything is length-varint encoded
|
|
198
|
-
const originalSource = stream.source
|
|
199
|
-
stream.source = (async function * (): AsyncGenerator<Uint8ArrayList, any, unknown> {
|
|
200
|
-
for await (const buf of originalSource) {
|
|
201
|
-
if (buf instanceof Uint8Array) {
|
|
202
|
-
yield new Uint8ArrayList(buf)
|
|
203
|
-
} else {
|
|
204
|
-
yield buf
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
})()
|
|
208
|
-
|
|
209
|
-
const abortSinkController = new AbortController()
|
|
210
|
-
const originalSink = stream.sink.bind(stream)
|
|
211
|
-
stream.sink = async (source) => {
|
|
212
|
-
abortSinkController.signal.addEventListener('abort', () => {
|
|
213
|
-
closeSource(source, log)
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
await originalSink(source)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const mockStream: Stream = {
|
|
220
|
-
...stream,
|
|
221
|
-
close: async (options) => {
|
|
222
|
-
await mockStream.closeRead(options)
|
|
223
|
-
await mockStream.closeWrite(options)
|
|
224
|
-
},
|
|
225
|
-
closeRead: async () => {
|
|
226
|
-
closeSource(originalSource, log)
|
|
227
|
-
mockStream.timeline.closeRead = Date.now()
|
|
228
|
-
|
|
229
|
-
if (mockStream.timeline.closeWrite != null) {
|
|
230
|
-
mockStream.timeline.close = Date.now()
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
closeWrite: async () => {
|
|
234
|
-
abortSinkController.abort()
|
|
235
|
-
mockStream.timeline.closeWrite = Date.now()
|
|
236
|
-
|
|
237
|
-
if (mockStream.timeline.closeRead != null) {
|
|
238
|
-
mockStream.timeline.close = Date.now()
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
abort: () => {
|
|
242
|
-
closeSource(originalSource, log)
|
|
243
|
-
mockStream.timeline.closeWrite = Date.now()
|
|
244
|
-
mockStream.timeline.closeRead = Date.now()
|
|
245
|
-
mockStream.timeline.close = Date.now()
|
|
246
|
-
},
|
|
247
|
-
direction: 'outbound',
|
|
248
|
-
protocol: '/foo/1.0.0',
|
|
249
|
-
timeline: {
|
|
250
|
-
open: Date.now()
|
|
251
|
-
},
|
|
252
|
-
metadata: {},
|
|
253
|
-
id: `stream-${Date.now()}`,
|
|
254
|
-
status: 'open',
|
|
255
|
-
readStatus: 'ready',
|
|
256
|
-
writeStatus: 'ready',
|
|
257
|
-
log: logger('mock-stream'),
|
|
258
|
-
...init
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return mockStream
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export interface StreamPairInit {
|
|
265
|
-
duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>
|
|
266
|
-
init?: StreamInit
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function streamPair (a: StreamPairInit, b: StreamPairInit, init: StreamInit = {}): [Stream, Stream] {
|
|
270
|
-
return [
|
|
271
|
-
mockStream(a.duplex, {
|
|
272
|
-
direction: 'outbound',
|
|
273
|
-
...init,
|
|
274
|
-
...(a.init ?? {})
|
|
275
|
-
}),
|
|
276
|
-
mockStream(b.duplex, {
|
|
277
|
-
direction: 'inbound',
|
|
278
|
-
...init,
|
|
279
|
-
...(b.init ?? {})
|
|
280
|
-
})
|
|
281
|
-
]
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export interface Peer {
|
|
285
|
-
peerId: PeerId
|
|
286
|
-
registrar: Registrar
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export function multiaddrConnectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ MultiaddrConnection, MultiaddrConnection ] {
|
|
290
|
-
const [peerBtoPeerA, peerAtoPeerB] = duplexPair<Uint8Array | Uint8ArrayList>()
|
|
291
|
-
|
|
292
|
-
return [
|
|
293
|
-
mockMultiaddrConnection(peerAtoPeerB, b.peerId),
|
|
294
|
-
mockMultiaddrConnection(peerBtoPeerA, a.peerId)
|
|
295
|
-
]
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
export function connectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ Connection, Connection ] {
|
|
299
|
-
const [peerBtoPeerA, peerAtoPeerB] = multiaddrConnectionPair(a, b)
|
|
300
|
-
|
|
301
|
-
return [
|
|
302
|
-
mockConnection(peerBtoPeerA, {
|
|
303
|
-
registrar: a.registrar
|
|
304
|
-
}),
|
|
305
|
-
mockConnection(peerAtoPeerB, {
|
|
306
|
-
registrar: b.registrar
|
|
307
|
-
})
|
|
308
|
-
]
|
|
309
|
-
}
|
package/src/mocks/duplex.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
2
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
3
|
-
|
|
4
|
-
export function mockDuplex (): Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array | Uint8ArrayList>, Promise<void>> {
|
|
5
|
-
return {
|
|
6
|
-
source: (async function * () {
|
|
7
|
-
yield * []
|
|
8
|
-
}()),
|
|
9
|
-
sink: async () => {}
|
|
10
|
-
}
|
|
11
|
-
}
|
package/src/mocks/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export { mockConnectionManager, mockNetwork } from './connection-manager.js'
|
|
2
|
-
export { mockConnection, mockStream, streamPair, connectionPair } from './connection.js'
|
|
3
|
-
export { mockMultiaddrConnection, mockMultiaddrConnPair } from './multiaddr-connection.js'
|
|
4
|
-
export { mockMuxer } from './muxer.js'
|
|
5
|
-
export { mockRegistrar } from './registrar.js'
|
|
6
|
-
export { mockUpgrader } from './upgrader.js'
|
|
7
|
-
export { mockDuplex } from './duplex.js'
|
|
8
|
-
export type { MockUpgraderInit } from './upgrader.js'
|
|
9
|
-
export type { MockNetworkComponents, MockConnectionManagerComponents, MockNetwork } from './connection-manager.js'
|
|
10
|
-
export type { MockConnectionOptions, StreamInit, StreamPairInit } from './connection.js'
|
|
11
|
-
export type { MockMultiaddrConnPairOptions } from './multiaddr-connection.js'
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { multiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import { abortableSource } from 'abortable-iterator'
|
|
4
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
5
|
-
import type { MultiaddrConnection, PeerId } from '@libp2p/interface'
|
|
6
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
-
import type { Duplex } from 'it-stream-types'
|
|
8
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
|
-
|
|
10
|
-
export function mockMultiaddrConnection (source: Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> & Partial<MultiaddrConnection>, peerId: PeerId): MultiaddrConnection {
|
|
11
|
-
const maConn: MultiaddrConnection = {
|
|
12
|
-
async close () {
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
abort: () => {},
|
|
16
|
-
timeline: {
|
|
17
|
-
open: Date.now()
|
|
18
|
-
},
|
|
19
|
-
remoteAddr: multiaddr(`/ip4/127.0.0.1/tcp/4001/p2p/${peerId.toString()}`),
|
|
20
|
-
log: logger('mock-maconn'),
|
|
21
|
-
...source
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return maConn
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MockMultiaddrConnPairOptions {
|
|
28
|
-
addrs: Multiaddr[]
|
|
29
|
-
remotePeer: PeerId
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Returns both sides of a mocked MultiaddrConnection
|
|
34
|
-
*/
|
|
35
|
-
export function mockMultiaddrConnPair (opts: MockMultiaddrConnPairOptions): { inbound: MultiaddrConnection, outbound: MultiaddrConnection } {
|
|
36
|
-
const { addrs, remotePeer } = opts
|
|
37
|
-
const controller = new AbortController()
|
|
38
|
-
const [localAddr, remoteAddr] = addrs
|
|
39
|
-
const [inboundStream, outboundStream] = duplexPair<Uint8Array | Uint8ArrayList>()
|
|
40
|
-
|
|
41
|
-
const outbound: MultiaddrConnection = {
|
|
42
|
-
...outboundStream,
|
|
43
|
-
remoteAddr: remoteAddr.toString().includes(`/p2p/${remotePeer.toString()}`) ? remoteAddr : remoteAddr.encapsulate(`/p2p/${remotePeer.toString()}`),
|
|
44
|
-
timeline: {
|
|
45
|
-
open: Date.now()
|
|
46
|
-
},
|
|
47
|
-
close: async () => {
|
|
48
|
-
outbound.timeline.close = Date.now()
|
|
49
|
-
controller.abort()
|
|
50
|
-
},
|
|
51
|
-
abort: (err: Error) => {
|
|
52
|
-
outbound.timeline.close = Date.now()
|
|
53
|
-
controller.abort(err)
|
|
54
|
-
},
|
|
55
|
-
log: logger('mock-maconn-outbound')
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const inbound: MultiaddrConnection = {
|
|
59
|
-
...inboundStream,
|
|
60
|
-
remoteAddr: localAddr,
|
|
61
|
-
timeline: {
|
|
62
|
-
open: Date.now()
|
|
63
|
-
},
|
|
64
|
-
close: async () => {
|
|
65
|
-
inbound.timeline.close = Date.now()
|
|
66
|
-
controller.abort()
|
|
67
|
-
},
|
|
68
|
-
abort: (err: Error) => {
|
|
69
|
-
outbound.timeline.close = Date.now()
|
|
70
|
-
controller.abort(err)
|
|
71
|
-
},
|
|
72
|
-
log: logger('mock-maconn-inbound')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Make the sources abortable so we can close them easily
|
|
76
|
-
inbound.source = abortableSource(inbound.source, controller.signal)
|
|
77
|
-
outbound.source = abortableSource(outbound.source, controller.signal)
|
|
78
|
-
|
|
79
|
-
return { inbound, outbound }
|
|
80
|
-
}
|