@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.
Files changed (59) hide show
  1. package/README.md +68 -0
  2. package/package.json +26 -61
  3. package/src/IMQCache.d.ts +10 -9
  4. package/src/IMQCache.js +16 -22
  5. package/src/IMQClient.d.ts +21 -16
  6. package/src/IMQClient.js +466 -270
  7. package/src/IMQDelay.d.ts +14 -0
  8. package/src/IMQDelay.js +26 -5
  9. package/src/IMQLock.d.ts +17 -15
  10. package/src/IMQLock.js +36 -36
  11. package/src/IMQMetadata.d.ts +8 -0
  12. package/src/IMQMetadata.js +8 -0
  13. package/src/IMQRPCDescription.d.ts +2 -2
  14. package/src/IMQRPCDescription.js +2 -2
  15. package/src/IMQRPCError.d.ts +7 -7
  16. package/src/IMQRPCError.js +7 -8
  17. package/src/IMQRPCOptions.d.ts +27 -3
  18. package/src/IMQRPCOptions.js +17 -3
  19. package/src/IMQRPCRequest.d.ts +1 -1
  20. package/src/IMQRPCResponse.d.ts +1 -1
  21. package/src/IMQRequestContext.js +1 -2
  22. package/src/IMQService.d.ts +3 -4
  23. package/src/IMQService.js +302 -220
  24. package/src/cache/ICache.d.ts +53 -0
  25. package/src/cache/RedisCache.d.ts +36 -31
  26. package/src/cache/RedisCache.js +104 -61
  27. package/src/decorators/cache.d.ts +12 -0
  28. package/src/decorators/cache.js +56 -41
  29. package/src/decorators/classType.d.ts +43 -0
  30. package/src/decorators/classType.js +83 -0
  31. package/src/decorators/expose.d.ts +5 -31
  32. package/src/decorators/expose.js +147 -129
  33. package/src/decorators/index.d.ts +2 -0
  34. package/src/decorators/index.js +4 -0
  35. package/src/decorators/indexed.d.ts +23 -0
  36. package/src/decorators/indexed.js +22 -13
  37. package/src/decorators/lock.d.ts +10 -11
  38. package/src/decorators/lock.js +63 -56
  39. package/src/decorators/logged.d.ts +11 -9
  40. package/src/decorators/logged.js +47 -40
  41. package/src/decorators/metadata.d.ts +24 -0
  42. package/src/decorators/metadata.js +45 -0
  43. package/src/decorators/property.d.ts +7 -57
  44. package/src/decorators/property.js +112 -83
  45. package/src/decorators/remote.d.ts +7 -7
  46. package/src/decorators/remote.js +19 -13
  47. package/src/helpers/fs.d.ts +13 -13
  48. package/src/helpers/fs.js +19 -19
  49. package/src/helpers/os-uuid.d.ts +2 -1
  50. package/src/helpers/os-uuid.js +86 -3
  51. package/src/helpers/pid.d.ts +19 -9
  52. package/src/helpers/pid.js +33 -33
  53. package/src/helpers/signature.d.ts +9 -7
  54. package/src/helpers/signature.js +80 -10
  55. package/src/index.d.ts +0 -1
  56. package/src/index.js +0 -1
  57. package/tsconfig.json +18 -14
  58. package/.github/workflows/build.yml +0 -28
  59. package/debug.d.ts +0 -1
package/README.md CHANGED
@@ -179,6 +179,74 @@ import { hello } from './clients/Hello';
179
179
  In this case all complex types defined within service implementation
180
180
  will be available under imported namespace of the client.
181
181
 
