Simulator first commit

This commit is contained in:
2019-09-18 11:11:16 +03:00
commit 6e1686be67
5028 changed files with 985331 additions and 0 deletions

281
node_modules/protobufjs/cli/pbjs.js generated vendored Normal file
View File

@ -0,0 +1,281 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var ProtoBuf = require(__dirname+"/../index.js"),
fs = require("fs"),
path = require("path"),
cli = require("ascli")("pbjs"),
yargs = require("yargs"),
util = require("./pbjs/util.js"),
glob = require("glob"),
pkg = require("../package.json");
/**
* pbjs namespace.
* @exports pbjs
* @namespace
*/
var pbjs = module.exports = {};
/**
* @alias pbjs/util
*/
pbjs.util = util;
/**
* Source formats.
* @type {!Object.<string,!function(string,!Object.<string,*>)>}
*/
pbjs.sources = {};
fs.readdirSync(__dirname+"/pbjs/sources").forEach(function(source) {
if (/\.js$/.test(source)) {
var src = require(__dirname + "/pbjs/sources/" + source);
if (!src.exclude)
pbjs.sources[source.substring(0, source.lastIndexOf("."))] = src;
}
});
/**
* Target formats.
* @type {!Object.<string,!function(!ProtoBuf.Builder,!Object.<string,*>)>}
*/
pbjs.targets = {};
fs.readdirSync(__dirname+"/pbjs/targets").forEach(function(target) {
if (/\.js$/.test(target))
pbjs.targets[target.substring(0, target.lastIndexOf("."))] = require(__dirname + "/pbjs/targets/" + target);
});
/**
* Status code: Operation successful
* @type {number}
*/
pbjs.STATUS_OK = 0;
/**
* Status code: Displaying usage information
* @type {number}
*/
pbjs.STATUS_USAGE = 1;
/**
* Status code: No such include directory
* @type {number}
*/
pbjs.STATUS_ERR_INCLUDE_DIR = 2;
/**
* Status code: No such source format
* @type {number}
*/
pbjs.STATUS_ERR_SOURCE_FORMAT = 3;
/**
* Status code: No such target format
* @type {number}
*/
pbjs.STATUS_ERR_TARGET_FORMAT = 4;
/**
* Status code: No such namespace
* @type {number}
*/
pbjs.STATUS_ERR_NAMESPACE = 5;
/**
* Status code: Illegal dependency
* @type {number}
*/
pbjs.STATUS_ERR_DEPENDENCY = 6;
/**
* Status code: No matching source files
* @type {number}
*/
pbjs.STATUS_ERR_NOSOURCE = 7;
// Makes a table of available source or target formats
function mkOptions(obj) {
var str = '';
Object.keys(obj).forEach(function(key) {
str += "\n "+util.pad(key, 10)+" "+obj[key].description;
});
return str;
}
/**
* Executes the program.
* @param {!Array.<string>} argv Command line arguments
* @returns {number} Status code
*/
pbjs.main = function(argv) {
var options = yargs
.usage(cli("pb".white.bold+"js".green.bold, util.pad("ProtoBuf.js v"+pkg['version'], 31, true)+" "+pkg['homepage'].grey) + "\n" +
"CLI utility to convert between .proto and JSON syntax / to generate classes.\n\n" +
"Usage: ".white.bold+path.basename(argv[1]).green.bold+" <source files...> [options] [> outFile]")
.help("help")
.version(pkg["version"])
.wrap(null)
.options({
source: {
alias: "s",
describe: "Specifies the source format. Valid formats are:\n" + mkOptions(pbjs.sources)+"\n"
},
target: {
alias: "t",
describe: "Specifies the target format. Valid formats are:\n" + mkOptions(pbjs.targets)+"\n"
},
using: {
alias: "u",
describe: "Specifies an option to apply to the volatile builder\nloading the source, e.g. convertFieldsToCamelCase.",
type: "array"
},
min: {
alias: "m",
describe: "Minifies the output.",
default: false
},
path: {
alias: "p",
describe: "Adds a directory to the include path."
},
legacy: {
alias: "l",
describe: "Includes legacy descriptors from google/protobuf/ if\nexplicitly referenced.",
default: false
},
quiet: {
alias: "q",
describe: "Suppresses any informatory output to stderr.",
default: false
},
out: {
alias: "o",
describe: "Send output to file instead of stdout.",
},
use: {
alias: "i",
describe: "Specifies an option to apply to the emitted builder\nutilized by your program, e.g. populateAccessors.",
type: "array"
},
exports: {
alias: "e",
describe: "Specifies the namespace to export. Defaults to export\nthe root namespace."
},
dependency: {
alias: "d",
describe: "Library dependency to use when generating classes.\nDefaults to 'protobufjs' for CommonJS, 'protobuf' for\nAMD modules and 'dcodeIO.ProtoBuf' for classes."
}
})
.alias("help", "h")
.alias("version", "v")
.check(function (args) {
if (args.source && typeof pbjs.sources[args.source] !== "function") {
return "Unrecognized source format: '" + args.source + "'";
}
if (args.target && typeof pbjs.targets[args.target] !== "function") {
return "Unrecognized target format: '" + args.target + "'";
}
if (args._.length < 3) {
return "The filename to parse is required.";
}
return true;
})
.parse(argv);
var start = Date.now(),
sourceFiles = options._.slice(2);
// Expand glob expressions
var sourceFilesExpand = [];
for (var i=0; i<sourceFiles.length; ++i) {
var filename = sourceFiles[i],
files = glob.sync(filename);
if (files.length === 0) {
cli.fail("No matching source files: "+filename);
return pbjs.STATUS_ERR_NOSOURCE;
}
files.forEach(function(filename) {
if (sourceFilesExpand.indexOf(filename) === -1)
sourceFilesExpand.push(filename);
});
}
sourceFiles = sourceFilesExpand;
if (!options.target)
options.target = "json";
// Set up include paths
var includePath = Array.isArray(options['path']) ? options['path'] : (typeof options['path'] === 'string' ? [options['path']] : []);
sourceFiles.forEach(function (sourceFile) {
var dir = path.dirname(sourceFile);
if (includePath.indexOf(dir) === -1) {
includePath.push(dir);
}
});
includePath.forEach(function(path) { // Verify that include paths actually exist
if (!fs.existsSync(path)) {
if (!options.quiet)
cli.fail("No such include directory: "+path);
return pbjs.STATUS_ERR_INCLUDE_DIR;
}
});
options.path = includePath;
// Detect source format if not specified, then verify
if (typeof options.source !== 'string') {
var source = fs.readFileSync(sourceFiles[0]).toString("utf8").trim();
if (source.substring(0,1) === "{")
options.source = "json";
else
options.source = "proto";
}
// Load the source files to a common builder
var builder = pbjs.sources[options.source](sourceFiles, options);
// Validate exports and dependency if set
if (typeof options.exports !== 'undefined') {
if (!(builder.lookup(options.exports) instanceof ProtoBuf.Reflect.Namespace)) {
if (!options.quiet)
cli.fail("No such export namespace: "+options.exports);
return pbjs.STATUS_ERR_NAMESPACE;
}
if (options.exports.charAt(0) === '.')
options.exports = options.exports.substring(1);
}
if (typeof options.dependency !== 'undefined')
if (typeof options.dependency !== 'string' || !options.dependency) {
if (!options.quiet)
cli.fail("Illegal dependency: "+options.dependency);
return pbjs.STATUS_ERR_DEPENDENCY;
}
// Perform target conversion
if (!options.quiet)
cli.error("\nProcessing: "+sourceFiles.join(", ")+" ...\n");
var res = pbjs.targets[options.target](builder, options);
if (options.out){
fs.writeFileSync(options.out, res);
}else
process.stdout.write(res);
if (!options.quiet)
cli.error(""),
cli.ok("Converted "+sourceFiles.length+" source files to "+options.target+" ("+res.length+" bytes, "+(Date.now()-start)+" ms)");
return pbjs.STATUS_OK;
};

