From c24ffec53feed8ea312e44a093f0a22c5e13a473 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 14 Oct 2024 06:16:14 -0700 Subject: [PATCH] Fake backpressure --- src/js/node/http.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 8a924eb061..f288ab858d 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -2233,19 +2233,35 @@ class ClientRequest extends (OutgoingMessage as unknown as typeof import("node:h (typeof chunk === "string" && (encoding === "utf-8" || encoding === "utf8" || !encoding)) || // Buffer ($isTypedArrayView(chunk) && (!encoding || encoding === "buffer" || encoding === "utf-8")); - + let bodySize = 0; if (!canSkipReEncodingData) { chunk = Buffer.from(chunk, encoding); + bodySize = chunk.length; + } else { + bodySize = chunk.length; } if (!this.#bodyChunks) { this.#bodyChunks = [chunk]; + if (callback) callback(); return true; } + + // Signal fake backpressure if the body size is > 1024 * 1024 + // So that code which loops forever until backpressure is signaled + // will eventually exit. + for (let chunk of this.#bodyChunks) { + bodySize += chunk.length; + if (bodySize > 1024 * 1024) { + break; + } + } + this.#bodyChunks.push(chunk); + if (callback) callback(); - return true; + return bodySize < 128 * 1024; } end(chunk, encoding, callback) {