@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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
9
|
+
return to;
|
|
10
|
+
};
|
|
11
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
12
|
+
var CacheDriver_exports = {};
|
|
13
|
+
module.exports = __toCommonJS(CacheDriver_exports);
|
|
File without changes
|
|
@@ -0,0 +1,113 @@
|
|
|
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);
|
|
15
|
+
var CacheService_exports = {};
|
|
16
|
+
__export(CacheService_exports, {
|
|
17
|
+
CacheService: () => CacheService
|
|
18
|
+
});
|
|
19
|
+
module.exports = __toCommonJS(CacheService_exports);
|
|
20
|
+
var import_MemoryDriver = require('./MemoryDriver.js');
|
|
21
|
+
class CacheService {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
this.driver = config.driver || new import_MemoryDriver.MemoryDriver(), this.prefix = config.prefix || "", this.defaultTtl = config.defaultTtl;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the full key with prefix.
|
|
27
|
+
*/
|
|
28
|
+
key(key) {
|
|
29
|
+
return this.prefix ? `${this.prefix}:${key}` : key;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get a value from cache.
|
|
33
|
+
*/
|
|
34
|
+
async get(key) {
|
|
35
|
+
return this.driver.get(this.key(key));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Set a value in cache.
|
|
39
|
+
* @param ttl Time to live in seconds
|
|
40
|
+
*/
|
|
41
|
+
async set(key, value, ttl) {
|
|
42
|
+
return this.driver.set(this.key(key), value, ttl ?? this.defaultTtl);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Delete a value from cache.
|
|
46
|
+
*/
|
|
47
|
+
async del(key) {
|
|
48
|
+
return this.driver.del(this.key(key));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Check if key exists.
|
|
52
|
+
*/
|
|
53
|
+
async has(key) {
|
|
54
|
+
return this.driver.has(this.key(key));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Clear all cached values.
|
|
58
|
+
*/
|
|
59
|
+
async clear() {
|
|
60
|
+
return this.driver.clear();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get value from cache or compute and store it.
|
|
64
|
+
* This is the cache-aside pattern - most commonly used method.
|
|
65
|
+
*
|
|
66
|
+
* @param key Cache key
|
|
67
|
+
* @param cb Callback to compute value if not cached
|
|
68
|
+
* @param ttl Time to live in seconds
|
|
69
|
+
*/
|
|
70
|
+
async getOrSet(key, cb, ttl) {
|
|
71
|
+
const cached = await this.get(key);
|
|
72
|
+
if (cached !== null)
|
|
73
|
+
return cached;
|
|
74
|
+
const value = await cb();
|
|
75
|
+
return await this.set(key, value, ttl), value;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get multiple values at once.
|
|
79
|
+
*/
|
|
80
|
+
async getMany(keys) {
|
|
81
|
+
return Promise.all(keys.map((key) => this.get(key)));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Set multiple values at once.
|
|
85
|
+
*/
|
|
86
|
+
async setMany(entries) {
|
|
87
|
+
return Promise.all(
|
|
88
|
+
entries.map((entry) => this.set(entry.key, entry.value, entry.ttl))
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Delete multiple values at once.
|
|
93
|
+
*/
|
|
94
|
+
async delMany(keys) {
|
|
95
|
+
return Promise.all(keys.map((key) => this.del(key)));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Close the cache driver connection.
|
|
99
|
+
*/
|
|
100
|
+
async close() {
|
|
101
|
+
await this.driver.close?.();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get the underlying driver (for advanced use).
|
|
105
|
+
*/
|
|
106
|
+
getDriver() {
|
|
107
|
+
return this.driver;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
111
|
+
0 && (module.exports = {
|
|
112
|
+
CacheService
|
|
113
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { MemoryDriver } from "./MemoryDriver.mjs";
|
|
2
|
+
class CacheService {
|
|
3
|
+
constructor(config = {}) {
|
|
4
|
+
this.driver = config.driver || new MemoryDriver(), this.prefix = config.prefix || "", this.defaultTtl = config.defaultTtl;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Get the full key with prefix.
|
|
8
|
+
*/
|
|
9
|
+
key(key) {
|
|
10
|
+
return this.prefix ? `${this.prefix}:${key}` : key;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get a value from cache.
|
|
14
|
+
*/
|
|
15
|
+
async get(key) {
|
|
16
|
+
return this.driver.get(this.key(key));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Set a value in cache.
|
|
20
|
+
* @param ttl Time to live in seconds
|
|
21
|
+
*/
|
|
22
|
+
async set(key, value, ttl) {
|
|
23
|
+
return this.driver.set(this.key(key), value, ttl ?? this.defaultTtl);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Delete a value from cache.
|
|
27
|
+
*/
|
|
28
|
+
async del(key) {
|
|
29
|
+
return this.driver.del(this.key(key));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if key exists.
|
|
33
|
+
*/
|
|
34
|
+
async has(key) {
|
|
35
|
+
return this.driver.has(this.key(key));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Clear all cached values.
|
|
39
|
+
*/
|
|
40
|
+
async clear() {
|
|
41
|
+
return this.driver.clear();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get value from cache or compute and store it.
|
|
45
|
+
* This is the cache-aside pattern - most commonly used method.
|
|
46
|
+
*
|
|
47
|
+
* @param key Cache key
|
|
48
|
+
* @param cb Callback to compute value if not cached
|
|
49
|
+
* @param ttl Time to live in seconds
|
|
50
|
+
*/
|
|
51
|
+
async getOrSet(key, cb, ttl) {
|
|
52
|
+
const cached = await this.get(key);
|
|
53
|
+
if (cached !== null)
|
|
54
|
+
return cached;
|
|
55
|
+
const value = await cb();
|
|
56
|
+
return await this.set(key, value, ttl), value;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get multiple values at once.
|
|
60
|
+
*/
|
|
61
|
+
async getMany(keys) {
|
|
62
|
+
return Promise.all(keys.map((key) => this.get(key)));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Set multiple values at once.
|
|
66
|
+
*/
|
|
67
|
+
async setMany(entries) {
|
|
68
|
+
return Promise.all(
|
|
69
|
+
entries.map((entry) => this.set(entry.key, entry.value, entry.ttl))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Delete multiple values at once.
|
|
74
|
+
*/
|
|
75
|
+
async delMany(keys) {
|
|
76
|
+
return Promise.all(keys.map((key) => this.del(key)));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Close the cache driver connection.
|
|
80
|
+
*/
|
|
81
|
+
async close() {
|
|
82
|
+
await this.driver.close?.();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the underlying driver (for advanced use).
|
|
86
|
+
*/
|
|
87
|
+
getDriver() {
|
|
88
|
+
return this.driver;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
CacheService
|
|
93
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
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);
|
|
15
|
+
var MemoryDriver_exports = {};
|
|
16
|
+
__export(MemoryDriver_exports, {
|
|
17
|
+
MemoryDriver: () => MemoryDriver
|
|
18
|
+
});
|
|
19
|
+
module.exports = __toCommonJS(MemoryDriver_exports);
|
|
20
|
+
class MemoryDriver {
|
|
21
|
+
constructor(cleanupIntervalMs = 0) {
|
|
22
|
+
this.name = "MemoryDriver";
|
|
23
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
24
|
+
this.cleanupInterval = null;
|
|
25
|
+
cleanupIntervalMs > 0 && (this.cleanupInterval = setInterval(() => this.cleanup(), cleanupIntervalMs));
|
|
26
|
+
}
|
|
27
|
+
async get(key) {
|
|
28
|
+
const entry = this.cache.get(key);
|
|
29
|
+
return entry ? entry.expiresAt !== null && Date.now() > entry.expiresAt ? (this.cache.delete(key), null) : entry.value : null;
|
|
30
|
+
}
|
|
31
|
+
async set(key, value, ttl) {
|
|
32
|
+
const expiresAt = ttl ? Date.now() + ttl : null;
|
|
33
|
+
return this.cache.set(key, { value, expiresAt }), !0;
|
|
34
|
+
}
|
|
35
|
+
async del(key) {
|
|
36
|
+
return this.cache.delete(key);
|
|
37
|
+
}
|
|
38
|
+
async has(key) {
|
|
39
|
+
const entry = this.cache.get(key);
|
|
40
|
+
return entry ? entry.expiresAt !== null && Date.now() > entry.expiresAt ? (this.cache.delete(key), !1) : !0 : !1;
|
|
41
|
+
}
|
|
42
|
+
async clear() {
|
|
43
|
+
this.cache.clear();
|
|
44
|
+
}
|
|
45
|
+
async close() {
|
|
46
|
+
this.cleanupInterval && (clearInterval(this.cleanupInterval), this.cleanupInterval = null), this.cache.clear();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Remove expired entries.
|
|
50
|
+
*/
|
|
51
|
+
cleanup() {
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
for (const [key, entry] of this.cache)
|
|
54
|
+
entry.expiresAt !== null && now > entry.expiresAt && this.cache.delete(key);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get cache stats (for debugging).
|
|
58
|
+
*/
|
|
59
|
+
stats() {
|
|
60
|
+
return { size: this.cache.size };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
MemoryDriver
|
|
66
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
class MemoryDriver {
|
|
2
|
+
constructor(cleanupIntervalMs = 0) {
|
|
3
|
+
this.name = "MemoryDriver";
|
|
4
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
5
|
+
this.cleanupInterval = null;
|
|
6
|
+
cleanupIntervalMs > 0 && (this.cleanupInterval = setInterval(() => this.cleanup(), cleanupIntervalMs));
|
|
7
|
+
}
|
|
8
|
+
async get(key) {
|
|
9
|
+
const entry = this.cache.get(key);
|
|
10
|
+
return entry ? entry.expiresAt !== null && Date.now() > entry.expiresAt ? (this.cache.delete(key), null) : entry.value : null;
|
|
11
|
+
}
|
|
12
|
+
async set(key, value, ttl) {
|
|
13
|
+
const expiresAt = ttl ? Date.now() + ttl : null;
|
|
14
|
+
return this.cache.set(key, { value, expiresAt }), !0;
|
|
15
|
+
}
|
|
16
|
+
async del(key) {
|
|
17
|
+
return this.cache.delete(key);
|
|
18
|
+
}
|
|
19
|
+
async has(key) {
|
|
20
|
+
const entry = this.cache.get(key);
|
|
21
|
+
return entry ? entry.expiresAt !== null && Date.now() > entry.expiresAt ? (this.cache.delete(key), !1) : !0 : !1;
|
|
22
|
+
}
|
|
23
|
+
async clear() {
|
|
24
|
+
this.cache.clear();
|
|
25
|
+
}
|
|
26
|
+
async close() {
|
|
27
|
+
this.cleanupInterval && (clearInterval(this.cleanupInterval), this.cleanupInterval = null), this.cache.clear();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Remove expired entries.
|
|
31
|
+
*/
|
|
32
|
+
cleanup() {
|
|
33
|
+
const now = Date.now();
|
|
34
|
+
for (const [key, entry] of this.cache)
|
|
35
|
+
entry.expiresAt !== null && now > entry.expiresAt && this.cache.delete(key);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get cache stats (for debugging).
|
|
39
|
+
*/
|
|
40
|
+
stats() {
|
|
41
|
+
return { size: this.cache.size };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
MemoryDriver
|
|
46
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
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);
|
|
15
|
+
var RedisDriver_exports = {};
|
|
16
|
+
__export(RedisDriver_exports, {
|
|
17
|
+
RedisDriver: () => RedisDriver
|
|
18
|
+
});
|
|
19
|
+
module.exports = __toCommonJS(RedisDriver_exports);
|
|
20
|
+
class RedisDriver {
|
|
21
|
+
constructor(config = {}) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.name = "RedisDriver";
|
|
24
|
+
this.client = null;
|
|
25
|
+
this.connected = !1;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Connect to Redis (lazy - connects on first use).
|
|
29
|
+
*/
|
|
30
|
+
async ensureConnected() {
|
|
31
|
+
if (this.connected) return;
|
|
32
|
+
const url = this.config.url || `redis://${this.config.host || "localhost"}:${this.config.port || 6379}`;
|
|
33
|
+
if (typeof Bun < "u" && Bun.redis)
|
|
34
|
+
this.client = new Bun.redis(url);
|
|
35
|
+
else
|
|
36
|
+
try {
|
|
37
|
+
const Redis = require("ioredis");
|
|
38
|
+
this.client = new Redis({
|
|
39
|
+
host: this.config.host || "localhost",
|
|
40
|
+
port: this.config.port || 6379,
|
|
41
|
+
password: this.config.password,
|
|
42
|
+
db: this.config.db || 0
|
|
43
|
+
});
|
|
44
|
+
} catch {
|
|
45
|
+
throw new Error("Redis client not available. Install ioredis or use Bun with Redis support.");
|
|
46
|
+
}
|
|
47
|
+
this.connected = !0;
|
|
48
|
+
}
|
|
49
|
+
async get(key) {
|
|
50
|
+
await this.ensureConnected();
|
|
51
|
+
const value = await this.client.get(key);
|
|
52
|
+
if (value === null)
|
|
53
|
+
return null;
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(value);
|
|
56
|
+
} catch {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async set(key, value, ttl) {
|
|
61
|
+
await this.ensureConnected();
|
|
62
|
+
const serialized = typeof value == "string" ? value : JSON.stringify(value);
|
|
63
|
+
return ttl ? await this.client.setex(key, ttl, serialized) : await this.client.set(key, serialized), !0;
|
|
64
|
+
}
|
|
65
|
+
async del(key) {
|
|
66
|
+
return await this.ensureConnected(), await this.client.del(key) > 0;
|
|
67
|
+
}
|
|
68
|
+
async has(key) {
|
|
69
|
+
return await this.ensureConnected(), await this.client.exists(key) > 0;
|
|
70
|
+
}
|
|
71
|
+
async clear() {
|
|
72
|
+
await this.ensureConnected(), await this.client.flushdb();
|
|
73
|
+
}
|
|
74
|
+
async close() {
|
|
75
|
+
this.client && this.connected && (await this.client.quit?.(), this.connected = !1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
RedisDriver
|
|
81
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
class RedisDriver {
|
|
2
|
+
constructor(config = {}) {
|
|
3
|
+
this.config = config;
|
|
4
|
+
this.name = "RedisDriver";
|
|
5
|
+
this.client = null;
|
|
6
|
+
this.connected = !1;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Connect to Redis (lazy - connects on first use).
|
|
10
|
+
*/
|
|
11
|
+
async ensureConnected() {
|
|
12
|
+
if (this.connected) return;
|
|
13
|
+
const url = this.config.url || `redis://${this.config.host || "localhost"}:${this.config.port || 6379}`;
|
|
14
|
+
if (typeof Bun < "u" && Bun.redis)
|
|
15
|
+
this.client = new Bun.redis(url);
|
|
16
|
+
else
|
|
17
|
+
try {
|
|
18
|
+
const Redis = require("ioredis");
|
|
19
|
+
this.client = new Redis({
|
|
20
|
+
host: this.config.host || "localhost",
|
|
21
|
+
port: this.config.port || 6379,
|
|
22
|
+
password: this.config.password,
|
|
23
|
+
db: this.config.db || 0
|
|
24
|
+
});
|
|
25
|
+
} catch {
|
|
26
|
+
throw new Error("Redis client not available. Install ioredis or use Bun with Redis support.");
|
|
27
|
+
}
|
|
28
|
+
this.connected = !0;
|
|
29
|
+
}
|
|
30
|
+
async get(key) {
|
|
31
|
+
await this.ensureConnected();
|
|
32
|
+
const value = await this.client.get(key);
|
|
33
|
+
if (value === null)
|
|
34
|
+
return null;
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(value);
|
|
37
|
+
} catch {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async set(key, value, ttl) {
|
|
42
|
+
await this.ensureConnected();
|
|
43
|
+
const serialized = typeof value == "string" ? value : JSON.stringify(value);
|
|
44
|
+
return ttl ? await this.client.setex(key, ttl, serialized) : await this.client.set(key, serialized), !0;
|
|
45
|
+
}
|
|
46
|
+
async del(key) {
|
|
47
|
+
return await this.ensureConnected(), await this.client.del(key) > 0;
|
|
48
|
+
}
|
|
49
|
+
async has(key) {
|
|
50
|
+
return await this.ensureConnected(), await this.client.exists(key) > 0;
|
|
51
|
+
}
|
|
52
|
+
async clear() {
|
|
53
|
+
await this.ensureConnected(), await this.client.flushdb();
|
|
54
|
+
}
|
|
55
|
+
async close() {
|
|
56
|
+
this.client && this.connected && (await this.client.quit?.(), this.connected = !1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
RedisDriver
|
|
61
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
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);
|
|
15
|
+
var JITCompiler_exports = {};
|
|
16
|
+
__export(JITCompiler_exports, {
|
|
17
|
+
compileHandler: () => compileHandler,
|
|
18
|
+
isAsyncFunction: () => isAsyncFunction,
|
|
19
|
+
isStaticHandler: () => isStaticHandler
|
|
20
|
+
});
|
|
21
|
+
module.exports = __toCommonJS(JITCompiler_exports);
|
|
22
|
+
const ASYNC_REGEX = /^async\s|^\([^)]*\)\s*=>\s*\{[\s\S]*await\s|^function\s*\*|\.then\s*\(/;
|
|
23
|
+
function isAsyncFunction(fn) {
|
|
24
|
+
if (fn.constructor.name === "AsyncFunction")
|
|
25
|
+
return !0;
|
|
26
|
+
const source = fn.toString();
|
|
27
|
+
return ASYNC_REGEX.test(source);
|
|
28
|
+
}
|
|
29
|
+
function isStaticHandler(fn) {
|
|
30
|
+
const source = fn.toString();
|
|
31
|
+
return source.includes("this.") || source.includes("await") ? !1 : !!source.match(/=>\s*["'`]|return\s+["'`]|=>\s*\{|=>\s*\d/);
|
|
32
|
+
}
|
|
33
|
+
function compileHandler(instance, methodName, params) {
|
|
34
|
+
const method = instance[methodName], bound = method.bind(instance), async = isAsyncFunction(method);
|
|
35
|
+
if (params.length === 0) {
|
|
36
|
+
if (isStaticHandler(method) && !async) {
|
|
37
|
+
const staticValue = bound();
|
|
38
|
+
return {
|
|
39
|
+
fn: bound,
|
|
40
|
+
isAsync: !1,
|
|
41
|
+
isStatic: !0,
|
|
42
|
+
staticValue
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
fn: bound,
|
|
47
|
+
isAsync: async,
|
|
48
|
+
isStatic: !1
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const argsCode = params.sort((a, b) => a.index - b.index).map((p) => buildArgExpression(p)).join(",");
|
|
52
|
+
if (params.some((p) => p.type === "body")) {
|
|
53
|
+
const code2 = `return async function(c){
|
|
54
|
+
await c.parseBody();
|
|
55
|
+
return h(${argsCode});
|
|
56
|
+
}`;
|
|
57
|
+
return {
|
|
58
|
+
fn: new Function("h", code2)(bound),
|
|
59
|
+
isAsync: !0,
|
|
60
|
+
isStatic: !1
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (async) {
|
|
64
|
+
const code2 = `return async function(c){
|
|
65
|
+
return await h(${argsCode});
|
|
66
|
+
}`;
|
|
67
|
+
return {
|
|
68
|
+
fn: new Function("h", code2)(bound),
|
|
69
|
+
isAsync: !0,
|
|
70
|
+
isStatic: !1
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const code = `return function(c){
|
|
74
|
+
return h(${argsCode});
|
|
75
|
+
}`;
|
|
76
|
+
return {
|
|
77
|
+
fn: new Function("h", code)(bound),
|
|
78
|
+
isAsync: !1,
|
|
79
|
+
isStatic: !1
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function escapeKey(key) {
|
|
83
|
+
return key.replace(/['\"\\]/g, "\\$&");
|
|
84
|
+
}
|
|
85
|
+
function buildArgExpression(param) {
|
|
86
|
+
const key = param.key ? escapeKey(param.key) : void 0;
|
|
87
|
+
switch (param.type) {
|
|
88
|
+
case "param":
|
|
89
|
+
return key ? `c.params['${key}']` : "c.params";
|
|
90
|
+
case "query":
|
|
91
|
+
return key ? `c.query['${key}']` : "c.query";
|
|
92
|
+
case "body":
|
|
93
|
+
return key ? `c.body['${key}']` : "c.body";
|
|
94
|
+
case "header":
|
|
95
|
+
return key ? `c.req.headers.get('${key}')` : "c.req.headers";
|
|
96
|
+
case "req":
|
|
97
|
+
return "c.req";
|
|
98
|
+
case "ctx":
|
|
99
|
+
return "c";
|
|
100
|
+
case "locals":
|
|
101
|
+
return key ? `c.locals['${key}']` : "c.locals";
|
|
102
|
+
default:
|
|
103
|
+
return "undefined";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
compileHandler,
|
|
109
|
+
isAsyncFunction,
|
|
110
|
+
isStaticHandler
|
|
111
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const ASYNC_REGEX = /^async\s|^\([^)]*\)\s*=>\s*\{[\s\S]*await\s|^function\s*\*|\.then\s*\(/;
|
|
2
|
+
function isAsyncFunction(fn) {
|
|
3
|
+
if (fn.constructor.name === "AsyncFunction")
|
|
4
|
+
return !0;
|
|
5
|
+
const source = fn.toString();
|
|
6
|
+
return ASYNC_REGEX.test(source);
|
|
7
|
+
}
|
|
8
|
+
function isStaticHandler(fn) {
|
|
9
|
+
const source = fn.toString();
|
|
10
|
+
return source.includes("this.") || source.includes("await") ? !1 : !!source.match(/=>\s*["'`]|return\s+["'`]|=>\s*\{|=>\s*\d/);
|
|
11
|
+
}
|
|
12
|
+
function compileHandler(instance, methodName, params) {
|
|
13
|
+
const method = instance[methodName], bound = method.bind(instance), async = isAsyncFunction(method);
|
|
14
|
+
if (params.length === 0) {
|
|
15
|
+
if (isStaticHandler(method) && !async) {
|
|
16
|
+
const staticValue = bound();
|
|
17
|
+
return {
|
|
18
|
+
fn: bound,
|
|
19
|
+
isAsync: !1,
|
|
20
|
+
isStatic: !0,
|
|
21
|
+
staticValue
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
fn: bound,
|
|
26
|
+
isAsync: async,
|
|
27
|
+
isStatic: !1
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const argsCode = params.sort((a, b) => a.index - b.index).map((p) => buildArgExpression(p)).join(",");
|
|
31
|
+
if (params.some((p) => p.type === "body")) {
|
|
32
|
+
const code2 = `return async function(c){
|
|
33
|
+
await c.parseBody();
|
|
34
|
+
return h(${argsCode});
|
|
35
|
+
}`;
|
|
36
|
+
return {
|
|
37
|
+
fn: new Function("h", code2)(bound),
|
|
38
|
+
isAsync: !0,
|
|
39
|
+
isStatic: !1
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (async) {
|
|
43
|
+
const code2 = `return async function(c){
|
|
44
|
+
return await h(${argsCode});
|
|
45
|
+
}`;
|
|
46
|
+
return {
|
|
47
|
+
fn: new Function("h", code2)(bound),
|
|
48
|
+
isAsync: !0,
|
|
49
|
+
isStatic: !1
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const code = `return function(c){
|
|
53
|
+
return h(${argsCode});
|
|
54
|
+
}`;
|
|
55
|
+
return {
|
|
56
|
+
fn: new Function("h", code)(bound),
|
|
57
|
+
isAsync: !1,
|
|
58
|
+
isStatic: !1
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function escapeKey(key) {
|
|
62
|
+
return key.replace(/['\"\\]/g, "\\$&");
|
|
63
|
+
}
|
|
64
|
+
function buildArgExpression(param) {
|
|
65
|
+
const key = param.key ? escapeKey(param.key) : void 0;
|
|
66
|
+
switch (param.type) {
|
|
67
|
+
case "param":
|
|
68
|
+
return key ? `c.params['${key}']` : "c.params";
|
|
69
|
+
case "query":
|
|
70
|
+
return key ? `c.query['${key}']` : "c.query";
|
|
71
|
+
case "body":
|
|
72
|
+
return key ? `c.body['${key}']` : "c.body";
|
|
73
|
+
case "header":
|
|
74
|
+
return key ? `c.req.headers.get('${key}')` : "c.req.headers";
|
|
75
|
+
case "req":
|
|
76
|
+
return "c.req";
|
|
77
|
+
case "ctx":
|
|
78
|
+
return "c";
|
|
79
|
+
case "locals":
|
|
80
|
+
return key ? `c.locals['${key}']` : "c.locals";
|
|
81
|
+
default:
|
|
82
|
+
return "undefined";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
compileHandler,
|
|
87
|
+
isAsyncFunction,
|
|
88
|
+
isStaticHandler
|
|
89
|
+
};
|