Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an entire text stream in node.js?

Tags:

In RingoJS there's a function called read which allows you to read an entire stream until the end is reached. This is useful when you're making a command line application. For example you may write a tac program as follows:

#!/usr/bin/env ringo  var string = system.stdin.read(); // read the entire input stream var lines = string.split("\n");   // split the lines  lines.reverse();                  // reverse the lines  var reversed = lines.join("\n");  // join the reversed lines system.stdout.write(reversed);    // write the reversed lines 

This allows you to fire up a shell and run the tac command. Then you type in as many lines as you wish to and after you're done you can press Ctrl+D (or Ctrl+Z on Windows) to signal the end of transmission.

I want to do the same thing in node.js but I can't find any function which would do so. I thought of using the readSync function from the fs library to simulate as follows, but to no avail:

fs.readSync(0, buffer, 0, buffer.length, null); 

The file descriptor for stdin (the first argument) is 0. So it should read the data from the keyboard. Instead it gives me the following error:

Error: ESPIPE, invalid seek     at Object.fs.readSync (fs.js:381:19)     at repl:1:4     at REPLServer.self.eval (repl.js:109:21)     at rli.on.self.bufferedCmd (repl.js:258:20)     at REPLServer.self.eval (repl.js:116:5)     at Interface.<anonymous> (repl.js:248:12)     at Interface.EventEmitter.emit (events.js:96:17)     at Interface._onLine (readline.js:200:10)     at Interface._line (readline.js:518:8)     at Interface._ttyWrite (readline.js:736:14) 

How would you synchronously collect all the data in an input text stream and return it as a string in node.js? A code example would be very helpful.

like image 200
Aadit M Shah Avatar asked Nov 16 '12 05:11

Aadit M Shah


People also ask

How do I read a node JS stream file?

Next, you will create a function below the switch statement called read() with a single parameter: the file path for the file you want to read. This function will create a readable stream from that file and listen for the data event on that stream. The code also checks for errors by listening for the error event.

How do you use readableStream?

The ReadableStream() constructorThe constructor takes two objects as parameters. The first object is required, and creates a model in JavaScript of the underlying source the data is being read from. The second object is optional, and allows you to specify a custom queuing strategy to use for your stream.

Is buffer a stream in node JS?

Buffer: In Node. js to manipulate a stream of binary data, the buffer module can be included in the code. However, the buffer is a global object in Node. js, hence it is not required to import it in code using the required method.

How do I write a stream file in node JS?

To write to a file using streams, you need to create a new writable stream. You can then write data to the stream at intervals, all at once, or according to data availability from a server or other process, then close the stream for good once all the data packets have been written.


1 Answers

As node.js is event and stream oriented there is no API to wait until end of stdin and buffer result, but it's easy to do manually

var content = ''; process.stdin.resume(); process.stdin.on('data', function(buf) { content += buf.toString(); }); process.stdin.on('end', function() {     // your code here     console.log(content.split('').reverse().join('')); }); 

In most cases it's better not to buffer data and process incoming chunks as they arrive (using chain of already available stream parsers like xml or zlib or your own FSM parser)

like image 84
Andrey Sidorov Avatar answered Sep 29 '22 03:09

Andrey Sidorov