2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-11-30 15:44:23 -05:00
|
|
|
exports.FetchResponse = exports.FetchRequest = exports.FetchCancelSignal = void 0;
|
|
|
|
/**
|
|
|
|
* Explain fetching here...
|
|
|
|
*
|
2022-12-02 21:27:06 -05:00
|
|
|
* @_section api/utils/fetching:Fetching Web Content [about-fetch]
|
2022-11-30 15:44:23 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
const base64_js_1 = require("./base64.js");
|
|
|
|
const data_js_1 = require("./data.js");
|
2022-09-15 22:58:45 -04:00
|
|
|
const errors_js_1 = require("./errors.js");
|
2022-09-05 16:57:11 -04:00
|
|
|
const properties_js_1 = require("./properties.js");
|
|
|
|
const utf8_js_1 = require("./utf8.js");
|
|
|
|
const geturl_js_1 = require("./geturl.js");
|
|
|
|
const MAX_ATTEMPTS = 12;
|
|
|
|
const SLOT_INTERVAL = 250;
|
|
|
|
// The global FetchGetUrlFunc implementation.
|
|
|
|
let getUrlFunc = geturl_js_1.getUrl;
|
|
|
|
const reData = new RegExp("^data:([^;:]*)?(;base64)?,(.*)$", "i");
|
|
|
|
const reIpfs = new RegExp("^ipfs:/\/(ipfs/)?(.*)$", "i");
|
|
|
|
// If locked, new Gateways cannot be added
|
|
|
|
let locked = false;
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
|
2022-11-30 15:44:23 -05:00
|
|
|
async function dataGatewayFunc(url, signal) {
|
2022-09-05 16:57:11 -04:00
|
|
|
try {
|
|
|
|
const match = url.match(reData);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error("invalid data");
|
|
|
|
}
|
|
|
|
return new FetchResponse(200, "OK", {
|
|
|
|
"content-type": (match[1] || "text/plain"),
|
2022-09-15 22:58:45 -04:00
|
|
|
}, (match[2] ? (0, base64_js_1.decodeBase64)(match[3]) : unpercent(match[3])));
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return new FetchResponse(599, "BAD REQUEST (invalid data: URI)", {}, null, new FetchRequest(url));
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Returns a [[FetchGatewayFunc]] for fetching content from a standard
|
|
|
|
* IPFS gateway hosted at %%baseUrl%%.
|
|
|
|
*/
|
|
|
|
function getIpfsGatewayFunc(baseUrl) {
|
2022-09-05 16:57:11 -04:00
|
|
|
async function gatewayIpfs(url, signal) {
|
|
|
|
try {
|
|
|
|
const match = url.match(reIpfs);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error("invalid link");
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
return new FetchRequest(`${baseUrl}${match[2]}`);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return new FetchResponse(599, "BAD REQUEST (invalid IPFS URI)", {}, null, new FetchRequest(url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gatewayIpfs;
|
|
|
|
}
|
|
|
|
const Gateways = {
|
2022-11-30 15:44:23 -05:00
|
|
|
"data": dataGatewayFunc,
|
2022-09-05 16:57:11 -04:00
|
|
|
"ipfs": getIpfsGatewayFunc("https:/\/gateway.ipfs.io/ipfs/")
|
|
|
|
};
|
|
|
|
const fetchSignals = new WeakMap();
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* @_ignore
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
class FetchCancelSignal {
|
|
|
|
#listeners;
|
|
|
|
#cancelled;
|
|
|
|
constructor(request) {
|
|
|
|
this.#listeners = [];
|
|
|
|
this.#cancelled = false;
|
|
|
|
fetchSignals.set(request, () => {
|
|
|
|
if (this.#cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.#cancelled = true;
|
|
|
|
for (const listener of this.#listeners) {
|
|
|
|
setTimeout(() => { listener(); }, 0);
|
|
|
|
}
|
|
|
|
this.#listeners = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
addListener(listener) {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(this.#cancelled, "singal already cancelled", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "fetchCancelSignal.addCancelListener"
|
|
|
|
});
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#listeners.push(listener);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
get cancelled() { return this.#cancelled; }
|
2022-09-05 16:57:11 -04:00
|
|
|
checkSignal() {
|
|
|
|
if (!this.cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(false, "cancelled", "CANCELLED", {});
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.FetchCancelSignal = FetchCancelSignal;
|
|
|
|
// Check the signal, throwing if it is cancelled
|
|
|
|
function checkSignal(signal) {
|
|
|
|
if (signal == null) {
|
|
|
|
throw new Error("missing signal; should not happen");
|
|
|
|
}
|
|
|
|
signal.checkSignal();
|
|
|
|
return signal;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Represents a request for a resource using a URI.
|
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* By default, the supported schemes are ``HTTP``, ``HTTPS``, ``data:``,
|
|
|
|
* and ``IPFS:``.
|
|
|
|
*
|
|
|
|
* Additional schemes can be added globally using [[registerGateway]].
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* req = new FetchRequest("https://www.ricmoo.com")
|
|
|
|
* resp = await req.send()
|
|
|
|
* resp.body.length
|
|
|
|
* //_result:
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
class FetchRequest {
|
|
|
|
#allowInsecure;
|
|
|
|
#gzip;
|
|
|
|
#headers;
|
|
|
|
#method;
|
|
|
|
#timeout;
|
|
|
|
#url;
|
|
|
|
#body;
|
|
|
|
#bodyType;
|
|
|
|
#creds;
|
|
|
|
// Hooks
|
|
|
|
#preflight;
|
|
|
|
#process;
|
|
|
|
#retry;
|
|
|
|
#signal;
|
2022-09-27 03:45:27 -04:00
|
|
|
#throttle;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The fetch URI to requrest.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get url() { return this.#url; }
|
|
|
|
set url(url) {
|
|
|
|
this.#url = String(url);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The fetch body, if any, to send as the request body. //(default: null)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*
|
|
|
|
* When setting a body, the intrinsic ``Content-Type`` is automatically
|
|
|
|
* set and will be used if **not overridden** by setting a custom
|
|
|
|
* header.
|
|
|
|
*
|
|
|
|
* If %%body%% is null, the body is cleared (along with the
|
|
|
|
* intrinsic ``Content-Type``) and the .
|
|
|
|
*
|
|
|
|
* If %%body%% is a string, the intrincis ``Content-Type`` is set to
|
|
|
|
* ``text/plain``.
|
|
|
|
*
|
|
|
|
* If %%body%% is a Uint8Array, the intrincis ``Content-Type`` is set to
|
|
|
|
* ``application/octet-stream``.
|
|
|
|
*
|
|
|
|
* If %%body%% is any other object, the intrincis ``Content-Type`` is
|
|
|
|
* set to ``application/json``.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get body() {
|
|
|
|
if (this.#body == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Uint8Array(this.#body);
|
|
|
|
}
|
|
|
|
set body(body) {
|
|
|
|
if (body == null) {
|
|
|
|
this.#body = undefined;
|
|
|
|
this.#bodyType = undefined;
|
|
|
|
}
|
|
|
|
else if (typeof (body) === "string") {
|
|
|
|
this.#body = (0, utf8_js_1.toUtf8Bytes)(body);
|
|
|
|
this.#bodyType = "text/plain";
|
|
|
|
}
|
|
|
|
else if (body instanceof Uint8Array) {
|
|
|
|
this.#body = body;
|
|
|
|
this.#bodyType = "application/octet-stream";
|
|
|
|
}
|
|
|
|
else if (typeof (body) === "object") {
|
|
|
|
this.#body = (0, utf8_js_1.toUtf8Bytes)(JSON.stringify(body));
|
|
|
|
this.#bodyType = "application/json";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("invalid body");
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Returns true if the request has a body.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
hasBody() {
|
|
|
|
return (this.#body != null);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The HTTP method to use when requesting the URI. If no method
|
|
|
|
* has been explicitly set, then ``GET`` is used if the body is
|
|
|
|
* null and ``POST`` otherwise.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get method() {
|
|
|
|
if (this.#method) {
|
|
|
|
return this.#method;
|
|
|
|
}
|
|
|
|
if (this.hasBody()) {
|
|
|
|
return "POST";
|
|
|
|
}
|
|
|
|
return "GET";
|
|
|
|
}
|
|
|
|
set method(method) {
|
|
|
|
if (method == null) {
|
|
|
|
method = "";
|
|
|
|
}
|
|
|
|
this.#method = String(method).toUpperCase();
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The headers that will be used when requesting the URI. All
|
|
|
|
* keys are lower-case.
|
|
|
|
*
|
|
|
|
* This object is a copy, so any chnages will **NOT** be reflected
|
|
|
|
* in the ``FetchRequest``.
|
|
|
|
*
|
|
|
|
* To set a header entry, use the ``setHeader`` method.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get headers() {
|
|
|
|
const headers = Object.assign({}, this.#headers);
|
|
|
|
if (this.#creds) {
|
|
|
|
headers["authorization"] = `Basic ${(0, base64_js_1.encodeBase64)((0, utf8_js_1.toUtf8Bytes)(this.#creds))}`;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
if (this.allowGzip) {
|
|
|
|
headers["accept-encoding"] = "gzip";
|
|
|
|
}
|
|
|
|
if (headers["content-type"] == null && this.#bodyType) {
|
|
|
|
headers["content-type"] = this.#bodyType;
|
|
|
|
}
|
|
|
|
if (this.body) {
|
|
|
|
headers["content-length"] = String(this.body.length);
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
return headers;
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Get the header for %%key%%, ignoring case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
getHeader(key) {
|
|
|
|
return this.headers[key.toLowerCase()];
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Set the header for %%key%% to %%value%%. All values are coerced
|
|
|
|
* to a string.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
setHeader(key, value) {
|
|
|
|
this.#headers[String(key).toLowerCase()] = String(value);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Clear all headers, resetting all intrinsic headers.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
clearHeaders() {
|
|
|
|
this.#headers = {};
|
|
|
|
}
|
|
|
|
[Symbol.iterator]() {
|
|
|
|
const headers = this.headers;
|
|
|
|
const keys = Object.keys(headers);
|
|
|
|
let index = 0;
|
|
|
|
return {
|
|
|
|
next: () => {
|
|
|
|
if (index < keys.length) {
|
|
|
|
const key = keys[index++];
|
|
|
|
return {
|
|
|
|
value: [key, headers[key]], done: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The value that will be sent for the ``Authorization`` header.
|
2022-11-30 15:44:23 -05:00
|
|
|
*
|
|
|
|
* To set the credentials, use the ``setCredentials`` method.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get credentials() {
|
|
|
|
return this.#creds || null;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Sets an ``Authorization`` for %%username%% with %%password%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
setCredentials(username, password) {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assertArgument)(!username.match(/:/), "invalid basic authentication username", "username", "[REDACTED]");
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#creds = `${username}:${password}`;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Enable and request gzip-encoded responses. The response will
|
|
|
|
* automatically be decompressed. //(default: true)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get allowGzip() {
|
|
|
|
return this.#gzip;
|
|
|
|
}
|
|
|
|
set allowGzip(value) {
|
|
|
|
this.#gzip = !!value;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Allow ``Authentication`` credentials to be sent over insecure
|
2022-11-30 15:44:23 -05:00
|
|
|
* channels. //(default: false)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get allowInsecureAuthentication() {
|
|
|
|
return !!this.#allowInsecure;
|
|
|
|
}
|
|
|
|
set allowInsecureAuthentication(value) {
|
|
|
|
this.#allowInsecure = !!value;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The timeout (in milliseconds) to wait for a complere response.
|
2022-11-30 15:44:23 -05:00
|
|
|
* //(default: 5 minutes)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get timeout() { return this.#timeout; }
|
|
|
|
set timeout(timeout) {
|
2022-09-15 22:58:45 -04:00
|
|
|
(0, errors_js_1.assertArgument)(timeout >= 0, "timeout must be non-zero", "timeout", timeout);
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#timeout = timeout;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called prior to each request, for example
|
|
|
|
* during a redirection or retry in case of server throttling.
|
|
|
|
*
|
|
|
|
* This offers an opportunity to populate headers or update
|
|
|
|
* content before sending a request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get preflightFunc() {
|
|
|
|
return this.#preflight || null;
|
|
|
|
}
|
|
|
|
set preflightFunc(preflight) {
|
|
|
|
this.#preflight = preflight;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called after each response, offering an
|
|
|
|
* opportunity to provide client-level throttling or updating
|
|
|
|
* response data.
|
|
|
|
*
|
|
|
|
* Any error thrown in this causes the ``send()`` to throw.
|
|
|
|
*
|
|
|
|
* To schedule a retry attempt (assuming the maximum retry limit
|
|
|
|
* has not been reached), use [[response.throwThrottleError]].
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get processFunc() {
|
|
|
|
return this.#process || null;
|
|
|
|
}
|
|
|
|
set processFunc(process) {
|
|
|
|
this.#process = process;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called on each retry attempt.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get retryFunc() {
|
|
|
|
return this.#retry || null;
|
|
|
|
}
|
|
|
|
set retryFunc(retry) {
|
|
|
|
this.#retry = retry;
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Create a new FetchRequest instance with default values.
|
|
|
|
*
|
|
|
|
* Once created, each property may be set before issuing a
|
|
|
|
* ``.send()`` to make teh request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
constructor(url) {
|
|
|
|
this.#url = String(url);
|
|
|
|
this.#allowInsecure = false;
|
2022-11-30 15:44:23 -05:00
|
|
|
this.#gzip = true;
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#headers = {};
|
|
|
|
this.#method = "";
|
2022-09-27 03:45:27 -04:00
|
|
|
this.#timeout = 300000;
|
|
|
|
this.#throttle = {
|
|
|
|
slotInterval: SLOT_INTERVAL,
|
|
|
|
maxAttempts: MAX_ATTEMPTS
|
|
|
|
};
|
|
|
|
}
|
2023-01-22 16:47:02 -05:00
|
|
|
toString() {
|
|
|
|
return `<FetchRequest method=${JSON.stringify(this.method)} url=${JSON.stringify(this.url)} headers=${JSON.stringify(this.headers)} body=${this.#body ? (0, data_js_1.hexlify)(this.#body) : "null"}>`;
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Update the throttle parameters used to determine maximum
|
|
|
|
* attempts and exponential-backoff properties.
|
|
|
|
*/
|
2022-09-27 03:45:27 -04:00
|
|
|
setThrottleParams(params) {
|
|
|
|
if (params.slotInterval != null) {
|
|
|
|
this.#throttle.slotInterval = params.slotInterval;
|
|
|
|
}
|
|
|
|
if (params.maxAttempts != null) {
|
|
|
|
this.#throttle.maxAttempts = params.maxAttempts;
|
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
async #send(attempt, expires, delay, _request, _response) {
|
2022-09-27 03:45:27 -04:00
|
|
|
if (attempt >= this.#throttle.maxAttempts) {
|
2022-09-05 16:57:11 -04:00
|
|
|
return _response.makeServerError("exceeded maximum retry limit");
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(getTime() <= expires, "timeout", "TIMEOUT", {
|
|
|
|
operation: "request.send", reason: "timeout", request: _request
|
|
|
|
});
|
2022-09-05 16:57:11 -04:00
|
|
|
if (delay > 0) {
|
|
|
|
await wait(delay);
|
|
|
|
}
|
|
|
|
let req = this.clone();
|
|
|
|
const scheme = (req.url.split(":")[0] || "").toLowerCase();
|
|
|
|
// Process any Gateways
|
|
|
|
if (scheme in Gateways) {
|
|
|
|
const result = await Gateways[scheme](req.url, checkSignal(_request.#signal));
|
|
|
|
if (result instanceof FetchResponse) {
|
|
|
|
let response = result;
|
|
|
|
if (this.processFunc) {
|
|
|
|
checkSignal(_request.#signal);
|
|
|
|
try {
|
|
|
|
response = await this.processFunc(req, response);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// Something went wrong during processing; throw a 5xx server error
|
|
|
|
if (error.throttle == null || typeof (error.stall) !== "number") {
|
|
|
|
response.makeServerError("error in post-processing function", error).assertOk();
|
|
|
|
}
|
|
|
|
// Ignore throttling
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
req = result;
|
|
|
|
}
|
|
|
|
// We have a preflight function; update the request
|
|
|
|
if (this.preflightFunc) {
|
|
|
|
req = await this.preflightFunc(req);
|
|
|
|
}
|
|
|
|
const resp = await getUrlFunc(req, checkSignal(_request.#signal));
|
|
|
|
let response = new FetchResponse(resp.statusCode, resp.statusMessage, resp.headers, resp.body, _request);
|
|
|
|
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
|
|
// Redirect
|
|
|
|
try {
|
|
|
|
const location = response.headers.location || "";
|
|
|
|
return req.redirect(location).#send(attempt + 1, expires, 0, _request, response);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
// Things won't get any better on another attempt; abort
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
else if (response.statusCode === 429) {
|
|
|
|
// Throttle
|
|
|
|
if (this.retryFunc == null || (await this.retryFunc(req, response, attempt))) {
|
|
|
|
const retryAfter = response.headers["retry-after"];
|
2022-09-27 03:45:27 -04:00
|
|
|
let delay = this.#throttle.slotInterval * Math.trunc(Math.random() * Math.pow(2, attempt));
|
2022-09-05 16:57:11 -04:00
|
|
|
if (typeof (retryAfter) === "string" && retryAfter.match(/^[1-9][0-9]*$/)) {
|
|
|
|
delay = parseInt(retryAfter);
|
|
|
|
}
|
|
|
|
return req.clone().#send(attempt + 1, expires, delay, _request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.processFunc) {
|
|
|
|
checkSignal(_request.#signal);
|
|
|
|
try {
|
|
|
|
response = await this.processFunc(req, response);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// Something went wrong during processing; throw a 5xx server error
|
|
|
|
if (error.throttle == null || typeof (error.stall) !== "number") {
|
|
|
|
response.makeServerError("error in post-processing function", error).assertOk();
|
|
|
|
}
|
|
|
|
// Throttle
|
2022-09-27 03:45:27 -04:00
|
|
|
let delay = this.#throttle.slotInterval * Math.trunc(Math.random() * Math.pow(2, attempt));
|
2022-09-05 16:57:11 -04:00
|
|
|
;
|
|
|
|
if (error.stall >= 0) {
|
|
|
|
delay = error.stall;
|
|
|
|
}
|
|
|
|
return req.clone().#send(attempt + 1, expires, delay, _request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Resolves to the response by sending the request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
send() {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(this.#signal == null, "request already sent", "UNSUPPORTED_OPERATION", { operation: "fetchRequest.send" });
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#signal = new FetchCancelSignal(this);
|
|
|
|
return this.#send(0, getTime() + this.timeout, 0, this, new FetchResponse(0, "", {}, null, this));
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Cancels the inflight response, causing a ``CANCELLED``
|
|
|
|
* error to be rejected from the [[send]].
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
cancel() {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(this.#signal != null, "request has not been sent", "UNSUPPORTED_OPERATION", { operation: "fetchRequest.cancel" });
|
2022-09-05 16:57:11 -04:00
|
|
|
const signal = fetchSignals.get(this);
|
|
|
|
if (!signal) {
|
|
|
|
throw new Error("missing signal; should not happen");
|
|
|
|
}
|
|
|
|
signal();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a new [[FetchRequest]] that represents the redirection
|
|
|
|
* to %%location%%.
|
|
|
|
*/
|
|
|
|
redirect(location) {
|
|
|
|
// Redirection; for now we only support absolute locataions
|
|
|
|
const current = this.url.split(":")[0].toLowerCase();
|
|
|
|
const target = location.split(":")[0].toLowerCase();
|
|
|
|
// Don't allow redirecting:
|
|
|
|
// - non-GET requests
|
|
|
|
// - downgrading the security (e.g. https => http)
|
|
|
|
// - to non-HTTP (or non-HTTPS) protocols [this could be relaxed?]
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(this.method === "GET" && (current !== "https" || target !== "http") && location.match(/^https?:/), `unsupported redirect`, "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: `redirect(${this.method} ${JSON.stringify(this.url)} => ${JSON.stringify(location)})`
|
|
|
|
});
|
2022-09-05 16:57:11 -04:00
|
|
|
// Create a copy of this request, with a new URL
|
|
|
|
const req = new FetchRequest(location);
|
|
|
|
req.method = "GET";
|
|
|
|
req.allowGzip = this.allowGzip;
|
|
|
|
req.timeout = this.timeout;
|
|
|
|
req.#headers = Object.assign({}, this.#headers);
|
|
|
|
if (this.#body) {
|
|
|
|
req.#body = new Uint8Array(this.#body);
|
|
|
|
}
|
|
|
|
req.#bodyType = this.#bodyType;
|
|
|
|
// Do not forward credentials unless on the same domain; only absolute
|
|
|
|
//req.allowInsecure = false;
|
|
|
|
// paths are currently supported; may want a way to specify to forward?
|
|
|
|
//setStore(req.#props, "creds", getStore(this.#pros, "creds"));
|
|
|
|
return req;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Create a new copy of this request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
clone() {
|
|
|
|
const clone = new FetchRequest(this.url);
|
|
|
|
// Preserve "default method" (i.e. null)
|
|
|
|
clone.#method = this.#method;
|
|
|
|
// Preserve "default body" with type, copying the Uint8Array is present
|
|
|
|
if (this.#body) {
|
|
|
|
clone.#body = this.#body;
|
|
|
|
}
|
|
|
|
clone.#bodyType = this.#bodyType;
|
|
|
|
// Preserve "default headers"
|
|
|
|
clone.#headers = Object.assign({}, this.#headers);
|
|
|
|
// Credentials is readonly, so we copy internally
|
|
|
|
clone.#creds = this.#creds;
|
|
|
|
if (this.allowGzip) {
|
|
|
|
clone.allowGzip = true;
|
|
|
|
}
|
|
|
|
clone.timeout = this.timeout;
|
|
|
|
if (this.allowInsecureAuthentication) {
|
|
|
|
clone.allowInsecureAuthentication = true;
|
|
|
|
}
|
|
|
|
clone.#preflight = this.#preflight;
|
|
|
|
clone.#process = this.#process;
|
|
|
|
clone.#retry = this.#retry;
|
|
|
|
return clone;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Locks all static configuration for gateways and FetchGetUrlFunc
|
|
|
|
* registration.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static lockConfig() {
|
|
|
|
locked = true;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Get the current Gateway function for %%scheme%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static getGateway(scheme) {
|
|
|
|
return Gateways[scheme.toLowerCase()] || null;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Use the %%func%% when fetching URIs using %%scheme%%.
|
|
|
|
*
|
|
|
|
* This method affects all requests globally.
|
|
|
|
*
|
|
|
|
* If [[lockConfig]] has been called, no change is made and this
|
|
|
|
* throws.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static registerGateway(scheme, func) {
|
|
|
|
scheme = scheme.toLowerCase();
|
|
|
|
if (scheme === "http" || scheme === "https") {
|
|
|
|
throw new Error(`cannot intercept ${scheme}; use registerGetUrl`);
|
|
|
|
}
|
|
|
|
if (locked) {
|
|
|
|
throw new Error("gateways locked");
|
|
|
|
}
|
|
|
|
Gateways[scheme] = func;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Use %%getUrl%% when fetching URIs over HTTP and HTTPS requests.
|
|
|
|
*
|
|
|
|
* This method affects all requests globally.
|
|
|
|
*
|
|
|
|
* If [[lockConfig]] has been called, no change is made and this
|
|
|
|
* throws.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static registerGetUrl(getUrl) {
|
|
|
|
if (locked) {
|
|
|
|
throw new Error("gateways locked");
|
|
|
|
}
|
|
|
|
getUrlFunc = getUrl;
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Creates a function that can "fetch" data URIs.
|
|
|
|
*
|
|
|
|
* Note that this is automatically done internally to support
|
|
|
|
* data URIs, so it is not necessary to register it.
|
|
|
|
*
|
|
|
|
* This is not generally something that is needed, but may
|
|
|
|
* be useful in a wrapper to perfom custom data URI functionality.
|
|
|
|
*/
|
|
|
|
static createDataGateway() {
|
|
|
|
return dataGatewayFunc;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Creates a function that will fetch IPFS (unvalidated) from
|
|
|
|
* a custom gateway baseUrl.
|
|
|
|
*
|
|
|
|
* The default IPFS gateway used internally is
|
|
|
|
* ``"https:/\/gateway.ipfs.io/ipfs/"``.
|
|
|
|
*/
|
|
|
|
static createIpfsGatewayFunc(baseUrl) {
|
|
|
|
return getIpfsGatewayFunc(baseUrl);
|
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
exports.FetchRequest = FetchRequest;
|
|
|
|
;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response for a FetchREquest.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
class FetchResponse {
|
|
|
|
#statusCode;
|
|
|
|
#statusMessage;
|
|
|
|
#headers;
|
|
|
|
#body;
|
|
|
|
#request;
|
|
|
|
#error;
|
|
|
|
toString() {
|
2023-01-22 16:47:02 -05:00
|
|
|
return `<FetchResponse status=${this.statusCode} body=${this.#body ? (0, data_js_1.hexlify)(this.#body) : "null"}>`;
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response status code.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get statusCode() { return this.#statusCode; }
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response status message.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get statusMessage() { return this.#statusMessage; }
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response headers. All keys are lower-case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-11-30 15:44:23 -05:00
|
|
|
get headers() { return Object.assign({}, this.#headers); }
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response body, or ``null`` if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get body() {
|
|
|
|
return (this.#body == null) ? null : new Uint8Array(this.#body);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response body as a UTF-8 encoded string, or the empty
|
|
|
|
* string (i.e. ``""``) if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* An error is thrown if the body is invalid UTF-8 data.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get bodyText() {
|
|
|
|
try {
|
|
|
|
return (this.#body == null) ? "" : (0, utf8_js_1.toUtf8String)(this.#body);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(false, "response body is not valid UTF-8 data", "UNSUPPORTED_OPERATION", {
|
2022-09-05 16:57:11 -04:00
|
|
|
operation: "bodyText", info: { response: this }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response body, decoded as JSON.
|
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* An error is thrown if the body is invalid JSON-encoded data
|
|
|
|
* or if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get bodyJson() {
|
|
|
|
try {
|
|
|
|
return JSON.parse(this.bodyText);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(false, "response body is not valid JSON", "UNSUPPORTED_OPERATION", {
|
2022-09-05 16:57:11 -04:00
|
|
|
operation: "bodyJson", info: { response: this }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[Symbol.iterator]() {
|
|
|
|
const headers = this.headers;
|
|
|
|
const keys = Object.keys(headers);
|
|
|
|
let index = 0;
|
|
|
|
return {
|
|
|
|
next: () => {
|
|
|
|
if (index < keys.length) {
|
|
|
|
const key = keys[index++];
|
|
|
|
return {
|
|
|
|
value: [key, headers[key]], done: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
constructor(statusCode, statusMessage, headers, body, request) {
|
|
|
|
this.#statusCode = statusCode;
|
|
|
|
this.#statusMessage = statusMessage;
|
2022-11-30 15:44:23 -05:00
|
|
|
this.#headers = Object.keys(headers).reduce((accum, k) => {
|
2022-09-05 16:57:11 -04:00
|
|
|
accum[k.toLowerCase()] = String(headers[k]);
|
|
|
|
return accum;
|
2022-11-30 15:44:23 -05:00
|
|
|
}, {});
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#body = ((body == null) ? null : new Uint8Array(body));
|
|
|
|
this.#request = (request || null);
|
|
|
|
this.#error = { message: "" };
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Return a Response with matching headers and body, but with
|
|
|
|
* an error status code (i.e. 599) and %%message%% with an
|
|
|
|
* optional %%error%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
makeServerError(message, error) {
|
|
|
|
let statusMessage;
|
|
|
|
if (!message) {
|
|
|
|
message = `${this.statusCode} ${this.statusMessage}`;
|
|
|
|
statusMessage = `CLIENT ESCALATED SERVER ERROR (${message})`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
statusMessage = `CLIENT ESCALATED SERVER ERROR (${this.statusCode} ${this.statusMessage}; ${message})`;
|
|
|
|
}
|
|
|
|
const response = new FetchResponse(599, statusMessage, this.headers, this.body, this.#request || undefined);
|
|
|
|
response.#error = { message, error };
|
|
|
|
return response;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* If called within a [request.processFunc](FetchRequest-processFunc)
|
|
|
|
* call, causes the request to retry as if throttled for %%stall%%
|
|
|
|
* milliseconds.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
throwThrottleError(message, stall) {
|
|
|
|
if (stall == null) {
|
|
|
|
stall = -1;
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
else {
|
|
|
|
(0, errors_js_1.assertArgument)(Number.isInteger(stall) && stall >= 0, "invalid stall timeout", "stall", stall);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
const error = new Error(message || "throttling requests");
|
|
|
|
(0, properties_js_1.defineProperties)(error, { stall, throttle: true });
|
|
|
|
throw error;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Get the header value for %%key%%, ignoring case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
getHeader(key) {
|
|
|
|
return this.headers[key.toLowerCase()];
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Returns true of the response has a body.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
hasBody() {
|
|
|
|
return (this.#body != null);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The request made for this response.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get request() { return this.#request; }
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Returns true if this response was a success statusCode.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
ok() {
|
|
|
|
return (this.#error.message === "" && this.statusCode >= 200 && this.statusCode < 300);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Throws a ``SERVER_ERROR`` if this response is not ok.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
assertOk() {
|
|
|
|
if (this.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let { message, error } = this.#error;
|
|
|
|
if (message === "") {
|
|
|
|
message = `server response ${this.statusCode} ${this.statusMessage}`;
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, errors_js_1.assert)(false, message, "SERVER_ERROR", {
|
2022-09-05 16:57:11 -04:00
|
|
|
request: (this.request || "unknown request"), response: this, error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.FetchResponse = FetchResponse;
|
|
|
|
function getTime() { return (new Date()).getTime(); }
|
|
|
|
function unpercent(value) {
|
|
|
|
return (0, utf8_js_1.toUtf8Bytes)(value.replace(/%([0-9a-f][0-9a-f])/gi, (all, code) => {
|
|
|
|
return String.fromCharCode(parseInt(code, 16));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
function wait(delay) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=fetch.js.map
|