diff --git a/tests/comp_bytes.js b/tests/comp_bytes.js index a2a649c8a..39c58fd15 100644 --- a/tests/comp_bytes.js +++ b/tests/comp_bytes.js @@ -2,11 +2,14 @@ var tape = require("tape"); var protobuf = require(".."); +var oldBufferImpl = Buffer.alloc === undefined; + // extends Buffer (CustomBuffer.prototype = Object.create(Buffer.prototype)).constructor = CustomBuffer; -function CustomBuffer() { - Buffer.call(this); +function CustomBuffer(arg, encodingOrOffset, length) { + Buffer.call(this, arg, encodingOrOffset, length); + CustomBuffer.toCustom(this); } CustomBuffer.isBuffer = Buffer.isBuffer.bind(Buffer); @@ -21,15 +24,24 @@ CustomBuffer.isCustom = function (b) { } CustomBuffer.from = function (valueOf, encodingOrOffset, length) { - return CustomBuffer.toCustom(Buffer.from(valueOf, encodingOrOffset, length)); + return CustomBuffer.toCustom(oldBufferImpl + ? new Buffer(valueOf, encodingOrOffset, length) + : Buffer.from(valueOf, encodingOrOffset, length) + ); } CustomBuffer.alloc = function (size, fill, encoding) { - return CustomBuffer.toCustom(Buffer.alloc(size, fill, encoding)); + return CustomBuffer.toCustom(oldBufferImpl + ? new Buffer(size, fill, encoding) + : Buffer.alloc(size, fill, encoding) + ); } CustomBuffer.allocUnsafe = function (size) { - return CustomBuffer.toCustom(Buffer.allocUnsafe(size)); + return CustomBuffer.toCustom(oldBufferImpl + ? new Buffer(size) + : Buffer.allocUnsafe(size) + ); } CustomBuffer.prototype.slice = function (start, end) { @@ -37,7 +49,6 @@ CustomBuffer.prototype.slice = function (start, end) { } tape.test("configure a custom encoder/decoder for bytes", function(test) { - var oldBuffer = protobuf.util.Buffer; protobuf.util.Buffer = CustomBuffer; @@ -63,7 +74,7 @@ tape.test("configure a custom encoder/decoder for bytes", function(test) { var Test = root.lookup("test.Test"); var buffer = Test.encode({ - data: Buffer.from('some-data') + data: CustomBuffer.from('some-data') }).finish(); test.ok(CustomBuffer.isCustom(buffer), "should encode the message with a custom buffer");