-
Notifications
You must be signed in to change notification settings - Fork 5
/
command.ts
265 lines (230 loc) · 8.15 KB
/
command.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
import {Chunk, ChunkType} from './chunk';
import {PassThrough, Readable, Transform, TransformOptions, Writable} from 'stream';
import {EventEmitter} from 'events';
import * as fs from 'fs';
export interface Ref {
ref(): void
unref(): void
}
export interface CommandContext {
args: string[]
env: Map<string, string>
workingDirectory: string
}
// get-stdin maintains reference to process.stdin, so it must be replaced with a permemant value
class FakeStream extends PassThrough {
constructor(private readonly options?: TransformOptions) {
super(options);
}
reset() {
this.removeAllListeners();
PassThrough.call(this, this.options);
}
}
export namespace real {
export const stderrWrite = process.stderr.write.bind(process.stderr);
export const stdoutWrite = process.stdout.write.bind(process.stdout);
export const processExit = process.exit.bind(process);
}
// console.log, console.error maintain references to write(), so it must be replaced with a permenant hook
let stderrWrite = real.stderrWrite;
let stdoutWrite = real.stdoutWrite;
let installed = false;
function install() {
if (installed) {
return;
}
installed = true;
process.stderr.write = function() {
return stderrWrite.apply(this, arguments);
};
process.stdout.write = function() {
return stdoutWrite.apply(this, arguments);
};
Object.defineProperty(process, 'stderr', {
configurable: true,
enumerable: true,
get: (stderr => () => stderr)(new FakeStream),
});
Object.defineProperty(process, 'stdout', {
configurable: true,
enumerable: true,
get: (stdout => () => stdout)(new FakeStream),
});
process.stderr.write = function() {
return stderrWrite.apply(this, arguments);
};
process.stdout.write = function() {
return stdoutWrite.apply(this, arguments);
};
if (typeof process.stdin.destroy === 'function') {
process.stdin.destroy();
}
Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: true,
get: (stdin => () => stdin)(new FakeStream),
});
}
export class Command {
constructor(private readonly main: () => void) {
install();
}
invoke(context: CommandContext, reader: Readable, writer: Writable, ref: Ref): Promise<number> {
const finalizers: (() => void)[] = [];
ref.unref();
// main module
finalizers.push((mainModule => () => process.mainModule = mainModule)(process.mainModule));
// arguments
finalizers.push((argv => () => process.argv = argv)(process.argv));
process.argv = process.argv.slice(0, 2).concat(context.args);
// environment variables
finalizers.push((env => () => process.env = env)(process.env));
process.env = {};
for (const [key, value] of context.env) {
process.env[key] = value;
}
// working directory
finalizers.push((workingDirectory => () => process.chdir(workingDirectory))(process.cwd()));
process.chdir(context.workingDirectory);
// stdin
const stdin = new CommandStdin(writer, ref);
finalizers.push(() => {
stdin.unpipe(process.stdin);
(process.stdin as FakeStream).reset();
});
reader.pipe(stdin).pipe(process.stdin);
function stdinNewListener(this: NodeJS.ReadStream, type: string) {
switch (type) {
case 'data':
case 'end':
if (!this.isPaused) {
ref.ref();
}
}
}
function stdinRemoveListener(this: EventEmitter, type: string) {
switch (type) {
case 'data':
case 'end':
if (!this.listenerCount('data') && !this.listenerCount('end')) {
ref.unref();
}
}
}
function stdinPause() {
ref.unref();
}
function stdinResume() {
if (this.listenerCount('data') || this.listenerCount('end')) {
ref.ref();
}
}
process.stdin
.on('pause', stdinPause)
.on('resume', stdinResume)
.on('removeListener', stdinRemoveListener)
.on('newListener', stdinNewListener);
process.stdin.once('end', function(this: EventEmitter) {
this.removeListener('removeListener', stdinRemoveListener);
this.removeListener('newListener', stdinNewListener);
this.removeListener('pause', stdinPause);
this.removeListener('resume', stdinResume);
ref.unref();
});
process.stdin.on('newListener', function listener(this: NodeJS.ReadStream, type) {
switch (type) {
case 'data':
case 'end':
this.removeListener('newListener', listener);
stdin.request();
}
});
// stdout
const stdout = new CommandStdout();
stdout.pipe(writer, {end:false});
finalizers.push((write => () => stdoutWrite = write)(stdoutWrite));
finalizers.push(() => (process.stdout as FakeStream).reset());
stdoutWrite = stdout.write.bind(stdout);
// stderr
const stderr = new CommandStderr();
stderr.pipe(writer, {end:false});
finalizers.push((write => () => stderrWrite = write)(stderrWrite));
finalizers.push(() => (process.stderr as FakeStream).reset());
stderrWrite = stderr.write.bind(stderr);
// errors
function uncaughtExceptionListener(err: Error) {
console.error(err.stack);
process.exit(1);
}
process.on('uncaughtException', uncaughtExceptionListener);
finalizers.push(() => process.removeListener('uncaughtException', uncaughtExceptionListener));
process.nextTick(this.main);
return new Promise(resolve => {
function finalize(code?: number | undefined) {
for (const finalizer of finalizers) {
finalizer();
}
ref.ref();
resolve(code || 0);
}
finalizers.push((exit => () => process.exit = exit)(process.exit));
process.exit = finalize as (code?: number | undefined) => never;
process.on('beforeExit', finalize);
finalizers.push(() => process.removeListener('beforeExit', finalize));
});
}
}
class CommandStdin extends Transform {
constructor(private readonly writer: Writable, private readonly ref: Ref) {
super({writableObjectMode:true});
}
request() {
// NodeJS will not flush this buffer
// a workaround is to send a newline (!) but that clutters the output
// this.writer.write(new Chunk(ChunkType.Stderr, Buffer.from('\n')));
try {
this.writer.write(new Chunk(ChunkType.StdinStart));
} catch (e) {
}
}
_transform(chunk: Chunk, encoding: string, callback: Function) {
switch (chunk.type) {
case ChunkType.Stdin:
callback(null, chunk.data);
this.request();
break;
case ChunkType.StdinEnd:
callback();
this.end();
break;
default:
callback(new CommandStdin.UnexpectedChunk(chunk.type));
}
}
}
namespace CommandStdin {
export class UnexpectedChunk extends Error {
constructor(type: ChunkType) {
super(`Unexpected ${String.fromCharCode(type)} chunk in stdin stream`);
}
}
}
class CommandOutput extends Transform {
constructor(private readonly type: ChunkType) {
super({readableObjectMode: true});
}
_transform(data: Buffer, encoding: string, callback: Function) {
callback(null, new Chunk(this.type, data));
}
}
class CommandStdout extends CommandOutput {
constructor() {
super(ChunkType.Stdout);
}
}
class CommandStderr extends CommandOutput {
constructor() {
super(ChunkType.Stderr);
}
}