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