@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
53 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AwsClientPool = void 0;
const node_http_handler_1 = require("@smithy/node-http-handler");
const util_retry_1 = require("@smithy/util-retry");
class AwsClientPool {
static defaultInstance = new AwsClientPool();
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: util_retry_1.RETRY_MODES.ADAPTIVE,
requestHandler: new node_http_handler_1.NodeHttpHandler({
connectionTimeout: 5_000,
socketTimeout: 15_000
})
});
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();
}
}
exports.AwsClientPool = AwsClientPool;
//# sourceMappingURL=ClientPool.js.map