@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
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.compileRouteHandler = compileRouteHandler;
|
|
4
|
-
exports.compileValidatedHandler = compileValidatedHandler;
|
|
5
|
-
const TEXT_HEADERS = Object.freeze({
|
|
6
|
-
'Content-Type': 'text/html'
|
|
7
|
-
});
|
|
8
|
-
const JSON_HEADERS = Object.freeze({
|
|
9
|
-
'Content-Type': 'application/json'
|
|
10
|
-
});
|
|
11
|
-
function isBodyInitResult(result) {
|
|
12
|
-
if (!result) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
if (result instanceof ReadableStream) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
if (result instanceof Blob || result instanceof ArrayBuffer) {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
if (ArrayBuffer.isView(result)) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
return result instanceof FormData || result instanceof URLSearchParams;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Cria handler inline com response building integrado.
|
|
28
|
-
* Usado quando não há parâmetros.
|
|
29
|
-
*/
|
|
30
|
-
function createInlineResponseHandler(handler, isAsync) {
|
|
31
|
-
if (isAsync) {
|
|
32
|
-
return async (c) => {
|
|
33
|
-
const r = await handler();
|
|
34
|
-
const s = c.getResponseStatus() || 200;
|
|
35
|
-
if (typeof r === 'string')
|
|
36
|
-
return new Response(r, { status: s, headers: TEXT_HEADERS });
|
|
37
|
-
if (r instanceof Response)
|
|
38
|
-
return r;
|
|
39
|
-
if (r == null)
|
|
40
|
-
return new Response('', { status: s, headers: TEXT_HEADERS });
|
|
41
|
-
if (typeof r === 'number' || typeof r === 'boolean') {
|
|
42
|
-
return new Response(String(r), { status: s, headers: TEXT_HEADERS });
|
|
43
|
-
}
|
|
44
|
-
if (isBodyInitResult(r)) {
|
|
45
|
-
return new Response(r, { status: s });
|
|
46
|
-
}
|
|
47
|
-
return new Response(JSON.stringify(r), { status: s, headers: JSON_HEADERS });
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
return (c) => {
|
|
51
|
-
const r = handler();
|
|
52
|
-
const s = c.getResponseStatus() || 200;
|
|
53
|
-
if (typeof r === 'string')
|
|
54
|
-
return new Response(r, { status: s, headers: TEXT_HEADERS });
|
|
55
|
-
if (r instanceof Response)
|
|
56
|
-
return r;
|
|
57
|
-
if (r == null)
|
|
58
|
-
return new Response('', { status: s, headers: TEXT_HEADERS });
|
|
59
|
-
if (typeof r === 'number' || typeof r === 'boolean') {
|
|
60
|
-
return new Response(String(r), { status: s, headers: TEXT_HEADERS });
|
|
61
|
-
}
|
|
62
|
-
if (isBodyInitResult(r)) {
|
|
63
|
-
return new Response(r, { status: s });
|
|
64
|
-
}
|
|
65
|
-
return new Response(JSON.stringify(r), { status: s, headers: JSON_HEADERS });
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
function escapeKey(key) {
|
|
69
|
-
return key.replace(/['\"\\]/g, '\\$&');
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Gera expressão de acesso a parâmetro inline.
|
|
73
|
-
* Otimizado para evitar chamadas de função quando possível.
|
|
74
|
-
*/
|
|
75
|
-
function buildArgExpression(param) {
|
|
76
|
-
const key = param.key ? escapeKey(param.key) : undefined;
|
|
77
|
-
switch (param.type) {
|
|
78
|
-
case 'param':
|
|
79
|
-
return key ? `c.param['${key}']` : 'c.param';
|
|
80
|
-
case 'query':
|
|
81
|
-
return key ? `c.query['${key}']` : 'c.query';
|
|
82
|
-
case 'body':
|
|
83
|
-
return key ? `c.body['${key}']` : 'c.body';
|
|
84
|
-
case 'headers':
|
|
85
|
-
return key ? `c.headers.get('${key}')` : 'c.headers';
|
|
86
|
-
case 'req':
|
|
87
|
-
return 'c.req';
|
|
88
|
-
case 'locals':
|
|
89
|
-
return 'c.locals';
|
|
90
|
-
default:
|
|
91
|
-
return 'undefined';
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Compila route handler em função otimizada.
|
|
96
|
-
*
|
|
97
|
-
* Estratégias de otimização:
|
|
98
|
-
* - Inline de acesso a parâmetros
|
|
99
|
-
* - Bind do handler no compile time
|
|
100
|
-
* - Código gerado monomórfico
|
|
101
|
-
* - Sem overhead de resolvers array
|
|
102
|
-
*/
|
|
103
|
-
/**
|
|
104
|
-
* Compila route handler em função otimizada que retorna Response inline.
|
|
105
|
-
*
|
|
106
|
-
* Estratégias de otimização:
|
|
107
|
-
* - Inline de acesso a parâmetros
|
|
108
|
-
* - Inline de response building (elimina executeFastPath)
|
|
109
|
-
* - Bind do handler no compile time
|
|
110
|
-
* - Código gerado monomórfico
|
|
111
|
-
* - Headers pré-frozen para otimização V8
|
|
112
|
-
*/
|
|
113
|
-
function compileRouteHandler(instance, methodName, paramInfos) {
|
|
114
|
-
const handler = instance[methodName].bind(instance);
|
|
115
|
-
if (paramInfos.length === 0) {
|
|
116
|
-
return createInlineResponseHandler(handler, false);
|
|
117
|
-
}
|
|
118
|
-
const hasDIParam = paramInfos.some((p) => p.type === 'di');
|
|
119
|
-
if (hasDIParam) {
|
|
120
|
-
return createFallbackHandler(handler, paramInfos);
|
|
121
|
-
}
|
|
122
|
-
const hasBodyParam = paramInfos.some((p) => p.type === 'body');
|
|
123
|
-
const argExpressions = paramInfos.map(buildArgExpression);
|
|
124
|
-
const argsCode = argExpressions.join(',');
|
|
125
|
-
if (hasBodyParam) {
|
|
126
|
-
const code = `return async function(c){
|
|
127
|
-
await c.getBody();
|
|
128
|
-
const r=await h(${argsCode});
|
|
129
|
-
const s=c.getResponseStatus()||200;
|
|
130
|
-
if(typeof r==='string')return new Response(r,{status:s,headers:TH});
|
|
131
|
-
if(r instanceof Response)return r;
|
|
132
|
-
if(r==null)return new Response('',{status:s,headers:TH});
|
|
133
|
-
if(typeof r==='number'||typeof r==='boolean')return new Response(String(r),{status:s,headers:TH});
|
|
134
|
-
if(isBI(r))return new Response(r,{status:s});
|
|
135
|
-
return new Response(JSON.stringify(r),{status:s,headers:JH});
|
|
136
|
-
}`;
|
|
137
|
-
return new Function('h', 'TH', 'JH', 'isBI', code)(handler, TEXT_HEADERS, JSON_HEADERS, isBodyInitResult);
|
|
138
|
-
}
|
|
139
|
-
const code = `return function(c){
|
|
140
|
-
const r=h(${argsCode});
|
|
141
|
-
const s=c.getResponseStatus()||200;
|
|
142
|
-
if(typeof r==='string')return new Response(r,{status:s,headers:TH});
|
|
143
|
-
if(r instanceof Response)return r;
|
|
144
|
-
if(r==null)return new Response('',{status:s,headers:TH});
|
|
145
|
-
if(typeof r==='number'||typeof r==='boolean')return new Response(String(r),{status:s,headers:TH});
|
|
146
|
-
if(isBI(r))return new Response(r,{status:s});
|
|
147
|
-
return new Response(JSON.stringify(r),{status:s,headers:JH});
|
|
148
|
-
}`;
|
|
149
|
-
return new Function('h', 'TH', 'JH', 'isBI', code)(handler, TEXT_HEADERS, JSON_HEADERS, isBodyInitResult);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Handler fallback para casos com DI.
|
|
153
|
-
*/
|
|
154
|
-
function createFallbackHandler(handler, paramInfos) {
|
|
155
|
-
return (ctx) => {
|
|
156
|
-
const args = resolveArgs(paramInfos, ctx);
|
|
157
|
-
return handler(...args);
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Resolve argumentos para fallback handler.
|
|
162
|
-
*/
|
|
163
|
-
function resolveArgs(paramInfos, ctx) {
|
|
164
|
-
const args = new Array(paramInfos.length);
|
|
165
|
-
let i = 0;
|
|
166
|
-
for (const param of paramInfos) {
|
|
167
|
-
args[i++] = resolveArg(param, ctx);
|
|
168
|
-
}
|
|
169
|
-
return args;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Resolve um argumento individual.
|
|
173
|
-
*/
|
|
174
|
-
function resolveArg(param, ctx) {
|
|
175
|
-
switch (param.type) {
|
|
176
|
-
case 'param':
|
|
177
|
-
return param.key ? ctx.param[param.key] : ctx.param;
|
|
178
|
-
case 'query':
|
|
179
|
-
return param.key ? ctx.query[param.key] : ctx.query;
|
|
180
|
-
case 'body':
|
|
181
|
-
return param.key ? ctx.body[param.key] : ctx.body;
|
|
182
|
-
case 'headers':
|
|
183
|
-
return param.key ? ctx.headers.get(param.key) : ctx.headers;
|
|
184
|
-
case 'req':
|
|
185
|
-
return ctx.req;
|
|
186
|
-
case 'locals':
|
|
187
|
-
return ctx.locals;
|
|
188
|
-
default:
|
|
189
|
-
return undefined;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Compila handler com validação inline.
|
|
194
|
-
*/
|
|
195
|
-
function compileValidatedHandler(instance, methodName, paramInfos, validatorAdapter) {
|
|
196
|
-
const handler = instance[methodName].bind(instance);
|
|
197
|
-
const hasBodyParam = paramInfos.some((p) => p.type === 'body');
|
|
198
|
-
if (paramInfos.length === 0) {
|
|
199
|
-
return createInlineResponseHandler(handler, false);
|
|
200
|
-
}
|
|
201
|
-
const { argAssignments, argList, tokenParams, tokenValues, } = buildValidatedArgs(paramInfos);
|
|
202
|
-
if (hasBodyParam) {
|
|
203
|
-
const code = `return async function(c){\nawait c.getBody();\n${argAssignments}\nconst r=await h(${argList});\nconst s=c.getResponseStatus()||200;\nif(typeof r==='string')return new Response(r,{status:s,headers:TH});\nif(r instanceof Response)return r;\nif(r==null)return new Response('',{status:s,headers:TH});\nif(typeof r==='number'||typeof r==='boolean')return new Response(String(r),{status:s,headers:TH});\nif(isBI(r))return new Response(r,{status:s});\nreturn new Response(JSON.stringify(r),{status:s,headers:JH});\n}`;
|
|
204
|
-
return new Function('h', 'va', ...tokenParams, 'TH', 'JH', 'isBI', code)(handler, validatorAdapter, ...tokenValues, TEXT_HEADERS, JSON_HEADERS, isBodyInitResult);
|
|
205
|
-
}
|
|
206
|
-
const code = `return function(c){\n${argAssignments}\nconst r=h(${argList});\nconst s=c.getResponseStatus()||200;\nif(typeof r==='string')return new Response(r,{status:s,headers:TH});\nif(r instanceof Response)return r;\nif(r==null)return new Response('',{status:s,headers:TH});\nif(typeof r==='number'||typeof r==='boolean')return new Response(String(r),{status:s,headers:TH});\nif(isBI(r))return new Response(r,{status:s});\nreturn new Response(JSON.stringify(r),{status:s,headers:JH});\n}`;
|
|
207
|
-
return new Function('h', 'va', ...tokenParams, 'TH', 'JH', 'isBI', code)(handler, validatorAdapter, ...tokenValues, TEXT_HEADERS, JSON_HEADERS, isBodyInitResult);
|
|
208
|
-
}
|
|
209
|
-
function buildValidatedArgs(paramInfos) {
|
|
210
|
-
const assignments = [];
|
|
211
|
-
const args = [];
|
|
212
|
-
const tokenParams = [];
|
|
213
|
-
const tokenValues = [];
|
|
214
|
-
let index = 0;
|
|
215
|
-
for (const param of paramInfos) {
|
|
216
|
-
const argName = `a${index}`;
|
|
217
|
-
const argExpr = buildArgExpression(param);
|
|
218
|
-
const tokenName = getTokenParamName(param, index, tokenParams, tokenValues);
|
|
219
|
-
const valueExpr = buildValidatedExpression(argExpr, tokenName);
|
|
220
|
-
assignments.push(`const ${argName}=${valueExpr};`);
|
|
221
|
-
args.push(argName);
|
|
222
|
-
index += 1;
|
|
223
|
-
}
|
|
224
|
-
return {
|
|
225
|
-
argAssignments: assignments.join('\n'),
|
|
226
|
-
argList: args.join(','),
|
|
227
|
-
tokenParams,
|
|
228
|
-
tokenValues,
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
function buildValidatedExpression(argExpr, tokenName) {
|
|
232
|
-
if (!tokenName) {
|
|
233
|
-
return argExpr;
|
|
234
|
-
}
|
|
235
|
-
return `va.validateAndTransform(${tokenName}, ${argExpr})`;
|
|
236
|
-
}
|
|
237
|
-
function getTokenParamName(param, index, tokenParams, tokenValues) {
|
|
238
|
-
if (!param.needsValidation || !param.token) {
|
|
239
|
-
return null;
|
|
240
|
-
}
|
|
241
|
-
const tokenName = `t${index}`;
|
|
242
|
-
tokenParams.push(tokenName);
|
|
243
|
-
tokenValues.push(param.token);
|
|
244
|
-
return tokenName;
|
|
245
|
-
}
|
package/dist/route/Matcher.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { TokenRouteWithProvider, TokenRouteWithProviderMap } from "../container";
|
|
2
|
-
import { Context } from "../domain";
|
|
3
|
-
interface ParsedUrl {
|
|
4
|
-
pathname: string;
|
|
5
|
-
}
|
|
6
|
-
declare class Matcher {
|
|
7
|
-
match(request: Request, routes: TokenRouteWithProviderMap, context: Context): TokenRouteWithProvider;
|
|
8
|
-
identifyRoute(route: TokenRouteWithProvider, url: ParsedUrl, context: Context): boolean;
|
|
9
|
-
}
|
|
10
|
-
export declare const RouteResolver: Matcher;
|
|
11
|
-
export {};
|
package/dist/route/Matcher.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RouteResolver = void 0;
|
|
4
|
-
function parseUrl(request) {
|
|
5
|
-
const url = request.url;
|
|
6
|
-
const startIndex = url.indexOf('/', 12);
|
|
7
|
-
const queryIndex = url.indexOf('?', startIndex);
|
|
8
|
-
if (queryIndex === -1) {
|
|
9
|
-
return { pathname: startIndex === -1 ? '/' : url.slice(startIndex) };
|
|
10
|
-
}
|
|
11
|
-
return { pathname: url.slice(startIndex, queryIndex) };
|
|
12
|
-
}
|
|
13
|
-
class Matcher {
|
|
14
|
-
match(request, routes, context) {
|
|
15
|
-
const method = request.method.toLowerCase();
|
|
16
|
-
if (!routes) {
|
|
17
|
-
throw new Error(`Method not allowed for ${request.url}`);
|
|
18
|
-
}
|
|
19
|
-
const routeMethod = routes.get(method);
|
|
20
|
-
const url = parseUrl(request);
|
|
21
|
-
const route = routeMethod?.find((route) => this.identifyRoute(route, url, context));
|
|
22
|
-
if (!route) {
|
|
23
|
-
throw new Error('Method not allowed');
|
|
24
|
-
}
|
|
25
|
-
return route;
|
|
26
|
-
}
|
|
27
|
-
identifyRoute(route, url, context) {
|
|
28
|
-
const urlPath = url.pathname.split('/');
|
|
29
|
-
const routePathSegments = route.path.split('/');
|
|
30
|
-
if (urlPath.length !== routePathSegments.length) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
return routePathSegments.every((path, index) => {
|
|
34
|
-
if (path === '*') {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
if (path.startsWith(':')) {
|
|
38
|
-
context.setParam({
|
|
39
|
-
...context.param,
|
|
40
|
-
[path.replace(':', '')]: urlPath[index]
|
|
41
|
-
});
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
return path === urlPath[index];
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.RouteResolver = new Matcher();
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Context } from '../domain/Context';
|
|
2
|
-
export type ParamDecoratorType = 'body' | 'query' | 'param' | 'headers' | 'req' | 'locals';
|
|
3
|
-
export interface ParamDecoratorMeta {
|
|
4
|
-
fun: (context: Context, data?: any) => any;
|
|
5
|
-
param?: any;
|
|
6
|
-
type?: ParamDecoratorType;
|
|
7
|
-
}
|
|
8
|
-
export interface ParamInfo {
|
|
9
|
-
type: ParamDecoratorType | 'di';
|
|
10
|
-
key?: string;
|
|
11
|
-
needsValidation: boolean;
|
|
12
|
-
token?: any;
|
|
13
|
-
}
|
|
14
|
-
export declare function analyzeParamDecorator(decoratorMeta: ParamDecoratorMeta | undefined, token: any): ParamInfo;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.analyzeParamDecorator = analyzeParamDecorator;
|
|
4
|
-
const ValidationCache_1 = require("../utils/ValidationCache");
|
|
5
|
-
function analyzeParamDecorator(decoratorMeta, token) {
|
|
6
|
-
if (!decoratorMeta) {
|
|
7
|
-
return { type: 'di', needsValidation: false, token };
|
|
8
|
-
}
|
|
9
|
-
const paramType = resolveParamType(decoratorMeta);
|
|
10
|
-
const key = decoratorMeta.param;
|
|
11
|
-
const needsValidation = typeof token === 'function' && (0, ValidationCache_1.isValidatable)(token);
|
|
12
|
-
if (paramType) {
|
|
13
|
-
return {
|
|
14
|
-
type: paramType,
|
|
15
|
-
key,
|
|
16
|
-
needsValidation,
|
|
17
|
-
token,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
return { type: 'di', needsValidation: false, token };
|
|
21
|
-
}
|
|
22
|
-
function resolveParamType(decoratorMeta) {
|
|
23
|
-
if (decoratorMeta.type) {
|
|
24
|
-
return decoratorMeta.type;
|
|
25
|
-
}
|
|
26
|
-
return inferTypeFromSource(decoratorMeta.fun);
|
|
27
|
-
}
|
|
28
|
-
function inferTypeFromSource(resolver) {
|
|
29
|
-
const funcStr = resolver.toString();
|
|
30
|
-
if (funcStr.includes('context.body')) {
|
|
31
|
-
return 'body';
|
|
32
|
-
}
|
|
33
|
-
if (funcStr.includes('context.query')) {
|
|
34
|
-
return 'query';
|
|
35
|
-
}
|
|
36
|
-
if (funcStr.includes('context.param')) {
|
|
37
|
-
return 'param';
|
|
38
|
-
}
|
|
39
|
-
if (funcStr.includes('context.headers')) {
|
|
40
|
-
return 'headers';
|
|
41
|
-
}
|
|
42
|
-
if (funcStr.includes('context.req')) {
|
|
43
|
-
return 'req';
|
|
44
|
-
}
|
|
45
|
-
if (funcStr.includes('context.locals')) {
|
|
46
|
-
return 'locals';
|
|
47
|
-
}
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { TokenRouteWithProvider } from '../container/ContainerConfiguration';
|
|
2
|
-
import type { ValidatorAdapter } from '../validation/ValidatorAdapter';
|
|
3
|
-
import type { Container } from '../container/container';
|
|
4
|
-
import { ProviderScope } from '../domain/provider-scope';
|
|
5
|
-
import { type CompiledRoute } from './CompiledRoute';
|
|
6
|
-
export interface RouteCompilerOptions {
|
|
7
|
-
container: Container;
|
|
8
|
-
controllerScopes: Map<any, ProviderScope>;
|
|
9
|
-
validatorAdapter: ValidatorAdapter;
|
|
10
|
-
hasOnRequestHook: boolean;
|
|
11
|
-
hasOnResponseHook: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare class RouteCompiler {
|
|
14
|
-
private container;
|
|
15
|
-
private controllerScopes;
|
|
16
|
-
private validatorAdapter;
|
|
17
|
-
private hasOnRequestHook;
|
|
18
|
-
private hasOnResponseHook;
|
|
19
|
-
constructor(options: RouteCompilerOptions);
|
|
20
|
-
compile(route: TokenRouteWithProvider): CompiledRoute | null;
|
|
21
|
-
private classifyRoute;
|
|
22
|
-
private analyzeMethodParams;
|
|
23
|
-
private createSimpleRoute;
|
|
24
|
-
private createStandardRouteWithInstance;
|
|
25
|
-
private createStandardRoute;
|
|
26
|
-
private createComplexRoute;
|
|
27
|
-
private createFallbackRoute;
|
|
28
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RouteCompiler = void 0;
|
|
4
|
-
const provider_scope_1 = require("../domain/provider-scope");
|
|
5
|
-
const Metadata_1 = require("../domain/Metadata");
|
|
6
|
-
const getMethodArgTypes_1 = require("../utils/getMethodArgTypes");
|
|
7
|
-
const CompiledRoute_1 = require("./CompiledRoute");
|
|
8
|
-
const ParamResolverFactory_1 = require("./ParamResolverFactory");
|
|
9
|
-
const JITCompiler_1 = require("./JITCompiler");
|
|
10
|
-
class RouteCompiler {
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.container = options.container;
|
|
13
|
-
this.controllerScopes = options.controllerScopes;
|
|
14
|
-
this.validatorAdapter = options.validatorAdapter;
|
|
15
|
-
this.hasOnRequestHook = options.hasOnRequestHook;
|
|
16
|
-
this.hasOnResponseHook = options.hasOnResponseHook;
|
|
17
|
-
}
|
|
18
|
-
compile(route) {
|
|
19
|
-
if (!route || !route.provider) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
const provider = this.container.get(route.provider);
|
|
23
|
-
if (!provider) {
|
|
24
|
-
return this.createFallbackRoute(route);
|
|
25
|
-
}
|
|
26
|
-
const scope = this.controllerScopes.get(provider.token);
|
|
27
|
-
const isSingleton = scope === provider_scope_1.ProviderScope.SINGLETON;
|
|
28
|
-
const instance = isSingleton ? provider.instance : null;
|
|
29
|
-
if (!instance) {
|
|
30
|
-
return this.createStandardRoute(route, provider);
|
|
31
|
-
}
|
|
32
|
-
const paramInfos = this.analyzeMethodParams(instance, route.methodName);
|
|
33
|
-
const routeType = this.classifyRoute(route, paramInfos, isSingleton);
|
|
34
|
-
if (routeType === CompiledRoute_1.RouteType.SIMPLE) {
|
|
35
|
-
return this.createSimpleRoute(route, instance, paramInfos);
|
|
36
|
-
}
|
|
37
|
-
if (routeType === CompiledRoute_1.RouteType.STANDARD) {
|
|
38
|
-
return this.createStandardRouteWithInstance(route, instance, paramInfos);
|
|
39
|
-
}
|
|
40
|
-
return this.createComplexRoute(route, paramInfos);
|
|
41
|
-
}
|
|
42
|
-
classifyRoute(route, paramInfos, isSingleton) {
|
|
43
|
-
if (!isSingleton) {
|
|
44
|
-
return CompiledRoute_1.RouteType.COMPLEX;
|
|
45
|
-
}
|
|
46
|
-
const hasMiddlewares = route.middlewares.length > 0;
|
|
47
|
-
const hasDIParams = paramInfos.some((p) => p.type === 'di');
|
|
48
|
-
const hasHooks = this.hasOnRequestHook || this.hasOnResponseHook;
|
|
49
|
-
if (hasMiddlewares || hasDIParams || hasHooks) {
|
|
50
|
-
return CompiledRoute_1.RouteType.STANDARD;
|
|
51
|
-
}
|
|
52
|
-
return CompiledRoute_1.RouteType.SIMPLE;
|
|
53
|
-
}
|
|
54
|
-
analyzeMethodParams(instance, methodName) {
|
|
55
|
-
const argTypes = (0, getMethodArgTypes_1.getMethodArgTypes)(instance, methodName);
|
|
56
|
-
const decoratorMetas = Metadata_1.Metadata.getParamDecoratorFunc(instance, methodName);
|
|
57
|
-
return argTypes.map((token, index) => {
|
|
58
|
-
const meta = decoratorMetas?.[index];
|
|
59
|
-
return (0, ParamResolverFactory_1.analyzeParamDecorator)(meta, token);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
createSimpleRoute(route, instance, paramInfos) {
|
|
63
|
-
const hasValidation = paramInfos.some((p) => p.needsValidation);
|
|
64
|
-
const validationIndices = paramInfos
|
|
65
|
-
.map((p, i) => (p.needsValidation ? i : -1))
|
|
66
|
-
.filter((i) => i >= 0);
|
|
67
|
-
const hasBodyParam = paramInfos.some((p) => p.type === 'body');
|
|
68
|
-
let boundHandler;
|
|
69
|
-
if (hasValidation) {
|
|
70
|
-
boundHandler = (0, JITCompiler_1.compileValidatedHandler)(instance, route.methodName, paramInfos, this.validatorAdapter);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
boundHandler = (0, JITCompiler_1.compileRouteHandler)(instance, route.methodName, paramInfos);
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
routeType: CompiledRoute_1.RouteType.SIMPLE,
|
|
77
|
-
controllerInstance: instance,
|
|
78
|
-
boundHandler,
|
|
79
|
-
paramResolvers: [],
|
|
80
|
-
needsLocalsContainer: false,
|
|
81
|
-
hasMiddlewares: false,
|
|
82
|
-
hasValidation,
|
|
83
|
-
validationIndices,
|
|
84
|
-
isAsync: hasBodyParam,
|
|
85
|
-
original: route,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
createStandardRouteWithInstance(route, instance, paramInfos) {
|
|
89
|
-
const hasValidation = paramInfos.some((p) => p.needsValidation);
|
|
90
|
-
const validationIndices = paramInfos
|
|
91
|
-
.map((p, i) => (p.needsValidation ? i : -1))
|
|
92
|
-
.filter((i) => i >= 0);
|
|
93
|
-
const hasDIParams = paramInfos.some((p) => p.type === 'di');
|
|
94
|
-
const hasBodyParam = paramInfos.some((p) => p.type === 'body');
|
|
95
|
-
const hasMiddlewares = route.middlewares.length > 0;
|
|
96
|
-
const needsLocalsContainer = hasDIParams || hasMiddlewares;
|
|
97
|
-
return {
|
|
98
|
-
routeType: CompiledRoute_1.RouteType.STANDARD,
|
|
99
|
-
controllerInstance: instance,
|
|
100
|
-
boundHandler: null,
|
|
101
|
-
paramResolvers: [],
|
|
102
|
-
needsLocalsContainer,
|
|
103
|
-
hasMiddlewares,
|
|
104
|
-
hasValidation,
|
|
105
|
-
validationIndices,
|
|
106
|
-
isAsync: hasBodyParam || hasDIParams,
|
|
107
|
-
original: route,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
createStandardRoute(route, provider) {
|
|
111
|
-
return {
|
|
112
|
-
routeType: CompiledRoute_1.RouteType.STANDARD,
|
|
113
|
-
controllerInstance: null,
|
|
114
|
-
boundHandler: null,
|
|
115
|
-
paramResolvers: [],
|
|
116
|
-
needsLocalsContainer: true,
|
|
117
|
-
hasMiddlewares: route.middlewares.length > 0,
|
|
118
|
-
hasValidation: false,
|
|
119
|
-
validationIndices: [],
|
|
120
|
-
isAsync: true,
|
|
121
|
-
original: route,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
createComplexRoute(route, paramInfos) {
|
|
125
|
-
const hasValidation = paramInfos.some((p) => p.needsValidation);
|
|
126
|
-
const validationIndices = paramInfos
|
|
127
|
-
.map((p, i) => (p.needsValidation ? i : -1))
|
|
128
|
-
.filter((i) => i >= 0);
|
|
129
|
-
return {
|
|
130
|
-
routeType: CompiledRoute_1.RouteType.COMPLEX,
|
|
131
|
-
controllerInstance: null,
|
|
132
|
-
boundHandler: null,
|
|
133
|
-
paramResolvers: [],
|
|
134
|
-
needsLocalsContainer: true,
|
|
135
|
-
hasMiddlewares: route.middlewares.length > 0,
|
|
136
|
-
hasValidation,
|
|
137
|
-
validationIndices,
|
|
138
|
-
isAsync: true,
|
|
139
|
-
original: route,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
createFallbackRoute(route) {
|
|
143
|
-
return {
|
|
144
|
-
routeType: CompiledRoute_1.RouteType.COMPLEX,
|
|
145
|
-
controllerInstance: null,
|
|
146
|
-
boundHandler: null,
|
|
147
|
-
paramResolvers: [],
|
|
148
|
-
needsLocalsContainer: true,
|
|
149
|
-
hasMiddlewares: route.middlewares.length > 0,
|
|
150
|
-
hasValidation: false,
|
|
151
|
-
validationIndices: [],
|
|
152
|
-
isAsync: true,
|
|
153
|
-
original: route,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.RouteCompiler = RouteCompiler;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { InjectorService, TokenRouteWithProvider } from "../container";
|
|
2
|
-
import { Context, LocalsContainer } from "../domain";
|
|
3
|
-
import type { CompiledRoute } from "./CompiledRoute";
|
|
4
|
-
declare class Router {
|
|
5
|
-
private readonly textHeaders;
|
|
6
|
-
executeRoute(routeStore: CompiledRoute | TokenRouteWithProvider, injector: InjectorService, context: Context, locals: LocalsContainer): Promise<Response>;
|
|
7
|
-
mountResponse(result: unknown, context: Context): Response;
|
|
8
|
-
private isNativeResponse;
|
|
9
|
-
private isBodyInit;
|
|
10
|
-
}
|
|
11
|
-
export declare const RouteExecutor: Router;
|
|
12
|
-
export {};
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RouteExecutor = void 0;
|
|
4
|
-
const events_1 = require("../events");
|
|
5
|
-
class Router {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.textHeaders = { "Content-Type": "text/html" };
|
|
8
|
-
}
|
|
9
|
-
async executeRoute(routeStore, injector, context, locals) {
|
|
10
|
-
const isCompiled = routeStore.routeType !== undefined;
|
|
11
|
-
const tokenRoute = isCompiled
|
|
12
|
-
? routeStore.original
|
|
13
|
-
: routeStore;
|
|
14
|
-
const controllerInstance = isCompiled
|
|
15
|
-
? routeStore.controllerInstance
|
|
16
|
-
: null;
|
|
17
|
-
const controller = controllerInstance
|
|
18
|
-
?? injector.resolveControllerInstance(tokenRoute.provider, locals);
|
|
19
|
-
if (!controller[tokenRoute.methodName]) {
|
|
20
|
-
throw new Error("Controller not found");
|
|
21
|
-
}
|
|
22
|
-
const result = await injector.invokeRoute(tokenRoute, context, locals, controller);
|
|
23
|
-
if (injector.hasOnResponseHook()) {
|
|
24
|
-
await injector.callHook(events_1.EventType.OnResponse, { context, result });
|
|
25
|
-
}
|
|
26
|
-
const status = context.getResponseStatus() || 200;
|
|
27
|
-
if (result instanceof Response) {
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
if (result === null || result === undefined) {
|
|
31
|
-
return new Response("", { status, headers: this.textHeaders });
|
|
32
|
-
}
|
|
33
|
-
const resultType = typeof result;
|
|
34
|
-
if (resultType === "string") {
|
|
35
|
-
return new Response(result, { status, headers: this.textHeaders });
|
|
36
|
-
}
|
|
37
|
-
if (resultType === "number" || resultType === "boolean") {
|
|
38
|
-
return new Response(String(result), { status, headers: this.textHeaders });
|
|
39
|
-
}
|
|
40
|
-
if (this.isBodyInit(result)) {
|
|
41
|
-
return new Response(result, { status });
|
|
42
|
-
}
|
|
43
|
-
return Response.json(result, { status });
|
|
44
|
-
}
|
|
45
|
-
mountResponse(result, context) {
|
|
46
|
-
const status = context.getResponseStatus() || 200;
|
|
47
|
-
if (this.isNativeResponse(result)) {
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
if (result === null || result === undefined) {
|
|
51
|
-
return new Response("", { status, headers: this.textHeaders });
|
|
52
|
-
}
|
|
53
|
-
const resultType = typeof result;
|
|
54
|
-
if (resultType === "string") {
|
|
55
|
-
return new Response(result, { status, headers: this.textHeaders });
|
|
56
|
-
}
|
|
57
|
-
if (resultType === "number" || resultType === "boolean") {
|
|
58
|
-
return new Response(String(result), { status, headers: this.textHeaders });
|
|
59
|
-
}
|
|
60
|
-
if (this.isBodyInit(result)) {
|
|
61
|
-
return new Response(result, { status });
|
|
62
|
-
}
|
|
63
|
-
return Response.json(result, { status });
|
|
64
|
-
}
|
|
65
|
-
isNativeResponse(result) {
|
|
66
|
-
return result instanceof Response;
|
|
67
|
-
}
|
|
68
|
-
isBodyInit(result) {
|
|
69
|
-
if (!result) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
if (result instanceof ReadableStream) {
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
if (result instanceof Blob || result instanceof ArrayBuffer) {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
if (ArrayBuffer.isView(result)) {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
return result instanceof FormData || result instanceof URLSearchParams;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
exports.RouteExecutor = new Router();
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export interface FindResult<T> {
|
|
2
|
-
store: T;
|
|
3
|
-
params: Record<string, any>;
|
|
4
|
-
}
|
|
5
|
-
export interface ParamNode<T> {
|
|
6
|
-
paramName: string | null;
|
|
7
|
-
names: Map<T, string>;
|
|
8
|
-
store: T | null;
|
|
9
|
-
inert: Node<T> | null;
|
|
10
|
-
}
|
|
11
|
-
export interface Node<T> {
|
|
12
|
-
part: string;
|
|
13
|
-
store: T | null;
|
|
14
|
-
inert: Map<number, Node<T>> | null;
|
|
15
|
-
params: ParamNode<T> | null;
|
|
16
|
-
wildcardStore: T | null;
|
|
17
|
-
}
|
|
18
|
-
export declare class Memoirist<T> {
|
|
19
|
-
root: Record<string, Node<T>>;
|
|
20
|
-
history: [string, string, T][];
|
|
21
|
-
private routeCache;
|
|
22
|
-
private static regex;
|
|
23
|
-
add(method: string, path: string, store: T): FindResult<T>['store'];
|
|
24
|
-
find(method: string, url: string): FindResult<T> | null;
|
|
25
|
-
updateStore(method: string, path: string, oldStore: T, newStore: T): boolean;
|
|
26
|
-
private invalidateCache;
|
|
27
|
-
private updateHistoryStore;
|
|
28
|
-
private normalizePath;
|
|
29
|
-
private findNode;
|
|
30
|
-
}
|
|
31
|
-
export default Memoirist;
|