@libp2p/interface-compliance-tests 1.1.6 → 1.1.7

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.
Files changed (43) hide show
  1. package/dist/src/mocks/connection.d.ts +2 -1
  2. package/dist/src/mocks/connection.d.ts.map +1 -1
  3. package/dist/src/mocks/connection.js +18 -12
  4. package/dist/src/mocks/connection.js.map +1 -1
  5. package/dist/src/mocks/muxer.d.ts +2 -2
  6. package/dist/src/mocks/muxer.d.ts.map +1 -1
  7. package/dist/src/mocks/muxer.js +175 -26
  8. package/dist/src/mocks/muxer.js.map +1 -1
  9. package/dist/src/mocks/upgrader.d.ts.map +1 -1
  10. package/dist/src/mocks/upgrader.js +2 -4
  11. package/dist/src/mocks/upgrader.js.map +1 -1
  12. package/dist/src/stream-muxer/base-test.js +1 -1
  13. package/dist/src/stream-muxer/base-test.js.map +1 -1
  14. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  15. package/dist/src/stream-muxer/close-test.js +7 -8
  16. package/dist/src/stream-muxer/close-test.js.map +1 -1
  17. package/dist/src/transport/dial-test.d.ts.map +1 -1
  18. package/dist/src/transport/dial-test.js +2 -1
  19. package/dist/src/transport/dial-test.js.map +1 -1
  20. package/dist/src/transport/filter-test.js +1 -1
  21. package/dist/src/transport/filter-test.js.map +1 -1
  22. package/dist/src/transport/listen-test.d.ts.map +1 -1
  23. package/dist/src/transport/listen-test.js +2 -1
  24. package/dist/src/transport/listen-test.js.map +1 -1
  25. package/dist/src/utils/is-valid-tick.d.ts +6 -0
  26. package/dist/src/utils/is-valid-tick.d.ts.map +1 -0
  27. package/dist/src/utils/is-valid-tick.js +15 -0
  28. package/dist/src/utils/is-valid-tick.js.map +1 -0
  29. package/package.json +1 -1
  30. package/src/mocks/connection.ts +27 -20
  31. package/src/mocks/muxer.ts +230 -28
  32. package/src/mocks/upgrader.ts +2 -5
  33. package/src/stream-muxer/base-test.ts +1 -1
  34. package/src/stream-muxer/close-test.ts +8 -10
  35. package/src/transport/dial-test.ts +2 -1
  36. package/src/transport/filter-test.ts +1 -1
  37. package/src/transport/listen-test.ts +2 -1
  38. package/src/utils/is-valid-tick.ts +18 -0
  39. package/dist/src/transport/utils/index.d.ts +0 -15
  40. package/dist/src/transport/utils/index.d.ts.map +0 -1
  41. package/dist/src/transport/utils/index.js +0 -137
  42. package/dist/src/transport/utils/index.js.map +0 -1
  43. package/src/transport/utils/index.ts +0 -172
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface-compliance-tests",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Compliance tests for JS libp2p interfaces",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interface-compliance-tests#readme",
@@ -7,17 +7,21 @@ import type { Connection, Stream, Metadata, ProtocolStream } from '@libp2p/inter
7
7
  import type { Muxer } from '@libp2p/interfaces/stream-muxer'
8
8
  import type { Duplex } from 'it-stream-types'
9
9
  import { mockMuxer } from './muxer.js'
10
+ import type { PeerId } from '@libp2p/interfaces/src/peer-id'
11
+ import { mockMultiaddrConnection } from './multiaddr-connection.js'
12
+ import { Multiaddr } from '@multiformats/multiaddr'
10
13
 
11
- export async function mockConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound' = 'inbound', muxer: Muxer = mockMuxer()): Promise<Connection> {
14
+ export async function mockConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound' = 'inbound', muxer?: Muxer): Promise<Connection> {
12
15
  const remoteAddr = maConn.remoteAddr
13
16
  const remotePeerIdStr = remoteAddr.getPeerId()
14
17
  const remotePeer = remotePeerIdStr != null ? peerIdFromString(remotePeerIdStr) : await createEd25519PeerId()
15
18
  const registry = new Map()
16
19
  const streams: Stream[] = []
17
20
  let streamId = 0
21
+ const mux = muxer ?? mockMuxer()
18
22
 
19
23
  void pipe(
20
- maConn, muxer, maConn
24
+ maConn, mux, maConn
21
25
  )
