@adaptivestone/framework 5.0.0-beta.9 → 5.0.0-rc.1
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/LICENCE +1 -1
- package/README.md +66 -2
- package/dist/Cli.d.ts +7 -0
- package/dist/Cli.js +19 -0
- package/dist/Cli.js.map +1 -0
- package/dist/cluster.d.ts +1 -0
- package/dist/cluster.js +67 -0
- package/dist/cluster.js.map +1 -0
- package/dist/codegen/appTypes.d.ts +19 -0
- package/dist/codegen/appTypes.js +126 -0
- package/dist/codegen/appTypes.js.map +1 -0
- package/dist/codegen/astEmit.d.ts +53 -0
- package/dist/codegen/astEmit.js +242 -0
- package/dist/codegen/astEmit.js.map +1 -0
- package/dist/codegen/astExtract.d.ts +78 -0
- package/dist/codegen/astExtract.js +539 -0
- package/dist/codegen/astExtract.js.map +1 -0
- package/dist/codegen/astModel.d.ts +14 -0
- package/dist/codegen/astModel.js +73 -0
- package/dist/codegen/astModel.js.map +1 -0
- package/dist/codegen/astResolve.d.ts +53 -0
- package/dist/codegen/astResolve.js +276 -0
- package/dist/codegen/astResolve.js.map +1 -0
- package/dist/codegen/astSpec.d.ts +25 -0
- package/dist/codegen/astSpec.js +63 -0
- package/dist/codegen/astSpec.js.map +1 -0
- package/dist/codegen/collectMetadata.d.ts +39 -0
- package/dist/codegen/collectMetadata.js +7 -0
- package/dist/codegen/collectMetadata.js.map +1 -0
- package/dist/codegen/emit.d.ts +30 -0
- package/dist/codegen/emit.js +213 -0
- package/dist/codegen/emit.js.map +1 -0
- package/dist/codegen/index.d.ts +30 -0
- package/dist/codegen/index.js +76 -0
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/paths.d.ts +9 -0
- package/dist/codegen/paths.js +25 -0
- package/dist/codegen/paths.js.map +1 -0
- package/dist/commands/CreateUser.d.ts +32 -0
- package/dist/commands/CreateUser.js +85 -0
- package/dist/commands/CreateUser.js.map +1 -0
- package/dist/commands/DropIndex.d.ts +16 -0
- package/dist/commands/DropIndex.js +30 -0
- package/dist/commands/DropIndex.js.map +1 -0
- package/dist/commands/GenerateRandomBytes.d.ts +7 -0
- package/dist/commands/GenerateRandomBytes.js +18 -0
- package/dist/commands/GenerateRandomBytes.js.map +1 -0
- package/dist/commands/GenerateTypes.d.ts +13 -0
- package/dist/commands/GenerateTypes.js +34 -0
- package/dist/commands/GenerateTypes.js.map +1 -0
- package/dist/commands/SyncIndexes.d.ts +6 -0
- package/dist/commands/SyncIndexes.js +31 -0
- package/dist/commands/SyncIndexes.js.map +1 -0
- package/dist/commands/migration/Create.d.ts +18 -0
- package/dist/commands/migration/Create.js +57 -0
- package/dist/commands/migration/Create.js.map +1 -0
- package/dist/commands/migration/Migrate.d.ts +6 -0
- package/dist/commands/migration/Migrate.js +73 -0
- package/dist/commands/migration/Migrate.js.map +1 -0
- package/dist/config/auth.d.ts +11 -0
- package/dist/config/auth.js +18 -0
- package/dist/config/auth.js.map +1 -0
- package/dist/config/http.d.ts +22 -0
- package/dist/config/http.js +40 -0
- package/dist/config/http.js.map +1 -0
- package/dist/config/i18n.d.ts +13 -0
- package/dist/config/i18n.js +13 -0
- package/dist/config/i18n.js.map +1 -0
- package/dist/config/ipDetector.d.ts +5 -0
- package/dist/config/ipDetector.js +15 -0
- package/dist/config/ipDetector.js.map +1 -0
- package/dist/config/log.d.ts +11 -0
- package/dist/config/log.js +21 -0
- package/dist/config/log.js.map +1 -0
- package/dist/config/mongo.d.ts +4 -0
- package/dist/config/mongo.js +4 -0
- package/dist/config/mongo.js.map +1 -0
- package/dist/config/rateLimiter.d.ts +13 -0
- package/dist/config/rateLimiter.js +17 -0
- package/dist/config/rateLimiter.js.map +1 -0
- package/dist/config/redis.d.ts +5 -0
- package/dist/config/redis.js +5 -0
- package/dist/config/redis.js.map +1 -0
- package/dist/controllers/Auth.d.ts +59 -0
- package/dist/controllers/Auth.js +356 -0
- package/dist/controllers/Auth.js.map +1 -0
- package/dist/controllers/Auth.routes.gen.d.ts +57 -0
- package/dist/controllers/Auth.routes.gen.js +10 -0
- package/dist/controllers/Auth.routes.gen.js.map +1 -0
- package/dist/controllers/Home.d.ts +14 -0
- package/dist/controllers/Home.js +26 -0
- package/dist/controllers/Home.js.map +1 -0
- package/dist/controllers/Home.routes.gen.d.ts +13 -0
- package/dist/controllers/Home.routes.gen.js +10 -0
- package/dist/controllers/Home.routes.gen.js.map +1 -0
- package/dist/controllers/index.d.ts +107 -0
- package/dist/controllers/index.js +548 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/folderConfig.d.ts +34 -0
- package/dist/folderConfig.js +14 -0
- package/dist/folderConfig.js.map +1 -0
- package/dist/helpers/appInstance.d.ts +10 -0
- package/dist/helpers/appInstance.js +19 -0
- package/dist/helpers/appInstance.js.map +1 -0
- package/dist/helpers/crypto.d.ts +26 -0
- package/dist/helpers/crypto.js +160 -0
- package/dist/helpers/crypto.js.map +1 -0
- package/dist/helpers/deprecation.d.ts +8 -0
- package/dist/helpers/deprecation.js +19 -0
- package/dist/helpers/deprecation.js.map +1 -0
- package/dist/helpers/env.d.ts +8 -0
- package/dist/helpers/env.js +15 -0
- package/dist/helpers/env.js.map +1 -0
- package/dist/helpers/files.d.ts +16 -0
- package/dist/helpers/files.js +83 -0
- package/dist/helpers/files.js.map +1 -0
- package/dist/helpers/logger.d.ts +6 -0
- package/dist/helpers/logger.js +34 -0
- package/dist/helpers/logger.js.map +1 -0
- package/dist/helpers/redis/clearNamespace.d.ts +10 -0
- package/dist/helpers/redis/clearNamespace.js +31 -0
- package/dist/helpers/redis/clearNamespace.js.map +1 -0
- package/dist/helpers/redis/redisConnection.d.ts +8 -0
- package/dist/helpers/redis/redisConnection.js +63 -0
- package/dist/helpers/redis/redisConnection.js.map +1 -0
- package/dist/helpers/yup.d.ts +22 -0
- package/dist/helpers/yup.js +39 -0
- package/dist/helpers/yup.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/{locales → dist/locales}/en/translation.json +5 -0
- package/{locales → dist/locales}/ru/translation.json +5 -0
- package/dist/models/KeyValue.d.ts +22 -0
- package/dist/models/KeyValue.js +17 -0
- package/dist/models/KeyValue.js.map +1 -0
- package/dist/models/Lock.d.ts +99 -0
- package/dist/models/Lock.js +123 -0
- package/dist/models/Lock.js.map +1 -0
- package/dist/models/Migration.d.ts +13 -0
- package/dist/models/Migration.js +14 -0
- package/dist/models/Migration.js.map +1 -0
- package/dist/models/Sequence.d.ts +28 -0
- package/dist/models/Sequence.js +31 -0
- package/dist/models/Sequence.js.map +1 -0
- package/dist/models/User.d.ts +769 -0
- package/dist/models/User.js +336 -0
- package/dist/models/User.js.map +1 -0
- package/dist/models/UserOld.d.ts +191 -0
- package/dist/models/UserOld.js +231 -0
- package/dist/models/UserOld.js.map +1 -0
- package/dist/modules/AbstractCommand.d.ts +51 -0
- package/dist/modules/AbstractCommand.js +60 -0
- package/dist/modules/AbstractCommand.js.map +1 -0
- package/dist/modules/AbstractConnector.d.ts +5 -0
- package/dist/modules/AbstractConnector.js +8 -0
- package/dist/modules/AbstractConnector.js.map +1 -0
- package/dist/modules/AbstractController.d.ts +99 -0
- package/dist/modules/AbstractController.js +83 -0
- package/dist/modules/AbstractController.js.map +1 -0
- package/dist/modules/AbstractModel.d.ts +29 -0
- package/dist/modules/AbstractModel.js +42 -0
- package/dist/modules/AbstractModel.js.map +1 -0
- package/dist/modules/Base.d.ts +29 -0
- package/dist/modules/Base.js +62 -0
- package/dist/modules/Base.js.map +1 -0
- package/dist/modules/BaseCli.d.ts +25 -0
- package/dist/modules/BaseCli.js +159 -0
- package/dist/modules/BaseCli.js.map +1 -0
- package/dist/modules/BaseModel.d.ts +57 -0
- package/dist/modules/BaseModel.js +37 -0
- package/dist/modules/BaseModel.js.map +1 -0
- package/dist/server.d.ts +130 -0
- package/dist/server.js +603 -0
- package/dist/server.js.map +1 -0
- package/dist/services/cache/Cache.d.ts +31 -0
- package/dist/services/cache/Cache.js +146 -0
- package/dist/services/cache/Cache.js.map +1 -0
- package/dist/services/http/HttpServer.d.ts +46 -0
- package/dist/services/http/HttpServer.js +114 -0
- package/dist/services/http/HttpServer.js.map +1 -0
- package/dist/services/http/files.d.ts +18 -0
- package/dist/services/http/files.js +19 -0
- package/dist/services/http/files.js.map +1 -0
- package/dist/services/http/middleware/AbstractMiddleware.d.ts +58 -0
- package/dist/services/http/middleware/AbstractMiddleware.js +73 -0
- package/dist/services/http/middleware/AbstractMiddleware.js.map +1 -0
- package/dist/services/http/middleware/Auth.d.ts +22 -0
- package/dist/services/http/middleware/Auth.js +30 -0
- package/dist/services/http/middleware/Auth.js.map +1 -0
- package/dist/services/http/middleware/Cors.d.ts +12 -0
- package/dist/services/http/middleware/Cors.js +45 -0
- package/dist/services/http/middleware/Cors.js.map +1 -0
- package/dist/services/http/middleware/GetUserByToken.d.ts +36 -0
- package/dist/services/http/middleware/GetUserByToken.js +59 -0
- package/dist/services/http/middleware/GetUserByToken.js.map +1 -0
- package/dist/services/http/middleware/I18n.d.ts +23 -0
- package/dist/services/http/middleware/I18n.js +61 -0
- package/dist/services/http/middleware/I18n.js.map +1 -0
- package/dist/services/http/middleware/IpDetector.d.ts +14 -0
- package/dist/services/http/middleware/IpDetector.js +80 -0
- package/dist/services/http/middleware/IpDetector.js.map +1 -0
- package/dist/services/http/middleware/Pagination.d.ts +26 -0
- package/dist/services/http/middleware/Pagination.js +70 -0
- package/dist/services/http/middleware/Pagination.js.map +1 -0
- package/dist/services/http/middleware/PrepareAppInfo.d.ts +8 -0
- package/dist/services/http/middleware/PrepareAppInfo.js +17 -0
- package/dist/services/http/middleware/PrepareAppInfo.js.map +1 -0
- package/dist/services/http/middleware/RateLimiter.d.ts +31 -0
- package/dist/services/http/middleware/RateLimiter.js +118 -0
- package/dist/services/http/middleware/RateLimiter.js.map +1 -0
- package/dist/services/http/middleware/RequestLogger.d.ts +8 -0
- package/dist/services/http/middleware/RequestLogger.js +20 -0
- package/dist/services/http/middleware/RequestLogger.js.map +1 -0
- package/dist/services/http/middleware/RequestParser.d.ts +15 -0
- package/dist/services/http/middleware/RequestParser.js +112 -0
- package/dist/services/http/middleware/RequestParser.js.map +1 -0
- package/dist/services/http/middleware/Role.d.ts +12 -0
- package/dist/services/http/middleware/Role.js +27 -0
- package/dist/services/http/middleware/Role.js.map +1 -0
- package/dist/services/http/routing/ExpressAdapter.d.ts +12 -0
- package/dist/services/http/routing/ExpressAdapter.js +95 -0
- package/dist/services/http/routing/ExpressAdapter.js.map +1 -0
- package/dist/services/http/routing/RouteNode.d.ts +105 -0
- package/dist/services/http/routing/RouteNode.js +21 -0
- package/dist/services/http/routing/RouteNode.js.map +1 -0
- package/dist/services/http/routing/RouteRegistry.d.ts +37 -0
- package/dist/services/http/routing/RouteRegistry.js +244 -0
- package/dist/services/http/routing/RouteRegistry.js.map +1 -0
- package/dist/services/http/routing/formatTree.d.ts +23 -0
- package/dist/services/http/routing/formatTree.js +159 -0
- package/dist/services/http/routing/formatTree.js.map +1 -0
- package/dist/services/http/routing/match.d.ts +31 -0
- package/dist/services/http/routing/match.js +164 -0
- package/dist/services/http/routing/match.js.map +1 -0
- package/dist/services/http/routing/middlewareNormalization.d.ts +11 -0
- package/dist/services/http/routing/middlewareNormalization.js +23 -0
- package/dist/services/http/routing/middlewareNormalization.js.map +1 -0
- package/dist/services/http/types.d.ts +74 -0
- package/dist/services/http/types.js +8 -0
- package/dist/services/http/types.js.map +1 -0
- package/dist/services/i18n/I18n.d.ts +15 -0
- package/dist/services/i18n/I18n.js +58 -0
- package/dist/services/i18n/I18n.js.map +1 -0
- package/dist/services/logging/SentryTransport.d.ts +13 -0
- package/dist/services/logging/SentryTransport.js +134 -0
- package/dist/services/logging/SentryTransport.js.map +1 -0
- package/dist/services/validate/ValidateService.d.ts +54 -0
- package/dist/services/validate/ValidateService.js +144 -0
- package/dist/services/validate/ValidateService.js.map +1 -0
- package/dist/services/validate/ValidationError.d.ts +35 -0
- package/dist/services/validate/ValidationError.js +90 -0
- package/dist/services/validate/ValidationError.js.map +1 -0
- package/dist/services/validate/contentType.d.ts +18 -0
- package/dist/services/validate/contentType.js +30 -0
- package/dist/services/validate/contentType.js.map +1 -0
- package/dist/services/validate/defineSchema.d.ts +23 -0
- package/dist/services/validate/defineSchema.js +25 -0
- package/dist/services/validate/defineSchema.js.map +1 -0
- package/dist/services/validate/drivers/StandardSchemaDriver.d.ts +15 -0
- package/dist/services/validate/drivers/StandardSchemaDriver.js +38 -0
- package/dist/services/validate/drivers/StandardSchemaDriver.js.map +1 -0
- package/dist/services/validate/drivers/YupDriver.d.ts +21 -0
- package/dist/services/validate/drivers/YupDriver.js +81 -0
- package/dist/services/validate/drivers/YupDriver.js.map +1 -0
- package/dist/services/validate/types.d.ts +104 -0
- package/dist/services/validate/types.js +11 -0
- package/dist/services/validate/types.js.map +1 -0
- package/dist/tests/frameworkVitestSetup.d.ts +1 -0
- package/dist/tests/frameworkVitestSetup.js +9 -0
- package/dist/tests/frameworkVitestSetup.js.map +1 -0
- package/dist/tests/globalSetupVitest.d.ts +3 -0
- package/dist/tests/globalSetupVitest.js +29 -0
- package/dist/tests/globalSetupVitest.js.map +1 -0
- package/dist/tests/setupVitest.d.ts +1 -0
- package/dist/tests/setupVitest.js +101 -0
- package/dist/tests/setupVitest.js.map +1 -0
- package/dist/tests/testHelpers.d.ts +361 -0
- package/dist/tests/testHelpers.js +48 -0
- package/dist/tests/testHelpers.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +83 -39
- package/.env.example +0 -18
- package/CHANGELOG.md +0 -956
- package/Cli.js +0 -21
- package/cluster.js +0 -25
- package/commands/CreateUser.js +0 -106
- package/commands/Documentation.js +0 -17
- package/commands/DropIndex.js +0 -38
- package/commands/GenerateRandomBytes.js +0 -21
- package/commands/GetOpenApiJson.js +0 -336
- package/commands/SyncIndexes.js +0 -40
- package/commands/migration/Create.js +0 -72
- package/commands/migration/Migrate.js +0 -56
- package/config/auth.js +0 -9
- package/config/http.js +0 -9
- package/config/i18n.js +0 -12
- package/config/ipDetector.js +0 -14
- package/config/log.js +0 -22
- package/config/mongo.js +0 -3
- package/config/rateLimiter.js +0 -16
- package/config/redis.js +0 -4
- package/config/validate.js +0 -3
- package/controllers/Auth.js +0 -201
- package/controllers/Home.js +0 -28
- package/controllers/index.js +0 -60
- package/eslint.config.js +0 -68
- package/folderConfig.js +0 -13
- package/helpers/files.js +0 -76
- package/helpers/logger.js +0 -16
- package/helpers/redis/clearNamespace.js +0 -14
- package/helpers/yup.js +0 -22
- package/index.js +0 -8
- package/jsconfig.json +0 -9
- package/models/Lock.js +0 -107
- package/models/Migration.js +0 -15
- package/models/Sequence.js +0 -22
- package/models/User.js +0 -280
- package/modules/AbstractCommand.js +0 -52
- package/modules/AbstractConnector.js +0 -9
- package/modules/AbstractController.js +0 -406
- package/modules/AbstractModel.d.ts +0 -48
- package/modules/AbstractModel.js +0 -92
- package/modules/Base.d.ts +0 -37
- package/modules/Base.js +0 -64
- package/modules/BaseCli.js +0 -157
- package/server.d.ts +0 -100
- package/server.js +0 -439
- package/services/cache/Cache.d.ts +0 -35
- package/services/cache/Cache.js +0 -124
- package/services/documentation/DocumentationGenerator.js +0 -169
- package/services/http/HttpServer.js +0 -89
- package/services/http/middleware/AbstractMiddleware.js +0 -51
- package/services/http/middleware/Auth.js +0 -20
- package/services/http/middleware/Cors.js +0 -46
- package/services/http/middleware/GetUserByToken.js +0 -47
- package/services/http/middleware/I18n.js +0 -117
- package/services/http/middleware/IpDetector.js +0 -59
- package/services/http/middleware/Pagination.js +0 -57
- package/services/http/middleware/PrepareAppInfo.js +0 -18
- package/services/http/middleware/RateLimiter.js +0 -134
- package/services/http/middleware/RequestLogger.js +0 -22
- package/services/http/middleware/RequestParser.js +0 -40
- package/services/http/middleware/Role.js +0 -29
- package/services/messaging/email/templates/.gitkeep +0 -0
- package/services/validate/ValidateService.js +0 -157
- package/services/validate/drivers/AbstractValidator.js +0 -37
- package/services/validate/drivers/CustomValidator.js +0 -51
- package/services/validate/drivers/YupValidator.js +0 -103
- package/tests/globalSetupVitest.js +0 -35
- package/tests/setup.js +0 -120
- package/tests/setupVitest.js +0 -111
- package/types/Expand.d.ts +0 -11
- package/types/ICommandArguments.d.ts +0 -41
- package/types/TFoldersConfig.d.ts +0 -20
- package/views/404.pug +0 -3
- package/views/home.pug +0 -3
- package/views/layouts/base.pug +0 -39
- package/vitest.config.js +0 -17
- /package/{commands → dist/migrations}/.gitkeep +0 -0
- /package/{services → dist/services}/messaging/email/resources/.gitkeep +0 -0
- /package/{migrations → dist/services/messaging/email/templates}/.gitkeep +0 -0
- /package/{services → dist/services}/messaging/email/templates/recovery/html.pug +0 -0
- /package/{services → dist/services}/messaging/email/templates/recovery/subject.pug +0 -0
- /package/{services → dist/services}/messaging/email/templates/recovery/text.pug +0 -0
- /package/{services → dist/services}/messaging/email/templates/verification/html.pug +0 -0
- /package/{services → dist/services}/messaging/email/templates/verification/subject.pug +0 -0
- /package/{services → dist/services}/messaging/email/templates/verification/text.pug +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { getRedisClient } from "../../helpers/redis/redisConnection.js";
|
|
2
|
+
import Base from "../../modules/Base.js";
|
|
3
|
+
class Cache extends Base {
|
|
4
|
+
whenReady;
|
|
5
|
+
redisClient;
|
|
6
|
+
redisNamespace = '';
|
|
7
|
+
promiseMapping = new Map();
|
|
8
|
+
constructor(app) {
|
|
9
|
+
super(app);
|
|
10
|
+
this.whenReady = this.#init();
|
|
11
|
+
}
|
|
12
|
+
async #init() {
|
|
13
|
+
// todo for now only redis. refactor for drives support in future
|
|
14
|
+
// at least memory and redis drivers should be presented
|
|
15
|
+
// memory drives should works on master process level
|
|
16
|
+
// we should support multiple cashe same time
|
|
17
|
+
const { namespace } = this.app.getConfig('redis');
|
|
18
|
+
this.redisClient = await getRedisClient();
|
|
19
|
+
this.redisNamespace = namespace;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* As framework support namespaces all key for cache go through this function
|
|
23
|
+
* Function return new key with added namespace
|
|
24
|
+
* @param key key to add namespace
|
|
25
|
+
*/
|
|
26
|
+
getKeyWithNameSpace(key) {
|
|
27
|
+
return `${this.redisNamespace}-${key}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get value from cache. Set and get if not eists
|
|
31
|
+
* @param key key to check
|
|
32
|
+
* @param onNotFound callback that will be executed if value not found on cahce
|
|
33
|
+
* @param storeTime how long we should store value on cache
|
|
34
|
+
*/
|
|
35
|
+
async getSetValue(keyValue, onNotFound, storeTime = 60 * 5) {
|
|
36
|
+
await this.whenReady;
|
|
37
|
+
const key = this.getKeyWithNameSpace(keyValue);
|
|
38
|
+
// 5 mins default
|
|
39
|
+
let resolve = (_value) => { };
|
|
40
|
+
let reject = (_value) => { };
|
|
41
|
+
if (this.promiseMapping.has(key)) {
|
|
42
|
+
return this.promiseMapping.get(key);
|
|
43
|
+
}
|
|
44
|
+
const inflight = new Promise((res, rej) => {
|
|
45
|
+
resolve = res;
|
|
46
|
+
reject = rej;
|
|
47
|
+
});
|
|
48
|
+
// Concurrent waiters get any rejection via their own `await`; this no-op
|
|
49
|
+
// handler prevents a zero-waiter `unhandledRejection` when `reject` fires.
|
|
50
|
+
inflight.catch(() => { });
|
|
51
|
+
this.promiseMapping.set(key, inflight);
|
|
52
|
+
// One try/finally so the mapping entry ALWAYS settles and clears — a redis
|
|
53
|
+
// failure mid-flight must not leave a forever-pending entry (per-key deadlock
|
|
54
|
+
// that would survive redis recovery).
|
|
55
|
+
try {
|
|
56
|
+
let parsedResult;
|
|
57
|
+
// A cache outage is NOT a request outage: if redis is unreachable, degrade
|
|
58
|
+
// to computing the value via `onNotFound` every call (slow, not broken)
|
|
59
|
+
// rather than failing. Only `onNotFound`'s own errors propagate.
|
|
60
|
+
let cached = null;
|
|
61
|
+
let cacheUsable = true;
|
|
62
|
+
let client;
|
|
63
|
+
try {
|
|
64
|
+
// Resolve the live client through the shared helper every time rather
|
|
65
|
+
// than reusing `this.redisClient`: after a shutdown/reconnect the module
|
|
66
|
+
// rebuilds the client, and the helper's single-flight connect avoids the
|
|
67
|
+
// "Socket already opened" throw two concurrent callers hit when each
|
|
68
|
+
// calls `connect()` itself.
|
|
69
|
+
client = await getRedisClient();
|
|
70
|
+
this.redisClient = client;
|
|
71
|
+
cached = await client.get(key);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
cacheUsable = false;
|
|
75
|
+
this.logger?.error(`Cache read failed for key '${key}', falling back to onNotFound: ${e}`);
|
|
76
|
+
}
|
|
77
|
+
let cacheHit = false;
|
|
78
|
+
if (cached) {
|
|
79
|
+
try {
|
|
80
|
+
parsedResult = JSON.parse(cached, (_jsonkey, value) => {
|
|
81
|
+
if (typeof value === 'string' && /^\d+n$/.test(value)) {
|
|
82
|
+
return BigInt(value.slice(0, value.length - 1));
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
});
|
|
86
|
+
cacheHit = true;
|
|
87
|
+
this.logger?.verbose(`getSetValueFromCache FROM CACHE key ${key}, value ${cached.substring(0, 100)}`);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// This class only ever stores `JSON.stringify` output, so a value that
|
|
91
|
+
// won't parse is genuine corruption. Treat it as a miss — recompute
|
|
92
|
+
// and overwrite — rather than returning `undefined` to the caller.
|
|
93
|
+
this.logger?.warn(`Corrupt cache value for key '${key}' — recomputing and overwriting`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!cacheHit) {
|
|
97
|
+
this.logger?.verbose(`getSetValueFromCache not found for key ${key}`);
|
|
98
|
+
parsedResult = await onNotFound();
|
|
99
|
+
const serialized = JSON.stringify(parsedResult, (_jsonkey, value) => typeof value === 'bigint' ? `${value}n` : value);
|
|
100
|
+
// Skip the write when the cache is unreachable, or the value serializes
|
|
101
|
+
// to `undefined` (which the redis client rejects). The write is
|
|
102
|
+
// best-effort: the value is already computed, so a failed write — like a
|
|
103
|
+
// failed read — must not fail the call.
|
|
104
|
+
if (cacheUsable && serialized !== undefined && client) {
|
|
105
|
+
await client
|
|
106
|
+
.set(key, serialized, { EX: storeTime })
|
|
107
|
+
.catch((e) => this.logger?.error(`Cache set failed for key '${key}': ${e}`));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
resolve(parsedResult);
|
|
111
|
+
return parsedResult;
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
this.logger?.error(`Cache getSetValue for key '${key}' error: ${e}`);
|
|
115
|
+
reject(e);
|
|
116
|
+
throw e;
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
this.promiseMapping.delete(key);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Remove key from cache
|
|
124
|
+
* @param key key to remove
|
|
125
|
+
*/
|
|
126
|
+
async removeKey(keyValue) {
|
|
127
|
+
await this.whenReady;
|
|
128
|
+
const key = this.getKeyWithNameSpace(keyValue);
|
|
129
|
+
// Fail-soft like the read/write paths: a redis blip during invalidation
|
|
130
|
+
// must not throw into business logic.
|
|
131
|
+
try {
|
|
132
|
+
const client = await getRedisClient();
|
|
133
|
+
this.redisClient = client;
|
|
134
|
+
return await client.del(key);
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
this.logger?.error(`Cache removeKey failed for key '${key}': ${e}`);
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
static get loggerGroup() {
|
|
142
|
+
return 'Cache_';
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export default Cache;
|
|
146
|
+
//# sourceMappingURL=Cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Cache.js","sourceRoot":"","sources":["../../../src/services/cache/Cache.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AACxE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ACN,CAAC;YACH,CAAC;YAED,OAAO,CAAC,YAAY,CAAC,CAAC;YACtB,OAAO,YAAY,CAAC;QACtB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,8BAA8B,GAAG,YAAY,CAAC,EAAE,CAAC,CAAC;YACrE,MAAM,CAAC,CAAC,CAAC,CAAC;YACV,MAAM,CAAC,CAAC;QACV,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS,CAAC,QAAgB;QAC9B,MAAM,IAAI,CAAC,SAAS,CAAC;QAErB,MAAM,GAAG,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QAC/C,wEAAwE;QACxE,sCAAsC;QACtC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,cAAc,EAAE,CAAC;YACtC,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC;YAC1B,OAAO,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,mCAAmC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;YACpE,OAAO,CAAC,CAAC;QACX,CAAC;IACH,CAAC;IAED,MAAM,KAAK,WAAW;QACpB,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAED,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Server } from 'node:http';
|
|
2
|
+
import type { Express, Request } from 'express';
|
|
3
|
+
import type { TFunction } from 'i18next';
|
|
4
|
+
import Base from '../../modules/Base.ts';
|
|
5
|
+
import type { IApp } from '../../server.ts';
|
|
6
|
+
import { RouteRegistry } from './routing/RouteRegistry.ts';
|
|
7
|
+
export interface FrameworkRequest extends Request {
|
|
8
|
+
appInfo: {
|
|
9
|
+
app: IApp;
|
|
10
|
+
ip?: string | undefined;
|
|
11
|
+
request: Record<string, unknown>;
|
|
12
|
+
query: Record<string, unknown>;
|
|
13
|
+
i18n?: {
|
|
14
|
+
t: TFunction;
|
|
15
|
+
language: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* HTTP server based on Express
|
|
21
|
+
*/
|
|
22
|
+
declare class HttpServer extends Base {
|
|
23
|
+
express: Express;
|
|
24
|
+
httpServer: Server;
|
|
25
|
+
routeRegistry: RouteRegistry;
|
|
26
|
+
constructor(app: IApp);
|
|
27
|
+
/** Mount the route adapter — single entry to the registry. */
|
|
28
|
+
mountAdapter(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Add handle for 404 error
|
|
31
|
+
*/
|
|
32
|
+
add404Page(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Add the 500 error handler. Express recognises 4-arg middleware as the
|
|
35
|
+
* error sink, so it must be registered last.
|
|
36
|
+
*/
|
|
37
|
+
addErrorHandler(): void;
|
|
38
|
+
static get loggerGroup(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Stop the HTTP server: refuse new connections and resolve once in-flight
|
|
41
|
+
* requests have drained, so a caller can await the drain before downstream
|
|
42
|
+
* resources (mongo, redis) are torn down.
|
|
43
|
+
*/
|
|
44
|
+
shutdown(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
export default HttpServer;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import Base from "../../modules/Base.js";
|
|
4
|
+
import Cors from "./middleware/Cors.js";
|
|
5
|
+
import I18nMiddleware from "./middleware/I18n.js";
|
|
6
|
+
import IpDetector from "./middleware/IpDetector.js";
|
|
7
|
+
import PrepareAppInfoMiddleware from "./middleware/PrepareAppInfo.js";
|
|
8
|
+
import RequestLoggerMiddleware from "./middleware/RequestLogger.js";
|
|
9
|
+
import RequestParserMiddleware from "./middleware/RequestParser.js";
|
|
10
|
+
import { createExpressAdapter } from "./routing/ExpressAdapter.js";
|
|
11
|
+
import { RouteRegistry } from "./routing/RouteRegistry.js";
|
|
12
|
+
/**
|
|
13
|
+
* HTTP server based on Express
|
|
14
|
+
*/
|
|
15
|
+
class HttpServer extends Base {
|
|
16
|
+
express;
|
|
17
|
+
httpServer;
|
|
18
|
+
routeRegistry;
|
|
19
|
+
constructor(app) {
|
|
20
|
+
super(app);
|
|
21
|
+
this.express = express();
|
|
22
|
+
this.express.disable('x-powered-by');
|
|
23
|
+
this.routeRegistry = new RouteRegistry();
|
|
24
|
+
const httpConfig = this.app.getConfig('http');
|
|
25
|
+
// Security headers first, so every response path (routes, 404, 405, errors)
|
|
26
|
+
// carries them. The header list is precomputed; `enabled` is re-read per
|
|
27
|
+
// request so it can be toggled via config without a reboot.
|
|
28
|
+
const securityHeaderEntries = Object.entries(httpConfig.securityHeaders ?? {}).filter(([key, value]) => key !== 'enabled' && typeof value === 'string');
|
|
29
|
+
this.express.use((_req, res, next) => {
|
|
30
|
+
const securityHeaders = this.app.getConfig('http')
|
|
31
|
+
.securityHeaders;
|
|
32
|
+
if (securityHeaders?.enabled) {
|
|
33
|
+
for (const [name, value] of securityHeaderEntries) {
|
|
34
|
+
res.setHeader(name, value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
next();
|
|
38
|
+
});
|
|
39
|
+
this.express.use(new RequestLoggerMiddleware(this.app).getMiddleware());
|
|
40
|
+
this.express.use(new PrepareAppInfoMiddleware(this.app).getMiddleware());
|
|
41
|
+
this.express.use(new IpDetector(this.app).getMiddleware());
|
|
42
|
+
this.express.use(new I18nMiddleware(this.app).getMiddleware());
|
|
43
|
+
this.express.use(new Cors(this.app, {
|
|
44
|
+
origins: httpConfig.corsDomains,
|
|
45
|
+
}).getMiddleware());
|
|
46
|
+
this.express.use(new RequestParserMiddleware(this.app).getMiddleware());
|
|
47
|
+
this.httpServer = http.createServer(this.express);
|
|
48
|
+
// A server that can't bind (EADDRINUSE / EACCES) has no purpose: exit so a
|
|
49
|
+
// supervisor restarts it, instead of lingering as a healthy-looking process
|
|
50
|
+
// serving nothing. Without this listener the 'error' would surface as an
|
|
51
|
+
// uncaughtException (only logged) — a silent dead process.
|
|
52
|
+
this.httpServer.on('error', (err) => {
|
|
53
|
+
this.logger?.error(`HTTP server failed to start: ${err}`);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
});
|
|
56
|
+
const listener = this.httpServer.listen(httpConfig.port, httpConfig.hostname, () => {
|
|
57
|
+
const address = listener.address();
|
|
58
|
+
const port = typeof address === 'string' ? 0 : address?.port || 0;
|
|
59
|
+
this.logger?.info(`App started and listening on port ${port}`);
|
|
60
|
+
if (+port !== +httpConfig.port) {
|
|
61
|
+
// in case we using port 0
|
|
62
|
+
this.app.updateConfig('http', { port });
|
|
63
|
+
this.logger?.info(`Updating http config to use new port ${port}. Old was ${httpConfig.port} `);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Mount the route adapter — single entry to the registry. */
|
|
68
|
+
mountAdapter() {
|
|
69
|
+
this.express.use(createExpressAdapter(this.routeRegistry, this.app));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Add handle for 404 error
|
|
73
|
+
*/
|
|
74
|
+
add404Page() {
|
|
75
|
+
this.express.use((_req, res) => {
|
|
76
|
+
// error handling
|
|
77
|
+
res.status(404).json({ message: '404' });
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Add the 500 error handler. Express recognises 4-arg middleware as the
|
|
82
|
+
* error sink, so it must be registered last.
|
|
83
|
+
*/
|
|
84
|
+
addErrorHandler() {
|
|
85
|
+
this.express.use((err, _req, res, next) => {
|
|
86
|
+
this.logger?.error(`Unhandled request error: ${err.stack ?? err}`);
|
|
87
|
+
// If the response already started (e.g. a handler that threw mid-stream),
|
|
88
|
+
// we can't set a 500 — hand off to Express's default finalizer instead
|
|
89
|
+
// of crashing with ERR_HTTP_HEADERS_SENT.
|
|
90
|
+
if (res.headersSent) {
|
|
91
|
+
return next(err);
|
|
92
|
+
}
|
|
93
|
+
res.status(500).json({ message: 'Something broke!' });
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
static get loggerGroup() {
|
|
97
|
+
return 'service';
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Stop the HTTP server: refuse new connections and resolve once in-flight
|
|
101
|
+
* requests have drained, so a caller can await the drain before downstream
|
|
102
|
+
* resources (mongo, redis) are torn down.
|
|
103
|
+
*/
|
|
104
|
+
shutdown() {
|
|
105
|
+
return new Promise((resolve) => {
|
|
106
|
+
this.httpServer.close(() => resolve());
|
|
107
|
+
// Idle keep-alive sockets would otherwise hold the drain open until the
|
|
108
|
+
// client/LB times out; active requests still finish.
|
|
109
|
+
this.httpServer.closeIdleConnections();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export default HttpServer;
|
|
114
|
+
//# sourceMappingURL=HttpServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpServer.js","sourceRoot":"","sources":["../../../src/services/http/HttpServer.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,WAAW,CAAC;AAQ7B,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,OAAO,IAAI,MAAM,uBAAuB,CAAC;AAEzC,OAAO,IAAI,MAAM,sBAAsB,CAAC;AACxC,OAAO,cAAc,MAAM,sBAAsB,CAAC;AAClD,OAAO,UAAU,MAAM,4BAA4B,CAAC;AACpD,OAAO,wBAAwB,MAAM,gCAAgC,CAAC;AACtE,OAAO,uBAAuB,MAAM,+BAA+B,CAAC;AACpE,OAAO,uBAAuB,MAAM,+BAA+B,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,O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,CAAC,CAAC;YACnB,CAAC;YACD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,kBAAkB,EAAE,CAAC,CAAC;QACxD,CAAC,CACF,CAAC;IACJ,CAAC;IAED,MAAM,KAAK,WAAW;QACpB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;YACvC,wEAAwE;YACxE,qDAAqD;YACrD,IAAI,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Uploaded-file type. Today it aliases formidable's `PersistentFile`; the
|
|
3
|
+
* future transport-neutral parser swap (P3) re-points it at the web-standard
|
|
4
|
+
* `File`. Import this instead of the concrete parser class so your file
|
|
5
|
+
* validation survives that swap untouched.
|
|
6
|
+
*
|
|
7
|
+
* Validate uploads with your validator's idiomatic `instanceof` check:
|
|
8
|
+
*
|
|
9
|
+
* import { File } from '@adaptivestone/framework/types.js';
|
|
10
|
+
* // zod: z.instanceof(File)
|
|
11
|
+
* // valibot: v.instance(File)
|
|
12
|
+
* // arktype: type.instanceOf(File)
|
|
13
|
+
* // yup: mixed().test('file', 'not a file', (v) => v instanceof File)
|
|
14
|
+
*
|
|
15
|
+
* `File` is both a value (for `instanceof`) and a type (for annotations),
|
|
16
|
+
* because it is a class.
|
|
17
|
+
*/
|
|
18
|
+
export { PersistentFile as File } from 'formidable';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Uploaded-file type. Today it aliases formidable's `PersistentFile`; the
|
|
3
|
+
* future transport-neutral parser swap (P3) re-points it at the web-standard
|
|
4
|
+
* `File`. Import this instead of the concrete parser class so your file
|
|
5
|
+
* validation survives that swap untouched.
|
|
6
|
+
*
|
|
7
|
+
* Validate uploads with your validator's idiomatic `instanceof` check:
|
|
8
|
+
*
|
|
9
|
+
* import { File } from '@adaptivestone/framework/types.js';
|
|
10
|
+
* // zod: z.instanceof(File)
|
|
11
|
+
* // valibot: v.instance(File)
|
|
12
|
+
* // arktype: type.instanceOf(File)
|
|
13
|
+
* // yup: mixed().test('file', 'not a file', (v) => v instanceof File)
|
|
14
|
+
*
|
|
15
|
+
* `File` is both a value (for `instanceof`) and a type (for annotations),
|
|
16
|
+
* because it is a class.
|
|
17
|
+
*/
|
|
18
|
+
export { PersistentFile as File } from 'formidable';
|
|
19
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../../src/services/http/files.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,cAAc,IAAI,IAAI,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { NextFunction, Response } from 'express';
|
|
2
|
+
import Base from '../../../modules/Base.ts';
|
|
3
|
+
import type { IApp } from '../../../server.ts';
|
|
4
|
+
import type { StandardSchemaV1 } from '../../validate/types.ts';
|
|
5
|
+
import type { FrameworkRequest } from '../HttpServer.ts';
|
|
6
|
+
declare class AbstractMiddleware extends Base {
|
|
7
|
+
params?: Record<string, unknown>;
|
|
8
|
+
constructor(app: IApp, params?: Record<string, unknown>);
|
|
9
|
+
static get description(): string;
|
|
10
|
+
get usedAuthParameters(): Array<{
|
|
11
|
+
name: string;
|
|
12
|
+
type: string;
|
|
13
|
+
in?: string;
|
|
14
|
+
scheme?: string;
|
|
15
|
+
description: string;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Schema describing query parameters this middleware consumes. Declared
|
|
19
|
+
* **statically** so the framework (and codegen) can read it without
|
|
20
|
+
* instantiating the middleware. Override with any Standard Schema-conformant
|
|
21
|
+
* schema (Zod, Valibot, ArkType, yup ≥1.7, `defineSchema`). Default `null`.
|
|
22
|
+
*
|
|
23
|
+
* Example:
|
|
24
|
+
* static get relatedQueryParameters() {
|
|
25
|
+
* return z.object({ page: z.number(), limit: z.number() });
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
static get relatedQueryParameters(): StandardSchemaV1 | null;
|
|
29
|
+
/**
|
|
30
|
+
* Schema describing request-body parameters this middleware consumes. Static;
|
|
31
|
+
* same rules as `relatedQueryParameters`.
|
|
32
|
+
*/
|
|
33
|
+
static get relatedRequestParameters(): StandardSchemaV1 | null;
|
|
34
|
+
/**
|
|
35
|
+
* @deprecated Since 5.0.0-beta.51 — declare the schema **statically**
|
|
36
|
+
* (`static get relatedQueryParameters()`) instead. The instance form forces
|
|
37
|
+
* the framework to instantiate the middleware just to read the schema (which
|
|
38
|
+
* runs its constructor side effects); it will be removed in v6.
|
|
39
|
+
*/
|
|
40
|
+
get relatedQueryParameters(): StandardSchemaV1 | null;
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated Since 5.0.0-beta.51 — use `static get relatedRequestParameters()`.
|
|
43
|
+
* Removed in v6.
|
|
44
|
+
*/
|
|
45
|
+
get relatedRequestParameters(): StandardSchemaV1 | null;
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated Since 5.0.0-beta.51 — read the static getters instead.
|
|
48
|
+
* Removed in v6.
|
|
49
|
+
*/
|
|
50
|
+
get relatedReqParameters(): {
|
|
51
|
+
request: StandardSchemaV1<unknown, unknown> | null;
|
|
52
|
+
query: StandardSchemaV1<unknown, unknown> | null;
|
|
53
|
+
};
|
|
54
|
+
middleware(_req: FrameworkRequest, _res: Response, next: NextFunction): Promise<void | Response>;
|
|
55
|
+
getMiddleware(): (_req: FrameworkRequest, _res: Response, next: NextFunction) => Promise<void | Response>;
|
|
56
|
+
static get loggerGroup(): string;
|
|
57
|
+
}
|
|
58
|
+
export default AbstractMiddleware;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import Base from "../../../modules/Base.js";
|
|
2
|
+
class AbstractMiddleware extends Base {
|
|
3
|
+
params;
|
|
4
|
+
constructor(app, params) {
|
|
5
|
+
super(app);
|
|
6
|
+
this.params = params;
|
|
7
|
+
}
|
|
8
|
+
static get description() {
|
|
9
|
+
return 'Middleware description. Please provide own';
|
|
10
|
+
}
|
|
11
|
+
get usedAuthParameters() {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Schema describing query parameters this middleware consumes. Declared
|
|
16
|
+
* **statically** so the framework (and codegen) can read it without
|
|
17
|
+
* instantiating the middleware. Override with any Standard Schema-conformant
|
|
18
|
+
* schema (Zod, Valibot, ArkType, yup ≥1.7, `defineSchema`). Default `null`.
|
|
19
|
+
*
|
|
20
|
+
* Example:
|
|
21
|
+
* static get relatedQueryParameters() {
|
|
22
|
+
* return z.object({ page: z.number(), limit: z.number() });
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
static get relatedQueryParameters() {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Schema describing request-body parameters this middleware consumes. Static;
|
|
30
|
+
* same rules as `relatedQueryParameters`.
|
|
31
|
+
*/
|
|
32
|
+
static get relatedRequestParameters() {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Since 5.0.0-beta.51 — declare the schema **statically**
|
|
37
|
+
* (`static get relatedQueryParameters()`) instead. The instance form forces
|
|
38
|
+
* the framework to instantiate the middleware just to read the schema (which
|
|
39
|
+
* runs its constructor side effects); it will be removed in v6.
|
|
40
|
+
*/
|
|
41
|
+
get relatedQueryParameters() {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @deprecated Since 5.0.0-beta.51 — use `static get relatedRequestParameters()`.
|
|
46
|
+
* Removed in v6.
|
|
47
|
+
*/
|
|
48
|
+
get relatedRequestParameters() {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated Since 5.0.0-beta.51 — read the static getters instead.
|
|
53
|
+
* Removed in v6.
|
|
54
|
+
*/
|
|
55
|
+
get relatedReqParameters() {
|
|
56
|
+
return {
|
|
57
|
+
request: this.relatedRequestParameters,
|
|
58
|
+
query: this.relatedQueryParameters,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async middleware(_req, _res, next) {
|
|
62
|
+
this.logger?.warn('Middleware is not implemented');
|
|
63
|
+
return next();
|
|
64
|
+
}
|
|
65
|
+
getMiddleware() {
|
|
66
|
+
return this.middleware.bind(this);
|
|
67
|
+
}
|
|
68
|
+
static get loggerGroup() {
|
|
69
|
+
return 'middleware';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export default AbstractMiddleware;
|
|
73
|
+
//# sourceMappingURL=AbstractMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractMiddleware.js","sourceRoot":"","sources":["../../../../src/services/http/middleware/AbstractMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,0BAA0B,CAAC;AAK5C,MAAM,kBAAmB,SAAQ,IAAI;IACnC,MAAM,CAA2B;IAEjC,YAAY,GAAS,EAAE,MAAgC;QACrD,KAAK,CAAC,GAAG,CAAC,CAAC;QACX,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,MAAM,KAAK,WAAW;QACpB,OAAO,4CAA4C,CAAC;IACtD,CAAC;IAED,IAAI,kBAAkB;QAOpB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED;;;;;;;;;;OAUG;IACH,MAAM,KAAK,sBAAsB;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,MAAM,KAAK,wBAAwB;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,IAAI,sBAAsB;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,IAAI,wBAAwB;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,IAAI,oBAAoB;QACtB,OAAO;YACL,OAAO,EAAE,IAAI,CAAC,wBAAwB;YACtC,KAAK,EAAE,IAAI,CAAC,sBAAsB;SACnC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,UAAU,CACd,IAAsB,EACtB,IAAc,EACd,IAAkB;QAGlB,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,+BAA+B,CAAC,CAAC;QACnD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,KAAK,WAAW;QACpB,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AAED,eAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { NextFunction, Response } from 'express';
|
|
2
|
+
import type { AppUser } from '../../../models/User.ts';
|
|
3
|
+
import type { FrameworkRequest } from '../HttpServer.ts';
|
|
4
|
+
import type { GetUserByTokenAppInfo } from '../middleware/GetUserByToken.ts';
|
|
5
|
+
import AbstractMiddleware from './AbstractMiddleware.ts';
|
|
6
|
+
declare class AuthMiddleware extends AbstractMiddleware {
|
|
7
|
+
static get description(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Type-only phantom (codegen reads it; runtime ignores it — see
|
|
10
|
+
* `GetUserByToken.provides`). Auth rejects unauthenticated requests at
|
|
11
|
+
* runtime, so past this point `appInfo.user` is guaranteed present. Declaring
|
|
12
|
+
* it as a *required* `user` narrows the optional `user?` that
|
|
13
|
+
* `GetUserByToken` contributes: `UnionAppInfoProvides` intersects the shapes,
|
|
14
|
+
* and `{ user?: U } & { user: U }` collapses to `{ user: U }`. Handlers
|
|
15
|
+
* behind `Auth` then read `appInfo.user` without a guard.
|
|
16
|
+
*/
|
|
17
|
+
static get provides(): {
|
|
18
|
+
user: AppUser;
|
|
19
|
+
};
|
|
20
|
+
middleware(req: FrameworkRequest & GetUserByTokenAppInfo, res: Response, next: NextFunction): Promise<void | Response<any, Record<string, any>>>;
|
|
21
|
+
}
|
|
22
|
+
export default AuthMiddleware;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import AbstractMiddleware from "./AbstractMiddleware.js";
|
|
2
|
+
class AuthMiddleware extends AbstractMiddleware {
|
|
3
|
+
static get description() {
|
|
4
|
+
return 'Allow to pass only if the user provided. Please use any middleware that provide user instance before';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Type-only phantom (codegen reads it; runtime ignores it — see
|
|
8
|
+
* `GetUserByToken.provides`). Auth rejects unauthenticated requests at
|
|
9
|
+
* runtime, so past this point `appInfo.user` is guaranteed present. Declaring
|
|
10
|
+
* it as a *required* `user` narrows the optional `user?` that
|
|
11
|
+
* `GetUserByToken` contributes: `UnionAppInfoProvides` intersects the shapes,
|
|
12
|
+
* and `{ user?: U } & { user: U }` collapses to `{ user: U }`. Handlers
|
|
13
|
+
* behind `Auth` then read `appInfo.user` without a guard.
|
|
14
|
+
*/
|
|
15
|
+
static get provides() {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
async middleware(req, res, next) {
|
|
19
|
+
if (!req.appInfo.user) {
|
|
20
|
+
this.logger?.info('User try to access resource without credentials');
|
|
21
|
+
return res.status(401).json({
|
|
22
|
+
error: 'AUTH001',
|
|
23
|
+
message: 'Please login to application',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export default AuthMiddleware;
|
|
30
|
+
//# sourceMappingURL=Auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Auth.js","sourceRoot":"","sources":["../../../../src/services/http/middleware/Auth.ts"],"names":[],"mappings":"AAIA,OAAO,kBAAkB,MAAM,yBAAyB,CAAC;AAEzD,MAAM,cAAe,SAAQ,kBAAkB;IAC7C,MAAM,KAAK,WAAW;QACpB,OAAO,sGAAsG,CAAC;IAChH,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,KAAK,QAAQ;QACjB,OAAO,EAAuB,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,UAAU,CACd,GAA6C,EAC7C,GAAa,EACb,IAAkB;QAElB,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YACtB,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,iDAAiD,CAAC,CAAC;YACrE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBAC1B,KAAK,EAAE,SAAS;gBAChB,OAAO,EAAE,6BAA6B;aACvC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;CACF;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NextFunction, Response } from 'express';
|
|
2
|
+
import type { IApp } from '../../../server.ts';
|
|
3
|
+
import type { FrameworkRequest } from '../HttpServer.ts';
|
|
4
|
+
import AbstractMiddleware from './AbstractMiddleware.ts';
|
|
5
|
+
declare class Cors extends AbstractMiddleware {
|
|
6
|
+
constructor(app: IApp, params: {
|
|
7
|
+
origins: (string | RegExp)[];
|
|
8
|
+
});
|
|
9
|
+
static get description(): string;
|
|
10
|
+
middleware(req: FrameworkRequest, res: Response, next: NextFunction): Promise<void | Response<any, Record<string, any>>>;
|
|
11
|
+
}
|
|
12
|
+
export default Cors;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import AbstractMiddleware from "./AbstractMiddleware.js";
|
|
2
|
+
class Cors extends AbstractMiddleware {
|
|
3
|
+
constructor(app, params) {
|
|
4
|
+
super(app);
|
|
5
|
+
this.params = params;
|
|
6
|
+
if (!Array.isArray(params?.origins) || !params.origins.length) {
|
|
7
|
+
throw new Error('Cors inited without origin config');
|
|
8
|
+
}
|
|
9
|
+
// An unanchored regex matches more than intended — `/example\.com/` also
|
|
10
|
+
// matches evil-example.com. Warn (don't change behavior) so the misconfig
|
|
11
|
+
// is visible at boot.
|
|
12
|
+
for (const origin of params.origins) {
|
|
13
|
+
if (origin instanceof RegExp &&
|
|
14
|
+
(!origin.source.startsWith('^') || !origin.source.endsWith('$'))) {
|
|
15
|
+
this.logger?.warn(`CORS regex /${origin.source}/ is not anchored — it can match unintended origins (e.g. evil-example.com). Anchor it with ^…$.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static get description() {
|
|
20
|
+
return 'Add CORS headers to request';
|
|
21
|
+
}
|
|
22
|
+
async middleware(req, res, next) {
|
|
23
|
+
for (const host of this.params?.origins) {
|
|
24
|
+
if ((typeof host === 'string' && req.headers.origin === host) ||
|
|
25
|
+
(host instanceof RegExp && host.test(req.headers.origin ?? ''))) {
|
|
26
|
+
res.set('Access-Control-Allow-Origin', req.headers.origin);
|
|
27
|
+
res.set('Vary', 'Origin');
|
|
28
|
+
if (req.method === 'OPTIONS') {
|
|
29
|
+
res.set('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
|
|
30
|
+
res.set('Vary', 'Origin, Access-Control-Request-Headers');
|
|
31
|
+
const allowedHeaders = req.headers['access-control-request-headers'];
|
|
32
|
+
if (allowedHeaders) {
|
|
33
|
+
res.set('Access-Control-Allow-Headers', allowedHeaders);
|
|
34
|
+
}
|
|
35
|
+
res.set('Content-Length', '0');
|
|
36
|
+
res.status(204);
|
|
37
|
+
return res.end();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return next();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export default Cors;
|
|
45
|
+
//# sourceMappingURL=Cors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Cors.js","sourceRoot":"","sources":["../../../../src/services/http/middleware/Cors.ts"],"names":[],"mappings":"AAGA,OAAO,kBAAkB,MAAM,yBAAyB,CAAC;AAEzD,MAAM,IAAK,SAAQ,kBAAkB;IACnC,YACE,GAAS,EACT,MAEC;QAED,KAAK,CAAC,GAAG,CAAC,CAAC;QACX,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC9D,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACvD,CAAC;QACD,yEAAyE;QACzE,0EAA0E;QAC1E,sBAAsB;QACtB,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACpC,IACE,MAAM,YAAY,MAAM;gBACxB,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAChE,CAAC;gBACD,IAAI,CAAC,MAAM,EAAE,IAAI,CACf,eAAe,MAAM,CAAC,MAAM,kGAAkG,CAC/H,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,KAAK,WAAW;QACpB,OAAO,6BAA6B,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,GAAqB,EAAE,GAAa,EAAE,IAAkB;QACvE,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE,OAA8B,EAAE,CAAC;YAC/D,IACE,CAAC,OAAO,IAAI,KAAK,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC;gBACzD,CAAC,IAAI,YAAY,MAAM,IAAI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