@universis/janitor 1.4.1 → 1.5.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.
@@ -1,6 +1,6 @@
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 _ioredis = require("ioredis");
4
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);}
5
5
 
6
6
  let superLoadIncrementScript;
@@ -46,16 +46,15 @@ class RedisClientStore extends _rateLimitRedis.default {
46
46
  */
47
47
  sendCommand: function () {
48
48
  const args = Array.from(arguments);
49
+ const [command] = args.splice(0, 1);
49
50
  const self = this;
50
- if (args[0] === 'SCRIPT') {
51
+ if (command === 'SCRIPT') {
51
52
  const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
52
53
  host: '127.0.0.1',
53
54
  port: 6379
54
55
  };
55
- const client = (0, _redis.createClient)(connectOptions);
56
- return client.connect().then(() => {
57
- return client.sendCommand(args);
58
- }).catch((error) => {
56
+ const client = new _ioredis.Redis(connectOptions);
57
+ return client.call(command, args).catch((error) => {
59
58
  if (error instanceof TypeError && error.message === 'Invalid argument type') {
60
59
  _common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
61
60
  }
@@ -73,45 +72,30 @@ class RedisClientStore extends _rateLimitRedis.default {
73
72
  host: '127.0.0.1',
74
73
  port: 6379
75
74
  };
76
- self.client = (0, _redis.createClient)(connectOptions);
77
- // handle socket close error
78
- self.client.on('error', (err) => {
79
- _common.TraceUtils.error('An error occurred while using redis client as speed limit service client store.');
80
- _common.TraceUtils.error(err);
81
- // handle socket close error
82
- if (err.message === 'Socket closed unexpectedly') {
83
- _common.TraceUtils.info('Socket closed unexpectedly. Disconnecting...');
84
- // delete increment script sha
85
- self.incrementScriptSha = null;
86
- // quit client
87
- self.client.disconnect();
88
- }
89
- });
75
+ self.client = new _ioredis.Redis(connectOptions);
90
76
  }
91
77
  if (self.client.isOpen) {
92
- return self.client.sendCommand(args).catch((error) => {
78
+ return self.client.call(command, args).catch((error) => {
93
79
  if (error instanceof TypeError && error.message === 'Invalid argument type') {
94
80
  _common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
95
81
  }
96
82
  return Promise.reject(error);
97
83
  });
98
84
  }
99
- return self.client.connect().then(() => {
100
- // send load script commands once
101
- return (() => {
102
- if (self.incrementScriptSha == null) {
103
- return this.postInit();
85
+ // send load script commands once
86
+ return (() => {
87
+ if (self.incrementScriptSha == null) {
88
+ return this.postInit();
89
+ }
90
+ return Promise.resolve();
91
+ })().then(() => {
92
+ // send command
93
+ args[0] = self.incrementScriptSha;
94
+ return self.client.call(command, args).catch((error) => {
95
+ if (error instanceof TypeError && error.message === 'Invalid argument type') {
96
+ _common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
104
97
  }
105
- return Promise.resolve();
106
- })().then(() => {
107
- // send command
108
- args[1] = self.incrementScriptSha;
109
- return self.client.sendCommand(args).catch((error) => {
110
- if (error instanceof TypeError && error.message === 'Invalid argument type') {
111
- _common.TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
112
- }
113
- return Promise.reject(error);
114
- });
98
+ return Promise.reject(error);
115
99
  });
116
100
  });
117
101
  }
@@ -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","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","on","err","info","incrementScriptSha","postInit","resolve","init","debug","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 // handle socket close error\n self.client.on('error', (err) => {\n TraceUtils.error('An error occurred while using redis client as speed limit service client store.');\n TraceUtils.error(err);\n // handle socket close error\n if (err.message === 'Socket closed unexpectedly') {\n TraceUtils.info('Socket closed unexpectedly. Disconnecting...');\n // delete increment script sha\n self.incrementScriptSha = null;\n // quit client\n self.client.disconnect();\n }\n });\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,GAAA,sBAAAA,GAAA,WAAAE,SAAA,0DAAAP,IAAA,gBAAAF,MAAA,GAAAU,MAAA,EAAAT,KAAA;;AAEnC,IAAIU,wBAAwB;AAC5B,IAAIC,kBAAkB;;AAEtB,SAASC,eAAe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AI,CAAC,MAAM;MAC5BI,kBAAU,CAACiB,KAAK,CAAC,kEAAkE,CAAC;IACxF,CAAC,CAAC,CAACpB,KAAK,CAAC,CAACc,GAAG,KAAK;MACdX,kBAAU,CAACF,KAAK,CAAC,6DAA6D,CAAC;MAC/EE,kBAAU,CAACF,KAAK,CAACa,GAAG,CAAC;IACzB,CAAC,CAAC;EACN;EACA,MAAMG,QAAQA,CAAA,EAAG;IACb,MAAM,CAACD,kBAAkB,EAAEK,YAAY,CAAC,GAAG,MAAMd,OAAO,CAACe,QAAQ,CAAC;IAC9D,MAAMnD,wBAAwB,CAACH,IAAI,CAAC,IAAI,CAAC;IACzC,MAAMI,kBAAkB,CAACJ,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;;IACF,IAAI,CAACgD,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACK,YAAY,GAAGA,YAAY;EACpC;;AAEJ,CAACE,OAAA,CAAA3C,gBAAA,GAAAA,gBAAA"}
1
+ {"version":3,"file":"RedisClientStore.js","names":["_common","require","_rateLimitRedis","_interopRequireDefault","_ioredis","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","command","splice","self","connectOptions","getApplication","getConfiguration","getSourceAt","host","port","client","Redis","catch","error","message","TraceUtils","warn","JSON","stringify","Promise","reject","finally","isOpen","disconnect","errDisconnect","incrementScriptSha","postInit","resolve","then","init","debug","err","getScriptSha","sequence","exports"],"sources":["../src/RedisClientStore.js"],"sourcesContent":["import { TraceUtils } from '@themost/common';\nimport RedisStore from 'rate-limit-redis';\nimport { Redis } from 'ioredis';\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 [command] = args.splice(0, 1);\n const self = this;\n if (command === 'SCRIPT') {\n const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {\n host: '127.0.0.1',\n port: 6379\n };\n const client = new Redis(connectOptions);\n return client.call(command, 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 }).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 = new Redis(connectOptions);\n }\n if (self.client.isOpen) {\n return self.client.call(command, 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 // 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[0] = self.incrementScriptSha;\n return self.client.call(command, 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 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,QAAA,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,K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AU,CAACF,KAAK,CAAC,6DAA6D,CAAC;MAC/EE,kBAAU,CAACF,KAAK,CAACkB,GAAG,CAAC;IACzB,CAAC,CAAC;EACN;EACA,MAAML,QAAQA,CAAA,EAAG;IACb,MAAM,CAACD,kBAAkB,EAAEO,YAAY,CAAC,GAAG,MAAMb,OAAO,CAACc,QAAQ,CAAC;IAC9D,MAAMlD,wBAAwB,CAACH,IAAI,CAAC,IAAI,CAAC;IACzC,MAAMI,kBAAkB,CAACJ,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;;IACF,IAAI,CAAC6C,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACO,YAAY,GAAGA,YAAY;EACpC;;AAEJ,CAACE,OAAA,CAAA1C,gBAAA,GAAAA,gBAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@universis/janitor",
3
- "version": "1.4.1",
3
+ "version": "1.5.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,8 +31,8 @@
31
31
  "dependencies": {
32
32
  "express-rate-limit": "^7.1.1",
33
33
  "express-slow-down": "^1.6.0",
34
- "rate-limit-redis": "^4.2.0",
35
- "redis": "^4.6.10"
34
+ "ioredis": "^5.3.2",
35
+ "rate-limit-redis": "^4.2.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@themost/common": "^2",
@@ -50,6 +50,7 @@
50
50
  "@babel/register": "^7.21.0",
51
51
  "@rollup/plugin-babel": "^6.0.3",
52
52
  "@types/express": "^4.17.6",
53
+ "@types/ioredis": "^5.0.0",
53
54
  "@types/jasmine": "^3.5.10",
54
55
  "dotenv": "^8.2.0",
55
56
  "eslint": "^8.39.0",
@@ -1,6 +1,6 @@
1
1
  import { TraceUtils } from '@themost/common';
2
2
  import RedisStore from 'rate-limit-redis';
3
- import { createClient } from 'redis';
3
+ import { Redis } from 'ioredis';
4
4
  import '@themost/promise-sequence';
5
5
 
6
6
  let superLoadIncrementScript;
@@ -46,16 +46,15 @@ class RedisClientStore extends RedisStore {
46
46
  */
47
47
  sendCommand: function () {
48
48
  const args = Array.from(arguments);
49
+ const [command] = args.splice(0, 1);
49
50
  const self = this;
50
- if (args[0] === 'SCRIPT') {
51
+ if (command === 'SCRIPT') {
51
52
  const connectOptions = service.getApplication().getConfiguration().getSourceAt('settings/redis/options') || {
52
53
  host: '127.0.0.1',
53
54
  port: 6379
54
55
  };
55
- const client = createClient(connectOptions);
56
- return client.connect().then(() => {
57
- return client.sendCommand(args);
58
- }).catch((error) => {
56
+ const client = new Redis(connectOptions);
57
+ return client.call(command, args).catch((error) => {
59
58
  if (error instanceof TypeError && error.message === 'Invalid argument type') {
60
59
  TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
61
60
  }
@@ -73,45 +72,30 @@ class RedisClientStore extends RedisStore {
73
72
  host: '127.0.0.1',
74
73
  port: 6379
75
74
  };
76
- self.client = createClient(connectOptions);
77
- // handle socket close error
78
- self.client.on('error', (err) => {
79
- TraceUtils.error('An error occurred while using redis client as speed limit service client store.');
80
- TraceUtils.error(err);
81
- // handle socket close error
82
- if (err.message === 'Socket closed unexpectedly') {
83
- TraceUtils.info('Socket closed unexpectedly. Disconnecting...');
84
- // delete increment script sha
85
- self.incrementScriptSha = null;
86
- // quit client
87
- self.client.disconnect();
88
- }
89
- });
75
+ self.client = new Redis(connectOptions);
90
76
  }
91
77
  if (self.client.isOpen) {
92
- return self.client.sendCommand(args).catch((error) => {
78
+ return self.client.call(command, args).catch((error) => {
93
79
  if (error instanceof TypeError && error.message === 'Invalid argument type') {
94
80
  TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
95
81
  }
96
82
  return Promise.reject(error);
97
83
  });
98
84
  }
99
- return self.client.connect().then(() => {
100
- // send load script commands once
101
- return (() => {
102
- if (self.incrementScriptSha == null) {
103
- return this.postInit();
85
+ // send load script commands once
86
+ return (() => {
87
+ if (self.incrementScriptSha == null) {
88
+ return this.postInit();
89
+ }
90
+ return Promise.resolve();
91
+ })().then(() => {
92
+ // send command
93
+ args[0] = self.incrementScriptSha;
94
+ return self.client.call(command, args).catch((error) => {
95
+ if (error instanceof TypeError && error.message === 'Invalid argument type') {
96
+ TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
104
97
  }
105
- return Promise.resolve();
106
- })().then(() => {
107
- // send command
108
- args[1] = self.incrementScriptSha;
109
- return self.client.sendCommand(args).catch((error) => {
110
- if (error instanceof TypeError && error.message === 'Invalid argument type') {
111
- TraceUtils.warn('RedisClientStore: Invalid argument type: ' + JSON.stringify(args));
112
- }
113
- return Promise.reject(error);
114
- });
98
+ return Promise.reject(error);
115
99
  });
116
100
  });
117
101
  }