@universis/janitor 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/RedisClientStore.js +31 -16
- package/dist/RedisClientStore.js.map +1 -1
- package/package.json +1 -1
- package/src/RedisClientStore.js +31 -16
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
|
|
|
@@ -43,7 +48,6 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
43
48
|
const args = Array.from(arguments);
|
|
44
49
|
const self = this;
|
|
45
50
|
if (args[0] === 'SCRIPT') {
|
|
46
|
-
_common.TraceUtils.debug('RedisClientStore: Creating new client for sending script command');
|
|
47
51
|
const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
|
|
48
52
|
host: '127.0.0.1',
|
|
49
53
|
port: 6379
|
|
@@ -58,7 +62,6 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
58
62
|
return Promise.reject(error);
|
|
59
63
|
}).finally(() => {
|
|
60
64
|
if (client.isOpen) {
|
|
61
|
-
_common.TraceUtils.debug('RedisClientStore: Closing client of sending script command');
|
|
62
65
|
client.disconnect().catch((errDisconnect) => {
|
|
63
66
|
_common.TraceUtils.error(errDisconnect);
|
|
64
67
|
});
|
|
@@ -66,7 +69,6 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
66
69
|
});
|
|
67
70
|
}
|
|
68
71
|
if (self.client == null) {
|
|
69
|
-
_common.TraceUtils.debug('RedisClientStore: Creating store client for sending commands');
|
|
70
72
|
const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
|
|
71
73
|
host: '127.0.0.1',
|
|
72
74
|
port: 6379
|
|
@@ -81,15 +83,14 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
81
83
|
return Promise.reject(error);
|
|
82
84
|
});
|
|
83
85
|
}
|
|
84
|
-
_common.TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');
|
|
85
86
|
return self.client.connect().then(() => {
|
|
86
87
|
// send load script commands once
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
return (() => {
|
|
89
|
+
if (self.incrementScriptSha == null) {
|
|
90
|
+
return this.postInit();
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
})().then(() => {
|
|
93
94
|
// send command
|
|
94
95
|
args[1] = self.incrementScriptSha;
|
|
95
96
|
return self.client.sendCommand(args).catch((error) => {
|
|
@@ -103,7 +104,21 @@ class RedisClientStore extends _rateLimitRedis.default {
|
|
|
103
104
|
}
|
|
104
105
|
}); /**
|
|
105
106
|
* @type {import('redis').RedisClientType}
|
|
106
|
-
*/_defineProperty(this, "client", void 0);this.init(options);
|
|
107
|
+
*/_defineProperty(this, "client", void 0);this.init(options);_common.TraceUtils.debug('RedisClientStore: Starting up and loading increment and get scripts.');
|
|
108
|
+
void this.postInit().then(() => {
|
|
109
|
+
_common.TraceUtils.debug('RedisClientStore: Successfully loaded increment and get scripts.');
|
|
110
|
+
}).catch((err) => {
|
|
111
|
+
_common.TraceUtils.error('RedisClientStore: Failed to load increment and get scripts.');
|
|
112
|
+
_common.TraceUtils.error(err);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async postInit() {
|
|
116
|
+
const [incrementScriptSha, getScriptSha] = await Promise.sequence([
|
|
117
|
+
() => superLoadIncrementScript.call(this),
|
|
118
|
+
() => superLoadGetScript.call(this)]);
|
|
119
|
+
|
|
120
|
+
this.incrementScriptSha = incrementScriptSha;
|
|
121
|
+
this.getScriptSha = getScriptSha;
|
|
107
122
|
}
|
|
108
123
|
|
|
109
124
|
}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","message","warn","JSON","stringify","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 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 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).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 \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,C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AACC,OAAO,KAAK,uBAAuB,EAAE;gBACzEd,kBAAU,CAACe,IAAI,CAAC,2CAA2C,GAAGC,IAAI,CAACC,SAAS,CAACtB,IAAI,CAAC,CAAC;cACvF;cACA,OAAOuB,OAAO,CAACC,MAAM,CAACN,KAAK,CAAC;YAChC,CAAC,CAAC;UACN,CAAC,CAAC;QACN,CAAC,CAAC;MACN;IACJ,CAAC,CAAC,CAAC,CA9EP;AACJ;AACA,OAFIrD,eAAA,yBA+EI,IAAI,CAACoE,IAAI,CAACnC,OAAO,CAAC;EAEtB;;AAEJ,CAACoC,OAAA,CAAAvC,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","connectOptions","getApplication","getConfiguration","getSourceAt","host","port","client","createClient","connect","then","catch","error","message","TraceUtils","warn","JSON","stringify","Promise","reject","finally","isOpen","disconnect","errDisconnect","incrementScriptSha","postInit","resolve","init","debug","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 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 client.disconnect().catch((errDisconnect) => {\n TraceUtils.error(errDisconnect);\n });\n }\n });\n }\n if (self.client == null) {\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 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,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,IAAA,GAAAF,KAAA,CAAAG,MAAA,CAAAC,WAAA,MAAAF,IAAA,KAAAG,SAAA,OAAAC,GAAA,GAAAJ,IAAA,CAAAK,IAAA,CAAAP,KAAA,EAAAC,IAAA,0BAAAK,GAA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,CAAA,EAAG;IACb,MAAM,CAACD,kBAAkB,EAAEM,YAAY,CAAC,GAAG,MAAMZ,OAAO,CAACa,QAAQ,CAAC;IAC9D,MAAMjD,wBAAwB,CAACH,IAAI,CAAC,IAAI,CAAC;IACzC,MAAMI,kBAAkB,CAACJ,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;;IACF,IAAI,CAAC6C,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACM,YAAY,GAAGA,YAAY;EACpC;;AAEJ,CAACE,OAAA,CAAAzC,gBAAA,GAAAA,gBAAA"}
|
package/package.json
CHANGED
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
|
|
|
@@ -43,7 +48,6 @@ class RedisClientStore extends RedisStore {
|
|
|
43
48
|
const args = Array.from(arguments);
|
|
44
49
|
const self = this;
|
|
45
50
|
if (args[0] === 'SCRIPT') {
|
|
46
|
-
TraceUtils.debug('RedisClientStore: Creating new client for sending script command');
|
|
47
51
|
const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
|
|
48
52
|
host: '127.0.0.1',
|
|
49
53
|
port: 6379
|
|
@@ -58,7 +62,6 @@ class RedisClientStore extends RedisStore {
|
|
|
58
62
|
return Promise.reject(error);
|
|
59
63
|
}).finally(() => {
|
|
60
64
|
if (client.isOpen) {
|
|
61
|
-
TraceUtils.debug('RedisClientStore: Closing client of sending script command');
|
|
62
65
|
client.disconnect().catch((errDisconnect) => {
|
|
63
66
|
TraceUtils.error(errDisconnect);
|
|
64
67
|
});
|
|
@@ -66,7 +69,6 @@ class RedisClientStore extends RedisStore {
|
|
|
66
69
|
});
|
|
67
70
|
}
|
|
68
71
|
if (self.client == null) {
|
|
69
|
-
TraceUtils.debug('RedisClientStore: Creating store client for sending commands');
|
|
70
72
|
const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
|
|
71
73
|
host: '127.0.0.1',
|
|
72
74
|
port: 6379
|
|
@@ -81,15 +83,14 @@ class RedisClientStore extends RedisStore {
|
|
|
81
83
|
return Promise.reject(error);
|
|
82
84
|
});
|
|
83
85
|
}
|
|
84
|
-
TraceUtils.debug('RedisClientStore: Opening redis store client for sending commands');
|
|
85
86
|
return self.client.connect().then(() => {
|
|
86
87
|
// send load script commands once
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
return (() => {
|
|
89
|
+
if (self.incrementScriptSha == null) {
|
|
90
|
+
return this.postInit();
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
})().then(() => {
|
|
93
94
|
// send command
|
|
94
95
|
args[1] = self.incrementScriptSha;
|
|
95
96
|
return self.client.sendCommand(args).catch((error) => {
|
|
@@ -102,8 +103,22 @@ class RedisClientStore extends RedisStore {
|
|
|
102
103
|
});
|
|
103
104
|
}
|
|
104
105
|
});
|
|
105
|
-
this.init(options)
|
|
106
|
-
|
|
106
|
+
this.init(options);
|
|
107
|
+
TraceUtils.debug('RedisClientStore: Starting up and loading increment and get scripts.');
|
|
108
|
+
void this.postInit().then(() => {
|
|
109
|
+
TraceUtils.debug('RedisClientStore: Successfully loaded increment and get scripts.');
|
|
110
|
+
}).catch((err) => {
|
|
111
|
+
TraceUtils.error('RedisClientStore: Failed to load increment and get scripts.');
|
|
112
|
+
TraceUtils.error(err);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async postInit() {
|
|
116
|
+
const [incrementScriptSha, getScriptSha] = await Promise.sequence([
|
|
117
|
+
() => superLoadIncrementScript.call(this),
|
|
118
|
+
() => superLoadGetScript.call(this)
|
|
119
|
+
]);
|
|
120
|
+
this.incrementScriptSha = incrementScriptSha;
|
|
121
|
+
this.getScriptSha = getScriptSha;
|
|
107
122
|
}
|
|
108
123
|
|
|
109
124
|
}
|