@fjell/registry 4.4.17 → 4.4.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/build.js +4 -0
  2. package/dist/Coordinate.d.ts +2 -1
  3. package/dist/Coordinate.d.ts.map +1 -0
  4. package/dist/Instance.d.ts +3 -2
  5. package/dist/Instance.d.ts.map +1 -0
  6. package/dist/Registry.d.ts +1 -0
  7. package/dist/Registry.d.ts.map +1 -0
  8. package/dist/RegistryHub.d.ts +1 -0
  9. package/dist/RegistryHub.d.ts.map +1 -0
  10. package/dist/RegistryStats.d.ts +1 -0
  11. package/dist/RegistryStats.d.ts.map +1 -0
  12. package/dist/errors/CoordinateError.d.ts +1 -0
  13. package/dist/errors/CoordinateError.d.ts.map +1 -0
  14. package/dist/errors/InstanceError.d.ts +1 -0
  15. package/dist/errors/InstanceError.d.ts.map +1 -0
  16. package/dist/errors/RegistryError.d.ts +1 -0
  17. package/dist/errors/RegistryError.d.ts.map +1 -0
  18. package/dist/errors/RegistryHubError.d.ts +1 -0
  19. package/dist/errors/RegistryHubError.d.ts.map +1 -0
  20. package/dist/errors/index.d.ts +1 -0
  21. package/dist/errors/index.d.ts.map +1 -0
  22. package/dist/index.d.ts +1 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +685 -10
  25. package/dist/index.js.map +7 -0
  26. package/dist/logger.d.ts +2 -1
  27. package/dist/logger.d.ts.map +1 -0
  28. package/dist/types.d.ts +1 -0
  29. package/dist/types.d.ts.map +1 -0
  30. package/docs/docs.config.ts +1 -32
  31. package/docs/package-lock.json +5129 -0
  32. package/docs/package.json +2 -2
  33. package/docs/src/types.d.ts +13 -5
  34. package/docs/timing-range.svg +40 -40
  35. package/package.json +21 -33
  36. package/tsconfig.docs.json +28 -0
  37. package/vitest.config.ts +17 -0
  38. package/MIGRATION_SUMMARY.md +0 -200
  39. package/dist/Coordinate.cjs +0 -32
  40. package/dist/Coordinate.js +0 -28
  41. package/dist/Instance.cjs +0 -24
  42. package/dist/Instance.js +0 -19
  43. package/dist/Registry.cjs +0 -182
  44. package/dist/Registry.js +0 -178
  45. package/dist/RegistryHub.cjs +0 -94
  46. package/dist/RegistryHub.js +0 -90
  47. package/dist/RegistryStats.cjs +0 -198
  48. package/dist/RegistryStats.js +0 -194
  49. package/dist/errors/CoordinateError.cjs +0 -70
  50. package/dist/errors/CoordinateError.js +0 -63
  51. package/dist/errors/InstanceError.cjs +0 -101
  52. package/dist/errors/InstanceError.js +0 -92
  53. package/dist/errors/RegistryError.cjs +0 -82
  54. package/dist/errors/RegistryError.js +0 -75
  55. package/dist/errors/RegistryHubError.cjs +0 -92
  56. package/dist/errors/RegistryHubError.js +0 -84
  57. package/dist/index.cjs +0 -821
  58. package/dist/index.cjs.map +0 -1
  59. package/dist/logger.cjs +0 -10
  60. package/dist/logger.js +0 -6
