Fix the `strict` and `ipcInput` options with the `serialization` option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 9547e51 · Browse files · 7 files·+87 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+87 added−8 removed
- tests:+77 added−4 removed
- other:+10 added−4 removed
Provenance
- Commit
9842f1a- Tree
896e144- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
9547e51
7 files changed+87 added−8 removed
modified:lib/ipc/incoming.js+2 added-1 removed
40 unmodified lines41424344454646474849505135 unmodified lines40 unmodified lines return; }
while (incomingMessages.length > 0) { // The message being processed is the oldest one, which is not `wrappedMessage` after the first iteration // eslint-disable-next-line no-await-in-loop await waitForOutgoingMessages(anyProcess, ipcEmitter, wrappedMessage); await waitForOutgoingMessages(anyProcess, ipcEmitter, incomingMessages[0]); // eslint-disable-next-line no-await-in-loop await scheduler.yield();
// eslint-disable-next-line no-await-in-loop35 unmodified linesmodified:lib/ipc/ipc-input.js+4 added-1 removed
23 unmodified lines2425262728282930313233343520 unmodified lines23 unmodified lines};
const validateJsonInput = ipcInput => { try { JSON.stringify(ipcInput); // `JSON.stringify()` returns `undefined` instead of throwing for values it cannot represent, such as functions and symbols if (JSON.stringify(ipcInput) === undefined) { throw new TypeError('The value cannot be represented as JSON.'); } } catch (error) { throw new Error('The `ipcInput` option is not serializable with JSON.', {cause: error}); }};20 unmodified linesmodified:lib/ipc/strict.js+2 added-1 removed
20 unmodified lines21 hasListeners,22 };23 };24 25+// This is a plain number, not a `BigInt`, since it must be serializable with `serialization: 'json'`25-let count = 0n;26+let count = 0;27 28 // Handles when both processes are calling `sendMessage()` with `strict` at the same time.29 // If neither process is listening, this would create a deadlock. We detect it and throw.30 export const validateStrictDeadlock = (outgoingMessages, wrappedMessage) => {84 unmodified linesmodified:lib/methods/main-sync.js+2 added-1 removed
50 unmodified lines51 const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;52 53 // Options validation logic specific to sync methods54 const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal, killDescendants}) => {55+ // Any value can be sent over IPC, including falsy ones, so only `undefined` means the option is not set55- if (ipcInput) {56+ if (ipcInput !== undefined) {57 throwInvalidSyncOption('ipcInput');58 }59 60 if (ipc) {107 unmodified linesadded:test/fixtures/ipc-echo-strict-deadlock.js+9 added
123456789#!/usr/bin/env nodeimport {sendMessage, getOneMessage} from '../../index.js';
// The pattern recommended to avoid a deadlock, when the other process is not listeningconst [message] = await Promise.all([ getOneMessage(), sendMessage('.', {strict: true}),]);await sendMessage(message);modified:test/ipc/ipc-input.js+21 added-3 removed
17 unmodified lines18 execa('empty.js', {ipcInput: foobarString, ipc: false});19 }, {message: /unless the `ipc` option is `true`/});20 });21 22-test('Cannot use the "ipcInput" option with execaSync()', t => {22+// Any value can be sent over IPC, including falsy ones, so those must be reported as `ipcInput`, not as `ipc`23+const testInvalidSyncIpcInput = (t, ipcInput) => {24 t.throws(() => {24- execaSync('empty.js', {ipcInput: foobarString});25+ execaSync('empty.js', {ipcInput});26 }, {message: /The "ipcInput" option cannot be used with synchronous/});26-});27+};28 29+test('Cannot use the "ipcInput" option with execaSync()', testInvalidSyncIpcInput, foobarString);30+test('Cannot use the "ipcInput" option with execaSync(), 0', testInvalidSyncIpcInput, 0);31+test('Cannot use the "ipcInput" option with execaSync(), empty string', testInvalidSyncIpcInput, '');32+test('Cannot use the "ipcInput" option with execaSync(), false', testInvalidSyncIpcInput, false);33+test('Cannot use the "ipcInput" option with execaSync(), null', testInvalidSyncIpcInput, null);34+35 test('Invalid "ipcInput" option v8 format', t => {36 const {message, cause} = t.throws(() => {37 execa('empty.js', {ipcInput() {}});38 });8 unmodified lines47 t.is(message, 'The `ipcInput` option is not serializable with JSON.');48 t.is(cause.message, 'Do not know how to serialize a BigInt');49 });50 51+// `JSON.stringify()` returns `undefined`, instead of throwing, for values it cannot represent52+const testUnrepresentableJson = (t, ipcInput) => {53+ const {message} = t.throws(() => {54+ execa('empty.js', {ipcInput, serialization: 'json', ipc: true});55+ });56+ t.is(message, 'The `ipcInput` option is not serializable with JSON.');57+};58+59+test('Invalid "ipcInput" option JSON format, function', testUnrepresentableJson, () => {});60+test('Invalid "ipcInput" option JSON format, symbol', testUnrepresentableJson, Symbol('test'));61+62 // An invalid `serialization` option must not crash the `ipcInput` validation, which is keyed by it.63 // `Object.prototype` properties are the trickiest values, since those are found on any object.64 const testInvalidSerialization = async (t, serialization) => {65 const {code} = await t.throwsAsync(execa('empty.js', {ipcInput: foobarString, serialization}));18 unmodified linesmodified:test/ipc/strict.js+47 added-1 removed
113 unmodified lines114 test('exports.sendMessage() "strict" fails if the current process uses once() and buffer false', async t => {115 const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}});116 const [message] = await once(subprocess.nodeChildProcess, 'message');117 t.deepEqual(message, {118- id: 0n,118+ id: 0,119 type: 'execa:ipc:request',120 message: foobarString,121 hasListeners: false,122 });108 unmodified lines231 test('Ignores "strict" responses with an Object.prototype property as id', async t => {232 const {ipcOutput} = await execa('ipc-send-strict-proto.js', {ipc: true});233 t.deepEqual(ipcOutput, []);234 });235+236+const testStrictJson = async (t, buffer) => {237+ const subprocess = execa('ipc-echo.js', {ipc: true, serialization: 'json', buffer});238+ await subprocess.sendMessage(foobarString, {strict: true});239+ t.is(await subprocess.getOneMessage(), foobarString);240+241+ const {ipcOutput} = await subprocess;242+ t.deepEqual(ipcOutput, buffer ? [foobarString] : []);243+};244+245+test('subprocess.sendMessage() "strict" works with serialization "json", buffer false', testStrictJson, false);246+test('subprocess.sendMessage() "strict" works with serialization "json", buffer true', testStrictJson, true);247+248+test('exports.sendMessage() "strict" works with serialization "json"', async t => {249+ const subprocess = execa('ipc-send-strict-get.js', {ipc: true, serialization: 'json'});250+ await subprocess.sendMessage(foobarString, {strict: true});251+252+ const {ipcOutput} = await subprocess;253+ t.deepEqual(ipcOutput, [foobarString, foobarString]);254+});255+256+test('subprocess.sendMessage() "strict" fails with serialization "json" if the subprocess is not listening', async t => {257+ const subprocess = execa('ipc-send.js', {ipc: true, serialization: 'json'});258+ const {message} = await t.throwsAsync(subprocess.sendMessage(foobarString, {strict: true}));259+ t.is(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');260+261+ const {ipcOutput} = await subprocess;262+ t.deepEqual(ipcOutput, [foobarString]);263+});264+265+const noop = () => {};266+267+// Messages received in the same batch are processed one after the other.268+// The deadlock must be detected on the message being processed, not on the first one received.269+// Without the detection, the subprocess would deadlock, so the `timeout` turns that hang into a failed assertion.270+test('Detects a "strict" deadlock on a later message of the same batch', async t => {271+ const subprocess = execa('ipc-echo-strict-deadlock.js', {ipc: true, buffer: {ipc: false}, timeout: 1e4});272+ await subprocess.sendMessage(foobarString);273+ subprocess.sendMessage(foobarString, {strict: true}).catch(noop);274+275+ const {exitCode, timedOut, stderr, ipcOutput} = await t.throwsAsync(subprocess);276+ t.false(timedOut);277+ t.is(exitCode, 1);278+ t.true(stderr.includes('Error: sendMessage() failed: the parent process is sending a message too, instead of listening to incoming messages.'));279+ t.deepEqual(ipcOutput, []);280+});