Skip to content
Myr

Support `AbortController` (#490)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Gyubong · committed by GitHub · parent 93ab929 · Browse files · 5 files+94 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Diff stats

5 files+94 added−7 removed

  • tests:+47 added−0 removed
  • other:+47 added−7 removed

5 files+94 added−7 removed

modified:test/kill.js+47 added
260 unmodified lines
261 subprocess.kill();262 const {isCanceled} = await t.throwsAsync(subprocess);263 t.false(isCanceled);264});265266if (globalThis.AbortController !== undefined) {267 test('calling abort throws an error with message "Command was canceled"', async t => {268 const abortController = new AbortController();269 const subprocess = execa('noop.js', [], {signal: abortController.signal});270 abortController.abort();271 await t.throwsAsync(subprocess, {message: /Command was canceled/});272 });273274 test('calling abort twice should show the same behaviour as calling it once', async t => {275 const abortController = new AbortController();276 const subprocess = execa('noop.js', [], {signal: abortController.signal});277 abortController.abort();278 abortController.abort();279 const {isCanceled} = await t.throwsAsync(subprocess);280 t.true(isCanceled);281 t.true(subprocess.killed);282 });283284 test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {285 const abortController = new AbortController();286 const subprocess = execa('noop.js', [], {signal: abortController.signal});287 const {isCanceled} = await subprocess;288 abortController.abort();289 t.false(isCanceled);290 });291292 test('calling cancel after abort should show the same behaviour as only calling cancel', async t => {293 const abortController = new AbortController();294 const subprocess = execa('noop.js', [], {signal: abortController.signal});295 abortController.abort();296 subprocess.cancel();297 const {isCanceled} = await t.throwsAsync(subprocess);298 t.true(isCanceled);299 t.true(subprocess.killed);300 });301302 test('calling abort after cancel should show the same behaviour as only calling cancel', async t => {303 const abortController = new AbortController();304 const subprocess = execa('noop.js', [], {signal: abortController.signal});305 subprocess.cancel();306 abortController.abort();307 const {isCanceled} = await t.throwsAsync(subprocess);308 t.true(isCanceled);309 t.true(subprocess.killed);310 });311}
modified:index.d.ts+30 added
207 unmodified lines
208 */209 readonly killSignal?: string | number;210211 /**212 You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).213214 When `AbortController.abort()` is called, [`.isCanceled`](https://github.com/sindresorhus/execa#iscanceled) becomes `false`.215216 *Requires Node.js 16 or later.*217218 @example219 ```js220 import {execa} from 'execa';221222 const abortController = new AbortController();223 const subprocess = execa('node', [], {signal: abortController.signal});224225 setTimeout(() => {226 abortController.abort();227 }, 1000);228229 try {230 await subprocess;231 } catch (error) {232 console.log(subprocess.killed); // true233 console.log(error.isCanceled); // true234 }235 ```236 */237 readonly signal?: AbortSignal;238239 /**240 If `true`, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to `true` automatically when the `shell` option is `true`.241242 @default false243 */
124 unmodified lines
368 all?: StdoutErrorType;369370 /**371 Whether the process was canceled.372373 You can cancel the spawned process using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.374 */375 isCanceled: boolean;376}377
209 unmodified lines
modified:index.js+1 added-1 removed
125 unmodified lines
126 command,127 escapedCommand,128 parsed,129 timedOut,130 isCanceled: context.isCanceled,130 isCanceled: context.isCanceled || (parsed.options.signal ? parsed.options.signal.aborted : false),131 killed: spawned.killed,132 });133134 if (!parsed.options.reject) {
133 unmodified lines
modified:index.test-d.ts+1 added
140 unmodified lines
141execa('unicorns', {timeout: 1000});142execa('unicorns', {maxBuffer: 1000});143execa('unicorns', {killSignal: 'SIGTERM'});144execa('unicorns', {killSignal: 9});145execa('unicorns', {signal: new AbortController().signal});146execa('unicorns', {windowsVerbatimArguments: true});147execa('unicorns', {windowsHide: false});148/* eslint-enable @typescript-eslint/no-floating-promises */149execa('unicorns').kill();
59 unmodified lines
modified:readme.md+15 added-6 removed
81 unmodified lines
8283```js84import {execa} from 'execa';8586const subprocess = execa('node');86const abortController = new AbortController();87const subprocess = execa('node', [], {signal: abortController.signal});8889setTimeout(() => {89 subprocess.cancel();90 abortController.abort();91}, 1000);9293try {94 await subprocess;
76 unmodified lines
171Milliseconds to wait for the child process to terminate before sending `SIGKILL`.172173Can be disabled with `false`.174174#### cancel()175176Similar to [`childProcess.kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal). This is preferred when cancelling the child process execution as the error is more descriptive and [`childProcessResult.isCanceled`](#iscanceled) is set to `true`.177175#### all176177Type: `ReadableStream | undefined`178
107 unmodified lines
286Type: `boolean`287288Whether the process was canceled.289290You can cancel the spawned process using the [`signal`](#signal-1) option.291292#### killed293294Type: `boolean`295
248 unmodified lines
544Default: `SIGTERM`545546Signal value to be used when the spawned process will be killed.547548#### signal549550Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)551552You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).553554When `AbortController.abort()` is called, [`.isCanceled`](#iscanceled) becomes `false`.555556*Requires Node.js 16 or later.*557558#### windowsVerbatimArguments559560Type: `boolean`\561Default: `false`
106 unmodified lines