Skip to content

Commit 430807f

Browse files
committed
src: replace byline with custom splitter
The byline package is generating deprecation warnings when running strong-log-transformer under node 10. Since the package appears to be unmaintained, we are re-implementing the functionality here. The implementation is heavily inspired by byline, split, and split2, but simplified for our specific use case. The existing tests fully cover this new implementation, including the corner case of the input stream being truncated in the middle of a multi-byte UTF8 sequence. Resolves #7. Signed-off-by: Ryan Graham <[email protected]>
1 parent b7d05fe commit 430807f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/logger.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ var stream = require('stream');
99
var util = require('util');
1010
var fs = require('fs');
1111

12-
var byline = require('byline');
1312
var through = require('through');
1413
var duplexer = require('duplexer');
14+
var StringDecoder = require('string_decoder').StringDecoder;
1515

1616
module.exports = Logger;
1717

@@ -30,7 +30,7 @@ var formatters = {
3030
function Logger(options) {
3131
var defaults = JSON.parse(JSON.stringify(Logger.DEFAULTS));
3232
options = util._extend(defaults, options || {});
33-
var catcher = new byline.LineStream;
33+
var catcher = deLiner();
3434
var emitter = catcher;
3535
var transforms = [
3636
objectifier(),
@@ -56,7 +56,7 @@ function Logger(options) {
5656

5757
transforms.push(formatters[options.format](options));
5858

59-
// restore line endings that were removed by byline
59+
// restore line endings that were removed by line splitting
6060
transforms.push(reLiner());
6161

6262
for (var t in transforms) {
@@ -66,6 +66,34 @@ function Logger(options) {
6666
return duplexer(catcher, emitter);
6767
}
6868

69+
function deLiner() {
70+
var decoder = new StringDecoder('utf8');
71+
var last = '';
72+
73+
return new stream.Transform({
74+
transform(chunk, _enc, callback) {
75+
last += decoder.write(chunk);
76+
var list = last.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
77+
last = list.pop();
78+
for (var i = 0; i < list.length; i++) {
79+
// swallow empty lines
80+
if (list[i]) {
81+
this.push(list[i]);
82+
}
83+
}
84+
callback();
85+
},
86+
flush(callback) {
87+
// incomplete UTF8 sequences become UTF8 replacement characters
88+
last += decoder.end();
89+
if (last) {
90+
this.push(last);
91+
}
92+
callback();
93+
},
94+
});
95+
}
96+
6997
function reLiner() {
7098
return through(appendNewline);
7199

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"test": "tap --100 test/test-*"
2828
},
2929
"dependencies": {
30-
"byline": "^5.0.0",
3130
"duplexer": "^0.1.1",
3231
"minimist": "^1.2.0",
3332
"through": "^2.3.4"

0 commit comments

Comments
 (0)