@protobufjs/fetch
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -14,8 +14,44 @@ export = fetch; | ||
/** | ||
* Options as used by {@link util.fetch}. | ||
* @typedef FetchOptions | ||
* @type {Object} | ||
* @property {boolean} [binary=false] Whether expecting a binary response | ||
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest | ||
*/ | ||
interface FetchOptions { | ||
binary?: boolean; | ||
xhr?: boolean | ||
} | ||
/** | ||
* Fetches the contents of a file. | ||
* @memberof util | ||
* @param {string} filename File path or url | ||
* @param {FetchOptions} options Fetch options | ||
* @param {FetchCallback} callback Callback function | ||
* @returns {undefined} | ||
*/ | ||
declare function fetch(filename: string, options: FetchOptions, callback: FetchCallback): void; | ||
/** | ||
* Fetches the contents of a file. | ||
* @name util.fetch | ||
* @function | ||
* @param {string} path File path or url | ||
* @param {FetchCallback} [callback] Callback function | ||
* @returns {Promise<string>|undefined} A Promise if `callback` has been omitted | ||
* @param {FetchCallback} callback Callback function | ||
* @returns {undefined} | ||
* @variation 2 | ||
*/ | ||
declare function fetch(path: string, callback?: FetchCallback): (Promise<string>|undefined); | ||
declare function fetch(path: string, callback: FetchCallback): void; | ||
/** | ||
* Fetches the contents of a file. | ||
* @name util.fetch | ||
* @function | ||
* @param {string} path File path or url | ||
* @param {FetchOptions} [options] Fetch options | ||
* @returns {Promise<string|Uint8Array>} Promise | ||
* @variation 3 | ||
*/ | ||
declare function fetch(path: string, options?: FetchOptions): Promise<(string|Uint8Array)>; |
100
index.js
@@ -19,34 +19,98 @@ "use strict"; | ||
/** | ||
* Options as used by {@link util.fetch}. | ||
* @typedef FetchOptions | ||
* @type {Object} | ||
* @property {boolean} [binary=false] Whether expecting a binary response | ||
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest | ||
*/ | ||
/** | ||
* Fetches the contents of a file. | ||
* @memberof util | ||
* @param {string} path File path or url | ||
* @param {FetchCallback} [callback] Callback function | ||
* @returns {Promise<string>|undefined} A Promise if `callback` has been omitted | ||
* @param {string} filename File path or url | ||
* @param {FetchOptions} options Fetch options | ||
* @param {FetchCallback} callback Callback function | ||
* @returns {undefined} | ||
*/ | ||
function fetch(path, callback) { | ||
function fetch(filename, options, callback) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} else if (!options) | ||
options = {}; | ||
if (!callback) | ||
return asPromise(fetch, this, path); // eslint-disable-line no-invalid-this | ||
if (fs && fs.readFile) | ||
return fs.readFile(path, "utf8", function fetchReadFileCallback(err, contents) { | ||
return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this | ||
// if a node-like filesystem is present, try it first but fall back to XHR if nothing is found. | ||
if (!options.xhr && fs && fs.readFile) | ||
return fs.readFile(filename, function fetchReadFileCallback(err, contents) { | ||
return err && typeof XMLHttpRequest !== "undefined" | ||
? fetch_xhr(path, callback) | ||
: callback(err, contents); | ||
? fetch.xhr(filename, options, callback) | ||
: err | ||
? callback(err) | ||
: callback(null, options.binary ? contents : contents.toString("utf8")); | ||
}); | ||
return fetch_xhr(path, callback); | ||
// use the XHR version otherwise. | ||
return fetch.xhr(filename, options, callback); | ||
} | ||
function fetch_xhr(path, callback) { | ||
/** | ||
* Fetches the contents of a file. | ||
* @name util.fetch | ||
* @function | ||
* @param {string} path File path or url | ||
* @param {FetchCallback} callback Callback function | ||
* @returns {undefined} | ||
* @variation 2 | ||
*/ | ||
/** | ||
* Fetches the contents of a file. | ||
* @name util.fetch | ||
* @function | ||
* @param {string} path File path or url | ||
* @param {FetchOptions} [options] Fetch options | ||
* @returns {Promise<string|Uint8Array>} Promise | ||
* @variation 3 | ||
*/ | ||
/**/ | ||
fetch.xhr = function fetch_xhr(filename, options, callback) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() { | ||
return xhr.readyState === 4 | ||
? xhr.status === 0 || xhr.status === 200 | ||
? callback(null, xhr.responseText) | ||
: callback(Error("status " + xhr.status)) | ||
: undefined; | ||
if (xhr.readyState !== 4) | ||
return undefined; | ||
// local cors security errors return status 0 / empty string, too. afaik this cannot be | ||
// reliably distinguished from an actually empty file for security reasons. feel free | ||
// to send a pull request if you are aware of a solution. | ||
if (xhr.status !== 0 && xhr.status !== 200) | ||
return callback(Error("status " + xhr.status)); | ||
// if binary data is expected, make sure that some sort of array is returned, even if | ||
// ArrayBuffers are not supported. the binary string fallback, however, is unsafe. | ||
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); | ||
}; | ||
xhr.open("GET", path); | ||
if (options.binary) { | ||
// ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers | ||
if ("overrideMimeType" in xhr) | ||
xhr.overrideMimeType("text/plain; charset=x-user-defined"); | ||
xhr.responseType = "arraybuffer"; | ||
} | ||
xhr.open("GET", filename); | ||
xhr.send(); | ||
} | ||
}; |
{ | ||
"name": "@protobufjs/fetch", | ||
"description": "Fetches the contents of a file accross node and browsers.", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>", | ||
@@ -11,8 +11,16 @@ "repository": { | ||
"dependencies": { | ||
"@protobufjs/aspromise": "^1.0.5", | ||
"@protobufjs/inquire": "^1.0.2" | ||
"@protobufjs/aspromise": "^1.1.1", | ||
"@protobufjs/inquire": "^1.1.0" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"main": "index.js", | ||
"types": "index.d.ts" | ||
"types": "index.d.ts", | ||
"devDependencies": { | ||
"istanbul": "^0.4.5", | ||
"tape": "^4.6.3" | ||
}, | ||
"scripts": { | ||
"test": "tape tests/*.js", | ||
"coverage": "istanbul cover node_modules/tape/bin/tape tests/*.js" | ||
} | ||
} |
@@ -10,5 +10,5 @@ @protobufjs/fetch | ||
* **fetch(path: `string`, [callback: `function(error: ?Error, [contents: string])`]): `Promise<string>|undefined`** | ||
* **fetch(path: `string`, [options: { binary: boolean } ], [callback: `function(error: ?Error, [contents: string])`]): `Promise<string|Uint8Array>|undefined`** | ||
Fetches the contents of a file. | ||
**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause) |
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
8762
6
1
160
0
2
6
Updated@protobufjs/aspromise@^1.1.1
Updated@protobufjs/inquire@^1.1.0