@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/IMQClient.js
CHANGED
|
@@ -1,13 +1,71 @@
|
|
|
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;
|
|
7
24
|
};
|
|
8
|
-
var
|
|
9
|
-
if (
|
|
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;
|
|
10
51
|
};
|
|
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
|
+
})();
|
|
11
69
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
70
|
exports.IMQClient = void 0;
|
|
13
71
|
exports.imqCallResolver = imqCallResolver;
|
|
@@ -35,236 +93,321 @@ exports.imqCallRejector = imqCallRejector;
|
|
|
35
93
|
* purchase a proprietary commercial license. Please contact us at
|
|
36
94
|
* <support@imqueue.com> to get commercial licensing options.
|
|
37
95
|
*/
|
|
38
|
-
const core_1 = require("@imqueue/core");
|
|
96
|
+
const core_1 = __importStar(require("@imqueue/core"));
|
|
39
97
|
const _1 = require(".");
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
98
|
+
const helpers_1 = require("./helpers");
|
|
99
|
+
const typescript_1 = require("typescript");
|
|
100
|
+
const node_events_1 = require("node:events");
|
|
101
|
+
const node_vm_1 = require("node:vm");
|
|
102
|
+
// Loaded via require (not a static import) so that consumers which type-check
|
|
103
|
+
// this source through a file: link do not need `resolveJsonModule` enabled.
|
|
104
|
+
const tsOptions = require('../tsconfig.json')
|
|
105
|
+
.compilerOptions;
|
|
45
106
|
const RX_SEMICOLON = /;+$/g;
|
|
46
107
|
/**
|
|
47
108
|
* Class IMQClient - base abstract class for service clients.
|
|
48
109
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
constructor(options, serviceName, name) {
|
|
60
|
-
var _a;
|
|
61
|
-
super();
|
|
62
|
-
this.resolvers = {};
|
|
63
|
-
const baseName = name || this.constructor.name;
|
|
64
|
-
this.baseName = baseName;
|
|
65
|
-
if (this.constructor.name === 'IMQClient') {
|
|
66
|
-
throw new TypeError('IMQClient class is abstract and cannot ' +
|
|
67
|
-
'be instantiated directly!');
|
|
110
|
+
let IMQClient = (() => {
|
|
111
|
+
let _classSuper = node_events_1.EventEmitter;
|
|
112
|
+
let _instanceExtraInitializers = [];
|
|
113
|
+
let _describe_decorators;
|
|
114
|
+
return class IMQClient extends _classSuper {
|
|
115
|
+
static {
|
|
116
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
117
|
+
_describe_decorators = [(0, _1.remote)()];
|
|
118
|
+
__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);
|
|
119
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
68
120
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
121
|
+
options = __runInitializers(this, _instanceExtraInitializers);
|
|
122
|
+
id;
|
|
123
|
+
name;
|
|
124
|
+
hostName;
|
|
125
|
+
serviceName;
|
|
126
|
+
queueName;
|
|
127
|
+
baseName;
|
|
128
|
+
imq;
|
|
129
|
+
subscriptionImq;
|
|
130
|
+
static singleImq;
|
|
131
|
+
static singleImqRefs = 0;
|
|
132
|
+
static maxListenersBumped = false;
|
|
133
|
+
destroyed = false;
|
|
134
|
+
signalHandlers = [];
|
|
135
|
+
logger;
|
|
136
|
+
resolvers = {};
|
|
137
|
+
/**
|
|
138
|
+
* Class constructor
|
|
139
|
+
*
|
|
140
|
+
* @param {Partial<IMQClientOptions>} options
|
|
141
|
+
* @param {string} serviceName
|
|
142
|
+
* @param {string} name
|
|
143
|
+
*/
|
|
144
|
+
constructor(options, serviceName, name) {
|
|
145
|
+
super();
|
|
146
|
+
const baseName = name || this.constructor.name;
|
|
147
|
+
this.baseName = baseName;
|
|
148
|
+
if (this.constructor.name === 'IMQClient') {
|
|
149
|
+
throw new TypeError('IMQClient class is abstract and cannot ' +
|
|
150
|
+
'be instantiated directly!');
|
|
151
|
+
}
|
|
152
|
+
this.options = { ..._1.DEFAULT_IMQ_CLIENT_OPTIONS, ...options };
|
|
153
|
+
this.id = (0, helpers_1.pid)(baseName);
|
|
154
|
+
this.logger = this.options.logger || /* istanbul ignore next */ console;
|
|
155
|
+
this.hostName =
|
|
156
|
+
IMQClient.singleImq?.name || `${(0, helpers_1.osUuid)()}-${this.id}:client`;
|
|
157
|
+
this.name = `${baseName}-${this.hostName}`;
|
|
158
|
+
this.serviceName = serviceName || baseName.replace(/Client$/, '');
|
|
159
|
+
this.queueName = this.options.singleQueue ? this.hostName : this.name;
|
|
160
|
+
this.imq = this.createImq();
|
|
161
|
+
this.subscriptionImq = this.createSubscriptionImq();
|
|
162
|
+
// raise the process listener limit on first use (many clients may
|
|
163
|
+
// coexist, each registering its own signal handlers)
|
|
164
|
+
if (!IMQClient.maxListenersBumped) {
|
|
165
|
+
IMQClient.maxListenersBumped = true;
|
|
166
|
+
process.setMaxListeners(10000);
|
|
167
|
+
}
|
|
168
|
+
helpers_1.SIGNALS.forEach((signal) => {
|
|
169
|
+
const handler = () => {
|
|
170
|
+
this.destroy().catch(this.logger.error);
|
|
171
|
+
setTimeout(() => process.exit(0), core_1.IMQ_SHUTDOWN_TIMEOUT);
|
|
172
|
+
};
|
|
173
|
+
// tracked so destroy() can unregister them (see below)
|
|
174
|
+
this.signalHandlers.push([signal, handler]);
|
|
175
|
+
process.on(signal, handler);
|
|
176
|
+
});
|
|
88
177
|
}
|
|
89
|
-
|
|
90
|
-
|
|
178
|
+
createImq() {
|
|
179
|
+
if (!this.options.singleQueue) {
|
|
180
|
+
return core_1.default.create(this.queueName, this.options);
|
|
181
|
+
}
|
|
182
|
+
if (!IMQClient.singleImq) {
|
|
183
|
+
IMQClient.singleImq = core_1.default.create(this.queueName, this.options);
|
|
184
|
+
}
|
|
185
|
+
// the shared queue is reference-counted so that destroying one client
|
|
186
|
+
// does not tear the transport down under the others
|
|
187
|
+
IMQClient.singleImqRefs++;
|
|
188
|
+
return IMQClient.singleImq;
|
|
91
189
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return this.
|
|
190
|
+
createSubscriptionImq() {
|
|
191
|
+
if (!this.options.singleQueue) {
|
|
192
|
+
return this.imq;
|
|
193
|
+
}
|
|
194
|
+
return core_1.default.create(this.name, this.options);
|
|
97
195
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
delay = 0;
|
|
196
|
+
/**
|
|
197
|
+
* Sends call to remote service method
|
|
198
|
+
*
|
|
199
|
+
* @param {...any[]} args
|
|
200
|
+
* @template T
|
|
201
|
+
* @returns {Promise<T>}
|
|
202
|
+
*/
|
|
203
|
+
async remoteCall(...args) {
|
|
204
|
+
const logger = this.options.logger || console;
|
|
205
|
+
const method = args.pop();
|
|
206
|
+
const from = this.queueName;
|
|
207
|
+
const to = this.serviceName;
|
|
208
|
+
let delay = 0;
|
|
209
|
+
let metadata;
|
|
210
|
+
if (args[args.length - 1] instanceof _1.IMQDelay) {
|
|
211
|
+
delay = args.pop().ms;
|
|
212
|
+
if (!isFinite(delay) || isNaN(delay) || delay < 0) {
|
|
213
|
+
delay = 0;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (args[args.length - 1] instanceof _1.IMQMetadata) {
|
|
217
|
+
metadata = args.pop();
|
|
121
218
|
}
|
|
219
|
+
const request = {
|
|
220
|
+
from,
|
|
221
|
+
method,
|
|
222
|
+
args,
|
|
223
|
+
...(metadata ? { metadata } : {}),
|
|
224
|
+
};
|
|
225
|
+
if (typeof this.options.beforeCall === 'function') {
|
|
226
|
+
const beforeCall = this.options.beforeCall.bind(this);
|
|
227
|
+
try {
|
|
228
|
+
await beforeCall(request);
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
logger.warn(_1.BEFORE_HOOK_ERROR, err);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const callTimeout = this.options.callTimeout;
|
|
235
|
+
return new Promise((resolve, reject) => {
|
|
236
|
+
let timer = null;
|
|
237
|
+
const clearTimer = () => {
|
|
238
|
+
if (timer) {
|
|
239
|
+
clearTimeout(timer);
|
|
240
|
+
timer = null;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const doResolve = (data) => {
|
|
244
|
+
clearTimer();
|
|
245
|
+
resolve(data);
|
|
246
|
+
};
|
|
247
|
+
const doReject = (err) => {
|
|
248
|
+
clearTimer();
|
|
249
|
+
reject(err);
|
|
250
|
+
};
|
|
251
|
+
void (async () => {
|
|
252
|
+
try {
|
|
253
|
+
const id = await this.imq.send(to, request, delay, doReject);
|
|
254
|
+
this.resolvers[id] = [
|
|
255
|
+
imqCallResolver(doResolve, request, this),
|
|
256
|
+
imqCallRejector(doReject, request, this),
|
|
257
|
+
];
|
|
258
|
+
if (callTimeout && callTimeout > 0) {
|
|
259
|
+
// reject and release the pending resolver if no
|
|
260
|
+
// response arrives in time; a requested delivery delay
|
|
261
|
+
// extends the budget accordingly
|
|
262
|
+
timer = setTimeout(() => {
|
|
263
|
+
delete this.resolvers[id];
|
|
264
|
+
doReject((0, _1.IMQError)('IMQ_RPC_CALL_TIMEOUT', `Call to ${to}.${method}() timed out after ${callTimeout} ms.`, new Error().stack, method, args));
|
|
265
|
+
}, callTimeout + delay);
|
|
266
|
+
timer.unref?.();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
imqCallRejector(doReject, request, this)(err);
|
|
271
|
+
}
|
|
272
|
+
})();
|
|
273
|
+
});
|
|
122
274
|
}
|
|
123
|
-
|
|
124
|
-
|
|
275
|
+
/**
|
|
276
|
+
* Adds subscription to service event channel
|
|
277
|
+
*
|
|
278
|
+
* @param {(data: JsonObject) => any} handler
|
|
279
|
+
* @return {Promise<void>}
|
|
280
|
+
*/
|
|
281
|
+
async subscribe(handler) {
|
|
282
|
+
return this.subscriptionImq.subscribe(this.serviceName, handler);
|
|
125
283
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Destroys subscription channel to service
|
|
286
|
+
*
|
|
287
|
+
* @return {Promise<void>}
|
|
288
|
+
*/
|
|
289
|
+
async unsubscribe() {
|
|
290
|
+
return this.subscriptionImq.unsubscribe();
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Broadcasts given payload to all other service clients subscribed.
|
|
294
|
+
* So this is like client-to-clients publishing.
|
|
295
|
+
*
|
|
296
|
+
* @param {JsonObject} payload
|
|
297
|
+
* @return {Promise<void>}
|
|
298
|
+
*/
|
|
299
|
+
async broadcast(payload) {
|
|
300
|
+
return this.imq.publish(payload, this.queueName);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Initializes client work
|
|
304
|
+
*
|
|
305
|
+
* @returns {Promise<void>}
|
|
306
|
+
*/
|
|
307
|
+
async start() {
|
|
308
|
+
this.imq.on('message', (message) => {
|
|
309
|
+
// the following condition below is hard to test with the
|
|
310
|
+
// current redis mock, BTW it was tested manually on real
|
|
311
|
+
// redis run
|
|
312
|
+
if (!this.resolvers[message.to]) {
|
|
313
|
+
// when there is no resolvers it means
|
|
314
|
+
// we have message in queue which was initiated
|
|
315
|
+
// by some process which is broken. So we provide an
|
|
316
|
+
// ability to handle enqueued messages via EventEmitter
|
|
317
|
+
// interface
|
|
318
|
+
this.emit(message.request.method, message);
|
|
319
|
+
}
|
|
320
|
+
const [resolve, reject] = this.resolvers[message.to] || [];
|
|
321
|
+
// make sure no memory leaking
|
|
322
|
+
delete this.resolvers[message.to];
|
|
323
|
+
if (message.error) {
|
|
324
|
+
return reject && reject(message.error, message);
|
|
325
|
+
}
|
|
326
|
+
resolve && resolve(message.data, message);
|
|
327
|
+
});
|
|
328
|
+
if (this.imq) {
|
|
329
|
+
await this.imq.start();
|
|
136
330
|
}
|
|
137
331
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Stops client work
|
|
334
|
+
*
|
|
335
|
+
* @returns {Promise<void>}
|
|
336
|
+
*/
|
|
337
|
+
async stop() {
|
|
338
|
+
await this.imq.stop();
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Destroys client
|
|
342
|
+
*
|
|
343
|
+
* @returns {Promise<void>}
|
|
344
|
+
*/
|
|
345
|
+
async destroy() {
|
|
346
|
+
if (this.destroyed) {
|
|
347
|
+
return;
|
|
145
348
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
349
|
+
this.destroyed = true;
|
|
350
|
+
// unregister this instance's process signal handlers so destroyed
|
|
351
|
+
// clients do not leak process-level listeners
|
|
352
|
+
for (const [signal, handler] of this.signalHandlers) {
|
|
353
|
+
process.removeListener(signal, handler);
|
|
149
354
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
// noinspection JSUnusedGlobalSymbols
|
|
163
|
-
/**
|
|
164
|
-
* Destroys subscription channel to service
|
|
165
|
-
*
|
|
166
|
-
* @return {Promise<void>}
|
|
167
|
-
*/
|
|
168
|
-
async unsubscribe() {
|
|
169
|
-
return this.subscriptionImq.unsubscribe();
|
|
170
|
-
}
|
|
171
|
-
// noinspection JSUnusedGlobalSymbols
|
|
172
|
-
/**
|
|
173
|
-
* Broadcasts given payload to all other service clients subscribed.
|
|
174
|
-
* So this is like client-to-clients publishing.
|
|
175
|
-
*
|
|
176
|
-
* @param {JsonObject} payload
|
|
177
|
-
* @return {Promise<void>}
|
|
178
|
-
*/
|
|
179
|
-
async broadcast(payload) {
|
|
180
|
-
return this.imq.publish(payload, this.queueName);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Initializes client work
|
|
184
|
-
*
|
|
185
|
-
* @returns {Promise<void>}
|
|
186
|
-
*/
|
|
187
|
-
async start() {
|
|
188
|
-
this.imq.on('message', (message) => {
|
|
189
|
-
// the following condition below is hard to test with the
|
|
190
|
-
// current redis mock, BTW it was tested manually on real
|
|
191
|
-
// redis run
|
|
192
|
-
// istanbul ignore if
|
|
193
|
-
if (!this.resolvers[message.to]) {
|
|
194
|
-
// when there is no resolvers it means
|
|
195
|
-
// we have message in queue which was initiated
|
|
196
|
-
// by some process which is broken. So we provide an
|
|
197
|
-
// ability to handle enqueued messages via EventEmitter
|
|
198
|
-
// interface
|
|
199
|
-
this.emit(message.request.method, message);
|
|
355
|
+
this.signalHandlers.length = 0;
|
|
356
|
+
await this.subscriptionImq.unsubscribe();
|
|
357
|
+
(0, helpers_1.forgetPid)(this.baseName, this.id, this.logger);
|
|
358
|
+
this.removeAllListeners();
|
|
359
|
+
// in singleQueue mode the subscription queue is a separate,
|
|
360
|
+
// per-client instance and must be torn down with the client
|
|
361
|
+
if (this.subscriptionImq !== this.imq) {
|
|
362
|
+
await this.subscriptionImq.destroy();
|
|
363
|
+
}
|
|
364
|
+
if (!this.options.singleQueue) {
|
|
365
|
+
await this.imq.destroy();
|
|
366
|
+
return;
|
|
200
367
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
368
|
+
// shared queue: only the last client tears it down
|
|
369
|
+
if (--IMQClient.singleImqRefs <= 0) {
|
|
370
|
+
IMQClient.singleImqRefs = 0;
|
|
371
|
+
const singleImq = IMQClient.singleImq;
|
|
372
|
+
IMQClient.singleImq = undefined;
|
|
373
|
+
await singleImq?.destroy();
|
|
206
374
|
}
|
|
207
|
-
resolve && resolve(message.data, message);
|
|
208
|
-
});
|
|
209
|
-
if (this.imq) {
|
|
210
|
-
await this.imq.start();
|
|
211
375
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
*/
|
|
239
|
-
async describe(delay) {
|
|
240
|
-
return await this.remoteCall(...arguments);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Creates client for a service with the given name
|
|
244
|
-
*
|
|
245
|
-
* @param {string} name
|
|
246
|
-
* @param {Partial<IMQServiceOptions>} options
|
|
247
|
-
* @returns {IMQClient}
|
|
248
|
-
*/
|
|
249
|
-
static async create(name, options) {
|
|
250
|
-
const clientOptions = Object.assign(Object.assign({}, _1.DEFAULT_IMQ_CLIENT_OPTIONS), options);
|
|
251
|
-
return await generator(name, clientOptions);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
376
|
+
/**
|
|
377
|
+
* Returns service description metadata.
|
|
378
|
+
*
|
|
379
|
+
* @param {IMQDelay} [_delay] - optional delivery delay; forwarded to the
|
|
380
|
+
* service through `arguments` by the `@remote` decorator
|
|
381
|
+
* @returns {Promise<Description>}
|
|
382
|
+
*/
|
|
383
|
+
async describe(_delay) {
|
|
384
|
+
return await this.remoteCall(...arguments);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Creates client for a service with the given name
|
|
388
|
+
*
|
|
389
|
+
* @param {string} name
|
|
390
|
+
* @param {Partial<IMQServiceOptions>} options
|
|
391
|
+
* @returns {IMQClient}
|
|
392
|
+
*/
|
|
393
|
+
static async create(name, options) {
|
|
394
|
+
const clientOptions = {
|
|
395
|
+
..._1.DEFAULT_IMQ_CLIENT_OPTIONS,
|
|
396
|
+
...options,
|
|
397
|
+
};
|
|
398
|
+
return await generator(name, clientOptions);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
})();
|
|
254
402
|
exports.IMQClient = IMQClient;
|
|
255
|
-
__decorate([
|
|
256
|
-
(0, _1.remote)(),
|
|
257
|
-
__metadata("design:type", Function),
|
|
258
|
-
__metadata("design:paramtypes", [_1.IMQDelay]),
|
|
259
|
-
__metadata("design:returntype", Promise)
|
|
260
|
-
], IMQClient.prototype, "describe", null);
|
|
261
403
|
/**
|
|
262
|
-
* Builds
|
|
404
|
+
* Builds a call resolver that resolves the pending promise and then runs the
|
|
405
|
+
* optional after-call hook.
|
|
263
406
|
*
|
|
264
|
-
* @param {(
|
|
265
|
-
* @param {IMQRPCRequest} req
|
|
266
|
-
* @param {IMQClient} client -
|
|
267
|
-
* @return {(data: any, res: IMQRPCResponse) => void} - hook-
|
|
407
|
+
* @param {(data: any) => void} resolve - the underlying promise resolver
|
|
408
|
+
* @param {IMQRPCRequest} req - the originating request message
|
|
409
|
+
* @param {IMQClient} client - the client the call belongs to
|
|
410
|
+
* @return {(data: any, res: IMQRPCResponse) => void} - a hook-aware resolver
|
|
268
411
|
*/
|
|
269
412
|
function imqCallResolver(resolve, req, client) {
|
|
270
413
|
return async (data, res) => {
|
|
@@ -282,12 +425,13 @@ function imqCallResolver(resolve, req, client) {
|
|
|
282
425
|
};
|
|
283
426
|
}
|
|
284
427
|
/**
|
|
285
|
-
* Builds
|
|
428
|
+
* Builds a call rejector that rejects the pending promise and then runs the
|
|
429
|
+
* optional after-call hook.
|
|
286
430
|
*
|
|
287
|
-
* @param {(err: any) => void} reject -
|
|
288
|
-
* @param {IMQRPCRequest} req -
|
|
289
|
-
* @param {IMQClient} client -
|
|
290
|
-
* @return {(err: any) => void} - hook-
|
|
431
|
+
* @param {(err: any) => void} reject - the underlying promise rejector
|
|
432
|
+
* @param {IMQRPCRequest} req - the originating request message
|
|
433
|
+
* @param {IMQClient} client - the client the call belongs to
|
|
434
|
+
* @return {(err: any, res?: IMQRPCResponse) => void} - a hook-aware rejector
|
|
291
435
|
*/
|
|
292
436
|
function imqCallRejector(reject, req, client) {
|
|
293
437
|
return async (err, res) => {
|
|
@@ -306,7 +450,6 @@ function imqCallRejector(reject, req, client) {
|
|
|
306
450
|
}
|
|
307
451
|
/**
|
|
308
452
|
* Class GeneratorClient - generator helper class implementation
|
|
309
|
-
* @access private
|
|
310
453
|
*/
|
|
311
454
|
class GeneratorClient extends IMQClient {
|
|
312
455
|
}
|
|
@@ -314,32 +457,31 @@ class GeneratorClient extends IMQClient {
|
|
|
314
457
|
* Fetches and returns service description using the timeout (to handle
|
|
315
458
|
* situations when the service is not started)
|
|
316
459
|
*
|
|
317
|
-
* @access private
|
|
318
460
|
* @param {string} name
|
|
319
461
|
* @param {IMQClientOptions} options
|
|
320
462
|
* @returns {Promise<Description>}
|
|
321
463
|
*/
|
|
322
464
|
async function getDescription(name, options) {
|
|
323
|
-
return new Promise(
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
465
|
+
return new Promise((resolve, reject) => {
|
|
466
|
+
void (async () => {
|
|
467
|
+
const client = new GeneratorClient(options, name, `${name}Client`);
|
|
468
|
+
await client.start();
|
|
469
|
+
const timeout = setTimeout(async () => {
|
|
470
|
+
await client.destroy();
|
|
471
|
+
timeout && clearTimeout(timeout);
|
|
472
|
+
reject(new EvalError('Generate client error: service remote ' +
|
|
473
|
+
`call timed-out! Is service "${name}" running?`));
|
|
474
|
+
}, options.timeout);
|
|
475
|
+
const description = await client.describe();
|
|
328
476
|
timeout && clearTimeout(timeout);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
const description = await client.describe();
|
|
333
|
-
timeout && clearTimeout(timeout);
|
|
334
|
-
await client.destroy();
|
|
335
|
-
resolve(description);
|
|
477
|
+
await client.destroy();
|
|
478
|
+
resolve(description);
|
|
479
|
+
})();
|
|
336
480
|
});
|
|
337
481
|
}
|
|
338
|
-
// codebeat:disable[LOC,ABC]
|
|
339
482
|
/**
|
|
340
483
|
* Client generator helper function
|
|
341
484
|
*
|
|
342
|
-
* @access private
|
|
343
485
|
* @param {string} name
|
|
344
486
|
* @param {IMQClientOptions} options
|
|
345
487
|
* @returns {Promise<string>}
|
|
@@ -348,8 +490,7 @@ async function generator(name, options) {
|
|
|
348
490
|
const description = await getDescription(name, options);
|
|
349
491
|
const serviceName = description.service.name;
|
|
350
492
|
const clientName = serviceName.replace(/Service$|$/, 'Client');
|
|
351
|
-
const namespaceName = serviceName.charAt(0).toLowerCase() +
|
|
352
|
-
serviceName.substr(1);
|
|
493
|
+
const namespaceName = serviceName.charAt(0).toLowerCase() + serviceName.substr(1);
|
|
353
494
|
let src = `/*!
|
|
354
495
|
* IMQ-RPC Service Client: ${description.service.name}
|
|
355
496
|
*
|
|
@@ -408,21 +549,24 @@ export namespace ${namespaceName} {\n`;
|
|
|
408
549
|
const args = methods[methodName].arguments;
|
|
409
550
|
const description = methods[methodName].description;
|
|
410
551
|
const ret = methods[methodName].returns;
|
|
411
|
-
const addArgs = [
|
|
552
|
+
const addArgs = [
|
|
553
|
+
{
|
|
412
554
|
description: 'if passed, will deliver given metadata to ' +
|
|
413
555
|
'service, and will initiate trace handler calls',
|
|
414
556
|
name: 'imqMetadata',
|
|
415
557
|
type: 'IMQMetadata',
|
|
416
558
|
tsType: 'IMQMetadata',
|
|
417
559
|
isOptional: true,
|
|
418
|
-
},
|
|
560
|
+
},
|
|
561
|
+
{
|
|
419
562
|
description: 'if passed the method will be called with ' +
|
|
420
563
|
'the specified delay over message queue',
|
|
421
564
|
name: 'imqDelay',
|
|
422
565
|
type: 'IMQDelay',
|
|
423
566
|
tsType: 'IMQDelay',
|
|
424
|
-
isOptional: true
|
|
425
|
-
}
|
|
567
|
+
isOptional: true,
|
|
568
|
+
},
|
|
569
|
+
];
|
|
426
570
|
let retType = ret.tsType.replace(/\r?\n/g, ' ').replace(/\s{2,}/g, ' ');
|
|
427
571
|
for (let i = 1; i <= 2; i++) {
|
|
428
572
|
const arg = args[args.length - i];
|
|
@@ -431,15 +575,16 @@ export namespace ${namespaceName} {\n`;
|
|
|
431
575
|
}
|
|
432
576
|
}
|
|
433
577
|
args.push(...addArgs); // make sure client expect them
|
|
434
|
-
// istanbul ignore if
|
|
435
578
|
if (retType === 'Promise') {
|
|
436
579
|
retType = 'Promise<any>';
|
|
437
580
|
}
|
|
438
581
|
src += ' /**\n';
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
582
|
+
src += description
|
|
583
|
+
? description
|
|
584
|
+
.split(/\r?\n/)
|
|
585
|
+
.map(line => ` * ${line}`)
|
|
586
|
+
.join('\n') + '\n *\n'
|
|
587
|
+
: '';
|
|
443
588
|
for (let i = 0, s = args.length; i < s; i++) {
|
|
444
589
|
const arg = args[i];
|
|
445
590
|
src += ` * @param {${toComment(arg.tsType)}} `;
|
|
@@ -454,9 +599,12 @@ export namespace ${namespaceName} {\n`;
|
|
|
454
599
|
src += ` public async ${methodName}(`;
|
|
455
600
|
for (let i = 0, s = args.length; i < s; i++) {
|
|
456
601
|
const arg = args[i];
|
|
457
|
-
src +=
|
|
458
|
-
|
|
459
|
-
|
|
602
|
+
src +=
|
|
603
|
+
arg.name +
|
|
604
|
+
(arg.isOptional ? '?' : '') +
|
|
605
|
+
': ' +
|
|
606
|
+
arg.tsType.replace(/\s{2,}/g, ' ') +
|
|
607
|
+
(i === s - 1 ? '' : ', ');
|
|
460
608
|
}
|
|
461
609
|
src += `): ${promisedType(retType)} {\n`;
|
|
462
610
|
src += ' '.repeat(12);
|
|
@@ -467,17 +615,14 @@ export namespace ${namespaceName} {\n`;
|
|
|
467
615
|
const module = await compile(name, src, options);
|
|
468
616
|
return module ? module[namespaceName] : /* istanbul ignore next */ null;
|
|
469
617
|
}
|
|
470
|
-
// codebeat:enable[LOC,ABC]
|
|
471
618
|
/**
|
|
472
|
-
* Return promised typedef of a given type if its missing
|
|
619
|
+
* Return the promised typedef of a given type if its missing
|
|
473
620
|
*
|
|
474
|
-
*c @access private
|
|
475
621
|
* @param {string} typedef
|
|
476
622
|
* @returns {string}
|
|
477
623
|
*/
|
|
478
624
|
function promisedType(typedef) {
|
|
479
|
-
|
|
480
|
-
if (!/^Promise</.test(typedef)) {
|
|
625
|
+
if (!typedef.startsWith('Promise<')) {
|
|
481
626
|
typedef = `Promise<${typedef}>`;
|
|
482
627
|
}
|
|
483
628
|
return typedef;
|
|
@@ -485,7 +630,6 @@ function promisedType(typedef) {
|
|
|
485
630
|
/**
|
|
486
631
|
* Removes Promise from type definition if any
|
|
487
632
|
*
|
|
488
|
-
* @access private
|
|
489
633
|
* @param {string} typedef
|
|
490
634
|
* @returns {string}
|
|
491
635
|
*/
|
|
@@ -495,7 +639,6 @@ function cleanType(typedef) {
|
|
|
495
639
|
/**
|
|
496
640
|
* Type to comment
|
|
497
641
|
*
|
|
498
|
-
* @access private
|
|
499
642
|
* @param {string} typedef
|
|
500
643
|
* @param {boolean} [promised]
|
|
501
644
|
* @returns {string}
|
|
@@ -504,15 +647,14 @@ function toComment(typedef, promised = false) {
|
|
|
504
647
|
if (promised) {
|
|
505
648
|
typedef = promisedType(typedef);
|
|
506
649
|
}
|
|
507
|
-
|
|
508
|
-
|
|
650
|
+
return typedef
|
|
651
|
+
.split(/\r?\n/)
|
|
509
652
|
.map((line, lineNum) => (lineNum ? ' * ' : '') + line)
|
|
510
653
|
.join('\n');
|
|
511
654
|
}
|
|
512
655
|
/**
|
|
513
656
|
* Compiles client source code and returns loaded module
|
|
514
657
|
*
|
|
515
|
-
* @access private
|
|
516
658
|
* @param {string} name
|
|
517
659
|
* @param {string} src
|
|
518
660
|
* @param {IMQClientOptions} options
|
|
@@ -522,25 +664,19 @@ async function compile(name, src, options) {
|
|
|
522
664
|
const path = options.path;
|
|
523
665
|
const srcFile = `${path}/${name}.ts`;
|
|
524
666
|
const jsFile = `${path}/${name}.js`;
|
|
525
|
-
const js =
|
|
667
|
+
const js = (0, typescript_1.transpile)(src, tsOptions);
|
|
526
668
|
if (options.write) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
await (0, _1.mkdir)(path);
|
|
669
|
+
if (!(await (0, helpers_1.fileExists)(path))) {
|
|
670
|
+
await (0, helpers_1.mkdir)(path);
|
|
530
671
|
}
|
|
531
|
-
await Promise.all([
|
|
532
|
-
(0, _1.writeFile)(srcFile, src),
|
|
533
|
-
(0, _1.writeFile)(jsFile, js),
|
|
534
|
-
]);
|
|
672
|
+
await Promise.all([(0, helpers_1.writeFile)(srcFile, src), (0, helpers_1.writeFile)(jsFile, js)]);
|
|
535
673
|
}
|
|
536
|
-
// istanbul ignore else
|
|
537
674
|
if (options.compile) {
|
|
538
|
-
const script = new
|
|
675
|
+
const script = new node_vm_1.Script(js);
|
|
539
676
|
const context = { exports: {}, require };
|
|
540
677
|
script.runInNewContext(context, { filename: jsFile });
|
|
541
678
|
return context.exports;
|
|
542
679
|
}
|
|
543
|
-
// istanbul ignore next
|
|
544
680
|
return null;
|
|
545
681
|
}
|
|
546
682
|
//# sourceMappingURL=IMQClient.js.map
|