summaryrefslogtreecommitdiff
path: root/ext/http/00_serve.js
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-04-19 18:09:50 -0600
committerGitHub <noreply@github.com>2024-04-19 18:09:50 -0600
commit365e1f48f7059f94d4eeb8f5ba8b3949b686b355 (patch)
tree94016de6377130ba9acff9c725614c609fb3a38f /ext/http/00_serve.js
parent93a40c1c643873ca3199a8cb2a6f29f0ca5bab68 (diff)
feat(ext/http): Add `addr` to HttpServer (#23442)
Adds an `addr` field to `HttpServer` to simplify the pattern `Deno.serve({ onListen({ port } => listenPort = port })`. This becomes: `const server = Deno.serve({}); port = server.addr.port`. Changes: - Refactors `serve` overloads to split TLS out (in preparation for landing a place for the TLS SNI information) - Adds an `addr` field to `HttpServer` that matches the `addr` field of the corresponding `Deno.Listener`s.
Diffstat (limited to 'ext/http/00_serve.js')
-rw-r--r--ext/http/00_serve.js28
1 files changed, 14 insertions, 14 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js
index 660287edb..52b833f10 100644
--- a/ext/http/00_serve.js
+++ b/ext/http/00_serve.js
@@ -552,7 +552,7 @@ function serve(arg1, arg2) {
const path = listener.addr.path;
return serveHttpOnListener(listener, signal, handler, onError, () => {
if (options.onListen) {
- options.onListen({ path });
+ options.onListen(listener.addr);
} else {
console.log(`Listening on ${path}`);
}
@@ -593,19 +593,18 @@ function serve(arg1, arg2) {
listenOpts.port = listener.addr.port;
}
- const onListen = (scheme) => {
- // If the hostname is "0.0.0.0", we display "localhost" in console
- // because browsers in Windows don't resolve "0.0.0.0".
- // See the discussion in https://github.com/denoland/deno_std/issues/1165
- const hostname = listenOpts.hostname == "0.0.0.0"
- ? "localhost"
- : listenOpts.hostname;
- const port = listenOpts.port;
+ const addr = listener.addr;
+ // If the hostname is "0.0.0.0", we display "localhost" in console
+ // because browsers in Windows don't resolve "0.0.0.0".
+ // See the discussion in https://github.com/denoland/deno_std/issues/1165
+ const hostname = addr.hostname == "0.0.0.0" ? "localhost" : addr.hostname;
+ addr.hostname = hostname;
+ const onListen = (scheme) => {
if (options.onListen) {
- options.onListen({ hostname, port });
+ options.onListen(addr);
} else {
- console.log(`Listening on ${scheme}${hostname}:${port}/`);
+ console.log(`Listening on ${scheme}${addr.hostname}:${addr.port}/`);
}
};
@@ -625,7 +624,7 @@ function serveHttpOnListener(listener, signal, handler, onError, onListen) {
onListen(context.scheme);
- return serveHttpOn(context, callback);
+ return serveHttpOn(context, listener.addr, callback);
}
/**
@@ -641,10 +640,10 @@ function serveHttpOnConnection(connection, signal, handler, onError, onListen) {
onListen(context.scheme);
- return serveHttpOn(context, callback);
+ return serveHttpOn(context, connection.localAddr, callback);
}
-function serveHttpOn(context, callback) {
+function serveHttpOn(context, addr, callback) {
let ref = true;
let currentPromise = null;
@@ -700,6 +699,7 @@ function serveHttpOn(context, callback) {
})();
return {
+ addr,
finished,
async shutdown() {
if (!context.closing && !context.closed) {