@gkoreli/ghostty-vt-js 0.3.2
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/LICENSE +21 -0
- package/README.md +89 -0
- package/THIRD_PARTY_NOTICES.md +45 -0
- package/dist/browser-terminal/buffer.d.ts +93 -0
- package/dist/browser-terminal/buffer.d.ts.map +1 -0
- package/dist/browser-terminal/buffer.js +337 -0
- package/dist/browser-terminal/buffer.js.map +1 -0
- package/dist/browser-terminal/encoding.d.ts +17 -0
- package/dist/browser-terminal/encoding.d.ts.map +1 -0
- package/dist/browser-terminal/encoding.js +22 -0
- package/dist/browser-terminal/encoding.js.map +1 -0
- package/dist/browser-terminal/event-emitter.d.ts +8 -0
- package/dist/browser-terminal/event-emitter.d.ts.map +1 -0
- package/dist/browser-terminal/event-emitter.js +27 -0
- package/dist/browser-terminal/event-emitter.js.map +1 -0
- package/dist/browser-terminal/frame-hold.d.ts +13 -0
- package/dist/browser-terminal/frame-hold.d.ts.map +1 -0
- package/dist/browser-terminal/frame-hold.js +17 -0
- package/dist/browser-terminal/frame-hold.js.map +1 -0
- package/dist/browser-terminal/geometry.d.ts +229 -0
- package/dist/browser-terminal/geometry.d.ts.map +1 -0
- package/dist/browser-terminal/geometry.js +299 -0
- package/dist/browser-terminal/geometry.js.map +1 -0
- package/dist/browser-terminal/ghostty.d.ts +478 -0
- package/dist/browser-terminal/ghostty.d.ts.map +1 -0
- package/dist/browser-terminal/ghostty.js +1212 -0
- package/dist/browser-terminal/ghostty.js.map +1 -0
- package/dist/browser-terminal/gpu/adapter.d.ts +70 -0
- package/dist/browser-terminal/gpu/adapter.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/adapter.js +260 -0
- package/dist/browser-terminal/gpu/adapter.js.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts +33 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js +159 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts +35 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js +73 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts +68 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.js +167 -0
- package/dist/browser-terminal/gpu/core/frame.js.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts +136 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js +281 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts +24 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js +316 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts +107 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js +698 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts +15 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.js +33 -0
- package/dist/browser-terminal/gpu/create-renderer.js.map +1 -0
- package/dist/browser-terminal/index.d.ts +47 -0
- package/dist/browser-terminal/index.d.ts.map +1 -0
- package/dist/browser-terminal/index.js +75 -0
- package/dist/browser-terminal/index.js.map +1 -0
- package/dist/browser-terminal/input-handler.d.ts +262 -0
- package/dist/browser-terminal/input-handler.d.ts.map +1 -0
- package/dist/browser-terminal/input-handler.js +978 -0
- package/dist/browser-terminal/input-handler.js.map +1 -0
- package/dist/browser-terminal/interfaces.d.ts +230 -0
- package/dist/browser-terminal/interfaces.d.ts.map +1 -0
- package/dist/browser-terminal/interfaces.js +5 -0
- package/dist/browser-terminal/interfaces.js.map +1 -0
- package/dist/browser-terminal/link-detector.d.ts +76 -0
- package/dist/browser-terminal/link-detector.d.ts.map +1 -0
- package/dist/browser-terminal/link-detector.js +165 -0
- package/dist/browser-terminal/link-detector.js.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts +56 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js +204 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts +68 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.js +107 -0
- package/dist/browser-terminal/providers/url-regex-provider.js.map +1 -0
- package/dist/browser-terminal/renderer-interface.d.ts +59 -0
- package/dist/browser-terminal/renderer-interface.d.ts.map +1 -0
- package/dist/browser-terminal/renderer-interface.js +2 -0
- package/dist/browser-terminal/renderer-interface.js.map +1 -0
- package/dist/browser-terminal/renderer.d.ts +254 -0
- package/dist/browser-terminal/renderer.d.ts.map +1 -0
- package/dist/browser-terminal/renderer.js +905 -0
- package/dist/browser-terminal/renderer.js.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts +45 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.js +89 -0
- package/dist/browser-terminal/scrollbar-overlay.js.map +1 -0
- package/dist/browser-terminal/selection-manager.d.ts +180 -0
- package/dist/browser-terminal/selection-manager.d.ts.map +1 -0
- package/dist/browser-terminal/selection-manager.js +913 -0
- package/dist/browser-terminal/selection-manager.js.map +1 -0
- package/dist/browser-terminal/terminal.d.ts +423 -0
- package/dist/browser-terminal/terminal.d.ts.map +1 -0
- package/dist/browser-terminal/terminal.js +1754 -0
- package/dist/browser-terminal/terminal.js.map +1 -0
- package/dist/browser-terminal/types.d.ts +595 -0
- package/dist/browser-terminal/types.d.ts.map +1 -0
- package/dist/browser-terminal/types.js +467 -0
- package/dist/browser-terminal/types.js.map +1 -0
- package/dist/browser-terminal/viewport-scroller.d.ts +94 -0
- package/dist/browser-terminal/viewport-scroller.d.ts.map +1 -0
- package/dist/browser-terminal/viewport-scroller.js +159 -0
- package/dist/browser-terminal/viewport-scroller.js.map +1 -0
- package/dist/browser-terminal/vt/effects.d.ts +114 -0
- package/dist/browser-terminal/vt/effects.d.ts.map +1 -0
- package/dist/browser-terminal/vt/effects.js +143 -0
- package/dist/browser-terminal/vt/effects.js.map +1 -0
- package/dist/browser-terminal/vt/enums.d.ts +155 -0
- package/dist/browser-terminal/vt/enums.d.ts.map +1 -0
- package/dist/browser-terminal/vt/enums.js +161 -0
- package/dist/browser-terminal/vt/enums.js.map +1 -0
- package/dist/browser-terminal/vt/mouse.d.ts +166 -0
- package/dist/browser-terminal/vt/mouse.d.ts.map +1 -0
- package/dist/browser-terminal/vt/mouse.js +259 -0
- package/dist/browser-terminal/vt/mouse.js.map +1 -0
- package/dist/browser-terminal/vt/paste.d.ts +53 -0
- package/dist/browser-terminal/vt/paste.d.ts.map +1 -0
- package/dist/browser-terminal/vt/paste.js +89 -0
- package/dist/browser-terminal/vt/paste.js.map +1 -0
- package/dist/browser-terminal/vt/render-state.d.ts +208 -0
- package/dist/browser-terminal/vt/render-state.d.ts.map +1 -0
- package/dist/browser-terminal/vt/render-state.js +776 -0
- package/dist/browser-terminal/vt/render-state.js.map +1 -0
- package/dist/browser-terminal/vt/style.d.ts +54 -0
- package/dist/browser-terminal/vt/style.d.ts.map +1 -0
- package/dist/browser-terminal/vt/style.js +92 -0
- package/dist/browser-terminal/vt/style.js.map +1 -0
- package/dist/browser-terminal/wasm-loader.d.ts +39 -0
- package/dist/browser-terminal/wasm-loader.d.ts.map +1 -0
- package/dist/browser-terminal/wasm-loader.js +73 -0
- package/dist/browser-terminal/wasm-loader.js.map +1 -0
- package/dist/browser-terminal/write-buffer.d.ts +40 -0
- package/dist/browser-terminal/write-buffer.d.ts.map +1 -0
- package/dist/browser-terminal/write-buffer.js +144 -0
- package/dist/browser-terminal/write-buffer.js.map +1 -0
- package/dist/protocol/index.d.ts +135 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +76 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/react/index.d.ts +59 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +146 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +130 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +191 -0
- package/dist/server/index.js.map +1 -0
- package/dist/terminal-screen-emulator/emulator.d.ts +66 -0
- package/dist/terminal-screen-emulator/emulator.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/emulator.js +286 -0
- package/dist/terminal-screen-emulator/emulator.js.map +1 -0
- package/dist/terminal-screen-emulator/index.d.ts +82 -0
- package/dist/terminal-screen-emulator/index.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/index.js +92 -0
- package/dist/terminal-screen-emulator/index.js.map +1 -0
- package/dist/terminal-screen-emulator/types.d.ts +382 -0
- package/dist/terminal-screen-emulator/types.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/types.js +91 -0
- package/dist/terminal-screen-emulator/types.js.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts +71 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.js +162 -0
- package/dist/terminal-screen-emulator/wasm-loader.js.map +1 -0
- package/dist/themes/index.d.ts +6 -0
- package/dist/themes/index.d.ts.map +1 -0
- package/dist/themes/index.js +51 -0
- package/dist/themes/index.js.map +1 -0
- package/dist/wasm/alloc.d.ts +170 -0
- package/dist/wasm/alloc.d.ts.map +1 -0
- package/dist/wasm/alloc.js +307 -0
- package/dist/wasm/alloc.js.map +1 -0
- package/dist/wasm/compile.d.ts +73 -0
- package/dist/wasm/compile.d.ts.map +1 -0
- package/dist/wasm/compile.js +82 -0
- package/dist/wasm/compile.js.map +1 -0
- package/dist/wasm/exports.d.ts +234 -0
- package/dist/wasm/exports.d.ts.map +1 -0
- package/dist/wasm/exports.js +15 -0
- package/dist/wasm/exports.js.map +1 -0
- package/dist/wasm/fn-table.d.ts +119 -0
- package/dist/wasm/fn-table.d.ts.map +1 -0
- package/dist/wasm/fn-table.js +168 -0
- package/dist/wasm/fn-table.js.map +1 -0
- package/dist/wasm/memory.d.ts +59 -0
- package/dist/wasm/memory.d.ts.map +1 -0
- package/dist/wasm/memory.js +89 -0
- package/dist/wasm/memory.js.map +1 -0
- package/dist/wasm/type-layouts.d.ts +61 -0
- package/dist/wasm/type-layouts.d.ts.map +1 -0
- package/dist/wasm/type-layouts.js +32 -0
- package/dist/wasm/type-layouts.js.map +1 -0
- package/package.json +94 -0
- package/wasm/ghostty-vt.wasm +0 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal session wire protocol — the contract between a browser terminal and
|
|
3
|
+
* whatever host owns its PTY.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this lives in the library (the shared session-protocol design)
|
|
6
|
+
*
|
|
7
|
+
* The geometry authority model only holds if *both* endpoints implement it the
|
|
8
|
+
* same way. When each consumer hand-rolls the frames, "who decides the size"
|
|
9
|
+
* gets answered N times with N bugs — the dropped-resize frame that started
|
|
10
|
+
* the geometry-authority design was exactly that. So the frame types and the authority rules ship
|
|
11
|
+
* from here, and consumers re-export rather than mirror them.
|
|
12
|
+
*
|
|
13
|
+
* ## The authority rules encoded below
|
|
14
|
+
*
|
|
15
|
+
* 1. **The host decides geometry; clients propose.** `attach` and `viewport`
|
|
16
|
+
* carry a *proposal* (what the client's host box can hold). The host
|
|
17
|
+
* arbitrates across attached clients and answers with `geometry`, which is
|
|
18
|
+
* the only frame a client may apply to its engine (the geometry-authority design,
|
|
19
|
+
* following tmux: clients report, server decides, server is the sole writer
|
|
20
|
+
* of the pty `winsize`).
|
|
21
|
+
* 2. **The proposal rides the attach frame.** Not a separate message sent
|
|
22
|
+
* "once connected" — that is precisely the frame that got dropped before a
|
|
23
|
+
* socket existed. One round trip, no window in which the host has to invent
|
|
24
|
+
* a default.
|
|
25
|
+
* 3. **Geometry decisions travel in-band**, in the same ordered stream as
|
|
26
|
+
* `output`. A client applies frames in arrival order, so a resize lands
|
|
27
|
+
* exactly where it belongs relative to the bytes it applies to — no
|
|
28
|
+
* separate control channel to race the data.
|
|
29
|
+
* 4. **Replay is state, not a byte fragment.** `attached.replay` is a
|
|
30
|
+
* self-contained sequence that reconstructs the screen (the replay design).
|
|
31
|
+
* A raw scrollback slice can begin mid escape sequence and carries none of
|
|
32
|
+
* the mode/SGR/cursor state its discarded prefix established.
|
|
33
|
+
*/
|
|
34
|
+
/** A cell grid. Cols/rows only — pixels never cross the wire. */
|
|
35
|
+
export interface TerminalGrid {
|
|
36
|
+
readonly cols: number;
|
|
37
|
+
readonly rows: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* How a host reconciles proposals when several clients are attached to one
|
|
41
|
+
* session. Vocabulary taken from tmux's `window-size` option.
|
|
42
|
+
*/
|
|
43
|
+
export type GeometryPolicy =
|
|
44
|
+
/** Newest proposal wins. Right when panes are effectively single-owner. */
|
|
45
|
+
'latest'
|
|
46
|
+
/**
|
|
47
|
+
* Smallest proposal across attached clients wins — the only policy under
|
|
48
|
+
* which every attached client can display the whole grid. tmux's default,
|
|
49
|
+
* and our fallback whenever more than one client is attached.
|
|
50
|
+
*/
|
|
51
|
+
| 'smallest'
|
|
52
|
+
/** Largest proposal wins; smaller clients see a cropped view. */
|
|
53
|
+
| 'largest'
|
|
54
|
+
/** Ignore proposals; geometry is set out of band. */
|
|
55
|
+
| 'manual';
|
|
56
|
+
/** Client → host. */
|
|
57
|
+
export type TerminalClientFrame =
|
|
58
|
+
/**
|
|
59
|
+
* Attach to a session, carrying the client's geometry proposal. The
|
|
60
|
+
* proposal is mandatory: a host must never have to invent a default
|
|
61
|
+
* (rule 2 above).
|
|
62
|
+
*/
|
|
63
|
+
{
|
|
64
|
+
readonly kind: 'attach';
|
|
65
|
+
readonly sessionId: string;
|
|
66
|
+
readonly viewport: TerminalGrid;
|
|
67
|
+
}
|
|
68
|
+
/** A new proposal (host box resized, font changed). Still only a proposal. */
|
|
69
|
+
| {
|
|
70
|
+
readonly kind: 'viewport';
|
|
71
|
+
readonly viewport: TerminalGrid;
|
|
72
|
+
}
|
|
73
|
+
/** Keystrokes and query responses, byte-exact (latin1 string). */
|
|
74
|
+
| {
|
|
75
|
+
readonly kind: 'input';
|
|
76
|
+
readonly data: string;
|
|
77
|
+
}
|
|
78
|
+
/** Terminate the session's process. */
|
|
79
|
+
| {
|
|
80
|
+
readonly kind: 'kill';
|
|
81
|
+
};
|
|
82
|
+
/** Host → client. */
|
|
83
|
+
export type TerminalServerFrame =
|
|
84
|
+
/**
|
|
85
|
+
* Attach accepted. `geometry` is the decided grid (already applied to the
|
|
86
|
+
* pty); `replay` reconstructs the current screen. Both are authoritative —
|
|
87
|
+
* the client adopts `geometry` even if it disagrees with its own proposal.
|
|
88
|
+
*/
|
|
89
|
+
{
|
|
90
|
+
readonly kind: 'attached';
|
|
91
|
+
readonly sessionId: string;
|
|
92
|
+
readonly geometry: TerminalGrid;
|
|
93
|
+
readonly replay: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The decided grid changed (this client's proposal was accepted, another
|
|
97
|
+
* client attached, or policy re-arbitrated). Ordered with `output`.
|
|
98
|
+
*/
|
|
99
|
+
| {
|
|
100
|
+
readonly kind: 'geometry';
|
|
101
|
+
readonly geometry: TerminalGrid;
|
|
102
|
+
}
|
|
103
|
+
/** PTY output, byte-exact (latin1 string). */
|
|
104
|
+
| {
|
|
105
|
+
readonly kind: 'output';
|
|
106
|
+
readonly data: string;
|
|
107
|
+
}
|
|
108
|
+
/** The process exited. */
|
|
109
|
+
| {
|
|
110
|
+
readonly kind: 'exit';
|
|
111
|
+
readonly code: number | null;
|
|
112
|
+
}
|
|
113
|
+
/** Protocol or session error; the client may surface it in-band. */
|
|
114
|
+
| {
|
|
115
|
+
readonly kind: 'error';
|
|
116
|
+
readonly message: string;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Arbitrate a decided grid from the currently attached clients' proposals.
|
|
120
|
+
*
|
|
121
|
+
* Shared by hosts so the policy is implemented once. Falls back to `smallest`
|
|
122
|
+
* whenever more than one client is attached under `latest`: with two viewports
|
|
123
|
+
* of different sizes there is no way to show the whole grid to both, which is
|
|
124
|
+
* the reasoning behind tmux's default.
|
|
125
|
+
*
|
|
126
|
+
* @param proposals Proposals of every attached client, in attach order (the
|
|
127
|
+
* last entry is the newest).
|
|
128
|
+
* @param policy Defaults to `latest`.
|
|
129
|
+
* @returns The decided grid, or `null` when nothing is attached (a state, not
|
|
130
|
+
* a number to invent).
|
|
131
|
+
*/
|
|
132
|
+
export declare function arbitrateGeometry(proposals: readonly TerminalGrid[], policy?: GeometryPolicy): TerminalGrid | null;
|
|
133
|
+
/** True when two grids are the same size. */
|
|
134
|
+
export declare function gridEquals(a: TerminalGrid | null, b: TerminalGrid | null): boolean;
|
|
135
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,iEAAiE;AACjE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAA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MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE;AACzD,oEAAoE;GAClE;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,SAAS,YAAY,EAAE,EAClC,MAAM,GAAE,cAAyB,GAChC,YAAY,GAAG,IAAI,CAoBrB;AAED,6CAA6C;AAC7C,wBAAgB,UAAU,CAAC,CAAC,EAAE,YAAY,GAAG,IAAI,EAAE,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAGlF"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal session wire protocol — the contract between a browser terminal and
|
|
3
|
+
* whatever host owns its PTY.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this lives in the library (the shared session-protocol design)
|
|
6
|
+
*
|
|
7
|
+
* The geometry authority model only holds if *both* endpoints implement it the
|
|
8
|
+
* same way. When each consumer hand-rolls the frames, "who decides the size"
|
|
9
|
+
* gets answered N times with N bugs — the dropped-resize frame that started
|
|
10
|
+
* the geometry-authority design was exactly that. So the frame types and the authority rules ship
|
|
11
|
+
* from here, and consumers re-export rather than mirror them.
|
|
12
|
+
*
|
|
13
|
+
* ## The authority rules encoded below
|
|
14
|
+
*
|
|
15
|
+
* 1. **The host decides geometry; clients propose.** `attach` and `viewport`
|
|
16
|
+
* carry a *proposal* (what the client's host box can hold). The host
|
|
17
|
+
* arbitrates across attached clients and answers with `geometry`, which is
|
|
18
|
+
* the only frame a client may apply to its engine (the geometry-authority design,
|
|
19
|
+
* following tmux: clients report, server decides, server is the sole writer
|
|
20
|
+
* of the pty `winsize`).
|
|
21
|
+
* 2. **The proposal rides the attach frame.** Not a separate message sent
|
|
22
|
+
* "once connected" — that is precisely the frame that got dropped before a
|
|
23
|
+
* socket existed. One round trip, no window in which the host has to invent
|
|
24
|
+
* a default.
|
|
25
|
+
* 3. **Geometry decisions travel in-band**, in the same ordered stream as
|
|
26
|
+
* `output`. A client applies frames in arrival order, so a resize lands
|
|
27
|
+
* exactly where it belongs relative to the bytes it applies to — no
|
|
28
|
+
* separate control channel to race the data.
|
|
29
|
+
* 4. **Replay is state, not a byte fragment.** `attached.replay` is a
|
|
30
|
+
* self-contained sequence that reconstructs the screen (the replay design).
|
|
31
|
+
* A raw scrollback slice can begin mid escape sequence and carries none of
|
|
32
|
+
* the mode/SGR/cursor state its discarded prefix established.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* Arbitrate a decided grid from the currently attached clients' proposals.
|
|
36
|
+
*
|
|
37
|
+
* Shared by hosts so the policy is implemented once. Falls back to `smallest`
|
|
38
|
+
* whenever more than one client is attached under `latest`: with two viewports
|
|
39
|
+
* of different sizes there is no way to show the whole grid to both, which is
|
|
40
|
+
* the reasoning behind tmux's default.
|
|
41
|
+
*
|
|
42
|
+
* @param proposals Proposals of every attached client, in attach order (the
|
|
43
|
+
* last entry is the newest).
|
|
44
|
+
* @param policy Defaults to `latest`.
|
|
45
|
+
* @returns The decided grid, or `null` when nothing is attached (a state, not
|
|
46
|
+
* a number to invent).
|
|
47
|
+
*/
|
|
48
|
+
export function arbitrateGeometry(proposals, policy = 'latest') {
|
|
49
|
+
if (proposals.length === 0)
|
|
50
|
+
return null;
|
|
51
|
+
if (proposals.length === 1)
|
|
52
|
+
return { ...proposals[0] };
|
|
53
|
+
switch (policy) {
|
|
54
|
+
case 'manual':
|
|
55
|
+
return null;
|
|
56
|
+
case 'largest':
|
|
57
|
+
return {
|
|
58
|
+
cols: Math.max(...proposals.map((p) => p.cols)),
|
|
59
|
+
rows: Math.max(...proposals.map((p) => p.rows)),
|
|
60
|
+
};
|
|
61
|
+
case 'latest':
|
|
62
|
+
// Multiple attachers under `latest` degrade to `smallest` — see above.
|
|
63
|
+
case 'smallest':
|
|
64
|
+
return {
|
|
65
|
+
cols: Math.min(...proposals.map((p) => p.cols)),
|
|
66
|
+
rows: Math.min(...proposals.map((p) => p.rows)),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** True when two grids are the same size. */
|
|
71
|
+
export function gridEquals(a, b) {
|
|
72
|
+
if (!a || !b)
|
|
73
|
+
return a === b;
|
|
74
|
+
return a.cols === b.cols && a.rows === b.rows;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAkEH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAC/B,SAAkC,EAClC,SAAyB,QAAQ;IAEjC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACxC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAEvD,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,QAAQ;YACX,OAAO,IAAI,CAAC;QACd,KAAK,SAAS;YACZ,OAAO;gBACL,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBAC/C,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;aAChD,CAAC;QACJ,KAAK,QAAQ,CAAC;QACd,uEAAuE;QACvE,KAAK,UAAU;YACb,OAAO;gBACL,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBAC/C,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;aAChD,CAAC;IACN,CAAC;AACH,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,UAAU,CAAC,CAAsB,EAAE,CAAsB;IACvE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<GhosttyTerminal />` — the batteries-included browser endpoint.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this ships here (the shared session-protocol design)
|
|
5
|
+
*
|
|
6
|
+
* The geometry protocol only holds if both endpoints implement it correctly.
|
|
7
|
+
* When every consumer hand-rolls the socket, the fit loop, reconnect, and the
|
|
8
|
+
* resize mirroring, "who decides the size" gets answered once per consumer —
|
|
9
|
+
* and the bug that started the geometry-authority design was exactly that: a resize frame sent before
|
|
10
|
+
* the socket existed, dropped by a `readyState` guard, leaving the PTY at its
|
|
11
|
+
* spawn size forever. Consumers should not be able to write that code.
|
|
12
|
+
*
|
|
13
|
+
* This component owns: engine init, socket lifecycle with reconnect, the
|
|
14
|
+
* geometry proposal/decision loop, and teardown. It owns **no product
|
|
15
|
+
* decisions** — no chrome, no status UI, no styling beyond filling its host.
|
|
16
|
+
*
|
|
17
|
+
* ## The protocol rules it enforces
|
|
18
|
+
*
|
|
19
|
+
* - The measured viewport rides the **attach** frame, so the host never has to
|
|
20
|
+
* invent a default and there is no window in which a proposal can be dropped.
|
|
21
|
+
* - Local measurements are only ever **proposals** (`onProposal` → `viewport`
|
|
22
|
+
* frame). The engine resizes exclusively on the host's `geometry` decision
|
|
23
|
+
* (or the `attached` frame's geometry), applied via `applyGeometry`.
|
|
24
|
+
* - Frames are applied in arrival order, which is what keeps a resize
|
|
25
|
+
* positioned correctly relative to the output bytes around it.
|
|
26
|
+
*
|
|
27
|
+
* React is a `peerDependency` and only this entry point needs it; the core
|
|
28
|
+
* (`./browser-terminal`, `./geometry`, `./protocol`) stays framework-free.
|
|
29
|
+
*/
|
|
30
|
+
import { type ITerminalOptions } from '../browser-terminal/index.js';
|
|
31
|
+
import type { RendererFailure } from '../browser-terminal/interfaces.js';
|
|
32
|
+
export interface GhosttyTerminalProps {
|
|
33
|
+
/** Session to attach to. Changing it tears down and re-attaches. */
|
|
34
|
+
sessionId: string;
|
|
35
|
+
/** WebSocket URL, or a path resolved against the current origin. */
|
|
36
|
+
url: string;
|
|
37
|
+
/** Passed through to the host element. */
|
|
38
|
+
className?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Merged into the host element's style, after the fill defaults. Only needed
|
|
41
|
+
* if you must override how the host sizes itself — see the note on
|
|
42
|
+
* {@link GhosttyTerminal} about why the host must not be content-sized.
|
|
43
|
+
*/
|
|
44
|
+
style?: React.CSSProperties;
|
|
45
|
+
/** Terminal appearance/behavior. Geometry is NOT configurable here — the host decides it. */
|
|
46
|
+
options?: Omit<ITerminalOptions, 'cols' | 'rows'>;
|
|
47
|
+
/** Reconnect backoff. Default 1500ms. Set 0 to disable reconnect. */
|
|
48
|
+
reconnectDelayMs?: number;
|
|
49
|
+
/** The session's process exited. */
|
|
50
|
+
onExit?: (code: number | null) => void;
|
|
51
|
+
/** OSC 0/2 title changes. */
|
|
52
|
+
onTitleChange?: (title: string) => void;
|
|
53
|
+
/** Protocol/session errors, already rendered into the terminal as well. */
|
|
54
|
+
onError?: (message: string) => void;
|
|
55
|
+
/** Active renderer failed to recover; remount the component to fall back. */
|
|
56
|
+
onRendererFailure?: (failure: RendererFailure) => void;
|
|
57
|
+
}
|
|
58
|
+
export declare function GhosttyTerminal({ sessionId, url, className, options, reconnectDelayMs, style, onExit, onTitleChange, onError, onRendererFailure, }: GhosttyTerminalProps): React.ReactElement;
|
|
59
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAIH,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AASzE,MAAM,WAAW,oBAAoB;IACnC,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,GAAG,EAAE,MAAM,CAAC;IACZ,0CAA0C;IAC1C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,6FAA6F;IAC7F,OAAO,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IAClD,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,oCAAoC;IACpC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC;IACvC,6BAA6B;IAC7B,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,6EAA6E;IAC7E,iBAAiB,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;CACxD;AAED,wBAAgB,eAAe,CAAC,EAC9B,SAAS,EACT,GAAG,EACH,SAAS,EACT,OAAO,EACP,gBAA6C,EAC7C,KAAK,EACL,MAAM,EACN,aAAa,EACb,OAAO,EACP,iBAAiB,GAClB,EAAE,oBAAoB,GAAG,KAAK,CAAC,YAAY,CAwH3C"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* `<GhosttyTerminal />` — the batteries-included browser endpoint.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this ships here (the shared session-protocol design)
|
|
6
|
+
*
|
|
7
|
+
* The geometry protocol only holds if both endpoints implement it correctly.
|
|
8
|
+
* When every consumer hand-rolls the socket, the fit loop, reconnect, and the
|
|
9
|
+
* resize mirroring, "who decides the size" gets answered once per consumer —
|
|
10
|
+
* and the bug that started the geometry-authority design was exactly that: a resize frame sent before
|
|
11
|
+
* the socket existed, dropped by a `readyState` guard, leaving the PTY at its
|
|
12
|
+
* spawn size forever. Consumers should not be able to write that code.
|
|
13
|
+
*
|
|
14
|
+
* This component owns: engine init, socket lifecycle with reconnect, the
|
|
15
|
+
* geometry proposal/decision loop, and teardown. It owns **no product
|
|
16
|
+
* decisions** — no chrome, no status UI, no styling beyond filling its host.
|
|
17
|
+
*
|
|
18
|
+
* ## The protocol rules it enforces
|
|
19
|
+
*
|
|
20
|
+
* - The measured viewport rides the **attach** frame, so the host never has to
|
|
21
|
+
* invent a default and there is no window in which a proposal can be dropped.
|
|
22
|
+
* - Local measurements are only ever **proposals** (`onProposal` → `viewport`
|
|
23
|
+
* frame). The engine resizes exclusively on the host's `geometry` decision
|
|
24
|
+
* (or the `attached` frame's geometry), applied via `applyGeometry`.
|
|
25
|
+
* - Frames are applied in arrival order, which is what keeps a resize
|
|
26
|
+
* positioned correctly relative to the output bytes around it.
|
|
27
|
+
*
|
|
28
|
+
* React is a `peerDependency` and only this entry point needs it; the core
|
|
29
|
+
* (`./browser-terminal`, `./geometry`, `./protocol`) stays framework-free.
|
|
30
|
+
*/
|
|
31
|
+
import { useEffect, useRef } from 'react';
|
|
32
|
+
import { init, Terminal } from '../browser-terminal/index.js';
|
|
33
|
+
const DEFAULT_RECONNECT_DELAY_MS = 1_500;
|
|
34
|
+
export function GhosttyTerminal({ sessionId, url, className, options, reconnectDelayMs = DEFAULT_RECONNECT_DELAY_MS, style, onExit, onTitleChange, onError, onRendererFailure, }) {
|
|
35
|
+
const hostRef = useRef(null);
|
|
36
|
+
// Callbacks live in refs so re-renders never tear down the session.
|
|
37
|
+
const callbacks = useRef({ onExit, onTitleChange, onError, onRendererFailure });
|
|
38
|
+
callbacks.current = { onExit, onTitleChange, onError, onRendererFailure };
|
|
39
|
+
const optionsRef = useRef(options);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const host = hostRef.current;
|
|
42
|
+
if (!host)
|
|
43
|
+
return;
|
|
44
|
+
let disposed = false;
|
|
45
|
+
let terminal = null;
|
|
46
|
+
let socket = null;
|
|
47
|
+
let retryTimer = null;
|
|
48
|
+
const send = (frame) => {
|
|
49
|
+
if (socket?.readyState === WebSocket.OPEN)
|
|
50
|
+
socket.send(JSON.stringify(frame));
|
|
51
|
+
};
|
|
52
|
+
const apply = (frame) => {
|
|
53
|
+
if (!terminal || disposed)
|
|
54
|
+
return;
|
|
55
|
+
switch (frame.kind) {
|
|
56
|
+
case 'attached':
|
|
57
|
+
// The host's decision wins over our proposal, always.
|
|
58
|
+
terminal.applyGeometry(frame.geometry);
|
|
59
|
+
// Self-contained screen state (RIS-prefixed), not a byte fragment.
|
|
60
|
+
terminal.write(frame.replay);
|
|
61
|
+
return;
|
|
62
|
+
case 'geometry':
|
|
63
|
+
terminal.applyGeometry(frame.geometry);
|
|
64
|
+
return;
|
|
65
|
+
case 'output':
|
|
66
|
+
terminal.write(frame.data);
|
|
67
|
+
return;
|
|
68
|
+
case 'exit':
|
|
69
|
+
callbacks.current.onExit?.(frame.code);
|
|
70
|
+
return;
|
|
71
|
+
case 'error':
|
|
72
|
+
terminal.write(`\r\n\x1b[31m${frame.message}\x1b[0m\r\n`);
|
|
73
|
+
callbacks.current.onError?.(frame.message);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const connect = () => {
|
|
78
|
+
if (disposed || !terminal)
|
|
79
|
+
return;
|
|
80
|
+
const absolute = /^wss?:/.test(url)
|
|
81
|
+
? url
|
|
82
|
+
: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}${url}`;
|
|
83
|
+
socket = new WebSocket(absolute);
|
|
84
|
+
socket.onopen = () => {
|
|
85
|
+
// The proposal rides the attach frame — the whole point. There is no
|
|
86
|
+
// "send dimensions once connected" step left to drop.
|
|
87
|
+
send({ kind: 'attach', sessionId, viewport: terminal.proposal });
|
|
88
|
+
};
|
|
89
|
+
socket.onmessage = (event) => {
|
|
90
|
+
let frame;
|
|
91
|
+
try {
|
|
92
|
+
frame = JSON.parse(String(event.data));
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
apply(frame);
|
|
98
|
+
};
|
|
99
|
+
socket.onclose = () => {
|
|
100
|
+
if (disposed || reconnectDelayMs <= 0)
|
|
101
|
+
return;
|
|
102
|
+
retryTimer = setTimeout(connect, reconnectDelayMs);
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
void (async () => {
|
|
106
|
+
await init();
|
|
107
|
+
if (disposed)
|
|
108
|
+
return;
|
|
109
|
+
terminal = new Terminal({ ...optionsRef.current });
|
|
110
|
+
terminal.open(host);
|
|
111
|
+
// Keystrokes and engine-generated query responses (DSR/DA) go to the pty.
|
|
112
|
+
terminal.onData((data) => send({ kind: 'input', data }));
|
|
113
|
+
// Measurements are proposals, never applied locally.
|
|
114
|
+
terminal.onProposal((viewport) => send({ kind: 'viewport', viewport }));
|
|
115
|
+
terminal.onTitleChange((title) => callbacks.current.onTitleChange?.(title));
|
|
116
|
+
terminal.onRendererFailure((failure) => callbacks.current.onRendererFailure?.(failure));
|
|
117
|
+
connect();
|
|
118
|
+
})();
|
|
119
|
+
return () => {
|
|
120
|
+
disposed = true;
|
|
121
|
+
if (retryTimer)
|
|
122
|
+
clearTimeout(retryTimer);
|
|
123
|
+
// Detach, never kill: the session outlives this component.
|
|
124
|
+
socket?.close();
|
|
125
|
+
terminal?.dispose();
|
|
126
|
+
};
|
|
127
|
+
}, [sessionId, url, reconnectDelayMs]);
|
|
128
|
+
return (_jsx("div", { ref: hostRef, className: className,
|
|
129
|
+
// The host MUST be given a size by the consumer (className or style):
|
|
130
|
+
// the terminal fills whatever box this element has, and an unsized host
|
|
131
|
+
// is 0×0 — the library warns loudly in that case rather than guessing.
|
|
132
|
+
//
|
|
133
|
+
// Deliberately NO inline width/height here: inline styles beat classes,
|
|
134
|
+
// so a default `height:'100%'` would silently override the consumer's
|
|
135
|
+
// own sizing (e.g. Tailwind `h-64`) — exactly the sort of cross-boundary
|
|
136
|
+
// style mutation the library-owned-subtree design removes. Before the
|
|
137
|
+
// canvas moved out of the layout flow this collision was masked by the
|
|
138
|
+
// canvas giving the host content height; now it would surface as a
|
|
139
|
+
// 0-height host.
|
|
140
|
+
//
|
|
141
|
+
// `position: relative` because the terminal mounts absolutely-positioned
|
|
142
|
+
// children into this element. Without a positioned ancestor those anchor
|
|
143
|
+
// to the page instead.
|
|
144
|
+
style: { display: 'block', position: 'relative', ...style } }));
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAyB,MAAM,8BAA8B,CAAC;AAQrF,MAAM,0BAA0B,GAAG,KAAK,CAAC;AA6BzC,MAAM,U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,CAAC,CAAC;YAExF,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,EAAE,CAAC;QAEL,OAAO,GAAG,EAAE;YACV,QAAQ,GAAG,IAAI,CAAC;YAChB,IAAI,UAAU;gBAAE,YAAY,CAAC,UAAU,CAAC,CAAC;YACzC,2DAA2D;YAC3D,MAAM,EAAE,KAAK,EAAE,CAAC;YAChB,QAAQ,EAAE,OAAO,EAAE,CAAC;QACtB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAEvC,OAAO,CACL,cACE,GAAG,EAAE,OAAO,EACZ,SAAS,EAAE,SAAS;QACpB,sEAAsE;QACtE,wEAAwE;QACxE,uEAAuE;QACvE,EAAE;QACF,wEAAwE;QACxE,sEAAsE;QACtE,yEAAyE;QACzE,sEAAsE;QACtE,uEAAuE;QACvE,mEAAmE;QACnE,iBAAiB;QACjB,EAAE;QACF,yEAAyE;QACzE,yEAAyE;QACzE,uBAAuB;QACvB,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,KAAK,EAAE,GAC3D,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal session host — the PTY-side endpoint of the session protocol.
|
|
3
|
+
*
|
|
4
|
+
* ## What this owns (the geometry-authority design)
|
|
5
|
+
*
|
|
6
|
+
* - **Geometry authority.** It is the *only* writer of the pty's size. Clients
|
|
7
|
+
* send proposals; this class arbitrates them under a {@link GeometryPolicy}
|
|
8
|
+
* and pushes the decision to the pty and to every attached client
|
|
9
|
+
* (Decision 1, tmux model).
|
|
10
|
+
* - **A mirror engine.** Every byte the pty emits is also fed to a headless VT
|
|
11
|
+
* engine, so the host knows the *current screen*, not just a byte log.
|
|
12
|
+
* - **Serialized replay.** On attach, the screen is serialized to a
|
|
13
|
+
* self-contained escape sequence (Decision 4) instead of replaying a raw
|
|
14
|
+
* scrollback slice, which can start mid-sequence and carries none of the
|
|
15
|
+
* state its discarded prefix established.
|
|
16
|
+
* - **Send ordering.** The host performs the sends, so the `attached` frame
|
|
17
|
+
* cannot be overtaken by live output — the interleaving window is closed by
|
|
18
|
+
* construction rather than by a lock.
|
|
19
|
+
*
|
|
20
|
+
* ## What this deliberately does NOT own
|
|
21
|
+
*
|
|
22
|
+
* Session *policy* stays with the application: the session registry, spawn
|
|
23
|
+
* command/cwd resolution, scope keys, retention, capacity limits, auth. This
|
|
24
|
+
* class takes an already-spawned {@link PtyHandle} so it stays runtime-agnostic
|
|
25
|
+
* (Bun, node-pty, ssh, a test double) and product-agnostic.
|
|
26
|
+
*/
|
|
27
|
+
import { type GeometryPolicy, type TerminalClientFrame, type TerminalGrid, type TerminalServerFrame } from "../protocol/index.js";
|
|
28
|
+
/** The pty this host drives. Implemented by the app (Bun, node-pty, ssh, …). */
|
|
29
|
+
export interface PtyHandle {
|
|
30
|
+
/** Write client input to the pty. */
|
|
31
|
+
write(data: string): void;
|
|
32
|
+
/** Apply a new `winsize`. Called ONLY by this host (single writer). */
|
|
33
|
+
resize(size: TerminalGrid): void;
|
|
34
|
+
/** Terminate the process. */
|
|
35
|
+
kill(): void;
|
|
36
|
+
}
|
|
37
|
+
/** An attached client. `send` must deliver frames in call order. */
|
|
38
|
+
export interface TerminalClient {
|
|
39
|
+
send(frame: TerminalServerFrame): void;
|
|
40
|
+
}
|
|
41
|
+
export interface TerminalSessionHostOptions {
|
|
42
|
+
sessionId: string;
|
|
43
|
+
pty: PtyHandle;
|
|
44
|
+
/**
|
|
45
|
+
* The pty's size at spawn. The host treats this as provisional: the first
|
|
46
|
+
* attach proposal replaces it. Pass the same value used for the spawn so the
|
|
47
|
+
* mirror engine starts consistent with the pty.
|
|
48
|
+
*/
|
|
49
|
+
initialGeometry: TerminalGrid;
|
|
50
|
+
/** Arbitration policy across attached clients. Default `latest`. */
|
|
51
|
+
policy?: GeometryPolicy;
|
|
52
|
+
/** Mirror-engine scrollback (lines). Default 0 — replay restores the screen. */
|
|
53
|
+
mirrorScrollback?: number;
|
|
54
|
+
}
|
|
55
|
+
export declare class TerminalSessionHost {
|
|
56
|
+
readonly sessionId: string;
|
|
57
|
+
private readonly pty;
|
|
58
|
+
private readonly policy;
|
|
59
|
+
private readonly screen;
|
|
60
|
+
/** Attach order matters for `latest`; Map preserves insertion order. */
|
|
61
|
+
private readonly clients;
|
|
62
|
+
private _geometry;
|
|
63
|
+
private disposed;
|
|
64
|
+
private constructor();
|
|
65
|
+
/** Create a host and its mirror engine. */
|
|
66
|
+
static create(opts: TerminalSessionHostOptions): Promise<TerminalSessionHost>;
|
|
67
|
+
/** The decided grid — authoritative for the pty, the mirror, and all clients. */
|
|
68
|
+
get geometry(): TerminalGrid;
|
|
69
|
+
/** Number of attached clients (arbitration degrades to `smallest` above 1). */
|
|
70
|
+
get clientCount(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Feed pty output through the mirror engine and fan it out to clients.
|
|
73
|
+
*
|
|
74
|
+
* Ordering: the mirror is updated *before* the broadcast, so a client that
|
|
75
|
+
* attaches during this call (impossible today — single-threaded — but true of
|
|
76
|
+
* any future async transport) can never receive a replay that lags the bytes
|
|
77
|
+
* its peers already have.
|
|
78
|
+
*/
|
|
79
|
+
ingest(data: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Attach a client with its geometry proposal, and send it the `attached`
|
|
82
|
+
* frame containing the decided geometry and a serialized screen.
|
|
83
|
+
*
|
|
84
|
+
* The host sends the frame itself, which is what closes the
|
|
85
|
+
* replay-vs-live-output interleaving window: registration and the snapshot
|
|
86
|
+
* send happen in one uninterrupted step, so no `output` frame can land
|
|
87
|
+
* between them.
|
|
88
|
+
*/
|
|
89
|
+
attach(client: TerminalClient, viewport: TerminalGrid): void;
|
|
90
|
+
/** Detach a client and re-arbitrate (its proposal no longer constrains). */
|
|
91
|
+
detach(client: TerminalClient): void;
|
|
92
|
+
/**
|
|
93
|
+
* Handle a client frame. Input is forwarded byte-exact; `viewport` is a
|
|
94
|
+
* proposal that may or may not become the decision.
|
|
95
|
+
*/
|
|
96
|
+
handle(client: TerminalClient, frame: TerminalClientFrame): void;
|
|
97
|
+
/**
|
|
98
|
+
* Serialize the current screen as a self-contained sequence.
|
|
99
|
+
*
|
|
100
|
+
* `RIS` (`ESC c`) first so the client's engine starts from a known state
|
|
101
|
+
* regardless of what it had before — the property a raw scrollback slice can
|
|
102
|
+
* never offer.
|
|
103
|
+
*
|
|
104
|
+
* Known limitation: this restores screen content and styling, not cursor
|
|
105
|
+
* position or application modes (the emulator renders cells, and
|
|
106
|
+
* `libghostty-vt` has no state-dump API yet). After a replay the cursor sits
|
|
107
|
+
* after the restored content. For a shell session the following resize nudge
|
|
108
|
+
* (or any keystroke) makes the app redraw and the cursor snaps back to truth.
|
|
109
|
+
* Cursor-exact replay needs an upstream state-dump — tracked in NEXT.md.
|
|
110
|
+
*/
|
|
111
|
+
serialize(): string;
|
|
112
|
+
/** Notify clients that the process exited. */
|
|
113
|
+
notifyExit(code: number | null): void;
|
|
114
|
+
/** Release the mirror engine. Does not kill the pty (the app owns lifetime). */
|
|
115
|
+
dispose(): void;
|
|
116
|
+
/**
|
|
117
|
+
* Recompute the decision from all attached proposals and, if it changed,
|
|
118
|
+
* write it to the pty (sole writer), the mirror engine, and every client.
|
|
119
|
+
*
|
|
120
|
+
* The client broadcast goes through the same ordered channel as `output`, so
|
|
121
|
+
* each client applies the resize in the right position relative to the bytes
|
|
122
|
+
* around it.
|
|
123
|
+
*
|
|
124
|
+
* @param exclude A client that must not receive the broadcast because it is
|
|
125
|
+
* mid-attach and will learn the decision from its `attached` frame.
|
|
126
|
+
*/
|
|
127
|
+
private rearbitrate;
|
|
128
|
+
private broadcast;
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.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