@navios/di 0.6.0 → 0.7.0

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 (51) hide show
  1. package/CHANGELOG.md +46 -8
  2. package/lib/browser/index.d.mts +6 -85
  3. package/lib/browser/index.d.mts.map +1 -1
  4. package/lib/browser/index.mjs +105 -485
  5. package/lib/browser/index.mjs.map +1 -1
  6. package/lib/{testing-BMGmmxH7.cjs → container-BCv3XS6m.cjs} +62 -457
  7. package/lib/container-BCv3XS6m.cjs.map +1 -0
  8. package/lib/{testing-DCXz8AJD.mjs → container-Bv6PZZLJ.mjs} +63 -445
  9. package/lib/container-Bv6PZZLJ.mjs.map +1 -0
  10. package/lib/{index-fKPuT65j.d.cts → container-CXDYDJSM.d.mts} +8 -63
  11. package/lib/container-CXDYDJSM.d.mts.map +1 -0
  12. package/lib/{index-S_qX2VLI.d.mts → container-b6mDUdGq.d.cts} +3 -68
  13. package/lib/container-b6mDUdGq.d.cts.map +1 -0
  14. package/lib/index.cjs +44 -52
  15. package/lib/index.cjs.map +1 -1
  16. package/lib/index.d.cts +6 -25
  17. package/lib/index.d.cts.map +1 -1
  18. package/lib/index.d.mts +6 -25
  19. package/lib/index.d.mts.map +1 -1
  20. package/lib/index.mjs +2 -2
  21. package/lib/testing/index.cjs +343 -4
  22. package/lib/testing/index.cjs.map +1 -0
  23. package/lib/testing/index.d.cts +65 -2
  24. package/lib/testing/index.d.cts.map +1 -0
  25. package/lib/testing/index.d.mts +65 -2
  26. package/lib/testing/index.d.mts.map +1 -0
  27. package/lib/testing/index.mjs +341 -2
  28. package/lib/testing/index.mjs.map +1 -0
  29. package/package.json +9 -9
  30. package/src/__tests__/async-local-storage.browser.spec.mts +18 -92
  31. package/src/__tests__/container.spec.mts +93 -0
  32. package/src/__tests__/e2e.browser.spec.mts +7 -15
  33. package/src/__tests__/errors.spec.mts +6 -6
  34. package/src/__tests__/library-findings.spec.mts +23 -21
  35. package/src/browser.mts +4 -9
  36. package/src/container/scoped-container.mts +14 -8
  37. package/src/errors/di-error.mts +2 -1
  38. package/src/index.mts +5 -8
  39. package/src/internal/context/async-local-storage.browser.mts +19 -0
  40. package/src/internal/context/async-local-storage.mts +46 -98
  41. package/src/internal/context/async-local-storage.types.mts +7 -0
  42. package/src/internal/context/resolution-context.mts +23 -5
  43. package/src/internal/context/sync-local-storage.mts +3 -1
  44. package/src/internal/core/instance-resolver.mts +8 -1
  45. package/src/internal/lifecycle/circular-detector.mts +15 -0
  46. package/tsdown.config.mts +12 -1
  47. package/vitest.config.mts +25 -3
  48. package/lib/index-S_qX2VLI.d.mts.map +0 -1
  49. package/lib/index-fKPuT65j.d.cts.map +0 -1
  50. package/lib/testing-BMGmmxH7.cjs.map +0 -1
  51. package/lib/testing-DCXz8AJD.mjs.map +0 -1
@@ -1,119 +1,5 @@
1
1
  import { z } from "zod/v4";
2
2
 
