@golemio/core 1.2.14-dev.575817871 → 1.2.15-dev.591279008
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/dist/output-gateway/redis/CacheMiddleware.js +5 -5
- package/dist/output-gateway/redis/CacheMiddleware.js.map +1 -1
- package/dist/output-gateway/redis/RedisConnector.d.ts +8 -3
- package/dist/output-gateway/redis/RedisConnector.js +51 -26
- package/dist/output-gateway/redis/RedisConnector.js.map +1 -1
- package/package.json +6 -4
|
@@ -4,15 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.useCacheMiddleware = exports.CacheMiddleware = void 0;
|
|
7
|
-
const
|
|
7
|
+
const apicache_plus_1 = __importDefault(require("apicache-plus"));
|
|
8
8
|
const config_1 = require("../config");
|
|
9
9
|
const Logger_1 = require("../Logger");
|
|
10
|
-
const
|
|
10
|
+
const RedisConnector_1 = require("./RedisConnector");
|
|
11
11
|
class MyCacheMiddleware {
|
|
12
12
|
constructor() {
|
|
13
13
|
this.defaultCacheTtl = parseInt(config_1.config.redis_ttl || "60000", 10);
|
|
14
14
|
// as default is used memory-cache
|
|
15
|
-
this.cacheWithRedisMiddleware =
|
|
15
|
+
this.cacheWithRedisMiddleware = apicache_plus_1.default.options({
|
|
16
16
|
defaultDuration: this.defaultCacheTtl.toString(),
|
|
17
17
|
}).middleware;
|
|
18
18
|
this.isNoCacheHeader = (req, res) => {
|
|
@@ -33,9 +33,9 @@ class MyCacheMiddleware {
|
|
|
33
33
|
}
|
|
34
34
|
init() {
|
|
35
35
|
if (config_1.config.redis_enable) {
|
|
36
|
-
this.redisClient =
|
|
36
|
+
this.redisClient = RedisConnector_1.RedisConnector.getConnection(RedisConnector_1.ConnectionType.APICACHE);
|
|
37
37
|
// sets redis to apicache
|
|
38
|
-
this.cacheWithRedisMiddleware =
|
|
38
|
+
this.cacheWithRedisMiddleware = apicache_plus_1.default.options({
|
|
39
39
|
defaultDuration: this.defaultCacheTtl.toString(),
|
|
40
40
|
redisClient: this.redisClient,
|
|
41
41
|
}).middleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheMiddleware.js","sourceRoot":"","sources":["../../../src/output-gateway/redis/CacheMiddleware.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"CacheMiddleware.js","sourceRoot":"","sources":["../../../src/output-gateway/redis/CacheMiddleware.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAqC;AAGrC,sCAAoC;AACpC,sCAAiC;AACjC,qDAAkE;AAElE,MAAM,iBAAiB;IAAvB;QACY,oBAAe,GAAW,QAAQ,CAAC,eAAM,CAAC,SAAS,IAAI,OAAO,EAAE,EAAE,CAAC,CAAC;QAI5E,kCAAkC;QAC1B,6BAAwB,GAAG,uBAAQ,CAAC,OAAO,CAAC;YAChD,eAAe,EAAE,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE;SACnD,CAAC,CAAC,UAAU,CAAC;QAoBP,oBAAe,GAAG,CAAC,GAAY,EAAE,GAAa,EAAW,EAAE;YAC9D,MAAM,WAAW,GAAuB,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;YACrE,OAAO,WAAW,KAAK,UAAU,CAAC;QACtC,CAAC,CAAC;QAMM,eAAU,GAAG,CAAC,MAAwB,EAAE,EAAE;YAC9C,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;gBACvD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE;oBACjC,MAAM,UAAU,GAAG,IAAI,CAAC,wBAAwB,CAAC,MAAM,IAAI,IAAI,CAAC,eAAe,CAAC,CAAC;oBACjF,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;iBAC9B;qBAAM;oBACH,IAAI,EAAE,CAAC;iBACV;YACL,CAAC,CAAC;QACN,CAAC,CAAC;IACN,CAAC;IArCU,IAAI;QACP,IAAI,eAAM,CAAC,YAAY,EAAE;YACrB,IAAI,CAAC,WAAW,GAAG,+BAAc,CAAC,aAAa,CAAC,+BAAc,CAAC,QAAQ,CAAC,CAAC;YAEzE,yBAAyB;YACzB,IAAI,CAAC,wBAAwB,GAAG,uBAAQ,CAAC,OAAO,CAAC;gBAC7C,eAAe,EAAE,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE;gBAChD,WAAW,EAAE,IAAI,CAAC,WAAW;aAChC,CAAC,CAAC,UAAU,CAAC;SACjB;aAAM;YACH,YAAG,CAAC,IAAI,CAAC,4DAA4D,CAAC,CAAC;SAC1E;IACL,CAAC;IAEM,kBAAkB;QACrB,OAAO,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;IAOM,aAAa,CAAC,MAAwB;QACzC,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;CAYJ;AAED,MAAM,eAAe,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAGvC,0CAAe;AAFxB,MAAM,kBAAkB,GAAG,CAAC,MAAwB,EAAE,EAAE,CAAC,eAAe,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AAErE,gDAAkB"}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { RedisClient } from "redis";
|
|
2
|
+
export declare enum ConnectionType {
|
|
3
|
+
DEFAULT = "DEFAULT",
|
|
4
|
+
APICACHE = "APICACHE"
|
|
5
|
+
}
|
|
2
6
|
declare class MyRedis {
|
|
3
|
-
private
|
|
7
|
+
private connections;
|
|
4
8
|
connect: () => Promise<RedisClient>;
|
|
5
|
-
getConnection(): RedisClient;
|
|
6
|
-
isConnected(): boolean;
|
|
9
|
+
getConnection(connectionType?: ConnectionType): RedisClient;
|
|
10
|
+
isConnected(connectionType?: ConnectionType): boolean;
|
|
7
11
|
disconnect(): Promise<void>;
|
|
12
|
+
private createConnection;
|
|
8
13
|
private checkOnConnect;
|
|
9
14
|
}
|
|
10
15
|
declare const RedisConnector: MyRedis;
|
|
@@ -12,62 +12,87 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RedisConnector = void 0;
|
|
15
|
+
exports.RedisConnector = exports.ConnectionType = void 0;
|
|
16
16
|
const errors_1 = require("@golemio/errors");
|
|
17
|
+
const promises_1 = require("timers/promises");
|
|
17
18
|
const redis_1 = __importDefault(require("redis"));
|
|
18
19
|
const config_1 = require("../config");
|
|
19
20
|
const Logger_1 = require("../Logger");
|
|
21
|
+
var ConnectionType;
|
|
22
|
+
(function (ConnectionType) {
|
|
23
|
+
ConnectionType["DEFAULT"] = "DEFAULT";
|
|
24
|
+
ConnectionType["APICACHE"] = "APICACHE";
|
|
25
|
+
})(ConnectionType = exports.ConnectionType || (exports.ConnectionType = {}));
|
|
26
|
+
const connectionOptions = {
|
|
27
|
+
[ConnectionType.DEFAULT]: {},
|
|
28
|
+
[ConnectionType.APICACHE]: { detect_buffers: true },
|
|
29
|
+
};
|
|
20
30
|
class MyRedis {
|
|
21
31
|
constructor() {
|
|
32
|
+
this.connections = new Map();
|
|
22
33
|
this.connect = () => __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
if (this.connections.has(ConnectionType.DEFAULT)) {
|
|
35
|
+
return this.connections.get(ConnectionType.DEFAULT);
|
|
36
|
+
}
|
|
37
|
+
for (const connectionType of Object.values(ConnectionType)) {
|
|
38
|
+
yield this.createConnection(connectionType);
|
|
39
|
+
}
|
|
40
|
+
return this.connections.get(ConnectionType.DEFAULT);
|
|
41
|
+
});
|
|
42
|
+
this.createConnection = (connectionType) => __awaiter(this, void 0, void 0, function* () {
|
|
23
43
|
try {
|
|
24
|
-
if (this.connection) {
|
|
25
|
-
return this.connection;
|
|
26
|
-
}
|
|
27
44
|
if (!config_1.config.redis_connection) {
|
|
28
45
|
throw new errors_1.CustomError("The ENV variable REDIS_CONN cannot be undefined.", true, this.constructor.name);
|
|
29
46
|
}
|
|
30
|
-
|
|
31
|
-
.createClient(config_1.config.redis_connection
|
|
47
|
+
const connection = redis_1.default
|
|
48
|
+
.createClient(config_1.config.redis_connection, connectionOptions[connectionType])
|
|
32
49
|
.on("message", (message) => Logger_1.log.info(message))
|
|
33
50
|
.on("error", (error) => Logger_1.log.warn(error))
|
|
34
51
|
.on("connect", () => Logger_1.log.info("Connected to Redis!"));
|
|
35
|
-
|
|
52
|
+
this.connections.set(connectionType, connection);
|
|
53
|
+
// waiting for the redis client to connect
|
|
36
54
|
const delay = 500; // millis
|
|
37
55
|
const attemps = 4;
|
|
38
|
-
yield
|
|
39
|
-
return
|
|
56
|
+
yield this.checkOnConnect(connectionType, attemps, attemps, delay);
|
|
57
|
+
return connection;
|
|
40
58
|
}
|
|
41
59
|
catch (err) {
|
|
42
60
|
throw new errors_1.CustomError("Error while connecting to Redis.", false, this.constructor.name, 1001, err);
|
|
43
61
|
}
|
|
44
62
|
});
|
|
45
|
-
this.checkOnConnect = (retry, attemps, delay) => (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
else {
|
|
53
|
-
setTimeout(() => this.checkOnConnect(retry - 1, attemps, delay)(resolve, reject), delay);
|
|
63
|
+
this.checkOnConnect = (connectionType, retry, attemps, delay) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
while (retry > 0) {
|
|
65
|
+
if (this.isConnected(connectionType)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
yield (0, promises_1.setTimeout)(delay);
|
|
69
|
+
retry -= 1;
|
|
54
70
|
}
|
|
55
|
-
|
|
71
|
+
throw new Error(`Failed to connect to Redis after ${attemps * delay} millis.`);
|
|
72
|
+
});
|
|
56
73
|
}
|
|
57
|
-
getConnection() {
|
|
58
|
-
|
|
74
|
+
getConnection(connectionType = ConnectionType.DEFAULT) {
|
|
75
|
+
const connection = this.connections.get(connectionType);
|
|
76
|
+
if (!connection) {
|
|
59
77
|
throw new errors_1.CustomError("Redis connection does not exists. First call connect() method.", false, this.constructor.name, 1002);
|
|
60
78
|
}
|
|
61
|
-
return
|
|
79
|
+
return connection;
|
|
62
80
|
}
|
|
63
|
-
isConnected() {
|
|
64
|
-
|
|
81
|
+
isConnected(connectionType = ConnectionType.DEFAULT) {
|
|
82
|
+
const connection = this.connections.get(connectionType);
|
|
83
|
+
return !!connection && connection.connected;
|
|
65
84
|
}
|
|
66
85
|
disconnect() {
|
|
67
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
87
|
Logger_1.log.info("Redis disconnect called");
|
|
69
|
-
|
|
70
|
-
yield
|
|
88
|
+
for (const connection of this.connections.values()) {
|
|
89
|
+
yield new Promise((resolve, reject) => {
|
|
90
|
+
connection.quit((err) => {
|
|
91
|
+
if (err)
|
|
92
|
+
reject(err);
|
|
93
|
+
resolve();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
71
96
|
}
|
|
72
97
|
});
|
|
73
98
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisConnector.js","sourceRoot":"","sources":["../../../src/output-gateway/redis/RedisConnector.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4CAA8C;AAC9C,
|
|
1
|
+
{"version":3,"file":"RedisConnector.js","sourceRoot":"","sources":["../../../src/output-gateway/redis/RedisConnector.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4CAA8C;AAC9C,8CAAsD;AACtD,kDAAuD;AACvD,sCAAoC;AACpC,sCAAiC;AAEjC,IAAY,cAGX;AAHD,WAAY,cAAc;IACtB,qCAAmB,CAAA;IACnB,uCAAqB,CAAA;AACzB,CAAC,EAHW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAGzB;AAED,MAAM,i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CAAC,iBAAiC,cAAc,CAAC,OAAO;QACtE,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QACxD,OAAO,CAAC,CAAC,UAAU,IAAI,UAAU,CAAC,SAAS,CAAC;IAChD,CAAC;IAEY,UAAU;;YACnB,YAAG,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;YAEpC,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,EAAE;gBAChD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBACxC,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;wBACpB,IAAI,GAAG;4BAAE,MAAM,CAAC,GAAG,CAAC,CAAC;wBACrB,OAAO,EAAE,CAAC;oBACd,CAAC,CAAC,CAAC;gBACP,CAAC,CAAC,CAAC;aACN;QACL,CAAC;KAAA;CAuCJ;AAED,MAAM,cAAc,GAAG,IAAI,OAAO,EAAE,CAAC;AAE5B,wCAAc"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@golemio/core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.15-dev.591279008",
|
|
4
4
|
"description": "Golemio Core Module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"@golemio/eslint-config": "^1.1.0",
|
|
33
33
|
"@ovos-media/ts-transform-paths": "^1.7.18-1",
|
|
34
34
|
"@types/amqplib": "^0.5.17",
|
|
35
|
-
"@types/apicache": "^1.6.1",
|
|
36
35
|
"@types/chai": "4.2.3",
|
|
37
36
|
"@types/chai-as-promised": "7.1.2",
|
|
38
37
|
"@types/content-type": "^1.1.3",
|
|
@@ -68,7 +67,7 @@
|
|
|
68
67
|
"dependencies": {
|
|
69
68
|
"@golemio/errors": "^1.0.1",
|
|
70
69
|
"@golemio/utils": "^1.0.0",
|
|
71
|
-
"@golemio/validator": "0.2.
|
|
70
|
+
"@golemio/validator": "0.2.3",
|
|
72
71
|
"@google-cloud/storage": "^5.18.0",
|
|
73
72
|
"@opentelemetry/api": "1.0.4",
|
|
74
73
|
"@opentelemetry/exporter-jaeger": "^1.2.0",
|
|
@@ -89,7 +88,7 @@
|
|
|
89
88
|
"@types/request-promise": "^4.1.45",
|
|
90
89
|
"ajv": "^8.10.0",
|
|
91
90
|
"amqplib": "^0.7.1",
|
|
92
|
-
"apicache": "^
|
|
91
|
+
"apicache-plus": "^2.3.1",
|
|
93
92
|
"aws-sdk": "^2.823.0",
|
|
94
93
|
"axios": "^0.24.0",
|
|
95
94
|
"basic-ftp": "^4.6.6",
|
|
@@ -122,5 +121,8 @@
|
|
|
122
121
|
"ssh2-sftp-client": "^7.1.0",
|
|
123
122
|
"winston": "^3.3.3",
|
|
124
123
|
"xml2js-es6-promise": "^1.1.1"
|
|
124
|
+
},
|
|
125
|
+
"resolutions": {
|
|
126
|
+
"moment": "2.29.4"
|
|
125
127
|
}
|
|
126
128
|
}
|