Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(unstable): remove "Deno.serve(handler, options)" overload #18759

Merged
merged 6 commits into from Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/bench/http/deno_flash_hono_router.js
Expand Up @@ -7,4 +7,4 @@ const [hostname, port] = addr.split(":");
const app = new Hono();
app.get("/", (c) => c.text("Hello, World!"));

Deno.serve(app.fetch, { port: Number(port), hostname });
Deno.serve({ port: Number(port), hostname }, app.fetch);
42 changes: 21 additions & 21 deletions cli/tests/unit/serve_test.ts
Expand Up @@ -160,19 +160,19 @@ Deno.test({ permissions: { net: true } }, async function httpServerOverload2() {
const promise = deferred();
const listeningPromise = deferred();

const server = Deno.serve(async (request) => {
const server = Deno.serve({
port: 4501,
signal: ac.signal,
onListen: onListen(listeningPromise),
onError: createOnErrorCb(ac),
}, async (request) => {
// FIXME(bartlomieju):
// make sure that request can be inspected
console.log(request);
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
assertEquals(await request.text(), "");
promise.resolve();
return new Response("Hello World", { headers: { "foo": "bar" } });
}, {
port: 4501,
signal: ac.signal,
onListen: onListen(listeningPromise),
onError: createOnErrorCb(ac),
});

await listeningPromise;
Expand Down Expand Up @@ -2158,10 +2158,7 @@ Deno.test(
const ac = new AbortController();
const promise = deferred();
let count = 0;
const server = Deno.serve(() => {
count++;
return new Response(`hello world ${count}`);
}, {
const server = Deno.serve({
async onListen() {
const res1 = await fetch("http://localhost:9000/");
assertEquals(await res1.text(), "hello world 1");
Expand All @@ -2173,6 +2170,9 @@ Deno.test(
ac.abort();
},
signal: ac.signal,
}, () => {
count++;
return new Response(`hello world ${count}`);
});

await promise;
Expand Down Expand Up @@ -2226,7 +2226,16 @@ Deno.test(
async function testIssue16567() {
const ac = new AbortController();
const promise = deferred();
const server = Deno.serve(() =>
const server = Deno.serve({
async onListen() {
const res1 = await fetch("http://localhost:9000/");
assertEquals((await res1.text()).length, 40 * 50_000);

promise.resolve();
ac.abort();
},
signal: ac.signal,
}, () =>
new Response(
new ReadableStream({
start(c) {
Expand All @@ -2237,16 +2246,7 @@ Deno.test(
c.close();
},
}),
), {
async onListen() {
const res1 = await fetch("http://localhost:9000/");
assertEquals((await res1.text()).length, 40 * 50_000);

promise.resolve();
ac.abort();
},
signal: ac.signal,
});
));

await promise;
await server;
Expand Down
8 changes: 4 additions & 4 deletions cli/tests/unit_node/async_hooks_test.ts
Expand Up @@ -41,7 +41,10 @@ Deno.test(async function bar() {
let differentScopeDone = false;
const als = new AsyncLocalStorage();
const ac = new AbortController();
const server = Deno.serve(() => {
const server = Deno.serve({
signal: ac.signal,
port: 4000,
}, () => {
const differentScope = als.run(123, () =>
AsyncResource.bind(() => {
differentScopeDone = true;
Expand All @@ -54,9 +57,6 @@ Deno.test(async function bar() {
await new Promise((res) => setTimeout(res, 10));
return new Response(als.getStore() as string); // "Hello World"
});
}, {
signal: ac.signal,
port: 4000,
});

const res = await fetch("http://localhost:4000");
Expand Down
5 changes: 1 addition & 4 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Expand Up @@ -1354,10 +1354,7 @@ declare namespace Deno {
*
* @category HTTP Server
*/
export function serve(
handler: ServeHandler,
options?: ServeOptions | ServeTlsOptions,
): Promise<void>;
export function serve(handler: ServeHandler): Promise<void>;
/** **UNSTABLE**: New API, yet to be vetted.
*
* Serves HTTP requests with the given handler.
Expand Down
3 changes: 1 addition & 2 deletions ext/http/01_http.js
Expand Up @@ -624,10 +624,9 @@ async function serve(arg1, arg2) {
let handler = undefined;
if (typeof arg1 === "function") {
handler = arg1;
options = arg2;
} else if (typeof arg2 === "function") {
handler = arg2;
options = arg1;
handler = arg2;
} else {
options = arg1;
}
Expand Down