@@ -14,6 +14,36 @@ const path = require('path');
1414
1515// Cache configuration
1616const CACHE_TTL_MS = 5 * 60 * 1000 ; // 5 minutes
17+ const REGISTRY_RELATIVE_PATH = path . join ( '.aiox-core' , 'core' , 'registry' , 'service-registry.json' ) ;
18+ const PACKAGE_REGISTRY_PATH = path . join ( __dirname , 'service-registry.json' ) ;
19+
20+ function uniquePaths ( paths ) {
21+ return Array . from ( new Set ( paths ) ) ;
22+ }
23+
24+ function getDefaultRegistryPaths ( ) {
25+ return uniquePaths ( [
26+ path . join ( process . cwd ( ) , REGISTRY_RELATIVE_PATH ) ,
27+ PACKAGE_REGISTRY_PATH ,
28+ ] ) ;
29+ }
30+
31+ async function readRegistryFile ( paths ) {
32+ const errors = [ ] ;
33+
34+ for ( const registryPath of paths ) {
35+ try {
36+ return {
37+ content : await fs . readFile ( registryPath , 'utf8' ) ,
38+ registryPath,
39+ } ;
40+ } catch ( error ) {
41+ errors . push ( `${ registryPath } : ${ error . message } ` ) ;
42+ }
43+ }
44+
45+ throw new Error ( `Failed to load registry: ${ errors . join ( '; ' ) } ` ) ;
46+ }
1747
1848/**
1949 * Service Registry class with caching
@@ -24,6 +54,7 @@ class ServiceRegistry {
2454 this . cache = null ;
2555 this . cacheTimestamp = 0 ;
2656 this . cacheTTL = options . cacheTTL || CACHE_TTL_MS ;
57+ this . resolvedRegistryPath = null ;
2758
2859 // Indexed lookups (built on load)
2960 this . _byId = new Map ( ) ;
@@ -45,16 +76,15 @@ class ServiceRegistry {
4576 return this . cache ;
4677 }
4778
48- // Determine registry path
49- const registryPath = this . registryPath ||
50- path . join ( process . cwd ( ) , '.aiox-core/core/registry/service-registry.json' ) ;
79+ const registryPaths = this . registryPath ? [ this . registryPath ] : getDefaultRegistryPaths ( ) ;
5180
5281 const startTime = Date . now ( ) ;
5382
5483 try {
55- const content = await fs . readFile ( registryPath , 'utf8' ) ;
84+ const { content, registryPath } = await readRegistryFile ( registryPaths ) ;
5685 this . cache = JSON . parse ( content ) ;
5786 this . cacheTimestamp = now ;
87+ this . resolvedRegistryPath = registryPath ;
5888
5989 // Build indexes
6090 this . _buildIndexes ( ) ;
@@ -66,6 +96,9 @@ class ServiceRegistry {
6696
6797 return this . cache ;
6898 } catch ( error ) {
99+ if ( error . message . startsWith ( 'Failed to load registry:' ) ) {
100+ throw error ;
101+ }
69102 throw new Error ( `Failed to load registry: ${ error . message } ` ) ;
70103 }
71104 }
@@ -276,6 +309,7 @@ class ServiceRegistry {
276309 clearCache ( ) {
277310 this . cache = null ;
278311 this . cacheTimestamp = 0 ;
312+ this . resolvedRegistryPath = null ;
279313 this . _byId . clear ( ) ;
280314 this . _byCategory . clear ( ) ;
281315 this . _byTag . clear ( ) ;
@@ -290,6 +324,7 @@ class ServiceRegistry {
290324 return {
291325 cached : this . cache !== null ,
292326 cacheAge : this . cache ? Date . now ( ) - this . cacheTimestamp : null ,
327+ registryPath : this . resolvedRegistryPath ,
293328 workerCount : this . _byId . size ,
294329 categoryCount : this . _byCategory . size ,
295330 tagCount : this . _byTag . size ,
@@ -326,5 +361,6 @@ async function loadRegistry(registryPath = null) {
326361module . exports = {
327362 ServiceRegistry,
328363 getRegistry,
364+ getDefaultRegistryPaths,
329365 loadRegistry,
330366} ;
0 commit comments