diff options
author | 2023-02-27 21:24:59 -0300 | |
---|---|---|
committer | 2023-02-27 16:24:59 -0800 | |
commit | 4b627457540c7c6ef79e7133672e282bb0e61702 (patch) | |
tree | eb7e95f56b9d2b655db6b227ccb7a085cfa4d47a | |
parent | 0afb1693d370c0dd1340b3eb7659d31ef3fc94a3 (diff) | |
download | bun-4b627457540c7c6ef79e7133672e282bb0e61702.tar.gz bun-4b627457540c7c6ef79e7133672e282bb0e61702.tar.zst bun-4b627457540c7c6ef79e7133672e282bb0e61702.zip |
add signal on http.Server.listen (#2223)
* add signal on http.Server.listen
* actual call close instead of just stopping the server
-rw-r--r-- | src/bun.js/http.exports.js | 6 | ||||
-rw-r--r-- | test/bun.js/node-http.test.ts | 26 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js index 59e2d3483..61bb77e04 100644 --- a/src/bun.js/http.exports.js +++ b/src/bun.js/http.exports.js @@ -207,6 +207,7 @@ export class Server extends EventEmitter { } this.#options = options; + if (callback) this.on("request", callback); } @@ -232,8 +233,13 @@ export class Server extends EventEmitter { if (typeof port === "function") { onListen = port; } else if (typeof port === "object") { + port?.signal?.addEventListener("abort", ()=> { + this.close(); + }); + host = port?.host; port = port?.port; + if (typeof port?.callback === "function") onListen = port?.callback; } const ResponseClass = this.#options.ServerResponse || ServerResponse; diff --git a/test/bun.js/node-http.test.ts b/test/bun.js/node-http.test.ts index b3ab4072b..3ba383c2e 100644 --- a/test/bun.js/node-http.test.ts +++ b/test/bun.js/node-http.test.ts @@ -352,6 +352,32 @@ describe("node:http", () => { }); }); + describe("signal", () => { + + it("should abort and close the server", done => { + const server = createServer((req, res) => { + res.writeHead(200, { "Content-Type": "text/plain" }); + res.end("Hello World"); + }); + + //force timeout to not hang tests + const interval = setTimeout(()=> { + expect(false).toBe(true); + server.close(); + done() + }, 100); + + const signal = AbortSignal.timeout(30); + signal.addEventListener("abort", ()=> { + clearTimeout(interval); + expect(true).toBe(true); + done() + }); + + server.listen({ signal, port: 8130 }); + }); + }); + describe("get", () => { let server; beforeAll(() => { |