@celljs/ta-exec 4.8.0 → 4.9.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.
- package/package.json +4 -3
- package/esm/node/exec-client.js.map +0 -1
- package/esm/node/exec-peer.js.map +0 -1
- package/esm/node/exec-protocol.js.map +0 -1
- package/esm/node/exec-remote-source.js.map +0 -1
- package/esm/node/exec-rpc.js.map +0 -1
- package/esm/node/exec-server.js.map +0 -1
- package/esm/node/exec-stdio.js.map +0 -1
- package/esm/node/index.js.map +0 -1
- package/esm/node/local-exec-environment.js.map +0 -1
- package/esm/node/remote-exec-environment.js.map +0 -1
- package/esm/node/ssh/node-dist.js.map +0 -1
- package/esm/node/ssh/ssh-askpass.js.map +0 -1
- package/esm/node/ssh/ssh-config.js.map +0 -1
- package/esm/node/ssh/ssh-host.js.map +0 -1
- package/esm/node/ssh/ssh-util.js.map +0 -1
- package/lib/node/exec-client.d.ts.map +0 -1
- package/lib/node/exec-client.js.map +0 -1
- package/lib/node/exec-peer.d.ts.map +0 -1
- package/lib/node/exec-peer.js.map +0 -1
- package/lib/node/exec-protocol.d.ts.map +0 -1
- package/lib/node/exec-protocol.js.map +0 -1
- package/lib/node/exec-remote-source.d.ts.map +0 -1
- package/lib/node/exec-remote-source.js.map +0 -1
- package/lib/node/exec-rpc.d.ts.map +0 -1
- package/lib/node/exec-rpc.js.map +0 -1
- package/lib/node/exec-server.d.ts.map +0 -1
- package/lib/node/exec-server.js.map +0 -1
- package/lib/node/exec-server.spec.d.ts +0 -2
- package/lib/node/exec-server.spec.d.ts.map +0 -1
- package/lib/node/exec-server.spec.js +0 -104
- package/lib/node/exec-server.spec.js.map +0 -1
- package/lib/node/exec-stdio.d.ts.map +0 -1
- package/lib/node/exec-stdio.js.map +0 -1
- package/lib/node/exec-stdio.spec.d.ts +0 -2
- package/lib/node/exec-stdio.spec.d.ts.map +0 -1
- package/lib/node/exec-stdio.spec.js +0 -106
- package/lib/node/exec-stdio.spec.js.map +0 -1
- package/lib/node/index.d.ts.map +0 -1
- package/lib/node/index.js.map +0 -1
- package/lib/node/local-exec-environment.d.ts.map +0 -1
- package/lib/node/local-exec-environment.js.map +0 -1
- package/lib/node/local-exec-environment.spec.d.ts +0 -2
- package/lib/node/local-exec-environment.spec.d.ts.map +0 -1
- package/lib/node/local-exec-environment.spec.js +0 -57
- package/lib/node/local-exec-environment.spec.js.map +0 -1
- package/lib/node/remote-exec-environment.d.ts.map +0 -1
- package/lib/node/remote-exec-environment.js.map +0 -1
- package/lib/node/ssh/node-dist.d.ts.map +0 -1
- package/lib/node/ssh/node-dist.js.map +0 -1
- package/lib/node/ssh/ssh-askpass.d.ts.map +0 -1
- package/lib/node/ssh/ssh-askpass.js.map +0 -1
- package/lib/node/ssh/ssh-config.d.ts.map +0 -1
- package/lib/node/ssh/ssh-config.js.map +0 -1
- package/lib/node/ssh/ssh-host.d.ts.map +0 -1
- package/lib/node/ssh/ssh-host.js.map +0 -1
- package/lib/node/ssh/ssh-host.spec.d.ts +0 -2
- package/lib/node/ssh/ssh-host.spec.d.ts.map +0 -1
- package/lib/node/ssh/ssh-host.spec.js +0 -182
- package/lib/node/ssh/ssh-host.spec.js.map +0 -1
- package/lib/node/ssh/ssh-util.d.ts.map +0 -1
- package/lib/node/ssh/ssh-util.js.map +0 -1
- package/lib/node/ssh/ssh-util.spec.d.ts +0 -2
- package/lib/node/ssh/ssh-util.spec.d.ts.map +0 -1
- package/lib/node/ssh/ssh-util.spec.js +0 -87
- package/lib/node/ssh/ssh-util.spec.js.map +0 -1
- package/src/node/exec-client.ts +0 -221
- package/src/node/exec-peer.ts +0 -186
- package/src/node/exec-protocol.ts +0 -89
- package/src/node/exec-remote-source.ts +0 -7
- package/src/node/exec-rpc.ts +0 -124
- package/src/node/exec-server.spec.ts +0 -113
- package/src/node/exec-server.ts +0 -107
- package/src/node/exec-stdio.spec.ts +0 -108
- package/src/node/exec-stdio.ts +0 -75
- package/src/node/index.ts +0 -14
- package/src/node/local-exec-environment.spec.ts +0 -60
- package/src/node/local-exec-environment.ts +0 -112
- package/src/node/remote-exec-environment.ts +0 -162
- package/src/node/ssh/node-dist.ts +0 -83
- package/src/node/ssh/ssh-askpass.ts +0 -117
- package/src/node/ssh/ssh-config.ts +0 -128
- package/src/node/ssh/ssh-host.spec.ts +0 -189
- package/src/node/ssh/ssh-host.ts +0 -300
- package/src/node/ssh/ssh-util.spec.ts +0 -97
- package/src/node/ssh/ssh-util.ts +0 -127
package/src/node/ssh/ssh-host.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import { type ChildProcess, spawn } from 'node:child_process';
|
|
2
|
-
import { createReadStream, mkdirSync } from 'node:fs';
|
|
3
|
-
import { dirname, join } from 'node:path';
|
|
4
|
-
import type { Readable } from 'node:stream';
|
|
5
|
-
import { ExecClient, type ExecConnection, ExecError } from '../exec-client';
|
|
6
|
-
import { EXEC_REMOTE_SOURCE } from '../exec-remote-source';
|
|
7
|
-
import { ExecRpc } from '../exec-rpc';
|
|
8
|
-
import { NodeDist } from './node-dist';
|
|
9
|
-
import { SshAskpass } from './ssh-askpass';
|
|
10
|
-
import { type SshProbe, type SshQuestion, SshUtil } from './ssh-util';
|
|
11
|
-
|
|
12
|
-
/** The steps of connecting, as TaRemoteStep names them. */
|
|
13
|
-
export type SshStep = 'connecting' | 'checking' | 'installing-node' | 'copying-server' | 'starting';
|
|
14
|
-
|
|
15
|
-
export interface SshHostOptions {
|
|
16
|
-
/** What ssh takes: a Host of its configuration, or `[user@]host[:port]`. */
|
|
17
|
-
host: string;
|
|
18
|
-
/** Where this machine keeps the sockets of ssh, the askpass script and downloaded Node.js. */
|
|
19
|
-
stateDir: string;
|
|
20
|
-
/** Default `ssh`, the OpenSSH client of the system with its configuration. */
|
|
21
|
-
ssh?: string;
|
|
22
|
-
/** Before the host, as `-F <file>`. */
|
|
23
|
-
sshArgs?: string[];
|
|
24
|
-
/** What ssh asks (a password, a new host key); undefined cancels. Without it, ssh asks nobody. */
|
|
25
|
-
ask?(question: SshQuestion): Promise<string | undefined>;
|
|
26
|
-
onStep?(step: SshStep, detail?: string): void;
|
|
27
|
-
/** Where Node.js comes from for a host without one; default nodejs.org. */
|
|
28
|
-
nodeMirror?: string;
|
|
29
|
-
/** For tests: the server to copy. */
|
|
30
|
-
server?: { hash: string; script: string };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface RunResult {
|
|
34
|
-
code: number | null;
|
|
35
|
-
stdout: string;
|
|
36
|
-
stderr: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** The pings of the client; the server ends after four times this without any message. */
|
|
40
|
-
const PING_MS = 15000;
|
|
41
|
-
const CHECK_MS = 150;
|
|
42
|
-
/** A Unix socket's path has at most 104 bytes: `/%C` takes 41, and ssh adds 17 while it creates one. */
|
|
43
|
-
const MAX_SOCKET_DIR = 44;
|
|
44
|
-
const STDERR_KEPT = 2000;
|
|
45
|
-
const STOP_MS = 2000;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Another machine through the `ssh` of this one, as VS Code's Remote SSH reaches it: one ssh of its own
|
|
49
|
-
* signs in (and asks through {@link SshHostOptions.ask}), the others share its connection
|
|
50
|
-
* (ControlMaster, never the user's own). The first time, it copies a small `ta exec-server` there,
|
|
51
|
-
* with Node.js when the host has none, and then runs it on the standard input and output of ssh.
|
|
52
|
-
* Only processes it started are ended; what the server started there ends with its input.
|
|
53
|
-
*/
|
|
54
|
-
export class SshHost {
|
|
55
|
-
readonly client: ExecClient;
|
|
56
|
-
protected master?: ChildProcess;
|
|
57
|
-
protected ready?: Promise<void>;
|
|
58
|
-
protected askpass?: SshAskpass;
|
|
59
|
-
protected readonly running = new Set<ChildProcess>();
|
|
60
|
-
protected disposed = false;
|
|
61
|
-
|
|
62
|
-
constructor(protected readonly options: SshHostOptions) {
|
|
63
|
-
this.client = new ExecClient({ epoch: Date.now(), pingMs: PING_MS, connect: () => this.launch() });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get host(): string {
|
|
67
|
-
return this.options.host;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** Ends the server there, the connection, and every ssh this object started. */
|
|
71
|
-
async dispose(): Promise<void> {
|
|
72
|
-
this.disposed = true;
|
|
73
|
-
this.client.close();
|
|
74
|
-
for (const child of this.running) {
|
|
75
|
-
child.kill();
|
|
76
|
-
}
|
|
77
|
-
const master = this.master;
|
|
78
|
-
this.master = undefined;
|
|
79
|
-
this.ready = undefined;
|
|
80
|
-
if (master && master.exitCode === null && master.signalCode === null) {
|
|
81
|
-
const exited = new Promise(resolve => master.once('close', resolve));
|
|
82
|
-
master.kill();
|
|
83
|
-
const timer = setTimeout(() => master.kill('SIGKILL'), STOP_MS);
|
|
84
|
-
await exited;
|
|
85
|
-
clearTimeout(timer);
|
|
86
|
-
}
|
|
87
|
-
await this.askpass?.close();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
protected async launch(): Promise<ExecConnection> {
|
|
91
|
-
if (this.disposed) {
|
|
92
|
-
throw new ExecError(`The connection to ${this.host} was closed.`, ExecRpc.ErrorCode.CONNECTION_LOST);
|
|
93
|
-
}
|
|
94
|
-
this.step('connecting');
|
|
95
|
-
await this.connectMaster();
|
|
96
|
-
this.step('checking');
|
|
97
|
-
const server = this.options.server ?? EXEC_REMOTE_SOURCE;
|
|
98
|
-
const probe = await this.probe(server.hash);
|
|
99
|
-
const node = probe.node ?? (await this.installNode(probe));
|
|
100
|
-
const script = `${probe.home}/${SshUtil.REMOTE_DIR}/${server.hash}/exec-server.js`;
|
|
101
|
-
if (!probe.server) {
|
|
102
|
-
this.step('copying-server');
|
|
103
|
-
await this.check(this.run(SshUtil.uploadScript(`${server.hash}/exec-server.js`), [], server.script), 'copy the server of Ta');
|
|
104
|
-
}
|
|
105
|
-
this.step('starting');
|
|
106
|
-
return this.start(node, script);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
protected async probe(hash: string): Promise<SshProbe> {
|
|
110
|
-
const result = await this.check(this.run(SshUtil.probeScript(hash)), 'look at the host');
|
|
111
|
-
const probe = SshUtil.parseProbe(result.stdout);
|
|
112
|
-
if (!probe) {
|
|
113
|
-
throw this.error(`${this.host} has no POSIX shell: Ta works on Linux and macOS hosts.`);
|
|
114
|
-
}
|
|
115
|
-
return probe;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
protected async installNode(probe: SshProbe): Promise<string> {
|
|
119
|
-
const platform = SshUtil.nodePlatform(probe.os, probe.arch);
|
|
120
|
-
if (!platform || probe.musl) {
|
|
121
|
-
throw this.error(
|
|
122
|
-
`${this.host} (${probe.os} ${probe.arch}${probe.musl ? ', musl' : ''}) has no Node.js ${SshUtil.MIN_NODE_MAJOR} or later, and Node.js has no build Ta can install there: install one there.`,
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
this.step('installing-node');
|
|
126
|
-
const tarball = await NodeDist.download(platform, join(this.options.stateDir, 'node'), this.options.nodeMirror, version =>
|
|
127
|
-
this.step('installing-node', version),
|
|
128
|
-
);
|
|
129
|
-
await this.check(this.run(SshUtil.installNodeScript(), [], createReadStream(tarball.file)), 'install Node.js');
|
|
130
|
-
return `${probe.home}/${SshUtil.REMOTE_DIR}/node/bin/node`;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** The server on the standard input and output of an ssh: one message per line. */
|
|
134
|
-
protected async start(node: string, script: string): Promise<ExecConnection> {
|
|
135
|
-
const child = this.spawn([SshUtil.remoteCommand(SshUtil.launchScript(), node, script)], await this.env());
|
|
136
|
-
const messages = new Set<(text: string) => void>();
|
|
137
|
-
let closed: ((reason?: string) => void) | undefined;
|
|
138
|
-
let closing = false;
|
|
139
|
-
let rest = '';
|
|
140
|
-
let stderr = '';
|
|
141
|
-
child.stdout?.on('data', data => {
|
|
142
|
-
const lines = (rest + data.toString()).split('\n');
|
|
143
|
-
rest = lines.pop() ?? '';
|
|
144
|
-
for (const line of lines) {
|
|
145
|
-
for (const listener of messages) {
|
|
146
|
-
listener(line);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
child.stderr?.on('data', data => (stderr = (stderr + data.toString()).slice(-STDERR_KEPT)));
|
|
151
|
-
child.stdin?.on('error', () => undefined);
|
|
152
|
-
child.once('close', code => {
|
|
153
|
-
const reason = stderr.trim() || `ssh to ${this.host} ended with code ${code}.`;
|
|
154
|
-
closed?.(closing ? undefined : `The connection to ${this.host} was lost: ${reason}`);
|
|
155
|
-
});
|
|
156
|
-
return {
|
|
157
|
-
send: text => child.stdin?.write(`${text}\n`),
|
|
158
|
-
close: () => {
|
|
159
|
-
closing = true;
|
|
160
|
-
child.kill();
|
|
161
|
-
},
|
|
162
|
-
onMessage: listener => messages.add(listener),
|
|
163
|
-
onClose: listener => (closed = listener),
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** The ssh that signs in, and stays for the others to share; none on Windows, whose ssh cannot share. */
|
|
168
|
-
protected connectMaster(): Promise<void> {
|
|
169
|
-
if (process.platform === 'win32') {
|
|
170
|
-
return Promise.resolve();
|
|
171
|
-
}
|
|
172
|
-
this.ready ??= this.openMaster().catch(error => {
|
|
173
|
-
this.ready = undefined;
|
|
174
|
-
throw error;
|
|
175
|
-
});
|
|
176
|
-
return this.ready;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
protected async openMaster(): Promise<void> {
|
|
180
|
-
mkdirSync(dirname(this.controlPath()), { recursive: true, mode: 0o700 });
|
|
181
|
-
const env = await this.env();
|
|
182
|
-
const master = spawn(
|
|
183
|
-
this.options.ssh ?? 'ssh',
|
|
184
|
-
[...this.common(), '-N', '-o', 'ControlMaster=yes', '-o', 'ControlPersist=no', '-o', `ControlPath=${this.controlPath()}`, '--', this.destination()],
|
|
185
|
-
{ stdio: ['ignore', 'ignore', 'pipe'], env },
|
|
186
|
-
);
|
|
187
|
-
this.master = master;
|
|
188
|
-
let stderr = '';
|
|
189
|
-
master.stderr?.on('data', data => (stderr = (stderr + data.toString()).slice(-STDERR_KEPT)));
|
|
190
|
-
const exited = new Promise<number | null>(resolve => master.once('close', resolve));
|
|
191
|
-
void exited.then(() => {
|
|
192
|
-
if (this.master === master) {
|
|
193
|
-
this.master = undefined;
|
|
194
|
-
this.ready = undefined;
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
for (;;) {
|
|
198
|
-
const done = await Promise.race([exited, new Promise<'wait'>(resolve => setTimeout(() => resolve('wait'), CHECK_MS))]);
|
|
199
|
-
if (done !== 'wait') {
|
|
200
|
-
throw this.error(stderr.trim() || `ssh cannot connect to ${this.host}.`);
|
|
201
|
-
}
|
|
202
|
-
if ((await this.control('check')) === 0) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
protected control(command: string): Promise<number | null> {
|
|
209
|
-
const child = spawn(this.options.ssh ?? 'ssh', [...this.common(), '-O', command, '-o', `ControlPath=${this.controlPath()}`, '--', this.destination()], {
|
|
210
|
-
stdio: 'ignore',
|
|
211
|
-
});
|
|
212
|
-
return new Promise(resolve => {
|
|
213
|
-
child.once('error', () => resolve(null));
|
|
214
|
-
child.once('close', resolve);
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Runs a script there, with `input` on its standard input. */
|
|
219
|
-
protected async run(script: string, args: string[] = [], input?: string | Readable): Promise<RunResult> {
|
|
220
|
-
const child = this.spawn([SshUtil.remoteCommand(script, ...args)], await this.env());
|
|
221
|
-
this.running.add(child);
|
|
222
|
-
let stdout = '';
|
|
223
|
-
let stderr = '';
|
|
224
|
-
child.stdout?.on('data', data => (stdout += data.toString()));
|
|
225
|
-
child.stderr?.on('data', data => (stderr = (stderr + data.toString()).slice(-STDERR_KEPT)));
|
|
226
|
-
child.stdin?.on('error', () => undefined);
|
|
227
|
-
if (typeof input === 'string') {
|
|
228
|
-
child.stdin?.end(input);
|
|
229
|
-
} else if (input) {
|
|
230
|
-
input.pipe(child.stdin as NodeJS.WritableStream);
|
|
231
|
-
} else {
|
|
232
|
-
child.stdin?.end();
|
|
233
|
-
}
|
|
234
|
-
return new Promise(resolve => {
|
|
235
|
-
child.once('error', error => resolve({ code: null, stdout, stderr: error.message }));
|
|
236
|
-
child.once('close', code => {
|
|
237
|
-
this.running.delete(child);
|
|
238
|
-
resolve({ code, stdout, stderr });
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
protected async check(running: Promise<RunResult>, what: string): Promise<RunResult> {
|
|
244
|
-
const result = await running;
|
|
245
|
-
if (result.code !== 0) {
|
|
246
|
-
throw this.error(`Ta could not ${what} on ${this.host}: ${result.stderr.trim() || `ssh ended with code ${result.code}`}`);
|
|
247
|
-
}
|
|
248
|
-
return result;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
protected spawn(remote: string[], env: NodeJS.ProcessEnv): ChildProcess {
|
|
252
|
-
const shared = process.platform === 'win32' ? [] : ['-o', 'ControlMaster=no', '-o', `ControlPath=${this.controlPath()}`];
|
|
253
|
-
return spawn(this.options.ssh ?? 'ssh', [...this.common(), ...shared, '-T', '--', this.destination(), ...remote], {
|
|
254
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
255
|
-
env,
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
protected common(): string[] {
|
|
260
|
-
return [
|
|
261
|
-
...(this.options.sshArgs ?? []),
|
|
262
|
-
'-o',
|
|
263
|
-
'ServerAliveInterval=15',
|
|
264
|
-
'-o',
|
|
265
|
-
'ServerAliveCountMax=3',
|
|
266
|
-
'-o',
|
|
267
|
-
'ConnectTimeout=20',
|
|
268
|
-
'-o',
|
|
269
|
-
'LogLevel=ERROR',
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
protected destination(): string {
|
|
274
|
-
return SshUtil.destination(this.host);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/** In the state directory, or a short one of this user when that path is too long for a socket. */
|
|
278
|
-
protected controlPath(): string {
|
|
279
|
-
const dir = join(this.options.stateDir, 'ssh');
|
|
280
|
-
const short = dir.length <= MAX_SOCKET_DIR ? dir : `/tmp/ta-ssh-${process.getuid?.() ?? 'user'}`;
|
|
281
|
-
return join(short, '%C');
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
protected async env(): Promise<NodeJS.ProcessEnv> {
|
|
285
|
-
const ask = this.options.ask;
|
|
286
|
-
if (!ask) {
|
|
287
|
-
return process.env;
|
|
288
|
-
}
|
|
289
|
-
this.askpass ??= new SshAskpass(dirname(this.controlPath()), ask);
|
|
290
|
-
return this.askpass.env();
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
protected step(step: SshStep, detail?: string): void {
|
|
294
|
-
this.options.onStep?.(step, detail);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
protected error(message: string): ExecError {
|
|
298
|
-
return new ExecError(message, ExecRpc.ErrorCode.CONNECTION_LOST);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { execFileSync } from 'node:child_process';
|
|
3
|
-
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { tmpdir } from 'node:os';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
|
-
import { NodeDist } from './node-dist';
|
|
7
|
-
import { SshConfig } from './ssh-config';
|
|
8
|
-
import { SshUtil } from './ssh-util';
|
|
9
|
-
|
|
10
|
-
describe('SshUtil', () => {
|
|
11
|
-
it('quotes an argument for the shell of the host', () => {
|
|
12
|
-
const value = `it's "$HOME" & more`;
|
|
13
|
-
expect(execFileSync('/bin/sh', ['-c', `printf %s ${SshUtil.quote(value)}`]).toString()).toBe(value);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('runs a script with its arguments in sh, whatever the remote shell', () => {
|
|
17
|
-
const command = SshUtil.remoteCommand('printf "%s|%s" "$1" "$2"', 'a b', `c'd`);
|
|
18
|
-
expect(execFileSync('/bin/sh', ['-c', command]).toString()).toBe(`a b|c'd`);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('gives ssh a URL for a host with a port', () => {
|
|
22
|
-
expect(SshUtil.destination('dev')).toBe('dev');
|
|
23
|
-
expect(SshUtil.destination('me@dev.example.com')).toBe('me@dev.example.com');
|
|
24
|
-
expect(SshUtil.destination('me@10.0.0.2:2222')).toBe('ssh://me@10.0.0.2:2222');
|
|
25
|
-
expect(SshUtil.destination('[::1]:22')).toBe('ssh://[::1]:22');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('tells what kind of answer ssh asks for', () => {
|
|
29
|
-
expect(SshUtil.question("me@dev's password: ").kind).toBe('secret');
|
|
30
|
-
expect(SshUtil.question("Enter passphrase for key '/home/me/.ssh/id_ed25519': ").kind).toBe('secret');
|
|
31
|
-
expect(SshUtil.question('Are you sure you want to continue connecting (yes/no/[fingerprint])? ').kind).toBe('confirm');
|
|
32
|
-
expect(SshUtil.question('Verification code: ').kind).toBe('secret');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('reads what the probe prints, past what else a shell printed', () => {
|
|
36
|
-
const home = mkdtempSync(join(tmpdir(), 'ta-probe-'));
|
|
37
|
-
const output = execFileSync('/bin/sh', ['-c', SshUtil.probeScript('0123456789abcdef')], { env: { ...process.env, HOME: home } }).toString();
|
|
38
|
-
const probe = SshUtil.parseProbe(`Last login: today\n${output}`);
|
|
39
|
-
expect(probe).toMatchObject({ home, server: false, musl: false });
|
|
40
|
-
expect(probe?.os).toBe(execFileSync('uname', ['-s']).toString().trim());
|
|
41
|
-
expect(SshUtil.parseProbe('bash: sh: command not found')).toBeUndefined();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('names the build of Node.js of a host', () => {
|
|
45
|
-
expect(SshUtil.nodePlatform('Linux', 'x86_64')).toBe('linux-x64');
|
|
46
|
-
expect(SshUtil.nodePlatform('Linux', 'aarch64')).toBe('linux-arm64');
|
|
47
|
-
expect(SshUtil.nodePlatform('Darwin', 'arm64')).toBe('darwin-arm64');
|
|
48
|
-
expect(SshUtil.nodePlatform('FreeBSD', 'amd64')).toBeUndefined();
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe('NodeDist', () => {
|
|
53
|
-
it('finds the tarball of a platform in the sums', () => {
|
|
54
|
-
const sums = [
|
|
55
|
-
`${'a'.repeat(64)} node-v22.9.0-darwin-arm64.tar.gz`,
|
|
56
|
-
`${'b'.repeat(64)} node-v22.9.0-linux-arm64.tar.xz`,
|
|
57
|
-
`${'c'.repeat(64)} node-v22.9.0-linux-arm64.tar.gz`,
|
|
58
|
-
].join('\n');
|
|
59
|
-
expect(NodeDist.find(sums, 'linux-arm64')).toEqual({ sha256: 'c'.repeat(64), name: 'node-v22.9.0-linux-arm64.tar.gz', version: 'v22.9.0' });
|
|
60
|
-
expect(NodeDist.find(sums, 'linux-x64')).toBeUndefined();
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe('SshConfig', () => {
|
|
65
|
-
it('reads Host, Match and Include lines', () => {
|
|
66
|
-
expect(SshConfig.parse('Host dev prod *.example.com !bad\n HostName 10.0.0.1\nhost="quoted"\nMatch all\nInclude conf.d/*')).toEqual([
|
|
67
|
-
{ host: ['dev', 'prod', '*.example.com', '!bad'] },
|
|
68
|
-
{ host: ['quoted'] },
|
|
69
|
-
{ match: true },
|
|
70
|
-
{ include: 'conf.d/*' },
|
|
71
|
-
]);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('matches hosts as ssh does', () => {
|
|
75
|
-
expect(SshConfig.matches(['*.example.com'], 'a.example.com')).toBe(true);
|
|
76
|
-
expect(SshConfig.matches(['*.example.com'], 'colima')).toBe(false);
|
|
77
|
-
expect(SshConfig.matches(['*', '!bad'], 'bad')).toBe(false);
|
|
78
|
-
expect(SshConfig.matches(['dev?'], 'dev1')).toBe(true);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('lists the hosts without wildcards, with those of files included where ssh reads them', () => {
|
|
82
|
-
const dir = mkdtempSync(join(tmpdir(), 'ta-ssh-config-'));
|
|
83
|
-
mkdirSync(join(dir, 'conf.d'));
|
|
84
|
-
writeFileSync(
|
|
85
|
-
join(dir, 'config'),
|
|
86
|
-
'Include top.conf\nHost a\nInclude conf.d/*.conf\nHost b *.corp\n\nInclude corp.conf\nMatch all\nInclude never.conf\n',
|
|
87
|
-
);
|
|
88
|
-
writeFileSync(join(dir, 'top.conf'), 'Host t\n');
|
|
89
|
-
writeFileSync(join(dir, 'conf.d', 'x.conf'), 'Host c a\n');
|
|
90
|
-
writeFileSync(join(dir, 'conf.d', 'y.txt'), 'Host d\n');
|
|
91
|
-
// Read only for b and the hosts of *.corp: its other hosts are not reachable by their names.
|
|
92
|
-
writeFileSync(join(dir, 'corp.conf'), 'Host colima x.corp\n');
|
|
93
|
-
writeFileSync(join(dir, 'never.conf'), 'Host n\n');
|
|
94
|
-
expect(SshConfig.hosts(join(dir, 'config'))).toEqual(['t', 'a', 'b', 'x.corp']);
|
|
95
|
-
expect(SshConfig.hosts(join(dir, 'missing'))).toEqual([]);
|
|
96
|
-
});
|
|
97
|
-
});
|
package/src/node/ssh/ssh-util.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/** What ssh asks while it connects, for a person to answer. */
|
|
2
|
-
export interface SshQuestion {
|
|
3
|
-
/** `confirm`: answered `yes` or `no` (a new host key); `secret`: not shown as typed. */
|
|
4
|
-
kind: 'secret' | 'confirm' | 'text';
|
|
5
|
-
message: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/** What a host has, as the probe script prints it. */
|
|
9
|
-
export interface SshProbe {
|
|
10
|
-
home: string;
|
|
11
|
-
os: string;
|
|
12
|
-
arch: string;
|
|
13
|
-
/** A Node.js of version 18 or later; none when it has none. */
|
|
14
|
-
node?: string;
|
|
15
|
-
musl: boolean;
|
|
16
|
-
/** The server of this version is there already. */
|
|
17
|
-
server: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** The pure parts of SshHost: arguments, scripts, and what comes back. */
|
|
21
|
-
export namespace SshUtil {
|
|
22
|
-
/** Where Ta keeps its server and Node.js on a host, under its home directory. */
|
|
23
|
-
export const REMOTE_DIR = '.ta/server';
|
|
24
|
-
export const MIN_NODE_MAJOR = 18;
|
|
25
|
-
|
|
26
|
-
/** One argument for a POSIX shell (and for fish, whose single quotes read `\'` the same way here). */
|
|
27
|
-
export function quote(value: string): string {
|
|
28
|
-
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** What ssh takes for the host of a location: `ssh://user@host:port` when it names a port. */
|
|
32
|
-
export function destination(host: string): string {
|
|
33
|
-
return /^(?:[^@/]+@)?(?:\[[^\]]+\]|[^:@/[\]]+):\d+$/.test(host) ? `ssh://${host}` : host;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** The command line ssh runs on the host: `script` in a POSIX shell, whatever the user's shell is. */
|
|
37
|
-
export function remoteCommand(script: string, ...args: string[]): string {
|
|
38
|
-
return ['sh', '-c', quote(script), 'sh', ...args.map(quote)].join(' ');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function question(prompt: string): SshQuestion {
|
|
42
|
-
const message = prompt.trim();
|
|
43
|
-
if (/\(yes\/no/i.test(message)) {
|
|
44
|
-
return { kind: 'confirm', message };
|
|
45
|
-
}
|
|
46
|
-
return { kind: /password|passphrase|\bpin\b|verification code|one-time/i.test(message) ? 'secret' : 'text', message };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Looks at the host: its system, a Node.js to use, and whether the server of `hash` is there. */
|
|
50
|
-
export function probeScript(hash: string): string {
|
|
51
|
-
return [
|
|
52
|
-
`h="$HOME"; d="$h/${REMOTE_DIR}"`,
|
|
53
|
-
'echo "ta-home=$h"; echo "ta-os=$(uname -s)"; echo "ta-arch=$(uname -m)"',
|
|
54
|
-
'case "$(ldd --version 2>&1)" in *musl*) echo "ta-libc=musl";; esac',
|
|
55
|
-
'ok() { [ -n "$1" ] && [ -x "$1" ] && v=$("$1" -e \'process.stdout.write(process.versions.node)\' 2>/dev/null) && [ "${v%%.*}" -ge ' +
|
|
56
|
-
MIN_NODE_MAJOR +
|
|
57
|
-
' ] 2>/dev/null; }',
|
|
58
|
-
'for n in "$d/node/bin/node" "$(command -v node 2>/dev/null)" "$("${SHELL:-/bin/sh}" -lc \'command -v node\' </dev/null 2>/dev/null | tail -n 1)" /usr/local/bin/node /opt/homebrew/bin/node "$h/.volta/bin/node" "$(ls -d "$h"/.nvm/versions/node/*/bin/node 2>/dev/null | tail -n 1)"; do',
|
|
59
|
-
' if ok "$n"; then echo "ta-node=$n"; break; fi',
|
|
60
|
-
'done',
|
|
61
|
-
`[ -f "$d/${hash}/exec-server.js" ] && echo "ta-server=1"`,
|
|
62
|
-
'exit 0',
|
|
63
|
-
].join('\n');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** undefined when the output is not the probe's: the host has no POSIX shell. */
|
|
67
|
-
export function parseProbe(output: string): SshProbe | undefined {
|
|
68
|
-
const values = new Map<string, string>();
|
|
69
|
-
for (const line of output.split(/\r?\n/)) {
|
|
70
|
-
const match = /^ta-(\w+)=(.*)$/.exec(line.trim());
|
|
71
|
-
if (match) {
|
|
72
|
-
values.set(match[1], match[2]);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const home = values.get('home');
|
|
76
|
-
const os = values.get('os');
|
|
77
|
-
if (!home || !os) {
|
|
78
|
-
return undefined;
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
home,
|
|
82
|
-
os,
|
|
83
|
-
arch: values.get('arch') ?? '',
|
|
84
|
-
node: values.get('node') || undefined,
|
|
85
|
-
musl: values.get('libc') === 'musl',
|
|
86
|
-
server: values.get('server') === '1',
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Writes the standard input to `file` under the remote directory, whole or not at all. */
|
|
91
|
-
export function uploadScript(file: string): string {
|
|
92
|
-
return `set -e; f="$HOME/${REMOTE_DIR}/${file}"; mkdir -p "$(dirname "$f")"; cat > "$f.tmp"; mv "$f.tmp" "$f"`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Unpacks the .tar.gz of Node.js on the standard input as node/ of the remote directory. */
|
|
96
|
-
export function installNodeScript(): string {
|
|
97
|
-
return `set -e; d="$HOME/${REMOTE_DIR}/node"; rm -rf "$d.tmp"; mkdir -p "$d.tmp"; tar -xzf - -C "$d.tmp" --strip-components=1; rm -rf "$d"; mv "$d.tmp" "$d"`;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Starts the server (`$2`, a script) with Node.js (`$1`) in the home directory, in a login shell of the
|
|
102
|
-
* user when it is a POSIX one, so that commands find what the user's profile puts on PATH.
|
|
103
|
-
*/
|
|
104
|
-
export function launchScript(): string {
|
|
105
|
-
return 'cd "$HOME" || exit 1; s="${SHELL:-/bin/sh}"; case "${s##*/}" in bash|zsh|sh|dash|ksh) exec "$s" -lc \'exec "$0" "$1"\' "$1" "$2";; esac; exec "$1" "$2"';
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** The build of Node.js for a host, as nodejs.org names them; undefined when there is none. */
|
|
109
|
-
export function nodePlatform(os: string, arch: string): string | undefined {
|
|
110
|
-
const systems: Record<string, string> = { Linux: 'linux', Darwin: 'darwin' };
|
|
111
|
-
const cpus: Record<string, string> = {
|
|
112
|
-
x86_64: 'x64',
|
|
113
|
-
amd64: 'x64',
|
|
114
|
-
aarch64: 'arm64',
|
|
115
|
-
arm64: 'arm64',
|
|
116
|
-
armv7l: 'armv7l',
|
|
117
|
-
ppc64le: 'ppc64le',
|
|
118
|
-
s390x: 's390x',
|
|
119
|
-
};
|
|
120
|
-
const system = systems[os];
|
|
121
|
-
const cpu = cpus[arch];
|
|
122
|
-
if (!system || !cpu || (system === 'darwin' && cpu !== 'x64' && cpu !== 'arm64')) {
|
|
123
|
-
return undefined;
|
|
124
|
-
}
|
|
125
|
-
return `${system}-${cpu}`;
|
|
126
|
-
}
|
|
127
|
-
}
|