@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
76 lines (73 loc) • 3.48 kB
JavaScript
import { __decorate, __param, __metadata } from '../../../../../external/tslib/tslib.es6.js';
import { ApiAuthorizationIamAttachmentCache } from './cache.class.js';
import { AUTHORIZATION_POLICY_ATTACHMENT_SOURCES_TOKEN } from '../../../../../constant/class/authorization/policy/attachment-sources-token.constant.js';
import { Injectable, Inject, Optional } from '@nestjs/common';
import { LoggerUtility } from '../../../../../utility/logger.utility.js';
const iamAttachmentResolverLogger = LoggerUtility.getLogger("ApiAuthorizationIamAttachmentResolver");
let ApiAuthorizationIamAttachmentResolver = class ApiAuthorizationIamAttachmentResolver {
cache;
sources;
constructor(cache, sources = []) {
this.cache = cache;
this.sources = sources;
}
clear() {
this.cache.clear();
}
async resolve(principal) {
const cachedAttachments = this.cache.get(principal);
if (cachedAttachments) {
iamAttachmentResolverLogger.verbose(`Using cached IAM attachments for principal "${principal.id}" (${cachedAttachments.attachments.length} attachments, ${cachedAttachments.boundaries.length} boundaries).`);
return cachedAttachments;
}
const attachments = new Map();
const boundaries = new Map();
for (const source of this.sources) {
const resolvedAttachments = await source.getAttachments(principal);
for (const attachment of resolvedAttachments.attachments) {
attachments.set(this.createAttachmentKey(attachment), attachment);
}
for (const boundary of resolvedAttachments.boundaries) {
boundaries.set(this.createAttachmentKey(boundary), boundary);
}
}
const result = {
attachments: [...attachments.values()],
boundaries: [...boundaries.values()],
};
this.cache.set(principal, result);
iamAttachmentResolverLogger.verbose(`Resolved ${result.attachments.length} IAM attachments and ${result.boundaries.length} boundaries for principal "${principal.id}" from ${this.sources.length} sources.`);
return result;
}
createAttachmentKey(attachment) {
return JSON.stringify({
metadata: this.normalizeAttachmentValue(attachment.metadata ?? {}),
policyId: attachment.policyId,
principalId: attachment.principalId,
principalType: attachment.principalType,
});
}
normalizeAttachmentValue(value) {
if (Array.isArray(value)) {
return value.map((item) => this.normalizeAttachmentValue(item));
}
if (!value || typeof value !== "object") {
return value;
}
const normalizedValue = {};
const recordValue = value;
const sortedKeys = Object.keys(recordValue).toSorted((left, right) => left.localeCompare(right));
for (const key of sortedKeys) {
normalizedValue[key] = this.normalizeAttachmentValue(recordValue[key]);
}
return normalizedValue;
}
};
ApiAuthorizationIamAttachmentResolver = __decorate([
Injectable(),
__param(1, Inject(AUTHORIZATION_POLICY_ATTACHMENT_SOURCES_TOKEN)),
__param(1, Optional()),
__metadata("design:paramtypes", [ApiAuthorizationIamAttachmentCache, Array])
], ApiAuthorizationIamAttachmentResolver);
export { ApiAuthorizationIamAttachmentResolver };
//# sourceMappingURL=resolver.class.js.map