afield 0.0.0-stage → 0.1.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.
Files changed (43) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +165 -2
  3. package/afield +214 -0
  4. package/package.json +42 -4
  5. package/pi-extensions/afield-session.ts +59 -0
  6. package/pi-extensions/elastic-footer.ts +167 -0
  7. package/server/attach.js +264 -0
  8. package/server/auth.js +169 -0
  9. package/server/client.js +64 -0
  10. package/server/control.js +65 -0
  11. package/server/identity.js +45 -0
  12. package/server/index.js +375 -0
  13. package/server/mirror.js +159 -0
  14. package/server/origin.js +51 -0
  15. package/server/picker.js +187 -0
  16. package/server/places.js +348 -0
  17. package/server/protocol.js +51 -0
  18. package/server/quick-tunnel.js +40 -0
  19. package/server/registry.js +190 -0
  20. package/server/serve.js +100 -0
  21. package/server/session-main.js +27 -0
  22. package/server/session.js +112 -0
  23. package/server/supervisor.js +432 -0
  24. package/server/viewer.js +65 -0
  25. package/src/app.js +1683 -0
  26. package/src/apple-touch-icon.png +0 -0
  27. package/src/index.html +96 -0
  28. package/src/keys.js +59 -0
  29. package/src/pi-icon.svg +13 -0
  30. package/src/style.css +559 -0
  31. package/src/transport.js +153 -0
  32. package/src/ttyd.js +51 -0
  33. package/src/viewport.js +168 -0
  34. package/tsconfig.json +14 -0
  35. package/vendor/wterm/LICENSE +191 -0
  36. package/vendor/wterm/README.md +7 -0
  37. package/vendor/wterm/packages/@wterm/dom/src/debug.ts +347 -0
  38. package/vendor/wterm/packages/@wterm/dom/src/hyperlink.ts +8 -0
  39. package/vendor/wterm/packages/@wterm/dom/src/index.ts +13 -0
  40. package/vendor/wterm/packages/@wterm/dom/src/input.ts +662 -0
  41. package/vendor/wterm/packages/@wterm/dom/src/renderer.ts +725 -0
  42. package/vendor/wterm/packages/@wterm/dom/src/terminal.css +195 -0
  43. package/vendor/wterm/packages/@wterm/dom/src/wterm.ts +609 -0
