@stacksjs/bun-router 0.0.19 → 0.0.21
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-1ahs68ys.js +18 -0
- package/dist/{chunk-y3xq6wq6.js → chunk-cgptvjdf.js} +1 -1
- package/dist/chunk-g3ybefhg.js +1923 -0
- package/dist/cli.js +3 -3
- package/dist/container/index.js +211 -1149
- package/dist/index.js +2222 -366
- package/dist/types.d.ts +5 -0
- package/package.json +1 -2
- package/dist/chunk-2mx7fq49.js +0 -4
- package/dist/chunk-a8p83068.js +0 -103
package/dist/container/index.js
CHANGED
|
@@ -1,1193 +1,255 @@
|
|
|
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
|
-
|
|
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,
|
|
3
|
+
AuthGuard,
|
|
4
|
+
AuthService,
|
|
5
|
+
AuthServiceProvider,
|
|
6
|
+
BaseDeferredServiceProvider,
|
|
7
|
+
BaseServiceProvider,
|
|
8
|
+
BindingBuilder,
|
|
9
|
+
BindingConditions,
|
|
10
|
+
CacheManager,
|
|
11
|
+
CacheServiceProvider,
|
|
12
|
+
ConfigService,
|
|
13
|
+
ConfigServiceProvider,
|
|
14
|
+
ConsoleLogger,
|
|
15
|
+
Container,
|
|
16
|
+
ContextualContainer,
|
|
17
|
+
DatabaseConnection,
|
|
18
|
+
DatabaseServiceProvider,
|
|
19
|
+
DefaultContextualBindingBuilder,
|
|
20
|
+
DefaultServiceProviderManager,
|
|
21
|
+
EnvironmentManager,
|
|
22
|
+
EnvironmentPresets,
|
|
23
|
+
EventBus,
|
|
24
|
+
EventDispatcher,
|
|
25
|
+
EventServiceProvider,
|
|
26
|
+
FileLogger,
|
|
27
|
+
INJECTABLE_METADATA_KEY,
|
|
28
|
+
INJECT_METADATA_KEY,
|
|
29
|
+
JwtService,
|
|
30
|
+
Logger,
|
|
31
|
+
LoggingServiceProvider,
|
|
32
|
+
MemoryCache,
|
|
33
|
+
OPTIONAL_METADATA_KEY,
|
|
34
|
+
PostRepository,
|
|
35
|
+
RedisCache,
|
|
36
|
+
TAGGED_METADATA_KEY,
|
|
37
|
+
UserRepository,
|
|
38
|
+
ValidationPipe,
|
|
39
|
+
ValidationServiceProvider,
|
|
40
|
+
Validator,
|
|
41
|
+
createContainer,
|
|
42
|
+
getContainer,
|
|
43
|
+
setContainer
|
|
44
|
+
} from "../chunk-g3ybefhg.js";
|
|
45
|
+
import"../chunk-1ahs68ys.js";
|
|
46
|
+
// src/container/decorators.ts
|
|
47
|
+
var CONTROLLER_METADATA_KEY = Symbol("controller");
|
|
48
|
+
var ROUTE_METADATA_KEY = Symbol("route");
|
|
49
|
+
var MIDDLEWARE_METADATA_KEY = Symbol("middleware");
|
|
50
|
+
var PARAM_METADATA_KEY = Symbol("param");
|
|
51
|
+
function Injectable(metadata = {}) {
|
|
52
|
+
return (target) => {
|
|
53
|
+
target[INJECTABLE_METADATA_KEY] = metadata;
|
|
54
|
+
return target;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function Inject(token, options) {
|
|
58
|
+
return function(target, _propertyKey, parameterIndex) {
|
|
59
|
+
const classConstructor = target.constructor === Function ? target : target.constructor;
|
|
60
|
+
if (!classConstructor[INJECT_METADATA_KEY]) {
|
|
61
|
+
classConstructor[INJECT_METADATA_KEY] = [];
|
|
62
|
+
}
|
|
63
|
+
const metadata = {
|
|
141
64
|
token,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
resolving: context?.resolving || new Set,
|
|
146
|
-
parent: context?.parent
|
|
65
|
+
optional: options?.optional,
|
|
66
|
+
tags: options?.tags,
|
|
67
|
+
when: options?.when
|
|
147
68
|
};
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
}
|
|
69
|
+
classConstructor[INJECT_METADATA_KEY][parameterIndex] = metadata;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function Optional() {
|
|
73
|
+
return (target, _propertyKey, parameterIndex) => {
|
|
74
|
+
if (!target[OPTIONAL_METADATA_KEY]) {
|
|
75
|
+
target[OPTIONAL_METADATA_KEY] = [];
|
|
76
|
+
}
|
|
77
|
+
target[OPTIONAL_METADATA_KEY][parameterIndex] = true;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function Tagged(tag) {
|
|
81
|
+
return function(target, _propertyKey, parameterIndex) {
|
|
82
|
+
if (!target[TAGGED_METADATA_KEY]) {
|
|
83
|
+
target[TAGGED_METADATA_KEY] = [];
|
|
84
|
+
}
|
|
85
|
+
target[TAGGED_METADATA_KEY][parameterIndex] = tag;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function Controller(prefix, options) {
|
|
89
|
+
return function(target) {
|
|
90
|
+
const metadata = {
|
|
91
|
+
prefix: prefix || "",
|
|
92
|
+
middleware: options?.middleware || [],
|
|
93
|
+
tags: options?.tags || []
|
|
387
94
|
};
|
|
388
|
-
|
|
389
|
-
return
|
|
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
|
-
}
|
|
95
|
+
target[CONTROLLER_METADATA_KEY] = metadata;
|
|
96
|
+
return target;
|
|
97
|
+
};
|
|
414
98
|
}
|
|
415
|
-
|
|
416
|
-
|
|
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
|
-
}
|
|
99
|
+
function Get(path = "", options) {
|
|
100
|
+
return createRouteDecorator("GET", path, options);
|
|
478
101
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
if (!globalContainer) {
|
|
482
|
-
globalContainer = new Container;
|
|
483
|
-
}
|
|
484
|
-
return globalContainer;
|
|
102
|
+
function Post(path = "", options) {
|
|
103
|
+
return createRouteDecorator("POST", path, options);
|
|
485
104
|
}
|
|
486
|
-
function
|
|
487
|
-
|
|
105
|
+
function Put(path = "", options) {
|
|
106
|
+
return createRouteDecorator("PUT", path, options);
|
|
488
107
|
}
|
|
489
|
-
function
|
|
490
|
-
return
|
|
108
|
+
function Delete(path = "", options) {
|
|
109
|
+
return createRouteDecorator("DELETE", path, options);
|
|
491
110
|
}
|
|
492
|
-
|
|
493
|
-
|
|
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
|
-
}
|
|
111
|
+
function Patch(path = "", options) {
|
|
112
|
+
return createRouteDecorator("PATCH", path, options);
|
|
541
113
|
}
|
|
542
|
-
|
|
543
|
-
|
|
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
|
-
}
|
|
114
|
+
function Options(path = "", options) {
|
|
115
|
+
return createRouteDecorator("OPTIONS", path, options);
|
|
628
116
|
}
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
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, []);
|
|
117
|
+
function Head(path = "", options) {
|
|
118
|
+
return createRouteDecorator("HEAD", path, options);
|
|
119
|
+
}
|
|
120
|
+
function createRouteDecorator(method, path, options) {
|
|
121
|
+
return function(target, propertyKey, descriptor) {
|
|
122
|
+
if (!target.constructor[ROUTE_METADATA_KEY]) {
|
|
123
|
+
target.constructor[ROUTE_METADATA_KEY] = [];
|
|
643
124
|
}
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
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
|
|
125
|
+
const metadata = {
|
|
126
|
+
method,
|
|
127
|
+
path,
|
|
128
|
+
middleware: options?.middleware || [],
|
|
129
|
+
tags: options?.tags || []
|
|
673
130
|
};
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
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
|
-
}
|
|
131
|
+
target.constructor[ROUTE_METADATA_KEY].push({
|
|
132
|
+
...metadata,
|
|
133
|
+
propertyKey,
|
|
134
|
+
handler: descriptor.value
|
|
699
135
|
});
|
|
700
|
-
|
|
701
|
-
|
|
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
|
-
}
|
|
136
|
+
return descriptor;
|
|
137
|
+
};
|
|
715
138
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
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}`);
|
|
139
|
+
function UseMiddleware(...middleware) {
|
|
140
|
+
return function(target, propertyKey, descriptor) {
|
|
141
|
+
if (propertyKey && descriptor) {
|
|
142
|
+
if (!target.constructor[MIDDLEWARE_METADATA_KEY]) {
|
|
143
|
+
target.constructor[MIDDLEWARE_METADATA_KEY] = new Map;
|
|
973
144
|
}
|
|
145
|
+
target.constructor[MIDDLEWARE_METADATA_KEY].set(propertyKey, middleware);
|
|
974
146
|
} else {
|
|
975
|
-
|
|
147
|
+
target[MIDDLEWARE_METADATA_KEY] = middleware;
|
|
976
148
|
}
|
|
977
|
-
|
|
978
|
-
|
|
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
|
-
}
|
|
149
|
+
return descriptor || target;
|
|
150
|
+
};
|
|
996
151
|
}
|
|
997
|
-
|
|
998
|
-
|
|
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
|
-
}
|
|
152
|
+
function Param(key) {
|
|
153
|
+
return createParamDecorator("param", key);
|
|
1007
154
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
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
|
-
}
|
|
1019
|
-
}
|
|
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
|
-
}
|
|
155
|
+
function Query(key) {
|
|
156
|
+
return createParamDecorator("query", key);
|
|
1037
157
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
}
|
|
158
|
+
function Body() {
|
|
159
|
+
return createParamDecorator("body");
|
|
1047
160
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
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
|
-
}
|
|
161
|
+
function Header(key) {
|
|
162
|
+
return createParamDecorator("header", key);
|
|
1057
163
|
}
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
name = "validation";
|
|
1061
|
-
register(container) {
|
|
1062
|
-
container.singleton("validator", Validator);
|
|
1063
|
-
container.factory("validationPipe", (validator) => new ValidationPipe(validator));
|
|
1064
|
-
}
|
|
164
|
+
function Cookie(key) {
|
|
165
|
+
return createParamDecorator("cookie", key);
|
|
1065
166
|
}
|
|
1066
|
-
|
|
1067
|
-
|
|
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
|
-
}
|
|
167
|
+
function InjectParam(token) {
|
|
168
|
+
return createParamDecorator("param", undefined, token);
|
|
1076
169
|
}
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
}
|
|
170
|
+
function createParamDecorator(type, key, token) {
|
|
171
|
+
return function(target, propertyKey, parameterIndex) {
|
|
172
|
+
if (!target.constructor[PARAM_METADATA_KEY]) {
|
|
173
|
+
target.constructor[PARAM_METADATA_KEY] = new Map;
|
|
174
|
+
}
|
|
175
|
+
if (!target.constructor[PARAM_METADATA_KEY].has(propertyKey)) {
|
|
176
|
+
target.constructor[PARAM_METADATA_KEY].set(propertyKey, []);
|
|
177
|
+
}
|
|
178
|
+
const metadata = {
|
|
179
|
+
type,
|
|
180
|
+
key,
|
|
181
|
+
token
|
|
182
|
+
};
|
|
183
|
+
target.constructor[PARAM_METADATA_KEY].get(propertyKey)[parameterIndex] = metadata;
|
|
184
|
+
};
|
|
1092
185
|
}
|
|
1093
186
|
|
|
1094
|
-
class
|
|
1095
|
-
|
|
1096
|
-
|
|
187
|
+
class MetadataReader {
|
|
188
|
+
static getInjectableMetadata(target) {
|
|
189
|
+
return target[INJECTABLE_METADATA_KEY];
|
|
1097
190
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
class FileLogger extends Logger {
|
|
1101
|
-
filePath;
|
|
1102
|
-
constructor(filePath) {
|
|
1103
|
-
super({});
|
|
1104
|
-
this.filePath = filePath;
|
|
191
|
+
static getInjectMetadata(target) {
|
|
192
|
+
return target[INJECT_METADATA_KEY] || [];
|
|
1105
193
|
}
|
|
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;
|
|
194
|
+
static getOptionalMetadata(target) {
|
|
195
|
+
return target[OPTIONAL_METADATA_KEY] || [];
|
|
1117
196
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
class PostRepository {
|
|
1121
|
-
db;
|
|
1122
|
-
constructor(db) {
|
|
1123
|
-
this.db = db;
|
|
197
|
+
static getTaggedMetadata(target) {
|
|
198
|
+
return target[TAGGED_METADATA_KEY] || [];
|
|
1124
199
|
}
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
class CacheManager {
|
|
1128
|
-
store;
|
|
1129
|
-
constructor(store) {
|
|
1130
|
-
this.store = store;
|
|
200
|
+
static getControllerMetadata(target) {
|
|
201
|
+
return target[CONTROLLER_METADATA_KEY];
|
|
1131
202
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
class MemoryCache {
|
|
1135
|
-
cache = new Map;
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
class RedisCache {
|
|
1139
|
-
config;
|
|
1140
|
-
constructor(config) {
|
|
1141
|
-
this.config = config;
|
|
203
|
+
static getAllRoutes(target) {
|
|
204
|
+
return target[ROUTE_METADATA_KEY] || [];
|
|
1142
205
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
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;
|
|
206
|
+
static getMiddlewareMetadata(target, propertyKey) {
|
|
207
|
+
if (propertyKey) {
|
|
208
|
+
const methodMiddleware = target[MIDDLEWARE_METADATA_KEY]?.get(propertyKey);
|
|
209
|
+
return methodMiddleware || [];
|
|
210
|
+
}
|
|
211
|
+
return target[MIDDLEWARE_METADATA_KEY] || [];
|
|
1165
212
|
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
class Validator {
|
|
1169
|
-
validate(_data, _rules) {
|
|
1170
|
-
return true;
|
|
213
|
+
static getParamMetadata(target, propertyKey) {
|
|
214
|
+
return target[PARAM_METADATA_KEY]?.get(propertyKey) || [];
|
|
1171
215
|
}
|
|
1172
216
|
}
|
|
1173
217
|
|
|
1174
|
-
class
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
218
|
+
class DecoratorContainer extends Container {
|
|
219
|
+
registerController(controllerClass) {
|
|
220
|
+
const metadata = MetadataReader.getControllerMetadata(controllerClass);
|
|
221
|
+
if (!metadata) {
|
|
222
|
+
throw new Error(`Class ${controllerClass.name} is not decorated with @Controller`);
|
|
223
|
+
}
|
|
224
|
+
this.singleton(controllerClass, controllerClass);
|
|
225
|
+
const routes = MetadataReader.getAllRoutes(controllerClass);
|
|
226
|
+
for (const _route of routes) {}
|
|
1178
227
|
}
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
228
|
+
resolve(token, context) {
|
|
229
|
+
if (typeof token === "function") {
|
|
230
|
+
const metadata = MetadataReader.getInjectableMetadata(token);
|
|
231
|
+
if (metadata) {
|
|
232
|
+
try {
|
|
233
|
+
return super.resolve(token, context);
|
|
234
|
+
} catch {
|
|
235
|
+
const scope = metadata.scope || "transient";
|
|
236
|
+
switch (scope) {
|
|
237
|
+
case "singleton":
|
|
238
|
+
this.singleton(token, token);
|
|
239
|
+
break;
|
|
240
|
+
case "transient":
|
|
241
|
+
this.transient(token, token);
|
|
242
|
+
break;
|
|
243
|
+
default:
|
|
244
|
+
this.transient(token, token);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return super.resolve(token, context);
|
|
1189
250
|
}
|
|
1190
251
|
}
|
|
252
|
+
var decoratorContainer = new DecoratorContainer;
|
|
1191
253
|
export {
|
|
1192
254
|
setContainer,
|
|
1193
255
|
getContainer,
|