@rayondigital/nest-dapr 0.10.2 → 0.10.3

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.
@@ -7,14 +7,9 @@ export declare class DaprActorClient {
7
7
  private actorProxyBuilders;
8
8
  private interfaces;
9
9
  private interfaceToActorTypeNames;
10
- private prefix;
11
- private delimiter;
12
- private typeNamePrefix;
13
10
  private allowInternalCalls;
14
11
  constructor(moduleRef: ModuleRef, daprClient: DaprClient);
15
12
  setAllowInternalCalls(allowInternalCalls: boolean): void;
16
- setPrefix(prefix: string, delimiter?: string): void;
17
- setTypeNamePrefix(prefix: string): void;
18
13
  register<T>(actorTypeName: string, actorType: Type<T> | Function, daprClient?: DaprClient): void;
19
14
  registerInterface<T>(actorType: Type<T> | Function, interfaceType: Type<T> | Function, daprClient?: DaprClient): void;
20
15
  getActorId(actorId: string): ActorId;
@@ -21,21 +21,11 @@ let DaprActorClient = class DaprActorClient {
21
21
  this.actorProxyBuilders = new Map();
22
22
  this.interfaces = new Map();
23
23
  this.interfaceToActorTypeNames = new Map();
24
- this.prefix = '';
25
- this.delimiter = '-';
26
- this.typeNamePrefix = '';
27
24
  this.allowInternalCalls = process.env.DAPR_ACTOR_ALLOW_INTERNAL_CALLS === 'true';
28
25
  }
29
26
  setAllowInternalCalls(allowInternalCalls) {
30
27
  this.allowInternalCalls = allowInternalCalls;
31
28
  }
32
- setPrefix(prefix, delimiter = '-') {
33
- this.prefix = prefix;
34
- this.delimiter = delimiter;
35
- }
36
- setTypeNamePrefix(prefix) {
37
- this.typeNamePrefix = prefix;
38
- }
39
29
  register(actorTypeName, actorType, daprClient) {
40
30
  this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
41
31
  const proxyBuilder = new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient);
@@ -53,13 +43,9 @@ let DaprActorClient = class DaprActorClient {
53
43
  this.actorProxyBuilders.set(this.formatActorTypeName(interfaceTypeName), proxyBuilder);
54
44
  }
55
45
  getActorId(actorId) {
56
- var _a;
57
46
  if (!actorId) {
58
47
  throw new Error('Actor id must be provided');
59
48
  }
60
- if (this.prefix) {
61
- return new dapr_1.ActorId(`${this.prefix}${(_a = this.delimiter) !== null && _a !== void 0 ? _a : '-'}${actorId}`);
62
- }
63
49
  return new dapr_1.ActorId(actorId);
64
50
  }
65
51
  getActor(actorType, actorId) {
@@ -98,16 +84,7 @@ let DaprActorClient = class DaprActorClient {
98
84
  }
99
85
  getActorTypeName(typeName) {
100
86
  if (this.interfaceToActorTypeNames.has(typeName)) {
101
- const actorTypeName = this.interfaceToActorTypeNames.get(typeName);
102
- if (this.actorProxyBuilders.has(actorTypeName)) {
103
- return actorTypeName;
104
- }
105
- else {
106
- return `${this.typeNamePrefix}${actorTypeName}`;
107
- }
108
- }
109
- if (this.typeNamePrefix) {
110
- return `${this.typeNamePrefix}${typeName}`;
87
+ return this.interfaceToActorTypeNames.get(typeName);
111
88
  }
112
89
  return typeName;
113
90
  }
@@ -8,6 +8,8 @@ export declare class NestActorManager {
8
8
  setupCSLWrapper(options: DaprModuleOptions, contextService: DaprContextService, invokeWrapperFn?: (actorId: ActorId, methodName: string, data: any, method: (actorId: ActorId, methodName: string, data: any) => Promise<any>) => Promise<any>): void;
9
9
  private catchAndLogUnhandledExceptions;
10
10
  private patchToSupportSerializableError;
11
+ private patchFireTimer;
12
+ private patchFireReminder;
11
13
  private patchDeactivate;
12
14
  private resolveDependencies;
13
15
  private static extractContext;
@@ -37,14 +37,11 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
37
37
  const originalCreateActor = ActorManager_1.default.prototype.createActor;
38
38
  const resolveDependencies = this.resolveDependencies;
39
39
  ActorManager_1.default.prototype.createActor = function (actorId) {
40
- var _a, _b;
40
+ var _a;
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
42
  const instance = (yield originalCreateActor.bind(this)(actorId));
43
- if ((_a = options === null || options === void 0 ? void 0 : options.actorOptions) === null || _a === void 0 ? void 0 : _a.typeNamePrefix) {
44
- instance['actorType'] = `${options.typeNamePrefix}${instance.actorType}`;
45
- }
46
43
  if (isLoggingEnabled) {
47
- const actorTypeName = (_b = this.actorCls.name) !== null && _b !== void 0 ? _b : instance.constructor.name;
44
+ const actorTypeName = (_a = this.actorCls.name) !== null && _a !== void 0 ? _a : instance.constructor.name;
48
45
  common_1.Logger.verbose(`Activating actor ${actorId}`, actorTypeName);
49
46
  }
50
47
  try {
@@ -65,9 +62,11 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
65
62
  };
66
63
  this.patchDeactivate(options);
67
64
  this.patchToSupportSerializableError(options);
68
- const isErrorHandlerEnabled = (_c = options === null || options === void 0 ? void 0 : options.catchErrors) !== null && _c !== void 0 ? _c : false;
65
+ const isErrorHandlerEnabled = (_c = options === null || options === void 0 ? void 0 : options.catchErrors) !== null && _c !== void 0 ? _c : true;
69
66
  if (isErrorHandlerEnabled) {
70
67
  this.catchAndLogUnhandledExceptions();
68
+ this.patchFireTimer();
69
+ this.patchFireReminder();
71
70
  }
72
71
  }
73
72
  setupReentrancy(options) {
@@ -182,6 +181,38 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
182
181
  });
183
182
  };
184
183
  }
