Output being written to process.stdout twice - node.js

I want to pass a message from a nodejs script to chrome extension using nativeMessaging and thus am using the chrome-native-messaging node module in the following way to pass a message to stdout.
var rs = new stream.Transform({objectMode: true});
var msg = {"message":"pingfromelectron"};
rs.push(msg);
rs.pipe(output).pipe(process.stdout);
However, this is writing the output twice to stdout as you can see in the screenshot below:
I am not quite sure why this is happening. Any pointers would be helpful.

Related

How do I use console.table but to stderr?

I find that console.table() is good, in a few cases, for error messages. However, it doesn't print to stderr. How do I print console.table to stderr in Node.js?
In Node.js, one can instantiate a custom Console instance with a different writable stream than stdout using the console module, e.g.:
const { Console } = require("console");
const console = new Console(process.stderr);
This would have any method that typically uses stdout, including table(), use stderr instead.

Process Object Stdout and StdIn do not work as expected

I am using vsCode for NodeJS.
My code was simple. So I was expecting stdout should wait for input from console. Once input is given, then it should go to exit.
But once i do > node process
the program goes to exit directly.
file - process.js ->
process.stdout.write('Ask me something : ');
process.stdin.on('data', function(answer){
console.log(answer.toString());
});
console.log('exit')
process.exit();
Output:
c:\node-projects\demo-nodejs>node process
Ask me something : exit
c:\node-projects\demo-nodejs>
How can I use process object to get standard input/output/print/exit the code instead of using other readline object?
There are a few question/answers such as this one already on this site that probably answer what you are asking. Your code is not working because the line with process.stdin.on is not a synchronous callback method call, it is an event handler that waits for the 'data' event.
If I understand what you are trying to do, I think it is easiest to import the 'readline' module. It is built-in to the nodejs executable, but not loaded to your code execution environment unless you require it.
That would make your code look like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Ask me something : ', function(answer){
console.log(answer.toString());
console.log('exit')
process.exit();
});
Note that the rl.question method call executes right away and calls the function(answer) function when the user is done with text input. It can take some understanding and getting used to the asynchronous program control flow in NodeJS, you might have to rethink some of the rest of your program structure.
The offical documentation for the readline NodeJS module and function is here.

Electron Readline

I'm trying to build an app using electron, it's designed to get a GUI later but for now i'm just trying to do this:
function test(){
console.log("In Test")
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
console.log(line);
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', test);
On launching npm start, I see the "In Test" log, but when I type something in my shell, it's not returned to me as I need it to be by the Readline module.
Am I missing something ?
In Electron the console becomes the Chromium console which is not like the terminal you're used to. Readline is not going to work as far as I know. The blink console in Chromium does not support reading input this way. It operates more like a debug REPL where you can type JS code, inspect variables, etc. It's not for user input. I don't think you're going to be able to get input supplied from that console into stdin, which is where the readline module is waiting to see data.
Update
I assumed OP was using the dev tools console expecting it to work like a shell. He was using it properly. The actual issue is a bug with node's readline module on Windows. Node devs are actively working to fix it. It's a regression bug that was fixed once before but appeared again in recent versions of node.

node.js STDIN works with process.stdin, not when piped

I'm using require("child_process").spawn and calling an executable, which at the beginning is pausing for a password to be input; if I use it like below:
var spawn = require("child_process").spawn;
var exe = spawn("NameOfExe.exe", [/*arguments*/], {stdio: [process.stdin, process.stdout, process.stderr]});
It works perfectly (Yes, I know I can do inherit, shush :p ). I can type inside my console and it'll accept it with no issues.
However, if I do it like this:
var spawn = require("child_process").spawn;
var exe = spawn("NameOfExe.exe", [/*arguments*/], {stdio: ["pipe", process.stdout, process.stderr]});
exe.stdin.write("Password\n");
then the executable doesn't even receive the stdin; it immediately goes to Execution Failed.
I'm completely at a loss for this. Any suggestions?
EDIT:
I think I may be onto something!
So I'm 99.99% certain that the executable is using C# and Console.ReadKey to get the password. However, according to Microsoft, an exception gets thrown whenever the In property is redirected somewhere else.
So this explains it, but is there a way around this using Node, or am I at the mercy of the people who made this executable?
The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.
You figured it out. It uses native bindings to the hardware controller/HAL, and not the shell, to handle stdio.

Node.js request stream ends/stalls when piped to writable file stream

I'm trying to pipe() data from Twitter's Streaming API to a file using modern Node.js Streams. I'm using a library I wrote called TweetPipe, which leverages EventStream and Request.
Setup:
var TweetPipe = require('tweet-pipe')
, fs = require('fs');
var tp = new TweetPipe(myOAuthCreds);
var file = fs.createWriteStream('./tweets.json');
Piping to STDOUT works and stream stays open:
tp.stream('statuses/filter', { track: ['bieber'] })
.pipe(tp.stringify())
.pipe(process.stdout);
Piping to the file writes one tweet and then the stream ends silently:
tp.stream('statuses/filter', { track: ['bieber'] })
.pipe(tp.stringify())
.pipe(file);
Could anyone tell me why this happens?
it's hard to say from what you have here, it sounds like the stream is getting cleaned up before you expect. This can be triggered a number of ways, see here https://github.com/joyent/node/blob/master/lib/stream.js#L89-112
A stream could emit 'end', and then something just stops.
Although I doubt this is the problem, one thing that concerns me is this
https://github.com/peeinears/tweet-pipe/blob/master/index.js#L173-174
destroy should be called after emitting error.
I would normally debug a problem like this by adding logging statements until I can see what is not happening right.
Can you post a script that can be run to reproduce?
(for extra points, include a package.json that specifies the dependencies :)
According to this, you should create an error handler on the stream created by tp.

Resources