ethers.js/utils/web.js

214 lines
7.2 KiB
JavaScript
Raw Permalink Normal View History

2018-07-14 17:21:32 -04:00
'use strict';
2018-06-13 15:39:39 -04:00
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var xmlhttprequest_1 = require("xmlhttprequest");
var base64_1 = require("./base64");
2018-10-14 19:05:38 -04:00
var properties_1 = require("./properties");
2018-07-14 17:21:32 -04:00
var utf8_1 = require("./utf8");
2018-09-24 16:07:14 -04:00
var errors = __importStar(require("../errors"));
function fetchJson(connection, json, processFunc) {
2018-09-06 16:41:02 -04:00
var headers = {};
var url = null;
2018-09-06 16:41:02 -04:00
var timeout = 2 * 60 * 1000;
if (typeof (connection) === 'string') {
url = connection;
}
else if (typeof (connection) === 'object') {
if (connection.url == null) {
2018-06-13 15:39:39 -04:00
errors.throwError('missing URL', errors.MISSING_ARGUMENT, { arg: 'url' });
}
url = connection.url;
2018-09-06 16:41:02 -04:00
if (typeof (connection.timeout) === 'number' && connection.timeout > 0) {
timeout = connection.timeout;
}
if (connection.headers) {
for (var key in connection.headers) {
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
}
}
if (connection.user != null && connection.password != null) {
if (url.substring(0, 6) !== 'https:' && connection.allowInsecure !== true) {
errors.throwError('basic authentication requires a secure https url', errors.INVALID_ARGUMENT, { arg: 'url', url: url, user: connection.user, password: '[REDACTED]' });
2018-06-13 15:39:39 -04:00
}
var authorization = connection.user + ':' + connection.password;
2018-09-06 16:41:02 -04:00
headers['authorization'] = {
2018-06-13 15:39:39 -04:00
key: 'Authorization',
value: 'Basic ' + base64_1.encode(utf8_1.toUtf8Bytes(authorization))
2018-09-06 16:41:02 -04:00
};
2018-06-13 15:39:39 -04:00
}
}
return new Promise(function (resolve, reject) {
var request = new xmlhttprequest_1.XMLHttpRequest();
2018-09-06 16:41:02 -04:00
var timer = null;
timer = setTimeout(function () {
if (timer == null) {
return;
}
timer = null;
reject(new Error('timeout'));
setTimeout(function () {
request.abort();
}, 0);
}, timeout);
var cancelTimeout = function () {
if (timer == null) {
return;
}
clearTimeout(timer);
timer = null;
};
2018-06-13 15:39:39 -04:00
if (json) {
request.open('POST', url, true);
2018-09-06 16:41:02 -04:00
headers['content-type'] = { key: 'Content-Type', value: 'application/json' };
2018-06-13 15:39:39 -04:00
}
else {
request.open('GET', url, true);
}
2018-09-06 16:41:02 -04:00
Object.keys(headers).forEach(function (key) {
var header = headers[key];
2018-06-13 15:39:39 -04:00
request.setRequestHeader(header.key, header.value);
});
request.onreadystatechange = function () {
if (request.readyState !== 4) {
return;
}
2018-09-24 16:07:14 -04:00
if (request.status != 200) {
cancelTimeout();
// @TODO: not any!
var error = new Error('invalid response - ' + request.status);
error.statusCode = request.status;
2018-10-04 20:25:07 -04:00
if (request.responseText) {
error.responseText = request.responseText;
}
2018-09-24 16:07:14 -04:00
reject(error);
return;
}
2018-09-06 16:41:02 -04:00
var result = null;
2018-06-13 15:39:39 -04:00
try {
2018-09-06 16:41:02 -04:00
result = JSON.parse(request.responseText);
2018-06-13 15:39:39 -04:00
}
catch (error) {
2018-09-06 16:41:02 -04:00
cancelTimeout();
2018-06-13 15:39:39 -04:00
// @TODO: not any!
var jsonError = new Error('invalid json response');
jsonError.orginialError = error;
jsonError.responseText = request.responseText;
jsonError.url = url;
reject(jsonError);
return;
}
if (processFunc) {
try {
result = processFunc(result);
}
catch (error) {
2018-09-06 16:41:02 -04:00
cancelTimeout();
2018-06-13 15:39:39 -04:00
error.url = url;
error.body = json;
error.responseText = request.responseText;
reject(error);
return;
}
}
2018-09-06 16:41:02 -04:00
cancelTimeout();
2018-06-13 15:39:39 -04:00
resolve(result);
};
request.onerror = function (error) {
2018-09-06 16:41:02 -04:00
cancelTimeout();
2018-06-13 15:39:39 -04:00
reject(error);
};
try {
if (json) {
request.send(json);
}
else {
request.send();
}
}
catch (error) {
2018-09-06 16:41:02 -04:00
cancelTimeout();
2018-06-13 15:39:39 -04:00
// @TODO: not any!
var connectionError = new Error('connection error');
connectionError.error = error;
reject(connectionError);
}
});
}
exports.fetchJson = fetchJson;
function poll(func, options) {
if (!options) {
options = {};
}
2018-10-14 19:05:38 -04:00
options = properties_1.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) {
var timer = null;
var done = false;
// Returns true if cancel was successful. Unsuccessful cancel means we're already done.
var cancel = function () {
if (done) {
return false;
}
done = true;
if (timer) {
clearTimeout(timer);
}
return true;
};
if (options.timeout) {
timer = setTimeout(function () {
if (cancel()) {
reject(new Error('timeout'));
}
}, options.timeout);
}
var attempt = 0;
function check() {
2018-07-03 16:44:05 -04:00
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++;
var 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);
}
2018-07-03 16:44:05 -04:00
return null;
}, function (error) {
if (cancel()) {
reject(error);
}
});
}
check();
});
}
exports.poll = poll;