1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

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

In preparation to stabilization of the API this overload was decided to
be removed.
This commit is contained in:
Bartek Iwańczuk 2023-04-26 14:54:03 +02:00 committed by GitHub
parent 036778c2e8
commit 1b450015e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 38 deletions

View file

@ -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);

View file

@ -248,7 +248,12 @@ 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);
@ -256,11 +261,6 @@ Deno.test({ permissions: { net: true } }, async function httpServerOverload2() {
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;
@ -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();
@ -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");
@ -2499,6 +2499,9 @@ Deno.test(
ac.abort();
},
signal: ac.signal,
}, () => {
count++;
return new Response(`hello world ${count}`);
});
await promise;
@ -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) {
@ -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;
@ -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(
@ -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(

View file

@ -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;
@ -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");

View file

@ -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.

View file

@ -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;