2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-11-23 21:38:13 +09:00
|
|
|
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());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
|
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
|
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
|
|
function step(op) {
|
|
|
|
if (f) throw new TypeError("Generator is already executing.");
|
|
|
|
while (_) try {
|
|
|
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
|
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
|
|
switch (op[0]) {
|
|
|
|
case 0: case 1: t = op; break;
|
|
|
|
case 4: _.label++; return { value: op[1], done: false };
|
|
|
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
|
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
|
|
default:
|
|
|
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
|
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
|
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
|
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
|
|
if (t[2]) _.ops.pop();
|
|
|
|
_.trys.pop(); continue;
|
|
|
|
}
|
|
|
|
op = body.call(thisArg, _);
|
|
|
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
|
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
|
|
}
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.poll = exports.fetchJson = exports._fetchData = void 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
var base64_1 = require("@ethersproject/base64");
|
2020-09-05 23:35:35 -04:00
|
|
|
var bytes_1 = require("@ethersproject/bytes");
|
2019-05-14 18:48:48 -04:00
|
|
|
var properties_1 = require("@ethersproject/properties");
|
|
|
|
var strings_1 = require("@ethersproject/strings");
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2020-04-18 05:14:55 -04:00
|
|
|
var geturl_1 = require("./geturl");
|
2020-07-14 02:33:30 -04:00
|
|
|
function staller(duration) {
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
setTimeout(resolve, duration);
|
|
|
|
});
|
|
|
|
}
|
2020-09-05 23:35:35 -04:00
|
|
|
function bodyify(value, type) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return value;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
if ((0, bytes_1.isBytesLike)(value)) {
|
2020-09-16 03:08:36 -04:00
|
|
|
if (type && (type.split("/")[0] === "text" || type.split(";")[0].trim() === "application/json")) {
|
2020-09-05 23:35:35 -04:00
|
|
|
try {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, strings_1.toUtf8String)(value);
|
2020-09-05 23:35:35 -04:00
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, bytes_1.hexlify)(value);
|
2020-09-05 23:35:35 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2022-09-13 21:28:52 -04:00
|
|
|
function unpercent(value) {
|
|
|
|
return (0, strings_1.toUtf8Bytes)(value.replace(/%([0-9a-f][0-9a-f])/gi, function (all, code) {
|
|
|
|
return String.fromCharCode(parseInt(code, 16));
|
|
|
|
}));
|
|
|
|
}
|
2020-07-31 01:32:26 -04:00
|
|
|
// This API is still a work in progress; the future changes will likely be:
|
|
|
|
// - ConnectionInfo => FetchDataRequest<T = any>
|
|
|
|
// - FetchDataRequest.body? = string | Uint8Array | { contentType: string, data: string | Uint8Array }
|
|
|
|
// - If string => text/plain, Uint8Array => application/octet-stream (if content-type unspecified)
|
|
|
|
// - FetchDataRequest.processFunc = (body: Uint8Array, response: FetchDataResponse) => T
|
|
|
|
// For this reason, it should be considered internal until the API is finalized
|
|
|
|
function _fetchData(connection, body, processFunc) {
|
2020-07-14 02:33:30 -04:00
|
|
|
// How many times to retry in the event of a throttle
|
|
|
|
var attemptLimit = (typeof (connection) === "object" && connection.throttleLimit != null) ? connection.throttleLimit : 12;
|
|
|
|
logger.assertArgument((attemptLimit > 0 && (attemptLimit % 1) === 0), "invalid connection throttle limit", "connection.throttleLimit", attemptLimit);
|
|
|
|
var throttleCallback = ((typeof (connection) === "object") ? connection.throttleCallback : null);
|
2020-07-15 14:00:36 -04:00
|
|
|
var throttleSlotInterval = ((typeof (connection) === "object" && typeof (connection.throttleSlotInterval) === "number") ? connection.throttleSlotInterval : 100);
|
|
|
|
logger.assertArgument((throttleSlotInterval > 0 && (throttleSlotInterval % 1) === 0), "invalid connection throttle slot interval", "connection.throttleSlotInterval", throttleSlotInterval);
|
2022-03-09 02:56:08 -05:00
|
|
|
var errorPassThrough = ((typeof (connection) === "object") ? !!(connection.errorPassThrough) : false);
|
2019-05-14 18:48:48 -04:00
|
|
|
var headers = {};
|
|
|
|
var url = null;
|
2019-06-12 01:01:04 -04:00
|
|
|
// @TODO: Allow ConnectionInfo to override some of these values
|
|
|
|
var options = {
|
|
|
|
method: "GET",
|
|
|
|
};
|
2019-09-28 02:36:19 -04:00
|
|
|
var allow304 = false;
|
2019-05-14 18:48:48 -04:00
|
|
|
var timeout = 2 * 60 * 1000;
|
|
|
|
if (typeof (connection) === "string") {
|
|
|
|
url = connection;
|
|
|
|
}
|
|
|
|
else if (typeof (connection) === "object") {
|
2019-06-12 01:01:04 -04:00
|
|
|
if (connection == null || connection.url == null) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("missing URL", "connection.url", connection);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
url = connection.url;
|
|
|
|
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]) };
|
2019-09-28 02:36:19 -04:00
|
|
|
if (["if-none-match", "if-modified-since"].indexOf(key.toLowerCase()) >= 0) {
|
|
|
|
allow304 = true;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-07 20:10:50 -04:00
|
|
|
options.allowGzip = !!connection.allowGzip;
|
2019-05-14 18:48:48 -04:00
|
|
|
if (connection.user != null && connection.password != null) {
|
2019-06-12 01:01:04 -04:00
|
|
|
if (url.substring(0, 6) !== "https:" && connection.allowInsecureAuthentication !== true) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("basic authentication requires a secure https url", logger_1.Logger.errors.INVALID_ARGUMENT, { argument: "url", url: url, user: connection.user, password: "[REDACTED]" });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
var authorization = connection.user + ":" + connection.password;
|
|
|
|
headers["authorization"] = {
|
|
|
|
key: "Authorization",
|
2021-10-16 02:29:27 -04:00
|
|
|
value: "Basic " + (0, base64_1.encode)((0, strings_1.toUtf8Bytes)(authorization))
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
}
|
2022-03-09 02:56:08 -05:00
|
|
|
if (connection.skipFetchSetup != null) {
|
|
|
|
options.skipFetchSetup = !!connection.skipFetchSetup;
|
|
|
|
}
|
2022-08-18 14:48:39 -04:00
|
|
|
if (connection.fetchOptions != null) {
|
|
|
|
options.fetchOptions = (0, properties_1.shallowCopy)(connection.fetchOptions);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2022-09-13 21:28:52 -04:00
|
|
|
var reData = new RegExp("^data:([^;:]*)?(;base64)?,(.*)$", "i");
|
2021-11-30 11:19:37 -05:00
|
|
|
var dataMatch = ((url) ? url.match(reData) : null);
|
|
|
|
if (dataMatch) {
|
|
|
|
try {
|
|
|
|
var response = {
|
|
|
|
statusCode: 200,
|
|
|
|
statusMessage: "OK",
|
2022-09-13 21:28:52 -04:00
|
|
|
headers: { "content-type": (dataMatch[1] || "text/plain") },
|
|
|
|
body: (dataMatch[2] ? (0, base64_1.decode)(dataMatch[3]) : unpercent(dataMatch[3]))
|
2021-11-30 11:19:37 -05:00
|
|
|
};
|
|
|
|
var result = response.body;
|
|
|
|
if (processFunc) {
|
|
|
|
result = processFunc(response.body, response);
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
logger.throwError("processing response error", logger_1.Logger.errors.SERVER_ERROR, {
|
|
|
|
body: bodyify(dataMatch[1], dataMatch[2]),
|
|
|
|
error: error,
|
|
|
|
requestBody: null,
|
|
|
|
requestMethod: "GET",
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 17:04:53 -04:00
|
|
|
if (body) {
|
2019-11-23 21:38:13 +09:00
|
|
|
options.method = "POST";
|
2020-07-30 17:04:53 -04:00
|
|
|
options.body = body;
|
|
|
|
if (headers["content-type"] == null) {
|
|
|
|
headers["content-type"] = { key: "Content-Type", value: "application/octet-stream" };
|
|
|
|
}
|
2020-09-11 02:10:58 -04:00
|
|
|
if (headers["content-length"] == null) {
|
|
|
|
headers["content-length"] = { key: "Content-Length", value: String(body.length) };
|
|
|
|
}
|
2019-11-23 21:38:13 +09:00
|
|
|
}
|
|
|
|
var flatHeaders = {};
|
|
|
|
Object.keys(headers).forEach(function (key) {
|
|
|
|
var header = headers[key];
|
|
|
|
flatHeaders[header.key] = header.value;
|
|
|
|
});
|
|
|
|
options.headers = flatHeaders;
|
|
|
|
var runningTimeout = (function () {
|
2019-05-14 18:48:48 -04:00
|
|
|
var timer = null;
|
2019-11-23 21:38:13 +09:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
|
|
|
if (timeout) {
|
|
|
|
timer = setTimeout(function () {
|
|
|
|
if (timer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer = null;
|
2020-06-12 04:57:38 -04:00
|
|
|
reject(logger.makeError("timeout", logger_1.Logger.errors.TIMEOUT, {
|
2020-09-05 23:35:35 -04:00
|
|
|
requestBody: bodyify(options.body, flatHeaders["content-type"]),
|
2020-06-12 04:57:38 -04:00
|
|
|
requestMethod: options.method,
|
|
|
|
timeout: timeout,
|
|
|
|
url: url
|
|
|
|
}));
|
2019-11-23 21:38:13 +09:00
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var cancel = function () {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (timer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
};
|
2019-11-23 21:38:13 +09:00
|
|
|
return { promise: promise, cancel: cancel };
|
|
|
|
})();
|
|
|
|
var runningFetch = (function () {
|
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2021-11-30 19:16:49 -05:00
|
|
|
var attempt, response, location_1, tryAgain, stall, retryAfter, error_1, body_1, result, error_2, tryAgain, timeout_1;
|
2019-11-23 21:38:13 +09:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
2020-07-14 02:33:30 -04:00
|
|
|
attempt = 0;
|
2019-11-23 21:38:13 +09:00
|
|
|
_a.label = 1;
|
|
|
|
case 1:
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!(attempt < attemptLimit)) return [3 /*break*/, 20];
|
2020-07-14 02:33:30 -04:00
|
|
|
response = null;
|
|
|
|
_a.label = 2;
|
2019-11-23 21:38:13 +09:00
|
|
|
case 2:
|
2021-11-30 19:16:49 -05:00
|
|
|
_a.trys.push([2, 9, , 10]);
|
2021-10-16 02:29:27 -04:00
|
|
|
return [4 /*yield*/, (0, geturl_1.getUrl)(url, options)];
|
2020-04-18 05:14:55 -04:00
|
|
|
case 3:
|
2020-07-14 02:33:30 -04:00
|
|
|
response = _a.sent();
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!(attempt < attemptLimit)) return [3 /*break*/, 8];
|
|
|
|
if (!(response.statusCode === 301 || response.statusCode === 302)) return [3 /*break*/, 4];
|
|
|
|
location_1 = response.headers.location || "";
|
|
|
|
if (options.method === "GET" && location_1.match(/^https:/)) {
|
|
|
|
url = response.headers.location;
|
|
|
|
return [3 /*break*/, 19];
|
|
|
|
}
|
|
|
|
return [3 /*break*/, 8];
|
|
|
|
case 4:
|
|
|
|
if (!(response.statusCode === 429)) return [3 /*break*/, 8];
|
2020-07-14 02:33:30 -04:00
|
|
|
tryAgain = true;
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!throttleCallback) return [3 /*break*/, 6];
|
2020-07-14 02:33:30 -04:00
|
|
|
return [4 /*yield*/, throttleCallback(attempt, url)];
|
|
|
|
case 5:
|
2021-11-30 19:16:49 -05:00
|
|
|
tryAgain = _a.sent();
|
|
|
|
_a.label = 6;
|
|
|
|
case 6:
|
|
|
|
if (!tryAgain) return [3 /*break*/, 8];
|
2020-07-15 14:00:36 -04:00
|
|
|
stall = 0;
|
|
|
|
retryAfter = response.headers["retry-after"];
|
|
|
|
if (typeof (retryAfter) === "string" && retryAfter.match(/^[1-9][0-9]*$/)) {
|
|
|
|
stall = parseInt(retryAfter) * 1000;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stall = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt)));
|
|
|
|
}
|
2020-07-30 17:04:53 -04:00
|
|
|
//console.log("Stalling 429");
|
2020-07-15 14:00:36 -04:00
|
|
|
return [4 /*yield*/, staller(stall)];
|
2021-11-30 19:16:49 -05:00
|
|
|
case 7:
|
2020-07-30 17:04:53 -04:00
|
|
|
//console.log("Stalling 429");
|
2020-07-14 02:33:30 -04:00
|
|
|
_a.sent();
|
2021-11-30 19:16:49 -05:00
|
|
|
return [3 /*break*/, 19];
|
|
|
|
case 8: return [3 /*break*/, 10];
|
|
|
|
case 9:
|
2019-11-23 21:38:13 +09:00
|
|
|
error_1 = _a.sent();
|
2020-04-18 05:14:55 -04:00
|
|
|
response = error_1.response;
|
2020-05-03 17:53:58 -04:00
|
|
|
if (response == null) {
|
2020-05-12 23:31:51 -04:00
|
|
|
runningTimeout.cancel();
|
2020-05-03 17:53:58 -04:00
|
|
|
logger.throwError("missing response", logger_1.Logger.errors.SERVER_ERROR, {
|
2020-09-05 23:35:35 -04:00
|
|
|
requestBody: bodyify(options.body, flatHeaders["content-type"]),
|
2020-06-12 04:57:38 -04:00
|
|
|
requestMethod: options.method,
|
2020-05-03 17:53:58 -04:00
|
|
|
serverError: error_1,
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
}
|
2021-11-30 19:16:49 -05:00
|
|
|
return [3 /*break*/, 10];
|
|
|
|
case 10:
|
2020-07-30 17:04:53 -04:00
|
|
|
body_1 = response.body;
|
2020-04-18 05:14:55 -04:00
|
|
|
if (allow304 && response.statusCode === 304) {
|
2020-07-30 17:04:53 -04:00
|
|
|
body_1 = null;
|
2019-11-23 21:38:13 +09:00
|
|
|
}
|
2022-03-09 02:56:08 -05:00
|
|
|
else if (!errorPassThrough && (response.statusCode < 200 || response.statusCode >= 300)) {
|
2019-11-23 21:38:13 +09:00
|
|
|
runningTimeout.cancel();
|
|
|
|
logger.throwError("bad response", logger_1.Logger.errors.SERVER_ERROR, {
|
2020-04-18 05:14:55 -04:00
|
|
|
status: response.statusCode,
|
|
|
|
headers: response.headers,
|
2020-09-05 23:35:35 -04:00
|
|
|
body: bodyify(body_1, ((response.headers) ? response.headers["content-type"] : null)),
|
|
|
|
requestBody: bodyify(options.body, flatHeaders["content-type"]),
|
2020-06-12 04:57:38 -04:00
|
|
|
requestMethod: options.method,
|
2020-04-18 05:14:55 -04:00
|
|
|
url: url
|
2019-09-28 02:36:19 -04:00
|
|
|
});
|
|
|
|
}
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!processFunc) return [3 /*break*/, 18];
|
|
|
|
_a.label = 11;
|
2020-07-14 02:33:30 -04:00
|
|
|
case 11:
|
2021-11-30 19:16:49 -05:00
|
|
|
_a.trys.push([11, 13, , 18]);
|
|
|
|
return [4 /*yield*/, processFunc(body_1, response)];
|
|
|
|
case 12:
|
2020-07-30 17:04:53 -04:00
|
|
|
result = _a.sent();
|
|
|
|
runningTimeout.cancel();
|
|
|
|
return [2 /*return*/, result];
|
2021-11-30 19:16:49 -05:00
|
|
|
case 13:
|
2019-11-23 21:38:13 +09:00
|
|
|
error_2 = _a.sent();
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!(error_2.throttleRetry && attempt < attemptLimit)) return [3 /*break*/, 17];
|
2020-07-14 02:33:30 -04:00
|
|
|
tryAgain = true;
|
2021-11-30 19:16:49 -05:00
|
|
|
if (!throttleCallback) return [3 /*break*/, 15];
|
2020-07-14 02:33:30 -04:00
|
|
|
return [4 /*yield*/, throttleCallback(attempt, url)];
|
|
|
|
case 14:
|
2021-11-30 19:16:49 -05:00
|
|
|
tryAgain = _a.sent();
|
|
|
|
_a.label = 15;
|
|
|
|
case 15:
|
|
|
|
if (!tryAgain) return [3 /*break*/, 17];
|
2020-07-15 14:00:36 -04:00
|
|
|
timeout_1 = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt)));
|
2020-07-30 17:04:53 -04:00
|
|
|
//console.log("Stalling callback");
|
2020-07-15 14:00:36 -04:00
|
|
|
return [4 /*yield*/, staller(timeout_1)];
|
2021-11-30 19:16:49 -05:00
|
|
|
case 16:
|
2020-07-30 17:04:53 -04:00
|
|
|
//console.log("Stalling callback");
|
2020-07-14 02:33:30 -04:00
|
|
|
_a.sent();
|
2021-11-30 19:16:49 -05:00
|
|
|
return [3 /*break*/, 19];
|
|
|
|
case 17:
|
2020-07-14 02:33:30 -04:00
|
|
|
runningTimeout.cancel();
|
2019-09-28 02:36:19 -04:00
|
|
|
logger.throwError("processing response error", logger_1.Logger.errors.SERVER_ERROR, {
|
2020-09-05 23:35:35 -04:00
|
|
|
body: bodyify(body_1, ((response.headers) ? response.headers["content-type"] : null)),
|
2020-06-12 04:57:38 -04:00
|
|
|
error: error_2,
|
2020-09-05 23:35:35 -04:00
|
|
|
requestBody: bodyify(options.body, flatHeaders["content-type"]),
|
2020-06-12 04:57:38 -04:00
|
|
|
requestMethod: options.method,
|
|
|
|
url: url
|
2019-09-28 02:36:19 -04:00
|
|
|
});
|
2021-11-30 19:16:49 -05:00
|
|
|
return [3 /*break*/, 18];
|
|
|
|
case 18:
|
2020-07-14 02:33:30 -04:00
|
|
|
runningTimeout.cancel();
|
2021-10-16 02:29:27 -04:00
|
|
|
// If we had a processFunc, it either returned a T or threw above.
|
2020-07-30 17:04:53 -04:00
|
|
|
// The "body" is now a Uint8Array.
|
|
|
|
return [2 /*return*/, body_1];
|
2021-11-30 19:16:49 -05:00
|
|
|
case 19:
|
2020-07-14 02:33:30 -04:00
|
|
|
attempt++;
|
|
|
|
return [3 /*break*/, 1];
|
2021-11-30 19:16:49 -05:00
|
|
|
case 20: return [2 /*return*/, logger.throwError("failed response", logger_1.Logger.errors.SERVER_ERROR, {
|
2020-09-05 23:35:35 -04:00
|
|
|
requestBody: bodyify(options.body, flatHeaders["content-type"]),
|
2020-07-30 17:04:53 -04:00
|
|
|
requestMethod: options.method,
|
|
|
|
url: url
|
|
|
|
})];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-09-28 02:36:19 -04:00
|
|
|
});
|
2019-06-12 01:01:04 -04:00
|
|
|
});
|
2019-11-23 21:38:13 +09:00
|
|
|
})();
|
|
|
|
return Promise.race([runningTimeout.promise, runningFetch]);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-07-31 01:32:26 -04:00
|
|
|
exports._fetchData = _fetchData;
|
2020-07-30 17:04:53 -04:00
|
|
|
function fetchJson(connection, json, processFunc) {
|
|
|
|
var processJsonFunc = function (value, response) {
|
|
|
|
var result = null;
|
|
|
|
if (value != null) {
|
|
|
|
try {
|
2021-10-16 02:29:27 -04:00
|
|
|
result = JSON.parse((0, strings_1.toUtf8String)(value));
|
2020-07-30 17:04:53 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
logger.throwError("invalid JSON", logger_1.Logger.errors.SERVER_ERROR, {
|
|
|
|
body: value,
|
|
|
|
error: error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (processFunc) {
|
|
|
|
result = processFunc(result, response);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// If we have json to send, we must
|
|
|
|
// - add content-type of application/json (unless already overridden)
|
|
|
|
// - convert the json to bytes
|
|
|
|
var body = null;
|
|
|
|
if (json != null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
body = (0, strings_1.toUtf8Bytes)(json);
|
2020-07-30 17:04:53 -04:00
|
|
|
// Create a connection with the content-type set for JSON
|
2021-10-16 02:29:27 -04:00
|
|
|
var updated = (typeof (connection) === "string") ? ({ url: connection }) : (0, properties_1.shallowCopy)(connection);
|
2020-07-30 17:04:53 -04:00
|
|
|
if (updated.headers) {
|
|
|
|
var hasContentType = (Object.keys(updated.headers).filter(function (k) { return (k.toLowerCase() === "content-type"); }).length) !== 0;
|
|
|
|
if (!hasContentType) {
|
2021-10-16 02:29:27 -04:00
|
|
|
updated.headers = (0, properties_1.shallowCopy)(updated.headers);
|
2020-07-30 17:04:53 -04:00
|
|
|
updated.headers["content-type"] = "application/json";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
updated.headers = { "content-type": "application/json" };
|
|
|
|
}
|
|
|
|
connection = updated;
|
|
|
|
}
|
2020-07-31 01:32:26 -04:00
|
|
|
return _fetchData(connection, body, processJsonFunc);
|
2020-07-30 17:04:53 -04:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
exports.fetchJson = fetchJson;
|
|
|
|
function poll(func, options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
options = (0, properties_1.shallowCopy)(options);
|
2019-05-14 18:48:48 -04: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) {
|
|
|
|
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 retryLimit = options.retryLimit;
|
|
|
|
var 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-04 23:01:04 -04:00
|
|
|
else if (options.oncePoll) {
|
|
|
|
options.oncePoll.once("poll", check);
|
|
|
|
}
|
2019-05-14 18:48:48 -04: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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}, function (error) {
|
|
|
|
if (cancel()) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
check();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
exports.poll = poll;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|