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