Skip to content

Commit

Permalink
BREAKING(unstable): remove "Deno.serve(handler, options)" overload (#…
Browse files Browse the repository at this point in the history
…18759)

In preparation to stabilization of the API this overload was decided to
be removed.
  • Loading branch information
bartlomieju committed Apr 26, 2023
1 parent 036778c commit 1b45001
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
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);
59 changes: 31 additions & 28 deletions cli/tests/unit/serve_test.ts
Expand Up @@ -248,19 +248,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 @@ -1015,12 +1015,15 @@ Deno.test(
const promise = deferred();
const ac = new AbortController();

const server = Deno.serve((request) => {
assert(request.body);
const server = Deno.serve(
{ port: 2333, signal: ac.signal },
(request) => {
assert(request.body);

promise.resolve();
return new Response(request.body);
}, { port: 2333, signal: ac.signal });
promise.resolve();
return new Response(request.body);
},
);

const ts = new TransformStream();
const writable = ts.writable.getWriter();
Expand Down Expand Up @@ -2484,10 +2487,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({ port }: { port: number }) {
const res1 = await fetch(`http://localhost:${port}/`);
assertEquals(await res1.text(), "hello world 1");
Expand All @@ -2499,6 +2499,9 @@ Deno.test(
ac.abort();
},
signal: ac.signal,
}, () => {
count++;
return new Response(`hello world ${count}`);
});

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

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

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

await promise;
await server;
Expand Down Expand Up @@ -2716,8 +2719,8 @@ Deno.test(
async function httpServeCurlH2C() {
const ac = new AbortController();
const server = Deno.serve(
() => new Response("hello world!"),
{ signal: ac.signal },
() => new Response("hello world!"),
);

assertEquals(
Expand Down Expand Up @@ -2747,12 +2750,12 @@ Deno.test(
async function httpsServeCurlH2C() {
const ac = new AbortController();
const server = Deno.serve(
() => new Response("hello world!"),
{
signal: ac.signal,
cert: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.crt"),
key: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.key"),
},
() => new Response("hello world!"),
);

assertEquals(
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 @@ -1361,10 +1361,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
1 change: 0 additions & 1 deletion ext/http/00_serve.js
Expand Up @@ -425,7 +425,6 @@ 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;
Expand Down

0 comments on commit 1b45001

Please sign in to comment.