Fix the "signal" option being silently ignored by synchronous methods
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 9c5c71c · Browse files · 4 files·+18 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
4 files+18 added−8 removed
- tests:+7 added−0 removed
- other:+11 added−8 removed
Provenance
- Commit
59de065- Tree
64b0666- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
9c5c71c
4 files changed+18 added−8 removed
modified:lib/arguments/options.js+2 added-1 removed
12345667891021 unmodified lines32333435363738394074 unmodified linesimport path from 'node:path';import process from 'node:process';import {npmRunPathEnv} from 'npm-run-path';import {normalizeForceKillAfterDelay} from '../terminate/kill.js';import {normalizeKillSignal} from '../terminate/signal.js';import {validateCancelSignal} from '../terminate/cancel.js';import {validateCancelSignal, validateRenamedSignalOption} from '../terminate/cancel.js';import {validateGracefulCancel} from '../terminate/graceful.js';import {validateTimeout} from '../terminate/timeout.js';import {handleNodeOption} from '../methods/node.js';import {validateIpcInputOption} from '../ipc/ipc-input.js';21 unmodified lines validateTimeout(options); validateEncoding(options); validateIpcInputOption(options); validateCancelSignal(options); validateRenamedSignalOption(options); validateGracefulCancel(options); options.shell = normalizeFileUrl(options.shell); options.killSignal = normalizeKillSignal(options.killSignal); options.forceKillAfterDelay = normalizeForceKillAfterDelay(options.forceKillAfterDelay);74 unmodified linesmodified:lib/methods/main-async.js+2 added-7 removed
72 unmodified lines73 };74 75 // Options normalization logic specific to async methods.76 // Prevent passing the `timeout` option directly to `child_process.spawn()`.77+// The `signal` option is not passed either: it has been renamed and is validated in `normalizeOptions()`, for both synchronous and asynchronous methods.78 // The returned object keeps a null prototype, otherwise a polluted `Object.prototype` would inject option values.78-const handleAsyncOptions = ({timeout, signal, ...options}) => {79- if (signal !== undefined) {80- throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');81- }79+const handleAsyncOptions = ({timeout, signal, ...options}) => ({__proto__: null, ...options, timeoutDuration: timeout});80 83- return {__proto__: null, ...options, timeoutDuration: timeout};84-};85-81 const spawnSubprocessAsync = ({file, commandArguments, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors}) => {82 let subprocess;83 try {84 subprocess = spawn(...concatenateShell(file, commandArguments, getSpawnOptions(options)));147 unmodified linesmodified:lib/terminate/cancel.js+7 added
5 unmodified lines678910111213141516171819207 unmodified lines5 unmodified lines throw new Error(`The \`cancelSignal\` option must be an AbortSignal: ${String(cancelSignal)}`); }};
// The `signal` option was renamed to `cancelSignal`. Validated for every method, otherwise synchronous methods would silently ignore it, since `child_process.spawnSync()` does not support that option either.export const validateRenamedSignalOption = ({signal}) => { if (signal !== undefined) { throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.'); }};
// Terminate the subprocess when aborting the `cancelSignal` option and `gracefulSignal` is `false`export const throwOnCancel = ({kill, cancelSignal, gracefulCancel, context, controller}) => cancelSignal === undefined || gracefulCancel ? [] : [terminateOnCancel(kill, cancelSignal, context, controller)];7 unmodified linesmodified:test/terminate/cancel.js+7 added
199 unmodified lines200 execa('empty.js', {signal: abortController.signal});201 }, {message: /renamed to "cancelSignal"/});202 });203 204+test('Throws when using the former "signal" option name, sync', t => {205+ const abortController = new AbortController();206+ t.throws(() => {207+ execaSync('empty.js', {signal: abortController.signal});208+ }, {message: /renamed to "cancelSignal"/});209+});210+211 test('Cannot use cancelSignal, sync', t => {212 const abortController = new AbortController();213 t.throws(() => {214 execaSync('empty.js', {cancelSignal: abortController.signal});2 unmodified lines