@@ -0,0 +1,347 @@
1
+ import type { TerminalCore, CellData } from "@wterm/core";
2
+
3
+ const FLAG_NAMES: Record<number, string> = {
4
+ 0x01: "bold",
5
+ 0x02: "dim",
6
+ 0x04: "italic",
7
+ 0x08: "underline",
8
+ 0x10: "blink",
9
+ 0x20: "reverse",
10
+ 0x40: "invisible",
11
+ 0x80: "strikethrough",
12
+ };
13
+
14
+ function flagsToNames(flags: number): string[] {
15
+ const names: string[] = [];
16
+ for (const [bit, name] of Object.entries(FLAG_NAMES)) {
17
+ if (flags & Number(bit)) names.push(name);
18
+ }
19
+ return names;
20
+ }
21
+
22
+ // -- Escape-sequence scanner (lightweight, no terminal emulation) --
23
+
24
+ export interface TraceEntry {
25
+ ts: number;
26
+ type: "csi" | "sgr" | "osc" | "esc" | "text";
27
+ raw: string;
28
+ params?: number[];
29
+ private?: string;
30
+ final?: string;
31
+ }
32
+
33
+ const ESC = 0x1b;
34
+
35
+ function scanSequences(data: string): TraceEntry[] {
36
+ const entries: TraceEntry[] = [];
37
+ const ts = Date.now();
38
+ let i = 0;
39
+ let textStart = 0;
40
+
41
+ const flushText = () => {
42
+ if (i > textStart) {
43
+ const raw = data.slice(textStart, i);
44
+ if (raw.length > 0 && !/^[\x00-\x1f]+$/.test(raw)) {
45
+ entries.push({ ts, type: "text", raw: raw.slice(0, 60) });
46
+ }
47
+ }
48
+ };
49
+
50
+ while (i < data.length) {
51
+ if (data.charCodeAt(i) !== ESC) {
52
+ i++;
53
+ continue;
54
+ }
55
+
56
+ flushText();
57
+ const seqStart = i;
58
+ i++; // skip ESC
59
+
60
+ if (i >= data.length) break;
61
+ const next = data[i];
62
+
63
+ if (next === "[") {
64
+ // CSI sequence
65
+ i++;
66
+ let priv = "";
67
+ if (
68
+ i < data.length &&
69
+ (data[i] === "?" || data[i] === ">" || data[i] === "!")
70
+ ) {
71
+ priv = data[i];
72
+ i++;
73
+ }
74
+ let paramStr = "";
75
+ while (
76
+ i < data.length &&
77
+ ((data.charCodeAt(i) >= 0x30 && data.charCodeAt(i) <= 0x3b) ||
78
+ data[i] === ":")
79
+ ) {
80
+ paramStr += data[i];
81
+ i++;
82
+ }
83
+ // skip intermediates
84
+ while (
85
+ i < data.length &&
86
+ data.charCodeAt(i) >= 0x20 &&
87
+ data.charCodeAt(i) <= 0x2f
88
+ ) {
89
+ i++;
90
+ }
91
+ let final = "";
92
+ if (
93
+ i < data.length &&
94
+ data.charCodeAt(i) >= 0x40 &&
95
+ data.charCodeAt(i) <= 0x7e
96
+ ) {
97
+ final = data[i];
98
+ i++;
99
+ }
100
+ const raw = data.slice(seqStart, i);
101
+ const params = paramStr
102
+ ? paramStr
103
+ .split(/[;:]/)
104
+ .map(Number)
105
+ .filter((n) => !isNaN(n))
106
+ : [];
107
+
108
+ const type = final === "m" ? "sgr" : "csi";
109
+ entries.push({
110
+ ts,
111
+ type,
112
+ raw,
113
+ params: params.length > 0 ? params : undefined,
114
+ private: priv || undefined,
115
+ final,
116
+ });
117
+ } else if (next === "]") {
118
+ // OSC sequence
119
+ i++;
120
+ while (
121
+ i < data.length &&
122
+ data.charCodeAt(i) !== 0x07 &&
123
+ !(
124
+ data.charCodeAt(i) === ESC &&
125
+ i + 1 < data.length &&
126
+ data[i + 1] === "\\"
127
+ )
128
+ ) {
129
+ i++;
130
+ }
131
+ if (i < data.length) {
132
+ if (data.charCodeAt(i) === 0x07) i++;
133
+ else if (data.charCodeAt(i) === ESC) i += 2; // ESC backslash
134
+ }
135
+ const raw = data.slice(seqStart, i);
136
+ entries.push({ ts, type: "osc", raw: raw.slice(0, 80) });
137
+ } else if (next >= " " && next <= "~") {
138
+ // Simple ESC + char
139
+ i++;
140
+ entries.push({
141
+ ts,
142
+ type: "esc",
143
+ raw: data.slice(seqStart, i),
144
+ final: next,
145
+ });
146
+ } else {
147
+ i++;
148
+ }
149
+ textStart = i;
150
+ }
151
+
152
+ flushText();
153
+ return entries;
154
+ }
155
+
156
+ // -- Cell inspector helpers --
157
+
158
+ export interface CellInfo extends CellData {
159
+ charStr: string;
160
+ flagNames: string[];
161
+ }
162
+
163
+ export interface GridSummary {
164
+ rows: number;
165
+ cols: number;
166
+ cursor: { row: number; col: number; visible: boolean };
167
+ altScreen: boolean;
168
+ scrollbackCount: number;
169
+ }
170
+
171
+ // -- Perf stats --
172
+
173
+ export interface PerfStats {
174
+ frameCount: number;
175
+ totalRenderMs: number;
176
+ avgRenderMs: number;
177
+ maxRenderMs: number;
178
+ lastDirtyRows: number;
179
+ }
180
+
181
+ // -- Unhandled sequence entry from WASM --
182
+
183
+ export interface UnhandledEntry {
184
+ final: string;
185
+ private: string;
186
+ paramCount: number;
187
+ params: number[];
188
+ }
189
+
190
+ // -- Main debug adapter --
191
+
192
+ const MAX_TRACES = 500;
193
+
194
+ export class DebugAdapter {
195
+ private _traces: TraceEntry[] = [];
196
+ private _bridge: TerminalCore | null = null;
197
+ private _perf: PerfStats = {
198
+ frameCount: 0,
199
+ totalRenderMs: 0,
200
+ avgRenderMs: 0,
201
+ maxRenderMs: 0,
202
+ lastDirtyRows: 0,
203
+ };
204
+
205
+ get traces(): readonly TraceEntry[] {
206
+ return this._traces;
207
+ }
208
+
209
+ get perf(): Readonly<PerfStats> {
210
+ return this._perf;
211
+ }
212
+
213
+ setBridge(bridge: TerminalCore): void {
214
+ this._bridge = bridge;
215
+ }
216
+
217
+ traceWrite(data: string | Uint8Array): void {
218
+ const str =
219
+ typeof data === "string" ? data : new TextDecoder().decode(data);
220
+ const entries = scanSequences(str);
221
+ for (const entry of entries) {
222
+ this._traces.push(entry);
223
+ }
224
+ if (this._traces.length > MAX_TRACES) {
225
+ this._traces = this._traces.slice(-MAX_TRACES);
226
+ }
227
+ }
228
+
229
+ recordRender(renderMs: number, dirtyRows: number): void {
230
+ this._perf.frameCount++;
231
+ this._perf.totalRenderMs += renderMs;
232
+ this._perf.avgRenderMs = this._perf.totalRenderMs / this._perf.frameCount;
233
+ if (renderMs > this._perf.maxRenderMs) {
234
+ this._perf.maxRenderMs = renderMs;
235
+ }
236
+ this._perf.lastDirtyRows = dirtyRows;
237
+ }
238
+
239
+ resetPerf(): void {
240
+ this._perf = {
241
+ frameCount: 0,
242
+ totalRenderMs: 0,
243
+ avgRenderMs: 0,
244
+ maxRenderMs: 0,
245
+ lastDirtyRows: 0,
246
+ };
247
+ }
248
+
249
+ // -- Cell inspector --
250
+
251
+ cell(row: number, col: number): CellInfo | null {
252
+ if (!this._bridge) return null;
253
+ const c = this._bridge.getCell(row, col);
254
+ return {
255
+ ...c,
256
+ charStr: c.chars ?? (c.char >= 32 ? String.fromCodePoint(c.char) : ""),
257
+ flagNames: flagsToNames(c.flags),
258
+ };
259
+ }
260
+
261
+ row(row: number): CellInfo[] | null {
262
+ if (!this._bridge) return null;
263
+ const cols = this._bridge.getCols();
264
+ const cells: CellInfo[] = [];
265
+ for (let c = 0; c < cols; c++) {
266
+ cells.push(this.cell(row, c)!);
267
+ }
268
+ return cells;
269
+ }
270
+
271
+ grid(): GridSummary | null {
272
+ if (!this._bridge) return null;
273
+ const cursor = this._bridge.getCursor();
274
+ return {
275
+ rows: this._bridge.getRows(),
276
+ cols: this._bridge.getCols(),
277
+ cursor,
278
+ altScreen: this._bridge.usingAltScreen(),
279
+ scrollbackCount: this._bridge.getScrollbackCount(),
280
+ };
281
+ }
282
+
283
+ unhandled(): UnhandledEntry[] {
284
+ if (!this._bridge) return [];
285
+ return this._bridge.getUnhandledSequences();
286
+ }
287
+
288
+ // -- Console-friendly dump --
289
+
290
+ dump(count = 50): void {
291
+ const entries = this._traces.slice(-count);
292
+ console.group(
293
+ `%cwterm debug — last ${entries.length} traces`,
294
+ "color: #569cd6; font-weight: bold",
295
+ );
296
+ for (const e of entries) {
297
+ const badge =
298
+ e.type === "sgr"
299
+ ? "%cSGR"
300
+ : e.type === "csi"
301
+ ? "%cCSI"
302
+ : e.type === "osc"
303
+ ? "%cOSC"
304
+ : e.type === "esc"
305
+ ? "%cESC"
306
+ : "%cTXT";
307
+ const color =
308
+ e.type === "sgr"
309
+ ? "background:#2d5a27;color:#fff;padding:1px 4px;border-radius:2px"
310
+ : e.type === "csi"
311
+ ? "background:#1e4a7a;color:#fff;padding:1px 4px;border-radius:2px"
312
+ : "background:#555;color:#fff;padding:1px 4px;border-radius:2px";
313
+ const detail = [
314
+ e.private ? `private=${e.private}` : "",
315
+ e.params ? `params=[${e.params}]` : "",
316
+ e.final ? `final=${e.final}` : "",
317
+ ]
318
+ .filter(Boolean)
319
+ .join(" ");
320
+
321
+ console.log(
322
+ `${badge} ${e.raw.slice(0, 40)}`,
323
+ color,
324
+ detail ? ` ${detail}` : "",
325
+ );
326
+ }
327
+ console.groupEnd();
328
+ }
329
+
330
+ dumpUnhandled(): void {
331
+ const entries = this.unhandled();
332
+ if (entries.length === 0) {
333
+ console.log("%cwterm debug — no unhandled sequences", "color: #6a9955");
334
+ return;
335
+ }
336
+ console.group(
337
+ `%cwterm debug — ${entries.length} unhandled sequences`,
338
+ "color: #d7ba7d; font-weight: bold",
339
+ );
340
+ for (const e of entries) {
341
+ console.log(
342
+ ` final=${e.final} private=${e.private || "-"} params=[${e.params.slice(0, e.paramCount)}]`,
343
+ );
344
+ }
345
+ console.groupEnd();
346
+ }
347
+ }
@@ -0,0 +1,8 @@
1
+ type ModifierEvent = Pick<KeyboardEvent, "ctrlKey" | "metaKey">;
2
+
3
+ export function isLinkActivationModifier(
4
+ event: ModifierEvent,
5
+ navigator: Navigator,
6
+ ): boolean {
7
+ return navigator.platform.startsWith("Mac") ? event.metaKey : event.ctrlKey;
8
+ }
@@ -0,0 +1,13 @@
1
+ export { WTerm } from "./wterm.js";
2
+ export type { WTermOptions } from "./wterm.js";
3
+ export { Renderer } from "./renderer.js";
4
+ export { InputHandler } from "./input.js";
5
+ export { DebugAdapter } from "./debug.js";
6
+ export type {
7
+ TraceEntry,
8
+ CellInfo,
9
+ GridSummary,
10
+ PerfStats,
11
+ UnhandledEntry,
12
+ } from "./debug.js";
13
+ export * from "@wterm/core";