@ikonintegration/ikapi 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +81 -0
- package/.github/workflows/npmpublish.yml +8 -19
- package/.github/workflows/prs.yml +12 -0
- package/README.md +89 -99
- package/dist/index.d.ts +16 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/package-lock.json +11881 -0
- package/dist/package.json +81 -0
- package/dist/src/API/Request.d.ts +125 -0
- package/dist/src/API/Request.js +185 -0
- package/dist/src/API/Request.js.map +1 -0
- package/dist/src/API/Response.d.ts +188 -0
- package/dist/src/API/Response.js +270 -0
- package/dist/src/API/Response.js.map +1 -0
- package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
- package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
- package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
- package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
- package/dist/src/BaseEvent/EventProcessor.js +101 -0
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
- package/dist/src/BaseEvent/Process.d.ts +50 -0
- package/dist/src/BaseEvent/Process.js +64 -0
- package/dist/src/BaseEvent/Process.js.map +1 -0
- package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
- package/dist/src/BaseEvent/StepTransaction.js +27 -0
- package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
- package/dist/src/BaseEvent/Transaction.d.ts +149 -0
- package/dist/src/BaseEvent/Transaction.js +224 -0
- package/dist/src/BaseEvent/Transaction.js.map +1 -0
- package/dist/src/Cache/Redis.d.ts +29 -0
- package/dist/src/Cache/Redis.js +80 -0
- package/dist/src/Cache/Redis.js.map +1 -0
- package/dist/src/Cache/types.d.ts +31 -0
- package/dist/src/Cache/types.js +2 -0
- package/dist/src/Cache/types.js.map +1 -0
- package/dist/src/Config/Configuration.d.ts +123 -0
- package/dist/src/Config/Configuration.js +109 -0
- package/dist/src/Config/Configuration.js.map +1 -0
- package/dist/src/Config/EnvironmentVar.d.ts +74 -0
- package/dist/src/Config/EnvironmentVar.js +138 -0
- package/dist/src/Config/EnvironmentVar.js.map +1 -0
- package/dist/src/Crypto/Crypto.d.ts +45 -0
- package/dist/src/Crypto/Crypto.js +72 -0
- package/dist/src/Crypto/Crypto.js.map +1 -0
- package/dist/src/Database/Database.d.ts +21 -0
- package/dist/src/Database/Database.js +15 -0
- package/dist/src/Database/Database.js.map +1 -0
- package/dist/src/Database/DatabaseManager.d.ts +47 -0
- package/dist/src/Database/DatabaseManager.js +60 -0
- package/dist/src/Database/DatabaseManager.js.map +1 -0
- package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
- package/dist/src/Database/DatabaseTransaction.js +126 -0
- package/dist/src/Database/DatabaseTransaction.js.map +1 -0
- package/dist/src/Database/index.d.ts +10 -0
- package/dist/src/Database/index.js +15 -0
- package/dist/src/Database/index.js.map +1 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
- package/dist/src/Database/types.d.ts +76 -0
- package/dist/src/Database/types.js +2 -0
- package/dist/src/Database/types.js.map +1 -0
- package/dist/src/Globals.d.ts +93 -0
- package/dist/src/Globals.js +99 -0
- package/dist/src/Globals.js.map +1 -0
- package/dist/src/Logger/Logger.d.ts +161 -0
- package/dist/src/Logger/Logger.js +299 -0
- package/dist/src/Logger/Logger.js.map +1 -0
- package/dist/src/Mailer/Mailer.d.ts +78 -0
- package/dist/src/Mailer/Mailer.js +182 -0
- package/dist/src/Mailer/Mailer.js.map +1 -0
- package/dist/src/Publisher/Publisher.d.ts +39 -0
- package/dist/src/Publisher/Publisher.js +77 -0
- package/dist/src/Publisher/Publisher.js.map +1 -0
- package/dist/src/Server/RouteResolver.d.ts +33 -0
- package/dist/src/Server/RouteResolver.js +100 -0
- package/dist/src/Server/RouteResolver.js.map +1 -0
- package/dist/src/Server/Router.d.ts +157 -0
- package/dist/src/Server/Router.js +32 -0
- package/dist/src/Server/Router.js.map +1 -0
- package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
- package/dist/src/Server/lib/ContainerServer.js +66 -0
- package/dist/src/Server/lib/ContainerServer.js.map +1 -0
- package/dist/src/Server/lib/Server.d.ts +45 -0
- package/dist/src/Server/lib/Server.js +93 -0
- package/dist/src/Server/lib/Server.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/GenericHandler.js +82 -0
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
- package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/HealthHandler.js +19 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
- package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
- package/dist/src/Server/lib/container/Proxy.js +143 -0
- package/dist/src/Server/lib/container/Proxy.js.map +1 -0
- package/dist/src/Server/lib/container/Utils.d.ts +14 -0
- package/dist/src/Server/lib/container/Utils.js +37 -0
- package/dist/src/Server/lib/container/Utils.js.map +1 -0
- package/dist/src/Util/AsyncSingleton.d.ts +31 -0
- package/dist/src/Util/AsyncSingleton.js +83 -0
- package/dist/src/Util/AsyncSingleton.js.map +1 -0
- package/dist/src/Util/Utils.d.ts +61 -0
- package/dist/src/Util/Utils.js +147 -0
- package/dist/src/Util/Utils.js.map +1 -0
- package/dist/src/Validation/Validator.d.ts +17 -0
- package/dist/src/Validation/Validator.js +39 -0
- package/dist/src/Validation/Validator.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/index.ts +41 -0
- package/jest.config.ts +37 -0
- package/jest.smoke.config.ts +34 -0
- package/package.json +66 -22
- package/src/API/Request.ts +214 -0
- package/src/API/Response.ts +370 -0
- package/src/BaseEvent/DynamoTransaction.ts +175 -0
- package/src/BaseEvent/EventProcessor.ts +140 -0
- package/src/BaseEvent/Process.ts +78 -0
- package/src/BaseEvent/StepTransaction.ts +35 -0
- package/src/BaseEvent/Transaction.ts +323 -0
- package/src/Cache/Redis.ts +89 -0
- package/src/Cache/types.ts +33 -0
- package/src/Config/Configuration.ts +199 -0
- package/src/Config/EnvironmentVar.ts +142 -0
- package/src/Crypto/Crypto.ts +89 -0
- package/src/Database/Database.ts +22 -0
- package/src/Database/DatabaseManager.ts +67 -0
- package/src/Database/DatabaseTransaction.ts +170 -0
- package/src/Database/index.ts +27 -0
- package/src/Database/integrations/dynamo/DynamoDatabase.ts +58 -0
- package/src/Database/integrations/kysely/KyselyDatabase.ts +99 -0
- package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
- package/src/Database/integrations/pgsql/PostgresDatabase.ts +56 -0
- package/src/Database/integrations/pgsql/PostgresTransaction.ts +87 -0
- package/src/Database/types.ts +85 -0
- package/src/Globals.ts +103 -0
- package/src/Logger/Logger.ts +363 -0
- package/src/Mailer/Mailer.ts +217 -0
- package/src/Publisher/Publisher.ts +96 -0
- package/src/Server/RouteResolver.ts +124 -0
- package/src/Server/Router.ts +200 -0
- package/src/Server/lib/ContainerServer.ts +65 -0
- package/src/Server/lib/Server.ts +109 -0
- package/src/Server/lib/container/GenericHandler.ts +76 -0
- package/src/Server/lib/container/GenericHandlerEvent.ts +154 -0
- package/src/Server/lib/container/HealthHandler.ts +11 -0
- package/src/Server/lib/container/Proxy.ts +172 -0
- package/src/Server/lib/container/Utils.ts +33 -0
- package/src/Util/AsyncSingleton.ts +86 -0
- package/src/Util/Utils.ts +131 -0
- package/src/Validation/Validator.ts +45 -0
- package/tests/API/Request.test.ts +273 -0
- package/tests/API/Response.test.ts +367 -0
- package/tests/BaseEvent/DynamoTransaction.test.ts +272 -0
- package/tests/BaseEvent/EventProcessor.test.ts +263 -0
- package/tests/BaseEvent/Process.test.ts +47 -0
- package/tests/BaseEvent/StepTransaction.test.ts +44 -0
- package/tests/BaseEvent/Transaction.test.ts +402 -0
- package/tests/Cache/Redis-client.test.ts +90 -0
- package/tests/Cache/Redis-cluster.test.ts +100 -0
- package/tests/Config/Config.test.ts +205 -0
- package/tests/Config/EnvironmentVar.test.ts +251 -0
- package/tests/Crypto/Crypto.test.ts +88 -0
- package/tests/Database/DatabaseManager.test.ts +79 -0
- package/tests/Database/integrations/dynamo/DynamoDatabase.test.ts +44 -0
- package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
- package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
- package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
- package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
- package/tests/Logger/Logger.test.ts +215 -0
- package/tests/Mailer/Mailer.test.ts +59 -0
- package/tests/Publisher/Publisher.test.ts +60 -0
- package/tests/Server/RouteResolver.test.ts +116 -0
- package/tests/Server/Router.test.ts +39 -0
- package/tests/Server/lib/ContainerServer.test.ts +531 -0
- package/tests/Server/lib/Server.test.ts +12 -0
- package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
- package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
- package/tests/Server/lib/container/Proxy.test.ts +268 -0
- package/tests/Server/lib/container/Utils.test.ts +47 -0
- package/tests/Test.utils.ts +78 -0
- package/tests/Utils/Utils.test.ts +229 -0
- package/tests/Validation/Validator.test.ts +82 -0
- package/tsconfig.json +26 -0
- package/tsconfig.smoke.json +26 -0
- package/index.js +0 -88
- package/src/API/IKRequest.js +0 -52
- package/src/API/IKResponse.js +0 -119
- package/src/API/IKUtils.js +0 -51
- package/src/BaseEvent/IKProcess.js +0 -77
- package/src/BaseEvent/IKTransaction.js +0 -139
- package/src/Cache/Prototype/IKCache.js +0 -17
- package/src/Cache/Redis/IKRedis.js +0 -148
- package/src/Database/DDB/IKDB.js +0 -56
- package/src/Database/DDB/IKDBBaseExpression.js +0 -130
- package/src/Database/DDB/IKDBBaseQuery.js +0 -151
- package/src/Database/DDB/IKDBQueryBatchGet.js +0 -37
- package/src/Database/DDB/IKDBQueryBatchWrite.js +0 -64
- package/src/Database/DDB/IKDBQueryDelete.js +0 -34
- package/src/Database/DDB/IKDBQueryGet.js +0 -48
- package/src/Database/DDB/IKDBQueryPut.js +0 -87
- package/src/Database/DDB/IKDBQueryScan.js +0 -45
- package/src/Database/DDB/IKDBQueryTransactionalWrite.js +0 -69
- package/src/Database/DDB/IKDBQueryUpdate.js +0 -221
- package/src/Database/DDB/_IKDBQueryTransactionalRead.js +0 -46
- package/src/Database/PSQL/IKDB.js +0 -41
- package/src/Database/PSQL/IKDBBaseQuery.js +0 -26
- package/src/Database/Prototype/IKDB.js +0 -21
- package/src/Database/Prototype/IKDBBaseQuery.js +0 -14
- package/src/IKDynamoStream.js +0 -42
- package/src/IKEventProcessor.js +0 -42
- package/src/IKGlobals.js +0 -24
- package/src/IKRouter.js +0 -47
- package/src/IKStepTransaction.js +0 -14
- package/src/Logger/IKLogger.js +0 -136
- package/src/Mailer/IKMailer.js +0 -69
- package/src/Publisher/IKPublisher.js +0 -44
- package/src/Tracker/IKExecutionTracker.js +0 -79
- package/src/Validation/IKValidation.js +0 -76
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var _a;
|
|
11
|
+
import { createCluster, createClient } from 'redis';
|
|
12
|
+
import AsyncSingleton from '../Util/AsyncSingleton.js';
|
|
13
|
+
/**
|
|
14
|
+
* Represents a Redis cache connection class.
|
|
15
|
+
*/
|
|
16
|
+
class Redis {
|
|
17
|
+
static connection(config) {
|
|
18
|
+
return _a.singleton.instance(config);
|
|
19
|
+
}
|
|
20
|
+
static connectionFactory(config) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (config.clusterMode)
|
|
23
|
+
return _a.redisClusterConnection(config);
|
|
24
|
+
else
|
|
25
|
+
return _a.redisClientConnection(config);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Establishes a connection to a Redis client based on the provided configuration
|
|
30
|
+
* and holds it for reusability. If connection is detected closed, new connection is
|
|
31
|
+
* initialized and established.
|
|
32
|
+
* @param {CacheConfig<'redis'>} config - The configuration object for connecting to the Redis client.
|
|
33
|
+
* @returns {Promise<RedisClientType>} A promise that resolves to the Redis client connection.
|
|
34
|
+
*/
|
|
35
|
+
static redisClientConnection(config) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
console.debug('Starting remote client cache connection');
|
|
38
|
+
const connection = _a.ClientFactory(Object.assign(Object.assign({ username: config.username }, (config.password ? { password: config.password } : {})), { disableOfflineQueue: true, socket: {
|
|
39
|
+
host: config.hostname,
|
|
40
|
+
tls: config.enableTLS,
|
|
41
|
+
connectTimeout: 10000,
|
|
42
|
+
} }));
|
|
43
|
+
yield connection.connect();
|
|
44
|
+
return connection;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
static redisClusterConnection(config) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
console.debug('Starting remote cluster cache connection');
|
|
50
|
+
const connection = _a.ClusterFactory({
|
|
51
|
+
defaults: Object.assign(Object.assign({ username: config.username }, (config.password ? { password: config.password } : {})), { socket: {
|
|
52
|
+
tls: config.enableTLS,
|
|
53
|
+
connectTimeout: 10000,
|
|
54
|
+
} }),
|
|
55
|
+
rootNodes: [
|
|
56
|
+
{
|
|
57
|
+
url: `redis://${config.username}:${config.username}@${config.hostname}:6379`,
|
|
58
|
+
disableOfflineQueue: true,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
yield connection.connect();
|
|
63
|
+
return connection;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
_a = Redis;
|
|
68
|
+
Redis.singleton = new AsyncSingleton(_a.connectionFactory, (c) => __awaiter(void 0, void 0, void 0, function* () { return c.isOpen; }));
|
|
69
|
+
/**
|
|
70
|
+
* A private static property that holds a reference to the redis.createClient function.
|
|
71
|
+
* This property is used to create client.
|
|
72
|
+
*/
|
|
73
|
+
Redis.ClientFactory = createClient;
|
|
74
|
+
/**
|
|
75
|
+
* A private static property that references the createCluster function.
|
|
76
|
+
* This property can be used to create clusters within the class.
|
|
77
|
+
*/
|
|
78
|
+
Redis.ClusterFactory = createCluster;
|
|
79
|
+
export default Redis;
|
|
80
|
+
//# sourceMappingURL=Redis.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Redis.js","sourceRoot":"","sources":["../../../src/Cache/Redis.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAqC,aAAa,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAGtF,OAAO,cAAc,MAAM,2BAA2B,CAAA;AACtD;;GAEG;AACH,MAAqB,KAAK;IAkBjB,MAAM,CAAC,UAAU,CACtB,MAA4B;QAE5B,OAAO,EAAK,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACzC,CAAC;IAEO,MAAM,CAAO,iBAAiB,CACpC,MAA4B;;YAE5B,IAAI,MAAM,CAAC,WAAW;gBAAE,OAAO,EAAK,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAA;;gBAC9D,OAAO,EAAK,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAA;QACjD,CAAC;KAAA;IAED;;;;;;OAMG;IACK,MAAM,CAAO,qBAAqB,CACxC,MAA4B;;YAE5B,OAAO,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAA;YACxD,MAAM,UAAU,GAAG,EAAK,CAAC,aAAa,+BACpC,QAAQ,EAAE,MAAM,CAAC,QAAQ,IACtB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,KACzD,mBAAmB,EAAE,IAAI,EACzB,MAAM,EAAE;oBACN,IAAI,EAAE,MAAM,CAAC,QAAQ;oBACrB,GAAG,EAAE,MAAM,CAAC,SAAS;oBACrB,cAAc,EAAE,KAAK;iBACtB,IACD,CAAA;YACF,MAAM,UAAU,CAAC,OAAO,EAAE,CAAA;YAE1B,OAAO,UAAiB,CAAA;QAC1B,CAAC;KAAA;IAEO,MAAM,CAAO,sBAAsB,CACzC,MAA4B;;YAE5B,OAAO,CAAC,KAAK,CAAC,0CAA0C,CAAC,CAAA;YACzD,MAAM,UAAU,GAAG,EAAK,CAAC,cAAc,CAAC;gBACtC,QAAQ,gCACN,QAAQ,EAAE,MAAM,CAAC,QAAQ,IACtB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,KACzD,MAAM,EAAE;wBACN,GAAG,EAAE,MAAM,CAAC,SAAS;wBACrB,cAAc,EAAE,KAAK;qBACtB,GACF;gBACD,SAAS,EAAE;oBACT;wBACE,GAAG,EAAE,WAAW,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,OAAO;wBAC5E,mBAAmB,EAAE,IAAI;qBAC1B;iBACF;aACF,CAAC,CAAA;YACF,MAAM,UAAU,CAAC,OAAO,EAAE,CAAA;YAE1B,OAAO,UAAiB,CAAA;QAC1B,CAAC;KAAA;;;AA/Ec,eAAS,GAAG,IAAI,cAAc,CAG3C,EAAK,CAAC,iBAAiB,EAAE,CAAM,CAAC,EAAC,EAAE,kDAAC,OAAA,CAAC,CAAC,MAAM,CAAA,GAAA,CAAC,AAHvB,CAGuB;AAE/C;;;GAGG;AACY,mBAAa,GAAG,YAAY,AAAf,CAAe;AAE3C;;;GAGG;AACY,oBAAc,GAAG,aAAa,AAAhB,CAAgB;eAhB1B,KAAK"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import Redis from './Redis.js';
|
|
2
|
+
/**
|
|
3
|
+
* Defines a type alias for specifying the cache type as 'redis'.
|
|
4
|
+
*/
|
|
5
|
+
export type CacheType = 'redis';
|
|
6
|
+
/**
|
|
7
|
+
* Defines a CacheClass type that has a 'redis' property which is a constructor function
|
|
8
|
+
* that takes in any number of arguments and returns an instance of Redis.
|
|
9
|
+
*/
|
|
10
|
+
export type CacheClass = {
|
|
11
|
+
redis: new (...args: any[]) => Redis;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Represents the base configuration for a cache, including host, username, password, and TLS settings.
|
|
15
|
+
*/
|
|
16
|
+
export type CacheBaseConfig = {
|
|
17
|
+
hostname: string;
|
|
18
|
+
username: string;
|
|
19
|
+
password?: string;
|
|
20
|
+
enableTLS: boolean;
|
|
21
|
+
clusterMode?: boolean;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Defines a CacheConfig type that extends CacheBaseConfig and includes a specific cache type.
|
|
25
|
+
* @template S - The specific cache type to be included in the CacheConfig.
|
|
26
|
+
* @extends CacheBaseConfig
|
|
27
|
+
* @property {S} type - The specific cache type included in the CacheConfig.
|
|
28
|
+
*/
|
|
29
|
+
export type CacheConfig<S extends CacheType> = CacheBaseConfig & {
|
|
30
|
+
type: S;
|
|
31
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/Cache/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the schema for the configuration object.
|
|
3
|
+
* @typedef {Object} ConfigurationSchema
|
|
4
|
+
* @property {Object} [name] - The name of the configuration property.
|
|
5
|
+
* @property {boolean} [name.isLocal] - Indicates if the property is for local use.
|
|
6
|
+
* @property {boolean} [name.isRemote] - Indicates if the property is for remote use.
|
|
7
|
+
* @property {boolean} [name.required] - Indicates if the property is required.
|
|
8
|
+
* @property {string} [name.cachingPolicy] - The caching policy for the property.
|
|
9
|
+
* @property {string} [name.nameOverride] - The optional name override if the key differs
|
|
10
|
+
*/
|
|
11
|
+
export type ConfigurationSchema = {
|
|
12
|
+
[name: string]: {
|
|
13
|
+
isLocal?: boolean;
|
|
14
|
+
isRemote?: boolean;
|
|
15
|
+
isSecure?: boolean;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
noPrefix?: boolean;
|
|
18
|
+
cachingPolicy?: string;
|
|
19
|
+
nameOverride?: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Extracts the properties from a given type that have the `isRemote` property set to `true`.
|
|
24
|
+
* @param {Type} - The type to extract properties from.
|
|
25
|
+
* @returns An object type with the extracted properties.
|
|
26
|
+
*/
|
|
27
|
+
type ExtractRemote<Type> = {
|
|
28
|
+
[Property in keyof Type]-?: Type[Property] extends {
|
|
29
|
+
isRemote: true;
|
|
30
|
+
} ? Property : null;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Extracts the properties from a given type that have the `isLocal` property set to `true`.
|
|
34
|
+
* @typeparam Type - The type to extract properties from.
|
|
35
|
+
* @returns An object type with the extracted properties as keys and their corresponding types.
|
|
36
|
+
*/
|
|
37
|
+
type ExtractLocal<Type> = {
|
|
38
|
+
[Property in keyof Type]-?: Type[Property] extends {
|
|
39
|
+
isLocal: true;
|
|
40
|
+
} ? Property : null;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new type by omitting keys from type T whose values are of type U.
|
|
44
|
+
* @param {T} - The original type
|
|
45
|
+
* @param {U} - The type to omit from T
|
|
46
|
+
* @returns A new type with the omitted keys
|
|
47
|
+
*/
|
|
48
|
+
type OmitKeysByValueType<T, U> = {
|
|
49
|
+
[P in keyof T]: T[P] extends U ? never : P;
|
|
50
|
+
}[keyof T];
|
|
51
|
+
/**
|
|
52
|
+
* Omit properties from an object type by their value type.
|
|
53
|
+
* @template T - The object type to omit properties from.
|
|
54
|
+
* @template V - The value type of properties to omit.
|
|
55
|
+
* @typedef OmitByValueType
|
|
56
|
+
* @property {T} T - The object type to omit properties from.
|
|
57
|
+
* @property {V} V - The value type of properties to omit.
|
|
58
|
+
* @returns {OmitKeysByValueType<T, V>} - The resulting object type after omitting properties.
|
|
59
|
+
*/
|
|
60
|
+
type OmitByValueType<T, V> = T extends infer _ ? {
|
|
61
|
+
[key in OmitKeysByValueType<T, V>]: T[key];
|
|
62
|
+
} : never;
|
|
63
|
+
/**
|
|
64
|
+
* Represents a configuration object with a specified schema and remote prefix.
|
|
65
|
+
* @template T - The type of the configuration schema.
|
|
66
|
+
*/
|
|
67
|
+
export default class Configuration<T extends ConfigurationSchema> {
|
|
68
|
+
/**
|
|
69
|
+
* The schema for the private readonly property.
|
|
70
|
+
* @type {T}
|
|
71
|
+
*/
|
|
72
|
+
private readonly schema;
|
|
73
|
+
/**
|
|
74
|
+
* The prefix used for remote resources.
|
|
75
|
+
* @type {string}
|
|
76
|
+
*/
|
|
77
|
+
private readonly remotePrefix;
|
|
78
|
+
/**
|
|
79
|
+
* Indicates if config is local and should attempt to override
|
|
80
|
+
* remote values from local.
|
|
81
|
+
* @type {string}
|
|
82
|
+
*/
|
|
83
|
+
private static readonly isLocal;
|
|
84
|
+
/**
|
|
85
|
+
* Constructs a new instance of the class.
|
|
86
|
+
* @param {T} schema - The schema object.
|
|
87
|
+
* @param {string} remotePrefix - The remote prefix string.
|
|
88
|
+
* @returns None
|
|
89
|
+
*/
|
|
90
|
+
constructor(schema: T, remotePrefix: string);
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves the value of a property from the environment variables or cache.
|
|
93
|
+
* @param {keyof OmitByValueType<ExtractLocal<T>, null>} propName - The name of the property to retrieve.
|
|
94
|
+
* @returns The value of the property.
|
|
95
|
+
*/
|
|
96
|
+
get(propName: keyof OmitByValueType<ExtractLocal<T>, null>): any;
|
|
97
|
+
/**
|
|
98
|
+
* Asynchronously retrieves the value of a property from the remote environment.
|
|
99
|
+
* @param {keyof OmitByValueType<ExtractRemote<T>, null>} propName - The name of the property to retrieve.
|
|
100
|
+
* @returns {Promise<any>} - A promise that resolves to the value of the property.
|
|
101
|
+
*/
|
|
102
|
+
asyncGet(propName: keyof OmitByValueType<ExtractRemote<T>, null>): Promise<any>;
|
|
103
|
+
/**
|
|
104
|
+
* Retrieves the cached value associated with the given key from the cache store.
|
|
105
|
+
* @param {string} valueKey - The key of the value to retrieve from the cache.
|
|
106
|
+
* @returns The cached value associated with the given key, or undefined if the key does not exist in the cache.
|
|
107
|
+
*/
|
|
108
|
+
private getCachedValue;
|
|
109
|
+
/**
|
|
110
|
+
* Caches a value with the specified key and expiration policy.
|
|
111
|
+
* @param {string} valueKey - The key to associate with the cached value.
|
|
112
|
+
* @param {any} value - The value to be cached.
|
|
113
|
+
* @param {string} [policy='1d'] - The expiration policy for the cached value. Defaults to '1d' (1 day).
|
|
114
|
+
* @returns None
|
|
115
|
+
*/
|
|
116
|
+
private cacheValue;
|
|
117
|
+
/**
|
|
118
|
+
* Resets the cache by creating a new instance of the MemCache class and assigning it to the cacheStore variable.
|
|
119
|
+
* @returns None
|
|
120
|
+
*/
|
|
121
|
+
private resetCache;
|
|
122
|
+
}
|
|
123
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import MemCache from 'node-cache';
|
|
11
|
+
import DurationParser from 'parse-duration';
|
|
12
|
+
import EnvironmentVar, { EnvironmentType } from './EnvironmentVar.js';
|
|
13
|
+
// Hold cached values at nodeVM level
|
|
14
|
+
// eslint-disable-next-line no-var
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new instance of a memory cache store.
|
|
17
|
+
* @returns {MemCache} A new instance of a memory cache store.
|
|
18
|
+
*/
|
|
19
|
+
let cacheStore = new MemCache();
|
|
20
|
+
/**
|
|
21
|
+
* Represents a configuration object with a specified schema and remote prefix.
|
|
22
|
+
* @template T - The type of the configuration schema.
|
|
23
|
+
*/
|
|
24
|
+
class Configuration {
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a new instance of the class.
|
|
27
|
+
* @param {T} schema - The schema object.
|
|
28
|
+
* @param {string} remotePrefix - The remote prefix string.
|
|
29
|
+
* @returns None
|
|
30
|
+
*/
|
|
31
|
+
constructor(schema, remotePrefix) {
|
|
32
|
+
this.schema = schema;
|
|
33
|
+
this.remotePrefix = remotePrefix;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the value of a property from the environment variables or cache.
|
|
37
|
+
* @param {keyof OmitByValueType<ExtractLocal<T>, null>} propName - The name of the property to retrieve.
|
|
38
|
+
* @returns The value of the property.
|
|
39
|
+
*/
|
|
40
|
+
get(propName) {
|
|
41
|
+
const propString = propName;
|
|
42
|
+
const propSchema = this.schema[propString];
|
|
43
|
+
let v = this.getCachedValue(propString);
|
|
44
|
+
v =
|
|
45
|
+
v ||
|
|
46
|
+
new EnvironmentVar(propSchema.nameOverride || propString, EnvironmentType.Local, !propSchema.required, !propSchema.noPrefix ? this.remotePrefix : '').syncResolve();
|
|
47
|
+
this.cacheValue(propString, v, propSchema.cachingPolicy);
|
|
48
|
+
return v;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Asynchronously retrieves the value of a property from the remote environment.
|
|
52
|
+
* @param {keyof OmitByValueType<ExtractRemote<T>, null>} propName - The name of the property to retrieve.
|
|
53
|
+
* @returns {Promise<any>} - A promise that resolves to the value of the property.
|
|
54
|
+
*/
|
|
55
|
+
asyncGet(propName) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const propString = propName;
|
|
58
|
+
const propSchema = this.schema[propString];
|
|
59
|
+
// check cache
|
|
60
|
+
let v = this.getCachedValue(propString);
|
|
61
|
+
if (v !== undefined)
|
|
62
|
+
return v;
|
|
63
|
+
// check for local override
|
|
64
|
+
if (Configuration.isLocal && process.env[propString] !== undefined) {
|
|
65
|
+
console.log(`Overriding remote variable ${propString} with local value!`);
|
|
66
|
+
const v = process.env[propString];
|
|
67
|
+
this.cacheValue(propString, v, propSchema.cachingPolicy);
|
|
68
|
+
return v;
|
|
69
|
+
}
|
|
70
|
+
// remote
|
|
71
|
+
v = yield new EnvironmentVar(propSchema.nameOverride || propString, propSchema.isSecure ? EnvironmentType.SecureRemote : EnvironmentType.PlainRemote, !propSchema.required, !propSchema.noPrefix ? this.remotePrefix : '').resolve();
|
|
72
|
+
this.cacheValue(propString, v, propSchema.cachingPolicy);
|
|
73
|
+
return v;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Retrieves the cached value associated with the given key from the cache store.
|
|
78
|
+
* @param {string} valueKey - The key of the value to retrieve from the cache.
|
|
79
|
+
* @returns The cached value associated with the given key, or undefined if the key does not exist in the cache.
|
|
80
|
+
*/
|
|
81
|
+
getCachedValue(valueKey) {
|
|
82
|
+
return cacheStore.get(valueKey);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Caches a value with the specified key and expiration policy.
|
|
86
|
+
* @param {string} valueKey - The key to associate with the cached value.
|
|
87
|
+
* @param {any} value - The value to be cached.
|
|
88
|
+
* @param {string} [policy='1d'] - The expiration policy for the cached value. Defaults to '1d' (1 day).
|
|
89
|
+
* @returns None
|
|
90
|
+
*/
|
|
91
|
+
cacheValue(valueKey, value, policy = '1d') {
|
|
92
|
+
cacheStore.set(valueKey, value, DurationParser(policy, 's') || '');
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resets the cache by creating a new instance of the MemCache class and assigning it to the cacheStore variable.
|
|
96
|
+
* @returns None
|
|
97
|
+
*/
|
|
98
|
+
resetCache() {
|
|
99
|
+
cacheStore = new MemCache();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Indicates if config is local and should attempt to override
|
|
104
|
+
* remote values from local.
|
|
105
|
+
* @type {string}
|
|
106
|
+
*/
|
|
107
|
+
Configuration.isLocal = process.env.NODE_ENV == 'local';
|
|
108
|
+
export default Configuration;
|
|
109
|
+
//# sourceMappingURL=Configuration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Configuration.js","sourceRoot":"","sources":["../../../src/Config/Configuration.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,cAAc,MAAM,gBAAgB,CAAA;AAE3C,OAAO,cAAc,EAAE,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErE,qCAAqC;AACrC,kCAAkC;AAClC;;;GAGG;AACH,IAAI,UAAU,GAAG,IAAI,QAAQ,EAAE,CAAA;AA6E/B;;;GAGG;AACH,MAAqB,aAAa;IAkBhC;;;;;OAKG;IACH,YAAY,MAAS,EAAE,YAAoB;QACzC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA;IAClC,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,QAAsD;QAC/D,MAAM,UAAU,GAAG,QAAkB,CAAA;QACrC,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;QAC1C,IAAI,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,CAAA;QACvC,CAAC;YACC,CAAC;gBACD,IAAI,cAAc,CAChB,UAAU,CAAC,YAAY,IAAI,UAAU,EACrC,eAAe,CAAC,KAAK,EACrB,CAAC,UAAU,CAAC,QAAQ,EACpB,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAC9C,C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ACV,CAAC;KAAA;IAED;;;;OAIG;IACK,cAAc,CAAC,QAAgB;QACrC,OAAO,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IACjC,CAAC;IAED;;;;;;OAMG;IACK,UAAU,CAAC,QAAgB,EAAE,KAAU,EAAE,SAAiB,IAAI;QACpE,UAAU,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;IACpE,CAAC;IAED;;;OAGG;IACK,UAAU;QAChB,UAAU,GAAG,IAAI,QAAQ,EAAE,CAAA;IAC7B,CAAC;;AA9FD;;;;GAIG;AACqB,qBAAO,GAAY,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAA;eAhBvD,aAAa"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An enumeration representing different types of environments.
|
|
3
|
+
* @enum {number}
|
|
4
|
+
* @readonly
|
|
5
|
+
* @property {number} PlainRemote - Represents a plain remote environment.
|
|
6
|
+
* @property {number} SecureRemote - Represents a secure remote environment.
|
|
7
|
+
* @property {number} Local - Represents a local environment.
|
|
8
|
+
*/
|
|
9
|
+
export declare enum EnvironmentType {
|
|
10
|
+
PlainRemote = 0,
|
|
11
|
+
SecureRemote = 1,
|
|
12
|
+
Local = 2
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Represents an environment variable with methods to resolve its value based on the environment type.
|
|
16
|
+
* @template T - The type of the environment variable value.
|
|
17
|
+
*/
|
|
18
|
+
export default class EnvironmentVar<T> {
|
|
19
|
+
/**
|
|
20
|
+
* The name of the parameter.
|
|
21
|
+
*/
|
|
22
|
+
private readonly paramName;
|
|
23
|
+
/**
|
|
24
|
+
* The type of environment.
|
|
25
|
+
* @readonly
|
|
26
|
+
*/
|
|
27
|
+
private readonly type;
|
|
28
|
+
/**
|
|
29
|
+
* A boolean flag indicating whether the field is optional or not.
|
|
30
|
+
*/
|
|
31
|
+
private readonly optional;
|
|
32
|
+
/**
|
|
33
|
+
* The prefix used for parameters in the class.
|
|
34
|
+
* @type {string}
|
|
35
|
+
*/
|
|
36
|
+
private readonly paramPrefix;
|
|
37
|
+
/**
|
|
38
|
+
* Constructs a new instance of the EnvironmentVar class.
|
|
39
|
+
* @param {string} paramName - The name of the environment variable.
|
|
40
|
+
* @param {EnvironmentType} type - The type of the environment.
|
|
41
|
+
* @param {boolean} [optional] - Indicates whether the environment variable is optional.
|
|
42
|
+
* @param {string} [paramPrefix] - The prefix to be added to the environment variable name.
|
|
43
|
+
*/
|
|
44
|
+
constructor(paramName: string, type: EnvironmentType, optional: boolean | undefined, paramPrefix: string);
|
|
45
|
+
/**
|
|
46
|
+
* Resolves the value of the environment variable synchronously.
|
|
47
|
+
* @returns {T} - The resolved value of the environment variable.
|
|
48
|
+
* @throws {Error} - If the environment type is not 'Local'.
|
|
49
|
+
*/
|
|
50
|
+
syncResolve(): T;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the value based on the environment type.
|
|
53
|
+
* @returns {Promise<any>} - The resolved value.
|
|
54
|
+
*/
|
|
55
|
+
resolve(): Promise<T | undefined>;
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves the value of a local environment variable.
|
|
58
|
+
* @returns {T} - The value of the local environment variable.
|
|
59
|
+
* @throws {Error} - If the local environment variable is not defined and is not optional.
|
|
60
|
+
*/
|
|
61
|
+
private getLocalValue;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the plain value of a remote environment parameter.
|
|
64
|
+
* @returns {Promise<T>} - A promise that resolves to the plain value of the parameter.
|
|
65
|
+
* @throws {Error} - If the parameter cannot be retrieved and is not optional.
|
|
66
|
+
*/
|
|
67
|
+
private getPlainValue;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves a secure value from the secrets manager.
|
|
70
|
+
* @returns {Promise<T>} - A promise that resolves to the secure value.
|
|
71
|
+
* @throws {Error} - Throws an error if the secure value cannot be retrieved and is not optional.
|
|
72
|
+
*/
|
|
73
|
+
private getSecureValue;
|
|
74
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
11
|
+
import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm';
|
|
12
|
+
// Explicity setting VM level variables in order to persist
|
|
13
|
+
// client across different important and instances.
|
|
14
|
+
// eslint-disable-next-line no-var
|
|
15
|
+
/**
|
|
16
|
+
* Variables to hold instances of SSMClient and SecretsManagerClient, initialized as null.
|
|
17
|
+
* @type {SSMClient | null} ssmClient - Instance of SSMClient or null if not initialized.
|
|
18
|
+
* @type {SecretsManagerClient | null} secretsClient - Instance of SecretsManagerClient or null if not initialized.
|
|
19
|
+
*/
|
|
20
|
+
let ssmClient = null, secretsClient = null;
|
|
21
|
+
/**
|
|
22
|
+
* An enumeration representing different types of environments.
|
|
23
|
+
* @enum {number}
|
|
24
|
+
* @readonly
|
|
25
|
+
* @property {number} PlainRemote - Represents a plain remote environment.
|
|
26
|
+
* @property {number} SecureRemote - Represents a secure remote environment.
|
|
27
|
+
* @property {number} Local - Represents a local environment.
|
|
28
|
+
*/
|
|
29
|
+
export var EnvironmentType;
|
|
30
|
+
(function (EnvironmentType) {
|
|
31
|
+
EnvironmentType[EnvironmentType["PlainRemote"] = 0] = "PlainRemote";
|
|
32
|
+
EnvironmentType[EnvironmentType["SecureRemote"] = 1] = "SecureRemote";
|
|
33
|
+
EnvironmentType[EnvironmentType["Local"] = 2] = "Local";
|
|
34
|
+
})(EnvironmentType || (EnvironmentType = {}));
|
|
35
|
+
/**
|
|
36
|
+
* Represents an environment variable with methods to resolve its value based on the environment type.
|
|
37
|
+
* @template T - The type of the environment variable value.
|
|
38
|
+
*/
|
|
39
|
+
export default class EnvironmentVar {
|
|
40
|
+
/**
|
|
41
|
+
* Constructs a new instance of the EnvironmentVar class.
|
|
42
|
+
* @param {string} paramName - The name of the environment variable.
|
|
43
|
+
* @param {EnvironmentType} type - The type of the environment.
|
|
44
|
+
* @param {boolean} [optional] - Indicates whether the environment variable is optional.
|
|
45
|
+
* @param {string} [paramPrefix] - The prefix to be added to the environment variable name.
|
|
46
|
+
*/
|
|
47
|
+
constructor(paramName, type, optional = true, paramPrefix) {
|
|
48
|
+
this.paramName = paramName;
|
|
49
|
+
this.type = type;
|
|
50
|
+
this.optional = !!optional;
|
|
51
|
+
this.paramPrefix = paramPrefix;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Resolves the value of the environment variable synchronously.
|
|
55
|
+
* @returns {T} - The resolved value of the environment variable.
|
|
56
|
+
* @throws {Error} - If the environment type is not 'Local'.
|
|
57
|
+
*/
|
|
58
|
+
syncResolve() {
|
|
59
|
+
if (this.type == EnvironmentType.Local)
|
|
60
|
+
return this.getLocalValue();
|
|
61
|
+
else {
|
|
62
|
+
throw new Error("EnvironmentVar syncResolve method can only be called for environments of type 'Local'");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolves the value based on the environment type.
|
|
67
|
+
* @returns {Promise<any>} - The resolved value.
|
|
68
|
+
*/
|
|
69
|
+
resolve() {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
if (this.type == EnvironmentType.Local)
|
|
72
|
+
return this.getLocalValue();
|
|
73
|
+
else if (this.type == EnvironmentType.SecureRemote)
|
|
74
|
+
return this.getSecureValue();
|
|
75
|
+
else
|
|
76
|
+
return this.getPlainValue();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Retrieves the value of a local environment variable.
|
|
81
|
+
* @returns {T} - The value of the local environment variable.
|
|
82
|
+
* @throws {Error} - If the local environment variable is not defined and is not optional.
|
|
83
|
+
*/
|
|
84
|
+
getLocalValue() {
|
|
85
|
+
const value = process.env[this.paramName];
|
|
86
|
+
if (!value && !this.optional) {
|
|
87
|
+
throw new Error(`Local environment ${this.paramName} is not defined, please reconfigure the application and redeploy!`);
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves the plain value of a remote environment parameter.
|
|
93
|
+
* @returns {Promise<T>} - A promise that resolves to the plain value of the parameter.
|
|
94
|
+
* @throws {Error} - If the parameter cannot be retrieved and is not optional.
|
|
95
|
+
*/
|
|
96
|
+
getPlainValue() {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
if (!ssmClient)
|
|
99
|
+
ssmClient = new SSMClient();
|
|
100
|
+
const pName = `${this.paramPrefix}${this.paramName}`;
|
|
101
|
+
try {
|
|
102
|
+
const data = yield ssmClient.send(new GetParameterCommand({ Name: pName }));
|
|
103
|
+
if (data.Parameter && data.Parameter.Value)
|
|
104
|
+
return data.Parameter.Value;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
console.error(error);
|
|
108
|
+
}
|
|
109
|
+
if (!this.optional) {
|
|
110
|
+
throw new Error(`Unable to retrieve plain remote env value: ${pName}`);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Retrieves a secure value from the secrets manager.
|
|
116
|
+
* @returns {Promise<T>} - A promise that resolves to the secure value.
|
|
117
|
+
* @throws {Error} - Throws an error if the secure value cannot be retrieved and is not optional.
|
|
118
|
+
*/
|
|
119
|
+
getSecureValue() {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
if (!secretsClient)
|
|
122
|
+
secretsClient = new SecretsManagerClient();
|
|
123
|
+
const pName = `${this.paramPrefix}${this.paramName}`;
|
|
124
|
+
try {
|
|
125
|
+
const data = yield secretsClient.send(new GetSecretValueCommand({ SecretId: pName }));
|
|
126
|
+
if (data.SecretString)
|
|
127
|
+
return data.SecretString;
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.error(error);
|
|
131
|
+
}
|
|
132
|
+
if (!this.optional) {
|
|
133
|
+
throw new Error(`Unable to retrieve secure remote env: ${pName}`);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=EnvironmentVar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentVar.js","sourceRoot":"","sources":["../../../src/Config/EnvironmentVar.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA;AAC7F,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAEpE,2DAA2D;AAC3D,mDAAmD;AACnD,kCAAkC;AAClC;;;;GAIG;AACH,IAAI,SAAS,GAAqB,IAAI,EACpC,aAAa,GAAgC,IAAI,CAAA;AAEnD;;;;;;;GAOG;AACH,MAAM,CAAN,IAAY,eAIX;AAJD,WAAY,eAAe;IACzB,mEAAW,CAAA;IACX,qEAAY,CAAA;IACZ,uDAAK,CAAA;AACP,CAAC,EAJW,eAAe,KAAf,eAAe,QAI1B;AACD;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IAoBjC;;;;;;OAMG;IACH,YAAY,SAAiB,EAAE,IAAqB,EAAE,QAAQ,G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functionality using AWS KMS.
|
|
4
|
+
*/
|
|
5
|
+
export default class Crypto {
|
|
6
|
+
/**
|
|
7
|
+
* The KMSClient instance used for making requests to the Key Management Service (KMS).
|
|
8
|
+
*/
|
|
9
|
+
private readonly client;
|
|
10
|
+
/**
|
|
11
|
+
* The AWS region of the object. This property is readonly and cannot be modified once set.
|
|
12
|
+
*/
|
|
13
|
+
private readonly region;
|
|
14
|
+
/**
|
|
15
|
+
* The unique identifier for the KMS.
|
|
16
|
+
* @readonly
|
|
17
|
+
* @type {string}
|
|
18
|
+
*/
|
|
19
|
+
private readonly keyId;
|
|
20
|
+
/**
|
|
21
|
+
* Specifies the encryption algorithm used for encryption.
|
|
22
|
+
*/
|
|
23
|
+
private readonly encryptionAlgorithm;
|
|
24
|
+
/**
|
|
25
|
+
* Constructs a new instance of a KeyManager with the provided region, keyId, and optional encryption algorithm.
|
|
26
|
+
* @param {string} region - The region where the key is stored.
|
|
27
|
+
* @param {string} keyId - The ID of the key.
|
|
28
|
+
* @param {EncryptionAlgorithmSpec} [optEncryptionAlgorithm='RSAES_OAEP_SHA_256'] - The encryption algorithm to use (default is RSAES_OAEP_SHA_256).
|
|
29
|
+
* @returns None
|
|
30
|
+
*/
|
|
31
|
+
constructor(region: string, keyId: string, optEncryptionAlgorithm?: EncryptionAlgorithmSpec);
|
|
32
|
+
/**
|
|
33
|
+
* Encrypts the given data using RSAES_OAEP_SHA_256 encryption algorithm.
|
|
34
|
+
* @param {string | any} data - The data to be encrypted.
|
|
35
|
+
* @returns {Promise<string | null>} - A promise that resolves to the encrypted data as a hexadecimal string.
|
|
36
|
+
*/
|
|
37
|
+
encryptData(data: string | any): Promise<string | null>;
|
|
38
|
+
/**
|
|
39
|
+
* Decrypts the given data using the RSAES_OAEP_SHA_256 encryption algorithm.
|
|
40
|
+
* @param {string} data - The encrypted data to decrypt.
|
|
41
|
+
* @returns {Promise<string | null>} - A promise that resolves to the decrypted plaintext string.
|
|
42
|
+
* If decryption fails, null is returned.
|
|
43
|
+
*/
|
|
44
|
+
decryptData(data: string): Promise<string | null>;
|
|
45
|
+
}
|