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 fileTake it
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–3why, one step back1import 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 back4import {execa} from '../../index.js';5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';6import {foobarString} from '../helpers/input.js';78setFixtureDirectory();9TweaksSindre Sorhusbefore adoption698a1b8
line 10why, one step back10const testRequiredIpcSubprocess = async (methodName, options) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 11why, one step back11 const subprocess = execa('empty.js', options);TweaksSindre Sorhusbefore adoption698a1b8
lines 12–13why, one step back12 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 back14 await subprocess;15};16TweaksSindre Sorhusbefore adoption698a1b8
lines 17–22why, one step back17test('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 back23TweaksSindre Sorhusbefore adoption698a1b8
lines 24–26why, one step back24const 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 back27};28TweaksSindre Sorhusbefore adoption698a1b8
lines 29–34why, one step back29test('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 back35TweaksSindre Sorhusbefore adoption698a1b8
line 36why, one step back36const testPostDisconnection = async methodName => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 37–38why, one step back37 const subprocess = execa('empty.js', {ipc: true});38 await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 39–40why, one step back39 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 back41};42TweaksSindre Sorhusbefore adoption698a1b8
lines 43–45why, one step back43test('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 back46TweaksSindre Sorhusbefore adoption698a1b8
line 47why, one step back47const testPostDisconnectionSubprocess = async methodName => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 48why, one step back48 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 back49 subprocess.nodeChildProcess.disconnect();TweaksSindre Sorhusbefore adoption698a1b8
lines 50–51why, one step back50 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 back52};53TweaksSindre Sorhusbefore adoption698a1b8
lines 54–56why, one step back54test('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 back57Improve IPC error messages (#1090)ehmickybefore adoptione6b012a
lines 58–63why, one step back58const 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';63TweaksSindre Sorhusbefore adoption698a1b8
line 64why, one step back64const testInvalidPayload = async (serialization, message, expectedMessage) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 65why, one step back65 const subprocess = execa('empty.js', {ipc: true, serialization});TweaksSindre Sorhusbefore adoption698a1b8
lines 66–68why, one step back66 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 back69 await subprocess;70};7172const cycleObject = {};73cycleObject.self = cycleObject;74const toJsonCycle = {toJSON: () => ({test: true, toJsonCycle})};75TweaksSindre Sorhusbefore adoption698a1b8
lines 76–88why, one step back76test('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 back89Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 90–103why, one step back90// Those messages cannot be converted to a string either91const 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));103TweaksSindre Sorhusbefore adoption698a1b8
line 104why, one step back104test('exports.sendMessage() validates payload', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 105why, one step back105 const subprocess = execa('ipc-echo-item.js', {ipc: true});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 106why, one step back106 await subprocess.sendMessage([undefined]);TweaksSindre Sorhusbefore adoption698a1b8
lines 107–109why, one step back107 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 back110});