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

cmd: support dash as stdin alias #13012

Closed
wants to merge 1 commit into from
Closed

Conversation

ebraminio
Copy link
Contributor

@ebraminio ebraminio commented May 13, 2017

  • make -j4 test (UNIX), or vcbuild test (Windows) passes
    (some of the tests are failing regardless of my change AFAICS so I assume this as passed)
  • tests and/or benchmarks are included
    (an available test is modified to cover this, and a more specific one also added)
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

cli

Dash as stdin alias is a usual convention between unix programs https://unix.stackexchange.com/a/16364 and I believe it should be available on nodejs also.

Here I've made a change that make node ignore dash as an option so it will effectively be treated like /dev/stdin

Simply, with my change this will be made possible with node:

echo "console.log(process.argv[2])" | node - --option-for-stdin-script

The reason I like to have this is because of my one-file webserver http://pad.js.org which in order to be able to pass extra option to its one-linear execution command on the fly (without having to download or install the script first, which it supports that also through npm and docker). BTW, have a look at that project also, I guess you will find it interesting, specially the way its source is distributed and several features it offers :)

Interestingly, I saw during my tests if I pass "/std/stdin" instead "-" on the above command, even without my change, it will be ran on macOS just fine but for some reasons it seems it doesn't work on Linux equally which perhaps should be filed as a bug separately (Edited: proposed another patch for that). However with this change, "-" will provide a cross platform solution for achieving this goal.

For my specific case, this patch will make this functionality possible:

curl pad.js.org | node - -h

which as mentioned before if you have access to macOS, this is already possible with the following command, but not on Linux and obviously ever Windows:

curl pad.js.org | node /dev/stdin -h

Thanks.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels May 13, 2017
@mscdex mscdex added cli Issues and PRs related to the Node.js command line interface. and removed c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels May 13, 2017
setTimeout(function() {
child.stdin.end('console.log(process.pid)');
}, 1);
}
Copy link
Contributor

@refack refack May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We like an \n on the last line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done for both