130
node_modules/protobufjs/cli/pbjs/sources/binary.js generated vendored Normal file
View File

@ -0,0 +1,130 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Binary descriptor set";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require(__dirname+"/../util.js"),
node_path = require("path"),
fs = require("fs");
/**
* pbjs source: Binary descriptor
* @exports pbjs/sources/binary
* @function
* @param {!Array.<string>} filenames Source files
* @param {!Object.<string,*>=} options Options
* @returns {!ProtoBuf.Builder}
*/
var binary = module.exports = function(filenames, options) {
options = options || [];
var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")),
loaded = [];
filenames.forEach(function(filename) {
var data = binary.load(filename, options, loaded);
builder["import"](data, filename);
});
builder.resolveAll();
return builder;
};
/**
* Module description.
* @type {string}
*/
binary.description = description;
binary.exclude = true; // Unfinished
/**
* Loads a binary descriptor.
* @param {string} filename Source file
* @param {!Object.<string,*>} options Options
* @param {!Array.<string>=} loaded An array of already loaded filenames
* @returns {*} JSON descriptor
*/
binary.load = function(filename, options, loaded) {
filename = node_path.resolve(filename);
loaded = loaded || [];
if (loaded.indexOf(filename) >= 0)
return {};
var data = fs.readFileSync(filename);
loaded.push(filename);
var builder = ProtoBuf.loadProtoFile(node_path.join("..", "..", "..", "src", "google", "protobuf", "descriptor.proto")),
FileDescriptorSet = builder.build("google.protobuf.FileDescriptorSet");
var fds = FileDescriptorSet.decode(data),
imports = [];
var json = {
"package": null,
"imports": imports
};
fds.file.forEach(function(fdp) {
imports.push(buildFileDescriptorProto(fdp));
});
return json;
};
function buildFileDescriptorProto(fdp) {
var pkg = fdp.package,
messages = [],
enums = [],
services = [],
extensions = [],
options = {},
imports = [];
fdp.message_type.forEach(function(dp) {
messages.push(buildMessageDescriptorProto(dp));
});
fdp.enum_type.forEach(function(edp) {
enums.push(buildEnumDescriptorProto(edp));
});
fdp.service.forEach(function(sdp) {
enums.push(buildServiceDescriptorProto(sdp));
});
fdp.extension.forEach(function(fdp) {
extensions.push(buildFieldDescriptorProtoAsExtension(fdp));
});
fdp.options.forEach(function(fo) {
// TODO
});
fdp.dependency.forEach(function(filename) {
// TODO
});
return {
"package": pkg,
"messages": messages,
"enums": enums,
"services": services,
"extensions": extensions,
"options": options,
"imports": imports
};
}
function buildMessageDescriptorProto(mdp) {
}
function buildEnumDescriptorProto(edp) {
}
function buildServiceDescriptorProto(sdp) {
}
function buildFieldDescriptorProtoAsExtension(fdp) {
}

