Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot trigger 'end' event using CTRL D when reading from stdin

Tags:

In the following code

process.stdin.resume(); process.stdin.setEncoding('utf8');  process.stdin.on('data', function(chunk) {   process.stdout.write('data: ' + chunk); });  process.stdin.on('end', function() {   process.stdout.write('end'); }); 

i can't trigger the 'end' event using ctrl+D, and ctrl+C just exit without triggering it.

hello data: hello data data: data foo data: foo ^F data: ♠ ^N data: ♫ ^D data: ♦ ^D^D data: ♦♦ 
like image 278
Fadwa Avatar asked May 06 '13 15:05

Fadwa


People also ask

How do I trigger the end of stdin?

Alternatively, after typing the input into shell, you can press Ctrl + D to send EOF (end-of-file) to trigger the event handler in process. stdin. on("end", ...)

What is process Stdin resume ()?

process. stdin. resume() A Readable Stream that points to a standard input stream (stdin). Standard input streams are paused by default, so you must call process.


2 Answers

I'd change this (key combo Ctrl+D):

process.stdin.on('end', function() {     process.stdout.write('end'); }); 

To this (key combo Ctrl+C):

process.on('SIGINT', function(){     process.stdout.write('\n end \n');     process.exit(); }); 

Further resources: process docs

like image 108
thtsigma Avatar answered Sep 19 '22 09:09

thtsigma


I too came upon this problem and found the answer here: Github issue

The readline interface that is provided by windows itself (e.g. the one that you are using now) does not support ^D. If you want more unix-y behaviour, use the readline built-in module and set stdin to raw mode. This will make node interpret raw keypresses and ^D will work. See http://nodejs.org/api/readline.html.

If you are on Windows, the readline interface does not support ^D by default. You will need to change that per the linked instructions.

like image 36
Mark Avatar answered Sep 21 '22 09:09

Mark