Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AbortController #490

Merged
merged 21 commits into from Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.d.ts
Expand Up @@ -204,6 +204,26 @@ export interface CommonOptions<EncodingType> {
*/
readonly killSignal?: string | number;

/**
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).

*Requires Node.js 16 or later.*

@example
```
import {execa} from 'execa';

const abortController = new AbortController();
const subprocess = execa('node', [], {signal: abortController.signal});

abortController.abort();

console.log(subprocess.killed);
//=> true
```
*/
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
readonly signal?: AbortSignal;

/**
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`.

Expand Down
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -109,6 +109,12 @@ export function execa(file, args, options) {
spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
spawned.cancel = spawnedCancel.bind(null, spawned, context);

if (parsed.options.signal) {
parsed.options.signal.addEventListener('abort', () => {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
spawned.cancel();
}, {once: true});
}

const handlePromise = async () => {
const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);
const stdout = handleOutput(parsed.options, stdoutResult);
Expand Down
26 changes: 26 additions & 0 deletions readme.md
Expand Up @@ -80,6 +80,8 @@ try {

### Cancelling a spawned process

jopemachine marked this conversation as resolved.
Show resolved Hide resolved
#### Use `cancel` method

```js
import {execa} from 'execa';

Expand All @@ -97,6 +99,20 @@ try {
}
```
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

#### Use `AbortController`

```js
import {execa} from 'execa';

const abortController = new AbortController();
const subprocess = execa('node', [], {signal: abortController.signal});

abortController.abort();

console.log(subprocess.killed);
//=> true
```
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

### Catching an error with the sync method

```js
Expand Down Expand Up @@ -542,6 +558,16 @@ Default: `SIGTERM`

Signal value to be used when the spawned process will be killed.

#### signal

Type: `object (AbortSignal)`
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).

See [this Example](#use-abortcontroller).

*Requires Node.js 16 or later.*

#### windowsVerbatimArguments

Type: `boolean`\
Expand Down
45 changes: 45 additions & 0 deletions test/kill.js
Expand Up @@ -262,3 +262,48 @@ test('calling cancel method on a process which has been killed does not make err
const {isCanceled} = await t.throwsAsync(subprocess);
t.false(isCanceled);
});

test('calling abort throws an error with message "Command was canceled"', async t => {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
const abortController = new AbortController();
const subprocess = execa('noop.js', [], {signal: abortController.signal});
abortController.abort();
await t.throwsAsync(subprocess, {message: /Command was canceled/});
});

test('calling abort twice should show the same behaviour as calling it once', async t => {
const abortController = new AbortController();
const subprocess = execa('noop.js', [], {signal: abortController.signal});
abortController.abort();
abortController.abort();
const {isCanceled} = await t.throwsAsync(subprocess);
t.true(isCanceled);
t.true(subprocess.killed);
});

test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {
const abortController = new AbortController();
const subprocess = execa('noop.js', [], {signal: abortController.signal});
const {isCanceled} = await subprocess;
abortController.abort();
t.false(isCanceled);
});

test('calling cancel after abort should show the same behaviour as only calling cancel', async t => {
const abortController = new AbortController();
const subprocess = execa('noop.js', [], {signal: abortController.signal});
abortController.abort();
subprocess.cancel();
const {isCanceled} = await t.throwsAsync(subprocess);
t.true(isCanceled);
t.true(subprocess.killed);
});

test('calling abort after cancel should show the same behaviour as only calling cancel', async t => {
const abortController = new AbortController();
const subprocess = execa('noop.js', [], {signal: abortController.signal});
subprocess.cancel();
abortController.abort();
const {isCanceled} = await t.throwsAsync(subprocess);
t.true(isCanceled);
t.true(subprocess.killed);
});