dsh-bash-terminal-ts 0.7.2-compat.2 → 0.8.1

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 (86) hide show
  1. package/README.de.md +3 -2
  2. package/README.en.md +102 -3
  3. package/README.es.md +3 -2
  4. package/README.fr.md +3 -2
  5. package/README.it.md +3 -2
  6. package/README.md +198 -4
  7. package/README.ru.md +3 -2
  8. package/cordis.patch.yml +66 -0
  9. package/dist/client.core.js +7888 -79
  10. package/dist/client.js +7888 -79
  11. package/dsh.plugin.json +22 -22
  12. package/lib/browser-i18n.d.ts +4 -0
  13. package/lib/browser-i18n.js +23 -0
  14. package/lib/client.js +7888 -79
  15. package/lib/dsh-types.d.ts +10 -0
  16. package/lib/dsh.d.ts +2 -0
  17. package/lib/index.d.ts +43 -0
  18. package/lib/index.js +176 -91
  19. package/lib/locales.d.ts +4 -0
  20. package/lib/locales.js +422 -0
  21. package/lib/settings-surface.d.ts +20 -0
  22. package/lib/settings-surface.js +49 -0
  23. package/lib/shell-id.d.ts +2 -0
  24. package/lib/shell-id.js +9 -0
  25. package/lib/shells.d.ts +114 -0
  26. package/lib/shells.js +187 -0
  27. package/lib/terminal.d.ts +30 -1
  28. package/lib/terminal.js +18 -5
  29. package/lib/webterminals/catalog.d.ts +36 -0
  30. package/lib/webterminals/catalog.js +93 -0
  31. package/lib/webterminals/host.d.ts +67 -0
  32. package/lib/webterminals/host.js +278 -0
  33. package/lib/webterminals/mount.d.ts +46 -0
  34. package/lib/webterminals/mount.js +171 -0
  35. package/lib/webterminals/protocol.d.ts +94 -0
  36. package/lib/webterminals/protocol.js +9 -0
  37. package/lib/webterminals/pty.d.ts +42 -0
  38. package/lib/webterminals/pty.js +64 -0
  39. package/lib/webterminals/registry.d.ts +139 -0
  40. package/lib/webterminals/registry.js +353 -0
  41. package/lib/webterminals/ws.d.ts +54 -0
  42. package/lib/webterminals/ws.js +140 -0
  43. package/locale/de.json +4 -0
  44. package/locale/en.json +4 -0
  45. package/locale/es.json +4 -0
  46. package/locale/fr.json +4 -0
  47. package/locale/it.json +4 -0
  48. package/locale/ja.json +4 -0
  49. package/locale/ko.json +4 -0
  50. package/locale/ru.json +4 -0
  51. package/locale/zh.json +4 -0
  52. package/package.json +131 -121
  53. package/scripts/build-browser-tests.mjs +36 -0
  54. package/scripts/build-client.mjs +10 -0
  55. package/scripts/check-artifacts.mjs +93 -0
  56. package/scripts/link-shells.mjs +75 -0
  57. package/src/browser-i18n.ts +27 -0
  58. package/src/client.tsx +539 -134
  59. package/src/dsh-types.ts +6 -0
  60. package/src/dsh.ts +2 -0
  61. package/src/index.ts +146 -35
  62. package/src/locales.ts +434 -0
  63. package/src/settings-surface.ts +59 -0
  64. package/src/shell-id.ts +11 -0
  65. package/src/shells.ts +250 -0
  66. package/src/terminal.ts +34 -5
  67. package/src/webterminals/browser/api.ts +212 -0
  68. package/src/webterminals/browser/bridge.ts +188 -0
  69. package/src/webterminals/browser/css.d.ts +16 -0
  70. package/src/webterminals/browser/face.ts +151 -0
  71. package/src/webterminals/browser/shell-picker.tsx +98 -0
  72. package/src/webterminals/browser/store.ts +40 -0
  73. package/src/webterminals/browser/tab.tsx +136 -0
  74. package/src/webterminals/browser/testkit.ts +21 -0
  75. package/src/webterminals/browser/url.ts +70 -0
  76. package/src/webterminals/browser/view.ts +381 -0
  77. package/src/webterminals/browser/xterm.ts +269 -0
  78. package/src/webterminals/catalog.ts +114 -0
  79. package/src/webterminals/host.ts +361 -0
  80. package/src/webterminals/mount.ts +227 -0
  81. package/src/webterminals/protocol.ts +78 -0
  82. package/src/webterminals/pty.ts +90 -0
  83. package/src/webterminals/registry.ts +447 -0
  84. package/src/webterminals/ws.ts +166 -0
  85. package/tsconfig.client.json +9 -1
  86. package/tsconfig.json +1 -1
