diff --git a/lib/nodejsUtils.js b/lib/nodejsUtils.js index a9f80735..83001064 100644 --- a/lib/nodejsUtils.js +++ b/lib/nodejsUtils.js @@ -14,13 +14,16 @@ module.exports = { * @return {Buffer} a new Buffer. */ newBufferFrom: function(data, encoding) { - // XXX We can't use `Buffer.from` which comes from `Uint8Array.from` - // in nodejs v4 (< v.4.5). It's not the expected implementation (and - // has a different signature). - // see https://github.com/nodejs/node/issues/8053 - // A condition on nodejs' version won't solve the issue as we don't - // control the Buffer polyfills that may or may not be used. - return new Buffer(data, encoding); + if (Buffer.from && Buffer.from !== Uint8Array.from) { + return Buffer.from(data, encoding); + } else { + if (typeof data === "number") { + // Safeguard for old Node.js versions. On newer versions, + // Buffer.from(number) / Buffer(number, encoding) already throw. + throw new Error("The \"data\" argument must not be a number"); + } + return new Buffer(data, encoding); + } }, /** * Create a new nodejs Buffer with the specified size. @@ -31,7 +34,9 @@ module.exports = { if (Buffer.alloc) { return Buffer.alloc(size); } else { - return new Buffer(size); + var buf = new Buffer(size); + buf.fill(0); + return buf; } }, /**