UNPKG

@cloud-copilot/iam-collect

Version:

Collect IAM information from AWS Accounts

51 lines 2 kB
import { NodeHttpHandler } from '@smithy/node-http-handler'; import { RETRY_MODES } from '@smithy/util-retry'; export class AwsClientPool { constructor() { this.clientCache = new Map(); } /** * Returns a client of the specified type with the specified credentials and region. * Will create a new client if one does not already exist in the cache. * * @param ClientType The client constructor to create an instance of. * @param credentials The credentials to use for the client. * @param region The region to use for the client. * @returns A client of the specified type with the specified credentials and region. */ client(ClientType, credentials, region, endpoint) { const cacheKey = this.getCacheKey(ClientType, credentials, region, endpoint); if (!this.clientCache.has(cacheKey)) { const client = new ClientType({ credentials, region, maxAttempts: 10, retryMode: RETRY_MODES.ADAPTIVE, requestHandler: new NodeHttpHandler({ connectionTimeout: 5000, socketTimeout: 15000 }) }); this.clientCache.set(cacheKey, client); } return this.clientCache.get(cacheKey); } getCacheKey(ClientType, credentials, region, endpoint) { return `${ClientType.name}:${credentials.accountId}:${credentials.accessKeyId}:${region}:${endpoint}`; } /** * Destroys all clients in the pool and empties the cache. * * NOT THREAD SAFE, this should only be called when all other operations are complete. */ clear() { this.clientCache.forEach((client) => { if (typeof client.destroy === 'function') { client.destroy(); } }); this.clientCache.clear(); } } AwsClientPool.defaultInstance = new AwsClientPool(); //# sourceMappingURL=ClientPool.js.map