Skip to content
Open
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: 11 additions & 6 deletions src/js/node/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ const { shouldUseEnvProxy } = require("node:_http_agent");
// normalized protocol list / callback on the server instance the way
// tls.Server does (test-https-argument-of-creating.js).
// https://github.com/nodejs/node/blob/v26.3.0/lib/https.js#L82-L97
function createServer(options, requestListener) {
function Server(options, requestListener): void {
if (!(this instanceof Server)) return new Server(options, requestListener);
if (typeof options === "function") {
requestListener = options;
options = {};
Expand All @@ -514,13 +515,17 @@ function createServer(options, requestListener) {
// ALPN requests are always answered with http/1.1.
options.ALPNProtocols = ["http/1.1"];
}
const server = http.createServer(options, requestListener);
http.Server.$call(this, options, requestListener);
const optionsALPNProtocols = options.ALPNProtocols;
if (optionsALPNProtocols) {
require("node:tls").convertALPNProtocols(optionsALPNProtocols, server);
require("node:tls").convertALPNProtocols(optionsALPNProtocols, this);
}
server.ALPNCallback = options.ALPNCallback;
return server;
this.ALPNCallback = options.ALPNCallback;
}
$toClass(Server, "Server", http.Server);

function createServer(options, requestListener) {
return new Server(options, requestListener);
}

var https = {
Expand All @@ -531,7 +536,7 @@ var https = {
timeout: 5000,
proxyEnv: shouldUseEnvProxy() ? process.env : undefined,
}),
Server: http.Server,
Server,
createServer,
get,
request,
Expand Down
46 changes: 46 additions & 0 deletions test/js/node/http/node-https-server-class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { afterEach, describe, expect, test } from "bun:test";
import http from "node:http";
import https from "node:https";
import { tls } from "harness";

describe("https.Server", () => {
const servers: http.Server[] = [];
afterEach(() => {
for (const server of servers.splice(0)) {
server.close();
}
});

test("is a distinct class that extends http.Server", () => {
expect(https.Server).not.toBe(http.Server);
expect(Object.getPrototypeOf(https.Server.prototype)).toBe(http.Server.prototype);
expect(Object.getPrototypeOf(https.Server)).toBe(http.Server);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});

test("createServer returns an https.Server that is also an http.Server", () => {
const server = https.createServer({ ...tls }, () => {});
servers.push(server);
expect(server).toBeInstanceOf(https.Server);
expect(server).toBeInstanceOf(http.Server);
expect(server.constructor).toBe(https.Server);
// the ALPN default that tls.Server applies
expect(server.ALPNProtocols).toBeDefined();
});

test("a plain http.Server is not an https.Server", () => {
const server = http.createServer(() => {});
servers.push(server);
expect(server).toBeInstanceOf(http.Server);
expect(server).not.toBeInstanceOf(https.Server);
expect(server.constructor).toBe(http.Server);
});

test("new https.Server() and https.Server() both construct", () => {
const a = new https.Server({ ...tls });
// Node's https.Server can be called without `new`, like tls.Server.
const b = (https.Server as any)({ ...tls });
servers.push(a, b);
expect(a).toBeInstanceOf(https.Server);
expect(b).toBeInstanceOf(https.Server);
});
});