@imqueue/rpc 2.1.0 → 3.1.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/README.md +68 -0
- package/package.json +26 -61
- package/src/IMQCache.d.ts +10 -9
- package/src/IMQCache.js +16 -22
- package/src/IMQClient.d.ts +21 -16
- package/src/IMQClient.js +466 -270
- package/src/IMQDelay.d.ts +14 -0
- package/src/IMQDelay.js +26 -5
- package/src/IMQLock.d.ts +17 -15
- package/src/IMQLock.js +36 -36
- package/src/IMQMetadata.d.ts +8 -0
- package/src/IMQMetadata.js +8 -0
- package/src/IMQRPCDescription.d.ts +2 -2
- package/src/IMQRPCDescription.js +2 -2
- package/src/IMQRPCError.d.ts +7 -7
- package/src/IMQRPCError.js +7 -8
- package/src/IMQRPCOptions.d.ts +27 -3
- package/src/IMQRPCOptions.js +17 -3
- package/src/IMQRPCRequest.d.ts +1 -1
- package/src/IMQRPCResponse.d.ts +1 -1
- package/src/IMQRequestContext.js +1 -2
- package/src/IMQService.d.ts +3 -4
- package/src/IMQService.js +302 -220
- package/src/cache/ICache.d.ts +53 -0
- package/src/cache/RedisCache.d.ts +36 -31
- package/src/cache/RedisCache.js +104 -61
- package/src/decorators/cache.d.ts +12 -0
- package/src/decorators/cache.js +56 -41
- package/src/decorators/classType.d.ts +43 -0
- package/src/decorators/classType.js +83 -0
- package/src/decorators/expose.d.ts +5 -31
- package/src/decorators/expose.js +147 -129
- package/src/decorators/index.d.ts +2 -0
- package/src/decorators/index.js +4 -0
- package/src/decorators/indexed.d.ts +23 -0
- package/src/decorators/indexed.js +22 -13
- package/src/decorators/lock.d.ts +10 -11
- package/src/decorators/lock.js +63 -56
- package/src/decorators/logged.d.ts +11 -9
- package/src/decorators/logged.js +47 -40
- package/src/decorators/metadata.d.ts +24 -0
- package/src/decorators/metadata.js +45 -0
- package/src/decorators/property.d.ts +7 -57
- package/src/decorators/property.js +112 -83
- package/src/decorators/remote.d.ts +7 -7
- package/src/decorators/remote.js +19 -13
- package/src/helpers/fs.d.ts +13 -13
- package/src/helpers/fs.js +19 -19
- package/src/helpers/os-uuid.d.ts +2 -1
- package/src/helpers/os-uuid.js +86 -3
- package/src/helpers/pid.d.ts +19 -9
- package/src/helpers/pid.js +33 -33
- package/src/helpers/signature.d.ts +9 -7
- package/src/helpers/signature.js +80 -10
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -1
- package/tsconfig.json +18 -14
- package/.github/workflows/build.yml +0 -28
- package/debug.d.ts +0 -1
|
@@ -21,13 +21,17 @@
|
|
|
21
21
|
* purchase a proprietary commercial license. Please contact us at
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
|
-
import { ICache } from '.';
|
|
25
24
|
import { ILogger, IRedisClient, IMQOptions } from '@imqueue/core';
|
|
25
|
+
import { ICache } from '.';
|
|
26
26
|
export interface IRedisCacheOptions extends Partial<IMQOptions> {
|
|
27
27
|
conn?: IRedisClient;
|
|
28
28
|
}
|
|
29
29
|
export declare const DEFAULT_REDIS_CACHE_OPTIONS: {
|
|
30
|
-
|
|
30
|
+
id?: string;
|
|
31
|
+
host?: string | undefined;
|
|
32
|
+
port?: number | undefined;
|
|
33
|
+
username?: string;
|
|
34
|
+
password?: string;
|
|
31
35
|
cleanup: boolean;
|
|
32
36
|
cleanupFilter: string;
|
|
33
37
|
vendor?: string;
|
|
@@ -38,74 +42,75 @@ export declare const DEFAULT_REDIS_CACHE_OPTIONS: {
|
|
|
38
42
|
safeDeliveryTtl?: number;
|
|
39
43
|
cluster?: import("@imqueue/core").IMessageQueueConnection[];
|
|
40
44
|
clusterManagers?: import("@imqueue/core/src/ClusterManager").ClusterManager[];
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
password?: string | undefined;
|
|
45
|
+
handleSignals?: boolean;
|
|
46
|
+
verbose?: boolean;
|
|
47
|
+
verboseExtended?: boolean;
|
|
48
|
+
prefix: string;
|
|
46
49
|
};
|
|
47
50
|
export declare const REDIS_CLIENT_INIT_ERROR = "Redis client is not initialized!";
|
|
48
51
|
/**
|
|
49
|
-
* Class RedisCache. Implements cache engine
|
|
52
|
+
* Class RedisCache. Implements a cache engine on top of Redis.
|
|
50
53
|
*/
|
|
51
54
|
export declare class RedisCache implements ICache {
|
|
52
55
|
private static redis?;
|
|
56
|
+
private static initPromise?;
|
|
53
57
|
private logger;
|
|
54
58
|
options: IRedisCacheOptions;
|
|
55
59
|
name: string;
|
|
56
60
|
ready: boolean;
|
|
57
61
|
/**
|
|
58
|
-
* Initializes cache instance
|
|
62
|
+
* Initializes the cache instance. The underlying Redis connection is
|
|
63
|
+
* shared between all instances; concurrent initializations share a single
|
|
64
|
+
* connection attempt.
|
|
59
65
|
*
|
|
60
|
-
* @param {IRedisCacheOptions} [options]
|
|
66
|
+
* @param {IRedisCacheOptions} [options] - Redis cache options
|
|
61
67
|
* @returns {Promise<RedisCache>}
|
|
62
68
|
*/
|
|
63
69
|
init(options?: IRedisCacheOptions): Promise<RedisCache>;
|
|
64
70
|
/**
|
|
65
|
-
* Returns fully qualified key name for a given generic key
|
|
71
|
+
* Returns the fully qualified key name for a given generic key.
|
|
66
72
|
*
|
|
67
|
-
* @
|
|
68
|
-
* @param {string} key
|
|
73
|
+
* @param {string} key - generic key to qualify
|
|
69
74
|
* @returns {string}
|
|
70
75
|
*/
|
|
71
76
|
private key;
|
|
72
77
|
/**
|
|
73
|
-
* Returns value stored in cache
|
|
78
|
+
* Returns the value stored in the cache under a given key.
|
|
74
79
|
*
|
|
75
|
-
* @param {string} key
|
|
76
|
-
* @returns {Promise<any>}
|
|
80
|
+
* @param {string} key - key to read the value for
|
|
81
|
+
* @returns {Promise<any>} - stored value, or undefined if not found
|
|
77
82
|
*/
|
|
78
83
|
get(key: string): Promise<any>;
|
|
79
84
|
/**
|
|
80
|
-
* Stores
|
|
81
|
-
* cached value will expire
|
|
82
|
-
* argument set to true
|
|
83
|
-
* not exist yet.
|
|
84
|
-
* will be serialized automatically.
|
|
85
|
+
* Stores the given value in the cache under the given key. If TTL is
|
|
86
|
+
* specified, the cached value will expire after the given number of
|
|
87
|
+
* milliseconds. If the NX argument is set to true, the key:value pair
|
|
88
|
+
* is created only if it does not exist yet. The given value can be any
|
|
89
|
+
* JSON-compatible object and will be serialized automatically.
|
|
85
90
|
*
|
|
86
|
-
* @param {string} key
|
|
87
|
-
* @param {any} value
|
|
88
|
-
* @param {number} ttl
|
|
89
|
-
* @param {boolean} nx
|
|
91
|
+
* @param {string} key - key to store the value under
|
|
92
|
+
* @param {any} value - value to store
|
|
93
|
+
* @param {number} [ttl] - time-to-live in milliseconds
|
|
94
|
+
* @param {boolean} [nx] - store only if the key does not exist yet
|
|
90
95
|
* @returns {Promise<boolean>}
|
|
91
96
|
*/
|
|
92
97
|
set(key: string, value: any, ttl?: number, nx?: boolean): Promise<boolean>;
|
|
93
98
|
/**
|
|
94
|
-
* Removes stored in cache
|
|
99
|
+
* Removes the value stored in the cache under the given key.
|
|
95
100
|
*
|
|
96
|
-
* @param {string} key
|
|
101
|
+
* @param {string} key - key to remove
|
|
97
102
|
* @returns {Promise<boolean>}
|
|
98
103
|
*/
|
|
99
104
|
del(key: string): Promise<boolean>;
|
|
100
105
|
/**
|
|
101
|
-
* Purges all keys from cache
|
|
106
|
+
* Purges all keys from the cache matching a given wildcard mask.
|
|
102
107
|
*
|
|
103
|
-
* @param {string} keyMask
|
|
104
|
-
* @return {boolean}
|
|
108
|
+
* @param {string} keyMask - wildcard mask to match keys against
|
|
109
|
+
* @return {Promise<boolean>}
|
|
105
110
|
*/
|
|
106
111
|
purge(keyMask: string): Promise<boolean>;
|
|
107
112
|
/**
|
|
108
|
-
* Safely destroys
|
|
113
|
+
* Safely destroys the Redis connection.
|
|
109
114
|
*
|
|
110
115
|
* @returns {Promise<void>}
|
|
111
116
|
*/
|
package/src/cache/RedisCache.js
CHANGED
|
@@ -1,79 +1,123 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RedisCache = exports.REDIS_CLIENT_INIT_ERROR = exports.DEFAULT_REDIS_CACHE_OPTIONS = void 0;
|
|
4
|
+
/*!
|
|
5
|
+
* RedisCache adapter implementation
|
|
6
|
+
*
|
|
7
|
+
* I'm Queue Software Project
|
|
8
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
9
|
+
*
|
|
10
|
+
* This program is free software: you can redistribute it and/or modify
|
|
11
|
+
* it under the terms of the GNU General Public License as published by
|
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
13
|
+
* (at your option) any later version.
|
|
14
|
+
*
|
|
15
|
+
* This program is distributed in the hope that it will be useful,
|
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+
* GNU General Public License for more details.
|
|
19
|
+
*
|
|
20
|
+
* You should have received a copy of the GNU General Public License
|
|
21
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
22
|
+
*
|
|
23
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
24
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
25
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
26
|
+
*/
|
|
4
27
|
const core_1 = require("@imqueue/core");
|
|
5
|
-
const
|
|
6
|
-
exports.DEFAULT_REDIS_CACHE_OPTIONS =
|
|
28
|
+
const node_os_1 = require("node:os");
|
|
29
|
+
exports.DEFAULT_REDIS_CACHE_OPTIONS = {
|
|
30
|
+
...core_1.DEFAULT_IMQ_OPTIONS,
|
|
31
|
+
prefix: 'imq-cache',
|
|
32
|
+
};
|
|
7
33
|
exports.REDIS_CLIENT_INIT_ERROR = 'Redis client is not initialized!';
|
|
8
34
|
/**
|
|
9
|
-
* Class RedisCache. Implements cache engine
|
|
35
|
+
* Class RedisCache. Implements a cache engine on top of Redis.
|
|
10
36
|
*/
|
|
11
37
|
class RedisCache {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
38
|
+
static redis;
|
|
39
|
+
// pending shared connection attempt; concurrent init() calls await this
|
|
40
|
+
// single promise instead of opening one connection each
|
|
41
|
+
static initPromise;
|
|
42
|
+
logger;
|
|
43
|
+
options;
|
|
44
|
+
name = RedisCache.name;
|
|
45
|
+
ready = false;
|
|
16
46
|
/**
|
|
17
|
-
* Initializes cache instance
|
|
47
|
+
* Initializes the cache instance. The underlying Redis connection is
|
|
48
|
+
* shared between all instances; concurrent initializations share a single
|
|
49
|
+
* connection attempt.
|
|
18
50
|
*
|
|
19
|
-
* @param {IRedisCacheOptions} [options]
|
|
51
|
+
* @param {IRedisCacheOptions} [options] - Redis cache options
|
|
20
52
|
* @returns {Promise<RedisCache>}
|
|
21
53
|
*/
|
|
22
54
|
async init(options) {
|
|
23
|
-
this.options =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
55
|
+
this.options = {
|
|
56
|
+
...exports.DEFAULT_REDIS_CACHE_OPTIONS,
|
|
57
|
+
...options,
|
|
58
|
+
};
|
|
59
|
+
this.logger = this.options.logger || console;
|
|
60
|
+
if (RedisCache.redis && !RedisCache.initPromise) {
|
|
61
|
+
this.ready = true;
|
|
28
62
|
return this;
|
|
29
63
|
}
|
|
30
|
-
if (this.options.conn) {
|
|
64
|
+
if (this.options.conn && !RedisCache.initPromise) {
|
|
31
65
|
this.logger.info('Re-using given connection for cache.');
|
|
32
66
|
RedisCache.redis = this.options.conn;
|
|
67
|
+
this.ready = true;
|
|
33
68
|
return this;
|
|
34
69
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
70
|
+
if (!RedisCache.initPromise) {
|
|
71
|
+
RedisCache.initPromise = new Promise((resolve, reject) => {
|
|
72
|
+
const connectionName = `${this.options.prefix}:${this.name}:pid:${process.pid}:host:${(0, node_os_1.hostname)()}`;
|
|
73
|
+
RedisCache.redis = new core_1.Redis({
|
|
74
|
+
port: Number(this.options.port),
|
|
75
|
+
host: String(this.options.host),
|
|
76
|
+
username: this.options.username,
|
|
77
|
+
password: this.options.password,
|
|
78
|
+
connectionName,
|
|
79
|
+
});
|
|
80
|
+
RedisCache.redis.on('ready', () => {
|
|
81
|
+
this.logger.info('%s: redis cache connected, host %s:%s, pid %s', this.name, this.options.host, this.options.port, process.pid);
|
|
82
|
+
resolve();
|
|
83
|
+
});
|
|
84
|
+
RedisCache.redis.on('error', (err) => {
|
|
85
|
+
this.logger.error(`${this.name}: error connecting redis, pid ${process.pid}:`, err);
|
|
86
|
+
reject(err);
|
|
87
|
+
});
|
|
53
88
|
});
|
|
54
|
-
}
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await RedisCache.initPromise;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
// the promise only guards the pending connection attempt; once it
|
|
95
|
+
// settles (either way), clear it so later init() calls observe
|
|
96
|
+
// the current RedisCache.redis state (or retry after a failure)
|
|
97
|
+
RedisCache.initPromise = undefined;
|
|
98
|
+
}
|
|
99
|
+
this.ready = true;
|
|
100
|
+
return this;
|
|
55
101
|
}
|
|
56
102
|
/**
|
|
57
|
-
* Returns fully qualified key name for a given generic key
|
|
103
|
+
* Returns the fully qualified key name for a given generic key.
|
|
58
104
|
*
|
|
59
|
-
* @
|
|
60
|
-
* @param {string} key
|
|
105
|
+
* @param {string} key - generic key to qualify
|
|
61
106
|
* @returns {string}
|
|
62
107
|
*/
|
|
63
108
|
key(key) {
|
|
64
109
|
return `${this.options.prefix}:${this.name}:${key}`;
|
|
65
110
|
}
|
|
66
111
|
/**
|
|
67
|
-
* Returns value stored in cache
|
|
112
|
+
* Returns the value stored in the cache under a given key.
|
|
68
113
|
*
|
|
69
|
-
* @param {string} key
|
|
70
|
-
* @returns {Promise<any>}
|
|
114
|
+
* @param {string} key - key to read the value for
|
|
115
|
+
* @returns {Promise<any>} - stored value, or undefined if not found
|
|
71
116
|
*/
|
|
72
117
|
async get(key) {
|
|
73
118
|
if (!RedisCache.redis) {
|
|
74
119
|
throw new TypeError(exports.REDIS_CLIENT_INIT_ERROR);
|
|
75
120
|
}
|
|
76
|
-
// noinspection ES6RedundantAwait
|
|
77
121
|
const data = await RedisCache.redis.get(this.key(key));
|
|
78
122
|
if (data) {
|
|
79
123
|
return JSON.parse(data);
|
|
@@ -81,16 +125,16 @@ class RedisCache {
|
|
|
81
125
|
return undefined;
|
|
82
126
|
}
|
|
83
127
|
/**
|
|
84
|
-
* Stores
|
|
85
|
-
* cached value will expire
|
|
86
|
-
* argument set to true
|
|
87
|
-
* not exist yet.
|
|
88
|
-
* will be serialized automatically.
|
|
128
|
+
* Stores the given value in the cache under the given key. If TTL is
|
|
129
|
+
* specified, the cached value will expire after the given number of
|
|
130
|
+
* milliseconds. If the NX argument is set to true, the key:value pair
|
|
131
|
+
* is created only if it does not exist yet. The given value can be any
|
|
132
|
+
* JSON-compatible object and will be serialized automatically.
|
|
89
133
|
*
|
|
90
|
-
* @param {string} key
|
|
91
|
-
* @param {any} value
|
|
92
|
-
* @param {number} ttl
|
|
93
|
-
* @param {boolean} nx
|
|
134
|
+
* @param {string} key - key to store the value under
|
|
135
|
+
* @param {any} value - value to store
|
|
136
|
+
* @param {number} [ttl] - time-to-live in milliseconds
|
|
137
|
+
* @param {boolean} [nx] - store only if the key does not exist yet
|
|
94
138
|
* @returns {Promise<boolean>}
|
|
95
139
|
*/
|
|
96
140
|
async set(key, value, ttl, nx = false) {
|
|
@@ -99,7 +143,7 @@ class RedisCache {
|
|
|
99
143
|
}
|
|
100
144
|
const args = [
|
|
101
145
|
this.key(key),
|
|
102
|
-
JSON.stringify(value && value.then ? await value : value)
|
|
146
|
+
JSON.stringify(value && value.then ? await value : value),
|
|
103
147
|
];
|
|
104
148
|
if (ttl && ttl > 0) {
|
|
105
149
|
args.push('PX', ttl);
|
|
@@ -110,29 +154,28 @@ class RedisCache {
|
|
|
110
154
|
return await RedisCache.redis.set.apply(RedisCache.redis, args);
|
|
111
155
|
}
|
|
112
156
|
/**
|
|
113
|
-
* Removes stored in cache
|
|
157
|
+
* Removes the value stored in the cache under the given key.
|
|
114
158
|
*
|
|
115
|
-
* @param {string} key
|
|
159
|
+
* @param {string} key - key to remove
|
|
116
160
|
* @returns {Promise<boolean>}
|
|
117
161
|
*/
|
|
118
162
|
async del(key) {
|
|
119
163
|
if (!RedisCache.redis) {
|
|
120
164
|
throw new TypeError(exports.REDIS_CLIENT_INIT_ERROR);
|
|
121
165
|
}
|
|
122
|
-
return !!await RedisCache.redis.del(this.key(key));
|
|
166
|
+
return !!(await RedisCache.redis.del(this.key(key)));
|
|
123
167
|
}
|
|
124
168
|
/**
|
|
125
|
-
* Purges all keys from cache
|
|
169
|
+
* Purges all keys from the cache matching a given wildcard mask.
|
|
126
170
|
*
|
|
127
|
-
* @param {string} keyMask
|
|
128
|
-
* @return {boolean}
|
|
171
|
+
* @param {string} keyMask - wildcard mask to match keys against
|
|
172
|
+
* @return {Promise<boolean>}
|
|
129
173
|
*/
|
|
130
174
|
async purge(keyMask) {
|
|
131
175
|
if (!RedisCache.redis) {
|
|
132
176
|
throw new TypeError(exports.REDIS_CLIENT_INIT_ERROR);
|
|
133
177
|
}
|
|
134
178
|
try {
|
|
135
|
-
// noinspection SpellCheckingInspection
|
|
136
179
|
await RedisCache.redis.eval(`for _,k in ipairs(redis.call('keys','${keyMask}')) do redis.call('del',k) end`, 0);
|
|
137
180
|
return true;
|
|
138
181
|
}
|
|
@@ -142,13 +185,13 @@ class RedisCache {
|
|
|
142
185
|
}
|
|
143
186
|
}
|
|
144
187
|
/**
|
|
145
|
-
* Safely destroys
|
|
188
|
+
* Safely destroys the Redis connection.
|
|
146
189
|
*
|
|
147
190
|
* @returns {Promise<void>}
|
|
148
191
|
*/
|
|
149
192
|
static async destroy() {
|
|
193
|
+
RedisCache.initPromise = undefined;
|
|
150
194
|
try {
|
|
151
|
-
// istanbul ignore else
|
|
152
195
|
if (RedisCache.redis) {
|
|
153
196
|
RedisCache.redis.removeAllListeners();
|
|
154
197
|
RedisCache.redis.disconnect(false);
|
|
@@ -156,7 +199,7 @@ class RedisCache {
|
|
|
156
199
|
delete RedisCache.redis;
|
|
157
200
|
}
|
|
158
201
|
}
|
|
159
|
-
catch
|
|
202
|
+
catch { }
|
|
160
203
|
}
|
|
161
204
|
}
|
|
162
205
|
exports.RedisCache = RedisCache;
|
|
@@ -31,4 +31,16 @@ export interface CacheDecorator {
|
|
|
31
31
|
(options?: CacheDecoratorOptions): (...args: any[]) => any;
|
|
32
32
|
globalOptions?: CacheDecoratorOptions;
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a `@cache()` method decorator that memoizes the decorated method's
|
|
36
|
+
* result in a cache adapter (RedisCache by default). On each call the cache is
|
|
37
|
+
* checked first; on a miss the method runs, and its result is stored under a
|
|
38
|
+
* key derived from the class name, method name, and arguments. The returned
|
|
39
|
+
* decorator is dual-mode: it works both as a standard (TC39) and as a legacy
|
|
40
|
+
* method decorator.
|
|
41
|
+
*
|
|
42
|
+
* @param {CacheDecoratorOptions} [options] - per-method cache options (adapter,
|
|
43
|
+
* ttl, nx); merged over `cache.globalOptions`
|
|
44
|
+
* @return {Function} - a dual-mode method decorator
|
|
45
|
+
*/
|
|
34
46
|
export declare const cache: CacheDecorator;
|
package/src/decorators/cache.js
CHANGED
|
@@ -25,56 +25,71 @@ exports.cache = void 0;
|
|
|
25
25
|
* <support@imqueue.com> to get commercial licensing options.
|
|
26
26
|
*/
|
|
27
27
|
const __1 = require("..");
|
|
28
|
-
|
|
28
|
+
const helpers_1 = require("../helpers");
|
|
29
|
+
/**
|
|
30
|
+
* Creates a `@cache()` method decorator that memoizes the decorated method's
|
|
31
|
+
* result in a cache adapter (RedisCache by default). On each call the cache is
|
|
32
|
+
* checked first; on a miss the method runs, and its result is stored under a
|
|
33
|
+
* key derived from the class name, method name, and arguments. The returned
|
|
34
|
+
* decorator is dual-mode: it works both as a standard (TC39) and as a legacy
|
|
35
|
+
* method decorator.
|
|
36
|
+
*
|
|
37
|
+
* @param {CacheDecoratorOptions} [options] - per-method cache options (adapter,
|
|
38
|
+
* ttl, nx); merged over `cache.globalOptions`
|
|
39
|
+
* @return {Function} - a dual-mode method decorator
|
|
40
|
+
*/
|
|
29
41
|
const cache = function (options) {
|
|
30
|
-
const cacheOptions =
|
|
42
|
+
const cacheOptions = {
|
|
43
|
+
...exports.cache.globalOptions,
|
|
44
|
+
...options,
|
|
45
|
+
};
|
|
31
46
|
let Adapter = cacheOptions.adapter || __1.RedisCache;
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
let cache = __1.IMQCache.get(Adapter);
|
|
39
|
-
// istanbul ignore next
|
|
40
|
-
if (cache && cache.ready) {
|
|
41
|
-
context.cache = cache;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
let opts = undefined;
|
|
45
|
-
if (context.imq && context.imq.writer) {
|
|
46
|
-
opts = { conn: context.imq.writer };
|
|
47
|
-
}
|
|
48
|
-
const logger = context.logger ||
|
|
49
|
-
(context.imq && context.imq.logger);
|
|
50
|
-
if (logger) {
|
|
51
|
-
opts = Object.assign(Object.assign({}, opts), { logger });
|
|
52
|
-
}
|
|
53
|
-
await __1.IMQCache.register(Adapter, opts).init();
|
|
54
|
-
context.cache = __1.IMQCache.get(Adapter);
|
|
55
|
-
}
|
|
47
|
+
const wrap = (original, methodName) => async function (...args) {
|
|
48
|
+
const className = this.constructor.name;
|
|
49
|
+
if (!this.cache) {
|
|
50
|
+
let cache = __1.IMQCache.get(Adapter);
|
|
51
|
+
if (cache && cache.ready) {
|
|
52
|
+
this.cache = cache;
|
|
56
53
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
else {
|
|
55
|
+
let opts = undefined;
|
|
56
|
+
if (this.imq && this.imq.writer) {
|
|
57
|
+
opts = { conn: this.imq.writer };
|
|
58
|
+
}
|
|
59
|
+
const logger = this.logger || (this.imq && this.imq.logger);
|
|
60
|
+
if (logger) {
|
|
61
|
+
opts = { ...opts, logger };
|
|
63
62
|
}
|
|
64
|
-
|
|
63
|
+
await __1.IMQCache.register(Adapter, opts).init();
|
|
64
|
+
this.cache = __1.IMQCache.get(Adapter);
|
|
65
65
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const key = (0, helpers_1.signature)(className, methodName, args);
|
|
69
|
+
let result = await this.cache.get(key);
|
|
70
|
+
if (result === undefined) {
|
|
71
|
+
result = original.apply(this, args);
|
|
72
|
+
await this.cache.set(key, result, cacheOptions.ttl, !!cacheOptions.nx);
|
|
71
73
|
}
|
|
72
|
-
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
(this.logger || this.cache.logger).warn('cache: Error fetching cached value for %s.%s(), args: %s!', className, methodName, JSON.stringify(args), err);
|
|
78
|
+
return original.apply(this, args);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// Dual-mode: standard (TC39) invocations pass a context object with a
|
|
82
|
+
// `kind` property; legacy ones pass (target, propertyKey, descriptor).
|
|
83
|
+
return function (target, context, descriptor) {
|
|
84
|
+
if (context && typeof context === 'object' && 'kind' in context) {
|
|
85
|
+
return wrap(target, context.name);
|
|
86
|
+
}
|
|
87
|
+
descriptor.value = wrap(descriptor.value, context);
|
|
88
|
+
return descriptor;
|
|
73
89
|
};
|
|
74
90
|
};
|
|
75
91
|
exports.cache = cache;
|
|
76
|
-
// codebeat:enable[BLOCK_NESTING]
|
|
77
92
|
exports.cache.globalOptions = {
|
|
78
|
-
adapter: __1.RedisCache
|
|
93
|
+
adapter: __1.RedisCache,
|
|
79
94
|
};
|
|
80
95
|
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements the '@classType' decorator factory.
|
|
3
|
+
*
|
|
4
|
+
* Applied to a complex-type class, it registers the class's '@property' field
|
|
5
|
+
* definitions into the RPC type description so the type can be exposed to
|
|
6
|
+
* service clients. It is required on any class that uses '@property':
|
|
7
|
+
* standard (TC39) field decorators cannot see the class they belong to, so a
|
|
8
|
+
* class-level decorator is needed to flush the collected properties under the
|
|
9
|
+
* class name.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ~~~typescript
|
|
13
|
+
* import { classType, property, expose, IMQService } from '@imqueue/rpc';
|
|
14
|
+
*
|
|
15
|
+
* @classType()
|
|
16
|
+
* class Address {
|
|
17
|
+
* @property('string')
|
|
18
|
+
* country: string;
|
|
19
|
+
*
|
|
20
|
+
* @property('string', true)
|
|
21
|
+
* zipCode?: string; // optional
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* @classType()
|
|
25
|
+
* class User {
|
|
26
|
+
* @property('string')
|
|
27
|
+
* firstName: string;
|
|
28
|
+
*
|
|
29
|
+
* @property('Array<Address>', true)
|
|
30
|
+
* addresses?: Address[];
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* class UserService extends IMQService {
|
|
34
|
+
* @expose()
|
|
35
|
+
* public save(user: User) {
|
|
36
|
+
* // now User (and Address) are properly exposed to clients
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* ~~~
|
|
40
|
+
*
|
|
41
|
+
* @return {(value: Function, context: ClassDecoratorContext) => void}
|
|
42
|
+
*/
|
|
43
|
+
export declare function classType(): any;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.classType = classType;
|
|
4
|
+
/*!
|
|
5
|
+
* IMQ-RPC Decorators: classType
|
|
6
|
+
*
|
|
7
|
+
* I'm Queue Software Project
|
|
8
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
9
|
+
*
|
|
10
|
+
* This program is free software: you can redistribute it and/or modify
|
|
11
|
+
* it under the terms of the GNU General Public License as published by
|
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
13
|
+
* (at your option) any later version.
|
|
14
|
+
*
|
|
15
|
+
* This program is distributed in the hope that it will be useful,
|
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+
* GNU General Public License for more details.
|
|
19
|
+
*
|
|
20
|
+
* You should have received a copy of the GNU General Public License
|
|
21
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
22
|
+
*
|
|
23
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
24
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
25
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
26
|
+
*/
|
|
27
|
+
const property_1 = require("./property");
|
|
28
|
+
/**
|
|
29
|
+
* Implements the '@classType' decorator factory.
|
|
30
|
+
*
|
|
31
|
+
* Applied to a complex-type class, it registers the class's '@property' field
|
|
32
|
+
* definitions into the RPC type description so the type can be exposed to
|
|
33
|
+
* service clients. It is required on any class that uses '@property':
|
|
34
|
+
* standard (TC39) field decorators cannot see the class they belong to, so a
|
|
35
|
+
* class-level decorator is needed to flush the collected properties under the
|
|
36
|
+
* class name.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ~~~typescript
|
|
40
|
+
* import { classType, property, expose, IMQService } from '@imqueue/rpc';
|
|
41
|
+
*
|
|
42
|
+
* @classType()
|
|
43
|
+
* class Address {
|
|
44
|
+
* @property('string')
|
|
45
|
+
* country: string;
|
|
46
|
+
*
|
|
47
|
+
* @property('string', true)
|
|
48
|
+
* zipCode?: string; // optional
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* @classType()
|
|
52
|
+
* class User {
|
|
53
|
+
* @property('string')
|
|
54
|
+
* firstName: string;
|
|
55
|
+
*
|
|
56
|
+
* @property('Array<Address>', true)
|
|
57
|
+
* addresses?: Address[];
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* class UserService extends IMQService {
|
|
61
|
+
* @expose()
|
|
62
|
+
* public save(user: User) {
|
|
63
|
+
* // now User (and Address) are properly exposed to clients
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* ~~~
|
|
67
|
+
*
|
|
68
|
+
* @return {(value: Function, context: ClassDecoratorContext) => void}
|
|
69
|
+
*/
|
|
70
|
+
function classType() {
|
|
71
|
+
// Dual-mode: standard (TC39) class decorators pass a context object with a
|
|
72
|
+
// `kind` property; legacy ones pass only the constructor. In legacy mode
|
|
73
|
+
// @property already registers each field directly, so there is nothing to
|
|
74
|
+
// flush and the class is returned unchanged.
|
|
75
|
+
return function (value, context) {
|
|
76
|
+
if (context && typeof context === 'object' && 'kind' in context) {
|
|
77
|
+
(0, property_1.registerType)(value, context.metadata);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=classType.js.map
|