--- /dev/null
+"use strict";\r
+module.exports = fetch;\r
+\r
+var asPromise = require("@protobufjs/aspromise"),\r
+ inquire = require("@protobufjs/inquire");\r
+\r
+var fs = inquire("fs");\r
+\r
+/**\r
+ * Node-style callback as used by {@link util.fetch}.\r
+ * @typedef FetchCallback\r
+ * @type {function}\r
+ * @param {?Error} error Error, if any, otherwise `null`\r
+ * @param {string} [contents] File contents, if there hasn't been an error\r
+ * @returns {undefined}\r
+ */\r
+\r
+/**\r
+ * Options as used by {@link util.fetch}.\r
+ * @typedef FetchOptions\r
+ * @type {Object}\r
+ * @property {boolean} [binary=false] Whether expecting a binary response\r
+ * @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest\r
+ */\r
+\r
+/**\r
+ * Fetches the contents of a file.\r
+ * @memberof util\r
+ * @param {string} filename File path or url\r
+ * @param {FetchOptions} options Fetch options\r
+ * @param {FetchCallback} callback Callback function\r
+ * @returns {undefined}\r
+ */\r
+function fetch(filename, options, callback) {\r
+ if (typeof options === "function") {\r
+ callback = options;\r
+ options = {};\r
+ } else if (!options)\r
+ options = {};\r
+\r
+ if (!callback)\r
+ return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this\r
+\r
+ // if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.\r
+ if (!options.xhr && fs && fs.readFile)\r
+ return fs.readFile(filename, function fetchReadFileCallback(err, contents) {\r
+ return err && typeof XMLHttpRequest !== "undefined"\r
+ ? fetch.xhr(filename, options, callback)\r
+ : err\r
+ ? callback(err)\r
+ : callback(null, options.binary ? contents : contents.toString("utf8"));\r
+ });\r
+\r
+ // use the XHR version otherwise.\r
+ return fetch.xhr(filename, options, callback);\r
+}\r
+\r
+/**\r
+ * Fetches the contents of a file.\r
+ * @name util.fetch\r
+ * @function\r
+ * @param {string} path File path or url\r
+ * @param {FetchCallback} callback Callback function\r
+ * @returns {undefined}\r
+ * @variation 2\r
+ */\r
+\r
+/**\r
+ * Fetches the contents of a file.\r
+ * @name util.fetch\r
+ * @function\r
+ * @param {string} path File path or url\r
+ * @param {FetchOptions} [options] Fetch options\r
+ * @returns {Promise<string|Uint8Array>} Promise\r
+ * @variation 3\r
+ */\r
+\r
+/**/\r
+fetch.xhr = function fetch_xhr(filename, options, callback) {\r
+ var xhr = new XMLHttpRequest();\r
+ xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {\r
+\r
+ if (xhr.readyState !== 4)\r
+ return undefined;\r
+\r
+ // local cors security errors return status 0 / empty string, too. afaik this cannot be\r
+ // reliably distinguished from an actually empty file for security reasons. feel free\r
+ // to send a pull request if you are aware of a solution.\r
+ if (xhr.status !== 0 && xhr.status !== 200)\r
+ return callback(Error("status " + xhr.status));\r
+\r
+ // if binary data is expected, make sure that some sort of array is returned, even if\r
+ // ArrayBuffers are not supported. the binary string fallback, however, is unsafe.\r
+ if (options.binary) {\r
+ var buffer = xhr.response;\r
+ if (!buffer) {\r
+ buffer = [];\r
+ for (var i = 0; i < xhr.responseText.length; ++i)\r
+ buffer.push(xhr.responseText.charCodeAt(i) & 255);\r
+ }\r
+ return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);\r
+ }\r
+ return callback(null, xhr.responseText);\r
+ };\r
+\r
+ if (options.binary) {\r
+ // ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers\r
+ if ("overrideMimeType" in xhr)\r
+ xhr.overrideMimeType("text/plain; charset=x-user-defined");\r
+ xhr.responseType = "arraybuffer";\r
+ }\r
+\r
+ xhr.open("GET", filename);\r
+ xhr.send();\r
+};\r