Show file lines with delay in Node.js - javascript

I am trying to make a program in node.js that reads a text file line by line and shows each of them with a delay of 2 seconds. The code I am testing is the following.
var fs = require('fs'),
readline = require('readline');
var FileSystem_Lectura=function()
{
this._rd=null;
}
FileSystem_Lectura.prototype.abrirArchivoCSV=function(nombreArchivo)
{
this._rd = readline.createInterface
({
input: fs.createReadStream(nombreArchivo),
output: process.stdout,
terminal: false
});
}
FileSystem_Lectura.prototype.leerArchivoCSV=function()
{
self=this;
this._rd.on('line',self.mostraLineasDelay);
}
FileSystem_Lectura.prototype.mostraLineasDelay=function(linea)
{
setTimeout(self.mostraLinea,20000,linea);
}
FileSystem_Lectura.prototype.mostraLinea=function(linea)
{
console.log("Linea:"+ linea);
}
var FS =new FileSystem_Lectura();
FS.abrirArchivoCSV(process.argv[2]);
FS.leerArchivoCSV();
The problem is that settimeout shows me all the lines together, it does not apply the delay. Except for the first line. So, how can I make it work properly?
From already thank you very much

Use pause/resume on your stream :
var fs = require('fs'),
readline = require('readline');
var FileSystem_Lectura=function()
{
this._rd=null;
}
FileSystem_Lectura.prototype.abrirArchivoCSV=function(nombreArchivo)
{
this._rd = readline.createInterface
({
input: fs.createReadStream(nombreArchivo),
output: process.stdout,
terminal: false
});
}
FileSystem_Lectura.prototype.leerArchivoCSV=function()
{
self=this;
this._rd.on('line',self.mostraLineasDelay);
}
FileSystem_Lectura.prototype.mostraLineasDelay=function(linea)
{
this._rd.pause();
setTimeout(self.mostraLinea,2000,linea);
}
FileSystem_Lectura.prototype.mostraLinea=function(linea)
{
console.log("Linea:"+ linea);
this._rd.resume();
}
Also, 2 seconds is 2000ms, not 20000.

This sort of thing is way easier these days:
import {createInterface} from 'readline';
import {createReadStream} from 'fs';
import {pipe, delay} from 'iter-ops';
const file = createInterface(createReadStream('./my-file.txt'));
const i = pipe(file, delay(2000)); //=> AsyncIterable<string>
(async function () {
for await(const line of i) {
console.log(line); // prints line-by-line, with 2s delays
}
})();
Libraries readline and fs are now standard. And iter-ops is an extra module.

Related

How to find condition of code in file using readline in nodejs?

I tried to read line by line in file using node js.but I have doubt how to get only condition content (if,for) using node js.
data.js
const fs = require('fs')
const path = require("path");
const file_path = path.resolve('./sample')
const check_console = require('./tests_helper')
check_console.files_paths(file_path).then(result =>console.log(result))
.catch(error =>console.log(error))
file.js
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(files[i])
});
lineReader.on('line', function (line) {
console.log("Line of Code "+line)
});
I got output
Line of Code console.log('hai')
Line of Code
Line of Code console.log('hai')
Line of Code
Line of Code if(10 == 10)
Line of Code {
Line of Code
Line of Code }
but I want Output
Line of Code if(10 == 10)
Line of Code {
Line of Code
Line of Code }
You can use "string".includes("substring"); method to check if that substring exists or not.
I have prepared a quick logic for your problem. line.includes("if") will check if a substring exists and some other condition check I have given to maintain the curly braces.
const fs = require('fs');
const readline = require('readline');
const r1 = readline.createInterface({
input: require('fs').createReadStream(__dirname + '/test.js')
});
found = false;
curlbrace = 0;
r1.on('line', function (line) {
if (line.includes("if")) {
found = true; //setting flag true if found search string
}
if (found && line.includes("{")) {
curlbrace++; //incrementing if breace is started
}
if (found && line.includes("}")) {
curlbrace--; //decrementing if brace is ended
}
if (found && curlbrace >= 0) {
console.log(line); //console the searched result
if (curlbrace == 0) {
found = false; //setting false to prevent it to show other contents
}
}
});

