Skip to content
Myr

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

7 files+87 added−8 removed

modified:lib/ipc/incoming.js+2 added-1 removed
modified:lib/ipc/ipc-input.js+4 added-1 removed
modified:lib/ipc/strict.js+2 added-1 removed
20 unmodified lines
21 hasListeners,22 };23};2425// This is a plain number, not a `BigInt`, since it must be serializable with `serialization: 'json'`25let count = 0n;26let count = 0;2728// 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.30export const validateStrictDeadlock = (outgoingMessages, wrappedMessage) => {
84 unmodified lines
modified:lib/methods/main-sync.js+2 added-1 removed
50 unmodified lines
51const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;5253// Options validation logic specific to sync methods54const 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 }5960 if (ipc) {
107 unmodified lines
added:test/fixtures/ipc-echo-strict-deadlock.js+9 added
modified:test/ipc/ipc-input.js+21 added-3 removed
17 unmodified lines
18 execa('empty.js', {ipcInput: foobarString, ipc: false});19 }, {message: /unless the `ipc` option is `true`/});20});2122test('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`23const 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};2829test('Cannot use the "ipcInput" option with execaSync()', testInvalidSyncIpcInput, foobarString);30test('Cannot use the "ipcInput" option with execaSync(), 0', testInvalidSyncIpcInput, 0);31test('Cannot use the "ipcInput" option with execaSync(), empty string', testInvalidSyncIpcInput, '');32test('Cannot use the "ipcInput" option with execaSync(), false', testInvalidSyncIpcInput, false);33test('Cannot use the "ipcInput" option with execaSync(), null', testInvalidSyncIpcInput, null);3435test('Invalid "ipcInput" option v8 format', t => {36 const {message, cause} = t.throws(() => {37 execa('empty.js', {ipcInput() {}});38 });
8 unmodified lines
47 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});5051// `JSON.stringify()` returns `undefined`, instead of throwing, for values it cannot represent52const 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};5859test('Invalid "ipcInput" option JSON format, function', testUnrepresentableJson, () => {});60test('Invalid "ipcInput" option JSON format, symbol', testUnrepresentableJson, Symbol('test'));6162// 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.64const testInvalidSerialization = async (t, serialization) => {65 const {code} = await t.throwsAsync(execa('empty.js', {ipcInput: foobarString, serialization}));
18 unmodified lines
modified:test/ipc/strict.js+47 added-1 removed
113 unmodified lines
114test('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 lines
231test('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});235236const 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);240241 const {ipcOutput} = await subprocess;242 t.deepEqual(ipcOutput, buffer ? [foobarString] : []);243};244245test('subprocess.sendMessage() "strict" works with serialization "json", buffer false', testStrictJson, false);246test('subprocess.sendMessage() "strict" works with serialization "json", buffer true', testStrictJson, true);247248test('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});251252 const {ipcOutput} = await subprocess;253 t.deepEqual(ipcOutput, [foobarString, foobarString]);254});255256test('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.');260261 const {ipcOutput} = await subprocess;262 t.deepEqual(ipcOutput, [foobarString]);263});264265const noop = () => {};266267// 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.270test('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);274275 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});