From 14fb5946ed1dd993b9275ac7bd39cf6a4bb4f0c5 Mon Sep 17 00:00:00 2001 From: Oskar Lebuda Date: Tue, 19 Nov 2024 15:18:07 +0100 Subject: [PATCH 1/2] fix(node-cluster): fix node cluster preset --- src/presets/node/runtime/node-cluster.ts | 12 +-- src/presets/node/runtime/node-server.ts | 105 ++++++++++++----------- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/src/presets/node/runtime/node-cluster.ts b/src/presets/node/runtime/node-cluster.ts index 1b6cfe5499..ec3b95c781 100644 --- a/src/presets/node/runtime/node-cluster.ts +++ b/src/presets/node/runtime/node-cluster.ts @@ -62,11 +62,13 @@ function runMaster() { } function runWorker() { - import("./node-server").catch((error) => { - console.error(error); - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); - }); + import("./node-server") + .then(({ serverWorker }) => serverWorker()) + .catch((error) => { + console.error(error); + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + }); } // Trap unhandled errors diff --git a/src/presets/node/runtime/node-server.ts b/src/presets/node/runtime/node-server.ts index a8737bda74..835a93a588 100644 --- a/src/presets/node/runtime/node-server.ts +++ b/src/presets/node/runtime/node-server.ts @@ -12,60 +12,65 @@ import { trapUnhandledNodeErrors, } from "nitropack/runtime/internal"; -const cert = process.env.NITRO_SSL_CERT; -const key = process.env.NITRO_SSL_KEY; +export const serverWorker = async () => { -const nitroApp = useNitroApp(); - -const server = - cert && key - ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App)) - : new HttpServer(toNodeListener(nitroApp.h3App)); - -const port = (destr(process.env.NITRO_PORT || process.env.PORT) || - 3000) as number; -const host = process.env.NITRO_HOST || process.env.HOST; - -const path = process.env.NITRO_UNIX_SOCKET; - -// @ts-ignore -const listener = server.listen(path ? { path } : { port, host }, (err) => { - if (err) { - console.error(err); - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); + const cert = process.env.NITRO_SSL_CERT; + const key = process.env.NITRO_SSL_KEY; + + const nitroApp = useNitroApp(); + + const server = + cert && key + ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App)) + : new HttpServer(toNodeListener(nitroApp.h3App)); + + const port = (destr(process.env.NITRO_PORT || process.env.PORT) || + 3000) as number; + const host = process.env.NITRO_HOST || process.env.HOST; + + const path = process.env.NITRO_UNIX_SOCKET; + + // @ts-ignore + const listener = server.listen(path ? { path } : { port, host }, (err) => { + if (err) { + console.error(err); + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + } + const protocol = cert && key ? "https" : "http"; + const addressInfo = listener.address() as AddressInfo; + if (typeof addressInfo === "string") { + console.log(`Listening on unix socket ${addressInfo}`); + return; + } + const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, ""); + const url = `${protocol}://${ + addressInfo.family === "IPv6" + ? `[${addressInfo.address}]` + : addressInfo.address + }:${addressInfo.port}${baseURL}`; + console.log(`Listening on ${url}`); + }); + + // Trap unhandled errors + trapUnhandledNodeErrors(); + + // Graceful shutdown + setupGracefulShutdown(listener, nitroApp); + + // Websocket support + // https://crossws.unjs.io/adapters/node + if (import.meta._websocket) { + const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket); + server.on("upgrade", handleUpgrade); } - const protocol = cert && key ? "https" : "http"; - const addressInfo = listener.address() as AddressInfo; - if (typeof addressInfo === "string") { - console.log(`Listening on unix socket ${addressInfo}`); - return; + + // Scheduled tasks + if (import.meta._tasks) { + startScheduleRunner(); } - const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, ""); - const url = `${protocol}://${ - addressInfo.family === "IPv6" - ? `[${addressInfo.address}]` - : addressInfo.address - }:${addressInfo.port}${baseURL}`; - console.log(`Listening on ${url}`); -}); - -// Trap unhandled errors -trapUnhandledNodeErrors(); - -// Graceful shutdown -setupGracefulShutdown(listener, nitroApp); - -// Websocket support -// https://crossws.unjs.io/adapters/node -if (import.meta._websocket) { - const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket); - server.on("upgrade", handleUpgrade); + } -// Scheduled tasks -if (import.meta._tasks) { - startScheduleRunner(); -} export default {}; From 39eecb12d8bc4aec89ae6587cbbe0b6ed417e772 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:52:02 +0000 Subject: [PATCH 2/2] chore: apply automated updates --- src/presets/node/runtime/node-server.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/presets/node/runtime/node-server.ts b/src/presets/node/runtime/node-server.ts index 835a93a588..2087647e66 100644 --- a/src/presets/node/runtime/node-server.ts +++ b/src/presets/node/runtime/node-server.ts @@ -13,23 +13,22 @@ import { } from "nitropack/runtime/internal"; export const serverWorker = async () => { - const cert = process.env.NITRO_SSL_CERT; const key = process.env.NITRO_SSL_KEY; - + const nitroApp = useNitroApp(); - + const server = cert && key ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App)) : new HttpServer(toNodeListener(nitroApp.h3App)); - + const port = (destr(process.env.NITRO_PORT || process.env.PORT) || 3000) as number; const host = process.env.NITRO_HOST || process.env.HOST; - + const path = process.env.NITRO_UNIX_SOCKET; - + // @ts-ignore const listener = server.listen(path ? { path } : { port, host }, (err) => { if (err) { @@ -51,26 +50,24 @@ export const serverWorker = async () => { }:${addressInfo.port}${baseURL}`; console.log(`Listening on ${url}`); }); - + // Trap unhandled errors trapUnhandledNodeErrors(); - + // Graceful shutdown setupGracefulShutdown(listener, nitroApp); - + // Websocket support // https://crossws.unjs.io/adapters/node if (import.meta._websocket) { const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket); server.on("upgrade", handleUpgrade); } - + // Scheduled tasks if (import.meta._tasks) { startScheduleRunner(); } - -} - +}; export default {};