182
+ ## Complex Types
183
+
184
+ To expose complex (object) types as service method arguments or return values,
185
+ annotate the class with `@classType()` and its fields with `@property()`:
186
+
187
+ ~~~typescript
188
+ import { classType, property, expose, IMQService } from '@imqueue/rpc';
189
+
190
+ @classType()
191
+ class Address {
192
+ @property('string')
193
+ country: string;
194
+
195
+ @property('string', true)
196
+ zipCode?: string; // optional
197
+ }
198
+
199
+ @classType()
200
+ class User {
201
+ @property('string')
202
+ firstName: string;
203
+
204
+ @property('Array<Address>', true)
205
+ addresses?: Address[];
206
+ }
207
+
208
+ class UserService extends IMQService {
209
+ /**
210
+ * Persists the given user
211
+ *
212
+ * @param {User} user - user to save
213
+ * @returns {Promise<boolean>}
214
+ */
215
+ @expose()
216
+ public async save(user: User): Promise<boolean> {
217
+ // User and Address are now exposed to generated clients
218
+ return true;
219
+ }
220
+ }
221
+ ~~~
222
+
223
+ The `@classType()` class decorator is **required** on every class that uses
224
+ `@property()` — without it the type will not be registered and will not appear
225
+ in generated clients. (Indexed types use `@indexed()`, which registers `@property`
226
+ fields as well.)
227
+
228
+ ## Requirements
229
+
230
+ This package uses **standard (TC39) decorators**. Consuming projects must set,
231
+ in their `tsconfig.json`:
232
+
233
+ ~~~json
234
+ {
235
+ "compilerOptions": {
236
+ "experimentalDecorators": false,
237
+ "removeComments": false,
238
+ "lib": ["es2023", "esnext.decorators"]
239
+ }
240
+ }
241
+ ~~~
242
+
243
+ Because standard decorators provide no runtime type reflection (there is no
244
+ `emitDecoratorMetadata`), the RPC layer derives argument and return types from
245
+ **JSDoc**. Therefore **every exposed method must be documented with JSDoc**
246
+ `@param`/`@returns` tags carrying the types (as shown in the examples above),
247
+ and `removeComments` must remain `false` so those comments survive compilation.
248
+ Undocumented parameters fall back to `any` in generated clients.
249
+
182
250
  ## Notes
183
251
 
184
252
  For image containers builds assign machine UUID in `/etc/machine-id` and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "RPC-like client-service implementation over messaging queue",
5
5
  "keywords": [
6
6
  "redis",
@@ -13,18 +13,24 @@
13
13
  "message-queue"
14
14
  ],
15
15
  "scripts": {
16
- "prepublishOnly": "./node_modules/.bin/tsc",
17
- "test": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && ./node_modules/.bin/nyc report --reporter=text-lcov",
18
- "test-fast": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/coverage/index.html', { wait: false }))\"",
19
- "test-local": "export COVERALLS_REPO_TOKEN=$IMQ_RPC_COVERALLS_TOKEN && npm test && /usr/bin/env node -e \"import('open').then(open => open.default('https://coveralls.io/github/imqueue/imq-rpc', { wait: false }));\"",
16
+ "clean-compiled": "npm run clean-js && npm run clean-typedefs && npm run clean-maps",
17
+ "build:deps": "if [ -f ../core/package.json ]; then npm run build --prefix ../core; fi",
18
+ "build": "npm run build:deps && npm run clean-compiled && tsc",
19
+ "prepublishOnly": "npm run build",
20
+ "lint": "oxlint",
21
+ "format": "oxfmt \"index.ts\" \"debug.ts\" \"src/**/*.ts\" \"test/**/*.ts\"",
22
+ "format:check": "oxfmt --check \"index.ts\" \"debug.ts\" \"src/**/*.ts\" \"test/**/*.ts\"",
23
+ "test": "npm run build && node --experimental-test-module-mocks --test --test-timeout=15000 $(find test -name '*.spec.js')",
24
+ "test-coverage": "npm run build && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-timeout=15000 $(find test -name '*.spec.js')",
25
+ "test-lcov": "npm run build && mkdir -p coverage && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
26
+ "test-coverage-html": "npm run test-lcov; genhtml coverage/lcov.info --output-directory coverage/html --ignore-errors inconsistent,corrupt,format,mismatch && echo \"Coverage report: file://$(pwd)/coverage/html/index.html\"",
27
+ "test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
20
28
  "clean-typedefs": "find . -name '*.d.ts' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
21
29
  "clean-maps": "find . -name '*.js.map' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
22
30
  "clean-js": "find . -name '*.js' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
23
31
  "clean-tests": "rm -rf .nyc_output coverage",
24
- "clean-doc": "rm -rf docs",
25
32
  "clean-benchmark": "rm -rf benchmark-result",
26
- "clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-doc && npm run clean-benchmark",
27
- "doc": "rm -rf docs && typedoc --excludePrivate --excludeExternals --hideGenerator --exclude \"**/+(debug|test|node_modules|docs|coverage|benchmark|.nyc_output)/**/*\" --out ./docs . && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/docs/index.html', { wait: false }));\""
33
+ "clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-benchmark"
28
34
  },
