Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Last active March 27, 2021 07:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toptensoftware/0237b4738056bf1b59a6573062eb40ff to your computer and use it in GitHub Desktop.
Save toptensoftware/0237b4738056bf1b59a6573062eb40ff to your computer and use it in GitHub Desktop.
Synchronously read a file line by line with NodeJS
let fs = require('fs');
let string_decoder = require('string_decoder');
// Synchronously read the content of a text file one line at a time
// Note, caller must complete the iteration for file to be closed
// ie: don't break out early from the loop calling this function
function* getFileLinesSync(filename, encoding)
{
let fd = fs.openSync(filename);
let buf = Buffer.allocUnsafe(32768);
let pos = 0;
let decoder = new string_decoder.StringDecoder(encoding || 'UTF8');
let lineStart = "";
while (true)
{
// Read buffer
let bytesRead = fs.readSync(fd, buf, 0, buf.length, pos);
pos += bytesRead;
// Decode string
let str;
if (bytesRead < buf.length)
str = lineStart + decoder.end(buf.subarray(0, bytesRead));
else
str = lineStart + decoder.write(buf);
// Split into lines and yield the complete ones
let lines = str.split(/\r?\n/);
for (let i=0; i<lines.length - 1; i++)
{
yield lines[i];
}
// The last line is the start of the first line in the next chunk
lineStart = lines[lines.length - 1];
// quit if eof
if (bytesRead < buf.length)
break;
}
// Final line
yield lineStart;
fs.closeSync(fd);
}
module.exports = {
getFileLinesSync,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment