summaryrefslogtreecommitdiff
path: root/ext/http/00_serve.js
diff options
context:
space:
mode:
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;