Skip to content

Commit 8b82e68

Browse files
authored
fix: ensure mock stream output is uint8arraylist (#2209)
When using mock streams pairs, we use a simple duplex to pass the input of one to the output of the other. The input can be a `Uint8Array` or a `Uint8ArrayList` but the output can only be `Uint8ArrayList` so ensure we output the correct type.
1 parent 1d14133 commit 8b82e68

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/interface-compliance-tests/src/mocks/connection.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as mss from '@libp2p/multistream-select'
44
import { peerIdFromString } from '@libp2p/peer-id'
55
import { duplexPair } from 'it-pair/duplex'
66
import { pipe } from 'it-pipe'
7+
import { Uint8ArrayList } from 'uint8arraylist'
78
import { mockMultiaddrConnection } from './multiaddr-connection.js'
89
import { mockMuxer } from './muxer.js'
910
import { mockRegistrar } from './registrar.js'
@@ -14,7 +15,6 @@ import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface/stream-m
1415
import type { Registrar } from '@libp2p/interface-internal/registrar'
1516
import type { Multiaddr } from '@multiformats/multiaddr'
1617
import type { Duplex, Source } from 'it-stream-types'
17-
import type { Uint8ArrayList } from 'uint8arraylist'
1818

1919
const log = logger('libp2p:mock-connection')
2020

@@ -177,6 +177,20 @@ export interface StreamInit {
177177
}
178178

179179
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init: StreamInit = {}): Stream {
180+
const originalSource = stream.source
181+
182+
// ensure stream output is `Uint8ArrayList` as it would be from an actual
183+
// Stream where everything is length-varint encoded
184+
stream.source = (async function * (): AsyncGenerator<Uint8ArrayList, any, unknown> {
185+
for await (const buf of originalSource) {
186+
if (buf instanceof Uint8Array) {
187+
yield new Uint8ArrayList(buf)
188+
} else {
189+
yield buf
190+
}
191+
}
192+
})()
193+
180194
return {
181195
...stream,
182196
close: async () => {},

0 commit comments

Comments
 (0)