@imqueue/rpc 2.0.26 → 2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "2.0.26",
3
+ "version": "2.1.0",
4
4
  "description": "RPC-like client-service implementation over messaging queue",
5
5
  "keywords": [
6
6
  "redis",
@@ -0,0 +1,25 @@
1
+ import { IMQMetadata } from './IMQMetadata';
2
+ import { IMQRPCRequest } from './IMQRPCRequest';
3
+ /**
4
+ * Runs the given function with `request` bound to the current async execution
5
+ * context, so any code reached from it (and its asynchronous continuations) can
6
+ * access the in-flight request's metadata via `currentMetadata()` without
7
+ * threading it through call signatures.
8
+ *
9
+ * The binding is scoped to the function: it is established for the duration of
10
+ * the call and automatically removed afterwards, which keeps concurrent
11
+ * requests isolated from one another.
12
+ *
13
+ * @param {IMQRPCRequest} request - the request to bind for this execution
14
+ * @param {() => T} fn - the work to run within the bound context
15
+ * @return {T}
16
+ */
17
+ export declare function runWithRequest<T>(request: IMQRPCRequest, fn: () => T): T;
18
+ /**
19
+ * Returns the metadata of the in-flight IMQ request for the current async
20
+ * execution, if any. Returns `undefined` outside of a `runWithRequest()` scope.
21
+ * The transport carries metadata as an opaque bag; callers interpret its fields.
22
+ *
23
+ * @return {IMQMetadata | undefined}
24
+ */
25
+ export declare function currentMetadata(): IMQMetadata | undefined;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runWithRequest = runWithRequest;
4
+ exports.currentMetadata = currentMetadata;
5
+ /*!
6
+ * IMQRequestContext implementation
7
+ *
8
+ * I'm Queue Software Project
9
+ * Copyright (C) 2025 imqueue.com <support@imqueue.com>
10
+ *
11
+ * This program is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU General Public License as published by
13
+ * the Free Software Foundation, either version 3 of the License, or
14
+ * (at your option) any later version.
15
+ *
16
+ * This program is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
23
+ *
24
+ * If you want to use this code in a closed source (commercial) project, you can
25
+ * purchase a proprietary commercial license. Please contact us at
26
+ * <support@imqueue.com> to get commercial licensing options.
27
+ */
28
+ const node_async_hooks_1 = require("node:async_hooks");
29
+ const storage = new node_async_hooks_1.AsyncLocalStorage();
30
+ /**
31
+ * Runs the given function with `request` bound to the current async execution
32
+ * context, so any code reached from it (and its asynchronous continuations) can
33
+ * access the in-flight request's metadata via `currentMetadata()` without
34
+ * threading it through call signatures.
35
+ *
36
+ * The binding is scoped to the function: it is established for the duration of
37
+ * the call and automatically removed afterwards, which keeps concurrent
38
+ * requests isolated from one another.
39
+ *
40
+ * @param {IMQRPCRequest} request - the request to bind for this execution
41
+ * @param {() => T} fn - the work to run within the bound context
42
+ * @return {T}
43
+ */
44
+ function runWithRequest(request, fn) {
45
+ return storage.run(request, fn);
46
+ }
47
+ /**
48
+ * Returns the metadata of the in-flight IMQ request for the current async
49
+ * execution, if any. Returns `undefined` outside of a `runWithRequest()` scope.
50
+ * The transport carries metadata as an opaque bag; callers interpret its fields.
51
+ *
52
+ * @return {IMQMetadata | undefined}
53
+ */
54
+ function currentMetadata() {
55
+ var _a;
56
+ return (_a = storage.getStore()) === null || _a === void 0 ? void 0 : _a.metadata;
57
+ }
58
+ //# sourceMappingURL=IMQRequestContext.js.map
@@ -60,6 +60,7 @@ export declare abstract class IMQService {
60
60
  * @return {Promise<string>}
61
61
  */
62
62
  private handleRequest;
63
+ private processRequest;
63
64
  /**
64
65
  * Initializes this instance of service and starts handling request
65
66
  * messages.
package/src/IMQService.js CHANGED
@@ -37,6 +37,7 @@ exports.send = send;
37
37
  const core_1 = require("@imqueue/core");
38
38
  const _1 = require(".");
39
39
  const os = require("os");
40
+ const IMQRequestContext_1 = require("./IMQRequestContext");
40
41
  const http = require("node:http");
41
42
  const cluster = require('cluster');
42
43
  class Description {
@@ -115,6 +116,9 @@ class IMQService {
115
116
  * @return {Promise<string>}
116
117
  */
117
118
  async handleRequest(request, id) {
119
+ return (0, IMQRequestContext_1.runWithRequest)(request, () => this.processRequest(request, id));
120
+ }
121
+ async processRequest(request, id) {
118
122
  const logger = this.options.logger || console;
119
123
  const method = request.method;
120
124
  const description = await this.describe();
@@ -38,8 +38,6 @@ export declare const DEFAULT_REDIS_CACHE_OPTIONS: {
38
38
  safeDeliveryTtl?: number;
39
39
  cluster?: import("@imqueue/core").IMessageQueueConnection[];
40
40
  clusterManagers?: import("@imqueue/core/src/ClusterManager").ClusterManager[];
41
- verbose?: boolean;
42
- verboseExtended?: boolean;
43
41
  id?: string | undefined;
44
42
  host?: string | undefined;
45
43
  port?: number | undefined;
package/src/index.d.ts CHANGED
@@ -33,3 +33,4 @@ export * from './IMQLock';
33
33
  export * from './IMQService';
34
34
  export * from './IMQClient';
35
35
  export * from './IMQMetadata';
36
+ export * from './IMQRequestContext';
package/src/index.js CHANGED
@@ -49,4 +49,5 @@ __exportStar(require("./IMQLock"), exports);
49
49
  __exportStar(require("./IMQService"), exports);
50
50
  __exportStar(require("./IMQClient"), exports);
51
51
  __exportStar(require("./IMQMetadata"), exports);
52
+ __exportStar(require("./IMQRequestContext"), exports);
52
53
  //# sourceMappingURL=index.js.map