Skip to content

Commit 735da43

Browse files
committed
CLI: Also reuse specified root in pbjs for JSON modules, see #653
1 parent 3a05624 commit 735da43

File tree

7 files changed

+92
-21
lines changed

7 files changed

+92
-21
lines changed

cli/targets/json-module.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ module.exports = json_module;
33

44
var util = require("../util");
55

6-
json_module.description = "JSON representation as a module"
6+
json_module.description = "JSON representation as a module";
77

88
function json_module(root, options, callback) {
9-
try {
10-
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
11-
output = util.wrap(output, options);
9+
try {
10+
var rootProp = util.safeProp(options.root || "default");
11+
var output = [
12+
"var $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
13+
];
14+
if (root.options) {
15+
var optionsJson = util.jsonSafeProp(JSON.stringify(root.options, null, 2));
16+
output.push(".setOptions(" + optionsJson + ")\n");
17+
}
18+
var json = util.jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
19+
output.push(".addJSON(" + json + ");");
20+
output = util.wrap(output.join(""), options);
1221
process.nextTick(function() {
1322
callback(null, output);
1423
});

cli/targets/json.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"use strict";
22
module.exports = json_target;
33

4-
var protobuf = require("../..");
5-
64
json_target.description = "JSON representation"
75

86
function json_target(root, options, callback) {

cli/targets/static-module.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module.exports = static_module_target;
66
// - CommonJS modules depend on the minimal build for reduced package size with browserify.
77
// - AMD and global scope depend on the full library for now.
88

9-
var path = require("path"),
10-
fs = require("fs"),
11-
util = require("../util");
9+
var util = require("../util");
1210

1311
var protobuf = require("../..");
1412

cli/targets/static.js

-11
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ function aOrAn(name) {
9393
: "a ") + name;
9494
}
9595

96-
// generate dot-notation property accessors where possible. this saves a few chars (i.e. m.hello
97-
// instead of m["hello"]) but has no measurable performance impact (on V8). not present within the
98-
// library itself because the reserved words check requires a rather longish regex.
99-
util.safeProp = (function(safeProp) {
100-
return function safeProp_dn(name) {
101-
return !/^[$\w]+$/.test(name) || cliUtil.reserved(name)
102-
? safeProp(name)
103-
: "." + name;
104-
}
105-
})(util.safeProp);
106-
10796
function buildNamespace(ref, ns) {
10897
if (!ns)
10998
return;

cli/util.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ exports.pad = function(str, len, l) {
139139
return str;
140140
};
141141

142-
exports.reserved = function(name) {
142+
exports.reserved = function reserved(name) {
143143
return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
144144
};
145+
146+
// generate dot-notation property accessors where possible. this saves a few chars (i.e. m.hello
147+
// instead of m["hello"]) but has no measurable performance impact (on V8). not present within the
148+
// library itself because the reserved words check requires a rather longish regex.
149+
exports.safeProp = protobuf.util.safeProp = (function(safeProp) {
150+
return function safeProp_dn(name) {
151+
return !/^[$\w]+$/.test(name) || exports.reserved(name)
152+
? safeProp(name)
153+
: "." + name;
154+
}
155+
})(protobuf.util.safeProp);
156+
157+
exports.jsonSafeProp = function(json) {
158+
return json.replace(/^( +)"(\w+)":/mg, function($0, $1, $2) {
159+
return exports.safeProp($2).charAt(0) === "."
160+
? $1 + $2 + ":"
161+
: $0;
162+
});
163+
};

tests/split/root.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
2+
"use strict";
3+
4+
var $protobuf = require("protobufjs");
5+
6+
var $root = ($protobuf.roots.split || ($protobuf.roots.split = new $protobuf.Root()))
7+
.setOptions({
8+
"(foo)": "bar"
9+
})
10+
.addJSON({
11+
com: {
12+
nested: {
13+
Outer: {
14+
fields: {
15+
inner: {
16+
type: "Inner",
17+
id: 1
18+
},
19+
other: {
20+
type: "Other",
21+
id: 2
22+
}
23+
},
24+
nested: {
25+
Inner: {
26+
fields: {}
27+
}
28+
}
29+
},
30+
Other: {
31+
fields: {
32+
"var": {
33+
rule: "required",
34+
type: "uint32",
35+
id: 1
36+
}
37+
}
38+
}
39+
}
40+
}
41+
});
42+
43+
module.exports = $root;

tests/split/test.proto

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
option (foo) = "bar";
2+
3+
package com;
4+
5+
message Outer {
6+
optional Inner inner = 1;
7+
optional Other other = 2;
8+
9+
message Inner {
10+
}
11+
}
12+
13+
message Other {
14+
required uint32 var = 1;
15+
}

0 commit comments

Comments
 (0)