diff --git a/components/client/typescript/src/predicates.ts b/components/client/typescript/src/predicates.ts index b56cc3c63..cbdef141d 100644 --- a/components/client/typescript/src/predicates.ts +++ b/components/client/typescript/src/predicates.ts @@ -1,4 +1,4 @@ -import * as fs from 'fs'; +import * as fs from 'fs/promises'; import * as path from 'path'; import { logger } from './util/logger'; import { @@ -17,17 +17,32 @@ const RegisteredPredicates = new Map(); const CompiledPredicateSchema = TypeCompiler.Compile(PredicateSchema); +// Async version of fs.existsSync +async function pathExists(path: string): Promise { + try { + await fs.access(path); + return true; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + return false; + } + throw error; // Re-throw other errors (e.g., permission issues) + } +} + /** * Looks on disk and returns a map of registered Predicates, where the key is the predicate `name` * as defined by the user. */ -export function recallPersistedPredicatesFromDisk(basePath: string): Map { +export async function recallPersistedPredicatesFromDisk( + basePath: string +): Promise> { RegisteredPredicates.clear(); try { - if (!fs.existsSync(basePath)) return RegisteredPredicates; - for (const file of fs.readdirSync(basePath)) { + if (!(await pathExists(basePath))) return RegisteredPredicates; + for (const file of await fs.readdir(basePath)) { if (file.endsWith('.json')) { - const text = fs.readFileSync(path.join(basePath, file), 'utf-8'); + const text = await fs.readFile(path.join(basePath, file), 'utf-8'); const predicate = JSON.parse(text) as JSON; if (CompiledPredicateSchema.Check(predicate)) { logger.info( @@ -44,11 +59,11 @@ export function recallPersistedPredicatesFromDisk(basePath: string): Map { await server.start([testPredicate], async () => {}); expect(fs.existsSync(`${observer.predicate_disk_file_path}/predicate-test.json`)).toBe(true); - const disk = recallPersistedPredicatesFromDisk(observer.predicate_disk_file_path); + const disk = await recallPersistedPredicatesFromDisk(observer.predicate_disk_file_path); const storedPredicate = disk.get('test'); expect(storedPredicate).not.toBeUndefined(); expect(storedPredicate?.name).toBe(testPredicate.name); @@ -102,8 +102,8 @@ describe('predicates', () => { }); describe('pre-stored', () => { - beforeEach(() => { - savePredicateToDisk(observer.predicate_disk_file_path, { + beforeEach(async () => { + await savePredicateToDisk(observer.predicate_disk_file_path, { uuid: 'e2777d77-473a-4c1d-9012-152deb36bf4c', name: 'test', version: 1, @@ -164,7 +164,7 @@ describe('predicates', () => { mockAgent.assertNoPendingInterceptors(); expect(fs.existsSync(`${observer.predicate_disk_file_path}/predicate-test.json`)).toBe(true); - const disk = recallPersistedPredicatesFromDisk(observer.predicate_disk_file_path); + const disk = await recallPersistedPredicatesFromDisk(observer.predicate_disk_file_path); const storedPredicate = disk.get('test'); // Should have a different uuid expect(storedPredicate?.uuid).not.toBe('e2777d77-473a-4c1d-9012-152deb36bf4c');