184
+ patchFireTimer() {
185
+ const originalFireTimer = ActorManager_1.default.prototype.fireTimer;
186
+ if (!originalFireTimer)
187
+ return;
188
+ ActorManager_1.default.prototype.fireTimer = function (actorId, timerName, requestBody) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ try {
191
+ return yield originalFireTimer.bind(this)(actorId, timerName, requestBody);
192
+ }
193
+ catch (error) {
194
+ common_1.Logger.error(`Error firing timer ${timerName} for actor ${actorId}`);
195
+ common_1.Logger.error(error);
196
+ }
197
+ });
198
+ };
199
+ }
200
+ patchFireReminder() {
201
+ const originalFireReminder = ActorManager_1.default.prototype.fireReminder;
202
+ if (!originalFireReminder)
203
+ return;
204
+ ActorManager_1.default.prototype.fireReminder = function (actorId, reminderName, requestBody) {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ try {
207
+ return yield originalFireReminder.bind(this)(actorId, reminderName, requestBody);
208
+ }
209
+ catch (error) {
210
+ common_1.Logger.error(`Error firing reminder ${reminderName} for actor ${actorId}`);
211
+ common_1.Logger.error(error);
212
+ }
213
+ });
214
+ };
215
+ }
185
216
  patchDeactivate(options) {
186
217
  var _a, _b;
187
218
  const isLoggingEnabled = (_b = (_a = options === null || options === void 0 ? void 0 : options.logging) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true;
@@ -25,9 +25,10 @@ export declare class DaprLoader implements OnApplicationBootstrap, OnApplication
25
25
  constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner, daprServer: DaprServer, daprMetadataAccessor: DaprMetadataAccessor, options: DaprModuleOptions, daprActorClient: DaprActorClient, moduleRef: ModuleRef, contextService: DaprContextService, pubSubClient: DaprPubSubClient, workflowClient: DaprWorkflowClient, actorManager: NestActorManager);
26
26
  onApplicationBootstrap(): Promise<void>;
27
27
  onApplicationShutdown(): Promise<void>;
28
- loadDaprHandlers(isActorsEnabled: boolean, isWorkflowEnabled: boolean): void;
28
+ loadDaprHandlers(isActorsEnabled: boolean, isWorkflowEnabled: boolean): Promise<void>;
29
29
  private subscribeToDaprPubSubEventIfListener;
30
30
  private subscribeToDaprBindingEventIfListener;
31
+ private registerHandlers;
31
32
  private registerActivity;
32
33
  private registerWorkflow;
33
34
  private registerActor;
@@ -20,15 +20,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
20
20
  step((generator = generator.apply(thisArg, _arguments || [])).next());
21
21
  });
