@imqueue/rpc 2.1.0 → 3.0.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/.github/workflows/build.yml +49 -5
- package/.oxfmtrc.json +9 -0
- package/.oxlintrc.json +37 -0
- package/README.md +68 -0
- package/package.json +21 -58
- package/scripts/strip-comment-coverage.mjs +464 -0
- package/src/IMQCache.d.ts +10 -9
- package/src/IMQCache.js +16 -22
- package/src/IMQClient.d.ts +21 -16
- package/src/IMQClient.js +407 -271
- 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 +30 -25
- 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 +118 -134
- 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/typedoc.json +19 -0
package/src/IMQService.js
CHANGED
|
@@ -1,12 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
19
|
+
var useValue = arguments.length > 2;
|
|
20
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
21
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
22
|
+
}
|
|
23
|
+
return useValue ? value : void 0;
|
|
24
|
+
};
|
|
25
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
26
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
27
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
28
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
29
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
30
|
+
var _, done = false;
|
|
31
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
32
|
+
var context = {};
|
|
33
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
34
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
35
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
36
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
37
|
+
if (kind === "accessor") {
|
|
38
|
+
if (result === void 0) continue;
|
|
39
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
40
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
41
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
42
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
43
|
+
}
|
|
44
|
+
else if (_ = accept(result)) {
|
|
45
|
+
if (kind === "field") initializers.unshift(_);
|
|
46
|
+
else descriptor[key] = _;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
50
|
+
done = true;
|
|
7
51
|
};
|
|
8
|
-
var
|
|
9
|
-
|
|
52
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
53
|
+
var ownKeys = function(o) {
|
|
54
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
55
|
+
var ar = [];
|
|
56
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
57
|
+
return ar;
|
|
58
|
+
};
|
|
59
|
+
return ownKeys(o);
|
|
60
|
+
};
|
|
61
|
+
return function (mod) {
|
|
62
|
+
if (mod && mod.__esModule) return mod;
|
|
63
|
+
var result = {};
|
|
64
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
65
|
+
__setModuleDefault(result, mod);
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
})();
|
|
69
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
70
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
71
|
};
|
|
11
72
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
73
|
exports.IMQService = exports.Description = void 0;
|
|
@@ -34,13 +95,16 @@ exports.send = send;
|
|
|
34
95
|
* purchase a proprietary commercial license. Please contact us at
|
|
35
96
|
* <support@imqueue.com> to get commercial licensing options.
|
|
36
97
|
*/
|
|
37
|
-
const core_1 = require("@imqueue/core");
|
|
98
|
+
const core_1 = __importStar(require("@imqueue/core"));
|
|
38
99
|
const _1 = require(".");
|
|
39
|
-
const
|
|
100
|
+
const helpers_1 = require("./helpers");
|
|
101
|
+
const node_os_1 = require("node:os");
|
|
102
|
+
const node_cluster_1 = __importDefault(require("node:cluster"));
|
|
40
103
|
const IMQRequestContext_1 = require("./IMQRequestContext");
|
|
41
|
-
const
|
|
42
|
-
const cluster = require('cluster');
|
|
104
|
+
const node_http_1 = require("node:http");
|
|
43
105
|
class Description {
|
|
106
|
+
service;
|
|
107
|
+
types;
|
|
44
108
|
}
|
|
45
109
|
exports.Description = Description;
|
|
46
110
|
const serviceDescriptions = new Map();
|
|
@@ -69,240 +133,258 @@ function getClassMethods(className) {
|
|
|
69
133
|
* @returns {boolean}
|
|
70
134
|
*/
|
|
71
135
|
function isValidArgsCount(argsInfo, args) {
|
|
72
|
-
|
|
73
|
-
return (argsInfo.some(argInfo => argInfo.isOptional)
|
|
136
|
+
return argsInfo.some(argInfo => argInfo.isOptional)
|
|
74
137
|
? argsInfo.length >= args.length
|
|
75
|
-
: argsInfo.length === args.length
|
|
138
|
+
: argsInfo.length === args.length;
|
|
76
139
|
}
|
|
77
140
|
/**
|
|
78
141
|
* Class IMQService
|
|
79
142
|
* Basic abstract service (server-side) implementation
|
|
80
143
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
144
|
+
let IMQService = (() => {
|
|
145
|
+
let _instanceExtraInitializers = [];
|
|
146
|
+
let _start_decorators;
|
|
147
|
+
let _stop_decorators;
|
|
148
|
+
let _destroy_decorators;
|
|
149
|
+
let _describe_decorators;
|
|
150
|
+
return class IMQService {
|
|
151
|
+
static {
|
|
152
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
153
|
+
_start_decorators = [(0, core_1.profile)()];
|
|
154
|
+
_stop_decorators = [(0, core_1.profile)()];
|
|
155
|
+
_destroy_decorators = [(0, core_1.profile)()];
|
|
156
|
+
_describe_decorators = [(0, _1.expose)()];
|
|
157
|
+
__esDecorate(this, null, _start_decorators, { kind: "method", name: "start", static: false, private: false, access: { has: obj => "start" in obj, get: obj => obj.start }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
158
|
+
__esDecorate(this, null, _stop_decorators, { kind: "method", name: "stop", static: false, private: false, access: { has: obj => "stop" in obj, get: obj => obj.stop }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
159
|
+
__esDecorate(this, null, _destroy_decorators, { kind: "method", name: "destroy", static: false, private: false, access: { has: obj => "destroy" in obj, get: obj => obj.destroy }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
160
|
+
__esDecorate(this, null, _describe_decorators, { kind: "method", name: "describe", static: false, private: false, access: { has: obj => "describe" in obj, get: obj => obj.describe }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
161
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
95
162
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
163
|
+
imq = __runInitializers(this, _instanceExtraInitializers);
|
|
164
|
+
logger;
|
|
165
|
+
cache;
|
|
166
|
+
metricsServer;
|
|
167
|
+
signalHandlers = [];
|
|
168
|
+
name;
|
|
169
|
+
options;
|
|
170
|
+
/**
|
|
171
|
+
* Class constructor
|
|
172
|
+
*
|
|
173
|
+
* @param {Partial<IMQServiceOptions>} options
|
|
174
|
+
* @param {string} [name]
|
|
175
|
+
*/
|
|
176
|
+
constructor(options, name) {
|
|
177
|
+
this.name = name || this.constructor.name;
|
|
178
|
+
if (this.name === 'IMQService') {
|
|
179
|
+
throw new TypeError('IMQService class is abstract and cannot ' +
|
|
180
|
+
'be instantiated directly!');
|
|
181
|
+
}
|
|
182
|
+
this.options = {
|
|
183
|
+
..._1.DEFAULT_IMQ_SERVICE_OPTIONS,
|
|
184
|
+
...options,
|
|
185
|
+
metricsServer: {
|
|
186
|
+
..._1.DEFAULT_IMQ_METRICS_SERVER_OPTIONS,
|
|
187
|
+
...options?.metricsServer,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
this.logger = this.options.logger || /* istanbul ignore next */ console;
|
|
191
|
+
this.imq = core_1.default.create(this.name, this.options);
|
|
192
|
+
this.handleRequest = this.handleRequest.bind(this);
|
|
193
|
+
helpers_1.SIGNALS.forEach((signal) => {
|
|
194
|
+
const handler = () => {
|
|
195
|
+
this.destroy().catch(this.logger.error);
|
|
196
|
+
if (this.metricsServer) {
|
|
197
|
+
this.metricsServer.close();
|
|
198
|
+
}
|
|
199
|
+
setTimeout(() => process.exit(0), core_1.IMQ_SHUTDOWN_TIMEOUT);
|
|
200
|
+
};
|
|
201
|
+
// tracked so destroy() can unregister them (see below)
|
|
202
|
+
this.signalHandlers.push([signal, handler]);
|
|
203
|
+
process.on(signal, handler);
|
|
204
|
+
});
|
|
205
|
+
// guard the async handler: a failure while processing or publishing
|
|
206
|
+
// the response (e.g. the broker went away mid-reply) must be logged,
|
|
207
|
+
// not surface as an unhandled rejection and crash the process
|
|
208
|
+
this.imq.on('message', ((request, id) => this.handleRequest(request, id).catch(err => this.logger.error(`${this.name}: error handling request:`, err))));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Handles incoming request and produces corresponding response
|
|
212
|
+
*
|
|
213
|
+
* @param {IMQRPCRequest} request - request message
|
|
214
|
+
* @param {string} id - message unique identifier
|
|
215
|
+
* @return {Promise<string>}
|
|
216
|
+
*/
|
|
217
|
+
async handleRequest(request, id) {
|
|
218
|
+
return (0, IMQRequestContext_1.runWithRequest)(request, () => this.processRequest(request, id));
|
|
219
|
+
}
|
|
220
|
+
async processRequest(request, id) {
|
|
221
|
+
const logger = this.options.logger || console;
|
|
222
|
+
const method = request.method;
|
|
223
|
+
const description = await this.describe();
|
|
224
|
+
const args = request.args;
|
|
225
|
+
let response = {
|
|
226
|
+
to: id,
|
|
227
|
+
data: null,
|
|
228
|
+
error: null,
|
|
229
|
+
request: request,
|
|
230
|
+
};
|
|
231
|
+
if (typeof this.options.beforeCall === 'function') {
|
|
232
|
+
const beforeCall = this.options.beforeCall.bind(this);
|
|
233
|
+
try {
|
|
234
|
+
await beforeCall(request, response);
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
logger.warn(_1.BEFORE_HOOK_ERROR, err);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (!this[method]) {
|
|
241
|
+
response.error = (0, _1.IMQError)('IMQ_RPC_NO_METHOD', `Method ${this.name}.${method}() does not exist.`, new Error().stack, method, args);
|
|
242
|
+
}
|
|
243
|
+
else if (!description.service.methods[method]) {
|
|
244
|
+
// Allow calling runtime-attached methods (own props) even if
|
|
245
|
+
// they are not present in the exposed service description.
|
|
246
|
+
// Deny access for prototype (class) methods not decorated with @expose.
|
|
247
|
+
const isOwn = Object.prototype.hasOwnProperty.call(this, method);
|
|
248
|
+
const value = this[method];
|
|
249
|
+
const proto = Object.getPrototypeOf(this);
|
|
250
|
+
const protoValue = proto && proto[method];
|
|
251
|
+
const isSameAsProto = typeof protoValue === 'function' && value === protoValue;
|
|
252
|
+
// Allow only truly dynamic own-instance functions (not the same as prototype)
|
|
253
|
+
if (!(isOwn && typeof value === 'function' && !isSameAsProto)) {
|
|
254
|
+
response.error = (0, _1.IMQError)('IMQ_RPC_NO_ACCESS', `Access to ${this.name}.${method}() denied!`, new Error().stack, method, args);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else if (!isValidArgsCount(description.service.methods[method].arguments, args)) {
|
|
258
|
+
response.error = (0, _1.IMQError)('IMQ_RPC_INVALID_ARGS_COUNT', `Invalid args count for ${this.name}.${method}().`, new Error().stack, method, args);
|
|
259
|
+
}
|
|
260
|
+
if (response.error) {
|
|
261
|
+
this.logger.warn(response.error);
|
|
262
|
+
return await send(request, response, this);
|
|
104
263
|
}
|
|
105
|
-
// istanbul ignore next
|
|
106
|
-
setTimeout(() => process.exit(0), core_1.IMQ_SHUTDOWN_TIMEOUT);
|
|
107
|
-
}));
|
|
108
|
-
this.imq.on('message', this.handleRequest);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Handles incoming request and produces corresponding response
|
|
112
|
-
*
|
|
113
|
-
* @access private
|
|
114
|
-
* @param {IMQRPCRequest} request - request message
|
|
115
|
-
* @param {string} id - message unique identifier
|
|
116
|
-
* @return {Promise<string>}
|
|
117
|
-
*/
|
|
118
|
-
async handleRequest(request, id) {
|
|
119
|
-
return (0, IMQRequestContext_1.runWithRequest)(request, () => this.processRequest(request, id));
|
|
120
|
-
}
|
|
121
|
-
async processRequest(request, id) {
|
|
122
|
-
const logger = this.options.logger || console;
|
|
123
|
-
const method = request.method;
|
|
124
|
-
const description = await this.describe();
|
|
125
|
-
const args = request.args;
|
|
126
|
-
let response = {
|
|
127
|
-
to: id,
|
|
128
|
-
data: null,
|
|
129
|
-
error: null,
|
|
130
|
-
request: request,
|
|
131
|
-
};
|
|
132
|
-
if (typeof this.options.beforeCall === 'function') {
|
|
133
|
-
const beforeCall = this.options.beforeCall.bind(this);
|
|
134
264
|
try {
|
|
135
|
-
|
|
265
|
+
response.data = this[method].apply(this, args);
|
|
266
|
+
if (response.data && response.data.then) {
|
|
267
|
+
response.data = await response.data;
|
|
268
|
+
}
|
|
136
269
|
}
|
|
137
270
|
catch (err) {
|
|
138
|
-
|
|
271
|
+
response.error = (0, _1.IMQError)(err.code || 'IMQ_RPC_CALL_ERROR', err.message, err.stack, method, args, err);
|
|
139
272
|
}
|
|
273
|
+
return await send(request, response, this);
|
|
140
274
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
275
|
+
/**
|
|
276
|
+
* Initializes this instance of service and starts handling request
|
|
277
|
+
* messages.
|
|
278
|
+
*
|
|
279
|
+
* @return {Promise<IMessageQueue | undefined>}
|
|
280
|
+
*/
|
|
281
|
+
async start() {
|
|
282
|
+
if (!this.options.multiProcess) {
|
|
283
|
+
this.logger.info('%s: starting single-worker, pid %s', this.name, process.pid);
|
|
284
|
+
this.describe();
|
|
285
|
+
return this.startWithMetricsServer();
|
|
286
|
+
}
|
|
287
|
+
if (node_cluster_1.default.isMaster) {
|
|
288
|
+
const numCpus = (0, node_os_1.cpus)().length;
|
|
289
|
+
const numWorkers = numCpus * this.options.childrenPerCore;
|
|
290
|
+
for (let i = 0; i < numWorkers; i++) {
|
|
291
|
+
this.logger.info('%s: starting worker #%s ...', this.name, i);
|
|
292
|
+
node_cluster_1.default.fork({ workerId: i });
|
|
293
|
+
}
|
|
294
|
+
node_cluster_1.default.on('exit', (worker) => {
|
|
295
|
+
/* node:coverage disable */
|
|
296
|
+
// exercised by tests, but V8 will not record coverage for an
|
|
297
|
+
// inline listener body that terminates via process.exit()
|
|
298
|
+
this.logger.info('%s: worker pid %s died, exiting', this.name, worker.process.pid);
|
|
299
|
+
process.exit(1);
|
|
300
|
+
/* node:coverage enable */
|
|
301
|
+
});
|
|
156
302
|
}
|
|
303
|
+
else {
|
|
304
|
+
this.logger.info('%s: worker #%s started, pid %s', this.name, process.env['workerId'], process.pid);
|
|
305
|
+
this.describe();
|
|
306
|
+
return this.startWithMetricsServer();
|
|
307
|
+
}
|
|
308
|
+
return this.startWithMetricsServer();
|
|
157
309
|
}
|
|
158
|
-
|
|
159
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Sends given data to service subscription channel
|
|
312
|
+
*
|
|
313
|
+
* @param {JsonObject} data
|
|
314
|
+
*/
|
|
315
|
+
async publish(data) {
|
|
316
|
+
await this.imq.publish(data);
|
|
160
317
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Stops service from handling messages
|
|
320
|
+
*
|
|
321
|
+
* @return {Promise<void>}
|
|
322
|
+
*/
|
|
323
|
+
async stop() {
|
|
324
|
+
await this.imq.stop();
|
|
164
325
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
326
|
+
/**
|
|
327
|
+
* Destroys this instance of service
|
|
328
|
+
*
|
|
329
|
+
* @return {Promise<void>}
|
|
330
|
+
*/
|
|
331
|
+
async destroy() {
|
|
332
|
+
// unregister this instance's process signal handlers so destroyed
|
|
333
|
+
// services do not leak process-level listeners
|
|
334
|
+
for (const [signal, handler] of this.signalHandlers) {
|
|
335
|
+
process.removeListener(signal, handler);
|
|
170
336
|
}
|
|
337
|
+
this.signalHandlers.length = 0;
|
|
338
|
+
await this.imq.unsubscribe();
|
|
339
|
+
await this.imq.destroy();
|
|
171
340
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
341
|
+
/**
|
|
342
|
+
* Returns service description metadata.
|
|
343
|
+
*
|
|
344
|
+
* @returns {Promise<Description>}
|
|
345
|
+
*/
|
|
346
|
+
describe() {
|
|
347
|
+
let description = serviceDescriptions.get(this.name) || null;
|
|
348
|
+
if (!description) {
|
|
349
|
+
description = {
|
|
350
|
+
service: {
|
|
351
|
+
name: this.name,
|
|
352
|
+
methods: getClassMethods(this.constructor.name),
|
|
353
|
+
},
|
|
354
|
+
types: _1.IMQRPCDescription.typesDescription,
|
|
355
|
+
};
|
|
356
|
+
serviceDescriptions.set(this.name, description);
|
|
357
|
+
}
|
|
358
|
+
return description;
|
|
188
359
|
}
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
cluster.fork({ workerId: i });
|
|
360
|
+
async startWithMetricsServer() {
|
|
361
|
+
const service = this.imq.start();
|
|
362
|
+
const metricServerOptions = this.options.metricsServer;
|
|
363
|
+
if (!(metricServerOptions && metricServerOptions.enabled)) {
|
|
364
|
+
return service;
|
|
195
365
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
366
|
+
this.logger.log('Starting metrics server...');
|
|
367
|
+
this.metricsServer = (0, node_http_1.createServer)(async (req, res) => {
|
|
368
|
+
if (req.url === '/metrics') {
|
|
369
|
+
const length = await this.imq.queueLength();
|
|
370
|
+
const content = metricServerOptions.queueLengthFormatter?.(length, 'queue_length') || String(length);
|
|
371
|
+
res.setHeader('Content-Type', 'plain/text');
|
|
372
|
+
res.setHeader('Content-Length', Buffer.byteLength(content));
|
|
373
|
+
res.writeHead(200);
|
|
374
|
+
res.end(content);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
res.writeHead(404);
|
|
378
|
+
res.end();
|
|
379
|
+
});
|
|
380
|
+
this.metricsServer.listen(metricServerOptions.port, '0.0.0.0', () => {
|
|
381
|
+
this.logger.info('%s: metrics server started on port %s', this.name, metricServerOptions.port);
|
|
200
382
|
});
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
this.logger.info('%s: worker #%s started, pid %s', this.name, process.env['workerId'], process.pid);
|
|
204
|
-
this.describe();
|
|
205
|
-
return this.startWithMetricsServer();
|
|
206
|
-
}
|
|
207
|
-
return this.startWithMetricsServer();
|
|
208
|
-
}
|
|
209
|
-
// noinspection JSUnusedGlobalSymbols
|
|
210
|
-
/**
|
|
211
|
-
* Sends given data to service subscription channel
|
|
212
|
-
*
|
|
213
|
-
* @param {JsonObject} data
|
|
214
|
-
*/
|
|
215
|
-
async publish(data) {
|
|
216
|
-
await this.imq.publish(data);
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Stops service from handling messages
|
|
220
|
-
*
|
|
221
|
-
* @return {Promise<void>}
|
|
222
|
-
*/
|
|
223
|
-
async stop() {
|
|
224
|
-
await this.imq.stop();
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Destroys this instance of service
|
|
228
|
-
*
|
|
229
|
-
* @return {Promise<void>}
|
|
230
|
-
*/
|
|
231
|
-
async destroy() {
|
|
232
|
-
await this.imq.unsubscribe();
|
|
233
|
-
await this.imq.destroy();
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Returns service description metadata.
|
|
237
|
-
*
|
|
238
|
-
* @returns {Promise<Description>}
|
|
239
|
-
*/
|
|
240
|
-
describe() {
|
|
241
|
-
let description = serviceDescriptions.get(this.name) || null;
|
|
242
|
-
if (!description) {
|
|
243
|
-
description = {
|
|
244
|
-
service: {
|
|
245
|
-
name: this.name,
|
|
246
|
-
methods: getClassMethods(this.constructor.name)
|
|
247
|
-
},
|
|
248
|
-
types: _1.IMQRPCDescription.typesDescription
|
|
249
|
-
};
|
|
250
|
-
serviceDescriptions.set(this.name, description);
|
|
251
|
-
}
|
|
252
|
-
return description;
|
|
253
|
-
}
|
|
254
|
-
async startWithMetricsServer() {
|
|
255
|
-
const service = this.imq.start();
|
|
256
|
-
const metricServerOptions = this.options.metricsServer;
|
|
257
|
-
if (!(metricServerOptions && metricServerOptions.enabled)) {
|
|
258
383
|
return service;
|
|
259
384
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
var _a;
|
|
263
|
-
if (req.url === '/metrics') {
|
|
264
|
-
const length = await this.imq.queueLength();
|
|
265
|
-
const content = ((_a = metricServerOptions.queueLengthFormatter) === null || _a === void 0 ? void 0 : _a.call(metricServerOptions, length, 'queue_length')) || String(length);
|
|
266
|
-
res.setHeader('Content-Type', 'plain/text');
|
|
267
|
-
res.setHeader('Content-Length', Buffer.byteLength(content));
|
|
268
|
-
res.writeHead(200);
|
|
269
|
-
res.end(content);
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
res.writeHead(404);
|
|
273
|
-
res.end();
|
|
274
|
-
});
|
|
275
|
-
this.metricsServer.listen(metricServerOptions.port, '0.0.0.0', () => {
|
|
276
|
-
this.logger.info('%s: metrics server started on port %s', this.name, metricServerOptions.port);
|
|
277
|
-
});
|
|
278
|
-
return service;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
385
|
+
};
|
|
386
|
+
})();
|
|
281
387
|
exports.IMQService = IMQService;
|
|
282
|
-
__decorate([
|
|
283
|
-
(0, core_1.profile)(),
|
|
284
|
-
__metadata("design:type", Function),
|
|
285
|
-
__metadata("design:paramtypes", []),
|
|
286
|
-
__metadata("design:returntype", Promise)
|
|
287
|
-
], IMQService.prototype, "start", null);
|
|
288
|
-
__decorate([
|
|
289
|
-
(0, core_1.profile)(),
|
|
290
|
-
__metadata("design:type", Function),
|
|
291
|
-
__metadata("design:paramtypes", []),
|
|
292
|
-
__metadata("design:returntype", Promise)
|
|
293
|
-
], IMQService.prototype, "stop", null);
|
|
294
|
-
__decorate([
|
|
295
|
-
(0, core_1.profile)(),
|
|
296
|
-
__metadata("design:type", Function),
|
|
297
|
-
__metadata("design:paramtypes", []),
|
|
298
|
-
__metadata("design:returntype", Promise)
|
|
299
|
-
], IMQService.prototype, "destroy", null);
|
|
300
|
-
__decorate([
|
|
301
|
-
(0, _1.expose)(),
|
|
302
|
-
__metadata("design:type", Function),
|
|
303
|
-
__metadata("design:paramtypes", []),
|
|
304
|
-
__metadata("design:returntype", Description)
|
|
305
|
-
], IMQService.prototype, "describe", null);
|
|
306
388
|
/**
|
|
307
389
|
* Sends IMQ response with support of after call optional hook
|
|
308
390
|
*
|
package/src/cache/ICache.d.ts
CHANGED
|
@@ -21,16 +21,69 @@
|
|
|
21
21
|
* purchase a proprietary commercial license. Please contact us at
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
|
+
/**
|
|
25
|
+
* Constructor signature for cache adapter implementations.
|
|
26
|
+
*/
|
|
24
27
|
export interface ICacheConstructor {
|
|
25
28
|
new (name?: string): ICache;
|
|
26
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Generic cache adapter interface. Any cache engine implementation must
|
|
32
|
+
* conform to this contract to be usable within IMQ.
|
|
33
|
+
*/
|
|
27
34
|
export interface ICache {
|
|
35
|
+
/**
|
|
36
|
+
* Adapter (cache) name.
|
|
37
|
+
*
|
|
38
|
+
* @type {string}
|
|
39
|
+
*/
|
|
28
40
|
name: string;
|
|
41
|
+
/**
|
|
42
|
+
* Whether the cache adapter is initialized and ready to use.
|
|
43
|
+
*
|
|
44
|
+
* @type {boolean}
|
|
45
|
+
*/
|
|
29
46
|
ready: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Initializes the cache adapter with the given adapter-specific options.
|
|
49
|
+
*
|
|
50
|
+
* @param {any} [options] - adapter-specific options
|
|
51
|
+
* @returns {void}
|
|
52
|
+
*/
|
|
30
53
|
init(options?: any): void;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the value stored in the cache under the given key.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} key - key to read the value for
|
|
58
|
+
* @returns {Promise<any>} - stored value, or undefined if not found
|
|
59
|
+
*/
|
|
31
60
|
get(key: string): Promise<any>;
|
|
61
|
+
/**
|
|
62
|
+
* Stores the given value in the cache under the given key.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} key - key to store the value under
|
|
65
|
+
* @param {any} value - value to store
|
|
66
|
+
* @param {number} [ttl] - time-to-live in milliseconds
|
|
67
|
+
* @returns {Promise<boolean>}
|
|
68
|
+
*/
|
|
32
69
|
set(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
70
|
+
/**
|
|
71
|
+
* Removes the value stored in the cache under the given key.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} key - key to remove
|
|
74
|
+
* @returns {Promise<boolean>}
|
|
75
|
+
*/
|
|
33
76
|
del(key: string): Promise<boolean>;
|
|
77
|
+
/**
|
|
78
|
+
* Purges all keys from the cache matching the given wildcard mask.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} keyMask - wildcard mask to match keys against
|
|
81
|
+
* @returns {Promise<boolean>}
|
|
82
|
+
*/
|
|
34
83
|
purge(keyMask: string): Promise<boolean>;
|
|
35
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Accepted cache adapter references: a constructor, an instance, or an
|
|
87
|
+
* adapter name.
|
|
88
|
+
*/
|
|
36
89
|
export type ICacheAdapter = ICacheConstructor | ICache | string;
|