Skip to content

Commit 4175cac

Browse files
authored
fix: only accept lists of messages in encoder (libp2p#236)
At runtime the encoder is supplied lists of messages by a `pushableV`, the only time the source has individual messages is during test runs so simplify by only accepting lists of messages.
1 parent 084d3dc commit 4175cac

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

src/encode.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ const encoder = new Encoder()
5555
/**
5656
* Encode and yield one or more messages
5757
*/
58-
export async function * encode (source: Source<Message | Message[]>) {
59-
for await (const msg of source) {
58+
export async function * encode (source: Source<Message[]>) {
59+
for await (const msgs of source) {
6060
const list = new Uint8ArrayList()
6161

62-
if (Array.isArray(msg)) {
63-
for (const m of msg) {
64-
encoder.write(m, list)
65-
}
66-
} else {
62+
for (const msg of msgs) {
6763
encoder.write(msg, list)
6864
}
6965

test/coder.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Uint8ArrayList } from 'uint8arraylist'
1313

1414
describe('coder', () => {
1515
it('should encode header', async () => {
16-
const source: Message[] = [{ id: 17, type: 0, data: new Uint8ArrayList(uint8ArrayFromString('17')) }]
16+
const source: Message[][] = [[{ id: 17, type: 0, data: new Uint8ArrayList(uint8ArrayFromString('17')) }]]
1717

1818
const data = uint8ArrayConcat(await all(encode(source)))
1919

@@ -29,34 +29,34 @@ describe('coder', () => {
2929
})
3030

3131
it('should encode several msgs into buffer', async () => {
32-
const source: Message[] = [
32+
const source: Message[][] = [[
3333
{ id: 17, type: 0, data: new Uint8ArrayList(uint8ArrayFromString('17')) },
3434
{ id: 19, type: 0, data: new Uint8ArrayList(uint8ArrayFromString('19')) },
3535
{ id: 21, type: 0, data: new Uint8ArrayList(uint8ArrayFromString('21')) }
36-
]
36+
]]
3737

3838
const data = uint8ArrayConcat(await all(encode(source)))
3939

4040
expect(data).to.equalBytes(uint8ArrayFromString('88010231379801023139a801023231', 'base16'))
4141
})
4242

4343
it('should encode from Uint8ArrayList', async () => {
44-
const source: NewStreamMessage[] = [{
44+
const source: NewStreamMessage[][] = [[{
4545
id: 17,
4646
type: 0,
4747
data: new Uint8ArrayList(
4848
uint8ArrayFromString(Math.random().toString()),
4949
uint8ArrayFromString(Math.random().toString())
5050
)
51-
}]
51+
}]]
5252

5353
const data = uint8ArrayConcat(await all(encode(source)))
5454

5555
expect(data).to.equalBytes(
5656
uint8ArrayConcat([
5757
uint8ArrayFromString('8801', 'base16'),
58-
Uint8Array.from([source[0].data.length]),
59-
source[0].data instanceof Uint8Array ? source[0].data : source[0].data.slice()
58+
Uint8Array.from([source[0][0].data.length]),
59+
source[0][0].data instanceof Uint8Array ? source[0][0].data : source[0][0].data.slice()
6060
])
6161
)
6262
})
@@ -77,7 +77,7 @@ describe('coder', () => {
7777
})
7878

7979
it('should encode zero length body msg', async () => {
80-
const source: Message[] = [{ id: 17, type: 0 }]
80+
const source: Message[][] = [[{ id: 17, type: 0 }]]
8181

8282
const data = uint8ArrayConcat(await all(encode(source)))
8383

test/mplex.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ describe('mplex', () => {
4343

4444
// max out the streams for this connection
4545
for (let i = 0; i < maxInboundStreams; i++) {
46-
const source: NewStreamMessage[] = [{
46+
const source: NewStreamMessage[][] = [[{
4747
id: i,
4848
type: 0,
4949
data: new Uint8ArrayList(uint8ArrayFromString('17'))
50-
}]
50+
}]]
5151

5252
const data = uint8ArrayConcat(await all(encode(source)))
5353

5454
stream.push(data)
5555
}
5656

5757
// simulate a new incoming stream
58-
const source: NewStreamMessage[] = [{
58+
const source: NewStreamMessage[][] = [[{
5959
id: 11,
6060
type: 0,
6161
data: new Uint8ArrayList(uint8ArrayFromString('17'))
62-
}]
62+
}]]
6363

6464
const data = uint8ArrayConcat(await all(encode(source)))
6565

@@ -89,13 +89,13 @@ describe('mplex', () => {
8989
const id = 17
9090

9191
// simulate a new incoming stream that sends lots of data
92-
const input: Source<Message> = (async function * send () {
92+
const input: Source<Message[]> = (async function * send () {
9393
const newStreamMessage: NewStreamMessage = {
9494
id,
9595
type: MessageTypes.NEW_STREAM,
9696
data: new Uint8ArrayList(new Uint8Array(1024))
9797
}
98-
yield newStreamMessage
98+
yield [newStreamMessage]
9999

100100
await delay(10)
101101

@@ -105,7 +105,7 @@ describe('mplex', () => {
105105
type: MessageTypes.MESSAGE_INITIATOR,
106106
data: new Uint8ArrayList(new Uint8Array(1024 * 1000))
107107
}
108-
yield dataMessage
108+
yield [dataMessage]
109109

110110
sent++
111111

@@ -118,7 +118,7 @@ describe('mplex', () => {
118118
id,
119119
type: MessageTypes.CLOSE_INITIATOR
120120
}
121-
yield closeMessage
121+
yield [closeMessage]
122122
})()
123123

124124
// create the muxer

test/restrict-size.spec.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ describe('restrict size', () => {
1616
it('should throw when size is too big', async () => {
1717
const maxSize = 32
1818

19-
const input: Message[] = [
20-
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(8)) },
21-
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
22-
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(maxSize)) },
23-
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(64)) }
19+
const input: Message[][] = [
20+
[{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(8)) }],
21+
[{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) }],
22+
[{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(maxSize)) }],
23+
[{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(64)) }]
2424
]
2525

2626
const output: Message[] = []
@@ -38,9 +38,9 @@ describe('restrict size', () => {
3838
} catch (err: any) {
3939
expect(err).to.have.property('code', 'ERR_MSG_TOO_BIG')
4040
expect(output).to.have.length(3)
41-
expect(output[0]).to.deep.equal(input[0])
42-
expect(output[1]).to.deep.equal(input[1])
43-
expect(output[2]).to.deep.equal(input[2])
41+
expect(output[0]).to.deep.equal(input[0][0])
42+
expect(output[1]).to.deep.equal(input[1][0])
43+
expect(output[2]).to.deep.equal(input[2][0])
4444
return
4545
}
4646
throw new Error('did not restrict size')
@@ -51,30 +51,30 @@ describe('restrict size', () => {
5151
id: 4,
5252
type: MessageTypes.CLOSE_RECEIVER
5353
}
54-
const input: Message[] = [message]
54+
const input: Message[][] = [[message]]
5555

5656
const output = await pipe(
5757
input,
5858
encode,
5959
decode(32),
6060
async (source) => await all(source)
6161
)
62-
expect(output).to.deep.equal(input)
62+
expect(output).to.deep.equal(input[0])
6363
})
6464

6565
it('should throw when unprocessed message queue size is too big', async () => {
6666
const maxMessageSize = 32
6767
const maxUnprocessedMessageQueueSize = 64
6868

69-
const input: Message[] = [
69+
const input: Message[][] = [[
7070
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7171
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7272
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7373
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7474
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7575
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) },
7676
{ id: 0, type: 1, data: new Uint8ArrayList(randomBytes(16)) }
77-
]
77+
]]
7878

7979
const output: Message[] = []
8080

0 commit comments

Comments
 (0)