Permalink
Browse files

Fixed issues with Root.fromJSON/#addJSON, search global for Long

  • Loading branch information...
dcodeIO committed Nov 30, 2016
1 parent 90cd46b commit aa922c07490f185c5f97cf28ebbd65200fc5e377
@@ -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>
```

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +31 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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",
@@ -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"
},
@@ -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);
@@ -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;
};
@@ -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");
}
@@ -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.
@@ -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);
};
/**
@@ -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;
};
/**
@@ -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);
@@ -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.
Oops, something went wrong.

0 comments on commit aa922c0

Please sign in to comment.