-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheachline.js
More file actions
179 lines (149 loc) · 3.8 KB
/
eachline.js
File metadata and controls
179 lines (149 loc) · 3.8 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
var stream = require('stream');
var Transform = stream.Transform;
var type = require("type-of");
function eachline(a,b,c){
var signature = Array.prototype.map.call(arguments, type).join();
var t = new Transformer();
switch(signature){
//stream.pipe(eachline(transformer)).pipe(stdio)
case "function":
t.encoding = "utf8";
t.ondata = a;
return t;
//stream.pipe(eachline("hex", transformer)).pipe(stdio)
case "string":
case "string,function":
t.encoding = a;
t.ondata = b;
return t;
//eachline(stream, ondata);
case "object":
case "object,function":
t.encoding = "utf8";
t.ondata = b;
a.pipe(t).pipe(new Dummy());
return t;
//eachline(stream, "hex", ondata);
case "object,string": //Readable,string
case "object,string,function": //Readable,string,function
t.encoding = b;
t.ondata = c;
a.pipe(t).pipe(new Dummy());
return t;
case "":
t.encoding = "utf8";
return t;
}
throw new Error("I don't know what you want");
};
module.exports = eachline;
module.exports.in = function(location, cb){
var args = Array.prototype.slice.call(arguments);
var web = /(https?):\/\//.exec(location);
if(web){
var ev = new (require('events').EventEmitter)();
location = require('url').parse(location);
location.agent = false;
require(web[1]).get(location, function(res){
args[0] = res;
var t = eachline.apply(this, args);
Object.keys(ev._events).forEach(function(event){
t._events[event] = ev._events[event];
});
})
.end();
return ev;
}
else {
args[0] = require('fs').createReadStream(location);
return eachline.apply(this, args);
}
}
//a dummy writer is needed if we're not pipe'in
function Dummy(){
stream.Writable.call(this);
}
Dummy.prototype = Object.create(stream.Writable.prototype, { constructor: { value: Dummy }});
Dummy.prototype._write = function(line, encoding, done) {
done();
};
function Transformer() {
this._line = 0;
Transform.call(this);
this._readableState.objectMode=true;
}
Transformer.prototype = Object.create(Transform.prototype, { constructor: { value: Transformer }});
module.exports.Transformer = Transformer;
function findEOL(bytes, i){
for(;i<bytes.length;i++){
var c = bytes[i];
if(c===13 || c===10){ //CR or LF
return i;
}
}
return false;
}
Transformer.prototype._transform = function(chunk, encoding, done) {
var xform = this,
start = 0,
enc = !/binary|buffer/.test(this.encoding)? this.encoding : false,
eol;
function next() {
if((eol=findEOL(chunk, start))!==false){
var line, hasCRLF = chunk[eol]===13 && chunk[eol+1]===10;
if(xform.remnant){
line = Buffer.concat([xform.remnant, chunk.slice(start, eol)]);
delete xform.remnant;
}
else {
line = chunk.slice(start, eol);
}
start = eol+(hasCRLF? 2:1);
if(enc)
line= line.toString(enc);
setImmediate(function () {
xform._pushline(line, next);
});
}
else {
if(xform.remnant){//no LF found in this chunk
xform.remnant = Buffer.concat([xform.remnant, chunk]);;
}
else {
xform.remnant = chunk.slice(start);
}
return done();
}
}
next();
};
Transformer.prototype._pushline = function(line, next){
var sigd = false;
var xform = this;
function signaled(data) {
if(sigd) return;
sigd=true;
if(data) xform.push(data, xform.encoding);
next && next();
}
if(xform.ondata){
line = xform.ondata(line, xform._line++, signaled);
if(xform.ondata.length === 3) return;
}
else {
xform._line++;
}
signaled(line);
};
Transformer.prototype._flush = function(done) {
if(this.remnant) {
var line = this.encoding && !/binary|buffer/.test(this.encoding)? this.remnant.toString(this.encoding) : this.remnant;
this._pushline(line);
}
done();
};
Object.defineProperty(Transformer.prototype, 'finished', {
get: function () {
return this._writableState.ending;
}
});