84
node_modules/protobufjs/cli/pbjs/sources/json.js generated vendored Normal file
View File

@ -0,0 +1,84 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Plain JSON descriptor";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require(__dirname+"/../util.js"),
node_path = require("path"),
fs = require("fs");
/**
* pbjs source: Plain JSON descriptor
* @exports pbjs/sources/json
* @function
* @param {!Array.<string>} filenames Source files
* @param {!Object.<string,*>=} options Options
* @returns {!ProtoBuf.Builder}
*/
var json = module.exports = function(filenames, options) {
options = options || [];
var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")),
loaded = [];
filenames.forEach(function(filename) {
var data = json.load(filename, options, loaded);
builder["import"](data, filename);
});
builder.resolveAll();
return builder;
};
/**
* Module description.
* @type {string}
*/
json.description = description;
/**
* Loads a JSON descriptor including imports.
* @param {string} filename Source file
* @param {!Object.<string,*>} options Options
* @param {!Array.<string>=} loaded An array of already loaded filenames
* @returns {*} JSON descriptor
*/
json.load = function(filename, options, loaded) {
filename = node_path.resolve(filename);
loaded = loaded || [];
if (loaded.indexOf(filename) >= 0)
return {};
var data = JSON.parse(fs.readFileSync(filename).toString("utf8")),
imports = data['imports'];
loaded.push(filename);
if (Array.isArray(imports)) {
for (var i=0; i<imports.length; ++i) {
// Skip pulled imports and legacy descriptors
if (typeof imports[i] !== 'string' || (util.isDescriptor(imports[i]) && !options.legacy))
continue;
// Merge imports, try include paths
(function() {
var path = options.path || [];
for (var j=0; j<path.length; ++j) {
var import_filename = node_path.resolve(path[j] + "/", imports[i]);
if (!fs.existsSync(import_filename))
continue;
imports[i] = json.load(import_filename, options, loaded);
return;
}
throw Error("File not found: "+imports[i]);
})();
}
}
return data;
};

84
node_modules/protobufjs/cli/pbjs/sources/proto.js generated vendored Normal file
View File

