@ethersproject/web
Advanced tools
Comparing version 5.0.2 to 5.0.3
@@ -1,1 +0,1 @@ | ||
export declare const version = "web/5.0.2"; | ||
export declare const version = "web/5.0.3"; |
@@ -1,2 +0,2 @@ | ||
export const version = "web/5.0.2"; | ||
export const version = "web/5.0.3"; | ||
//# sourceMappingURL=_version.js.map |
@@ -1,16 +0,3 @@ | ||
export declare type GetUrlResponse = { | ||
statusCode: number; | ||
statusMessage: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
body: string; | ||
}; | ||
export declare type Options = { | ||
method?: string; | ||
body?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
import type { GetUrlResponse, Options } from "./types"; | ||
export { GetUrlResponse, Options }; | ||
export declare function getUrl(href: string, options?: Options): Promise<GetUrlResponse>; |
@@ -11,2 +11,3 @@ "use strict"; | ||
}; | ||
import { arrayify } from "@ethersproject/bytes"; | ||
export function getUrl(href, options) { | ||
@@ -28,3 +29,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield fetch(href, request); | ||
const body = yield response.text(); | ||
const body = yield response.arrayBuffer(); | ||
const headers = {}; | ||
@@ -45,3 +46,3 @@ if (response.headers.forEach) { | ||
statusMessage: response.statusText, | ||
body: body, | ||
body: arrayify(new Uint8Array(body)), | ||
}; | ||
@@ -48,0 +49,0 @@ }); |
@@ -1,16 +0,3 @@ | ||
export declare type GetUrlResponse = { | ||
statusCode: number; | ||
statusMessage: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
body: string; | ||
}; | ||
export declare type Options = { | ||
method?: string; | ||
body?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
import type { GetUrlResponse, Options } from "./types"; | ||
export { GetUrlResponse, Options }; | ||
export declare function getUrl(href: string, options?: Options): Promise<GetUrlResponse>; |
@@ -14,2 +14,3 @@ "use strict"; | ||
import { parse } from "url"; | ||
import { concat } from "@ethersproject/bytes"; | ||
import { Logger } from "@ethersproject/logger"; | ||
@@ -34,8 +35,8 @@ import { version } from "./_version"; | ||
}; | ||
resp.setEncoding("utf8"); | ||
//resp.setEncoding("utf8"); | ||
resp.on("data", (chunk) => { | ||
if (response.body == null) { | ||
response.body = ""; | ||
response.body = new Uint8Array(0); | ||
} | ||
response.body += chunk; | ||
response.body = concat([response.body, chunk]); | ||
}); | ||
@@ -94,3 +95,3 @@ resp.on("end", () => { | ||
if (options.body) { | ||
req.write(options.body); | ||
req.write(Buffer.from(options.body)); | ||
} | ||
@@ -97,0 +98,0 @@ req.end(); |
@@ -35,3 +35,4 @@ export declare type ConnectionInfo = { | ||
}; | ||
export declare function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo, body?: Uint8Array, processFunc?: (value: Uint8Array, response: FetchJsonResponse) => T): Promise<T>; | ||
export declare function fetchJson(connection: string | ConnectionInfo, json?: string, processFunc?: (value: any, response: FetchJsonResponse) => any): Promise<any>; | ||
export declare function poll<T>(func: () => Promise<T>, options?: PollOptions): Promise<T>; |
@@ -13,3 +13,3 @@ "use strict"; | ||
import { shallowCopy } from "@ethersproject/properties"; | ||
import { toUtf8Bytes } from "@ethersproject/strings"; | ||
import { toUtf8Bytes, toUtf8String } from "@ethersproject/strings"; | ||
import { Logger } from "@ethersproject/logger"; | ||
@@ -24,3 +24,9 @@ import { version } from "./_version"; | ||
} | ||
export function fetchJson(connection, json, processFunc) { | ||
// 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 | ||
export function _fetchData(connection, body, processFunc) { | ||
// How many times to retry in the event of a throttle | ||
@@ -70,6 +76,8 @@ const attemptLimit = (typeof (connection) === "object" && connection.throttleLimit != null) ? connection.throttleLimit : 12; | ||
} | ||
if (json) { | ||
if (body) { | ||
options.method = "POST"; | ||
options.body = json; | ||
headers["content-type"] = { key: "Content-Type", value: "application/json" }; | ||
options.body = body; | ||
if (headers["content-type"] == null) { | ||
headers["content-type"] = { key: "Content-Type", value: "application/octet-stream" }; | ||
} | ||
} | ||
@@ -130,2 +138,3 @@ const flatHeaders = {}; | ||
} | ||
//console.log("Stalling 429"); | ||
yield staller(stall); | ||
@@ -163,22 +172,8 @@ continue; | ||
} | ||
let json = null; | ||
if (body != null) { | ||
if (processFunc) { | ||
try { | ||
json = JSON.parse(body); | ||
} | ||
catch (error) { | ||
const result = yield processFunc(body, response); | ||
runningTimeout.cancel(); | ||
logger.throwError("invalid JSON", Logger.errors.SERVER_ERROR, { | ||
body: body, | ||
error: error, | ||
requestBody: (options.body || null), | ||
requestMethod: options.method, | ||
url: url | ||
}); | ||
return result; | ||
} | ||
} | ||
if (processFunc) { | ||
try { | ||
json = yield processFunc(json, response); | ||
} | ||
catch (error) { | ||
@@ -193,2 +188,3 @@ // Allow the processFunc to trigger a throttle | ||
const timeout = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt))); | ||
//console.log("Stalling callback"); | ||
yield staller(timeout); | ||
@@ -200,3 +196,3 @@ continue; | ||
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, { | ||
body: json, | ||
body: body, | ||
error: error, | ||
@@ -210,4 +206,11 @@ requestBody: (options.body || null), | ||
runningTimeout.cancel(); | ||
return json; | ||
// If we had a processFunc, it eitehr returned a T or threw above. | ||
// The "body" is now a Uint8Array. | ||
return body; | ||
} | ||
return logger.throwError("failed response", Logger.errors.SERVER_ERROR, { | ||
requestBody: (options.body || null), | ||
requestMethod: options.method, | ||
url: url | ||
}); | ||
}); | ||
@@ -217,2 +220,43 @@ })(); | ||
} | ||
export function fetchJson(connection, json, processFunc) { | ||
let processJsonFunc = (value, response) => { | ||
let result = null; | ||
if (value != null) { | ||
try { | ||
result = JSON.parse(toUtf8String(value)); | ||
} | ||
catch (error) { | ||
logger.throwError("invalid JSON", 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 | ||
let body = null; | ||
if (json != null) { | ||
body = toUtf8Bytes(json); | ||
// Create a connection with the content-type set for JSON | ||
const updated = (typeof (connection) === "string") ? ({ url: connection }) : shallowCopy(connection); | ||
if (updated.headers) { | ||
const hasContentType = (Object.keys(updated.headers).filter((k) => (k.toLowerCase() === "content-type")).length) !== 0; | ||
if (!hasContentType) { | ||
updated.headers = shallowCopy(updated.headers); | ||
updated.headers["content-type"] = "application/json"; | ||
} | ||
} | ||
else { | ||
updated.headers = { "content-type": "application/json" }; | ||
} | ||
connection = updated; | ||
} | ||
return _fetchData(connection, body, processJsonFunc); | ||
} | ||
export function poll(func, options) { | ||
@@ -219,0 +263,0 @@ if (!options) { |
@@ -1,1 +0,1 @@ | ||
export declare const version = "web/5.0.2"; | ||
export declare const version = "web/5.0.3"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.version = "web/5.0.2"; | ||
exports.version = "web/5.0.3"; | ||
//# sourceMappingURL=_version.js.map |
@@ -1,16 +0,3 @@ | ||
export declare type GetUrlResponse = { | ||
statusCode: number; | ||
statusMessage: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
body: string; | ||
}; | ||
export declare type Options = { | ||
method?: string; | ||
body?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
import type { GetUrlResponse, Options } from "./types"; | ||
export { GetUrlResponse, Options }; | ||
export declare function getUrl(href: string, options?: Options): Promise<GetUrlResponse>; |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var bytes_1 = require("@ethersproject/bytes"); | ||
function getUrl(href, options) { | ||
@@ -62,3 +63,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
response = _a.sent(); | ||
return [4 /*yield*/, response.text()]; | ||
return [4 /*yield*/, response.arrayBuffer()]; | ||
case 2: | ||
@@ -81,3 +82,3 @@ body = _a.sent(); | ||
statusMessage: response.statusText, | ||
body: body, | ||
body: bytes_1.arrayify(new Uint8Array(body)), | ||
}]; | ||
@@ -84,0 +85,0 @@ } |
@@ -1,16 +0,3 @@ | ||
export declare type GetUrlResponse = { | ||
statusCode: number; | ||
statusMessage: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
body: string; | ||
}; | ||
export declare type Options = { | ||
method?: string; | ||
body?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
import type { GetUrlResponse, Options } from "./types"; | ||
export { GetUrlResponse, Options }; | ||
export declare function getUrl(href: string, options?: Options): Promise<GetUrlResponse>; |
@@ -45,2 +45,3 @@ "use strict"; | ||
var url_1 = require("url"); | ||
var bytes_1 = require("@ethersproject/bytes"); | ||
var logger_1 = require("@ethersproject/logger"); | ||
@@ -65,8 +66,8 @@ var _version_1 = require("./_version"); | ||
}; | ||
resp.setEncoding("utf8"); | ||
//resp.setEncoding("utf8"); | ||
resp.on("data", function (chunk) { | ||
if (response.body == null) { | ||
response.body = ""; | ||
response.body = new Uint8Array(0); | ||
} | ||
response.body += chunk; | ||
response.body = bytes_1.concat([response.body, chunk]); | ||
}); | ||
@@ -126,3 +127,3 @@ resp.on("end", function () { | ||
if (options.body) { | ||
req.write(options.body); | ||
req.write(Buffer.from(options.body)); | ||
} | ||
@@ -129,0 +130,0 @@ req.end(); |
@@ -35,3 +35,4 @@ export declare type ConnectionInfo = { | ||
}; | ||
export declare function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo, body?: Uint8Array, processFunc?: (value: Uint8Array, response: FetchJsonResponse) => T): Promise<T>; | ||
export declare function fetchJson(connection: string | ConnectionInfo, json?: string, processFunc?: (value: any, response: FetchJsonResponse) => any): Promise<any>; | ||
export declare function poll<T>(func: () => Promise<T>, options?: PollOptions): Promise<T>; |
105
lib/index.js
@@ -51,3 +51,9 @@ "use strict"; | ||
} | ||
function fetchJson(connection, json, processFunc) { | ||
// 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) { | ||
// How many times to retry in the event of a throttle | ||
@@ -97,6 +103,8 @@ var attemptLimit = (typeof (connection) === "object" && connection.throttleLimit != null) ? connection.throttleLimit : 12; | ||
} | ||
if (json) { | ||
if (body) { | ||
options.method = "POST"; | ||
options.body = json; | ||
headers["content-type"] = { key: "Content-Type", value: "application/json" }; | ||
options.body = body; | ||
if (headers["content-type"] == null) { | ||
headers["content-type"] = { key: "Content-Type", value: "application/octet-stream" }; | ||
} | ||
} | ||
@@ -138,3 +146,3 @@ var flatHeaders = {}; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var attempt, response, tryAgain, stall, retryAfter, error_1, body, json_1, error_2, tryAgain, timeout_1; | ||
var attempt, response, tryAgain, stall, retryAfter, error_1, body_1, result, error_2, tryAgain, timeout_1; | ||
return __generator(this, function (_a) { | ||
@@ -171,4 +179,6 @@ switch (_a.label) { | ||
} | ||
//console.log("Stalling 429"); | ||
return [4 /*yield*/, staller(stall)]; | ||
case 6: | ||
//console.log("Stalling 429"); | ||
_a.sent(); | ||
@@ -191,5 +201,5 @@ return [3 /*break*/, 18]; | ||
case 9: | ||
body = response.body; | ||
body_1 = response.body; | ||
if (allow304 && response.statusCode === 304) { | ||
body = null; | ||
body_1 = null; | ||
} | ||
@@ -201,3 +211,3 @@ else if (response.statusCode < 200 || response.statusCode >= 300) { | ||
headers: response.headers, | ||
body: body, | ||
body: body_1, | ||
requestBody: (options.body || null), | ||
@@ -208,18 +218,2 @@ requestMethod: options.method, | ||
} | ||
json_1 = null; | ||
if (body != null) { | ||
try { | ||
json_1 = JSON.parse(body); | ||
} | ||
catch (error) { | ||
runningTimeout.cancel(); | ||
logger.throwError("invalid JSON", logger_1.Logger.errors.SERVER_ERROR, { | ||
body: body, | ||
error: error, | ||
requestBody: (options.body || null), | ||
requestMethod: options.method, | ||
url: url | ||
}); | ||
} | ||
} | ||
if (!processFunc) return [3 /*break*/, 17]; | ||
@@ -229,6 +223,7 @@ _a.label = 10; | ||
_a.trys.push([10, 12, , 17]); | ||
return [4 /*yield*/, processFunc(json_1, response)]; | ||
return [4 /*yield*/, processFunc(body_1, response)]; | ||
case 11: | ||
json_1 = _a.sent(); | ||
return [3 /*break*/, 17]; | ||
result = _a.sent(); | ||
runningTimeout.cancel(); | ||
return [2 /*return*/, result]; | ||
case 12: | ||
@@ -246,4 +241,6 @@ error_2 = _a.sent(); | ||
timeout_1 = throttleSlotInterval * parseInt(String(Math.random() * Math.pow(2, attempt))); | ||
//console.log("Stalling callback"); | ||
return [4 /*yield*/, staller(timeout_1)]; | ||
case 15: | ||
//console.log("Stalling callback"); | ||
_a.sent(); | ||
@@ -254,3 +251,3 @@ return [3 /*break*/, 18]; | ||
logger.throwError("processing response error", logger_1.Logger.errors.SERVER_ERROR, { | ||
body: json_1, | ||
body: body_1, | ||
error: error_2, | ||
@@ -264,7 +261,13 @@ requestBody: (options.body || null), | ||
runningTimeout.cancel(); | ||
return [2 /*return*/, json_1]; | ||
// If we had a processFunc, it eitehr returned a T or threw above. | ||
// The "body" is now a Uint8Array. | ||
return [2 /*return*/, body_1]; | ||
case 18: | ||
attempt++; | ||
return [3 /*break*/, 1]; | ||
case 19: return [2 /*return*/]; | ||
case 19: return [2 /*return*/, logger.throwError("failed response", logger_1.Logger.errors.SERVER_ERROR, { | ||
requestBody: (options.body || null), | ||
requestMethod: options.method, | ||
url: url | ||
})]; | ||
} | ||
@@ -276,2 +279,44 @@ }); | ||
} | ||
exports._fetchData = _fetchData; | ||
function fetchJson(connection, json, processFunc) { | ||
var processJsonFunc = function (value, response) { | ||
var result = null; | ||
if (value != null) { | ||
try { | ||
result = JSON.parse(strings_1.toUtf8String(value)); | ||
} | ||
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) { | ||
body = strings_1.toUtf8Bytes(json); | ||
// Create a connection with the content-type set for JSON | ||
var updated = (typeof (connection) === "string") ? ({ url: connection }) : properties_1.shallowCopy(connection); | ||
if (updated.headers) { | ||
var hasContentType = (Object.keys(updated.headers).filter(function (k) { return (k.toLowerCase() === "content-type"); }).length) !== 0; | ||
if (!hasContentType) { | ||
updated.headers = properties_1.shallowCopy(updated.headers); | ||
updated.headers["content-type"] = "application/json"; | ||
} | ||
} | ||
else { | ||
updated.headers = { "content-type": "application/json" }; | ||
} | ||
connection = updated; | ||
} | ||
return _fetchData(connection, body, processJsonFunc); | ||
} | ||
exports.fetchJson = fetchJson; | ||
@@ -278,0 +323,0 @@ function poll(func, options) { |
@@ -14,2 +14,3 @@ { | ||
"@ethersproject/base64": "^5.0.0", | ||
"@ethersproject/bytes": "^5.0.0", | ||
"@ethersproject/logger": "^5.0.0", | ||
@@ -39,5 +40,5 @@ "@ethersproject/properties": "^5.0.0", | ||
}, | ||
"tarballHash": "0x706e9cf2a72162332269c71f0e1c5ca6dbaa252e627710ed676fd342dbe89e40", | ||
"tarballHash": "0x3e054301f081b63cc8d656df7bae48d95a779404564143b289f5274498c27772", | ||
"types": "./lib/index.d.ts", | ||
"version": "5.0.2" | ||
"version": "5.0.3" | ||
} |
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
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
86618
34
1218
5
+ Added@ethersproject/bytes@^5.0.0