diff --git a/tests/comp_bytes.js b/tests/comp_bytes.js index ab088ecf3..39c58fd15 100644 --- a/tests/comp_bytes.js +++ b/tests/comp_bytes.js @@ -1,39 +1,54 @@ var tape = require("tape"); -class CustomBuffer extends Buffer { - static toCustom(b) { - b._isCustom = true; - return b; - } - - static isCustom(b) { - return !!b._isCustom; - } - - static from(...args) { - return CustomBuffer.toCustom(Buffer.from(...args)); - } - - static alloc(...args) { - return CustomBuffer.toCustom(Buffer.alloc(...args)); - } - - static allocUnsafe(...args) { - return CustomBuffer.toCustom(Buffer.allocUnsafe(...args)); - } - - static allocUnsafeSlow(...args) { - return CustomBuffer.toCustom(Buffer.allocUnsafeSlow(...args)); - } - - slice(...args) { - return CustomBuffer.toCustom(super.slice(...args)); - } +var protobuf = require(".."); + +var oldBufferImpl = Buffer.alloc === undefined; + +// extends Buffer +(CustomBuffer.prototype = Object.create(Buffer.prototype)).constructor = CustomBuffer; + +function CustomBuffer(arg, encodingOrOffset, length) { + Buffer.call(this, arg, encodingOrOffset, length); + CustomBuffer.toCustom(this); } -tape.test("configure a custom encoder/decoder for bytes", function(test) { +CustomBuffer.isBuffer = Buffer.isBuffer.bind(Buffer); - var protobuf = require(".."); +CustomBuffer.toCustom = function (b) { + b._isCustom = true; + return b; +} + +CustomBuffer.isCustom = function (b) { + return !!b._isCustom; +} + +CustomBuffer.from = function (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(oldBufferImpl + ? new Buffer(size, fill, encoding) + : Buffer.alloc(size, fill, encoding) + ); +} + +CustomBuffer.allocUnsafe = function (size) { + return CustomBuffer.toCustom(oldBufferImpl + ? new Buffer(size) + : Buffer.allocUnsafe(size) + ); +} + +CustomBuffer.prototype.slice = function (start, end) { + return CustomBuffer.toCustom(this.slice(start, end)); +} + +tape.test("configure a custom encoder/decoder for bytes", function(test) { var oldBuffer = protobuf.util.Buffer; protobuf.util.Buffer = CustomBuffer; @@ -59,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");