init project

This commit is contained in:
root
2026-07-31 13:12:54 -04:00
parent 0da92d5e02
commit f3863f760c
7215 changed files with 1860260 additions and 1 deletions
+8
View File
@@ -0,0 +1,8 @@
# Changelog
## [1.1.1](https://github.com/protobufjs/protobuf.js/compare/fetch-v1.1.0...fetch-v1.1.1) (2026-05-17)
### Bug Fixes
* Backport bundler-safe optional module lookups ([#2254](https://github.com/protobufjs/protobuf.js/issues/2254)) ([0853a62](https://github.com/protobufjs/protobuf.js/commit/0853a625680f9247596b84ef48082b8f4e554797))
+26
View File
@@ -0,0 +1,26 @@
Copyright (c) 2016, Daniel Wirtz All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of its author, nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+13
View File
@@ -0,0 +1,13 @@
@protobufjs/fetch
=================
[![npm](https://img.shields.io/npm/v/@protobufjs/fetch.svg)](https://www.npmjs.com/package/@protobufjs/fetch)
Fetches the contents of a file accross node and browsers.
API
---
* **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)
+56
View File
@@ -0,0 +1,56 @@
export = fetch;
/**
* Node-style callback as used by {@link util.fetch}.
* @typedef FetchCallback
* @type {function}
* @param {?Error} error Error, if any, otherwise `null`
* @param {string} [contents] File contents, if there hasn't been an error
* @returns {undefined}
*/
type FetchCallback = (error: Error, contents?: string) => void;
/**
* Options as used by {@link util.fetch}.
* @typedef IFetchOptions
* @type {Object}
* @property {boolean} [binary=false] Whether expecting a binary response
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
*/
interface IFetchOptions {
binary?: boolean;
xhr?: boolean;
}
/**
* Fetches the contents of a file.
* @memberof util
* @param {string} filename File path or url
* @param {IFetchOptions} options Fetch options
* @param {FetchCallback} callback Callback function
* @returns {undefined}
*/
declare function fetch(filename: string, options: IFetchOptions, 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 {undefined}
* @variation 2
*/
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 {IFetchOptions} [options] Fetch options
* @returns {Promise<string|Uint8Array>} Promise
* @variation 3
*/
declare function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
+112
View File
@@ -0,0 +1,112 @@
"use strict";
module.exports = fetch;
var asPromise = require("@protobufjs/aspromise"),
fs = require("./util/fs");
/**
* Node-style callback as used by {@link util.fetch}.
* @typedef FetchCallback
* @type {function}
* @param {?Error} error Error, if any, otherwise `null`
* @param {string} [contents] File contents, if there hasn't been an error
* @returns {undefined}
*/
/**
* Options as used by {@link util.fetch}.
* @interface IFetchOptions
* @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} filename File path or url
* @param {IFetchOptions} options Fetch options
* @param {FetchCallback} callback Callback function
* @returns {undefined}
*/
function fetch(filename, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
} else if (!options)
options = {};
if (!callback)
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(filename, options, callback)
: err
? callback(err)
: callback(null, options.binary ? contents : contents.toString("utf8"));
});
// use the XHR version otherwise.
return fetch.xhr(filename, options, 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 {IFetchOptions} [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() {
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);
};
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();
};
+27
View File
@@ -0,0 +1,27 @@
{
"name": "@protobufjs/fetch",
"description": "Fetches the contents of a file accross node and browsers.",
"version": "1.1.1",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
"repository": {
"type": "git",
"url": "https://github.com/dcodeIO/protobuf.js.git"
},
"dependencies": {
"@protobufjs/aspromise": "^1.1.1"
},
"license": "BSD-3-Clause",
"main": "index.js",
"types": "index.d.ts",
"browser": {
"fs": false
},
"devDependencies": {
"istanbul": "^0.4.5",
"tape": "^5.0.0"
},
"scripts": {
"test": "tape tests/*.js",
"coverage": "istanbul cover node_modules/tape/bin/tape tests/*.js"
}
}
+1
View File
@@ -0,0 +1 @@
file.txt
+158
View File
@@ -0,0 +1,158 @@
var tape = require("tape");
var fetch = require("..");
tape.test("fetch", function(test) {
if (typeof Promise !== "undefined")
test.test(test.name + " - promise", function(test) {
var promise = fetch("NOTFOUND");
test.plan(2);
test.ok(promise instanceof Promise, "should return a promise if callback has been omitted");
promise
.then(function() {
test.fail("should not resolve");
})
.catch(function(err) {
test.ok(err, "should reject with an error");
});
});
test.test(test.name + " - node fs", function(test) {
test.test(test.name + " - string", function(test) {
test.plan(2);
fetch(require.resolve("./data/file.txt"), function(err, contents) {
test.notOk(err, "should not return an error");
test.equal(contents, "file.txt", "should return contents as a string");
});
});
test.test(test.name + " - binary", function(test) {
test.plan(2);
fetch(require.resolve("./data/file.txt"), { binary: true }, function(err, contents) {
test.notOk(err, "should not return an error");
test.same(contents, new Buffer("file.txt", "utf8"), "should return contents as a Buffer");
});
});
test.test(test.name + " - fallback", function(test) {
test.plan(2);
global.XMLHttpRequest = fakeXHR(0);
fetch("file.txt", function(err, contents) {
delete global.XMLHttpRequest;
test.notOk(err, "should not return an error");
test.same(contents, "file.txt", "should return contents as a string");
});
});
test.end();
});
test.test(test.name + " - XMLHttpRequest", function(test) {
test.test(test.name + " - 404", function(test) {
global.XMLHttpRequest = fakeXHR(404);
fetch("file.txt", { xhr: true }, function(err) {
delete global.XMLHttpRequest;
test.ok(err, "should return an error");
test.end();
});
});
test.test(test.name + " - string", function(test) {
global.XMLHttpRequest = fakeXHR(0);
test.plan(2);
fetch("file.txt", { xhr: true }, function(err, contents) {
delete global.XMLHttpRequest;
test.notOk(err, "should not return an error");
test.equal(contents, "file.txt", "should return contents as a string");
});
});
test.test(test.name + " - binary", function(test) {
global.XMLHttpRequest = fakeXHR(200);
test.plan(2);
fetch("file.txt", { xhr: true, binary: true }, function(err, contents) {
delete global.XMLHttpRequest;
test.notOk(err, "should not return an error");
test.same(contents, new Uint8Array([0x66, 0x69, 0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74]), "should return contents as an Uint8Array");
});
});
});
test.test(test.name + " - XMLHttpRequest (ancient)", function(test) {
test.test(test.name + " - string", function(test) {
global.XMLHttpRequest = fakeXHR(0, true);
test.plan(2);
fetch("file.txt", { xhr: true }, function(err, contents) {
delete global.XMLHttpRequest;
test.notOk(err, "should not return an error");
test.equal(contents, "file.txt", "should return contents as a string");
});
});
test.test(test.name + " - binary", function(test) {
global.XMLHttpRequest = fakeXHR(200, true);
var U8 = global.Uint8Array;
delete global.Uint8Array;
test.plan(2);
fetch("file.txt", { xhr: true, binary: true }, function(err, contents) {
delete global.XMLHttpRequest;
global.Uint8Array = U8;
test.notOk(err, "should not return an error");
test.same(contents, [0x66, 0x69, 0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74], "should return contents as an Array");
});
});
});
});
function fakeXHR(status, ancient) {
var UNSENT = 0,
OPENED = 1,
HEADERS_RECEIVED = 2,
LOADING = 3,
DONE = 4;
function XMLHttpRequest() {
this.status = 0;
this.readyState = UNSENT;
}
if (!ancient)
XMLHttpRequest.prototype.overrideMimeType = function(mimeType) {
this._mimeType = mimeType;
};
XMLHttpRequest.prototype.open = function open(method, path) {
this._method = method;
this._path = path;
this.readyState = OPENED;
};
XMLHttpRequest.prototype.send = function send() {
var self = this;
setTimeout(function() {
self.onreadystatechange(); // opened
self.readyState = HEADERS_RECEIVED;
self.onreadystatechange();
self.readyState = LOADING;
self.onreadystatechange();
self.readyState = DONE;
self.status = status;
if (self.responseType === "arraybuffer" && !ancient) {
var buf = new Buffer(self._path, "utf8");
var abuf = new ArrayBuffer(buf.length);
var view = new Uint8Array(abuf);
for (var i = 0; i < buf.length; ++i)
view[i] = buf[i];
self.response = abuf;
} else
self.responseText = self._path;
self.onreadystatechange();
});
};
return XMLHttpRequest;
}
+11
View File
@@ -0,0 +1,11 @@
"use strict";
var fs = null;
try {
fs = require(/* webpackIgnore: true */ "fs");
if (!fs || !fs.readFile || !fs.readFileSync)
fs = null;
} catch (e) {
// `fs` is unavailable in browsers and browser-like bundles.
}
module.exports = fs;