-
Notifications
You must be signed in to change notification settings - Fork 71
/
Shell.ts
417 lines (367 loc) Β· 14.7 KB
/
Shell.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { EOL } from 'os';
import { inspect } from 'util';
import { spawn, ChildProcess, SpawnOptions } from 'child_process';
import { Readable, Writable } from 'stream';
import { once } from 'events';
import Debugger from 'debug';
import pTimeout from 'p-timeout';
import PQueue from 'p-queue';
import kindOf from 'kind-of';
import { customAlphabet } from 'nanoid';
import { AccumulateStream } from 'accumulate-stream';
import { trimBuffer } from 'trim-buffer';
import { ProcessError, InvocationError } from './errors';
import { JavaScriptTypes, Converters, Converter, SHELL_CONVERTERS } from './converters';
export type StdioStreams = {
stdin: Writable;
stdout: Readable;
stderr: Readable;
};
export type Debuggers = {
comment: Debugger.Debugger;
command: Debugger.Debugger;
output: Debugger.Debugger;
};
export type ExecutableOptions = {
[key: string]: boolean | string;
};
export type ShellSpawnOptions = Omit<
SpawnOptions,
'argv0' | 'stdio' | 'detached' | 'serialization' | 'shell' | 'timeout'
>;
export type InvocationResult = {
command: string;
raw: string;
hadErrors: boolean;
startTime: number;
duration?: number;
stdout?: Buffer;
stderr?: Buffer;
};
export type ShellOptions = {
// executable must support reading from stdin (-c -) and can be either:
// 1) a globally installed executable like "bash", "pwsh", etc.. that appears on $PATH
// 2) a local path to a shell executable
executable?: string;
executableOptions?: ExecutableOptions;
spawnOptions?: ShellSpawnOptions;
inputEncoding?: BufferEncoding;
outputEncoding?: BufferEncoding;
debug?: boolean;
disposeTimeout?: number;
invocationTimeout?: number;
throwOnInvocationError?: boolean;
};
export type ShellCtor = { new (options?: ShellOptions): Shell };
export type ShellDerived = ShellCtor & typeof Shell;
const DELIMITER_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const DELIMITER_LENGTH = 16;
export abstract class Shell {
private readonly executable: string;
private readonly executableOptions: string[];
private readonly spawnOptions: SpawnOptions;
private readonly invocationQueue: PQueue;
private readonly debuggers: Debuggers;
private readonly process: ChildProcess;
private isExited = false;
private readonly exitPromise: Promise<void>;
private readonly resultsEncoding: BufferEncoding;
private readonly invocationTimeout: number;
private readonly throwOnInvocationError: boolean;
protected abstract writeToOutput(input: string): string;
protected abstract writeToError(input: string): string;
protected static converters: Converters = SHELL_CONVERTERS;
public readonly streams: StdioStreams;
public readonly history: InvocationResult[] = [];
constructor(options: ShellOptions = {}) {
this.executable = this.setExecutable(options);
this.executableOptions = this.setExecutableOptions(options);
this.spawnOptions = { ...options.spawnOptions, stdio: 'pipe', detached: false, shell: false };
this.invocationQueue = this.setInvocationQueue();
this.process = spawn(this.executable, this.executableOptions, this.spawnOptions);
this.streams = {
stdin: this.process.stdin as Writable,
stdout: this.process.stdout as Readable,
stderr: this.process.stderr as Readable,
};
this.debuggers = this.setDebuggers(options);
this.debuggers.comment(`shell process started: "${this.executable} ${this.executableOptions.join(' ')}"`);
this.exitPromise = this.setProcessExitListeners();
this.resultsEncoding = options.outputEncoding ?? 'utf-8';
this.setProcessStdioEncoding(options);
this.setProcessKillOptions(options);
this.invocationTimeout = options.invocationTimeout ?? 0;
this.throwOnInvocationError = options.throwOnInvocationError ?? true;
}
private detachStreamFromOutput(stream: Writable): void {
this.streams.stdout.unpipe(stream);
this.streams.stderr.unpipe(stream);
stream.end();
}
private async readBulkFromOutput(stream: Readable, delimiter: string): Promise<Buffer> {
let firstDelimiterOccurrence = -1;
let secondDelimiterOccurrence = -1;
const accumulator = new AccumulateStream({
custom: {
event: delimiter,
isDone(chunk): boolean {
const buffer = this.getBuffer();
// search for 2 delimiter occurrences
if (firstDelimiterOccurrence === -1) {
firstDelimiterOccurrence = buffer.indexOf(delimiter);
}
const secondDelimiterByteOffset =
chunk.length + delimiter.length < buffer.length ? chunk.length + delimiter.length : delimiter.length + 1;
secondDelimiterOccurrence = buffer.indexOf(delimiter, -secondDelimiterByteOffset);
// found 2 different delimiters
return firstDelimiterOccurrence !== secondDelimiterOccurrence && secondDelimiterOccurrence !== -1;
},
},
});
accumulator.on('chunk', ({ chunk }: { chunk: Buffer }): void =>
this.debuggers.output(trimBuffer(chunk).toString(this.resultsEncoding)),
);
stream.pipe(accumulator);
const [accumulatedOutput] = await once(accumulator, 'data');
this.detachStreamFromOutput(accumulator);
// slice and trim output buffer
return trimBuffer(accumulatedOutput.slice(firstDelimiterOccurrence + delimiter.length, secondDelimiterOccurrence));
}
protected setExecutable({ executable }: { executable?: string }): string {
if (!executable) {
throw new Error(`unable to determine shell executable
${inspect(
{ platform: process.platform, SHELL: process.env.SHELL, PATH: process.env.PATH, ...process.versions },
{ compact: true, depth: 5, breakLength: 80 },
)}`);
}
return executable;
}
private setExecutableOptions({ executableOptions = {} }: { executableOptions?: ExecutableOptions }): string[] {
let options: string[] = [];
Object.entries(executableOptions).forEach(([name, value]) => {
if (!name.startsWith('-') && name.startsWith('--')) {
throw new Error(`option ${name} must starts with a dash "-" | "--"`);
}
switch (typeof value) {
case 'string':
options = [...options, name, value as string];
break;
case 'boolean':
options = (!value as boolean) ? [...options] : [...options, name];
break;
default:
throw new Error(`option ${name} value must be string or boolean`);
}
});
return options;
}
private setInvocationQueue(): PQueue {
return new PQueue({
concurrency: 1,
autoStart: true,
});
}
private clearInvocationQueue(): void {
this.invocationQueue.pause();
this.invocationQueue.removeAllListeners();
this.invocationQueue.clear();
}
private setDebuggers({ debug }: { debug?: boolean }): Debuggers {
const namespace = `${this.executable.split('/')?.pop()?.toUpperCase()}:${this.process.pid}`;
const _debugger = Debugger(namespace);
// eslint-disable-next-line no-console
_debugger.log = console.log.bind(console);
if (debug) {
// debug package manages its state globally - https://github.com/visionmedia/debug#set-dynamically
Debugger.enable(`${process.env.DEBUG},${namespace}*`);
}
return {
comment: _debugger.extend(' #'),
command: _debugger.extend(' $'),
output: _debugger.extend(' >'),
};
}
private setProcessExitListeners(): Promise<void> {
let useStderrOnExit = false;
// listen to stderr for catching starter errors
const stderr = new AccumulateStream({ count: 3 });
this.streams.stderr.pipe(stderr);
once(stderr, 'data').then(() => {
// probably there is no starter error after 3 chunks so disable this listener
this.detachStreamFromOutput(stderr);
});
// set up process exit listeners
const processErrorPromise = once(this.process, 'error').then(([error]) => {
throw new ProcessError(this.process, error);
});
const processExitPromise = once(this.process, 'exit').then(([code, signal]) => {
const error = !useStderrOnExit ? undefined : new Error(stderr.getBuffer().toString(this.resultsEncoding));
if (signal && signal !== 'SIGTERM') {
throw new ProcessError(this.process, error);
}
// code 143 === SIGTERM
if (code && code !== 0 && code !== 143) {
throw new ProcessError(this.process, error);
}
});
// catch EPIPE error to avoid node crash and let process's 'exit' event to determine result
once(this.streams.stdin, 'error').then(() => {
useStderrOnExit = true;
});
// ordered process exit scenarios:
// ENOENT - spawn error - `error` listener
// EPIPE - bad process option - `exit` listener
// EXIT<code/signal> - process exit with either 0 or error - `exit` listener
return Promise.race([processErrorPromise, processExitPromise]).finally(() => {
this.isExited = true;
this.clearInvocationQueue();
this.streams.stdin.end();
this.detachStreamFromOutput(stderr);
this.debuggers.comment(`shell process exited`);
});
}
private setProcessStdioEncoding({
inputEncoding = 'utf-8',
outputEncoding = 'utf-8',
}: {
inputEncoding?: BufferEncoding;
outputEncoding?: BufferEncoding;
}): void {
this.streams.stdin.setDefaultEncoding(inputEncoding);
this.streams.stdout.setEncoding(outputEncoding);
this.streams.stderr.setEncoding(outputEncoding);
}
private setProcessKillOptions({ disposeTimeout }: { disposeTimeout?: number }): void {
if (disposeTimeout) {
setTimeout(async () => {
await this.dispose();
}, disposeTimeout);
}
}
private async invokeCommand(command: string): Promise<InvocationResult> {
this.debuggers.comment('starting command invocation');
this.debuggers.command(command);
// update history with current command
this.history.unshift({
command,
raw: '',
hadErrors: false,
startTime: Date.now(),
});
const currentHistoryRecord = this.history[0];
// generate output bulk delimiter
const bulkDelimiter = customAlphabet(DELIMITER_ALPHABET, DELIMITER_LENGTH)();
// init bulk readers for both output streams
const outputPromise = Promise.all([
this.readBulkFromOutput(this.streams.stdout, bulkDelimiter),
this.readBulkFromOutput(this.streams.stderr, bulkDelimiter),
]);
// race output and error
let isCommandFailed = false;
let isCommandTimedOut = false;
const isCommandDone = Promise.race([this.exitPromise, outputPromise]);
// write delimiter then command then delimiter to process input so `readBulkFromOutput` will detect the exact command output
this.streams.stdin.write(this.writeToError(bulkDelimiter));
this.streams.stdin.write(EOL);
this.streams.stdin.write(this.writeToOutput(bulkDelimiter));
this.streams.stdin.write(EOL);
this.streams.stdin.write(command);
this.streams.stdin.write(EOL);
this.streams.stdin.write(this.writeToError(bulkDelimiter));
this.streams.stdin.write(EOL);
this.streams.stdin.write(this.writeToOutput(bulkDelimiter));
this.streams.stdin.write(EOL);
try {
// wait for command to finish and apply timeout if needed
// [EXIT] process crushed -> throw ProcessError
if (!this.invocationTimeout) {
await isCommandDone;
} else {
await pTimeout(isCommandDone, this.invocationTimeout, () => {
isCommandTimedOut = true;
});
}
} finally {
// calculate results and update history
currentHistoryRecord.duration = Date.now() - currentHistoryRecord.startTime;
if (!this.isExited) {
if (!isCommandTimedOut) {
const [stdout, stderr] = await outputPromise;
currentHistoryRecord.stdout = stdout;
currentHistoryRecord.stderr = stderr;
isCommandFailed = stderr.length !== 0;
if (!isCommandFailed) {
// [EXIT] invocation succeeded -> return result
currentHistoryRecord.raw = currentHistoryRecord.stdout.toString(this.resultsEncoding);
this.debuggers.comment('command invocation succeeded');
} else {
// [EXIT] invocation failed -> return failed result
currentHistoryRecord.hadErrors = true;
currentHistoryRecord.raw = currentHistoryRecord.stderr.toString(this.resultsEncoding);
this.debuggers.comment('command invocation failed');
}
} else {
// [EXIT] invocationTimeout reached -> return partial failed result
currentHistoryRecord.hadErrors = true;
this.debuggers.comment('command invocation timed out');
}
} else {
// [EXIT] user killed process gracefully -> return partial result
this.debuggers.comment('command invocation stopped');
}
}
// return or throw command result
if (this.throwOnInvocationError && currentHistoryRecord.hadErrors) {
throw new InvocationError(currentHistoryRecord.raw);
}
return currentHistoryRecord;
}
public async invoke(command: string): Promise<InvocationResult> {
if (this.isExited) {
throw new InvocationError('invoke called after process exited');
}
return this.invocationQueue.add(() => this.invokeCommand(command));
}
public async dispose(signal: NodeJS.Signals = 'SIGTERM'): Promise<void> {
if (!this.isExited) {
this.clearInvocationQueue();
this.process.kill(signal);
}
return this.exitPromise;
}
private static convertToShell(this: ShellDerived, jsObject: unknown): string {
const objectType = kindOf(jsObject) as JavaScriptTypes;
const hasConverter = this.converters.has(objectType);
if (!hasConverter) {
throw new Error(`cannot convert ${objectType} object to its shell representation`);
}
return (this.converters.get(objectType) as Converter).call(undefined, jsObject, this.convertToShell.bind(this));
}
public static command(this: ShellDerived, literals: readonly string[], ...expressions: unknown[]): string {
return literals
.map((literal, index) => {
return `${literal}${this.convertToShell(expressions?.[index])}`;
})
.join('');
}
public static async invoke(this: ShellDerived, command: string, options?: ShellOptions): Promise<InvocationResult> {
const shell = new this(options);
try {
return await shell.invoke(command);
} finally {
await shell.dispose();
}
}
public static async $(
this: ShellDerived,
literals: readonly string[],
...expressions: unknown[]
): Promise<InvocationResult> {
return this.invoke(this.command(literals, ...expressions));
}
public static $$(this: ShellDerived, literals: readonly string[], ...expressions: unknown[]) {
return (options?: ShellOptions): Promise<InvocationResult> =>
this.invoke(this.command(literals, ...expressions), options);
}
}