Skip to content
Myr

validation.js

This file

Path

test/ipc/validation.js

110 lines · 6.9 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertThrows, assertRejects} from '../helpers/assert.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 4–9why, one step back
4import {execa} from '../../index.js';
5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';6import {foobarString} from '../helpers/input.js';78setFixtureDirectory();9

TweaksSindre Sorhusbefore adoption698a1b8

line 10why, one step back
10const testRequiredIpcSubprocess = async (methodName, options) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 11why, one step back
11 const subprocess = execa('empty.js', options);

TweaksSindre Sorhusbefore adoption698a1b8

lines 12–13why, one step back
12 const {message} = await assertThrows(() => subprocess[methodName]());
13 assert.ok(message.includes(`subprocess.${methodName}() can only be used`));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 14–16why, one step back
14 await subprocess;
15};16

TweaksSindre Sorhusbefore adoption698a1b8

lines 17–22why, one step back
17test('Cannot use subprocess.sendMessage() without ipc option', () => testRequiredIpcSubprocess('sendMessage', {}));
18test('Cannot use subprocess.sendMessage() with ipc: false', () => testRequiredIpcSubprocess('sendMessage', {ipc: false}));19test('Cannot use subprocess.getOneMessage() without ipc option', () => testRequiredIpcSubprocess('getOneMessage', {}));20test('Cannot use subprocess.getOneMessage() with ipc: false', () => testRequiredIpcSubprocess('getOneMessage', {ipc: false}));21test('Cannot use subprocess.getEachMessage() without ipc option', () => testRequiredIpcSubprocess('getEachMessage', {}));22test('Cannot use subprocess.getEachMessage() with ipc: false', () => testRequiredIpcSubprocess('getEachMessage', {ipc: false}));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 23why, one step back
23

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–26why, one step back
24const testRequiredIpcExports = async (methodName, options) => {
25 const {message} = await assertRejects(execa('ipc-any.js', [methodName], options));26 assert.ok(message.includes(`${methodName}() can only be used`));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 27–28why, one step back
27};
28

TweaksSindre Sorhusbefore adoption698a1b8

lines 29–34why, one step back
29test('Cannot use exports.sendMessage() without ipc option', () => testRequiredIpcExports('sendMessage', {}));
30test('Cannot use exports.sendMessage() with ipc: false', () => testRequiredIpcExports('sendMessage', {ipc: false}));31test('Cannot use exports.getOneMessage() without ipc option', () => testRequiredIpcExports('getOneMessage', {}));32test('Cannot use exports.getOneMessage() with ipc: false', () => testRequiredIpcExports('getOneMessage', {ipc: false}));33test('Cannot use exports.getEachMessage() without ipc option', () => testRequiredIpcExports('getEachMessage', {}));34test('Cannot use exports.getEachMessage() with ipc: false', () => testRequiredIpcExports('getEachMessage', {ipc: false}));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 35why, one step back
35

TweaksSindre Sorhusbefore adoption698a1b8

line 36why, one step back
36const testPostDisconnection = async methodName => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 37–38why, one step back
37 const subprocess = execa('empty.js', {ipc: true});
38 await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

lines 39–40why, one step back
39 const {message} = assertThrows(() => subprocess[methodName](foobarString));
40 assert.ok(message.includes(`subprocess.${methodName}() cannot be used`));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 41–42why, one step back
41};
42

TweaksSindre Sorhusbefore adoption698a1b8

lines 43–45why, one step back
43test('subprocess.sendMessage() after disconnection', () => testPostDisconnection('sendMessage'));
44test('subprocess.getOneMessage() after disconnection', () => testPostDisconnection('getOneMessage'));45test('subprocess.getEachMessage() after disconnection', () => testPostDisconnection('getEachMessage'));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 46why, one step back
46

TweaksSindre Sorhusbefore adoption698a1b8

line 47why, one step back
47const testPostDisconnectionSubprocess = async methodName => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 48why, one step back
48 const subprocess = execa('ipc-disconnect.js', [methodName], {ipc: true});

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 49why, one step back
49 subprocess.nodeChildProcess.disconnect();

TweaksSindre Sorhusbefore adoption698a1b8

lines 50–51why, one step back
50 const {message} = await assertRejects(subprocess);
51 assert.ok(message.includes(`${methodName}() cannot be used`));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 52–53why, one step back
52};
53

TweaksSindre Sorhusbefore adoption698a1b8

lines 54–56why, one step back
54test('exports.sendMessage() after disconnection', () => testPostDisconnectionSubprocess('sendMessage'));
55test('exports.getOneMessage() after disconnection', () => testPostDisconnectionSubprocess('getOneMessage'));56test('exports.getEachMessage() after disconnection', () => testPostDisconnectionSubprocess('getEachMessage'));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 57why, one step back
57

