-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadly.js
173 lines (160 loc) · 4.05 KB
/
readly.js
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
var fs = require('fs');
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var stream = require('stream');
var Transform = stream.Transform;
function ReadlyEvent(filename, encoding, eol) {
if (encoding) {
this.encoding = encoding;
} else {
this.encoding = 'utf-8'
}
if (eol) {
this.eol = eol;
} else {
this.eol = require('os').EOL;
}
if (typeof filename === "string") {
try {
stats = fs.lstatSync(filename);
if (!stats.isFile()) {
throw new Error("filename is a directory name, not a file. " + filename);
}
this.filename = filename;
} catch (e) {
throw new Error("file does not exist " + filename);
}
} else {
this.stream = filename;
this.stream.setEncoding(this.encoding);
}
EventEmitter.call(this);
this.linesSent = 0;
this.lastLine;
this.start;
this.count;
}
util.inherits(ReadlyEvent, EventEmitter);
util.inherits(ReadlyTransform, Transform);
function ReadlyTransform(opts) {
Transform.call(this, opts);
if (opts && opts.encoding) {
this.encoding = opts.encoding;
} else {
this.encoding = 'utf-8'
}
if (opts && opts.eol) {
this.eol = opts.eol;
} else {
this.eol = require('os').EOL;
}
this.lastLine;
this.start = 0;
this.count;
this.linesSent = 0;
}
function handle(self, data, callback, finish) {
if (typeof data !== "string") {
data = data.toString();
}
var currLines = data.split(self.eol);
if (self.lastLine) {
data = self.lastLine + data;
}
var l = data.split(self.eol)
self.lastLine = l.pop();
if (self.lastLine.indexOf(self.eol) > -1) {
l.push(self.lastLine);
self.lastLine = null;
}
while (self.start && self.start > 0 && l.length > 0) {
l.shift();
self.start--;
}
if (l.length) {
if (self.count) {
while (l.length > 0 && self.linesSent < self.count) {
self.linesSent++;
callback(l.shift(), l.length);
}
} else {
while (l.length > 0) {
callback(l.shift(), l.length);
}
}
} else {
callback(null, 0);
}
if (self.count && self.linesSent >= self.count) {
if (finish) {
finish();
}
}
}
ReadlyTransform.prototype._transform = function(chunk, enc, callback) {
var self = this;
handle(this, chunk, function(line, left) {
if (line) {
self.push(line);
}
if (!left) {
callback();
}
});
};
ReadlyTransform.prototype._flush = function(callback) {
if (this.lastLine) {
this.push(this.lastLine);
}
callback();
}
ReadlyEvent.prototype.read = function(start, count) {
var self = this;
if (this.filename) {
this.stream = fs.createReadStream(self.filename, {
flags: 'r',
encoding: self.encoding,
fd: null,
mode: 0666,
bufferSize: 64 * 1024
});
}
if (!start) {
start = 0;
}
self.start = start;
self.count = count;
var finish = function() {
if (self.filename) {
self.stream.destroy();
}
self.linesSent = 0;
self.start = undefined;
self.count = undefined;
self.lastLine = undefined;
self.emit("end");
}
self.stream.on('data', function(data) {
handle(self, data, function(line) {
if (line) {
self.emit("line", line);
}
}, finish);
});
self.stream.on('end', function() {
if (self.lastLine && self.start < 1) {
self.emit("line", self.lastLine);
}
finish();
});
}
ReadlyEvent.prototype.readAll = function() {
this.read();
}
ReadlyEvent.prototype.readFirst = function(count) {
this.read(0, count);
}
module.exports = {
Emitter: ReadlyEvent,
Transform: ReadlyTransform
}