@libp2p/interface-compliance-tests 3.0.7 → 4.0.0-e66f4891
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/README.md +12 -3
- package/dist/src/connection/index.d.ts +5 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +150 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection-encryption/index.d.ts +5 -0
- package/dist/src/connection-encryption/index.d.ts.map +1 -0
- package/dist/src/connection-encryption/index.js +71 -0
- package/dist/src/connection-encryption/index.js.map +1 -0
- package/dist/src/connection-encryption/utils/index.d.ts +3 -0
- package/dist/src/connection-encryption/utils/index.d.ts.map +1 -0
- package/dist/src/connection-encryption/utils/index.js +19 -0
- package/dist/src/connection-encryption/utils/index.js.map +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/is-valid-tick.d.ts.map +1 -1
- package/dist/src/is-valid-tick.js.map +1 -1
- package/dist/src/mocks/connection-encrypter.d.ts +3 -0
- package/dist/src/mocks/connection-encrypter.d.ts.map +1 -0
- package/dist/src/mocks/connection-encrypter.js +98 -0
- package/dist/src/mocks/connection-encrypter.js.map +1 -0
- package/dist/src/mocks/connection-gater.d.ts +3 -0
- package/dist/src/mocks/connection-gater.d.ts.map +1 -0
- package/dist/src/mocks/connection-gater.js +17 -0
- package/dist/src/mocks/connection-gater.js.map +1 -0
- package/dist/src/mocks/connection-manager.d.ts +29 -0
- package/dist/src/mocks/connection-manager.d.ts.map +1 -0
- package/dist/src/mocks/connection-manager.js +145 -0
- package/dist/src/mocks/connection-manager.js.map +1 -0
- package/dist/src/mocks/connection.d.ts +32 -0
- package/dist/src/mocks/connection.d.ts.map +1 -0
- package/dist/src/mocks/connection.js +167 -0
- package/dist/src/mocks/connection.js.map +1 -0
- package/dist/src/mocks/duplex.d.ts +3 -0
- package/dist/src/mocks/duplex.d.ts.map +1 -0
- package/dist/src/mocks/duplex.js +9 -0
- package/dist/src/mocks/duplex.js.map +1 -0
- package/dist/src/mocks/index.d.ts +13 -0
- package/dist/src/mocks/index.d.ts.map +1 -0
- package/dist/src/mocks/index.js +11 -0
- package/dist/src/mocks/index.js.map +1 -0
- package/dist/src/mocks/metrics.d.ts +3 -0
- package/dist/src/mocks/metrics.d.ts.map +1 -0
- package/dist/src/mocks/metrics.js +115 -0
- package/dist/src/mocks/metrics.js.map +1 -0
- package/dist/src/mocks/multiaddr-connection.d.ts +17 -0
- package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -0
- package/dist/src/mocks/multiaddr-connection.js +60 -0
- package/dist/src/mocks/multiaddr-connection.js.map +1 -0
- package/dist/src/mocks/muxer.d.ts +36 -0
- package/dist/src/mocks/muxer.d.ts.map +1 -0
- package/dist/src/mocks/muxer.js +213 -0
- package/dist/src/mocks/muxer.js.map +1 -0
- package/dist/src/mocks/peer-discovery.d.ts +22 -0
- package/dist/src/mocks/peer-discovery.d.ts.map +1 -0
- package/dist/src/mocks/peer-discovery.js +47 -0
- package/dist/src/mocks/peer-discovery.js.map +1 -0
- package/dist/src/mocks/registrar.d.ts +18 -0
- package/dist/src/mocks/registrar.d.ts.map +1 -0
- package/dist/src/mocks/registrar.js +66 -0
- package/dist/src/mocks/registrar.js.map +1 -0
- package/dist/src/mocks/upgrader.d.ts +10 -0
- package/dist/src/mocks/upgrader.d.ts.map +1 -0
- package/dist/src/mocks/upgrader.js +31 -0
- package/dist/src/mocks/upgrader.js.map +1 -0
- package/dist/src/peer-discovery/index.d.ts +5 -0
- package/dist/src/peer-discovery/index.d.ts.map +1 -0
- package/dist/src/peer-discovery/index.js +66 -0
- package/dist/src/peer-discovery/index.js.map +1 -0
- package/dist/src/pubsub/api.d.ts +6 -0
- package/dist/src/pubsub/api.d.ts.map +1 -0
- package/dist/src/pubsub/api.js +87 -0
- package/dist/src/pubsub/api.js.map +1 -0
- package/dist/src/pubsub/connection-handlers.d.ts +6 -0
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -0
- package/dist/src/pubsub/connection-handlers.js +329 -0
- package/dist/src/pubsub/connection-handlers.js.map +1 -0
- package/dist/src/pubsub/emit-self.d.ts +6 -0
- package/dist/src/pubsub/emit-self.d.ts.map +1 -0
- package/dist/src/pubsub/emit-self.js +80 -0
- package/dist/src/pubsub/emit-self.js.map +1 -0
- package/dist/src/pubsub/index.d.ts +18 -0
- package/dist/src/pubsub/index.d.ts.map +1 -0
- package/dist/src/pubsub/index.js +17 -0
- package/dist/src/pubsub/index.js.map +1 -0
- package/dist/src/pubsub/messages.d.ts +6 -0
- package/dist/src/pubsub/messages.d.ts.map +1 -0
- package/dist/src/pubsub/messages.js +48 -0
- package/dist/src/pubsub/messages.js.map +1 -0
- package/dist/src/pubsub/multiple-nodes.d.ts +6 -0
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -0
- package/dist/src/pubsub/multiple-nodes.js +350 -0
- package/dist/src/pubsub/multiple-nodes.js.map +1 -0
- package/dist/src/pubsub/two-nodes.d.ts +6 -0
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -0
- package/dist/src/pubsub/two-nodes.js +217 -0
- package/dist/src/pubsub/two-nodes.js.map +1 -0
- package/dist/src/pubsub/utils.d.ts +6 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -0
- package/dist/src/pubsub/utils.js +22 -0
- package/dist/src/pubsub/utils.js.map +1 -0
- package/dist/src/stream-muxer/base-test.d.ts +5 -0
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/base-test.js +153 -0
- package/dist/src/stream-muxer/base-test.js.map +1 -0
- package/dist/src/stream-muxer/close-test.d.ts +5 -0
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/close-test.js +357 -0
- package/dist/src/stream-muxer/close-test.js.map +1 -0
- package/dist/src/stream-muxer/fixtures/pb/message.d.ts +13 -0
- package/dist/src/stream-muxer/fixtures/pb/message.d.ts.map +1 -0
- package/dist/src/stream-muxer/fixtures/pb/message.js +67 -0
- package/dist/src/stream-muxer/fixtures/pb/message.js.map +1 -0
- package/dist/src/stream-muxer/index.d.ts +5 -0
- package/dist/src/stream-muxer/index.d.ts.map +1 -0
- package/dist/src/stream-muxer/index.js +13 -0
- package/dist/src/stream-muxer/index.js.map +1 -0
- package/dist/src/stream-muxer/mega-stress-test.d.ts +5 -0
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/mega-stress-test.js +11 -0
- package/dist/src/stream-muxer/mega-stress-test.js.map +1 -0
- package/dist/src/stream-muxer/spawner.d.ts +4 -0
- package/dist/src/stream-muxer/spawner.d.ts.map +1 -0
- package/dist/src/stream-muxer/spawner.js +37 -0
- package/dist/src/stream-muxer/spawner.js.map +1 -0
- package/dist/src/stream-muxer/stress-test.d.ts +5 -0
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/stress-test.js +23 -0
- package/dist/src/stream-muxer/stress-test.js.map +1 -0
- package/dist/src/transport/dial-test.d.ts +5 -0
- package/dist/src/transport/dial-test.d.ts.map +1 -0
- package/dist/src/transport/dial-test.js +98 -0
- package/dist/src/transport/dial-test.js.map +1 -0
- package/dist/src/transport/filter-test.d.ts +5 -0
- package/dist/src/transport/filter-test.d.ts.map +1 -0
- package/dist/src/transport/filter-test.js +18 -0
- package/dist/src/transport/filter-test.js.map +1 -0
- package/dist/src/transport/index.d.ts +15 -0
- package/dist/src/transport/index.d.ts.map +1 -0
- package/dist/src/transport/index.js +11 -0
- package/dist/src/transport/index.js.map +1 -0
- package/dist/src/transport/listen-test.d.ts +5 -0
- package/dist/src/transport/listen-test.d.ts.map +1 -0
- package/dist/src/transport/listen-test.js +152 -0
- package/dist/src/transport/listen-test.js.map +1 -0
- package/package.json +74 -97
- package/src/connection/index.ts +182 -0
- package/src/connection-encryption/index.ts +97 -0
- package/src/connection-encryption/utils/index.ts +24 -0
- package/src/index.ts +0 -1
- package/src/is-valid-tick.ts +0 -1
- package/src/mocks/connection-encrypter.ts +113 -0
- package/src/mocks/connection-gater.ts +18 -0
- package/src/mocks/connection-manager.ts +211 -0
- package/src/mocks/connection.ts +226 -0
- package/src/mocks/duplex.ts +10 -0
- package/src/mocks/index.ts +12 -0
- package/src/mocks/metrics.ts +162 -0
- package/src/mocks/multiaddr-connection.ts +76 -0
- package/src/mocks/muxer.ts +303 -0
- package/src/mocks/peer-discovery.ts +60 -0
- package/src/mocks/registrar.ts +88 -0
- package/src/mocks/upgrader.ts +49 -0
- package/src/peer-discovery/index.ts +90 -0
- package/src/pubsub/api.ts +114 -0
- package/src/pubsub/connection-handlers.ts +413 -0
- package/src/pubsub/emit-self.ts +99 -0
- package/src/pubsub/index.ts +34 -0
- package/src/pubsub/messages.ts +59 -0
- package/src/pubsub/multiple-nodes.ts +440 -0
- package/src/pubsub/two-nodes.ts +273 -0
- package/src/pubsub/utils.ts +29 -0
- package/src/stream-muxer/base-test.ts +196 -0
- package/src/stream-muxer/close-test.ts +442 -0
- package/src/stream-muxer/fixtures/pb/message.proto +7 -0
- package/src/stream-muxer/fixtures/pb/message.ts +87 -0
- package/src/stream-muxer/index.ts +15 -0
- package/src/stream-muxer/mega-stress-test.ts +14 -0
- package/src/stream-muxer/spawner.ts +55 -0
- package/src/stream-muxer/stress-test.ts +27 -0
- package/src/transport/dial-test.ts +124 -0
- package/src/transport/filter-test.ts +25 -0
- package/src/transport/index.ts +25 -0
- package/src/transport/listen-test.ts +191 -0
- package/dist/typedoc-urls.json +0 -3
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { logger } from '@libp2p/logger'
|
|
3
|
+
import * as mss from '@libp2p/multistream-select'
|
|
4
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
|
+
import { duplexPair } from 'it-pair/duplex'
|
|
6
|
+
import { pipe } from 'it-pipe'
|
|
7
|
+
import { mockMultiaddrConnection } from './multiaddr-connection.js'
|
|
8
|
+
import { mockMuxer } from './muxer.js'
|
|
9
|
+
import { mockRegistrar } from './registrar.js'
|
|
10
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
11
|
+
import type { MultiaddrConnection, Connection, Stream, Direction, ConnectionTimeline, ConnectionStatus } from '@libp2p/interface/connection'
|
|
12
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
13
|
+
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
|
|
14
|
+
import type { Registrar } from '@libp2p/interface-internal/registrar'
|
|
15
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
17
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
18
|
+
|
|
19
|
+
const log = logger('libp2p:mock-connection')
|
|
20
|
+
|
|
21
|
+
export interface MockConnectionOptions {
|
|
22
|
+
direction?: Direction
|
|
23
|
+
registrar?: Registrar
|
|
24
|
+
muxerFactory?: StreamMuxerFactory
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface MockConnectionInit {
|
|
28
|
+
remoteAddr: Multiaddr
|
|
29
|
+
remotePeer: PeerId
|
|
30
|
+
direction: Direction
|
|
31
|
+
maConn: MultiaddrConnection
|
|
32
|
+
muxer: StreamMuxer
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class MockConnection implements Connection {
|
|
36
|
+
public id: string
|
|
37
|
+
public remoteAddr: Multiaddr
|
|
38
|
+
public remotePeer: PeerId
|
|
39
|
+
public direction: Direction
|
|
40
|
+
public timeline: ConnectionTimeline
|
|
41
|
+
public multiplexer?: string
|
|
42
|
+
public encryption?: string
|
|
43
|
+
public status: ConnectionStatus
|
|
44
|
+
public streams: Stream[]
|
|
45
|
+
public tags: string[]
|
|
46
|
+
public transient: boolean
|
|
47
|
+
|
|
48
|
+
private readonly muxer: StreamMuxer
|
|
49
|
+
private readonly maConn: MultiaddrConnection
|
|
50
|
+
|
|
51
|
+
constructor (init: MockConnectionInit) {
|
|
52
|
+
const { remoteAddr, remotePeer, direction, maConn, muxer } = 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.transient = false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async newStream (protocols: string | string[], options?: AbortOptions): Promise<Stream> {
|
|
71
|
+
if (!Array.isArray(protocols)) {
|
|
72
|
+
protocols = [protocols]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (protocols.length === 0) {
|
|
76
|
+
throw new Error('protocols must have a length')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (this.status !== 'open') {
|
|
80
|
+
throw new CodeError('connection must be open to create streams', 'ERR_CONNECTION_CLOSED')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const id = `${Math.random()}`
|
|
84
|
+
const stream = await this.muxer.newStream(id)
|
|
85
|
+
const result = await mss.select(stream, protocols, options)
|
|
86
|
+
|
|
87
|
+
stream.protocol = result.protocol
|
|
88
|
+
stream.direction = 'outbound'
|
|
89
|
+
stream.sink = result.stream.sink
|
|
90
|
+
stream.source = result.stream.source
|
|
91
|
+
|
|
92
|
+
this.streams.push(stream)
|
|
93
|
+
|
|
94
|
+
return stream
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
addStream (stream: Stream): void {
|
|
98
|
+
this.streams.push(stream)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
removeStream (id: string): void {
|
|
102
|
+
this.streams = this.streams.filter(stream => stream.id !== id)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async close (options?: AbortOptions): Promise<void> {
|
|
106
|
+
this.status = 'closing'
|
|
107
|
+
await Promise.all(
|
|
108
|
+
this.streams.map(async s => s.close(options))
|
|
109
|
+
)
|
|
110
|
+
await this.maConn.close()
|
|
111
|
+
this.status = 'closed'
|
|
112
|
+
this.timeline.close = Date.now()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
abort (err: Error): void {
|
|
116
|
+
this.status = 'closing'
|
|
117
|
+
this.streams.forEach(s => {
|
|
118
|
+
s.abort(err)
|
|
119
|
+
})
|
|
120
|
+
this.maConn.abort(err)
|
|
121
|
+
this.status = 'closed'
|
|
122
|
+
this.timeline.close = Date.now()
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectionOptions = {}): Connection {
|
|
127
|
+
const remoteAddr = maConn.remoteAddr
|
|
128
|
+
const remotePeerIdStr = remoteAddr.getPeerId() ?? '12D3KooWCrhmFM1BCPGBkNzbPfDk4cjYmtAYSpZwUBC69Qg2kZyq'
|
|
129
|
+
|
|
130
|
+
if (remotePeerIdStr == null) {
|
|
131
|
+
throw new Error('Remote multiaddr must contain a peer id')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const remotePeer = peerIdFromString(remotePeerIdStr)
|
|
135
|
+
const direction = opts.direction ?? 'inbound'
|
|
136
|
+
const registrar = opts.registrar ?? mockRegistrar()
|
|
137
|
+
const muxerFactory = opts.muxerFactory ?? mockMuxer()
|
|
138
|
+
|
|
139
|
+
const muxer = muxerFactory.createStreamMuxer({
|
|
140
|
+
direction,
|
|
141
|
+
onIncomingStream: (muxedStream) => {
|
|
142
|
+
try {
|
|
143
|
+
mss.handle(muxedStream, registrar.getProtocols())
|
|
144
|
+
.then(({ stream, protocol }) => {
|
|
145
|
+
log('%s: incoming stream opened on %s', direction, protocol)
|
|
146
|
+
muxedStream.protocol = protocol
|
|
147
|
+
muxedStream.sink = stream.sink
|
|
148
|
+
muxedStream.source = stream.source
|
|
149
|
+
|
|
150
|
+
connection.addStream(muxedStream)
|
|
151
|
+
const { handler } = registrar.getHandler(protocol)
|
|
152
|
+
|
|
153
|
+
handler({ connection, stream: muxedStream })
|
|
154
|
+
}).catch(err => {
|
|
155
|
+
log.error(err)
|
|
156
|
+
})
|
|
157
|
+
} catch (err: any) {
|
|
158
|
+
log.error(err)
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
onStreamEnd: (muxedStream) => {
|
|
162
|
+
connection.removeStream(muxedStream.id)
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
void pipe(
|
|
167
|
+
maConn, muxer, maConn
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
const connection = new MockConnection({
|
|
171
|
+
remoteAddr,
|
|
172
|
+
remotePeer,
|
|
173
|
+
direction,
|
|
174
|
+
maConn,
|
|
175
|
+
muxer
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
return connection
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>): Stream {
|
|
182
|
+
return {
|
|
183
|
+
...stream,
|
|
184
|
+
close: async () => {},
|
|
185
|
+
closeRead: async () => {},
|
|
186
|
+
closeWrite: async () => {},
|
|
187
|
+
abort: () => {},
|
|
188
|
+
direction: 'outbound',
|
|
189
|
+
protocol: '/foo/1.0.0',
|
|
190
|
+
timeline: {
|
|
191
|
+
open: Date.now()
|
|
192
|
+
},
|
|
193
|
+
metadata: {},
|
|
194
|
+
id: `stream-${Date.now()}`,
|
|
195
|
+
status: 'open',
|
|
196
|
+
readStatus: 'ready',
|
|
197
|
+
writeStatus: 'ready'
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface Peer {
|
|
202
|
+
peerId: PeerId
|
|
203
|
+
registrar: Registrar
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function multiaddrConnectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ MultiaddrConnection, MultiaddrConnection ] {
|
|
207
|
+
const [peerBtoPeerA, peerAtoPeerB] = duplexPair<Uint8Array>()
|
|
208
|
+
|
|
209
|
+
return [
|
|
210
|
+
mockMultiaddrConnection(peerAtoPeerB, b.peerId),
|
|
211
|
+
mockMultiaddrConnection(peerBtoPeerA, a.peerId)
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function connectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ Connection, Connection ] {
|
|
216
|
+
const [peerBtoPeerA, peerAtoPeerB] = multiaddrConnectionPair(a, b)
|
|
217
|
+
|
|
218
|
+
return [
|
|
219
|
+
mockConnection(peerBtoPeerA, {
|
|
220
|
+
registrar: a.registrar
|
|
221
|
+
}),
|
|
222
|
+
mockConnection(peerAtoPeerB, {
|
|
223
|
+
registrar: b.registrar
|
|
224
|
+
})
|
|
225
|
+
]
|
|
226
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { mockConnectionEncrypter } from './connection-encrypter.js'
|
|
2
|
+
export { mockConnectionGater } from './connection-gater.js'
|
|
3
|
+
export { mockConnectionManager, mockNetwork } from './connection-manager.js'
|
|
4
|
+
export { mockConnection, mockStream, connectionPair } from './connection.js'
|
|
5
|
+
export { mockMultiaddrConnection, mockMultiaddrConnPair } from './multiaddr-connection.js'
|
|
6
|
+
export { mockMuxer } from './muxer.js'
|
|
7
|
+
export { mockRegistrar } from './registrar.js'
|
|
8
|
+
export { mockUpgrader } from './upgrader.js'
|
|
9
|
+
export { mockDuplex } from './duplex.js'
|
|
10
|
+
export { mockMetrics } from './metrics.js'
|
|
11
|
+
export type { MockUpgraderInit } from './upgrader.js'
|
|
12
|
+
export type { MockNetworkComponents } from './connection-manager.js'
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface/connection'
|
|
2
|
+
import type { Metric, MetricGroup, StopTimer, Metrics, CalculatedMetricOptions, MetricOptions } from '@libp2p/interface/metrics'
|
|
3
|
+
|
|
4
|
+
class DefaultMetric implements Metric {
|
|
5
|
+
public value: number = 0
|
|
6
|
+
|
|
7
|
+
update (value: number): void {
|
|
8
|
+
this.value = value
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
increment (value: number = 1): void {
|
|
12
|
+
this.value += value
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
decrement (value: number = 1): void {
|
|
16
|
+
this.value -= value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
reset (): void {
|
|
20
|
+
this.value = 0
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
timer (): StopTimer {
|
|
24
|
+
const start = Date.now()
|
|
25
|
+
|
|
26
|
+
return () => {
|
|
27
|
+
this.value = Date.now() - start
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class DefaultGroupMetric implements MetricGroup {
|
|
33
|
+
public values: Record<string, number> = {}
|
|
34
|
+
|
|
35
|
+
update (values: Record<string, number>): void {
|
|
36
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
37
|
+
this.values[key] = value
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
increment (values: Record<string, number | unknown>): void {
|
|
42
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
43
|
+
this.values[key] = this.values[key] ?? 0
|
|
44
|
+
const inc = typeof value === 'number' ? value : 1
|
|
45
|
+
|
|
46
|
+
this.values[key] += Number(inc)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
decrement (values: Record<string, number | unknown>): void {
|
|
51
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
52
|
+
this.values[key] = this.values[key] ?? 0
|
|
53
|
+
const dec = typeof value === 'number' ? value : 1
|
|
54
|
+
|
|
55
|
+
this.values[key] -= Number(dec)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
reset (): void {
|
|
60
|
+
this.values = {}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
timer (key: string): StopTimer {
|
|
64
|
+
const start = Date.now()
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
this.values[key] = Date.now() - start
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class MockMetrics implements Metrics {
|
|
73
|
+
public metrics = new Map<string, any>()
|
|
74
|
+
|
|
75
|
+
trackMultiaddrConnection (maConn: MultiaddrConnection): void {
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
trackProtocolStream (stream: Stream, connection: Connection): void {
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
registerMetric (name: string, opts: CalculatedMetricOptions): void
|
|
84
|
+
registerMetric (name: string, opts?: MetricOptions): Metric
|
|
85
|
+
registerMetric (name: string, opts: any): any {
|
|
86
|
+
if (name == null ?? name.trim() === '') {
|
|
87
|
+
throw new Error('Metric name is required')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (opts?.calculate != null) {
|
|
91
|
+
// calculated metric
|
|
92
|
+
this.metrics.set(name, opts.calculate)
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const metric = new DefaultMetric()
|
|
97
|
+
this.metrics.set(name, metric)
|
|
98
|
+
|
|
99
|
+
return metric
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
registerCounter (name: string, opts: CalculatedMetricOptions): void
|
|
103
|
+
registerCounter (name: string, opts?: MetricOptions): Metric
|
|
104
|
+
registerCounter (name: string, opts: any): any {
|
|
105
|
+
if (name == null ?? name.trim() === '') {
|
|
106
|
+
throw new Error('Metric name is required')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (opts?.calculate != null) {
|
|
110
|
+
// calculated metric
|
|
111
|
+
this.metrics.set(name, opts.calculate)
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const metric = new DefaultMetric()
|
|
116
|
+
this.metrics.set(name, metric)
|
|
117
|
+
|
|
118
|
+
return metric
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
registerMetricGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
|
|
122
|
+
registerMetricGroup (name: string, opts?: MetricOptions): MetricGroup
|
|
123
|
+
registerMetricGroup (name: string, opts: any): any {
|
|
124
|
+
if (name == null ?? name.trim() === '') {
|
|
125
|
+
throw new Error('Metric name is required')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (opts?.calculate != null) {
|
|
129
|
+
// calculated metric
|
|
130
|
+
this.metrics.set(name, opts.calculate)
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const metric = new DefaultGroupMetric()
|
|
135
|
+
this.metrics.set(name, metric)
|
|
136
|
+
|
|
137
|
+
return metric
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
registerCounterGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
|
|
141
|
+
registerCounterGroup (name: string, opts?: MetricOptions): MetricGroup
|
|
142
|
+
registerCounterGroup (name: string, opts: any): any {
|
|
143
|
+
if (name == null ?? name.trim() === '') {
|
|
144
|
+
throw new Error('Metric name is required')
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (opts?.calculate != null) {
|
|
148
|
+
// calculated metric
|
|
149
|
+
this.metrics.set(name, opts.calculate)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const metric = new DefaultGroupMetric()
|
|
154
|
+
this.metrics.set(name, metric)
|
|
155
|
+
|
|
156
|
+
return metric
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function mockMetrics (): () => Metrics {
|
|
161
|
+
return () => new MockMetrics()
|
|
162
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
2
|
+
import { abortableSource } from 'abortable-iterator'
|
|
3
|
+
import { duplexPair } from 'it-pair/duplex'
|
|
4
|
+
import type { MultiaddrConnection } from '@libp2p/interface/connection'
|
|
5
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
6
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
+
import type { Duplex } from 'it-stream-types'
|
|
8
|
+
|
|
9
|
+
export function mockMultiaddrConnection (source: Duplex<AsyncGenerator<Uint8Array>> & Partial<MultiaddrConnection>, peerId: PeerId): MultiaddrConnection {
|
|
10
|
+
const maConn: MultiaddrConnection = {
|
|
11
|
+
async close () {
|
|
12
|
+
|
|
13
|
+
},
|
|
14
|
+
abort: () => {},
|
|
15
|
+
timeline: {
|
|
16
|
+
open: Date.now()
|
|
17
|
+
},
|
|
18
|
+
remoteAddr: multiaddr(`/ip4/127.0.0.1/tcp/4001/p2p/${peerId.toString()}`),
|
|
19
|
+
...source
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return maConn
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface MockMultiaddrConnPairOptions {
|
|
26
|
+
addrs: Multiaddr[]
|
|
27
|
+
remotePeer: PeerId
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns both sides of a mocked MultiaddrConnection
|
|
32
|
+
*/
|
|
33
|
+
export function mockMultiaddrConnPair (opts: MockMultiaddrConnPairOptions): { inbound: MultiaddrConnection, outbound: MultiaddrConnection } {
|
|
34
|
+
const { addrs, remotePeer } = opts
|
|
35
|
+
const controller = new AbortController()
|
|
36
|
+
const [localAddr, remoteAddr] = addrs
|
|
37
|
+
const [inboundStream, outboundStream] = duplexPair<Uint8Array>()
|
|
38
|
+
|
|
39
|
+
const outbound: MultiaddrConnection = {
|
|
40
|
+
...outboundStream,
|
|
41
|
+
remoteAddr: remoteAddr.toString().includes(`/p2p/${remotePeer.toString()}`) ? remoteAddr : remoteAddr.encapsulate(`/p2p/${remotePeer.toString()}`),
|
|
42
|
+
timeline: {
|
|
43
|
+
open: Date.now()
|
|
44
|
+
},
|
|
45
|
+
close: async () => {
|
|
46
|
+
outbound.timeline.close = Date.now()
|
|
47
|
+
controller.abort()
|
|
48
|
+
},
|
|
49
|
+
abort: (err: Error) => {
|
|
50
|
+
outbound.timeline.close = Date.now()
|
|
51
|
+
controller.abort(err)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const inbound: MultiaddrConnection = {
|
|
56
|
+
...inboundStream,
|
|
57
|
+
remoteAddr: localAddr,
|
|
58
|
+
timeline: {
|
|
59
|
+
open: Date.now()
|
|
60
|
+
},
|
|
61
|
+
close: async () => {
|
|
62
|
+
inbound.timeline.close = Date.now()
|
|
63
|
+
controller.abort()
|
|
64
|
+
},
|
|
65
|
+
abort: (err: Error) => {
|
|
66
|
+
outbound.timeline.close = Date.now()
|
|
67
|
+
controller.abort(err)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Make the sources abortable so we can close them easily
|
|
72
|
+
inbound.source = abortableSource(inbound.source, controller.signal)
|
|
73
|
+
outbound.source = abortableSource(outbound.source, controller.signal)
|
|
74
|
+
|
|
75
|
+
return { inbound, outbound }
|
|
76
|
+
}
|