aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/http.exports.js38
1 files changed, 29 insertions, 9 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js
index 6e2eedacb..2e7f0aed3 100644
--- a/src/bun.js/http.exports.js
+++ b/src/bun.js/http.exports.js
@@ -35,16 +35,32 @@ export class Server extends EventEmitter {
}
}
- listen(...args) {
+ address() {
+ return this.#server.hostname;
+ }
+
+ listen(port, host, onListen) {
const server = this;
- const [options, listening_cb] = _normalizeArgs(args);
+ if (typeof host === "function") {
+ onListen = host;
+ }
+
+ if (typeof port === "function") {
+ onListen = port;
+ }
+
+ if (typeof port === "object") {
+ host = port?.host;
+ port = port?.port;
+ if (typeof port?.callback === "function") onListen = port?.callback;
+ }
const ResponseClass = this.#options.ServerResponse || ServerResponse;
const RequestClass = this.#options.IncomingMessage || IncomingMessage;
try {
this.#server = Bun.serve({
- port: options.port,
- hostname: options.host,
+ port,
+ hostName: host,
fetch(req) {
var pendingResponse;
@@ -84,12 +100,16 @@ export class Server extends EventEmitter {
},
});
- if (listening_cb) listening_cb();
+ if (onListen)
+ setTimeout(
+ () => onListen(null, this.#server.hostname, this.#server.port),
+ 0,
+ );
} catch (err) {
- this.emit(
- "error",
- new Error(`bun-http-polyfill: Bun.serve failed: ${err.message}`),
- );
+ if (onListen) {
+ setTimeout(onListen, 0, err);
+ }
+ this.emit("error", err);
}
}
}