Fix HTTP listen behavior being non-compliant with node (#5689)

* Fix HTTP listen behavior being non-compliant with node

* Add error code for address in use

* use SystemError

---------

Co-authored-by: SuperAuguste <19855629+SuperAuguste@users.noreply.github.com>
This commit is contained in:
dave caruso
2023-09-18 17:57:48 -04:00
committed by GitHub
parent 8f8ab301b4
commit f77df12894
4 changed files with 30 additions and 20 deletions

View File

@@ -5393,24 +5393,18 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
if (error_instance == .zero) {
switch (this.config.address) {
.tcp => |tcp| {
error_instance = ZigString.init(
std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server",
).toErrorInstance(
this.globalThis,
);
error_instance = (JSC.SystemError{
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server"),
.code = bun.String.static("EADDRINUSE"),
.syscall = bun.String.static("listen"),
}).toErrorInstance(this.globalThis);
},
.unix => |unix| {
error_instance = ZigString.init(
std.fmt.bufPrint(
&output_buf,
"Failed to listen on unix socket {}",
.{
strings.QuotedFormatter{ .text = bun.sliceTo(unix, 0) },
},
) catch "Failed to start server",
).toErrorInstance(
this.globalThis,
);
error_instance = (JSC.SystemError{
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to listen on unix socket {}", .{strings.QuotedFormatter{ .text = bun.sliceTo(unix, 0) }}) catch "Failed to start server"),
.code = bun.String.static("EADDRINUSE"),
.syscall = bun.String.static("listen"),
}).toErrorInstance(this.globalThis);
},
}
}

View File

@@ -540,7 +540,7 @@ class Server extends EventEmitter {
});
setTimeout(emitListeningNextTick, 1, this, onListen, null, this.#server.hostname, this.#server.port);
} catch (err) {
setTimeout(emitListeningNextTick, 1, this, onListen, err);
server.emit("error", err);
}
return this;

File diff suppressed because one or more lines are too long

View File

@@ -926,4 +926,20 @@ describe("node:http", () => {
}
});
});
test("error event not fired, issue#4651", done => {
const server = createServer((req, res) => {
res.end();
});
server.listen({ port: 42069 }, () => {
const server2 = createServer((_, res) => {
res.end();
});
server2.on("error", err => {
expect(err.code).toBe("EADDRINUSE");
done();
});
server2.listen({ port: 42069 }, () => {});
});
});
});