Skip to content

cmd: support dash as stdin alias#13012

Closed
ebraminio wants to merge 1 commit into
nodejs:masterfrom
ebraminio:master
Closed

cmd: support dash as stdin alias#13012
ebraminio wants to merge 1 commit into
nodejs:masterfrom
ebraminio:master

Conversation

@ebraminio

@ebraminio ebraminio commented May 13, 2017

Copy link
Copy Markdown
Contributor
  • 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

@refack refack May 13, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

@refack refack May 13, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread lib/internal/bootstrap_node.js Outdated

@refack refack May 13, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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.

@refack refack left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread src/node.cc Outdated

Copy link
Copy Markdown
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'

Copy link
Copy Markdown
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

refack commented May 13, 2017

Copy link
Copy Markdown
Contributor

@ebraminio

Copy link
Copy Markdown
Contributor Author

I think I've fixed these

@refack

refack commented May 13, 2017

Copy link
Copy Markdown
Contributor

@ebraminio

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Great, it seems CI don't dislike this anymore

@refack

refack commented May 13, 2017

Copy link
Copy Markdown
Contributor

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

@ebraminio

Copy link
Copy Markdown
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

refack commented May 13, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

Comment thread src/node.cc Outdated

@addaleax addaleax May 14, 2017

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Done

@Fishrock123

Copy link
Copy Markdown
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
Comment thread src/node.cc Outdated

Copy link
Copy Markdown
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.

@refack refack May 20, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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 :)

Comment thread doc/api/cli.md Outdated

@ebraminio ebraminio May 20, 2017

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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
Copy Markdown
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

refack commented May 23, 2017

Copy link
Copy Markdown
Contributor

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

@Fishrock123 are you still planning on landing this?

@addaleax

Copy link
Copy Markdown
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 <fishrock123@rocketmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
@Fishrock123

Copy link
Copy Markdown
Contributor

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

@Fishrock123

Fishrock123 commented May 23, 2017

Copy link
Copy Markdown
Contributor

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 <fishrock123@rocketmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
jasnell pushed a commit that referenced this pull request May 28, 2017
PR-URL: #13012
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
@jasnell jasnell mentioned this pull request May 28, 2017
@gibfahn

gibfahn commented Jan 15, 2018

Copy link
Copy Markdown
Member

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
Copy Markdown
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