@ethersproject/web
Advanced tools
Comparing version 5.5.0 to 5.5.1
@@ -1,2 +0,2 @@ | ||
export declare const version = "web/5.5.0"; | ||
export declare const version = "web/5.5.1"; | ||
//# sourceMappingURL=_version.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export const version = "web/5.5.0"; | ||
export const version = "web/5.5.1"; | ||
//# sourceMappingURL=_version.js.map |
@@ -11,3 +11,3 @@ "use strict"; | ||
}; | ||
import { encode as base64Encode } from "@ethersproject/base64"; | ||
import { decode as base64Decode, encode as base64Encode } from "@ethersproject/base64"; | ||
import { hexlify, isBytesLike } from "@ethersproject/bytes"; | ||
@@ -96,2 +96,28 @@ import { shallowCopy } from "@ethersproject/properties"; | ||
} | ||
const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i"); | ||
const dataMatch = ((url) ? url.match(reData) : null); | ||
if (dataMatch) { | ||
try { | ||
const response = { | ||
statusCode: 200, | ||
statusMessage: "OK", | ||
headers: { "content-type": dataMatch[1] }, | ||
body: base64Decode(dataMatch[2]) | ||
}; | ||
let result = response.body; | ||
if (processFunc) { | ||
result = processFunc(response.body, response); | ||
} | ||
return Promise.resolve(result); | ||
} | ||
catch (error) { | ||
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, { | ||
body: bodyify(dataMatch[1], dataMatch[2]), | ||
error: error, | ||
requestBody: null, | ||
requestMethod: "GET", | ||
url: url | ||
}); | ||
} | ||
} | ||
if (body) { | ||
@@ -146,20 +172,30 @@ options.method = "POST"; | ||
response = yield getUrl(url, options); | ||
// Exponential back-off throttling | ||
if (response.statusCode === 429 && attempt < attemptLimit) { | ||
let tryAgain = true; | ||
if (throttleCallback) { | ||
tryAgain = yield throttleCallback(attempt, url); | ||
if (attempt < attemptLimit) { | ||
if (response.statusCode === 301 || response.statusCode === 302) { | ||
// Redirection; for now we only support absolute locataions | ||
const location = response.headers.location || ""; | ||
if (options.method === "GET" && location.match(/^https:/)) { | ||
url = response.headers.location; | ||
continue; | ||
} | ||
} | ||
if (tryAgain) { | ||
let stall = 0; | ||
const retryAfter = response.headers["retry-after"]; | ||
if (typeof (retryAfter) === "string" && retryAfter.match(/^[1-9][0-9]*$/)) { | ||
stall = parseInt(retryAfter) * 1000; | ||
else if (response.statusCode === 429) { | ||
// Exponential back-off throttling | ||
let tryAgain = true; | ||
if (throttleCallback) { | ||
tryAgain = yield throttleCallback(attempt, url); | ||
} | ||
else { | ||
stall = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt))); | ||
if (tryAgain) { | ||
let stall = 0; | ||
const 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))); | ||
} | ||
//console.log("Stalling 429"); | ||
yield staller(stall); | ||
continue; | ||
} | ||
//console.log("Stalling 429"); | ||
yield staller(stall); | ||
continue; | ||
} | ||
@@ -166,0 +202,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export declare const version = "web/5.5.0"; | ||
export declare const version = "web/5.5.1"; | ||
//# sourceMappingURL=_version.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.version = void 0; | ||
exports.version = "web/5.5.0"; | ||
exports.version = "web/5.5.1"; | ||
//# sourceMappingURL=_version.js.map |
103
lib/index.js
@@ -124,2 +124,28 @@ "use strict"; | ||
} | ||
var reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i"); | ||
var dataMatch = ((url) ? url.match(reData) : null); | ||
if (dataMatch) { | ||
try { | ||
var response = { | ||
statusCode: 200, | ||
statusMessage: "OK", | ||
headers: { "content-type": dataMatch[1] }, | ||
body: (0, base64_1.decode)(dataMatch[2]) | ||
}; | ||
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 | ||
}); | ||
} | ||
} | ||
if (body) { | ||
@@ -170,3 +196,3 @@ options.method = "POST"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var attempt, response, tryAgain, stall, retryAfter, error_1, body_1, result, error_2, tryAgain, timeout_1; | ||
var attempt, response, location_1, tryAgain, stall, retryAfter, error_1, body_1, result, error_2, tryAgain, timeout_1; | ||
return __generator(this, function (_a) { | ||
@@ -178,19 +204,28 @@ switch (_a.label) { | ||
case 1: | ||
if (!(attempt < attemptLimit)) return [3 /*break*/, 19]; | ||
if (!(attempt < attemptLimit)) return [3 /*break*/, 20]; | ||
response = null; | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 8, , 9]); | ||
_a.trys.push([2, 9, , 10]); | ||
return [4 /*yield*/, (0, geturl_1.getUrl)(url, options)]; | ||
case 3: | ||
response = _a.sent(); | ||
if (!(response.statusCode === 429 && attempt < attemptLimit)) return [3 /*break*/, 7]; | ||
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]; | ||
tryAgain = true; | ||
if (!throttleCallback) return [3 /*break*/, 5]; | ||
if (!throttleCallback) return [3 /*break*/, 6]; | ||
return [4 /*yield*/, throttleCallback(attempt, url)]; | ||
case 4: | ||
case 5: | ||
tryAgain = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
if (!tryAgain) return [3 /*break*/, 7]; | ||
_a.label = 6; | ||
case 6: | ||
if (!tryAgain) return [3 /*break*/, 8]; | ||
stall = 0; | ||
@@ -206,8 +241,8 @@ retryAfter = response.headers["retry-after"]; | ||
return [4 /*yield*/, staller(stall)]; | ||
case 6: | ||
case 7: | ||
//console.log("Stalling 429"); | ||
_a.sent(); | ||
return [3 /*break*/, 18]; | ||
case 7: return [3 /*break*/, 9]; | ||
case 8: | ||
return [3 /*break*/, 19]; | ||
case 8: return [3 /*break*/, 10]; | ||
case 9: | ||
error_1 = _a.sent(); | ||
@@ -224,4 +259,4 @@ response = error_1.response; | ||
} | ||
return [3 /*break*/, 9]; | ||
case 9: | ||
return [3 /*break*/, 10]; | ||
case 10: | ||
body_1 = response.body; | ||
@@ -242,30 +277,30 @@ if (allow304 && response.statusCode === 304) { | ||
} | ||
if (!processFunc) return [3 /*break*/, 17]; | ||
_a.label = 10; | ||
case 10: | ||
_a.trys.push([10, 12, , 17]); | ||
if (!processFunc) return [3 /*break*/, 18]; | ||
_a.label = 11; | ||
case 11: | ||
_a.trys.push([11, 13, , 18]); | ||
return [4 /*yield*/, processFunc(body_1, response)]; | ||
case 11: | ||
case 12: | ||
result = _a.sent(); | ||
runningTimeout.cancel(); | ||
return [2 /*return*/, result]; | ||
case 12: | ||
case 13: | ||
error_2 = _a.sent(); | ||
if (!(error_2.throttleRetry && attempt < attemptLimit)) return [3 /*break*/, 16]; | ||
if (!(error_2.throttleRetry && attempt < attemptLimit)) return [3 /*break*/, 17]; | ||
tryAgain = true; | ||
if (!throttleCallback) return [3 /*break*/, 14]; | ||
if (!throttleCallback) return [3 /*break*/, 15]; | ||
return [4 /*yield*/, throttleCallback(attempt, url)]; | ||
case 13: | ||
case 14: | ||
tryAgain = _a.sent(); | ||
_a.label = 14; | ||
case 14: | ||
if (!tryAgain) return [3 /*break*/, 16]; | ||
_a.label = 15; | ||
case 15: | ||
if (!tryAgain) return [3 /*break*/, 17]; | ||
timeout_1 = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt))); | ||
//console.log("Stalling callback"); | ||
return [4 /*yield*/, staller(timeout_1)]; | ||
case 15: | ||
case 16: | ||
//console.log("Stalling callback"); | ||
_a.sent(); | ||
return [3 /*break*/, 18]; | ||
case 16: | ||
return [3 /*break*/, 19]; | ||
case 17: | ||
runningTimeout.cancel(); | ||
@@ -279,4 +314,4 @@ logger.throwError("processing response error", logger_1.Logger.errors.SERVER_ERROR, { | ||
}); | ||
return [3 /*break*/, 17]; | ||
case 17: | ||
return [3 /*break*/, 18]; | ||
case 18: | ||
runningTimeout.cancel(); | ||
@@ -286,6 +321,6 @@ // If we had a processFunc, it either returned a T or threw above. | ||
return [2 /*return*/, body_1]; | ||
case 18: | ||
case 19: | ||
attempt++; | ||
return [3 /*break*/, 1]; | ||
case 19: return [2 /*return*/, logger.throwError("failed response", logger_1.Logger.errors.SERVER_ERROR, { | ||
case 20: return [2 /*return*/, logger.throwError("failed response", logger_1.Logger.errors.SERVER_ERROR, { | ||
requestBody: bodyify(options.body, flatHeaders["content-type"]), | ||
@@ -292,0 +327,0 @@ requestMethod: options.method, |
@@ -28,3 +28,3 @@ { | ||
], | ||
"gitHead": "73a46efea32c3f9a4833ed77896a216e3d3752a0", | ||
"gitHead": "eb432aa1f44ad2cc268d000b266eae9b03db1d17", | ||
"keywords": [ | ||
@@ -50,5 +50,5 @@ "Ethereum", | ||
"sideEffects": false, | ||
"tarballHash": "0x1ba437b3de41bf55152c0e51b659134b7c3f58caeb50aeb65f1c3c5d81b622de", | ||
"tarballHash": "0x111d1874bfaa91100367dfc7b0d920f66787685d2c5aa45903eb1b6ba627a757", | ||
"types": "./lib/index.d.ts", | ||
"version": "5.5.0" | ||
"version": "5.5.1" | ||
} |
@@ -1,1 +0,1 @@ | ||
export const version = "web/5.5.0"; | ||
export const version = "web/5.5.1"; |
"use strict"; | ||
import { encode as base64Encode } from "@ethersproject/base64"; | ||
import { decode as base64Decode, encode as base64Encode } from "@ethersproject/base64"; | ||
import { hexlify, isBytesLike } from "@ethersproject/bytes"; | ||
@@ -153,3 +153,30 @@ import { shallowCopy } from "@ethersproject/properties"; | ||
} | ||
const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i"); | ||
const dataMatch = ((url) ? url.match(reData): null); | ||
if (dataMatch) { | ||
try { | ||
const response = { | ||
statusCode: 200, | ||
statusMessage: "OK", | ||
headers: { "content-type": dataMatch[1] }, | ||
body: base64Decode(dataMatch[2]) | ||
}; | ||
let result: T = <T><unknown>response.body; | ||
if (processFunc) { | ||
result = processFunc(response.body, response); | ||
} | ||
return Promise.resolve(<T><unknown>result); | ||
} catch (error) { | ||
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, { | ||
body: bodyify(dataMatch[1], dataMatch[2]), | ||
error: error, | ||
requestBody: null, | ||
requestMethod: "GET", | ||
url: url | ||
}); | ||
} | ||
} | ||
if (body) { | ||
@@ -208,22 +235,32 @@ options.method = "POST"; | ||
// Exponential back-off throttling | ||
if (response.statusCode === 429 && attempt < attemptLimit) { | ||
let tryAgain = true; | ||
if (throttleCallback) { | ||
tryAgain = await throttleCallback(attempt, url); | ||
} | ||
if (attempt < attemptLimit) { | ||
if (response.statusCode === 301 || response.statusCode === 302) { | ||
// Redirection; for now we only support absolute locataions | ||
const location = response.headers.location || ""; | ||
if (options.method === "GET" && location.match(/^https:/)) { | ||
url = response.headers.location; | ||
continue; | ||
} | ||
if (tryAgain) { | ||
let stall = 0; | ||
} else if (response.statusCode === 429) { | ||
// Exponential back-off throttling | ||
let tryAgain = true; | ||
if (throttleCallback) { | ||
tryAgain = await throttleCallback(attempt, url); | ||
} | ||
const 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))); | ||
if (tryAgain) { | ||
let stall = 0; | ||
const 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))); | ||
} | ||
//console.log("Stalling 429"); | ||
await staller(stall); | ||
continue; | ||
} | ||
//console.log("Stalling 429"); | ||
await staller(stall); | ||
continue; | ||
} | ||
@@ -230,0 +267,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120893
1781