-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
child_process: add promises API #62337
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 'use strict'; | ||
|
|
||
| const { promisify } = require('internal/util'); | ||
| const { exec, execFile } = require('child_process'); | ||
|
|
||
| module.exports = { | ||
| exec: promisify(exec), | ||
| execFile: promisify(execFile), | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Flags: --no-warnings | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const child_process = require('child_process'); | ||
|
|
||
| const { exec, execFile } = require('child_process/promises'); | ||
|
|
||
| // Delegated functions are the same as util.promisify(child_process.exec/execFile). | ||
| { | ||
| const { promisify } = require('util'); | ||
| assert.strictEqual(exec, promisify(child_process.exec)); | ||
| assert.strictEqual(execFile, promisify(child_process.execFile)); | ||
| } | ||
|
|
||
| // exec resolves with { stdout, stderr }. | ||
| { | ||
| const promise = exec(...common.escapePOSIXShell`"${process.execPath}" -p 42`); | ||
|
|
||
| promise.then(common.mustCall((result) => { | ||
| assert.deepStrictEqual(result, { stdout: '42\n', stderr: '' }); | ||
| })); | ||
| } | ||
|
|
||
| // execFile resolves with { stdout, stderr }. | ||
| { | ||
| const promise = execFile(process.execPath, ['-p', '42']); | ||
|
|
||
| promise.then(common.mustCall((result) => { | ||
| assert.deepStrictEqual(result, { stdout: '42\n', stderr: '' }); | ||
| })); | ||
| } | ||
|
|
||
| // exec rejects when command does not exist. | ||
| { | ||
| const promise = exec('doesntexist'); | ||
|
|
||
| promise.catch(common.mustCall((err) => { | ||
| assert(err.message.includes('doesntexist')); | ||
| })); | ||
| } | ||
|
|
||
| // execFile rejects when file does not exist. | ||
| { | ||
| const promise = execFile('doesntexist', ['-p', '42']); | ||
|
|
||
| promise.catch(common.mustCall((err) => { | ||
| assert(err.message.includes('doesntexist')); | ||
| })); | ||
| } | ||
|
|
||
| // Rejected errors include stdout, stderr, and code properties. | ||
| const failingCodeWithStdoutErr = | ||
| 'console.log(42);console.error(43);process.exit(1)'; | ||
|
|
||
| { | ||
| assert.rejects(exec(...common.escapePOSIXShell`"${process.execPath}" -e "${failingCodeWithStdoutErr}"`), | ||
| { | ||
| code: 1, | ||
| stdout: '42\n', | ||
| stderr: '43\n', | ||
| }).then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| execFile(process.execPath, ['-e', failingCodeWithStdoutErr]) | ||
| .catch(common.mustCall((err) => { | ||
| assert.strictEqual(err.code, 1); | ||
| assert.strictEqual(err.stdout, '42\n'); | ||
| assert.strictEqual(err.stderr, '43\n'); | ||
| })); | ||
| } | ||
|
|
||
| // execFile with timeout rejects with killed process. | ||
| { | ||
| assert.rejects(execFile(process.execPath, ['-e', 'setInterval(()=>{}, 99)'], { timeout: 5 }), { | ||
| killed: true, | ||
| }).then(common.mustCall()); | ||
| } | ||
|
|
||
| // encoding option returns strings. | ||
| { | ||
| execFile(process.execPath, ['-p', '"hello"'], { encoding: 'utf8' }) | ||
| .then(common.mustCall((result) => { | ||
| assert.deepStrictEqual(result, { | ||
| stdout: 'hello\n', | ||
| stderr: '', | ||
| }); | ||
| })); | ||
| } | ||
|
|
||
| // encoding 'buffer' returns Buffer instances. | ||
| { | ||
| execFile(process.execPath, ['-p', '"hello"'], { encoding: 'buffer' }) | ||
| .then(common.mustCall((result) => { | ||
| assert.deepStrictEqual(result, { | ||
| stderr: Buffer.from(''), | ||
| stdout: Buffer.from('hello\n'), | ||
| }); | ||
| })); | ||
| } | ||
|
|
||
| // AbortSignal cancels exec. | ||
| { | ||
| const waitCommand = common.isWindows ? | ||
| `"${process.execPath}" -e "setInterval(()=>{}, 99)"` : | ||
| 'sleep 2m'; | ||
| const ac = new AbortController(); | ||
| const promise = exec(waitCommand, { signal: ac.signal }); | ||
|
|
||
| assert.rejects(promise, { | ||
| name: 'AbortError', | ||
| }).then(common.mustCall()); | ||
| ac.abort(); | ||
| } | ||
|
|
||
| // AbortSignal cancels execFile. | ||
| { | ||
| const ac = new AbortController(); | ||
| const promise = execFile( | ||
| process.execPath, | ||
| ['-e', 'setInterval(()=>{}, 99)'], | ||
| { signal: ac.signal }, | ||
| ); | ||
|
|
||
| assert.rejects(promise, { | ||
| name: 'AbortError', | ||
| }).then(common.mustCall()); | ||
| ac.abort(); | ||
| } | ||
|
|
||
| // Already-aborted signal rejects immediately. | ||
| { | ||
| const signal = AbortSignal.abort(); | ||
| const promise = exec('echo hello', { signal }); | ||
|
|
||
| assert.rejects(promise, { name: 'AbortError' }) | ||
| .then(common.mustCall()); | ||
| } | ||
|
|
||
| // maxBuffer causes rejection. | ||
| { | ||
| assert.rejects(execFile( | ||
| process.execPath, | ||
| ['-e', "console.log('a'.repeat(100))"], | ||
| { maxBuffer: 10 }, | ||
| ), { | ||
| name: 'RangeError', | ||
| code: 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER', | ||
| }).then(common.mustCall()); | ||
| } | ||
|
|
||
| // timeout causes the child process to be killed. | ||
| { | ||
| const promise = execFile( | ||
| process.execPath, | ||
| ['-e', 'setInterval(()=>{}, 99)'], | ||
| { timeout: 1 }, | ||
| ); | ||
|
|
||
| promise.catch(common.mustCall((err) => { | ||
| assert.ok(err.killed || err.signal); | ||
| })); | ||
| } | ||
|
|
||
| // Module can be loaded with node: scheme. | ||
| { | ||
| assert.strictEqual(require('node:child_process/promises'), require('child_process/promises')); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed
If we really want it, we should be checking for strict equality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — replaced it with the strict equality check between
node:and non-prefixed require, which is more useful.