From 5a969f493c506bf9eddc20a29206ab2ca80d8e21 Mon Sep 17 00:00:00 2001 From: fraxken Date: Thu, 27 Nov 2025 14:26:46 +0100 Subject: [PATCH] refactor(websocket): enhance response types --- .../server/src/websocket/commands/remove.ts | 7 ++- .../server/src/websocket/commands/search.ts | 7 ++- workspaces/server/src/websocket/index.ts | 45 +++++++++---------- .../server/src/websocket/websocket.types.ts | 39 ++++++++++++++++ 4 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 workspaces/server/src/websocket/websocket.types.ts diff --git a/workspaces/server/src/websocket/commands/remove.ts b/workspaces/server/src/websocket/commands/remove.ts index 25e88fac..846ecb13 100644 --- a/workspaces/server/src/websocket/commands/remove.ts +++ b/workspaces/server/src/websocket/commands/remove.ts @@ -2,12 +2,15 @@ import type { PayloadsList } from "@nodesecure/cache"; // Import Internal Dependencies -import type { WebSocketContext } from "../index.js"; +import type { + WebSocketContext, + WebSocketResponse +} from "../websocket.types.js"; export async function* remove( pkg: string, context: WebSocketContext -) { +): AsyncGenerator { const { cache, logger } = context; logger.info(`[ws|remove](pkg: ${pkg})`); diff --git a/workspaces/server/src/websocket/commands/search.ts b/workspaces/server/src/websocket/commands/search.ts index be605f8d..5e8b1e95 100644 --- a/workspaces/server/src/websocket/commands/search.ts +++ b/workspaces/server/src/websocket/commands/search.ts @@ -3,12 +3,15 @@ import * as scanner from "@nodesecure/scanner"; import type { PayloadsList } from "@nodesecure/cache"; // Import Internal Dependencies -import type { WebSocketContext } from "../index.js"; +import type { + WebSocketContext, + WebSocketResponse +} from "../websocket.types.js"; export async function* search( pkg: string, context: WebSocketContext -) { +): AsyncGenerator { const { logger, cache } = context; logger.info(`[ws|search](pkg: ${pkg})`); diff --git a/workspaces/server/src/websocket/index.ts b/workspaces/server/src/websocket/index.ts index 7050d353..e79ab633 100644 --- a/workspaces/server/src/websocket/index.ts +++ b/workspaces/server/src/websocket/index.ts @@ -1,30 +1,17 @@ // Import Third-party Dependencies import { WebSocketServer, type WebSocket } from "ws"; import { match } from "ts-pattern"; -import { appCache, type PayloadsList } from "@nodesecure/cache"; -import type { Payload } from "@nodesecure/scanner"; +import { appCache } from "@nodesecure/cache"; // Import Internal Dependencies import { logger } from "../logger.js"; import { search } from "./commands/search.js"; import { remove } from "./commands/remove.js"; - -export interface WebSocketContext { - socket: WebSocket; - cache: typeof appCache; - logger: typeof logger; -} - -export type WebSocketMessage = { - action: "SEARCH" | "REMOVE"; - pkg: string; - [key: string]: any; -}; - -type WebSocketResponse = Payload | PayloadsList | { - status: "RELOAD" | "SCAN"; - pkg: string; -}; +import type { + WebSocketResponse, + WebSocketContext, + WebSocketMessage +} from "./websocket.types.js"; export class WebSocketServerInstanciator { constructor() { @@ -50,7 +37,11 @@ export class WebSocketServerInstanciator { socket: WebSocket, message: WebSocketMessage ) { - const ctx = { socket, cache: appCache, logger }; + const ctx: WebSocketContext = { + socket, + cache: appCache, + logger + }; const socketMessages = match(message.action) .with("SEARCH", () => search(message.pkg, ctx)) @@ -64,12 +55,17 @@ export class WebSocketServerInstanciator { async initializeServer( stopInitializationOnError = false - ) { + ): Promise { try { - const { current, mru, lru, availables, root } = await appCache.payloadsList(); + const { + current, mru, lru, availables, root, lastUsed + } = await appCache.payloadsList(); logger.info(`[ws|init](mru: ${mru}|lru: ${lru}|availables: ${availables}|current: ${current}|root: ${root})`); - if (mru === void 0 || current === void 0) { + if ( + mru === void 0 || + current === void 0 + ) { throw new Error("Payloads list not found in cache."); } @@ -79,7 +75,8 @@ export class WebSocketServerInstanciator { mru, lru, availables, - root + root, + lastUsed }; } catch { diff --git a/workspaces/server/src/websocket/websocket.types.ts b/workspaces/server/src/websocket/websocket.types.ts new file mode 100644 index 00000000..6897d873 --- /dev/null +++ b/workspaces/server/src/websocket/websocket.types.ts @@ -0,0 +1,39 @@ +// Import Third-party Dependencies +import type { WebSocket } from "ws"; +import type { PayloadsList, appCache } from "@nodesecure/cache"; +import type { Payload } from "@nodesecure/scanner"; + +// Import Internal Dependencies +import type { logger } from "../logger.js"; + +/** + * A (NodeSecure) scan is in progress + */ +type ScanResponse = { + status: "SCAN"; + pkg: string; +}; + +/** + * Initialize or Reload the list of packages available in cache + */ +type CachedResponse = { + status: "INIT" | "RELOAD"; +} & PayloadsList; + +export type WebSocketResponse = + | Payload + | CachedResponse + | ScanResponse; + +export type WebSocketMessage = { + action: "SEARCH" | "REMOVE"; + pkg: string; + [key: string]: any; +}; + +export interface WebSocketContext { + socket: WebSocket; + cache: typeof appCache; + logger: typeof logger; +}