@navios/di 0.6.1 → 0.7.1
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.
- package/CHANGELOG.md +47 -0
- package/lib/browser/index.d.mts +15 -84
- package/lib/browser/index.d.mts.map +1 -1
- package/lib/browser/index.mjs +121 -484
- package/lib/browser/index.mjs.map +1 -1
- package/lib/{index-DW3K5sOX.d.cts → container-BuAutHGg.d.mts} +17 -62
- package/lib/container-BuAutHGg.d.mts.map +1 -0
- package/lib/{testing-DIaIRiJz.cjs → container-DnzgpfBe.cjs} +81 -459
- package/lib/container-DnzgpfBe.cjs.map +1 -0
- package/lib/{testing-BG_fa9TJ.mjs → container-Pb_Y4Z4x.mjs} +81 -446
- package/lib/container-Pb_Y4Z4x.mjs.map +1 -0
- package/lib/{index-7jfWsiG4.d.mts → container-oGTgX2iX.d.cts} +12 -67
- package/lib/container-oGTgX2iX.d.cts.map +1 -0
- package/lib/index.cjs +44 -52
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +6 -25
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +6 -25
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +2 -2
- package/lib/testing/index.cjs +343 -4
- package/lib/testing/index.cjs.map +1 -0
- package/lib/testing/index.d.cts +65 -2
- package/lib/testing/index.d.cts.map +1 -0
- package/lib/testing/index.d.mts +65 -2
- package/lib/testing/index.d.mts.map +1 -0
- package/lib/testing/index.mjs +341 -2
- package/lib/testing/index.mjs.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/async-local-storage.browser.spec.mts +18 -92
- package/src/__tests__/container.spec.mts +93 -0
- package/src/__tests__/e2e.browser.spec.mts +7 -15
- package/src/__tests__/library-findings.spec.mts +23 -21
- package/src/browser.mts +4 -9
- package/src/container/scoped-container.mts +14 -8
- package/src/index.mts +5 -8
- package/src/internal/context/async-local-storage.browser.mts +19 -0
- package/src/internal/context/async-local-storage.mts +46 -98
- package/src/internal/context/async-local-storage.types.mts +7 -0
- package/src/internal/context/resolution-context.mts +23 -5
- package/src/internal/context/sync-local-storage.mts +3 -1
- package/src/internal/core/instance-resolver.mts +8 -1
- package/src/internal/lifecycle/circular-detector.mts +15 -0
- package/src/token/registry.mts +21 -0
- package/tsdown.config.mts +12 -1
- package/vitest.config.mts +25 -3
- package/lib/index-7jfWsiG4.d.mts.map +0 -1
- package/lib/index-DW3K5sOX.d.cts.map +0 -1
- package/lib/testing-BG_fa9TJ.mjs.map +0 -1
- package/lib/testing-DIaIRiJz.cjs.map +0 -1
package/lib/browser/index.mjs
CHANGED
|
@@ -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
|
/**
|
|
@@ -218,6 +104,7 @@ var FactoryInjectionToken = class {
|
|
|
218
104
|
//#endregion
|
|
219
105
|
//#region src/token/registry.mts
|
|
220
106
|
var Registry = class {
|
|
107
|
+
parent;
|
|
221
108
|
factories = /* @__PURE__ */ new Map();
|
|
222
109
|
constructor(parent) {
|
|
223
110
|
this.parent = parent;
|
|
@@ -246,6 +133,23 @@ var Registry = class {
|
|
|
246
133
|
delete(token) {
|
|
247
134
|
this.factories.delete(token.id);
|
|
248
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Updates the scope of an already registered factory.
|
|
138
|
+
* This is useful when you need to dynamically change a service's scope
|
|
139
|
+
* (e.g., when a singleton controller has request-scoped dependencies).
|
|
140
|
+
*
|
|
141
|
+
* @param token The injection token to update
|
|
142
|
+
* @param scope The new scope to set
|
|
143
|
+
* @returns true if the scope was updated, false if the token was not found
|
|
144
|
+
*/ updateScope(token, scope) {
|
|
145
|
+
const factory = this.factories.get(token.id);
|
|
146
|
+
if (factory) {
|
|
147
|
+
factory.scope = scope;
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
if (this.parent) return this.parent.updateScope(token, scope);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
249
153
|
};
|
|
250
154
|
const globalRegistry = new Registry();
|
|
251
155
|
|
|
@@ -310,6 +214,70 @@ var DIError = class DIError extends Error {
|
|
|
310
214
|
}
|
|
311
215
|
};
|
|
312
216
|
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/internal/context/sync-local-storage.mts
|
|
219
|
+
/**
|
|
220
|
+
* A synchronous-only polyfill for AsyncLocalStorage.
|
|
221
|
+
*
|
|
222
|
+
* This provides the same API as Node's AsyncLocalStorage but only works
|
|
223
|
+
* for synchronous code paths. It uses a simple stack-based approach.
|
|
224
|
+
*
|
|
225
|
+
* Limitations:
|
|
226
|
+
* - Context does NOT propagate across async boundaries (setTimeout, promises, etc.)
|
|
227
|
+
* - Only suitable for environments where DI resolution is synchronous
|
|
228
|
+
*
|
|
229
|
+
* This is acceptable for browser environments where:
|
|
230
|
+
* 1. Constructors are typically synchronous
|
|
231
|
+
* 2. Circular dependency detection mainly needs sync tracking
|
|
232
|
+
*/
|
|
233
|
+
var SyncLocalStorage = class {
|
|
234
|
+
stack = [];
|
|
235
|
+
/**
|
|
236
|
+
* Runs a function within the given store context.
|
|
237
|
+
* The context is only available synchronously within the function.
|
|
238
|
+
*/
|
|
239
|
+
run(store, fn) {
|
|
240
|
+
this.stack.push(store);
|
|
241
|
+
try {
|
|
242
|
+
return fn();
|
|
243
|
+
} finally {
|
|
244
|
+
this.stack.pop();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Gets the current store value, or undefined if not in a context.
|
|
249
|
+
*/
|
|
250
|
+
getStore() {
|
|
251
|
+
return this.stack.length > 0 ? this.stack[this.stack.length - 1] : void 0;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Exits the current context and runs the function without any store.
|
|
255
|
+
* This matches AsyncLocalStorage.exit() behavior.
|
|
256
|
+
*/
|
|
257
|
+
exit(fn) {
|
|
258
|
+
const savedStack = this.stack;
|
|
259
|
+
this.stack = [];
|
|
260
|
+
try {
|
|
261
|
+
return fn();
|
|
262
|
+
} finally {
|
|
263
|
+
this.stack = savedStack;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region src/internal/context/async-local-storage.browser.mts
|
|
270
|
+
/**
|
|
271
|
+
* Browser implementation using SyncLocalStorage.
|
|
272
|
+
*
|
|
273
|
+
* This module is used in browser environments where async_hooks is not available.
|
|
274
|
+
* It provides synchronous-only context tracking which is sufficient for
|
|
275
|
+
* browser-based DI resolution.
|
|
276
|
+
*/
|
|
277
|
+
function createAsyncLocalStorage() {
|
|
278
|
+
return new SyncLocalStorage();
|
|
279
|
+
}
|
|
280
|
+
|
|
313
281
|
//#endregion
|
|
314
282
|
//#region src/internal/context/resolution-context.mts
|
|
315
283
|
/**
|
|
@@ -318,7 +286,16 @@ var DIError = class DIError extends Error {
|
|
|
318
286
|
* This allows tracking which service is being instantiated even across
|
|
319
287
|
* async boundaries (like when inject() is called inside a constructor).
|
|
320
288
|
* Essential for circular dependency detection.
|
|
321
|
-
|
|
289
|
+
*
|
|
290
|
+
* The actual implementation varies by environment:
|
|
291
|
+
* - Production: No-op (returns undefined, run() just calls fn directly)
|
|
292
|
+
* - Development: Real AsyncLocalStorage with full async tracking
|
|
293
|
+
* - Browser: SyncLocalStorage for synchronous-only tracking
|
|
294
|
+
*/ let resolutionContext = null;
|
|
295
|
+
function getResolutionContext() {
|
|
296
|
+
if (!resolutionContext) resolutionContext = createAsyncLocalStorage();
|
|
297
|
+
return resolutionContext;
|
|
298
|
+
}
|
|
322
299
|
/**
|
|
323
300
|
* Runs a function within a resolution context.
|
|
324
301
|
*
|
|
@@ -329,7 +306,7 @@ var DIError = class DIError extends Error {
|
|
|
329
306
|
* @param getHolder Function to retrieve holders by name
|
|
330
307
|
* @param fn The function to run within the context
|
|
331
308
|
*/ function withResolutionContext(waiterHolder, getHolder, fn) {
|
|
332
|
-
return
|
|
309
|
+
return getResolutionContext().run({
|
|
333
310
|
waiterHolder,
|
|
334
311
|
getHolder
|
|
335
312
|
}, fn);
|
|
@@ -340,7 +317,7 @@ var DIError = class DIError extends Error {
|
|
|
340
317
|
* Returns undefined if we're not inside a resolution context
|
|
341
318
|
* (e.g., when resolving a top-level service that has no parent).
|
|
342
319
|
*/ function getCurrentResolutionContext() {
|
|
343
|
-
return
|
|
320
|
+
return getResolutionContext().getStore();
|
|
344
321
|
}
|
|
345
322
|
/**
|
|
346
323
|
* Runs a function outside any resolution context.
|
|
@@ -350,7 +327,7 @@ var DIError = class DIError extends Error {
|
|
|
350
327
|
*
|
|
351
328
|
* @param fn The function to run without resolution context
|
|
352
329
|
*/ function withoutResolutionContext(fn) {
|
|
353
|
-
return
|
|
330
|
+
return getResolutionContext().run(void 0, fn);
|
|
354
331
|
}
|
|
355
332
|
|
|
356
333
|
//#endregion
|
|
@@ -492,6 +469,8 @@ const provideFactoryContext = defaultInjectors.provideFactoryContext;
|
|
|
492
469
|
*
|
|
493
470
|
* Uses BFS to traverse the waitingFor graph starting from a target holder
|
|
494
471
|
* and checks if following the chain leads back to the waiter, indicating a circular dependency.
|
|
472
|
+
*
|
|
473
|
+
* Note: In production (NODE_ENV === 'production'), detection is skipped for performance.
|
|
495
474
|
*/ var CircularDetector = class {
|
|
496
475
|
/**
|
|
497
476
|
* Detects if waiting for `targetName` from `waiterName` would create a cycle.
|
|
@@ -499,6 +478,8 @@ const provideFactoryContext = defaultInjectors.provideFactoryContext;
|
|
|
499
478
|
* This works by checking if `targetName` (or any holder in its waitingFor chain)
|
|
500
479
|
* is currently waiting for `waiterName`. If so, waiting would create a deadlock.
|
|
501
480
|
*
|
|
481
|
+
* In production mode, this always returns null to skip the BFS traversal overhead.
|
|
482
|
+
*
|
|
502
483
|
* @param waiterName The name of the holder that wants to wait
|
|
503
484
|
* @param targetName The name of the holder being waited on
|
|
504
485
|
* @param getHolder Function to retrieve a holder by name
|
|
@@ -944,7 +925,8 @@ var RequestStorage = class {
|
|
|
944
925
|
*/ async resolveRequestScoped(token, args) {
|
|
945
926
|
const instanceName = this.parent.getServiceLocator().getInstanceIdentifier(token, args);
|
|
946
927
|
const existingHolder = this.requestContextHolder.get(instanceName);
|
|
947
|
-
if (existingHolder)
|
|
928
|
+
if (existingHolder) if (existingHolder.status === InstanceStatus.Error) this.requestContextHolder.delete(instanceName);
|
|
929
|
+
else {
|
|
948
930
|
const [error, readyHolder] = await BaseHolderManager.waitForHolderReady(existingHolder);
|
|
949
931
|
if (error) throw error;
|
|
950
932
|
return readyHolder.instance;
|
|
@@ -1115,7 +1097,12 @@ var SingletonStorage = class {
|
|
|
1115
1097
|
return [void 0, readyResult[1].instance];
|
|
1116
1098
|
}
|
|
1117
1099
|
return null;
|
|
1118
|
-
default:
|
|
1100
|
+
default:
|
|
1101
|
+
if (holder) {
|
|
1102
|
+
this.logger?.log(`[InstanceResolver] Removing failed instance ${instanceName} from storage to allow retry`);
|
|
1103
|
+
storage.delete(instanceName);
|
|
1104
|
+
}
|
|
1105
|
+
return null;
|
|
1119
1106
|
}
|
|
1120
1107
|
}
|
|
1121
1108
|
/**
|
|
@@ -1912,7 +1899,7 @@ var SingletonStorage = class {
|
|
|
1912
1899
|
|
|
1913
1900
|
//#endregion
|
|
1914
1901
|
//#region src/container/container.mts
|
|
1915
|
-
function applyDecs2203RFactory$
|
|
1902
|
+
function applyDecs2203RFactory$1() {
|
|
1916
1903
|
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
1917
1904
|
return function addInitializer(initializer) {
|
|
1918
1905
|
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
@@ -2186,18 +2173,18 @@ function applyDecs2203RFactory$2() {
|
|
|
2186
2173
|
};
|
|
2187
2174
|
};
|
|
2188
2175
|
}
|
|
2189
|
-
function _apply_decs_2203_r$
|
|
2190
|
-
return (_apply_decs_2203_r$
|
|
2176
|
+
function _apply_decs_2203_r$1(targetClass, memberDecs, classDecs, parentClass) {
|
|
2177
|
+
return (_apply_decs_2203_r$1 = applyDecs2203RFactory$1())(targetClass, memberDecs, classDecs, parentClass);
|
|
2191
2178
|
}
|
|
2192
|
-
var _dec$
|
|
2179
|
+
var _dec$1, _initClass$1;
|
|
2193
2180
|
let _Container;
|
|
2194
|
-
_dec$
|
|
2181
|
+
_dec$1 = Injectable();
|
|
2195
2182
|
var Container = class {
|
|
2196
2183
|
registry;
|
|
2197
2184
|
logger;
|
|
2198
2185
|
injectors;
|
|
2199
2186
|
static {
|
|
2200
|
-
({c: [_Container, _initClass$
|
|
2187
|
+
({c: [_Container, _initClass$1]} = _apply_decs_2203_r$1(this, [], [_dec$1]));
|
|
2201
2188
|
}
|
|
2202
2189
|
constructor(registry = globalRegistry, logger = null, injectors = defaultInjectors) {
|
|
2203
2190
|
this.registry = registry;
|
|
@@ -2327,7 +2314,7 @@ var Container = class {
|
|
|
2327
2314
|
return this.serviceLocator.clearAll();
|
|
2328
2315
|
}
|
|
2329
2316
|
static {
|
|
2330
|
-
_initClass$
|
|
2317
|
+
_initClass$1();
|
|
2331
2318
|
}
|
|
2332
2319
|
};
|
|
2333
2320
|
|
|
@@ -2345,7 +2332,7 @@ function Factory({ scope = InjectableScope.Singleton, token, registry = globalRe
|
|
|
2345
2332
|
|
|
2346
2333
|
//#endregion
|
|
2347
2334
|
//#region src/event-emitter.mts
|
|
2348
|
-
function applyDecs2203RFactory
|
|
2335
|
+
function applyDecs2203RFactory() {
|
|
2349
2336
|
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
2350
2337
|
return function addInitializer(initializer) {
|
|
2351
2338
|
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
@@ -2619,15 +2606,15 @@ function applyDecs2203RFactory$1() {
|
|
|
2619
2606
|
};
|
|
2620
2607
|
};
|
|
2621
2608
|
}
|
|
2622
|
-
function _apply_decs_2203_r
|
|
2623
|
-
return (_apply_decs_2203_r
|
|
2609
|
+
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
2610
|
+
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
2624
2611
|
}
|
|
2625
|
-
var _dec
|
|
2612
|
+
var _dec, _initClass;
|
|
2626
2613
|
let _EventEmitter;
|
|
2627
|
-
_dec
|
|
2614
|
+
_dec = Injectable({ scope: InjectableScope.Transient });
|
|
2628
2615
|
var EventEmitter = class {
|
|
2629
2616
|
static {
|
|
2630
|
-
({c: [_EventEmitter, _initClass
|
|
2617
|
+
({c: [_EventEmitter, _initClass]} = _apply_decs_2203_r(this, [], [_dec]));
|
|
2631
2618
|
}
|
|
2632
2619
|
listeners = /* @__PURE__ */ new Map();
|
|
2633
2620
|
on(event, listener) {
|
|
@@ -2653,361 +2640,11 @@ var EventEmitter = class {
|
|
|
2653
2640
|
if (!this.listeners.has(event)) return;
|
|
2654
2641
|
return Promise.all(Array.from(this.listeners.get(event)).map((listener) => listener(...args)));
|
|
2655
2642
|
}
|
|
2656
|
-
static {
|
|
2657
|
-
_initClass$1();
|
|
2658
|
-
}
|
|
2659
|
-
};
|
|
2660
|
-
|
|
2661
|
-
//#endregion
|
|
2662
|
-
//#region src/testing/test-container.mts
|
|
2663
|
-
function applyDecs2203RFactory() {
|
|
2664
|
-
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
2665
|
-
return function addInitializer(initializer) {
|
|
2666
|
-
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
2667
|
-
assertCallable(initializer, "An initializer");
|
|
2668
|
-
initializers.push(initializer);
|
|
2669
|
-
};
|
|
2670
|
-
}
|
|
2671
|
-
function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
|
|
2672
|
-
var kindStr;
|
|
2673
|
-
switch (kind) {
|
|
2674
|
-
case 1:
|
|
2675
|
-
kindStr = "accessor";
|
|
2676
|
-
break;
|
|
2677
|
-
case 2:
|
|
2678
|
-
kindStr = "method";
|
|
2679
|
-
break;
|
|
2680
|
-
case 3:
|
|
2681
|
-
kindStr = "getter";
|
|
2682
|
-
break;
|
|
2683
|
-
case 4:
|
|
2684
|
-
kindStr = "setter";
|
|
2685
|
-
break;
|
|
2686
|
-
default: kindStr = "field";
|
|
2687
|
-
}
|
|
2688
|
-
var ctx = {
|
|
2689
|
-
kind: kindStr,
|
|
2690
|
-
name: isPrivate ? "#" + name : name,
|
|
2691
|
-
static: isStatic,
|
|
2692
|
-
private: isPrivate,
|
|
2693
|
-
metadata
|
|
2694
|
-
};
|
|
2695
|
-
var decoratorFinishedRef = { v: false };
|
|
2696
|
-
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
|
|
2697
|
-
var get, set;
|
|
2698
|
-
if (kind === 0) if (isPrivate) {
|
|
2699
|
-
get = desc.get;
|
|
2700
|
-
set = desc.set;
|
|
2701
|
-
} else {
|
|
2702
|
-
get = function() {
|
|
2703
|
-
return this[name];
|
|
2704
|
-
};
|
|
2705
|
-
set = function(v) {
|
|
2706
|
-
this[name] = v;
|
|
2707
|
-
};
|
|
2708
|
-
}
|
|
2709
|
-
else if (kind === 2) get = function() {
|
|
2710
|
-
return desc.value;
|
|
2711
|
-
};
|
|
2712
|
-
else {
|
|
2713
|
-
if (kind === 1 || kind === 3) get = function() {
|
|
2714
|
-
return desc.get.call(this);
|
|
2715
|
-
};
|
|
2716
|
-
if (kind === 1 || kind === 4) set = function(v) {
|
|
2717
|
-
desc.set.call(this, v);
|
|
2718
|
-
};
|
|
2719
|
-
}
|
|
2720
|
-
ctx.access = get && set ? {
|
|
2721
|
-
get,
|
|
2722
|
-
set
|
|
2723
|
-
} : get ? { get } : { set };
|
|
2724
|
-
try {
|
|
2725
|
-
return dec(value, ctx);
|
|
2726
|
-
} finally {
|
|
2727
|
-
decoratorFinishedRef.v = true;
|
|
2728
|
-
}
|
|
2729
|
-
}
|
|
2730
|
-
function assertNotFinished(decoratorFinishedRef, fnName) {
|
|
2731
|
-
if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
|
|
2732
|
-
}
|
|
2733
|
-
function assertCallable(fn, hint) {
|
|
2734
|
-
if (typeof fn !== "function") throw new TypeError(hint + " must be a function");
|
|
2735
|
-
}
|
|
2736
|
-
function assertValidReturnValue(kind, value) {
|
|
2737
|
-
var type = typeof value;
|
|
2738
|
-
if (kind === 1) {
|
|
2739
|
-
if (type !== "object" || value === null) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
|
|
2740
|
-
if (value.get !== void 0) assertCallable(value.get, "accessor.get");
|
|
2741
|
-
if (value.set !== void 0) assertCallable(value.set, "accessor.set");
|
|
2742
|
-
if (value.init !== void 0) assertCallable(value.init, "accessor.init");
|
|
2743
|
-
} else if (type !== "function") {
|
|
2744
|
-
var hint;
|
|
2745
|
-
if (kind === 0) hint = "field";
|
|
2746
|
-
else if (kind === 10) hint = "class";
|
|
2747
|
-
else hint = "method";
|
|
2748
|
-
throw new TypeError(hint + " decorators must return a function or void 0");
|
|
2749
|
-
}
|
|
2750
|
-
}
|
|
2751
|
-
function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
|
|
2752
|
-
var decs = decInfo[0];
|
|
2753
|
-
var desc, init, value;
|
|
2754
|
-
if (isPrivate) if (kind === 0 || kind === 1) desc = {
|
|
2755
|
-
get: decInfo[3],
|
|
2756
|
-
set: decInfo[4]
|
|
2757
|
-
};
|
|
2758
|
-
else if (kind === 3) desc = { get: decInfo[3] };
|
|
2759
|
-
else if (kind === 4) desc = { set: decInfo[3] };
|
|
2760
|
-
else desc = { value: decInfo[3] };
|
|
2761
|
-
else if (kind !== 0) desc = Object.getOwnPropertyDescriptor(base, name);
|
|
2762
|
-
if (kind === 1) value = {
|
|
2763
|
-
get: desc.get,
|
|
2764
|
-
set: desc.set
|
|
2765
|
-
};
|
|
2766
|
-
else if (kind === 2) value = desc.value;
|
|
2767
|
-
else if (kind === 3) value = desc.get;
|
|
2768
|
-
else if (kind === 4) value = desc.set;
|
|
2769
|
-
var newValue, get, set;
|
|
2770
|
-
if (typeof decs === "function") {
|
|
2771
|
-
newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
2772
|
-
if (newValue !== void 0) {
|
|
2773
|
-
assertValidReturnValue(kind, newValue);
|
|
2774
|
-
if (kind === 0) init = newValue;
|
|
2775
|
-
else if (kind === 1) {
|
|
2776
|
-
init = newValue.init;
|
|
2777
|
-
get = newValue.get || value.get;
|
|
2778
|
-
set = newValue.set || value.set;
|
|
2779
|
-
value = {
|
|
2780
|
-
get,
|
|
2781
|
-
set
|
|
2782
|
-
};
|
|
2783
|
-
} else value = newValue;
|
|
2784
|
-
}
|
|
2785
|
-
} else for (var i = decs.length - 1; i >= 0; i--) {
|
|
2786
|
-
var dec = decs[i];
|
|
2787
|
-
newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
2788
|
-
if (newValue !== void 0) {
|
|
2789
|
-
assertValidReturnValue(kind, newValue);
|
|
2790
|
-
var newInit;
|
|
2791
|
-
if (kind === 0) newInit = newValue;
|
|
2792
|
-
else if (kind === 1) {
|
|
2793
|
-
newInit = newValue.init;
|
|
2794
|
-
get = newValue.get || value.get;
|
|
2795
|
-
set = newValue.set || value.set;
|
|
2796
|
-
value = {
|
|
2797
|
-
get,
|
|
2798
|
-
set
|
|
2799
|
-
};
|
|
2800
|
-
} else value = newValue;
|
|
2801
|
-
if (newInit !== void 0) if (init === void 0) init = newInit;
|
|
2802
|
-
else if (typeof init === "function") init = [init, newInit];
|
|
2803
|
-
else init.push(newInit);
|
|
2804
|
-
}
|
|
2805
|
-
}
|
|
2806
|
-
if (kind === 0 || kind === 1) {
|
|
2807
|
-
if (init === void 0) init = function(instance, init$1) {
|
|
2808
|
-
return init$1;
|
|
2809
|
-
};
|
|
2810
|
-
else if (typeof init !== "function") {
|
|
2811
|
-
var ownInitializers = init;
|
|
2812
|
-
init = function(instance, init$1) {
|
|
2813
|
-
var value$1 = init$1;
|
|
2814
|
-
for (var i$1 = 0; i$1 < ownInitializers.length; i$1++) value$1 = ownInitializers[i$1].call(instance, value$1);
|
|
2815
|
-
return value$1;
|
|
2816
|
-
};
|
|
2817
|
-
} else {
|
|
2818
|
-
var originalInitializer = init;
|
|
2819
|
-
init = function(instance, init$1) {
|
|
2820
|
-
return originalInitializer.call(instance, init$1);
|
|
2821
|
-
};
|
|
2822
|
-
}
|
|
2823
|
-
ret.push(init);
|
|
2824
|
-
}
|
|
2825
|
-
if (kind !== 0) {
|
|
2826
|
-
if (kind === 1) {
|
|
2827
|
-
desc.get = value.get;
|
|
2828
|
-
desc.set = value.set;
|
|
2829
|
-
} else if (kind === 2) desc.value = value;
|
|
2830
|
-
else if (kind === 3) desc.get = value;
|
|
2831
|
-
else if (kind === 4) desc.set = value;
|
|
2832
|
-
if (isPrivate) if (kind === 1) {
|
|
2833
|
-
ret.push(function(instance, args) {
|
|
2834
|
-
return value.get.call(instance, args);
|
|
2835
|
-
});
|
|
2836
|
-
ret.push(function(instance, args) {
|
|
2837
|
-
return value.set.call(instance, args);
|
|
2838
|
-
});
|
|
2839
|
-
} else if (kind === 2) ret.push(value);
|
|
2840
|
-
else ret.push(function(instance, args) {
|
|
2841
|
-
return value.call(instance, args);
|
|
2842
|
-
});
|
|
2843
|
-
else Object.defineProperty(base, name, desc);
|
|
2844
|
-
}
|
|
2845
|
-
}
|
|
2846
|
-
function applyMemberDecs(Class, decInfos, metadata) {
|
|
2847
|
-
var ret = [];
|
|
2848
|
-
var protoInitializers;
|
|
2849
|
-
var staticInitializers;
|
|
2850
|
-
var existingProtoNonFields = /* @__PURE__ */ new Map();
|
|
2851
|
-
var existingStaticNonFields = /* @__PURE__ */ new Map();
|
|
2852
|
-
for (var i = 0; i < decInfos.length; i++) {
|
|
2853
|
-
var decInfo = decInfos[i];
|
|
2854
|
-
if (!Array.isArray(decInfo)) continue;
|
|
2855
|
-
var kind = decInfo[1];
|
|
2856
|
-
var name = decInfo[2];
|
|
2857
|
-
var isPrivate = decInfo.length > 3;
|
|
2858
|
-
var isStatic = kind >= 5;
|
|
2859
|
-
var base;
|
|
2860
|
-
var initializers;
|
|
2861
|
-
if (isStatic) {
|
|
2862
|
-
base = Class;
|
|
2863
|
-
kind = kind - 5;
|
|
2864
|
-
staticInitializers = staticInitializers || [];
|
|
2865
|
-
initializers = staticInitializers;
|
|
2866
|
-
} else {
|
|
2867
|
-
base = Class.prototype;
|
|
2868
|
-
protoInitializers = protoInitializers || [];
|
|
2869
|
-
initializers = protoInitializers;
|
|
2870
|
-
}
|
|
2871
|
-
if (kind !== 0 && !isPrivate) {
|
|
2872
|
-
var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
|
|
2873
|
-
var existingKind = existingNonFields.get(name) || 0;
|
|
2874
|
-
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);
|
|
2875
|
-
else if (!existingKind && kind > 2) existingNonFields.set(name, kind);
|
|
2876
|
-
else existingNonFields.set(name, true);
|
|
2877
|
-
}
|
|
2878
|
-
applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
|
|
2879
|
-
}
|
|
2880
|
-
pushInitializers(ret, protoInitializers);
|
|
2881
|
-
pushInitializers(ret, staticInitializers);
|
|
2882
|
-
return ret;
|
|
2883
|
-
}
|
|
2884
|
-
function pushInitializers(ret, initializers) {
|
|
2885
|
-
if (initializers) ret.push(function(instance) {
|
|
2886
|
-
for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
|
|
2887
|
-
return instance;
|
|
2888
|
-
});
|
|
2889
|
-
}
|
|
2890
|
-
function applyClassDecs(targetClass, classDecs, metadata) {
|
|
2891
|
-
if (classDecs.length > 0) {
|
|
2892
|
-
var initializers = [];
|
|
2893
|
-
var newClass = targetClass;
|
|
2894
|
-
var name = targetClass.name;
|
|
2895
|
-
for (var i = classDecs.length - 1; i >= 0; i--) {
|
|
2896
|
-
var decoratorFinishedRef = { v: false };
|
|
2897
|
-
try {
|
|
2898
|
-
var nextNewClass = classDecs[i](newClass, {
|
|
2899
|
-
kind: "class",
|
|
2900
|
-
name,
|
|
2901
|
-
addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
|
|
2902
|
-
metadata
|
|
2903
|
-
});
|
|
2904
|
-
} finally {
|
|
2905
|
-
decoratorFinishedRef.v = true;
|
|
2906
|
-
}
|
|
2907
|
-
if (nextNewClass !== void 0) {
|
|
2908
|
-
assertValidReturnValue(10, nextNewClass);
|
|
2909
|
-
newClass = nextNewClass;
|
|
2910
|
-
}
|
|
2911
|
-
}
|
|
2912
|
-
return [defineMetadata(newClass, metadata), function() {
|
|
2913
|
-
for (var i$1 = 0; i$1 < initializers.length; i$1++) initializers[i$1].call(newClass);
|
|
2914
|
-
}];
|
|
2915
|
-
}
|
|
2916
|
-
}
|
|
2917
|
-
function defineMetadata(Class, metadata) {
|
|
2918
|
-
return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
|
|
2919
|
-
configurable: true,
|
|
2920
|
-
enumerable: true,
|
|
2921
|
-
value: metadata
|
|
2922
|
-
});
|
|
2923
|
-
}
|
|
2924
|
-
return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
|
|
2925
|
-
if (parentClass !== void 0) var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
|
|
2926
|
-
var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
|
|
2927
|
-
var e = applyMemberDecs(targetClass, memberDecs, metadata);
|
|
2928
|
-
if (!classDecs.length) defineMetadata(targetClass, metadata);
|
|
2929
|
-
return {
|
|
2930
|
-
e,
|
|
2931
|
-
get c() {
|
|
2932
|
-
return applyClassDecs(targetClass, classDecs, metadata);
|
|
2933
|
-
}
|
|
2934
|
-
};
|
|
2935
|
-
};
|
|
2936
|
-
}
|
|
2937
|
-
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
2938
|
-
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
2939
|
-
}
|
|
2940
|
-
var _dec, _initClass, _Container$1;
|
|
2941
|
-
/**
|
|
2942
|
-
* A binding builder for the TestContainer that allows chaining binding operations.
|
|
2943
|
-
*/ var TestBindingBuilder = class {
|
|
2944
|
-
container;
|
|
2945
|
-
token;
|
|
2946
|
-
constructor(container, token) {
|
|
2947
|
-
this.container = container;
|
|
2948
|
-
this.token = token;
|
|
2949
|
-
}
|
|
2950
|
-
/**
|
|
2951
|
-
* Binds the token to a specific value.
|
|
2952
|
-
* This is useful for testing with mock values or constants.
|
|
2953
|
-
* @param value The value to bind to the token
|
|
2954
|
-
*/ toValue(value) {
|
|
2955
|
-
const instanceName = this.container.getServiceLocator().getInstanceIdentifier(this.token);
|
|
2956
|
-
this.container.getServiceLocator().getManager().storeCreatedHolder(instanceName, value, InjectableType.Class, InjectableScope.Singleton);
|
|
2957
|
-
return this.container;
|
|
2958
|
-
}
|
|
2959
|
-
/**
|
|
2960
|
-
* Binds the token to a class constructor.
|
|
2961
|
-
* @param target The class constructor to bind to
|
|
2962
|
-
*/ toClass(target) {
|
|
2963
|
-
this.container["registry"].set(this.token, InjectableScope.Singleton, target, InjectableType.Class);
|
|
2964
|
-
return this.container;
|
|
2965
|
-
}
|
|
2966
|
-
};
|
|
2967
|
-
let _TestContainer;
|
|
2968
|
-
_dec = Injectable();
|
|
2969
|
-
var TestContainer = class extends (_Container$1 = _Container) {
|
|
2970
|
-
static {
|
|
2971
|
-
({c: [_TestContainer, _initClass]} = _apply_decs_2203_r(this, [], [_dec], _Container$1));
|
|
2972
|
-
}
|
|
2973
|
-
constructor(registry = globalRegistry, logger = null, injectors = void 0) {
|
|
2974
|
-
super(registry, logger, injectors);
|
|
2975
|
-
}
|
|
2976
|
-
bind(token) {
|
|
2977
|
-
let realToken = token;
|
|
2978
|
-
if (typeof token === "function") realToken = getInjectableToken(token);
|
|
2979
|
-
return new TestBindingBuilder(this, realToken);
|
|
2980
|
-
}
|
|
2981
|
-
bindValue(token, value) {
|
|
2982
|
-
return this.bind(token).toValue(value);
|
|
2983
|
-
}
|
|
2984
|
-
bindClass(token, target) {
|
|
2985
|
-
return this.bind(token).toClass(target);
|
|
2986
|
-
}
|
|
2987
|
-
/**
|
|
2988
|
-
* Creates a new TestContainer instance with the same configuration.
|
|
2989
|
-
* This is useful for creating isolated test containers.
|
|
2990
|
-
* @returns A new TestContainer instance
|
|
2991
|
-
*/ createChild() {
|
|
2992
|
-
return new _TestContainer(this.registry, this.logger, this.injectors);
|
|
2993
|
-
}
|
|
2994
2643
|
static {
|
|
2995
2644
|
_initClass();
|
|
2996
2645
|
}
|
|
2997
2646
|
};
|
|
2998
2647
|
|
|
2999
2648
|
//#endregion
|
|
3000
|
-
|
|
3001
|
-
/**
|
|
3002
|
-
* Browser-specific entry point for @navios/di.
|
|
3003
|
-
*
|
|
3004
|
-
* This entry point forces the use of SyncLocalStorage instead of
|
|
3005
|
-
* Node's AsyncLocalStorage, making it safe for browser environments.
|
|
3006
|
-
*
|
|
3007
|
-
* The browser build is automatically selected by bundlers that respect
|
|
3008
|
-
* the "browser" condition in package.json exports.
|
|
3009
|
-
*/ __testing__.forceSyncMode();
|
|
3010
|
-
|
|
3011
|
-
//#endregion
|
|
3012
|
-
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 };
|
|
2649
|
+
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 };
|
|
3013
2650
|
//# sourceMappingURL=index.mjs.map
|