Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): convert brotli chunks with proper byte offset #27455

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]]);
});
Loading