From 1ec76fb22acd9874b9bc493143cdb30e5baeb17a Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Tue, 16 Jul 2024 17:46:40 +0530 Subject: [PATCH] fix(ext/node): http request uploads of subarray of buffer should work (#24603) Closes https://github.com/denoland/deno/issues/24571 --- ext/node/polyfills/_http_outgoing.ts | 2 +- tests/unit_node/http_test.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ext/node/polyfills/_http_outgoing.ts b/ext/node/polyfills/_http_outgoing.ts index 35526a3031..a6edc1144f 100644 --- a/ext/node/polyfills/_http_outgoing.ts +++ b/ext/node/polyfills/_http_outgoing.ts @@ -540,7 +540,7 @@ export class OutgoingMessage extends Stream { data = Buffer.from(data, encoding); } if (data instanceof Buffer) { - data = new Uint8Array(data.buffer); + data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); } if (data.buffer.byteLength > 0) { this._bodyWriter.write(data).then(() => { diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index 9a37722c78..b9fe767e6f 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -1406,3 +1406,25 @@ Deno.test("[node/http] Server.address() can be null", () => { const server = http.createServer((_req, res) => res.end("it works")); assertEquals(server.address(), null); }); + +Deno.test("[node/http] ClientRequest PUT subarray", async () => { + const buffer = Buffer.from("hello world"); + const payload = buffer.subarray(6, 11); + let body = ""; + const { promise, resolve, reject } = Promise.withResolvers(); + const req = http.request("http://localhost:4545/echo_server", { + method: "PUT", + }, (resp) => { + resp.on("data", (chunk) => { + body += chunk; + }); + + resp.on("end", () => { + resolve(); + }); + }); + req.once("error", (e) => reject(e)); + req.end(payload); + await promise; + assertEquals(body, "world"); +});