@message-queue-toolkit/sns
Version:
SNS adapter for message-queue-toolkit
35 lines • 1.53 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearCachedCallerIdentity = exports.buildTopicArn = void 0;
const client_sts_1 = require("@aws-sdk/client-sts");
let callerIdentityPromise;
let callerIdentityCached;
/**
* Manually builds the ARN of a topic based on the current AWS account and the topic name.
* It follows the following pattern: arn:aws:sns:<region>:<account-id>:<topic-name>
* Doc -> https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html
*/
const buildTopicArn = async (client, topicName) => {
const identityResponse = await getAndCacheCallerIdentity(client);
const region = typeof client.config.region === 'string' ? client.config.region : await client.config.region();
return `arn:aws:sns:${region}:${identityResponse.Account}:${topicName}`;
};
exports.buildTopicArn = buildTopicArn;
const clearCachedCallerIdentity = () => {
callerIdentityPromise = undefined;
callerIdentityCached = undefined;
};
exports.clearCachedCallerIdentity = clearCachedCallerIdentity;
const getAndCacheCallerIdentity = async (client) => {
if (!callerIdentityCached) {
if (!callerIdentityPromise) {
callerIdentityPromise = client.send(new client_sts_1.GetCallerIdentityCommand({})).then((response) => {
callerIdentityCached = response;
});
}
await callerIdentityPromise;
callerIdentityPromise = undefined;
}
return callerIdentityCached;
};
//# sourceMappingURL=stsUtils.js.map
;