Skip to content

Commit 72044e9

Browse files
authored
refactor(websocket): enhance response types (#610)
2 parents cda7ae6 + 5a969f4 commit 72044e9

File tree

4 files changed

+70
-28
lines changed

4 files changed

+70
-28
lines changed

workspaces/server/src/websocket/commands/remove.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import type { PayloadsList } from "@nodesecure/cache";
33

44
// Import Internal Dependencies
5-
import type { WebSocketContext } from "../index.js";
5+
import type {
6+
WebSocketContext,
7+
WebSocketResponse
8+
} from "../websocket.types.js";
69

710
export async function* remove(
811
pkg: string,
912
context: WebSocketContext
10-
) {
13+
): AsyncGenerator<WebSocketResponse, void, unknown> {
1114
const { cache, logger } = context;
1215

1316
logger.info(`[ws|remove](pkg: ${pkg})`);

workspaces/server/src/websocket/commands/search.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import * as scanner from "@nodesecure/scanner";
33
import type { PayloadsList } from "@nodesecure/cache";
44

55
// Import Internal Dependencies
6-
import type { WebSocketContext } from "../index.js";
6+
import type {
7+
WebSocketContext,
8+
WebSocketResponse
9+
} from "../websocket.types.js";
710

811
export async function* search(
912
pkg: string,
1013
context: WebSocketContext
11-
) {
14+
): AsyncGenerator<WebSocketResponse, void, unknown> {
1215
const { logger, cache } = context;
1316
logger.info(`[ws|search](pkg: ${pkg})`);
1417

workspaces/server/src/websocket/index.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
// Import Third-party Dependencies
22
import { WebSocketServer, type WebSocket } from "ws";
33
import { 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
87
import { logger } from "../logger.js";
98
import { search } from "./commands/search.js";
109
import { 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

2916
export 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 {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Import Third-party Dependencies
2+
import type { WebSocket } from "ws";
3+
import type { PayloadsList, appCache } from "@nodesecure/cache";
4+
import type { Payload } from "@nodesecure/scanner";
5+
6+
// Import Internal Dependencies
7+
import type { logger } from "../logger.js";
8+
9+
/**
10+
* A (NodeSecure) scan is in progress
11+
*/
12+
type ScanResponse = {
13+
status: "SCAN";
14+
pkg: string;
15+
};
16+
17+
/**
18+
* Initialize or Reload the list of packages available in cache
19+
*/
20+
type CachedResponse = {
21+
status: "INIT" | "RELOAD";
22+
} & PayloadsList;
23+
24+
export type WebSocketResponse =
25+
| Payload
26+
| CachedResponse
27+
| ScanResponse;
28+
29+
export type WebSocketMessage = {
30+
action: "SEARCH" | "REMOVE";
31+
pkg: string;
32+
[key: string]: any;
33+
};
34+
35+
export interface WebSocketContext {
36+
socket: WebSocket;
37+
cache: typeof appCache;
38+
logger: typeof logger;
39+
}

0 commit comments

Comments
 (0)