@celljs/ta-exec 4.5.0 → 4.6.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.
Files changed (51) hide show
  1. package/README.md +24 -2
  2. package/esm/node/exec-client.d.ts +52 -0
  3. package/esm/node/exec-client.js +128 -0
  4. package/esm/node/exec-client.js.map +1 -0
  5. package/esm/node/exec-protocol.d.ts +2 -0
  6. package/esm/node/exec-rpc.d.ts +90 -0
  7. package/esm/node/exec-rpc.js +52 -0
  8. package/esm/node/exec-rpc.js.map +1 -0
  9. package/esm/node/exec-server.d.ts +61 -0
  10. package/esm/node/exec-server.js +217 -0
  11. package/esm/node/exec-server.js.map +1 -0
  12. package/esm/node/index.d.ts +4 -0
  13. package/esm/node/index.js +4 -0
  14. package/esm/node/index.js.map +1 -1
  15. package/esm/node/remote-exec-environment.d.ts +26 -0
  16. package/esm/node/remote-exec-environment.js +115 -0
  17. package/esm/node/remote-exec-environment.js.map +1 -0
  18. package/lib/node/exec-client.d.ts +53 -0
  19. package/lib/node/exec-client.d.ts.map +1 -0
  20. package/lib/node/exec-client.js +133 -0
  21. package/lib/node/exec-client.js.map +1 -0
  22. package/lib/node/exec-protocol.d.ts +2 -0
  23. package/lib/node/exec-protocol.d.ts.map +1 -1
  24. package/lib/node/exec-rpc.d.ts +91 -0
  25. package/lib/node/exec-rpc.d.ts.map +1 -0
  26. package/lib/node/exec-rpc.js +55 -0
  27. package/lib/node/exec-rpc.js.map +1 -0
  28. package/lib/node/exec-server.d.ts +62 -0
  29. package/lib/node/exec-server.d.ts.map +1 -0
  30. package/lib/node/exec-server.js +222 -0
  31. package/lib/node/exec-server.js.map +1 -0
  32. package/lib/node/exec-server.spec.d.ts +2 -0
  33. package/lib/node/exec-server.spec.d.ts.map +1 -0
  34. package/lib/node/exec-server.spec.js +101 -0
  35. package/lib/node/exec-server.spec.js.map +1 -0
  36. package/lib/node/index.d.ts +4 -0
  37. package/lib/node/index.d.ts.map +1 -1
  38. package/lib/node/index.js +4 -0
  39. package/lib/node/index.js.map +1 -1
  40. package/lib/node/remote-exec-environment.d.ts +27 -0
  41. package/lib/node/remote-exec-environment.d.ts.map +1 -0
  42. package/lib/node/remote-exec-environment.js +119 -0
  43. package/lib/node/remote-exec-environment.js.map +1 -0
  44. package/package.json +5 -3
  45. package/src/node/exec-client.ts +152 -0
  46. package/src/node/exec-protocol.ts +2 -0
  47. package/src/node/exec-rpc.ts +104 -0
  48. package/src/node/exec-server.spec.ts +110 -0
  49. package/src/node/exec-server.ts +243 -0
  50. package/src/node/index.ts +4 -0
  51. package/src/node/remote-exec-environment.ts +124 -0
