Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/commands/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as SemVer from "semver";
import * as i18n from "@nodesecure/i18n";
import {
cache,
logger,
buildServer,
WebSocketServerInstanciator
} from "@nodesecure/server";
Expand Down Expand Up @@ -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, () => {
Expand Down
2 changes: 1 addition & 1 deletion workspaces/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 24 additions & 12 deletions workspaces/server/src/websocket/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -14,8 +14,20 @@ import type {
WebSocketMessage
} from "./websocket.types.ts";

export interface WebSocketServerInstanciatorOptions {
logger: Logger<never, boolean>;
cache: AppCache;
}

export class WebSocketServerInstanciator {
constructor() {
#logger: Logger<never, boolean>;
#cache: AppCache;

constructor(
options: WebSocketServerInstanciatorOptions
) {
this.#logger = options.logger;
this.#cache = options.cache;
const websocket = new WebSocketServer({
port: 1338
});
Expand All @@ -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 {
Expand All @@ -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);
}
});
}
Expand All @@ -68,14 +80,14 @@ export class WebSocketServerInstanciator {
stopInitializationOnError = false
): Promise<WebSocketResponse | null> {
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})`
);

Expand All @@ -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);
}
Expand Down