@celljs/ta-host 4.6.1 → 4.8.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/README.en.md +70 -0
- package/README.md +16 -3
- package/cell.schema.json +21 -0
- package/cell.yml +7 -0
- package/esm/node/auth-server-impl.d.ts +2 -0
- package/esm/node/auth-server-impl.js +7 -0
- package/esm/node/auth-server-impl.js.map +1 -1
- package/esm/node/exec-environment-factory.d.ts +8 -4
- package/esm/node/exec-environment-factory.js +28 -6
- package/esm/node/exec-environment-factory.js.map +1 -1
- package/esm/node/extension-providers.d.ts +22 -0
- package/esm/node/extension-providers.js +52 -0
- package/esm/node/extension-providers.js.map +1 -0
- package/esm/node/index.d.ts +3 -0
- package/esm/node/index.js +3 -0
- package/esm/node/index.js.map +1 -1
- package/esm/node/package-host-extension.d.ts +4 -2
- package/esm/node/package-host-extension.js +4 -3
- package/esm/node/package-host-extension.js.map +1 -1
- package/esm/node/package-hosts.d.ts +6 -2
- package/esm/node/package-hosts.js +32 -0
- package/esm/node/package-hosts.js.map +1 -1
- package/esm/node/remote/index.d.ts +5 -0
- package/esm/node/remote/index.js +6 -0
- package/esm/node/remote/index.js.map +1 -0
- package/esm/node/remote/pi-directories.d.ts +14 -0
- package/esm/node/remote/pi-directories.js +64 -0
- package/esm/node/remote/pi-directories.js.map +1 -0
- package/esm/node/remote/port-forwards.d.ts +35 -0
- package/esm/node/remote/port-forwards.js +150 -0
- package/esm/node/remote/port-forwards.js.map +1 -0
- package/esm/node/remote/remote-hosts.d.ts +40 -0
- package/esm/node/remote/remote-hosts.js +170 -0
- package/esm/node/remote/remote-hosts.js.map +1 -0
- package/esm/node/remote/remote-protocol.d.ts +51 -0
- package/esm/node/remote/remote-protocol.js +5 -0
- package/esm/node/remote/remote-protocol.js.map +1 -0
- package/esm/node/remote/remote-server-impl.d.ts +19 -0
- package/esm/node/remote/remote-server-impl.js +66 -0
- package/esm/node/remote/remote-server-impl.js.map +1 -0
- package/esm/node/remote-ui-context.d.ts +6 -1
- package/esm/node/remote-ui-context.js +31 -3
- package/esm/node/remote-ui-context.js.map +1 -1
- package/esm/node/session-files.d.ts +18 -0
- package/esm/node/session-files.js +32 -0
- package/esm/node/session-files.js.map +1 -0
- package/esm/node/settings-server-impl.d.ts +1 -0
- package/esm/node/settings-server-impl.js +8 -0
- package/esm/node/settings-server-impl.js.map +1 -1
- package/esm/node/settings-store.d.ts +9 -2
- package/esm/node/settings-store.js +26 -5
- package/esm/node/settings-store.js.map +1 -1
- package/esm/node/ta-server-impl.d.ts +2 -0
- package/esm/node/ta-server-impl.js +16 -4
- package/esm/node/ta-server-impl.js.map +1 -1
- package/esm/node/ta-session-factory.d.ts +19 -2
- package/esm/node/ta-session-factory.js +54 -11
- package/esm/node/ta-session-factory.js.map +1 -1
- package/esm/node/ta-session.d.ts +4 -0
- package/esm/node/ta-session.js +6 -1
- package/esm/node/ta-session.js.map +1 -1
- package/esm/node/terminal-service.d.ts +18 -2
- package/esm/node/terminal-service.js +46 -10
- package/esm/node/terminal-service.js.map +1 -1
- package/esm/node/terminal-util.d.ts +14 -0
- package/esm/node/terminal-util.js +30 -1
- package/esm/node/terminal-util.js.map +1 -1
- package/esm/node/web-extension-catalog.d.ts +3 -1
- package/esm/node/web-extension-catalog.js +8 -2
- package/esm/node/web-extension-catalog.js.map +1 -1
- package/esm/node/working-directory.d.ts +12 -1
- package/esm/node/working-directory.js +22 -1
- package/esm/node/working-directory.js.map +1 -1
- package/esm/node/workspace-files.d.ts +1 -1
- package/esm/node/workspace-files.js +3 -3
- package/esm/node/workspace-files.js.map +1 -1
- package/esm/node/workspace-protocol.d.ts +2 -0
- package/esm/node/workspace-server-impl.d.ts +2 -1
- package/esm/node/workspace-server-impl.js +13 -1
- package/esm/node/workspace-server-impl.js.map +1 -1
- package/esm/node/workspace-service.d.ts +1 -0
- package/esm/node/workspace-service.js +15 -3
- package/esm/node/workspace-service.js.map +1 -1
- package/lib/node/auth-server-impl.d.ts +2 -0
- package/lib/node/auth-server-impl.d.ts.map +1 -1
- package/lib/node/auth-server-impl.js +7 -0
- package/lib/node/auth-server-impl.js.map +1 -1
- package/lib/node/exec-environment-factory.d.ts +8 -4
- package/lib/node/exec-environment-factory.d.ts.map +1 -1
- package/lib/node/exec-environment-factory.js +28 -6
- package/lib/node/exec-environment-factory.js.map +1 -1
- package/lib/node/extension-providers.d.ts +23 -0
- package/lib/node/extension-providers.d.ts.map +1 -0
- package/lib/node/extension-providers.js +55 -0
- package/lib/node/extension-providers.js.map +1 -0
- package/lib/node/index.d.ts +3 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +3 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/package-host-extension.d.ts +4 -2
- package/lib/node/package-host-extension.d.ts.map +1 -1
- package/lib/node/package-host-extension.js +4 -3
- package/lib/node/package-host-extension.js.map +1 -1
- package/lib/node/package-hosts.d.ts +6 -2
- package/lib/node/package-hosts.d.ts.map +1 -1
- package/lib/node/package-hosts.js +32 -0
- package/lib/node/package-hosts.js.map +1 -1
- package/lib/node/remote/index.d.ts +6 -0
- package/lib/node/remote/index.d.ts.map +1 -0
- package/lib/node/remote/index.js +22 -0
- package/lib/node/remote/index.js.map +1 -0
- package/lib/node/remote/pi-directories.d.ts +15 -0
- package/lib/node/remote/pi-directories.d.ts.map +1 -0
- package/lib/node/remote/pi-directories.js +67 -0
- package/lib/node/remote/pi-directories.js.map +1 -0
- package/lib/node/remote/port-forwards.d.ts +36 -0
- package/lib/node/remote/port-forwards.d.ts.map +1 -0
- package/lib/node/remote/port-forwards.js +153 -0
- package/lib/node/remote/port-forwards.js.map +1 -0
- package/lib/node/remote/remote-hosts.d.ts +41 -0
- package/lib/node/remote/remote-hosts.d.ts.map +1 -0
- package/lib/node/remote/remote-hosts.js +173 -0
- package/lib/node/remote/remote-hosts.js.map +1 -0
- package/lib/node/remote/remote-protocol.d.ts +52 -0
- package/lib/node/remote/remote-protocol.d.ts.map +1 -0
- package/lib/node/remote/remote-protocol.js +8 -0
- package/lib/node/remote/remote-protocol.js.map +1 -0
- package/lib/node/remote/remote-server-impl.d.ts +20 -0
- package/lib/node/remote/remote-server-impl.d.ts.map +1 -0
- package/lib/node/remote/remote-server-impl.js +69 -0
- package/lib/node/remote/remote-server-impl.js.map +1 -0
- package/lib/node/remote/remote.spec.d.ts +2 -0
- package/lib/node/remote/remote.spec.d.ts.map +1 -0
- package/lib/node/remote/remote.spec.js +130 -0
- package/lib/node/remote/remote.spec.js.map +1 -0
- package/lib/node/remote-ui-context.d.ts +6 -1
- package/lib/node/remote-ui-context.d.ts.map +1 -1
- package/lib/node/remote-ui-context.js +31 -3
- package/lib/node/remote-ui-context.js.map +1 -1
- package/lib/node/remote-ui-context.spec.js +23 -0
- package/lib/node/remote-ui-context.spec.js.map +1 -1
- package/lib/node/session-files.d.ts +19 -0
- package/lib/node/session-files.d.ts.map +1 -0
- package/lib/node/session-files.js +36 -0
- package/lib/node/session-files.js.map +1 -0
- package/lib/node/session-files.spec.d.ts +2 -0
- package/lib/node/session-files.spec.d.ts.map +1 -0
- package/lib/node/session-files.spec.js +51 -0
- package/lib/node/session-files.spec.js.map +1 -0
- package/lib/node/settings-server-impl.d.ts +1 -0
- package/lib/node/settings-server-impl.d.ts.map +1 -1
- package/lib/node/settings-server-impl.js +8 -0
- package/lib/node/settings-server-impl.js.map +1 -1
- package/lib/node/settings-store.d.ts +9 -2
- package/lib/node/settings-store.d.ts.map +1 -1
- package/lib/node/settings-store.js +26 -5
- package/lib/node/settings-store.js.map +1 -1
- package/lib/node/settings-store.spec.js +5 -2
- package/lib/node/settings-store.spec.js.map +1 -1
- package/lib/node/ta-server-impl.d.ts +2 -0
- package/lib/node/ta-server-impl.d.ts.map +1 -1
- package/lib/node/ta-server-impl.js +16 -4
- package/lib/node/ta-server-impl.js.map +1 -1
- package/lib/node/ta-session-factory.d.ts +19 -2
- package/lib/node/ta-session-factory.d.ts.map +1 -1
- package/lib/node/ta-session-factory.js +53 -10
- package/lib/node/ta-session-factory.js.map +1 -1
- package/lib/node/ta-session.d.ts +4 -0
- package/lib/node/ta-session.d.ts.map +1 -1
- package/lib/node/ta-session.js +6 -1
- package/lib/node/ta-session.js.map +1 -1
- package/lib/node/terminal-service.d.ts +18 -2
- package/lib/node/terminal-service.d.ts.map +1 -1
- package/lib/node/terminal-service.js +45 -9
- package/lib/node/terminal-service.js.map +1 -1
- package/lib/node/terminal-service.spec.js +31 -0
- package/lib/node/terminal-service.spec.js.map +1 -1
- package/lib/node/terminal-util.d.ts +14 -0
- package/lib/node/terminal-util.d.ts.map +1 -1
- package/lib/node/terminal-util.js +30 -1
- package/lib/node/terminal-util.js.map +1 -1
- package/lib/node/terminal-util.spec.js +24 -0
- package/lib/node/terminal-util.spec.js.map +1 -1
- package/lib/node/web-extension-catalog.d.ts +3 -1
- package/lib/node/web-extension-catalog.d.ts.map +1 -1
- package/lib/node/web-extension-catalog.js +7 -1
- package/lib/node/web-extension-catalog.js.map +1 -1
- package/lib/node/web-extension-catalog.spec.js +1 -1
- package/lib/node/web-extension-catalog.spec.js.map +1 -1
- package/lib/node/working-directory.d.ts +12 -1
- package/lib/node/working-directory.d.ts.map +1 -1
- package/lib/node/working-directory.js +22 -1
- package/lib/node/working-directory.js.map +1 -1
- package/lib/node/workspace-files.d.ts +1 -1
- package/lib/node/workspace-files.d.ts.map +1 -1
- package/lib/node/workspace-files.js +3 -3
- package/lib/node/workspace-files.js.map +1 -1
- package/lib/node/workspace-protocol.d.ts +2 -0
- package/lib/node/workspace-protocol.d.ts.map +1 -1
- package/lib/node/workspace-server-impl.d.ts +2 -1
- package/lib/node/workspace-server-impl.d.ts.map +1 -1
- package/lib/node/workspace-server-impl.js +12 -0
- package/lib/node/workspace-server-impl.js.map +1 -1
- package/lib/node/workspace-service.d.ts +1 -0
- package/lib/node/workspace-service.d.ts.map +1 -1
- package/lib/node/workspace-service.js +15 -3
- package/lib/node/workspace-service.js.map +1 -1
- package/lib/node/workspace-service.spec.js +9 -0
- package/lib/node/workspace-service.spec.js.map +1 -1
- package/package.json +14 -10
- package/src/node/auth-server-impl.ts +6 -0
- package/src/node/exec-environment-factory.ts +26 -6
- package/src/node/extension-providers.ts +49 -0
- package/src/node/index.ts +3 -0
- package/src/node/package-host-extension.ts +7 -4
- package/src/node/package-hosts.ts +32 -2
- package/src/node/remote/index.ts +5 -0
- package/src/node/remote/pi-directories.ts +49 -0
- package/src/node/remote/port-forwards.ts +148 -0
- package/src/node/remote/remote-hosts.ts +170 -0
- package/src/node/remote/remote-protocol.ts +57 -0
- package/src/node/remote/remote-server-impl.ts +60 -0
- package/src/node/remote/remote.spec.ts +142 -0
- package/src/node/remote-ui-context.spec.ts +25 -0
- package/src/node/remote-ui-context.ts +34 -5
- package/src/node/session-files.spec.ts +52 -0
- package/src/node/session-files.ts +41 -0
- package/src/node/settings-server-impl.ts +9 -0
- package/src/node/settings-store.spec.ts +5 -2
- package/src/node/settings-store.ts +25 -6
- package/src/node/ta-server-impl.ts +18 -4
- package/src/node/ta-session-factory.ts +56 -10
- package/src/node/ta-session.ts +9 -1
- package/src/node/terminal-service.spec.ts +35 -0
- package/src/node/terminal-service.ts +47 -15
- package/src/node/terminal-util.spec.ts +31 -0
- package/src/node/terminal-util.ts +32 -1
- package/src/node/web-extension-catalog.spec.ts +1 -1
- package/src/node/web-extension-catalog.ts +7 -2
- package/src/node/working-directory.ts +35 -2
- package/src/node/workspace-files.ts +3 -3
- package/src/node/workspace-protocol.ts +2 -0
- package/src/node/workspace-server-impl.ts +18 -0
- package/src/node/workspace-service.spec.ts +11 -1
- package/src/node/workspace-service.ts +15 -3
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { CODING_AGENT_OPTIONS, type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
3
|
+
import { type Application, ApplicationLifecycle, Autowired, Component, Value } from '@celljs/core';
|
|
4
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
5
|
+
import { type ExecClient, SshConfig, SshHost, type SshQuestion } from '@celljs/ta-exec';
|
|
6
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
7
|
+
import { TaClient, TaLocation, type TaRemoteEvent, type TaRemoteHost, type TaRemotePrompt } from '@celljs/ta-protocol';
|
|
8
|
+
import { TaErrors } from '../ta-errors';
|
|
9
|
+
import { TaWorkingDirectory } from '../working-directory';
|
|
10
|
+
import { RemoteHosts, TA_REMOTE_OPTIONS, type TaRemoteOptions } from './remote-protocol';
|
|
11
|
+
|
|
12
|
+
/** A prompt nobody answers is cancelled after this: ssh gives up. */
|
|
13
|
+
const PROMPT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
14
|
+
|
|
15
|
+
interface Waiting {
|
|
16
|
+
prompt: TaRemotePrompt;
|
|
17
|
+
settle(value: string | undefined): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* One SshHost per host, which connects on the first call there and again after a loss. What ssh asks
|
|
22
|
+
* goes to every client that watches, and the first answer counts, as with the dialogs of sessions.
|
|
23
|
+
*/
|
|
24
|
+
@Component([RemoteHosts, ApplicationLifecycle])
|
|
25
|
+
export class DefaultRemoteHosts implements RemoteHosts, ApplicationLifecycle<Application> {
|
|
26
|
+
@Value(TA_REMOTE_OPTIONS)
|
|
27
|
+
protected readonly options?: TaRemoteOptions;
|
|
28
|
+
|
|
29
|
+
@Value(CODING_AGENT_OPTIONS)
|
|
30
|
+
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
31
|
+
|
|
32
|
+
@Autowired(TaWorkingDirectory)
|
|
33
|
+
protected readonly workingDirectory: TaWorkingDirectory;
|
|
34
|
+
|
|
35
|
+
protected readonly hosts = new Map<string, SshHost>();
|
|
36
|
+
protected readonly waiting = new Map<string, Waiting>();
|
|
37
|
+
protected readonly watchers = new Set<RpcConnection>();
|
|
38
|
+
protected readonly disconnectListeners = new Set<(host: string) => void>();
|
|
39
|
+
|
|
40
|
+
client(host: string): ExecClient {
|
|
41
|
+
this.check(host);
|
|
42
|
+
let ssh = this.hosts.get(host);
|
|
43
|
+
if (!ssh) {
|
|
44
|
+
ssh = new SshHost({
|
|
45
|
+
host,
|
|
46
|
+
stateDir: this.codingAgentOptions?.agentDir ?? TaBrand.agentDir(),
|
|
47
|
+
ssh: this.options?.ssh || undefined,
|
|
48
|
+
sshArgs: this.options?.sshArgs,
|
|
49
|
+
nodeMirror: this.options?.nodeMirror || undefined,
|
|
50
|
+
ask: question => this.ask(host, question),
|
|
51
|
+
onStep: (step, detail) => this.broadcast({ type: 'progress', host, step, detail }),
|
|
52
|
+
});
|
|
53
|
+
ssh.client.onState((connected, error) => {
|
|
54
|
+
if (connected) {
|
|
55
|
+
this.workingDirectory.addRecentHost(host);
|
|
56
|
+
}
|
|
57
|
+
this.broadcast(error ? { type: 'connection', host, connected, error } : { type: 'connection', host, connected });
|
|
58
|
+
});
|
|
59
|
+
this.hosts.set(host, ssh);
|
|
60
|
+
}
|
|
61
|
+
return ssh.client;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
list(): TaRemoteHost[] {
|
|
65
|
+
const recent = this.workingDirectory.recentHosts();
|
|
66
|
+
const config = SshConfig.hosts().filter(host => TaLocation.isHost(host) && !recent.includes(host));
|
|
67
|
+
return [
|
|
68
|
+
...recent.map(host => ({ host, source: 'recent' as const, connected: this.connected(host) })),
|
|
69
|
+
...config.map(host => ({ host, source: 'config' as const, connected: this.connected(host) })),
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
add(host: string): void {
|
|
74
|
+
this.workingDirectory.addRecentHost(this.check(host));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async remove(host: string): Promise<void> {
|
|
78
|
+
await this.disconnect(host);
|
|
79
|
+
this.workingDirectory.removeRecentHost(host);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async resolve(location: string): Promise<string> {
|
|
83
|
+
const remote = this.parse(location);
|
|
84
|
+
if (!remote || remote.path) {
|
|
85
|
+
return location;
|
|
86
|
+
}
|
|
87
|
+
return TaLocation.ssh(remote.host, (await this.client(remote.host).info()).cwd);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async disconnect(host: string): Promise<void> {
|
|
91
|
+
const ssh = this.hosts.get(host);
|
|
92
|
+
this.hosts.delete(host);
|
|
93
|
+
for (const listener of this.disconnectListeners) {
|
|
94
|
+
listener(host);
|
|
95
|
+
}
|
|
96
|
+
await ssh?.dispose();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
onDisconnect(listener: (host: string) => void): () => void {
|
|
100
|
+
this.disconnectListeners.add(listener);
|
|
101
|
+
return () => this.disconnectListeners.delete(listener);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
watch(connection: RpcConnection): TaRemotePrompt[] {
|
|
105
|
+
if (!this.watchers.has(connection)) {
|
|
106
|
+
this.watchers.add(connection);
|
|
107
|
+
connection.onClose(() => this.watchers.delete(connection));
|
|
108
|
+
}
|
|
109
|
+
return [...this.waiting.values()].map(({ prompt }) => prompt);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
answer(promptId: string, value: string | null): boolean {
|
|
113
|
+
const waiting = this.waiting.get(promptId);
|
|
114
|
+
waiting?.settle(typeof value === 'string' ? value : undefined);
|
|
115
|
+
return waiting !== undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async onStop(): Promise<void> {
|
|
119
|
+
for (const waiting of [...this.waiting.values()]) {
|
|
120
|
+
waiting.settle(undefined);
|
|
121
|
+
}
|
|
122
|
+
await Promise.all([...this.hosts.keys()].map(host => this.disconnect(host)));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected ask(host: string, question: SshQuestion): Promise<string | undefined> {
|
|
126
|
+
const prompt: TaRemotePrompt = { promptId: randomUUID(), host, ...question };
|
|
127
|
+
return new Promise(resolve => {
|
|
128
|
+
const timer = setTimeout(() => settle(undefined), PROMPT_TIMEOUT_MS);
|
|
129
|
+
const settle = (value: string | undefined) => {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
if (this.waiting.delete(prompt.promptId)) {
|
|
132
|
+
this.broadcast({ type: 'prompt_closed', promptId: prompt.promptId });
|
|
133
|
+
resolve(value);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
this.waiting.set(prompt.promptId, { prompt, settle });
|
|
137
|
+
this.broadcast({ type: 'prompt', prompt });
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
protected broadcast(event: TaRemoteEvent): void {
|
|
142
|
+
for (const connection of this.watchers) {
|
|
143
|
+
if (!connection.closed) {
|
|
144
|
+
connection
|
|
145
|
+
.client<TaClient>(TaClient)
|
|
146
|
+
.onRemoteEvent(event)
|
|
147
|
+
.catch(() => undefined);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
protected check(host: string): string {
|
|
153
|
+
if (typeof host !== 'string' || !TaLocation.isHost(host)) {
|
|
154
|
+
throw TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config, or [user@]host[:port].');
|
|
155
|
+
}
|
|
156
|
+
return host;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
protected connected(host: string): boolean {
|
|
160
|
+
return this.hosts.get(host)?.client.connected ?? false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
protected parse(location: string): TaLocation.Remote | undefined {
|
|
164
|
+
try {
|
|
165
|
+
return TaLocation.parse(location);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
throw TaErrors.invalidArgument((error as Error).message, 'Write ssh://<host>/<absolute path>.');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
2
|
+
import type { ExecClient } from '@celljs/ta-exec';
|
|
3
|
+
import type { TaForwardedPort, TaRemoteHost, TaRemotePrompt } from '@celljs/ta-protocol';
|
|
4
|
+
|
|
5
|
+
export const TA_REMOTE_OPTIONS = 'ta.remote';
|
|
6
|
+
|
|
7
|
+
/** `ta.remote`: how ta serve reaches other machines. */
|
|
8
|
+
export interface TaRemoteOptions {
|
|
9
|
+
/** The ssh command; default `ssh`. */
|
|
10
|
+
ssh?: string;
|
|
11
|
+
/** Before each host, as `-F <file>`. */
|
|
12
|
+
sshArgs?: string[];
|
|
13
|
+
/** Where Node.js comes from for hosts without one; empty: nodejs.org. */
|
|
14
|
+
nodeMirror?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const RemoteHosts = Symbol('RemoteHosts');
|
|
18
|
+
export const PiDirectories = Symbol('PiDirectories');
|
|
19
|
+
export const PortForwards = Symbol('PortForwards');
|
|
20
|
+
|
|
21
|
+
/** The hosts of `ssh://` locations, one connection each, and what their ssh asks the users. */
|
|
22
|
+
export interface RemoteHosts {
|
|
23
|
+
/** The connection to a host, made on its first call. */
|
|
24
|
+
client(host: string): ExecClient;
|
|
25
|
+
list(): TaRemoteHost[];
|
|
26
|
+
/** Keeps a host in the list without connecting; throws for what is not a host. */
|
|
27
|
+
add(host: string): void;
|
|
28
|
+
/** Disconnects and drops it from the list of those opened before. */
|
|
29
|
+
remove(host: string): Promise<void>;
|
|
30
|
+
/** The location with the home directory as its path when it has none. */
|
|
31
|
+
resolve(location: string): Promise<string>;
|
|
32
|
+
disconnect(host: string): Promise<void>;
|
|
33
|
+
/** Runs once a host is disconnected on purpose. */
|
|
34
|
+
onDisconnect(listener: (host: string) => void): () => void;
|
|
35
|
+
/** The connection hears TaClient.onRemoteEvent; the prompts that wait now. */
|
|
36
|
+
watch(connection: RpcConnection): TaRemotePrompt[];
|
|
37
|
+
answer(promptId: string, value: string | null): boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The directory pi works in for a location. pi reads and writes this machine's files in it (its
|
|
42
|
+
* sessions are named after it, the project's settings are there), so a directory of another machine
|
|
43
|
+
* gets one here, empty, under `<agent directory>/remote/`; the agent's tools work there.
|
|
44
|
+
*/
|
|
45
|
+
export interface PiDirectories {
|
|
46
|
+
/** Creates it when it is not there. */
|
|
47
|
+
toPi(location: string): string;
|
|
48
|
+
/** The location of a directory that `toPi` gave; any other is a directory of this machine. */
|
|
49
|
+
fromPi(cwd: string): string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Ports of other machines, opened on this one. */
|
|
53
|
+
export interface PortForwards {
|
|
54
|
+
forward(cwd: string, port: number): Promise<TaForwardedPort>;
|
|
55
|
+
list(): TaForwardedPort[];
|
|
56
|
+
close(host: string, remotePort: number): Promise<void>;
|
|
57
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Autowired } from '@celljs/core';
|
|
2
|
+
import { Rpc } from '@celljs/rpc';
|
|
3
|
+
import { RpcContext } from '@celljs/rpc/lib/node';
|
|
4
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
5
|
+
import { type TaForwardedPort, type TaRemoteHost, type TaRemotePrompt, TaRemoteServer } from '@celljs/ta-protocol';
|
|
6
|
+
import { TaErrors } from '../ta-errors';
|
|
7
|
+
import { PortForwards, RemoteHosts } from './remote-protocol';
|
|
8
|
+
|
|
9
|
+
/** The other machines that conversations work on, for the clients. */
|
|
10
|
+
@Rpc(TaRemoteServer)
|
|
11
|
+
export class TaRemoteServerImpl implements TaRemoteServer {
|
|
12
|
+
@Autowired(RemoteHosts)
|
|
13
|
+
protected readonly hosts: RemoteHosts;
|
|
14
|
+
|
|
15
|
+
@Autowired(PortForwards)
|
|
16
|
+
protected readonly ports: PortForwards;
|
|
17
|
+
|
|
18
|
+
@Autowired(ExecEnvironmentFactory)
|
|
19
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
20
|
+
|
|
21
|
+
async listHosts(): Promise<TaRemoteHost[]> {
|
|
22
|
+
return TaErrors.guard(() => this.hosts.list());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
addHost(host: string): Promise<void> {
|
|
26
|
+
return TaErrors.guard(() => this.hosts.add(host));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
removeHost(host: string): Promise<void> {
|
|
30
|
+
return TaErrors.guard(() => this.hosts.remove(host));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
resolveLocation(location: string): Promise<string> {
|
|
34
|
+
return TaErrors.guard(() => this.hosts.resolve(location));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
forwardPort(port: number, cwd?: string): Promise<TaForwardedPort> {
|
|
38
|
+
return TaErrors.guard(async () => this.ports.forward(cwd || (await this.environments.defaultCwd()), port));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async listForwardedPorts(): Promise<TaForwardedPort[]> {
|
|
42
|
+
return this.ports.list();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
closeForwardedPort(host: string, remotePort: number): Promise<void> {
|
|
46
|
+
return TaErrors.guard(() => this.ports.close(host, remotePort));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async watch(): Promise<TaRemotePrompt[]> {
|
|
50
|
+
return this.hosts.watch(RpcContext.requireConnection());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async answer(promptId: string, value: string | null): Promise<boolean> {
|
|
54
|
+
return this.hosts.answer(promptId, value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
disconnect(host: string): Promise<void> {
|
|
58
|
+
return TaErrors.guard(() => this.hosts.disconnect(host));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { afterAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { existsSync, mkdtempSync } from 'node:fs';
|
|
3
|
+
import { createServer } from 'node:net';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
7
|
+
import { type ExecEnvironment, type ExecSocket, LocalExecEnvironment } from '@celljs/ta-exec';
|
|
8
|
+
import type { TaRemoteEvent } from '@celljs/ta-protocol';
|
|
9
|
+
import { DefaultPiDirectories } from './pi-directories';
|
|
10
|
+
import { DefaultPortForwards } from './port-forwards';
|
|
11
|
+
import { DefaultRemoteHosts } from './remote-hosts';
|
|
12
|
+
|
|
13
|
+
const root = mkdtempSync(join(tmpdir(), 'ta-remote-'));
|
|
14
|
+
|
|
15
|
+
function directories(execUrl = ''): DefaultPiDirectories {
|
|
16
|
+
return Object.assign(new DefaultPiDirectories(), { codingAgentOptions: { agentDir: root }, execOptions: { url: execUrl } });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('DefaultPiDirectories', () => {
|
|
20
|
+
it('keeps a directory of this machine', () => {
|
|
21
|
+
expect(directories().toPi('/work/app')).toBe('/work/app');
|
|
22
|
+
expect(directories().fromPi('/work/app')).toBe('/work/app');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('gives a directory of another machine one here, and back', () => {
|
|
26
|
+
const dirs = directories();
|
|
27
|
+
const cwd = dirs.toPi('ssh://me@dev:2222/srv/app');
|
|
28
|
+
expect(cwd).toBe(join(root, 'remote', 'ssh', 'me@dev:2222', 'srv', 'app'));
|
|
29
|
+
expect(existsSync(cwd)).toBe(true);
|
|
30
|
+
expect(dirs.fromPi(cwd)).toBe('ssh://me@dev:2222/srv/app');
|
|
31
|
+
expect(dirs.fromPi(dirs.toPi('ssh://dev/'))).toBe('ssh://dev/');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('gives every directory one here with an exec server', () => {
|
|
35
|
+
const dirs = directories('ws://exec');
|
|
36
|
+
const cwd = dirs.toPi('/srv/app');
|
|
37
|
+
expect(cwd).toBe(join(root, 'remote', 'exec', 'srv', 'app'));
|
|
38
|
+
expect(dirs.fromPi(cwd)).toBe('/srv/app');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('DefaultRemoteHosts', () => {
|
|
43
|
+
it('asks every client that watches, and the first answer counts', async () => {
|
|
44
|
+
const events: TaRemoteEvent[][] = [[], []];
|
|
45
|
+
const connections = events.map(
|
|
46
|
+
received =>
|
|
47
|
+
({
|
|
48
|
+
closed: false,
|
|
49
|
+
onClose: () => undefined,
|
|
50
|
+
client: () => ({ onRemoteEvent: async (event: TaRemoteEvent) => void received.push(event) }),
|
|
51
|
+
}) as unknown as RpcConnection,
|
|
52
|
+
);
|
|
53
|
+
const hosts = new DefaultRemoteHosts();
|
|
54
|
+
expect(hosts.watch(connections[0])).toEqual([]);
|
|
55
|
+
const asked = (hosts as unknown as { ask: DefaultRemoteHosts['ask'] }).ask.call(hosts, 'dev', { kind: 'secret', message: 'Password:' });
|
|
56
|
+
const [prompt] = hosts.watch(connections[1]);
|
|
57
|
+
expect(prompt).toMatchObject({ host: 'dev', kind: 'secret', message: 'Password:' });
|
|
58
|
+
expect(events[0]).toEqual([{ type: 'prompt', prompt }]);
|
|
59
|
+
expect(hosts.answer(prompt.promptId, 's3cret')).toBe(true);
|
|
60
|
+
expect(hosts.answer(prompt.promptId, 'again')).toBe(false);
|
|
61
|
+
expect(await asked).toBe('s3cret');
|
|
62
|
+
expect(events[1]).toEqual([{ type: 'prompt_closed', promptId: prompt.promptId }]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('refuses a host that ssh would read as an option', () => {
|
|
66
|
+
expect(() => new DefaultRemoteHosts().client('-oProxyCommand=x')).toThrow();
|
|
67
|
+
expect(() => new DefaultRemoteHosts().add('-oProxyCommand=x')).toThrow();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('keeps the hosts added, the latest first, and forgets one removed', async () => {
|
|
71
|
+
let recent: string[] = [];
|
|
72
|
+
const hosts = Object.assign(new DefaultRemoteHosts(), {
|
|
73
|
+
workingDirectory: {
|
|
74
|
+
recentHosts: () => recent,
|
|
75
|
+
addRecentHost: (host: string) => (recent = [host, ...recent.filter(other => other !== host)]),
|
|
76
|
+
removeRecentHost: (host: string) => (recent = recent.filter(other => other !== host)),
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
hosts.add('dev');
|
|
80
|
+
hosts.add('me@prod:2222');
|
|
81
|
+
expect(hosts.list().filter(host => host.source === 'recent')).toEqual([
|
|
82
|
+
{ host: 'me@prod:2222', source: 'recent', connected: false },
|
|
83
|
+
{ host: 'dev', source: 'recent', connected: false },
|
|
84
|
+
]);
|
|
85
|
+
await hosts.remove('dev');
|
|
86
|
+
expect(recent).toEqual(['me@prod:2222']);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('DefaultPortForwards', () => {
|
|
91
|
+
/** A machine whose connections are those of this one, as the server of Ta makes them there. */
|
|
92
|
+
class Remote extends LocalExecEnvironment {
|
|
93
|
+
async connect(port: number): Promise<ExecSocket> {
|
|
94
|
+
const { connect } = await import('node:net');
|
|
95
|
+
const socket = connect(port, '127.0.0.1');
|
|
96
|
+
await new Promise((resolve, reject) => socket.once('connect', resolve).once('error', reject));
|
|
97
|
+
return {
|
|
98
|
+
write: data => socket.write(data),
|
|
99
|
+
end: () => socket.end(),
|
|
100
|
+
onData: listener => socket.on('data', listener),
|
|
101
|
+
onClose: listener => socket.once('close', listener),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const forwards = new DefaultPortForwards();
|
|
106
|
+
const local = new LocalExecEnvironment(root);
|
|
107
|
+
const remote = new Remote(root);
|
|
108
|
+
Object.assign(forwards, {
|
|
109
|
+
environments: { create: (cwd: string): ExecEnvironment => (cwd.startsWith('ssh://') ? remote : local) },
|
|
110
|
+
hosts: { onDisconnect: () => () => undefined },
|
|
111
|
+
});
|
|
112
|
+
afterAll(() => forwards.onStop());
|
|
113
|
+
|
|
114
|
+
it('keeps a port of this machine as it is', async () => {
|
|
115
|
+
expect(await forwards.forward('/work', 3000)).toEqual({ host: '', remotePort: 3000, localPort: 3000 });
|
|
116
|
+
await expect(forwards.forward('/work', 70000)).rejects.toThrow();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('opens a port of another machine here, and carries data both ways', async () => {
|
|
120
|
+
const server = createServer(socket => socket.on('data', data => socket.end(`echo:${data}`)));
|
|
121
|
+
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
122
|
+
const port = (server.address() as { port: number }).port;
|
|
123
|
+
try {
|
|
124
|
+
const forwarded = await forwards.forward('ssh://dev/srv', port);
|
|
125
|
+
expect(forwarded.host).toBe('dev');
|
|
126
|
+
expect(forwarded.localPort).not.toBe(port);
|
|
127
|
+
expect(await forwards.forward('ssh://dev/srv', port)).toEqual(forwarded);
|
|
128
|
+
expect(forwards.list()).toEqual([forwarded]);
|
|
129
|
+
const { connect } = await import('node:net');
|
|
130
|
+
const client = connect(forwarded.localPort, '127.0.0.1');
|
|
131
|
+
const received: Buffer[] = [];
|
|
132
|
+
client.on('data', data => received.push(data));
|
|
133
|
+
client.write('hi');
|
|
134
|
+
await new Promise(resolve => client.once('close', resolve));
|
|
135
|
+
expect(Buffer.concat(received).toString()).toBe('echo:hi');
|
|
136
|
+
await forwards.close('dev', port);
|
|
137
|
+
expect(forwards.list()).toEqual([]);
|
|
138
|
+
} finally {
|
|
139
|
+
server.close();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -65,4 +65,29 @@ describe('RemoteUiContext', () => {
|
|
|
65
65
|
expect(ui.theme.fg('accent', 'text')).toBe('text');
|
|
66
66
|
expect(ui.theme.bold('text')).toBe('text');
|
|
67
67
|
});
|
|
68
|
+
|
|
69
|
+
it('keeps its methods when pi copies it, as it does when it binds extensions', () => {
|
|
70
|
+
const { ui, events } = setup();
|
|
71
|
+
// pi's runner copies the context so: only own properties survive.
|
|
72
|
+
const copy: Pick<RemoteUiContext, 'notify' | 'setStatus'> = Object.assign({}, ui);
|
|
73
|
+
copy.notify('Saved', 'warning');
|
|
74
|
+
copy.setStatus('k', 'v');
|
|
75
|
+
expect(events.map(event => event.method)).toEqual(['notify', 'setStatus']);
|
|
76
|
+
expect(events[0]).toMatchObject({ type: 'extension_ui_request', message: 'Saved', notifyType: 'warning' });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('keeps what the extensions show now, for a client that attaches later', () => {
|
|
80
|
+
const { ui } = setup();
|
|
81
|
+
ui.setStatus('wx', '[WeChat] ⏳ signing in');
|
|
82
|
+
ui.setStatus('wx', '[WeChat] ✅ connected');
|
|
83
|
+
ui.setStatus('git', 'clean');
|
|
84
|
+
ui.setStatus('git', undefined);
|
|
85
|
+
ui.setWidget('todo', ['- a', '- b'], { placement: 'belowEditor' });
|
|
86
|
+
ui.setWidget('gone', ['x']);
|
|
87
|
+
ui.setWidget('gone', undefined);
|
|
88
|
+
expect(ui.shownRequests).toMatchObject([
|
|
89
|
+
{ method: 'setStatus', statusKey: 'wx', statusText: '[WeChat] ✅ connected' },
|
|
90
|
+
{ method: 'setWidget', widgetKey: 'todo', widgetLines: ['- a', '- b'], widgetPlacement: 'belowEditor' },
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
68
93
|
});
|
|
@@ -29,15 +29,32 @@ const PLAIN_THEME = new Proxy(
|
|
|
29
29
|
*/
|
|
30
30
|
export class RemoteUiContext implements ExtensionUIContext {
|
|
31
31
|
protected readonly pending = new Map<string, PendingDialog>();
|
|
32
|
+
/** The last status and widget of each key, for clients that attach later. */
|
|
33
|
+
protected readonly shown = new Map<string, RpcExtensionUIRequest>();
|
|
32
34
|
|
|
33
35
|
readonly theme = PLAIN_THEME;
|
|
34
36
|
|
|
35
|
-
constructor(protected readonly publish: (event: TaEvent) => void) {
|
|
37
|
+
constructor(protected readonly publish: (event: TaEvent) => void) {
|
|
38
|
+
// pi copies the context it is given (`{ ...ui }`), which keeps only own properties: the methods go on the object.
|
|
39
|
+
for (let proto = Object.getPrototypeOf(this); proto && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
40
|
+
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
41
|
+
const method = Object.getOwnPropertyDescriptor(proto, name)?.value;
|
|
42
|
+
if (name !== 'constructor' && typeof method === 'function' && !Object.getOwnPropertyDescriptor(this, name)) {
|
|
43
|
+
Object.defineProperty(this, name, { value: method.bind(this), enumerable: true, writable: true, configurable: true });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
36
48
|
|
|
37
49
|
get pendingRequests(): RpcExtensionUIRequest[] {
|
|
38
50
|
return [...this.pending.values()].map(dialog => dialog.request);
|
|
39
51
|
}
|
|
40
52
|
|
|
53
|
+
/** What the extensions show now with `setStatus` and `setWidget`. */
|
|
54
|
+
get shownRequests(): RpcExtensionUIRequest[] {
|
|
55
|
+
return [...this.shown.values()];
|
|
56
|
+
}
|
|
57
|
+
|
|
41
58
|
/** False when the dialog was answered before or ended. */
|
|
42
59
|
respond(response: RpcExtensionUIResponse): boolean {
|
|
43
60
|
const dialog = this.pending.get(response.id);
|
|
@@ -73,13 +90,15 @@ export class RemoteUiContext implements ExtensionUIContext {
|
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
setStatus(key: string, text: string | undefined): void {
|
|
76
|
-
this.tell({ method: 'setStatus', statusKey: key, statusText: text });
|
|
93
|
+
const request = this.tell({ method: 'setStatus', statusKey: key, statusText: text });
|
|
94
|
+
this.keep(`status:${key}`, text ? request : undefined);
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
setWidget(key: string, content: unknown, options?: { placement?: 'aboveEditor' | 'belowEditor' }): void {
|
|
80
98
|
// Clients cannot run a component factory; pi's RPC mode ignores those too.
|
|
81
99
|
if (content === undefined || Array.isArray(content)) {
|
|
82
|
-
this.tell({ method: 'setWidget', widgetKey: key, widgetLines: content, widgetPlacement: options?.placement });
|
|
100
|
+
const request = this.tell({ method: 'setWidget', widgetKey: key, widgetLines: content, widgetPlacement: options?.placement });
|
|
101
|
+
this.keep(`widget:${key}`, content?.length ? request : undefined);
|
|
83
102
|
}
|
|
84
103
|
}
|
|
85
104
|
|
|
@@ -131,8 +150,18 @@ export class RemoteUiContext implements ExtensionUIContext {
|
|
|
131
150
|
}
|
|
132
151
|
setToolsExpanded(): void {}
|
|
133
152
|
|
|
134
|
-
protected tell(request: DialogRequest):
|
|
135
|
-
|
|
153
|
+
protected tell(request: DialogRequest): RpcExtensionUIRequest {
|
|
154
|
+
const full = { type: 'extension_ui_request', id: randomUUID(), ...request } as RpcExtensionUIRequest;
|
|
155
|
+
this.publish(full);
|
|
156
|
+
return full;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
protected keep(key: string, request: RpcExtensionUIRequest | undefined): void {
|
|
160
|
+
if (request) {
|
|
161
|
+
this.shown.set(key, request);
|
|
162
|
+
} else {
|
|
163
|
+
this.shown.delete(key);
|
|
164
|
+
}
|
|
136
165
|
}
|
|
137
166
|
|
|
138
167
|
protected dialog<T>(
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { afterAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { homedir, tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { type ExecEnvironment, LocalExecEnvironment } from '@celljs/ta-exec';
|
|
6
|
+
import { ExecSessionFiles } from './session-files';
|
|
7
|
+
|
|
8
|
+
describe('ExecSessionFiles', () => {
|
|
9
|
+
const dir = mkdtempSync(join(tmpdir(), 'ta-session-files-'));
|
|
10
|
+
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
11
|
+
|
|
12
|
+
it("resolves paths as pi's tools do", () => {
|
|
13
|
+
const files = new ExecSessionFiles(new LocalExecEnvironment(dir), dir);
|
|
14
|
+
expect(files.resolve('src/a.ts')).toBe(join(dir, 'src', 'a.ts'));
|
|
15
|
+
expect(files.resolve('@src/a.ts')).toBe(join(dir, 'src', 'a.ts'));
|
|
16
|
+
expect(files.resolve('~/notes.md')).toBe(join(homedir(), 'notes.md'));
|
|
17
|
+
expect(files.resolve('.')).toBe(dir);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('reads, writes with the directories it needs, and removes', async () => {
|
|
21
|
+
const files = new ExecSessionFiles(new LocalExecEnvironment(dir), dir);
|
|
22
|
+
const path = files.resolve('deep/down/a.txt');
|
|
23
|
+
expect(await files.read(path)).toBeUndefined();
|
|
24
|
+
await files.write(path, new TextEncoder().encode('hi'));
|
|
25
|
+
expect(readFileSync(path, 'utf8')).toBe('hi');
|
|
26
|
+
expect(new TextDecoder().decode(await files.read(path))).toBe('hi');
|
|
27
|
+
mkdirSync(join(dir, 'folder'));
|
|
28
|
+
expect(await files.read(files.resolve('folder'))).toBeUndefined();
|
|
29
|
+
await files.remove(path);
|
|
30
|
+
expect(await files.read(path)).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('maps the directory pi works in for another machine into that machine', async () => {
|
|
34
|
+
const written: string[] = [];
|
|
35
|
+
const remote = {
|
|
36
|
+
cwd: '/home/me/app',
|
|
37
|
+
local: false,
|
|
38
|
+
mkdir: async (path: string) => {
|
|
39
|
+
written.push(`mkdir ${path}`);
|
|
40
|
+
},
|
|
41
|
+
writeFile: async (path: string) => {
|
|
42
|
+
written.push(`write ${path}`);
|
|
43
|
+
},
|
|
44
|
+
} as unknown as ExecEnvironment;
|
|
45
|
+
const piCwd = join(dir, 'remote', 'host-app');
|
|
46
|
+
const files = new ExecSessionFiles(remote, piCwd);
|
|
47
|
+
expect(files.resolve('src/a.ts')).toBe('/home/me/app/src/a.ts');
|
|
48
|
+
expect(files.resolve('.')).toBe('/home/me/app');
|
|
49
|
+
await files.write('/home/me/app/src/a.ts', new Uint8Array());
|
|
50
|
+
expect(written).toEqual(['mkdir /home/me/app/src', 'write /home/me/app/src/a.ts']);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { dirname, posix, resolve } from 'node:path';
|
|
3
|
+
import type { ExecEnvironment } from '@celljs/ta-exec';
|
|
4
|
+
import { ExecOperations } from '@celljs/ta-harness';
|
|
5
|
+
import type { TaSessionFiles } from '@celljs/ta-protocol';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The files of a session on its execution environment. `piCwd` is the directory pi works in for it
|
|
9
|
+
* (PiDirectories): pi's tools resolve paths there, and ExecOperations maps them into the environment.
|
|
10
|
+
*/
|
|
11
|
+
export class ExecSessionFiles implements TaSessionFiles {
|
|
12
|
+
protected readonly operations: ExecOperations;
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
protected readonly environment: ExecEnvironment,
|
|
16
|
+
protected readonly piCwd: string,
|
|
17
|
+
) {
|
|
18
|
+
this.operations = new ExecOperations(environment, piCwd);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** As pi's tools resolve a path of their arguments: without a leading `@`, `~` the home directory, against pi's directory. */
|
|
22
|
+
resolve(path: string): string {
|
|
23
|
+
const bare = path.startsWith('@') ? path.slice(1) : path;
|
|
24
|
+
const expanded = bare === '~' || bare.startsWith('~/') ? homedir() + bare.slice(1) : bare;
|
|
25
|
+
return this.operations.map(resolve(this.piCwd, expanded));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async read(path: string): Promise<Uint8Array | undefined> {
|
|
29
|
+
const stat = await this.environment.stat(path);
|
|
30
|
+
return stat && !stat.isDirectory ? this.environment.readFile(path) : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async write(path: string, data: Uint8Array): Promise<void> {
|
|
34
|
+
await this.environment.mkdir(this.environment.local ? dirname(path) : posix.dirname(path));
|
|
35
|
+
await this.environment.writeFile(path, data);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
remove(path: string): Promise<void> {
|
|
39
|
+
return this.environment.remove(path, { recursive: false });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -27,6 +27,15 @@ export class TaSettingsServerImpl implements TaSettingsServer {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
setLanguage(language: string): Promise<void> {
|
|
31
|
+
return TaErrors.guard(async () => {
|
|
32
|
+
if (typeof language !== 'string' || !/^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$/.test(language)) {
|
|
33
|
+
throw TaErrors.invalidArgument(`${String(language)} is not a language tag.`, 'Pass a tag such as zh-CN or en-US.');
|
|
34
|
+
}
|
|
35
|
+
process.env.TA_LANGUAGE = language;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
30
39
|
protected async cwd(cwd?: string): Promise<string> {
|
|
31
40
|
return cwd || (await this.environments.defaultCwd());
|
|
32
41
|
}
|