@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.
- package/README.md +68 -0
- package/package.json +26 -61
- package/src/IMQCache.d.ts +10 -9
- package/src/IMQCache.js +16 -22
- package/src/IMQClient.d.ts +21 -16
- package/src/IMQClient.js +466 -270
- package/src/IMQDelay.d.ts +14 -0
- package/src/IMQDelay.js +26 -5
- package/src/IMQLock.d.ts +17 -15
- package/src/IMQLock.js +36 -36
- package/src/IMQMetadata.d.ts +8 -0
- package/src/IMQMetadata.js +8 -0
- package/src/IMQRPCDescription.d.ts +2 -2
- package/src/IMQRPCDescription.js +2 -2
- package/src/IMQRPCError.d.ts +7 -7
- package/src/IMQRPCError.js +7 -8
- package/src/IMQRPCOptions.d.ts +27 -3
- package/src/IMQRPCOptions.js +17 -3
- package/src/IMQRPCRequest.d.ts +1 -1
- package/src/IMQRPCResponse.d.ts +1 -1
- package/src/IMQRequestContext.js +1 -2
- package/src/IMQService.d.ts +3 -4
- package/src/IMQService.js +302 -220
- package/src/cache/ICache.d.ts +53 -0
- package/src/cache/RedisCache.d.ts +36 -31
- 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 +147 -129
- package/src/decorators/index.d.ts +2 -0
- package/src/decorators/index.js +4 -0
- package/src/decorators/indexed.d.ts +23 -0
- package/src/decorators/indexed.js +22 -13
- package/src/decorators/lock.d.ts +10 -11
- package/src/decorators/lock.js +63 -56
- package/src/decorators/logged.d.ts +11 -9
- package/src/decorators/logged.js +47 -40
- package/src/decorators/metadata.d.ts +24 -0
- package/src/decorators/metadata.js +45 -0
- package/src/decorators/property.d.ts +7 -57
- package/src/decorators/property.js +112 -83
- package/src/decorators/remote.d.ts +7 -7
- package/src/decorators/remote.js +19 -13
- package/src/helpers/fs.d.ts +13 -13
- package/src/helpers/fs.js +19 -19
- package/src/helpers/os-uuid.d.ts +2 -1
- package/src/helpers/os-uuid.js +86 -3
- package/src/helpers/pid.d.ts +19 -9
- package/src/helpers/pid.js +33 -33
- package/src/helpers/signature.d.ts +9 -7
- package/src/helpers/signature.js +80 -10
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -1
- package/tsconfig.json +18 -14
- package/.github/workflows/build.yml +0 -28
- package/debug.d.ts +0 -1
package/src/decorators/remote.js
CHANGED
|
@@ -25,22 +25,28 @@ exports.remote = remote;
|
|
|
25
25
|
* <support@imqueue.com> to get commercial licensing options.
|
|
26
26
|
*/
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Creates a `@remote()` method decorator for client classes. The decorated
|
|
29
|
+
* method has the remote method name appended to its arguments and is then
|
|
30
|
+
* forwarded to `remoteCall()`. The returned decorator is dual-mode: it works
|
|
31
|
+
* both as a standard (TC39) and as a legacy (experimentalDecorators) method
|
|
32
|
+
* decorator.
|
|
29
33
|
*
|
|
30
|
-
* @return {
|
|
31
|
-
* target: any,
|
|
32
|
-
* methodName: (string),
|
|
33
|
-
* descriptor: TypedPropertyDescriptor<(...args: any[]) => any>
|
|
34
|
-
* ) => void}
|
|
34
|
+
* @return {Function} - a dual-mode method decorator
|
|
35
35
|
*/
|
|
36
36
|
function remote() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
const wrap = (original, methodName) => function (...args) {
|
|
38
|
+
args.push(methodName);
|
|
39
|
+
// istanbul ignore next
|
|
40
|
+
return original ? original.apply(this, args) : undefined;
|
|
41
|
+
};
|
|
42
|
+
// Dual-mode: standard (TC39) invocations pass a context object with a
|
|
43
|
+
// `kind` property; legacy ones pass (target, propertyKey, descriptor).
|
|
44
|
+
return function (target, context, descriptor) {
|
|
45
|
+
if (context && typeof context === 'object' && 'kind' in context) {
|
|
46
|
+
return wrap(target, String(context.name));
|
|
47
|
+
}
|
|
48
|
+
descriptor.value = wrap(descriptor.value, String(context));
|
|
49
|
+
return descriptor;
|
|
44
50
|
};
|
|
45
51
|
}
|
|
46
52
|
//# sourceMappingURL=remote.js.map
|
package/src/helpers/fs.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Checks
|
|
2
|
+
* Checks whether a file exists at the given path.
|
|
3
3
|
*
|
|
4
|
-
* @param {string} path
|
|
5
|
-
* @return {Promise<
|
|
4
|
+
* @param {string} path - path to the file to check
|
|
5
|
+
* @return {Promise<boolean>} - true if the file exists, false otherwise
|
|
6
6
|
*/
|
|
7
|
-
export declare function fileExists(path: string): Promise<
|
|
7
|
+
export declare function fileExists(path: string): Promise<boolean>;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Asynchronously creates a directory at the given path.
|
|
10
10
|
*
|
|
11
|
-
* @param {string} path
|
|
12
|
-
* @return {Promise<
|
|
11
|
+
* @param {string} path - path of the directory to create
|
|
12
|
+
* @return {Promise<void>}
|
|
13
13
|
*/
|
|
14
|
-
export declare function mkdir(path: string): Promise<
|
|
14
|
+
export declare function mkdir(path: string): Promise<void>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* Asynchronously writes the given content to a file at the given path.
|
|
17
17
|
*
|
|
18
|
-
* @param {string} path
|
|
19
|
-
* @param {string} content
|
|
20
|
-
* @return {Promise<
|
|
18
|
+
* @param {string} path - path of the file to write
|
|
19
|
+
* @param {string} content - content to write to the file
|
|
20
|
+
* @return {Promise<void>}
|
|
21
21
|
*/
|
|
22
|
-
export declare function writeFile(path: string, content: string): Promise<
|
|
22
|
+
export declare function writeFile(path: string, content: string): Promise<void>;
|
package/src/helpers/fs.js
CHANGED
|
@@ -26,42 +26,42 @@ exports.writeFile = writeFile;
|
|
|
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
|
|
29
|
+
const node_fs_1 = require("node:fs");
|
|
30
30
|
/**
|
|
31
|
-
* Checks
|
|
31
|
+
* Checks whether a file exists at the given path.
|
|
32
32
|
*
|
|
33
|
-
* @param {string} path
|
|
34
|
-
* @return {Promise<
|
|
33
|
+
* @param {string} path - path to the file to check
|
|
34
|
+
* @return {Promise<boolean>} - true if the file exists, false otherwise
|
|
35
35
|
*/
|
|
36
36
|
function fileExists(path) {
|
|
37
|
-
return new Promise(resolve =>
|
|
37
|
+
return new Promise(resolve => (0, node_fs_1.access)(path, err => resolve(!err)));
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
*
|
|
40
|
+
* Asynchronously creates a directory at the given path.
|
|
41
41
|
*
|
|
42
|
-
* @param {string} path
|
|
43
|
-
* @return {Promise<
|
|
42
|
+
* @param {string} path - path of the directory to create
|
|
43
|
+
* @return {Promise<void>}
|
|
44
44
|
*/
|
|
45
45
|
function mkdir(path) {
|
|
46
|
-
return new Promise((resolve, reject) =>
|
|
46
|
+
return new Promise((resolve, reject) => (0, node_fs_1.mkdir)(path, generalCallback.bind(null, resolve, reject)));
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
49
|
+
* Asynchronously writes the given content to a file at the given path.
|
|
50
50
|
*
|
|
51
|
-
* @param {string} path
|
|
52
|
-
* @param {string} content
|
|
53
|
-
* @return {Promise<
|
|
51
|
+
* @param {string} path - path of the file to write
|
|
52
|
+
* @param {string} content - content to write to the file
|
|
53
|
+
* @return {Promise<void>}
|
|
54
54
|
*/
|
|
55
55
|
function writeFile(path, content) {
|
|
56
|
-
return new Promise((resolve, reject) =>
|
|
56
|
+
return new Promise((resolve, reject) => (0, node_fs_1.writeFile)(path, content, { encoding: 'utf8' }, generalCallback.bind(null, resolve, reject)));
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
* Constructs
|
|
60
|
-
*
|
|
59
|
+
* Constructs a callback that settles a promise using the given resolve and
|
|
60
|
+
* reject functions, rejecting when an error is provided.
|
|
61
61
|
*
|
|
62
|
-
* @param {() => void} resolve
|
|
63
|
-
* @param {(
|
|
64
|
-
* @param {Error} err
|
|
62
|
+
* @param {(value: void | PromiseLike<void>) => void} resolve - promise resolver
|
|
63
|
+
* @param {(reason?: unknown) => void} reject - promise rejecter
|
|
64
|
+
* @param {Error | null} [err] - error passed by the underlying fs operation
|
|
65
65
|
*/
|
|
66
66
|
function generalCallback(resolve, reject, err) {
|
|
67
67
|
if (err) {
|
package/src/helpers/os-uuid.d.ts
CHANGED
package/src/helpers/os-uuid.js
CHANGED
|
@@ -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
|
|
27
|
+
const node_child_process_1 = require("node:child_process");
|
|
28
|
+
const node_crypto_1 = require("node:crypto");
|
|
28
29
|
/**
|
|
29
|
-
* Returns
|
|
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
|
-
|
|
114
|
+
if (cachedUuid === undefined) {
|
|
115
|
+
cachedUuid = machineIdSync(true);
|
|
116
|
+
}
|
|
117
|
+
return cachedUuid;
|
|
35
118
|
}
|
|
36
119
|
//# sourceMappingURL=os-uuid.js.map
|
package/src/helpers/pid.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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;
|
package/src/helpers/pid.js
CHANGED
|
@@ -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
|
|
30
|
-
const
|
|
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
|
-
*
|
|
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
|
|
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 (!
|
|
54
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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 {
|
|
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:
|
|
12
|
+
export declare function signature(className: string, methodName: string | symbol, args: readonly unknown[]): string;
|
package/src/helpers/signature.js
CHANGED
|
@@ -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
|
|
27
|
+
const node_crypto_1 = require("node:crypto");
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
* and
|
|
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
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
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 =
|
|
39
|
-
|
|
40
|
-
|
|
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
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);
|
package/tsconfig.json
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"
|
|
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
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
"removeComments": false,
|
|
18
|
+
"newLine": "lf",
|
|
19
|
+
|
|
20
|
+
"strict": true,
|
|
21
|
+
"strictPropertyInitialization": false,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
"noFallthroughCasesInSwitch": true
|
|
20
24
|
}
|
|
21
25
|
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
name: Node.js CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
pull_request:
|
|
6
|
-
|
|
7
|
-
jobs:
|
|
8
|
-
build:
|
|
9
|
-
runs-on: ubuntu-latest
|
|
10
|
-
|
|
11
|
-
strategy:
|
|
12
|
-
matrix:
|
|
13
|
-
node-version: [lts/*]
|
|
14
|
-
|
|
15
|
-
steps:
|
|
16
|
-
- name: Checkout repository
|
|
17
|
-
uses: actions/checkout@v4
|
|
18
|
-
|
|
19
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
20
|
-
uses: actions/setup-node@v4
|
|
21
|
-
with:
|
|
22
|
-
node-version: ${{ matrix.node-version }}
|
|
23
|
-
|
|
24
|
-
- name: Install dependencies
|
|
25
|
-
run: npm install
|
|
26
|
-
|
|
27
|
-
- name: Run tests
|
|
28
|
-
run: npm test
|
package/debug.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|