Skip to content

Commit

Permalink
Properly check Buffer.prototype.set with node v4
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 18, 2016
1 parent 22a64c6 commit 7fac9d6
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 24 deletions.
11 changes: 6 additions & 5 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion scripts/gentests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var fs = require("fs"),
[
"tests/data/package.proto",
"tests/data/rpc.proto",
"tests/data/mapbox/vector_tile.proto"
"tests/data/mapbox/vector_tile.proto",
"tests/data/ambiguous-names.proto"
]
.forEach(function(file) {
var out = file.replace(/\.proto$/, ".js");
Expand Down
9 changes: 5 additions & 4 deletions src/writer_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ function BufferWriter() {
* @returns {Uint8Array} Buffer
*/
BufferWriter.alloc = function alloc_buffer(size) {
BufferWriter.alloc = Buffer.allocUnsafe
return (BufferWriter.alloc = Buffer.allocUnsafe
? Buffer.allocUnsafe
: function allocUnsafe_new(size) { return new Buffer(size); };
return BufferWriter.alloc(size); // overridden
: function allocUnsafe_new(size) {
return new Buffer(size);
})(size);
};

var writeBytesBuffer = Buffer && Buffer.prototype.subarray
var writeBytesBuffer = Buffer && Buffer.from && Buffer.prototype.set.name !== "deprecated"
? function writeBytesBuffer_set(val, buf, pos) {
buf.set(val, pos); // faster than copy (requires node > 0.12)
}
Expand Down
271 changes: 271 additions & 0 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
"use strict"; // eslint-disable-line strict

var $protobuf = require("../../runtime");

// Lazily resolved type references
var $lazyTypes = [];

// Exported root namespace
var $root = {};

/** @alias A */
$root.A = (function() {

/**
* Constructs a new A.
* @exports A
* @constructor
* @param {Object} [properties] Properties to set
*/
function A(properties) {
if (properties) {
var keys = Object.keys(properties);
for (var i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
}
}

/** @alias A.prototype */
var $prototype = A.prototype;

/**
* A whatever.
* @name A#whatever
* @type {string}
*/
$prototype["whatever"] = "";

/**
* Encodes the specified A.
* @function
* @param {A|Object} message A or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} Writer
*/
A.encode = (function() {
/* eslint-disable */
var Writer = $protobuf.Writer;
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function encode(m, w) {
w||(w=Writer.create())
if(m["whatever"]!==undefined&&m["whatever"]!=="")
w.uint32(10).string(m["whatever"])
return w
}
/* eslint-enable */
})();

/**
* Encodes the specified A, length delimited.
* @param {A|Object} message A or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} Writer
*/
A.encodeDelimited = function encodeDelimited(message, writer) {
return this.encode(message, writer).ldelim();
};

/**
* Decodes a A from the specified reader or buffer.
* @function
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
* @param {number} [length] Message length if known beforehand
* @returns {A} A
*/
A.decode = (function() {
/* eslint-disable */
var Reader = $protobuf.Reader;
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function decode(r, l) {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.A
while(r.pos<c){
var t=r.int32()
switch(t>>>3){
case 1:
m["whatever"]=r.string()
break
default:
r.skipType(t&7)
break
}
}
return m
}
/* eslint-enable */
})();

/**
* Decodes a A from the specified reader or buffer, length delimited.
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
* @returns {A} A
*/
A.decodeDelimited = function decodeDelimited(readerOrBuffer) {
readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);
return this.decode(readerOrBuffer, readerOrBuffer.uint32());
};

/**
* Verifies a A.
* @function
* @param {A|Object} message A or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
A.verify = (function() {
/* eslint-disable */
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function verify(m) {
if(m["whatever"]!==undefined){
if(!util.isString(m["whatever"]))
return"invalid value for field .A.whatever (string expected)"
}
return null
}
/* eslint-enable */
})();

return A;
})();

/** @alias B */
$root.B = (function() {

/**
* Constructs a new B.
* @exports B
* @constructor
* @param {Object} [properties] Properties to set
*/
function B(properties) {
if (properties) {
var keys = Object.keys(properties);
for (var i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
}
}

/** @alias B.prototype */
var $prototype = B.prototype;

/**
* B A.
* @name B#A
* @type {A}
*/
$prototype["A"] = null;

/**
* Encodes the specified B.
* @function
* @param {B|Object} message B or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} Writer
*/
B.encode = (function() {
/* eslint-disable */
var Writer = $protobuf.Writer;
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function encode(m, w) {
w||(w=Writer.create())
if(m["A"]!==undefined&&m["A"]!==null)
types[0].encode(m["A"],w.fork()).len&&w.ldelim(1)||w.reset()
return w
}
/* eslint-enable */
})();

/**
* Encodes the specified B, length delimited.
* @param {B|Object} message B or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} Writer
*/
B.encodeDelimited = function encodeDelimited(message, writer) {
return this.encode(message, writer).ldelim();
};

/**
* Decodes a B from the specified reader or buffer.
* @function
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
* @param {number} [length] Message length if known beforehand
* @returns {B} B
*/
B.decode = (function() {
/* eslint-disable */
var Reader = $protobuf.Reader;
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function decode(r, l) {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.B
while(r.pos<c){
var t=r.int32()
switch(t>>>3){
case 1:
m["A"]=types[0].decode(r,r.uint32())
break
default:
r.skipType(t&7)
break
}
}
return m
}
/* eslint-enable */
})();

/**
* Decodes a B from the specified reader or buffer, length delimited.
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
* @returns {B} B
*/
B.decodeDelimited = function decodeDelimited(readerOrBuffer) {
readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);
return this.decode(readerOrBuffer, readerOrBuffer.uint32());
};

/**
* Verifies a B.
* @function
* @param {B|Object} message B or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
B.verify = (function() {
/* eslint-disable */
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function verify(m) {
if(m["A"]!==undefined&&m["A"]!==null){
var r;
if(r=types[0].verify(m["A"]))
return r
}
return null
}
/* eslint-enable */
})();

return B;
})();

// Resolve lazy types
$lazyTypes.forEach(function(types) {
types.forEach(function(path, i) {
if (!path)
return;
path = path.split('.');
var ptr = $root;
while (path.length)
ptr = ptr[path.shift()];
types[i] = ptr;
});
});

$protobuf.roots["test_ambiguous-names"] = $root;

module.exports = $root;
6 changes: 5 additions & 1 deletion tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict"; // eslint-disable-line strict

var $protobuf = require("../../runtime");
var $protobuf = require("../../../runtime");

// Lazily resolved type references
var $lazyTypes = [];
Expand Down Expand Up @@ -121,6 +121,7 @@ $root.vector_tile = (function() {

/**
* Verifies a Tile.
* @function
* @param {vector_tile.Tile|Object} message Tile or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Expand Down Expand Up @@ -329,6 +330,7 @@ $root.vector_tile = (function() {

/**
* Verifies a Value.
* @function
* @param {vector_tile.Tile.Value|Object} message Value or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Expand Down Expand Up @@ -530,6 +532,7 @@ $root.vector_tile = (function() {

/**
* Verifies a Feature.
* @function
* @param {vector_tile.Tile.Feature|Object} message Feature or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Expand Down Expand Up @@ -742,6 +745,7 @@ $root.vector_tile = (function() {

/**
* Verifies a Layer.
* @function
* @param {vector_tile.Tile.Layer|Object} message Layer or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Expand Down
Loading

0 comments on commit 7fac9d6

Please sign in to comment.