ethers.js/packages/web/lib.esm/index.js

248 lines
8.8 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fetch from "cross-fetch";
import { encode as base64Encode } from "@ethersproject/base64";
import { shallowCopy } from "@ethersproject/properties";
import { toUtf8Bytes } from "@ethersproject/strings";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
function getResponse(response) {
const headers = {};
if (response.headers.forEach) {
response.headers.forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
}
else {
((response.headers).keys)().forEach((key) => {
headers[key.toLowerCase()] = response.headers.get(key);
});
}
return {
statusCode: response.status,
status: response.statusText,
headers: headers
};
}
export function fetchJson(connection, json, processFunc) {
2019-11-20 12:57:38 +03:00
const headers = {};
let url = null;
2019-06-12 08:01:04 +03:00
// @TODO: Allow ConnectionInfo to override some of these values
2019-11-20 12:57:38 +03:00
const options = {
2019-06-12 08:01:04 +03:00
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrer: "client",
};
2019-09-28 09:36:19 +03:00
let allow304 = false;
let timeout = 2 * 60 * 1000;
2019-05-15 01:48:48 +03:00
if (typeof (connection) === "string") {
url = connection;
}
else if (typeof (connection) === "object") {
2019-06-12 08:01:04 +03:00
if (connection == null || connection.url == null) {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("missing URL", "connection.url", connection);
2019-05-15 01:48:48 +03:00
}
url = connection.url;
if (typeof (connection.timeout) === "number" && connection.timeout > 0) {
timeout = connection.timeout;
}
if (connection.headers) {
2019-11-20 12:57:38 +03:00
for (const key in connection.headers) {
2019-05-15 01:48:48 +03:00
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
2019-09-28 09:36:19 +03:00
if (["if-none-match", "if-modified-since"].indexOf(key.toLowerCase()) >= 0) {
allow304 = true;
}
2019-05-15 01:48:48 +03:00
}
}
if (connection.user != null && connection.password != null) {
2019-06-12 08:01:04 +03:00
if (url.substring(0, 6) !== "https:" && connection.allowInsecureAuthentication !== true) {
logger.throwError("basic authentication requires a secure https url", Logger.errors.INVALID_ARGUMENT, { argument: "url", url: url, user: connection.user, password: "[REDACTED]" });
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const authorization = connection.user + ":" + connection.password;
2019-05-15 01:48:48 +03:00
headers["authorization"] = {
key: "Authorization",
value: "Basic " + base64Encode(toUtf8Bytes(authorization))
2019-05-15 01:48:48 +03:00
};
}
}
if (json) {
options.method = "POST";
options.body = json;
headers["content-type"] = { key: "Content-Type", value: "application/json" };
}
const flatHeaders = {};
Object.keys(headers).forEach((key) => {
const header = headers[key];
flatHeaders[header.key] = header.value;
});
options.headers = flatHeaders;
const runningTimeout = (function () {
let timer = null;
const promise = new Promise(function (resolve, reject) {
if (timeout) {
timer = setTimeout(() => {
if (timer == null) {
return;
}
timer = null;
reject(logger.makeError("timeout", Logger.errors.TIMEOUT, { timeout: timeout }));
}, timeout);
}
});
const cancel = function () {
2019-05-15 01:48:48 +03:00
if (timer == null) {
return;
}
clearTimeout(timer);
timer = null;
};
return { promise, cancel };
})();
const runningFetch = (function () {
return __awaiter(this, void 0, void 0, function* () {
let response = null;
let body = null;
while (true) {
try {
response = yield fetch(url, options);
}
catch (error) {
console.log(error);
}
body = yield response.text();
2019-09-28 09:36:19 +03:00
if (allow304 && response.status === 304) {
2020-01-19 05:48:12 +03:00
body = null;
break;
2019-09-28 09:36:19 +03:00
}
else if (!response.ok) {
runningTimeout.cancel();
logger.throwError("bad response", Logger.errors.SERVER_ERROR, {
2019-06-12 08:01:04 +03:00
status: response.status,
body: body,
type: response.type,
url: response.url
});
2019-05-15 01:48:48 +03:00
}
2019-09-28 09:36:19 +03:00
else {
break;
2019-05-15 01:48:48 +03:00
}
}
runningTimeout.cancel();
let json = null;
if (body != null) {
try {
json = JSON.parse(body);
2019-05-15 01:48:48 +03:00
}
catch (error) {
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, {
body: body,
error: error,
url: url
});
}
}
if (processFunc) {
try {
json = yield processFunc(json, getResponse(response));
}
catch (error) {
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
body: json,
error: error
});
}
}
return json;
2019-06-12 08:01:04 +03:00
});
})();
return Promise.race([runningTimeout.promise, runningFetch]);
2019-05-15 01:48:48 +03:00
}
export function poll(func, options) {
2019-05-15 01:48:48 +03:00
if (!options) {
options = {};
}
options = shallowCopy(options);
2019-05-15 01:48:48 +03:00
if (options.floor == null) {
options.floor = 0;
}
if (options.ceiling == null) {
options.ceiling = 10000;
}
if (options.interval == null) {
options.interval = 250;
}
return new Promise(function (resolve, reject) {
let timer = null;
let done = false;
2019-05-15 01:48:48 +03:00
// Returns true if cancel was successful. Unsuccessful cancel means we're already done.
2019-11-20 12:57:38 +03:00
const cancel = () => {
2019-05-15 01:48:48 +03:00
if (done) {
return false;
}
done = true;
if (timer) {
clearTimeout(timer);
}
return true;
};
if (options.timeout) {
timer = setTimeout(() => {
2019-05-15 01:48:48 +03:00
if (cancel()) {
reject(new Error("timeout"));
}
}, options.timeout);
}
2019-11-20 12:57:38 +03:00
const retryLimit = options.retryLimit;
let attempt = 0;
2019-05-15 01:48:48 +03:00
function check() {
return func().then(function (result) {
// If we have a result, or are allowed null then we're done
if (result !== undefined) {
if (cancel()) {
resolve(result);
}
}
else if (options.onceBlock) {
options.onceBlock.once("block", check);
// Otherwise, exponential back-off (up to 10s) our next request
}
else if (!done) {
attempt++;
if (attempt > retryLimit) {
if (cancel()) {
reject(new Error("retry limit reached"));
}
return;
}
let timeout = options.interval * parseInt(String(Math.random() * Math.pow(2, attempt)));
2019-05-15 01:48:48 +03:00
if (timeout < options.floor) {
timeout = options.floor;
}
if (timeout > options.ceiling) {
timeout = options.ceiling;
}
setTimeout(check, timeout);
}
return null;
}, function (error) {
if (cancel()) {
reject(error);
}
});
}
check();
});
}