package/dist/index.cjs DELETED
@@ -1,821 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const Logging = require('@fjell/logging');
6
-
7
- const LibLogger = Logging.getLogger('@fjell/registry');
8
-
9
- const logger$3 = LibLogger.get("Coordinate");
10
- const createCoordinate = (kta, scopes = [])=>{
11
- const ktArray = Array.isArray(kta) ? kta : [
12
- kta
13
- ];
14
- const toString = ()=>{
15
- logger$3.debug("toString", {
16
- kta,
17
- scopes
18
- });
19
- return `${ktArray.join(', ')} - ${scopes.join(', ')}`;
20
- };
21
- logger$3.debug("createCoordinate", {
22
- kta: ktArray,
23
- scopes,
24
- toString
25
- });
26
- return {
27
- kta: ktArray,
28
- scopes,
29
- toString
30
- };
31
- };
32
-
33
- const logger$2 = LibLogger.get("Instance");
34
- const createInstance = (registry, coordinate)=>{
35
- logger$2.debug("createInstance", {
36
- coordinate,
37
- registry
38
- });
39
- return {
40
- coordinate,
41
- registry
42
- };
43
- };
44
- const isInstance = (instance)=>{
45
- return instance !== null && instance !== undefined && instance.coordinate !== undefined && instance.registry !== undefined;
46
- };
47
-
48
- function _define_property$4(obj, key, value) {
49
- if (key in obj) {
50
- Object.defineProperty(obj, key, {
51
- value: value,
52
- enumerable: true,
53
- configurable: true,
54
- writable: true
55
- });
56
- } else {
57
- obj[key] = value;
58
- }
59
- return obj;
60
- }
61
- /**
62
- * Internal class for tracking Registry statistics with complex coordinate combinations and client tracking
63
- */ class RegistryStats {
64
- /**
65
- * Records a get() call for the specified coordinate and client
66
- */ recordGetCall(kta, scopes, client) {
67
- this.totalCalls++;
68
- const ktaKey = kta.join('.');
69
- const scopeKey = this.createScopeKey(scopes || []);
70
- const clientKey = this.createClientKey(client);
71
- if (!this.coordinateCalls.has(ktaKey)) {
72
- this.coordinateCalls.set(ktaKey, new Map());
73
- }
74
- const scopeMap = this.coordinateCalls.get(ktaKey);
75
- if (!scopeMap.has(scopeKey)) {
76
- scopeMap.set(scopeKey, new Map());
77
- }
78
- const clientMap = scopeMap.get(scopeKey);
79
- const currentCount = clientMap.get(clientKey) || 0;
80
- clientMap.set(clientKey, currentCount + 1);
81
- }
82
- /**
83
- * Gets the current statistics snapshot
84
- */ getStatistics() {
85
- const coordinateCallRecords = [];
86
- let serviceCalls = 0;
87
- let applicationCalls = 0;
88
- let unidentifiedCalls = 0;
89
- for (const [ktaKey, scopeMap] of this.coordinateCalls){
90
- for (const [scopeKey, clientMap] of scopeMap){
91
- const clientCalls = [];
92
- let totalCount = 0;
93
- for (const [clientKey, count] of clientMap){
94
- const client = this.parseClientKey(clientKey);
95
- if (client !== null) {
96
- clientCalls.push({
97
- client,
98
- count
99
- });
100
- }
101
- totalCount += count;
102
- // Update client summary
103
- if (clientKey === '__no_client__') {
104
- unidentifiedCalls += count;
105
- } else if (typeof client === 'string') {
106
- applicationCalls += count;
107
- } else if (client !== null) {
108
- serviceCalls += count;
109
- }
110
- }
111
- coordinateCallRecords.push({
112
- kta: ktaKey.split('.'),
113
- scopes: this.parseScopeKey(scopeKey),
114
- count: totalCount,
115
- clientCalls: [
116
- ...clientCalls
117
- ] // Return a copy
118
- });
119
- }
120
- }
121
- return {
122
- totalGetCalls: this.totalCalls,
123
- coordinateCallRecords: [
124
- ...coordinateCallRecords
125
- ],
126
- clientSummary: {
127
- serviceCalls,
128
- applicationCalls,
129
- unidentifiedCalls
130
- }
131
- };
132
- }
133
- /**
134
- * Gets call count for a specific coordinate combination
135
- */ getCallCount(kta, scopes) {
136
- const ktaKey = kta.join('.');
137
- const scopeKey = this.createScopeKey(scopes || []);
138
- const scopeMap = this.coordinateCalls.get(ktaKey);
139
- if (!scopeMap) return 0;
140
- const clientMap = scopeMap.get(scopeKey);
141
- if (!clientMap) return 0;
142
- let total = 0;
143
- for (const count of clientMap.values()){
144
- total += count;
145
- }
146
- return total;
147
- }
148
- /**
149
- * Gets call count for a specific coordinate combination from a specific client
150
- */ getCallCountByClient(kta, scopes, client) {
151
- const ktaKey = kta.join('.');
152
- const scopeKey = this.createScopeKey(scopes || []);
153
- const clientKey = this.createClientKey(client);
154
- const scopeMap = this.coordinateCalls.get(ktaKey);
155
- if (!scopeMap) return 0;
156
- const clientMap = scopeMap.get(scopeKey);
157
- if (!clientMap) return 0;
158
- return clientMap.get(clientKey) || 0;
159
- }
160
- /**
161
- * Gets total calls for a specific kta (across all scopes)
162
- */ getTotalCallsForKta(kta) {
163
- const ktaKey = kta.join('.');
164
- const scopeMap = this.coordinateCalls.get(ktaKey);
165
- if (!scopeMap) return 0;
166
- let total = 0;
167
- for (const clientMap of scopeMap.values()){
168
- for (const count of clientMap.values()){
169
- total += count;
170
- }
171
- }
172
- return total;
173
- }
174
- /**
175
- * Gets all unique kta paths that have been called
176
- */ getCalledKtaPaths() {
177
- const ktaPaths = [];
178
- for (const ktaKey of this.coordinateCalls.keys()){
179
- ktaPaths.push(ktaKey.split('.'));
180
- }
181
- return ktaPaths;
182
- }
183
- /**
184
- * Creates a normalized scope key from scopes array
185
- */ createScopeKey(scopes) {
186
- if (scopes.length === 0) return '__no_scopes__';
187
- return [
188
- ...scopes
189
- ].sort().join(',');
190
- }
191
- /**
192
- * Parses a scope key back to scopes array
193
- */ parseScopeKey(scopeKey) {
194
- if (scopeKey === '__no_scopes__') return [];
195
- return scopeKey.split(',');
196
- }
197
- /**
198
- * Creates a normalized client key from client identifier
199
- */ createClientKey(client) {
200
- if (!client) return '__no_client__';
201
- if (typeof client === 'string') {
202
- return `app:${client}`;
203
- }
204
- // Service client
205
- const coordKey = `${client.coordinate.kta.join('.')};${this.createScopeKey(client.coordinate.scopes)}`;
206
- return `service:${client.registryType}:${coordKey}`;
207
- }
208
- /**
209
- * Parses a client key back to client identifier
210
- */ parseClientKey(clientKey) {
211
- if (clientKey === '__no_client__') return null;
212
- if (clientKey.startsWith('app:')) {
213
- return clientKey.substring(4);
214
- }
215
- if (clientKey.startsWith('service:')) {
216
- const parts = clientKey.substring(8).split(':');
217
- if (parts.length !== 2) return null;
218
- const registryType = parts[0];
219
- const coordParts = parts[1].split(';');
220
- if (coordParts.length !== 2) return null;
221
- const kta = coordParts[0].split('.');
222
- const scopes = this.parseScopeKey(coordParts[1]);
223
- return {
224
- registryType,
225
- coordinate: {
226
- kta,
227
- scopes
228
- }
229
- };
230
- }
231
- return null;
232
- }
233
- constructor(){
234
- _define_property$4(this, "totalCalls", 0);
235
- // Map structure: ktaKey -> scopeKey -> clientKey -> count
236
- _define_property$4(this, "coordinateCalls", new Map());
237
- }
238
- }
239
-
240
- const logger$1 = LibLogger.get("Registry");
241
- const findScopedInstance = (scopedInstances, requestedScopes)=>{
242
- if (!requestedScopes || requestedScopes.length === 0) {
243
- var _scopedInstances_;
244
- // Return first instance if no scopes specified
245
- const firstInstance = (_scopedInstances_ = scopedInstances[0]) === null || _scopedInstances_ === void 0 ? void 0 : _scopedInstances_.instance;
246
- if (!firstInstance) {
247
- throw new Error('No instances available');
248
- }
249
- return firstInstance;
250
- }
251
- // Find instance that matches all requested scopes
252
- const matchingInstance = scopedInstances.find((scopedInstance)=>{
253
- if (!scopedInstance.scopes) return false;
254
- return requestedScopes.every((scope)=>scopedInstance.scopes && scopedInstance.scopes.includes(scope));
255
- });
256
- if (!matchingInstance) {
257
- throw new Error(`No instance found matching scopes: ${requestedScopes.join(', ')}`);
258
- }
259
- return matchingInstance.instance;
260
- };
261
- const createRegistry = (type, registryHub)=>{
262
- const instanceTree = {};
263
- // Statistics tracking
264
- const registryStats = new RegistryStats();
265
- /**
266
- * Creates a proxied Registry that automatically injects client information for service-to-service calls
267
- */ const createProxiedRegistry = (callingCoordinate)=>{
268
- const serviceClient = {
269
- registryType: type,
270
- coordinate: {
271
- kta: callingCoordinate.kta,
272
- scopes: callingCoordinate.scopes
273
- }
274
- };
275
- return {
276
- ...registry,
277
- get: (kta, options)=>{
278
- // Automatically inject the calling service as the client if no client is specified
279
- const clientToUse = (options === null || options === void 0 ? void 0 : options.client) || serviceClient;
280
- return registry.get(kta, {
281
- ...options,
282
- client: clientToUse
283
- });
284
- }
285
- };
286
- };
287
- const createInstance = (kta, scopes, factory)=>{
288
- logger$1.debug(`Creating and registering instance for key path and scopes`, kta, scopes, `in registry type: ${type}`);
289
- // Create coordinate for the instance
290
- const coordinate = createCoordinate(kta, scopes);
291
- // Create a proxied registry that automatically tracks this service as the client
292
- const proxiedRegistry = createProxiedRegistry(coordinate);
293
- // Use factory to create the instance with the proxied registry
294
- const instance = factory(coordinate, {
295
- registry: proxiedRegistry,
296
- registryHub
297
- });
298
- // Validate the created instance
299
- if (!isInstance(instance)) {
300
- throw new Error(`Factory did not return a valid instance for: ${kta.join('.')}`);
301
- }
302
- // Register the instance
303
- registerInternal(kta, instance, {
304
- scopes
305
- });
306
- return instance;
307
- };
308
- const registerInternal = (kta, instance, options)=>{
309
- const keyPath = [
310
- ...kta
311
- ].reverse(); // Work from most specific to least specific
312
- let currentLevel = instanceTree;
313
- logger$1.debug(`Registering instance for key path and scopes`, keyPath, options === null || options === void 0 ? void 0 : options.scopes, `in registry type: ${type}`);
314
- if (!isInstance(instance)) {
315
- throw new Error(`Attempting to register a non-instance: ${kta.join('.')}`);
316
- }
317
- // Navigate to the correct location in the tree
318
- for(let i = 0; i < keyPath.length; i++){
319
- const keyType = keyPath[i];
320
- const isLeaf = i === keyPath.length - 1;
321
- if (!currentLevel[keyType]) {
322
- currentLevel[keyType] = {
323
- instances: [],
324
- children: isLeaf ? null : {}
325
- };
326
- }
327
- if (isLeaf) {
328
- // Add instance to the leaf node
329
- currentLevel[keyType].instances.push({
330
- scopes: options === null || options === void 0 ? void 0 : options.scopes,
331
- instance
332
- });
333
- } else {
334
- // Navigate deeper into the tree
335
- if (!currentLevel[keyType].children) {
336
- currentLevel[keyType].children = {};
337
- }
338
- currentLevel = currentLevel[keyType].children;
339
- }
340
- }
341
- };
342
- const register = (kta, instance, options)=>{
343
- logger$1.debug('Using deprecated register method. Consider using createInstance instead.');
344
- registerInternal(kta, instance, options);
345
- };
346
- const get = (kta, options)=>{
347
- // Track statistics with kta, scopes, and client
348
- registryStats.recordGetCall(kta, options === null || options === void 0 ? void 0 : options.scopes, options === null || options === void 0 ? void 0 : options.client);
349
- const keyPath = [
350
- ...kta
351
- ].reverse();
352
- let currentLevel = instanceTree;
353
- // Navigate to the target node
354
- for(let i = 0; i < keyPath.length; i++){
355
- const keyType = keyPath[i];
356
- const isLeaf = i === keyPath.length - 1;
357
- if (!currentLevel[keyType]) {
358
- throw new Error(`Instance not found for key path: ${kta.join('.')}, Missing key: ${keyType}`);
359
- }
360
- if (isLeaf) {
361
- // Found the target node, extract instance
362
- const scopedInstances = currentLevel[keyType].instances;
363
- if (scopedInstances.length === 0) {
364
- throw new Error(`No instances registered for key path: ${kta.join('.')}`);
365
- }
366
- return findScopedInstance(scopedInstances, options === null || options === void 0 ? void 0 : options.scopes);
367
- } else {
368
- // Continue navigation
369
- if (!currentLevel[keyType].children) {
370
- throw new Error(`Instance not found for key path: ${kta.join('.')}, No children for: ${keyType}`);
371
- }
372
- currentLevel = currentLevel[keyType].children;
373
- }
374
- }
375
- return null;
376
- };
377
- const getCoordinates = ()=>{
378
- const coordinates = [];
379
- const traverseTree = (node)=>{
380
- for(const keyType in node){
381
- const treeNode = node[keyType];
382
- // Collect coordinates from instances at this level
383
- for (const scopedInstance of treeNode.instances){
384
- coordinates.push(scopedInstance.instance.coordinate);
385
- }
386
- // Recursively traverse children if they exist
387
- if (treeNode.children) {
388
- traverseTree(treeNode.children);
389
- }
390
- }
391
- };
392
- traverseTree(instanceTree);
393
- return coordinates;
394
- };
395
- const getStatistics = ()=>{
396
- return registryStats.getStatistics();
397
- };
398
- const registry = {
399
- type,
400
- registryHub,
401
- createInstance,
402
- register,
403
- get,
404
- getCoordinates,
405
- getStatistics,
406
- instanceTree
407
- };
408
- return registry;
409
- };
410
-
411
- /**
412
- * Base class for all registry-related errors
413
- */ function _define_property$3(obj, key, value) {
414
- if (key in obj) {
415
- Object.defineProperty(obj, key, {
416
- value: value,
417
- enumerable: true,
418
- configurable: true,
419
- writable: true
420
- });
421
- } else {
422
- obj[key] = value;
423
- }
424
- return obj;
425
- }
426
- class RegistryError extends Error {
427
- getDetails() {
428
- const details = [
429
- this.message
430
- ];
431
- if (this.registryType) {
432
- details.push(`Registry Type: ${this.registryType}`);
433
- }
434
- if (this.context) {
435
- details.push(`Context: ${JSON.stringify(this.context, null, 2)}`);
436
- }
437
- return details.join('\n');
438
- }
439
- constructor(message, registryType, context){
440
- super(message), _define_property$3(this, "registryType", void 0), _define_property$3(this, "context", void 0);
441
- this.name = this.constructor.name;
442
- this.registryType = registryType;
443
- this.context = context;
444
- // Maintains proper stack trace for where our error was thrown
445
- if (Error.captureStackTrace) {
446
- Error.captureStackTrace(this, this.constructor);
447
- }
448
- }
449
- }
450
- /**
451
- * Thrown when attempting to create a registry with invalid parameters
452
- */ class RegistryCreationError extends RegistryError {
453
- constructor(type, reason, context){
454
- super(`Failed to create registry of type '${type}': ${reason}`, type, context);
455
- }
456
- }
457
- /**
458
- * Thrown when a factory function returns an invalid instance
459
- */ class InvalidFactoryResultError extends RegistryError {
460
- constructor(keyPath, factoryResult, registryType){
461
- const keyPathStr = keyPath.join('.');
462
- super(`Factory did not return a valid instance for: ${keyPathStr}. ` + `Expected instance with 'coordinate' and 'registry' properties, got: ${typeof factoryResult}`, registryType, {
463
- keyPath,
464
- factoryResult: typeof factoryResult
465
- }), _define_property$3(this, "keyPath", void 0), _define_property$3(this, "factoryResult", void 0);
466
- this.keyPath = keyPath;
467
- this.factoryResult = factoryResult;
468
- }
469
- }
470
- /**
471
- * Thrown when attempting to register a non-instance object
472
- */ class InvalidInstanceRegistrationError extends RegistryError {
473
- constructor(keyPath, attemptedRegistration, registryType){
474
- const keyPathStr = keyPath.join('.');
475
- super(`Attempting to register a non-instance: ${keyPathStr}. ` + `Expected instance with 'coordinate' and 'registry' properties, got: ${typeof attemptedRegistration}`, registryType, {
476
- keyPath,
477
- attemptedRegistration: typeof attemptedRegistration
478
- }), _define_property$3(this, "keyPath", void 0), _define_property$3(this, "attemptedRegistration", void 0);
479
- this.keyPath = keyPath;
480
- this.attemptedRegistration = attemptedRegistration;
481
- }
482
- }
483
-
484
- function _define_property$2(obj, key, value) {
485
- if (key in obj) {
486
- Object.defineProperty(obj, key, {
487
- value: value,
488
- enumerable: true,
489
- configurable: true,
490
- writable: true
491
- });
492
- } else {
493
- obj[key] = value;
494
- }
495
- return obj;
496
- }
497
- /**
498
- * Base class for registry hub-related errors
499
- */ class RegistryHubError extends RegistryError {
500
- constructor(message, hubType, context){
501
- const enrichedContext = hubType ? {
502
- ...context,
503
- hubType
504
- } : context;
505
- super(message, '', enrichedContext), _define_property$2(this, "hubType", void 0);
506
- this.hubType = hubType;
507
- }
508
- }
509
- /**
510
- * Thrown when attempting to register a registry with a type that already exists
511
- */ class DuplicateRegistryTypeError extends RegistryHubError {
512
- constructor(type, context){
513
- super(`Registry already registered under type: ${type}. ` + `Each registry type must be unique within a registry hub.`, '', {
514
- ...context,
515
- duplicateType: type
516
- }), _define_property$2(this, "duplicateType", void 0);
517
- this.duplicateType = type;
518
- }
519
- }
520
- /**
521
- * Thrown when attempting to access a registry type that doesn't exist
522
- */ class RegistryTypeNotFoundError extends RegistryHubError {
523
- constructor(requestedType, availableTypes = [], context){
524
- let message = `No registry registered under type: ${requestedType}`;
525
- if (availableTypes.length > 0) {
526
- message += `. Available types: [${availableTypes.join(', ')}]`;
527
- }
528
- super(message, '', {
529
- ...context,
530
- requestedType,
531
- availableTypes
532
- }), _define_property$2(this, "requestedType", void 0), _define_property$2(this, "availableTypes", void 0);
533
- this.requestedType = requestedType;
534
- this.availableTypes = availableTypes;
535
- }
536
- }
537
- /**
538
- * Thrown when a registry factory function fails to create a valid registry
539
- */ class RegistryFactoryError extends RegistryHubError {
540
- constructor(type, factoryError, context){
541
- super(`Registry factory failed to create registry of type '${type}': ${factoryError.message}`, '', {
542
- ...context,
543
- attemptedType: type,
544
- originalError: factoryError.message
545
- }), _define_property$2(this, "factoryError", void 0), _define_property$2(this, "attemptedType", void 0);
546
- this.factoryError = factoryError;
547
- this.attemptedType = type;
548
- }
549
- }
550
- /**
551
- * Thrown when a factory returns an invalid registry object
552
- */ class InvalidRegistryFactoryResultError extends RegistryHubError {
553
- constructor(type, factoryResult, context){
554
- super(`Registry factory returned invalid registry for type '${type}'. ` + `Expected registry with 'type', 'get', 'register', and 'createInstance' properties, ` + `got: ${typeof factoryResult}`, '', {
555
- ...context,
556
- attemptedType: type,
557
- factoryResult: typeof factoryResult
558
- }), _define_property$2(this, "factoryResult", void 0), _define_property$2(this, "attemptedType", void 0);
559
- this.factoryResult = factoryResult;
560
- this.attemptedType = type;
561
- }
562
- }
563
-
564
- const logger = LibLogger.get("RegistryHub");
565
- const createRegistryHub = ()=>{
566
- const registries = {};
567
- const createRegistry = (type, factory)=>{
568
- logger.debug(`Creating new registry with type: ${type}`);
569
- if (registries[type]) {
570
- throw new DuplicateRegistryTypeError(type);
571
- }
572
- // Create the registry with a reference to this hub
573
- const registry = factory(type, hub);
574
- // Ensure the created registry has a reference to this hub if not already set
575
- if (!('registryHub' in registry) || registry.registryHub !== hub) {
576
- // @ts-expect-error: registryHub is optional and may be readonly, but we want to set it for hub awareness
577
- registry.registryHub = hub;
578
- }
579
- // Register the created registry
580
- registries[type] = registry;
581
- logger.debug(`Successfully created and registered new registry with type: ${type}`);
582
- return registry;
583
- };
584
- const registerRegistry = (registry)=>{
585
- const type = registry.type;
586
- logger.debug(`Registering registry with type: ${type}`);
587
- if (registries[type]) {
588
- throw new DuplicateRegistryTypeError(type);
589
- }
590
- registries[type] = registry;
591
- // Ensure the created registry has a reference to this hub if not already set
592
- if (!('registryHub' in registry) || registry.registryHub !== hub) {
593
- // @ts-expect-error: registryHub is optional and may be readonly, but we want to set it for hub awareness
594
- registry.registryHub = hub;
595
- }
596
- logger.debug(`Successfully registered registry with type: ${type}`);
597
- };
598
- const get = (type, kta, options)=>{
599
- var _options_scopes;
600
- logger.debug(`Looking up instance for type: ${type}, kta: ${kta.join('.')}, scopes: ${(options === null || options === void 0 ? void 0 : (_options_scopes = options.scopes) === null || _options_scopes === void 0 ? void 0 : _options_scopes.join(',')) || 'none'}`);
601
- const registry = registries[type];
602
- if (!registry) {
603
- const availableTypes = Object.keys(registries);
604
- throw new RegistryTypeNotFoundError(type, availableTypes);
605
- }
606
- return registry.get(kta, options);
607
- };
608
- const getRegistry = (type)=>{
609
- return registries[type] || null;
610
- };
611
- const getRegisteredTypes = ()=>{
612
- return Object.keys(registries);
613
- };
614
- const unregisterRegistry = (type)=>{
615
- if (registries[type]) {
616
- delete registries[type];
617
- logger.debug(`Unregistered registry under type: ${type}`);
618
- return true;
619
- }
620
- return false;
621
- };
622
- const getAllCoordinates = ()=>{
623
- const allCoordinates = [];
624
- for(const registryType in registries){
625
- const registry = registries[registryType];
626
- const coordinates = registry.getCoordinates();
627
- coordinates.forEach((coordinate)=>{
628
- allCoordinates.push({
629
- coordinate,
630
- registryType
631
- });
632
- });
633
- }
634
- logger.debug(`Retrieved ${allCoordinates.length} total coordinates from ${Object.keys(registries).length} registries`);
635
- return allCoordinates;
636
- };
637
- const hub = {
638
- createRegistry,
639
- registerRegistry,
640
- get,
641
- getRegistry,
642
- getRegisteredTypes,
643
- getAllCoordinates,
644
- unregisterRegistry
645
- };
646
- return hub;
647
- };
648
-
649
- function _define_property$1(obj, key, value) {
650
- if (key in obj) {
651
- Object.defineProperty(obj, key, {
652
- value: value,
653
- enumerable: true,
654
- configurable: true,
655
- writable: true
656
- });
657
- } else {
658
- obj[key] = value;
659
- }
660
- return obj;
661
- }
662
- /**
663
- * Base class for instance-related errors
664
- */ class InstanceError extends RegistryError {
665
- constructor(message, keyPath, registryType, context){
666
- super(message, registryType, {
667
- ...context,
668
- keyPath
669
- }), _define_property$1(this, "keyPath", void 0);
670
- this.keyPath = keyPath;
671
- }
672
- }
673
- /**
674
- * Thrown when an instance cannot be found for a given key path
675
- */ class InstanceNotFoundError extends InstanceError {
676
- constructor(keyPath, missingKey, registryType, context){
677
- const keyPathStr = keyPath.join('.');
678
- let message = `Instance not found for key path: ${keyPathStr}`;
679
- if (missingKey) {
680
- message += `, Missing key: ${missingKey}`;
681
- }
682
- super(message, keyPath, registryType, {
683
- ...context,
684
- missingKey
685
- }), _define_property$1(this, "missingKey", void 0);
686
- this.missingKey = missingKey;
687
- }
688
- }
689
- /**
690
- * Thrown when no instances are registered for a key path that exists in the tree
691
- */ class NoInstancesRegisteredError extends InstanceError {
692
- constructor(keyPath, registryType, context){
693
- const keyPathStr = keyPath.join('.');
694
- super(`No instances registered for key path: ${keyPathStr}. ` + `The key path exists in the registry tree but contains no instances.`, keyPath, registryType, context);
695
- }
696
- }
697
- /**
698
- * Thrown when no instances are available (empty instances array)
699
- */ class NoInstancesAvailableError extends InstanceError {
700
- constructor(keyPath, registryType, context){
701
- const keyPathStr = keyPath.join('.');
702
- super(`No instances available for key path: ${keyPathStr}. ` + `This typically indicates an internal registry state issue.`, keyPath, registryType, context);
703
- }
704
- }
705
- /**
706
- * Thrown when no instance matches the requested scopes
707
- */ class ScopeNotFoundError extends InstanceError {
708
- constructor(keyPath, requestedScopes, availableScopes = [], registryType){
709
- const keyPathStr = keyPath.join('.');
710
- const scopesStr = requestedScopes.join(', ');
711
- const availableScopesStr = availableScopes.map((scopes)=>`[${scopes.join(', ')}]`).join(', ');
712
- let message = `No instance found matching scopes: ${scopesStr} for key path: ${keyPathStr}`;
713
- if (availableScopes.length > 0) {
714
- message += `. Available scopes: ${availableScopesStr}`;
715
- }
716
- super(message, keyPath, registryType, {
717
- requestedScopes,
718
- availableScopes
719
- }), _define_property$1(this, "requestedScopes", void 0), _define_property$1(this, "availableScopes", void 0);
720
- this.requestedScopes = requestedScopes;
721
- this.availableScopes = availableScopes;
722
- }
723
- }
724
- /**
725
- * Thrown when a key path has no children but children are expected
726
- */ class NoChildrenAvailableError extends InstanceError {
727
- constructor(keyPath, parentKey, registryType, context){
728
- const keyPathStr = keyPath.join('.');
729
- super(`Instance not found for key path: ${keyPathStr}, No children for: ${parentKey}. ` + `The path cannot be traversed further as '${parentKey}' has no child nodes.`, keyPath, registryType, {
730
- ...context,
731
- parentKey
732
- }), _define_property$1(this, "parentKey", void 0);
733
- this.parentKey = parentKey;
734
- }
735
- }
736
-
737
- function _define_property(obj, key, value) {
738
- if (key in obj) {
739
- Object.defineProperty(obj, key, {
740
- value: value,
741
- enumerable: true,
742
- configurable: true,
743
- writable: true
744
- });
745
- } else {
746
- obj[key] = value;
747
- }
748
- return obj;
749
- }
750
- /**
751
- * Base class for coordinate-related errors
752
- */ class CoordinateError extends RegistryError {
753
- constructor(message, kta, scopes, context){
754
- super(message, '', {
755
- ...context,
756
- kta,
757
- scopes
758
- }), _define_property(this, "kta", void 0), _define_property(this, "scopes", void 0);
759
- this.kta = kta;
760
- this.scopes = scopes;
761
- }
762
- }
763
- /**
764
- * Thrown when coordinate creation fails due to invalid parameters
765
- */ class InvalidCoordinateError extends CoordinateError {
766
- constructor(kta, scopes, reason, context){
767
- super(`Invalid coordinate parameters: ${reason}. ` + `KTA: ${JSON.stringify(kta)}, Scopes: [${scopes.join(', ')}]`, kta, scopes, {
768
- ...context,
769
- reason
770
- });
771
- }
772
- }
773
- /**
774
- * Thrown when KTA (Key Type Array) is invalid
775
- */ class InvalidKTAError extends CoordinateError {
776
- constructor(kta, reason, context){
777
- super(`Invalid KTA (Key Type Array): ${reason}. ` + `Expected string or array of strings, got: ${JSON.stringify(kta)}`, kta, [], {
778
- ...context,
779
- reason
780
- });
781
- }
782
- }
783
- /**
784
- * Thrown when scopes array contains invalid values
785
- */ class InvalidScopesError extends CoordinateError {
786
- constructor(scopes, invalidScopes, reason, context){
787
- super(`Invalid scopes: ${reason}. ` + `Invalid scope values: ${JSON.stringify(invalidScopes)}`, null, scopes.filter((s)=>typeof s === 'string'), {
788
- ...context,
789
- reason,
790
- invalidScopes
791
- }), _define_property(this, "invalidScopes", void 0);
792
- this.invalidScopes = invalidScopes;
793
- }
794
- }
795
-
796
- exports.CoordinateError = CoordinateError;
797
- exports.DuplicateRegistryTypeError = DuplicateRegistryTypeError;
798
- exports.InstanceError = InstanceError;
799
- exports.InstanceNotFoundError = InstanceNotFoundError;
800
- exports.InvalidCoordinateError = InvalidCoordinateError;
801
- exports.InvalidFactoryResultError = InvalidFactoryResultError;
802
- exports.InvalidInstanceRegistrationError = InvalidInstanceRegistrationError;
803
- exports.InvalidKTAError = InvalidKTAError;
804
- exports.InvalidRegistryFactoryResultError = InvalidRegistryFactoryResultError;
805
- exports.InvalidScopesError = InvalidScopesError;
806
- exports.NoChildrenAvailableError = NoChildrenAvailableError;
807
- exports.NoInstancesAvailableError = NoInstancesAvailableError;
808
- exports.NoInstancesRegisteredError = NoInstancesRegisteredError;
809
- exports.RegistryCreationError = RegistryCreationError;
810
- exports.RegistryError = RegistryError;
811
- exports.RegistryFactoryError = RegistryFactoryError;
812
- exports.RegistryHubError = RegistryHubError;
813
- exports.RegistryStats = RegistryStats;
814
- exports.RegistryTypeNotFoundError = RegistryTypeNotFoundError;
815
- exports.ScopeNotFoundError = ScopeNotFoundError;
816
- exports.createCoordinate = createCoordinate;
817
- exports.createInstance = createInstance;
818
- exports.createRegistry = createRegistry;
819
- exports.createRegistryHub = createRegistryHub;
820
- exports.isInstance = isInstance;
821
- //# sourceMappingURL=index.cjs.map