2019-09-20 14:41:40 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import * as childProcess from 'child_process'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as util from 'util'
|
2019-11-01 20:37:02 +00:00
|
|
|
import { emittedOnce } from './events-helpers'
|
|
|
|
import { ifdescribe, ifit } from './spec-helpers'
|
|
|
|
import { webContents, WebContents } from 'electron'
|
2019-09-20 14:41:40 +00:00
|
|
|
|
|
|
|
const features = process.electronBinding('features')
|
|
|
|
|
|
|
|
describe('node feature', () => {
|
|
|
|
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures')
|
|
|
|
describe('child_process', () => {
|
|
|
|
describe('child_process.fork', () => {
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Works in browser process', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
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', () => {
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Can be scheduled in time', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
setTimeout(done, 0)
|
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Can be promisified', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
describe('NODE_OPTIONS', () => {
|
|
|
|
let child: childProcess.ChildProcessWithoutNullStreams
|
|
|
|
let exitPromise: Promise<any[]>
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Fails for options disallowed by Node.js itself', (done) => {
|
|
|
|
after(async () => {
|
2019-11-02 01:06:15 +00:00
|
|
|
const [code, signal] = await exitPromise
|
|
|
|
expect(signal).to.equal(null)
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
// Exit code 9 indicates cli flag parsing failure
|
|
|
|
expect(code).to.equal(9)
|
2019-11-02 01:06:15 +00:00
|
|
|
child.kill()
|
2020-02-07 02:59:38 +00:00
|
|
|
})
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2019-11-03 18:46:12 +00:00
|
|
|
const env = Object.assign({}, process.env, { NODE_OPTIONS: '--v8-options' })
|
2019-11-02 01:06:15 +00:00
|
|
|
child = childProcess.spawn(process.execPath, { env })
|
2020-02-07 02:59:38 +00:00
|
|
|
exitPromise = emittedOnce(child, 'exit')
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
let output = ''
|
|
|
|
let success = false
|
|
|
|
const cleanup = () => {
|
2019-11-02 01:06:15 +00:00
|
|
|
child.stderr.removeListener('data', listener)
|
|
|
|
child.stdout.removeListener('data', listener)
|
|
|
|
}
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => {
|
2019-11-02 01:06:15 +00:00
|
|
|
output += data
|
|
|
|
if (/electron: --v8-options is not allowed in NODE_OPTIONS/m.test(output)) {
|
2020-02-07 02:59:38 +00:00
|
|
|
success = true
|
2019-11-02 01:06:15 +00:00
|
|
|
cleanup()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
2020-02-07 02:59:38 +00:00
|
|
|
child.on('exit', () => {
|
|
|
|
if (!success) {
|
|
|
|
cleanup()
|
|
|
|
done(new Error(`Unexpected output: ${output.toString()}`))
|
|
|
|
}
|
|
|
|
})
|
2019-11-02 01:06:15 +00:00
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Disallows crypto-related options', (done) => {
|
|
|
|
after(() => {
|
|
|
|
child.kill()
|
|
|
|
})
|
|
|
|
|
2019-11-03 18:46:12 +00:00
|
|
|
const env = Object.assign({}, process.env, { NODE_OPTIONS: '--use-openssl-ca' })
|
2019-11-02 01:06:15 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--enable-logging'], { env })
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
let output = ''
|
|
|
|
const cleanup = () => {
|
2019-11-02 01:06:15 +00:00
|
|
|
child.stderr.removeListener('data', listener)
|
|
|
|
child.stdout.removeListener('data', listener)
|
|
|
|
}
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => {
|
2019-11-02 01:06:15 +00:00
|
|
|
output += data
|
|
|
|
if (/The NODE_OPTION --use-openssl-ca is not supported in Electron/m.test(output)) {
|
|
|
|
cleanup()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Node.js cli flags', () => {
|
|
|
|
let child: childProcess.ChildProcessWithoutNullStreams
|
|
|
|
let exitPromise: Promise<any[]>
|
|
|
|
|
|
|
|
it('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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-20 14:41:40 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect-brk', path.join(fixtures, 'module', 'run-as-node.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
2019-09-20 14:41:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
let output = ''
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
|
|
|
child.stderr.removeListener('data', listener)
|
|
|
|
child.stdout.removeListener('data', listener)
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
const listener = (data: Buffer) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
output += data
|
2020-02-07 02:59:38 +00:00
|
|
|
if (/Debugger listening on ws:/m.test(output)) {
|
2019-09-20 14:41:40 +00:00
|
|
|
cleanup()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
2019-09-20 14:41:40 +00:00
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Supports starting the v8 inspector with --inspect and a provided port', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect=17364', path.join(fixtures, 'module', 'run-as-node.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
2019-09-20 14:41:40 +00:00
|
|
|
})
|
|
|
|
exitPromise = emittedOnce(child, 'exit')
|
|
|
|
|
|
|
|
let output = ''
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => { output += data }
|
|
|
|
const cleanup = () => {
|
|
|
|
child.stderr.removeListener('data', listener)
|
|
|
|
child.stdout.removeListener('data', listener)
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
|
|
|
child.on('exit', () => {
|
|
|
|
cleanup()
|
2019-10-09 17:59:08 +00:00
|
|
|
if (/^Debugger listening on ws:/m.test(output)) {
|
2019-09-20 14:41:40 +00:00
|
|
|
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364')
|
|
|
|
done()
|
2020-02-07 02:59:38 +00:00
|
|
|
} else {
|
|
|
|
done(new Error(`Unexpected output: ${output.toString()}`))
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
})
|
2019-09-20 14:41:40 +00:00
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Does not start the v8 inspector when --inspect is after a -- argument', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'noop.js'), '--', '--inspect'])
|
|
|
|
exitPromise = emittedOnce(child, 'exit')
|
|
|
|
|
|
|
|
let output = ''
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => { output += data }
|
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
2019-09-20 14:41:40 +00:00
|
|
|
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
|
2020-02-07 02:59:38 +00:00
|
|
|
ifit(process.platform !== 'win32')('Does does not crash when quitting with the inspector connected', function (done) {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'delay-exit'), '--inspect=0'], {
|
|
|
|
stdio: ['ipc']
|
|
|
|
}) as childProcess.ChildProcessWithoutNullStreams
|
|
|
|
exitPromise = emittedOnce(child, 'exit')
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
|
|
|
child.stderr.removeListener('data', listener)
|
|
|
|
child.stdout.removeListener('data', listener)
|
|
|
|
}
|
|
|
|
|
2019-09-20 14:41:40 +00:00
|
|
|
let output = ''
|
2020-02-07 02:59:38 +00:00
|
|
|
let success = false
|
|
|
|
function listener (data: Buffer) {
|
2019-09-20 14:41:40 +00:00
|
|
|
output += data
|
|
|
|
if (output.trim().indexOf('Debugger listening on ws://') > -1 && output.indexOf('\n') > -1) {
|
|
|
|
const socketMatch = output.trim().match(/(ws:\/\/.+:[0-9]+\/.+?)\n/gm)
|
|
|
|
if (socketMatch && socketMatch[0]) {
|
|
|
|
const w = (webContents as any).create({}) as WebContents
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
.then(() => w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
const connection = new WebSocket(${JSON.stringify(socketMatch[0])})
|
|
|
|
connection.onopen = () => {
|
|
|
|
resolve()
|
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
})`))
|
|
|
|
.then(() => {
|
|
|
|
(w as any).destroy()
|
|
|
|
child.send('plz-quit')
|
2020-02-07 02:59:38 +00:00
|
|
|
success = true
|
|
|
|
cleanup()
|
2019-09-20 14:41:40 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
child.stderr.on('data', listener)
|
|
|
|
child.stdout.on('data', listener)
|
|
|
|
child.on('exit', () => {
|
|
|
|
if (!success) cleanup()
|
|
|
|
})
|
2019-09-20 14:41:40 +00:00
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Supports js binding', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect', path.join(fixtures, 'module', 'inspector-binding.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' },
|
2019-09-20 14:41:40 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Can find a module using a package.json main field', () => {
|
2019-09-20 14:41:40 +00:00
|
|
|
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')])
|
|
|
|
expect(result.status).to.equal(0)
|
|
|
|
})
|
2019-10-09 17:59:08 +00:00
|
|
|
})
|