diff options
author | 2023-01-22 14:58:22 -0800 | |
---|---|---|
committer | 2023-01-22 14:58:22 -0800 | |
commit | befd97a891d7de50ae130cdf262b2bf6d5ac69bc (patch) | |
tree | e44daebefa409499fd0c1c03e72e357bbe5bc999 | |
parent | 481dbf7c6e1a900136f6988218f3613720b31f97 (diff) | |
download | bun-befd97a891d7de50ae130cdf262b2bf6d5ac69bc.tar.gz bun-befd97a891d7de50ae130cdf262b2bf6d5ac69bc.tar.zst bun-befd97a891d7de50ae130cdf262b2bf6d5ac69bc.zip |
[node:http] Add `address()` and fix callback / options parsing
-rw-r--r-- | src/bun.js/http.exports.js | 38 |
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); } } } |