@noxfly/noxus 2.1.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/dist/app-injector-B3MvgV3k.d.mts +95 -0
- package/dist/app-injector-B3MvgV3k.d.ts +95 -0
- package/dist/child.d.mts +209 -0
- package/dist/child.d.ts +209 -0
- package/dist/child.js +1426 -0
- package/dist/child.mjs +1356 -0
- package/dist/{index-CI3OMzNR.d.mts → index-BxWQVi6C.d.ts} +3 -68
- package/dist/{index-CI3OMzNR.d.ts → index-DQBQQfMw.d.mts} +3 -68
- package/dist/main.d.mts +6 -193
- package/dist/main.d.ts +6 -193
- package/dist/main.js +69 -5
- package/dist/main.mjs +63 -3
- package/dist/renderer.d.mts +2 -1
- package/dist/renderer.d.ts +2 -1
- package/dist/renderer.js +48 -4
- package/dist/renderer.mjs +47 -3
- package/package.json +6 -1
- package/src/DI/app-injector.ts +51 -10
- package/src/decorators/inject.decorator.ts +24 -0
- package/src/main.ts +2 -0
- package/src/non-electron-process.ts +23 -0
- package/src/utils/forward-ref.ts +31 -0
- package/tsup.config.ts +1 -0
package/dist/child.js
ADDED
|
@@ -0,0 +1,1426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2025 NoxFly
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @author NoxFly
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
var __create = Object.create;
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
12
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
28
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
29
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
30
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
31
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
32
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
33
|
+
mod
|
|
34
|
+
));
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
37
|
+
|
|
38
|
+
// src/non-electron-process.ts
|
|
39
|
+
var non_electron_process_exports = {};
|
|
40
|
+
__export(non_electron_process_exports, {
|
|
41
|
+
AppInjector: () => AppInjector,
|
|
42
|
+
BadGatewayException: () => BadGatewayException,
|
|
43
|
+
BadRequestException: () => BadRequestException,
|
|
44
|
+
ConflictException: () => ConflictException,
|
|
45
|
+
ForbiddenException: () => ForbiddenException,
|
|
46
|
+
ForwardReference: () => ForwardReference,
|
|
47
|
+
GatewayTimeoutException: () => GatewayTimeoutException,
|
|
48
|
+
HttpVersionNotSupportedException: () => HttpVersionNotSupportedException,
|
|
49
|
+
INJECTABLE_METADATA_KEY: () => INJECTABLE_METADATA_KEY,
|
|
50
|
+
INJECT_METADATA_KEY: () => INJECT_METADATA_KEY,
|
|
51
|
+
Inject: () => Inject,
|
|
52
|
+
Injectable: () => Injectable,
|
|
53
|
+
InsufficientStorageException: () => InsufficientStorageException,
|
|
54
|
+
InternalServerException: () => InternalServerException,
|
|
55
|
+
Logger: () => Logger,
|
|
56
|
+
LoopDetectedException: () => LoopDetectedException,
|
|
57
|
+
MethodNotAllowedException: () => MethodNotAllowedException,
|
|
58
|
+
NetworkAuthenticationRequiredException: () => NetworkAuthenticationRequiredException,
|
|
59
|
+
NetworkConnectTimeoutException: () => NetworkConnectTimeoutException,
|
|
60
|
+
NotAcceptableException: () => NotAcceptableException,
|
|
61
|
+
NotExtendedException: () => NotExtendedException,
|
|
62
|
+
NotFoundException: () => NotFoundException,
|
|
63
|
+
NotImplementedException: () => NotImplementedException,
|
|
64
|
+
PaymentRequiredException: () => PaymentRequiredException,
|
|
65
|
+
RequestTimeoutException: () => RequestTimeoutException,
|
|
66
|
+
ResponseException: () => ResponseException,
|
|
67
|
+
RootInjector: () => RootInjector,
|
|
68
|
+
ServiceUnavailableException: () => ServiceUnavailableException,
|
|
69
|
+
TooManyRequestsException: () => TooManyRequestsException,
|
|
70
|
+
UnauthorizedException: () => UnauthorizedException,
|
|
71
|
+
UpgradeRequiredException: () => UpgradeRequiredException,
|
|
72
|
+
VariantAlsoNegotiatesException: () => VariantAlsoNegotiatesException,
|
|
73
|
+
forwardRef: () => forwardRef,
|
|
74
|
+
getInjectableMetadata: () => getInjectableMetadata,
|
|
75
|
+
hasInjectableMetadata: () => hasInjectableMetadata,
|
|
76
|
+
inject: () => inject
|
|
77
|
+
});
|
|
78
|
+
module.exports = __toCommonJS(non_electron_process_exports);
|
|
79
|
+
|
|
80
|
+
// src/DI/app-injector.ts
|
|
81
|
+
var import_reflect_metadata2 = require("reflect-metadata");
|
|
82
|
+
|
|
83
|
+
// src/decorators/inject.decorator.ts
|
|
84
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
85
|
+
var INJECT_METADATA_KEY = "custom:inject";
|
|
86
|
+
function Inject(token) {
|
|
87
|
+
return (target, propertyKey, parameterIndex) => {
|
|
88
|
+
const existingParameters = Reflect.getOwnMetadata(INJECT_METADATA_KEY, target) || [];
|
|
89
|
+
existingParameters[parameterIndex] = token;
|
|
90
|
+
Reflect.defineMetadata(INJECT_METADATA_KEY, existingParameters, target);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
__name(Inject, "Inject");
|
|
94
|
+
|
|
95
|
+
// src/exceptions.ts
|
|
96
|
+
var _ResponseException = class _ResponseException extends Error {
|
|
97
|
+
constructor(statusOrMessage, message) {
|
|
98
|
+
let statusCode;
|
|
99
|
+
if (typeof statusOrMessage === "number") {
|
|
100
|
+
statusCode = statusOrMessage;
|
|
101
|
+
} else if (typeof statusOrMessage === "string") {
|
|
102
|
+
message = statusOrMessage;
|
|
103
|
+
}
|
|
104
|
+
super(message ?? "");
|
|
105
|
+
__publicField(this, "status", 0);
|
|
106
|
+
if (statusCode !== void 0) {
|
|
107
|
+
this.status = statusCode;
|
|
108
|
+
}
|
|
109
|
+
this.name = this.constructor.name.replace(/([A-Z])/g, " $1");
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
__name(_ResponseException, "ResponseException");
|
|
113
|
+
var ResponseException = _ResponseException;
|
|
114
|
+
var _BadRequestException = class _BadRequestException extends ResponseException {
|
|
115
|
+
constructor() {
|
|
116
|
+
super(...arguments);
|
|
117
|
+
__publicField(this, "status", 400);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
__name(_BadRequestException, "BadRequestException");
|
|
121
|
+
var BadRequestException = _BadRequestException;
|
|
122
|
+
var _UnauthorizedException = class _UnauthorizedException extends ResponseException {
|
|
123
|
+
constructor() {
|
|
124
|
+
super(...arguments);
|
|
125
|
+
__publicField(this, "status", 401);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
__name(_UnauthorizedException, "UnauthorizedException");
|
|
129
|
+
var UnauthorizedException = _UnauthorizedException;
|
|
130
|
+
var _PaymentRequiredException = class _PaymentRequiredException extends ResponseException {
|
|
131
|
+
constructor() {
|
|
132
|
+
super(...arguments);
|
|
133
|
+
__publicField(this, "status", 402);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
__name(_PaymentRequiredException, "PaymentRequiredException");
|
|
137
|
+
var PaymentRequiredException = _PaymentRequiredException;
|
|
138
|
+
var _ForbiddenException = class _ForbiddenException extends ResponseException {
|
|
139
|
+
constructor() {
|
|
140
|
+
super(...arguments);
|
|
141
|
+
__publicField(this, "status", 403);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
__name(_ForbiddenException, "ForbiddenException");
|
|
145
|
+
var ForbiddenException = _ForbiddenException;
|
|
146
|
+
var _NotFoundException = class _NotFoundException extends ResponseException {
|
|
147
|
+
constructor() {
|
|
148
|
+
super(...arguments);
|
|
149
|
+
__publicField(this, "status", 404);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
__name(_NotFoundException, "NotFoundException");
|
|
153
|
+
var NotFoundException = _NotFoundException;
|
|
154
|
+
var _MethodNotAllowedException = class _MethodNotAllowedException extends ResponseException {
|
|
155
|
+
constructor() {
|
|
156
|
+
super(...arguments);
|
|
157
|
+
__publicField(this, "status", 405);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
__name(_MethodNotAllowedException, "MethodNotAllowedException");
|
|
161
|
+
var MethodNotAllowedException = _MethodNotAllowedException;
|
|
162
|
+
var _NotAcceptableException = class _NotAcceptableException extends ResponseException {
|
|
163
|
+
constructor() {
|
|
164
|
+
super(...arguments);
|
|
165
|
+
__publicField(this, "status", 406);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
__name(_NotAcceptableException, "NotAcceptableException");
|
|
169
|
+
var NotAcceptableException = _NotAcceptableException;
|
|
170
|
+
var _RequestTimeoutException = class _RequestTimeoutException extends ResponseException {
|
|
171
|
+
constructor() {
|
|
172
|
+
super(...arguments);
|
|
173
|
+
__publicField(this, "status", 408);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
__name(_RequestTimeoutException, "RequestTimeoutException");
|
|
177
|
+
var RequestTimeoutException = _RequestTimeoutException;
|
|
178
|
+
var _ConflictException = class _ConflictException extends ResponseException {
|
|
179
|
+
constructor() {
|
|
180
|
+
super(...arguments);
|
|
181
|
+
__publicField(this, "status", 409);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
__name(_ConflictException, "ConflictException");
|
|
185
|
+
var ConflictException = _ConflictException;
|
|
186
|
+
var _UpgradeRequiredException = class _UpgradeRequiredException extends ResponseException {
|
|
187
|
+
constructor() {
|
|
188
|
+
super(...arguments);
|
|
189
|
+
__publicField(this, "status", 426);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
__name(_UpgradeRequiredException, "UpgradeRequiredException");
|
|
193
|
+
var UpgradeRequiredException = _UpgradeRequiredException;
|
|
194
|
+
var _TooManyRequestsException = class _TooManyRequestsException extends ResponseException {
|
|
195
|
+
constructor() {
|
|
196
|
+
super(...arguments);
|
|
197
|
+
__publicField(this, "status", 429);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
__name(_TooManyRequestsException, "TooManyRequestsException");
|
|
201
|
+
var TooManyRequestsException = _TooManyRequestsException;
|
|
202
|
+
var _InternalServerException = class _InternalServerException extends ResponseException {
|
|
203
|
+
constructor() {
|
|
204
|
+
super(...arguments);
|
|
205
|
+
__publicField(this, "status", 500);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
__name(_InternalServerException, "InternalServerException");
|
|
209
|
+
var InternalServerException = _InternalServerException;
|
|
210
|
+
var _NotImplementedException = class _NotImplementedException extends ResponseException {
|
|
211
|
+
constructor() {
|
|
212
|
+
super(...arguments);
|
|
213
|
+
__publicField(this, "status", 501);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
__name(_NotImplementedException, "NotImplementedException");
|
|
217
|
+
var NotImplementedException = _NotImplementedException;
|
|
218
|
+
var _BadGatewayException = class _BadGatewayException extends ResponseException {
|
|
219
|
+
constructor() {
|
|
220
|
+
super(...arguments);
|
|
221
|
+
__publicField(this, "status", 502);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
__name(_BadGatewayException, "BadGatewayException");
|
|
225
|
+
var BadGatewayException = _BadGatewayException;
|
|
226
|
+
var _ServiceUnavailableException = class _ServiceUnavailableException extends ResponseException {
|
|
227
|
+
constructor() {
|
|
228
|
+
super(...arguments);
|
|
229
|
+
__publicField(this, "status", 503);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
__name(_ServiceUnavailableException, "ServiceUnavailableException");
|
|
233
|
+
var ServiceUnavailableException = _ServiceUnavailableException;
|
|
234
|
+
var _GatewayTimeoutException = class _GatewayTimeoutException extends ResponseException {
|
|
235
|
+
constructor() {
|
|
236
|
+
super(...arguments);
|
|
237
|
+
__publicField(this, "status", 504);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
__name(_GatewayTimeoutException, "GatewayTimeoutException");
|
|
241
|
+
var GatewayTimeoutException = _GatewayTimeoutException;
|
|
242
|
+
var _HttpVersionNotSupportedException = class _HttpVersionNotSupportedException extends ResponseException {
|
|
243
|
+
constructor() {
|
|
244
|
+
super(...arguments);
|
|
245
|
+
__publicField(this, "status", 505);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
__name(_HttpVersionNotSupportedException, "HttpVersionNotSupportedException");
|
|
249
|
+
var HttpVersionNotSupportedException = _HttpVersionNotSupportedException;
|
|
250
|
+
var _VariantAlsoNegotiatesException = class _VariantAlsoNegotiatesException extends ResponseException {
|
|
251
|
+
constructor() {
|
|
252
|
+
super(...arguments);
|
|
253
|
+
__publicField(this, "status", 506);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
__name(_VariantAlsoNegotiatesException, "VariantAlsoNegotiatesException");
|
|
257
|
+
var VariantAlsoNegotiatesException = _VariantAlsoNegotiatesException;
|
|
258
|
+
var _InsufficientStorageException = class _InsufficientStorageException extends ResponseException {
|
|
259
|
+
constructor() {
|
|
260
|
+
super(...arguments);
|
|
261
|
+
__publicField(this, "status", 507);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
__name(_InsufficientStorageException, "InsufficientStorageException");
|
|
265
|
+
var InsufficientStorageException = _InsufficientStorageException;
|
|
266
|
+
var _LoopDetectedException = class _LoopDetectedException extends ResponseException {
|
|
267
|
+
constructor() {
|
|
268
|
+
super(...arguments);
|
|
269
|
+
__publicField(this, "status", 508);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
__name(_LoopDetectedException, "LoopDetectedException");
|
|
273
|
+
var LoopDetectedException = _LoopDetectedException;
|
|
274
|
+
var _NotExtendedException = class _NotExtendedException extends ResponseException {
|
|
275
|
+
constructor() {
|
|
276
|
+
super(...arguments);
|
|
277
|
+
__publicField(this, "status", 510);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
__name(_NotExtendedException, "NotExtendedException");
|
|
281
|
+
var NotExtendedException = _NotExtendedException;
|
|
282
|
+
var _NetworkAuthenticationRequiredException = class _NetworkAuthenticationRequiredException extends ResponseException {
|
|
283
|
+
constructor() {
|
|
284
|
+
super(...arguments);
|
|
285
|
+
__publicField(this, "status", 511);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
__name(_NetworkAuthenticationRequiredException, "NetworkAuthenticationRequiredException");
|
|
289
|
+
var NetworkAuthenticationRequiredException = _NetworkAuthenticationRequiredException;
|
|
290
|
+
var _NetworkConnectTimeoutException = class _NetworkConnectTimeoutException extends ResponseException {
|
|
291
|
+
constructor() {
|
|
292
|
+
super(...arguments);
|
|
293
|
+
__publicField(this, "status", 599);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
__name(_NetworkConnectTimeoutException, "NetworkConnectTimeoutException");
|
|
297
|
+
var NetworkConnectTimeoutException = _NetworkConnectTimeoutException;
|
|
298
|
+
|
|
299
|
+
// src/utils/forward-ref.ts
|
|
300
|
+
var _ForwardReference = class _ForwardReference {
|
|
301
|
+
constructor(forwardRefFn) {
|
|
302
|
+
__publicField(this, "forwardRefFn");
|
|
303
|
+
this.forwardRefFn = forwardRefFn;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
__name(_ForwardReference, "ForwardReference");
|
|
307
|
+
var ForwardReference = _ForwardReference;
|
|
308
|
+
function forwardRef(fn) {
|
|
309
|
+
return new ForwardReference(fn);
|
|
310
|
+
}
|
|
311
|
+
__name(forwardRef, "forwardRef");
|
|
312
|
+
|
|
313
|
+
// src/DI/app-injector.ts
|
|
314
|
+
var _AppInjector = class _AppInjector {
|
|
315
|
+
constructor(name = null) {
|
|
316
|
+
__publicField(this, "name");
|
|
317
|
+
__publicField(this, "bindings", /* @__PURE__ */ new Map());
|
|
318
|
+
__publicField(this, "singletons", /* @__PURE__ */ new Map());
|
|
319
|
+
__publicField(this, "scoped", /* @__PURE__ */ new Map());
|
|
320
|
+
this.name = name;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Typically used to create a dependency injection scope
|
|
324
|
+
* at the "scope" level (i.e., per-request lifetime).
|
|
325
|
+
*
|
|
326
|
+
* SHOULD NOT BE USED by anything else than the framework itself.
|
|
327
|
+
*/
|
|
328
|
+
createScope() {
|
|
329
|
+
const scope = new _AppInjector();
|
|
330
|
+
scope.bindings = this.bindings;
|
|
331
|
+
scope.singletons = this.singletons;
|
|
332
|
+
return scope;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Called when resolving a dependency,
|
|
336
|
+
* i.e., retrieving the instance of a given class.
|
|
337
|
+
*/
|
|
338
|
+
resolve(target) {
|
|
339
|
+
if (target instanceof ForwardReference) {
|
|
340
|
+
return new Proxy({}, {
|
|
341
|
+
get: /* @__PURE__ */ __name((obj, prop, receiver) => {
|
|
342
|
+
const realType = target.forwardRefFn();
|
|
343
|
+
const instance = this.resolve(realType);
|
|
344
|
+
const value = Reflect.get(instance, prop, receiver);
|
|
345
|
+
return typeof value === "function" ? value.bind(instance) : value;
|
|
346
|
+
}, "get"),
|
|
347
|
+
set: /* @__PURE__ */ __name((obj, prop, value, receiver) => {
|
|
348
|
+
const realType = target.forwardRefFn();
|
|
349
|
+
const instance = this.resolve(realType);
|
|
350
|
+
return Reflect.set(instance, prop, value, receiver);
|
|
351
|
+
}, "set"),
|
|
352
|
+
getPrototypeOf: /* @__PURE__ */ __name(() => {
|
|
353
|
+
const realType = target.forwardRefFn();
|
|
354
|
+
return realType.prototype;
|
|
355
|
+
}, "getPrototypeOf")
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
const binding = this.bindings.get(target);
|
|
359
|
+
if (!binding) {
|
|
360
|
+
if (target === void 0) {
|
|
361
|
+
throw new InternalServerException("Failed to resolve a dependency injection : Undefined target type.\nThis might be caused by a circular dependency.");
|
|
362
|
+
}
|
|
363
|
+
const name = target.name || "unknown";
|
|
364
|
+
throw new InternalServerException(`Failed to resolve a dependency injection : No binding for type ${name}.
|
|
365
|
+
Did you forget to use @Injectable() decorator ?`);
|
|
366
|
+
}
|
|
367
|
+
switch (binding.lifetime) {
|
|
368
|
+
case "transient":
|
|
369
|
+
return this.instantiate(binding.implementation);
|
|
370
|
+
case "scope": {
|
|
371
|
+
if (this.scoped.has(target)) {
|
|
372
|
+
return this.scoped.get(target);
|
|
373
|
+
}
|
|
374
|
+
const instance = this.instantiate(binding.implementation);
|
|
375
|
+
this.scoped.set(target, instance);
|
|
376
|
+
return instance;
|
|
377
|
+
}
|
|
378
|
+
case "singleton": {
|
|
379
|
+
if (binding.instance === void 0 && this.name === "root") {
|
|
380
|
+
binding.instance = this.instantiate(binding.implementation);
|
|
381
|
+
this.singletons.set(target, binding.instance);
|
|
382
|
+
}
|
|
383
|
+
return binding.instance;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Instantiates a class, resolving its dependencies.
|
|
389
|
+
*/
|
|
390
|
+
instantiate(target) {
|
|
391
|
+
const paramTypes = Reflect.getMetadata("design:paramtypes", target) || [];
|
|
392
|
+
const injectParams = Reflect.getMetadata(INJECT_METADATA_KEY, target) || [];
|
|
393
|
+
const params = paramTypes.map((paramType, index) => {
|
|
394
|
+
const overrideToken = injectParams[index];
|
|
395
|
+
const actualToken = overrideToken !== void 0 ? overrideToken : paramType;
|
|
396
|
+
return this.resolve(actualToken);
|
|
397
|
+
});
|
|
398
|
+
return new target(...params);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
__name(_AppInjector, "AppInjector");
|
|
402
|
+
var AppInjector = _AppInjector;
|
|
403
|
+
function inject(t) {
|
|
404
|
+
return RootInjector.resolve(t);
|
|
405
|
+
}
|
|
406
|
+
__name(inject, "inject");
|
|
407
|
+
var RootInjector = new AppInjector("root");
|
|
408
|
+
|
|
409
|
+
// src/decorators/guards.decorator.ts
|
|
410
|
+
function getGuardForController(controllerName) {
|
|
411
|
+
const key = `${controllerName}`;
|
|
412
|
+
return authorizations.get(key) ?? [];
|
|
413
|
+
}
|
|
414
|
+
__name(getGuardForController, "getGuardForController");
|
|
415
|
+
function getGuardForControllerAction(controllerName, actionName) {
|
|
416
|
+
const key = `${controllerName}.${actionName}`;
|
|
417
|
+
return authorizations.get(key) ?? [];
|
|
418
|
+
}
|
|
419
|
+
__name(getGuardForControllerAction, "getGuardForControllerAction");
|
|
420
|
+
var authorizations = /* @__PURE__ */ new Map();
|
|
421
|
+
|
|
422
|
+
// src/decorators/controller.decorator.ts
|
|
423
|
+
function getControllerMetadata(target) {
|
|
424
|
+
return Reflect.getMetadata(CONTROLLER_METADATA_KEY, target);
|
|
425
|
+
}
|
|
426
|
+
__name(getControllerMetadata, "getControllerMetadata");
|
|
427
|
+
var CONTROLLER_METADATA_KEY = Symbol("CONTROLLER_METADATA_KEY");
|
|
428
|
+
|
|
429
|
+
// src/decorators/injectable.metadata.ts
|
|
430
|
+
var INJECTABLE_METADATA_KEY = Symbol("INJECTABLE_METADATA_KEY");
|
|
431
|
+
function defineInjectableMetadata(target, lifetime) {
|
|
432
|
+
Reflect.defineMetadata(INJECTABLE_METADATA_KEY, lifetime, target);
|
|
433
|
+
}
|
|
434
|
+
__name(defineInjectableMetadata, "defineInjectableMetadata");
|
|
435
|
+
function getInjectableMetadata(target) {
|
|
436
|
+
return Reflect.getMetadata(INJECTABLE_METADATA_KEY, target);
|
|
437
|
+
}
|
|
438
|
+
__name(getInjectableMetadata, "getInjectableMetadata");
|
|
439
|
+
function hasInjectableMetadata(target) {
|
|
440
|
+
return Reflect.hasMetadata(INJECTABLE_METADATA_KEY, target);
|
|
441
|
+
}
|
|
442
|
+
__name(hasInjectableMetadata, "hasInjectableMetadata");
|
|
443
|
+
|
|
444
|
+
// src/decorators/method.decorator.ts
|
|
445
|
+
function createRouteDecorator(verb) {
|
|
446
|
+
return (path2) => {
|
|
447
|
+
return (target, propertyKey) => {
|
|
448
|
+
const existingRoutes = Reflect.getMetadata(ROUTE_METADATA_KEY, target.constructor) || [];
|
|
449
|
+
const metadata = {
|
|
450
|
+
method: verb,
|
|
451
|
+
path: path2.trim().replace(/^\/|\/$/g, ""),
|
|
452
|
+
handler: propertyKey,
|
|
453
|
+
guards: getGuardForControllerAction(target.constructor.__controllerName, propertyKey)
|
|
454
|
+
};
|
|
455
|
+
existingRoutes.push(metadata);
|
|
456
|
+
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target.constructor);
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
__name(createRouteDecorator, "createRouteDecorator");
|
|
461
|
+
function getRouteMetadata(target) {
|
|
462
|
+
return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
|
|
463
|
+
}
|
|
464
|
+
__name(getRouteMetadata, "getRouteMetadata");
|
|
465
|
+
var Get = createRouteDecorator("GET");
|
|
466
|
+
var Post = createRouteDecorator("POST");
|
|
467
|
+
var Put = createRouteDecorator("PUT");
|
|
468
|
+
var Patch = createRouteDecorator("PATCH");
|
|
469
|
+
var Delete = createRouteDecorator("DELETE");
|
|
470
|
+
var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
|
|
471
|
+
|
|
472
|
+
// src/decorators/module.decorator.ts
|
|
473
|
+
function getModuleMetadata(target) {
|
|
474
|
+
return Reflect.getMetadata(MODULE_METADATA_KEY, target);
|
|
475
|
+
}
|
|
476
|
+
__name(getModuleMetadata, "getModuleMetadata");
|
|
477
|
+
var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
|
|
478
|
+
|
|
479
|
+
// src/router.ts
|
|
480
|
+
var import_reflect_metadata4 = require("reflect-metadata");
|
|
481
|
+
|
|
482
|
+
// src/decorators/middleware.decorator.ts
|
|
483
|
+
function getMiddlewaresForController(controllerName) {
|
|
484
|
+
const key = `${controllerName}`;
|
|
485
|
+
return middlewares.get(key) ?? [];
|
|
486
|
+
}
|
|
487
|
+
__name(getMiddlewaresForController, "getMiddlewaresForController");
|
|
488
|
+
function getMiddlewaresForControllerAction(controllerName, actionName) {
|
|
489
|
+
const key = `${controllerName}.${actionName}`;
|
|
490
|
+
return middlewares.get(key) ?? [];
|
|
491
|
+
}
|
|
492
|
+
__name(getMiddlewaresForControllerAction, "getMiddlewaresForControllerAction");
|
|
493
|
+
var middlewares = /* @__PURE__ */ new Map();
|
|
494
|
+
|
|
495
|
+
// src/request.ts
|
|
496
|
+
var import_reflect_metadata3 = require("reflect-metadata");
|
|
497
|
+
var _Request = class _Request {
|
|
498
|
+
constructor(event, senderId, id, method, path2, body) {
|
|
499
|
+
__publicField(this, "event");
|
|
500
|
+
__publicField(this, "senderId");
|
|
501
|
+
__publicField(this, "id");
|
|
502
|
+
__publicField(this, "method");
|
|
503
|
+
__publicField(this, "path");
|
|
504
|
+
__publicField(this, "body");
|
|
505
|
+
__publicField(this, "context", RootInjector.createScope());
|
|
506
|
+
__publicField(this, "params", {});
|
|
507
|
+
this.event = event;
|
|
508
|
+
this.senderId = senderId;
|
|
509
|
+
this.id = id;
|
|
510
|
+
this.method = method;
|
|
511
|
+
this.path = path2;
|
|
512
|
+
this.body = body;
|
|
513
|
+
this.path = path2.replace(/^\/|\/$/g, "");
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
__name(_Request, "Request");
|
|
517
|
+
var Request = _Request;
|
|
518
|
+
|
|
519
|
+
// src/utils/logger.ts
|
|
520
|
+
var fs = __toESM(require("fs"));
|
|
521
|
+
var path = __toESM(require("path"));
|
|
522
|
+
function getPrettyTimestamp() {
|
|
523
|
+
const now = /* @__PURE__ */ new Date();
|
|
524
|
+
return `${now.getDate().toString().padStart(2, "0")}/${(now.getMonth() + 1).toString().padStart(2, "0")}/${now.getFullYear()} ${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
|
|
525
|
+
}
|
|
526
|
+
__name(getPrettyTimestamp, "getPrettyTimestamp");
|
|
527
|
+
function getLogPrefix(callee, messageType, color) {
|
|
528
|
+
const timestamp = getPrettyTimestamp();
|
|
529
|
+
const spaces = " ".repeat(10 - messageType.length);
|
|
530
|
+
let colReset = Logger.colors.initial;
|
|
531
|
+
let colCallee = Logger.colors.yellow;
|
|
532
|
+
if (color === void 0) {
|
|
533
|
+
color = "";
|
|
534
|
+
colReset = "";
|
|
535
|
+
colCallee = "";
|
|
536
|
+
}
|
|
537
|
+
return `${color}[APP] ${process.pid} - ${colReset}${timestamp}${spaces}${color}${messageType.toUpperCase()}${colReset} ${colCallee}[${callee}]${colReset}`;
|
|
538
|
+
}
|
|
539
|
+
__name(getLogPrefix, "getLogPrefix");
|
|
540
|
+
function formatObject(prefix, arg, enableColor = true) {
|
|
541
|
+
const json = JSON.stringify(arg, null, 2);
|
|
542
|
+
let colStart = "";
|
|
543
|
+
let colLine = "";
|
|
544
|
+
let colReset = "";
|
|
545
|
+
if (enableColor) {
|
|
546
|
+
colStart = Logger.colors.darkGrey;
|
|
547
|
+
colLine = Logger.colors.grey;
|
|
548
|
+
colReset = Logger.colors.initial;
|
|
549
|
+
}
|
|
550
|
+
const prefixedJson = json.split("\n").map((line, idx) => idx === 0 ? `${colStart}${line}` : `${prefix} ${colLine}${line}`).join("\n") + colReset;
|
|
551
|
+
return prefixedJson;
|
|
552
|
+
}
|
|
553
|
+
__name(formatObject, "formatObject");
|
|
554
|
+
function formattedArgs(prefix, args, color) {
|
|
555
|
+
let colReset = Logger.colors.initial;
|
|
556
|
+
if (color === void 0) {
|
|
557
|
+
color = "";
|
|
558
|
+
colReset = "";
|
|
559
|
+
}
|
|
560
|
+
return args.map((arg) => {
|
|
561
|
+
if (typeof arg === "string") {
|
|
562
|
+
return `${color}${arg}${colReset}`;
|
|
563
|
+
} else if (typeof arg === "object") {
|
|
564
|
+
return formatObject(prefix, arg, color !== "");
|
|
565
|
+
}
|
|
566
|
+
return arg;
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
__name(formattedArgs, "formattedArgs");
|
|
570
|
+
function getCallee() {
|
|
571
|
+
const stack = new Error().stack?.split("\n") ?? [];
|
|
572
|
+
const caller = stack[3]?.trim().match(/at (.+?)(?:\..+)? .+$/)?.[1]?.replace("Object", "").replace(/^_/, "") || "App";
|
|
573
|
+
return caller;
|
|
574
|
+
}
|
|
575
|
+
__name(getCallee, "getCallee");
|
|
576
|
+
function canLog(level) {
|
|
577
|
+
return logLevels.has(level);
|
|
578
|
+
}
|
|
579
|
+
__name(canLog, "canLog");
|
|
580
|
+
function processLogQueue(filepath) {
|
|
581
|
+
const state = fileStates.get(filepath);
|
|
582
|
+
if (!state || state.isWriting || state.queue.length === 0) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
state.isWriting = true;
|
|
586
|
+
const messagesToWrite = state.queue.join("\n") + "\n";
|
|
587
|
+
state.queue = [];
|
|
588
|
+
const dir = path.dirname(filepath);
|
|
589
|
+
fs.mkdir(dir, {
|
|
590
|
+
recursive: true
|
|
591
|
+
}, (err) => {
|
|
592
|
+
if (err) {
|
|
593
|
+
console.error(`[Logger] Failed to create directory ${dir}`, err);
|
|
594
|
+
state.isWriting = false;
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
fs.appendFile(filepath, messagesToWrite, {
|
|
598
|
+
encoding: "utf-8"
|
|
599
|
+
}, (err2) => {
|
|
600
|
+
state.isWriting = false;
|
|
601
|
+
if (err2) {
|
|
602
|
+
console.error(`[Logger] Failed to write log to ${filepath}`, err2);
|
|
603
|
+
}
|
|
604
|
+
if (state.queue.length > 0) {
|
|
605
|
+
processLogQueue(filepath);
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
__name(processLogQueue, "processLogQueue");
|
|
611
|
+
function enqueue(filepath, message) {
|
|
612
|
+
if (!fileStates.has(filepath)) {
|
|
613
|
+
fileStates.set(filepath, {
|
|
614
|
+
queue: [],
|
|
615
|
+
isWriting: false
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
const state = fileStates.get(filepath);
|
|
619
|
+
state.queue.push(message);
|
|
620
|
+
processLogQueue(filepath);
|
|
621
|
+
}
|
|
622
|
+
__name(enqueue, "enqueue");
|
|
623
|
+
function output(level, args) {
|
|
624
|
+
if (!canLog(level)) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
const callee = getCallee();
|
|
628
|
+
{
|
|
629
|
+
const prefix = getLogPrefix(callee, level, logLevelColors[level]);
|
|
630
|
+
const data = formattedArgs(prefix, args, logLevelColors[level]);
|
|
631
|
+
logLevelChannel[level](prefix, ...data);
|
|
632
|
+
}
|
|
633
|
+
{
|
|
634
|
+
const prefix = getLogPrefix(callee, level);
|
|
635
|
+
const data = formattedArgs(prefix, args);
|
|
636
|
+
const filepath = fileSettings.get(level)?.filepath;
|
|
637
|
+
if (filepath) {
|
|
638
|
+
const message = prefix + " " + data.join(" ").replace(/\x1b\[[0-9;]*m/g, "");
|
|
639
|
+
enqueue(filepath, message);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
__name(output, "output");
|
|
644
|
+
(function(Logger2) {
|
|
645
|
+
function setLogLevel(level) {
|
|
646
|
+
logLevels.clear();
|
|
647
|
+
if (Array.isArray(level)) {
|
|
648
|
+
for (const lvl of level) {
|
|
649
|
+
logLevels.add(lvl);
|
|
650
|
+
}
|
|
651
|
+
} else {
|
|
652
|
+
const targetRank = logLevelRank[level];
|
|
653
|
+
for (const [lvl, rank] of Object.entries(logLevelRank)) {
|
|
654
|
+
if (rank >= targetRank) {
|
|
655
|
+
logLevels.add(lvl);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
__name(setLogLevel, "setLogLevel");
|
|
661
|
+
Logger2.setLogLevel = setLogLevel;
|
|
662
|
+
function log(...args) {
|
|
663
|
+
output("log", args);
|
|
664
|
+
}
|
|
665
|
+
__name(log, "log");
|
|
666
|
+
Logger2.log = log;
|
|
667
|
+
function info(...args) {
|
|
668
|
+
output("info", args);
|
|
669
|
+
}
|
|
670
|
+
__name(info, "info");
|
|
671
|
+
Logger2.info = info;
|
|
672
|
+
function warn(...args) {
|
|
673
|
+
output("warn", args);
|
|
674
|
+
}
|
|
675
|
+
__name(warn, "warn");
|
|
676
|
+
Logger2.warn = warn;
|
|
677
|
+
function error(...args) {
|
|
678
|
+
output("error", args);
|
|
679
|
+
}
|
|
680
|
+
__name(error, "error");
|
|
681
|
+
Logger2.error = error;
|
|
682
|
+
function errorStack(...args) {
|
|
683
|
+
output("error", args);
|
|
684
|
+
}
|
|
685
|
+
__name(errorStack, "errorStack");
|
|
686
|
+
Logger2.errorStack = errorStack;
|
|
687
|
+
function debug(...args) {
|
|
688
|
+
output("debug", args);
|
|
689
|
+
}
|
|
690
|
+
__name(debug, "debug");
|
|
691
|
+
Logger2.debug = debug;
|
|
692
|
+
function comment(...args) {
|
|
693
|
+
output("comment", args);
|
|
694
|
+
}
|
|
695
|
+
__name(comment, "comment");
|
|
696
|
+
Logger2.comment = comment;
|
|
697
|
+
function critical(...args) {
|
|
698
|
+
output("critical", args);
|
|
699
|
+
}
|
|
700
|
+
__name(critical, "critical");
|
|
701
|
+
Logger2.critical = critical;
|
|
702
|
+
function enableFileLogging(filepath, levels = [
|
|
703
|
+
"debug",
|
|
704
|
+
"comment",
|
|
705
|
+
"log",
|
|
706
|
+
"info",
|
|
707
|
+
"warn",
|
|
708
|
+
"error",
|
|
709
|
+
"critical"
|
|
710
|
+
]) {
|
|
711
|
+
for (const level of levels) {
|
|
712
|
+
fileSettings.set(level, {
|
|
713
|
+
filepath
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
__name(enableFileLogging, "enableFileLogging");
|
|
718
|
+
Logger2.enableFileLogging = enableFileLogging;
|
|
719
|
+
function disableFileLogging(levels = [
|
|
720
|
+
"debug",
|
|
721
|
+
"comment",
|
|
722
|
+
"log",
|
|
723
|
+
"info",
|
|
724
|
+
"warn",
|
|
725
|
+
"error",
|
|
726
|
+
"critical"
|
|
727
|
+
]) {
|
|
728
|
+
for (const level of levels) {
|
|
729
|
+
fileSettings.delete(level);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
__name(disableFileLogging, "disableFileLogging");
|
|
733
|
+
Logger2.disableFileLogging = disableFileLogging;
|
|
734
|
+
Logger2.colors = {
|
|
735
|
+
black: "\x1B[0;30m",
|
|
736
|
+
grey: "\x1B[0;37m",
|
|
737
|
+
red: "\x1B[0;31m",
|
|
738
|
+
green: "\x1B[0;32m",
|
|
739
|
+
brown: "\x1B[0;33m",
|
|
740
|
+
blue: "\x1B[0;34m",
|
|
741
|
+
purple: "\x1B[0;35m",
|
|
742
|
+
darkGrey: "\x1B[1;30m",
|
|
743
|
+
lightRed: "\x1B[1;31m",
|
|
744
|
+
lightGreen: "\x1B[1;32m",
|
|
745
|
+
yellow: "\x1B[1;33m",
|
|
746
|
+
lightBlue: "\x1B[1;34m",
|
|
747
|
+
magenta: "\x1B[1;35m",
|
|
748
|
+
cyan: "\x1B[1;36m",
|
|
749
|
+
white: "\x1B[1;37m",
|
|
750
|
+
initial: "\x1B[0m"
|
|
751
|
+
};
|
|
752
|
+
})(Logger || (Logger = {}));
|
|
753
|
+
var fileSettings = /* @__PURE__ */ new Map();
|
|
754
|
+
var fileStates = /* @__PURE__ */ new Map();
|
|
755
|
+
var logLevels = /* @__PURE__ */ new Set();
|
|
756
|
+
var logLevelRank = {
|
|
757
|
+
debug: 0,
|
|
758
|
+
comment: 1,
|
|
759
|
+
log: 2,
|
|
760
|
+
info: 3,
|
|
761
|
+
warn: 4,
|
|
762
|
+
error: 5,
|
|
763
|
+
critical: 6
|
|
764
|
+
};
|
|
765
|
+
var logLevelColors = {
|
|
766
|
+
debug: Logger.colors.purple,
|
|
767
|
+
comment: Logger.colors.grey,
|
|
768
|
+
log: Logger.colors.green,
|
|
769
|
+
info: Logger.colors.blue,
|
|
770
|
+
warn: Logger.colors.brown,
|
|
771
|
+
error: Logger.colors.red,
|
|
772
|
+
critical: Logger.colors.lightRed
|
|
773
|
+
};
|
|
774
|
+
var logLevelChannel = {
|
|
775
|
+
debug: console.debug,
|
|
776
|
+
comment: console.debug,
|
|
777
|
+
log: console.log,
|
|
778
|
+
info: console.info,
|
|
779
|
+
warn: console.warn,
|
|
780
|
+
error: console.error,
|
|
781
|
+
critical: console.error
|
|
782
|
+
};
|
|
783
|
+
Logger.setLogLevel("debug");
|
|
784
|
+
var Logger;
|
|
785
|
+
|
|
786
|
+
// src/utils/radix-tree.ts
|
|
787
|
+
var _a;
|
|
788
|
+
var RadixNode = (_a = class {
|
|
789
|
+
/**
|
|
790
|
+
* Creates a new RadixNode.
|
|
791
|
+
* @param segment - The segment of the path this node represents.
|
|
792
|
+
*/
|
|
793
|
+
constructor(segment) {
|
|
794
|
+
__publicField(this, "segment");
|
|
795
|
+
__publicField(this, "children", []);
|
|
796
|
+
__publicField(this, "value");
|
|
797
|
+
__publicField(this, "isParam");
|
|
798
|
+
__publicField(this, "paramName");
|
|
799
|
+
this.segment = segment;
|
|
800
|
+
this.isParam = segment.startsWith(":");
|
|
801
|
+
if (this.isParam) {
|
|
802
|
+
this.paramName = segment.slice(1);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Matches a child node against a given segment.
|
|
807
|
+
* This method checks if the segment matches any of the children nodes.
|
|
808
|
+
* @param segment - The segment to match against the children of this node.
|
|
809
|
+
* @returns A child node that matches the segment, or undefined if no match is found.
|
|
810
|
+
*/
|
|
811
|
+
matchChild(segment) {
|
|
812
|
+
for (const child of this.children) {
|
|
813
|
+
if (child.isParam || segment.startsWith(child.segment)) return child;
|
|
814
|
+
}
|
|
815
|
+
return void 0;
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Finds a child node that matches the segment exactly.
|
|
819
|
+
* This method checks if there is a child node that matches the segment exactly.
|
|
820
|
+
* @param segment - The segment to find an exact match for among the children of this node.
|
|
821
|
+
* @returns A child node that matches the segment exactly, or undefined if no match is found.
|
|
822
|
+
*/
|
|
823
|
+
findExactChild(segment) {
|
|
824
|
+
return this.children.find((c) => c.segment === segment);
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Adds a child node to this node's children.
|
|
828
|
+
* This method adds a new child node to the list of children for this node.
|
|
829
|
+
* @param node - The child node to add to this node's children.
|
|
830
|
+
*/
|
|
831
|
+
addChild(node) {
|
|
832
|
+
this.children.push(node);
|
|
833
|
+
}
|
|
834
|
+
}, __name(_a, "RadixNode"), _a);
|
|
835
|
+
var _RadixTree = class _RadixTree {
|
|
836
|
+
constructor() {
|
|
837
|
+
__publicField(this, "root", new RadixNode(""));
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Inserts a path and its associated value into the Radix Tree.
|
|
841
|
+
* This method normalizes the path and inserts it into the tree, associating it with
|
|
842
|
+
* @param path - The path to insert into the tree.
|
|
843
|
+
* @param value - The value to associate with the path.
|
|
844
|
+
*/
|
|
845
|
+
insert(path2, value) {
|
|
846
|
+
const segments = this.normalize(path2);
|
|
847
|
+
this.insertRecursive(this.root, segments, value);
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Recursively inserts a path into the Radix Tree.
|
|
851
|
+
* This method traverses the tree and inserts the segments of the path, creating new nodes
|
|
852
|
+
* @param node - The node to start inserting from.
|
|
853
|
+
* @param segments - The segments of the path to insert.
|
|
854
|
+
* @param value - The value to associate with the path.
|
|
855
|
+
*/
|
|
856
|
+
insertRecursive(node, segments, value) {
|
|
857
|
+
if (segments.length === 0) {
|
|
858
|
+
node.value = value;
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
const segment = segments[0] ?? "";
|
|
862
|
+
let child = node.children.find((c) => c.isParam === segment.startsWith(":") && (c.isParam || c.segment === segment));
|
|
863
|
+
if (!child) {
|
|
864
|
+
child = new RadixNode(segment);
|
|
865
|
+
node.addChild(child);
|
|
866
|
+
}
|
|
867
|
+
this.insertRecursive(child, segments.slice(1), value);
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Searches for a path in the Radix Tree.
|
|
871
|
+
* This method normalizes the path and searches for it in the tree, returning the node
|
|
872
|
+
* @param path - The path to search for in the Radix Tree.
|
|
873
|
+
* @returns An ISearchResult containing the node and parameters if a match is found, otherwise undefined.
|
|
874
|
+
*/
|
|
875
|
+
search(path2) {
|
|
876
|
+
const segments = this.normalize(path2);
|
|
877
|
+
return this.searchRecursive(this.root, segments, {});
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Recursively searches for a path in the Radix Tree.
|
|
881
|
+
* This method traverses the tree and searches for the segments of the path, collecting parameters
|
|
882
|
+
* @param node - The node to start searching from.
|
|
883
|
+
* @param segments - The segments of the path to search for.
|
|
884
|
+
* @param params - The parameters collected during the search.
|
|
885
|
+
* @returns An ISearchResult containing the node and parameters if a match is found, otherwise undefined.
|
|
886
|
+
*/
|
|
887
|
+
searchRecursive(node, segments, params) {
|
|
888
|
+
if (segments.length === 0) {
|
|
889
|
+
if (node.value !== void 0) {
|
|
890
|
+
return {
|
|
891
|
+
node,
|
|
892
|
+
params
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
return void 0;
|
|
896
|
+
}
|
|
897
|
+
const [segment, ...rest] = segments;
|
|
898
|
+
for (const child of node.children) {
|
|
899
|
+
if (child.isParam) {
|
|
900
|
+
const paramName = child.paramName;
|
|
901
|
+
const childParams = {
|
|
902
|
+
...params,
|
|
903
|
+
[paramName]: segment ?? ""
|
|
904
|
+
};
|
|
905
|
+
if (rest.length === 0) {
|
|
906
|
+
return {
|
|
907
|
+
node: child,
|
|
908
|
+
params: childParams
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
const result = this.searchRecursive(child, rest, childParams);
|
|
912
|
+
if (result) return result;
|
|
913
|
+
} else if (segment === child.segment) {
|
|
914
|
+
if (rest.length === 0) {
|
|
915
|
+
return {
|
|
916
|
+
node: child,
|
|
917
|
+
params
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
const result = this.searchRecursive(child, rest, params);
|
|
921
|
+
if (result) return result;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return void 0;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Normalizes a path into an array of segments.
|
|
928
|
+
* This method removes leading and trailing slashes, splits the path by slashes, and
|
|
929
|
+
* @param path - The path to normalize.
|
|
930
|
+
* @returns An array of normalized path segments.
|
|
931
|
+
*/
|
|
932
|
+
normalize(path2) {
|
|
933
|
+
const segments = path2.replace(/^\/+|\/+$/g, "").split("/").filter(Boolean);
|
|
934
|
+
return [
|
|
935
|
+
"",
|
|
936
|
+
...segments
|
|
937
|
+
];
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
__name(_RadixTree, "RadixTree");
|
|
941
|
+
var RadixTree = _RadixTree;
|
|
942
|
+
|
|
943
|
+
// src/router.ts
|
|
944
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
945
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
946
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
947
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
948
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
949
|
+
}
|
|
950
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
951
|
+
var ATOMIC_HTTP_METHODS = /* @__PURE__ */ new Set([
|
|
952
|
+
"GET",
|
|
953
|
+
"POST",
|
|
954
|
+
"PUT",
|
|
955
|
+
"PATCH",
|
|
956
|
+
"DELETE"
|
|
957
|
+
]);
|
|
958
|
+
function isAtomicHttpMethod(method) {
|
|
959
|
+
return typeof method === "string" && ATOMIC_HTTP_METHODS.has(method);
|
|
960
|
+
}
|
|
961
|
+
__name(isAtomicHttpMethod, "isAtomicHttpMethod");
|
|
962
|
+
var _Router = class _Router {
|
|
963
|
+
constructor() {
|
|
964
|
+
__publicField(this, "routes", new RadixTree());
|
|
965
|
+
__publicField(this, "rootMiddlewares", []);
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Registers a controller class with the router.
|
|
969
|
+
* This method extracts the route metadata from the controller class and registers it in the routing tree.
|
|
970
|
+
* It also handles the guards and middlewares associated with the controller.
|
|
971
|
+
* @param controllerClass - The controller class to register.
|
|
972
|
+
*/
|
|
973
|
+
registerController(controllerClass) {
|
|
974
|
+
const controllerMeta = getControllerMetadata(controllerClass);
|
|
975
|
+
const controllerGuards = getGuardForController(controllerClass.name);
|
|
976
|
+
const controllerMiddlewares = getMiddlewaresForController(controllerClass.name);
|
|
977
|
+
if (!controllerMeta) throw new Error(`Missing @Controller decorator on ${controllerClass.name}`);
|
|
978
|
+
const routeMetadata = getRouteMetadata(controllerClass);
|
|
979
|
+
for (const def of routeMetadata) {
|
|
980
|
+
const fullPath = `${controllerMeta.path}/${def.path}`.replace(/\/+/g, "/");
|
|
981
|
+
const routeGuards = getGuardForControllerAction(controllerClass.name, def.handler);
|
|
982
|
+
const routeMiddlewares = getMiddlewaresForControllerAction(controllerClass.name, def.handler);
|
|
983
|
+
const guards = /* @__PURE__ */ new Set([
|
|
984
|
+
...controllerGuards,
|
|
985
|
+
...routeGuards
|
|
986
|
+
]);
|
|
987
|
+
const middlewares2 = /* @__PURE__ */ new Set([
|
|
988
|
+
...controllerMiddlewares,
|
|
989
|
+
...routeMiddlewares
|
|
990
|
+
]);
|
|
991
|
+
const routeDef = {
|
|
992
|
+
method: def.method,
|
|
993
|
+
path: fullPath,
|
|
994
|
+
controller: controllerClass,
|
|
995
|
+
handler: def.handler,
|
|
996
|
+
guards: [
|
|
997
|
+
...guards
|
|
998
|
+
],
|
|
999
|
+
middlewares: [
|
|
1000
|
+
...middlewares2
|
|
1001
|
+
]
|
|
1002
|
+
};
|
|
1003
|
+
this.routes.insert(fullPath + "/" + def.method, routeDef);
|
|
1004
|
+
const hasActionGuards = routeDef.guards.length > 0;
|
|
1005
|
+
const actionGuardsInfo = hasActionGuards ? "<" + routeDef.guards.map((g) => g.name).join("|") + ">" : "";
|
|
1006
|
+
Logger.log(`Mapped {${routeDef.method} /${fullPath}}${actionGuardsInfo} route`);
|
|
1007
|
+
}
|
|
1008
|
+
const hasCtrlGuards = controllerMeta.guards.length > 0;
|
|
1009
|
+
const controllerGuardsInfo = hasCtrlGuards ? "<" + controllerMeta.guards.map((g) => g.name).join("|") + ">" : "";
|
|
1010
|
+
Logger.log(`Mapped ${controllerClass.name}${controllerGuardsInfo} controller's routes`);
|
|
1011
|
+
return this;
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Defines a middleware for the root of the application.
|
|
1015
|
+
* This method allows you to register a middleware that will be applied to all requests
|
|
1016
|
+
* to the application, regardless of the controller or action.
|
|
1017
|
+
* @param middleware - The middleware class to register.
|
|
1018
|
+
*/
|
|
1019
|
+
defineRootMiddleware(middleware) {
|
|
1020
|
+
this.rootMiddlewares.push(middleware);
|
|
1021
|
+
return this;
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Shuts down the message channel for a specific sender ID.
|
|
1025
|
+
* This method closes the IPC channel for the specified sender ID and
|
|
1026
|
+
* removes it from the messagePorts map.
|
|
1027
|
+
* @param channelSenderId - The ID of the sender channel to shut down.
|
|
1028
|
+
*/
|
|
1029
|
+
async handle(request) {
|
|
1030
|
+
if (request.method === "BATCH") {
|
|
1031
|
+
return this.handleBatch(request);
|
|
1032
|
+
}
|
|
1033
|
+
return this.handleAtomic(request);
|
|
1034
|
+
}
|
|
1035
|
+
async handleAtomic(request) {
|
|
1036
|
+
Logger.comment(`> ${request.method} /${request.path}`);
|
|
1037
|
+
const t0 = performance.now();
|
|
1038
|
+
const response = {
|
|
1039
|
+
requestId: request.id,
|
|
1040
|
+
status: 200,
|
|
1041
|
+
body: null
|
|
1042
|
+
};
|
|
1043
|
+
let isCritical = false;
|
|
1044
|
+
try {
|
|
1045
|
+
const routeDef = this.findRoute(request);
|
|
1046
|
+
await this.resolveController(request, response, routeDef);
|
|
1047
|
+
if (response.status > 400) {
|
|
1048
|
+
throw new ResponseException(response.status, response.error);
|
|
1049
|
+
}
|
|
1050
|
+
} catch (error) {
|
|
1051
|
+
response.body = void 0;
|
|
1052
|
+
if (error instanceof ResponseException) {
|
|
1053
|
+
response.status = error.status;
|
|
1054
|
+
response.error = error.message;
|
|
1055
|
+
response.stack = error.stack;
|
|
1056
|
+
} else if (error instanceof Error) {
|
|
1057
|
+
isCritical = true;
|
|
1058
|
+
response.status = 500;
|
|
1059
|
+
response.error = error.message || "Internal Server Error";
|
|
1060
|
+
response.stack = error.stack || "No stack trace available";
|
|
1061
|
+
} else {
|
|
1062
|
+
isCritical = true;
|
|
1063
|
+
response.status = 500;
|
|
1064
|
+
response.error = "Unknown error occurred";
|
|
1065
|
+
response.stack = "No stack trace available";
|
|
1066
|
+
}
|
|
1067
|
+
} finally {
|
|
1068
|
+
const t1 = performance.now();
|
|
1069
|
+
const message = `< ${response.status} ${request.method} /${request.path} ${Logger.colors.yellow}${Math.round(t1 - t0)}ms${Logger.colors.initial}`;
|
|
1070
|
+
if (response.status < 400) {
|
|
1071
|
+
Logger.log(message);
|
|
1072
|
+
} else if (response.status < 500) {
|
|
1073
|
+
Logger.warn(message);
|
|
1074
|
+
} else {
|
|
1075
|
+
if (isCritical) {
|
|
1076
|
+
Logger.critical(message);
|
|
1077
|
+
} else {
|
|
1078
|
+
Logger.error(message);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (response.error !== void 0) {
|
|
1082
|
+
if (isCritical) {
|
|
1083
|
+
Logger.critical(response.error);
|
|
1084
|
+
} else {
|
|
1085
|
+
Logger.error(response.error);
|
|
1086
|
+
}
|
|
1087
|
+
if (response.stack !== void 0) {
|
|
1088
|
+
Logger.errorStack(response.stack);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return response;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
async handleBatch(request) {
|
|
1095
|
+
Logger.comment(`> ${request.method} /${request.path}`);
|
|
1096
|
+
const t0 = performance.now();
|
|
1097
|
+
const response = {
|
|
1098
|
+
requestId: request.id,
|
|
1099
|
+
status: 200,
|
|
1100
|
+
body: {
|
|
1101
|
+
responses: []
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
let isCritical = false;
|
|
1105
|
+
try {
|
|
1106
|
+
const payload = this.normalizeBatchPayload(request.body);
|
|
1107
|
+
const batchResponses = [];
|
|
1108
|
+
for (const [index, item] of payload.requests.entries()) {
|
|
1109
|
+
const subRequestId = item.requestId ?? `${request.id}:${index}`;
|
|
1110
|
+
const atomicRequest = new Request(request.event, request.senderId, subRequestId, item.method, item.path, item.body);
|
|
1111
|
+
batchResponses.push(await this.handleAtomic(atomicRequest));
|
|
1112
|
+
}
|
|
1113
|
+
response.body.responses = batchResponses;
|
|
1114
|
+
} catch (error) {
|
|
1115
|
+
response.body = void 0;
|
|
1116
|
+
if (error instanceof ResponseException) {
|
|
1117
|
+
response.status = error.status;
|
|
1118
|
+
response.error = error.message;
|
|
1119
|
+
response.stack = error.stack;
|
|
1120
|
+
} else if (error instanceof Error) {
|
|
1121
|
+
isCritical = true;
|
|
1122
|
+
response.status = 500;
|
|
1123
|
+
response.error = error.message || "Internal Server Error";
|
|
1124
|
+
response.stack = error.stack || "No stack trace available";
|
|
1125
|
+
} else {
|
|
1126
|
+
isCritical = true;
|
|
1127
|
+
response.status = 500;
|
|
1128
|
+
response.error = "Unknown error occurred";
|
|
1129
|
+
response.stack = "No stack trace available";
|
|
1130
|
+
}
|
|
1131
|
+
} finally {
|
|
1132
|
+
const t1 = performance.now();
|
|
1133
|
+
const message = `< ${response.status} ${request.method} /${request.path} ${Logger.colors.yellow}${Math.round(t1 - t0)}ms${Logger.colors.initial}`;
|
|
1134
|
+
if (response.status < 400) {
|
|
1135
|
+
Logger.log(message);
|
|
1136
|
+
} else if (response.status < 500) {
|
|
1137
|
+
Logger.warn(message);
|
|
1138
|
+
} else {
|
|
1139
|
+
if (isCritical) {
|
|
1140
|
+
Logger.critical(message);
|
|
1141
|
+
} else {
|
|
1142
|
+
Logger.error(message);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (response.error !== void 0) {
|
|
1146
|
+
if (isCritical) {
|
|
1147
|
+
Logger.critical(response.error);
|
|
1148
|
+
} else {
|
|
1149
|
+
Logger.error(response.error);
|
|
1150
|
+
}
|
|
1151
|
+
if (response.stack !== void 0) {
|
|
1152
|
+
Logger.errorStack(response.stack);
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
return response;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
normalizeBatchPayload(body) {
|
|
1159
|
+
if (body === null || typeof body !== "object") {
|
|
1160
|
+
throw new BadRequestException("Batch payload must be an object containing a requests array.");
|
|
1161
|
+
}
|
|
1162
|
+
const possiblePayload = body;
|
|
1163
|
+
const { requests } = possiblePayload;
|
|
1164
|
+
if (!Array.isArray(requests)) {
|
|
1165
|
+
throw new BadRequestException("Batch payload must define a requests array.");
|
|
1166
|
+
}
|
|
1167
|
+
const normalizedRequests = requests.map((entry, index) => this.normalizeBatchItem(entry, index));
|
|
1168
|
+
return {
|
|
1169
|
+
requests: normalizedRequests
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
normalizeBatchItem(entry, index) {
|
|
1173
|
+
if (entry === null || typeof entry !== "object") {
|
|
1174
|
+
throw new BadRequestException(`Batch request at index ${index} must be an object.`);
|
|
1175
|
+
}
|
|
1176
|
+
const { requestId, path: path2, method, body } = entry;
|
|
1177
|
+
if (requestId !== void 0 && typeof requestId !== "string") {
|
|
1178
|
+
throw new BadRequestException(`Batch request at index ${index} has an invalid requestId.`);
|
|
1179
|
+
}
|
|
1180
|
+
if (typeof path2 !== "string" || path2.length === 0) {
|
|
1181
|
+
throw new BadRequestException(`Batch request at index ${index} must define a non-empty path.`);
|
|
1182
|
+
}
|
|
1183
|
+
if (typeof method !== "string") {
|
|
1184
|
+
throw new BadRequestException(`Batch request at index ${index} must define an HTTP method.`);
|
|
1185
|
+
}
|
|
1186
|
+
const normalizedMethod = method.toUpperCase();
|
|
1187
|
+
if (!isAtomicHttpMethod(normalizedMethod)) {
|
|
1188
|
+
throw new BadRequestException(`Batch request at index ${index} uses the unsupported method ${method}.`);
|
|
1189
|
+
}
|
|
1190
|
+
return {
|
|
1191
|
+
requestId,
|
|
1192
|
+
path: path2,
|
|
1193
|
+
method: normalizedMethod,
|
|
1194
|
+
body
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Finds the route definition for a given request.
|
|
1199
|
+
* This method searches the routing tree for a matching route based on the request's path and method.
|
|
1200
|
+
* If no matching route is found, it throws a NotFoundException.
|
|
1201
|
+
* @param request - The Request object containing the method and path to search for.
|
|
1202
|
+
* @returns The IRouteDefinition for the matched route.
|
|
1203
|
+
*/
|
|
1204
|
+
findRoute(request) {
|
|
1205
|
+
const matchedRoutes = this.routes.search(request.path);
|
|
1206
|
+
if (matchedRoutes?.node === void 0 || matchedRoutes.node.children.length === 0) {
|
|
1207
|
+
throw new NotFoundException(`No route matches ${request.method} ${request.path}`);
|
|
1208
|
+
}
|
|
1209
|
+
const routeDef = matchedRoutes.node.findExactChild(request.method);
|
|
1210
|
+
if (routeDef?.value === void 0) {
|
|
1211
|
+
throw new MethodNotAllowedException(`Method Not Allowed for ${request.method} ${request.path}`);
|
|
1212
|
+
}
|
|
1213
|
+
return routeDef.value;
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Resolves the controller for a given route definition.
|
|
1217
|
+
* This method creates an instance of the controller class and prepares the request parameters.
|
|
1218
|
+
* It also runs the request pipeline, which includes executing middlewares and guards.
|
|
1219
|
+
* @param request - The Request object containing the request data.
|
|
1220
|
+
* @param response - The IResponse object to populate with the response data.
|
|
1221
|
+
* @param routeDef - The IRouteDefinition for the matched route.
|
|
1222
|
+
* @return A Promise that resolves when the controller action has been executed.
|
|
1223
|
+
* @throws UnauthorizedException if the request is not authorized by the guards.
|
|
1224
|
+
*/
|
|
1225
|
+
async resolveController(request, response, routeDef) {
|
|
1226
|
+
const controllerInstance = request.context.resolve(routeDef.controller);
|
|
1227
|
+
Object.assign(request.params, this.extractParams(request.path, routeDef.path));
|
|
1228
|
+
await this.runRequestPipeline(request, response, routeDef, controllerInstance);
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* Runs the request pipeline for a given request.
|
|
1232
|
+
* This method executes the middlewares and guards associated with the route,
|
|
1233
|
+
* and finally calls the controller action.
|
|
1234
|
+
* @param request - The Request object containing the request data.
|
|
1235
|
+
* @param response - The IResponse object to populate with the response data.
|
|
1236
|
+
* @param routeDef - The IRouteDefinition for the matched route.
|
|
1237
|
+
* @param controllerInstance - The instance of the controller class.
|
|
1238
|
+
* @return A Promise that resolves when the request pipeline has been executed.
|
|
1239
|
+
* @throws ResponseException if the response status is not successful.
|
|
1240
|
+
*/
|
|
1241
|
+
async runRequestPipeline(request, response, routeDef, controllerInstance) {
|
|
1242
|
+
const middlewares2 = [
|
|
1243
|
+
.../* @__PURE__ */ new Set([
|
|
1244
|
+
...this.rootMiddlewares,
|
|
1245
|
+
...routeDef.middlewares
|
|
1246
|
+
])
|
|
1247
|
+
];
|
|
1248
|
+
const middlewareMaxIndex = middlewares2.length - 1;
|
|
1249
|
+
const guardsMaxIndex = middlewareMaxIndex + routeDef.guards.length;
|
|
1250
|
+
let index = -1;
|
|
1251
|
+
const dispatch = /* @__PURE__ */ __name(async (i) => {
|
|
1252
|
+
if (i <= index) throw new Error("next() called multiple times");
|
|
1253
|
+
index = i;
|
|
1254
|
+
if (i <= middlewareMaxIndex) {
|
|
1255
|
+
const nextFn = dispatch.bind(null, i + 1);
|
|
1256
|
+
await this.runMiddleware(request, response, nextFn, middlewares2[i]);
|
|
1257
|
+
if (response.status >= 400) {
|
|
1258
|
+
throw new ResponseException(response.status, response.error);
|
|
1259
|
+
}
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
if (i <= guardsMaxIndex) {
|
|
1263
|
+
const guardIndex = i - middlewares2.length;
|
|
1264
|
+
const guardType = routeDef.guards[guardIndex];
|
|
1265
|
+
await this.runGuard(request, guardType);
|
|
1266
|
+
await dispatch(i + 1);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
const action = controllerInstance[routeDef.handler];
|
|
1270
|
+
response.body = await action.call(controllerInstance, request, response);
|
|
1271
|
+
if (response.body === void 0) {
|
|
1272
|
+
response.body = {};
|
|
1273
|
+
}
|
|
1274
|
+
}, "dispatch");
|
|
1275
|
+
await dispatch(0);
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Runs a middleware function in the request pipeline.
|
|
1279
|
+
* This method creates an instance of the middleware and invokes its `invoke` method,
|
|
1280
|
+
* passing the request, response, and next function.
|
|
1281
|
+
* @param request - The Request object containing the request data.
|
|
1282
|
+
* @param response - The IResponse object to populate with the response data.
|
|
1283
|
+
* @param next - The NextFunction to call to continue the middleware chain.
|
|
1284
|
+
* @param middlewareType - The type of the middleware to run.
|
|
1285
|
+
* @return A Promise that resolves when the middleware has been executed.
|
|
1286
|
+
*/
|
|
1287
|
+
async runMiddleware(request, response, next, middlewareType) {
|
|
1288
|
+
const middleware = request.context.resolve(middlewareType);
|
|
1289
|
+
await middleware.invoke(request, response, next);
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Runs a guard to check if the request is authorized.
|
|
1293
|
+
* This method creates an instance of the guard and calls its `canActivate` method.
|
|
1294
|
+
* If the guard returns false, it throws an UnauthorizedException.
|
|
1295
|
+
* @param request - The Request object containing the request data.
|
|
1296
|
+
* @param guardType - The type of the guard to run.
|
|
1297
|
+
* @return A Promise that resolves if the guard allows the request, or throws an UnauthorizedException if not.
|
|
1298
|
+
* @throws UnauthorizedException if the guard denies access to the request.
|
|
1299
|
+
*/
|
|
1300
|
+
async runGuard(request, guardType) {
|
|
1301
|
+
const guard = request.context.resolve(guardType);
|
|
1302
|
+
const allowed = await guard.canActivate(request);
|
|
1303
|
+
if (!allowed) throw new UnauthorizedException(`Unauthorized for ${request.method} ${request.path}`);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Extracts parameters from the actual request path based on the template path.
|
|
1307
|
+
* This method splits the actual path and the template path into segments,
|
|
1308
|
+
* then maps the segments to parameters based on the template.
|
|
1309
|
+
* @param actual - The actual request path.
|
|
1310
|
+
* @param template - The template path to extract parameters from.
|
|
1311
|
+
* @returns An object containing the extracted parameters.
|
|
1312
|
+
*/
|
|
1313
|
+
extractParams(actual, template) {
|
|
1314
|
+
const aParts = actual.split("/");
|
|
1315
|
+
const tParts = template.split("/");
|
|
1316
|
+
const params = {};
|
|
1317
|
+
tParts.forEach((part, i) => {
|
|
1318
|
+
if (part.startsWith(":")) {
|
|
1319
|
+
params[part.slice(1)] = aParts[i] ?? "";
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
return params;
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
__name(_Router, "Router");
|
|
1326
|
+
var Router = _Router;
|
|
1327
|
+
Router = _ts_decorate([
|
|
1328
|
+
Injectable("singleton")
|
|
1329
|
+
], Router);
|
|
1330
|
+
|
|
1331
|
+
// src/DI/injector-explorer.ts
|
|
1332
|
+
var _InjectorExplorer = class _InjectorExplorer {
|
|
1333
|
+
/**
|
|
1334
|
+
* Registers the class as injectable.
|
|
1335
|
+
* When a class is instantiated, if it has dependencies and those dependencies
|
|
1336
|
+
* are listed using this method, they will be injected into the class constructor.
|
|
1337
|
+
*/
|
|
1338
|
+
static register(target, lifetime) {
|
|
1339
|
+
if (RootInjector.bindings.has(target)) return RootInjector;
|
|
1340
|
+
RootInjector.bindings.set(target, {
|
|
1341
|
+
implementation: target,
|
|
1342
|
+
lifetime
|
|
1343
|
+
});
|
|
1344
|
+
if (lifetime === "singleton") {
|
|
1345
|
+
RootInjector.resolve(target);
|
|
1346
|
+
}
|
|
1347
|
+
if (getModuleMetadata(target)) {
|
|
1348
|
+
Logger.log(`${target.name} dependencies initialized`);
|
|
1349
|
+
return RootInjector;
|
|
1350
|
+
}
|
|
1351
|
+
const controllerMeta = getControllerMetadata(target);
|
|
1352
|
+
if (controllerMeta) {
|
|
1353
|
+
const router = RootInjector.resolve(Router);
|
|
1354
|
+
router?.registerController(target);
|
|
1355
|
+
return RootInjector;
|
|
1356
|
+
}
|
|
1357
|
+
const routeMeta = getRouteMetadata(target);
|
|
1358
|
+
if (routeMeta) {
|
|
1359
|
+
return RootInjector;
|
|
1360
|
+
}
|
|
1361
|
+
if (getInjectableMetadata(target)) {
|
|
1362
|
+
Logger.log(`Registered ${target.name} as ${lifetime}`);
|
|
1363
|
+
return RootInjector;
|
|
1364
|
+
}
|
|
1365
|
+
return RootInjector;
|
|
1366
|
+
}
|
|
1367
|
+
};
|
|
1368
|
+
__name(_InjectorExplorer, "InjectorExplorer");
|
|
1369
|
+
var InjectorExplorer = _InjectorExplorer;
|
|
1370
|
+
|
|
1371
|
+
// src/decorators/injectable.decorator.ts
|
|
1372
|
+
function Injectable(lifetime = "scope") {
|
|
1373
|
+
return (target) => {
|
|
1374
|
+
if (typeof target !== "function" || !target.prototype) {
|
|
1375
|
+
throw new Error(`@Injectable can only be used on classes, not on ${typeof target}`);
|
|
1376
|
+
}
|
|
1377
|
+
defineInjectableMetadata(target, lifetime);
|
|
1378
|
+
InjectorExplorer.register(target, lifetime);
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
__name(Injectable, "Injectable");
|
|
1382
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1383
|
+
0 && (module.exports = {
|
|
1384
|
+
AppInjector,
|
|
1385
|
+
BadGatewayException,
|
|
1386
|
+
BadRequestException,
|
|
1387
|
+
ConflictException,
|
|
1388
|
+
ForbiddenException,
|
|
1389
|
+
ForwardReference,
|
|
1390
|
+
GatewayTimeoutException,
|
|
1391
|
+
HttpVersionNotSupportedException,
|
|
1392
|
+
INJECTABLE_METADATA_KEY,
|
|
1393
|
+
INJECT_METADATA_KEY,
|
|
1394
|
+
Inject,
|
|
1395
|
+
Injectable,
|
|
1396
|
+
InsufficientStorageException,
|
|
1397
|
+
InternalServerException,
|
|
1398
|
+
Logger,
|
|
1399
|
+
LoopDetectedException,
|
|
1400
|
+
MethodNotAllowedException,
|
|
1401
|
+
NetworkAuthenticationRequiredException,
|
|
1402
|
+
NetworkConnectTimeoutException,
|
|
1403
|
+
NotAcceptableException,
|
|
1404
|
+
NotExtendedException,
|
|
1405
|
+
NotFoundException,
|
|
1406
|
+
NotImplementedException,
|
|
1407
|
+
PaymentRequiredException,
|
|
1408
|
+
RequestTimeoutException,
|
|
1409
|
+
ResponseException,
|
|
1410
|
+
RootInjector,
|
|
1411
|
+
ServiceUnavailableException,
|
|
1412
|
+
TooManyRequestsException,
|
|
1413
|
+
UnauthorizedException,
|
|
1414
|
+
UpgradeRequiredException,
|
|
1415
|
+
VariantAlsoNegotiatesException,
|
|
1416
|
+
forwardRef,
|
|
1417
|
+
getInjectableMetadata,
|
|
1418
|
+
hasInjectableMetadata,
|
|
1419
|
+
inject
|
|
1420
|
+
});
|
|
1421
|
+
/**
|
|
1422
|
+
* @copyright 2025 NoxFly
|
|
1423
|
+
* @license MIT
|
|
1424
|
+
* @author NoxFly
|
|
1425
|
+
*/
|
|
1426
|
+
//# sourceMappingURL=child.js.map
|