@imqueue/rpc 2.0.26 → 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.d.ts +25 -0
- package/src/IMQRequestContext.js +57 -0
- package/src/IMQService.d.ts +4 -4
- package/src/IMQService.js +303 -217
- package/src/cache/ICache.d.ts +53 -0
- package/src/cache/RedisCache.d.ts +28 -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 +1 -1
- package/src/index.js +1 -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,12 +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
|
|
40
|
-
const
|
|
41
|
-
const
|
|
100
|
+
const helpers_1 = require("./helpers");
|
|
101
|
+
const node_os_1 = require("node:os");
|
|
102
|
+
const node_cluster_1 = __importDefault(require("node:cluster"));
|
|
103
|
+
const IMQRequestContext_1 = require("./IMQRequestContext");
|
|
104
|
+
const node_http_1 = require("node:http");
|
|
42
105
|
class Description {
|
|
106
|
+
service;
|
|
107
|
+
types;
|
|
43
108
|
}
|
|
44
109
|
exports.Description = Description;
|
|
45
110
|
const serviceDescriptions = new Map();
|
|
@@ -68,237 +133,258 @@ function getClassMethods(className) {
|
|
|
68
133
|
* @returns {boolean}
|
|
69
134
|
*/
|
|
70
135
|
function isValidArgsCount(argsInfo, args) {
|
|
71
|
-
|
|
72
|
-
return (argsInfo.some(argInfo => argInfo.isOptional)
|
|
136
|
+
return argsInfo.some(argInfo => argInfo.isOptional)
|
|
73
137
|
? argsInfo.length >= args.length
|
|
74
|
-
: argsInfo.length === args.length
|
|
138
|
+
: argsInfo.length === args.length;
|
|
75
139
|
}
|
|
76
140
|
/**
|
|
77
141
|
* Class IMQService
|
|
78
142
|
* Basic abstract service (server-side) implementation
|
|
79
143
|
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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 });
|
|
94
162
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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);
|
|
103
263
|
}
|
|
104
|
-
// istanbul ignore next
|
|
105
|
-
setTimeout(() => process.exit(0), core_1.IMQ_SHUTDOWN_TIMEOUT);
|
|
106
|
-
}));
|
|
107
|
-
this.imq.on('message', this.handleRequest);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Handles incoming request and produces corresponding response
|
|
111
|
-
*
|
|
112
|
-
* @access private
|
|
113
|
-
* @param {IMQRPCRequest} request - request message
|
|
114
|
-
* @param {string} id - message unique identifier
|
|
115
|
-
* @return {Promise<string>}
|
|
116
|
-
*/
|
|
117
|
-
async handleRequest(request, id) {
|
|
118
|
-
const logger = this.options.logger || console;
|
|
119
|
-
const method = request.method;
|
|
120
|
-
const description = await this.describe();
|
|
121
|
-
const args = request.args;
|
|
122
|
-
let response = {
|
|
123
|
-
to: id,
|
|
124
|
-
data: null,
|
|
125
|
-
error: null,
|
|
126
|
-
request: request,
|
|
127
|
-
};
|
|
128
|
-
if (typeof this.options.beforeCall === 'function') {
|
|
129
|
-
const beforeCall = this.options.beforeCall.bind(this);
|
|
130
264
|
try {
|
|
131
|
-
|
|
265
|
+
response.data = this[method].apply(this, args);
|
|
266
|
+
if (response.data && response.data.then) {
|
|
267
|
+
response.data = await response.data;
|
|
268
|
+
}
|
|
132
269
|
}
|
|
133
270
|
catch (err) {
|
|
134
|
-
|
|
271
|
+
response.error = (0, _1.IMQError)(err.code || 'IMQ_RPC_CALL_ERROR', err.message, err.stack, method, args, err);
|
|
135
272
|
}
|
|
273
|
+
return await send(request, response, this);
|
|
136
274
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
});
|
|
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();
|
|
152
307
|
}
|
|
308
|
+
return this.startWithMetricsServer();
|
|
153
309
|
}
|
|
154
|
-
|
|
155
|
-
|
|
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);
|
|
156
317
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Stops service from handling messages
|
|
320
|
+
*
|
|
321
|
+
* @return {Promise<void>}
|
|
322
|
+
*/
|
|
323
|
+
async stop() {
|
|
324
|
+
await this.imq.stop();
|
|
160
325
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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);
|
|
166
336
|
}
|
|
337
|
+
this.signalHandlers.length = 0;
|
|
338
|
+
await this.imq.unsubscribe();
|
|
339
|
+
await this.imq.destroy();
|
|
167
340
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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;
|
|
184
359
|
}
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
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;
|
|
191
365
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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);
|
|
196
382
|
});
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
this.logger.info('%s: worker #%s started, pid %s', this.name, process.env['workerId'], process.pid);
|
|
200
|
-
this.describe();
|
|
201
|
-
return this.startWithMetricsServer();
|
|
202
|
-
}
|
|
203
|
-
return this.startWithMetricsServer();
|
|
204
|
-
}
|
|
205
|
-
// noinspection JSUnusedGlobalSymbols
|
|
206
|
-
/**
|
|
207
|
-
* Sends given data to service subscription channel
|
|
208
|
-
*
|
|
209
|
-
* @param {JsonObject} data
|
|
210
|
-
*/
|
|
211
|
-
async publish(data) {
|
|
212
|
-
await this.imq.publish(data);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Stops service from handling messages
|
|
216
|
-
*
|
|
217
|
-
* @return {Promise<void>}
|
|
218
|
-
*/
|
|
219
|
-
async stop() {
|
|
220
|
-
await this.imq.stop();
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Destroys this instance of service
|
|
224
|
-
*
|
|
225
|
-
* @return {Promise<void>}
|
|
226
|
-
*/
|
|
227
|
-
async destroy() {
|
|
228
|
-
await this.imq.unsubscribe();
|
|
229
|
-
await this.imq.destroy();
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Returns service description metadata.
|
|
233
|
-
*
|
|
234
|
-
* @returns {Promise<Description>}
|
|
235
|
-
*/
|
|
236
|
-
describe() {
|
|
237
|
-
let description = serviceDescriptions.get(this.name) || null;
|
|
238
|
-
if (!description) {
|
|
239
|
-
description = {
|
|
240
|
-
service: {
|
|
241
|
-
name: this.name,
|
|
242
|
-
methods: getClassMethods(this.constructor.name)
|
|
243
|
-
},
|
|
244
|
-
types: _1.IMQRPCDescription.typesDescription
|
|
245
|
-
};
|
|
246
|
-
serviceDescriptions.set(this.name, description);
|
|
247
|
-
}
|
|
248
|
-
return description;
|
|
249
|
-
}
|
|
250
|
-
async startWithMetricsServer() {
|
|
251
|
-
const service = this.imq.start();
|
|
252
|
-
const metricServerOptions = this.options.metricsServer;
|
|
253
|
-
if (!(metricServerOptions && metricServerOptions.enabled)) {
|
|
254
383
|
return service;
|
|
255
384
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
var _a;
|
|
259
|
-
if (req.url === '/metrics') {
|
|
260
|
-
const length = await this.imq.queueLength();
|
|
261
|
-
const content = ((_a = metricServerOptions.queueLengthFormatter) === null || _a === void 0 ? void 0 : _a.call(metricServerOptions, length, 'queue_length')) || String(length);
|
|
262
|
-
res.setHeader('Content-Type', 'plain/text');
|
|
263
|
-
res.setHeader('Content-Length', Buffer.byteLength(content));
|
|
264
|
-
res.writeHead(200);
|
|
265
|
-
res.end(content);
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
res.writeHead(404);
|
|
269
|
-
res.end();
|
|
270
|
-
});
|
|
271
|
-
this.metricsServer.listen(metricServerOptions.port, '0.0.0.0', () => {
|
|
272
|
-
this.logger.info('%s: metrics server started on port %s', this.name, metricServerOptions.port);
|
|
273
|
-
});
|
|
274
|
-
return service;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
385
|
+
};
|
|
386
|
+
})();
|
|
277
387
|
exports.IMQService = IMQService;
|
|
278
|
-
__decorate([
|
|
279
|
-
(0, core_1.profile)(),
|
|
280
|
-
__metadata("design:type", Function),
|
|
281
|
-
__metadata("design:paramtypes", []),
|
|
282
|
-
__metadata("design:returntype", Promise)
|
|
283
|
-
], IMQService.prototype, "start", null);
|
|
284
|
-
__decorate([
|
|
285
|
-
(0, core_1.profile)(),
|
|
286
|
-
__metadata("design:type", Function),
|
|
287
|
-
__metadata("design:paramtypes", []),
|
|
288
|
-
__metadata("design:returntype", Promise)
|
|
289
|
-
], IMQService.prototype, "stop", null);
|
|
290
|
-
__decorate([
|
|
291
|
-
(0, core_1.profile)(),
|
|
292
|
-
__metadata("design:type", Function),
|
|
293
|
-
__metadata("design:paramtypes", []),
|
|
294
|
-
__metadata("design:returntype", Promise)
|
|
295
|
-
], IMQService.prototype, "destroy", null);
|
|
296
|
-
__decorate([
|
|
297
|
-
(0, _1.expose)(),
|
|
298
|
-
__metadata("design:type", Function),
|
|
299
|
-
__metadata("design:paramtypes", []),
|
|
300
|
-
__metadata("design:returntype", Description)
|
|
301
|
-
], IMQService.prototype, "describe", null);
|
|
302
388
|
/**
|
|
303
389
|
* Sends IMQ response with support of after call optional hook
|
|
304
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;
|