123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- "use strict";
- module.exports = fetch;
- var asPromise = require("@protobufjs/aspromise"),
- inquire = require("@protobufjs/inquire");
- var fs = inquire("fs");
- function fetch(filename, options, callback) {
- if (typeof options === "function") {
- callback = options;
- options = {};
- } else if (!options)
- options = {};
- if (!callback)
- return asPromise(fetch, this, filename, options);
-
- if (!options.xhr && fs && fs.readFile)
- return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
- return err && typeof XMLHttpRequest !== "undefined"
- ? fetch.xhr(filename, options, callback)
- : err
- ? callback(err)
- : callback(null, options.binary ? contents : contents.toString("utf8"));
- });
-
- return fetch.xhr(filename, options, callback);
- }
- = function fetchOnReadyStateChange() {
- if (xhr.readyState !== 4)
- return undefined;
-
-
-
- if (xhr.status !== 0 && xhr.status !== 200)
- return callback(Error("status " + xhr.status));
-
-
- if (options.binary) {
- var buffer = xhr.response;
- if (!buffer) {
- buffer = [];
- for (var i = 0; i < xhr.responseText.length; ++i)
- buffer.push(xhr.responseText.charCodeAt(i) & 255);
- }
- return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
- }
- return callback(null, xhr.responseText);
- };
- if (options.binary) {
-
- if ("overrideMimeType" in xhr)
- xhr.overrideMimeType("text/plain; charset=x-user-defined");
- xhr.responseType = "arraybuffer";
- }
- xhr.open("GET", filename);
- xhr.send();
- };
|