let found = '';
for (const args of [[], ['-']]) {
const child = spawn(process.execPath, args, {
env: Object.assign(process.env, {
Copy link
Contributor

@refack refack May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want the arguments to assign the other way around?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copied… is this an existing bug? :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copied from the old version, I guess better to not touch it now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 this is a super weird construct. IMHO If it's to suss out a bug it should be near https://github.com/nodejs/node/blob/master/test/parallel/test-child-process-spawn-shell.js#L51
Personaly I'd rather see this fixed here, but I won't block for it.

@@ -123,7 +123,7 @@
const internalModule = NativeModule.require('internal/module');
internalModule.addBuiltinLibsToObject(global);
evalScript('[eval]');
} else if (process.argv[1]) {
} else if (process.argv[1] && process.argv[1] !== '-') {
Copy link
Contributor

@refack refack May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO -- is much more common alias for "the rest will be piped through stdin". If it's not too much work I'd be happy if you adjust.
So you could do echo "console.log(process.argv[2])" | node -h --

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got you -h is an arg to the script. Not to node.
Retracting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already use -- for separating arguments, and that’s what it’s commonly used for (on Unixes, at least). - is the most common choice for “stdin”, by far.

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last comment is a real nit.

src/node.cc Outdated
while (index < nargs && argv[index][0] == '-' && !short_circuit) {
while (index < nargs &&
// starts with dash but is not only a dash which is special
argv[index][0] == '-' && strlen(argv[index]) != 1 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could replace the strlen check with argv[index][1] == '\0'

let found = '';
for (const args of [[], ['-']]) {
const child = spawn(process.execPath, args, {
env: Object.assign(process.env, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copied… is this an existing bug? :/

@refack
Copy link
Contributor

refack commented May 13, 2017

@ebraminio
Copy link
Contributor Author

I think I've fixed these

@refack
Copy link
Contributor

refack commented May 13, 2017

@ebraminio
Copy link
Contributor Author

Thank you :) Is linter giving a true found error? I couldn't find anything useful from its console info.

setTimeout(function() {
child.stdin.end('console.log(process.pid)');
}, 1);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here just one blank line, i.e. file should end }\n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super weird, I really didn't want to put two... I guess done now

@ebraminio ebraminio force-pushed the master branch 2 times, most recently from eefb4e3 to 1497e2b Compare May 13, 2017 19:08
@ebraminio
Copy link
Contributor Author

Great, it seems CI don't dislike this anymore

@refack
Copy link
Contributor

refack commented May 13, 2017

Yeah I run a linter CI: https://ci.nodejs.org/job/node-test-linter/9043/

@ebraminio
Copy link
Contributor Author

No meaningful output on test/linux-fips AFAICS

@refack refack added the semver-minor PRs that contain new features and should be released in the next minor version. label May 13, 2017
@refack refack self-assigned this May 13, 2017
@refack
Copy link
Contributor

refack commented May 13, 2017

I've retriggered linux-fips: https://ci.nodejs.org/job/node-test-commit-linux-fips/8382/
But even if it borks again, consider the CI green. Now we wait for this to "mature"...

I'm assuming this is your first contribution so I've put down a little explanation. If it's not ignore the rest 😉

Our "landing" policy is to wait 2 days (3-4 if it's weekend) so that stakeholders can chime in. Since I've marked this as semver-minor there shouldn't be any extra steps. If someone else judges this as semver-major then we need two members of the CTC to sign off on the change.

Good luck, and thank you very much for the contribution 🥇

@ebraminio
Copy link
Contributor Author

np, I just wanted to know if my part is complete :)

src/node.cc Outdated
@@ -3780,7 +3780,10 @@ static void ParseArgs(int* argc,

unsigned int index = 1;
bool short_circuit = false;
while (index < nargs && argv[index][0] == '-' && !short_circuit) {
while (index < nargs &&
// starts with dash but is not only a dash which is special
Copy link
Member

@addaleax addaleax May 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d drop this comment, or change it to something like // skip "-", because currently it described how it does stuff, not what it does, so it’s directly redundant to the code.

child.stdin.end('console.log(process.argv[2])');

let actual = '';
child.stdout.on('data', (chunk) => actual += chunk);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should call child.stdout.setEncoding('utf8'); before using .on('data') with strings

@ebraminio
Copy link
Contributor Author

Done

@Fishrock123
Copy link
Contributor

Going to land this at the end of today, then.

@Fishrock123 Fishrock123 added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels May 19, 2017
@ebraminio ebraminio force-pushed the master branch 3 times, most recently from 877fbfe to 0283fc0 Compare May 20, 2017 13:21
@@ -3598,6 +3598,8 @@ static void PrintHelp() {
" does not appear to be a terminal\n"
" -r, --require module to preload (option can be "
"repeated)\n"
" - script read from stdin (default; "
"interactive mode if a tty)"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added this line from the python -h, please review it before the merge.

Copy link
Contributor

@refack refack May 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean node -h? ahh "from"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant I've copied this literally from the similar place on python -h :)

@@ -10,7 +10,7 @@ To view this documentation as a manual page in a terminal, run `man node`.

## Synopsis

`node [options] [v8 options] [script.js | -e "script"] [--] [arguments]`
`node [options] [v8 options] [script.js | -e "script" | -] [--] [arguments]`
Copy link
Contributor Author

@ebraminio ebraminio May 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed space after the dash as its section start also is not space padded.

@refack refack removed their assignment May 20, 2017
child.on('close', common.mustCall(function(c) {
assert.strictEqual(c, 0);
assert.strictEqual(found, wanted);
console.log('ok');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the console output necessary to the test? If not, please remove :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ebraminio ebraminio force-pushed the master branch 2 times, most recently from 3a63d34 to 55e51d6 Compare May 22, 2017 17:44
@refack
Copy link
Contributor

refack commented May 23, 2017

Going to land this at the end of today, then.

@Fishrock123 are you still planning on landing this?

@addaleax
Copy link
Member

Landed in 594b5d7, thanks for the PR!

@addaleax addaleax closed this May 23, 2017
addaleax pushed a commit that referenced this pull request May 23, 2017
PR-URL: #13012
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
@Fishrock123
Copy link
Contributor

@refack ok not really, go ahead and land it please

@Fishrock123
Copy link
Contributor

Fishrock123 commented May 23, 2017

huh, guess github didn't update...

Thanks @addaleax 😬 😓

jasnell pushed a commit that referenced this pull request May 24, 2017
PR-URL: #13012
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
jasnell pushed a commit that referenced this pull request May 28, 2017
PR-URL: #13012
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
@jasnell jasnell mentioned this pull request May 28, 2017
@gibfahn
Copy link
Member

gibfahn commented Jan 15, 2018

Release team were +1 on backporting to v6.x if someone would be willing to raise a backport PR, see the guide for more info.

@silverwind
Copy link
Contributor

If this gets backported, #18440 should probably also be applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. cli Issues and PRs related to the Node.js command line interface. lib / src Issues and PRs related to general changes in the lib or src directory. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants