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

Add Promise-based versions for some functions in child_process #49904

Open
thernstig opened this issue Sep 27, 2023 · 7 comments
Open

Add Promise-based versions for some functions in child_process #49904

thernstig opened this issue Sep 27, 2023 · 7 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@thernstig
Copy link
Contributor

thernstig commented Sep 27, 2023

What is the problem this feature will solve?

It would be nice if child_process functionality, such as exec and execFile, had Promised based versions by default. Currently it does not, see https://nodejs.org/api/child_process.html

Note that only child_process functions that can be async/await should be considered. This does not include things such as fork or spawn.

What is the feature you are proposing to solve the problem?

A new child_process/promises import path that can be used similar to how fs promises are used:

// Using ESM Module syntax:
import { exec } from 'child_process/promises';

try {
  const { stdout } = await exec(
    'sysctl -n net.ipv4.ip_local_port_range'
  );
  console.log('successfully executed the child process command');
} catch (error) {
  console.error('there was an error:', error.message);
}

The ask is basically that child_process/promises could just return a variant such as const exec = util.promisify(process.exec); as a convenience.

What alternatives have you considered?

I am already using const exec = util.promisify(process.exec); but that is not as nice. And this new proposal follows along what is happening in other Node.js APIs.

(The original ask in #38823 was perceived as asking for the entire module and all APIs to be async/await. This issue here narrows it down to only focus on the functions that can be.)

@thernstig thernstig added the feature request Issues that request new features to be added to Node.js. label Sep 27, 2023
@mifi
Copy link
Contributor

mifi commented Sep 28, 2023

I would like to see execFile have a promisified counterpart as well, because very often I need to provide dynamic arguments to a command, and in that case, exec can be unsafe and vulnerable to arbitrary command execution.

@bnoordhuis
Copy link
Member

FTR, that already exists, it's why you can util.promisify(child_process.execFile).

General expectation management: open a pull request if you want to see this happen. exec and execFile have util.promisify.custom hooks that can be repurposed.

@thernstig
Copy link
Contributor Author

I would like to see execFile have a promisified counterpart as well, because very often I need to provide dynamic arguments to a command, and in that case, exec can be unsafe and vulnerable to arbitrary command execution.

That was the intention, and I added execFile as well as an example to the main post. Rationale being it is most often more safe to use, so good to highlight it as another example.

Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Mar 27, 2024
@thernstig
Copy link
Contributor Author

Great APIs makes devs happy, so bumping.

@github-actions github-actions bot removed the stale label Mar 28, 2024
@RedYetiDev
Copy link
Member

I may be misunderstanding this, but doesn't child_process.execSync / child_process.spawnSync achieve this?

@thernstig
Copy link
Contributor Author

I may be misunderstanding this, but doesn't child_process.execSync / child_process.spawnSync achieve this?

You are misunderstanding. The functions you list are sync, not async. I think you need to read up a bit more about this in the linked documents in the original post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Status: Pending Triage
Development

Successfully merging a pull request may close this issue.

4 participants