Skip to content

Commit

Permalink
fix(ext/node): polyfill response._implicitHeader method (denoland#18738)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Apr 18, 2023
1 parent d2d62b6 commit 6efaef6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
29 changes: 29 additions & 0 deletions cli/tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import http from "node:http";
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
import { assertSpyCalls, spy } from "../../../test_util/std/testing/mock.ts";
import { deferred } from "../../../test_util/std/async/deferred.ts";

Deno.test("[node/http] ServerResponse _implicitHeader", async () => {
const d = deferred<void>();
const server = http.createServer((_req, res) => {
const writeHeadSpy = spy(res, "writeHead");
// deno-lint-ignore no-explicit-any
(res as any)._implicitHeader();
assertSpyCalls(writeHeadSpy, 1);
writeHeadSpy.restore();
res.end("Hello World");
});

server.listen(async () => {
const { port } = server.address() as { port: number };
const res = await fetch(`https://localhost:${port}`);
assertEquals(await res.text(), "Hello World");
server.close(() => {
d.resolve();
});
});

await d;
});
7 changes: 6 additions & 1 deletion ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export class ServerResponse extends NodeWritable {
return this.#headers.has(name);
}

writeHead(status: number, headers: Record<string, string>) {
writeHead(status: number, headers: Record<string, string> = {}) {
this.statusCode = status;
for (const k in headers) {
if (Object.hasOwn(headers, k)) {
Expand Down Expand Up @@ -540,6 +540,11 @@ export class ServerResponse extends NodeWritable {
// @ts-expect-error The signature for cb is stricter than the one implemented here
return super.end(chunk, encoding, cb);
}

// Undocumented API used by `npm:compression`.
_implicitHeader() {
this.writeHead(this.statusCode);
}
}

// TODO(@AaronO): optimize
Expand Down

0 comments on commit 6efaef6

Please sign in to comment.