From fa3386468e241697fbd89b06a2c1e2844be88265 Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Mon, 14 Aug 2023 15:28:53 -0700 Subject: [PATCH] reduce max resp chunk size well below webrtc max --- protocol/src/index.ts | 3 +++ server/src/server.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/protocol/src/index.ts b/protocol/src/index.ts index 805fc8d..c8d775a 100644 --- a/protocol/src/index.ts +++ b/protocol/src/index.ts @@ -49,4 +49,7 @@ export type WSErrorPayload = { message: string; }; +// WebRTC max is 16K, let's say 8K to be safe +export const MAX_CHUNK_SIZE = 8 * 1024; + export { Transport } from "./Transport"; diff --git a/server/src/server.ts b/server/src/server.ts index 6bea84c..ae09848 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -5,6 +5,7 @@ import { C2SRequestTypes, HTTPRequestPayload, HTTPResponsePayload, + MAX_CHUNK_SIZE, ProtoBareHeaders, S2CRequestType, S2CRequestTypes, @@ -235,7 +236,13 @@ export class AdriftServer { const { payload, body } = resp; this.sendHTTPResponseStart(seq, payload); for await (const chunk of body) { - this.sendHTTPResponseChunk(seq, new Uint8Array(chunk)); + let chunkPart = null; + let chunkRest = chunk; + do { + chunkPart = chunkRest.slice(0, MAX_CHUNK_SIZE); + chunkRest = chunkRest.slice(MAX_CHUNK_SIZE); + this.sendHTTPResponseChunk(seq, new Uint8Array(chunkPart)); + } while (chunkRest.byteLength > 0); } this.sendHTTPResponseEnd(seq); break;