Skip to content

Commit

Permalink
Fixed issues with Root.fromJSON/#addJSON, search global for Long
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Nov 30, 2016
1 parent 90cd46b commit aa922c0
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 65 deletions.
20 changes: 17 additions & 3 deletions dist/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
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.

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

Development:
CDN usage
---------

Development:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.0.0/dist/protobuf.js"></script>
```

Production:

```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.0.0/dist/protobuf.min.js"></script>
```

**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/releases) your project depends upon.

Frontend usage
--------------

Development:
```
<script src="node_modules/protobufjs/dist/protobuf.js"></script>
```

Production:
```
<script src="node_modules/protobufjs/dist/protobuf.min.js"></script>
```
57 changes: 29 additions & 28 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.

8 changes: 4 additions & 4 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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.0.0",
"version": "6.0.1",
"description": "Protocol Buffers for JavaScript.",
"author": "Daniel Wirtz",
"license": "Apache-2.0",
Expand Down Expand Up @@ -31,7 +31,7 @@
"lint": "eslint src",
"test": "tape tests/*.js | tap-spec",
"zuul": "zuul --ui tape --no-coverage --concurrency 1 -- tests/*.js",
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 -- tests/*.js",
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"bench": "node bench",
"all": "npm run lint && npm run test && npm run build && npm run docs && npm run types && npm run bench"
},
Expand Down
84 changes: 84 additions & 0 deletions scripts/prof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
var protobuf = require(".."),
data = require("../bench/bench.json");

var root = protobuf.Root.fromJSON({
"nested": {
"Test": {
"fields": {
"string": {
"type": "string",
"id": 1
},
"uint32": {
"type": "uint32",
"id": 2
},
"inner": {
"type": "Inner",
"id": 3
}
},
"nested": {
"Inner": {
"fields": {
"int32": {
"type": "int32",
"id": 1
},
"innerInner": {
"type": "InnerInner",
"id": 2
},
"outer": {
"type": "Outer",
"id": 3
}
},
"nested": {
"InnerInner": {
"fields": {
"long": {
"type": "int64",
"id": 1
},
"enum": {
"type": "Enum",
"id": 2
},
"sint32": {
"type": "sint32",
"id": 3
}
}
}
}
},
"Enum": {
"values": {
"ONE": 0,
"TWO": 1,
"THREE": 2,
"FOUR": 3,
"FIVE": 4
}
}
}
},
"Outer": {
"fields": {
"bool": {
"rule": "repeated",
"type": "bool",
"id": 1,
"options": {
"packed": true
}
}
}
}
}
});

var Test = root.lookup("Test");
for (var i = 0; i < 100000000; ++i)
Test.encode(data);
19 changes: 9 additions & 10 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,19 @@ Namespace.arrayToJSON = arrayToJSON;

/**
* Adds nested elements to this namespace from JSON.
* @param {Object.<string,*>} json Nested JSON
* @param {Object.<string,*>} nestedJson Nested JSON
* @returns {Namespace} `this`
*/
NamespacePrototype.addJSON = function addJSON(json) {
if (json) {
var keys = Object.keys(json);
for (var i = 0; i < keys.length; ++i) {
var nested = json[keys[i]];
NamespacePrototype.addJSON = function addJSON(nestedJson) {
var ns = this;
if (nestedJson)
Object.keys(nestedJson).forEach(function(nestedName) {
var nested = nestedJson[nestedName];
for (var j = 0; j < nestedTypes.length; ++j)
if (nestedTypes[j].testJSON(nested))
return this.add(nestedTypes[j].fromJSON(keys[i], nested));
throw _TypeError("json." + keys[i], "JSON for " + nestedError);
}
}
return ns.add(nestedTypes[j].fromJSON(nestedName, nested));
throw _TypeError("nested." + nestedName, "JSON for " + nestedError);
});
return this;
};

Expand Down
6 changes: 3 additions & 3 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ function parse(source, root) {
}
if (token.charAt(0) === '-' && !acceptNegative)
throw illegal(token, "id");
if (/^\-?[1-9][0-9]*$/.test(token))
if (/^-?[1-9][0-9]*$/.test(token))
return parseInt(token, 10);
if (/^\-?0[x][0-9a-f]+$/.test(tokenLower))
if (/^-?0[x][0-9a-f]+$/.test(tokenLower))
return parseInt(token, 16);
if (/^\-?0[0-7]+$/.test(token))
if (/^-?0[0-7]+$/.test(token))
return parseInt(token, 8);
throw illegal(token, "id");
}
Expand Down
4 changes: 1 addition & 3 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function indexOutOfRange(reader, writeLength) {
*/
function Reader(buffer) {
if (!(this instanceof Reader))
return util.Buffer && (!buffer || util.Buffer.isBuffer(buffer))
? new BufferReader(buffer)
: new Reader(buffer);
return util.Buffer && (!buffer || util.Buffer.isBuffer(buffer)) && new BufferReader(buffer) || new Reader(buffer);

/**
* Read buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Root(options) {
Root.fromJSON = function fromJSON(json, root) {
if (!root)
root = new Root();
return root.addJSON(json);
return root.setOptions(json.options).addJSON(json.nested);
};

/**
Expand Down
7 changes: 6 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ Service.testJSON = function testJSON(json) {
* @throws {TypeError} If arguments are invalid
*/
Service.fromJSON = function fromJSON(name, json) {
return new Service(name, json.options);
var service = new Service(name, json.options);
if (json.methods)
Object.keys(json.methods).forEach(function(methodName) {
service.add(Method.fromJSON(methodName, json.methods[methodName]));
});
return service;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Type.fromJSON = function fromJSON(name, json) {
for (var i = 0; i < nestedTypes.length; ++i) {
if (nestedTypes[i].testJSON(nested)) {
type.add(nestedTypes[i].fromJSON(nestedName, nested));
break;
return;
}
}
throw Error("invalid nested object in " + type + ": " + nestedName);
Expand Down
5 changes: 3 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ if (isNode)
* If you assign any compatible long implementation to this property, the library will use it.
* @type {?Function}
*/
util.Long = global.Long || null;
util.Long = global.dcodeIO && global.dcodeIO.Long || null;

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

/**
* Tests if the specified value is a string.
Expand Down
Loading

0 comments on commit aa922c0

Please sign in to comment.