@co0ontty/wand 0.3.0 → 0.4.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.
@@ -18,7 +18,7 @@ export function renderApp(configPath) {
18
18
  <meta name="theme-color" content="#f6f1e8" media="(prefers-color-scheme: light)" />
19
19
  <meta name="theme-color" content="#1f1b17" media="(prefers-color-scheme: dark)" />
20
20
  <meta name="apple-mobile-web-app-capable" content="yes" />
21
- <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
21
+ <meta name="apple-mobile-web-app-status-bar-style" content="default" />
22
22
  <meta name="apple-mobile-web-app-title" content="Wand" />
23
23
  <meta name="mobile-web-app-capable" content="yes" />
24
24
  <meta name="application-name" content="Wand" />
@@ -26,7 +26,7 @@ export function renderApp(configPath) {
26
26
  <meta name="msapplication-TileColor" content="#c5653d" />
27
27
  <meta name="msapplication-tap-highlight" content="no" />
28
28
  <link rel="icon" href="/icon.svg" type="image/svg+xml" />
29
- <link rel="apple-touch-icon" href="/icon.svg" />
29
+ <link rel="apple-touch-icon" href="/icon-192.png" />
30
30
  <link rel="manifest" href="/manifest.json" />
31
31
  <link rel="stylesheet" href="/vendor/xterm/css/xterm.css" />
32
32
  <style>
@@ -0,0 +1,27 @@
1
+ /**
2
+ * WebSocket broadcast manager for process events.
3
+ * Handles debounced output events, backpressure control, and client subscriptions.
4
+ */
5
+ import { WebSocketServer } from "ws";
6
+ import type { SessionSnapshot } from "./types.js";
7
+ export interface ProcessEvent {
8
+ type: "output" | "status" | "started" | "ended" | "usage" | "task";
9
+ sessionId: string;
10
+ data?: unknown;
11
+ }
12
+ export declare class WsBroadcastManager {
13
+ private wss;
14
+ private clients;
15
+ private outputDebounceCache;
16
+ private eventEmitter;
17
+ constructor(wss: WebSocketServer);
18
+ /** Set up connection handling. Should be called once during server startup. */
19
+ setup(getSession: (id: string) => SessionSnapshot | null): void;
20
+ /** Emit a process event to all subscribed WebSocket clients. */
21
+ emitEvent(event: ProcessEvent): void;
22
+ /** Flush any pending debounced output for a session (e.g., before session close). */
23
+ flushOutput(sessionId: string): void;
24
+ private broadcast;
25
+ private processWsQueue;
26
+ private readSessionCookie;
27
+ }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * WebSocket broadcast manager for process events.
3
+ * Handles debounced output events, backpressure control, and client subscriptions.
4
+ */
5
+ import { WebSocket } from "ws";
6
+ import { EventEmitter } from "node:events";
7
+ import { validateSession } from "./auth.js";
8
+ // ── Constants ──
9
+ const MAX_QUEUE_SIZE = 500;
10
+ const OUTPUT_DEBOUNCE_MS = 16;
11
+ // ── Manager ──
12
+ export class WsBroadcastManager {
13
+ wss;
14
+ clients = new Set();
15
+ outputDebounceCache = new Map();
16
+ eventEmitter = new EventEmitter();
17
+ constructor(wss) {
18
+ this.wss = wss;
19
+ }
20
+ /** Set up connection handling. Should be called once during server startup. */
21
+ setup(getSession) {
22
+ this.wss.on("connection", (ws, req) => {
23
+ const sessionToken = this.readSessionCookie(req);
24
+ if (!sessionToken || !validateSession(sessionToken)) {
25
+ ws.close(1008, "Unauthorized");
26
+ return;
27
+ }
28
+ const client = {
29
+ ws,
30
+ sendQueue: [],
31
+ sendInProgress: false,
32
+ backpressurePaused: false,
33
+ lastOutputBySession: new Map(),
34
+ };
35
+ this.clients.add(client);
36
+ ws.on("close", () => {
37
+ this.clients.delete(client);
38
+ });
39
+ ws.on("error", () => {
40
+ // Already closed, ignore
41
+ });
42
+ ws.on("message", (data) => {
43
+ try {
44
+ const msg = JSON.parse(data.toString());
45
+ if (msg.type === "subscribe" && msg.sessionId) {
46
+ const snapshot = getSession(msg.sessionId);
47
+ if (snapshot) {
48
+ ws.send(JSON.stringify({
49
+ type: "init",
50
+ sessionId: msg.sessionId,
51
+ data: { ...snapshot, messages: snapshot.messages, output: snapshot.output },
52
+ }));
53
+ }
54
+ else {
55
+ ws.send(JSON.stringify({
56
+ type: "error",
57
+ sessionId: msg.sessionId,
58
+ error: "Session not found",
59
+ }));
60
+ }
61
+ }
62
+ }
63
+ catch {
64
+ // Ignore malformed messages
65
+ }
66
+ });
67
+ });
68
+ }
69
+ /** Emit a process event to all subscribed WebSocket clients. */
70
+ emitEvent(event) {
71
+ // Debounce output events to reduce flicker during rapid streaming
72
+ if (event.type === "output") {
73
+ const existing = this.outputDebounceCache.get(event.sessionId);
74
+ if (existing) {
75
+ clearTimeout(existing.timer);
76
+ // Accumulate chunk data across debounce window so the browser can
77
+ // write incrementally instead of doing a full terminal reset.
78
+ const prevData = existing.event.data;
79
+ const curData = event.data;
80
+ const prevChunk = prevData?.chunk;
81
+ const curChunk = curData?.chunk;
82
+ if (prevChunk && curChunk) {
83
+ event = { ...event, data: { ...curData, chunk: prevChunk + curChunk } };
84
+ }
85
+ else if (prevChunk && !curChunk) {
86
+ event = { ...event, data: { ...curData, chunk: prevChunk } };
87
+ }
88
+ }
89
+ const timer = setTimeout(() => {
90
+ this.outputDebounceCache.delete(event.sessionId);
91
+ this.broadcast(event);
92
+ }, OUTPUT_DEBOUNCE_MS);
93
+ this.outputDebounceCache.set(event.sessionId, { event, timer });
94
+ return;
95
+ }
96
+ // Non-output events are sent immediately
97
+ this.broadcast(event);
98
+ }
99
+ /** Flush any pending debounced output for a session (e.g., before session close). */
100
+ flushOutput(sessionId) {
101
+ const existing = this.outputDebounceCache.get(sessionId);
102
+ if (existing) {
103
+ clearTimeout(existing.timer);
104
+ this.outputDebounceCache.delete(sessionId);
105
+ this.broadcast(existing.event);
106
+ }
107
+ }
108
+ // ── Internal ──
109
+ broadcast(event) {
110
+ const message = JSON.stringify(event);
111
+ for (const client of this.clients) {
112
+ if (client.ws.readyState !== WebSocket.OPEN)
113
+ continue;
114
+ // Apply backpressure if queue is too large
115
+ if (client.sendQueue.length >= MAX_QUEUE_SIZE) {
116
+ client.backpressurePaused = true;
117
+ continue;
118
+ }
119
+ if (client.backpressurePaused)
120
+ continue;
121
+ client.sendQueue.push(message);
122
+ this.processWsQueue(client);
123
+ }
124
+ }
125
+ processWsQueue(client) {
126
+ if (client.sendInProgress || client.sendQueue.length === 0) {
127
+ return;
128
+ }
129
+ if (client.backpressurePaused) {
130
+ if (client.sendQueue.length < MAX_QUEUE_SIZE * 0.8) {
131
+ client.backpressurePaused = false;
132
+ }
133
+ else {
134
+ return;
135
+ }
136
+ }
137
+ // Check socket state before dequeuing to avoid dropping messages
138
+ if (client.ws.readyState !== WebSocket.OPEN) {
139
+ // Socket closed — discard remaining queue and remove client
140
+ client.sendQueue.length = 0;
141
+ this.clients.delete(client);
142
+ return;
143
+ }
144
+ client.sendInProgress = true;
145
+ const message = client.sendQueue.shift();
146
+ client.ws.send(message, (err) => {
147
+ client.sendInProgress = false;
148
+ if (err)
149
+ return;
150
+ this.processWsQueue(client);
151
+ });
152
+ }
153
+ readSessionCookie(req) {
154
+ const cookie = req.headers.cookie;
155
+ if (!cookie)
156
+ return undefined;
157
+ const match = cookie.split(";").map((part) => part.trim()).find((part) => part.startsWith("wand_session="));
158
+ return match?.slice("wand_session=".length);
159
+ }
160
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@co0ontty/wand",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A web terminal for local CLI tools like Claude.",
5
5
  "type": "module",
6
6
  "bin": {