summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/http/00_serve.js51
-rw-r--r--ext/node/polyfills/http.ts2
2 files changed, 29 insertions, 24 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;
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts
index adc5845b5..065ad2e0f 100644
--- a/ext/node/polyfills/http.ts
+++ b/ext/node/polyfills/http.ts
@@ -1577,7 +1577,7 @@ class ServerImpl extends EventEmitter {
this.emit("listening");
},
},
- ).then(() => this.#servePromise!.resolve());
+ ).finished.then(() => this.#servePromise!.resolve());
}
setTimeout() {