@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.
Files changed (63) hide show
  1. package/.github/workflows/build.yml +49 -5
  2. package/.oxfmtrc.json +9 -0
  3. package/.oxlintrc.json +37 -0
  4. package/README.md +68 -0
  5. package/package.json +21 -58
  6. package/scripts/strip-comment-coverage.mjs +464 -0
  7. package/src/IMQCache.d.ts +10 -9
  8. package/src/IMQCache.js +16 -22
  9. package/src/IMQClient.d.ts +21 -16
  10. package/src/IMQClient.js +407 -271
  11. package/src/IMQDelay.d.ts +14 -0
  12. package/src/IMQDelay.js +26 -5
  13. package/src/IMQLock.d.ts +17 -15
  14. package/src/IMQLock.js +36 -36
  15. package/src/IMQMetadata.d.ts +8 -0
  16. package/src/IMQMetadata.js +8 -0
  17. package/src/IMQRPCDescription.d.ts +2 -2
  18. package/src/IMQRPCDescription.js +2 -2
  19. package/src/IMQRPCError.d.ts +7 -7
  20. package/src/IMQRPCError.js +7 -8
  21. package/src/IMQRPCOptions.d.ts +27 -3
  22. package/src/IMQRPCOptions.js +17 -3
  23. package/src/IMQRPCRequest.d.ts +1 -1
  24. package/src/IMQRPCResponse.d.ts +1 -1
  25. package/src/IMQRequestContext.d.ts +25 -0
  26. package/src/IMQRequestContext.js +57 -0
  27. package/src/IMQService.d.ts +4 -4
  28. package/src/IMQService.js +303 -217
  29. package/src/cache/ICache.d.ts +53 -0
  30. package/src/cache/RedisCache.d.ts +28 -25
  31. package/src/cache/RedisCache.js +104 -61
  32. package/src/decorators/cache.d.ts +12 -0
  33. package/src/decorators/cache.js +56 -41
  34. package/src/decorators/classType.d.ts +43 -0
  35. package/src/decorators/classType.js +83 -0
  36. package/src/decorators/expose.d.ts +5 -31
  37. package/src/decorators/expose.js +118 -134
  38. package/src/decorators/index.d.ts +2 -0
  39. package/src/decorators/index.js +4 -0
  40. package/src/decorators/indexed.d.ts +23 -0
  41. package/src/decorators/indexed.js +22 -13
  42. package/src/decorators/lock.d.ts +10 -11
  43. package/src/decorators/lock.js +63 -56
  44. package/src/decorators/logged.d.ts +11 -9
  45. package/src/decorators/logged.js +47 -40
  46. package/src/decorators/metadata.d.ts +24 -0
  47. package/src/decorators/metadata.js +45 -0
  48. package/src/decorators/property.d.ts +7 -57
  49. package/src/decorators/property.js +112 -83
  50. package/src/decorators/remote.d.ts +7 -7
  51. package/src/decorators/remote.js +19 -13
  52. package/src/helpers/fs.d.ts +13 -13
  53. package/src/helpers/fs.js +19 -19
  54. package/src/helpers/os-uuid.d.ts +2 -1
  55. package/src/helpers/os-uuid.js +86 -3
  56. package/src/helpers/pid.d.ts +19 -9
  57. package/src/helpers/pid.js +33 -33
  58. package/src/helpers/signature.d.ts +9 -7
  59. package/src/helpers/signature.js +80 -10
  60. package/src/index.d.ts +1 -1
  61. package/src/index.js +1 -1
  62. package/tsconfig.json +18 -14
  63. package/typedoc.json +19 -0
@@ -24,13 +24,96 @@ exports.osUuid = osUuid;
24
24
  * purchase a proprietary commercial license. Please contact us at
25
25
  * <support@imqueue.com> to get commercial licensing options.
26
26
  */
