@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
69 lines • 2.26 kB
JavaScript
import { createHash } from 'crypto';
import {} from './PathBasedPersistenceAdapter.js';
export class InMemoryPathBasedPersistenceAdapter {
constructor() {
this.fileSystem = {};
}
async writeFile(filePath, data) {
this.fileSystem[filePath] = data.toString();
}
async writeWithOptimisticLock(filePath, data, lockId) {
const currentData = await this.readFileWithHash(filePath);
if (currentData && currentData.hash !== lockId) {
return false;
}
await this.writeFile(filePath, data);
return true;
}
async readFile(filePath) {
return this.fileSystem[filePath];
}
async readFileWithHash(filePath) {
const contents = this.fileSystem[filePath];
if (!contents) {
return undefined;
}
const hash = createHash('sha256');
hash.update(contents);
return {
data: contents,
hash: hash.digest('hex')
};
}
async deleteFile(filePath) {
delete this.fileSystem[filePath];
}
async deleteDirectory(dirPath) {
for (const key in this.fileSystem) {
if (key.startsWith(dirPath + '/')) {
delete this.fileSystem[key];
}
}
}
async listDirectory(dirPath) {
const keys = Object.keys(this.fileSystem).filter((key) => key.startsWith(dirPath + '/'));
const allMatches = new Set(keys.map((key) => key
.slice(dirPath.length + 1)
.split('/')
.at(0)));
return Array.from(allMatches);
}
async findWithPattern(baseDir, pathParts, filename) {
//convert the parts to a regex where some parts may have "*"
const regexParts = pathParts.map((part) => {
if (part === '*') {
return '[^/]+';
}
return part;
});
const regex = new RegExp(`^${baseDir}/(${regexParts.join('/')})/${filename}$`);
const result = [];
for (const key in this.fileSystem) {
if (regex.test(key)) {
result.push(this.fileSystem[key]);
}
}
return result;
}
}
//# sourceMappingURL=InMemoryPathBasedPersistenceAdapter.js.map