From b92ad88813314c103949c049d692c5f599fde62c Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 28 Nov 2025 22:45:51 +0100 Subject: [PATCH] refactor(server/ws): inject logger and cache in the constructor --- src/commands/http.js | 6 +++- workspaces/server/README.md | 2 +- workspaces/server/src/websocket/index.ts | 36 ++++++++++++++++-------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/commands/http.js b/src/commands/http.js index ebad023b..11e04a2c 100644 --- a/src/commands/http.js +++ b/src/commands/http.js @@ -10,6 +10,7 @@ import * as SemVer from "semver"; import * as i18n from "@nodesecure/i18n"; import { cache, + logger, buildServer, WebSocketServerInstanciator } from "@nodesecure/server"; @@ -69,7 +70,10 @@ export async function start( open(link); }); - new WebSocketServerInstanciator(); + new WebSocketServerInstanciator({ + cache, + logger + }); for (const eventName of ["SIGINT", "SIGTERM"]) { process.on(eventName, () => { diff --git a/workspaces/server/README.md b/workspaces/server/README.md index 40370f9e..e9157eeb 100644 --- a/workspaces/server/README.md +++ b/workspaces/server/README.md @@ -178,7 +178,7 @@ All static files (UI, assets, etc.) are served from the project root directory. The `WebSocketServerInstanciator` class sets up and manages a WebSocket server for real-time communication with NodeSecure clients. It provides live updates and cache management features for package analysis. ```js -new WebSocketServerInstanciator(); +new WebSocketServerInstanciator({ cache, logger }); ``` - Initializes a WebSocket server on port 1338. - Listens for client connections and incoming messages. diff --git a/workspaces/server/src/websocket/index.ts b/workspaces/server/src/websocket/index.ts index de757027..139c1f0d 100644 --- a/workspaces/server/src/websocket/index.ts +++ b/workspaces/server/src/websocket/index.ts @@ -1,10 +1,10 @@ // Import Third-party Dependencies import { WebSocketServer, type WebSocket } from "ws"; import { match } from "ts-pattern"; +import type { Logger } from "pino"; +import type { AppCache } from "@nodesecure/cache"; // Import Internal Dependencies -import { logger } from "../logger.ts"; -import { cache } from "../cache.ts"; import { search } from "./commands/search.ts"; import { remove } from "./commands/remove.ts"; import { context } from "./websocket.als.ts"; @@ -14,8 +14,20 @@ import type { WebSocketMessage } from "./websocket.types.ts"; +export interface WebSocketServerInstanciatorOptions { + logger: Logger; + cache: AppCache; +} + export class WebSocketServerInstanciator { - constructor() { + #logger: Logger; + #cache: AppCache; + + constructor( + options: WebSocketServerInstanciatorOptions + ) { + this.#logger = options.logger; + this.#cache = options.cache; const websocket = new WebSocketServer({ port: 1338 }); @@ -37,12 +49,12 @@ export class WebSocketServerInstanciator { ) { const ctx: WebSocketContext = { socket, - cache, - logger + cache: this.#cache, + logger: this.#logger }; const commandName = message.commandName; - logger.info(`[ws|command.${commandName.toLowerCase()}] ${message.spec}`); + this.#logger.info(`[ws|command.${commandName.toLowerCase()}] ${message.spec}`); context.run(ctx, async() => { try { @@ -58,8 +70,8 @@ export class WebSocketServerInstanciator { catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); - logger.error(`[ws|command.${commandName}](error: ${errorMessage})`); - logger.debug(error); + this.#logger.error(`[ws|command.${commandName}](error: ${errorMessage})`); + this.#logger.debug(error); } }); } @@ -68,14 +80,14 @@ export class WebSocketServerInstanciator { stopInitializationOnError = false ): Promise { try { - const cached = await cache.payloadsList(); + const cached = await this.#cache.payloadsList(); if ( cached.mru === void 0 || cached.current === void 0 ) { throw new Error("Payloads list not found in cache."); } - logger.info( + this.#logger.info( `[ws|init](current: ${cached.current}|root: ${cached.root})` ); @@ -89,8 +101,8 @@ export class WebSocketServerInstanciator { return null; } - logger.error("[ws|init] creating new payloads list in cache"); - await cache.initPayloadsList(); + this.#logger.error("[ws|init] creating new payloads list in cache"); + await this.#cache.initPayloadsList(); return this.initializeServer(true); }