node:http: preserve this value for onListen callback (#10533)

This commit is contained in:
Meghan Denny
2024-04-26 18:42:29 -07:00
committed by GitHub
parent 609ef6a8ad
commit 8280defc30
2 changed files with 16 additions and 1 deletions

View File

@@ -337,7 +337,7 @@ class Agent extends EventEmitter {
function emitListeningNextTick(self, onListen, err, hostname, port) {
if (typeof onListen === "function") {
try {
onListen(err, hostname, port);
onListen.$apply(self, [err, hostname, port]);
} catch (err) {
self.emit("error", err);
}

View File

@@ -140,6 +140,21 @@ describe("node:http", () => {
listenResponse.close();
});
it("listen callback should be bound to server", async () => {
const server = createServer();
const { resolve, reject, promise } = Promise.withResolvers();
server.listen(0, function () {
try {
expect(this === server).toBeTrue();
resolve();
} catch (e) {
reject();
}
});
await promise;
server.close();
});
it("option method should be uppercase (#7250)", async () => {
try {
var server = createServer((req, res) => {