-
Notifications
You must be signed in to change notification settings - Fork 3
/
StdinoutStream.js
251 lines (215 loc) · 6.09 KB
/
StdinoutStream.js
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
const childProcess = require('child_process');
const Stream = require('stream');
const debug = require('./debug');
function noopFilterStderr() {
return true;
}
module.exports = class StdinoutStream extends Stream.Transform {
constructor(name, binary, args, options) {
super();
this.args = args;
this.binary = binary;
this.errorline = null;
this.filterStderr = null;
this.logger = null;
this.process = null;
this.hasEnded = false;
this.hasErrored = false;
this.hasOutput = false;
this.logger = debug(name);
options = options || {};
if (typeof options.filterStderr === 'function') {
this.filterStderr = options.filterStderr;
} else {
this.filterStderr = noopFilterStderr;
}
}
// implementation local methods
__cleanUp() {
if (this.process) {
this.hasEnded = true;
this.process.kill();
return false;
} else if (!this.hasOutput) {
this.hasOutput = true; // set condition for reentrancy
return this.__error('stream ended without emitting any data');
} else {
this.hasEnded = true;
return true;
}
}
__error(messageOrError) {
if (this.hasErrored) {
return;
}
const error =
typeof messageOrError === 'string'
? new Error(`${this.logger.namespace}: ${messageOrError}`)
: messageOrError;
this.errorline = null;
this.hasErrored = true;
this.emit('error', error);
this.__cleanUp();
return false;
}
__execCommand() {
const onSpawned = (process) => {
this.process = process;
if (this.hasEnded) {
return;
}
const boundError = (errorPrefix) => (messageOrError) => {
messageOrError =
typeof messageOrError !== 'string'
? messageOrError.message
: messageOrError;
let errorSuffix;
if (!this.errorline && !this.logger.enabled) {
errorSuffix = messageOrError;
} else if (!this.errorline) {
errorSuffix = messageOrError;
} else {
errorSuffix = `${messageOrError} (${this.errorline.trimEnd()})`;
}
return this.__error(`[${errorPrefix}] ${errorSuffix}`);
};
const processError = boundError('PROCESS');
const stdinError = boundError('STDIN');
const stdoutError = boundError('STDOUT');
const decodeStderr = (line) => {
let errorMessage;
try {
errorMessage = line.toString();
} catch (e) {
return 'undecodeable output on stderr';
}
if (!errorMessage) {
// empty line output on stderr => ignore
return null;
} else if (!this.filterStderr(errorMessage)) {
// filter indicated a non-error => ignore
return null;
}
return errorMessage;
};
this.process.stderr.on('data', (line) => {
if (this.errorline !== null) {
// ignore
} else if ((this.errorline = decodeStderr(line)) !== null) {
this.hasErrored = true;
}
});
this.process.stdin.on('error', stdinError);
this.process.stdout
.on('error', stdoutError)
.on('readable', () => {
if (this.hasEnded) {
return;
}
this.hasOutput = true;
let chunk;
while ((chunk = this.process.stdout.read()) !== null) {
this.push(chunk);
}
})
.on('end', () => {
if (!this.hasEnded) {
if (this.process) {
// stdout closed before the process exited
// therefore record a marker that this is the case
// and wait until the process itself shuts downs
this.logger('STDOUT: early close');
this.hasEnded = true;
} else {
this.__error('stream ended outputting prematurely');
}
}
});
// exit
this.process.on('error', (err) => {
if (err.code === 'ENOENT') {
this.errorline = err.message;
}
processError('unable to execute binary');
});
this.process.on('exit', (exitCode) => {
this.process = null;
if (this.hasErrored || exitCode === null) {
exitCode = -1;
this.hasErrored = false; // force error handling to occur
}
this.logger(`PROCESS: exit code ${0} and hasEnded=${this.hasEnded}`);
if (exitCode !== 0) {
processError(`exited with code ${exitCode}`);
} else if (this.__cleanUp()) {
this.logger('PROCESS: pushing final chunk on exit code 0');
this.push(null);
}
});
};
try {
const spawned = this.__spawnBinary(this.args);
onSpawned(spawned);
} catch (err) {
this.__error(err);
}
}
__spawnBinary(spawnArgs) {
if (this.binary) {
return childProcess.spawn(this.binary, spawnArgs);
} else {
throw new Error('Unable to locate the gifsicle binary file.');
}
}
// implementation stream methods
_transform(chunk, encoding, cb) {
if (this.hasEnded) return;
if (!this.process) this.__execCommand();
this.process.stdin.write(chunk);
cb();
}
_destroy() {
if (this.hasEnded) return;
this.hasEnded = true;
this.hasOutput = true; // set condition for reentrancy
this.__cleanUp();
}
// implementation hook methods
end(chunk) {
if (chunk !== undefined) {
this.write(chunk);
}
if (this.process) {
this.process.stdin.end();
} else {
this.__cleanUp();
}
}
on(type, callback) {
if (type === 'data') {
type = 'readable';
callback = ((ondata) => {
return function onreadable() {
let chunk;
while ((chunk = this.read()) !== null) {
ondata(chunk);
}
};
})(callback);
}
Stream.Readable.prototype.on.call(this, type, callback);
return this;
}
pause() {
if (this.process) {
this.process.stdout.pause();
}
this.isPaused = true;
}
resume() {
if (this.process) {
this.process.stdout.resume();
}
this.isPaused = false;
}
};