22
22
  };
23
- var __importDefault = (this && this.__importDefault) || function (mod) {
24
- return (mod && mod.__esModule) ? mod : { "default": mod };
25
- };
26
23
  var DaprLoader_1;
27
24
  Object.defineProperty(exports, "__esModule", { value: true });
28
25
  exports.DaprLoader = void 0;
29
26
  const dapr_1 = require("@dapr/dapr");
30
- const ActorManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorManager"));
31
- const ActorRuntime_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorRuntime"));
32
27
  const common_1 = require("@nestjs/common");
33
28
  const core_1 = require("@nestjs/core");
34
29
  const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
@@ -56,45 +51,38 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
56
51
  this.logger = new common_1.Logger(DaprLoader_1.name);
57
52
  }
58
53
  onApplicationBootstrap() {
59
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
60
55
  return __awaiter(this, void 0, void 0, function* () {
61
56
  this.logger.log('Dapr initializing');
62
57
  const isEnabled = !this.options.disabled;
63
58
  const isActorsEnabled = (_b = (_a = this.options.actorOptions) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true;
64
59
  const isWorkflowEnabled = (_d = (_c = this.options.workflowOptions) === null || _c === void 0 ? void 0 : _c.enabled) !== null && _d !== void 0 ? _d : false;
60
+ if (isActorsEnabled && isEnabled) {
61
+ this.logger.log('Registering Dapr actors');
62
+ yield this.daprServer.actor.init();
63
+ }
65
64
  if (isWorkflowEnabled) {
66
65
  this.workflowRuntime = new dapr_1.WorkflowRuntime({
67
66
  daprHost: this.options.serverHost,
68
67
  daprPort: (_f = (_e = this.options.workflowOptions) === null || _e === void 0 ? void 0 : _e.daprPort) !== null && _f !== void 0 ? _f : '3501',
69
68
  });
70
69
  }
70
+ if ((_g = this.options.pubsubOptions) === null || _g === void 0 ? void 0 : _g.defaultName) {
71
+ this.pubSubClient.setDefaultName(this.options.pubsubOptions.defaultName);
72
+ }
73
+ yield this.loadDaprHandlers(isActorsEnabled, isWorkflowEnabled);
71
74
  if (isActorsEnabled) {
72
75
  this.actorManager.setup(this.moduleRef, this.options);
73
76
  if (this.options.contextProvider !== dapr_module_1.DaprContextProvider.None) {
74
77
  this.actorManager.setupCSLWrapper(this.options, this.contextService);
75
78
  }
76
- if ((_j = (_h = (_g = this.options.clientOptions) === null || _g === void 0 ? void 0 : _g.actor) === null || _h === void 0 ? void 0 : _h.reentrancy) === null || _j === void 0 ? void 0 : _j.enabled) {
79
+ if ((_k = (_j = (_h = this.options.clientOptions) === null || _h === void 0 ? void 0 : _h.actor) === null || _j === void 0 ? void 0 : _j.reentrancy) === null || _k === void 0 ? void 0 : _k.enabled) {
77
80
  this.actorManager.setupReentrancy(this.options);
78
81
  }
79
- }
80
- if ((_k = this.options.pubsubOptions) === null || _k === void 0 ? void 0 : _k.defaultName) {
81
- this.pubSubClient.setDefaultName(this.options.pubsubOptions.defaultName);
82
- }
83
- if (isActorsEnabled) {
84
82
  if (this.options.actorOptions) {
85
83
  this.daprActorClient.setAllowInternalCalls((_m = (_l = this.options.actorOptions) === null || _l === void 0 ? void 0 : _l.allowInternalCalls) !== null && _m !== void 0 ? _m : false);
86
- this.daprActorClient.setPrefix((_p = (_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.prefix) !== null && _p !== void 0 ? _p : '', (_r = (_q = this.options.actorOptions) === null || _q === void 0 ? void 0 : _q.delimiter) !== null && _r !== void 0 ? _r : '-');
87
- this.daprActorClient.setTypeNamePrefix((_t = (_s = this.options.actorOptions) === null || _s === void 0 ? void 0 : _s.typeNamePrefix) !== null && _t !== void 0 ? _t : '');
88
- if ((_u = this.options.actorOptions) === null || _u === void 0 ? void 0 : _u.prefix) {
89
- this.logger.log(`Actors will be prefixed with ${(_w = (_v = this.options.actorOptions) === null || _v === void 0 ? void 0 : _v.prefix) !== null && _w !== void 0 ? _w : ''} and delimited with ${(_y = (_x = this.options.actorOptions) === null || _x === void 0 ? void 0 : _x.delimiter) !== null && _y !== void 0 ? _y : '-'}`);
90
- }
91
- }
92
- if (isEnabled) {
93
- this.logger.log('Registering Dapr actors');
94
- yield this.daprServer.actor.init();
95
84
  }
96
85
  }
97
- this.loadDaprHandlers(isActorsEnabled, isWorkflowEnabled);
98
86
  if (isEnabled && this.options.serverPort !== '0') {
99
87
  this.logger.log('Starting Dapr server');
100
88
  if (this.options.catchErrors) {
@@ -111,12 +99,12 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
111
99
  yield this.daprServer.start();
112
100
  this.logger.log('Dapr server started');
113
101
  }
114
- if (isEnabled && this.options.workflowOptions.enabled) {
102
+ if (isEnabled && isWorkflowEnabled) {
115
103
  this.logger.log('Starting Dapr workflow runtime');
116
104
  yield this.workflowRuntime.start();
117
105
  yield this.workflowClient.start({
118
106
  daprHost: this.options.serverHost,
119
- daprPort: (_z = this.options.workflowOptions.daprPort) !== null && _z !== void 0 ? _z : '3501',
107
+ daprPort: (_o = this.options.workflowOptions.daprPort) !== null && _o !== void 0 ? _o : '3501',
120
108
  });
121
109
  }
122
110
  if (!isActorsEnabled || !isEnabled)
@@ -155,44 +143,34 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
155
143
  });
156
144
  }
157
145
  loadDaprHandlers(isActorsEnabled, isWorkflowEnabled) {
158
- const providers = this.discoveryService.getProviders();
159
- if (isActorsEnabled) {
160
- providers
161
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
162
- wrapper.metatype &&
163
- this.daprMetadataAccessor.getDaprActorMetadata(wrapper.metatype))
164
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
165
- yield this.registerActor(wrapper.metatype);
166
- }));
167
- }
168
- const controllers = this.discoveryService.getControllers();
169
- [...providers, ...controllers]
170
- .filter((wrapper) => wrapper.isDependencyTreeStatic())
171
- .filter((wrapper) => wrapper.instance)
172
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
173
- const { instance } = wrapper;
174
- const prototype = Object.getPrototypeOf(instance) || {};
175
- this.metadataScanner.scanFromPrototype(instance, prototype, (methodKey) => __awaiter(this, void 0, void 0, function* () {
176
- yield this.subscribeToDaprPubSubEventIfListener(instance, methodKey);
177
- yield this.subscribeToDaprBindingEventIfListener(instance, methodKey);
178
- }));
179
- }));
180
- if (isWorkflowEnabled) {
181
- providers
182
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
183
- wrapper.metatype &&
184
- this.daprMetadataAccessor.getDaprActivityMetadata(wrapper.metatype))
185
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
186
- yield this.registerActivity(wrapper.metatype);
187
- }));
188
- providers
189
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
190
- wrapper.metatype &&
191
- this.daprMetadataAccessor.getDaprWorkflowMetadata(wrapper.metatype))
192
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
193
- yield this.registerWorkflow(wrapper.metatype);
194
- }));
195
- }
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const providers = this.discoveryService.getProviders();
148
+ if (isActorsEnabled) {
149
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
150
+ wrapper.metatype &&
151
+ this.daprMetadataAccessor.getDaprActorMetadata(wrapper.metatype))) {
152
+ yield this.registerActor(instanceWrapper.metatype);
153
+ }
154
+ }
155
+ const controllers = this.discoveryService.getControllers();
156
+ for (const instanceWrapper of [...providers, ...controllers]
157
+ .filter((wrapper) => wrapper.isDependencyTreeStatic())
158
+ .filter((wrapper) => wrapper.instance)) {
159
+ yield this.registerHandlers(instanceWrapper);
160
+ }
161
+ if (isWorkflowEnabled) {
162
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
163
+ wrapper.metatype &&
164
+ this.daprMetadataAccessor.getDaprActivityMetadata(wrapper.metatype))) {
165
+ yield this.registerActivity(instanceWrapper.metatype);
166
+ }
167
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
168
+ wrapper.metatype &&
169
+ this.daprMetadataAccessor.getDaprWorkflowMetadata(wrapper.metatype))) {
170
+ yield this.registerWorkflow(instanceWrapper.metatype);
171
+ }
172
+ }
173
+ });
196
174
  }
