eip-cloud-services 1.1.8 → 1.1.10
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/eslint.config.js +51 -0
- package/package.json +2 -1
- package/src/redis.js +60 -9
package/eslint.config.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module.exports = [ {
|
|
2
|
+
languageOptions: {
|
|
3
|
+
ecmaVersion: 2021,
|
|
4
|
+
sourceType: 'module',
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaFeatures: {
|
|
7
|
+
jsx: true
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
rules: {
|
|
12
|
+
'max-len': 'off',
|
|
13
|
+
'semi': [ 'error', 'always' ],
|
|
14
|
+
'no-underscore-dangle': 'off',
|
|
15
|
+
'class-methods-use-this': 'off',
|
|
16
|
+
'no-param-reassign': 'off',
|
|
17
|
+
'no-restricted-syntax': 'warn',
|
|
18
|
+
'no-continue': 'warn',
|
|
19
|
+
'consistent-return': 'warn',
|
|
20
|
+
'guard-for-in': 'warn',
|
|
21
|
+
'import/no-commonjs': 'off',
|
|
22
|
+
'import/no-dynamic-require': 'off',
|
|
23
|
+
'new-cap': 'warn',
|
|
24
|
+
'no-nested-ternary': 'off',
|
|
25
|
+
'prefer-const': 'error',
|
|
26
|
+
'newline-before-return': [ 'error', 'always' ],
|
|
27
|
+
'quotes': [ 'error', 'single' ],
|
|
28
|
+
'no-multiple-empty-lines': [ 'error', { max: 1 } ],
|
|
29
|
+
'space-before-function-paren': [ 'error', 'always' ],
|
|
30
|
+
'func-call-spacing': [ 'error', 'always' ],
|
|
31
|
+
'block-spacing': [ 'error', 'always' ],
|
|
32
|
+
'space-in-parens': [ 'error', 'always' ],
|
|
33
|
+
'object-curly-spacing': [ 'error', 'always' ],
|
|
34
|
+
'no-spaced-func': 'off',
|
|
35
|
+
'space-unary-ops': [ 'error', { 'words': true, 'nonwords': false, 'overrides': { 'new': false, '++': false } } ],
|
|
36
|
+
'arrow-spacing': [ 'error', { 'before': true, 'after': true } ],
|
|
37
|
+
'indent': [ 'error', 4, { 'SwitchCase': 1 } ],
|
|
38
|
+
'key-spacing': [ 'error', { 'beforeColon': false, 'afterColon': true } ],
|
|
39
|
+
'computed-property-spacing': [ 'error', 'always' ],
|
|
40
|
+
'array-bracket-spacing': [ 'error', 'never' ],
|
|
41
|
+
'comma-spacing': [ 'error', { 'before': false, 'after': true } ],
|
|
42
|
+
'brace-style': [ 'error', 'stroustrup' ],
|
|
43
|
+
'space-infix-ops': 'error',
|
|
44
|
+
'keyword-spacing': [ 'error', { overrides: {
|
|
45
|
+
if: { after: true },
|
|
46
|
+
for: { after: true },
|
|
47
|
+
while: { after: true }
|
|
48
|
+
} } ],
|
|
49
|
+
'array-bracket-spacing': [ 'error', 'always' ],
|
|
50
|
+
},
|
|
51
|
+
} ];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eip-cloud-services",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.10",
|
|
4
4
|
"description": "Houses a collection of helpers for connecting with Cloud services.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"chai": "^4.3.7",
|
|
13
|
+
"eslint": "^9.1.1",
|
|
13
14
|
"mocha": "^10.2.0"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
package/src/redis.js
CHANGED
|
@@ -78,6 +78,13 @@ const getClient = async ( clientId = 'main' ) => {
|
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
const deleteClient = async ( clientId ) => {
|
|
82
|
+
if ( clients[ clientId ] ){
|
|
83
|
+
await clients[ clientId ].quit ();
|
|
84
|
+
delete clients[ clientId ];
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
81
88
|
/**
|
|
82
89
|
* Retrieves a Redis client based on the provided identifier.
|
|
83
90
|
* This function is primarily used internally within the module.
|
|
@@ -600,6 +607,46 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
|
|
|
600
607
|
}
|
|
601
608
|
};
|
|
602
609
|
|
|
610
|
+
/**
|
|
611
|
+
* Increments the integer value of a key by the provided amount, or by 1 if no amount is specified.
|
|
612
|
+
* @param {string} key - The key whose value is to be incremented.
|
|
613
|
+
* @param {number} [increment=1] - The amount by which to increment the key's value. Defaults to 1.
|
|
614
|
+
* @returns {Promise<number>} - A promise that resolves with the new value after increment.
|
|
615
|
+
*
|
|
616
|
+
* @description This method uses the Redis INCRBY command to increment the value of the key.
|
|
617
|
+
* It defaults to incrementing by 1 if no specific increment value is provided.
|
|
618
|
+
*/
|
|
619
|
+
exports.increment = async (key, increment = 1) => {
|
|
620
|
+
try {
|
|
621
|
+
const client = await getClient();
|
|
622
|
+
const fullKey = (!config?.redis?.prefix || key.startsWith(config?.redis?.prefix)) ? key : config?.redis?.prefix + key;
|
|
623
|
+
return await client.incrby(fullKey, increment);
|
|
624
|
+
} catch (error) {
|
|
625
|
+
console.log('REDIS LIB ERROR [increment]', error);
|
|
626
|
+
throw error;
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Decrements the integer value of a key by the provided amount, or by 1 if no amount is specified.
|
|
632
|
+
* @param {string} key - The key whose value is to be decremented.
|
|
633
|
+
* @param {number} [decrement=1] - The amount by which to decrement the key's value. Defaults to 1.
|
|
634
|
+
* @returns {Promise<number>} - A promise that resolves with the new value after decrement.
|
|
635
|
+
*
|
|
636
|
+
* @description This method uses the Redis DECRBY command to decrement the value of the key.
|
|
637
|
+
* It defaults to decrementing by 1 if no specific decrement value is provided.
|
|
638
|
+
*/
|
|
639
|
+
exports.decrement = async (key, decrement = 1) => {
|
|
640
|
+
try {
|
|
641
|
+
const client = await getClient();
|
|
642
|
+
const fullKey = (!config?.redis?.prefix || key.startsWith(config?.redis?.prefix)) ? key : config?.redis?.prefix + key;
|
|
643
|
+
return await client.decrby(fullKey, decrement);
|
|
644
|
+
} catch (error) {
|
|
645
|
+
console.log('REDIS LIB ERROR [decrement]', error);
|
|
646
|
+
throw error;
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
|
|
603
650
|
/**
|
|
604
651
|
* Retrieves the expiration time of a key in seconds.
|
|
605
652
|
* @param {string} key - The key to get the expiration time of.
|
|
@@ -700,6 +747,7 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
700
747
|
messageHandler ( message );
|
|
701
748
|
}
|
|
702
749
|
} );
|
|
750
|
+
|
|
703
751
|
await client.subscribe ( channel );
|
|
704
752
|
|
|
705
753
|
return;
|
|
@@ -721,21 +769,24 @@ exports.subscribe = async ( channel, messageHandler ) => {
|
|
|
721
769
|
*
|
|
722
770
|
* @description This method unsubscribes from the specified channel in Redis for incoming messages.
|
|
723
771
|
*/
|
|
724
|
-
exports.unsubscribe = async (channel) => {
|
|
772
|
+
exports.unsubscribe = async ( channel ) => {
|
|
725
773
|
try {
|
|
726
|
-
if (typeof channel === 'string') {
|
|
727
|
-
const client = await getClient(`${channel}_sub`);
|
|
774
|
+
if ( typeof channel === 'string' ) {
|
|
775
|
+
const client = await getClient ( `${channel}_sub` );
|
|
728
776
|
|
|
729
|
-
client.removeAllListeners('message');
|
|
777
|
+
client.removeAllListeners ( 'message' );
|
|
730
778
|
|
|
731
|
-
await client.unsubscribe(channel);
|
|
779
|
+
await client.unsubscribe ( channel );
|
|
780
|
+
await deleteClient ( `${channel}_sub` );
|
|
732
781
|
|
|
733
782
|
return;
|
|
734
|
-
} else {
|
|
735
|
-
throw new Error('redis.unsubscribe expects a string channel and a function as handler');
|
|
736
783
|
}
|
|
737
|
-
|
|
738
|
-
|
|
784
|
+
else {
|
|
785
|
+
throw new Error ( 'redis.unsubscribe expects a string channel and a function as handler' );
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
catch ( error ) {
|
|
789
|
+
console.error ( 'REDIS LIB ERROR [unsubscribe]', error );
|
|
739
790
|
throw error;
|
|
740
791
|
}
|
|
741
792
|
};
|