Delay using readline in nodejs

function readFile(){
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(FILE_PATH)
});
lineReader.on('line', function (line) {
setTimeout(() => {
console.log("HEYYYYY");
}, 10000);
});
}
Why does this only waits 10 seconds once , and the prints 'hey' ? I want to print hey each 10 seconds but it's not working. No idea why.
Edit: This is going to be repeated by the amount of lines that there are on a file (look at the listener 'line') I need to delay 10s between each line.
I had the same problem and I solved it with the "Example: Read File Stream Line-by-Line" found in: https://nodejs.org/api/readline.html
In your case it would be something like this:
const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream(FILE_PATH);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
await sleep(10000)
}
}
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
This example would print you a line every 10 seconds.
It's not waiting 10 seconds once. its just that each line is read so fast, there's almost not difference in the start time. you can add a variable that increase the delay by 10 seconds in each callback so you each line is print each 10 seconds.
function readFile(){
var delay = 0;
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(FILE_PATH)
});
lineReader.on('line', function (line) {
delay += 10000;
setTimeout(() => {
console.log("HEYYYYY");
}, 10000+delay);
});
}

How to update in html page during nodejs readline process

I want to dynamically change the content of a element in my html page during nodejs readline process.
Here is a jsfiddle example to shown the display effect I want to fulfill:
https://jsfiddle.net/09kuyn7v/
But I want to dynamically display lines from my local file, but not from an array defined within the function as in the jsfiddle example.
I have used readline module in my read-file-version clickTest() function:
function clickTest(){
var fs = require('fs');
var lineReader = require('readline').createInterface({
input: fs.createReadStream(filePath)
});
lineReader.on('line', function(line){
document.getElementById("demo").innerHTML += line;
});
}
But when I click the button, the page was just like being freezed and then the lines were displayed simultaneously (not one by one as shown in the jsfiddle example above).
First of all, every time you call that function you do require('readline') and require('fs') so I would move that up the script.
I would suggest two approaches:
Pausing read
var readline = require('readline');
var fs = require('fs');
function clickTest(){
var lineReader = readline.createInterface({
input: fs.createReadStream(filePath)
});
lineReader.on('line', function(line){
// pause emitting of lines...
lineReader.pause();
// write line to dom
document.getElementById("demo").innerHTML += line;
// Resume after some time
setTimeout(function(){
lineReader.resume();
}, 1000);
});
lineReader.on('end', function(){
lineReader.close();
});
}
This approach should read one line, then pause and resume after some time you specify.
Buffering lines
var readline = require('readline');
var fs = require('fs');
var lines = [];
function clickTest(){
var lineReader = readline.createInterface({
input: fs.createReadStream(filePath)
});
lineReader.on('line', function(line){
lines.push(line)
});
lineReader.on('end', function(){
lineReader.close();
printLine(0);
});
}
function printLine(index){
// write line to dom
document.getElementById("demo").innerHTML += lines[index];
if (index < lines.length - 1){
setTimeout(function(){
printLine(index + 1);
}, 1000);
}
}
This approach will save all the lines into an array and then slowly prints them out.
Please note that I haven't got node-webkit to actually test it, so you might find a bug in the code, but it should give you general idea

What is the most efficient way to read only the first line of a file in Node JS?

