Skip to content

Commit fa8a118

Browse files
authored
refactor(server/ws): inject logger and cache in the constructor (#620)
1 parent dde24ce commit fa8a118

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/commands/http.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as SemVer from "semver";
1010
import * as i18n from "@nodesecure/i18n";
1111
import {
1212
cache,
13+
logger,
1314
buildServer,
1415
WebSocketServerInstanciator
1516
} from "@nodesecure/server";
@@ -69,7 +70,10 @@ export async function start(
6970
open(link);
7071
});
7172

72-
new WebSocketServerInstanciator();
73+
new WebSocketServerInstanciator({
74+
cache,
75+
logger
76+
});
7377

7478
for (const eventName of ["SIGINT", "SIGTERM"]) {
7579
process.on(eventName, () => {

workspaces/server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ All static files (UI, assets, etc.) are served from the project root directory.
178178
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.
179179
180180
```js
181-
new WebSocketServerInstanciator();
181+
new WebSocketServerInstanciator({ cache, logger });
182182
```
183183
- Initializes a WebSocket server on port 1338.
184184
- Listens for client connections and incoming messages.

workspaces/server/src/websocket/index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Import Third-party Dependencies
22
import { WebSocketServer, type WebSocket } from "ws";
33
import { match } from "ts-pattern";
4+
import type { Logger } from "pino";
5+
import type { AppCache } from "@nodesecure/cache";
46

57
// Import Internal Dependencies
6-
import { logger } from "../logger.ts";
7-
import { cache } from "../cache.ts";
88
import { search } from "./commands/search.ts";
99
import { remove } from "./commands/remove.ts";
1010
import { context } from "./websocket.als.ts";
@@ -14,8 +14,20 @@ import type {
1414
WebSocketMessage
1515
} from "./websocket.types.ts";
1616

17+
export interface WebSocketServerInstanciatorOptions {
18+
logger: Logger<never, boolean>;
19+
cache: AppCache;
20+
}
21+
1722
export class WebSocketServerInstanciator {
18-
constructor() {
23+
#logger: Logger<never, boolean>;
24+
#cache: AppCache;
25+
26+
constructor(
27+
options: WebSocketServerInstanciatorOptions
28+
) {
29+
this.#logger = options.logger;
30+
this.#cache = options.cache;
1931
const websocket = new WebSocketServer({
2032
port: 1338
2133
});
@@ -37,12 +49,12 @@ export class WebSocketServerInstanciator {
3749
) {
3850
const ctx: WebSocketContext = {
3951
socket,
40-
cache,
41-
logger
52+
cache: this.#cache,
53+
logger: this.#logger
4254
};
4355

4456
const commandName = message.commandName;
45-
logger.info(`[ws|command.${commandName.toLowerCase()}] ${message.spec}`);
57+
this.#logger.info(`[ws|command.${commandName.toLowerCase()}] ${message.spec}`);
4658

4759
context.run(ctx, async() => {
4860
try {
@@ -58,8 +70,8 @@ export class WebSocketServerInstanciator {
5870
catch (error) {
5971
const errorMessage = error instanceof Error ? error.message : String(error);
6072

61-
logger.error(`[ws|command.${commandName}](error: ${errorMessage})`);
62-
logger.debug(error);
73+
this.#logger.error(`[ws|command.${commandName}](error: ${errorMessage})`);
74+
this.#logger.debug(error);
6375
}
6476
});
6577
}
@@ -68,14 +80,14 @@ export class WebSocketServerInstanciator {
6880
stopInitializationOnError = false
6981
): Promise<WebSocketResponse | null> {
7082
try {
71-
const cached = await cache.payloadsList();
83+
const cached = await this.#cache.payloadsList();
7284
if (
7385
cached.mru === void 0 ||
7486
cached.current === void 0
7587
) {
7688
throw new Error("Payloads list not found in cache.");
7789
}
78-
logger.info(
90+
this.#logger.info(
7991
`[ws|init](current: ${cached.current}|root: ${cached.root})`
8092
);
8193

@@ -89,8 +101,8 @@ export class WebSocketServerInstanciator {
89101
return null;
90102
}
91103

92-
logger.error("[ws|init] creating new payloads list in cache");
93-
await cache.initPayloadsList();
104+
this.#logger.error("[ws|init] creating new payloads list in cache");
105+
await this.#cache.initPayloadsList();
94106

95107
return this.initializeServer(true);
96108
}

0 commit comments

Comments
 (0)