@rayondigital/nest-dapr 0.9.9 → 0.9.11

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.
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ActorProxyBuilder = void 0;
16
16
  const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClient"));
17
17
  const dapr_context_service_1 = require("../dapr-context-service");
18
+ const common_1 = require("@nestjs/common");
18
19
  class ActorProxyBuilder {
19
20
  constructor(moduleRef, actorTypeClass, ...args) {
20
21
  this.moduleRef = moduleRef;
@@ -39,8 +40,9 @@ class ActorProxyBuilder {
39
40
  return (...args) => __awaiter(this, void 0, void 0, function* () {
40
41
  const originalBody = args.length > 0 ? args : null;
41
42
  const body = yield this.prepareBody(this.daprContextService, args, originalBody);
42
- const res = yield actorClient.actor.invoke(actorTypeClassName, actorId, propKey, body);
43
- return res;
43
+ const correlationId = this.daprContextService.getCorrelationId(true);
44
+ const response = yield actorClient.actor.invoke(actorTypeClassName, actorId, propKey, body, correlationId);
45
+ return response;
44
46
  });
45
47
  },
46
48
  };
@@ -72,6 +74,7 @@ class ActorProxyBuilder {
72
74
  return body;
73
75
  }
74
76
  catch (error) {
77
+ common_1.Logger.error(error);
75
78
  return body;
76
79
  }
77
80
  });
@@ -4,10 +4,10 @@ import { DaprContextService } from '../dapr-context-service';
4
4
  import { DaprModuleActorOptions } from '../dapr.module';
5
5
  export declare class NestActorManager {
6
6
  setup(moduleRef: ModuleRef, options: DaprModuleActorOptions, onActivateFn?: (actorId: ActorId, instance: AbstractActor) => Promise<void>): void;
7
- private resolveDependencies;
7
+ setupReentrancy(): void;
8
8
  setupCSLWrapper(contextService: DaprContextService, invokeWrapperFn?: (actorId: ActorId, methodName: string, data: any, method: (actorId: ActorId, methodName: string, data: any) => Promise<any>) => Promise<any>): void;
9
+ private resolveDependencies;
9
10
  private static extractContext;
10
- private static removeContext;
11
11
  }
12
12
  export interface ActorMethodInvocation {
13
13
  actorId: ActorId;
@@ -21,10 +21,11 @@ var NestActorManager_1;
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.NestActorManager = void 0;
23
23
  const crypto_1 = require("crypto");
24
+ const ActorClientHTTP_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClientHTTP"));
24
25
  const ActorManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorManager"));
25
26
  const common_1 = require("@nestjs/common");
26
27
  const instance_wrapper_1 = require("@nestjs/core/injector/instance-wrapper");
27
- const nestjs_cls_1 = require("nestjs-cls");
28
+ const dapr_context_service_1 = require("../dapr-context-service");
28
29
  let NestActorManager = NestActorManager_1 = class NestActorManager {
29
30
  setup(moduleRef, options, onActivateFn) {
30
31
  const originalCreateActor = ActorManager_1.default.prototype.createActor;
@@ -49,34 +50,20 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
49
50
  });
50
51
  };
51
52
  }
52
- resolveDependencies(moduleRef, instance) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const type = instance.constructor;
55
- try {
56
- const injector = moduleRef['injector'];
57
- const wrapper = new instance_wrapper_1.InstanceWrapper({
58
- name: type && type.name,
59
- metatype: type,
60
- isResolved: false,
61
- scope: common_1.Scope.TRANSIENT,
62
- durable: true,
53
+ setupReentrancy() {
54
+ ActorClientHTTP_1.default.prototype.invoke = function (actorType, actorId, methodName, body, reentrancyId) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const urlSafeId = encodeURIComponent(actorId.getId());
57
+ const result = yield this.client.execute(`/actors/${actorType}/${urlSafeId}/method/${methodName}`, {
58
+ method: 'POST',
59
+ body,
60
+ headers: {
61
+ 'Dapr-Reentrancy-Id': reentrancyId,
62
+ },
63
63
  });
64
- const properties = injector.reflectProperties(wrapper.metatype);
65
- for (const item of properties) {
66
- if ('type' in item && item.type) {
67
- const propertyType = item.type;
68
- const resolved = yield moduleRef.get(propertyType, { strict: false });
69
- if (resolved) {
70
- instance[item.key] = resolved;
71
- }
72
- }
73
- }
74
- }
75
- catch (error) {
76
- console.error(error);
77
- throw error;
78
- }
79
- });
64
+ return result;
65
+ });
66
+ };
80
67
  }