Imagine you have many long text files, and you need to only extract data from the first line of each one (without reading any further content). What is the best way in Node JS to do it?
Thanks!
I ended up adopting this solution, which seems the most performant I've seen so far:
var fs = require('fs');
var Q = require('q');
function readFirstLine (path) {
return Q.promise(function (resolve, reject) {
var rs = fs.createReadStream(path, {encoding: 'utf8'});
var acc = '';
var pos = 0;
var index;
rs
.on('data', function (chunk) {
index = chunk.indexOf('\n');
acc += chunk;
index !== -1 ? rs.close() : pos += chunk.length;
})
.on('close', function () {
resolve(acc.slice(0, pos + index));
})
.on('error', function (err) {
reject(err);
})
});
}
I created a npm module for convenience, named "firstline".
Thanks to #dandavis for the suggestion to use String.prototype.slice()!
There's a built-in module almost for this case - readline. It avoids messing with chunks and so forth. The code would look like the following:
const fs = require('fs');
const readline = require('readline');
async function getFirstLine(pathToFile) {
const readable = fs.createReadStream(pathToFile);
const reader = readline.createInterface({ input: readable });
const line = await new Promise((resolve) => {
reader.on('line', (line) => {
reader.close();
resolve(line);
});
});
readable.close();
return line;
}
I know this doesn't exactly answer the question but for those who are looking for a READABLE and simple way to do so:
const fs = require('fs').promises;
async function getFirstLine(filePath) {
const fileContent = await fs.readFile(filePath, 'utf-8');
return (fileContent.match(/(^.*)/) || [])[1] || '';
}
NOTE:
naturaly, this will only work with text files, which I assumed you used from your description
this will work with empty files and will return an empty string
this regexp is very performant since it is simple (no OR conditions`or complex matches) and only reads the first line
Please try this:
https://github.com/yinrong/node-line-stream-util#get-head-lines
It unpipe the upstream once got the head lines.
Node.js >= 16
In all current versions of Node.js, readline.createInterface can be used as an async iterable, to read a file line by line - or just for the first line. This is also safe to use with empty files.
Unfortunately, the error handling logic is broken in versions of Node.js before 16, where certain file system errors may go uncaught even if the code is wrapped in a try-catch block because of the way asynchronous errors are propagated in streams. So I recommend using this method only in Node.js >= 16.
import { createReadStream } from "fs";
import { createInterface } from "readline";
async function readFirstLine(path) {
const inputStream = createReadStream(path);
try {
for await (const line of createInterface(inputStream)) return line;
return ''; // If the file is empty.
}
finally {
inputStream.destroy(); // Destroy file stream.
}
}
const firstLine = await readFirstLine("path/to/file");
//Here you go;
var lineReader = require('line-reader');
var async = require('async');
exports.readManyFiles = function(files) {
async.map(files,
function(file, callback))
lineReader.open(file, function(reader) {
if (reader.hasNextLine()) {
reader.nextLine(function(line) {
callback(null,line);
});
}
});
},
function(err, allLines) {
//do whatever you want to with the lines
})
}

process.stdout.write() not working in Node.js readline CLI program

I am using the readline module to create a command line interface (CLI) for an application in Node.js.
The problem is that I can not scroll up to view the past commands as I usually can in Terminal. My CLI is just a fixed window and if I print too much out to the screen, I lose information at the top and there is no way to scroll up to see it.
(I am running my program on Mac OSX Mavericks)
Thanks in advance.
Code Snippet:
var readline = require('readline');
var Cli = function () {
this.txtI = process.stdin;
this.txtO = process.stdout;
process.stdout.write('CLI initialized.');
this.rl = readline.createInterface({input: this.txtI, output: this.txtO });
this.rl.setPrompt('>>>');
this.rl.prompt();
this.rl.on('line', function(line) {
var input = line.toString().trim();
if (input) {
this.txtO.write('cmd: ' + input);
}
this.rl.prompt();
}.bind(this)).on('close', function() {
this.txtO.write('Have a great day!');
process.exit(0);
}.bind(this));
};
new Cli();
Save this file as snippet.js and run
node snippet.js
in terminal.
It probably is working, just readline is overwriting your line. Try outputting multiple lines:
process.stdout.write("1\n2\n3\n4\n5");
Readline is quite an awesome module. History is already there. As is the possibility to add completion. Try the snippet below.
var readline = require('readline');
function createCLI(opt) {
var rl = readline.createInterface({
input : opt.input,
output : opt.output,
terminal : opt.terminal || true,
completer : opt.completer ||
function asyncCompleter(linePartial, callback){
var completion = linePartial.split(/[ ]+/);
callback(null, [completion, linePartial]);
}
});
rl.on('line', function(line) {
if( !line.trim() ){ this.prompt(); }
else { this.write(line); }
}).on('close', function() {
this.output.write('\n Have a great day!');
process.exit(0);
}).setPrompt(' > ');
rl.output.write(' CLI initialized\n');
return rl;
}
var cli = createCLI({
input : process.stdin,
output : process.stdout
});
cli.prompt();

Categories