From c2dbeaaf1504411e011ab2edbbf2a080c2364593 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 6 Dec 2018 15:20:35 +0000 Subject: [PATCH] fix(NA): deprecation warnings for Buffer use. --- lib/byline.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/byline.js b/lib/byline.js index 21843cb..56dfacd 100644 --- a/lib/byline.js +++ b/lib/byline.js @@ -140,16 +140,26 @@ LineStream.prototype._flush = function(done) { this._pushBuffer(this._chunkEncoding, 0, done); }; +LineStream.prototype._toBuffer = function(string, encoding) { + if (typeof Buffer.from === 'function') { + return Buffer.from(string, encoding); + } else { + // this was deprecated in node v5 in favor + // of Buffer.from(string, encoding) + return new Buffer(string, encoding); + } +} + // see Readable::push LineStream.prototype._reencode = function(line, chunkEncoding) { if (this.encoding && this.encoding != chunkEncoding) { - return new Buffer(line, chunkEncoding).toString(this.encoding); + return this._toBuffer(line, chunkEncoding).toString(this.encoding); } else if (this.encoding) { // this should be the most common case, i.e. we're using an encoded source stream return line; } else { - return new Buffer(line, chunkEncoding); + return this._toBuffer(line, chunkEncoding); } };