@ikonintegration/ikapi 5.0.2 → 5.0.4
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/package.json +1 -1
- package/src/BaseEvent/Launchable/ContainerExec.ts +6 -2
- package/src/Mailer/Mailer.ts +42 -52
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -28
- package/dist/index.js.map +0 -1
- package/dist/package-lock.json +0 -11901
- package/dist/package.json +0 -82
- package/dist/src/API/Request.d.ts +0 -125
- package/dist/src/API/Request.js +0 -185
- package/dist/src/API/Request.js.map +0 -1
- package/dist/src/API/Response.d.ts +0 -188
- package/dist/src/API/Response.js +0 -270
- package/dist/src/API/Response.js.map +0 -1
- package/dist/src/BaseEvent/DynamoTransaction.d.ts +0 -70
- package/dist/src/BaseEvent/DynamoTransaction.js +0 -104
- package/dist/src/BaseEvent/DynamoTransaction.js.map +0 -1
- package/dist/src/BaseEvent/EventProcessor.d.ts +0 -58
- package/dist/src/BaseEvent/EventProcessor.js +0 -101
- package/dist/src/BaseEvent/EventProcessor.js.map +0 -1
- package/dist/src/BaseEvent/Launchable/ContainerExec.d.ts +0 -42
- package/dist/src/BaseEvent/Launchable/ContainerExec.js +0 -102
- package/dist/src/BaseEvent/Launchable/ContainerExec.js.map +0 -1
- package/dist/src/BaseEvent/Launchable/index.d.ts +0 -34
- package/dist/src/BaseEvent/Launchable/index.js +0 -43
- package/dist/src/BaseEvent/Launchable/index.js.map +0 -1
- package/dist/src/BaseEvent/Launchable/types.d.ts +0 -10
- package/dist/src/BaseEvent/Launchable/types.js +0 -2
- package/dist/src/BaseEvent/Launchable/types.js.map +0 -1
- package/dist/src/BaseEvent/Process.d.ts +0 -50
- package/dist/src/BaseEvent/Process.js +0 -64
- package/dist/src/BaseEvent/Process.js.map +0 -1
- package/dist/src/BaseEvent/StepTransaction.d.ts +0 -23
- package/dist/src/BaseEvent/StepTransaction.js +0 -27
- package/dist/src/BaseEvent/StepTransaction.js.map +0 -1
- package/dist/src/BaseEvent/Transaction.d.ts +0 -149
- package/dist/src/BaseEvent/Transaction.js +0 -224
- package/dist/src/BaseEvent/Transaction.js.map +0 -1
- package/dist/src/Cache/Redis.d.ts +0 -29
- package/dist/src/Cache/Redis.js +0 -80
- package/dist/src/Cache/Redis.js.map +0 -1
- package/dist/src/Cache/types.d.ts +0 -31
- package/dist/src/Cache/types.js +0 -2
- package/dist/src/Cache/types.js.map +0 -1
- package/dist/src/Config/Configuration.d.ts +0 -123
- package/dist/src/Config/Configuration.js +0 -109
- package/dist/src/Config/Configuration.js.map +0 -1
- package/dist/src/Config/EnvironmentVar.d.ts +0 -74
- package/dist/src/Config/EnvironmentVar.js +0 -138
- package/dist/src/Config/EnvironmentVar.js.map +0 -1
- package/dist/src/Crypto/Crypto.d.ts +0 -45
- package/dist/src/Crypto/Crypto.js +0 -72
- package/dist/src/Crypto/Crypto.js.map +0 -1
- package/dist/src/Database/Database.d.ts +0 -21
- package/dist/src/Database/Database.js +0 -15
- package/dist/src/Database/Database.js.map +0 -1
- package/dist/src/Database/DatabaseManager.d.ts +0 -47
- package/dist/src/Database/DatabaseManager.js +0 -60
- package/dist/src/Database/DatabaseManager.js.map +0 -1
- package/dist/src/Database/DatabaseTransaction.d.ts +0 -101
- package/dist/src/Database/DatabaseTransaction.js +0 -126
- package/dist/src/Database/DatabaseTransaction.js.map +0 -1
- package/dist/src/Database/index.d.ts +0 -10
- package/dist/src/Database/index.js +0 -15
- package/dist/src/Database/index.js.map +0 -1
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +0 -35
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +0 -59
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +0 -1
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +0 -66
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +0 -86
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +0 -1
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +0 -70
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +0 -118
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +0 -1
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +0 -36
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +0 -54
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +0 -1
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +0 -63
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +0 -61
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +0 -1
- package/dist/src/Database/types.d.ts +0 -76
- package/dist/src/Database/types.js +0 -2
- package/dist/src/Database/types.js.map +0 -1
- package/dist/src/Globals.d.ts +0 -93
- package/dist/src/Globals.js +0 -99
- package/dist/src/Globals.js.map +0 -1
- package/dist/src/Logger/Logger.d.ts +0 -161
- package/dist/src/Logger/Logger.js +0 -299
- package/dist/src/Logger/Logger.js.map +0 -1
- package/dist/src/Mailer/Mailer.d.ts +0 -78
- package/dist/src/Mailer/Mailer.js +0 -182
- package/dist/src/Mailer/Mailer.js.map +0 -1
- package/dist/src/Publisher/Publisher.d.ts +0 -39
- package/dist/src/Publisher/Publisher.js +0 -77
- package/dist/src/Publisher/Publisher.js.map +0 -1
- package/dist/src/Server/RouteResolver.d.ts +0 -33
- package/dist/src/Server/RouteResolver.js +0 -100
- package/dist/src/Server/RouteResolver.js.map +0 -1
- package/dist/src/Server/Router.d.ts +0 -157
- package/dist/src/Server/Router.js +0 -32
- package/dist/src/Server/Router.js.map +0 -1
- package/dist/src/Server/lib/ContainerServer.d.ts +0 -42
- package/dist/src/Server/lib/ContainerServer.js +0 -66
- package/dist/src/Server/lib/ContainerServer.js.map +0 -1
- package/dist/src/Server/lib/Server.d.ts +0 -45
- package/dist/src/Server/lib/Server.js +0 -93
- package/dist/src/Server/lib/Server.js.map +0 -1
- package/dist/src/Server/lib/container/GenericHandler.d.ts +0 -9
- package/dist/src/Server/lib/container/GenericHandler.js +0 -82
- package/dist/src/Server/lib/container/GenericHandler.js.map +0 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +0 -52
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +0 -132
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +0 -1
- package/dist/src/Server/lib/container/HealthHandler.d.ts +0 -9
- package/dist/src/Server/lib/container/HealthHandler.js +0 -19
- package/dist/src/Server/lib/container/HealthHandler.js.map +0 -1
- package/dist/src/Server/lib/container/Proxy.d.ts +0 -67
- package/dist/src/Server/lib/container/Proxy.js +0 -143
- package/dist/src/Server/lib/container/Proxy.js.map +0 -1
- package/dist/src/Server/lib/container/Utils.d.ts +0 -14
- package/dist/src/Server/lib/container/Utils.js +0 -37
- package/dist/src/Server/lib/container/Utils.js.map +0 -1
- package/dist/src/Util/AsyncSingleton.d.ts +0 -31
- package/dist/src/Util/AsyncSingleton.js +0 -83
- package/dist/src/Util/AsyncSingleton.js.map +0 -1
- package/dist/src/Util/Utils.d.ts +0 -61
- package/dist/src/Util/Utils.js +0 -147
- package/dist/src/Util/Utils.js.map +0 -1
- package/dist/src/Validation/Validator.d.ts +0 -17
- package/dist/src/Validation/Validator.js +0 -39
- package/dist/src/Validation/Validator.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -1,224 +0,0 @@
|
|
|
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 Request from '../API/Request.js';
|
|
11
|
-
import Response from '../API/Response.js';
|
|
12
|
-
import { DatabaseManager } from '../Database/DatabaseManager.js';
|
|
13
|
-
import Globals from '../Globals.js';
|
|
14
|
-
import Logger from '../Logger/Logger.js';
|
|
15
|
-
import Publisher from '../Publisher/Publisher.js';
|
|
16
|
-
/**
|
|
17
|
-
* Represents a transaction object that handles the execution of a request and manages the response.
|
|
18
|
-
* @template InputType - The type of the input data for the transaction.
|
|
19
|
-
* @template ResponseInnerType - The type of the inner response data for the transaction.
|
|
20
|
-
* @template MiscRespType - The type of miscellaneous response data for the transaction.
|
|
21
|
-
*/
|
|
22
|
-
export default class Transaction {
|
|
23
|
-
/**
|
|
24
|
-
* Constructs a new instance of the Transaction class.
|
|
25
|
-
* @param {APIGatewayEvent | SQSEvent | DynamoDBStreamEvent} event - The event object passed to the Lambda function.
|
|
26
|
-
* @param {Context} context - The context object passed to the Lambda function.
|
|
27
|
-
* @param {TransactionConfig} [config] - Optional configuration object for the transaction.
|
|
28
|
-
* @returns None
|
|
29
|
-
*/
|
|
30
|
-
constructor(event, context, config) {
|
|
31
|
-
/**
|
|
32
|
-
* The instance of the DatabaseManager class used for managing the database.
|
|
33
|
-
*/
|
|
34
|
-
this.databaseManager = DatabaseManager.INSTANCE;
|
|
35
|
-
/**
|
|
36
|
-
* An array of database transactions.
|
|
37
|
-
* @type {DatabaseTransaction[]}
|
|
38
|
-
*/
|
|
39
|
-
this.transactions = [];
|
|
40
|
-
const transactionId = context.awsRequestId
|
|
41
|
-
? context.awsRequestId
|
|
42
|
-
: event.requestContext
|
|
43
|
-
? event.requestContext.requestId
|
|
44
|
-
: 'unknown';
|
|
45
|
-
// transaction ctx
|
|
46
|
-
this.event = event;
|
|
47
|
-
this.context = context;
|
|
48
|
-
// when set, this will be called with the response context right before calling the context suceed/fail - useful for writing the resp for example.
|
|
49
|
-
this.responseProxy = null;
|
|
50
|
-
// transaction flags
|
|
51
|
-
this.syncReturn = !!(config === null || config === void 0 ? void 0 : config.syncReturn);
|
|
52
|
-
this.retrowErrors = !!(config === null || config === void 0 ? void 0 : config.throwOnErrors); /* retrow internal errors */
|
|
53
|
-
// components
|
|
54
|
-
this.logger = new Logger(config === null || config === void 0 ? void 0 : config.logger, transactionId);
|
|
55
|
-
this.request = new Request(this.event, this.context, this.logger);
|
|
56
|
-
this.publisher = new Publisher(config === null || config === void 0 ? void 0 : config.publisher);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Executes a transaction using the provided execution function and returns a promise
|
|
60
|
-
* that resolves to the response or miscellaneous response.
|
|
61
|
-
* @param {TransactionExecution<Transaction<InputType, ResponseInnerType, MiscRespType>, ResponseInnerType, MiscRespType>} executionFunc - The execution function to be executed.
|
|
62
|
-
* @returns {Promise<Response<ResponseInnerType | ResponseErrorType> | MiscRespType>} - A promise that resolves to the response or miscellaneous response.
|
|
63
|
-
*/
|
|
64
|
-
execute(executionFunc) {
|
|
65
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
yield this.executeLoggerFlush(() => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
yield this.executeDBTransactions(() => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
return yield this.iexecute(executionFunc);
|
|
69
|
-
}));
|
|
70
|
-
}));
|
|
71
|
-
// return raw response if sync return is requested
|
|
72
|
-
if (this.syncReturn)
|
|
73
|
-
return this.response;
|
|
74
|
-
// allow request to async succeed through lambda context
|
|
75
|
-
return null;
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Executes a transaction using the provided execution function and handles the response.
|
|
80
|
-
* @param {TransactionExecution<Transaction<InputType, ResponseInnerType, MiscRespType>, ResponseInnerType, MiscRespType>} executionFunc - The function to execute the transaction.
|
|
81
|
-
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating whether the execution failed or not.
|
|
82
|
-
*/
|
|
83
|
-
iexecute(executionFunc) {
|
|
84
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
let executionFailed = true; //failed til we say no!
|
|
86
|
-
//safe execution handler
|
|
87
|
-
try {
|
|
88
|
-
//Execute
|
|
89
|
-
this.logger.debug('Starting main request code');
|
|
90
|
-
this.response = yield executionFunc(this);
|
|
91
|
-
//Answer client
|
|
92
|
-
if (this.response && this.response instanceof Response) {
|
|
93
|
-
yield this.response.build(this.context, this, this.syncReturn);
|
|
94
|
-
executionFailed = !!(this.response.getBody() && this.response.getBody()['rollback']);
|
|
95
|
-
}
|
|
96
|
-
else if (this.syncReturn && this.response) {
|
|
97
|
-
this.logger.log('Sync return with different response object');
|
|
98
|
-
this.logger.debug(this.response);
|
|
99
|
-
executionFailed = false;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.response = this.getErrorResponse(Globals.ErrorResponseInvalidServerResponse, Globals.ErrorCode_APIError);
|
|
103
|
-
yield this.response.build(this.context, this, this.syncReturn);
|
|
104
|
-
this.logger.error('Invalid response object from main request code.');
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (e) {
|
|
108
|
-
/*EXECUTION FAIL*/
|
|
109
|
-
this.logger.error('Exception when executing main request code.');
|
|
110
|
-
this.logger.exception(e);
|
|
111
|
-
//retrow?
|
|
112
|
-
if (this.retrowErrors)
|
|
113
|
-
throw e;
|
|
114
|
-
//envelope exception?
|
|
115
|
-
this.response = this.getErrorResponse(Globals.ErrorResponseUnhandledError, Globals.ErrorCode_APIError);
|
|
116
|
-
yield this.response.build(this.context, this, this.syncReturn);
|
|
117
|
-
}
|
|
118
|
-
return executionFailed;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Retrieves a database transaction based on the provided database configuration.
|
|
123
|
-
* @param {DbConfig<S>} config - The configuration object for the database.
|
|
124
|
-
* @returns {Promise<DatabaseTransactionType<S, DBSchema>>} A promise that resolves to the database transaction.
|
|
125
|
-
*/
|
|
126
|
-
getDbTransaction(config) {
|
|
127
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
-
const db = this.getDatabase(config);
|
|
129
|
-
const dbTrans = yield db.transaction();
|
|
130
|
-
this.transactions.push(dbTrans);
|
|
131
|
-
return dbTrans;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Retrieves a database instance based on the provided configuration.
|
|
136
|
-
* @param {DbConfig<S>} config - The configuration object specifying the type of database.
|
|
137
|
-
* @returns {DatabaseImplType<S, DBSchema>} A database instance based on the provided configuration.
|
|
138
|
-
*/
|
|
139
|
-
getDatabase(config) {
|
|
140
|
-
return this.databaseManager.create(config);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Get the remaining time in milliseconds for the current execution context.
|
|
144
|
-
* @returns {number} The remaining time in milliseconds, or -1 if the time is not available.
|
|
145
|
-
*/
|
|
146
|
-
getRemainingTimeInMillis() {
|
|
147
|
-
var _a, _b;
|
|
148
|
-
return ((_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.getRemainingTimeInMillis) === null || _b === void 0 ? void 0 : _b.call(_a)) || -1;
|
|
149
|
-
}
|
|
150
|
-
/*
|
|
151
|
-
* Executes a series of database transactions in a safe manner.
|
|
152
|
-
* @param {Function} safeExecution - The function that contains the database transactions to be executed.
|
|
153
|
-
* @returns None
|
|
154
|
-
* @throws {Error} - If an exception occurs during the execution of the transactions and `retrowErrors` is true.
|
|
155
|
-
*/
|
|
156
|
-
executeDBTransactions(safeExecution) {
|
|
157
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
-
try {
|
|
159
|
-
// Execute
|
|
160
|
-
const execFailed = yield safeExecution();
|
|
161
|
-
for (const transaction of [...this.transactions].reverse()) {
|
|
162
|
-
try {
|
|
163
|
-
yield transaction[execFailed ? 'closeFailure' : 'closeSuccess']();
|
|
164
|
-
}
|
|
165
|
-
catch (e) {
|
|
166
|
-
this.logger.error('Exception when closing DB transactions after success.');
|
|
167
|
-
this.logger.exception(e);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
/* this part of the code handle exceptions at transaction level,
|
|
173
|
-
so probably a bug but we still handle such */
|
|
174
|
-
for (const transaction of [...this.transactions].reverse()) {
|
|
175
|
-
try {
|
|
176
|
-
yield transaction.closeFailure();
|
|
177
|
-
}
|
|
178
|
-
catch (e) {
|
|
179
|
-
this.logger.error('Exception when closing DB transactions after failure.');
|
|
180
|
-
this.logger.exception(e);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
this.logger.error('Exception when executing DB transactions.');
|
|
184
|
-
this.logger.log(e.stack);
|
|
185
|
-
//retrow?
|
|
186
|
-
if (this.retrowErrors)
|
|
187
|
-
throw e;
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Executes a logger flush operation with error handling and logging.
|
|
193
|
-
* @param {Function} safeExecution - The function to execute safely.
|
|
194
|
-
* @returns None
|
|
195
|
-
* @throws {Error} - If `retrowErrors` is true and an error occurs during execution.
|
|
196
|
-
*/
|
|
197
|
-
executeLoggerFlush(safeExecution) {
|
|
198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
try {
|
|
200
|
-
yield safeExecution();
|
|
201
|
-
}
|
|
202
|
-
catch (e) {
|
|
203
|
-
this.logger.error('Exception when flushing logs.');
|
|
204
|
-
this.logger.exception(e);
|
|
205
|
-
//retrow?
|
|
206
|
-
if (this.retrowErrors)
|
|
207
|
-
throw e;
|
|
208
|
-
}
|
|
209
|
-
finally {
|
|
210
|
-
this.logger.debug('Transaction ended');
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Generates an error response with the provided error message and code.
|
|
216
|
-
* @param {string} error - The error message to include in the response.
|
|
217
|
-
* @param {string} code - The error code associated with the response.
|
|
218
|
-
* @returns {Response<ResponseErrorType>} - The error response object.
|
|
219
|
-
*/
|
|
220
|
-
getErrorResponse(error, code) {
|
|
221
|
-
return Response.BadRequestResponseWithRollback(error, code);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
//# sourceMappingURL=Transaction.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Transaction.js","sourceRoot":"","sources":["../../../src/BaseEvent/Transaction.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,OAAO,MAAM,mBAAmB,CAAA;AACvC,OAAO,QAA+B,MAAM,oBAAoB,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAQhE,OAAO,OAAO,MAAM,eAAe,CAAA;AACnC,OAAO,MAAwB,MAAM,qBAAqB,CAAA;AAC1D,OAAO,SAA8B,MAAM,2BAA2B,CAAA;AAkCtE;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IA8D9B;;;;;;OAMG;IACH,YACE,KAAuD,EACvD,OAAgB,EAChB,MAA0B;QAjE5B;;WAEG;QACK,oBAAe,GAAoB,eAAe,CAAC,QAAQ,CAAA;QACnE;;;WAGG;QACK,iBAAY,GAA0B,EAAE,CAAA;QA2D9C,MAAM,aAAa,GAAG,OAAO,CAAC,YAAY;YACxC,CAAC,CAAC,OAAO,CAAC,YAAY;YACtB,CAAC,CAAmB,KAAM,CAAC,cAAc;gBACvC,CAAC,CAAmB,KAAM,CAAC,cAAc,CAAC,SAAS;gBACnD,CAAC,CAAC,SAAS,CAAA;QACf,kBAAkB;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,kJAAkJ;QAClJ,IAAI,CAAC,aAAa,GAAG,IAAI,CAAA;QACzB,oBAAoB;QACpB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,CAAA,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AC,CAAA;IAC7D,CAAC;CACF"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { RedisClientType, RedisClusterType } from 'redis';
|
|
2
|
-
import { CacheConfig } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Represents a Redis cache connection class.
|
|
5
|
-
*/
|
|
6
|
-
export default class Redis {
|
|
7
|
-
private static singleton;
|
|
8
|
-
/**
|
|
9
|
-
* A private static property that holds a reference to the redis.createClient function.
|
|
10
|
-
* This property is used to create client.
|
|
11
|
-
*/
|
|
12
|
-
private static ClientFactory;
|
|
13
|
-
/**
|
|
14
|
-
* A private static property that references the createCluster function.
|
|
15
|
-
* This property can be used to create clusters within the class.
|
|
16
|
-
*/
|
|
17
|
-
private static ClusterFactory;
|
|
18
|
-
static connection(config: CacheConfig<'redis'>): Promise<RedisClientType | RedisClusterType>;
|
|
19
|
-
private static connectionFactory;
|
|
20
|
-
/**
|
|
21
|
-
* Establishes a connection to a Redis client based on the provided configuration
|
|
22
|
-
* and holds it for reusability. If connection is detected closed, new connection is
|
|
23
|
-
* initialized and established.
|
|
24
|
-
* @param {CacheConfig<'redis'>} config - The configuration object for connecting to the Redis client.
|
|
25
|
-
* @returns {Promise<RedisClientType>} A promise that resolves to the Redis client connection.
|
|
26
|
-
*/
|
|
27
|
-
private static redisClientConnection;
|
|
28
|
-
private static redisClusterConnection;
|
|
29
|
-
}
|
package/dist/src/Cache/Redis.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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;;Y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AAI,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"}
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
};
|
package/dist/src/Cache/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/Cache/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,123 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,109 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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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