Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "tsx --watch ./example/index.ts",
"test": "bun run test:node && bun run test:node-cluster",
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && npm install --prefix ./test/node/stop/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js && node ./test/node/stop/index.js",
"test:node-cluster": "node ./test/cluster/cluster.cjs port && node ./test/cluster/cluster.cjs object && node ./test/cluster/cluster.cjs true && node ./test/cluster/cluster.cjs false",
"build": "bun build.ts",
"release": "npm run build && npm run test && npm publish --access public"
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export const node = () => {
"This adapter doesn't support Bun requestIP method"
)
},
stop() {
return server.close()
stop(closeActiveConnections) {
return server.close(closeActiveConnections)
},
upgrade() {
throw new Error(
Expand All @@ -140,6 +140,9 @@ export const node = () => {
raw: server
} satisfies Server

// `stop` of the web standard adapter reads `app.server`.
app.server = serverInfo

if (callback) callback(serverInfo)

// @ts-ignore private property
Expand All @@ -150,6 +153,9 @@ export const node = () => {
app.event.start[i].fn(this)

process.on('beforeExit', () => {
// `app.stop()` already closed the server and ran the stop hooks.
if (app.server !== serverInfo) return

server.close()

if (app.event.stop)
Expand Down
65 changes: 65 additions & 0 deletions test/node/stop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
if ('Bun' in globalThis) {
throw new Error('❌ Use Node.js to run this test!');
}

import { Elysia } from 'elysia';
import { node } from '@elysiajs/node';

let stopHookCalls = 0;

const app = new Elysia({ adapter: node() })
.onStop(() => {
stopHookCalls++;
})
.get('/', () => 'hi')
// A response that never ends, like an SSE stream.
.get(
'/stream',
() =>
new Response(
new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode('open'));
}
})
)
)
.listen(0);

if (!app.server) {
throw new Error('❌ app.server is not set after listen');
}

const server = app.server.raw;
await server.ready();

if ((await fetch(server.url).then((response) => response.text())) !== 'hi') {
throw new Error('❌ The server does not answer');
}

const stream = await fetch(new URL('/stream', server.url));
await stream.body.getReader().read();

// Without `closeActiveConnections`, `stop` waits for the stream to end.
const hang = setTimeout(() => {
console.error('❌ app.stop(true) does not close the open stream');
process.exit(1);
}, 5000);
await app.stop(true);
clearTimeout(hang);

if (app.server) {
throw new Error('❌ app.server is not reset after stop');
}

if (await fetch(server.url).then(() => true, () => false)) {
throw new Error('❌ The server still answers after stop');
}

process.emit('beforeExit', 0);

if (stopHookCalls !== 1) {
throw new Error(`❌ The stop hooks ran ${stopHookCalls} times, expected 1`);
}

console.log('✅ app.stop(true) stops the server');
7 changes: 7 additions & 0 deletions test/node/stop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"dependencies": {
"@elysiajs/node": "../../..",
"elysia": "^1.3.3"
}
}