@stacksjs/bun-router 0.0.18 → 0.0.19
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/dist/chunk-2mx7fq49.js +4 -0
- package/dist/chunk-a8p83068.js +103 -0
- package/dist/{chunk-cgptvjdf.js → chunk-y3xq6wq6.js} +1 -1
- package/dist/cli.js +4 -3
- package/dist/container/index.js +1149 -211
- package/dist/index.js +369 -2209
- package/package.json +6 -5
- package/dist/chunk-1ahs68ys.js +0 -18
- package/dist/chunk-g3ybefhg.js +0 -1923
package/dist/container/index.js
CHANGED
|
@@ -1,255 +1,1193 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
3
|
+
RouterException
|
|
4
|
+
} from "../chunk-a8p83068.js";
|
|
5
|
+
import"../chunk-2mx7fq49.js";
|
|
6
|
+
|
|
7
|
+
// src/errors/router-errors.ts
|
|
8
|
+
class CircularDependencyException extends RouterException {
|
|
9
|
+
dependencyChain;
|
|
10
|
+
suggestion;
|
|
11
|
+
constructor(dependencyChain, context) {
|
|
12
|
+
const message = CircularDependencyException.buildMessage(dependencyChain);
|
|
13
|
+
const suggestion = CircularDependencyException.buildSuggestion(dependencyChain);
|
|
14
|
+
super({
|
|
15
|
+
code: "CIRCULAR_DEPENDENCY",
|
|
16
|
+
message,
|
|
17
|
+
statusCode: 500,
|
|
18
|
+
context,
|
|
19
|
+
retryable: false,
|
|
20
|
+
severity: "critical",
|
|
21
|
+
category: "system"
|
|
22
|
+
});
|
|
23
|
+
this.dependencyChain = dependencyChain;
|
|
24
|
+
this.suggestion = suggestion;
|
|
25
|
+
}
|
|
26
|
+
static buildMessage(chain) {
|
|
27
|
+
const lines = [
|
|
28
|
+
"Circular dependency detected in the dependency injection container!",
|
|
29
|
+
"",
|
|
30
|
+
"Dependency chain:",
|
|
31
|
+
""
|
|
32
|
+
];
|
|
33
|
+
for (let i = 0;i < chain.length; i++) {
|
|
34
|
+
const isLast = i === chain.length - 1;
|
|
35
|
+
const prefix = i === 0 ? "\u250C\u2500\u25BA" : isLast ? "\u2514\u2500\u25BA" : "\u251C\u2500\u25BA";
|
|
36
|
+
lines.push(` ${prefix} ${chain[i]}`);
|
|
37
|
+
if (!isLast) {
|
|
38
|
+
lines.push(" \u2502");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
lines.push(" \u2502");
|
|
42
|
+
lines.push(` \u2514\u2500\u2500 cycles back to: ${chain[0]}`);
|
|
43
|
+
return lines.join(`
|
|
44
|
+
`);
|
|
45
|
+
}
|
|
46
|
+
static buildSuggestion(chain) {
|
|
47
|
+
return `How to fix:
|
|
48
|
+
|
|
49
|
+
1. Break the cycle by introducing an interface:
|
|
50
|
+
\u2022 Create an interface for one of the dependencies
|
|
51
|
+
\u2022 Use lazy injection: container.bind('${chain[0]}').lazy()
|
|
52
|
+
|
|
53
|
+
2. Restructure your dependencies:
|
|
54
|
+
\u2022 Extract shared logic into a separate service
|
|
55
|
+
\u2022 Use events/callbacks instead of direct dependencies
|
|
56
|
+
|
|
57
|
+
3. Use setter injection instead of constructor injection:
|
|
58
|
+
\u2022 Remove the dependency from the constructor
|
|
59
|
+
\u2022 Set it after instantiation using a setter method
|
|
60
|
+
|
|
61
|
+
4. Check if the dependency is actually needed:
|
|
62
|
+
\u2022 Sometimes circular dependencies indicate a design issue
|
|
63
|
+
\u2022 Consider if the classes have too many responsibilities
|
|
64
|
+
|
|
65
|
+
Dependency chain involved:
|
|
66
|
+
${chain.map((dep, i) => ` ${i + 1}. ${dep}`).join(`
|
|
67
|
+
`)}`;
|
|
68
|
+
}
|
|
69
|
+
toJSON() {
|
|
70
|
+
return {
|
|
71
|
+
...super.toJSON(),
|
|
72
|
+
dependencyChain: this.dependencyChain,
|
|
73
|
+
suggestion: this.suggestion
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/container/container.ts
|
|
79
|
+
var INJECTABLE_METADATA_KEY = Symbol("injectable");
|
|
80
|
+
var INJECT_METADATA_KEY = Symbol("inject");
|
|
81
|
+
var OPTIONAL_METADATA_KEY = Symbol("optional");
|
|
82
|
+
var TAGGED_METADATA_KEY = Symbol("tagged");
|
|
83
|
+
|
|
84
|
+
class Container {
|
|
85
|
+
bindings = new Map;
|
|
86
|
+
tagIndex = new Map;
|
|
87
|
+
instances = new Map;
|
|
88
|
+
scopedInstances = new Map;
|
|
89
|
+
interceptors = new Map;
|
|
90
|
+
parent;
|
|
91
|
+
children = new Set;
|
|
92
|
+
options;
|
|
93
|
+
constructor(options = {}) {
|
|
94
|
+
this.options = {
|
|
95
|
+
autoRegister: options.autoRegister ?? true,
|
|
96
|
+
strictMode: options.strictMode ?? false,
|
|
97
|
+
maxDepth: options.maxDepth ?? 50,
|
|
98
|
+
enableDecorators: options.enableDecorators ?? true,
|
|
99
|
+
enableInterception: options.enableInterception ?? false,
|
|
100
|
+
defaultScope: options.defaultScope ?? "transient"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
bind(token) {
|
|
104
|
+
return new BindingBuilder(this, token);
|
|
105
|
+
}
|
|
106
|
+
register(binding) {
|
|
107
|
+
const existing = this.bindings.get(binding.token);
|
|
108
|
+
if (existing?.metadata?.tags) {
|
|
109
|
+
for (const tag of existing.metadata.tags) {
|
|
110
|
+
this.tagIndex.get(tag)?.delete(binding.token);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.bindings.set(binding.token, binding);
|
|
114
|
+
if (binding.metadata?.tags) {
|
|
115
|
+
for (const tag of binding.metadata.tags) {
|
|
116
|
+
let tokens = this.tagIndex.get(tag);
|
|
117
|
+
if (!tokens) {
|
|
118
|
+
tokens = new Set;
|
|
119
|
+
this.tagIndex.set(tag, tokens);
|
|
120
|
+
}
|
|
121
|
+
tokens.add(binding.token);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
singleton(token, implementation) {
|
|
127
|
+
return this.bind(token).to(implementation).inSingletonScope().build();
|
|
128
|
+
}
|
|
129
|
+
transient(token, implementation) {
|
|
130
|
+
return this.bind(token).to(implementation).inTransientScope().build();
|
|
131
|
+
}
|
|
132
|
+
value(token, value) {
|
|
133
|
+
return this.bind(token).toValue(value).build();
|
|
134
|
+
}
|
|
135
|
+
factory(token, factory) {
|
|
136
|
+
return this.bind(token).toFactory(factory).build();
|
|
137
|
+
}
|
|
138
|
+
resolve(token, context) {
|
|
139
|
+
const resolutionContext = {
|
|
140
|
+
container: this,
|
|
64
141
|
token,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
142
|
+
environment: context?.environment || "development",
|
|
143
|
+
requestId: context?.requestId || this.generateRequestId(),
|
|
144
|
+
depth: context?.depth || 0,
|
|
145
|
+
resolving: context?.resolving || new Set,
|
|
146
|
+
parent: context?.parent
|
|
68
147
|
};
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
148
|
+
return this.resolveInternal(token, resolutionContext);
|
|
149
|
+
}
|
|
150
|
+
resolveAll(token) {
|
|
151
|
+
const tokens = new Set;
|
|
152
|
+
if (this.bindings.has(token)) {
|
|
153
|
+
tokens.add(token);
|
|
154
|
+
}
|
|
155
|
+
if (typeof token === "string") {
|
|
156
|
+
for (const tagged of this.tagIndex.get(token) ?? []) {
|
|
157
|
+
tokens.add(tagged);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return Array.from(tokens, (t) => this.resolve(t));
|
|
161
|
+
}
|
|
162
|
+
resolveTagged(tag) {
|
|
163
|
+
const tokens = this.tagIndex.get(tag);
|
|
164
|
+
if (!tokens || tokens.size === 0) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
return Array.from(tokens, (token) => this.resolve(token));
|
|
168
|
+
}
|
|
169
|
+
getTaggedBindings(tag) {
|
|
170
|
+
const tokens = this.tagIndex.get(tag);
|
|
171
|
+
if (!tokens || tokens.size === 0) {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
const bindings = [];
|
|
175
|
+
for (const token of tokens) {
|
|
176
|
+
const binding = this.bindings.get(token);
|
|
177
|
+
if (binding) {
|
|
178
|
+
bindings.push(binding);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return bindings;
|
|
182
|
+
}
|
|
183
|
+
addInterceptor(token, interceptor) {
|
|
184
|
+
const key = this.tokenToString(token);
|
|
185
|
+
if (!this.interceptors.has(key)) {
|
|
186
|
+
this.interceptors.set(key, []);
|
|
187
|
+
}
|
|
188
|
+
this.interceptors.get(key).push(interceptor);
|
|
189
|
+
}
|
|
190
|
+
registerController(controller) {
|
|
191
|
+
this.singleton(controller, controller);
|
|
192
|
+
}
|
|
193
|
+
getController(controller) {
|
|
194
|
+
return this.resolve(controller);
|
|
195
|
+
}
|
|
196
|
+
isBound(token) {
|
|
197
|
+
return this.bindings.has(token) || (this.parent?.isBound(token) ?? false);
|
|
198
|
+
}
|
|
199
|
+
unbind(token) {
|
|
200
|
+
const binding = this.bindings.get(token);
|
|
201
|
+
if (binding?.metadata?.tags) {
|
|
202
|
+
for (const tag of binding.metadata.tags) {
|
|
203
|
+
this.tagIndex.get(tag)?.delete(token);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
this.bindings.delete(token);
|
|
207
|
+
this.instances.delete(token);
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
createChild(options) {
|
|
211
|
+
const child = new Container({ ...this.options, ...options });
|
|
212
|
+
child.parent = this;
|
|
213
|
+
this.children.add(child);
|
|
214
|
+
return child;
|
|
215
|
+
}
|
|
216
|
+
createScope(scopeId) {
|
|
217
|
+
const scope = this.createChild();
|
|
218
|
+
scope.scopedInstances.set(scopeId, new Map);
|
|
219
|
+
return scope;
|
|
220
|
+
}
|
|
221
|
+
getBindings() {
|
|
222
|
+
return new Map(this.bindings);
|
|
223
|
+
}
|
|
224
|
+
clear() {
|
|
225
|
+
this.bindings.clear();
|
|
226
|
+
this.tagIndex.clear();
|
|
227
|
+
this.instances.clear();
|
|
228
|
+
this.scopedInstances.clear();
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
intercept(token, interceptor) {
|
|
232
|
+
if (!this.interceptors.has(token)) {
|
|
233
|
+
this.interceptors.set(token, []);
|
|
234
|
+
}
|
|
235
|
+
this.interceptors.get(token).push(interceptor);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
resolveInternal(token, context) {
|
|
239
|
+
if (context.resolving.has(token)) {
|
|
240
|
+
const chain = Array.from(context.resolving).map((t) => this.tokenToString(t));
|
|
241
|
+
chain.push(this.tokenToString(token));
|
|
242
|
+
throw new CircularDependencyException(chain);
|
|
243
|
+
}
|
|
244
|
+
if (context.depth > this.options.maxDepth) {
|
|
245
|
+
const chain = Array.from(context.resolving).map((t) => this.tokenToString(t));
|
|
246
|
+
throw new Error(`Maximum resolution depth exceeded (${this.options.maxDepth}).
|
|
247
|
+
` + `This usually indicates a circular or very deep dependency chain.
|
|
248
|
+
` + `Current resolution chain:
|
|
249
|
+
${chain.map((t, i) => ` ${i + 1}. ${t}`).join(`
|
|
250
|
+
`)}`);
|
|
251
|
+
}
|
|
252
|
+
context.resolving.add(token);
|
|
253
|
+
context.depth++;
|
|
254
|
+
try {
|
|
255
|
+
const binding = this.bindings.get(token);
|
|
256
|
+
if (binding) {
|
|
257
|
+
if (binding.metadata.when && !binding.metadata.when(context)) {
|
|
258
|
+
throw new Error(`Contextual binding condition not met for: ${this.tokenToString(token)}`);
|
|
259
|
+
}
|
|
260
|
+
return this.createInstance(binding, context);
|
|
261
|
+
}
|
|
262
|
+
if (this.parent) {
|
|
263
|
+
const parentContext = {
|
|
264
|
+
resolving: new Set,
|
|
265
|
+
depth: 0,
|
|
266
|
+
container: this.parent,
|
|
267
|
+
token
|
|
268
|
+
};
|
|
269
|
+
return this.parent.resolveInternal(token, parentContext);
|
|
270
|
+
}
|
|
271
|
+
if (this.options.autoRegister && typeof token === "function") {
|
|
272
|
+
return this.autoRegisterAndResolve(token, context);
|
|
273
|
+
}
|
|
274
|
+
if (this.options.strictMode) {
|
|
275
|
+
throw new Error(`No binding found for: ${this.tokenToString(token)}`);
|
|
276
|
+
}
|
|
277
|
+
if (typeof token === "function") {
|
|
278
|
+
return this.createInstanceFromConstructor(token, context);
|
|
279
|
+
}
|
|
280
|
+
throw new Error(`Cannot resolve: ${this.tokenToString(token)}`);
|
|
281
|
+
} finally {
|
|
282
|
+
context.resolving.delete(token);
|
|
283
|
+
context.depth--;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
createInstance(binding, context) {
|
|
287
|
+
let instance;
|
|
288
|
+
if (binding.metadata.scope === "singleton" && this.instances.has(binding.token)) {
|
|
289
|
+
instance = this.instances.get(binding.token);
|
|
290
|
+
} else if (binding.metadata.scope === "scoped" && context.requestId) {
|
|
291
|
+
const scopedMap = this.scopedInstances.get(context.requestId);
|
|
292
|
+
if (scopedMap?.has(binding.token)) {
|
|
293
|
+
instance = scopedMap.get(binding.token);
|
|
294
|
+
} else {
|
|
295
|
+
instance = this.createNewInstance(binding, context);
|
|
296
|
+
if (!scopedMap) {
|
|
297
|
+
this.scopedInstances.set(context.requestId, new Map);
|
|
298
|
+
}
|
|
299
|
+
this.scopedInstances.get(context.requestId).set(binding.token, instance);
|
|
300
|
+
}
|
|
301
|
+
} else {
|
|
302
|
+
instance = this.createNewInstance(binding, context);
|
|
303
|
+
if (binding.metadata.scope === "singleton") {
|
|
304
|
+
this.instances.set(binding.token, instance);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const tokenKey = this.tokenToString(binding.token);
|
|
308
|
+
if (this.interceptors.has(tokenKey)) {
|
|
309
|
+
const interceptors = this.interceptors.get(tokenKey);
|
|
310
|
+
for (const interceptor of interceptors) {
|
|
311
|
+
instance = interceptor(instance);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return instance;
|
|
315
|
+
}
|
|
316
|
+
createNewInstance(binding, context) {
|
|
317
|
+
if (binding.value !== undefined) {
|
|
318
|
+
return binding.value;
|
|
319
|
+
}
|
|
320
|
+
if (binding.factory) {
|
|
321
|
+
const dependencies = this.resolveDependencies(binding.metadata.dependencies || [], context);
|
|
322
|
+
return binding.factory(...dependencies);
|
|
323
|
+
}
|
|
324
|
+
if (binding.instance) {
|
|
325
|
+
return binding.instance;
|
|
326
|
+
}
|
|
327
|
+
if (binding.implementation) {
|
|
328
|
+
return this.createInstanceFromConstructor(binding.implementation, context);
|
|
329
|
+
}
|
|
330
|
+
throw new Error(`Invalid binding configuration for: ${this.tokenToString(binding.token)}`);
|
|
331
|
+
}
|
|
332
|
+
createInstanceFromConstructor(constructor, context) {
|
|
333
|
+
const dependencies = this.getConstructorDependencies(constructor);
|
|
334
|
+
const resolvedDependencies = this.resolveDependencies(dependencies, context);
|
|
335
|
+
const instance = new constructor(...resolvedDependencies);
|
|
336
|
+
if (this.options.enableDecorators) {
|
|
337
|
+
this.applyDecorators(instance, constructor);
|
|
338
|
+
}
|
|
339
|
+
return instance;
|
|
340
|
+
}
|
|
341
|
+
getConstructorDependencies(constructor) {
|
|
342
|
+
let injectMetadata = Reflect.getMetadata?.(INJECT_METADATA_KEY, constructor) || constructor[INJECT_METADATA_KEY];
|
|
343
|
+
if (!injectMetadata && constructor.constructor) {
|
|
344
|
+
injectMetadata = constructor.constructor[INJECT_METADATA_KEY];
|
|
345
|
+
}
|
|
346
|
+
if (!injectMetadata && typeof constructor === "function" && constructor.prototype) {
|
|
347
|
+
const classMetadata = constructor[INJECT_METADATA_KEY];
|
|
348
|
+
if (classMetadata) {
|
|
349
|
+
injectMetadata = classMetadata;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (!injectMetadata && typeof constructor === "function") {
|
|
353
|
+
const directMetadata = constructor[INJECT_METADATA_KEY];
|
|
354
|
+
if (directMetadata) {
|
|
355
|
+
injectMetadata = directMetadata;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (injectMetadata) {
|
|
359
|
+
return injectMetadata.map((meta) => meta.token);
|
|
360
|
+
}
|
|
361
|
+
const paramTypes = Reflect.getMetadata?.("design:paramtypes", constructor);
|
|
362
|
+
if (paramTypes) {
|
|
363
|
+
return paramTypes;
|
|
364
|
+
}
|
|
365
|
+
return this.parseConstructorDependencies(constructor);
|
|
366
|
+
}
|
|
367
|
+
parseConstructorDependencies(constructor) {
|
|
368
|
+
const constructorString = constructor.toString();
|
|
369
|
+
const match = constructorString.match(/constructor\s*\(([^)]*)\)/);
|
|
370
|
+
if (!match || !match[1]) {
|
|
371
|
+
return [];
|
|
372
|
+
}
|
|
373
|
+
return match[1].split(",").map((param) => param.trim().split(":")[0].trim()).filter((param) => param.length > 0);
|
|
374
|
+
}
|
|
375
|
+
resolveDependencies(dependencies, context) {
|
|
376
|
+
return dependencies.map((dep) => this.resolveInternal(dep, context));
|
|
377
|
+
}
|
|
378
|
+
autoRegisterAndResolve(constructor, context) {
|
|
379
|
+
const metadata = Reflect.getMetadata?.(INJECTABLE_METADATA_KEY, constructor);
|
|
380
|
+
const binding = {
|
|
381
|
+
token: constructor,
|
|
382
|
+
implementation: constructor,
|
|
383
|
+
metadata: {
|
|
384
|
+
scope: metadata?.scope || this.options.defaultScope,
|
|
385
|
+
tags: metadata?.tags
|
|
386
|
+
}
|
|
94
387
|
};
|
|
95
|
-
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
388
|
+
this.register(binding);
|
|
389
|
+
return this.resolveInternal(constructor, context);
|
|
390
|
+
}
|
|
391
|
+
applyDecorators(instance, constructor) {
|
|
392
|
+
const decorators = Reflect.getMetadata?.("decorators", constructor);
|
|
393
|
+
if (decorators) {
|
|
394
|
+
for (const decorator of decorators) {
|
|
395
|
+
decorator(instance);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
hasTag(binding, tag) {
|
|
400
|
+
return binding.metadata.tags?.includes(tag) ?? false;
|
|
401
|
+
}
|
|
402
|
+
tokenToString(token) {
|
|
403
|
+
if (typeof token === "string")
|
|
404
|
+
return token;
|
|
405
|
+
if (typeof token === "symbol")
|
|
406
|
+
return token.toString();
|
|
407
|
+
if (typeof token === "function")
|
|
408
|
+
return token.name || "anonymous";
|
|
409
|
+
return "unknown";
|
|
410
|
+
}
|
|
411
|
+
generateRequestId() {
|
|
412
|
+
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
413
|
+
}
|
|
101
414
|
}
|
|
102
|
-
|
|
103
|
-
|
|
415
|
+
|
|
416
|
+
class BindingBuilder {
|
|
417
|
+
container;
|
|
418
|
+
binding;
|
|
419
|
+
constructor(container, token) {
|
|
420
|
+
this.container = container;
|
|
421
|
+
this.binding = {
|
|
422
|
+
token,
|
|
423
|
+
metadata: {
|
|
424
|
+
scope: "transient"
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
to(implementation) {
|
|
429
|
+
this.binding.implementation = implementation;
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
toValue(value) {
|
|
433
|
+
this.binding.value = value;
|
|
434
|
+
this.binding.metadata.scope = "singleton";
|
|
435
|
+
return this;
|
|
436
|
+
}
|
|
437
|
+
toFactory(factory) {
|
|
438
|
+
this.binding.factory = factory;
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
toInstance(instance) {
|
|
442
|
+
this.binding.instance = instance;
|
|
443
|
+
this.binding.metadata.scope = "singleton";
|
|
444
|
+
return this;
|
|
445
|
+
}
|
|
446
|
+
inSingletonScope() {
|
|
447
|
+
this.binding.metadata.scope = "singleton";
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
inTransientScope() {
|
|
451
|
+
this.binding.metadata.scope = "transient";
|
|
452
|
+
return this;
|
|
453
|
+
}
|
|
454
|
+
inScopedScope() {
|
|
455
|
+
this.binding.metadata.scope = "scoped";
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
inRequestScope() {
|
|
459
|
+
this.binding.metadata.scope = "request";
|
|
460
|
+
return this;
|
|
461
|
+
}
|
|
462
|
+
withTags(...tags) {
|
|
463
|
+
this.binding.metadata.tags = [...this.binding.metadata.tags || [], ...tags];
|
|
464
|
+
return this;
|
|
465
|
+
}
|
|
466
|
+
when(condition) {
|
|
467
|
+
this.binding.metadata.when = condition;
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
lazy() {
|
|
471
|
+
this.binding.metadata.lazy = true;
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
build() {
|
|
475
|
+
this.container.register(this.binding);
|
|
476
|
+
return this.container;
|
|
477
|
+
}
|
|
104
478
|
}
|
|
105
|
-
|
|
106
|
-
|
|
479
|
+
var globalContainer = null;
|
|
480
|
+
function getContainer() {
|
|
481
|
+
if (!globalContainer) {
|
|
482
|
+
globalContainer = new Container;
|
|
483
|
+
}
|
|
484
|
+
return globalContainer;
|
|
107
485
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
486
|
+
function setContainer(container) {
|
|
487
|
+
globalContainer = container;
|
|
110
488
|
}
|
|
111
|
-
function
|
|
112
|
-
return
|
|
489
|
+
function createContainer(options) {
|
|
490
|
+
return new Container(options);
|
|
113
491
|
}
|
|
114
|
-
|
|
115
|
-
|
|
492
|
+
// src/container/contextual-binding.ts
|
|
493
|
+
class EnvironmentManager {
|
|
494
|
+
environments = new Map;
|
|
495
|
+
currentEnvironment;
|
|
496
|
+
variables = new Map;
|
|
497
|
+
constructor(defaultEnvironment = "development") {
|
|
498
|
+
this.currentEnvironment = defaultEnvironment;
|
|
499
|
+
this.loadEnvironmentVariables();
|
|
500
|
+
}
|
|
501
|
+
register(config) {
|
|
502
|
+
this.environments.set(config.name, config);
|
|
503
|
+
return this;
|
|
504
|
+
}
|
|
505
|
+
setEnvironment(name) {
|
|
506
|
+
if (!this.environments.has(name)) {
|
|
507
|
+
throw new Error(`Environment ${name} is not registered`);
|
|
508
|
+
}
|
|
509
|
+
this.currentEnvironment = name;
|
|
510
|
+
this.loadEnvironmentVariables();
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
getCurrentEnvironment() {
|
|
514
|
+
return this.currentEnvironment;
|
|
515
|
+
}
|
|
516
|
+
getEnvironment(name) {
|
|
517
|
+
return this.environments.get(name || this.currentEnvironment);
|
|
518
|
+
}
|
|
519
|
+
is(...environments) {
|
|
520
|
+
return environments.includes(this.currentEnvironment);
|
|
521
|
+
}
|
|
522
|
+
get(key, defaultValue) {
|
|
523
|
+
return this.variables.get(key) ?? defaultValue;
|
|
524
|
+
}
|
|
525
|
+
set(key, value) {
|
|
526
|
+
this.variables.set(key, value);
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
hasFeature(feature) {
|
|
530
|
+
const env = this.getEnvironment();
|
|
531
|
+
return env?.features.includes(feature) ?? false;
|
|
532
|
+
}
|
|
533
|
+
loadEnvironmentVariables() {
|
|
534
|
+
const env = this.getEnvironment();
|
|
535
|
+
if (env) {
|
|
536
|
+
for (const [key, value] of Object.entries(env.variables)) {
|
|
537
|
+
this.variables.set(key, value);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
116
541
|
}
|
|
117
|
-
|
|
118
|
-
|
|
542
|
+
|
|
543
|
+
class DefaultContextualBindingBuilder {
|
|
544
|
+
container;
|
|
545
|
+
binding;
|
|
546
|
+
constructor(container, token) {
|
|
547
|
+
this.container = container;
|
|
548
|
+
this.binding = {
|
|
549
|
+
token,
|
|
550
|
+
conditions: [],
|
|
551
|
+
priority: 0,
|
|
552
|
+
metadata: {
|
|
553
|
+
scope: "transient"
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
when(condition) {
|
|
558
|
+
this.binding.conditions.push(condition);
|
|
559
|
+
return this;
|
|
560
|
+
}
|
|
561
|
+
whenEnvironment(...environments) {
|
|
562
|
+
const condition = {
|
|
563
|
+
type: "environment",
|
|
564
|
+
predicate: (context) => environments.includes(context.environment || "development"),
|
|
565
|
+
description: `Environment is one of: ${environments.join(", ")}`
|
|
566
|
+
};
|
|
567
|
+
return this.when(condition);
|
|
568
|
+
}
|
|
569
|
+
whenTag(tag) {
|
|
570
|
+
const condition = {
|
|
571
|
+
type: "tag",
|
|
572
|
+
predicate: (context) => {
|
|
573
|
+
return context.parent?.token?.toString().includes(tag) ?? false;
|
|
574
|
+
},
|
|
575
|
+
description: `Parent has tag: ${tag}`
|
|
576
|
+
};
|
|
577
|
+
return this.when(condition);
|
|
578
|
+
}
|
|
579
|
+
whenParent(parentToken) {
|
|
580
|
+
const condition = {
|
|
581
|
+
type: "parent",
|
|
582
|
+
predicate: (context) => context.parent?.token === parentToken,
|
|
583
|
+
description: `Parent token is: ${parentToken.toString()}`
|
|
584
|
+
};
|
|
585
|
+
return this.when(condition);
|
|
586
|
+
}
|
|
587
|
+
whenRequest(predicate) {
|
|
588
|
+
const condition = {
|
|
589
|
+
type: "request",
|
|
590
|
+
predicate,
|
|
591
|
+
description: "Custom request condition"
|
|
592
|
+
};
|
|
593
|
+
return this.when(condition);
|
|
594
|
+
}
|
|
595
|
+
withPriority(priority) {
|
|
596
|
+
this.binding.priority = priority;
|
|
597
|
+
return this;
|
|
598
|
+
}
|
|
599
|
+
withFallback(fallback) {
|
|
600
|
+
this.binding.fallback = fallback;
|
|
601
|
+
return this;
|
|
602
|
+
}
|
|
603
|
+
to(implementation) {
|
|
604
|
+
this.binding.implementation = implementation;
|
|
605
|
+
return this;
|
|
606
|
+
}
|
|
607
|
+
toValue(value) {
|
|
608
|
+
this.binding.value = value;
|
|
609
|
+
this.binding.metadata.scope = "singleton";
|
|
610
|
+
return this;
|
|
611
|
+
}
|
|
612
|
+
toFactory(factory) {
|
|
613
|
+
this.binding.factory = factory;
|
|
614
|
+
return this;
|
|
615
|
+
}
|
|
616
|
+
inSingletonScope() {
|
|
617
|
+
this.binding.metadata.scope = "singleton";
|
|
618
|
+
return this;
|
|
619
|
+
}
|
|
620
|
+
inTransientScope() {
|
|
621
|
+
this.binding.metadata.scope = "transient";
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
build() {
|
|
625
|
+
this.container.registerContextualBinding(this.binding);
|
|
626
|
+
return this.container;
|
|
627
|
+
}
|
|
119
628
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
629
|
+
|
|
630
|
+
class ContextualContainer extends Container {
|
|
631
|
+
contextualBindings = new Map;
|
|
632
|
+
environmentManager;
|
|
633
|
+
constructor(options) {
|
|
634
|
+
super(options);
|
|
635
|
+
this.environmentManager = new EnvironmentManager;
|
|
636
|
+
}
|
|
637
|
+
bindContextual(token) {
|
|
638
|
+
return new DefaultContextualBindingBuilder(this, token);
|
|
639
|
+
}
|
|
640
|
+
registerContextualBinding(binding) {
|
|
641
|
+
if (!this.contextualBindings.has(binding.token)) {
|
|
642
|
+
this.contextualBindings.set(binding.token, []);
|
|
124
643
|
}
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
644
|
+
const bindings = this.contextualBindings.get(binding.token);
|
|
645
|
+
bindings.push(binding);
|
|
646
|
+
bindings.sort((a, b) => b.priority - a.priority);
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
getEnvironmentManager() {
|
|
650
|
+
return this.environmentManager;
|
|
651
|
+
}
|
|
652
|
+
forDevelopment(token) {
|
|
653
|
+
return this.bindContextual(token).whenEnvironment("development");
|
|
654
|
+
}
|
|
655
|
+
forProduction(token) {
|
|
656
|
+
return this.bindContextual(token).whenEnvironment("production");
|
|
657
|
+
}
|
|
658
|
+
forTesting(token) {
|
|
659
|
+
return this.bindContextual(token).whenEnvironment("test", "testing");
|
|
660
|
+
}
|
|
661
|
+
forStaging(token) {
|
|
662
|
+
return this.bindContextual(token).whenEnvironment("staging");
|
|
663
|
+
}
|
|
664
|
+
resolve(token, context) {
|
|
665
|
+
const resolutionContext = {
|
|
666
|
+
container: this,
|
|
667
|
+
token,
|
|
668
|
+
environment: context?.environment || this.environmentManager.getCurrentEnvironment(),
|
|
669
|
+
requestId: context?.requestId || this.generateContextualRequestId(),
|
|
670
|
+
depth: context?.depth || 0,
|
|
671
|
+
resolving: context?.resolving || new Set,
|
|
672
|
+
parent: context?.parent
|
|
130
673
|
};
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
674
|
+
const contextualBinding = this.findContextualBinding(token, resolutionContext);
|
|
675
|
+
if (contextualBinding) {
|
|
676
|
+
return this.createInstanceFromContextualBinding(contextualBinding, resolutionContext);
|
|
677
|
+
}
|
|
678
|
+
return super.resolve(token, resolutionContext);
|
|
679
|
+
}
|
|
680
|
+
findContextualBinding(token, context) {
|
|
681
|
+
const bindings = this.contextualBindings.get(token);
|
|
682
|
+
if (!bindings)
|
|
683
|
+
return;
|
|
684
|
+
for (const binding of bindings) {
|
|
685
|
+
if (this.evaluateConditions(binding.conditions, context)) {
|
|
686
|
+
return binding;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
evaluateConditions(conditions, context) {
|
|
692
|
+
return conditions.every((condition) => {
|
|
693
|
+
try {
|
|
694
|
+
return condition.predicate(context);
|
|
695
|
+
} catch (error) {
|
|
696
|
+
console.warn(`Error evaluating binding condition: ${condition.description}`, error);
|
|
697
|
+
return false;
|
|
698
|
+
}
|
|
135
699
|
});
|
|
136
|
-
|
|
137
|
-
|
|
700
|
+
}
|
|
701
|
+
createInstanceFromContextualBinding(binding, context) {
|
|
702
|
+
try {
|
|
703
|
+
return this.createInstance(binding, context);
|
|
704
|
+
} catch (error) {
|
|
705
|
+
if (binding.fallback) {
|
|
706
|
+
console.warn(`Contextual binding failed, using fallback for ${binding.token.toString()}`);
|
|
707
|
+
return this.createInstance(binding.fallback, context);
|
|
708
|
+
}
|
|
709
|
+
throw error;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
generateContextualRequestId() {
|
|
713
|
+
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
714
|
+
}
|
|
138
715
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
716
|
+
|
|
717
|
+
class EnvironmentPresets {
|
|
718
|
+
static development() {
|
|
719
|
+
return {
|
|
720
|
+
name: "development",
|
|
721
|
+
variables: {
|
|
722
|
+
DEBUG: true,
|
|
723
|
+
LOG_LEVEL: "debug",
|
|
724
|
+
CACHE_TTL: 60,
|
|
725
|
+
DB_POOL_SIZE: 5
|
|
726
|
+
},
|
|
727
|
+
services: {
|
|
728
|
+
logger: "ConsoleLogger",
|
|
729
|
+
cache: "MemoryCache",
|
|
730
|
+
database: "SQLiteDatabase"
|
|
731
|
+
},
|
|
732
|
+
features: ["hot-reload", "debug-toolbar", "detailed-errors"]
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
static production() {
|
|
736
|
+
return {
|
|
737
|
+
name: "production",
|
|
738
|
+
variables: {
|
|
739
|
+
DEBUG: false,
|
|
740
|
+
LOG_LEVEL: "error",
|
|
741
|
+
CACHE_TTL: 3600,
|
|
742
|
+
DB_POOL_SIZE: 20
|
|
743
|
+
},
|
|
744
|
+
services: {
|
|
745
|
+
logger: "FileLogger",
|
|
746
|
+
cache: "RedisCache",
|
|
747
|
+
database: "PostgreSQLDatabase"
|
|
748
|
+
},
|
|
749
|
+
features: ["performance-monitoring", "error-tracking"]
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
static testing() {
|
|
753
|
+
return {
|
|
754
|
+
name: "test",
|
|
755
|
+
variables: {
|
|
756
|
+
DEBUG: false,
|
|
757
|
+
LOG_LEVEL: "warn",
|
|
758
|
+
CACHE_TTL: 1,
|
|
759
|
+
DB_POOL_SIZE: 1
|
|
760
|
+
},
|
|
761
|
+
services: {
|
|
762
|
+
logger: "NullLogger",
|
|
763
|
+
cache: "MemoryCache",
|
|
764
|
+
database: "InMemoryDatabase"
|
|
765
|
+
},
|
|
766
|
+
features: ["test-doubles", "fast-teardown"]
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
static staging() {
|
|
770
|
+
return {
|
|
771
|
+
name: "staging",
|
|
772
|
+
variables: {
|
|
773
|
+
DEBUG: true,
|
|
774
|
+
LOG_LEVEL: "info",
|
|
775
|
+
CACHE_TTL: 1800,
|
|
776
|
+
DB_POOL_SIZE: 10
|
|
777
|
+
},
|
|
778
|
+
services: {
|
|
779
|
+
logger: "FileLogger",
|
|
780
|
+
cache: "RedisCache",
|
|
781
|
+
database: "PostgreSQLDatabase"
|
|
782
|
+
},
|
|
783
|
+
features: ["performance-monitoring", "debug-toolbar"]
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
class BindingConditions {
|
|
789
|
+
static environment(...environments) {
|
|
790
|
+
return {
|
|
791
|
+
type: "environment",
|
|
792
|
+
predicate: (context) => environments.includes(context.environment || "development"),
|
|
793
|
+
description: `Environment is one of: ${environments.join(", ")}`
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
static feature(featureName) {
|
|
797
|
+
return {
|
|
798
|
+
type: "custom",
|
|
799
|
+
predicate: (context) => {
|
|
800
|
+
const container = context.container;
|
|
801
|
+
return container.getEnvironmentManager().hasFeature(featureName);
|
|
802
|
+
},
|
|
803
|
+
description: `Feature ${featureName} is enabled`
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
static envVar(key, value) {
|
|
807
|
+
return {
|
|
808
|
+
type: "custom",
|
|
809
|
+
predicate: (context) => {
|
|
810
|
+
const container = context.container;
|
|
811
|
+
return container.getEnvironmentManager().get(key) === value;
|
|
812
|
+
},
|
|
813
|
+
description: `Environment variable ${key} equals ${value}`
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
static requestPattern(pattern) {
|
|
817
|
+
return {
|
|
818
|
+
type: "request",
|
|
819
|
+
predicate: (context) => pattern.test(context.requestId || ""),
|
|
820
|
+
description: `Request ID matches pattern: ${pattern.toString()}`
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
static timeRange(startHour, endHour) {
|
|
824
|
+
return {
|
|
825
|
+
type: "custom",
|
|
826
|
+
predicate: () => {
|
|
827
|
+
const hour = new Date().getHours();
|
|
828
|
+
return hour >= startHour && hour <= endHour;
|
|
829
|
+
},
|
|
830
|
+
description: `Time is between ${startHour}:00 and ${endHour}:00`
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
static custom(predicate, description) {
|
|
834
|
+
return {
|
|
835
|
+
type: "custom",
|
|
836
|
+
predicate,
|
|
837
|
+
description: description || "Custom condition"
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
// src/container/service-provider.ts
|
|
842
|
+
class BaseServiceProvider {
|
|
843
|
+
version;
|
|
844
|
+
dependencies;
|
|
845
|
+
environment;
|
|
846
|
+
priority = 0;
|
|
847
|
+
boot(_container) {}
|
|
848
|
+
shutdown(_container) {}
|
|
849
|
+
singleton(container, token, implementation) {
|
|
850
|
+
container.singleton(token, implementation);
|
|
851
|
+
}
|
|
852
|
+
transient(container, token, implementation) {
|
|
853
|
+
container.transient(token, implementation);
|
|
854
|
+
}
|
|
855
|
+
value(container, token, value) {
|
|
856
|
+
container.value(token, value);
|
|
857
|
+
}
|
|
858
|
+
factory(container, token, factory) {
|
|
859
|
+
container.factory(token, factory);
|
|
860
|
+
}
|
|
861
|
+
when(container, condition) {
|
|
862
|
+
return {
|
|
863
|
+
bind: (token) => {
|
|
864
|
+
return container.bind(token).when(condition);
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
forEnvironment(container, ...environments) {
|
|
869
|
+
return this.when(container, (context) => environments.includes(context.environment || "development"));
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
class BaseDeferredServiceProvider extends BaseServiceProvider {
|
|
874
|
+
isDeferred() {
|
|
875
|
+
return true;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
class DefaultServiceProviderManager {
|
|
880
|
+
providers = new Map;
|
|
881
|
+
deferredProviders = new Map;
|
|
882
|
+
loadedProviders = new Set;
|
|
883
|
+
bootedProviders = new Set;
|
|
884
|
+
container;
|
|
885
|
+
constructor(container) {
|
|
886
|
+
this.container = container;
|
|
887
|
+
}
|
|
888
|
+
register(provider) {
|
|
889
|
+
if (this.isConditionalProvider(provider)) {
|
|
890
|
+
const shouldLoad = provider.shouldLoad(this.container);
|
|
891
|
+
if (shouldLoad instanceof Promise) {
|
|
892
|
+
shouldLoad.then((load) => {
|
|
893
|
+
if (load) {
|
|
894
|
+
this.doRegister(provider);
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
} else if (!shouldLoad) {
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
this.doRegister(provider);
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
async boot() {
|
|
905
|
+
const providers = Array.from(this.providers.values()).sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
906
|
+
this.validateDependencies(providers);
|
|
907
|
+
for (const provider of providers) {
|
|
908
|
+
if (!this.loadedProviders.has(provider.name)) {
|
|
909
|
+
await this.loadProvider(provider);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
for (const provider of providers) {
|
|
913
|
+
if (!this.bootedProviders.has(provider.name)) {
|
|
914
|
+
await this.bootProvider(provider);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
async shutdown() {
|
|
919
|
+
const providers = Array.from(this.providers.values()).reverse();
|
|
920
|
+
for (const provider of providers) {
|
|
921
|
+
if (provider.shutdown) {
|
|
922
|
+
try {
|
|
923
|
+
await provider.shutdown(this.container);
|
|
924
|
+
} catch (error) {
|
|
925
|
+
console.error(`Error shutting down provider ${provider.name}:`, error);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
this.providers.clear();
|
|
930
|
+
this.deferredProviders.clear();
|
|
931
|
+
this.loadedProviders.clear();
|
|
932
|
+
this.bootedProviders.clear();
|
|
933
|
+
}
|
|
934
|
+
getProvider(name) {
|
|
935
|
+
return this.providers.get(name);
|
|
936
|
+
}
|
|
937
|
+
getProviders() {
|
|
938
|
+
return Array.from(this.providers.values());
|
|
939
|
+
}
|
|
940
|
+
async loadDeferredProvider(token) {
|
|
941
|
+
for (const [name, provider] of this.deferredProviders) {
|
|
942
|
+
if (provider.provides().includes(token) && !this.loadedProviders.has(name)) {
|
|
943
|
+
await this.loadProvider(provider);
|
|
944
|
+
await this.bootProvider(provider);
|
|
945
|
+
this.deferredProviders.delete(name);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
doRegister(provider) {
|
|
950
|
+
if (this.providers.has(provider.name)) {
|
|
951
|
+
throw new Error(`Provider ${provider.name} is already registered`);
|
|
952
|
+
}
|
|
953
|
+
this.providers.set(provider.name, provider);
|
|
954
|
+
if (this.isDeferredProvider(provider)) {
|
|
955
|
+
this.deferredProviders.set(provider.name, provider);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
async loadProvider(provider) {
|
|
959
|
+
try {
|
|
960
|
+
await provider.register(this.container);
|
|
961
|
+
this.loadedProviders.add(provider.name);
|
|
962
|
+
} catch (error) {
|
|
963
|
+
throw new Error(`Failed to load provider ${provider.name}: ${error}`);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
async bootProvider(provider) {
|
|
967
|
+
if (provider.boot) {
|
|
968
|
+
try {
|
|
969
|
+
await provider.boot(this.container);
|
|
970
|
+
this.bootedProviders.add(provider.name);
|
|
971
|
+
} catch (error) {
|
|
972
|
+
throw new Error(`Failed to boot provider ${provider.name}: ${error}`);
|
|
144
973
|
}
|
|
145
|
-
target.constructor[MIDDLEWARE_METADATA_KEY].set(propertyKey, middleware);
|
|
146
974
|
} else {
|
|
147
|
-
|
|
975
|
+
this.bootedProviders.add(provider.name);
|
|
148
976
|
}
|
|
149
|
-
|
|
150
|
-
|
|
977
|
+
}
|
|
978
|
+
validateDependencies(providers) {
|
|
979
|
+
const providerNames = new Set(providers.map((p) => p.name));
|
|
980
|
+
for (const provider of providers) {
|
|
981
|
+
if (provider.dependencies) {
|
|
982
|
+
for (const dependency of provider.dependencies) {
|
|
983
|
+
if (!providerNames.has(dependency)) {
|
|
984
|
+
throw new Error(`Provider ${provider.name} depends on ${dependency}, but it's not registered`);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
isDeferredProvider(provider) {
|
|
991
|
+
return "provides" in provider && "isDeferred" in provider;
|
|
992
|
+
}
|
|
993
|
+
isConditionalProvider(provider) {
|
|
994
|
+
return "shouldLoad" in provider;
|
|
995
|
+
}
|
|
151
996
|
}
|
|
152
|
-
|
|
153
|
-
|
|
997
|
+
|
|
998
|
+
class ConfigServiceProvider extends BaseServiceProvider {
|
|
999
|
+
name = "config";
|
|
1000
|
+
priority = 100;
|
|
1001
|
+
register(container) {
|
|
1002
|
+
container.singleton("config", ConfigService);
|
|
1003
|
+
this.forEnvironment(container, "development").bind("config.debug").toValue(true);
|
|
1004
|
+
this.forEnvironment(container, "production").bind("config.debug").toValue(false);
|
|
1005
|
+
this.forEnvironment(container, "test").bind("config.debug").toValue(false);
|
|
1006
|
+
}
|
|
154
1007
|
}
|
|
155
|
-
|
|
156
|
-
|
|
1008
|
+
|
|
1009
|
+
class LoggingServiceProvider extends BaseServiceProvider {
|
|
1010
|
+
name = "logging";
|
|
1011
|
+
priority = 90;
|
|
1012
|
+
register(container) {
|
|
1013
|
+
container.factory("logger", (config) => {
|
|
1014
|
+
return new Logger(config.get("logging", {}));
|
|
1015
|
+
});
|
|
1016
|
+
this.forEnvironment(container, "development").bind("logger").toFactory(() => new ConsoleLogger);
|
|
1017
|
+
this.forEnvironment(container, "production").bind("logger").toFactory((config) => new FileLogger(config.get("logging.file", "./app.log")));
|
|
1018
|
+
}
|
|
157
1019
|
}
|
|
158
|
-
|
|
159
|
-
|
|
1020
|
+
|
|
1021
|
+
class DatabaseServiceProvider extends BaseServiceProvider {
|
|
1022
|
+
name = "database";
|
|
1023
|
+
dependencies = ["config", "logging"];
|
|
1024
|
+
register(container) {
|
|
1025
|
+
container.singleton("database", DatabaseConnection);
|
|
1026
|
+
container.transient("userRepository", UserRepository);
|
|
1027
|
+
container.transient("postRepository", PostRepository);
|
|
1028
|
+
}
|
|
1029
|
+
async boot(container) {
|
|
1030
|
+
const db = container.resolve("database");
|
|
1031
|
+
await db.connect();
|
|
1032
|
+
}
|
|
1033
|
+
async shutdown(container) {
|
|
1034
|
+
const db = container.resolve("database");
|
|
1035
|
+
await db.disconnect();
|
|
1036
|
+
}
|
|
160
1037
|
}
|
|
161
|
-
|
|
162
|
-
|
|
1038
|
+
|
|
1039
|
+
class CacheServiceProvider extends BaseServiceProvider {
|
|
1040
|
+
name = "cache";
|
|
1041
|
+
dependencies = ["config"];
|
|
1042
|
+
register(container) {
|
|
1043
|
+
container.singleton("cache", CacheManager);
|
|
1044
|
+
this.forEnvironment(container, "development").bind("cache.store").toFactory(() => new MemoryCache);
|
|
1045
|
+
this.forEnvironment(container, "production").bind("cache.store").toFactory((config) => new RedisCache(config.get("cache.redis")));
|
|
1046
|
+
}
|
|
163
1047
|
}
|
|
164
|
-
|
|
165
|
-
|
|
1048
|
+
|
|
1049
|
+
class AuthServiceProvider extends BaseServiceProvider {
|
|
1050
|
+
name = "auth";
|
|
1051
|
+
dependencies = ["config", "database"];
|
|
1052
|
+
register(container) {
|
|
1053
|
+
container.singleton("authService", AuthService);
|
|
1054
|
+
container.singleton("jwtService", JwtService);
|
|
1055
|
+
container.transient("authGuard", AuthGuard);
|
|
1056
|
+
}
|
|
166
1057
|
}
|
|
167
|
-
|
|
168
|
-
|
|
1058
|
+
|
|
1059
|
+
class ValidationServiceProvider extends BaseServiceProvider {
|
|
1060
|
+
name = "validation";
|
|
1061
|
+
register(container) {
|
|
1062
|
+
container.singleton("validator", Validator);
|
|
1063
|
+
container.factory("validationPipe", (validator) => new ValidationPipe(validator));
|
|
1064
|
+
}
|
|
169
1065
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
key,
|
|
181
|
-
token
|
|
182
|
-
};
|
|
183
|
-
target.constructor[PARAM_METADATA_KEY].get(propertyKey)[parameterIndex] = metadata;
|
|
184
|
-
};
|
|
1066
|
+
|
|
1067
|
+
class EventServiceProvider extends BaseDeferredServiceProvider {
|
|
1068
|
+
name = "events";
|
|
1069
|
+
provides() {
|
|
1070
|
+
return ["eventBus", "eventDispatcher", EventBus, EventDispatcher];
|
|
1071
|
+
}
|
|
1072
|
+
register(container) {
|
|
1073
|
+
container.singleton("eventBus", EventBus);
|
|
1074
|
+
container.singleton("eventDispatcher", EventDispatcher);
|
|
1075
|
+
}
|
|
185
1076
|
}
|
|
186
1077
|
|
|
187
|
-
class
|
|
188
|
-
|
|
189
|
-
return
|
|
1078
|
+
class ConfigService {
|
|
1079
|
+
get(key, defaultValue) {
|
|
1080
|
+
return defaultValue;
|
|
190
1081
|
}
|
|
191
|
-
|
|
192
|
-
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
class Logger {
|
|
1085
|
+
config;
|
|
1086
|
+
constructor(config) {
|
|
1087
|
+
this.config = config;
|
|
193
1088
|
}
|
|
194
|
-
|
|
195
|
-
|
|
1089
|
+
log(message) {
|
|
1090
|
+
console.log(message);
|
|
196
1091
|
}
|
|
197
|
-
|
|
198
|
-
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
class ConsoleLogger extends Logger {
|
|
1095
|
+
constructor() {
|
|
1096
|
+
super({});
|
|
199
1097
|
}
|
|
200
|
-
|
|
201
|
-
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
class FileLogger extends Logger {
|
|
1101
|
+
filePath;
|
|
1102
|
+
constructor(filePath) {
|
|
1103
|
+
super({});
|
|
1104
|
+
this.filePath = filePath;
|
|
202
1105
|
}
|
|
203
|
-
|
|
204
|
-
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
class DatabaseConnection {
|
|
1109
|
+
async connect() {}
|
|
1110
|
+
async disconnect() {}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
class UserRepository {
|
|
1114
|
+
db;
|
|
1115
|
+
constructor(db) {
|
|
1116
|
+
this.db = db;
|
|
205
1117
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
class PostRepository {
|
|
1121
|
+
db;
|
|
1122
|
+
constructor(db) {
|
|
1123
|
+
this.db = db;
|
|
212
1124
|
}
|
|
213
|
-
|
|
214
|
-
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
class CacheManager {
|
|
1128
|
+
store;
|
|
1129
|
+
constructor(store) {
|
|
1130
|
+
this.store = store;
|
|
215
1131
|
}
|
|
216
1132
|
}
|
|
217
1133
|
|
|
218
|
-
class
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
for (const _route of routes) {}
|
|
1134
|
+
class MemoryCache {
|
|
1135
|
+
cache = new Map;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
class RedisCache {
|
|
1139
|
+
config;
|
|
1140
|
+
constructor(config) {
|
|
1141
|
+
this.config = config;
|
|
227
1142
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
class AuthService {
|
|
1146
|
+
jwt;
|
|
1147
|
+
userRepo;
|
|
1148
|
+
constructor(jwt, userRepo) {
|
|
1149
|
+
this.jwt = jwt;
|
|
1150
|
+
this.userRepo = userRepo;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
class JwtService {
|
|
1155
|
+
config;
|
|
1156
|
+
constructor(config) {
|
|
1157
|
+
this.config = config;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
class AuthGuard {
|
|
1162
|
+
auth;
|
|
1163
|
+
constructor(auth) {
|
|
1164
|
+
this.auth = auth;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
class Validator {
|
|
1169
|
+
validate(_data, _rules) {
|
|
1170
|
+
return true;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
class ValidationPipe {
|
|
1175
|
+
validator;
|
|
1176
|
+
constructor(validator) {
|
|
1177
|
+
this.validator = validator;
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
class EventBus {
|
|
1182
|
+
emit(_event, _data) {}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
class EventDispatcher {
|
|
1186
|
+
bus;
|
|
1187
|
+
constructor(bus) {
|
|
1188
|
+
this.bus = bus;
|
|
250
1189
|
}
|
|
251
1190
|
}
|
|
252
|
-
var decoratorContainer = new DecoratorContainer;
|
|
253
1191
|
export {
|
|
254
1192
|
setContainer,
|
|
255
1193
|
getContainer,
|