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
Provenance
- Commit
c6e791a- Tree
3a31af5- Author
Gyubong <jopemachine@naver.com>- Committer
GitHub <noreply@github.com>- Parent
93ab929
5 files changed+94 added−7 removed
modified:test/kill.js+47 added
260 unmodified lines261 subprocess.kill();262 const {isCanceled} = await t.throwsAsync(subprocess);263 t.false(isCanceled);264 });265+266+if (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+ });273+274+ 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+ });283+284+ 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+ });291+292+ 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+ });301+302+ 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 lines208 */209 readonly killSignal?: string | number;210 211 /**212+ You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).213+214+ When `AbortController.abort()` is called, [`.isCanceled`](https://github.com/sindresorhus/execa#iscanceled) becomes `false`.215+216+ *Requires Node.js 16 or later.*217+218+ @example219+ ```js220+ import {execa} from 'execa';221+222+ const abortController = new AbortController();223+ const subprocess = execa('node', [], {signal: abortController.signal});224+225+ setTimeout(() => {226+ abortController.abort();227+ }, 1000);228+229+ try {230+ await subprocess;231+ } catch (error) {232+ console.log(subprocess.killed); // true233+ console.log(error.isCanceled); // true234+ }235+ ```236+ */237+ readonly signal?: AbortSignal;238+239+ /**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`.241 242 @default false243 */124 unmodified lines368 all?: StdoutErrorType;369 370 /**371 Whether the process was canceled.372+373+ 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 linesmodified:index.js+1 added-1 removed
125 unmodified lines126 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 });133 134 if (!parsed.options.reject) {133 unmodified linesmodified:index.test-d.ts+1 added
140 unmodified lines141 execa('unicorns', {timeout: 1000});142 execa('unicorns', {maxBuffer: 1000});143 execa('unicorns', {killSignal: 'SIGTERM'});144 execa('unicorns', {killSignal: 9});145+execa('unicorns', {signal: new AbortController().signal});146 execa('unicorns', {windowsVerbatimArguments: true});147 execa('unicorns', {windowsHide: false});148 /* eslint-enable @typescript-eslint/no-floating-promises */149 execa('unicorns').kill();59 unmodified linesmodified:readme.md+15 added-6 removed
81 unmodified lines82 83 ```js84 import {execa} from 'execa';85 86-const subprocess = execa('node');86+const abortController = new AbortController();87+const subprocess = execa('node', [], {signal: abortController.signal});88 89 setTimeout(() => {89- subprocess.cancel();90+ abortController.abort();91 }, 1000);92 93 try {94 await subprocess;76 unmodified lines171 Milliseconds to wait for the child process to terminate before sending `SIGKILL`.172 173 Can be disabled with `false`.174 174-#### cancel()175-176-Similar 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`.177-175 #### all176 177 Type: `ReadableStream | undefined`178 107 unmodified lines286 Type: `boolean`287 288 Whether the process was canceled.289 290+You can cancel the spawned process using the [`signal`](#signal-1) option.291+292 #### killed293 294 Type: `boolean`295 248 unmodified lines544 Default: `SIGTERM`545 546 Signal value to be used when the spawned process will be killed.547 548+#### signal549+550+Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)551+552+You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).553+554+When `AbortController.abort()` is called, [`.isCanceled`](#iscanceled) becomes `false`.555+556+*Requires Node.js 16 or later.*557+558 #### windowsVerbatimArguments559 560 Type: `boolean`\561 Default: `false`106 unmodified lines