@wopr-network/defcon 1.1.0 → 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.
- package/dist/src/execution/cli.js +9 -0
- package/dist/src/ws/broadcast.d.ts +21 -0
- package/dist/src/ws/broadcast.js +105 -0
- package/package.json +3 -1
|
@@ -22,6 +22,7 @@ import { DrizzleInvocationRepository } from "../repositories/drizzle/invocation.
|
|
|
22
22
|
import * as schema from "../repositories/drizzle/schema.js";
|
|
23
23
|
import { entities, entityHistory, flowDefinitions, flowVersions, gateDefinitions, gateResults, invocations, stateDefinitions, transitionRules, } from "../repositories/drizzle/schema.js";
|
|
24
24
|
import { DrizzleTransitionLogRepository } from "../repositories/drizzle/transition-log.repo.js";
|
|
25
|
+
import { WebSocketBroadcaster } from "../ws/broadcast.js";
|
|
25
26
|
import { createMcpServer, startStdioServer } from "./mcp-server.js";
|
|
26
27
|
import { provisionWorktree } from "./provision-worktree.js";
|
|
27
28
|
const DB_DEFAULT = process.env.DEFCON_DB_PATH ?? "./defcon.db";
|
|
@@ -244,6 +245,14 @@ program
|
|
|
244
245
|
workerToken,
|
|
245
246
|
corsOrigin: restCorsResult.origin ?? undefined,
|
|
246
247
|
});
|
|
248
|
+
if (adminToken) {
|
|
249
|
+
const wsBroadcaster = new WebSocketBroadcaster({
|
|
250
|
+
server: restHttpServer,
|
|
251
|
+
engine,
|
|
252
|
+
adminToken,
|
|
253
|
+
});
|
|
254
|
+
eventEmitter.register(wsBroadcaster);
|
|
255
|
+
}
|
|
247
256
|
restHttpServer.listen(httpPort, httpHost, () => {
|
|
248
257
|
const addr = restHttpServer?.address();
|
|
249
258
|
const boundPort = addr && typeof addr === "object" ? addr.port : httpPort;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type http from "node:http";
|
|
2
|
+
import type { Engine } from "../engine/engine.js";
|
|
3
|
+
import type { EngineEvent, IEventBusAdapter } from "../engine/event-types.js";
|
|
4
|
+
export interface WebSocketBroadcasterDeps {
|
|
5
|
+
server: http.Server;
|
|
6
|
+
engine: Engine;
|
|
7
|
+
adminToken: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class WebSocketBroadcaster implements IEventBusAdapter {
|
|
10
|
+
private wss;
|
|
11
|
+
private engine;
|
|
12
|
+
private adminToken;
|
|
13
|
+
constructor(deps: WebSocketBroadcasterDeps);
|
|
14
|
+
private isWsPath;
|
|
15
|
+
private authenticate;
|
|
16
|
+
private tokenMatches;
|
|
17
|
+
private sendSnapshot;
|
|
18
|
+
emit(event: EngineEvent): Promise<void>;
|
|
19
|
+
get clientCount(): number;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wopr-network/defcon",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"packageManager": "pnpm@9.15.4",
|
|
6
6
|
"engines": {
|
|
@@ -45,12 +45,14 @@
|
|
|
45
45
|
"commander": "^14.0.3",
|
|
46
46
|
"drizzle-orm": "^0.45.1",
|
|
47
47
|
"handlebars": "^4.7.8",
|
|
48
|
+
"ws": "^8.19.0",
|
|
48
49
|
"zod": "^4.3.6"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
52
|
"@biomejs/biome": "^2.4.4",
|
|
52
53
|
"@types/better-sqlite3": "^7.6.13",
|
|
53
54
|
"@types/node": "^25.3.3",
|
|
55
|
+
"@types/ws": "^8.18.1",
|
|
54
56
|
"@vitest/coverage-v8": "^4.0.18",
|
|
55
57
|
"drizzle-kit": "^0.31.4",
|
|
56
58
|
"tsx": "^4.21.0",
|