@ -0,0 +1,84 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Plain .proto descriptor";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require(__dirname+"/../util.js"),
fs = require("fs"),
node_path = require("path");
/**
* pbjs source: Plain .proto descriptor
* @exports pbjs/sources/proto
* @function
* @param {!Array.<string>} filenames Source files
* @param {!Object.<string,*>=} options Options
* @returns {!ProtoBuf.Builder}
*/
var proto = module.exports = function(filenames, options) {
options = options || [];
var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")),
loaded = [];
filenames.forEach(function(filename) {
var data = proto.load(filename, options, loaded);
builder["import"](data, filename);
});
builder.resolveAll();
return builder;
};
/**
* Module description.
* @type {string}
*/
proto.description = description;
/**
* Loads a .proto descriptor including imports.
* @param {string} filename Source file
* @param {!Object.<string,*>} options Options
* @param {!Array.<string>=} loaded An array of already loaded filenames
* @returns {*} JSON descriptor
*/
proto.load = function(filename, options, loaded) {
filename = node_path.resolve(filename);
loaded = loaded || [];
if (loaded.indexOf(filename) >= 0)
return {};
var data = ProtoBuf.DotProto.Parser.parse(fs.readFileSync(filename).toString("utf8"));
loaded.push(filename);
if (Array.isArray(data['imports'])) {
var imports = data['imports'];
for (var i=0; i<imports.length; i++) {
// Skip pulled imports and legacy descriptors
if (typeof imports[i] !== 'string' || (util.isDescriptor(imports[i]) && !options.legacy))
continue;
// Merge imports, try include paths
(function() {
var path = options.path || [];
for (var j=0; j<path.length; ++j) {
var import_filename = node_path.resolve(path[j] + "/", imports[i]);
if (!fs.existsSync(import_filename))
continue;
imports[i] = proto.load(import_filename, options, loaded);
return;
}
throw Error("File not found: "+imports[i]);
})();
}
}
return data;
};

45
node_modules/protobufjs/cli/pbjs/targets/amd.js generated vendored Normal file
View File

@ -0,0 +1,45 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Runtime structures as AMD module";
var ProtoBuf = require("../../../index.js"),
util = require("../util.js"),
js = require("./js.js");
/**
* pbjs target: Runtime structures as an AMD module
* @exports pbjs/targets/amd
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var amd = module.exports = function(builder, options) {
options = options || {};
return [
"define([", JSON.stringify(options.dependency || "protobuf"), "]", options.min ? "," : ", ",
"function(ProtoBuf)", options.min ? "{" : " {\n ",
"return ProtoBuf",
util.indent(js.build(builder, options), options.min ? "" : " "), options.min ? "" : "\n",
"});"
].join('');
};
/**
* Module description.
* @type {string}
*/
amd.description = description;

42
node_modules/protobufjs/cli/pbjs/targets/commonjs.js generated vendored Normal file
View File

@ -0,0 +1,42 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Runtime structures as CommonJS module";
var util = require("../util.js"),
js = require("./js.js");
/**
* pbjs target: Runtime structures as a CommonJS module
* @exports pbjs/targets/commonjs
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var commonjs = module.exports = function(builder, options) {
options = options || {};
return [
"module.exports", options.min ? "=" : " = ",
"require(", JSON.stringify(options.dependency || "protobufjs"), ")",
js.build(builder, options)
].join('');
};
/**
* Module description.
* @type {string}
*/
commonjs.description = description;

63
node_modules/protobufjs/cli/pbjs/targets/js.js generated vendored Normal file
View File

@ -0,0 +1,63 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Runtime structures";
var util = require("../util.js"),
json = require("./json.js");
/**
* pbjs target: Runtime structures
* @exports pbjs/targets/js
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var js = module.exports = function(builder, options) {
options = options || {};
var varName = "_root";
if (options.exports)
varName = options.exports.substring(options.exports.lastIndexOf(".")+1);
return [
"var ", varName, options.min ? "=" : " = ", options.dependency || "dcodeIO.ProtoBuf",
js.build(builder, options)
].join('');
};
/**
* Builds the core js target.
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
js.build = function(builder, options) {
options = options || {};
return [
".newBuilder(",
JSON.stringify(util.getBuilderOptions(options, "use"), null, options.min ? 0 : 4),
")['import'](",
json(builder, options),
").build(",
typeof options.exports === 'string' ? JSON.stringify(options.exports.split(".")) : "",
");"
].join('');
};
/**
* Module description.
* @type {string}
*/
js.description = description;

260
node_modules/protobufjs/cli/pbjs/targets/json.js generated vendored Normal file
View File

