forked from protobufjs/protobuf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for buffer configuration. (protobufjs#1257)
* Added support for buffer configuration. * Fixed tests for node 4. * Fixed deprecated Buffer methods.
- Loading branch information
Showing
6 changed files
with
159 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var tape = require("tape"); | ||
|
||
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); | ||
} | ||
|
||
CustomBuffer.isBuffer = Buffer.isBuffer.bind(Buffer); | ||
|
||
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; | ||
protobuf.configure(); | ||
|
||
var root = protobuf.Root.fromJSON({ | ||
nested: { | ||
test: { | ||
nested: { | ||
Test: { | ||
fields: { | ||
data: { | ||
type: "bytes", | ||
id: 1 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
var Test = root.lookup("test.Test"); | ||
|
||
var buffer = Test.encode({ | ||
data: CustomBuffer.from('some-data') | ||
}).finish(); | ||
test.ok(CustomBuffer.isCustom(buffer), "should encode the message with a custom buffer"); | ||
|
||
var decoded = Test.decode(buffer); | ||
test.ok(CustomBuffer.isCustom(decoded.data), "should decode `data` into a custom buffer"); | ||
|
||
protobuf.util.Buffer = oldBuffer; | ||
protobuf.configure(); | ||
|
||
test.end(); | ||
|
||
}); |