Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support reading from a pipe, such as stdin #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use strict';

const lineByLine = require('./readlines.js');
const liner = new lineByLine('./test/fixtures/normalFile.txt');

const fname = process.argv.length > 2 ? process.argv[2] : './test/fixtures/normalFile.txt';
const enc = process.argv.length > 3 ? process.argv[3] : 'ascii';
console.log('Reading', enc, 'encoded file', fname);

const liner = new lineByLine(fname === '-' ? 0 : fname);

let line;
let lineNumber = 0;

while (line = liner.next()) {
console.log('Line ' + lineNumber + ': ' + line.toString('ascii'));
console.log('Line ' + lineNumber + ': ' + line.toString(enc));
lineNumber++;
}

console.log('end of line reached');
console.log('end of file reached');
20 changes: 16 additions & 4 deletions readlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LineByLine {
reset() {
this.eofReached = false;
this.linesCache = [];
this.seekable = true;
this.fdPosition = 0;
}

Expand Down Expand Up @@ -87,9 +88,18 @@ class LineByLine {
let bytesRead;
const buffers = [];
do {
const readBuffer = new Buffer(this.options.readChunk);
const readBuffer = Buffer.alloc(this.options.readChunk);

bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk, this.fdPosition);
if (this.seekable) {
try {
bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk, this.fdPosition);
} catch (e) {
this.seekable = false;
}
}
if (!this.seekable) {
bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk);
}
totalBytesRead = totalBytesRead + bytesRead;

this.fdPosition = this.fdPosition + bytesRead;
Expand All @@ -100,10 +110,11 @@ class LineByLine {
let bufferData = Buffer.concat(buffers);

if (bytesRead < this.options.readChunk) {
this.eofReached = true;
bufferData = bufferData.slice(0, totalBytesRead);
}

this.eofReached = bytesRead === 0;

if (totalBytesRead) {
this.linesCache = this._extractLines(bufferData);

Expand All @@ -116,11 +127,12 @@ class LineByLine {
}

next() {
if (!this.fd) return false;
if (this.fd === null) return false;

let line = false;

if (this.eofReached && this.linesCache.length === 0) {
this.close();
return line;
}

Expand Down
1 change: 1 addition & 0 deletions test/readlines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ test('should correctly processes NULL character in lines', (t) => {
t.equals(liner.next().toString(), 'line wi'+String.fromCharCode(0)+'th null', 'line 1: line with null');
t.equals(liner.next().toString(), 'another line without null', 'line 2: another line without null');

t.equals(liner.next(), false, 'line 3: false');
t.equals(liner.fd, null, 'fd is null');
t.end();
})