@ -0,0 +1,260 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Plain JSON descriptor";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require("../util.js");
/**
* pbjs target: Plain JSON descriptor
* @exports pbjs/targets/json
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var json = module.exports = function(builder, options) {
options = options || {};
builder.resolveAll();
// Set the pointer to the lowest common namespace (with options)
var ptr = builder.ns;
while (ptr.children.length === 1 && Object.keys(ptr.options).length === 0 && ptr.children[0].className === "Namespace")
ptr = ptr.children[0];
// Start by building the package namespace
var pkg = ptr.fqn().substring(1),
out = {
"package": pkg !== "" ? pkg : null
};
buildNamespace(ptr, out);
return JSON.stringify(out, null, options.min ? 0 : 4);
};
/**
* Module description.
* @type {string}
*/
json.description = description;
/**
* Builds all structures in a namespace.
* @param {!ProtoBuf.Reflect.Namespace} ns Namespace to build
* @param {!Object.<string,*>} out Extended output object
*/
function buildNamespace(ns, out) {
var messages, enums, services;
util.extend(out, {
"syntax" : ns.syntax || 'proto2',
"options" : out.options || {},
"messages" : messages = [],
"enums" : enums = [],
"services" : services = []
});
if (!(ns instanceof ProtoBuf.Reflect.Message))
out['isNamespace'] = true;
util.extend(out["options"], buildOptions(ns.options));
ns.getChildren(ProtoBuf.Reflect.Enum).forEach(function(enm) {
enums.push(buildEnum(enm));
});
if (enums.length === 0)
delete out["enums"];
ns.getChildren(ProtoBuf.Reflect.Message).forEach(function(msg) {
messages.push(buildMessage(msg));
});
ns.getChildren(ProtoBuf.Reflect.Service).forEach(function(svc) {
services.push(buildService(svc));
});
if (services.length === 0)
delete out["services"];
Array.prototype.push.apply(messages, buildExtensions(ns));
ns.getChildren(ProtoBuf.Reflect.Namespace).forEach(function(innerNs) {
if (innerNs.className !== "Namespace")
return;
var emptyMessage = {
"name": innerNs.name,
"fields": []
};
buildNamespace(innerNs, emptyMessage);
messages.push(emptyMessage);
});
if (messages.length === 0)
delete out["messages"];
if (Object.keys(out["options"]).length === 0)
delete out["options"];
}
/**
* Builds extensions declared in the specified namespace.
* @param {!ProtoBuf.Reflect.Namespace} ns Namespace
* @returns {!Array.<!*>}
*/
function buildExtensions(ns) {
var exts = util.groupExtensions(ns);
if (exts === null)
return [];
var messages = [];
Object.keys(exts).forEach(function(extFqn) {
var extMsg = ns.resolve(extFqn),
extFields = exts[extFqn];
var fields, ext = {
"ref" : ns.qn(extMsg),
"fields" : fields = []
};
extFields.forEach(function(extField) {
fields.push(buildMessageField(extField));
});
messages.push(ext);
});
return messages;
}
/**
* Builds block-level options.
* @param {!Object.<string,*>} options Options
* @returns {!Object.<string,*>}
*/
function buildOptions(options) {
Object.keys(options = options || {}).forEach(function(key) {
var val = options[key];
switch (typeof val) {
case 'string':
case 'number':
case 'boolean':
case 'object':
break;
default:
throw Error("Illegal option type: "+typeof(val));
}
});
return options;
}
/**
* Builds a message.
* @param {!ProtoBuf.Reflect.Message} msg Message
* @returns {!*}
*/
function buildMessage(msg) {
var fields, oneofs;
var out = {
"name" : msg.name,
"syntax" : msg.syntax || 'proto2',
"options" : {},
"fields" : fields = [],
"oneofs" : oneofs = {}
};
msg.getChildren(ProtoBuf.Reflect.Message.Field).forEach(function(fld) {
if (fld instanceof ProtoBuf.Reflect.Message.ExtensionField)
return;
fields.push(buildMessageField(fld));
});
msg.getChildren(ProtoBuf.Reflect.Message.OneOf).forEach(function(oneof) {
oneofs[oneof.name] = buildMessageOneof(oneof);
});
if (msg.extensions)
out["extensions"] = msg.extensions;
if (Object.keys(oneofs).length === 0)
delete out["oneofs"];
buildNamespace(msg, out);
return out;
}
/**
* Builds a message field.
* @param {!ProtoBuf.Reflect.Message.Field} fld Message field
* @returns {!*}
*/
function buildMessageField(fld) {
return {
"rule" : fld.map ? "map" : (fld.repeated ? "repeated" : (fld.required ? "required" : "optional")),
"type" : fld.resolvedType ? fld.parent.qn(fld.resolvedType) : fld.type['name'],
"keytype" : (typeof(fld.keyType) === 'string') ? fld.keyType : (fld.keyType !== null ? fld.keyType.name : undefined),
"name" : fld instanceof ProtoBuf.Reflect.Message.ExtensionField ? fld.name.substring(fld.name.lastIndexOf(".")+1): fld.name,
"id" : fld.id,
"options" : Object.keys(fld.options).length > 0 ? buildOptions(fld.options) : undefined,
"oneof" : fld.oneof ? fld.oneof.name : undefined
};
}
/**
* Builds a message oneof.
* @param {!ProtoBuf.Reflect.message.OneOf} oneof Message oneof
* @returns {!Array.<!*>}
*/
function buildMessageOneof(oneof) {
var out = [];
oneof.fields.forEach(function(fld) {
out.push(fld.id);
});
return out;
}
/**
* Builds an enum.
* @param {!ProtoBuf.Reflect.Enum} enm Enum
* @returns {!*}
*/
function buildEnum(enm) {
var values;
var out = {
"name" : enm.name,
"syntax" : enm.syntax || 'proto2',
"values" : values = []
};
enm.getChildren(ProtoBuf.Reflect.Enum.Value).forEach(function(val) {
values.push(buildEnumValue(val));
});
if (Object.keys(enm.options).length > 0)
out["options"] = buildOptions(enm.options);
return out;
}
/**
* Builds an enum value.
* @param {!ProtoBuf.Reflect.Enum.Value} val Enum value
* @returns {!*}
*/
function buildEnumValue(val) {
return {
"name" : val.name,
"id" : val.id
};
}
/**
* Builds a service.
* @param {!ProtoBuf.Reflect.Service} svc Service
* @returns {!*}
*/
function buildService(svc) {
var rpc;
var out = {
"name": svc.name,
"options": buildOptions(svc.options),
"rpc": rpc = {}
};
svc.getChildren(ProtoBuf.Reflect.Service.RPCMethod).forEach(function(mtd) {
rpc[mtd.name] = {
"request": svc.qn(mtd.resolvedRequestType),
"request_stream": mtd.requestStream,
"response": svc.qn(mtd.resolvedResponseType),
"response_stream": mtd.responseStream,
"options": buildOptions(mtd.options)
};
});
return out;
}

