@universis/janitor 1.1.0 → 1.3.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/dist/RedisClientStore.js +46 -15
- package/dist/RedisClientStore.js.map +1 -1
- package/package.json +2 -2
- package/src/RedisClientStore.js +46 -15
package/dist/RedisClientStore.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.RedisClientStore = void 0;var _common = require("@themost/common");
|
|
2
2
|
var _rateLimitRedis = _interopRequireDefault(require("rate-limit-redis"));
|
|
3
|
-
var _redis = require("redis");
|
|
3
|
+
var _redis = require("redis");
|
|
4
|
+
require("@themost/promise-sequence");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _defineProperty(obj, key, value) {key = _toPropertyKey(key);if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}function _toPropertyKey(arg) {var key = _toPrimitive(arg, "string");return typeof key === "symbol" ? key : String(key);}function _toPrimitive(input, hint) {if (typeof input !== "object" || input === null) return input;var prim = input[Symbol.toPrimitive];if (prim !== undefined) {var res = prim.call(input, hint || "default");if (typeof res !== "object") return res;throw new TypeError("@@toPrimitive must return a primitive value.");}return (hint === "string" ? String : Number)(input);}
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
let superLoadIncrementScript;
|
|
7
|
+
let superLoadGetScript;
|
|
7
8
|
|
|
8
9
|
function noLoadGetScript() {
|
|
9
10
|
|
|
@@ -13,11 +14,15 @@ function noLoadIncrementScript() {
|
|
|
13
14
|
|
|
14
15
|
//
|
|
15
16
|
}
|
|
16
|
-
if (
|
|
17
|
+
if (_rateLimitRedis.default.prototype.loadIncrementScript.name === 'loadIncrementScript') {
|
|
18
|
+
// get super method for future use
|
|
19
|
+
superLoadIncrementScript = _rateLimitRedis.default.prototype.loadIncrementScript;
|
|
17
20
|
_rateLimitRedis.default.prototype.loadIncrementScript = noLoadIncrementScript;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
if (
|
|
23
|
+
if (_rateLimitRedis.default.prototype.loadGetScript.name === 'loadGetScript') {
|
|
24
|
+
// get super method
|
|
25
|
+
superLoadGetScript = _rateLimitRedis.default.prototype.loadGetScript;
|
|
21
26
|
_rateLimitRedis.default.prototype.loadGetScript = noLoadGetScript;
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -52,7 +57,9 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
52
57
|
return client.connect().then(() => {
|
|
53
58
|
return client.sendCommand(args);
|
|
54
59
|
}).catch((error) => {
|
|
55
|
-
|
|
60
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
61
|
+
_common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
62
|
+
}
|
|
56
63
|
return Promise.reject(error);
|
|
57
64
|
}).finally(() => {
|
|
58
65
|
if (client.isOpen) {
|
|
@@ -72,26 +79,50 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
72
79
|
self.client = (0, _redis.createClient)(connectOptions);
|
|
73
80
|
}
|
|
74
81
|
if (self.client.isOpen) {
|
|
75
|
-
return self.client.sendCommand(args)
|
|
82
|
+
return self.client.sendCommand(args).catch((error) => {
|
|
83
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
84
|
+
_common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
85
|
+
}
|
|
86
|
+
return Promise.reject(error);
|
|
87
|
+
});
|
|
76
88
|
}
|
|
77
89
|
_common.TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');
|
|
78
90
|
return self.client.connect().then(() => {
|
|
79
91
|
// send load script commands once
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
return (() => {
|
|
93
|
+
if (self.incrementScriptSha == null) {
|
|
94
|
+
return this.postInit();
|
|
95
|
+
}
|
|
96
|
+
return Promise.resolve();
|
|
97
|
+
})().then(() => {
|
|
86
98
|
// send command
|
|
87
99
|
args[1] = self.incrementScriptSha;
|
|
88
|
-
return self.client.sendCommand(args)
|
|
100
|
+
return self.client.sendCommand(args).catch((error) => {
|
|
101
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
102
|
+
_common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
103
|
+
}
|
|
104
|
+
return Promise.reject(error);
|
|
105
|
+
});
|
|
89
106
|
});
|
|
90
107
|
});
|
|
91
108
|
}
|
|
92
109
|
}); /**
|
|
93
110
|
* @type {import('redis').RedisClientType}
|
|
94
|
-
*/_defineProperty(this, "client", void 0);this.init(options);
|
|
111
|
+
*/_defineProperty(this, "client", void 0);this.init(options);_common.TraceUtils.debug('RedisClientStore: Starting up and loading increment and get scripts.');
|
|
112
|
+
void this.postInit().then(() => {
|
|
113
|
+
_common.TraceUtils.debug('RedisClientStore: Successfully loaded increment and get scripts.');
|
|
114
|
+
}).catch((err) => {
|
|
115
|
+
_common.TraceUtils.error('RedisClientStore: Failed to load increment and get scripts.');
|
|
116
|
+
_common.TraceUtils.error(err);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async postInit() {
|
|
120
|
+
const [incrementScriptSha, getScriptSha] = await Promise.sequence([
|
|
121
|
+
() => superLoadIncrementScript.call(this),
|
|
122
|
+
() => superLoadGetScript.call(this)]);
|
|
123
|
+
|
|
124
|
+
this.incrementScriptSha = incrementScriptSha;
|
|
125
|
+
this.getScriptSha = getScriptSha;
|
|
95
126
|
}
|
|
96
127
|
|
|
97
128
|
}exports.RedisClientStore = RedisClientStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisClientStore.js","names":["_common","require","_rateLimitRedis","_interopRequireDefault","_redis","obj","__esModule","default","_defineProperty","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","arg","_toPrimitive","String","input","hint","prim","Symbol","toPrimitive","undefined","res","call","TypeError","Number","superLoadIncrementScript","RedisStore","prototype","loadIncrementScript","superLoadGetScript","loadGetScript","noLoadGetScript","noLoadIncrementScript","RedisClientStore","constructor","service","options","sendCommand","args","Array","from","arguments","self","TraceUtils","debug","connectOptions","getApplication","getConfiguration","getSourceAt","host","port","client","createClient","connect","then","catch","error","Promise","reject","finally","isOpen","disconnect","errDisconnect","all","results","incrementScriptSha","getScriptSha","init","exports"],"sources":["../src/RedisClientStore.js"],"sourcesContent":["import { TraceUtils } from '@themost/common';\nimport RedisStore from 'rate-limit-redis';\nimport { createClient } from 'redis';\n\nconst superLoadIncrementScript = RedisStore.prototype.loadIncrementScript;\nconst superLoadGetScript = RedisStore.prototype.loadGetScript;\n\nfunction noLoadGetScript() {\n //\n}\n\nfunction noLoadIncrementScript() {\n //\n}\n\nif (superLoadIncrementScript != noLoadIncrementScript) {\n RedisStore.prototype.loadIncrementScript = noLoadIncrementScript;\n}\n\nif (superLoadGetScript != noLoadGetScript) {\n RedisStore.prototype.loadGetScript = noLoadGetScript;\n}\n\nclass RedisClientStore extends RedisStore {\n\n /**\n * @type {import('redis').RedisClientType}\n */\n client;\n\n /**\n * \n * @param {import('@themost/common').ApplicationService} service\n * @param {{windowMs: number}} options\n */\n constructor(service, options) {\n super({\n /**\n * @param {...string} args\n * @returns {Promise<*>}\n */\n sendCommand: function () {\n const args = Array.from(arguments);\n const self = this;\n if (args[0] === 'SCRIPT') {\n TraceUtils.debug('RedisClientStore: Creating new client for sending script command');\n const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {\n host: '127.0.0.1',\n port: 6379\n };\n const client = createClient(connectOptions);\n return client.connect().then(() => {\n return client.sendCommand(args);\n }).catch((error) => {\n TraceUtils.error(error);\n return Promise.reject(error);\n }).finally(() => {\n if (client.isOpen) {\n TraceUtils.debug('RedisClientStore: Closing client of sending script command');\n client.disconnect().catch((errDisconnect) => {\n TraceUtils.error(errDisconnect);\n });\n }\n });\n }\n if (self.client == null) {\n TraceUtils.debug('RedisClientStore: Creating store client for sending commands');\n const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {\n host: '127.0.0.1',\n port: 6379\n };\n self.client = createClient(connectOptions);\n }\n if (self.client.isOpen) {\n return self.client.sendCommand(args);\n }\n TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');\n return self.client.connect().then(() => {\n // send load script commands once\n return Promise.all([\n superLoadIncrementScript.call(self),\n superLoadGetScript.call(self)\n ]).then((results) => {\n self.incrementScriptSha = results[0];\n self.getScriptSha = results[1];\n // send command \n args[1] = self.incrementScriptSha;\n return self.client.sendCommand(args);\n });\n });\n }\n });\n this.init(options)\n \n }\n\n}\n\nexport {\n RedisClientStore\n}"],"mappings":"6GAAA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA,UAAqC,SAAAE,uBAAAE,GAAA,UAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA,aAAAG,gBAAAH,GAAA,EAAAI,GAAA,EAAAC,KAAA,GAAAD,GAAA,GAAAE,cAAA,CAAAF,GAAA,MAAAA,GAAA,IAAAJ,GAAA,GAAAO,MAAA,CAAAC,cAAA,CAAAR,GAAA,EAAAI,GAAA,IAAAC,KAAA,EAAAA,KAAA,EAAAI,UAAA,QAAAC,YAAA,QAAAC,QAAA,kBAAAX,GAAA,CAAAI,GAAA,IAAAC,KAAA,SAAAL,GAAA,WAAAM,eAAAM,GAAA,OAAAR,GAAA,GAAAS,YAAA,CAAAD,GAAA,0BAAAR,GAAA,gBAAAA,GAAA,GAAAU,MAAA,CAAAV,GAAA,YAAAS,aAAAE,KAAA,EAAAC,IAAA,cAAAD,KAAA,iBAAAA,KAAA,kBAAAA,KAAA,KAAAE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AI,CAACoB,IAAI,CAAC;UACnCb,kBAAkB,CAACP,IAAI,CAACoB,IAAI,CAAC,CAChC,CAAC;UAACY,IAAI,CAAC,CAACU,OAAO,KAAK;YACjBtB,IAAI,CAACuB,kBAAkB,GAAGD,OAAO,CAAC,CAAC,CAAC;YACpCtB,IAAI,CAACwB,YAAY,GAAGF,OAAO,CAAC,CAAC,CAAC;YAC9B;YACA1B,IAAI,CAAC,CAAC,CAAC,GAAGI,IAAI,CAACuB,kBAAkB;YACjC,OAAOvB,IAAI,CAACS,MAAM,CAACd,WAAW,CAACC,IAAI,CAAC;UACxC,CAAC,CAAC;QACN,CAAC,CAAC;MACN;IACJ,CAAC,CAAC,CAAC,CAlEP;AACJ;AACA,OAFInC,eAAA,yBAmEI,IAAI,CAACgE,IAAI,CAAC/B,OAAO,CAAC;EAEtB;;AAEJ,CAACgC,OAAA,CAAAnC,gBAAA,GAAAA,gBAAA"}
|
|
1
|
+
{"version":3,"file":"RedisClientStore.js","names":["_common","require","_rateLimitRedis","_interopRequireDefault","_redis","obj","__esModule","default","_defineProperty","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","arg","_toPrimitive","String","input","hint","prim","Symbol","toPrimitive","undefined","res","call","TypeError","Number","superLoadIncrementScript","superLoadGetScript","noLoadGetScript","noLoadIncrementScript","RedisStore","prototype","loadIncrementScript","name","loadGetScript","RedisClientStore","constructor","service","options","sendCommand","args","Array","from","arguments","self","TraceUtils","debug","connectOptions","getApplication","getConfiguration","getSourceAt","host","port","client","createClient","connect","then","catch","error","message","warn","JSON","stringify","Promise","reject","finally","isOpen","disconnect","errDisconnect","incrementScriptSha","postInit","resolve","init","err","getScriptSha","sequence","exports"],"sources":["../src/RedisClientStore.js"],"sourcesContent":["import { TraceUtils } from '@themost/common';\nimport RedisStore from 'rate-limit-redis';\nimport { createClient } from 'redis';\nimport '@themost/promise-sequence';\n\nlet superLoadIncrementScript;\nlet superLoadGetScript;\n\nfunction noLoadGetScript() {\n //\n}\n\nfunction noLoadIncrementScript() {\n //\n}\n\nif (RedisStore.prototype.loadIncrementScript.name === 'loadIncrementScript') {\n // get super method for future use\n superLoadIncrementScript = RedisStore.prototype.loadIncrementScript;\n RedisStore.prototype.loadIncrementScript = noLoadIncrementScript;\n}\n\nif (RedisStore.prototype.loadGetScript.name === 'loadGetScript') {\n // get super method\n superLoadGetScript = RedisStore.prototype.loadGetScript;\n RedisStore.prototype.loadGetScript = noLoadGetScript;\n}\n\nclass RedisClientStore extends RedisStore {\n\n /**\n * @type {import('redis').RedisClientType}\n */\n client;\n\n /**\n * \n * @param {import('@themost/common').ApplicationService} service\n * @param {{windowMs: number}} options\n */\n constructor(service, options) {\n super({\n /**\n * @param {...string} args\n * @returns {Promise<*>}\n */\n sendCommand: function () {\n const args = Array.from(arguments);\n const self = this;\n if (args[0] === 'SCRIPT') {\n TraceUtils.debug('RedisClientStore: Creating new client for sending script command');\n const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {\n host: '127.0.0.1',\n port: 6379\n };\n const client = createClient(connectOptions);\n return client.connect().then(() => {\n return client.sendCommand(args);\n }).catch((error) => {\n if (error instanceof TypeError && error.message === 'Invalid argument type') {\n TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));\n }\n return Promise.reject(error);\n }).finally(() => {\n if (client.isOpen) {\n TraceUtils.debug('RedisClientStore: Closing client of sending script command');\n client.disconnect().catch((errDisconnect) => {\n TraceUtils.error(errDisconnect);\n });\n }\n });\n }\n if (self.client == null) {\n TraceUtils.debug('RedisClientStore: Creating store client for sending commands');\n const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {\n host: '127.0.0.1',\n port: 6379\n };\n self.client = createClient(connectOptions);\n }\n if (self.client.isOpen) {\n return self.client.sendCommand(args).catch((error) => {\n if (error instanceof TypeError && error.message === 'Invalid argument type') {\n TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));\n }\n return Promise.reject(error);\n });\n }\n TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');\n return self.client.connect().then(() => {\n // send load script commands once\n return (() => {\n if (self.incrementScriptSha == null) {\n return this.postInit();\n }\n return Promise.resolve();\n })().then(() => {\n // send command \n args[1] = self.incrementScriptSha;\n return self.client.sendCommand(args).catch((error) => {\n if (error instanceof TypeError && error.message === 'Invalid argument type') {\n TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));\n }\n return Promise.reject(error);\n });\n });\n });\n }\n });\n this.init(options);\n TraceUtils.debug('RedisClientStore: Starting up and loading increment and get scripts.');\n void this.postInit().then(() => {\n TraceUtils.debug('RedisClientStore: Successfully loaded increment and get scripts.');\n }).catch((err) => {\n TraceUtils.error('RedisClientStore: Failed to load increment and get scripts.');\n TraceUtils.error(err);\n });\n }\n async postInit() {\n const [incrementScriptSha, getScriptSha] = await Promise.sequence([\n () => superLoadIncrementScript.call(this),\n () => superLoadGetScript.call(this)\n ]);\n this.incrementScriptSha = incrementScriptSha;\n this.getScriptSha = getScriptSha;\n }\n\n}\n\nexport {\n RedisClientStore\n}"],"mappings":"6GAAA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACAA,OAAA,8BAAmC,SAAAE,uBAAAE,GAAA,UAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA,aAAAG,gBAAAH,GAAA,EAAAI,GAAA,EAAAC,KAAA,GAAAD,GAAA,GAAAE,cAAA,CAAAF,GAAA,MAAAA,GAAA,IAAAJ,GAAA,GAAAO,MAAA,CAAAC,cAAA,CAAAR,GAAA,EAAAI,GAAA,IAAAC,KAAA,EAAAA,KAAA,EAAAI,UAAA,QAAAC,YAAA,QAAAC,QAAA,kBAAAX,GAAA,CAAAI,GAAA,IAAAC,KAAA,SAAAL,GAAA,WAAAM,eAAAM,GAAA,OAAAR,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CAAC;YAChC,CAAC,CAAC;UACN,CAAC,CAAC;QACN,CAAC,CAAC;MACN;IACJ,CAAC,CAAC,CAAC,CA9EP;AACJ;AACA,OAFItD,eAAA,yBA+EI,IAAI,CAACoE,IAAI,CAAClC,OAAO,CAAC,CAClBO,kBAAU,CAACC,KAAK,CAAC,sEAAsE,CAAC;IACxF,KAAK,IAAI,CAACwB,QAAQ,EAAE,CAACd,IAAI,CAAC,MAAM;MAC5BX,kBAAU,CAACC,KAAK,CAAC,kEAAkE,CAAC;IACxF,CAAC,CAAC,CAACW,KAAK,CAAC,CAACgB,GAAG,KAAK;MACd5B,kBAAU,CAACa,KAAK,CAAC,6DAA6D,CAAC;MAC/Eb,kBAAU,CAACa,KAAK,CAACe,GAAG,CAAC;IACzB,CAAC,CAAC;EACN;EACA,MAAMH,QAAQA,CAAA,EAAG;IACb,MAAM,CAACD,kBAAkB,EAAEK,YAAY,CAAC,GAAG,MAAMX,OAAO,CAACY,QAAQ,CAAC;IAC9D,MAAMjD,wBAAwB,CAACH,IAAI,CAAC,IAAI,CAAC;IACzC,MAAMI,kBAAkB,CAACJ,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;;IACF,IAAI,CAAC8C,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACK,YAAY,GAAGA,YAAY;EACpC;;AAEJ,CAACE,OAAA,CAAAzC,gBAAA,GAAAA,gBAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universis/janitor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Universis api plugin for rate limiting requests",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"express-rate-limit": "^7.1.1",
|
|
33
33
|
"express-slow-down": "^1.6.0",
|
|
34
|
-
"rate-limit-redis": "^4.
|
|
34
|
+
"rate-limit-redis": "^4.2.0",
|
|
35
35
|
"redis": "^4.6.10"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
package/src/RedisClientStore.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { TraceUtils } from '@themost/common';
|
|
2
2
|
import RedisStore from 'rate-limit-redis';
|
|
3
3
|
import { createClient } from 'redis';
|
|
4
|
+
import '@themost/promise-sequence';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
let superLoadIncrementScript;
|
|
7
|
+
let superLoadGetScript;
|
|
7
8
|
|
|
8
9
|
function noLoadGetScript() {
|
|
9
10
|
//
|
|
@@ -13,11 +14,15 @@ function noLoadIncrementScript() {
|
|
|
13
14
|
//
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
if (
|
|
17
|
+
if (RedisStore.prototype.loadIncrementScript.name === 'loadIncrementScript') {
|
|
18
|
+
// get super method for future use
|
|
19
|
+
superLoadIncrementScript = RedisStore.prototype.loadIncrementScript;
|
|
17
20
|
RedisStore.prototype.loadIncrementScript = noLoadIncrementScript;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
if (
|
|
23
|
+
if (RedisStore.prototype.loadGetScript.name === 'loadGetScript') {
|
|
24
|
+
// get super method
|
|
25
|
+
superLoadGetScript = RedisStore.prototype.loadGetScript;
|
|
21
26
|
RedisStore.prototype.loadGetScript = noLoadGetScript;
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -52,7 +57,9 @@ class RedisClientStore extends RedisStore {
|
|
|
52
57
|
return client.connect().then(() => {
|
|
53
58
|
return client.sendCommand(args);
|
|
54
59
|
}).catch((error) => {
|
|
55
|
-
|
|
60
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
61
|
+
TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
62
|
+
}
|
|
56
63
|
return Promise.reject(error);
|
|
57
64
|
}).finally(() => {
|
|
58
65
|
if (client.isOpen) {
|
|
@@ -72,26 +79,50 @@ class RedisClientStore extends RedisStore {
|
|
|
72
79
|
self.client = createClient(connectOptions);
|
|
73
80
|
}
|
|
74
81
|
if (self.client.isOpen) {
|
|
75
|
-
return self.client.sendCommand(args)
|
|
82
|
+
return self.client.sendCommand(args).catch((error) => {
|
|
83
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
84
|
+
TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
85
|
+
}
|
|
86
|
+
return Promise.reject(error);
|
|
87
|
+
});
|
|
76
88
|
}
|
|
77
89
|
TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');
|
|
78
90
|
return self.client.connect().then(() => {
|
|
79
91
|
// send load script commands once
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
return (() => {
|
|
93
|
+
if (self.incrementScriptSha == null) {
|
|
94
|
+
return this.postInit();
|
|
95
|
+
}
|
|
96
|
+
return Promise.resolve();
|
|
97
|
+
})().then(() => {
|
|
86
98
|
// send command
|
|
87
99
|
args[1] = self.incrementScriptSha;
|
|
88
|
-
return self.client.sendCommand(args)
|
|
100
|
+
return self.client.sendCommand(args).catch((error) => {
|
|
101
|
+
if (error instanceof TypeError && error.message === 'Invalid argument type') {
|
|
102
|
+
TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
|
|
103
|
+
}
|
|
104
|
+
return Promise.reject(error);
|
|
105
|
+
});
|
|
89
106
|
});
|
|
90
107
|
});
|
|
91
108
|
}
|
|
92
109
|
});
|
|
93
|
-
this.init(options)
|
|
94
|
-
|
|
110
|
+
this.init(options);
|
|
111
|
+
TraceUtils.debug('RedisClientStore: Starting up and loading increment and get scripts.');
|
|
112
|
+
void this.postInit().then(() => {
|
|
113
|
+
TraceUtils.debug('RedisClientStore: Successfully loaded increment and get scripts.');
|
|
114
|
+
}).catch((err) => {
|
|
115
|
+
TraceUtils.error('RedisClientStore: Failed to load increment and get scripts.');
|
|
116
|
+
TraceUtils.error(err);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async postInit() {
|
|
120
|
+
const [incrementScriptSha, getScriptSha] = await Promise.sequence([
|
|
121
|
+
() => superLoadIncrementScript.call(this),
|
|
122
|
+
() => superLoadGetScript.call(this)
|
|
123
|
+
]);
|
|
124
|
+
this.incrementScriptSha = incrementScriptSha;
|
|
125
|
+
this.getScriptSha = getScriptSha;
|
|
95
126
|
}
|
|
96
127
|
|
|
97
128
|
}
|