UNPKG

@sigiljs/sigil

Version:

TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating

116 lines (115 loc) 3.93 kB
import l from "node:http"; import o from "node:https"; import n from "../route/route.mjs"; import a from "../utils/make-log.mjs"; import { defaultTemplate as p } from "./misc/response-templates.mjs"; import { getReadableServerAddress as u } from "../utils/get-readable-server-address.mjs"; import g from "../utils/setup-graceful-shutdown.mjs"; class z { /** * Bound logger function configured with Sigil debug options. */ logger; /** * Underlying HTTP or HTTPS server instance, undefined in serverless mode. */ $server; /** * Reference to the handler function for incoming HTTP messages. */ $incomingMessageHandlerRef; /** * Registered middleware callbacks, keyed by identifier. */ $middlewares = /* @__PURE__ */ new Map(); /** * Registered plugins for extending framework behavior. */ $plugins = /* @__PURE__ */ new Map(); /** * Template function for generating HTTP responses. */ $responseTemplate; /** * Set of registered routes with their mount paths. */ $routes = /* @__PURE__ */ new Set(); /** * Sigil runtime configuration options. */ $options; /** * Root router instance for mounting application routes. */ $root = new n(); /** * Flag indicating whether the framework has completed initialization. */ $initialized = !1; /** * Internal flag to prevent multiple plugin initializations. */ $pluginsInitialized = !1; /** * Constructs the Sigil core framework instance. * Initializes logger, HTTP/S server (unless serverless), response template, * and triggers initial plugin updates in serverless mode. * * @param options configuration options for Sigil (server, debug, etc.). */ constructor(s) { this.$options = s || {}, this.logger = a.bind({}, this.$options.debug); const t = this.$options.server?.https; this.$server = s?.serverless ? void 0 : t?.cert ? o.createServer(t, (e, i) => this.$incomingMessageHandlerRef?.(e, i)) : l.createServer((e, i) => this.$incomingMessageHandlerRef?.(e, i)), this.$server?.on("listening", () => { const e = u(this.$server), i = t?.cert ? "https" : "http"; this.logger({ message: `Application successfully started @ ${i}://${e.address}:${e.port}`, level: "success", module: "sigil", json: { milestone: "start", ok: !0, serverless: !1, protocol: i } }); for (const r of this.$plugins.values()) r.onInternalServerStarted(this.$server); }), this.$responseTemplate = s?.responseTemplate ?? p, s?.serverless && (this.$initialized = !0, this.logger({ message: "Application internal server will not be started due to serverless mode", level: "warning", module: "sigil", json: { milestone: "start", ok: !0, serverless: !0 } }), this.$updateCallback().catch((e) => { console.error(e); })), g({ server: this.$server, timeoutMs: 100, cleanup: async () => { for (const e of this.$plugins.values()) await e.onBeforeExit(); } }); } /** * Callback to initialize and update all registered plugins. * Called once when the server is ready and on subsequent route updates. * * @returns promise resolving after plugin hooks are executed. */ async $updateCallback() { if (this.$initialized) { if (!this.$pluginsInitialized) { this.$pluginsInitialized = !0; for (const t of this.$plugins.values()) await t.onInitialize(); const s = Array.from(this.$plugins.values()).map((t) => t.name); this.logger({ message: `Successfully initialized ${this.$plugins.size} plugin(s): ${s.join(", ")}`, level: "success", module: "registry", json: { milestone: "plugin", ok: !0, plugins: s } }); } for (const s of this.$plugins.values()) s.onUpdateCallback(); } } } export { z as default };