Repository navigation
Expand file tree
/
Copy pathreadlines.js
More file actions
196 lines (157 loc) · 5.7 KB
/
Copy pathreadlines.js
File metadata and controls
196 lines (157 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const fs = require('fs');
const LF = 0x0a; // \n - Unix/Linux/macOS
const CR = 0x0d; // \r - Classic Mac OS / part of Windows CRLF
const STDIN_FD = 0;
/**
* @class
*/
class LineByLine {
constructor(file, options) {
options = options || {};
if (!options.readChunk) options.readChunk = 1024;
if (typeof file === 'number') {
this.fd = file;
} else {
this.fd = fs.openSync(file, 'r');
}
this.options = options;
this.isStdin = this.fd === STDIN_FD;
this.reset();
}
_searchInBuffer(buffer) {
for (let i = 0; i < buffer.length; i++) {
const byte = buffer[i];
if (byte === LF || byte === CR) {
return i;
}
}
return -1;
}
reset() {
this.eofReached = false;
this.linesCache = [];
this.fdPosition = 0;
this.lastChunkEndedWithCR = false;
this.lineIsComplete = false;
}
close() {
// Don't close stdin
if (!this.isStdin) {
fs.closeSync(this.fd);
}
this.fd = null;
}
_extractLines(buffer, isEof) {
const lines = [];
let lineStart = 0;
// If last chunk ended with CR and this one starts with LF, skip the LF
if (this.lastChunkEndedWithCR && buffer.length > 0 && buffer[0] === LF) {
lineStart = 1;
}
this.lastChunkEndedWithCR = false;
this.lineIsComplete = false;
for (let i = lineStart; i < buffer.length; i++) {
const byte = buffer[i];
if (byte === LF) {
// LF found - extract line (without the LF)
lines.push(buffer.slice(lineStart, i));
lineStart = i + 1;
} else if (byte === CR) {
const lineEnd = i;
// Check if this is the last byte in the buffer
if (i + 1 >= buffer.length) {
// CR at end of buffer - might be start of CRLF
if (!isEof) {
// Not at EOF, mark that we ended with CR
this.lastChunkEndedWithCR = true;
}
// Extract line without the CR
lines.push(buffer.slice(lineStart, lineEnd));
lineStart = i + 1;
} else if (buffer[i + 1] === LF) {
// CRLF - skip both characters
lines.push(buffer.slice(lineStart, lineEnd));
i++; // Skip the LF
lineStart = i + 1;
} else {
// Standalone CR (classic Mac)
lines.push(buffer.slice(lineStart, lineEnd));
lineStart = i + 1;
}
}
}
// Add any remaining content (incomplete line without newline)
if (lineStart < buffer.length) {
lines.push(buffer.slice(lineStart));
} else if (lineStart === buffer.length) {
this.lineIsComplete = true;
}
return lines;
}
_readChunk(lineLeftovers) {
let totalBytesRead = 0;
let bytesRead;
const buffers = [];
do {
const readBuffer = Buffer.alloc(this.options.readChunk);
// For stdin (non-seekable), pass null for position to read from current position
const position = this.isStdin ? null : this.fdPosition;
bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk, position);
totalBytesRead = totalBytesRead + bytesRead;
this.fdPosition = this.fdPosition + bytesRead;
buffers.push(readBuffer);
} while (bytesRead && this._searchInBuffer(buffers[buffers.length-1]) === -1);
let bufferData = Buffer.concat(buffers);
if (bytesRead < this.options.readChunk) {
this.eofReached = true;
bufferData = bufferData.slice(0, totalBytesRead);
}
if (totalBytesRead) {
this.linesCache = this._extractLines(bufferData, this.eofReached);
if (lineLeftovers) {
if (this.linesCache.length > 0) {
this.linesCache[0] = Buffer.concat([lineLeftovers, this.linesCache[0]]);
} else {
this.linesCache.push(lineLeftovers);
}
}
}
return totalBytesRead;
}
next() {
// Check for null specifically, not falsy (fd 0 is stdin and is valid)
if (this.fd === null) return null;
let line = null;
if (this.eofReached && this.linesCache.length === 0) {
return line;
}
let bytesRead;
if (!this.linesCache.length) {
bytesRead = this._readChunk();
}
if (this.linesCache.length) {
line = this.linesCache.shift();
// Check if this might be an incomplete line (no newline found yet)
// This happens when we read a chunk that doesn't contain a newline
if (!this.eofReached && !this.lineIsComplete && this.linesCache.length === 0) {
bytesRead = this._readChunk(line);
if (bytesRead && this.linesCache.length) {
line = this.linesCache.shift();
}
}
}
if (this.eofReached && this.linesCache.length === 0) {
this.close();
}
return line;
}
/**
* Returns true if the last line has been read and there are no more lines.
* @returns {boolean}
*/
isLast() {
return this.fd === null || (this.eofReached && this.linesCache.length === 0);
}
}
module.exports = LineByLine;