electron/spec-main/node-spec.ts

207 lines
6.9 KiB
TypeScript
Raw Normal View History

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'
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)
})
})
})
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()
}
})
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 = ''
function cleanup () {
child.stderr.removeListener('data', errorDataListener)
child.stdout.removeListener('data', outDataHandler)
}
function errorDataListener (data: Buffer) {
output += data
refactor: use v8 serialization for ipc (#20214) * refactor: use v8 serialization for ipc * cloning process.env doesn't work * serialize host objects by enumerating key/values * new serialization can handle NaN, Infinity, and undefined correctly * can't allocate v8 objects during GC * backport microtasks fix * fix compile * fix node_stream_loader reentrancy * update subframe spec to expect undefined instead of null * write undefined instead of crashing when serializing host objects * fix webview spec * fix download spec * buffers are transformed into uint8arrays * can't serialize promises * fix chrome.i18n.getMessage * fix devtools tests * fix zoom test * fix debug build * fix lint * update ipcRenderer tests * fix printToPDF test * update patch * remove accidentally re-added remote-side spec * wip * don't attempt to serialize host objects * jump through different hoops to set options.webContents sometimes * whoops * fix lint * clean up error-handling logic * fix memory leak * fix lint * convert host objects using old base::Value serialization * fix lint more * fall back to base::Value-based serialization * remove commented-out code * add docs to breaking-changes.md * Update breaking-changes.md * update ipcRenderer and WebContents docs * lint * use named values for format tag * save a memcpy for ~30% speedup * get rid of calls to ShallowClone * extra debugging for paranoia * d'oh, use the correct named tags * apparently msstl doesn't like this DCHECK * funny story about that DCHECK * disable remote-related functions when enable_remote_module = false * nits * use EnableIf to disable remote methods in mojom * fix include * review comments
2019-10-09 17:59:08 +00:00
if (/^Debugger listening on ws:/m.test(output)) {
cleanup()
done()
}
}
function outDataHandler (data: Buffer) {
cleanup()
done(new Error(`Unexpected output: ${data.toString()}`))
}
child.stderr.on('data', errorDataListener)
child.stdout.on('data', outDataHandler)
})
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 = ''
function cleanup () {
child.stderr.removeListener('data', errorDataListener)
child.stdout.removeListener('data', outDataHandler)
}
function errorDataListener (data: Buffer) {
output += data
refactor: use v8 serialization for ipc (#20214) * refactor: use v8 serialization for ipc * cloning process.env doesn't work * serialize host objects by enumerating key/values * new serialization can handle NaN, Infinity, and undefined correctly * can't allocate v8 objects during GC * backport microtasks fix * fix compile * fix node_stream_loader reentrancy * update subframe spec to expect undefined instead of null * write undefined instead of crashing when serializing host objects * fix webview spec * fix download spec * buffers are transformed into uint8arrays * can't serialize promises * fix chrome.i18n.getMessage * fix devtools tests * fix zoom test * fix debug build * fix lint * update ipcRenderer tests * fix printToPDF test * update patch * remove accidentally re-added remote-side spec * wip * don't attempt to serialize host objects * jump through different hoops to set options.webContents sometimes * whoops * fix lint * clean up error-handling logic * fix memory leak * fix lint * convert host objects using old base::Value serialization * fix lint more * fall back to base::Value-based serialization * remove commented-out code * add docs to breaking-changes.md * Update breaking-changes.md * update ipcRenderer and WebContents docs * lint * use named values for format tag * save a memcpy for ~30% speedup * get rid of calls to ShallowClone * extra debugging for paranoia * d'oh, use the correct named tags * apparently msstl doesn't like this DCHECK * funny story about that DCHECK * disable remote-related functions when enable_remote_module = false * nits * use EnableIf to disable remote methods in mojom * fix include * review comments
2019-10-09 17:59:08 +00:00
if (/^Debugger listening on ws:/m.test(output)) {
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364')
cleanup()
done()
}
}
function outDataHandler (data: Buffer) {
cleanup()
done(new Error(`Unexpected output: ${data.toString()}`))
}
child.stderr.on('data', errorDataListener)
child.stdout.on('data', outDataHandler)
})
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 = ''
function dataListener (data: Buffer) {
output += data
}
child.stderr.on('data', dataListener)
child.stdout.on('data', dataListener)
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 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')
let output = ''
function dataListener (data: Buffer) {
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]) {
child.stderr.removeListener('data', dataListener)
child.stdout.removeListener('data', dataListener)
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')
done()
})
}
}
}
child.stderr.on('data', dataListener)
child.stdout.on('data', dataListener)
})
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)
})
refactor: use v8 serialization for ipc (#20214) * refactor: use v8 serialization for ipc * cloning process.env doesn't work * serialize host objects by enumerating key/values * new serialization can handle NaN, Infinity, and undefined correctly * can't allocate v8 objects during GC * backport microtasks fix * fix compile * fix node_stream_loader reentrancy * update subframe spec to expect undefined instead of null * write undefined instead of crashing when serializing host objects * fix webview spec * fix download spec * buffers are transformed into uint8arrays * can't serialize promises * fix chrome.i18n.getMessage * fix devtools tests * fix zoom test * fix debug build * fix lint * update ipcRenderer tests * fix printToPDF test * update patch * remove accidentally re-added remote-side spec * wip * don't attempt to serialize host objects * jump through different hoops to set options.webContents sometimes * whoops * fix lint * clean up error-handling logic * fix memory leak * fix lint * convert host objects using old base::Value serialization * fix lint more * fall back to base::Value-based serialization * remove commented-out code * add docs to breaking-changes.md * Update breaking-changes.md * update ipcRenderer and WebContents docs * lint * use named values for format tag * save a memcpy for ~30% speedup * get rid of calls to ShallowClone * extra debugging for paranoia * d'oh, use the correct named tags * apparently msstl doesn't like this DCHECK * funny story about that DCHECK * disable remote-related functions when enable_remote_module = false * nits * use EnableIf to disable remote methods in mojom * fix include * review comments
2019-10-09 17:59:08 +00:00
})