@libp2p/interface-compliance-tests 6.5.0 → 7.0.0-55b7e5fea

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 (133) hide show
  1. package/dist/src/connection-encryption/index.d.ts.map +1 -1
  2. package/dist/src/connection-encryption/index.js +24 -15
  3. package/dist/src/connection-encryption/index.js.map +1 -1
  4. package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
  5. package/dist/src/stream-muxer/base-test.js +62 -345
  6. package/dist/src/stream-muxer/base-test.js.map +1 -1
  7. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  8. package/dist/src/stream-muxer/close-test.js +254 -320
  9. package/dist/src/stream-muxer/close-test.js.map +1 -1
  10. package/dist/src/stream-muxer/index.js +2 -2
  11. package/dist/src/stream-muxer/index.js.map +1 -1
  12. package/dist/src/stream-muxer/{mega-stress-test.d.ts → stream-test.d.ts} +2 -2
  13. package/dist/src/stream-muxer/stream-test.d.ts.map +1 -0
  14. package/dist/src/stream-muxer/stream-test.js +290 -0
  15. package/dist/src/stream-muxer/stream-test.js.map +1 -0
  16. package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
  17. package/dist/src/stream-muxer/stress-test.js +70 -16
  18. package/dist/src/stream-muxer/stress-test.js.map +1 -1
  19. package/dist/src/transport/index.d.ts.map +1 -1
  20. package/dist/src/transport/index.js +235 -205
  21. package/dist/src/transport/index.js.map +1 -1
  22. package/dist/src/transport/utils.d.ts.map +1 -1
  23. package/dist/src/transport/utils.js +3 -2
  24. package/dist/src/transport/utils.js.map +1 -1
  25. package/package.json +25 -53
  26. package/src/connection-encryption/index.ts +27 -20
  27. package/src/stream-muxer/base-test.ts +75 -413
  28. package/src/stream-muxer/close-test.ts +305 -343
  29. package/src/stream-muxer/index.ts +2 -2
  30. package/src/stream-muxer/stream-test.ts +381 -0
  31. package/src/stream-muxer/stress-test.ts +92 -18
  32. package/src/transport/index.ts +281 -241
  33. package/src/transport/utils.ts +3 -2
  34. package/dist/src/connection-encryption/utils/index.d.ts +0 -3
  35. package/dist/src/connection-encryption/utils/index.d.ts.map +0 -1
  36. package/dist/src/connection-encryption/utils/index.js +0 -21
  37. package/dist/src/connection-encryption/utils/index.js.map +0 -1
  38. package/dist/src/matchers.d.ts +0 -12
  39. package/dist/src/matchers.d.ts.map +0 -1
  40. package/dist/src/matchers.js +0 -14
  41. package/dist/src/matchers.js.map +0 -1
  42. package/dist/src/mocks/connection-manager.d.ts +0 -27
  43. package/dist/src/mocks/connection-manager.d.ts.map +0 -1
  44. package/dist/src/mocks/connection-manager.js +0 -147
  45. package/dist/src/mocks/connection-manager.js.map +0 -1
  46. package/dist/src/mocks/connection.d.ts +0 -41
  47. package/dist/src/mocks/connection.d.ts.map +0 -1
  48. package/dist/src/mocks/connection.js +0 -236
  49. package/dist/src/mocks/connection.js.map +0 -1
  50. package/dist/src/mocks/duplex.d.ts +0 -4
  51. package/dist/src/mocks/duplex.d.ts.map +0 -1
  52. package/dist/src/mocks/duplex.js +0 -9
  53. package/dist/src/mocks/duplex.js.map +0 -1
  54. package/dist/src/mocks/index.d.ts +0 -12
  55. package/dist/src/mocks/index.d.ts.map +0 -1
  56. package/dist/src/mocks/index.js +0 -8
  57. package/dist/src/mocks/index.js.map +0 -1
  58. package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
  59. package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
  60. package/dist/src/mocks/multiaddr-connection.js +0 -64
  61. package/dist/src/mocks/multiaddr-connection.js.map +0 -1
  62. package/dist/src/mocks/muxer.d.ts +0 -36
  63. package/dist/src/mocks/muxer.d.ts.map +0 -1
  64. package/dist/src/mocks/muxer.js +0 -234
  65. package/dist/src/mocks/muxer.js.map +0 -1
  66. package/dist/src/mocks/registrar.d.ts +0 -16
  67. package/dist/src/mocks/registrar.d.ts.map +0 -1
  68. package/dist/src/mocks/registrar.js +0 -66
  69. package/dist/src/mocks/registrar.js.map +0 -1
  70. package/dist/src/mocks/upgrader.d.ts +0 -9
  71. package/dist/src/mocks/upgrader.d.ts.map +0 -1
  72. package/dist/src/mocks/upgrader.js +0 -46
  73. package/dist/src/mocks/upgrader.js.map +0 -1
  74. package/dist/src/pubsub/api.d.ts +0 -6
  75. package/dist/src/pubsub/api.d.ts.map +0 -1
  76. package/dist/src/pubsub/api.js +0 -88
  77. package/dist/src/pubsub/api.js.map +0 -1
  78. package/dist/src/pubsub/connection-handlers.d.ts +0 -6
  79. package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
  80. package/dist/src/pubsub/connection-handlers.js +0 -329
  81. package/dist/src/pubsub/connection-handlers.js.map +0 -1
  82. package/dist/src/pubsub/emit-self.d.ts +0 -6
  83. package/dist/src/pubsub/emit-self.d.ts.map +0 -1
  84. package/dist/src/pubsub/emit-self.js +0 -80
  85. package/dist/src/pubsub/emit-self.js.map +0 -1
  86. package/dist/src/pubsub/index.d.ts +0 -18
  87. package/dist/src/pubsub/index.d.ts.map +0 -1
  88. package/dist/src/pubsub/index.js +0 -17
  89. package/dist/src/pubsub/index.js.map +0 -1
  90. package/dist/src/pubsub/messages.d.ts +0 -6
  91. package/dist/src/pubsub/messages.d.ts.map +0 -1
  92. package/dist/src/pubsub/messages.js +0 -48
  93. package/dist/src/pubsub/messages.js.map +0 -1
  94. package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
  95. package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
  96. package/dist/src/pubsub/multiple-nodes.js +0 -350
  97. package/dist/src/pubsub/multiple-nodes.js.map +0 -1
  98. package/dist/src/pubsub/two-nodes.d.ts +0 -6
  99. package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
  100. package/dist/src/pubsub/two-nodes.js +0 -216
  101. package/dist/src/pubsub/two-nodes.js.map +0 -1
  102. package/dist/src/pubsub/utils.d.ts +0 -5
  103. package/dist/src/pubsub/utils.d.ts.map +0 -1
  104. package/dist/src/pubsub/utils.js +0 -27
  105. package/dist/src/pubsub/utils.js.map +0 -1
  106. package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
  107. package/dist/src/stream-muxer/mega-stress-test.js +0 -11
  108. package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
  109. package/dist/src/stream-muxer/spawner.d.ts +0 -4
  110. package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
  111. package/dist/src/stream-muxer/spawner.js +0 -39
  112. package/dist/src/stream-muxer/spawner.js.map +0 -1
  113. package/dist/typedoc-urls.json +0 -44
  114. package/src/connection-encryption/utils/index.ts +0 -27
  115. package/src/matchers.ts +0 -18
  116. package/src/mocks/connection-manager.ts +0 -216
  117. package/src/mocks/connection.ts +0 -309
  118. package/src/mocks/duplex.ts +0 -11
  119. package/src/mocks/index.ts +0 -11
  120. package/src/mocks/multiaddr-connection.ts +0 -80
  121. package/src/mocks/muxer.ts +0 -331
  122. package/src/mocks/registrar.ts +0 -86
  123. package/src/mocks/upgrader.ts +0 -65
  124. package/src/pubsub/api.ts +0 -116
  125. package/src/pubsub/connection-handlers.ts +0 -413
  126. package/src/pubsub/emit-self.ts +0 -99
  127. package/src/pubsub/index.ts +0 -34
  128. package/src/pubsub/messages.ts +0 -59
  129. package/src/pubsub/multiple-nodes.ts +0 -440
  130. package/src/pubsub/two-nodes.ts +0 -272
  131. package/src/pubsub/utils.ts +0 -34
  132. package/src/stream-muxer/mega-stress-test.ts +0 -14
  133. package/src/stream-muxer/spawner.ts +0 -57
