Skip to content

Commit

Permalink
fix(ext/node): convert brotli chunks with proper byte offset (#27455)
Browse files Browse the repository at this point in the history
Fixes #27029
Fixes #26086
  • Loading branch information
littledivy authored Dec 28, 2024
1 parent fdd0edf commit 5194222
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ext/node/polyfills/_brotli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const {
ArrayPrototypeMap,
TypedArrayPrototypeSlice,
TypedArrayPrototypeSubarray,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeGetByteOffset,
DataViewPrototypeGetBuffer,
TypedArrayPrototypeGetBuffer,
DataViewPrototypeGetByteLength,
DataViewPrototypeGetByteOffset,
} = primordials;
const { isTypedArray, isDataView, close } = core;
import {
Expand Down Expand Up @@ -40,9 +43,17 @@ const toU8 = (input) => {
}

if (isTypedArray(input)) {
return new Uint8Array(TypedArrayPrototypeGetBuffer(input));
return new Uint8Array(
TypedArrayPrototypeGetBuffer(input),
TypedArrayPrototypeGetByteOffset(input),
TypedArrayPrototypeGetByteLength(input),
);
} else if (isDataView(input)) {
return new Uint8Array(DataViewPrototypeGetBuffer(input));
return new Uint8Array(
DataViewPrototypeGetBuffer(input),
DataViewPrototypeGetByteOffset(input),
DataViewPrototypeGetByteLength(input),
);
}

return input;
Expand Down
58 changes: 58 additions & 0 deletions tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import http, {
} from "node:http";
import url from "node:url";
import https from "node:https";
import zlib from "node:zlib";
import net, { Socket } from "node:net";
import fs from "node:fs";
import { text } from "node:stream/consumers";
Expand Down Expand Up @@ -1823,3 +1824,60 @@ Deno.test("[node/http] ServerResponse socket", async () => {

await promise;
});

Deno.test("[node/http] decompress brotli response", {
permissions: { net: true },
}, async () => {
let received = false;
const ac = new AbortController();
const server = Deno.serve({ port: 5928, signal: ac.signal }, (_req) => {
received = true;
return Response.json([
["accept-language", "*"],
["host", "localhost:3000"],
["user-agent", "Deno/2.1.1"],
], {});
});
const { promise, resolve, reject } = Promise.withResolvers<void>();
let body = "";

const request = http.get(
"http://localhost:5928/",
{
headers: {
"accept-encoding": "gzip, deflate, br, zstd",
},
},
(resp) => {
const decompress = zlib.createBrotliDecompress();
resp.on("data", (chunk) => {
decompress.write(chunk);
});

resp.on("end", () => {
decompress.end();
});

decompress.on("data", (chunk) => {
body += chunk;
});

decompress.on("end", () => {
resolve();
});
},
);
request.on("error", reject);
request.end(() => {
assert(received);
});

await promise;
ac.abort();
await server.finished;

assertEquals(JSON.parse(body), [["accept-language", "*"], [
"host",
"localhost:3000",
], ["user-agent", "Deno/2.1.1"]]);
});

0 comments on commit 5194222

Please sign in to comment.