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

fix(http): Use the addEventListener method instead of onabort properties #2124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ export async function serveListener(
): Promise<void> {
const server = new Server({ handler, onError: options?.onError });

if (options?.signal) {
options.signal.onabort = () => server.close();
}
options?.signal?.addEventListener("abort", () => server.close(), {
once: true,
});

return await server.serve(listener);
}
Expand Down Expand Up @@ -550,9 +550,9 @@ export async function serve(
onError: options.onError,
});

if (options?.signal) {
options.signal.onabort = () => server.close();
}
options?.signal?.addEventListener("abort", () => server.close(), {
once: true,
});

return await server.listenAndServe();
}
Expand Down Expand Up @@ -605,9 +605,9 @@ export async function serveTls(
onError: options.onError,
});

if (options?.signal) {
options.signal.onabort = () => server.close();
}
options?.signal?.addEventListener("abort", () => server.close(), {
once: true,
});

return await server.listenAndServeTls(options.certFile, options.keyFile);
}
Expand Down Expand Up @@ -651,9 +651,9 @@ export async function listenAndServe(
): Promise<void> {
const server = new Server({ ...config, handler });

if (options?.signal) {
options.signal.onabort = () => server.close();
}
options?.signal?.addEventListener("abort", () => server.close(), {
once: true,
});

return await server.listenAndServe();
}
Expand Down Expand Up @@ -703,9 +703,9 @@ export async function listenAndServeTls(
): Promise<void> {
const server = new Server({ ...config, handler });

if (options?.signal) {
options.signal.onabort = () => server.close();
}
options?.signal?.addEventListener("abort", () => server.close(), {
once: true,
});

return await server.listenAndServeTls(certFile, keyFile);
}
82 changes: 81 additions & 1 deletion http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
assert,
assertEquals,
assertRejects,
assertStrictEquals,
assertThrows,
unreachable,
} from "../testing/asserts.ts";
Expand Down Expand Up @@ -258,7 +259,86 @@ Deno.test(
);

Deno.test(
"serve should not throw if abort when the server is already closed",
"serveListener should not overwrite an abort signal handler",
async () => {
const listenOptions = {
hostname: "localhost",
port: 4505,
};
const listener = Deno.listen(listenOptions);
const handler = () => new Response();
const onAbort = () => {};
const abortController = new AbortController();

abortController.signal.onabort = onAbort;

const servePromise = serveListener(listener, handler, {
signal: abortController.signal,
});

try {
assertStrictEquals(abortController.signal.onabort, onAbort);
} finally {
abortController.abort();
await servePromise;
}
},
);

Deno.test(
"serve should not overwrite an abort signal handler",
async () => {
const handler = () => new Response();
const onAbort = () => {};
const abortController = new AbortController();

abortController.signal.onabort = onAbort;

const servePromise = serve(handler, {
hostname: "localhost",
port: 4505,
signal: abortController.signal,
});

try {
assertStrictEquals(abortController.signal.onabort, onAbort);
} finally {
abortController.abort();
await servePromise;
}
},
);

Deno.test(
"serveTls should not overwrite an abort signal handler",
async () => {
const certFile = join(testdataDir, "tls/localhost.crt");
const keyFile = join(testdataDir, "tls/localhost.key");
const handler = () => new Response();
const onAbort = () => {};
const abortController = new AbortController();

abortController.signal.onabort = onAbort;

const servePromise = serveTls(handler, {
hostname: "localhost",
port: 4505,
certFile,
keyFile,
signal: abortController.signal,
});

try {
assertStrictEquals(abortController.signal.onabort, onAbort);
} finally {
abortController.abort();
await servePromise;
}
},
);

Deno.test(
"serveListener should not throw if abort when the server is already closed",
async () => {
const listenOptions = {
hostname: "localhost",
Expand Down