Skip to content

Commit 7fac9d6

Browse files
committed
Properly check Buffer.prototype.set with node v4
1 parent 22a64c6 commit 7fac9d6

16 files changed

+307
-24
lines changed

dist/protobuf.js

+6-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

12 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

+6-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

15 Bytes
Binary file not shown.

dist/runtime/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/gentests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var fs = require("fs"),
55
[
66
"tests/data/package.proto",
77
"tests/data/rpc.proto",
8-
"tests/data/mapbox/vector_tile.proto"
8+
"tests/data/mapbox/vector_tile.proto",
9+
"tests/data/ambiguous-names.proto"
910
]
1011
.forEach(function(file) {
1112
var out = file.replace(/\.proto$/, ".js");

src/writer_buffer.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ function BufferWriter() {
2727
* @returns {Uint8Array} Buffer
2828
*/
2929
BufferWriter.alloc = function alloc_buffer(size) {
30-
BufferWriter.alloc = Buffer.allocUnsafe
30+
return (BufferWriter.alloc = Buffer.allocUnsafe
3131
? Buffer.allocUnsafe
32-
: function allocUnsafe_new(size) { return new Buffer(size); };
33-
return BufferWriter.alloc(size); // overridden
32+
: function allocUnsafe_new(size) {
33+
return new Buffer(size);
34+
})(size);
3435
};
3536

36-
var writeBytesBuffer = Buffer && Buffer.prototype.subarray
37+
var writeBytesBuffer = Buffer && Buffer.from && Buffer.prototype.set.name !== "deprecated"
3738
? function writeBytesBuffer_set(val, buf, pos) {
3839
buf.set(val, pos); // faster than copy (requires node > 0.12)
3940
}

tests/data/ambiguous-names.js

+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
"use strict"; // eslint-disable-line strict
2+
3+
var $protobuf = require("../../runtime");
4+
5+
// Lazily resolved type references
6+
var $lazyTypes = [];
7+
8+
// Exported root namespace
9+
var $root = {};
10+
11+
/** @alias A */
12+
$root.A = (function() {
13+
14+
/**
15+
* Constructs a new A.
16+
* @exports A
17+
* @constructor
18+
* @param {Object} [properties] Properties to set
19+
*/
20+
function A(properties) {
21+
if (properties) {
22+
var keys = Object.keys(properties);
23+
for (var i = 0; i < keys.length; ++i)
24+
this[keys[i]] = properties[keys[i]];
25+
}
26+
}
27+
28+
/** @alias A.prototype */
29+
var $prototype = A.prototype;
30+
31+
/**
32+
* A whatever.
33+
* @name A#whatever
34+
* @type {string}
35+
*/
36+
$prototype["whatever"] = "";
37+
38+
/**
39+
* Encodes the specified A.
40+
* @function
41+
* @param {A|Object} message A or plain object to encode
42+
* @param {Writer} [writer] Writer to encode to
43+
* @returns {Writer} Writer
44+
*/
45+
A.encode = (function() {
46+
/* eslint-disable */
47+
var Writer = $protobuf.Writer;
48+
var util = $protobuf.util;
49+
var types; $lazyTypes.push(types = [null]);
50+
return function encode(m, w) {
51+
w||(w=Writer.create())
52+
if(m["whatever"]!==undefined&&m["whatever"]!=="")
53+
w.uint32(10).string(m["whatever"])
54+
return w
55+
}
56+
/* eslint-enable */
57+
})();
58+
59+
/**
60+
* Encodes the specified A, length delimited.
61+
* @param {A|Object} message A or plain object to encode
62+
* @param {Writer} [writer] Writer to encode to
63+
* @returns {Writer} Writer
64+
*/
65+
A.encodeDelimited = function encodeDelimited(message, writer) {
66+
return this.encode(message, writer).ldelim();
67+
};
68+
69+
/**
70+
* Decodes a A from the specified reader or buffer.
71+
* @function
72+
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
73+
* @param {number} [length] Message length if known beforehand
74+
* @returns {A} A
75+
*/
76+
A.decode = (function() {
77+
/* eslint-disable */
78+
var Reader = $protobuf.Reader;
79+
var util = $protobuf.util;
80+
var types; $lazyTypes.push(types = [null]);
81+
return function decode(r, l) {
82+
r instanceof Reader||(r=Reader.create(r))
83+
var c=l===undefined?r.len:r.pos+l,m=new $root.A
84+
while(r.pos<c){
85+
var t=r.int32()
86+
switch(t>>>3){
87+
case 1:
88+
m["whatever"]=r.string()
89+
break
90+
default:
91+
r.skipType(t&7)
92+
break
93+
}
94+
}
95+
return m
96+
}
97+
/* eslint-enable */
98+
})();
99+
100+
/**
101+
* Decodes a A from the specified reader or buffer, length delimited.
102+
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
103+
* @returns {A} A
104+
*/
105+
A.decodeDelimited = function decodeDelimited(readerOrBuffer) {
106+
readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);
107+
return this.decode(readerOrBuffer, readerOrBuffer.uint32());
108+
};
109+
110+
/**
111+
* Verifies a A.
112+
* @function
113+
* @param {A|Object} message A or plain object to verify
114+
* @returns {?string} `null` if valid, otherwise the reason why it is not
115+
*/
116+
A.verify = (function() {
117+
/* eslint-disable */
118+
var util = $protobuf.util;
119+
var types; $lazyTypes.push(types = [null]);
120+
return function verify(m) {
121+
if(m["whatever"]!==undefined){
122+
if(!util.isString(m["whatever"]))
123+
return"invalid value for field .A.whatever (string expected)"
124+
}
125+
return null
126+
}
127+
/* eslint-enable */
128+
})();
129+
130+
return A;
131+
})();
132+
133+
/** @alias B */
134+
$root.B = (function() {
135+
136+
/**
137+
* Constructs a new B.
138+
* @exports B
139+
* @constructor
140+
* @param {Object} [properties] Properties to set
141+
*/
142+
function B(properties) {
143+
if (properties) {
144+
var keys = Object.keys(properties);
145+
for (var i = 0; i < keys.length; ++i)
146+
this[keys[i]] = properties[keys[i]];
147+
}
148+
}
149+
150+
/** @alias B.prototype */
151+
var $prototype = B.prototype;
152+
153+
/**
154+
* B A.
155+
* @name B#A
156+
* @type {A}
157+
*/
158+
$prototype["A"] = null;
159+
160+
/**
161+
* Encodes the specified B.
162+
* @function
163+
* @param {B|Object} message B or plain object to encode
164+
* @param {Writer} [writer] Writer to encode to
165+
* @returns {Writer} Writer
166+
*/
167+
B.encode = (function() {
168+
/* eslint-disable */
169+
var Writer = $protobuf.Writer;
170+
var util = $protobuf.util;
171+
var types; $lazyTypes.push(types = ["A"]);
172+
return function encode(m, w) {
173+
w||(w=Writer.create())
174+
if(m["A"]!==undefined&&m["A"]!==null)
175+
types[0].encode(m["A"],w.fork()).len&&w.ldelim(1)||w.reset()
176+
return w
177+
}
178+
/* eslint-enable */
179+
})();
180+
181+
/**
182+
* Encodes the specified B, length delimited.
183+
* @param {B|Object} message B or plain object to encode
184+
* @param {Writer} [writer] Writer to encode to
185+
* @returns {Writer} Writer
186+
*/
187+
B.encodeDelimited = function encodeDelimited(message, writer) {
188+
return this.encode(message, writer).ldelim();
189+
};
190+
191+
/**
192+
* Decodes a B from the specified reader or buffer.
193+
* @function
194+
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
195+
* @param {number} [length] Message length if known beforehand
196+
* @returns {B} B
197+
*/
198+
B.decode = (function() {
199+
/* eslint-disable */
200+
var Reader = $protobuf.Reader;
201+
var util = $protobuf.util;
202+
var types; $lazyTypes.push(types = ["A"]);
203+
return function decode(r, l) {
204+
r instanceof Reader||(r=Reader.create(r))
205+
var c=l===undefined?r.len:r.pos+l,m=new $root.B
206+
while(r.pos<c){
207+
var t=r.int32()
208+
switch(t>>>3){
209+
case 1:
210+
m["A"]=types[0].decode(r,r.uint32())
211+
break
212+
default:
213+
r.skipType(t&7)
214+
break
215+
}
216+
}
217+
return m
218+
}
219+
/* eslint-enable */
220+
})();
221+
222+
/**
223+
* Decodes a B from the specified reader or buffer, length delimited.
224+
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
225+
* @returns {B} B
226+
*/
227+
B.decodeDelimited = function decodeDelimited(readerOrBuffer) {
228+
readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);
229+
return this.decode(readerOrBuffer, readerOrBuffer.uint32());
230+
};
231+
232+
/**
233+
* Verifies a B.
234+
* @function
235+
* @param {B|Object} message B or plain object to verify
236+
* @returns {?string} `null` if valid, otherwise the reason why it is not
237+
*/
238+
B.verify = (function() {
239+
/* eslint-disable */
240+
var util = $protobuf.util;
241+
var types; $lazyTypes.push(types = ["A"]);
242+
return function verify(m) {
243+
if(m["A"]!==undefined&&m["A"]!==null){
244+
var r;
245+
if(r=types[0].verify(m["A"]))
246+
return r
247+
}
248+
return null
249+
}
250+
/* eslint-enable */
251+
})();
252+
253+
return B;
254+
})();
255+
256+
// Resolve lazy types
257+
$lazyTypes.forEach(function(types) {
258+
types.forEach(function(path, i) {
259+
if (!path)
260+
return;
261+
path = path.split('.');
262+
var ptr = $root;
263+
while (path.length)
264+
ptr = ptr[path.shift()];
265+
types[i] = ptr;
266+
});
267+
});
268+
269+
$protobuf.roots["test_ambiguous-names"] = $root;
270+
271+
module.exports = $root;

tests/data/mapbox/vector_tile.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict"; // eslint-disable-line strict
22

3-
var $protobuf = require("../../runtime");
3+
var $protobuf = require("../../../runtime");
44

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

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

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

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

743746
/**
744747
* Verifies a Layer.
748+
* @function
745749
* @param {vector_tile.Tile.Layer|Object} message Layer or plain object to verify
746750
* @returns {?string} `null` if valid, otherwise the reason why it is not
747751
*/

0 commit comments

Comments
 (0)