Skip to content

Commit

Permalink
feat(serve): Support second parameter in deno serve (denoland#25606)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored Sep 12, 2024
1 parent 018329a commit 7477c2d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 8 deletions.
4 changes: 1 addition & 3 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5090,9 +5090,7 @@ declare namespace Deno {
*
* @category HTTP Server
*/
fetch: (
request: Request,
) => Response | Promise<Response>;
fetch: ServeHandler;
}

/** Options which can be set when calling {@linkcode Deno.serve}.
Expand Down
4 changes: 2 additions & 2 deletions ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,8 @@ function registerDeclarativeServer(exports) {
);
}
},
handler: (req) => {
return exports.fetch(req);
handler: (req, connInfo) => {
return exports.fetch(req, connInfo);
},
});
};
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/serve/basic/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"tests": {
"basic_win": {
"if": "windows",
"args": "serve --host 0.0.0.0 --port 12345 main.ts",
"args": "serve --check --host 0.0.0.0 --port 12345 main.ts",
"output": "main.out"
},
"basic_not_win": {
"if": "unix",
"args": "serve --host 0.0.0.0 --port 12345 main.ts",
"args": "serve --check --host 0.0.0.0 --port 12345 main.ts",
"output": "main_not_win.out"
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/specs/serve/basic/main.out
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Check [WILDCARD]
deno serve: Listening on http://localhost:12345/
2 changes: 1 addition & 1 deletion tests/specs/serve/basic/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export default {
fetch(req) {
return new Response("Hello world!");
},
};
} satisfies Deno.ServeDefaultExport;
1 change: 1 addition & 0 deletions tests/specs/serve/basic/main_not_win.out
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Check [WILDCARD]
deno serve: Listening on http://0.0.0.0:12345/
6 changes: 6 additions & 0 deletions tests/specs/serve/conn_info/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"args": "serve --check --port 12468 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 0
}
3 changes: 3 additions & 0 deletions tests/specs/serve/conn_info/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Check [WILDCARD]main.ts
deno serve: Listening on http://[WILDCARD]
ServeHandlerInfo {}
19 changes: 19 additions & 0 deletions tests/specs/serve/conn_info/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(async () => {
for (let i = 0; i < 1000; i++) {
try {
const resp = await fetch("http://localhost:12468/");
Deno.exit(0);
} catch {
await new Promise((r) => setTimeout(r, 10));
}
}

Deno.exit(2);
})();

export default {
fetch(request, connInfo) {
console.log(connInfo);
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;

0 comments on commit 7477c2d

Please sign in to comment.