@autofleet/matmon 2.4.0 → 2.4.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/README.md +16 -14
- package/lib/index.cjs +2 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +70 -0
- package/lib/index.d.ts +70 -4
- package/lib/index.js +2 -15
- package/lib/index.js.map +1 -0
- package/package.json +25 -11
- package/lib/cache.d.ts +0 -25
- package/lib/cache.js +0 -140
- package/lib/cache.test.d.ts +0 -1
- package/lib/cache.test.js +0 -168
- package/lib/locking.d.ts +0 -3
- package/lib/locking.js +0 -18
- package/lib/logger.d.ts +0 -2
- package/lib/logger.js +0 -8
- package/lib/orm-cache/adapter.d.ts +0 -17
- package/lib/orm-cache/adapter.js +0 -2
- package/lib/orm-cache/errors.d.ts +0 -2
- package/lib/orm-cache/errors.js +0 -6
- package/lib/orm-cache/index.d.ts +0 -12
- package/lib/orm-cache/index.js +0 -39
- package/lib/orm-cache/sequelize-adapter.d.ts +0 -13
- package/lib/orm-cache/sequelize-adapter.js +0 -156
- package/lib/promise-utils.d.ts +0 -1
- package/lib/promise-utils.js +0 -19
- package/lib/redis/errors.d.ts +0 -13
- package/lib/redis/errors.js +0 -25
- package/lib/redis/index.d.ts +0 -21
- package/lib/redis/index.js +0 -148
- package/lib/redis/index.test.d.ts +0 -1
- package/lib/redis/index.test.js +0 -62
package/lib/redis/errors.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RedisLockError = exports.RedisCacheError = void 0;
|
|
4
|
-
const redis_1 = require("redis");
|
|
5
|
-
class RedisCacheError extends redis_1.RedisError {
|
|
6
|
-
constructor(msg, err) {
|
|
7
|
-
super();
|
|
8
|
-
this.innerError = err;
|
|
9
|
-
this.message = msg;
|
|
10
|
-
}
|
|
11
|
-
toString() {
|
|
12
|
-
return `${this.message}: ${this.innerError}`;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.RedisCacheError = RedisCacheError;
|
|
16
|
-
class RedisLockError extends RedisCacheError {
|
|
17
|
-
constructor(msg, err, key) {
|
|
18
|
-
super(msg, err);
|
|
19
|
-
this.key = key;
|
|
20
|
-
}
|
|
21
|
-
toString() {
|
|
22
|
-
return `${this.message} (${this.key}): ${this.innerError}`;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.RedisLockError = RedisLockError;
|
package/lib/redis/index.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
declare class RedisCache {
|
|
2
|
-
private client;
|
|
3
|
-
private locker;
|
|
4
|
-
private lockTimeout;
|
|
5
|
-
private lockDuration;
|
|
6
|
-
locks: any;
|
|
7
|
-
private baseTTL;
|
|
8
|
-
private lockRetries;
|
|
9
|
-
keyPrefix: string;
|
|
10
|
-
useLock: boolean;
|
|
11
|
-
constructor(options: any);
|
|
12
|
-
get(key: any): Promise<any>;
|
|
13
|
-
getMultiple(keys: any): Promise<any>;
|
|
14
|
-
set(key: any, value: any): Promise<void>;
|
|
15
|
-
setMultiple(keyValues: Record<string, any>): Promise<any>;
|
|
16
|
-
remove(key: any): Promise<void>;
|
|
17
|
-
removeMultiple(keys: any): Promise<void>;
|
|
18
|
-
getClient(): any;
|
|
19
|
-
private lock;
|
|
20
|
-
}
|
|
21
|
-
export default RedisCache;
|
package/lib/redis/index.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const redis_1 = __importDefault(require("redis"));
|
|
7
|
-
const redis_lock_1 = __importDefault(require("redis-lock"));
|
|
8
|
-
const util_1 = require("util");
|
|
9
|
-
const errors_1 = require("./errors");
|
|
10
|
-
const logger_1 = __importDefault(require("../logger"));
|
|
11
|
-
const promise_utils_1 = require("../promise-utils");
|
|
12
|
-
(0, promise_utils_1.promisifyAll)(redis_1.default.RedisClient.prototype);
|
|
13
|
-
(0, promise_utils_1.promisifyAll)(redis_1.default.Multi.prototype);
|
|
14
|
-
const { env } = process;
|
|
15
|
-
const HOST = env.REDIS_HOST_NAME || '127.0.0.1';
|
|
16
|
-
const PORT = env.REDIS_HOST_PORT || 6379;
|
|
17
|
-
const SERVICE_NAME = env.AF_SERVICE_NAME;
|
|
18
|
-
const DEFAULT_LOCK_TIMEOUT = 5000;
|
|
19
|
-
const DEFAULT_LOCK_DURATION = 1000;
|
|
20
|
-
const DEFAULT_BASE_TTL = 3600;
|
|
21
|
-
const KEY_PREFIX = SERVICE_NAME + ':';
|
|
22
|
-
if (!SERVICE_NAME) {
|
|
23
|
-
throw new Error('SERVICE_NAME cannot be null, please check your env.AF_SERVICE_NAME');
|
|
24
|
-
}
|
|
25
|
-
class RedisCache {
|
|
26
|
-
constructor(options) {
|
|
27
|
-
this.client = redis_1.default.createClient({
|
|
28
|
-
host: options.host || HOST,
|
|
29
|
-
port: options.port || PORT,
|
|
30
|
-
});
|
|
31
|
-
this.locker = (0, util_1.promisify)((0, redis_lock_1.default)(this.client, options.lockRetries ?? 10));
|
|
32
|
-
this.lockTimeout = options.lockTimeout ?? DEFAULT_LOCK_TIMEOUT;
|
|
33
|
-
this.lockDuration = options.lockDuration ?? DEFAULT_LOCK_DURATION;
|
|
34
|
-
this.baseTTL = options.ttl ?? DEFAULT_BASE_TTL;
|
|
35
|
-
this.locks = {};
|
|
36
|
-
this.useLock = !!options.useLock;
|
|
37
|
-
this.keyPrefix = KEY_PREFIX;
|
|
38
|
-
}
|
|
39
|
-
async get(key) {
|
|
40
|
-
const keyWithPrefix = KEY_PREFIX + key;
|
|
41
|
-
let value;
|
|
42
|
-
try {
|
|
43
|
-
// Try to get the value from redis.
|
|
44
|
-
value = await this.client.getAsync(keyWithPrefix);
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
throw new errors_1.RedisCacheError('Failed to get a value', err);
|
|
48
|
-
}
|
|
49
|
-
if (this.useLock) {
|
|
50
|
-
let lock;
|
|
51
|
-
try {
|
|
52
|
-
// Try to lock the key.
|
|
53
|
-
lock = await this.lock(keyWithPrefix);
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
throw new errors_1.RedisLockError('Failed to lock key', err, keyWithPrefix);
|
|
57
|
-
}
|
|
58
|
-
// If the lock did not fail, add it to a locks dictionary.
|
|
59
|
-
this.locks[keyWithPrefix] = lock;
|
|
60
|
-
}
|
|
61
|
-
return JSON.parse(value);
|
|
62
|
-
}
|
|
63
|
-
async getMultiple(keys) {
|
|
64
|
-
const keysWithPrefix = keys.map(key => KEY_PREFIX + key);
|
|
65
|
-
let values;
|
|
66
|
-
try {
|
|
67
|
-
if (keysWithPrefix.length === 0) {
|
|
68
|
-
return [];
|
|
69
|
-
}
|
|
70
|
-
// Try to get the value from redis.
|
|
71
|
-
values = await this.client.mgetAsync(keysWithPrefix);
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
throw new errors_1.RedisCacheError('Failed to get a value', err);
|
|
75
|
-
}
|
|
76
|
-
return values.map(value => JSON.parse(value));
|
|
77
|
-
}
|
|
78
|
-
async set(key, value) {
|
|
79
|
-
const keyWithPrefix = KEY_PREFIX + key;
|
|
80
|
-
const ttl = parseInt(String(this.baseTTL * (Math.random() + 1)), 10);
|
|
81
|
-
try {
|
|
82
|
-
await this.client.setAsync(keyWithPrefix, JSON.stringify(value), 'EX', ttl);
|
|
83
|
-
if (this.locks[keyWithPrefix]) {
|
|
84
|
-
await this.locks[keyWithPrefix]();
|
|
85
|
-
delete this.locks[keyWithPrefix];
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
catch (err) {
|
|
89
|
-
throw new errors_1.RedisCacheError('Failed to set a key-value pair', err);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async setMultiple(keyValues) {
|
|
93
|
-
if (typeof keyValues !== 'object') {
|
|
94
|
-
const error = new errors_1.RedisCacheError('keyValues must be an object', new Error('keyValues must be an object'));
|
|
95
|
-
logger_1.default.error('keyValues must be an object', { error });
|
|
96
|
-
throw error;
|
|
97
|
-
}
|
|
98
|
-
if (keyValues === null || keyValues === undefined || Object.keys(keyValues).length === 0) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const keyValuesWithPrefix = Object.entries(keyValues).map(([key, value]) => [KEY_PREFIX + key, JSON.stringify(value)]);
|
|
102
|
-
const ttl = Math.trunc(this.baseTTL * (Math.random() + 1));
|
|
103
|
-
try {
|
|
104
|
-
const multi = this.client.multi();
|
|
105
|
-
const setPromise = multi.msetAsync(...keyValuesWithPrefix.flat());
|
|
106
|
-
keyValuesWithPrefix.map(([key]) => {
|
|
107
|
-
return multi.expireAsync(key, ttl);
|
|
108
|
-
});
|
|
109
|
-
await multi.exec();
|
|
110
|
-
return setPromise;
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
throw new errors_1.RedisCacheError('Failed to set multiple key-value pairs', err);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
async remove(key) {
|
|
117
|
-
const keyWithPrefix = KEY_PREFIX + key;
|
|
118
|
-
try {
|
|
119
|
-
if (this.locks[keyWithPrefix]) {
|
|
120
|
-
await this.locks[keyWithPrefix]();
|
|
121
|
-
delete this.locks[keyWithPrefix];
|
|
122
|
-
}
|
|
123
|
-
await this.client.delAsync(keyWithPrefix);
|
|
124
|
-
}
|
|
125
|
-
catch (err) {
|
|
126
|
-
throw new errors_1.RedisCacheError(`Failed to delete key ${keyWithPrefix}`, err);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async removeMultiple(keys) {
|
|
130
|
-
const keysWithPrefix = keys.map(key => KEY_PREFIX + key);
|
|
131
|
-
try {
|
|
132
|
-
await this.client.delAsync(keysWithPrefix);
|
|
133
|
-
}
|
|
134
|
-
catch (err) {
|
|
135
|
-
throw new errors_1.RedisCacheError(`Failed to delete multiple keys ${keysWithPrefix.join('|')}`, err);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
getClient() {
|
|
139
|
-
return this.client;
|
|
140
|
-
}
|
|
141
|
-
lock(key) {
|
|
142
|
-
return Promise.race([
|
|
143
|
-
this.locker(key, this.lockDuration),
|
|
144
|
-
new Promise((resolve, reject) => setTimeout(() => reject(), this.lockTimeout)),
|
|
145
|
-
]);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
exports.default = RedisCache;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/redis/index.test.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const redis_1 = __importDefault(require("redis"));
|
|
7
|
-
const vitest_1 = require("vitest");
|
|
8
|
-
const index_1 = __importDefault(require("./index"));
|
|
9
|
-
const promise_utils_1 = require("../promise-utils");
|
|
10
|
-
(0, promise_utils_1.promisifyAll)(redis_1.default.RedisClient.prototype);
|
|
11
|
-
(0, vitest_1.describe)('RedisCache', () => {
|
|
12
|
-
let redisCache;
|
|
13
|
-
const testClient = redis_1.default.createClient();
|
|
14
|
-
(0, vitest_1.beforeEach)(() => {
|
|
15
|
-
redisCache = new index_1.default({});
|
|
16
|
-
});
|
|
17
|
-
(0, vitest_1.describe)('setMultiple', () => {
|
|
18
|
-
(0, vitest_1.it)('should set multiple key-value pairs in redis', async () => {
|
|
19
|
-
const keyValues = { testKey1: 'testValue1', testKey2: 'testValue2' };
|
|
20
|
-
const keyValuesArray = Object.entries(keyValues);
|
|
21
|
-
await redisCache.setMultiple(keyValues);
|
|
22
|
-
const expectedKeyValues = keyValuesArray.map(([key, value]) => [`${redisCache.keyPrefix}${key}`, JSON.stringify(value)]);
|
|
23
|
-
await Promise.all(expectedKeyValues.map(async ([key, value]) => {
|
|
24
|
-
const valueInRedis = await testClient.getAsync(key);
|
|
25
|
-
(0, vitest_1.expect)(valueInRedis).toBe(value);
|
|
26
|
-
}));
|
|
27
|
-
// Check that the keys expire after a random amount of time.
|
|
28
|
-
const currentTTL = await testClient.ttlAsync(expectedKeyValues[0][0]);
|
|
29
|
-
(0, vitest_1.expect)(currentTTL).toBeGreaterThanOrEqual(0);
|
|
30
|
-
});
|
|
31
|
-
(0, vitest_1.it)('should not set any key-value pairs if keyValues is an empty object', async () => {
|
|
32
|
-
const currentKeys = await testClient.keysAsync(`${redisCache.keyPrefix}*`);
|
|
33
|
-
await redisCache.setMultiple({});
|
|
34
|
-
const keys = await testClient.keysAsync(`${redisCache.keyPrefix}*`);
|
|
35
|
-
(0, vitest_1.expect)(keys).toEqual(currentKeys);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
(0, vitest_1.describe)('set', () => {
|
|
39
|
-
(0, vitest_1.it)('should set a key-value pair in redis', async () => {
|
|
40
|
-
const key = 'testKey';
|
|
41
|
-
const value = 'testValue';
|
|
42
|
-
await redisCache.set(key, value);
|
|
43
|
-
const keyWithPrefix = `${redisCache.keyPrefix}${key}`;
|
|
44
|
-
const valueInRedis = await testClient.getAsync(keyWithPrefix);
|
|
45
|
-
(0, vitest_1.expect)(valueInRedis).toBe(JSON.stringify(value));
|
|
46
|
-
// Check that the key expires after a random amount of time.
|
|
47
|
-
const currentTTL = await testClient.ttlAsync(keyWithPrefix);
|
|
48
|
-
(0, vitest_1.expect)(currentTTL).toBeGreaterThanOrEqual(0);
|
|
49
|
-
});
|
|
50
|
-
(0, vitest_1.it)('should release the lock after setting a key-value pair', async () => {
|
|
51
|
-
const key = 'testKeyWithLock';
|
|
52
|
-
const value = 'testValueWithLock';
|
|
53
|
-
redisCache.useLock = true;
|
|
54
|
-
await redisCache.set(key, value);
|
|
55
|
-
const keyWithPrefix = `${redisCache.keyPrefix}${key}`;
|
|
56
|
-
const valueInRedis = await testClient.getAsync(keyWithPrefix);
|
|
57
|
-
(0, vitest_1.expect)(valueInRedis).toBe(JSON.stringify(value));
|
|
58
|
-
// Check that the lock is released
|
|
59
|
-
(0, vitest_1.expect)(redisCache.locks[keyWithPrefix]).toBeUndefined();
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
});
|