UNPKG

@cloud-copilot/iam-data

Version:
55 lines 1.82 kB
import { readRelativeFile } from './readRelativeFile.js'; const dataCache = {}; const requestCache = {}; export async function readDataFile(...pathParts) { pathParts.unshift('data'); const file = pathParts.join('/'); // If the data is already cached, return it immediately. if (dataCache[file]) { return dataCache[file]; } // If there's an ongoing request, return the existing Promise. if (requestCache[file] !== undefined) { return requestCache[file]; } // Create a new Promise and store it in dataQueues synchronously. requestCache[file] = (async () => { try { const data = await readRelativeFile(pathParts); dataCache[file] = data; // Cache the fetched data. return data; } finally { delete requestCache[file]; // Clean up the queue regardless of success or failure. } })(); return requestCache[file]; } /** * Read the action data for a service * * @param serviceKey the service key to read the action data for * @returns the action data for the service */ export async function readActionData(serviceKey) { return readDataFile('actions', `${serviceKey}.json`); } /** * Read the condition key data for a service * * @param serviceKey the service key to read the condition key data for * @returns the condition key data */ export async function readConditionKeys(serviceKey) { return readDataFile('conditionKeys', `${serviceKey}.json`); } /** * Read the resource type data for a service * * @param serviceKey the service key to read the resource type data for * @returns the resource type data */ export async function readResourceTypes(serviceKey) { return readDataFile('resourceTypes', `${serviceKey}.json`); } //# sourceMappingURL=data.js.map