Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / node_modules / protobufjs / src / ProtoBuf / Util.js
1 /**\r
2  * @alias ProtoBuf.Util\r
3  * @expose\r
4  */\r
5 ProtoBuf.Util = (function() {\r
6     "use strict";\r
7 \r
8     /**\r
9      * ProtoBuf utilities.\r
10      * @exports ProtoBuf.Util\r
11      * @namespace\r
12      */\r
13     var Util = {};\r
14 \r
15     /**\r
16      * Flag if running in node or not.\r
17      * @type {boolean}\r
18      * @const\r
19      * @expose\r
20      */\r
21     Util.IS_NODE = !!(\r
22         typeof process === 'object' && process+'' === '[object process]' && !process['browser']\r
23     );\r
24 \r
25     /**\r
26      * Constructs a XMLHttpRequest object.\r
27      * @return {XMLHttpRequest}\r
28      * @throws {Error} If XMLHttpRequest is not supported\r
29      * @expose\r
30      */\r
31     Util.XHR = function() {\r
32         // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html\r
33         var XMLHttpFactories = [\r
34             function () {return new XMLHttpRequest()},\r
35             function () {return new ActiveXObject("Msxml2.XMLHTTP")},\r
36             function () {return new ActiveXObject("Msxml3.XMLHTTP")},\r
37             function () {return new ActiveXObject("Microsoft.XMLHTTP")}\r
38         ];\r
39         /** @type {?XMLHttpRequest} */\r
40         var xhr = null;\r
41         for (var i=0;i<XMLHttpFactories.length;i++) {\r
42             try { xhr = XMLHttpFactories[i](); }\r
43             catch (e) { continue; }\r
44             break;\r
45         }\r
46         if (!xhr)\r
47             throw Error("XMLHttpRequest is not supported");\r
48         return xhr;\r
49     };\r
50 \r
51     /**\r
52      * Fetches a resource.\r
53      * @param {string} path Resource path\r
54      * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will\r
55      *   be fetched synchronously. If the request failed, contents will be null.\r
56      * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.\r
57      * @expose\r
58      */\r
59     Util.fetch = function(path, callback) {\r
60         if (callback && typeof callback != 'function')\r
61             callback = null;\r
62         if (Util.IS_NODE) {\r
63             var fs = require("fs");\r
64             if (callback) {\r
65                 fs.readFile(path, function(err, data) {\r
66                     if (err)\r
67                         callback(null);\r
68                     else\r
69                         callback(""+data);\r
70                 });\r
71             } else\r
72                 try {\r
73                     return fs.readFileSync(path);\r
74                 } catch (e) {\r
75                     return null;\r
76                 }\r
77         } else {\r
78             var xhr = Util.XHR();\r
79             xhr.open('GET', path, callback ? true : false);\r
80             // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');\r
81             xhr.setRequestHeader('Accept', 'text/plain');\r
82             if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');\r
83             if (callback) {\r
84                 xhr.onreadystatechange = function() {\r
85                     if (xhr.readyState != 4) return;\r
86                     if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))\r
87                         callback(xhr.responseText);\r
88                     else\r
89                         callback(null);\r
90                 };\r
91                 if (xhr.readyState == 4)\r
92                     return;\r
93                 xhr.send(null);\r
94             } else {\r
95                 xhr.send(null);\r
96                 if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))\r
97                     return xhr.responseText;\r
98                 return null;\r
99             }\r
100         }\r
101     };\r
102 \r
103     /**\r
104      * Converts a string to camel case.\r
105      * @param {string} str\r
106      * @returns {string}\r
107      * @expose\r
108      */\r
109     Util.toCamelCase = function(str) {\r
110         return str.replace(/_([a-zA-Z])/g, function ($0, $1) {\r
111             return $1.toUpperCase();\r
112         });\r
113     };\r
114     \r
115     return Util;\r
116 })();\r