feat: init
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
import { useNitroApp } from "nitropack/runtime";
|
||||
import { normalize } from "pathe";
|
||||
const nitroApp = useNitroApp();
|
||||
async function cli() {
|
||||
const url = process.argv[2] || "/";
|
||||
const debug = (label, ...args) => console.debug(`> ${label}:`, ...args);
|
||||
const r = await nitroApp.localCall({ url });
|
||||
debug("URL", url);
|
||||
debug("StatusCode", r.status);
|
||||
debug("StatusMessage", r.statusText);
|
||||
for (const header of Object.entries(r.headers)) {
|
||||
debug(header[0], header[1]);
|
||||
}
|
||||
console.log("\n", r.body?.toString());
|
||||
}
|
||||
if (process.argv.some((arg) => import.meta.url.includes(normalize(arg)))) {
|
||||
cli().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
import cluster from "node:cluster";
|
||||
import os from "node:os";
|
||||
import {
|
||||
getGracefulShutdownConfig,
|
||||
trapUnhandledNodeErrors
|
||||
} from "nitropack/runtime/internal";
|
||||
function runMaster() {
|
||||
const numberOfWorkers = Number.parseInt(process.env.NITRO_CLUSTER_WORKERS || "") || (os.cpus().length > 0 ? os.cpus().length : 1);
|
||||
for (let i = 0; i < numberOfWorkers; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
let isShuttingDown = false;
|
||||
cluster.on("exit", () => {
|
||||
if (!isShuttingDown) {
|
||||
cluster.fork();
|
||||
}
|
||||
});
|
||||
const shutdownConfig = getGracefulShutdownConfig();
|
||||
if (!shutdownConfig.disabled) {
|
||||
async function onShutdown() {
|
||||
if (isShuttingDown) {
|
||||
return;
|
||||
}
|
||||
isShuttingDown = true;
|
||||
await new Promise((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
console.warn("Timeout reached for graceful shutdown. Forcing exit.");
|
||||
resolve();
|
||||
}, shutdownConfig.timeout);
|
||||
cluster.on("exit", () => {
|
||||
if (Object.values(cluster.workers || {}).every((w) => !w || w.isDead())) {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
} else {
|
||||
}
|
||||
});
|
||||
});
|
||||
if (shutdownConfig.forceExit) {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
for (const signal of shutdownConfig.signals) {
|
||||
process.once(signal, onShutdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
function runWorker() {
|
||||
import("./node-server.mjs").catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
trapUnhandledNodeErrors();
|
||||
if (cluster.isPrimary) {
|
||||
runMaster();
|
||||
} else {
|
||||
runWorker();
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
export declare const listener: import("h3").NodeListener;
|
||||
/** @experimental */
|
||||
export declare const websocket: any;
|
||||
/** @deprecated use new `listener` export instead */
|
||||
export declare const handler: import("h3").NodeListener;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
import { toNodeListener } from "h3";
|
||||
import { useNitroApp } from "nitropack/runtime";
|
||||
import {
|
||||
startScheduleRunner,
|
||||
trapUnhandledNodeErrors
|
||||
} from "nitropack/runtime/internal";
|
||||
const nitroApp = useNitroApp();
|
||||
export const listener = toNodeListener(nitroApp.h3App);
|
||||
export const websocket = import.meta._websocket ? nitroApp.h3App.websocket : void 0;
|
||||
export const handler = listener;
|
||||
trapUnhandledNodeErrors();
|
||||
if (import.meta._tasks) {
|
||||
startScheduleRunner();
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
declare const _default: {};
|
||||
export default _default;
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import "#nitro-internal-pollyfills";
|
||||
import { Server as HttpServer } from "node:http";
|
||||
import { Server as HttpsServer } from "node:https";
|
||||
import wsAdapter from "crossws/adapters/node";
|
||||
import destr from "destr";
|
||||
import { toNodeListener } from "h3";
|
||||
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
|
||||
import {
|
||||
setupGracefulShutdown,
|
||||
startScheduleRunner,
|
||||
trapUnhandledNodeErrors
|
||||
} from "nitropack/runtime/internal";
|
||||
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) || 3e3;
|
||||
const host = process.env.NITRO_HOST || process.env.HOST;
|
||||
const path = process.env.NITRO_UNIX_SOCKET;
|
||||
const listener = server.listen(path ? { path } : { port, host }, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
const protocol = cert && key ? "https" : "http";
|
||||
const addressInfo = listener.address();
|
||||
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}`);
|
||||
});
|
||||
trapUnhandledNodeErrors();
|
||||
setupGracefulShutdown(listener, nitroApp);
|
||||
if (import.meta._websocket) {
|
||||
const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket);
|
||||
server.on("upgrade", handleUpgrade);
|
||||
}
|
||||
if (import.meta._tasks) {
|
||||
startScheduleRunner();
|
||||
}
|
||||
export default {};
|
||||
Reference in New Issue
Block a user