29
35
  "repository": {
30
36
  "type": "git",
@@ -37,62 +43,21 @@
37
43
  "author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
38
44
  "license": "GPL-3.0-only",
39
45
  "dependencies": {
40
- "@imqueue/core": "^2.0.26",
41
- "@types/node": "^24.9.1",
42
- "acorn": "^8.15.0",
43
- "farmhash": "^5.0.1",
44
- "node-machine-id": "^1.1.12",
45
- "reflect-metadata": "^0.2.2",
46
- "typescript": "^5.9.3"
46
+ "@imqueue/core": "^3.1.0",
47
+ "acorn": "^8.17.0",
48
+ "typescript": "^7.0.2"
47
49
  },
48
50
  "devDependencies": {
49
- "@types/chai": "^5.2.2",
50
- "@types/mocha": "^10.0.10",
51
- "@types/mock-require": "^3.0.0",
52
- "@types/sinon": "^17.0.4",
53
- "chai": "^6.0.1",
54
- "coveralls-next": "^5.0.0",
55
- "minimist": "^1.2.8",
56
- "mocha": "^11.7.2",
57
- "mocha-lcov-reporter": "^1.3.0",
58
- "mock-require": "^3.0.3",
59
- "nyc": "^17.1.0",
60
- "open": "^10.1.2",
61
- "sinon": "^21.0.0",
62
- "source-map-support": "^0.5.21",
63
- "ts-node": "^10.9.2",
64
- "typedoc": "^0.28.12"
51
+ "@types/node": "^24.9.1",
52
+ "oxfmt": "0.57.0",
53
+ "oxlint": "1.72.0"
65
54
  },
66
55
  "main": "index.js",
67
- "typescript": {
68
- "definitions": "index.d.ts"
69
- },
70
- "mocha": {
71
- "require": [
72
- "ts-node/register",
73
- "source-map-support/register"
74
- ],
75
- "recursive": true,
76
- "bail": true,
77
- "full-trace": true
78
- },
79
- "nyc": {
80
- "check-coverage": false,
81
- "extension": [
82
- ".ts"
83
- ],
84
- "exclude": [
85
- "**/*.d.ts",
86
- "**/test/**"
87
- ],
88
- "require": [
89
- "ts-node/register"
90
- ],
91
- "reporter": [
92
- "html",
93
- "text",
94
- "text-summary",
95
- "lcovonly"
96
- ]
56
+ "types": "index.d.ts",
57
+ "exports": {
58
+ ".": {
59
+ "types": "./index.d.ts",
60
+ "default": "./index.js"
61
+ }
97
62
  }
98
63
  }
package/src/IMQCache.d.ts CHANGED
@@ -31,31 +31,32 @@ export declare class IMQCache {
31
31
  [name: string]: ICache;
32
32
  };
33
33
  /**
34
- * Registers given cache adapter
34
+ * Registers the given cache adapter.
35
35
  *
36
- * @param { ICache | string} adapter - adapter name or class or instance
37
- * @param {any} options - adapter specific options
36
+ * @param {ICacheAdapter | string} adapter - adapter name, class or
37
+ * instance
38
+ * @param {any} [options] - adapter-specific options
38
39
  * @returns {IMQCache}
39
40
  */
40
41
  static register(adapter: ICacheAdapter | string, options?: any): typeof IMQCache;
41
42
  /**
42
- * Overrides existing adapter options with the given
43
+ * Overrides existing adapter options with the given ones.
43
44
  *
44
- * @param {ICache | string} adapter - adapter to apply options to
45
- * @param {any} options - adapter specific options
45
+ * @param {ICacheAdapter | string} adapter - adapter to apply options to
46
+ * @param {any} options - adapter-specific options
46
47
  * @returns {IMQCache}
47
48
  */
48
49
  static apply(adapter: ICacheAdapter | string, options: any): typeof IMQCache;
49
50
  /**
50
- * Initializes all registered cache adapters
51
+ * Initializes all registered cache adapters.
51
52
  *
52
53
  * @returns {Promise<any>}
53
54
  */
54
55
  static init(): Promise<any>;
55
56
  /**
56
- * Returns registered cache adapter by its given name or class
57
+ * Returns a registered cache adapter by its given name or class.
57
58
  *
58
- * @param { ICache | string} adapter - adapter name or class
59
+ * @param {ICacheAdapter} adapter - adapter name, class or instance
59
60
  * @returns {ICache} - adapter instance
60
61
  */
61
62
  static get(adapter: ICacheAdapter): ICache;
package/src/IMQCache.js CHANGED
@@ -5,23 +5,24 @@ exports.IMQCache = void 0;
5
5
  * Generic cache registry
6
6
  */
7
7
  class IMQCache {
8
+ static options = {};
9
+ static adapters = {};
8
10
  /**
9
- * Registers given cache adapter
11
+ * Registers the given cache adapter.
10
12
  *
11
- * @param { ICache | string} adapter - adapter name or class or instance
12
- * @param {any} options - adapter specific options
13
+ * @param {ICacheAdapter | string} adapter - adapter name, class or
14
+ * instance
15
+ * @param {any} [options] - adapter-specific options
13
16
  * @returns {IMQCache}
14
17
  */
15
18
  static register(adapter, options) {
16
19
  const self = IMQCache;
17
20
  if (typeof adapter === 'string') {
18
- // istanbul ignore else
19
21
  if (!self.adapters[adapter]) {
20
- self.adapters[adapter] = new (require(`${__dirname}/cache/${adapter}.js`)[adapter])();
22
+ self.adapters[adapter] = (new (require(`${__dirname}/cache/${adapter}.js`)[adapter])());
21
23
  }
22
24
  }
23
25
  else {
24
- // istanbul ignore else
25
26
  if (!self.adapters[adapter.name]) {
26
27
  if (typeof adapter === 'function') {
27
28
  self.adapters[adapter.name] =
@@ -37,10 +38,10 @@ class IMQCache {
37
38
  return self;
38
39
  }
39
40
  /**
40
- * Overrides existing adapter options with the given
41
+ * Overrides existing adapter options with the given ones.
41
42
  *
42
- * @param {ICache | string} adapter - adapter to apply options to
43
- * @param {any} options - adapter specific options
43
+ * @param {ICacheAdapter | string} adapter - adapter to apply options to
44
+ * @param {any} options - adapter-specific options
44
45
  * @returns {IMQCache}
45
46
  */
46
47
  static apply(adapter, options) {
@@ -48,15 +49,13 @@ class IMQCache {
48
49
  if (!options) {
49
50
  return self;
50
51
  }
51
- const name = typeof adapter === 'string'
52
- ? adapter
53
- : adapter.name;
52
+ const name = typeof adapter === 'string' ? adapter : adapter.name;
54
53
  let opts = self.options[name] || {};
55
- self.options[name] = Object.assign(Object.assign({}, opts), options);
54
+ self.options[name] = { ...opts, ...options };
56
55
  return self;
57
56
  }
58
57
  /**
59
- * Initializes all registered cache adapters
58
+ * Initializes all registered cache adapters.
60
59
  *
61
60
  * @returns {Promise<any>}
62
61
  */
@@ -64,7 +63,6 @@ class IMQCache {
64
63
  const self = IMQCache;
65
64
  const promises = [];
66
65
  for (let adapter of Object.keys(self.adapters)) {
67
- // istanbul ignore else
68
66
  if (!self.adapters[adapter].ready) {
69
67
  promises.push(self.adapters[adapter].init(self.options[adapter]));
70
68
  }
@@ -73,18 +71,14 @@ class IMQCache {
73
71
  return self;
74
72
  }
75
73
  /**
76
- * Returns registered cache adapter by its given name or class
74
+ * Returns a registered cache adapter by its given name or class.
77
75
  *
78
- * @param { ICache | string} adapter - adapter name or class
76
+ * @param {ICacheAdapter} adapter - adapter name, class or instance
79
77
  * @returns {ICache} - adapter instance
80
78
  */
81
79
  static get(adapter) {
82
- return IMQCache.adapters[typeof adapter === 'string'
83
- ? adapter
84
- : adapter.name];
80
+ return IMQCache.adapters[typeof adapter === 'string' ? adapter : adapter.name];
85
81
  }
86
82
  }
87
83
  exports.IMQCache = IMQCache;
88
- IMQCache.options = {};
89
- IMQCache.adapters = {};
90
84
  //# sourceMappingURL=IMQCache.js.map
@@ -23,7 +23,7 @@
23
23
  */
24
24
  import { JsonObject } from '@imqueue/core';
25
25
  import { IMQClientOptions, IMQRPCResponse, IMQRPCRequest, IMQDelay, Description } from '.';
26
- import { EventEmitter } from 'events';
26
+ import { EventEmitter } from 'node:events';
27
27
  /**
28
28
  * Class IMQClient - base abstract class for service clients.
29
29
  */
@@ -37,13 +37,16 @@ export declare abstract class IMQClient extends EventEmitter {
37
37
  private readonly baseName;
38
38
  private readonly imq;
39
39
  private readonly subscriptionImq;
40
- private static singleImq;
40
+ private static singleImq?;
41
+ private static singleImqRefs;
42
+ private static maxListenersBumped;
43
+ private destroyed;
44
+ private readonly signalHandlers;
41
45
  private readonly logger;
42
46
  private resolvers;
43
47
  /**
44
48
  * Class constructor
45
49
  *
46
- * @constructor
47
50
  * @param {Partial<IMQClientOptions>} options
48
51
  * @param {string} serviceName
49
52
  * @param {string} name
@@ -54,7 +57,6 @@ export declare abstract class IMQClient extends EventEmitter {
54
57
  /**
55
58
  * Sends call to remote service method
56
59
  *
57
- * @access protected
58
60
  * @param {...any[]} args
59
61
  * @template T
60
62
  * @returns {Promise<T>}
@@ -102,10 +104,11 @@ export declare abstract class IMQClient extends EventEmitter {
102
104
  /**
103
105
  * Returns service description metadata.
104
106
  *
105
- * @param {IMQDelay} delay
107
+ * @param {IMQDelay} [_delay] - optional delivery delay; forwarded to the
108
+ * service through `arguments` by the `@remote` decorator
106
109
  * @returns {Promise<Description>}
107
110
  */
108
- describe(delay?: IMQDelay): Promise<Description>;
111
+ describe(_delay?: IMQDelay): Promise<Description>;
109
112
  /**
110
113
  * Creates client for a service with the given name
111
114
  *
@@ -116,20 +119,22 @@ export declare abstract class IMQClient extends EventEmitter {
116
119
  static create(name: string, options?: Partial<IMQClientOptions>): Promise<any>;
117
120
  }
118
121
  /**
119
- * Builds and returns call resolver, which supports after call optional hook
122
+ * Builds a call resolver that resolves the pending promise and then runs the
123
+ * optional after-call hook.
120
124
  *
121
- * @param {(...args: any[]) => void} resolve - source promise like resolver
122
- * @param {IMQRPCRequest} req - request message
123
- * @param {IMQClient} client - imq client
124
- * @return {(data: any, res: IMQRPCResponse) => void} - hook-supported resolve
125
+ * @param {(data: any) => void} resolve - the underlying promise resolver
126
+ * @param {IMQRPCRequest} req - the originating request message
127
+ * @param {IMQClient} client - the client the call belongs to
128
+ * @return {(data: any, res: IMQRPCResponse) => void} - a hook-aware resolver
125
129
  */
126
130
  export declare function imqCallResolver(resolve: (data: any) => void, req: IMQRPCRequest, client: IMQClient): (data: any, res: IMQRPCResponse) => void;
127
131
  /**
128
- * Builds and returns call rejector, which supports after call optional hook
132
+ * Builds a call rejector that rejects the pending promise and then runs the
133
+ * optional after-call hook.
129
134
  *
130
- * @param {(err: any) => void} reject - source promise like rejector
131
- * @param {IMQRPCRequest} req - call request
132
- * @param {IMQClient} client - imq client
133
- * @return {(err: any) => void} - hook-supported reject
135
+ * @param {(err: any) => void} reject - the underlying promise rejector
136
+ * @param {IMQRPCRequest} req - the originating request message
137
+ * @param {IMQClient} client - the client the call belongs to
138
+ * @return {(err: any, res?: IMQRPCResponse) => void} - a hook-aware rejector
134
139
  */
135
140
  export declare function imqCallRejector(reject: (err: any) => void, req: IMQRPCRequest, client: IMQClient): (err: any, res?: IMQRPCResponse) => void;