27
- const { machineIdSync } = require('node-machine-id');
27
+ const node_child_process_1 = require("node:child_process");
28
+ const node_crypto_1 = require("node:crypto");
28
29
  /**
29
- * Returns machine UUID
30
+ * Returns the base path to the Windows registry query tool, accounting for
31
+ * 32-bit processes running on 64-bit Windows (which must go through the
32
+ * "sysnative" redirector to reach the real System32).
33
+ *
34
+ * @returns {string}
35
+ */
36
+ function winRegBase() {
37
+ const useSysnative = process.arch === 'ia32' &&
38
+ Object.prototype.hasOwnProperty.call(process.env, 'PROCESSOR_ARCHITEW6432');
39
+ return useSysnative
40
+ ? '%windir%\\sysnative\\cmd.exe /c %windir%\\System32'
41
+ : '%windir%\\System32';
42
+ }
43
+ /**
44
+ * Returns the platform-specific shell command that emits the raw machine
45
+ * identifier. Mirrors the commands used by the node-machine-id package.
46
+ *
47
+ * @returns {string}
48
+ */
49
+ function idCommand() {
50
+ switch (process.platform) {
51
+ case 'darwin':
52
+ return 'ioreg -rd1 -c IOPlatformExpertDevice';
53
+ case 'win32':
54
+ return (`${winRegBase()}\\REG.exe QUERY ` +
55
+ 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography ' +
56
+ '/v MachineGuid');
57
+ case 'linux':
58
+ return ('( cat /var/lib/dbus/machine-id /etc/machine-id ' +
59
+ '2> /dev/null || hostname ) | head -n 1 || :');
60
+ case 'freebsd':
61
+ return 'kenv -q smbios.system.uuid || sysctl -n kern.hostuuid';
62
+ default:
63
+ throw new Error(`Unsupported platform: ${process.platform}`);
64
+ }
65
+ }
66
+ /**
67
+ * Extracts the bare GUID from the raw command output for the current platform.
68
+ *
69
+ * @param {string} raw - unprocessed command output
70
+ * @returns {string}
71
+ */
72
+ function parseId(raw) {
73
+ switch (process.platform) {
74
+ case 'darwin':
75
+ return raw
76
+ .split('IOPlatformUUID')[1]
77
+ .split('\n')[0]
78
+ .replace(/=|\s+|"/gi, '')
79
+ .toLowerCase();
80
+ case 'win32':
81
+ return raw
82
+ .split('REG_SZ')[1]
83
+ .replace(/\r+|\n+|\s+/gi, '')
84
+ .toLowerCase();
85
+ default: // linux, freebsd
86
+ return raw.replace(/\r+|\n+|\s+/gi, '').toLowerCase();
87
+ }
88
+ }
89
+ /**
90
+ * Returns the OS machine identifier synchronously, replicating the approach
91
+ * used by the node-machine-id package: it runs the platform-specific command,
92
+ * parses out the GUID, and, unless the original id is requested, returns its
93
+ * sha256 hash.
94
+ *
95
+ * @param {boolean} [original] - when true returns the raw machine GUID,
96
+ * otherwise returns its sha256 hash
97
+ * @returns {string}
98
+ */
99
+ function machineIdSync(original) {
100
+ const id = parseId((0, node_child_process_1.execSync)(idCommand()).toString());
101
+ return original ? id : (0, node_crypto_1.createHash)('sha256').update(id).digest('hex');
102
+ }
103
+ // The machine id cannot change while the process is running, so it is
104
+ // resolved once and memoized: osUuid() is called from every client
105
+ // constructor, and shelling out each time would needlessly slow startup.
106
+ let cachedUuid;
107
+ /**
108
+ * Returns the machine UUID. The underlying platform command runs only once
109
+ * per process; subsequent calls return the memoized value.
30
110
  *
31
111
  * @returns {string}
32
112
  */
33
113
  function osUuid() {
34
- return machineIdSync({ original: true });
114
+ if (cachedUuid === undefined) {
115
+ cachedUuid = machineIdSync(true);
116
+ }
117
+ return cachedUuid;
35
118
  }
36
119
  //# sourceMappingURL=os-uuid.js.map
@@ -1,21 +1,31 @@
1
1
  import { ILogger } from '@imqueue/core';
2
+ /**
3
+ * OS signals that should trigger pid file cleanup on process termination.
4
+ */
2
5
  export declare const SIGNALS: string[];
6
+ /**
7
+ * Base temporary directory used by imq-rpc.
8
+ */
3
9
  export declare const IMQ_TMP_DIR: string;
10
+ /**
11
+ * Directory where imq-rpc stores its pid files.
12
+ */
4
13
  export declare const IMQ_PID_DIR: string;
5
14
  /**
6
- * Returns increment-based process identifier for a given name
15
+ * Returns an increment-based process identifier for the given service name,
16
+ * creating the corresponding pid file under the given directory.
7
17
  *
8
- * @param {string} name - name of a service to create pid file for
9
- * @param {string} path - directory to
10
- * @returns {number}
18
+ * @param {string} name - name of the service to create the pid file for
19
+ * @param {string} [path] - directory to store the pid file in
20
+ * @returns {number} - the allocated increment-based identifier
11
21
  */
12
22
  export declare function pid(name: string, path?: string): number;
13
23
  /**
14
- * Removes pid file for a given name and id
24
+ * Removes the pid file for the given service name and identifier.
15
25
  *
16
- * @param {string} name
17
- * @param {number} id
18
- * @param {ILogger} logger
19
- * @param {string} [path]
26
+ * @param {string} name - name of the service whose pid file to remove
27
+ * @param {number} id - increment-based identifier of the pid file
28
+ * @param {ILogger} logger - logger instance
29
+ * @param {string} [path] - directory the pid file is stored in
20
30
  */
21
31
  export declare function forgetPid(name: string, id: number, logger: ILogger, path?: string): void;
@@ -26,42 +26,42 @@ exports.forgetPid = forgetPid;
26
26
  * purchase a proprietary commercial license. Please contact us at
27
27
  * <support@imqueue.com> to get commercial licensing options.
28
28
  */
29
- const path = require("path");
30
- const fs = require("fs");
31
- exports.SIGNALS = [
32
- 'SIGTERM',
33
- 'SIGINT',
34
- 'SIGHUP',
35
- 'SIGQUIT',
36
- ];
37
- exports.IMQ_TMP_DIR = process.env.TMPDIR ||
38
- // istanbul ignore next
39
- '/tmp';
40
- exports.IMQ_PID_DIR = path.resolve(exports.IMQ_TMP_DIR, '.imq-rpc');
29
+ const node_path_1 = require("node:path");
30
+ const node_fs_1 = require("node:fs");
41
31
  /**
42
- * Returns increment-based process identifier for a given name
32
+ * OS signals that should trigger pid file cleanup on process termination.
33
+ */
34
+ exports.SIGNALS = ['SIGTERM', 'SIGINT', 'SIGHUP', 'SIGQUIT'];
35
+ /**
36
+ * Base temporary directory used by imq-rpc.
37
+ */
38
+ exports.IMQ_TMP_DIR = process.env.TMPDIR || '/tmp';
39
+ /**
40
+ * Directory where imq-rpc stores its pid files.
41
+ */
42
+ exports.IMQ_PID_DIR = (0, node_path_1.resolve)(exports.IMQ_TMP_DIR, '.imq-rpc');
43
+ /**
44
+ * Returns an increment-based process identifier for the given service name,
45
+ * creating the corresponding pid file under the given directory.
43
46
  *
44
- * @param {string} name - name of a service to create pid file for
45
- * @param {string} path - directory to
46
- * @returns {number}
47
+ * @param {string} name - name of the service to create the pid file for
48
+ * @param {string} [path] - directory to store the pid file in
49
+ * @returns {number} - the allocated increment-based identifier
47
50
  */
48
- function pid(name,
49
- // istanbul ignore next
50
- path = exports.IMQ_PID_DIR) {
51
+ function pid(name, path = exports.IMQ_PID_DIR) {
51
52
  const pidFile = `${path}/${name}`;
52
53
  const pidOpts = { encoding: 'utf8', flag: 'wx' };
53
- if (!fs.existsSync(path)) {
54
- fs.mkdirSync(path);
54
+ if (!(0, node_fs_1.existsSync)(path)) {
55
+ (0, node_fs_1.mkdirSync)(path);
55
56
  }
56
57
  let id = 0;
57
58
  let done = false;
58
59
  while (!done) {
59
60
  try {
60
- fs.writeFileSync(`${pidFile}-${id}.pid`, process.pid + '', pidOpts);
61
+ (0, node_fs_1.writeFileSync)(`${pidFile}-${id}.pid`, process.pid + '', pidOpts);
61
62
  done = true;
62
63
  }
63
64
  catch (err) {
64
- // istanbul ignore next
65
65
  if (err.code === 'EEXIST') {
66
66
  id++;
67
67
  }
@@ -73,19 +73,19 @@ path = exports.IMQ_PID_DIR) {
73
73
  return id;
74
74
  }
75
75
  /**
76
- * Removes pid file for a given name and id
76
+ * Removes the pid file for the given service name and identifier.
77
77
  *
78
- * @param {string} name
79
- * @param {number} id
80
- * @param {ILogger} logger
81
- * @param {string} [path]
78
+ * @param {string} name - name of the service whose pid file to remove
79
+ * @param {number} id - increment-based identifier of the pid file
80
+ * @param {ILogger} logger - logger instance
81
+ * @param {string} [path] - directory the pid file is stored in
82
82
  */
83
- function forgetPid(name, id, logger,
84
- // istanbul ignore next
85
- path = exports.IMQ_PID_DIR) {
83
+ function forgetPid(name, id, logger, path = exports.IMQ_PID_DIR) {
86
84
  try {
87
- fs.unlinkSync(`${path}/${name}-${id}.pid`);
85
+ (0, node_fs_1.unlinkSync)(`${path}/${name}-${id}.pid`);
86
+ }
87
+ catch {
88
+ /* ignore */
88
89
  }
89
- catch (err) { /* ignore */ }
90
90
  }
91
91
  //# sourceMappingURL=pid.js.map
@@ -1,10 +1,12 @@
1
1
  /**
2
- * Constructs and returns hash string for a given set of className, methodName
3
- * and arguments.
2
+ * Constructs and returns a hash string for the given set of className,
3
+ * methodName, and arguments. The hash is deterministic: argument objects that
4
+ * differ only in key insertion order produce the same signature, and circular
5
+ * arguments are handled without throwing.
4
6
  *
5
- * @param {string} className
6
- * @param {string | symbol} methodName
7
- * @param {any[]} args
8
- * @returns {string}
7
+ * @param {string} className - name of the class the method belongs to
8
+ * @param {string | symbol} methodName - name of the method being called
9
+ * @param {readonly unknown[]} args - arguments passed to the method
10
+ * @returns {string} - hexadecimal hash string
9
11
  */
10
- export declare function signature(className: string, methodName: string | symbol, args: any[]): string;
12
+ export declare function signature(className: string, methodName: string | symbol, args: readonly unknown[]): string;
@@ -24,19 +24,89 @@ exports.signature = signature;
24
24
  * purchase a proprietary commercial license. Please contact us at
25
25
  * <support@imqueue.com> to get commercial licensing options.
26
26
  */
27
- const farmhash_1 = require("farmhash");
27
+ const node_crypto_1 = require("node:crypto");
28
28
  /**
29
- * Constructs and returns hash string for a given set of className, methodName
30
- * and arguments.
29
+ * Serializes a value into a deterministic, JSON-compatible string. It mirrors
30
+ * `JSON.stringify` semantics (including `toJSON` support and how `undefined`,
31
+ * functions, and non-finite numbers are treated), with two deliberate
32
+ * differences that make it safe for hashing:
31
33
  *
32
- * @param {string} className
33
- * @param {string | symbol} methodName
34
- * @param {any[]} args
35
- * @returns {string}
34
+ * - object keys are sorted, so two objects that differ only in key insertion
35
+ * order serialize identically (for objects whose keys are already sorted,
36
+ * the output is byte-identical to `JSON.stringify`, which keeps previously
37
+ * generated cache/lock keys valid);
38
+ * - circular references serialize as the marker string `"[Circular]"` instead
39
+ * of throwing. Repeated non-circular references to the same object are
40
+ * serialized in full each time, exactly as `JSON.stringify` does.
41
+ *
42
+ * @param {unknown} value - value to serialize
43
+ * @param {object[]} ancestors - stack of objects on the current descent path,
44
+ * used for cycle detection
45
+ * @returns {string | undefined} - the serialized value, or `undefined` where
46
+ * JSON would omit it (`undefined`, functions, and symbols)
47
+ */
48
+ function serialize(value, ancestors) {
49
+ if (value === null) {
50
+ return 'null';
51
+ }
52
+ switch (typeof value) {
53
+ case 'string':
54
+ return JSON.stringify(value);
55
+ case 'number':
56
+ return isFinite(value) ? String(value) : 'null';
57
+ case 'boolean':
58
+ return value ? 'true' : 'false';
59
+ case 'bigint':
60
+ // JSON.stringify throws on bigint; serialize it as a string
61
+ // instead so hashing never fails on valid runtime input
62
+ return `"${String(value)}"`;
63
+ case 'undefined':
64
+ case 'function':
65
+ case 'symbol':
66
+ return undefined;
67
+ }
68
+ const obj = value;
69
+ if (typeof obj.toJSON === 'function') {
70
+ return serialize(obj.toJSON(), ancestors);
71
+ }
72
+ if (ancestors.includes(obj)) {
73
+ return '"[Circular]"';
74
+ }
75
+ ancestors.push(obj);
76
+ try {
77
+ if (Array.isArray(obj)) {
78
+ const items = obj.map(item => serialize(item, ancestors) ?? 'null');
79
+ return `[${items.join(',')}]`;
80
+ }
81
+ const parts = [];
82
+ for (const key of Object.keys(obj).sort()) {
83
+ const serialized = serialize(obj[key], ancestors);
84
+ if (serialized !== undefined) {
85
+ parts.push(`${JSON.stringify(key)}:${serialized}`);
86
+ }
87
+ }
88
+ return `{${parts.join(',')}}`;
89
+ }
90
+ finally {
91
+ ancestors.pop();
92
+ }
93
+ }
94
+ /**
95
+ * Constructs and returns a hash string for the given set of className,
96
+ * methodName, and arguments. The hash is deterministic: argument objects that
97
+ * differ only in key insertion order produce the same signature, and circular
98
+ * arguments are handled without throwing.
99
+ *
100
+ * @param {string} className - name of the class the method belongs to
101
+ * @param {string | symbol} methodName - name of the method being called
102
+ * @param {readonly unknown[]} args - arguments passed to the method
103
+ * @returns {string} - hexadecimal hash string
36
104
  */
37
105
  function signature(className, methodName, args) {
38
- const data = JSON.stringify([className, methodName, args]);
39
- const hashBigInt = (0, farmhash_1.fingerprint64)(data);
40
- return hashBigInt.toString(16);
106
+ const data = serialize([className, methodName, args], []);
107
+ // one-shot native hash truncated to 64 bits (16 hex chars) — same key
108
+ // space as the previous farmhash fingerprint64, but ~3-4x faster per
109
+ // call on typical payloads (no WASM boundary, no bigint conversion)
110
+ return (0, node_crypto_1.hash)('sha256', data, 'hex').slice(0, 16);
41
111
  }
42
112
  //# sourceMappingURL=signature.js.map
package/src/index.d.ts CHANGED
@@ -20,7 +20,6 @@
20
20
  * <support@imqueue.com> to get commercial licensing options.
21
21
  */
22
22
  export * from './decorators';
23
- export * from './helpers';
24
23
  export * from './cache';
25
24
  export * from './IMQDelay';
26
25
  export * from './IMQCache';
@@ -33,3 +32,4 @@ export * from './IMQLock';
33
32
  export * from './IMQService';
34
33
  export * from './IMQClient';
35
34
  export * from './IMQMetadata';
35
+ export * from './IMQRequestContext';
package/src/index.js CHANGED
@@ -36,7 +36,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  * <support@imqueue.com> to get commercial licensing options.
37
37
  */
38
38
  __exportStar(require("./decorators"), exports);
39
- __exportStar(require("./helpers"), exports);
40
39
  __exportStar(require("./cache"), exports);
41
40
  __exportStar(require("./IMQDelay"), exports);
42
41
  __exportStar(require("./IMQCache"), exports);
@@ -49,4 +48,5 @@ __exportStar(require("./IMQLock"), exports);
49
48
  __exportStar(require("./IMQService"), exports);
50
49
  __exportStar(require("./IMQClient"), exports);
51
50
  __exportStar(require("./IMQMetadata"), exports);
51
+ __exportStar(require("./IMQRequestContext"), exports);
52
52
  //# sourceMappingURL=index.js.map
package/tsconfig.json CHANGED
@@ -1,21 +1,25 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "module": "commonjs",
3
+ "target": "es2023",
4
+ "lib": ["es2023", "esnext.decorators"],
5
+ "moduleDetection": "force",
6
+
7
+ "module": "nodenext",
8
+ "moduleResolution": "nodenext",
9
+ "resolveJsonModule": true,
10
+ "esModuleInterop": true,
11
+
12
+ "isolatedModules": true,
13
+
4
14
  "declaration": true,
5
- "noImplicitAny": true,
6
- "strictNullChecks": true,
7
- "removeComments": false,
8
- "noUnusedLocals": false,
9
- "noUnusedParameters": false,
10
- "moduleResolution": "node",
11
15
  "sourceMap": true,
12
16
  "inlineSources": true,
13
- "experimentalDecorators": true,
14
- "emitDecoratorMetadata": true,
15
- "target": "es2017",
16
- "lib": [
17
- "dom",
18
- "es2017"
19
- ]
17
+ "removeComments": false,
18
+ "newLine": "lf",
19
+
20
+ "strict": true,
21
+ "strictPropertyInitialization": false,
22
+ "noImplicitOverride": true,
23
+ "noFallthroughCasesInSwitch": true
20
24
  }
21
25
  }
package/typedoc.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "name": "@imqueue/rpc",
4
+ "entryPoints": ["."],
5
+ "entryPointStrategy": "expand",
6
+ "exclude": [
7
+ "**/+(debug|test|node_modules|docs|coverage|benchmark|.nyc_output)/**/*",
8
+ "**/*.d.ts"
9
+ ],
10
+ "excludePrivate": true,
11
+ "excludeExternals": true,
12
+ "hideGenerator": true,
13
+ "externalSymbolLinkMappings": {
14
+ "@types/node": {
15
+ "EventEmitter.defaultMaxListeners": "https://nodejs.org/api/events.html#eventsdefaultmaxlisteners"
16
+ }
17
+ },
18
+ "out": "docs"
19
+ }