Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
eed3f17
child_process: allow promisified exec to be cancel
metcoder95 Jul 7, 2020
0ef841d
child_process: fix validation issue
metcoder95 Jul 13, 2020
70cb86c
child_process: use Primordials instead of plain Promise
metcoder95 Oct 23, 2020
a6ae623
child_process: refactor according to review
metcoder95 Oct 23, 2020
988ea3f
child_process: remove license from test
metcoder95 Oct 23, 2020
78e7521
child_process: improve testing
metcoder95 Oct 23, 2020
c8210f6
child_process: fix linter issues
metcoder95 Oct 23, 2020
3dca212
child_process: check if AbortSignal is aborted before rejecting/resol…
metcoder95 Oct 28, 2020
4163c44
child_process: check for signal before looking is aborted
metcoder95 Oct 28, 2020
9e3291e
child_process: refactor signal to use spawn function
metcoder95 Dec 22, 2020
3b14ebe
child_process: fix linter issues
metcoder95 Dec 22, 2020
087f924
child_process: revert check for extra callback
metcoder95 Dec 30, 2020
0fd0719
child_process: fix double AbortError emit
metcoder95 Dec 30, 2020
a41ac9a
child_process: fix linter issues
metcoder95 Dec 30, 2020
f9af3fd
child_process: fix fork issue with AbortController
metcoder95 Jan 12, 2021
2b19cd5
child_process: remove whitespace
metcoder95 Jan 12, 2021
f642015
child_process: re-order alphabetically the options
metcoder95 Jan 14, 2021
dade71b
child_process: refactor spawnWithSignal
metcoder95 Jan 18, 2021
5e7aa7d
child_process: remove wrapper function
metcoder95 Jan 18, 2021
2305734
child_process: revert refactor to spawn
metcoder95 Jan 18, 2021
a60b935
child_process: fix tests
metcoder95 Jan 18, 2021
fe077b0
child_process: refactor
metcoder95 Jan 21, 2021
72ff68a
child_process: refactor tests
metcoder95 Jan 21, 2021
026021a
child_process: refactor listeners args
metcoder95 Jan 21, 2021
c093035
child_process: inline listener
metcoder95 Jan 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
child_process: allow promisified exec to be cancel
Using new experimental AbortController, add support for promisified
exec to be cancelled.
  • Loading branch information
metcoder95 committed Jan 12, 2021
commit eed3f17252f0da5a823e6a68d6e4a1a656a2d44a
34 changes: 32 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ const {
convertToValidSignal,
getSystemErrorName
} = require('internal/util');

Comment thread
aduh95 marked this conversation as resolved.
Outdated
const { isArrayBufferView } = require('internal/util/types');
let debug = require('internal/util/debuglog').debuglog(
'child_process',
(fn) => {
debug = fn;
}
);
let DOMException;
function lazyDOMException(message) {
Comment thread
aduh95 marked this conversation as resolved.
Outdated
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message);
}
const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');

Expand Down Expand Up @@ -183,15 +190,38 @@ function exec(command, options, callback) {
}

const customPromiseExecFunction = (orig) => {
return (...args) => {
return (file, options = {}) => {
Comment thread
jasnell marked this conversation as resolved.
Outdated
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});

promise.child = orig(...args, (err, stdout, stderr) => {
if (options == null || typeof options !== 'object') {
return Promise.reject(
new ERR_INVALID_ARG_TYPE(
'options',
'Object',
options));
}

const { signal } = options;
if (signal == null ||
typeof signal !== 'object' ||
!('aborted' in signal)) {
return Promise.reject(
new ERR_INVALID_ARG_TYPE(
'options.signal',
'AbortSignal',
signal));
}

signal.addEventListener('abort', () => {
reject(lazyDOMException('AbortError'));
}, { once: true });

promise.child = orig(file, options, (err, stdout, stderr) => {
if (err !== null) {
err.stdout = stdout;
err.stderr = stderr;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

Comment thread
metcoder95 marked this conversation as resolved.
Outdated
'use strict';
const common = require('../common');
const assert = require('assert');
const exec = require('child_process').exec;
const { promisify } = require('util');

let pwdcommand, dir;
const execPromisifed = promisify(exec);

if (common.isWindows) {
pwdcommand = 'echo %cd%';
dir = 'c:\\windows';
} else {
pwdcommand = 'pwd';
dir = '/dev';
}


{
const ac = new AbortController();
const signal = ac.signal;
assert.rejects(execPromisifed(pwdcommand, { cwd: dir, signal }), /AbortError/);
ac.abort();
}

{
assert.rejects(execPromisifed(pwdcommand, { cwd: dir, signal: {} }), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.signal" property must be an ' +
'instance of AbortSignal. Received an instance of Object'
});
}

{
function signal() {}
assert.rejects(execPromisifed(pwdcommand, { cwd: dir, signal }), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.signal" property must be an ' +
'instance of AbortSignal. Received function signal'
});
}