Skip to content

Commit aa922c0

Browse files
committed
Fixed issues with Root.fromJSON/#addJSON, search global for Long
1 parent 90cd46b commit aa922c0

16 files changed

+165
-65
lines changed

dist/README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
This folder contains prebuilt browser versions of [protobuf.js](https://github.com/dcodeIO/protobuf.js). When sending pull requests, it is not required to update these.
22

3-
Prebuilt files are in source control to enable pain-free CDN usage through [RawGit](http://rawgit.com/):
3+
Prebuilt files are in source control to enable pain-free frontend respectively CDN usage:
44

5-
Development:
5+
CDN usage
6+
---------
67

8+
Development:
79
```
810
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.0.0/dist/protobuf.js"></script>
911
```
1012

1113
Production:
12-
1314
```
1415
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.0.0/dist/protobuf.min.js"></script>
1516
```
1617

1718
**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/releases) your project depends upon.
19+
20+
Frontend usage
21+
--------------
22+
23+
Development:
24+
```
25+
<script src="node_modules/protobufjs/dist/protobuf.js"></script>
26+
```
27+
28+
Production:
29+
```
30+
<script src="node_modules/protobufjs/dist/protobuf.min.js"></script>
31+
```

dist/protobuf.js

+29-28
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

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

dist/protobuf.min.js.gz

31 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.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protobufjs",
3-
"version": "6.0.0",
3+
"version": "6.0.1",
44
"description": "Protocol Buffers for JavaScript.",
55
"author": "Daniel Wirtz",
66
"license": "Apache-2.0",
@@ -31,7 +31,7 @@
3131
"lint": "eslint src",
3232
"test": "tape tests/*.js | tap-spec",
3333
"zuul": "zuul --ui tape --no-coverage --concurrency 1 -- tests/*.js",
34-
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 -- tests/*.js",
34+
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
3535
"bench": "node bench",
3636
"all": "npm run lint && npm run test && npm run build && npm run docs && npm run types && npm run bench"
3737
},

scripts/prof.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var protobuf = require(".."),
2+
data = require("../bench/bench.json");
3+
4+
var root = protobuf.Root.fromJSON({
5+
"nested": {
6+
"Test": {
7+
"fields": {
8+
"string": {
9+
"type": "string",
10+
"id": 1
11+
},
12+
"uint32": {
13+
"type": "uint32",
14+
"id": 2
15+
},
16+
"inner": {
17+
"type": "Inner",
18+
"id": 3
19+
}
20+
},
21+
"nested": {
22+
"Inner": {
23+
"fields": {
24+
"int32": {
25+
"type": "int32",
26+
"id": 1
27+
},
28+
"innerInner": {
29+
"type": "InnerInner",
30+
"id": 2
31+
},
32+
"outer": {
33+
"type": "Outer",
34+
"id": 3
35+
}
36+
},
37+
"nested": {
38+
"InnerInner": {
39+
"fields": {
40+
"long": {
41+
"type": "int64",
42+
"id": 1
43+
},
44+
"enum": {
45+
"type": "Enum",
46+
"id": 2
47+
},
48+
"sint32": {
49+
"type": "sint32",
50+
"id": 3
51+
}
52+
}
53+
}
54+
}
55+
},
56+
"Enum": {
57+
"values": {
58+
"ONE": 0,
59+
"TWO": 1,
60+
"THREE": 2,
61+
"FOUR": 3,
62+
"FIVE": 4
63+
}
64+
}
65+
}
66+
},
67+
"Outer": {
68+
"fields": {
69+
"bool": {
70+
"rule": "repeated",
71+
"type": "bool",
72+
"id": 1,
73+
"options": {
74+
"packed": true
75+
}
76+
}
77+
}
78+
}
79+
}
80+
});
81+
82+
var Test = root.lookup("Test");
83+
for (var i = 0; i < 100000000; ++i)
84+
Test.encode(data);

src/namespace.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,19 @@ Namespace.arrayToJSON = arrayToJSON;
118118

119119
/**
120120
* Adds nested elements to this namespace from JSON.
121-
* @param {Object.<string,*>} json Nested JSON
121+
* @param {Object.<string,*>} nestedJson Nested JSON
122122
* @returns {Namespace} `this`
123123
*/
124-
NamespacePrototype.addJSON = function addJSON(json) {
125-
if (json) {
126-
var keys = Object.keys(json);
127-
for (var i = 0; i < keys.length; ++i) {
128-
var nested = json[keys[i]];
124+
NamespacePrototype.addJSON = function addJSON(nestedJson) {
125+
var ns = this;
126+
if (nestedJson)
127+
Object.keys(nestedJson).forEach(function(nestedName) {
128+
var nested = nestedJson[nestedName];
129129
for (var j = 0; j < nestedTypes.length; ++j)
130130
if (nestedTypes[j].testJSON(nested))
131-
return this.add(nestedTypes[j].fromJSON(keys[i], nested));
132-
throw _TypeError("json." + keys[i], "JSON for " + nestedError);
133-
}
134-
}
131+
return ns.add(nestedTypes[j].fromJSON(nestedName, nested));
132+
throw _TypeError("nested." + nestedName, "JSON for " + nestedError);
133+
});
135134
return this;
136135
};
137136

src/parse.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ function parse(source, root) {
159159
}
160160
if (token.charAt(0) === '-' && !acceptNegative)
161161
throw illegal(token, "id");
162-
if (/^\-?[1-9][0-9]*$/.test(token))
162+
if (/^-?[1-9][0-9]*$/.test(token))
163163
return parseInt(token, 10);
164-
if (/^\-?0[x][0-9a-f]+$/.test(tokenLower))
164+
if (/^-?0[x][0-9a-f]+$/.test(tokenLower))
165165
return parseInt(token, 16);
166-
if (/^\-?0[0-7]+$/.test(token))
166+
if (/^-?0[0-7]+$/.test(token))
167167
return parseInt(token, 8);
168168
throw illegal(token, "id");
169169
}

src/reader.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ function indexOutOfRange(reader, writeLength) {
2121
*/
2222
function Reader(buffer) {
2323
if (!(this instanceof Reader))
24-
return util.Buffer && (!buffer || util.Buffer.isBuffer(buffer))
25-
? new BufferReader(buffer)
26-
: new Reader(buffer);
24+
return util.Buffer && (!buffer || util.Buffer.isBuffer(buffer)) && new BufferReader(buffer) || new Reader(buffer);
2725

2826
/**
2927
* Read buffer.

src/root.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Root(options) {
4141
Root.fromJSON = function fromJSON(json, root) {
4242
if (!root)
4343
root = new Root();
44-
return root.addJSON(json);
44+
return root.setOptions(json.options).addJSON(json.nested);
4545
};
4646

4747
/**

src/service.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ Service.testJSON = function testJSON(json) {
7474
* @throws {TypeError} If arguments are invalid
7575
*/
7676
Service.fromJSON = function fromJSON(name, json) {
77-
return new Service(name, json.options);
77+
var service = new Service(name, json.options);
78+
if (json.methods)
79+
Object.keys(json.methods).forEach(function(methodName) {
80+
service.add(Method.fromJSON(methodName, json.methods[methodName]));
81+
});
82+
return service;
7883
};
7984

8085
/**

src/type.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Type.fromJSON = function fromJSON(name, json) {
205205
for (var i = 0; i < nestedTypes.length; ++i) {
206206
if (nestedTypes[i].testJSON(nested)) {
207207
type.add(nestedTypes[i].fromJSON(nestedName, nested));
208-
break;
208+
return;
209209
}
210210
}
211211
throw Error("invalid nested object in " + type + ": " + nestedName);

src/util.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ if (isNode)
3232
* If you assign any compatible long implementation to this property, the library will use it.
3333
* @type {?Function}
3434
*/
35-
util.Long = global.Long || null;
35+
util.Long = global.dcodeIO && global.dcodeIO.Long || null;
3636

37-
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty
37+
if (!util.Long)
38+
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty
3839

3940
/**
4041
* Tests if the specified value is a string.

0 commit comments

Comments
 (0)