2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { encode as base64Encode } from "@ethersproject/base64";
|
|
|
|
import { shallowCopy } from "@ethersproject/properties";
|
|
|
|
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
import { getUrl, GetUrlResponse } from "./geturl";
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
// Exported Types
|
|
|
|
export type ConnectionInfo = {
|
|
|
|
url: string,
|
|
|
|
user?: string,
|
|
|
|
password?: string,
|
2019-06-12 05:40:45 +03:00
|
|
|
allowInsecureAuthentication?: boolean,
|
2019-11-23 15:21:27 +03:00
|
|
|
throttleLimit?: number,
|
2019-05-15 01:25:46 +03:00
|
|
|
timeout?: number,
|
|
|
|
headers?: { [key: string]: string | number }
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface OnceBlockable {
|
|
|
|
once(eventName: "block", handler: () => void): void;
|
|
|
|
}
|
|
|
|
|
2020-05-05 05:43:44 +03:00
|
|
|
export interface OncePollable {
|
|
|
|
once(eventName: "poll", handler: () => void): void;
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
export type PollOptions = {
|
|
|
|
timeout?: number,
|
|
|
|
floor?: number,
|
|
|
|
ceiling?: number,
|
|
|
|
interval?: number,
|
|
|
|
retryLimit?: number,
|
|
|
|
onceBlock?: OnceBlockable
|
2020-05-05 05:43:44 +03:00
|
|
|
oncePoll?: OncePollable
|
2019-05-15 01:25:46 +03:00
|
|
|
};
|
|
|
|
|
2019-09-28 04:55:40 +03:00
|
|
|
export type FetchJsonResponse = {
|
|
|
|
statusCode: number;
|
|
|
|
headers: { [ header: string ]: string };
|
|
|
|
};
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
type Header = { key: string, value: string };
|
|
|
|
|
2019-09-28 04:55:40 +03:00
|
|
|
export function fetchJson(connection: string | ConnectionInfo, json?: string, processFunc?: (value: any, response: FetchJsonResponse) => any): Promise<any> {
|
2019-11-01 17:33:51 +03:00
|
|
|
const headers: { [key: string]: Header } = { };
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
let url: string = null;
|
|
|
|
|
2019-06-12 05:40:45 +03:00
|
|
|
// @TODO: Allow ConnectionInfo to override some of these values
|
2019-11-01 17:33:51 +03:00
|
|
|
const options: any = {
|
2019-06-12 05:40:45 +03:00
|
|
|
method: "GET",
|
|
|
|
};
|
|
|
|
|
2019-09-28 04:55:40 +03:00
|
|
|
let allow304 = false;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
let timeout = 2 * 60 * 1000;
|
|
|
|
|
|
|
|
if (typeof(connection) === "string") {
|
|
|
|
url = connection;
|
|
|
|
|
|
|
|
} else if (typeof(connection) === "object") {
|
2019-06-12 05:40:45 +03:00
|
|
|
if (connection == null || connection.url == null) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("missing URL", "connection.url", connection);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
url = connection.url;
|
|
|
|
|
|
|
|
if (typeof(connection.timeout) === "number" && connection.timeout > 0) {
|
|
|
|
timeout = connection.timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection.headers) {
|
2019-11-01 17:33:51 +03:00
|
|
|
for (const key in connection.headers) {
|
2019-05-15 01:25:46 +03:00
|
|
|
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
|
2019-09-28 04:55:40 +03:00
|
|
|
if (["if-none-match", "if-modified-since"].indexOf(key.toLowerCase()) >= 0) {
|
|
|
|
allow304 = true;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection.user != null && connection.password != null) {
|
2019-06-12 05:40:45 +03:00
|
|
|
if (url.substring(0, 6) !== "https:" && connection.allowInsecureAuthentication !== true) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwError(
|
2019-05-15 01:25:46 +03:00
|
|
|
"basic authentication requires a secure https url",
|
2019-08-02 01:04:06 +03:00
|
|
|
Logger.errors.INVALID_ARGUMENT,
|
|
|
|
{ argument: "url", url: url, user: connection.user, password: "[REDACTED]" }
|
2019-05-15 01:25:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const authorization = connection.user + ":" + connection.password;
|
2019-05-15 01:25:46 +03:00
|
|
|
headers["authorization"] = {
|
|
|
|
key: "Authorization",
|
|
|
|
value: "Basic " + base64Encode(toUtf8Bytes(authorization))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
if (json) {
|
|
|
|
options.method = "POST";
|
|
|
|
options.body = json;
|
|
|
|
headers["content-type"] = { key: "Content-Type", value: "application/json" };
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
const flatHeaders: { [ key: string ]: string } = { };
|
|
|
|
Object.keys(headers).forEach((key) => {
|
|
|
|
const header = headers[key];
|
|
|
|
flatHeaders[header.key] = header.value;
|
|
|
|
});
|
|
|
|
options.headers = flatHeaders;
|
2019-06-12 05:40:45 +03:00
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
const runningTimeout = (function() {
|
2020-02-04 08:50:27 +03:00
|
|
|
let timer: NodeJS.Timer = null;
|
2019-11-23 15:21:27 +03:00
|
|
|
const promise = new Promise(function(resolve, reject) {
|
|
|
|
if (timeout) {
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (timer == null) { return; }
|
|
|
|
timer = null;
|
|
|
|
|
2020-06-12 11:41:07 +03:00
|
|
|
reject(logger.makeError("timeout", Logger.errors.TIMEOUT, {
|
|
|
|
requestBody: (options.body || null),
|
|
|
|
requestMethod: options.method,
|
|
|
|
timeout: timeout,
|
|
|
|
url: url
|
|
|
|
}));
|
2019-11-23 15:21:27 +03:00
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
const cancel = function() {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (timer == null) { return; }
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
return { promise, cancel };
|
|
|
|
})();
|
|
|
|
|
|
|
|
const runningFetch = (async function() {
|
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
let response: GetUrlResponse = null;
|
|
|
|
try {
|
|
|
|
response = await getUrl(url, options);
|
|
|
|
} catch (error) {
|
|
|
|
response = (<any>error).response;
|
2020-05-03 23:24:18 +03:00
|
|
|
if (response == null) {
|
2020-05-12 23:55:14 +03:00
|
|
|
runningTimeout.cancel();
|
2020-05-03 23:24:18 +03:00
|
|
|
logger.throwError("missing response", Logger.errors.SERVER_ERROR, {
|
2020-06-12 11:41:07 +03:00
|
|
|
requestBody: (options.body || null),
|
|
|
|
requestMethod: options.method,
|
2020-05-03 23:24:18 +03:00
|
|
|
serverError: error,
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
}
|
2020-04-18 09:46:52 +03:00
|
|
|
}
|
2019-11-23 15:21:27 +03:00
|
|
|
|
2020-05-03 23:24:18 +03:00
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
let body = response.body;
|
2019-11-23 15:21:27 +03:00
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
if (allow304 && response.statusCode === 304) {
|
|
|
|
body = null;
|
2019-11-23 15:21:27 +03:00
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
} else if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
|
|
runningTimeout.cancel();
|
|
|
|
logger.throwError("bad response", Logger.errors.SERVER_ERROR, {
|
|
|
|
status: response.statusCode,
|
|
|
|
headers: response.headers,
|
|
|
|
body: body,
|
2020-06-12 11:41:07 +03:00
|
|
|
requestBody: (options.body || null),
|
|
|
|
requestMethod: options.method,
|
2020-04-18 09:46:52 +03:00
|
|
|
url: url
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
runningTimeout.cancel();
|
|
|
|
|
|
|
|
let json: any = null;
|
|
|
|
if (body != null) {
|
|
|
|
try {
|
|
|
|
json = JSON.parse(body);
|
|
|
|
} catch (error) {
|
|
|
|
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, {
|
|
|
|
body: body,
|
|
|
|
error: error,
|
2020-06-12 11:41:07 +03:00
|
|
|
requestBody: (options.body || null),
|
|
|
|
requestMethod: options.method,
|
2019-11-23 15:21:27 +03:00
|
|
|
url: url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
if (processFunc) {
|
|
|
|
try {
|
2020-04-18 09:46:52 +03:00
|
|
|
json = await processFunc(json, response);
|
2019-11-23 15:21:27 +03:00
|
|
|
} catch (error) {
|
|
|
|
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
|
|
|
|
body: json,
|
2020-06-12 11:41:07 +03:00
|
|
|
error: error,
|
|
|
|
requestBody: (options.body || null),
|
|
|
|
requestMethod: options.method,
|
|
|
|
url: url
|
2019-11-23 15:21:27 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-23 15:21:27 +03:00
|
|
|
return json;
|
|
|
|
})();
|
|
|
|
|
|
|
|
return Promise.race([ runningTimeout.promise, runningFetch ]);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-05-21 06:42:08 +03:00
|
|
|
export function poll<T>(func: () => Promise<T>, options?: PollOptions): Promise<T> {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (!options) { options = {}; }
|
|
|
|
options = shallowCopy(options);
|
|
|
|
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) {
|
|
|
|
|
2020-02-04 08:50:27 +03:00
|
|
|
let timer: NodeJS.Timer = null;
|
2019-05-15 01:25:46 +03:00
|
|
|
let done: boolean = false;
|
|
|
|
|
|
|
|
// Returns true if cancel was successful. Unsuccessful cancel means we're already done.
|
2019-11-01 17:33:51 +03:00
|
|
|
const cancel = (): boolean => {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (done) { return false; }
|
|
|
|
done = true;
|
|
|
|
if (timer) { clearTimeout(timer); }
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.timeout) {
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (cancel()) { reject(new Error("timeout")); }
|
|
|
|
}, options.timeout)
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const retryLimit = options.retryLimit;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
let attempt = 0;
|
|
|
|
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); }
|
|
|
|
|
2020-05-05 05:43:44 +03:00
|
|
|
} else if (options.oncePoll) {
|
|
|
|
options.oncePoll.once("poll", check);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
} 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)));
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|