267
node_modules/protobufjs/cli/pbjs/targets/proto.js generated vendored Normal file
View File

@ -0,0 +1,267 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Plain .proto descriptor";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require("../util.js");
/**
* pbjs target: Plain .proto descriptor
* @exports pbjs/targets/proto
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var proto = module.exports = function(builder, options) {
options = options || {};
builder.resolveAll();
// Set the pointer to the lowest common namespace (with options)
var ptr = builder.ns;
while (ptr.children.length === 1 && Object.keys(ptr.options).length === 0 && ptr.children[0].className === "Namespace")
ptr = ptr.children[0];
var out = [];
function trim() {
out[out.length-1] = out[out.length-1].replace(/\n{2,}$/, "\n");
}
// Builds a set of top level options
function buildOptions(opt, indent) {
var keys;
if ((keys = Object.keys(opt)).length === 0)
return;
keys.forEach(function(key) {
if (!options.min)
out.push(indent);
out.push("option ", key, options.min ? "=" : " = ", value(opt[key]), options.min ? ";" : ";\n");
});
if (!options.min)
out[out.length-1] += "\n";
}
// Builds everything within a namespace
function buildNamespace(ns, indent) {
ns.getChildren(ProtoBuf.Reflect.Enum).forEach(function(enm) {
buildEnum(enm, indent);
});
ns.getChildren(ProtoBuf.Reflect.Message).forEach(function(msg) {
if (!msg.isGroup) // legacy groups are build within the respective field
buildMessage(msg, indent);
});
var exts = util.groupExtensions(ns);
if (exts !== null) {
Object.keys(exts).forEach(function(extFqn) {
var extMsg = ns.resolve(extFqn),
extFields = exts[extFqn];
if (!options.min)
out.push(indent);
out.push("extend ", ns.qn(extMsg), options.min ? "{" : " {\n");
extFields.forEach(function(extField) {
buildMessageField(ns, extField, indent+" ", false);
});
if (!options.min)
out.push(indent);
out.push(options.min ? "}" : "}\n\n");
});
}
ns.getChildren(ProtoBuf.Reflect.Service).forEach(function(svc) {
buildService(svc, indent);
});
ns.getChildren(ProtoBuf.Reflect.Namespace).forEach(function(innerNs) {
if (innerNs.className !== "Namespace")
return;
if (!options.min)
out.push(indent);
out.push("message ", innerNs.name, options.min ? "{" : " {\n");
buildNamespace(innerNs, indent+" ");
if (!options.min)
out.push(indent);
out.push(options.min ? "}" : "}\n");
});
trim();
}
// Builds a message
function buildMessage(msg, indent) {
if (!msg.isGroup) {
if (!options.min)
out.push(indent);
out.push("message ", msg.name);
}
out.push(options.min ? "{" : " {\n");
buildOptions(msg.options, indent+" ");
var n = 0, oneofFields = [];
msg.getChildren(ProtoBuf.Reflect.Message.OneOf).forEach(function(oneof) {
if (!options.min)
out.push(indent, " ");
out.push("oneof ", oneof.name, options.min ? "{" : " {\n");
oneof.fields.forEach(function(fld) {
buildMessageField(msg, fld, indent+" ", true);
oneofFields.push(fld);
});
if (!options.min)
out.push(indent, " ");
out.push(options.min ? "}" : "}\n");
});
msg.getChildren(ProtoBuf.Reflect.Message.Field).forEach(function(fld) {
if (fld instanceof ProtoBuf.Reflect.Message.ExtensionField)
return;
if (oneofFields.indexOf(fld) >= 0)
return;
buildMessageField(msg, fld, indent+" ", false);
n++;
});
if (n > 0 && !options.min)
out[out.length-1] += "\n";
if (msg.extensions) { // array of ranges
if (!options.min)
out.push(indent, " ");
out.push("extensions ");
msg.extensions.forEach(function(range, index) {
if (index > 0)
out.push(options.min ? "," : ", ");
out.push(value(range[0]));
if (range[1] !== range[0])
out.push(" to ", range[1] === ProtoBuf.ID_MAX ? "max" : value(range[1]));
});
out.push(options.min ? ";" : ";\n\n");
}
buildNamespace(msg, indent+" ");
if (!options.min)
out.push(indent);
out.push(options.min ? "}" : "}\n\n");
}
// Builds a message field
function buildMessageField(msg, fld, indent, isOneOf) {
var isGroup = false;
if (!options.min)
out.push(indent);
if (!isOneOf)
out.push(fld.required ? "required " : (fld.repeated ? "repeated " : "optional "));
if (fld.resolvedType !== null) {
if (fld.resolvedType instanceof ProtoBuf.Reflect.Message && fld.resolvedType.isGroup) {
// inline legacy groups
out.push("group ");
isGroup = true;
}
out.push(msg.qn(fld.resolvedType));
} else
out.push(fld.type['name']);
if (!isGroup)
out.push(" ", fld instanceof ProtoBuf.Reflect.Message.ExtensionField ? fld.name.substring(fld.name.lastIndexOf(".")+1) : fld.name);
out.push(options.min ? "=" : " = ", fld.id);
if (isGroup) // inline
buildMessage(fld.resolvedType, indent);
else {
var keys = Object.keys(fld.options);
if (keys.length > 0) {
out.push(options.min ? "[" : " [");
var n = 0;
keys.forEach(function (key) {
if (n > 0)
out.push(options.min ? "," : ", ");
out.push(key, options.min ? "=" : " = ",
// BEWARE: Monkey patch for string enum defaults
key === "default" && fld.type === ProtoBuf.TYPES["enum"] && typeof fld.options[key] === 'string' ? fld.options[key] : value(fld.options[key])
);
n++;
});
out.push("]");
}
out.push(options.min ? ";" : ";\n");
}
}
// Builds an enum
function buildEnum(enm, indent) {
if (!options.min)
out.push(indent);
out.push("enum ", enm.name, options.min ? "{" : " {\n");
buildOptions(enm.options, indent+" ");
enm.getChildren(ProtoBuf.Reflect.Enum.Value).forEach(function(val) {
if (!options.min)
out.push(indent, " ");
out.push(val.name, options.min ? "=" : " = ", val.id, options.min? ";" : ";\n");
});
if (!options.min)
out.push(indent);
out.push(options.min ? "}" : "}\n\n");
}
// Builds a service
function buildService(svc, indent) {
if (!options.min)
out.push(indent);
out.push("service ", svc.name, options.min ? "{" : " {\n");
buildOptions(svc.options, indent+" ");
svc.getChildren(ProtoBuf.Reflect.Service.RPCMethod).forEach(function(rpc) {
if (!options.min)
out.push(indent+" ");
out.push("rpc ", rpc.name, "(", svc.qn(rpc.resolvedRequestType), ") returns(", svc.qn(rpc.resolvedResponseType), ")");
var keys = Object.keys(rpc.options);
if (keys.length === 0) {
out.push(options.min ? ";" : ";\n")
} else {
out.push(options.min ? "{" : " {\n");
buildOptions(rpc.options, indent+" ");
trim();
if (!options.min)
out.push(indent+" ");
out.push(options.min ? "}" : "}\n");
}
if (!options.min)
out[out.length-1] += "\n";
});
trim();
out.push(options.min ? "}" : "}\n");
}
// Start by building the package namespace
var pkg = ptr.fqn().substring(1);
if (pkg !== "")
out.push("package ", pkg, options.min ? ";" : ";\n\n");
buildOptions(ptr.options, "");
buildNamespace(ptr, "");
return out.join('');
};
/**
* Module description.
* @type {string}
*/
proto.description = description;
/**
* Converts a JavaScript value to a .proto value.
* @param {*} v Value
* @returns {string} Dot proto value
*/
function value(v) {
switch (typeof v) {
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return v.toString();
case 'string':
return '"'+v.replace(/"/g, '\\"')+'"';
default:
throw new Error("illegal value type: "+typeof(v));
}
}

128
node_modules/protobufjs/cli/pbjs/util.js generated vendored Normal file
View File

@ -0,0 +1,128 @@
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var ProtoBuf = require("../../index.js");
/**
* Utility namespace.
* @exports pbjs/util
* @namespace
*/
var util = module.exports = {};
/**
* Extracts builder options with the specified prefix from a set of CLI options.
* @param {!Object.<string,*>} options CLI options
* @param {string} prefix Prefix
* @returns {!Object.<string,*>}
*/
util.getBuilderOptions = function(options, prefix) {
if (!options[prefix])
return {};
var builderOptions = {};
options[prefix].forEach(function(kv) {
var key, val;
var p = kv.indexOf("=");
if (p < 0) {
key = kv;
val = true;
} else {
key = kv.substring(0, p);
val = kv.substring(p+1);
if (val === "true")
val = true;
else if (val === "false")
val = false;
else {
var intval = parseInt(val, 10);
if (intval == val)
val = intval;
}
}
builderOptions[key] = val;
});
return builderOptions;
};
/**
* Pads a string to the specified length.
* @param {string} str String to pad
* @param {number} len Pad length
* @param {boolean=} left Whether to pad to the left, defaults to `false`
* @returns {string}
*/
util.pad = function(str, len, left) {
while (str.length < len)
left ? str = " "+str : str += " ";
return str;
};
/**
* Indents a string by the specified whitespace.
* @param {string} str String to indent
* @param {string|number} ws Whitespace string or number of whitespaces
* @returns {string}
*/
util.indent = function(str, ws) {
if (ws === 0 || ws === "")
return str;
var lines = str.split(/\r?\n/);
if (typeof ws === 'number') {
var n = ws; ws = "";
while (ws.length < n) ws += " ";
}
for (var i=1; i<lines.length; ++i)
lines[i] = ws+lines[i];
return lines.join("\n");
};
/**
* Extends an object with additional properties.
* @param {!Object.<string,*>} subject Subject to extend
* @param {!Object.<string,*>} extension Extensions to apply
*/
util.extend = function(subject, extension) {
Object.keys(extension).forEach(function(key) {
subject[key] = extension[key];
});
};
/**
* Groups extensions by extended message.
* @param {!ProtoBuf.Reflect.Namespace} ns Namespace
* @returns {?Object.<string,!Array.<!ProtoBuf.Reflect.Message.ExtensionField>>}
*/
util.groupExtensions = function(ns) {
var exts = {},
n = 0;
ns.getChildren(ProtoBuf.Reflect.Extension).forEach(function(ext) {
var msg = ext.field.parent,
fqn = msg.fqn();
if (!exts[fqn])
exts[fqn] = [];
exts[fqn].push(ext.field);
n++;
});
return n > 0 ? exts : null;
};
/**
* Tests if the specified import name is referencing an internal descriptor.
* @param {string} name Import name
* @returns {boolean}
*/
util.isDescriptor = function(name) {
return /^google\/protobuf\/descriptor/.test(name);
};