Skip to content

Commit cd7850a

Browse files
feat: implement NITRO_UNIX_SOCKET and graceful shutdown env vars
Wire NITRO_UNIX_SOCKET, NITRO_SHUTDOWN_DISABLED, and NITRO_SHUTDOWN_TIMEOUT through to srvx serve options in the Node, Bun, and Deno server presets. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e102ff6 commit cd7850a

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

docs/2.deploy/10.runtimes/1.node.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ You can customize server behavior using following environment variables:
3131

3232
- `NITRO_PORT` or `PORT` (defaults to `3000`)
3333
- `NITRO_HOST` or `HOST`
34-
- `NITRO_UNIX_SOCKET` - if provided (a path to the desired socket file) the service will be served over the provided UNIX socket.
34+
- `NITRO_UNIX_SOCKET` - if provided (a path to the desired socket file) the service will be served over the provided UNIX socket (Node.js and Bun only).
3535
- `NITRO_SSL_CERT` and `NITRO_SSL_KEY` - if both are present, this will launch the server in HTTPS mode. In the vast majority of cases, this should not be used other than for testing, and the Nitro server should be run behind a reverse proxy like nginx or Cloudflare which terminates SSL.
36-
- `NITRO_SHUTDOWN_DISABLED` - Disables the graceful shutdown feature when set to `'true'`. If it's set to `'true'`, the graceful shutdown is bypassed to speed up the development process. Defaults to `'false'`.
37-
- `NITRO_SHUTDOWN_SIGNALS` - Allows you to specify which signals should be handled. Each signal should be separated with a space. Defaults to `'SIGINT SIGTERM'`.
38-
- `NITRO_SHUTDOWN_TIMEOUT` - Sets the amount of time (in milliseconds) before a forced shutdown occurs. Defaults to `'30000'` milliseconds.
39-
- `NITRO_SHUTDOWN_FORCE` - When set to true, it triggers `process.exit()` at the end of the shutdown process. If it's set to `'false'`, the process will simply let the event loop clear. Defaults to `'true'`.
36+
- `NITRO_SHUTDOWN_DISABLED` - Disables the graceful shutdown feature when set to `'true'`. Defaults to `'false'`.
37+
- `NITRO_SHUTDOWN_TIMEOUT` - Sets the amount of time (in milliseconds) before a forced shutdown occurs. Defaults to `5000` milliseconds.
4038

4139
## Cluster mode
4240

src/presets/bun/runtime/bun.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;
1313
const host = process.env.NITRO_HOST || process.env.HOST;
1414
const cert = process.env.NITRO_SSL_CERT;
1515
const key = process.env.NITRO_SSL_KEY;
16-
// const socketPath = process.env.NITRO_UNIX_SOCKET; // TODO
16+
const socketPath = process.env.NITRO_UNIX_SOCKET;
17+
18+
const _shutdownTimeout = Number.parseInt(process.env.NITRO_SHUTDOWN_TIMEOUT || "", 10);
19+
const gracefulShutdown =
20+
process.env.NITRO_SHUTDOWN_DISABLED === "true"
21+
? false
22+
: _shutdownTimeout > 0
23+
? { gracefulTimeout: _shutdownTimeout / 1000 }
24+
: undefined;
1725

1826
const nitroApp = useNitroApp();
1927

@@ -35,7 +43,9 @@ serve({
3543
hostname: host,
3644
tls: cert && key ? { cert, key } : undefined,
3745
fetch: _fetch,
46+
gracefulShutdown,
3847
bun: {
48+
unix: socketPath,
3949
websocket: import.meta._websocket ? ws?.websocket : undefined,
4050
},
4151
});

src/presets/deno/runtime/deno-server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;
1414
const host = process.env.NITRO_HOST || process.env.HOST;
1515
const cert = process.env.NITRO_SSL_CERT;
1616
const key = process.env.NITRO_SSL_KEY;
17-
// const socketPath = process.env.NITRO_UNIX_SOCKET; // TODO
17+
18+
const _shutdownTimeout = Number.parseInt(process.env.NITRO_SHUTDOWN_TIMEOUT || "", 10);
19+
const gracefulShutdown =
20+
process.env.NITRO_SHUTDOWN_DISABLED === "true"
21+
? false
22+
: _shutdownTimeout > 0
23+
? { gracefulTimeout: _shutdownTimeout / 1000 }
24+
: undefined;
1825

1926
const nitroApp = useNitroApp();
2027

@@ -35,6 +42,7 @@ serve({
3542
hostname: host,
3643
tls: cert && key ? { cert, key } : undefined,
3744
fetch: _fetch,
45+
gracefulShutdown,
3846
});
3947

4048
trapUnhandledErrors();

src/presets/node/runtime/node-server.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;
1313
const host = process.env.NITRO_HOST || process.env.HOST;
1414
const cert = process.env.NITRO_SSL_CERT;
1515
const key = process.env.NITRO_SSL_KEY;
16-
// const socketPath = process.env.NITRO_UNIX_SOCKET; // TODO
16+
const socketPath = process.env.NITRO_UNIX_SOCKET;
17+
18+
const _shutdownTimeout = Number.parseInt(process.env.NITRO_SHUTDOWN_TIMEOUT || "", 10);
19+
const gracefulShutdown =
20+
process.env.NITRO_SHUTDOWN_DISABLED === "true"
21+
? false
22+
: _shutdownTimeout > 0
23+
? { gracefulTimeout: _shutdownTimeout / 1000 }
24+
: undefined;
1725

1826
const nitroApp = useNitroApp();
1927

@@ -22,6 +30,8 @@ const server = serve({
2230
hostname: host,
2331
tls: cert && key ? { cert, key } : undefined,
2432
fetch: nitroApp.fetch,
33+
gracefulShutdown,
34+
node: socketPath ? { path: socketPath } : undefined,
2535
});
2636

2737
if (import.meta._websocket) {

0 commit comments

Comments
 (0)