197
175
  subscribeToDaprPubSubEventIfListener(instance, methodKey) {
198
176
  var _a, _b;
@@ -244,6 +222,16 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
244
222
  }));
245
223
  });
246
224
  }
225
+ registerHandlers(instanceWrapper) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const instance = instanceWrapper.instance;
228
+ const prototype = Object.getPrototypeOf(instance) || {};
229
+ this.metadataScanner.scanFromPrototype(instance, prototype, (methodKey) => __awaiter(this, void 0, void 0, function* () {
230
+ yield this.subscribeToDaprPubSubEventIfListener(instance, methodKey);
231
+ yield this.subscribeToDaprBindingEventIfListener(instance, methodKey);
232
+ }));
233
+ });
234
+ }
247
235
  registerActivity(activityType) {
248
236
  var _a, _b;
249
237
  return __awaiter(this, void 0, void 0, function* () {
@@ -275,29 +263,14 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
275
263
  });
276
264
  }
277
265
  registerActor(actorType) {
278
- var _a, _b, _c, _d, _e;
266
+ var _a, _b, _c, _d;
279
267
  return __awaiter(this, void 0, void 0, function* () {
280
268
  if (!actorType)
281
269
  return;
282
- let actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
283
270
  const daprActorMetadata = this.daprMetadataAccessor.getDaprActorMetadata(actorType);
271
+ const actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
284
272
  const interfaceTypeName = (_c = (_b = daprActorMetadata === null || daprActorMetadata === void 0 ? void 0 : daprActorMetadata.interfaceType) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : (_d = daprActorMetadata === null || daprActorMetadata === void 0 ? void 0 : daprActorMetadata.interfaceType) === null || _d === void 0 ? void 0 : _d.constructor.name;
285
- if ((_e = this.options.actorOptions) === null || _e === void 0 ? void 0 : _e.typeNamePrefix) {
286
- actorTypeName = this.options.actorOptions.typeNamePrefix + actorTypeName;
287
- try {
288
- const actorManager = ActorRuntime_1.default.getInstanceByDaprClient(this.daprServer.client);
289
- const managers = actorManager['actorManagers'];
290
- if (!managers.has(actorTypeName)) {
291
- managers.set(actorTypeName, new ActorManager_1.default(actorType, this.daprServer.client));
292
- }
293
- }
294
- catch (err) {
295
- yield this.daprServer.actor.registerActor(actorType);
296
- }
297
- }
298
- else {
299
- yield this.daprServer.actor.registerActor(actorType);
300
- }
273
+ yield this.daprServer.actor.registerActor(actorType);
301
274
  this.logger.log(`Registering Dapr Actor: ${actorTypeName} of type ${interfaceTypeName !== null && interfaceTypeName !== void 0 ? interfaceTypeName : 'unknown'}`);
302
275
  this.daprActorClient.register(actorTypeName, actorType, this.daprServer.client);
303
276
  if (daprActorMetadata.interfaceType) {
@@ -22,9 +22,6 @@ export interface DaprModuleLoggingOptions {
22
22
  }
23
23
  export interface DaprModuleActorOptions {
24
24
  enabled: boolean;
25
- prefix?: string;
26
- delimiter?: string;
27
- typeNamePrefix?: string;
28
25
  allowInternalCalls?: boolean;
29
26
  }
30
27
  export interface DaprModuleWorkflowOptions {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
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",
@@ -1 +0,0 @@
1
- export declare function fromJson(json: any): any;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fromJson = void 0;
4
- function fromJson(json) {
5
- try {
6
- if (typeof json === 'string') {
7
- const isJson = json.includes('{') || json.includes('[');
8
- if (isJson)
9
- return JSON.parse(json);
10
- }
11
- return json;
12
- }
13
- catch (error) {
14
- return json;
15
- }
16
- }
17
- exports.fromJson = fromJson;