@@ -0,0 +1,166 @@
1
+ // The browser terminal's screen socket: one WebSocket per attached terminal.
2
+ //
3
+ // Why a socket at all when the config channel is plain HTTP: keystrokes and PTY
4
+ // output are a bidirectional byte stream with backpressure, and polling it would
5
+ // either lag the typing or hammer the host. Everything that is *not* a stream
6
+ // (the catalog, the switches, allocation, close) stays on the existing
7
+ // `/api/bash-terminal/config` bridge, which every supported host line already
8
+ // serves - so this file adds exactly one route.
9
+ //
10
+ // Auth is the same rule the config bridge uses: the peer must be a loopback
11
+ // address. That is a DNS-rebinding / LAN defence, not authentication - anything
12
+ // that can already reach this host's loopback port can open a shell here, exactly
13
+ // as the built-in terminal let it. Which is also why there is no model-facing
14
+ // surface: no tool, no skill, no prompt injection path.
15
+
16
+ import { WebSocket, WebSocketServer } from "ws";
17
+ import type { Duplex } from "node:stream";
18
+ import type { UiClientFrame, UiEnvironment, UiServerFrame } from "./protocol.js";
19
+ import { UiTerminalError } from "./registry.js";
20
+ import type { UiTerminalRegistry } from "./registry.js";
21
+
22
+ /** The socket path, sibling to the config bridge. */
23
+ export const WS_PATH = "/bash-terminal/ws";
24
+
25
+ /** The host webserver surface this module needs. */
26
+ export interface UpgradeCapableServer {
27
+ /**
28
+ * The JSON route face (the same one the config bridge uses). Typed loosely on
29
+ * purpose: the handler's request/response shapes are the Node http pair, and
30
+ * naming them here would drag `@types/node` into every caller's client graph.
31
+ */
32
+ register?(route: { kind: string; path: string; handler: unknown }): (() => void) | void;
33
+ registerUpgrade?(route: {
34
+ path: string;
35
+ handler: (request: { url?: string; socket?: { remoteAddress?: string } }, socket: Duplex, head: Buffer) => void;
36
+ }): (() => void) | void;
37
+ }
38
+
39
+ /** What the caller supplies so this module stays testable. */
40
+ export interface SocketDeps {
41
+ registry: UiTerminalRegistry;
42
+ /** The limits to advertise for one terminal's cwd. */
43
+ environmentFor(cwd: string): UiEnvironment;
44
+ /** Same loopback rule as the config bridge. */
45
+ isLoopback(request: { socket?: { remoteAddress?: string } }): boolean;
46
+ }
47
+
48
+ /** The mount outcome, so the config bridge can report a reason instead of silence. */
49
+ export type SocketMount = { ok: true; dispose(): void } | { ok: false; reason: "no-websocket-route" };
50
+
51
+ /**
52
+ * Register the socket route.
53
+ * @returns a disposer, or the reason the host cannot carry an upgrade (older
54
+ * builds expose `register` only) - the caller then reports `no-websocket-route`
55
+ * rather than mounting a terminal that can never receive output.
56
+ */
57
+ export function mountTerminalSocket(deps: SocketDeps, webServer: UpgradeCapableServer | undefined): SocketMount {
58
+ if (webServer?.registerUpgrade === undefined) return { ok: false, reason: "no-websocket-route" };
59
+ const wss = new WebSocketServer({ noServer: true });
60
+ const disposeRoute = webServer.registerUpgrade({
61
+ path: WS_PATH,
62
+ handler: (request, socket, head) => {
63
+ if (!deps.isLoopback(request)) {
64
+ socket.destroy();
65
+ return;
66
+ }
67
+ wss.handleUpgrade(request as never, socket as never, head, (ws) => {
68
+ // The identity comes from the HANDSHAKE url: on a server-side socket
69
+ // `ws.url` is not part of the documented shape, and reading it there
70
+ // silently degrades every attach into "no such terminal".
71
+ attach(deps, ws as WebSocket & { url?: string }, (request.url ?? WS_PATH));
72
+ });
73
+ },
74
+ });
75
+ return {
76
+ ok: true,
77
+ dispose: () => {
78
+ try {
79
+ if (typeof disposeRoute === "function") disposeRoute();
80
+ } catch { /* the route is already gone */ }
81
+ for (const client of Array.from(wss.clients)) {
82
+ try {
83
+ client.close(1001, "plugin unloaded");
84
+ } catch { /* already closed */ }
85
+ }
86
+ wss.close();
87
+ },
88
+ };
89
+ }
90
+
91
+ /** Bind one connected socket to its terminal, both directions. */
92
+ function attach(deps: SocketDeps, ws: WebSocket, requestUrl: string): void {
93
+ const send = (frame: UiServerFrame): void => {
94
+ if (ws.readyState !== WebSocket.OPEN) return;
95
+ try {
96
+ ws.send(JSON.stringify(frame));
97
+ } catch {
98
+ // A socket that cannot take a frame is already closing; the detach below
99
+ // runs on the close event and the registry's reaper handles the process.
100
+ }
101
+ };
102
+ const id = new URL(requestUrl, "http://dsh.internal").searchParams.get("id") ?? "";
103
+ const info = id.length === 0 ? undefined : deps.registry.get(id);
104
+ if (info === undefined) {
105
+ send({ t: "error", code: "missingTerminal", message: "no such terminal (it was closed, or the host restarted)" });
106
+ ws.close(4404, "missing terminal");
107
+ return;
108
+ }
109
+ const terminalId = info.id;
110
+ const detach = deps.registry.attach(terminalId, {
111
+ onData: (data) => { send({ t: "data", data }); },
112
+ onExit: (finalInfo) => { send({ t: "exit", info: finalInfo }); },
113
+ });
114
+ // The snapshot is taken AFTER the attach, and the event loop cannot interleave a
115
+ // chunk between the two, so no byte is both replayed and streamed live.
116
+ send({
117
+ t: "snapshot",
118
+ screen: deps.registry.screen(terminalId),
119
+ info: deps.registry.get(terminalId) ?? info,
120
+ environment: deps.environmentFor(info.cwd),
121
+ });
122
+ ws.on("message", (raw: unknown) => { void handleFrame(raw); });
123
+ ws.on("close", () => { detach(); });
124
+
125
+ /** One inbound client frame -> the registry. */
126
+ async function handleFrame(raw: unknown): Promise<void> {
127
+ let frame: UiClientFrame;
128
+ try {
129
+ const text = typeof raw === "string" ? raw : Buffer.isBuffer(raw) ? raw.toString("utf8") : "";
130
+ frame = JSON.parse(text) as UiClientFrame;
131
+ } catch {
132
+ send({ t: "error", code: "bad-request", message: "socket frames must be JSON" });
133
+ return;
134
+ }
135
+ try {
136
+ switch (frame.t) {
137
+ case "input":
138
+ await deps.registry.write(terminalId, typeof frame.data === "string" ? frame.data : "");
139
+ return;
140
+ case "resize": {
141
+ const resized = await deps.registry.resize(terminalId, frame.cols, frame.rows);
142
+ send({ t: "resized", resized });
143
+ return;
144
+ }
145
+ case "signal":
146
+ await deps.registry.signal(terminalId, frame.signal);
147
+ return;
148
+ case "close":
149
+ detach();
150
+ await deps.registry.close(terminalId);
151
+ ws.close(1000, "closed");
152
+ return;
153
+ case "ping":
154
+ send({ t: "pong" });
155
+ return;
156
+ default:
157
+ send({ t: "error", code: "bad-request", message: "unknown socket frame" });
158
+ }
159
+ } catch (error) {
160
+ const failure = error instanceof UiTerminalError
161
+ ? { code: error.code, message: error.message }
162
+ : { code: "spawn-failed", message: error instanceof Error ? error.message : String(error) };
163
+ send({ t: "error", ...failure });
164
+ }
165
+ }
166
+ }
@@ -13,5 +13,13 @@
13
13
  "skipLibCheck": true,
14
14
  "types": []
15
15
  },
16
- "include": ["src/client.tsx", "src/client-types.d.ts"]
16
+ "include": [
17
+ "src/client.tsx",
18
+ "src/locales.ts",
19
+ "src/browser-i18n.ts",
20
+ "src/webterminals/browser/**/*.ts",
21
+ "src/webterminals/browser/**/*.tsx",
22
+ "src/webterminals/protocol.ts",
23
+ "src/shell-id.ts"
24
+ ]
17
25
  }
package/tsconfig.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "types": ["node"]
20
20
  },
21
21
  "include": ["src/**/*.ts"],
22
- "exclude": ["src/client.tsx"]
22
+ "exclude": ["src/client.tsx", "src/webterminals/browser"]
23
23
  }