Improve IPC error messages (#1090)ehmickybefore adoptione6b012a

lines 58–63why, one step back
58const INVALID_TYPE_MESSAGE = 'The "message" argument must be one of type string';
59const UNDEFINED_MESSAGE = 'The "message" argument must be specified';60const CLONE_MESSAGE = 'could not be cloned';61const CYCLE_MESSAGE = 'Converting circular structure to JSON';62const MAX_CALL_STACK_MESSAGE = 'Maximum call stack size exceeded';63

TweaksSindre Sorhusbefore adoption698a1b8

line 64why, one step back
64const testInvalidPayload = async (serialization, message, expectedMessage) => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 65why, one step back
65 const subprocess = execa('empty.js', {ipc: true, serialization});

TweaksSindre Sorhusbefore adoption698a1b8

lines 66–68why, one step back
66 const error = await assertRejects(subprocess.sendMessage(message));
67 assert.ok(error.message.includes('subprocess.sendMessage()\'s argument type is invalid: the message cannot be serialized'));68 assert.ok(error.cause.message.includes(expectedMessage));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 69–75why, one step back
69 await subprocess;
70};7172const cycleObject = {};73cycleObject.self = cycleObject;74const toJsonCycle = {toJSON: () => ({test: true, toJsonCycle})};75

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–88why, one step back
76test('subprocess.sendMessage() cannot send undefined', () => testInvalidPayload('advanced', undefined, UNDEFINED_MESSAGE));
77test('subprocess.sendMessage() cannot send bigints', () => testInvalidPayload('advanced', 0n, INVALID_TYPE_MESSAGE));78test('subprocess.sendMessage() cannot send symbols', () => testInvalidPayload('advanced', Symbol('test'), INVALID_TYPE_MESSAGE));79test('subprocess.sendMessage() cannot send functions', () => testInvalidPayload('advanced', () => {}, INVALID_TYPE_MESSAGE));80test('subprocess.sendMessage() cannot send promises', () => testInvalidPayload('advanced', Promise.resolve(), CLONE_MESSAGE));81test('subprocess.sendMessage() cannot send proxies', () => testInvalidPayload('advanced', new Proxy({}, {}), CLONE_MESSAGE));82test('subprocess.sendMessage() cannot send Intl', () => testInvalidPayload('advanced', new Intl.Collator(), CLONE_MESSAGE));83test('subprocess.sendMessage() cannot send undefined, JSON', () => testInvalidPayload('json', undefined, UNDEFINED_MESSAGE));84test('subprocess.sendMessage() cannot send bigints, JSON', () => testInvalidPayload('json', 0n, INVALID_TYPE_MESSAGE));85test('subprocess.sendMessage() cannot send symbols, JSON', () => testInvalidPayload('json', Symbol('test'), INVALID_TYPE_MESSAGE));86test('subprocess.sendMessage() cannot send functions, JSON', () => testInvalidPayload('json', () => {}, INVALID_TYPE_MESSAGE));87test('subprocess.sendMessage() cannot send cycles, JSON', () => testInvalidPayload('json', cycleObject, CYCLE_MESSAGE));88test('subprocess.sendMessage() cannot send cycles in toJSON(), JSON', () => testInvalidPayload('json', toJsonCycle, MAX_CALL_STACK_MESSAGE));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 89why, one step back
89

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 90–103why, one step back
90// Those messages cannot be converted to a string either
91const nullPrototypeCycle = {__proto__: null};92nullPrototypeCycle.self = nullPrototypeCycle;93const throwingToString = {94 callback() {},95 toString() {96 throw new Error('toString() failed');97 },98};99100test('subprocess.sendMessage() cannot send null-prototype objects with functions', () => testInvalidPayload('advanced', {__proto__: null, callback() {}}, CLONE_MESSAGE));101test('subprocess.sendMessage() cannot send objects with functions and a throwing toString()', () => testInvalidPayload('advanced', throwingToString, CLONE_MESSAGE));102test('subprocess.sendMessage() cannot send null-prototype cycles, JSON', () => testInvalidPayload('json', nullPrototypeCycle, CYCLE_MESSAGE));103

TweaksSindre Sorhusbefore adoption698a1b8

line 104why, one step back
104test('exports.sendMessage() validates payload', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 105why, one step back
105 const subprocess = execa('ipc-echo-item.js', {ipc: true});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 106why, one step back
106 await subprocess.sendMessage([undefined]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 107–109why, one step back
107 const {message} = await assertRejects(subprocess);
108 assert.ok(message.includes('Error: sendMessage()\'s argument type is invalid: the message cannot be serialized'));109 assert.ok(message.includes(UNDEFINED_MESSAGE));

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 110why, one step back
110});