mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 11:59:00 +00:00
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:
@@ -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);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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 }, () => {});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user