81
68
  setupCSLWrapper(contextService, invokeWrapperFn) {
82
69
  const clsService = contextService.getService();
@@ -88,11 +75,15 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
88
75
  return __awaiter(this, void 0, void 0, function* () {
89
76
  try {
90
77
  return yield clsService.run(() => __awaiter(this, void 0, void 0, function* () {
91
- clsService.setIfUndefined(nestjs_cls_1.CLS_ID, (0, crypto_1.randomUUID)());
78
+ var _a;
79
+ contextService.setIdIfNotDefined();
92
80
  const context = NestActorManager_1.extractContext(data);
93
81
  if (context) {
94
82
  contextService.set(context);
95
- data = NestActorManager_1.removeContext(data);
83
+ const correlationId = (_a = context[dapr_context_service_1.DAPR_CORRELATION_ID_KEY]) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)();
84
+ if (correlationId) {
85
+ contextService.setCorrelationId(correlationId);
86
+ }
96
87
  }
97
88
  if (invokeWrapperFn) {
98
89
  return yield invokeWrapperFn(actorId, methodName, data, originalCallActor.bind(this));
@@ -110,36 +101,56 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
110
101
  });
111
102
  };
112
103
  }
113
- static extractContext(data) {
114
- if (!data)
115
- return undefined;
116
- if (Array.isArray(data)) {
117
- const lastItem = data[data.length - 1];
118
- if (lastItem['$t'] === 'ctx') {
119
- return lastItem;
104
+ resolveDependencies(moduleRef, instance) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const type = instance.constructor;
107
+ try {
108
+ const injector = moduleRef['injector'];
109
+ const wrapper = new instance_wrapper_1.InstanceWrapper({
110
+ name: type && type.name,
111
+ metatype: type,
112
+ isResolved: false,
113
+ scope: common_1.Scope.TRANSIENT,
114
+ durable: true,
115
+ });
116
+ const properties = injector.reflectProperties(wrapper.metatype);
117
+ for (const item of properties) {
118
+ if ('type' in item && item.type) {
119
+ const propertyType = item.type;
120
+ const resolved = yield moduleRef.get(propertyType, { strict: false });
121
+ if (resolved) {
122
+ instance[item.key] = resolved;
123
+ }
124
+ }
125
+ }
120
126
  }
121
- }
122
- if (data['$t'] === 'ctx') {
123
- return data;
124
- }
125
- return data['$ctx'];
127
+ catch (error) {
128
+ console.error(error);
129
+ throw error;
130
+ }
131
+ });
126
132
  }
127
- static removeContext(data) {
133
+ static extractContext(data) {
128
134
  if (!data)
129
135
  return undefined;
130
136
  if (Array.isArray(data)) {
131
137
  const lastItem = data[data.length - 1];
132
138
  if (lastItem['$t'] === 'ctx') {
133
- return data.slice(0, data.length - 1);
139
+ data.pop();
140
+ return lastItem;
134
141
  }
135
142
  }
136
143
  if (data['$t'] === 'ctx') {
137
- return undefined;
144
+ const context = Object.assign({}, data);
145
+ data = undefined;
146
+ return context;
138
147
  }
139
148
  if (data['$ctx']) {
149
+ const context = Object.assign({}, data['$ctx']);
140
150
  data['$ctx'] = undefined;
151
+ return context;
141
152
  }
142
- return data;
153
+ return undefined;
143
154
  }
144
155
  };
145
156
  NestActorManager = NestActorManager_1 = __decorate([
@@ -1,10 +1,17 @@
1
1
  import { ClsService } from 'nestjs-cls';
2
2
  export declare const DAPR_CONTEXT_KEY = "context";
3
+ export declare const DAPR_CORRELATION_ID_KEY = "correlationId";
3
4
  export declare class DaprContextService {
4
5
  private readonly cls;
5
6
  constructor(cls: ClsService);
6
7
  getService(): ClsService;
7
- setByKey<T>(key: string, value: T): void;
8
+ getId(): string;
9
+ setIdIfNotDefined(id?: string): void;
10
+ getCorrelationId(createIfNotDefined?: boolean): any;
11
+ setCorrelationId(value?: string): void;
12
+ setCorrelationIdIfNotDefined(value?: string): string;
13
+ setByKey<T>(key: string | symbol, value: T): void;
14
+ setByKeyIfNotDefined<T>(key: string | symbol, value: T): any;
8
15
  set<T>(value: T): void;
9
16
  getByKey<T>(key: string): T | undefined;
10
17
  get<T>(): T | undefined;
@@ -9,10 +9,12 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DaprContextService = exports.DAPR_CONTEXT_KEY = void 0;
12
+ exports.DaprContextService = exports.DAPR_CORRELATION_ID_KEY = exports.DAPR_CONTEXT_KEY = void 0;
13
+ const crypto_1 = require("crypto");
13
14
  const common_1 = require("@nestjs/common");
14
15
  const nestjs_cls_1 = require("nestjs-cls");
15
16
  exports.DAPR_CONTEXT_KEY = 'context';
17
+ exports.DAPR_CORRELATION_ID_KEY = 'correlationId';
16
18
  let DaprContextService = class DaprContextService {
17
19
  constructor(cls) {
18
20
  this.cls = cls;
@@ -21,9 +23,48 @@ let DaprContextService = class DaprContextService {
21
23
  var _a;
22
24
  return (_a = this.cls) !== null && _a !== void 0 ? _a : nestjs_cls_1.ClsServiceManager.getClsService();
23
25
  }
26
+ getId() {
27
+ try {
28
+ return this.getService().getId();
29
+ }
30
+ catch (error) {
31
+ return undefined;
32
+ }
33
+ }
34
+ setIdIfNotDefined(id) {
35
+ this.setByKeyIfNotDefined(nestjs_cls_1.CLS_ID, id !== null && id !== void 0 ? id : (0, crypto_1.randomUUID)().toString());
36
+ }
37
+ getCorrelationId(createIfNotDefined = false) {
38
+ const byKey = this.getByKey(exports.DAPR_CORRELATION_ID_KEY);
39
+ if (byKey)
40
+ return byKey;
41
+ const context = this.get();
42
+ if (context)
43
+ return context[exports.DAPR_CORRELATION_ID_KEY];
44
+ if (createIfNotDefined)
45
+ return this.setCorrelationIdIfNotDefined();
46
+ return undefined;
47
+ }
48
+ setCorrelationId(value) {
49
+ this.setByKey(exports.DAPR_CORRELATION_ID_KEY, value !== null && value !== void 0 ? value : (0, crypto_1.randomUUID)().toString());
50
+ }
51
+ setCorrelationIdIfNotDefined(value) {
52
+ if (!value)
53
+ value = (0, crypto_1.randomUUID)().toString();
54
+ this.setByKeyIfNotDefined(exports.DAPR_CORRELATION_ID_KEY, value);
55
+ return value;
56
+ }
24
57
  setByKey(key, value) {
25
58
  this.getService().set(key, value);
26
59
  }
60
+ setByKeyIfNotDefined(key, value) {
61
+ try {
62
+ this.getService().setIfUndefined(key, value);
63
+ }
64
+ catch (error) {
65
+ return undefined;
66
+ }
67
+ }
27
68
  set(value) {
28
69
  this.setByKey(exports.DAPR_CONTEXT_KEY, value);
29
70
  }
@@ -38,7 +79,11 @@ let DaprContextService = class DaprContextService {
38
79
  }
39
80
  get() {
40
81
  try {
41
- return this.getByKey(exports.DAPR_CONTEXT_KEY);
82
+ const contextObject = this.getByKey(exports.DAPR_CONTEXT_KEY);
83
+ if (!contextObject[exports.DAPR_CORRELATION_ID_KEY]) {
84
+ contextObject[exports.DAPR_CORRELATION_ID_KEY] = this.getByKey(exports.DAPR_CORRELATION_ID_KEY);
85
+ }
86
+ return contextObject;
42
87
  }
43
88
  catch (error) {
44
89
  return undefined;
@@ -50,7 +50,7 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
50
50
  this.logger = new common_1.Logger(DaprLoader_1.name);
51
51
  }
52
52
  onApplicationBootstrap() {
53
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
53
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
54
54
  return __awaiter(this, void 0, void 0, function* () {
55
55
  if (this.options.disabled) {
56
56
  this.logger.log('Dapr server is disabled');
@@ -60,6 +60,9 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
60
60
  if (this.options.contextProvider !== dapr_module_1.DaprContextProvider.None) {
61
61
  this.actorManager.setupCSLWrapper(this.contextService);
62
62
  }
63
+ if ((_c = (_b = (_a = this.options.clientOptions) === null || _a === void 0 ? void 0 : _a.actor) === null || _b === void 0 ? void 0 : _b.reentrancy) === null || _c === void 0 ? void 0 : _c.enabled) {
64
+ this.actorManager.setupReentrancy();
65
+ }
63
66
  yield this.daprServer.actor.init();
64
67
  this.loadDaprHandlers();
65
68
  if (this.options.serverPort === '0') {
@@ -74,10 +77,10 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
74
77
  this.logger.log(`Registered Actors: ${resRegisteredActors.join(', ')}`);
75
78
  }
76
79
  if (this.options.actorOptions) {
77
- this.daprActorClient.setPrefix((_b = (_a = this.options.actorOptions) === null || _a === void 0 ? void 0 : _a.prefix) !== null && _b !== void 0 ? _b : '', (_d = (_c = this.options.actorOptions) === null || _c === void 0 ? void 0 : _c.delimiter) !== null && _d !== void 0 ? _d : '-');
78
- this.daprActorClient.setTypeNamePrefix((_f = (_e = this.options.actorOptions) === null || _e === void 0 ? void 0 : _e.typeNamePrefix) !== null && _f !== void 0 ? _f : '');
79
- if ((_g = this.options.actorOptions) === null || _g === void 0 ? void 0 : _g.prefix) {
80
- this.logger.log(`Actors will be prefixed with ${(_j = (_h = this.options.actorOptions) === null || _h === void 0 ? void 0 : _h.prefix) !== null && _j !== void 0 ? _j : ''} and delimited with ${(_l = (_k = this.options.actorOptions) === null || _k === void 0 ? void 0 : _k.delimiter) !== null && _l !== void 0 ? _l : '-'}`);
80
+ this.daprActorClient.setPrefix((_e = (_d = this.options.actorOptions) === null || _d === void 0 ? void 0 : _d.prefix) !== null && _e !== void 0 ? _e : '', (_g = (_f = this.options.actorOptions) === null || _f === void 0 ? void 0 : _f.delimiter) !== null && _g !== void 0 ? _g : '-');
81
+ this.daprActorClient.setTypeNamePrefix((_j = (_h = this.options.actorOptions) === null || _h === void 0 ? void 0 : _h.typeNamePrefix) !== null && _j !== void 0 ? _j : '');
82
+ if ((_k = this.options.actorOptions) === null || _k === void 0 ? void 0 : _k.prefix) {
83
+ this.logger.log(`Actors will be prefixed with ${(_m = (_l = this.options.actorOptions) === null || _l === void 0 ? void 0 : _l.prefix) !== null && _m !== void 0 ? _m : ''} and delimited with ${(_p = (_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.delimiter) !== null && _p !== void 0 ? _p : '-'}`);
81
84
  }
82
85
  }
83
86
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.9",
3
+ "version": "0.9.11",
4
4
  "description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",