-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (86 loc) · 2.05 KB
/
index.js
File metadata and controls
97 lines (86 loc) · 2.05 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
var through = require('through3')
, LF = '\n'.charCodeAt(0);
/**
* Reads stream buffers and writes arrays of lines.
*
* Designed to be piped to another transform stream that
* operates on the array of lines.
*
* ## Options
*
* @param encoding The encoding to use when converting buffers to strings.
*/
function Line(options) {
options = options || {};
this.encoding = options.encoding || 'utf8';
this.eol = /\r?\n/;
if(options.buffer) {
this.body = [];
}
this.options = options;
}
/**
* Transform function.
*/
function transform(chunk, encoding, cb) {
var lines = []
, i = 0
, s = 0
, b
, line;
if(Array.isArray(chunk)) {
lines = chunk;
}else if(typeof chunk === 'string') {
lines = chunk.split(this.eol);
}else if(Buffer.isBuffer(chunk)) {
if(this.buffer && this.buffer.length) {
chunk = Buffer.concat(
[this.buffer, chunk], this.buffer.length + chunk.length);
}
while((b = chunk[i]) !== undefined) {
if(b === LF) {
line = new Buffer(i - s);
chunk.copy(line, 0, s, i);
s = i + 1;
lines.push(line.toString(this.encoding));
this.buffer = new Buffer(chunk.length - i - 1);
chunk.copy(this.buffer, 0, i + 1);
}
i++;
}
// nothing was found
if(!line) {
this.buffer = chunk;
}
}else{
return cb(new Error('line stream accepts buffers or strings'));
}
// how to handle the lines
if(this.body) {
this.body = this.body.concat(lines);
}else{
this.push(lines);
this.emit('lines', lines);
}
cb();
}
function flush(cb) {
var lines = [];
// unterminated last line, need to flush it
if(this.buffer) {
lines = [this.buffer.toString(this.encoding)];
if(this.body) {
this.body = this.body.concat(lines);
}else{
this.push(lines);
}
}
// buffered array of all lines
if(this.body) {
this.push(this.body);
}
this.emit('lines', this.body || lines);
this.buffer = null;
cb();
}
module.exports = through.transform(transform, flush, {ctor: Line});