Skip to content

Commit

Permalink
Fixed tests for node 4.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Jun 24, 2019
1 parent 3dd409e commit 7d8b2aa
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions tests/comp_bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,23 +24,31 @@ 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) {
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;
Expand All @@ -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");

Expand Down

0 comments on commit 7d8b2aa

Please sign in to comment.