2019-05-15 01:48:48 +03:00
|
|
|
"use strict";
|
2019-08-25 09:39:20 +03:00
|
|
|
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);
|
|
|
|
export function fetchJson(connection, json, processFunc) {
|
|
|
|
let headers = {};
|
|
|
|
let url = null;
|
2019-06-12 08:01:04 +03:00
|
|
|
// @TODO: Allow ConnectionInfo to override some of these values
|
2019-08-25 09:39:20 +03:00
|
|
|
let options = {
|
2019-06-12 08:01:04 +03:00
|
|
|
method: "GET",
|
|
|
|
mode: "cors",
|
|
|
|
cache: "no-cache",
|
|
|
|
credentials: "same-origin",
|
|
|
|
redirect: "follow",
|
|
|
|
referrer: "client",
|
|
|
|
};
|
2019-08-25 09:39:20 +03:00
|
|
|
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-08-25 09:39:20 +03:00
|
|
|
for (let key in connection.headers) {
|
2019-05-15 01:48:48 +03:00
|
|
|
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (connection.user != null && connection.password != null) {
|
2019-06-12 08:01:04 +03:00
|
|
|
if (url.substring(0, 6) !== "https:" && connection.allowInsecureAuthentication !== true) {
|
2019-08-25 09:39:20 +03:00
|
|
|
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-08-25 09:39:20 +03:00
|
|
|
let authorization = connection.user + ":" + connection.password;
|
2019-05-15 01:48:48 +03:00
|
|
|
headers["authorization"] = {
|
|
|
|
key: "Authorization",
|
2019-08-25 09:39:20 +03:00
|
|
|
value: "Basic " + base64Encode(toUtf8Bytes(authorization))
|
2019-05-15 01:48:48 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
2019-08-25 09:39:20 +03:00
|
|
|
let timer = null;
|
2019-06-12 08:01:04 +03:00
|
|
|
if (timeout) {
|
2019-08-25 09:39:20 +03:00
|
|
|
timer = setTimeout(() => {
|
2019-06-12 08:01:04 +03:00
|
|
|
if (timer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer = null;
|
2019-08-25 09:39:20 +03:00
|
|
|
reject(logger.makeError("timeout", Logger.errors.TIMEOUT, { timeout: timeout }));
|
2019-06-12 08:01:04 +03:00
|
|
|
}, timeout);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let cancelTimeout = () => {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (timer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
};
|
|
|
|
if (json) {
|
2019-06-12 08:01:04 +03:00
|
|
|
options.method = "POST";
|
|
|
|
options.body = json;
|
2019-05-15 01:48:48 +03:00
|
|
|
headers["content-type"] = { key: "Content-Type", value: "application/json" };
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let flatHeaders = {};
|
|
|
|
Object.keys(headers).forEach((key) => {
|
|
|
|
let header = headers[key];
|
2019-06-12 08:01:04 +03:00
|
|
|
flatHeaders[header.key] = header.value;
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
2019-06-12 08:01:04 +03:00
|
|
|
options.headers = flatHeaders;
|
2019-08-25 09:39:20 +03:00
|
|
|
return fetch(url, options).then((response) => {
|
|
|
|
return response.text().then((body) => {
|
2019-06-12 08:01:04 +03:00
|
|
|
if (!response.ok) {
|
2019-08-25 09:39:20 +03:00
|
|
|
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-06-12 08:01:04 +03:00
|
|
|
return body;
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}).then((text) => {
|
|
|
|
let json = null;
|
2019-05-15 01:48:48 +03:00
|
|
|
try {
|
2019-06-12 08:01:04 +03:00
|
|
|
json = JSON.parse(text);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
catch (error) {
|
2019-08-25 09:39:20 +03:00
|
|
|
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, {
|
2019-06-12 08:01:04 +03:00
|
|
|
body: text,
|
|
|
|
error: error,
|
|
|
|
url: url
|
|
|
|
});
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
if (processFunc) {
|
|
|
|
try {
|
2019-06-12 08:01:04 +03:00
|
|
|
json = processFunc(json);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
catch (error) {
|
2019-08-25 09:39:20 +03:00
|
|
|
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
|
2019-06-12 08:01:04 +03:00
|
|
|
body: json,
|
|
|
|
error: error
|
|
|
|
});
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 08:01:04 +03:00
|
|
|
return json;
|
2019-08-25 09:39:20 +03:00
|
|
|
}, (error) => {
|
2019-06-12 08:01:04 +03:00
|
|
|
throw error;
|
2019-08-25 09:39:20 +03:00
|
|
|
}).then((result) => {
|
2019-05-15 01:48:48 +03:00
|
|
|
cancelTimeout();
|
|
|
|
resolve(result);
|
2019-08-25 09:39:20 +03:00
|
|
|
}, (error) => {
|
2019-05-15 01:48:48 +03:00
|
|
|
cancelTimeout();
|
|
|
|
reject(error);
|
2019-06-12 08:01:04 +03:00
|
|
|
});
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export function poll(func, options) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
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) {
|
2019-08-25 09:39:20 +03:00
|
|
|
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-08-25 09:39:20 +03:00
|
|
|
let cancel = () => {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (done) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (options.timeout) {
|
2019-08-25 09:39:20 +03:00
|
|
|
timer = setTimeout(() => {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (cancel()) {
|
|
|
|
reject(new Error("timeout"));
|
|
|
|
}
|
|
|
|
}, options.timeout);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let 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;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|