Skip to content

Commit 122f1e6

Browse files
authored
fix(@libp2p/webrtc): set max message size in alignment with spec (#2050)
1 parent d9159dd commit 122f1e6

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

packages/transport-webrtc/src/stream.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ export interface WebRTCStreamInit extends AbstractStreamInit {
2929
}
3030

3131
// Max message size that can be sent to the DataChannel
32-
const MAX_MESSAGE_SIZE = 16 * 1024
32+
export const MAX_MESSAGE_SIZE = 16 * 1024
3333

3434
// How much can be buffered to the DataChannel at once
35-
const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
35+
export const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
3636

3737
// How long time we wait for the 'bufferedamountlow' event to be emitted
38-
const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000
38+
export const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000
3939

4040
// protobuf field definition overhead
41-
const PROTOBUF_OVERHEAD = 3
41+
export const PROTOBUF_OVERHEAD = 5
42+
43+
// Length of varint, in bytes.
44+
export const VARINT_LENGTH = 2
4245

4346
export class WebRTCStream extends AbstractStream {
4447
/**
@@ -58,6 +61,10 @@ export class WebRTCStream extends AbstractStream {
5861
private readonly incomingData: Pushable<Uint8Array>
5962

6063
private messageQueue?: Uint8ArrayList
64+
65+
/**
66+
* The maximum size of a message in bytes
67+
*/
6168
private readonly maxDataSize: number
6269

6370
constructor (init: WebRTCStreamInit) {
@@ -70,7 +77,7 @@ export class WebRTCStream extends AbstractStream {
7077
this.dataChannelOptions = {
7178
bufferedAmountLowEventTimeout: init.dataChannelOptions?.bufferedAmountLowEventTimeout ?? BUFFERED_AMOUNT_LOW_TIMEOUT,
7279
maxBufferedAmount: init.dataChannelOptions?.maxBufferedAmount ?? MAX_BUFFERED_AMOUNT,
73-
maxMessageSize: init.dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE
80+
maxMessageSize: init.dataChannelOptions?.maxMessageSize ?? init.maxDataSize
7481
}
7582
this.maxDataSize = init.maxDataSize
7683

@@ -275,7 +282,7 @@ export function createStream (options: WebRTCStreamOptions): WebRTCStream {
275282
return new WebRTCStream({
276283
id: direction === 'inbound' ? (`i${channel.id}`) : `r${channel.id}`,
277284
direction,
278-
maxDataSize: (dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE) - PROTOBUF_OVERHEAD,
285+
maxDataSize: (dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE) - PROTOBUF_OVERHEAD - VARINT_LENGTH,
279286
dataChannelOptions,
280287
onEnd,
281288
channel,

packages/transport-webrtc/test/stream.spec.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@ import * as lengthPrefixed from 'it-length-prefixed'
66
import { pushable } from 'it-pushable'
77
import { Uint8ArrayList } from 'uint8arraylist'
88
import { Message } from '../src/pb/message.js'
9-
import { createStream } from '../src/stream.js'
9+
import { MAX_BUFFERED_AMOUNT, MAX_MESSAGE_SIZE, PROTOBUF_OVERHEAD, createStream } from '../src/stream.js'
1010

1111
const mockDataChannel = (opts: { send: (bytes: Uint8Array) => void, bufferedAmount?: number }): RTCDataChannel => {
1212
return {
1313
readyState: 'open',
14-
close: () => {},
15-
addEventListener: (_type: string, _listener: () => void) => {},
16-
removeEventListener: (_type: string, _listener: () => void) => {},
14+
close: () => { },
15+
addEventListener: (_type: string, _listener: () => void) => { },
16+
removeEventListener: (_type: string, _listener: () => void) => { },
1717
...opts
1818
} as RTCDataChannel
1919
}
2020

21-
const MAX_MESSAGE_SIZE = 16 * 1024
22-
2321
describe('Max message size', () => {
2422
it(`sends messages smaller or equal to ${MAX_MESSAGE_SIZE} bytes in one`, async () => {
2523
const sent: Uint8ArrayList = new Uint8ArrayList()
26-
const data = new Uint8Array(MAX_MESSAGE_SIZE - 5)
24+
const data = new Uint8Array(MAX_MESSAGE_SIZE - PROTOBUF_OVERHEAD)
2725
const p = pushable()
2826

2927
// Make sure that the data that ought to be sent will result in a message with exactly MAX_MESSAGE_SIZE
@@ -42,8 +40,7 @@ describe('Max message size', () => {
4240
p.end()
4341
await webrtcStream.sink(p)
4442

45-
// length(message) + message + length(FIN) + FIN
46-
expect(length(sent)).to.equal(4)
43+
expect(length(sent)).to.equal(6)
4744

4845
for (const buf of sent) {
4946
expect(buf.byteLength).to.be.lessThanOrEqual(MAX_MESSAGE_SIZE)
@@ -80,7 +77,6 @@ describe('Max message size', () => {
8077
})
8178

8279
it('closes the stream if bufferamountlow timeout', async () => {
83-
const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024 + 1
8480
const timeout = 100
8581
let closed = false
8682
const webrtcStream = createStream({
@@ -91,7 +87,7 @@ describe('Max message size', () => {
9187
send: () => {
9288
throw new Error('Expected to not send')
9389
},
94-
bufferedAmount: MAX_BUFFERED_AMOUNT
90+
bufferedAmount: MAX_BUFFERED_AMOUNT + 1
9591
}),
9692
direction: 'outbound',
9793
onEnd: () => {

0 commit comments

Comments
 (0)