11// Import Third-party Dependencies
22import { WebSocketServer , type WebSocket } from "ws" ;
33import { match } from "ts-pattern" ;
4- import { appCache , type PayloadsList } from "@nodesecure/cache" ;
5- import type { Payload } from "@nodesecure/scanner" ;
4+ import { appCache } from "@nodesecure/cache" ;
65
76// Import Internal Dependencies
87import { logger } from "../logger.js" ;
98import { search } from "./commands/search.js" ;
109import { remove } from "./commands/remove.js" ;
11-
12- export interface WebSocketContext {
13- socket : WebSocket ;
14- cache : typeof appCache ;
15- logger : typeof logger ;
16- }
17-
18- export type WebSocketMessage = {
19- action : "SEARCH" | "REMOVE" ;
20- pkg : string ;
21- [ key : string ] : any ;
22- } ;
23-
24- type WebSocketResponse = Payload | PayloadsList | {
25- status : "RELOAD" | "SCAN" ;
26- pkg : string ;
27- } ;
10+ import type {
11+ WebSocketResponse ,
12+ WebSocketContext ,
13+ WebSocketMessage
14+ } from "./websocket.types.js" ;
2815
2916export class WebSocketServerInstanciator {
3017 constructor ( ) {
@@ -50,7 +37,11 @@ export class WebSocketServerInstanciator {
5037 socket : WebSocket ,
5138 message : WebSocketMessage
5239 ) {
53- const ctx = { socket, cache : appCache , logger } ;
40+ const ctx : WebSocketContext = {
41+ socket,
42+ cache : appCache ,
43+ logger
44+ } ;
5445
5546 const socketMessages = match ( message . action )
5647 . with ( "SEARCH" , ( ) => search ( message . pkg , ctx ) )
@@ -64,12 +55,17 @@ export class WebSocketServerInstanciator {
6455
6556 async initializeServer (
6657 stopInitializationOnError = false
67- ) {
58+ ) : Promise < WebSocketResponse | null > {
6859 try {
69- const { current, mru, lru, availables, root } = await appCache . payloadsList ( ) ;
60+ const {
61+ current, mru, lru, availables, root, lastUsed
62+ } = await appCache . payloadsList ( ) ;
7063 logger . info ( `[ws|init](mru: ${ mru } |lru: ${ lru } |availables: ${ availables } |current: ${ current } |root: ${ root } )` ) ;
7164
72- if ( mru === void 0 || current === void 0 ) {
65+ if (
66+ mru === void 0 ||
67+ current === void 0
68+ ) {
7369 throw new Error ( "Payloads list not found in cache." ) ;
7470 }
7571
@@ -79,7 +75,8 @@ export class WebSocketServerInstanciator {
7975 mru,
8076 lru,
8177 availables,
82- root
78+ root,
79+ lastUsed
8380 } ;
8481 }
8582 catch {
0 commit comments