2020-03-20 20:28:31 +00:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { expect } from 'chai';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow, ipcMain, IpcMainInvokeEvent, MessageChannelMain } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { closeAllWindows } from './window-helpers';
|
|
|
|
import { emittedOnce } from './events-helpers';
|
2020-03-11 01:16:58 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
describe('ipc module', () => {
|
|
|
|
describe('invoke', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w = (null as unknown as BrowserWindow);
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
after(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.destroy();
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
async function rendererInvoke (...args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-05-31 17:25:19 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
const result = await ipcRenderer.invoke('test', ...args);
|
|
|
|
ipcRenderer.send('result', { result });
|
2019-05-31 17:25:19 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('result', { error: e.message });
|
2019-05-31 17:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('receives a response from a synchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', (e: IpcMainInvokeEvent, arg: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg).to.equal(123);
|
|
|
|
return 3;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg).to.deep.equal({ result: 3 });
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('receives a response from an asynchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg).to.equal(123);
|
2020-05-20 20:18:48 +00:00
|
|
|
await new Promise(setImmediate);
|
2020-03-20 20:28:31 +00:00
|
|
|
return 3;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg).to.deep.equal({ result: 3 });
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('receives an error from a synchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('some error');
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg.error).to.match(/some error/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('receives an error from an asynchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', async () => {
|
2020-05-20 20:18:48 +00:00
|
|
|
await new Promise(setImmediate);
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('some error');
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg.error).to.match(/some error/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('throws an error if no handler is registered', async () => {
|
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg.error).to.match(/No handler registered/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('throws an error when invoking a handler that was removed', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.handle('test', () => {});
|
|
|
|
ipcMain.removeHandler('test');
|
2019-05-31 17:25:19 +00:00
|
|
|
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(arg.error).to.match(/No handler registered/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
it('forbids multiple handlers', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.handle('test', () => {});
|
2019-06-05 00:10:31 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(() => { ipcMain.handle('test', () => {}); }).to.throw(/second handler/);
|
2019-06-05 00:10:31 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.removeHandler('test');
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-11 01:16:58 +00:00
|
|
|
|
|
|
|
it('throws an error in the renderer if the reply callback is dropped', async () => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
ipcMain.handleOnce('test', () => new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
setTimeout(() => v8Util.requestGarbageCollectionForTesting());
|
2020-03-11 01:16:58 +00:00
|
|
|
/* never resolve */
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
|
|
|
w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
const [, { error }] = await emittedOnce(ipcMain, 'result');
|
|
|
|
expect(error).to.match(/reply was never sent/);
|
|
|
|
});
|
|
|
|
});
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
describe('ordering', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w = (null as unknown as BrowserWindow);
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
2019-06-05 00:10:31 +00:00
|
|
|
after(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.destroy();
|
|
|
|
});
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
it('between send and sendSync is consistent', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received: number[] = [];
|
|
|
|
ipcMain.on('test-async', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
|
|
|
|
const done = new Promise(resolve => ipcMain.once('done', () => { resolve(); }));
|
2019-11-01 20:37:02 +00:00
|
|
|
function rendererStressTest () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-11-01 20:37:02 +00:00
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
|
|
switch ((Math.random() * 2) | 0) {
|
|
|
|
case 0:
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('test-async', i);
|
|
|
|
break;
|
2019-11-01 20:37:02 +00:00
|
|
|
case 1:
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.sendSync('test-sync', i);
|
|
|
|
break;
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('done');
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.executeJavaScript(`(${rendererStressTest})()`);
|
|
|
|
await done;
|
2019-06-05 00:10:31 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.removeAllListeners('test-async');
|
|
|
|
ipcMain.removeAllListeners('test-sync');
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(received).to.have.lengthOf(1000);
|
|
|
|
expect(received).to.deep.equal([...received].sort((a, b) => a - b));
|
|
|
|
});
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
it('between send, sendSync, and invoke is consistent', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received: number[] = [];
|
|
|
|
ipcMain.handle('test-invoke', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-async', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
|
|
|
|
const done = new Promise(resolve => ipcMain.once('done', () => { resolve(); }));
|
2019-11-01 20:37:02 +00:00
|
|
|
function rendererStressTest () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-11-01 20:37:02 +00:00
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
|
|
switch ((Math.random() * 3) | 0) {
|
|
|
|
case 0:
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('test-async', i);
|
|
|
|
break;
|
2019-11-01 20:37:02 +00:00
|
|
|
case 1:
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.sendSync('test-sync', i);
|
|
|
|
break;
|
2019-11-01 20:37:02 +00:00
|
|
|
case 2:
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.invoke('test-invoke', i);
|
|
|
|
break;
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('done');
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.executeJavaScript(`(${rendererStressTest})()`);
|
|
|
|
await done;
|
2019-06-05 00:10:31 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.removeHandler('test-invoke');
|
|
|
|
ipcMain.removeAllListeners('test-async');
|
|
|
|
ipcMain.removeAllListeners('test-sync');
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(received).to.have.lengthOf(1000);
|
|
|
|
expect(received).to.deep.equal([...received].sort((a, b) => a - b));
|
|
|
|
});
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
describe('MessagePort', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(closeAllWindows);
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can send a port to the main process', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const p = emittedOnce(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const channel = new MessageChannel();
|
|
|
|
require('electron').ipcRenderer.postMessage('port', 'hi', [channel.port1]);
|
|
|
|
}})()`);
|
|
|
|
const [ev, msg] = await p;
|
|
|
|
expect(msg).to.equal('hi');
|
|
|
|
expect(ev.ports).to.have.length(1);
|
|
|
|
const [port] = ev.ports;
|
|
|
|
expect(port).to.be.an.instanceOf(EventEmitter);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can communicate between main and renderer', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const p = emittedOnce(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const channel = new MessageChannel();
|
|
|
|
(channel.port2 as any).onmessage = (ev: any) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
channel.port2.postMessage(ev.data * 2);
|
|
|
|
};
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
|
|
|
|
}})()`);
|
|
|
|
const [ev] = await p;
|
|
|
|
expect(ev.ports).to.have.length(1);
|
|
|
|
const [port] = ev.ports;
|
|
|
|
port.start();
|
|
|
|
port.postMessage(42);
|
|
|
|
const [ev2] = await emittedOnce(port, 'message');
|
|
|
|
expect(ev2.data).to.equal(84);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can receive a port from a renderer over a MessagePort connection', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
function fn () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const channel1 = new MessageChannel();
|
|
|
|
const channel2 = new MessageChannel();
|
|
|
|
channel1.port2.postMessage('', [channel2.port1]);
|
|
|
|
channel2.port2.postMessage('matryoshka');
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel1.port1]);
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.executeJavaScript(`(${fn})()`);
|
|
|
|
const [{ ports: [port1] }] = await emittedOnce(ipcMain, 'port');
|
|
|
|
port1.start();
|
|
|
|
const [{ ports: [port2] }] = await emittedOnce(port1, 'message');
|
|
|
|
port2.start();
|
|
|
|
const [{ data }] = await emittedOnce(port2, 'message');
|
|
|
|
expect(data).to.equal('matryoshka');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can forward a port from one renderer to another renderer', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w1 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
const w2 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w1.loadURL('about:blank');
|
|
|
|
w2.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
w1.webContents.executeJavaScript(`(${function () {
|
|
|
|
const channel = new MessageChannel();
|
|
|
|
(channel.port2 as any).onmessage = (ev: any) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
require('electron').ipcRenderer.send('message received', ev.data);
|
|
|
|
};
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
|
|
|
|
}})()`);
|
|
|
|
const [{ ports: [port] }] = await emittedOnce(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w2.webContents.executeJavaScript(`(${function () {
|
|
|
|
require('electron').ipcRenderer.on('port', ({ ports: [port] }: any) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
port.postMessage('a message');
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
w2.webContents.postMessage('port', '', [port]);
|
|
|
|
const [, data] = await emittedOnce(ipcMain, 'message received');
|
|
|
|
expect(data).to.equal('a message');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2020-03-13 17:00:50 +00:00
|
|
|
describe('close event', () => {
|
|
|
|
describe('in renderer', () => {
|
|
|
|
it('is emitted when the main process closes its end of the port', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-13 17:00:50 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2020-03-13 17:00:50 +00:00
|
|
|
ipcRenderer.on('port', (e) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [port] = e.ports;
|
2020-03-13 17:00:50 +00:00
|
|
|
port.start();
|
|
|
|
(port as any).onclose = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('closed');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
|
|
|
port1.close();
|
|
|
|
await emittedOnce(ipcMain, 'closed');
|
|
|
|
});
|
2020-03-13 17:00:50 +00:00
|
|
|
|
|
|
|
it('is emitted when the other end of a port is garbage-collected', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-13 17:00:50 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port2 } = new MessageChannel();
|
2020-03-13 17:00:50 +00:00
|
|
|
await new Promise(resolve => {
|
|
|
|
port2.start();
|
2020-03-20 20:28:31 +00:00
|
|
|
(port2 as any).onclose = resolve;
|
2020-06-23 03:32:45 +00:00
|
|
|
process._linkedBinding('electron_common_v8_util').requestGarbageCollectionForTesting();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
});
|
2020-03-13 17:00:50 +00:00
|
|
|
|
|
|
|
it('is emitted when the other end of a port is sent to nowhere', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
ipcMain.once('do-a-gc', () => v8Util.requestGarbageCollectionForTesting());
|
2020-03-13 17:00:50 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannel();
|
2020-03-13 17:00:50 +00:00
|
|
|
await new Promise(resolve => {
|
|
|
|
port2.start();
|
2020-03-20 20:28:31 +00:00
|
|
|
(port2 as any).onclose = resolve;
|
|
|
|
require('electron').ipcRenderer.postMessage('nobody-listening', null, [port1]);
|
|
|
|
require('electron').ipcRenderer.send('do-a-gc');
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-13 17:00:50 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
describe('MessageChannelMain', () => {
|
|
|
|
it('can be created', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
expect(port1).not.to.be.null();
|
|
|
|
expect(port2).not.to.be.null();
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can send messages within the process', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.postMessage('hello');
|
|
|
|
port1.start();
|
|
|
|
const [ev] = await emittedOnce(port1, 'message');
|
|
|
|
expect(ev.data).to.equal('hello');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can pass one end to a WebContents', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2020-03-12 01:07:54 +00:00
|
|
|
ipcRenderer.on('port', (ev) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [port] = ev.ports;
|
2020-03-12 01:07:54 +00:00
|
|
|
port.onmessage = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('done');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
|
|
|
await emittedOnce(ipcMain, 'done');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can be passed over another channel', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2020-03-12 01:07:54 +00:00
|
|
|
ipcRenderer.on('port', (e1) => {
|
|
|
|
e1.ports[0].onmessage = (e2) => {
|
|
|
|
e2.ports[0].onmessage = (e3) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('done', e3.data);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
const { port1: port3, port2: port4 } = new MessageChannelMain();
|
|
|
|
port1.postMessage(null, [port4]);
|
|
|
|
port3.postMessage('hello');
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
|
|
|
const [, message] = await emittedOnce(ipcMain, 'done');
|
|
|
|
expect(message).to.equal('hello');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can send messages to a closed port', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.start();
|
|
|
|
port2.on('message', () => { throw new Error('unexpected message received'); });
|
|
|
|
port1.close();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('can send messages to a port whose remote end is closed', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.start();
|
|
|
|
port2.on('message', () => { throw new Error('unexpected message received'); });
|
|
|
|
port2.close();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing null ports', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1 } = new MessageChannelMain();
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
port1.postMessage(null, [null] as any);
|
|
|
|
}).to.throw(/conversion failure/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing duplicate ports', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
const { port1: port3 } = new MessageChannelMain();
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
port1.postMessage(null, [port3, port3]);
|
|
|
|
}).to.throw(/duplicate/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing ports that have already been neutered', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
const { port1: port3 } = new MessageChannelMain();
|
|
|
|
port1.postMessage(null, [port3]);
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
port1.postMessage(null, [port3]);
|
|
|
|
}).to.throw(/already neutered/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing itself', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1 } = new MessageChannelMain();
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
port1.postMessage(null, [port1]);
|
|
|
|
}).to.throw(/contains the source port/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
describe('GC behavior', () => {
|
|
|
|
it('is not collected while it could still receive messages', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let trigger: Function;
|
|
|
|
const promise = new Promise(resolve => { trigger = resolve; });
|
2020-03-12 01:07:54 +00:00
|
|
|
const port1 = (() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
|
|
|
|
port2.on('message', (e) => { trigger(e.data); });
|
|
|
|
port2.start();
|
|
|
|
return port1;
|
|
|
|
})();
|
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
expect(await promise).to.equal('hello');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
describe('WebContents.postMessage', () => {
|
|
|
|
it('sends a message', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2020-03-12 01:07:54 +00:00
|
|
|
ipcRenderer.on('foo', (e, msg) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('bar', msg);
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
w.webContents.postMessage('foo', { some: 'message' });
|
|
|
|
const [, msg] = await emittedOnce(ipcMain, 'bar');
|
|
|
|
expect(msg).to.deep.equal({ some: 'message' });
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
describe('error handling', () => {
|
|
|
|
it('throws on missing channel', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(w.webContents.postMessage as any)();
|
|
|
|
}).to.throw(/Insufficient number of arguments/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws on invalid channel', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage(null as any, '', []);
|
|
|
|
}).to.throw(/Error processing argument at index 0/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws on missing message', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(w.webContents.postMessage as any)('channel');
|
|
|
|
}).to.throw(/Insufficient number of arguments/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws on non-serializable message', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage('channel', w);
|
|
|
|
}).to.throw(/An object could not be cloned/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws on invalid transferable list', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage('', '', null as any);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws on transferring non-transferable', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(w.webContents.postMessage as any)('channel', '', [123]);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing null ports', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage('foo', null, [null] as any);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing duplicate ports', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const { port1 } = new MessageChannelMain();
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage('foo', null, [port1, port1]);
|
|
|
|
}).to.throw(/duplicate/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
it('throws when passing ports that have already been neutered', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
w.webContents.postMessage('foo', null, [port1]);
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.postMessage('foo', null, [port1]);
|
|
|
|
}).to.throw(/already neutered/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|