@@ -0,0 +1,124 @@
1
+ import type { ExecClient } from './exec-client';
2
+ import type { DirEntry, ExecEnvironment, ExecOptions, ExecProcess, ExecResult, FileStat, SpawnOptions } from './exec-protocol';
3
+ import { ExecRpc } from './exec-rpc';
4
+
5
+ type Listener<T extends unknown[]> = (...args: T) => void;
6
+
7
+ /** A directory on the machine of a `ta exec-server`. */
8
+ export class RemoteExecEnvironment implements ExecEnvironment {
9
+ readonly local: boolean = false;
10
+
11
+ constructor(
12
+ protected readonly client: ExecClient,
13
+ readonly cwd: string,
14
+ ) {}
15
+
16
+ async exec(command: string, options: ExecOptions = {}): Promise<ExecResult> {
17
+ if (options.signal?.aborted) {
18
+ return { exitCode: null };
19
+ }
20
+ const id = this.client.processId();
21
+ const stop = this.client.listen(id, (method, params) => {
22
+ if (method === ExecRpc.Notification.OUTPUT && params.data) {
23
+ options.onData?.(ExecRpc.fromBase64(params.data));
24
+ }
25
+ });
26
+ const kill = () => void this.client.call(ExecRpc.Method.KILL, { root: this.cwd, id }).catch(() => undefined);
27
+ options.signal?.addEventListener('abort', kill, { once: true });
28
+ try {
29
+ const params: ExecRpc.ExecParams = {
30
+ root: this.cwd,
31
+ id,
32
+ command,
33
+ cwd: options.cwd,
34
+ env: RemoteExecEnvironment.delta(options.env),
35
+ timeout: options.timeout,
36
+ };
37
+ return await this.client.call<ExecResult>(ExecRpc.Method.EXEC, params);
38
+ } finally {
39
+ options.signal?.removeEventListener('abort', kill);
40
+ stop();
41
+ }
42
+ }
43
+
44
+ spawn(command: string, args: string[], options: SpawnOptions = {}): ExecProcess {
45
+ const id = this.client.processId();
46
+ const stdout = new Set<Listener<[Uint8Array]>>();
47
+ const stderr = new Set<Listener<[Uint8Array]>>();
48
+ const exit = new Set<Listener<[number | null]>>();
49
+ let ended = false;
50
+ const end = (exitCode: number | null) => {
51
+ if (!ended) {
52
+ ended = true;
53
+ stop();
54
+ for (const listener of exit) {
55
+ listener(exitCode);
56
+ }
57
+ }
58
+ };
59
+ const stop = this.client.listen(id, (method, params) => {
60
+ if (method === ExecRpc.Notification.EXIT) {
61
+ end(params.exitCode ?? null);
62
+ } else if (params.data) {
63
+ const data = ExecRpc.fromBase64(params.data);
64
+ for (const listener of method === ExecRpc.Notification.STDERR ? stderr : stdout) {
65
+ listener(data);
66
+ }
67
+ }
68
+ });
69
+ const params: ExecRpc.SpawnParams = { root: this.cwd, id, command, args, cwd: options.cwd, env: RemoteExecEnvironment.delta(options.env) };
70
+ this.client.call(ExecRpc.Method.SPAWN, params).catch(() => end(null));
71
+ return {
72
+ write: data => void this.client.call(ExecRpc.Method.WRITE, { root: this.cwd, id, data: ExecRpc.toBase64(data) }).catch(() => undefined),
73
+ onStdout: listener => stdout.add(listener),
74
+ onStderr: listener => stderr.add(listener),
75
+ onExit: listener => exit.add(listener),
76
+ kill: () => void this.client.call(ExecRpc.Method.KILL, { root: this.cwd, id }).catch(() => undefined),
77
+ };
78
+ }
79
+
80
+ async readFile(path: string): Promise<Uint8Array> {
81
+ return ExecRpc.fromBase64(await this.client.call<string>(ExecRpc.Method.READ_FILE, this.path(path)));
82
+ }
83
+
84
+ writeFile(path: string, data: Uint8Array | string): Promise<void> {
85
+ return this.client.call(ExecRpc.Method.WRITE_FILE, { ...this.path(path), data: ExecRpc.toBase64(data) });
86
+ }
87
+
88
+ async stat(path: string): Promise<FileStat | undefined> {
89
+ return (await this.client.call<FileStat | null>(ExecRpc.Method.STAT, this.path(path))) ?? undefined;
90
+ }
91
+
92
+ readdir(path: string): Promise<DirEntry[]> {
93
+ return this.client.call(ExecRpc.Method.READDIR, this.path(path));
94
+ }
95
+
96
+ mkdir(path: string): Promise<void> {
97
+ return this.client.call(ExecRpc.Method.MKDIR, this.path(path));
98
+ }
99
+
100
+ remove(path: string, options: { recursive?: boolean } = {}): Promise<void> {
101
+ return this.client.call(ExecRpc.Method.REMOVE, { ...this.path(path), recursive: options.recursive });
102
+ }
103
+
104
+ protected path(path: string): ExecRpc.PathParams {
105
+ return { root: this.cwd, path };
106
+ }
107
+
108
+ /**
109
+ * What a tool sets on top of the environment. Tools pass this process's whole environment plus
110
+ * their own variables; the host's variables (API keys among them) must not reach the other machine.
111
+ */
112
+ static delta(env: NodeJS.ProcessEnv | undefined): Record<string, string> | undefined {
113
+ if (!env) {
114
+ return undefined;
115
+ }
116
+ const delta: Record<string, string> = {};
117
+ for (const [key, value] of Object.entries(env)) {
118
+ if (value !== undefined && process.env[key] !== value) {
119
+ delta[key] = value;
120
+ }
121
+ }
122
+ return delta;
123
+ }
124
+ }