summaryrefslogtreecommitdiff
path: root/ext/http/00_serve.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-05-19 02:59:23 +0200
committerGitHub <noreply@github.com>2023-05-19 02:59:23 +0200
commit5b0752234993ee69e47c32db478d2a296f73f396 (patch)
tree6501e364a82d6ff98845d87fe38744bda7fd1a70 /ext/http/00_serve.js
parent8724ba9d084127147e2bb2c997a6bf2f38c9b3d2 (diff)
BREAKING(unstable): change return type of Deno.serve() API (#19189)
This commit changes the return type of an unstable `Deno.serve()` API to instead return a `Deno.Server` object that has a `finished` field. This change is done in preparation to be able to ref/unref the HTTP server.
Diffstat (limited to 'ext/http/00_serve.js')
-rw-r--r--ext/http/00_serve.js51
1 files changed, 28 insertions, 23 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js
index 35af49b04..9075ae651 100644
--- a/ext/http/00_serve.js
+++ b/ext/http/00_serve.js
@@ -563,7 +563,7 @@ function mapToCallback(responseBodies, context, signal, callback, onError) {
};
}
-async function serve(arg1, arg2) {
+function serve(arg1, arg2) {
let options = undefined;
let handler = undefined;
if (typeof arg1 === "function") {
@@ -653,33 +653,38 @@ async function serve(arg1, arg2) {
onListen({ port: listenOpts.port });
- while (true) {
- const rid = context.serverRid;
- let req;
- try {
- req = await op_http_wait(rid);
- } catch (error) {
- if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
+ // Run the server
+ const finished = (async () => {
+ while (true) {
+ const rid = context.serverRid;
+ let req;
+ try {
+ req = await op_http_wait(rid);
+ } catch (error) {
+ if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
+ break;
+ }
+ throw new Deno.errors.Http(error);
+ }
+ if (req === 0xffffffff) {
break;
}
- throw new Deno.errors.Http(error);
+ PromisePrototypeCatch(callback(req), (error) => {
+ // Abnormal exit
+ console.error(
+ "Terminating Deno.serve loop due to unexpected error",
+ error,
+ );
+ context.close();
+ });
}
- if (req === 0xffffffff) {
- break;
+
+ for (const streamRid of new SafeSetIterator(responseBodies)) {
+ core.tryClose(streamRid);
}
- PromisePrototypeCatch(callback(req), (error) => {
- // Abnormal exit
- console.error(
- "Terminating Deno.serve loop due to unexpected error",
- error,
- );
- context.close();
- });
- }
+ })();
- for (const streamRid of new SafeSetIterator(responseBodies)) {
- core.tryClose(streamRid);
- }
+ return { finished };
}
internals.upgradeHttpRaw = upgradeHttpRaw;