3
- //#region rolldown:runtime
4
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
5
- if (typeof require !== "undefined") return require.apply(this, arguments);
6
- throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function.");
7
- });
8
-
9
- //#endregion
10
- //#region src/internal/context/sync-local-storage.mts
11
- /**
12
- * A synchronous-only polyfill for AsyncLocalStorage.
13
- *
14
- * This provides the same API as Node's AsyncLocalStorage but only works
15
- * for synchronous code paths. It uses a simple stack-based approach.
16
- *
17
- * Limitations:
18
- * - Context does NOT propagate across async boundaries (setTimeout, promises, etc.)
19
- * - Only suitable for environments where DI resolution is synchronous
20
- *
21
- * This is acceptable for browser environments where:
22
- * 1. Constructors are typically synchronous
23
- * 2. Circular dependency detection mainly needs sync tracking
24
- */
25
- var SyncLocalStorage = class {
26
- stack = [];
27
- /**
28
- * Runs a function within the given store context.
29
- * The context is only available synchronously within the function.
30
- */
31
- run(store, fn) {
32
- this.stack.push(store);
33
- try {
34
- return fn();
35
- } finally {
36
- this.stack.pop();
37
- }
38
- }
39
- /**
40
- * Gets the current store value, or undefined if not in a context.
41
- */
42
- getStore() {
43
- return this.stack.length > 0 ? this.stack[this.stack.length - 1] : void 0;
44
- }
45
- /**
46
- * Exits the current context and runs the function without any store.
47
- * This matches AsyncLocalStorage.exit() behavior.
48
- */
49
- exit(fn) {
50
- const savedStack = this.stack;
51
- this.stack = [];
52
- try {
53
- return fn();
54
- } finally {
55
- this.stack = savedStack;
56
- }
57
- }
58
- };
59
-
60
- //#endregion
61
- //#region src/internal/context/async-local-storage.mts
62
- /**
63
- * Cross-platform AsyncLocalStorage wrapper.
64
- *
65
- * Provides AsyncLocalStorage on Node.js/Bun and falls back to
66
- * a synchronous-only polyfill in browser environments.
67
- */
68
- /**
69
- * Detects if we're running in a Node.js-like environment with async_hooks support.
70
- */ function hasAsyncHooksSupport() {
71
- if (typeof process !== "undefined" && process.versions && process.versions.node) return true;
72
- if (typeof process !== "undefined" && process.versions && "bun" in process.versions) return true;
73
- if (typeof globalThis.Deno !== "undefined") return true;
74
- return false;
75
- }
76
- let AsyncLocalStorageClass = null;
77
- let initialized = false;
78
- let forceSyncMode = false;
79
- /**
80
- * Gets the appropriate AsyncLocalStorage implementation for the current environment.
81
- *
82
- * - On Node.js/Bun/Deno: Returns the native AsyncLocalStorage
83
- * - On browsers: Returns SyncLocalStorage polyfill
84
- */ function getAsyncLocalStorageClass() {
85
- if (initialized) return AsyncLocalStorageClass;
86
- initialized = true;
87
- if (!forceSyncMode && hasAsyncHooksSupport()) try {
88
- AsyncLocalStorageClass = __require("node:async_hooks").AsyncLocalStorage;
89
- } catch {
90
- AsyncLocalStorageClass = SyncLocalStorage;
91
- }
92
- else AsyncLocalStorageClass = SyncLocalStorage;
93
- return AsyncLocalStorageClass;
94
- }
95
- /**
96
- * Creates a new AsyncLocalStorage instance appropriate for the current environment.
97
- */ function createAsyncLocalStorage() {
98
- return new (getAsyncLocalStorageClass())();
99
- }
100
- /**
101
- * Testing utilities for forcing specific modes.
102
- * Only exported for testing purposes.
103
- */ const __testing__ = {
104
- forceSyncMode: () => {
105
- initialized = false;
106
- forceSyncMode = true;
107
- AsyncLocalStorageClass = null;
108
- },
109
- reset: () => {
110
- initialized = false;
111
- forceSyncMode = false;
112
- AsyncLocalStorageClass = null;
113
- }
114
- };
115
-
116
- //#endregion
117
3
  //#region src/enums/injectable-scope.enum.mts
