@wopr-network/defcon 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ import { createHash, timingSafeEqual } from "node:crypto";
2
+ import { WebSocketServer } from "ws";
3
+ export class WebSocketBroadcaster {
4
+ wss;
5
+ engine;
6
+ adminToken;
7
+ constructor(deps) {
8
+ this.engine = deps.engine;
9
+ this.adminToken = deps.adminToken;
10
+ this.wss = new WebSocketServer({ noServer: true });
11
+ deps.server.on("upgrade", (req, socket, head) => {
12
+ if (!this.isWsPath(req.url)) {
13
+ socket.destroy();
14
+ return;
15
+ }
16
+ if (!this.authenticate(req)) {
17
+ socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
18
+ socket.destroy();
19
+ return;
20
+ }
21
+ this.wss.handleUpgrade(req, socket, head, (ws) => {
22
+ this.wss.emit("connection", ws, req);
23
+ });
24
+ });
25
+ this.wss.on("connection", (ws) => {
26
+ this.sendSnapshot(ws);
27
+ ws.on("close", () => {
28
+ // Client removed automatically from wss.clients on close
29
+ });
30
+ });
31
+ }
32
+ isWsPath(url) {
33
+ if (!url)
34
+ return false;
35
+ const pathname = url.split("?")[0];
36
+ return pathname === "/ws";
37
+ }
38
+ authenticate(req) {
39
+ // Try Authorization header first
40
+ const authHeader = req.headers.authorization;
41
+ if (authHeader) {
42
+ const lower = authHeader.toLowerCase();
43
+ if (lower.startsWith("bearer ")) {
44
+ const token = authHeader.slice(7).trim();
45
+ if (token && this.tokenMatches(token))
46
+ return true;
47
+ }
48
+ }
49
+ // Try ?token= query param
50
+ const url = new URL(req.url ?? "/", "http://localhost");
51
+ const queryToken = url.searchParams.get("token");
52
+ if (queryToken && this.tokenMatches(queryToken))
53
+ return true;
54
+ return false;
55
+ }
56
+ tokenMatches(callerToken) {
57
+ const hashA = createHash("sha256").update(this.adminToken).digest();
58
+ const hashB = createHash("sha256").update(callerToken).digest();
59
+ return timingSafeEqual(hashA, hashB);
60
+ }
61
+ async sendSnapshot(ws) {
62
+ try {
63
+ const status = await this.engine.getStatus();
64
+ const msg = JSON.stringify({
65
+ type: "snapshot",
66
+ payload: { status },
67
+ timestamp: new Date().toISOString(),
68
+ });
69
+ if (ws.readyState === ws.OPEN) {
70
+ ws.send(msg);
71
+ }
72
+ }
73
+ catch {
74
+ if (ws.readyState === ws.OPEN) {
75
+ ws.send(JSON.stringify({
76
+ type: "snapshot",
77
+ payload: { status: { flows: {}, activeInvocations: 0, pendingClaims: 0 } },
78
+ timestamp: new Date().toISOString(),
79
+ }));
80
+ }
81
+ }
82
+ }
83
+ async emit(event) {
84
+ const { type, emittedAt, ...rest } = event;
85
+ const msg = JSON.stringify({
86
+ type,
87
+ payload: rest,
88
+ timestamp: emittedAt.toISOString(),
89
+ });
90
+ for (const client of this.wss.clients) {
91
+ if (client.readyState === client.OPEN) {
92
+ client.send(msg);
93
+ }
94
+ }
95
+ }
96
+ get clientCount() {
97
+ return this.wss.clients.size;
98
+ }
99
+ close() {
100
+ for (const client of this.wss.clients) {
101
+ client.terminate();
102
+ }
103
+ return new Promise((resolve) => this.wss.close(() => resolve()));
104
+ }
105
+ }
@@ -0,0 +1,19 @@
1
+ PRAGMA foreign_keys=OFF;--> statement-breakpoint
2
+ CREATE TABLE `__new_gate_definitions` (
3
+ `id` text PRIMARY KEY NOT NULL,
4
+ `name` text NOT NULL,
5
+ `type` text NOT NULL,
6
+ `command` text,
7
+ `function_ref` text,
8
+ `api_config` text,
9
+ `timeout_ms` integer,
10
+ `failure_prompt` text,
11
+ `timeout_prompt` text
12
+ );
13
+ --> statement-breakpoint
14
+ INSERT INTO `__new_gate_definitions`("id", "name", "type", "command", "function_ref", "api_config", "timeout_ms", "failure_prompt", "timeout_prompt") SELECT "id", "name", "type", "command", "function_ref", "api_config", "timeout_ms", "failure_prompt", "timeout_prompt" FROM `gate_definitions`;--> statement-breakpoint
15
+ DROP TABLE `gate_definitions`;--> statement-breakpoint
16
+ ALTER TABLE `__new_gate_definitions` RENAME TO `gate_definitions`;--> statement-breakpoint
17
+ PRAGMA foreign_keys=ON;--> statement-breakpoint
18
+ CREATE UNIQUE INDEX `gate_definitions_name_unique` ON `gate_definitions` (`name`);--> statement-breakpoint
19
+ ALTER TABLE `flow_definitions` ADD `paused` integer DEFAULT 0;