--- /dev/null
+/**\r
+ * @alias ProtoBuf.Util\r
+ * @expose\r
+ */\r
+ProtoBuf.Util = (function() {\r
+ "use strict";\r
+\r
+ /**\r
+ * ProtoBuf utilities.\r
+ * @exports ProtoBuf.Util\r
+ * @namespace\r
+ */\r
+ var Util = {};\r
+\r
+ /**\r
+ * Flag if running in node or not.\r
+ * @type {boolean}\r
+ * @const\r
+ * @expose\r
+ */\r
+ Util.IS_NODE = !!(\r
+ typeof process === 'object' && process+'' === '[object process]' && !process['browser']\r
+ );\r
+\r
+ /**\r
+ * Constructs a XMLHttpRequest object.\r
+ * @return {XMLHttpRequest}\r
+ * @throws {Error} If XMLHttpRequest is not supported\r
+ * @expose\r
+ */\r
+ Util.XHR = function() {\r
+ // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html\r
+ var XMLHttpFactories = [\r
+ function () {return new XMLHttpRequest()},\r
+ function () {return new ActiveXObject("Msxml2.XMLHTTP")},\r
+ function () {return new ActiveXObject("Msxml3.XMLHTTP")},\r
+ function () {return new ActiveXObject("Microsoft.XMLHTTP")}\r
+ ];\r
+ /** @type {?XMLHttpRequest} */\r
+ var xhr = null;\r
+ for (var i=0;i<XMLHttpFactories.length;i++) {\r
+ try { xhr = XMLHttpFactories[i](); }\r
+ catch (e) { continue; }\r
+ break;\r
+ }\r
+ if (!xhr)\r
+ throw Error("XMLHttpRequest is not supported");\r
+ return xhr;\r
+ };\r
+\r
+ /**\r
+ * Fetches a resource.\r
+ * @param {string} path Resource path\r
+ * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will\r
+ * be fetched synchronously. If the request failed, contents will be null.\r
+ * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.\r
+ * @expose\r
+ */\r
+ Util.fetch = function(path, callback) {\r
+ if (callback && typeof callback != 'function')\r
+ callback = null;\r
+ if (Util.IS_NODE) {\r
+ var fs = require("fs");\r
+ if (callback) {\r
+ fs.readFile(path, function(err, data) {\r
+ if (err)\r
+ callback(null);\r
+ else\r
+ callback(""+data);\r
+ });\r
+ } else\r
+ try {\r
+ return fs.readFileSync(path);\r
+ } catch (e) {\r
+ return null;\r
+ }\r
+ } else {\r
+ var xhr = Util.XHR();\r
+ xhr.open('GET', path, callback ? true : false);\r
+ // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');\r
+ xhr.setRequestHeader('Accept', 'text/plain');\r
+ if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');\r
+ if (callback) {\r
+ xhr.onreadystatechange = function() {\r
+ if (xhr.readyState != 4) return;\r
+ if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))\r
+ callback(xhr.responseText);\r
+ else\r
+ callback(null);\r
+ };\r
+ if (xhr.readyState == 4)\r
+ return;\r
+ xhr.send(null);\r
+ } else {\r
+ xhr.send(null);\r
+ if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))\r
+ return xhr.responseText;\r
+ return null;\r
+ }\r
+ }\r
+ };\r
+\r
+ /**\r
+ * Converts a string to camel case.\r
+ * @param {string} str\r
+ * @returns {string}\r
+ * @expose\r
+ */\r
+ Util.toCamelCase = function(str) {\r
+ return str.replace(/_([a-zA-Z])/g, function ($0, $1) {\r
+ return $1.toUpperCase();\r
+ });\r
+ };\r
+ \r
+ return Util;\r
+})();\r