22
26
 
23
27
  return {
@@ -44,7 +48,7 @@ export async function mockConnection (maConn: MultiaddrConnection, direction: 'i
44
48
  }
45
49
 
46
50
  const id = `${streamId++}`
47
- const stream: Stream = muxer.newStream(id)
51
+ const stream: Stream = mux.newStream(id)
48
52
  const streamData: ProtocolStream = {
49
53
  protocol: protocols[0],
50
54
  stream
@@ -79,23 +83,26 @@ export function mockStream (stream: Duplex<Uint8Array>): Stream {
79
83
  }
80
84
  }
81
85
 
82
- export function connectionPair (): [ Connection, Connection ] {
86
+ export async function connectionPair (peerA: PeerId, peerB: PeerId): Promise<[ Connection, Connection ]> {
83
87
  const [d0, d1] = duplexPair<Uint8Array>()
84
88
 
85
- return [
86
- // @ts-expect-error not a complete implementation
87
- {
88
- newStream: async (multicodecs: string[]) => await Promise.resolve({
89
- stream: mockStream(d0),
90
- protocol: multicodecs[0]
91
- })
92
- },
93
- // @ts-expect-error not a complete implementation
94
- {
95
- newStream: async (multicodecs: string[]) => await Promise.resolve({
96
- stream: mockStream(d1),
97
- protocol: multicodecs[0]
98
- })
99
- }
100
- ]
89
+ return [{
90
+ ...await mockConnection(mockMultiaddrConnection({
91
+ ...d0,
92
+ remoteAddr: new Multiaddr(`/ip4/127.0.0.1/tcp/4001/p2p/${peerA.toString()}`)
93
+ })),
94
+ newStream: async (multicodecs: string[]) => await Promise.resolve({
95
+ stream: mockStream(d0),
96
+ protocol: multicodecs[0]
97
+ })
98
+ }, {
99
+ ...await mockConnection(mockMultiaddrConnection({
100
+ ...d1,
101
+ remoteAddr: new Multiaddr(`/ip4/127.0.0.1/tcp/4001/p2p/${peerB.toString()}`)
102
+ })),
103
+ newStream: async (multicodecs: string[]) => await Promise.resolve({
104
+ stream: mockStream(d1),
105
+ protocol: multicodecs[0]
106
+ })
107
+ }]
101
108
  }
@@ -1,41 +1,243 @@
1
- import { pair } from 'it-pair'
2
- import { pushable } from 'it-pushable'
3
- import drain from 'it-drain'
1
+ import { Pushable, pushable } from 'it-pushable'
2
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
+ import { abortableSource } from 'abortable-iterator'
4
5
  import type { Stream } from '@libp2p/interfaces/connection'
5
- import type { Muxer } from '@libp2p/interfaces/stream-muxer'
6
+ import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
7
+ import type { Source } from 'it-stream-types'
6
8
 
7
- export function mockMuxer (): Muxer {
9
+ interface DataMessage {
10
+ id: string
11
+ type: 'data'
12
+ chunk: string
13
+ }
14
+
15
+ interface ResetMessage {
16
+ id: string
17
+ type: 'reset'
18
+ }
19
+
20
+ interface CloseMessage {
21
+ id: string
22
+ type: 'close'
23
+ }
24
+
25
+ type StreamMessage = DataMessage | ResetMessage | CloseMessage
26
+
27
+ class MuxedStream {
28
+ public id: string
29
+ public input: Pushable<Uint8Array>
30
+ public stream: Stream
31
+
32
+ private sourceClosed: boolean
33
+ private sinkClosed: boolean
34
+ private readonly controller: AbortController
35
+ private readonly onEnd: () => void
36
+
37
+ constructor (opts: { id: string, push: Pushable<StreamMessage>, onEnd: () => void }) {
38
+ const { id, push, onEnd } = opts
39
+
40
+ this.id = id
41
+ this.controller = new AbortController()
42
+ this.onEnd = onEnd
43
+ this.sourceClosed = false
44
+ this.sinkClosed = false
45
+ this.input = pushable<Uint8Array>({
46
+ onEnd: () => {
47
+ this.sourceClosed = true
48
+ this.maybeEndStream()
49
+ }
50
+ })
51
+ this.stream = {
52
+ id,
53
+ sink: async (source) => {
54
+ source = abortableSource(source, this.controller.signal)
55
+
56
+ try {
57
+ for await (const chunk of source) {
58
+ const dataMsg: DataMessage = {
59
+ id,
60
+ type: 'data',
61
+ chunk: uint8ArrayToString(chunk, 'base64')
62
+ }
63
+
64
+ push.push(dataMsg)
65
+ }
66
+
67
+ const closeMsg: CloseMessage = {
68
+ id,
69
+ type: 'close'
70
+ }
71
+
72
+ push.push(closeMsg)
73
+ } catch (err) {
74
+ if (!this.controller.signal.aborted) {
75
+ throw err
76
+ }
77
+ }
78
+
79
+ this.closeSink()
80
+ },
81
+ source: this.input,
82
+ close: () => {
83
+ const closeMsg: CloseMessage = {
84
+ id,
85
+ type: 'close'
86
+ }
87
+ push.push(closeMsg)
88
+
89
+ this.closeSink()
90
+ this.closeSource()
91
+ },
92
+ abort: () => {
93
+ const resetMsg: ResetMessage = {
94
+ id,
95
+ type: 'reset'
96
+ }
97
+ push.push(resetMsg)
98
+
99
+ this.closeSink()
100
+ this.closeSource()
101
+ },
102
+ reset: () => {
103
+ const resetMsg: ResetMessage = {
104
+ id,
105
+ type: 'reset'
106
+ }
107
+ push.push(resetMsg)
108
+
109
+ this.closeSink()
110
+ this.closeSource()
111
+ },
112
+ timeline: {
113
+ open: Date.now()
114
+ }
115
+ }
116
+ }
117
+
118
+ maybeEndStream () {
119
+ if (this.stream.timeline.close != null) {
120
+ // already ended
121
+ return
122
+ }
123
+
124
+ if (this.sinkClosed && this.sourceClosed) {
125
+ this.stream.timeline.close = Date.now()
126
+ this.onEnd()
127
+ }
128
+ }
129
+
130
+ closeSource () {
131
+ this.sourceClosed = true
132
+ this.input.end()
133
+ }
134
+
135
+ closeSink () {
136
+ this.sinkClosed = true
137
+ this.controller.abort()
138
+ this.maybeEndStream()
139
+ }
140
+ }
141
+
142
+ export function mockMuxer (options?: MuxerOptions): Muxer {
8
143
  let streamId = 0
9
- let streams: Stream[] = []
10
- const p = pushable<Uint8Array>()
144
+ const streams = new Map<string, MuxedStream>()
145
+
146
+ // process incoming messages from the other muxer
147
+ const muxerSource = pushable<Uint8Array>({
148
+ onEnd: () => {
149
+ for (const muxedStream of streams.values()) {
150
+ muxedStream.stream.close()
151
+ }
152
+ }
153
+ })
154
+
155
+ // receives messages from all of the muxed streams
156
+ const push = pushable<StreamMessage>()
157
+ void Promise.resolve().then(async () => {
158
+ for await (const message of push) {
159
+ if (message.type === 'data') {
160
+ muxerSource.push(uint8ArrayFromString(JSON.stringify({
161
+ id: message.id,
162
+ type: message.type,
163
+ chunk: message.chunk
164
+ })))
165
+ } else {
166
+ muxerSource.push(uint8ArrayFromString(JSON.stringify({
167
+ id: message.id,
168
+ type: message.type
169
+ })))
170
+ }
171
+ }
172
+ })
173
+
174
+ function createStream (name?: string): MuxedStream {
175
+ const id = name ?? `${streamId++}`
176
+
177
+ const muxedStream: MuxedStream = new MuxedStream({
178
+ id,
179
+ push,
180
+ onEnd: () => {
181
+ streams.delete(id)
182
+
183
+ if (options?.onStreamEnd != null) {
184
+ options?.onStreamEnd(muxedStream.stream)
185
+ }
186
+ }
187
+ })
188
+
189
+ return muxedStream
190
+ }
11
191
 
12
192
  const muxer: Muxer = {
13
- source: p,
14
- sink: async (source) => {
15
- await drain(source)
193
+ // receive incoming messages
194
+ async sink (source: Source<Uint8Array>) {
195
+ for await (const buf of source) {
196
+ const message: StreamMessage = JSON.parse(uint8ArrayToString(buf))
197
+ let muxedStream = streams.get(message.id)
198
+
199
+ if (muxedStream == null) {
200
+ muxedStream = createStream(message.id)
201
+ streams.set(muxedStream.stream.id, muxedStream)
202
+
203
+ if (options?.onStream != null) {
204
+ options.onStream(muxedStream.stream)
205
+ }
206
+ }
207
+
208
+ if (message.type === 'data') {
209
+ muxedStream.input.push(uint8ArrayFromString(message.chunk, 'base64'))
210
+ } else if (message.type === 'reset') {
211
+ muxedStream.closeSink()
212
+ muxedStream.closeSource()
213
+ } else if (message.type === 'close') {
214
+ muxedStream.closeSource()
215
+ }
216
+ }
217
+
218
+ for (const muxedStream of streams.values()) {
219
+ muxedStream.stream.close()
220
+ }
221
+
222
+ muxerSource.end()
16
223
  },
224
+
225
+ source: muxerSource,
226
+
17
227
  get streams () {
18
- return streams
228
+ return Array.from(streams.values()).map(({ stream }) => stream)
19
229
  },
20
- newStream: (name?: string) => {
21
- const echo = pair<Uint8Array>()
22
-
23
- const id = `${streamId++}`
24
- const stream: Stream = {
25
- id,
26
- sink: echo.sink,
27
- source: echo.source,
28
- close: () => {
29
- streams = streams.filter(s => s !== stream)
30
- },
31
- abort: () => {},
32
- reset: () => {},
33
- timeline: {
34
- open: 0
35
- }
230
+
231
+ newStream (name?: string) {
232
+ const storedStream = createStream(name)
233
+
234
+ streams.set(storedStream.stream.id, storedStream)
235
+
236
+ if (options?.onStream != null) {
237
+ options.onStream(storedStream.stream)
36
238
  }
37
239
 
38
- return stream
240
+ return storedStream.stream
39
241
  }
40
242
  }
41
243
 
@@ -1,5 +1,4 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
- import { mockMuxer } from './muxer.js'
3
2
  import { mockConnection } from './connection.js'
4
3
  import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
5
4
  import type { Muxer } from '@libp2p/interfaces/stream-muxer'
@@ -16,16 +15,14 @@ export function mockUpgrader (options: MockUpgraderOptions = {}) {
16
15
  return multiaddrConnection
17
16
  }
18
17
 
19
- const muxer = options.muxer ?? mockMuxer()
20
-
21
18
  const upgrader: Upgrader = {
22
19
  async upgradeOutbound (multiaddrConnection) {
23
20
  ensureProps(multiaddrConnection)
24
- return await mockConnection(multiaddrConnection, 'outbound', muxer)
21
+ return await mockConnection(multiaddrConnection, 'outbound', options.muxer)
25
22
  },
26
23
  async upgradeInbound (multiaddrConnection) {
27
24
  ensureProps(multiaddrConnection)
28
- return await mockConnection(multiaddrConnection, 'inbound', muxer)
25
+ return await mockConnection(multiaddrConnection, 'inbound', options.muxer)
29
26
  }
30
27
  }
31
28
 
@@ -7,7 +7,7 @@ import all from 'it-all'
7
7
  import defer from 'p-defer'
8
8
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
9
9
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
- import { isValidTick } from '../transport/utils/index.js'
10
+ import { isValidTick } from '../utils/is-valid-tick.js'
11
11
  import type { DeferredPromise } from 'p-defer'
12
12
  import type { TestSetup } from '../index.js'
13
13
  import type { Stream } from '@libp2p/interfaces/connection'
@@ -4,14 +4,12 @@ import { duplexPair } from 'it-pair/duplex'
4
4
  import { abortableSource, abortableDuplex } from 'abortable-iterator'
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
6
  import drain from 'it-drain'
7
- import { mockUpgrader, mockMultiaddrConnection } from '../transport/utils/index.js'
7
+ import { mockUpgrader } from '../mocks/upgrader.js'
8
+ import { mockMultiaddrConnection } from '../mocks/multiaddr-connection.js'
9
+ import { expect } from 'aegir/utils/chai.js'
10
+ import delay from 'delay'
8
11
  import type { TestSetup } from '../index.js'
9
12
  import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
10
- import { expect } from 'aegir/utils/chai.js'
11
-
12
- async function pause (ms: number) {
13
- return await new Promise(resolve => setTimeout(resolve, ms))
14
- }
15
13
 
16
14
  function randomBuffer () {
17
15
  return uint8ArrayFromString(Math.random().toString())
@@ -21,7 +19,7 @@ const infiniteRandom = {
21
19
  [Symbol.asyncIterator]: async function * () {
22
20
  while (true) {
23
21
  yield randomBuffer()
24
- await pause(10)
22
+ await delay(50)
25
23
  }
26
24
  }
27
25
  }
@@ -31,7 +29,7 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
31
29
  it('closing underlying socket closes streams', async () => {
32
30
  const muxer = await common.setup({
33
31
  onStream: (stream) => {
34
- void pipe(stream, stream)
32
+ void pipe(stream, drain)
35
33
  }
36
34
  })
37
35
  const upgrader = mockUpgrader({ muxer })
@@ -93,12 +91,12 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
93
91
  })
94
92
 
95
93
  // Pause, and then send some data and close the first stream
96
- await pause(50)
94
+ await delay(50)
97
95
  await pipe([randomBuffer()], stream, drain)
98
96
  closed = true
99
97
 
100
98
  // Abort all the other streams later
101
- await pause(50)
99
+ await delay(50)
102
100
  controllers.forEach(c => c.abort())
103
101
 
104
102
  // These should now all resolve without error
@@ -1,5 +1,6 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
- import { isValidTick, mockUpgrader } from './utils/index.js'
2
+ import { isValidTick } from '../utils/is-valid-tick.js'
3
+ import { mockUpgrader } from '../mocks/upgrader.js'
3
4
  import { goodbye } from 'it-goodbye'
4
5
  import all from 'it-all'
5
6
  import { pipe } from 'it-pipe'
@@ -1,5 +1,5 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
- import { mockUpgrader } from './utils/index.js'
2
+ import { mockUpgrader } from '../mocks/upgrader.js'
3
3
  import type { TestSetup } from '../index.js'
4
4
  import type { Transport } from '@libp2p/interfaces/transport'
5
5
  import type { TransportTestFixtures, SetupArgs } from './index.js'
@@ -4,7 +4,8 @@ import sinon from 'sinon'
4
4
  import pWaitFor from 'p-wait-for'
5
5
  import { pipe } from 'it-pipe'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
- import { isValidTick, mockUpgrader } from './utils/index.js'
7
+ import { isValidTick } from '../utils/is-valid-tick.js'
8
+ import { mockUpgrader } from '../mocks/upgrader.js'
8
9
  import defer from 'p-defer'
9
10
  import { CustomEvent } from '@libp2p/interfaces'
10
11
  import type { TestSetup } from '../index.js'
@@ -0,0 +1,18 @@
1
+
2
+ /**
3
+ * A tick is considered valid if it happened between now
4
+ * and `ms` milliseconds ago
5
+ */
6
+ export function isValidTick (date?: number, ms: number = 5000) {
7
+ if (date == null) {
8
+ throw new Error('date must be a number')
9
+ }
10
+
11
+ const now = Date.now()
12
+
13
+ if (date > now - ms && date <= now) {
14
+ return true
15
+ }
16
+
17
+ return false
18
+ }
@@ -1,15 +0,0 @@
1
- import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport';
2
- import type { Muxer } from '@libp2p/interfaces/stream-muxer';
3
- import type { Duplex } from 'it-stream-types';
4
- /**
5
- * A tick is considered valid if it happened between now
6
- * and `ms` milliseconds ago
7
- */
8
- export declare function isValidTick(date?: number, ms?: number): boolean;
9
- export declare function mockMultiaddrConnection(source: Duplex<Uint8Array>): MultiaddrConnection;
10
- export declare function mockMuxer(): Muxer;
11
- export interface MockUpgraderOptions {
12
- muxer?: Muxer;
13
- }
14
- export declare function mockUpgrader(options?: MockUpgraderOptions): Upgrader;
15
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transport/utils/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAA;AAEjF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C;;;GAGG;AACH,wBAAgB,WAAW,CAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAE,MAAa,WAY5D;AAED,wBAAgB,uBAAuB,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,GAAG,mBAAmB,CAaxF;AAED,wBAAgB,SAAS,IAAK,KAAK,CAoClC;AAED,MAAM,WAAW,mBAAmB;IAClC,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED,wBAAgB,YAAY,CAAE,OAAO,GAAE,mBAAwB,YAuB9D"}
@@ -1,137 +0,0 @@
1
- import { expect } from 'aegir/utils/chai.js';
2
- import { pair } from 'it-pair';
3
- import { peerIdFromString } from '@libp2p/peer-id';
4
- import * as PeerIdFactory from '@libp2p/peer-id-factory';
5
- import { pushable } from 'it-pushable';
6
- import drain from 'it-drain';
7
- import { Multiaddr } from '@multiformats/multiaddr';
8
- import { pipe } from 'it-pipe';
9
- /**
10
- * A tick is considered valid if it happened between now
11
- * and `ms` milliseconds ago
12
- */
13
- export function isValidTick(date, ms = 5000) {
14
- if (date == null) {
15
- throw new Error('date must be a number');
16
- }
17
- const now = Date.now();
18
- if (date > now - ms && date <= now) {
19
- return true;
20
- }
21
- return false;
22
- }
23
- export function mockMultiaddrConnection(source) {
24
- const maConn = {
25
- ...source,
26
- async close() {
27
- },
28
- timeline: {
29
- open: Date.now()
30
- },
31
- remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001')
32
- };
33
- return maConn;
34
- }
35
- export function mockMuxer() {
36
- let streamId = 0;
37
- let streams = [];
38
- const p = pushable();
39
- const muxer = {
40
- source: p,
41
- sink: async (source) => {
42
- await drain(source);
43
- },
44
- get streams() {
45
- return streams;
46
- },
47
- newStream: (name) => {
48
- const echo = pair();
49
- const id = `${streamId++}`;
50
- const stream = {
51
- id,
52
- sink: echo.sink,
53
- source: echo.source,
54
- close: () => {
55
- streams = streams.filter(s => s !== stream);
56
- },
57
- abort: () => { },
58
- reset: () => { },
59
- timeline: {
60
- open: 0
61
- }
62
- };
63
- return stream;
64
- }
65
- };
66
- return muxer;
67
- }
68
- export function mockUpgrader(options = {}) {
69
- const ensureProps = (multiaddrConnection) => {
70
- ['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
71
- expect(multiaddrConnection).to.have.property(prop);
72
- });
73
- expect(isValidTick(multiaddrConnection.timeline.open)).to.equal(true);
74
- return multiaddrConnection;
75
- };
76
- const muxer = options.muxer ?? mockMuxer();
77
- const upgrader = {
78
- async upgradeOutbound(multiaddrConnection) {
79
- ensureProps(multiaddrConnection);
80
- return await createConnection(multiaddrConnection, 'outbound', muxer);
81
- },
82
- async upgradeInbound(multiaddrConnection) {
83
- ensureProps(multiaddrConnection);
84
- return await createConnection(multiaddrConnection, 'inbound', muxer);
85
- }
86
- };
87
- return upgrader;
88
- }
89
- async function createConnection(maConn, direction, muxer) {
90
- const remoteAddr = maConn.remoteAddr;
91
- const remotePeerIdStr = remoteAddr.getPeerId();
92
- const remotePeer = remotePeerIdStr != null ? peerIdFromString(remotePeerIdStr) : await PeerIdFactory.createEd25519PeerId();
93
- const streams = [];
94
- let streamId = 0;
95
- const registry = new Map();
96
- void pipe(maConn, muxer, maConn);
97
- return {
98
- id: 'mock-connection',
99
- remoteAddr,
100
- remotePeer,
101
- stat: {
102
- status: 'OPEN',
103
- direction,
104
- timeline: maConn.timeline,
105
- multiplexer: 'test-multiplexer',
106
- encryption: 'yes-yes-very-secure'
107
- },
108
- registry,
109
- tags: [],
110
- streams,
111
- newStream: async (protocols) => {
112
- if (!Array.isArray(protocols)) {
113
- protocols = [protocols];
114
- }
115
- if (protocols.length === 0) {
116
- throw new Error('protocols must have a length');
117
- }
118
- const id = `${streamId++}`;
119
- const stream = muxer.newStream(id);
120
- const streamData = {
121
- protocol: protocols[0],
122
- stream
123
- };
124
- registry.set(id, streamData);
125
- return streamData;
126
- },
127
- addStream: (stream, metadata) => {
128
- },
129
- removeStream: (id) => {
130
- registry.delete(id);
131
- },
132
- close: async () => {
133
- await maConn.close();
134
- }
135
- };
136
- }
137
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transport/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAM9B;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAE,IAAa,EAAE,KAAa,IAAI;IAC3D,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;KACzC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IAEtB,IAAI,IAAI,GAAG,GAAG,GAAG,EAAE,IAAI,IAAI,IAAI,GAAG,EAAE;QAClC,OAAO,IAAI,CAAA;KACZ;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