@openzeppelin/defender-sdk-base-client
Advanced tools
Comparing version 1.12.0 to 1.13.0
@@ -6,4 +6,5 @@ export declare abstract class BaseActionClient { | ||
constructor(credentials: string, arn: string); | ||
private invoke; | ||
protected execute<T>(request: object): Promise<T>; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseActionClient = void 0; | ||
const lambda_1 = __importDefault(require("aws-sdk/clients/lambda")); | ||
const lambda_1 = require("../utils/lambda"); | ||
const rate_limit_1 = require("../utils/rate-limit"); | ||
@@ -27,14 +24,23 @@ const time_1 = require("../utils/time"); | ||
this.arn = arn; | ||
const creds = credentials ? JSON.parse(credentials) : undefined; | ||
this.invocationRateLimit = rate_limit_1.rateLimitModule.createCounterFor(arn, 300); | ||
this.lambda = new lambda_1.default(creds | ||
? { | ||
credentials: { | ||
accessKeyId: creds.AccessKeyId, | ||
secretAccessKey: creds.SecretAccessKey, | ||
sessionToken: creds.SessionToken, | ||
}, | ||
} | ||
: undefined); | ||
this.lambda = (0, lambda_1.getLambdaFromCredentials)(credentials); | ||
} | ||
async invoke(FunctionName, Payload) { | ||
if ((0, lambda_1.isLambdaV3)(this.lambda)) { | ||
return this.lambda.invoke({ | ||
FunctionName, | ||
Payload, | ||
InvocationType: 'RequestResponse', | ||
}); | ||
} | ||
else { | ||
return this.lambda | ||
.invoke({ | ||
FunctionName, | ||
Payload, | ||
InvocationType: 'RequestResponse', | ||
}) | ||
.promise(); | ||
} | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
@@ -45,15 +51,11 @@ async execute(request) { | ||
this.invocationRateLimit.incrementRateFor(invocationTimeStamp); | ||
const invocationRequestResult = await this.lambda | ||
.invoke({ | ||
FunctionName: this.arn, | ||
Payload: JSON.stringify(request), | ||
InvocationType: 'RequestResponse', | ||
}) | ||
.promise(); | ||
const invocationRequestResult = await this.invoke(this.arn, JSON.stringify(request)); | ||
if (invocationRequestResult.FunctionError) { | ||
throw new Error(`Error while attempting request: ${cleanError(invocationRequestResult.Payload)}`); | ||
} | ||
return JSON.parse(invocationRequestResult.Payload); | ||
return JSON.parse((0, lambda_1.isLambdaV3)(this.lambda) | ||
? invocationRequestResult.Payload.transformToString() | ||
: invocationRequestResult.Payload); | ||
} | ||
} | ||
exports.BaseActionClient = BaseActionClient; |
/// <reference types="node" /> | ||
import { AxiosInstance } from 'axios'; | ||
import { AxiosError, AxiosInstance } from 'axios'; | ||
import https from 'https'; | ||
export type RetryConfig = { | ||
retries: number; | ||
retryDelay: (retryCount: number, error: AxiosError) => number; | ||
retryCondition?: (error: AxiosError) => boolean | Promise<boolean>; | ||
}; | ||
type ApiFunction<TResponse> = (api: AxiosInstance) => Promise<TResponse>; | ||
export declare abstract class BaseApiClient { | ||
@@ -10,2 +16,3 @@ private api; | ||
private httpsAgent?; | ||
private retryConfig; | ||
protected abstract getPoolId(): string; | ||
@@ -18,2 +25,3 @@ protected abstract getPoolClientId(): string; | ||
httpsAgent?: https.Agent; | ||
retryConfig?: Partial<RetryConfig>; | ||
}); | ||
@@ -24,4 +32,7 @@ protected init(): Promise<AxiosInstance>; | ||
}): Promise<AxiosInstance>; | ||
protected apiCall<T>(fn: (api: AxiosInstance) => Promise<T>): Promise<T>; | ||
private withRetry; | ||
protected apiCall<TResponse>(apiFunction: ApiFunction<TResponse>): Promise<TResponse>; | ||
} | ||
export declare const exponentialDelay: (retryNumber?: number, _error?: AxiosError | undefined, delayFactor?: number) => number; | ||
export {}; | ||
//# sourceMappingURL=client.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseApiClient = void 0; | ||
exports.exponentialDelay = exports.BaseApiClient = void 0; | ||
const api_1 = require("./api"); | ||
const auth_1 = require("./auth"); | ||
const time_1 = require("../utils/time"); | ||
class BaseApiClient { | ||
@@ -15,2 +16,3 @@ constructor(params) { | ||
this.httpsAgent = params.httpsAgent; | ||
this.retryConfig = { retries: 3, retryDelay: exports.exponentialDelay, ...params.retryConfig }; | ||
} | ||
@@ -41,29 +43,48 @@ async init() { | ||
} | ||
// prettier-ignore | ||
async apiCall(fn) { | ||
const api = await this.init(); | ||
async withRetry(axiosInstance, apiFunction, { retryCount, retryDelay } = { retryCount: 0, retryDelay: 0 }) { | ||
try { | ||
return await fn(api); | ||
await (0, time_1.sleep)(retryDelay); | ||
return await apiFunction(axiosInstance); | ||
} | ||
catch (error) { | ||
// this means ID token has expired so we'll recreate session and try again | ||
if (error.response && error.response.status === 401 && error.response.statusText === 'Unauthorized') { | ||
if (isAuthenticationError(error)) { | ||
this.api = undefined; | ||
const api = await this.refresh(); | ||
return await fn(api); | ||
return await this.withRetry(api, apiFunction, { retryCount, retryDelay }); | ||
} | ||
// Cloudflare error | ||
if (error.response && error.response.status === 520 && error.response.data.includes('Cloudflare')) { | ||
this.api = undefined; | ||
const headersOverride = { | ||
'Connection': 'upgrade', | ||
'Upgrade': 'HTTP/2.0' | ||
}; | ||
const api = await this.refresh({ headers: headersOverride }); | ||
return await fn(api); | ||
const updatedRetryState = { | ||
retryCount: retryCount + 1, | ||
retryDelay: this.retryConfig.retryDelay(retryCount + 1, error), | ||
}; | ||
if (updatedRetryState.retryCount > this.retryConfig.retries) | ||
throw error; | ||
if (isCloudFlareError(error)) { | ||
const apiWithUpgradeHeaders = await this.refresh({ | ||
headers: { | ||
Connection: 'upgrade', | ||
Upgrade: 'HTTP/2.0', | ||
}, | ||
}); | ||
return await this.withRetry(apiWithUpgradeHeaders, apiFunction, updatedRetryState); | ||
} | ||
if (await (this.retryConfig?.retryCondition?.(error) ?? true)) | ||
await this.withRetry(axiosInstance, apiFunction, updatedRetryState); | ||
throw error; | ||
} | ||
} | ||
// prettier-ignore | ||
async apiCall(apiFunction) { | ||
const api = await this.init(); | ||
return this.withRetry(api, apiFunction); | ||
} | ||
} | ||
exports.BaseApiClient = BaseApiClient; | ||
const isAuthenticationError = (axiosError) => axiosError.response?.status === 401 && axiosError.response?.statusText === 'Unauthorized'; | ||
const isCloudFlareError = (axiosError) => axiosError.response?.status === 520 && (axiosError.response?.data).includes('Cloudflare'); | ||
const exponentialDelay = (retryNumber = 0, _error = undefined, delayFactor = 100) => { | ||
const delay = 2 ** retryNumber * delayFactor; | ||
const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay | ||
return delay + randomSum; | ||
}; | ||
exports.exponentialDelay = exponentialDelay; |
export { createApi, createAuthenticatedApi } from './api/api'; | ||
export { authenticate } from './api/auth'; | ||
export { BaseApiClient } from './api/client'; | ||
export { BaseApiClient, RetryConfig } from './api/client'; | ||
export * from './utils/network'; | ||
@@ -5,0 +5,0 @@ export declare const VERSION: any; |
export type Network = SupportedNetwork | TenantNetwork; | ||
export type SupportedNetwork = PublicNetwork | CustomNetwork; | ||
export type PublicNetwork = 'mainnet' | 'sepolia' | 'holesky' | 'goerli' | 'xdai' | 'sokol' | 'fuse' | 'bsc' | 'bsctest' | 'fantom' | 'fantomtest' | 'moonbase' | 'moonriver' | 'moonbeam' | 'matic' | 'mumbai' | 'amoy' | 'matic-zkevm' | 'matic-zkevm-testnet' | 'avalanche' | 'fuji' | 'arbitrum' | 'arbitrum-nova' | 'arbitrum-goerli' | 'arbitrum-sepolia' | 'optimism' | 'optimism-sepolia' | 'celo' | 'alfajores' | 'harmony-s0' | 'harmony-test-s0' | 'aurora' | 'auroratest' | 'hedera' | 'hederatest' | 'zksync' | 'zksync-goerli' | 'zksync-sepolia' | 'base' | 'base-goerli' | 'base-sepolia' | 'linea-goerli' | 'linea' | 'mantle' | 'scroll' | 'scroll-sepolia' | 'meld' | 'meld-kanazawa'; | ||
export type PublicNetwork = 'mainnet' | 'sepolia' | 'holesky' | 'xdai' | 'sokol' | 'fuse' | 'bsc' | 'bsctest' | 'fantom' | 'fantomtest' | 'moonbase' | 'moonriver' | 'moonbeam' | 'matic' | 'mumbai' | 'amoy' | 'matic-zkevm' | 'matic-zkevm-testnet' | 'avalanche' | 'fuji' | 'arbitrum' | 'arbitrum-nova' | 'arbitrum-sepolia' | 'optimism' | 'optimism-sepolia' | 'celo' | 'alfajores' | 'harmony-s0' | 'harmony-test-s0' | 'aurora' | 'auroratest' | 'hedera' | 'hederatest' | 'zksync' | 'zksync-sepolia' | 'base' | 'base-sepolia' | 'linea-goerli' | 'linea' | 'mantle' | 'scroll' | 'scroll-sepolia' | 'meld' | 'meld-kanazawa'; | ||
export type CustomNetwork = 'x-dfk-avax-chain' | 'x-dfk-avax-chain-test' | 'x-security-alliance'; | ||
@@ -5,0 +5,0 @@ export type TenantNetwork = string; |
@@ -9,3 +9,2 @@ "use strict"; | ||
'holesky', | ||
'goerli', | ||
'xdai', | ||
@@ -30,3 +29,2 @@ 'sokol', | ||
'arbitrum-nova', | ||
'arbitrum-goerli', | ||
'arbitrum-sepolia', | ||
@@ -44,6 +42,4 @@ 'optimism', | ||
'zksync', | ||
'zksync-goerli', | ||
'zksync-sepolia', | ||
'base', | ||
'base-goerli', | ||
'base-sepolia', | ||
@@ -77,3 +73,2 @@ 'linea-goerli', | ||
'holesky': 17000, | ||
'goerli': 5, | ||
'xdai': 100, | ||
@@ -100,3 +95,2 @@ 'sokol': 77, | ||
'arbitrum-nova': 42170, | ||
'arbitrum-goerli': 421613, | ||
'arbitrum-sepolia': 421614, | ||
@@ -112,6 +106,4 @@ 'celo': 42220, | ||
'zksync': 324, | ||
'zksync-goerli': 280, | ||
'zksync-sepolia': 300, | ||
'base': 8453, | ||
'base-goerli': 84531, | ||
'base-sepolia': 84532, | ||
@@ -118,0 +110,0 @@ 'linea': 59144, |
export declare const getTimestampInSeconds: () => number; | ||
export declare const sleep: (millisecond: number) => Promise<unknown>; | ||
//# sourceMappingURL=time.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTimestampInSeconds = void 0; | ||
exports.sleep = exports.getTimestampInSeconds = void 0; | ||
const getTimestampInSeconds = () => Math.floor(Date.now() / 1000); | ||
exports.getTimestampInSeconds = getTimestampInSeconds; | ||
const sleep = (millisecond) => new Promise((resolve) => setTimeout(resolve, millisecond)); | ||
exports.sleep = sleep; |
{ | ||
"name": "@openzeppelin/defender-sdk-base-client", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"description": "", | ||
@@ -17,2 +17,3 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@aws-sdk/client-lambda": "^3.563.0", | ||
"@types/async-retry": "^1.4.4", | ||
@@ -19,0 +20,0 @@ "aws-sdk": "^2.1589.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
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
74125
34
622
3