electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

Home Page:https://electronjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot access piped stdin in main process

s-a opened this issue · comments

I succesfuly got access to stdin data at
https://github.com/s-a/iron-node/blob/e844eac23fb8a0f7f2285dab9318ab3a4fe2fc78/bin/run.js#L17

But having trouble to access stdin at the main process module.
https://github.com/s-a/iron-node/blob/e844eac23fb8a0f7f2285dab9318ab3a4fe2fc78/app/index.js#L11

I Created a test script at https://github.com/s-a/iron-node/blob/e844eac23fb8a0f7f2285dab9318ab3a4fe2fc78/package.json#L16 pipe-test

Well I tried a few variants and feel that this could be an electron bug on windows (not tried other OS s)

Ref.: s-a/iron-node#78

commented

On Windows only console programs can have stdin, but Electron is a GUI program.

To support this we have to either provide a build of Electron that is compiled as console program, like what Node.js does, or you can work around this in your app by saving stdin into a file and then pass it to Electron.

Adding a new build would put lots of burden on our maintenance, so unless there are strong requests to add it, we won't do that.

Thanks for the reply. For what its worth, I ended making a REPL using named pipe, and having a parent Node.js process both start my Electron app and connect through it using the net library. Works like a charm.

@stelcheck is there any source available where I could take a peek? I have a very similar problem in a pet project which I would like to use on windows.

@ccoenen https://gist.github.com/stelcheck/a0c798ff31df5f2c0e5d72513948dcf1

It's pretty simple, just an IPC connection through a socket file, a REPL server on the electron side. I use the console script to start the electron app for convenience.

@zcbenz is there any appetite for supporting this? I'd really like to integrate with Chrome's "Native Messaging" but right now it seems like there's no way to do that without a go-between application and manually configuring a secure channel between electron and the go-between application.

Same here, there is a need for stdin for chrome native apps.

On Windows only console programs can have stdin, but Electron is a GUI program.

To support this we have to either provide a build of Electron that is compiled as console program, like what Node.js does, or you can work around this in your app by saving stdin into a file and then pass it to Electron.

Adding a new build would put lots of burden on our maintenance, so unless there are strong requests to add it, we won't do that.

That's not true. In WPF framework, Swing framework and more, stdio is supported even though they were all developed for GUI apps.

Well I tried a few variants and feel that this could be an electron bug on windows (not tried other OS s)

Any news on that?
I am trying to implement A CLI using Electron and I need to get user input.
Currently not working on windows 10 with Electron 8.0.0

(Electron main process) Not working on Windows:

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
  rl.question('Question: ', (question) => {
    console.log(question)
    rl.close();
});

@giladnavot I spent literally a month on this. I'm no expert, but this seems to me to be impossible on Windows. Your only option is a native Node application bridge via IPC as far as I'm aware, or a non-Node solution.

@slapbox same conclusion. Can't seem to access stdin at all on Windows from Electron. Works fine on mac/ubunutu.

@zcbenz Is there any chance this could be revisited? I am asking as I know the build infra has come a long way, since the original answer in early 2016. Build is now synchronized with chromium nightly, and having forward/backward fix ports with bots, etc. Is it something the build infra team would be willing to tackle now that there have been many enhancements?

This would be a very useful feature for securely passing information from any parent to an electron child process on windows.

It is certainly deceptive to have the process.stdin object available but broken. It should be removed or throw an error on use if it is not functional. I just spent an hour trying to debug why I couldn't stream data to electron from a parent process launching it.

Edit: I modified electron to be a console app since supposedly that is the only obstacle to getting this working, but I still could not get verified node.js code samples working. So there is probably bugs in the electron on top of the console app issue, which is itself an easy fix (ship two binaries!)

Edit 2: Actually electron could be a console app and only need one binary. cli.js could specify windowsHide: true when launching electron to hide the console window on Windows. So I am not sure where the problem is with making electron a console app. I am still not sure why I can't get stdin to work even if it is a console app, but Googling around suggest changes were made to imported node source code which cause stdin support to no longer function regardless. I have not looked at it myself though.