Path: blob/master/node_modules/@protobufjs/fetch/index.js
1126 views
"use strict";1module.exports = fetch;23var asPromise = require("@protobufjs/aspromise"),4inquire = require("@protobufjs/inquire");56var fs = inquire("fs");78/**9* Node-style callback as used by {@link util.fetch}.10* @typedef FetchCallback11* @type {function}12* @param {?Error} error Error, if any, otherwise `null`13* @param {string} [contents] File contents, if there hasn't been an error14* @returns {undefined}15*/1617/**18* Options as used by {@link util.fetch}.19* @typedef FetchOptions20* @type {Object}21* @property {boolean} [binary=false] Whether expecting a binary response22* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest23*/2425/**26* Fetches the contents of a file.27* @memberof util28* @param {string} filename File path or url29* @param {FetchOptions} options Fetch options30* @param {FetchCallback} callback Callback function31* @returns {undefined}32*/33function fetch(filename, options, callback) {34if (typeof options === "function") {35callback = options;36options = {};37} else if (!options)38options = {};3940if (!callback)41return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this4243// if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.44if (!options.xhr && fs && fs.readFile)45return fs.readFile(filename, function fetchReadFileCallback(err, contents) {46return err && typeof XMLHttpRequest !== "undefined"47? fetch.xhr(filename, options, callback)48: err49? callback(err)50: callback(null, options.binary ? contents : contents.toString("utf8"));51});5253// use the XHR version otherwise.54return fetch.xhr(filename, options, callback);55}5657/**58* Fetches the contents of a file.59* @name util.fetch60* @function61* @param {string} path File path or url62* @param {FetchCallback} callback Callback function63* @returns {undefined}64* @variation 265*/6667/**68* Fetches the contents of a file.69* @name util.fetch70* @function71* @param {string} path File path or url72* @param {FetchOptions} [options] Fetch options73* @returns {Promise<string|Uint8Array>} Promise74* @variation 375*/7677/**/78fetch.xhr = function fetch_xhr(filename, options, callback) {79var xhr = new XMLHttpRequest();80xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {8182if (xhr.readyState !== 4)83return undefined;8485// local cors security errors return status 0 / empty string, too. afaik this cannot be86// reliably distinguished from an actually empty file for security reasons. feel free87// to send a pull request if you are aware of a solution.88if (xhr.status !== 0 && xhr.status !== 200)89return callback(Error("status " + xhr.status));9091// if binary data is expected, make sure that some sort of array is returned, even if92// ArrayBuffers are not supported. the binary string fallback, however, is unsafe.93if (options.binary) {94var buffer = xhr.response;95if (!buffer) {96buffer = [];97for (var i = 0; i < xhr.responseText.length; ++i)98buffer.push(xhr.responseText.charCodeAt(i) & 255);99}100return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);101}102return callback(null, xhr.responseText);103};104105if (options.binary) {106// ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers107if ("overrideMimeType" in xhr)108xhr.overrideMimeType("text/plain; charset=x-user-defined");109xhr.responseType = "arraybuffer";110}111112xhr.open("GET", filename);113xhr.send();114};115116117