forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-spec.ts
340 lines (294 loc) · 11 KB
/
node-spec.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
import { expect } from 'chai';
import * as childProcess from 'child_process';
import * as path from 'path';
import * as util from 'util';
import { emittedOnce } from './events-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import { webContents, WebContents } from 'electron/main';
const features = process.electronBinding('features');
describe('node feature', () => {
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
describe('child_process', () => {
describe('child_process.fork', () => {
it('Works in browser process', (done) => {
const child = childProcess.fork(path.join(fixtures, 'module', 'ping.js'));
child.on('message', (msg) => {
expect(msg).to.equal('message');
done();
});
child.send('message');
});
});
});
describe('contexts', () => {
describe('setTimeout called under Chromium event loop in browser process', () => {
it('Can be scheduled in time', (done) => {
setTimeout(done, 0);
});
it('Can be promisified', (done) => {
util.promisify(setTimeout)(0).then(done);
});
});
describe('setInterval called under Chromium event loop in browser process', () => {
it('can be scheduled in time', (done) => {
let interval: any = null;
let clearing = false;
const clear = () => {
if (interval === null || clearing) return;
// interval might trigger while clearing (remote is slow sometimes)
clearing = true;
clearInterval(interval);
clearing = false;
interval = null;
done();
};
interval = setInterval(clear, 10);
});
});
});
describe('NODE_OPTIONS', () => {
let child: childProcess.ChildProcessWithoutNullStreams;
let exitPromise: Promise<any[]>;
it('Fails for options disallowed by Node.js itself', (done) => {
after(async () => {
const [code, signal] = await exitPromise;
expect(signal).to.equal(null);
// Exit code 9 indicates cli flag parsing failure
expect(code).to.equal(9);
child.kill();
});
const env = Object.assign({}, process.env, { NODE_OPTIONS: '--v8-options' });
child = childProcess.spawn(process.execPath, { env });
exitPromise = emittedOnce(child, 'exit');
let output = '';
let success = false;
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
const listener = (data: Buffer) => {
output += data;
if (/electron: --v8-options is not allowed in NODE_OPTIONS/m.test(output)) {
success = true;
cleanup();
done();
}
};
child.stderr.on('data', listener);
child.stdout.on('data', listener);
child.on('exit', () => {
if (!success) {
cleanup();
done(new Error(`Unexpected output: ${output.toString()}`));
}
});
});
it('Disallows crypto-related options', (done) => {
after(() => {
child.kill();
});
const env = Object.assign({}, process.env, { NODE_OPTIONS: '--use-openssl-ca' });
child = childProcess.spawn(process.execPath, ['--enable-logging'], { env });
let output = '';
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
const listener = (data: Buffer) => {
output += data;
if (/The NODE_OPTION --use-openssl-ca is not supported in Electron/m.test(output)) {
cleanup();
done();
}
};
child.stderr.on('data', listener);
child.stdout.on('data', listener);
});
});
describe('Node.js cli flags', () => {
let child: childProcess.ChildProcessWithoutNullStreams;
let exitPromise: Promise<any[]>;
ifit(features.isRunAsNodeEnabled())('Prohibits crypto-related flags in ELECTRON_RUN_AS_NODE mode', (done) => {
after(async () => {
const [code, signal] = await exitPromise;
expect(signal).to.equal(null);
expect(code).to.equal(9);
child.kill();
});
child = childProcess.spawn(process.execPath, ['--force-fips'], {
env: { ELECTRON_RUN_AS_NODE: 'true' }
});
exitPromise = emittedOnce(child, 'exit');
let output = '';
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
const listener = (data: Buffer) => {
output += data;
if (/.*The Node.js cli flag --force-fips is not supported in Electron/m.test(output)) {
cleanup();
done();
}
};
child.stderr.on('data', listener);
child.stdout.on('data', listener);
});
});
ifdescribe(features.isRunAsNodeEnabled())('inspector', () => {
let child: childProcess.ChildProcessWithoutNullStreams;
let exitPromise: Promise<any[]>;
afterEach(async () => {
if (child && exitPromise) {
const [code, signal] = await exitPromise;
expect(signal).to.equal(null);
expect(code).to.equal(0);
} else if (child) {
child.kill();
}
child = null as any;
exitPromise = null as any;
});
it('Supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
child = childProcess.spawn(process.execPath, ['--inspect-brk', path.join(fixtures, 'module', 'run-as-node.js')], {
env: { ELECTRON_RUN_AS_NODE: 'true' }
});
let output = '';
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
const listener = (data: Buffer) => {
output += data;
if (/Debugger listening on ws:/m.test(output)) {
cleanup();
done();
}
};
child.stderr.on('data', listener);
child.stdout.on('data', listener);
});
it('Supports starting the v8 inspector with --inspect and a provided port', (done) => {
child = childProcess.spawn(process.execPath, ['--inspect=17364', path.join(fixtures, 'module', 'run-as-node.js')], {
env: { ELECTRON_RUN_AS_NODE: 'true' }
});
exitPromise = emittedOnce(child, 'exit');
let output = '';
const listener = (data: Buffer) => { output += data; };
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
child.stderr.on('data', listener);
child.stdout.on('data', listener);
child.on('exit', () => {
cleanup();
if (/^Debugger listening on ws:/m.test(output)) {
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364');
done();
} else {
done(new Error(`Unexpected output: ${output.toString()}`));
}
});
});
it('Does not start the v8 inspector when --inspect is after a -- argument', (done) => {
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'noop.js'), '--', '--inspect']);
exitPromise = emittedOnce(child, 'exit');
let output = '';
const listener = (data: Buffer) => { output += data; };
child.stderr.on('data', listener);
child.stdout.on('data', listener);
child.on('exit', () => {
if (output.trim().startsWith('Debugger listening on ws://')) {
done(new Error('Inspector was started when it should not have been'));
} else {
done();
}
});
});
// IPC Electron child process not supported on Windows
ifit(process.platform !== 'win32')('does not crash when quitting with the inspector connected', function (done) {
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'delay-exit'), '--inspect=0'], {
stdio: ['ipc']
}) as childProcess.ChildProcessWithoutNullStreams;
exitPromise = emittedOnce(child, 'exit');
const cleanup = () => {
child.stderr.removeListener('data', listener);
child.stdout.removeListener('data', listener);
};
let output = '';
const success = false;
function listener (data: Buffer) {
output += data;
console.log(data.toString()); // NOTE: temporary debug logging to try to catch flake.
const match = /^Debugger listening on (ws:\/\/.+:\d+\/.+)\n/m.exec(output.trim());
if (match) {
cleanup();
// NOTE: temporary debug logging to try to catch flake.
child.stderr.on('data', (m) => console.log(m.toString()));
child.stdout.on('data', (m) => console.log(m.toString()));
const w = (webContents as any).create({}) as WebContents;
w.loadURL('about:blank')
.then(() => w.executeJavaScript(`new Promise(resolve => {
const connection = new WebSocket(${JSON.stringify(match[1])})
connection.onopen = () => {
connection.onclose = () => resolve()
connection.close()
}
})`))
.then(() => {
(w as any).destroy();
child.send('plz-quit');
done();
});
}
}
child.stderr.on('data', listener);
child.stdout.on('data', listener);
child.on('exit', () => {
if (!success) cleanup();
});
});
it('Supports js binding', (done) => {
child = childProcess.spawn(process.execPath, ['--inspect', path.join(fixtures, 'module', 'inspector-binding.js')], {
env: { ELECTRON_RUN_AS_NODE: 'true' },
stdio: ['ipc']
}) as childProcess.ChildProcessWithoutNullStreams;
exitPromise = emittedOnce(child, 'exit');
child.on('message', ({ cmd, debuggerEnabled, success }) => {
if (cmd === 'assert') {
expect(debuggerEnabled).to.be.true();
expect(success).to.be.true();
done();
}
});
});
});
it('Can find a module using a package.json main field', () => {
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]);
expect(result.status).to.equal(0);
});
ifit(features.isRunAsNodeEnabled())('handles Promise timeouts correctly', (done) => {
const scriptPath = path.join(fixtures, 'module', 'node-promise-timer.js');
const child = childProcess.spawn(process.execPath, [scriptPath], {
env: { ELECTRON_RUN_AS_NODE: 'true' }
});
emittedOnce(child, 'exit').then(([code, signal]) => {
expect(code).to.equal(0);
expect(signal).to.equal(null);
child.kill();
done();
});
});
it('performs microtask checkpoint correctly', (done) => {
const f3 = async () => {
return new Promise((resolve, reject) => {
reject(new Error('oops'));
});
};
process.once('unhandledRejection', () => done('catch block is delayed to next tick'));
setTimeout(() => {
f3().catch(() => done());
});
});
});