1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | var stream = require('stream'), util = require('util');
|
23 |
|
24 | function byline(readStream, options) {
|
25 | return module.exports.createStream(readStream, options);
|
26 | }
|
27 | exports.default = byline;
|
28 |
|
29 | module.exports.createStream = function (readStream, options) {
|
30 | if (readStream) {
|
31 | return createLineStream(readStream, options);
|
32 | }
|
33 | else {
|
34 | return new LineStream(options);
|
35 | }
|
36 | };
|
37 | function createLineStream(readStream, options) {
|
38 | if (!readStream) {
|
39 | throw new Error('expected readStream');
|
40 | }
|
41 | if (!readStream.readable) {
|
42 | throw new Error('readStream must be readable');
|
43 | }
|
44 | var ls = new LineStream(options);
|
45 | readStream.pipe(ls);
|
46 | return ls;
|
47 | }
|
48 | exports.createLineStream = createLineStream;
|
49 |
|
50 |
|
51 |
|
52 | module.exports.LineStream = LineStream;
|
53 | function LineStream(options) {
|
54 | stream.Transform.call(this, options);
|
55 | options = options || {};
|
56 |
|
57 |
|
58 | this._readableState.objectMode = true;
|
59 | this._lineBuffer = [];
|
60 | this._keepEmptyLines = options.keepEmptyLines || false;
|
61 | this._lastChunkEndedWithCR = false;
|
62 |
|
63 | this.on('pipe', function (src) {
|
64 | if (!this.encoding) {
|
65 |
|
66 | if (src instanceof stream.Readable) {
|
67 | this.encoding = src._readableState.encoding;
|
68 | }
|
69 | }
|
70 | });
|
71 | }
|
72 | util.inherits(LineStream, stream.Transform);
|
73 | LineStream.prototype._transform = function (chunk, encoding, done) {
|
74 |
|
75 | encoding = encoding || 'utf8';
|
76 | if (Buffer.isBuffer(chunk)) {
|
77 | if (encoding == 'buffer') {
|
78 | chunk = chunk.toString();
|
79 | encoding = 'utf8';
|
80 | }
|
81 | else {
|
82 | chunk = chunk.toString(encoding);
|
83 | }
|
84 | }
|
85 | this._chunkEncoding = encoding;
|
86 | var lines = chunk.split(/\r\n|\r|\n/g);
|
87 |
|
88 | if (this._lastChunkEndedWithCR && chunk[0] == '\n') {
|
89 | lines.shift();
|
90 | }
|
91 | if (this._lineBuffer.length > 0) {
|
92 | this._lineBuffer[this._lineBuffer.length - 1] += lines[0];
|
93 | lines.shift();
|
94 | }
|
95 | this._lastChunkEndedWithCR = chunk[chunk.length - 1] == '\r';
|
96 | this._lineBuffer = this._lineBuffer.concat(lines);
|
97 | this._pushBuffer(encoding, 1, done);
|
98 | };
|
99 | LineStream.prototype._pushBuffer = function (encoding, keep, done) {
|
100 |
|
101 | while (this._lineBuffer.length > keep) {
|
102 | var line = this._lineBuffer.shift();
|
103 |
|
104 | if (this._keepEmptyLines || line.length > 0) {
|
105 | if (!this.push(this._reencode(line, encoding))) {
|
106 |
|
107 | var self = this;
|
108 | setImmediate(function () {
|
109 | self._pushBuffer(encoding, keep, done);
|
110 | });
|
111 | return;
|
112 | }
|
113 | }
|
114 | }
|
115 | done();
|
116 | };
|
117 | LineStream.prototype._flush = function (done) {
|
118 | this._pushBuffer(this._chunkEncoding, 0, done);
|
119 | };
|
120 |
|
121 | LineStream.prototype._reencode = function (line, chunkEncoding) {
|
122 | if (this.encoding && this.encoding != chunkEncoding) {
|
123 | return Buffer.from(line, chunkEncoding).toString(this.encoding);
|
124 | }
|
125 | else if (this.encoding) {
|
126 |
|
127 | return line;
|
128 | }
|
129 | else {
|
130 | return Buffer.from(line, chunkEncoding);
|
131 | }
|
132 | };
|
133 |
|
\ | No newline at end of file |