118
4
  let InjectableScope = /* @__PURE__ */ function(InjectableScope$1) {
119
5
  /**
@@ -277,9 +163,10 @@ let DIErrorCode = /* @__PURE__ */ function(DIErrorCode$1) {
277
163
  DIErrorCode$1["UnknownError"] = "UnknownError";
278
164
  return DIErrorCode$1;
279
165
  }({});
280
- var DIError = class DIError {
166
+ var DIError = class DIError extends Error {
281
167
  context;
282
168
  constructor(code, message, context) {
169
+ super(message);
283
170
  this.code = code;
284
171
  this.message = message;
285
172
  this.context = context;
@@ -309,6 +196,70 @@ var DIError = class DIError {
309
196
  }
310
197
  };
311
198
 
199
+ //#endregion
200
+ //#region src/internal/context/sync-local-storage.mts
201
+ /**
202
+ * A synchronous-only polyfill for AsyncLocalStorage.
203
+ *
204
+ * This provides the same API as Node's AsyncLocalStorage but only works
205
+ * for synchronous code paths. It uses a simple stack-based approach.
206
+ *
207
+ * Limitations:
208
+ * - Context does NOT propagate across async boundaries (setTimeout, promises, etc.)
209
+ * - Only suitable for environments where DI resolution is synchronous
210
+ *
211
+ * This is acceptable for browser environments where:
212
+ * 1. Constructors are typically synchronous
213
+ * 2. Circular dependency detection mainly needs sync tracking
214
+ */
215
+ var SyncLocalStorage = class {
216
+ stack = [];
217
+ /**
218
+ * Runs a function within the given store context.
219
+ * The context is only available synchronously within the function.
220
+ */
221
+ run(store, fn) {
222
+ this.stack.push(store);
223
+ try {
224
+ return fn();
225
+ } finally {
226
+ this.stack.pop();
227
+ }
228
+ }
229
+ /**
230
+ * Gets the current store value, or undefined if not in a context.
231
+ */
232
+ getStore() {
233
+ return this.stack.length > 0 ? this.stack[this.stack.length - 1] : void 0;
234
+ }
235
+ /**
236
+ * Exits the current context and runs the function without any store.
237
+ * This matches AsyncLocalStorage.exit() behavior.
238
+ */
239
+ exit(fn) {
240
+ const savedStack = this.stack;
241
+ this.stack = [];
242
+ try {
243
+ return fn();
244
+ } finally {
245
+ this.stack = savedStack;
246
+ }
247
+ }
248
+ };
249
+
250
+ //#endregion
251
+ //#region src/internal/context/async-local-storage.browser.mts
252
+ /**
253
+ * Browser implementation using SyncLocalStorage.
254
+ *
255
+ * This module is used in browser environments where async_hooks is not available.
256
+ * It provides synchronous-only context tracking which is sufficient for
257
+ * browser-based DI resolution.
258
+ */
259
+ function createAsyncLocalStorage() {
260
+ return new SyncLocalStorage();
261
+ }
262
+
312
263
  //#endregion
313
264
  //#region src/internal/context/resolution-context.mts
314
265
  /**
@@ -317,7 +268,16 @@ var DIError = class DIError {
317
268
  * This allows tracking which service is being instantiated even across
318
269
  * async boundaries (like when inject() is called inside a constructor).
319
270
  * Essential for circular dependency detection.
320
- */ const resolutionContext = createAsyncLocalStorage();
271
+ *
272
+ * The actual implementation varies by environment:
273
+ * - Production: No-op (returns undefined, run() just calls fn directly)
274
+ * - Development: Real AsyncLocalStorage with full async tracking
275
+ * - Browser: SyncLocalStorage for synchronous-only tracking
276
+ */ let resolutionContext = null;
277
+ function getResolutionContext() {
278
+ if (!resolutionContext) resolutionContext = createAsyncLocalStorage();
279
+ return resolutionContext;
280
+ }
321
281
  /**
322
282
  * Runs a function within a resolution context.
323
283
  *
@@ -328,7 +288,7 @@ var DIError = class DIError {
328
288
  * @param getHolder Function to retrieve holders by name
329
289
  * @param fn The function to run within the context
330
290
  */ function withResolutionContext(waiterHolder, getHolder, fn) {
331
- return resolutionContext.run({
291
+ return getResolutionContext().run({
332
292
  waiterHolder,
333
293
  getHolder
334
294
  }, fn);
@@ -339,7 +299,7 @@ var DIError = class DIError {
339
299
  * Returns undefined if we're not inside a resolution context
340
300
  * (e.g., when resolving a top-level service that has no parent).
341
301
  */ function getCurrentResolutionContext() {
342
- return resolutionContext.getStore();
302
+ return getResolutionContext().getStore();
343
303
  }
344
304
  /**
345
305
  * Runs a function outside any resolution context.
@@ -349,7 +309,7 @@ var DIError = class DIError {
349
309
  *
350
310
  * @param fn The function to run without resolution context
351
311
  */ function withoutResolutionContext(fn) {
352
- return resolutionContext.run(void 0, fn);
312
+ return getResolutionContext().run(void 0, fn);
353
313
  }
354
314
 
355
315
  //#endregion
@@ -491,6 +451,8 @@ const provideFactoryContext = defaultInjectors.provideFactoryContext;
491
451
  *
492
452
  * Uses BFS to traverse the waitingFor graph starting from a target holder
493
453
  * and checks if following the chain leads back to the waiter, indicating a circular dependency.
454
+ *
455
+ * Note: In production (NODE_ENV === 'production'), detection is skipped for performance.
494
456
  */ var CircularDetector = class {
495
457
  /**
496
458
  * Detects if waiting for `targetName` from `waiterName` would create a cycle.
@@ -498,6 +460,8 @@ const provideFactoryContext = defaultInjectors.provideFactoryContext;
498
460
  * This works by checking if `targetName` (or any holder in its waitingFor chain)
499
461
  * is currently waiting for `waiterName`. If so, waiting would create a deadlock.
500
462
  *
463
+ * In production mode, this always returns null to skip the BFS traversal overhead.
464
+ *
501
465
  * @param waiterName The name of the holder that wants to wait
502
466
  * @param targetName The name of the holder being waited on
503
467
  * @param getHolder Function to retrieve a holder by name
@@ -943,7 +907,8 @@ var RequestStorage = class {
943
907
  */ async resolveRequestScoped(token, args) {
944
908
  const instanceName = this.parent.getServiceLocator().getInstanceIdentifier(token, args);
945
909
  const existingHolder = this.requestContextHolder.get(instanceName);
946
- if (existingHolder) {
910
+ if (existingHolder) if (existingHolder.status === InstanceStatus.Error) this.requestContextHolder.delete(instanceName);
911
+ else {
947
912
  const [error, readyHolder] = await BaseHolderManager.waitForHolderReady(existingHolder);
948
913
  if (error) throw error;
949
914
  return readyHolder.instance;
@@ -1114,7 +1079,12 @@ var SingletonStorage = class {
1114
1079
  return [void 0, readyResult[1].instance];
1115
1080
  }
1116
1081
  return null;
1117
- default: return [error];
1082
+ default:
1083
+ if (holder) {
1084
+ this.logger?.log(`[InstanceResolver] Removing failed instance ${instanceName} from storage to allow retry`);
1085
+ storage.delete(instanceName);
1086
+ }
1087
+ return null;
1118
1088
  }
1119
1089
  }
1120
1090
  /**
@@ -1911,7 +1881,7 @@ var SingletonStorage = class {
1911
1881
 
1912
1882
  //#endregion
1913
1883
  //#region src/container/container.mts
1914
- function applyDecs2203RFactory$2() {
1884
+ function applyDecs2203RFactory$1() {
1915
1885
  function createAddInitializerMethod(initializers, decoratorFinishedRef) {
1916
1886
  return function addInitializer(initializer) {
1917
1887
  assertNotFinished(decoratorFinishedRef, "addInitializer");
@@ -2185,18 +2155,18 @@ function applyDecs2203RFactory$2() {
2185
2155
  };
2186
2156
  };
2187
2157
  }
2188
- function _apply_decs_2203_r$2(targetClass, memberDecs, classDecs, parentClass) {
2189
- return (_apply_decs_2203_r$2 = applyDecs2203RFactory$2())(targetClass, memberDecs, classDecs, parentClass);
2158
+ function _apply_decs_2203_r$1(targetClass, memberDecs, classDecs, parentClass) {
2159
+ return (_apply_decs_2203_r$1 = applyDecs2203RFactory$1())(targetClass, memberDecs, classDecs, parentClass);
2190
2160
  }
2191
- var _dec$2, _initClass$2;
2161
+ var _dec$1, _initClass$1;
2192
2162
  let _Container;
2193
- _dec$2 = Injectable();
2163
+ _dec$1 = Injectable();
2194
2164
  var Container = class {
2195
2165
  registry;
2196
2166
  logger;
2197
2167
  injectors;
2198
2168
  static {
2199
- ({c: [_Container, _initClass$2]} = _apply_decs_2203_r$2(this, [], [_dec$2]));
2169
+ ({c: [_Container, _initClass$1]} = _apply_decs_2203_r$1(this, [], [_dec$1]));
2200
2170
  }
2201
2171
  constructor(registry = globalRegistry, logger = null, injectors = defaultInjectors) {
2202
2172
  this.registry = registry;
@@ -2326,7 +2296,7 @@ var Container = class {
2326
2296
  return this.serviceLocator.clearAll();
2327
2297
  }
2328
2298
  static {
2329
- _initClass$2();
2299
+ _initClass$1();
2330
2300
  }
2331
2301
  };
2332
2302
 
@@ -2344,7 +2314,7 @@ function Factory({ scope = InjectableScope.Singleton, token, registry = globalRe
2344
2314
 
2345
2315
  //#endregion
2346
2316
  //#region src/event-emitter.mts
2347
- function applyDecs2203RFactory$1() {
2317
+ function applyDecs2203RFactory() {
2348
2318
  function createAddInitializerMethod(initializers, decoratorFinishedRef) {
2349
2319
  return function addInitializer(initializer) {
2350
2320
  assertNotFinished(decoratorFinishedRef, "addInitializer");
@@ -2618,15 +2588,15 @@ function applyDecs2203RFactory$1() {
2618
2588
  };
2619
2589
  };
2620
2590
  }
2621
- function _apply_decs_2203_r$1(targetClass, memberDecs, classDecs, parentClass) {
2622
- return (_apply_decs_2203_r$1 = applyDecs2203RFactory$1())(targetClass, memberDecs, classDecs, parentClass);
2591
+ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
2592
+ return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
2623
2593
  }
2624
- var _dec$1, _initClass$1;
2594
+ var _dec, _initClass;
2625
2595
  let _EventEmitter;
2626
- _dec$1 = Injectable({ scope: InjectableScope.Transient });
2596
+ _dec = Injectable({ scope: InjectableScope.Transient });
2627
2597
  var EventEmitter = class {
2628
2598
  static {
2629
- ({c: [_EventEmitter, _initClass$1]} = _apply_decs_2203_r$1(this, [], [_dec$1]));
2599
+ ({c: [_EventEmitter, _initClass]} = _apply_decs_2203_r(this, [], [_dec]));
2630
2600
  }
2631
2601
  listeners = /* @__PURE__ */ new Map();
2632
2602
  on(event, listener) {
@@ -2652,361 +2622,11 @@ var EventEmitter = class {
2652
2622
  if (!this.listeners.has(event)) return;
2653
2623
  return Promise.all(Array.from(this.listeners.get(event)).map((listener) => listener(...args)));
2654
2624
  }
2655
- static {
2656
- _initClass$1();
2657
- }
2658
- };
2659
-
2660
- //#endregion
2661
- //#region src/testing/test-container.mts
2662
- function applyDecs2203RFactory() {
2663
- function createAddInitializerMethod(initializers, decoratorFinishedRef) {
2664
- return function addInitializer(initializer) {
2665
- assertNotFinished(decoratorFinishedRef, "addInitializer");
2666
- assertCallable(initializer, "An initializer");
2667
- initializers.push(initializer);
2668
- };
2669
- }
2670
- function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
2671
- var kindStr;
2672
- switch (kind) {
2673
- case 1:
2674
- kindStr = "accessor";
2675
- break;
2676
- case 2:
2677
- kindStr = "method";
2678
- break;
2679
- case 3:
2680
- kindStr = "getter";
2681
- break;
2682
- case 4:
2683
- kindStr = "setter";
2684
- break;
2685
- default: kindStr = "field";
2686
- }
2687
- var ctx = {
2688
- kind: kindStr,
2689
- name: isPrivate ? "#" + name : name,
2690
- static: isStatic,
2691
- private: isPrivate,
2692
- metadata
2693
- };
2694
- var decoratorFinishedRef = { v: false };
2695
- ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
2696
- var get, set;
2697
- if (kind === 0) if (isPrivate) {
2698
- get = desc.get;
2699
- set = desc.set;
2700
- } else {
2701
- get = function() {
2702
- return this[name];
2703
- };
2704
- set = function(v) {
2705
- this[name] = v;
2706
- };
2707
- }
2708
- else if (kind === 2) get = function() {
2709
- return desc.value;
2710
- };
2711
- else {
2712
- if (kind === 1 || kind === 3) get = function() {
2713
- return desc.get.call(this);
2714
- };
2715
- if (kind === 1 || kind === 4) set = function(v) {
2716
- desc.set.call(this, v);
2717
- };
2718
- }
2719
- ctx.access = get && set ? {
2720
- get,
2721
- set
2722
- } : get ? { get } : { set };
2723
- try {
2724
- return dec(value, ctx);
2725
- } finally {
2726
- decoratorFinishedRef.v = true;
2727
- }
2728
- }
2729
- function assertNotFinished(decoratorFinishedRef, fnName) {
2730
- if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
2731
- }
2732
- function assertCallable(fn, hint) {
2733
- if (typeof fn !== "function") throw new TypeError(hint + " must be a function");
2734
- }
2735
- function assertValidReturnValue(kind, value) {
2736
- var type = typeof value;
2737
- if (kind === 1) {
2738
- if (type !== "object" || value === null) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
2739
- if (value.get !== void 0) assertCallable(value.get, "accessor.get");
2740
- if (value.set !== void 0) assertCallable(value.set, "accessor.set");
2741
- if (value.init !== void 0) assertCallable(value.init, "accessor.init");
2742
- } else if (type !== "function") {
2743
- var hint;
2744
- if (kind === 0) hint = "field";
2745
- else if (kind === 10) hint = "class";
2746
- else hint = "method";
2747
- throw new TypeError(hint + " decorators must return a function or void 0");
2748
- }
2749
- }
2750
- function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
2751
- var decs = decInfo[0];
2752
- var desc, init, value;
2753
- if (isPrivate) if (kind === 0 || kind === 1) desc = {
2754
- get: decInfo[3],
2755
- set: decInfo[4]
2756
- };
2757
- else if (kind === 3) desc = { get: decInfo[3] };
2758
- else if (kind === 4) desc = { set: decInfo[3] };
2759
- else desc = { value: decInfo[3] };
2760
- else if (kind !== 0) desc = Object.getOwnPropertyDescriptor(base, name);
2761
- if (kind === 1) value = {
2762
- get: desc.get,
2763
- set: desc.set
2764
- };
2765
- else if (kind === 2) value = desc.value;
2766
- else if (kind === 3) value = desc.get;
2767
- else if (kind === 4) value = desc.set;
2768
- var newValue, get, set;
2769
- if (typeof decs === "function") {
2770
- newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
2771
- if (newValue !== void 0) {
2772
- assertValidReturnValue(kind, newValue);
2773
- if (kind === 0) init = newValue;
2774
- else if (kind === 1) {
2775
- init = newValue.init;
2776
- get = newValue.get || value.get;
2777
- set = newValue.set || value.set;
2778
- value = {
2779
- get,
2780
- set
2781
- };
2782
- } else value = newValue;
2783
- }
2784
- } else for (var i = decs.length - 1; i >= 0; i--) {
2785
- var dec = decs[i];
2786
- newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
2787
- if (newValue !== void 0) {
2788
- assertValidReturnValue(kind, newValue);
2789
- var newInit;
2790
- if (kind === 0) newInit = newValue;
2791
- else if (kind === 1) {
2792
- newInit = newValue.init;
2793
- get = newValue.get || value.get;
2794
- set = newValue.set || value.set;
2795
- value = {
2796
- get,
2797
- set
2798
- };
2799
- } else value = newValue;
2800
- if (newInit !== void 0) if (init === void 0) init = newInit;
2801
- else if (typeof init === "function") init = [init, newInit];
2802
- else init.push(newInit);
2803
- }
2804
- }
2805
- if (kind === 0 || kind === 1) {
2806
- if (init === void 0) init = function(instance, init$1) {
2807
- return init$1;
2808
- };
2809
- else if (typeof init !== "function") {
2810
- var ownInitializers = init;
2811
- init = function(instance, init$1) {
2812
- var value$1 = init$1;
2813
- for (var i$1 = 0; i$1 < ownInitializers.length; i$1++) value$1 = ownInitializers[i$1].call(instance, value$1);
2814
- return value$1;
2815
- };
2816
- } else {
2817
- var originalInitializer = init;
2818
- init = function(instance, init$1) {
2819
- return originalInitializer.call(instance, init$1);
2820
- };
2821
- }
2822
- ret.push(init);
2823
- }
2824
- if (kind !== 0) {
2825
- if (kind === 1) {
2826
- desc.get = value.get;
2827
- desc.set = value.set;
2828
- } else if (kind === 2) desc.value = value;
2829
- else if (kind === 3) desc.get = value;
2830
- else if (kind === 4) desc.set = value;
2831
- if (isPrivate) if (kind === 1) {
2832
- ret.push(function(instance, args) {
2833
- return value.get.call(instance, args);
2834
- });
2835
- ret.push(function(instance, args) {
2836
- return value.set.call(instance, args);
2837
- });
2838
- } else if (kind === 2) ret.push(value);
2839
- else ret.push(function(instance, args) {
2840
- return value.call(instance, args);
2841
- });
2842
- else Object.defineProperty(base, name, desc);
2843
- }
2844
- }
2845
- function applyMemberDecs(Class, decInfos, metadata) {
2846
- var ret = [];
2847
- var protoInitializers;
2848
- var staticInitializers;
2849
- var existingProtoNonFields = /* @__PURE__ */ new Map();
2850
- var existingStaticNonFields = /* @__PURE__ */ new Map();
2851
- for (var i = 0; i < decInfos.length; i++) {
2852
- var decInfo = decInfos[i];
2853
- if (!Array.isArray(decInfo)) continue;
2854
- var kind = decInfo[1];
2855
- var name = decInfo[2];
2856
- var isPrivate = decInfo.length > 3;
2857
- var isStatic = kind >= 5;
2858
- var base;
2859
- var initializers;
2860
- if (isStatic) {
2861
- base = Class;
2862
- kind = kind - 5;
2863
- staticInitializers = staticInitializers || [];
2864
- initializers = staticInitializers;
2865
- } else {
2866
- base = Class.prototype;
2867
- protoInitializers = protoInitializers || [];
2868
- initializers = protoInitializers;
2869
- }
2870
- if (kind !== 0 && !isPrivate) {
2871
- var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
2872
- var existingKind = existingNonFields.get(name) || 0;
2873
- if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
2874
- else if (!existingKind && kind > 2) existingNonFields.set(name, kind);
2875
- else existingNonFields.set(name, true);
2876
- }
2877
- applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
2878
- }
2879
- pushInitializers(ret, protoInitializers);
2880
- pushInitializers(ret, staticInitializers);
2881
- return ret;
2882
- }
2883
- function pushInitializers(ret, initializers) {
2884
- if (initializers) ret.push(function(instance) {
2885
- for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
2886
- return instance;
2887
- });
2888
- }
2889
- function applyClassDecs(targetClass, classDecs, metadata) {
2890
- if (classDecs.length > 0) {
2891
- var initializers = [];
2892
- var newClass = targetClass;
2893
- var name = targetClass.name;
2894
- for (var i = classDecs.length - 1; i >= 0; i--) {
2895
- var decoratorFinishedRef = { v: false };
2896
- try {
2897
- var nextNewClass = classDecs[i](newClass, {
2898
- kind: "class",
2899
- name,
2900
- addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
2901
- metadata
2902
- });
2903
- } finally {
2904
- decoratorFinishedRef.v = true;
2905
- }
2906
- if (nextNewClass !== void 0) {
2907
- assertValidReturnValue(10, nextNewClass);
2908
- newClass = nextNewClass;
2909
- }
2910
- }
2911
- return [defineMetadata(newClass, metadata), function() {
2912
- for (var i$1 = 0; i$1 < initializers.length; i$1++) initializers[i$1].call(newClass);
2913
- }];
2914
- }
2915
- }
2916
- function defineMetadata(Class, metadata) {
2917
- return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
2918
- configurable: true,
2919
- enumerable: true,
2920
- value: metadata
2921
- });
2922
- }
2923
- return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
2924
- if (parentClass !== void 0) var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
2925
- var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
2926
- var e = applyMemberDecs(targetClass, memberDecs, metadata);
2927
- if (!classDecs.length) defineMetadata(targetClass, metadata);
2928
- return {
2929
- e,
2930
- get c() {
2931
- return applyClassDecs(targetClass, classDecs, metadata);
2932
- }
2933
- };
2934
- };
2935
- }
2936
- function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
2937
- return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
2938
- }
2939
- var _dec, _initClass, _Container$1;
2940
- /**
2941
- * A binding builder for the TestContainer that allows chaining binding operations.
2942
- */ var TestBindingBuilder = class {
2943
- container;
2944
- token;
2945
- constructor(container, token) {
2946
- this.container = container;
2947
- this.token = token;
2948
- }
2949
- /**
2950
- * Binds the token to a specific value.
2951
- * This is useful for testing with mock values or constants.
2952
- * @param value The value to bind to the token
2953
- */ toValue(value) {
2954
- const instanceName = this.container.getServiceLocator().getInstanceIdentifier(this.token);
2955
- this.container.getServiceLocator().getManager().storeCreatedHolder(instanceName, value, InjectableType.Class, InjectableScope.Singleton);
2956
- return this.container;
2957
- }
2958
- /**
2959
- * Binds the token to a class constructor.
2960
- * @param target The class constructor to bind to
2961
- */ toClass(target) {
2962
- this.container["registry"].set(this.token, InjectableScope.Singleton, target, InjectableType.Class);
2963
- return this.container;
2964
- }
2965
- };
2966
- let _TestContainer;
2967
- _dec = Injectable();
2968
- var TestContainer = class extends (_Container$1 = _Container) {
2969
- static {
2970
- ({c: [_TestContainer, _initClass]} = _apply_decs_2203_r(this, [], [_dec], _Container$1));
2971
- }
2972
- constructor(registry = globalRegistry, logger = null, injectors = void 0) {
2973
- super(registry, logger, injectors);
2974
- }
2975
- bind(token) {
2976
- let realToken = token;
2977
- if (typeof token === "function") realToken = getInjectableToken(token);
2978
- return new TestBindingBuilder(this, realToken);
2979
- }
2980
- bindValue(token, value) {
2981
- return this.bind(token).toValue(value);
2982
- }
2983
- bindClass(token, target) {
2984
- return this.bind(token).toClass(target);
2985
- }
2986
- /**
2987
- * Creates a new TestContainer instance with the same configuration.
2988
- * This is useful for creating isolated test containers.
2989
- * @returns A new TestContainer instance
2990
- */ createChild() {
2991
- return new _TestContainer(this.registry, this.logger, this.injectors);
2992
- }
2993
2625
  static {
2994
2626
  _initClass();
2995
2627
  }
2996
2628
  };
2997
2629
 
2998
2630
  //#endregion
2999
- //#region src/browser.mts
3000
- /**
3001
- * Browser-specific entry point for @navios/di.
3002
- *
3003
- * This entry point forces the use of SyncLocalStorage instead of
3004
- * Node's AsyncLocalStorage, making it safe for browser environments.
3005
- *
3006
- * The browser build is automatically selected by bundlers that respect
3007
- * the "browser" condition in package.json exports.
3008
- */ __testing__.forceSyncMode();
3009
-
3010
- //#endregion
3011
- export { BaseHolderManager, BoundInjectionToken, CircularDetector, _Container as Container, DIError, DIErrorCode, DefaultRequestContext, _EventEmitter as EventEmitter, Factory, FactoryInjectionToken, HolderManager, Injectable, InjectableScope, InjectableTokenMeta, InjectableType, InjectionToken, InstanceResolver, InstanceStatus, Instantiator, Invalidator, LifecycleEventBus, Registry, RequestStorage, ScopedContainer, ServiceLocator, SingletonStorage, TestBindingBuilder, _TestContainer as TestContainer, TokenProcessor, asyncInject, createRequestContext, defaultInjectors, getCurrentResolutionContext, getInjectableToken, getInjectors, globalRegistry, inject, optional, provideFactoryContext, resolutionContext, withResolutionContext, withoutResolutionContext, wrapSyncInit };
2631
+ export { BaseHolderManager, BoundInjectionToken, CircularDetector, _Container as Container, DIError, DIErrorCode, DefaultRequestContext, _EventEmitter as EventEmitter, Factory, FactoryInjectionToken, HolderManager, Injectable, InjectableScope, InjectableTokenMeta, InjectableType, InjectionToken, InstanceResolver, InstanceStatus, Instantiator, Invalidator, LifecycleEventBus, Registry, RequestStorage, ScopedContainer, ServiceLocator, SingletonStorage, TokenProcessor, asyncInject, createRequestContext, defaultInjectors, getCurrentResolutionContext, getInjectableToken, getInjectors, globalRegistry, inject, optional, provideFactoryContext, withResolutionContext, withoutResolutionContext, wrapSyncInit };
3012
2632
  //# sourceMappingURL=index.mjs.map