@@ -1,458 +1,120 @@
1
+ import { multiaddrConnectionPair, byteStream } from '@libp2p/utils'
1
2
  import { expect } from 'aegir/chai'
2
- import all from 'it-all'
3
- import { byteStream } from 'it-byte-stream'
4
- import map from 'it-map'
5
- import { duplexPair } from 'it-pair/duplex'
6
- import { pipe } from 'it-pipe'
7
- import defer from 'p-defer'
8
- import { Uint8ArrayList } from 'uint8arraylist'
3
+ import { pEvent } from 'p-event'
9
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
11
- import { isValidTick } from '../is-valid-tick.js'
12
5
  import type { TestSetup } from '../index.js'
13
- import type { Stream, StreamMuxerFactory } from '@libp2p/interface'
14
- import type { Source } from 'it-stream-types'
15
- import type { DeferredPromise } from 'p-defer'
6
+ import type { Stream, StreamMuxer, StreamMuxerFactory } from '@libp2p/interface'
16
7
 
17
8
  export default (common: TestSetup<StreamMuxerFactory>): void => {
18
9
  describe('base', () => {
19
- it('should open a stream from the dialer', async () => {
20
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
21
- const onStreamPromise: DeferredPromise<Stream> = defer()
22
- const onStreamEndPromise: DeferredPromise<Stream> = defer()
10
+ let dialer: StreamMuxer
11
+ let listener: StreamMuxer
12
+
13
+ beforeEach(async () => {
14
+ const [outboundConnection, inboundConnection] = multiaddrConnectionPair()
23
15
 
24
16
  const dialerFactory = await common.setup()
25
- const dialer = dialerFactory.createStreamMuxer({
26
- direction: 'outbound'
27
- })
17
+ dialer = dialerFactory.createStreamMuxer(outboundConnection)
28
18
 
29
19
  const listenerFactory = await common.setup()
30
- const listener = listenerFactory.createStreamMuxer({
31
- direction: 'inbound',
32
- onIncomingStream: (stream) => {
33
- onStreamPromise.resolve(stream)
34
- },
35
- onStreamEnd: (stream) => {
36
- onStreamEndPromise.resolve(stream)
37
- }
38
- })
39
-
40
- void pipe(p[0], dialer, p[0])
41
- void pipe(p[1], listener, p[1])
42
-
43
- const dialerStream = await dialer.newStream()
44
- expect(dialer.streams).to.include(dialerStream)
45
- expect(isValidTick(dialerStream.timeline.open)).to.equal(true)
46
-
47
- const dialerBytes = byteStream(dialerStream)
48
- void dialerBytes.write(uint8ArrayFromString('hello'))
20
+ listener = listenerFactory.createStreamMuxer(inboundConnection)
21
+ })
49
22
 
50
- const listenerStream = await onStreamPromise.promise
51
- expect(isValidTick(listenerStream.timeline.open)).to.equal(true)
52
- // Make sure the stream is being tracked
53
- expect(listener.streams).to.include(listenerStream)
23
+ afterEach(async () => {
24
+ await dialer?.close()
25
+ await listener?.close()
26
+ })
54
27
 
55
- await dialerStream.close()
56
- await listenerStream.close()
28
+ it('should have a protocol', async () => {
29
+ expect(dialer.protocol).to.be.a('string')
30
+ })
57
31
 
58
- // Make sure stream is closed properly
59
- const endedStream = await onStreamEndPromise.promise
60
- expect(listener.streams).to.not.include(endedStream)
32
+ it('should be open', async () => {
33
+ expect(dialer.status).to.equal('open')
34
+ })
61
35
 
62
- if (endedStream.timeline.close == null) {
63
- throw new Error('timeline had no close time')
64
- }
36
+ it('should be closing during closing', async () => {
37
+ const closePromise = dialer.close()
38
+ expect(dialer.status).to.equal('closing')
65
39
 
66
- // Make sure the stream is removed from tracking
67
- expect(isValidTick(endedStream.timeline.close)).to.equal(true)
40
+ await closePromise
41
+ })
68
42
 
43
+ it('should be closed after closing', async () => {
69
44
  await dialer.close()
70
- await listener.close()
71
45
 
72
- // ensure we have no streams left
73
- expect(dialer.streams).to.have.length(0)
74
- expect(listener.streams).to.have.length(0)
46
+ expect(dialer.status).to.equal('closed')
75
47
  })
76
48
 
77
- it('should open a stream from the listener', async () => {
78
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
79
- const onStreamPromise: DeferredPromise<Stream> = defer()
80
- const onStreamEndPromise: DeferredPromise<Stream> = defer()
81
- const dialerFactory = await common.setup()
82
- const dialer = dialerFactory.createStreamMuxer({
83
- direction: 'outbound',
84
- onIncomingStream: (stream: Stream) => {
85
- onStreamPromise.resolve(stream)
86
- },
87
- onStreamEnd: (stream) => {
88
- onStreamEndPromise.resolve(stream)
89
- }
90
- })
91
-
92
- const listenerFactory = await common.setup()
93
- const listener = listenerFactory.createStreamMuxer({
94
- direction: 'inbound'
95
- })
96
-
97
- void pipe(p[0], dialer, p[0])
98
- void pipe(p[1], listener, p[1])
49
+ it('should open a stream', async () => {
50
+ const [
51
+ listenerStream,
52
+ dialerStream
53
+ ] = await Promise.all([
54
+ pEvent<'stream', CustomEvent<Stream>>(listener, 'stream').then(evt => evt.detail),
55
+ dialer.createStream()
56
+ ])
99
57
 
100
- const listenerStream = await listener.newStream()
58
+ const dialerBytes = byteStream(dialerStream)
101
59
  const listenerBytes = byteStream(listenerStream)
102
- void listenerBytes.write(uint8ArrayFromString('hello'))
103
-
104
- const dialerStream = await onStreamPromise.promise
105
60
 
106
- expect(isValidTick(dialerStream.timeline.open)).to.equal(true)
107
- expect(listener.streams).to.include(listenerStream)
108
- expect(isValidTick(listenerStream.timeline.open)).to.equal(true)
61
+ const input = uint8ArrayFromString('hello')
109
62
 
110
- await dialerStream.close()
111
- await listenerStream.close()
112
-
113
- // Make sure stream is closed properly
114
- const endedStream = await onStreamEndPromise.promise
115
- expect(dialer.streams).to.not.include(endedStream)
116
-
117
- if (endedStream.timeline.close == null) {
118
- throw new Error('timeline had no close time')
119
- }
120
-
121
- // Make sure the stream is removed from tracking
122
- expect(isValidTick(endedStream.timeline.close)).to.equal(true)
63
+ const [, output] = await Promise.all([
64
+ dialerBytes.write(input),
65
+ listenerBytes.read()
66
+ ])
123
67
 
124
- await dialer.close()
125
- await listener.close()
68
+ expect(output?.subarray()).to.equalBytes(input.subarray())
126
69
  })
127
70
 
128
71
  it('should open a stream on both sides', async () => {
129
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
130
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
131
- const onListenerStreamPromise: DeferredPromise<Stream> = defer()
132
- const dialerFactory = await common.setup()
133
- const dialer = dialerFactory.createStreamMuxer({
134
- direction: 'outbound',
135
- onIncomingStream: (stream) => {
136
- onDialerStreamPromise.resolve(stream)
137
- }
138
- })
139
-
140
- const listenerFactory = await common.setup()
141
- const listener = listenerFactory.createStreamMuxer({
142
- direction: 'inbound',
143
- onIncomingStream: (stream) => {
144
- onListenerStreamPromise.resolve(stream)
145
- }
146
- })
147
-
148
- void pipe(p[0], dialer, p[0])
149
- void pipe(p[1], listener, p[1])
72
+ const [
73
+ listenerInboundStream,
74
+ dialerOutboundStream,
150
75
 
151
- const dialerInitiatorStream = await dialer.newStream()
152
- const listenerInitiatorStream = await listener.newStream()
76
+ dialerInboundStream,
77
+ listenerOutboundStream
78
+ ] = await Promise.all([
79
+ pEvent<'stream', CustomEvent<Stream>>(listener, 'stream').then(evt => evt.detail),
80
+ dialer.createStream(),
153
81
 
154
- await Promise.all([
155
- dialerInitiatorStream.close(),
156
- listenerInitiatorStream.close(),
157
- onDialerStreamPromise.promise.then(async stream => { await stream.close() }),
158
- onListenerStreamPromise.promise.then(async stream => { await stream.close() })
82
+ pEvent<'stream', CustomEvent<Stream>>(dialer, 'stream').then(evt => evt.detail),
83
+ listener.createStream()
159
84
  ])
160
85
 
161
- await Promise.all([
162
- dialer.close(),
163
- listener.close()
164
- ])
165
- })
86
+ const dialerOutboundBytes = byteStream(dialerOutboundStream)
87
+ const listenerInboundBytes = byteStream(listenerInboundStream)
166
88
 
167
- it('should open a stream on one side, write, open a stream on the other side', async () => {
168
- const toString = (source: Source<Uint8ArrayList>): AsyncGenerator<string> => map(source, (u) => uint8ArrayToString(u.subarray()))
169
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
170
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
171
- const onListenerStreamPromise: DeferredPromise<Stream> = defer()
172
- const dialerFactory = await common.setup()
173
- const dialer = dialerFactory.createStreamMuxer({
174
- direction: 'outbound',
175
- onIncomingStream: (stream) => {
176
- onDialerStreamPromise.resolve(stream)
177
- }
178
- })
179
- const listenerFactory = await common.setup()
180
- const listener = listenerFactory.createStreamMuxer({
181
- direction: 'inbound',
182
- onIncomingStream: (stream) => {
183
- onListenerStreamPromise.resolve(stream)
184
- }
185
- })
186
-
187
- void pipe(p[0], dialer, p[0])
188
- void pipe(p[1], listener, p[1])
89
+ const listenerOutboundBytes = byteStream(listenerOutboundStream)
90
+ const dialerInboundBytes = byteStream(dialerInboundStream)
189
91
 
190
- const dialerConn = await dialer.newStream()
191
- const listenerConn = await listener.newStream()
92
+ const inputA = uint8ArrayFromString('hello')
93
+ const inputB = uint8ArrayFromString('world')
192
94
 
193
- void pipe([new Uint8ArrayList(uint8ArrayFromString('hey'))], dialerConn)
194
- void pipe([new Uint8ArrayList(uint8ArrayFromString('hello'))], listenerConn)
95
+ const [, outputA] = await Promise.all([
96
+ dialerOutboundBytes.write(inputA),
97
+ listenerInboundBytes.read(),
195
98
 
196
- const [
197
- dialerStream,
198
- listenerStream
199
- ] = await Promise.all([
200
- onDialerStreamPromise.promise,
201
- onListenerStreamPromise.promise
99
+ listenerOutboundBytes.write(inputB),
100
+ dialerInboundBytes.read()
202
101
  ])
203
102
 
103
+ expect(outputA?.subarray()).to.equalBytes(inputA.subarray())
104
+ expect(inputB?.subarray()).to.equalBytes(inputB.subarray())
105
+ })
106
+
107
+ it('should store a stream in the streams list', async () => {
204
108
  const [
205
- listenerChunks,
206
- dialerChunks
109
+ listenerStream,
110
+ dialerStream
207
111
  ] = await Promise.all([
208
- pipe(listenerStream, toString, async (source) => all(source)),
209
- pipe(dialerStream, toString, async (source) => all(source))
112
+ pEvent<'stream', CustomEvent<Stream>>(listener, 'stream').then(evt => evt.detail),
113
+ dialer.createStream()
210
114
  ])
211
115
 
212
- expect(listenerChunks).to.be.eql(['hey'])
213
- expect(dialerChunks).to.be.eql(['hello'])
214
- })
215
-
216
- it('should echo a small value via a pipe', async () => {
217
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
218
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
219
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
220
- const dialerFactory = await common.setup()
221
- const dialer = dialerFactory.createStreamMuxer({
222
- direction: 'outbound',
223
- onIncomingStream: (stream) => {
224
- onDialerStreamPromise.resolve(stream)
225
- }
226
- })
227
- const listenerFactory = await common.setup()
228
- const listener = listenerFactory.createStreamMuxer({
229
- direction: 'inbound',
230
- onIncomingStream: (stream) => {
231
- void Promise.resolve().then(async () => {
232
- const output = new Uint8ArrayList()
233
-
234
- for await (const buf of stream.source) {
235
- output.append(buf)
236
- }
237
-
238
- onDataReceivedPromise.resolve(output.subarray())
239
- })
240
- }
241
- })
242
-
243
- void pipe(p[0], dialer, p[0])
244
- void pipe(p[1], listener, p[1])
245
-
246
- const stream = await dialer.newStream()
247
- const input = Uint8Array.from([0, 1, 2, 3, 4])
248
-
249
- await pipe(
250
- [input],
251
- stream
252
- )
253
- await stream.close()
254
-
255
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
256
- })
257
-
258
- it('should echo a large value via a pipe', async () => {
259
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
260
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
261
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
262
- const dialerFactory = await common.setup()
263
- const dialer = dialerFactory.createStreamMuxer({
264
- direction: 'outbound',
265
- onIncomingStream: (stream) => {
266
- onDialerStreamPromise.resolve(stream)
267
- }
268
- })
269
- const listenerFactory = await common.setup()
270
- const listener = listenerFactory.createStreamMuxer({
271
- direction: 'inbound',
272
- onIncomingStream: (stream) => {
273
- void Promise.resolve().then(async () => {
274
- const output = new Uint8ArrayList()
275
-
276
- for await (const buf of stream.source) {
277
- output.append(buf)
278
- }
279
-
280
- onDataReceivedPromise.resolve(output.subarray())
281
- })
282
- }
283
- })
284
-
285
- void pipe(p[0], dialer, p[0])
286
- void pipe(p[1], listener, p[1])
287
-
288
- const stream = await dialer.newStream()
289
- const input = Uint8Array.from(new Array(1024 * 1024 * 10).fill(0))
290
-
291
- await pipe(
292
- [input],
293
- stream
294
- )
295
- await stream.close()
296
-
297
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
298
- })
299
-
300
- it('should echo a small value via sink', async () => {
301
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
302
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
303
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
304
- const dialerFactory = await common.setup()
305
- const dialer = dialerFactory.createStreamMuxer({
306
- direction: 'outbound',
307
- onIncomingStream: (stream) => {
308
- onDialerStreamPromise.resolve(stream)
309
- }
310
- })
311
- const listenerFactory = await common.setup()
312
- const listener = listenerFactory.createStreamMuxer({
313
- direction: 'inbound',
314
- onIncomingStream: (stream) => {
315
- void Promise.resolve().then(async () => {
316
- const output = new Uint8ArrayList()
317
-
318
- for await (const buf of stream.source) {
319
- output.append(buf)
320
- }
321
-
322
- onDataReceivedPromise.resolve(output.subarray())
323
- })
324
- }
325
- })
326
-
327
- void pipe(p[0], dialer, p[0])
328
- void pipe(p[1], listener, p[1])
329
-
330
- const stream = await dialer.newStream()
331
- const input = Uint8Array.from([0, 1, 2, 3, 4])
332
-
333
- await stream.sink([input])
334
- await stream.close()
335
-
336
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
337
- })
338
-
339
- it('should echo a large value via sink', async () => {
340
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
341
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
342
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
343
- const dialerFactory = await common.setup()
344
- const dialer = dialerFactory.createStreamMuxer({
345
- direction: 'outbound',
346
- onIncomingStream: (stream) => {
347
- onDialerStreamPromise.resolve(stream)
348
- }
349
- })
350
- const listenerFactory = await common.setup()
351
- const listener = listenerFactory.createStreamMuxer({
352
- direction: 'inbound',
353
- onIncomingStream: (stream) => {
354
- void Promise.resolve().then(async () => {
355
- const output = new Uint8ArrayList()
356
-
357
- for await (const buf of stream.source) {
358
- output.append(buf)
359
- }
360
-
361
- onDataReceivedPromise.resolve(output.subarray())
362
- })
363
- }
364
- })
365
-
366
- void pipe(p[0], dialer, p[0])
367
- void pipe(p[1], listener, p[1])
368
-
369
- const stream = await dialer.newStream()
370
- const input = Uint8Array.from(new Array(1024 * 1024 * 10).fill(0))
371
-
372
- await stream.sink([input])
373
- await stream.close()
374
-
375
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
376
- })
377
-
378
- it('should echo a small value via a pushable', async () => {
379
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
380
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
381
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
382
- const dialerFactory = await common.setup()
383
- const dialer = dialerFactory.createStreamMuxer({
384
- direction: 'outbound',
385
- onIncomingStream: (stream) => {
386
- onDialerStreamPromise.resolve(stream)
387
- }
388
- })
389
- const listenerFactory = await common.setup()
390
- const listener = listenerFactory.createStreamMuxer({
391
- direction: 'inbound',
392
- onIncomingStream: (stream) => {
393
- void Promise.resolve().then(async () => {
394
- const output = new Uint8ArrayList()
395
-
396
- for await (const buf of stream.source) {
397
- output.append(buf)
398
- }
399
-
400
- onDataReceivedPromise.resolve(output.subarray())
401
- })
402
- }
403
- })
404
-
405
- void pipe(p[0], dialer, p[0])
406
- void pipe(p[1], listener, p[1])
407
-
408
- const stream = await dialer.newStream()
409
- const input = Uint8Array.from([0, 1, 2, 3, 4])
410
-
411
- const pushable = byteStream(stream)
412
- await pushable.write(input)
413
- await pushable.unwrap().close()
414
-
415
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
416
- })
417
-
418
- it('should echo a large value via a pushable', async () => {
419
- const p = duplexPair<Uint8Array | Uint8ArrayList>()
420
- const onDialerStreamPromise: DeferredPromise<Stream> = defer()
421
- const onDataReceivedPromise: DeferredPromise<Uint8Array> = defer()
422
- const dialerFactory = await common.setup()
423
- const dialer = dialerFactory.createStreamMuxer({
424
- direction: 'outbound',
425
- onIncomingStream: (stream) => {
426
- onDialerStreamPromise.resolve(stream)
427
- }
428
- })
429
- const listenerFactory = await common.setup()
430
- const listener = listenerFactory.createStreamMuxer({
431
- direction: 'inbound',
432
- onIncomingStream: (stream) => {
433
- void Promise.resolve().then(async () => {
434
- const output = new Uint8ArrayList()
435
-
436
- for await (const buf of stream.source) {
437
- output.append(buf)
438
- }
439
-
440
- onDataReceivedPromise.resolve(output.subarray())
441
- })
442
- }
443
- })
444
-
445
- void pipe(p[0], dialer, p[0])
446
- void pipe(p[1], listener, p[1])
447
-
448
- const stream = await dialer.newStream()
449
- const input = Uint8Array.from(new Array(1024 * 1024 * 10).fill(0))
450
-
451
- const pushable = byteStream(stream)
452
- await pushable.write(input)
453
- await pushable.unwrap().close()
454
-
455
- expect(await onDataReceivedPromise.promise).to.equalBytes(input)
116
+ expect(dialer.streams).to.include(dialerStream, 'dialer did not store outbound stream')
117
+ expect(listener.streams).to.include(listenerStream, 'listener did not store inbound stream')
456
118
  })
457
119
  })
458
120
  }