Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. module.exports = fetch;
  3. var asPromise = require("@protobufjs/aspromise"),
  4. inquire = require("@protobufjs/inquire");
  5. var fs = inquire("fs");
  6. /**
  7. * Node-style callback as used by {@link util.fetch}.
  8. * @typedef FetchCallback
  9. * @type {function}
  10. * @param {?Error} error Error, if any, otherwise `null`
  11. * @param {string} [contents] File contents, if there hasn't been an error
  12. * @returns {undefined}
  13. */
  14. /**
  15. * Options as used by {@link util.fetch}.
  16. * @typedef FetchOptions
  17. * @type {Object}
  18. * @property {boolean} [binary=false] Whether expecting a binary response
  19. * @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
  20. */
  21. /**
  22. * Fetches the contents of a file.
  23. * @memberof util
  24. * @param {string} filename File path or url
  25. * @param {FetchOptions} options Fetch options
  26. * @param {FetchCallback} callback Callback function
  27. * @returns {undefined}
  28. */
  29. function fetch(filename, options, callback) {
  30. if (typeof options === "function") {
  31. callback = options;
  32. options = {};
  33. } else if (!options)
  34. options = {};
  35. if (!callback)
  36. return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
  37. // if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
  38. if (!options.xhr && fs && fs.readFile)
  39. return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
  40. return err && typeof XMLHttpRequest !== "undefined"
  41. ? fetch.xhr(filename, options, callback)
  42. : err
  43. ? callback(err)
  44. : callback(null, options.binary ? contents : contents.toString("utf8"));
  45. });
  46. // use the XHR version otherwise.
  47. return fetch.xhr(filename, options, callback);
  48. }
  49. /**
  50. * Fetches the contents of a file.
  51. * @name util.fetch
  52. * @function
  53. * @param {string} path File path or url
  54. * @param {FetchCallback} callback Callback function
  55. * @returns {undefined}
  56. * @variation 2
  57. */
  58. /**
  59. * Fetches the contents of a file.
  60. * @name util.fetch
  61. * @function
  62. * @param {string} path File path or url
  63. * @param {FetchOptions} [options] Fetch options
  64. * @returns {Promise<string|Uint8Array>} Promise
  65. * @variation 3
  66. */
  67. /**/
  68. fetch.xhr = function fetch_xhr(filename, options, callback) {
  69. var xhr = new XMLHttpRequest();
  70. xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
  71. if (xhr.readyState !== 4)
  72. return undefined;
  73. // local cors security errors return status 0 / empty string, too. afaik this cannot be
  74. // reliably distinguished from an actually empty file for security reasons. feel free
  75. // to send a pull request if you are aware of a solution.
  76. if (xhr.status !== 0 && xhr.status !== 200)
  77. return callback(Error("status " + xhr.status));
  78. // if binary data is expected, make sure that some sort of array is returned, even if
  79. // ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
  80. if (options.binary) {
  81. var buffer = xhr.response;
  82. if (!buffer) {
  83. buffer = [];
  84. for (var i = 0; i < xhr.responseText.length; ++i)
  85. buffer.push(xhr.responseText.charCodeAt(i) & 255);
  86. }
  87. return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
  88. }
  89. return callback(null, xhr.responseText);
  90. };
  91. if (options.binary) {
  92. // ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
  93. if ("overrideMimeType" in xhr)
  94. xhr.overrideMimeType("text/plain; charset=x-user-defined");
  95. xhr.responseType = "arraybuffer";
  96. }
  97. xhr.open("GET", filename);
  98. xhr.send();
  99. };