From 641d3e60211631c2c680b3e63e8aa7acfa00e338 Mon Sep 17 00:00:00 2001 From: Yusuke Kawasaki Date: Wed, 17 Oct 2018 11:02:40 +0900 Subject: [PATCH] re #98: Buffer.from() and Buffer.alloc() node v4.5+ required --- lib/bufferish-buffer.js | 19 +++---------------- lib/ext-unpacker.js | 2 +- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/bufferish-buffer.js b/lib/bufferish-buffer.js index abbd985..1c2ca1e 100644 --- a/lib/bufferish-buffer.js +++ b/lib/bufferish-buffer.js @@ -3,21 +3,12 @@ var Bufferish = require("./bufferish"); var Buffer = Bufferish.global; -var exports = module.exports = Bufferish.hasBuffer ? alloc(0) : []; +var exports = module.exports = Buffer ? Buffer.alloc(0) : []; -exports.alloc = Bufferish.hasBuffer && Buffer.alloc || alloc; +exports.alloc = Buffer && Buffer.alloc; exports.concat = Bufferish.concat; exports.from = from; -/** - * @param size {Number} - * @returns {Buffer|Uint8Array|Array} - */ - -function alloc(size) { - return new Buffer(size); -} - /** * @param value {Array|ArrayBuffer|Buffer|String} * @returns {Buffer} @@ -38,9 +29,5 @@ function from(value) { } // Array-like to Buffer - if (Buffer.from && Buffer.from.length !== 1) { - return Buffer.from(value); // node v6+ - } else { - return new Buffer(value); // node v4 - } + return Buffer.from(value); // node v4.5+ } diff --git a/lib/ext-unpacker.js b/lib/ext-unpacker.js index 03e525d..e0257e8 100644 --- a/lib/ext-unpacker.js +++ b/lib/ext-unpacker.js @@ -47,7 +47,7 @@ function setExtUnpackers(codec) { } if (Bufferish.hasBuffer) { - codec.addExtUnpacker(0x1B, unpackClass(Buffer)); + codec.addExtUnpacker(0x1B, Buffer.from); } }