@carno.js/core 0.2.11 → 1.0.2
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/LICENSE +673 -673
- package/README.md +188 -0
- package/dist/Carno.js +272 -333
- package/dist/Carno.mjs +268 -0
- package/dist/DefaultRoutes.js +51 -0
- package/dist/DefaultRoutes.mjs +34 -0
- package/dist/bun/index.js +183 -0
- package/dist/bun/index.js.map +86 -0
- package/dist/cache/CacheDriver.js +13 -0
- package/dist/cache/CacheDriver.mjs +0 -0
- package/dist/cache/CacheService.js +113 -0
- package/dist/cache/CacheService.mjs +93 -0
- package/dist/cache/MemoryDriver.js +66 -0
- package/dist/cache/MemoryDriver.mjs +46 -0
- package/dist/cache/RedisDriver.js +81 -0
- package/dist/cache/RedisDriver.mjs +61 -0
- package/dist/compiler/JITCompiler.js +111 -0
- package/dist/compiler/JITCompiler.mjs +89 -0
- package/dist/container/Container.js +99 -0
- package/dist/container/Container.mjs +78 -0
- package/dist/context/Context.js +97 -0
- package/dist/context/Context.mjs +77 -0
- package/dist/cors/CorsHandler.js +90 -0
- package/dist/cors/CorsHandler.mjs +68 -0
- package/dist/decorators/Controller.js +42 -0
- package/dist/decorators/Controller.mjs +22 -0
- package/dist/decorators/Inject.js +30 -0
- package/dist/decorators/Inject.mjs +10 -0
- package/dist/decorators/Middleware.js +35 -0
- package/dist/decorators/Middleware.mjs +15 -0
- package/dist/decorators/Service.js +31 -0
- package/dist/decorators/Service.mjs +12 -0
- package/dist/decorators/methods.js +60 -0
- package/dist/decorators/methods.mjs +34 -0
- package/dist/decorators/params.js +63 -0
- package/dist/decorators/params.mjs +37 -0
- package/dist/events/Lifecycle.js +69 -0
- package/dist/events/Lifecycle.mjs +41 -0
- package/dist/exceptions/HttpException.js +112 -26
- package/dist/exceptions/HttpException.mjs +82 -0
- package/dist/index.js +129 -31
- package/dist/index.mjs +94 -0
- package/dist/metadata.js +34 -0
- package/dist/metadata.mjs +9 -0
- package/dist/middleware/CarnoMiddleware.js +13 -0
- package/dist/middleware/CarnoMiddleware.mjs +0 -0
- package/dist/router/RadixRouter.js +121 -0
- package/dist/router/RadixRouter.mjs +101 -0
- package/dist/testing/TestHarness.js +81 -0
- package/dist/testing/TestHarness.mjs +60 -0
- package/dist/utils/Metadata.js +53 -0
- package/dist/utils/Metadata.mjs +31 -0
- package/dist/validation/ValibotAdapter.js +67 -0
- package/dist/validation/ValibotAdapter.mjs +48 -0
- package/dist/validation/ValidatorAdapter.js +35 -19
- package/dist/validation/ValidatorAdapter.mjs +14 -0
- package/dist/validation/ZodAdapter.js +80 -0
- package/dist/validation/ZodAdapter.mjs +59 -0
- package/package.json +21 -63
- package/src/Carno.ts +605 -0
- package/src/DefaultRoutes.ts +34 -0
- package/src/cache/CacheDriver.ts +50 -0
- package/src/cache/CacheService.ts +139 -0
- package/src/cache/MemoryDriver.ts +104 -0
- package/src/cache/RedisDriver.ts +116 -0
- package/src/compiler/JITCompiler.ts +167 -0
- package/src/container/Container.ts +168 -0
- package/src/context/Context.ts +128 -0
- package/src/cors/CorsHandler.ts +145 -0
- package/src/decorators/Controller.ts +63 -0
- package/src/decorators/Inject.ts +16 -0
- package/src/decorators/Middleware.ts +22 -0
- package/src/decorators/Service.ts +18 -0
- package/src/decorators/methods.ts +58 -0
- package/src/decorators/params.ts +47 -0
- package/src/events/Lifecycle.ts +97 -0
- package/src/exceptions/HttpException.ts +99 -0
- package/src/index.ts +92 -0
- package/src/metadata.ts +46 -0
- package/src/middleware/CarnoMiddleware.ts +14 -0
- package/src/router/RadixRouter.ts +225 -0
- package/src/testing/TestHarness.ts +177 -0
- package/src/utils/Metadata.ts +43 -0
- package/src/validation/ValibotAdapter.ts +95 -0
- package/src/validation/ValidatorAdapter.ts +69 -0
- package/src/validation/ZodAdapter.ts +102 -0
- package/dist/Carno.d.ts +0 -75
- package/dist/cache/bento-cache.driver.d.ts +0 -13
- package/dist/cache/bento-cache.driver.js +0 -55
- package/dist/cache/cache.service.d.ts +0 -8
- package/dist/cache/cache.service.js +0 -6
- package/dist/commons/decorators/Injectable.decorator.d.ts +0 -20
- package/dist/commons/decorators/Injectable.decorator.js +0 -33
- package/dist/commons/decorators/controller.decorator.d.ts +0 -8
- package/dist/commons/decorators/controller.decorator.js +0 -22
- package/dist/commons/decorators/http.decorators.d.ts +0 -13
- package/dist/commons/decorators/http.decorators.js +0 -63
- package/dist/commons/decorators/index.d.ts +0 -6
- package/dist/commons/decorators/index.js +0 -22
- package/dist/commons/decorators/inject.decorator.d.ts +0 -1
- package/dist/commons/decorators/inject.decorator.js +0 -5
- package/dist/commons/decorators/middleware.decorator.d.ts +0 -2
- package/dist/commons/decorators/middleware.decorator.js +0 -30
- package/dist/commons/decorators/service.decorator.d.ts +0 -2
- package/dist/commons/decorators/service.decorator.js +0 -7
- package/dist/commons/decorators/validation.decorator.d.ts +0 -32
- package/dist/commons/decorators/validation.decorator.js +0 -40
- package/dist/commons/http-code.enum.d.ts +0 -50
- package/dist/commons/http-code.enum.js +0 -54
- package/dist/commons/index.d.ts +0 -3
- package/dist/commons/index.js +0 -19
- package/dist/commons/registries/ProviderControl.d.ts +0 -77
- package/dist/commons/registries/ProviderControl.js +0 -112
- package/dist/commons/registries/ProviderRegistry.d.ts +0 -7
- package/dist/commons/registries/ProviderRegistry.js +0 -20
- package/dist/constants.d.ts +0 -8
- package/dist/constants.js +0 -11
- package/dist/container/ContainerConfiguration.d.ts +0 -45
- package/dist/container/ContainerConfiguration.js +0 -121
- package/dist/container/DependencyResolver.d.ts +0 -20
- package/dist/container/DependencyResolver.js +0 -85
- package/dist/container/InjectorService.d.ts +0 -58
- package/dist/container/InjectorService.js +0 -286
- package/dist/container/MethodInvoker.d.ts +0 -21
- package/dist/container/MethodInvoker.js +0 -83
- package/dist/container/RouteResolver.d.ts +0 -27
- package/dist/container/RouteResolver.js +0 -173
- package/dist/container/container.d.ts +0 -41
- package/dist/container/container.js +0 -71
- package/dist/container/createContainer.d.ts +0 -3
- package/dist/container/createContainer.js +0 -12
- package/dist/container/createInjector.d.ts +0 -2
- package/dist/container/createInjector.js +0 -7
- package/dist/container/index.d.ts +0 -6
- package/dist/container/index.js +0 -22
- package/dist/container/middleware.resolver.d.ts +0 -9
- package/dist/container/middleware.resolver.js +0 -35
- package/dist/default-routes-carno.d.ts +0 -3
- package/dist/default-routes-carno.js +0 -29
- package/dist/domain/BaseContext.d.ts +0 -15
- package/dist/domain/BaseContext.js +0 -2
- package/dist/domain/CarnoClosure.d.ts +0 -1
- package/dist/domain/CarnoClosure.js +0 -2
- package/dist/domain/CarnoMiddleware.d.ts +0 -5
- package/dist/domain/CarnoMiddleware.js +0 -2
- package/dist/domain/Context.d.ts +0 -58
- package/dist/domain/Context.js +0 -188
- package/dist/domain/FastContext.d.ts +0 -34
- package/dist/domain/FastContext.js +0 -59
- package/dist/domain/LocalsContainer.d.ts +0 -4
- package/dist/domain/LocalsContainer.js +0 -10
- package/dist/domain/Metadata.d.ts +0 -449
- package/dist/domain/Metadata.js +0 -511
- package/dist/domain/cors-config.d.ts +0 -12
- package/dist/domain/cors-config.js +0 -18
- package/dist/domain/cors-headers-cache.d.ts +0 -17
- package/dist/domain/cors-headers-cache.js +0 -101
- package/dist/domain/http-method.d.ts +0 -7
- package/dist/domain/http-method.js +0 -11
- package/dist/domain/index.d.ts +0 -10
- package/dist/domain/index.js +0 -26
- package/dist/domain/provider-scope.d.ts +0 -5
- package/dist/domain/provider-scope.js +0 -9
- package/dist/domain/provider-type.d.ts +0 -6
- package/dist/domain/provider-type.js +0 -10
- package/dist/domain/provider.d.ts +0 -37
- package/dist/domain/provider.js +0 -70
- package/dist/events/hooks.decorator.d.ts +0 -3
- package/dist/events/hooks.decorator.js +0 -29
- package/dist/events/index.d.ts +0 -2
- package/dist/events/index.js +0 -18
- package/dist/events/on-event.d.ts +0 -13
- package/dist/events/on-event.js +0 -11
- package/dist/exceptions/HttpException.d.ts +0 -9
- package/dist/exceptions/index.d.ts +0 -1
- package/dist/exceptions/index.js +0 -17
- package/dist/index.d.ts +0 -16
- package/dist/route/CompiledRoute.d.ts +0 -23
- package/dist/route/CompiledRoute.js +0 -9
- package/dist/route/FastPathExecutor.d.ts +0 -12
- package/dist/route/FastPathExecutor.js +0 -50
- package/dist/route/JITCompiler.d.ts +0 -28
- package/dist/route/JITCompiler.js +0 -245
- package/dist/route/Matcher.d.ts +0 -11
- package/dist/route/Matcher.js +0 -48
- package/dist/route/ParamResolverFactory.d.ts +0 -14
- package/dist/route/ParamResolverFactory.js +0 -49
- package/dist/route/RouteCompiler.d.ts +0 -28
- package/dist/route/RouteCompiler.js +0 -157
- package/dist/route/RouteExecutor.d.ts +0 -12
- package/dist/route/RouteExecutor.js +0 -84
- package/dist/route/memoirist.d.ts +0 -31
- package/dist/route/memoirist.js +0 -373
- package/dist/services/logger.service.d.ts +0 -23
- package/dist/services/logger.service.js +0 -47
- package/dist/testing/core-testing.d.ts +0 -24
- package/dist/testing/core-testing.js +0 -102
- package/dist/testing/index.d.ts +0 -1
- package/dist/testing/index.js +0 -17
- package/dist/utils/ValidationCache.d.ts +0 -5
- package/dist/utils/ValidationCache.js +0 -35
- package/dist/utils/ancestorOf.d.ts +0 -2
- package/dist/utils/ancestorOf.js +0 -10
- package/dist/utils/ancestorsOf.d.ts +0 -6
- package/dist/utils/ancestorsOf.js +0 -20
- package/dist/utils/classOf.d.ts +0 -13
- package/dist/utils/classOf.js +0 -21
- package/dist/utils/cleanObject.d.ts +0 -6
- package/dist/utils/cleanObject.js +0 -22
- package/dist/utils/constructorOf.d.ts +0 -11
- package/dist/utils/constructorOf.js +0 -18
- package/dist/utils/createInstance.d.ts +0 -1
- package/dist/utils/createInstance.js +0 -7
- package/dist/utils/decoratorTypeOf.d.ts +0 -11
- package/dist/utils/decoratorTypeOf.js +0 -32
- package/dist/utils/deepClone.d.ts +0 -6
- package/dist/utils/deepClone.js +0 -63
- package/dist/utils/deepMerge.d.ts +0 -9
- package/dist/utils/deepMerge.js +0 -62
- package/dist/utils/descriptorOf.d.ts +0 -8
- package/dist/utils/descriptorOf.js +0 -16
- package/dist/utils/formatValidationErrors.d.ts +0 -5
- package/dist/utils/formatValidationErrors.js +0 -80
- package/dist/utils/getClassOrSymbol.d.ts +0 -1
- package/dist/utils/getClassOrSymbol.js +0 -8
- package/dist/utils/getConstructorArgNames.d.ts +0 -1
- package/dist/utils/getConstructorArgNames.js +0 -12
- package/dist/utils/getMethodArgTypes.d.ts +0 -1
- package/dist/utils/getMethodArgTypes.js +0 -9
- package/dist/utils/getValue.d.ts +0 -32
- package/dist/utils/getValue.js +0 -47
- package/dist/utils/hasJsonMethod.d.ts +0 -1
- package/dist/utils/hasJsonMethod.js +0 -6
- package/dist/utils/index.d.ts +0 -15
- package/dist/utils/index.js +0 -31
- package/dist/utils/isArray.d.ts +0 -13
- package/dist/utils/isArray.js +0 -21
- package/dist/utils/isArrowFn.d.ts +0 -1
- package/dist/utils/isArrowFn.js +0 -7
- package/dist/utils/isBoolean.d.ts +0 -7
- package/dist/utils/isBoolean.js +0 -15
- package/dist/utils/isBuffer.d.ts +0 -7
- package/dist/utils/isBuffer.js +0 -19
- package/dist/utils/isClass.d.ts +0 -1
- package/dist/utils/isClass.js +0 -26
- package/dist/utils/isCollection.d.ts +0 -6
- package/dist/utils/isCollection.js +0 -20
- package/dist/utils/isDate.d.ts +0 -6
- package/dist/utils/isDate.js +0 -11
- package/dist/utils/isEmpty.d.ts +0 -6
- package/dist/utils/isEmpty.js +0 -12
- package/dist/utils/isFunction.d.ts +0 -1
- package/dist/utils/isFunction.js +0 -6
- package/dist/utils/isInheritedFrom.d.ts +0 -1
- package/dist/utils/isInheritedFrom.js +0 -24
- package/dist/utils/isMomentObject.d.ts +0 -1
- package/dist/utils/isMomentObject.js +0 -6
- package/dist/utils/isMongooseObject.d.ts +0 -2
- package/dist/utils/isMongooseObject.js +0 -11
- package/dist/utils/isNil.d.ts +0 -1
- package/dist/utils/isNil.js +0 -6
- package/dist/utils/isNumber.d.ts +0 -7
- package/dist/utils/isNumber.js +0 -15
- package/dist/utils/isObject.d.ts +0 -1
- package/dist/utils/isObject.js +0 -6
- package/dist/utils/isObservable.d.ts +0 -1
- package/dist/utils/isObservable.js +0 -6
- package/dist/utils/isPlainObject.d.ts +0 -7
- package/dist/utils/isPlainObject.js +0 -16
- package/dist/utils/isPrimitive.d.ts +0 -14
- package/dist/utils/isPrimitive.js +0 -28
- package/dist/utils/isPrimitiveType.d.ts +0 -1
- package/dist/utils/isPrimitiveType.js +0 -11
- package/dist/utils/isPromise.d.ts +0 -7
- package/dist/utils/isPromise.js +0 -14
- package/dist/utils/isProtectedKey.d.ts +0 -5
- package/dist/utils/isProtectedKey.js +0 -10
- package/dist/utils/isRegExp.d.ts +0 -1
- package/dist/utils/isRegExp.js +0 -6
- package/dist/utils/isRequestScope.d.ts +0 -11
- package/dist/utils/isRequestScope.js +0 -23
- package/dist/utils/isSerializable.d.ts +0 -1
- package/dist/utils/isSerializable.js +0 -11
- package/dist/utils/isStream.d.ts +0 -1
- package/dist/utils/isStream.js +0 -6
- package/dist/utils/isString.d.ts +0 -6
- package/dist/utils/isString.js +0 -14
- package/dist/utils/isSymbol.d.ts +0 -6
- package/dist/utils/isSymbol.js +0 -14
- package/dist/utils/methodsOf.d.ts +0 -9
- package/dist/utils/methodsOf.js +0 -24
- package/dist/utils/nameOf.d.ts +0 -14
- package/dist/utils/nameOf.js +0 -31
- package/dist/utils/objectKeys.d.ts +0 -1
- package/dist/utils/objectKeys.js +0 -7
- package/dist/utils/primitiveOf.d.ts +0 -1
- package/dist/utils/primitiveOf.js +0 -18
- package/dist/utils/prototypeOf.d.ts +0 -6
- package/dist/utils/prototypeOf.js +0 -12
- package/dist/utils/setValue.d.ts +0 -1
- package/dist/utils/setValue.js +0 -32
- package/dist/utils/toMap.d.ts +0 -3
- package/dist/utils/toMap.js +0 -34
- package/dist/utils/toStringConstructor.d.ts +0 -1
- package/dist/utils/toStringConstructor.js +0 -10
- package/dist/validation/ValidatorAdapter.d.ts +0 -66
- package/dist/validation/adapters/ClassValidatorAdapter.d.ts +0 -23
- package/dist/validation/adapters/ClassValidatorAdapter.js +0 -47
- package/dist/validation/adapters/ZodAdapter.d.ts +0 -14
- package/dist/validation/adapters/ZodAdapter.js +0 -56
- package/dist/validation/adapters/index.d.ts +0 -4
- package/dist/validation/adapters/index.js +0 -7
- package/dist/validation/index.d.ts +0 -3
- package/dist/validation/index.js +0 -20
package/dist/Carno.mjs
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { CONTROLLER_META, ROUTES_META, PARAMS_META, MIDDLEWARE_META } from "./metadata.mjs";
|
|
3
|
+
import { compileHandler } from "./compiler/JITCompiler.mjs";
|
|
4
|
+
import { Context } from "./context/Context.mjs";
|
|
5
|
+
import { Container, Scope } from "./container/Container.mjs";
|
|
6
|
+
import { CorsHandler } from "./cors/CorsHandler.mjs";
|
|
7
|
+
import { HttpException } from "./exceptions/HttpException.mjs";
|
|
8
|
+
import { ValidationException } from "./validation/ZodAdapter.mjs";
|
|
9
|
+
import { EventType, hasEventHandlers, getEventHandlers } from "./events/Lifecycle.mjs";
|
|
10
|
+
import { CacheService } from "./cache/CacheService.mjs";
|
|
11
|
+
import { DEFAULT_STATIC_ROUTES } from "./DefaultRoutes.mjs";
|
|
12
|
+
import { ZodAdapter } from "./validation/ZodAdapter.mjs";
|
|
13
|
+
const NOT_FOUND_RESPONSE = new Response("Not Found", { status: 404 }), TEXT_OPTS = Object.freeze({
|
|
14
|
+
status: 200,
|
|
15
|
+
headers: { "Content-Type": "text/plain" }
|
|
16
|
+
}), JSON_OPTS = Object.freeze({
|
|
17
|
+
status: 200,
|
|
18
|
+
headers: { "Content-Type": "application/json" }
|
|
19
|
+
}), INTERNAL_ERROR_RESPONSE = new Response(
|
|
20
|
+
'{"statusCode":500,"message":"Internal Server Error"}',
|
|
21
|
+
{ status: 500, headers: { "Content-Type": "application/json" } }
|
|
22
|
+
);
|
|
23
|
+
class Carno {
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this._controllers = [];
|
|
27
|
+
this._services = [];
|
|
28
|
+
this._middlewares = [];
|
|
29
|
+
this.routes = {};
|
|
30
|
+
this.container = new Container();
|
|
31
|
+
this.corsHandler = null;
|
|
32
|
+
this.hasCors = !1;
|
|
33
|
+
this.validator = null;
|
|
34
|
+
// Cached lifecycle event flags - checked once at startup
|
|
35
|
+
this.hasInitHooks = !1;
|
|
36
|
+
this.hasBootHooks = !1;
|
|
37
|
+
this.hasShutdownHooks = !1;
|
|
38
|
+
if (this.config.exports = this.config.exports || [], this.config.globalMiddlewares = this.config.globalMiddlewares || [], this.config.cors && (this.corsHandler = new CorsHandler(this.config.cors), this.hasCors = !0), this.config.validation === void 0 || this.config.validation === !0)
|
|
39
|
+
this.validator = new ZodAdapter();
|
|
40
|
+
else if (typeof this.config.validation == "function") {
|
|
41
|
+
const AdapterClass = this.config.validation;
|
|
42
|
+
this.validator = new AdapterClass();
|
|
43
|
+
} else this.config.validation && (this.validator = this.config.validation);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Use a Carno plugin.
|
|
47
|
+
* Imports controllers, services and global middlewares from another Carno instance.
|
|
48
|
+
*/
|
|
49
|
+
use(plugin) {
|
|
50
|
+
plugin._controllers.length > 0 && this._controllers.push(...plugin._controllers);
|
|
51
|
+
for (const exported of plugin.config.exports || []) {
|
|
52
|
+
const existingService = this.findServiceInPlugin(plugin, exported), serviceToAdd = this.shouldCloneService(existingService) ? { ...existingService } : exported;
|
|
53
|
+
this._services.push(serviceToAdd);
|
|
54
|
+
}
|
|
55
|
+
return plugin._services.length > 0 && this._services.push(...plugin._services), plugin.config.globalMiddlewares && this._middlewares.push(...plugin.config.globalMiddlewares), plugin._middlewares.length > 0 && this._middlewares.push(...plugin._middlewares), this;
|
|
56
|
+
}
|
|
57
|
+
findServiceInPlugin(plugin, exported) {
|
|
58
|
+
return plugin._services.find(
|
|
59
|
+
(s) => this.getServiceToken(s) === this.getServiceToken(exported)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
getServiceToken(service) {
|
|
63
|
+
return service?.token || service;
|
|
64
|
+
}
|
|
65
|
+
shouldCloneService(service) {
|
|
66
|
+
return !!(service?.useValue !== void 0 || service?.useClass);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Register one or more services/providers.
|
|
70
|
+
*/
|
|
71
|
+
services(serviceClass) {
|
|
72
|
+
const items = Array.isArray(serviceClass) ? serviceClass : [serviceClass];
|
|
73
|
+
return this._services.push(...items), this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Register one or more global middlewares.
|
|
77
|
+
*/
|
|
78
|
+
middlewares(handler) {
|
|
79
|
+
const items = Array.isArray(handler) ? handler : [handler];
|
|
80
|
+
return this._middlewares.push(...items), this;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Register one or more controllers.
|
|
84
|
+
*/
|
|
85
|
+
controllers(controllerClass) {
|
|
86
|
+
const items = Array.isArray(controllerClass) ? controllerClass : [controllerClass];
|
|
87
|
+
return this._controllers.push(...items), this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get a service instance from the container.
|
|
91
|
+
*/
|
|
92
|
+
get(token) {
|
|
93
|
+
return this.container.get(token);
|
|
94
|
+
}
|
|
95
|
+
listen(port = 3e3) {
|
|
96
|
+
this.bootstrap(), this.compileRoutes();
|
|
97
|
+
const config = {
|
|
98
|
+
port,
|
|
99
|
+
fetch: this.handleNotFound.bind(this),
|
|
100
|
+
error: this.handleError.bind(this),
|
|
101
|
+
routes: {
|
|
102
|
+
...DEFAULT_STATIC_ROUTES,
|
|
103
|
+
...this.routes
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
this.server = Bun.serve(config), this.hasBootHooks && this.executeLifecycleHooks(EventType.BOOT), this.hasShutdownHooks && this.registerShutdownHandlers(), this.config.disableStartupLog || console.log(`Carno running on port ${port}`);
|
|
107
|
+
}
|
|
108
|
+
bootstrap() {
|
|
109
|
+
this.hasInitHooks = hasEventHandlers(EventType.INIT), this.hasBootHooks = hasEventHandlers(EventType.BOOT), this.hasShutdownHooks = hasEventHandlers(EventType.SHUTDOWN), this.container.register({
|
|
110
|
+
token: Container,
|
|
111
|
+
useValue: this.container
|
|
112
|
+
});
|
|
113
|
+
const cacheConfig = typeof this.config.cache == "object" ? this.config.cache : {};
|
|
114
|
+
this.container.register({
|
|
115
|
+
token: CacheService,
|
|
116
|
+
useValue: new CacheService(cacheConfig)
|
|
117
|
+
});
|
|
118
|
+
for (const service of this._services)
|
|
119
|
+
this.container.register(service);
|
|
120
|
+
for (const ControllerClass of this._controllers)
|
|
121
|
+
this.container.register(ControllerClass);
|
|
122
|
+
this.hasInitHooks && this.executeLifecycleHooks(EventType.INIT);
|
|
123
|
+
for (const service of this._services) {
|
|
124
|
+
const token = typeof service == "function" ? service : service.token, serviceConfig = typeof service == "function" ? null : service;
|
|
125
|
+
(!serviceConfig || serviceConfig.scope !== Scope.REQUEST) && this.container.get(token);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
compileRoutes() {
|
|
129
|
+
for (const ControllerClass of this._controllers)
|
|
130
|
+
this.compileController(ControllerClass);
|
|
131
|
+
}
|
|
132
|
+
compileController(ControllerClass, parentPath = "", inheritedMiddlewares = []) {
|
|
133
|
+
const meta = Reflect.getMetadata(CONTROLLER_META, ControllerClass) || { path: "" }, basePath = parentPath + (meta.path || ""), routes = Reflect.getMetadata(ROUTES_META, ControllerClass) || [], middlewares = Reflect.getMetadata(MIDDLEWARE_META, ControllerClass) || [], instance = this.container.get(ControllerClass), controllerMiddlewares = middlewares.filter((m) => !m.target).map((m) => m.handler), scopedMiddlewares = [...inheritedMiddlewares, ...controllerMiddlewares];
|
|
134
|
+
for (const route of routes) {
|
|
135
|
+
const fullPath = this.normalizePath(basePath + route.path), params = Reflect.getMetadata(PARAMS_META, ControllerClass, route.handlerName) || [], routeMiddlewares = middlewares.filter((m) => m.target === route.handlerName).map((m) => m.handler), paramTypes = Reflect.getMetadata("design:paramtypes", ControllerClass.prototype, route.handlerName) || [];
|
|
136
|
+
let bodyDtoType = null;
|
|
137
|
+
for (const param of params)
|
|
138
|
+
if (param.type === "body" && !param.key) {
|
|
139
|
+
const dtoType = paramTypes[param.index];
|
|
140
|
+
dtoType && this.validator?.hasValidation(dtoType) && (bodyDtoType = dtoType);
|
|
141
|
+
}
|
|
142
|
+
const compiled = compileHandler(instance, route.handlerName, params), resolvedMiddlewares = [
|
|
143
|
+
...this.config.globalMiddlewares || [],
|
|
144
|
+
...this._middlewares,
|
|
145
|
+
...scopedMiddlewares,
|
|
146
|
+
...routeMiddlewares
|
|
147
|
+
].map((m) => this.resolveMiddleware(m)), hasMiddlewares = resolvedMiddlewares.length > 0, method = route.method.toUpperCase();
|
|
148
|
+
compiled.isStatic && !hasMiddlewares ? this.registerRoute(fullPath, method, this.createStaticResponse(compiled.staticValue)) : this.registerRoute(fullPath, method, this.createHandler(compiled, params, resolvedMiddlewares, bodyDtoType));
|
|
149
|
+
}
|
|
150
|
+
if (meta.children)
|
|
151
|
+
for (const ChildController of meta.children)
|
|
152
|
+
this.container.has(ChildController) || this.container.register(ChildController), this.compileController(ChildController, basePath, scopedMiddlewares);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Register a route with Bun's native router format.
|
|
156
|
+
* Path: "/users/:id", Method: "GET", Handler: Function or Response
|
|
157
|
+
*/
|
|
158
|
+
registerRoute(path, method, handler) {
|
|
159
|
+
this.routes[path] || (this.routes[path] = {}), this.routes[path][method] = handler;
|
|
160
|
+
}
|
|
161
|
+
createStaticResponse(value) {
|
|
162
|
+
const isString = typeof value == "string", body = isString ? value : JSON.stringify(value), opts = isString ? TEXT_OPTS : JSON_OPTS;
|
|
163
|
+
return new Response(body, opts);
|
|
164
|
+
}
|
|
165
|
+
createHandler(compiled, params, middlewares, bodyDtoType) {
|
|
166
|
+
const handler = compiled.fn, hasMiddlewares = middlewares.length > 0, hasParams = params.length > 0, applyCors = this.hasCors ? this.applyCors.bind(this) : null, validator = bodyDtoType ? this.validator : null, hasMiddlewaresOrValidation = hasMiddlewares || !!validator;
|
|
167
|
+
return !hasMiddlewaresOrValidation && !hasParams ? compiled.isAsync ? async (req) => {
|
|
168
|
+
const ctx = new Context(req), result = await handler(ctx), response = this.buildResponse(result);
|
|
169
|
+
return applyCors ? applyCors(response, req) : response;
|
|
170
|
+
} : (req) => {
|
|
171
|
+
const ctx = new Context(req), result = handler(ctx), response = this.buildResponse(result);
|
|
172
|
+
return applyCors ? applyCors(response, req) : response;
|
|
173
|
+
} : !hasMiddlewaresOrValidation && hasParams ? compiled.isAsync ? async (req) => {
|
|
174
|
+
const ctx = new Context(req, req.params), result = await handler(ctx), response = this.buildResponse(result);
|
|
175
|
+
return applyCors ? applyCors(response, req) : response;
|
|
176
|
+
} : (req) => {
|
|
177
|
+
const ctx = new Context(req, req.params), result = handler(ctx), response = this.buildResponse(result);
|
|
178
|
+
return applyCors ? applyCors(response, req) : response;
|
|
179
|
+
} : async (req) => {
|
|
180
|
+
const ctx = new Context(req, req.params || {});
|
|
181
|
+
for (const middleware of middlewares) {
|
|
182
|
+
const result2 = await middleware(ctx);
|
|
183
|
+
if (result2 instanceof Response)
|
|
184
|
+
return applyCors ? applyCors(result2, req) : result2;
|
|
185
|
+
}
|
|
186
|
+
validator && bodyDtoType && (await ctx.parseBody(), validator.validateOrThrow(bodyDtoType, ctx.body));
|
|
187
|
+
const result = compiled.isAsync ? await handler(ctx) : handler(ctx), response = this.buildResponse(result);
|
|
188
|
+
return applyCors ? applyCors(response, req) : response;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
resolveMiddleware(middleware) {
|
|
192
|
+
if (typeof middleware == "function" && middleware.prototype?.handle) {
|
|
193
|
+
const instance = this.container.get(middleware);
|
|
194
|
+
return (ctx) => instance.handle(ctx, () => {
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return middleware;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Apply CORS headers to a response.
|
|
201
|
+
*/
|
|
202
|
+
applyCors(response, req) {
|
|
203
|
+
const origin = req.headers.get("origin");
|
|
204
|
+
return origin && this.corsHandler ? this.corsHandler.apply(response, origin) : response;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Fallback handler - only called for unmatched routes.
|
|
208
|
+
* All matched routes go through Bun's native router.
|
|
209
|
+
*/
|
|
210
|
+
handleNotFound(req) {
|
|
211
|
+
if (this.hasCors && req.method === "OPTIONS") {
|
|
212
|
+
const origin = req.headers.get("origin");
|
|
213
|
+
if (origin)
|
|
214
|
+
return this.corsHandler.preflight(origin);
|
|
215
|
+
}
|
|
216
|
+
return NOT_FOUND_RESPONSE;
|
|
217
|
+
}
|
|
218
|
+
buildResponse(result) {
|
|
219
|
+
return result instanceof Response ? result : typeof result == "string" ? new Response(result, TEXT_OPTS) : result === void 0 ? new Response(null, { status: 204 }) : Response.json(result);
|
|
220
|
+
}
|
|
221
|
+
normalizePath(path) {
|
|
222
|
+
return path.startsWith("/") || (path = "/" + path), path !== "/" && path.endsWith("/") && (path = path.slice(0, -1)), path.replace(/\/+/g, "/");
|
|
223
|
+
}
|
|
224
|
+
hasParams(path) {
|
|
225
|
+
return path.includes(":") || path.includes("*");
|
|
226
|
+
}
|
|
227
|
+
stop() {
|
|
228
|
+
this.server?.stop?.();
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Error handler for Bun.serve.
|
|
232
|
+
* Converts exceptions to proper HTTP responses.
|
|
233
|
+
*/
|
|
234
|
+
handleError(error) {
|
|
235
|
+
let response;
|
|
236
|
+
return error instanceof HttpException || error instanceof ValidationException ? response = error.toResponse() : (console.error("Unhandled error:", error), response = INTERNAL_ERROR_RESPONSE), this.hasCors && this.corsHandler ? this.corsHandler.apply(response, "*") : response;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Execute lifecycle hooks for a specific event type.
|
|
240
|
+
*/
|
|
241
|
+
executeLifecycleHooks(type) {
|
|
242
|
+
const handlers = getEventHandlers(type);
|
|
243
|
+
for (const handler of handlers)
|
|
244
|
+
try {
|
|
245
|
+
const instance = this.container.has(handler.target) ? this.container.get(handler.target) : null;
|
|
246
|
+
if (instance && typeof instance[handler.methodName] == "function") {
|
|
247
|
+
const result = instance[handler.methodName]();
|
|
248
|
+
result instanceof Promise && result.catch(
|
|
249
|
+
(err) => console.error(`Error in ${type} hook ${handler.methodName}:`, err)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
} catch (err) {
|
|
253
|
+
console.error(`Error in ${type} hook ${handler.methodName}:`, err);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Register SIGTERM/SIGINT handlers for graceful shutdown.
|
|
258
|
+
*/
|
|
259
|
+
registerShutdownHandlers() {
|
|
260
|
+
const shutdown = () => {
|
|
261
|
+
this.executeLifecycleHooks(EventType.SHUTDOWN), this.stop(), process.exit(0);
|
|
262
|
+
};
|
|
263
|
+
process.on("SIGTERM", shutdown), process.on("SIGINT", shutdown);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
export {
|
|
267
|
+
Carno
|
|
268
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
8
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod), __decorateClass = (decorators, target, key, kind) => {
|
|
15
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
16
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
17
|
+
return kind && result && __defProp(target, key, result), result;
|
|
18
|
+
};
|
|
19
|
+
var DefaultRoutes_exports = {};
|
|
20
|
+
__export(DefaultRoutes_exports, {
|
|
21
|
+
DEFAULT_STATIC_ROUTES: () => DEFAULT_STATIC_ROUTES,
|
|
22
|
+
DefaultRoutes: () => DefaultRoutes
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(DefaultRoutes_exports);
|
|
25
|
+
var import_Controller = require('./decorators/Controller.js'), import_methods = require('./decorators/methods.js');
|
|
26
|
+
let DefaultRoutes = class {
|
|
27
|
+
favicon() {
|
|
28
|
+
return new Response(null, { status: 204 });
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
__decorateClass([
|
|
32
|
+
(0, import_methods.Get)("/favicon.ico")
|
|
33
|
+
], DefaultRoutes.prototype, "favicon", 1), DefaultRoutes = __decorateClass([
|
|
34
|
+
(0, import_Controller.Controller)()
|
|
35
|
+
], DefaultRoutes);
|
|
36
|
+
const DEFAULT_STATIC_ROUTES = {
|
|
37
|
+
"/health": new Response('{"status":"ok"}', {
|
|
38
|
+
status: 200,
|
|
39
|
+
headers: { "Content-Type": "application/json" }
|
|
40
|
+
}),
|
|
41
|
+
"/ready": new Response('{"ready":true}', {
|
|
42
|
+
status: 200,
|
|
43
|
+
headers: { "Content-Type": "application/json" }
|
|
44
|
+
}),
|
|
45
|
+
"/favicon.ico": new Response(null, { status: 204 })
|
|
46
|
+
};
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
DEFAULT_STATIC_ROUTES,
|
|
50
|
+
DefaultRoutes
|
|
51
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
5
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
6
|
+
return kind && result && __defProp(target, key, result), result;
|
|
7
|
+
};
|
|
8
|
+
import { Controller } from "./decorators/Controller.mjs";
|
|
9
|
+
import { Get } from "./decorators/methods.mjs";
|
|
10
|
+
let DefaultRoutes = class {
|
|
11
|
+
favicon() {
|
|
12
|
+
return new Response(null, { status: 204 });
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
__decorateClass([
|
|
16
|
+
Get("/favicon.ico")
|
|
17
|
+
], DefaultRoutes.prototype, "favicon", 1), DefaultRoutes = __decorateClass([
|
|
18
|
+
Controller()
|
|
19
|
+
], DefaultRoutes);
|
|
20
|
+
const DEFAULT_STATIC_ROUTES = {
|
|
21
|
+
"/health": new Response('{"status":"ok"}', {
|
|
22
|
+
status: 200,
|
|
23
|
+
headers: { "Content-Type": "application/json" }
|
|
24
|
+
}),
|
|
25
|
+
"/ready": new Response('{"ready":true}', {
|
|
26
|
+
status: 200,
|
|
27
|
+
headers: { "Content-Type": "application/json" }
|
|
28
|
+
}),
|
|
29
|
+
"/favicon.ico": new Response(null, { status: 204 })
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
DEFAULT_STATIC_ROUTES,
|
|
33
|
+
DefaultRoutes
|
|
34
|
+
};
|