diff --git a/src/protocol/protocol.ts b/src/protocol/protocol.ts index 8affec42..f3322504 100644 --- a/src/protocol/protocol.ts +++ b/src/protocol/protocol.ts @@ -1,6 +1,8 @@ import { CommandInterface, NetworkType, BackendOptions, DosConfig, InitFsEntry, InitFileEntry } from "../emulators"; import { CommandInterfaceEventsImpl } from "../impl/ci-impl"; +const maxDataChunkSize = 4 * 1024 * 1024; + export type ClientMessage = "wc-install" | "wc-run" | @@ -664,6 +666,22 @@ export class CommandInterfaceOverTransportLayer implements CommandInterface { } private async sendDataChunk(chunk: DataChunk): Promise { + if (chunk.data === null || chunk.data.byteLength <= maxDataChunkSize) { + return this.sendFullDataChunk(chunk); + } else { + let pos = 0; + while (pos < chunk.data.byteLength) { + await this.sendFullDataChunk({ + type: chunk.type, + name: chunk.name, + data: chunk.data.slice(pos, Math.min(chunk.data.byteLength, pos + maxDataChunkSize)), + }) + pos += maxDataChunkSize; + } + } + } + + private async sendFullDataChunk(chunk: DataChunk): Promise { const key = this.dataChunkKey(chunk); if (this.dataChunkPromise[key] !== undefined) { throw new Error("sendDataChunk should be accepted before sending new one");