@celljs/ta-exec 4.6.0 → 4.7.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 +43 -0
- package/README.md +5 -1
- package/esm/node/exec-client.d.ts +28 -11
- package/esm/node/exec-client.js +89 -33
- package/esm/node/exec-client.js.map +1 -1
- package/esm/node/exec-peer.d.ts +40 -0
- package/esm/node/exec-peer.js +167 -0
- package/esm/node/exec-peer.js.map +1 -0
- package/esm/node/exec-protocol.d.ts +14 -0
- package/esm/node/exec-remote-source.d.ts +5 -0
- package/esm/node/exec-remote-source.js +9 -0
- package/esm/node/exec-remote-source.js.map +1 -0
- package/esm/node/exec-rpc.d.ts +21 -3
- package/esm/node/exec-rpc.js +14 -3
- package/esm/node/exec-rpc.js.map +1 -1
- package/esm/node/exec-server.d.ts +2 -26
- package/esm/node/exec-server.js +3 -132
- package/esm/node/exec-server.js.map +1 -1
- package/esm/node/exec-stdio.d.ts +27 -0
- package/esm/node/exec-stdio.js +60 -0
- package/esm/node/exec-stdio.js.map +1 -0
- package/esm/node/index.d.ts +8 -0
- package/esm/node/index.js +8 -0
- package/esm/node/index.js.map +1 -1
- package/esm/node/local-exec-environment.js +8 -8
- package/esm/node/local-exec-environment.js.map +1 -1
- package/esm/node/remote-exec-environment.d.ts +2 -1
- package/esm/node/remote-exec-environment.js +39 -0
- package/esm/node/remote-exec-environment.js.map +1 -1
- package/esm/node/ssh/node-dist.d.ts +20 -0
- package/esm/node/ssh/node-dist.js +77 -0
- package/esm/node/ssh/node-dist.js.map +1 -0
- package/esm/node/ssh/ssh-askpass.d.ts +24 -0
- package/esm/node/ssh/ssh-askpass.js +112 -0
- package/esm/node/ssh/ssh-askpass.js.map +1 -0
- package/esm/node/ssh/ssh-config.d.ts +20 -0
- package/esm/node/ssh/ssh-config.js +127 -0
- package/esm/node/ssh/ssh-config.js.map +1 -0
- package/esm/node/ssh/ssh-host.d.ts +73 -0
- package/esm/node/ssh/ssh-host.js +258 -0
- package/esm/node/ssh/ssh-host.js.map +1 -0
- package/esm/node/ssh/ssh-util.d.ts +45 -0
- package/esm/node/ssh/ssh-util.js +110 -0
- package/esm/node/ssh/ssh-util.js.map +1 -0
- package/lib/node/exec-client.d.ts +28 -11
- package/lib/node/exec-client.d.ts.map +1 -1
- package/lib/node/exec-client.js +89 -33
- package/lib/node/exec-client.js.map +1 -1
- package/lib/node/exec-peer.d.ts +41 -0
- package/lib/node/exec-peer.d.ts.map +1 -0
- package/lib/node/exec-peer.js +171 -0
- package/lib/node/exec-peer.js.map +1 -0
- package/lib/node/exec-protocol.d.ts +14 -0
- package/lib/node/exec-protocol.d.ts.map +1 -1
- package/lib/node/exec-remote-source.d.ts +6 -0
- package/lib/node/exec-remote-source.d.ts.map +1 -0
- package/lib/node/exec-remote-source.js +10 -0
- package/lib/node/exec-remote-source.js.map +1 -0
- package/lib/node/exec-rpc.d.ts +21 -3
- package/lib/node/exec-rpc.d.ts.map +1 -1
- package/lib/node/exec-rpc.js +14 -3
- package/lib/node/exec-rpc.js.map +1 -1
- package/lib/node/exec-server.d.ts +2 -26
- package/lib/node/exec-server.d.ts.map +1 -1
- package/lib/node/exec-server.js +4 -134
- package/lib/node/exec-server.js.map +1 -1
- package/lib/node/exec-server.spec.js +1 -1
- package/lib/node/exec-server.spec.js.map +1 -1
- package/lib/node/exec-stdio.d.ts +28 -0
- package/lib/node/exec-stdio.d.ts.map +1 -0
- package/lib/node/exec-stdio.js +64 -0
- package/lib/node/exec-stdio.js.map +1 -0
- package/lib/node/exec-stdio.spec.d.ts +2 -0
- package/lib/node/exec-stdio.spec.d.ts.map +1 -0
- package/lib/node/exec-stdio.spec.js +105 -0
- package/lib/node/exec-stdio.spec.js.map +1 -0
- package/lib/node/index.d.ts +8 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +8 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/local-exec-environment.d.ts.map +1 -1
- package/lib/node/local-exec-environment.js +8 -8
- package/lib/node/local-exec-environment.js.map +1 -1
- package/lib/node/remote-exec-environment.d.ts +2 -1
- package/lib/node/remote-exec-environment.d.ts.map +1 -1
- package/lib/node/remote-exec-environment.js +39 -0
- package/lib/node/remote-exec-environment.js.map +1 -1
- package/lib/node/ssh/node-dist.d.ts +21 -0
- package/lib/node/ssh/node-dist.d.ts.map +1 -0
- package/lib/node/ssh/node-dist.js +80 -0
- package/lib/node/ssh/node-dist.js.map +1 -0
- package/lib/node/ssh/ssh-askpass.d.ts +25 -0
- package/lib/node/ssh/ssh-askpass.d.ts.map +1 -0
- package/lib/node/ssh/ssh-askpass.js +114 -0
- package/lib/node/ssh/ssh-askpass.js.map +1 -0
- package/lib/node/ssh/ssh-config.d.ts +21 -0
- package/lib/node/ssh/ssh-config.d.ts.map +1 -0
- package/lib/node/ssh/ssh-config.js +130 -0
- package/lib/node/ssh/ssh-config.js.map +1 -0
- package/lib/node/ssh/ssh-host.d.ts +74 -0
- package/lib/node/ssh/ssh-host.d.ts.map +1 -0
- package/lib/node/ssh/ssh-host.js +262 -0
- package/lib/node/ssh/ssh-host.js.map +1 -0
- package/lib/node/ssh/ssh-host.spec.d.ts +2 -0
- package/lib/node/ssh/ssh-host.spec.d.ts.map +1 -0
- package/lib/node/ssh/ssh-host.spec.js +182 -0
- package/lib/node/ssh/ssh-host.spec.js.map +1 -0
- package/lib/node/ssh/ssh-util.d.ts +46 -0
- package/lib/node/ssh/ssh-util.d.ts.map +1 -0
- package/lib/node/ssh/ssh-util.js +113 -0
- package/lib/node/ssh/ssh-util.js.map +1 -0
- package/lib/node/ssh/ssh-util.spec.d.ts +2 -0
- package/lib/node/ssh/ssh-util.spec.d.ts.map +1 -0
- package/lib/node/ssh/ssh-util.spec.js +87 -0
- package/lib/node/ssh/ssh-util.spec.js.map +1 -0
- package/package.json +3 -3
- package/src/node/exec-client.ts +108 -39
- package/src/node/exec-peer.ts +186 -0
- package/src/node/exec-protocol.ts +15 -0
- package/src/node/exec-remote-source.ts +7 -0
- package/src/node/exec-rpc.ts +23 -3
- package/src/node/exec-server.spec.ts +1 -1
- package/src/node/exec-server.ts +4 -140
- package/src/node/exec-stdio.spec.ts +107 -0
- package/src/node/exec-stdio.ts +75 -0
- package/src/node/index.ts +8 -0
- package/src/node/local-exec-environment.ts +7 -6
- package/src/node/remote-exec-environment.ts +39 -1
- package/src/node/ssh/node-dist.ts +83 -0
- package/src/node/ssh/ssh-askpass.ts +117 -0
- package/src/node/ssh/ssh-config.ts +128 -0
- package/src/node/ssh/ssh-host.spec.ts +189 -0
- package/src/node/ssh/ssh-host.ts +300 -0
- package/src/node/ssh/ssh-util.spec.ts +97 -0
- package/src/node/ssh/ssh-util.ts +127 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SshUtil = void 0;
|
|
4
|
+
/** The pure parts of SshHost: arguments, scripts, and what comes back. */
|
|
5
|
+
var SshUtil;
|
|
6
|
+
(function (SshUtil) {
|
|
7
|
+
/** Where Ta keeps its server and Node.js on a host, under its home directory. */
|
|
8
|
+
SshUtil.REMOTE_DIR = '.ta/server';
|
|
9
|
+
SshUtil.MIN_NODE_MAJOR = 18;
|
|
10
|
+
/** One argument for a POSIX shell (and for fish, whose single quotes read `\'` the same way here). */
|
|
11
|
+
function quote(value) {
|
|
12
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
13
|
+
}
|
|
14
|
+
SshUtil.quote = quote;
|
|
15
|
+
/** What ssh takes for the host of a location: `ssh://user@host:port` when it names a port. */
|
|
16
|
+
function destination(host) {
|
|
17
|
+
return /^(?:[^@/]+@)?(?:\[[^\]]+\]|[^:@/[\]]+):\d+$/.test(host) ? `ssh://${host}` : host;
|
|
18
|
+
}
|
|
19
|
+
SshUtil.destination = destination;
|
|
20
|
+
/** The command line ssh runs on the host: `script` in a POSIX shell, whatever the user's shell is. */
|
|
21
|
+
function remoteCommand(script, ...args) {
|
|
22
|
+
return ['sh', '-c', quote(script), 'sh', ...args.map(quote)].join(' ');
|
|
23
|
+
}
|
|
24
|
+
SshUtil.remoteCommand = remoteCommand;
|
|
25
|
+
function question(prompt) {
|
|
26
|
+
const message = prompt.trim();
|
|
27
|
+
if (/\(yes\/no/i.test(message)) {
|
|
28
|
+
return { kind: 'confirm', message };
|
|
29
|
+
}
|
|
30
|
+
return { kind: /password|passphrase|\bpin\b|verification code|one-time/i.test(message) ? 'secret' : 'text', message };
|
|
31
|
+
}
|
|
32
|
+
SshUtil.question = question;
|
|
33
|
+
/** Looks at the host: its system, a Node.js to use, and whether the server of `hash` is there. */
|
|
34
|
+
function probeScript(hash) {
|
|
35
|
+
return [
|
|
36
|
+
`h="$HOME"; d="$h/${SshUtil.REMOTE_DIR}"`,
|
|
37
|
+
'echo "ta-home=$h"; echo "ta-os=$(uname -s)"; echo "ta-arch=$(uname -m)"',
|
|
38
|
+
'case "$(ldd --version 2>&1)" in *musl*) echo "ta-libc=musl";; esac',
|
|
39
|
+
'ok() { [ -n "$1" ] && [ -x "$1" ] && v=$("$1" -e \'process.stdout.write(process.versions.node)\' 2>/dev/null) && [ "${v%%.*}" -ge ' + SshUtil.MIN_NODE_MAJOR +
|
|
40
|
+
' ] 2>/dev/null; }',
|
|
41
|
+
'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',
|
|
42
|
+
' if ok "$n"; then echo "ta-node=$n"; break; fi',
|
|
43
|
+
'done',
|
|
44
|
+
`[ -f "$d/${hash}/exec-server.js" ] && echo "ta-server=1"`,
|
|
45
|
+
'exit 0',
|
|
46
|
+
].join('\n');
|
|
47
|
+
}
|
|
48
|
+
SshUtil.probeScript = probeScript;
|
|
49
|
+
/** undefined when the output is not the probe's: the host has no POSIX shell. */
|
|
50
|
+
function parseProbe(output) {
|
|
51
|
+
var _a;
|
|
52
|
+
const values = new Map();
|
|
53
|
+
for (const line of output.split(/\r?\n/)) {
|
|
54
|
+
const match = /^ta-(\w+)=(.*)$/.exec(line.trim());
|
|
55
|
+
if (match) {
|
|
56
|
+
values.set(match[1], match[2]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const home = values.get('home');
|
|
60
|
+
const os = values.get('os');
|
|
61
|
+
if (!home || !os) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
home,
|
|
66
|
+
os,
|
|
67
|
+
arch: (_a = values.get('arch')) !== null && _a !== void 0 ? _a : '',
|
|
68
|
+
node: values.get('node') || undefined,
|
|
69
|
+
musl: values.get('libc') === 'musl',
|
|
70
|
+
server: values.get('server') === '1',
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
SshUtil.parseProbe = parseProbe;
|
|
74
|
+
/** Writes the standard input to `file` under the remote directory, whole or not at all. */
|
|
75
|
+
function uploadScript(file) {
|
|
76
|
+
return `set -e; f="$HOME/${SshUtil.REMOTE_DIR}/${file}"; mkdir -p "$(dirname "$f")"; cat > "$f.tmp"; mv "$f.tmp" "$f"`;
|
|
77
|
+
}
|
|
78
|
+
SshUtil.uploadScript = uploadScript;
|
|
79
|
+
/** Unpacks the .tar.gz of Node.js on the standard input as node/ of the remote directory. */
|
|
80
|
+
function installNodeScript() {
|
|
81
|
+
return `set -e; d="$HOME/${SshUtil.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"`;
|
|
82
|
+
}
|
|
83
|
+
SshUtil.installNodeScript = installNodeScript;
|
|
84
|
+
/**
|
|
85
|
+
* Starts the server (`$2`, a script) with Node.js (`$1`) in the home directory, in a login shell of the
|
|
86
|
+
* user when it is a POSIX one, so that commands find what the user's profile puts on PATH.
|
|
87
|
+
*/
|
|
88
|
+
function launchScript() {
|
|
89
|
+
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"';
|
|
90
|
+
}
|
|
91
|
+
SshUtil.launchScript = launchScript;
|
|
92
|
+
/** The build of Node.js for a host, as nodejs.org names them; undefined when there is none. */
|
|
93
|
+
function nodePlatform(os, arch) {
|
|
94
|
+
const systems = { Linux: 'linux', Darwin: 'darwin' };
|
|
95
|
+
const cpus = {
|
|
96
|
+
x86_64: 'x64',
|
|
97
|
+
amd64: 'x64',
|
|
98
|
+
aarch64: 'arm64',
|
|
99
|
+
arm64: 'arm64',
|
|
100
|
+
armv7l: 'armv7l',
|
|
101
|
+
ppc64le: 'ppc64le',
|
|
102
|
+
s390x: 's390x',
|
|
103
|
+
};
|
|
104
|
+
const system = systems[os];
|
|
105
|
+
const cpu = cpus[arch];
|
|
106
|
+
if (!system || !cpu || (system === 'darwin' && cpu !== 'x64' && cpu !== 'arm64')) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
return `${system}-${cpu}`;
|
|
110
|
+
}
|
|
111
|
+
SshUtil.nodePlatform = nodePlatform;
|
|
112
|
+
})(SshUtil || (exports.SshUtil = SshUtil = {}));
|
|
113
|
+
//# sourceMappingURL=ssh-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssh-util.js","sourceRoot":"","sources":["../../../src/node/ssh/ssh-util.ts"],"names":[],"mappings":";;;AAmBA,0EAA0E;AAC1E,IAAiB,OAAO,CA0GvB;AA1GD,WAAiB,OAAO;IACpB,iFAAiF;IACpE,kBAAU,GAAG,YAAY,CAAC;IAC1B,sBAAc,GAAG,EAAE,CAAC;IAEjC,sGAAsG;IACtG,SAAgB,KAAK,CAAC,KAAa;QAC/B,OAAO,IA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7F,SAAgB,iBAAiB;QAC7B,OAAO,oBAAoB,kBAAU,wHAAwH,CAAC;IAClK,CAAC;IAFe,QAAA,iBAAiB,oBAEhC,CAAA;IAED;;;OAGG;IACH,SAAgB,YAAY;QACxB,OAAO,yJAAyJ,CAAC;IACrK,CAAC;IAFe,QAAA,YAAY,eAE3B,CAAA;IAED,+FAA+F;IAC/F,SAAgB,YAAY,CAAC,EAAU,EAAE,IAAY;QACjD,MAAM,OAAO,GAA2B,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QAC7E,MAAM,IAAI,GAA2B;YACjC,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,KAAK;YACZ,OAAO,EAAE,OAAO;YAChB,KAAK,EAAE,OAAO;YACd,MAAM,EAAE,QAAQ;YAChB,OAAO,EAAE,SAAS;YAClB,KAAK,EAAE,OAAO;SACjB,CAAC;QACF,MAAM,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;QACvB,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,GAAG,KAAK,KAAK,IAAI,GAAG,KAAK,OAAO,CAAC,EAAE,CAAC;YAC/E,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,OAAO,GAAG,MAAM,IAAI,GAAG,EAAE,CAAC;IAC9B,CAAC;IAjBe,QAAA,YAAY,eAiB3B,CAAA;AACL,CAAC,EA1GgB,OAAO,aAAP,OAAO,GAAP,OAAO,QA0GvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssh-util.spec.d.ts","sourceRoot":"","sources":["../../../src/node/ssh/ssh-util.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_os_1 = require("node:os");
|
|
7
|
+
const node_path_1 = require("node:path");
|
|
8
|
+
const node_dist_1 = require("./node-dist");
|
|
9
|
+
const ssh_config_1 = require("./ssh-config");
|
|
10
|
+
const ssh_util_1 = require("./ssh-util");
|
|
11
|
+
(0, bun_test_1.describe)('SshUtil', () => {
|
|
12
|
+
(0, bun_test_1.it)('quotes an argument for the shell of the host', () => {
|
|
13
|
+
const value = `it's "$HOME" & more`;
|
|
14
|
+
(0, bun_test_1.expect)((0, node_child_process_1.execFileSync)('/bin/sh', ['-c', `printf %s ${ssh_util_1.SshUtil.quote(value)}`]).toString()).toBe(value);
|
|
15
|
+
});
|
|
16
|
+
(0, bun_test_1.it)('runs a script with its arguments in sh, whatever the remote shell', () => {
|
|
17
|
+
const command = ssh_util_1.SshUtil.remoteCommand('printf "%s|%s" "$1" "$2"', 'a b', `c'd`);
|
|
18
|
+
(0, bun_test_1.expect)((0, node_child_process_1.execFileSync)('/bin/sh', ['-c', command]).toString()).toBe(`a b|c'd`);
|
|
19
|
+
});
|
|
20
|
+
(0, bun_test_1.it)('gives ssh a URL for a host with a port', () => {
|
|
21
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.destination('dev')).toBe('dev');
|
|
22
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.destination('me@dev.example.com')).toBe('me@dev.example.com');
|
|
23
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.destination('me@10.0.0.2:2222')).toBe('ssh://me@10.0.0.2:2222');
|
|
24
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.destination('[::1]:22')).toBe('ssh://[::1]:22');
|
|
25
|
+
});
|
|
26
|
+
(0, bun_test_1.it)('tells what kind of answer ssh asks for', () => {
|
|
27
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.question("me@dev's password: ").kind).toBe('secret');
|
|
28
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.question("Enter passphrase for key '/home/me/.ssh/id_ed25519': ").kind).toBe('secret');
|
|
29
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.question('Are you sure you want to continue connecting (yes/no/[fingerprint])? ').kind).toBe('confirm');
|
|
30
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.question('Verification code: ').kind).toBe('secret');
|
|
31
|
+
});
|
|
32
|
+
(0, bun_test_1.it)('reads what the probe prints, past what else a shell printed', () => {
|
|
33
|
+
const home = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'ta-probe-'));
|
|
34
|
+
const output = (0, node_child_process_1.execFileSync)('/bin/sh', ['-c', ssh_util_1.SshUtil.probeScript('0123456789abcdef')], { env: { ...process.env, HOME: home } }).toString();
|
|
35
|
+
const probe = ssh_util_1.SshUtil.parseProbe(`Last login: today\n${output}`);
|
|
36
|
+
(0, bun_test_1.expect)(probe).toMatchObject({ home, server: false, musl: false });
|
|
37
|
+
(0, bun_test_1.expect)(probe === null || probe === void 0 ? void 0 : probe.os).toBe((0, node_child_process_1.execFileSync)('uname', ['-s']).toString().trim());
|
|
38
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.parseProbe('bash: sh: command not found')).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
(0, bun_test_1.it)('names the build of Node.js of a host', () => {
|
|
41
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.nodePlatform('Linux', 'x86_64')).toBe('linux-x64');
|
|
42
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.nodePlatform('Linux', 'aarch64')).toBe('linux-arm64');
|
|
43
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.nodePlatform('Darwin', 'arm64')).toBe('darwin-arm64');
|
|
44
|
+
(0, bun_test_1.expect)(ssh_util_1.SshUtil.nodePlatform('FreeBSD', 'amd64')).toBeUndefined();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
(0, bun_test_1.describe)('NodeDist', () => {
|
|
48
|
+
(0, bun_test_1.it)('finds the tarball of a platform in the sums', () => {
|
|
49
|
+
const sums = [
|
|
50
|
+
`${'a'.repeat(64)} node-v22.9.0-darwin-arm64.tar.gz`,
|
|
51
|
+
`${'b'.repeat(64)} node-v22.9.0-linux-arm64.tar.xz`,
|
|
52
|
+
`${'c'.repeat(64)} node-v22.9.0-linux-arm64.tar.gz`,
|
|
53
|
+
].join('\n');
|
|
54
|
+
(0, bun_test_1.expect)(node_dist_1.NodeDist.find(sums, 'linux-arm64')).toEqual({ sha256: 'c'.repeat(64), name: 'node-v22.9.0-linux-arm64.tar.gz', version: 'v22.9.0' });
|
|
55
|
+
(0, bun_test_1.expect)(node_dist_1.NodeDist.find(sums, 'linux-x64')).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
(0, bun_test_1.describe)('SshConfig', () => {
|
|
59
|
+
(0, bun_test_1.it)('reads Host, Match and Include lines', () => {
|
|
60
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.parse('Host dev prod *.example.com !bad\n HostName 10.0.0.1\nhost="quoted"\nMatch all\nInclude conf.d/*')).toEqual([
|
|
61
|
+
{ host: ['dev', 'prod', '*.example.com', '!bad'] },
|
|
62
|
+
{ host: ['quoted'] },
|
|
63
|
+
{ match: true },
|
|
64
|
+
{ include: 'conf.d/*' },
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
(0, bun_test_1.it)('matches hosts as ssh does', () => {
|
|
68
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.matches(['*.example.com'], 'a.example.com')).toBe(true);
|
|
69
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.matches(['*.example.com'], 'colima')).toBe(false);
|
|
70
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.matches(['*', '!bad'], 'bad')).toBe(false);
|
|
71
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.matches(['dev?'], 'dev1')).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
(0, bun_test_1.it)('lists the hosts without wildcards, with those of files included where ssh reads them', () => {
|
|
74
|
+
const dir = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'ta-ssh-config-'));
|
|
75
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(dir, 'conf.d'));
|
|
76
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'config'), 'Include top.conf\nHost a\nInclude conf.d/*.conf\nHost b *.corp\n\nInclude corp.conf\nMatch all\nInclude never.conf\n');
|
|
77
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'top.conf'), 'Host t\n');
|
|
78
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'conf.d', 'x.conf'), 'Host c a\n');
|
|
79
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'conf.d', 'y.txt'), 'Host d\n');
|
|
80
|
+
// Read only for b and the hosts of *.corp: its other hosts are not reachable by their names.
|
|
81
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'corp.conf'), 'Host colima x.corp\n');
|
|
82
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'never.conf'), 'Host n\n');
|
|
83
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.hosts((0, node_path_1.join)(dir, 'config'))).toEqual(['t', 'a', 'b', 'x.corp']);
|
|
84
|
+
(0, bun_test_1.expect)(ssh_config_1.SshConfig.hosts((0, node_path_1.join)(dir, 'missing'))).toEqual([]);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
//# sourceMappingURL=ssh-util.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssh-util.spec.js","sourceRoot":"","sources":["../../../src/node/ssh/ssh-util.spec.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAChD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BAAM,EAAC,sBAAS,CAAC,KAAK,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-exec",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "Where Ta runs commands and touches files: the ExecEnvironment interface, this machine, and ta exec-server for another one",
|
|
5
5
|
"homepage": "https://github.com/cellbang/cell",
|
|
6
6
|
"bugs": {
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"src"
|
|
23
23
|
],
|
|
24
24
|
"scripts": {
|
|
25
|
-
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
25
|
+
"build": "bun scripts/remote-server.ts && tsc -b && bun ../../scripts/build-esm.ts",
|
|
26
26
|
"clean": "rm -rf lib esm *.tsbuildinfo",
|
|
27
27
|
"test": "bun test ./lib",
|
|
28
28
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@celljs/rpc": "4.
|
|
31
|
+
"@celljs/rpc": "4.7.0"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
package/src/node/exec-client.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import { ExecRpc } from './exec-rpc';
|
|
2
2
|
|
|
3
|
+
/** One connection to `ta exec-server`, whatever carries it: a WebSocket, or ssh (SshHost). */
|
|
4
|
+
export interface ExecConnection {
|
|
5
|
+
send(text: string): void;
|
|
6
|
+
close(): void;
|
|
7
|
+
onMessage(listener: (text: string) => void): void;
|
|
8
|
+
/** Once, with what went wrong when it was not closed on purpose. */
|
|
9
|
+
onClose(listener: (reason?: string) => void): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
export interface ExecClientOptions {
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
token: string;
|
|
13
|
+
/** Opens a connection; rejects with an ExecError when it cannot. See {@link ExecClient.webSocket}. */
|
|
14
|
+
connect(): Promise<ExecConnection>;
|
|
7
15
|
/** Larger than the epoch of any host that ran these sessions before; see {@link ExecRpc.InitializeParams}. */
|
|
8
16
|
epoch: number;
|
|
17
|
+
/** Pings this often (milliseconds) while connected, for a server that ends without them (over stdio). */
|
|
18
|
+
pingMs?: number;
|
|
9
19
|
}
|
|
10
20
|
|
|
11
21
|
/** A failed call to `ta exec-server`. */
|
|
@@ -32,20 +42,49 @@ interface PendingCall {
|
|
|
32
42
|
* host with a newer epoch it refuses every call: this host must stop running sessions there.
|
|
33
43
|
*/
|
|
34
44
|
export class ExecClient {
|
|
35
|
-
protected
|
|
36
|
-
protected current?:
|
|
45
|
+
protected connection?: Promise<ExecConnection>;
|
|
46
|
+
protected current?: ExecConnection;
|
|
37
47
|
protected nextCall = 0;
|
|
38
48
|
protected nextProcess = 0;
|
|
39
49
|
protected fenced = false;
|
|
50
|
+
protected pinger?: ReturnType<typeof setInterval>;
|
|
40
51
|
protected initialized?: ExecRpc.InitializeResult;
|
|
41
52
|
protected readonly pending = new Map<number, PendingCall>();
|
|
42
53
|
protected readonly listeners = new Map<number, NotificationListener>();
|
|
54
|
+
protected readonly stateListeners = new Set<(connected: boolean, error?: string) => void>();
|
|
43
55
|
|
|
44
56
|
constructor(protected readonly options: ExecClientOptions) {}
|
|
45
57
|
|
|
58
|
+
/** Connects to a `ta exec-server` listening on a WebSocket. */
|
|
59
|
+
static webSocket(url: string, token: string): () => Promise<ExecConnection> {
|
|
60
|
+
return async () => {
|
|
61
|
+
const address = new URL(url);
|
|
62
|
+
address.searchParams.set('token', token);
|
|
63
|
+
const socket = new WebSocket(address);
|
|
64
|
+
await new Promise<void>((resolve, reject) => {
|
|
65
|
+
socket.onopen = () => resolve();
|
|
66
|
+
socket.onerror = () => reject(new ExecError(`Cannot connect to ${url}.`, ExecRpc.ErrorCode.CONNECTION_LOST));
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
send: text => socket.send(text),
|
|
70
|
+
close: () => socket.close(),
|
|
71
|
+
onMessage: listener => {
|
|
72
|
+
socket.onmessage = message => listener(String(message.data));
|
|
73
|
+
},
|
|
74
|
+
onClose: listener => {
|
|
75
|
+
socket.onclose = () => listener();
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get connected(): boolean {
|
|
82
|
+
return this.current !== undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
46
85
|
async call<T>(method: string, params: object): Promise<T> {
|
|
47
|
-
const
|
|
48
|
-
return this.send<T>(
|
|
86
|
+
const connection = await this.connect();
|
|
87
|
+
return this.send<T>(connection, method, params);
|
|
49
88
|
}
|
|
50
89
|
|
|
51
90
|
/** What the server said when this client connected. */
|
|
@@ -54,7 +93,7 @@ export class ExecClient {
|
|
|
54
93
|
return this.initialized as ExecRpc.InitializeResult;
|
|
55
94
|
}
|
|
56
95
|
|
|
57
|
-
/** An id for a process, which its notifications carry. */
|
|
96
|
+
/** An id for a process or a TCP connection, which its notifications carry. */
|
|
58
97
|
processId(): number {
|
|
59
98
|
return ++this.nextProcess;
|
|
60
99
|
}
|
|
@@ -64,59 +103,82 @@ export class ExecClient {
|
|
|
64
103
|
return () => this.listeners.delete(id);
|
|
65
104
|
}
|
|
66
105
|
|
|
106
|
+
/** Runs when a connection opens (true), is lost or closed (false), or cannot be opened (false, with why). */
|
|
107
|
+
onState(listener: (connected: boolean, error?: string) => void): () => void {
|
|
108
|
+
this.stateListeners.add(listener);
|
|
109
|
+
return () => this.stateListeners.delete(listener);
|
|
110
|
+
}
|
|
111
|
+
|
|
67
112
|
close(): void {
|
|
68
|
-
this.current
|
|
69
|
-
this.
|
|
70
|
-
|
|
113
|
+
const current = this.current;
|
|
114
|
+
this.connection = undefined;
|
|
115
|
+
current?.close();
|
|
116
|
+
if (current) {
|
|
117
|
+
this.lost(current);
|
|
118
|
+
}
|
|
71
119
|
}
|
|
72
120
|
|
|
73
|
-
protected connect(): Promise<
|
|
121
|
+
protected connect(): Promise<ExecConnection> {
|
|
74
122
|
if (this.fenced) {
|
|
75
123
|
return Promise.reject(new ExecError('A host with a newer epoch took over the execution environment.', ExecRpc.ErrorCode.STALE_EPOCH));
|
|
76
124
|
}
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
125
|
+
this.connection ??= this.open().catch(error => {
|
|
126
|
+
this.connection = undefined;
|
|
127
|
+
for (const listener of this.stateListeners) {
|
|
128
|
+
listener(false, error instanceof Error ? error.message : String(error));
|
|
129
|
+
}
|
|
79
130
|
throw error;
|
|
80
131
|
});
|
|
81
|
-
return this.
|
|
132
|
+
return this.connection;
|
|
82
133
|
}
|
|
83
134
|
|
|
84
|
-
protected async open(): Promise<
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
socket.onopen = () => resolve();
|
|
90
|
-
socket.onerror = () => reject(new ExecError(`Cannot connect to ${this.options.url}.`, ExecRpc.ErrorCode.CONNECTION_LOST));
|
|
91
|
-
});
|
|
92
|
-
socket.onmessage = message => this.receive(String(message.data));
|
|
93
|
-
this.current = socket;
|
|
94
|
-
socket.onclose = () => this.lost(socket);
|
|
135
|
+
protected async open(): Promise<ExecConnection> {
|
|
136
|
+
const connection = await this.options.connect();
|
|
137
|
+
connection.onMessage(text => this.receive(text));
|
|
138
|
+
connection.onClose(reason => this.lost(connection, reason));
|
|
139
|
+
this.current = connection;
|
|
95
140
|
try {
|
|
96
|
-
this.initialized = await this.send(
|
|
141
|
+
this.initialized = await this.send(connection, ExecRpc.Method.INITIALIZE, { epoch: this.options.epoch } satisfies ExecRpc.InitializeParams);
|
|
97
142
|
} catch (error) {
|
|
98
143
|
if ((error as ExecError).code === ExecRpc.ErrorCode.STALE_EPOCH) {
|
|
99
144
|
this.fenced = true;
|
|
100
145
|
}
|
|
101
|
-
|
|
146
|
+
connection.close();
|
|
102
147
|
throw error;
|
|
103
148
|
}
|
|
104
|
-
|
|
149
|
+
if (this.options.pingMs) {
|
|
150
|
+
clearInterval(this.pinger);
|
|
151
|
+
this.pinger = setInterval(() => connection.send(JSON.stringify({ jsonrpc: '2.0', method: ExecRpc.PING })), this.options.pingMs);
|
|
152
|
+
this.pinger.unref?.();
|
|
153
|
+
}
|
|
154
|
+
for (const listener of this.stateListeners) {
|
|
155
|
+
listener(true);
|
|
156
|
+
}
|
|
157
|
+
return connection;
|
|
105
158
|
}
|
|
106
159
|
|
|
107
|
-
protected send<T>(
|
|
108
|
-
if (
|
|
160
|
+
protected send<T>(connection: ExecConnection, method: string, params: object): Promise<T> {
|
|
161
|
+
if (this.current !== connection) {
|
|
109
162
|
return Promise.reject(new ExecError('The connection to the execution environment was lost.', ExecRpc.ErrorCode.CONNECTION_LOST));
|
|
110
163
|
}
|
|
111
164
|
const id = ++this.nextCall;
|
|
112
165
|
return new Promise<T>((resolve, reject) => {
|
|
113
166
|
this.pending.set(id, { resolve: resolve as (result: unknown) => void, reject });
|
|
114
|
-
|
|
167
|
+
connection.send(JSON.stringify({ jsonrpc: '2.0', id, method, params } satisfies ExecRpc.Message));
|
|
115
168
|
});
|
|
116
169
|
}
|
|
117
170
|
|
|
171
|
+
/** Anything but JSON-RPC is left alone: a shell of the other machine may have printed it first. */
|
|
118
172
|
protected receive(text: string): void {
|
|
119
|
-
|
|
173
|
+
let message: ExecRpc.Message;
|
|
174
|
+
try {
|
|
175
|
+
message = JSON.parse(text);
|
|
176
|
+
} catch {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (message?.jsonrpc !== '2.0') {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
120
182
|
if (message.method) {
|
|
121
183
|
this.listeners.get(message.params?.id)?.(message.method, message.params);
|
|
122
184
|
return;
|
|
@@ -134,19 +196,26 @@ export class ExecClient {
|
|
|
134
196
|
}
|
|
135
197
|
}
|
|
136
198
|
|
|
137
|
-
/** Calls fail, and processes are gone: the server ended them with the connection. */
|
|
138
|
-
protected lost(
|
|
139
|
-
if (this.current
|
|
140
|
-
|
|
141
|
-
this.socket = undefined;
|
|
199
|
+
/** Calls fail, and processes are gone: the server ended them with the connection. `reason`: it was not closed on purpose. */
|
|
200
|
+
protected lost(connection: ExecConnection, reason?: string): void {
|
|
201
|
+
if (this.current !== connection) {
|
|
202
|
+
return;
|
|
142
203
|
}
|
|
204
|
+
this.current = undefined;
|
|
205
|
+
this.connection = undefined;
|
|
206
|
+
clearInterval(this.pinger);
|
|
207
|
+
this.pinger = undefined;
|
|
208
|
+
const message = reason ?? 'The connection to the execution environment was lost.';
|
|
143
209
|
for (const call of this.pending.values()) {
|
|
144
|
-
call.reject(new ExecError(
|
|
210
|
+
call.reject(new ExecError(message, ExecRpc.ErrorCode.CONNECTION_LOST));
|
|
145
211
|
}
|
|
146
212
|
this.pending.clear();
|
|
147
213
|
for (const listener of [...this.listeners.values()]) {
|
|
148
214
|
listener(ExecRpc.Notification.EXIT, { exitCode: null });
|
|
149
215
|
}
|
|
150
216
|
this.listeners.clear();
|
|
217
|
+
for (const listener of this.stateListeners) {
|
|
218
|
+
listener(false, reason);
|
|
219
|
+
}
|
|
151
220
|
}
|
|
152
221
|
}
|