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
7 changes: 5 additions & 2 deletions workspaces/server/src/websocket/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebSocketResponse, void, unknown> {
const { cache, logger } = context;

logger.info(`[ws|remove](pkg: ${pkg})`);
Expand Down
7 changes: 5 additions & 2 deletions workspaces/server/src/websocket/commands/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebSocketResponse, void, unknown> {
const { logger, cache } = context;
logger.info(`[ws|search](pkg: ${pkg})`);

Expand Down
45 changes: 21 additions & 24 deletions workspaces/server/src/websocket/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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))
Expand All @@ -64,12 +55,17 @@ export class WebSocketServerInstanciator {

async initializeServer(
stopInitializationOnError = false
) {
): Promise<WebSocketResponse | null> {
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.");
}

Expand All @@ -79,7 +75,8 @@ export class WebSocketServerInstanciator {
mru,
lru,
availables,
root
root,
lastUsed
};
}
catch {
Expand Down
39 changes: 39 additions & 0 deletions workspaces/server/src/websocket/websocket.types.ts
Original file line number Diff line number Diff line change
@@ -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;
}