@rayondigital/nest-dapr 0.9.21 → 0.9.22

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.
@@ -4,13 +4,19 @@ import Class from '@dapr/dapr/types/Class';
4
4
  import { DaprClientOptions } from '@dapr/dapr/types/DaprClientOptions';
5
5
  import { ModuleRef } from '@nestjs/core';
6
6
  import { DaprContextService } from '../dapr-context-service';
7
+ import { ActorRuntimeService } from './actor-runtime.service';
7
8
  export declare class ActorProxyBuilder<T> {
8
9
  moduleRef: ModuleRef;
9
10
  daprContextService: DaprContextService;
11
+ actorRuntimeService: ActorRuntimeService;
10
12
  actorClient: ActorClient;
11
13
  actorTypeClass: Class<T>;
14
+ allowInternalCalls: boolean;
12
15
  constructor(moduleRef: ModuleRef, actorTypeClass: Class<T>, daprClient: DaprClient);
13
16
  constructor(moduleRef: ModuleRef, actorTypeClass: Class<T>, host: string, port: string, communicationProtocol: CommunicationProtocolEnum, clientOptions: DaprClientOptions);
14
17
  build(actorId: ActorId, actorTypeName?: string): T;
18
+ private isInternalActor;
19
+ private callInternalActorMethod;
20
+ private callExternalActorMethod;
15
21
  private prepareBody;
16
22
  }
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ActorProxyBuilder = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const dapr_context_service_1 = require("../dapr-context-service");
15
+ const actor_runtime_service_1 = require("./actor-runtime.service");
15
16
  const client_cache_1 = require("./client-cache");
16
17
  class ActorProxyBuilder {
17
18
  constructor(moduleRef, actorTypeClass, ...args) {
@@ -19,6 +20,9 @@ class ActorProxyBuilder {
19
20
  this.daprContextService = moduleRef.get(dapr_context_service_1.DaprContextService, {
20
21
  strict: false,
21
22
  });
23
+ this.actorRuntimeService = moduleRef.get(actor_runtime_service_1.ActorRuntimeService, {
24
+ strict: false,
25
+ });
22
26
  this.actorTypeClass = actorTypeClass;
23
27
  if (args.length == 1) {
24
28
  const [daprClient] = args;
@@ -32,21 +36,42 @@ class ActorProxyBuilder {
32
36
  }
33
37
  build(actorId, actorTypeName) {
34
38
  const actorTypeClassName = actorTypeName !== null && actorTypeName !== void 0 ? actorTypeName : this.actorTypeClass.name;
35
- const actorClient = this.actorClient;
36
39
  const handler = {
37
40
  get: (_target, propKey, _receiver) => {
38
41
  return (...args) => __awaiter(this, void 0, void 0, function* () {
39
- const originalBody = args.length > 0 ? args : null;
40
- const body = yield this.prepareBody(this.daprContextService, args, originalBody);
41
- const correlationId = this.daprContextService.getCorrelationId(true);
42
- const response = yield actorClient.actor.invoke(actorTypeClassName, actorId, propKey, body, correlationId);
43
- return response;
42
+ if (this.isInternalActor(actorId)) {
43
+ return yield this.callInternalActorMethod(actorTypeClassName, actorId, propKey, args);
44
+ }
45
+ else {
46
+ return yield this.callExternalActorMethod(actorTypeClassName, actorId, propKey, args);
47
+ }
44
48
  });
45
49
  },
46
50
  };
47
51
  const proxy = new Proxy(this.actorTypeClass, handler);
48
52
  return proxy;
49
53
  }
54
+ isInternalActor(actorId) {
55
+ if (!this.allowInternalCalls)
56
+ return false;
57
+ return this.actorRuntimeService.hasActor(this.actorTypeClass, actorId.getId());
58
+ }
59
+ callInternalActorMethod(actorTypeClassName, actorId, methodName, args) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ console.log('calling internal actor method');
62
+ const actorManager = this.actorRuntimeService.getActorManager(actorTypeClassName);
63
+ const requestBody = JSON.stringify(args);
64
+ return yield actorManager.invoke(actorId, methodName, Buffer.from(requestBody));
65
+ });
66
+ }
67
+ callExternalActorMethod(actorTypeClassName, actorId, methodName, args) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const originalBody = args.length > 0 ? args : null;
70
+ const body = yield this.prepareBody(this.daprContextService, args, originalBody);
71
+ const correlationId = this.daprContextService.getCorrelationId(true);
72
+ return yield this.actorClient.actor.invoke(actorTypeClassName, actorId, methodName, body, correlationId);
73
+ });
74
+ }
50
75
  prepareBody(daprContextService, args, body) {
51
76
  return __awaiter(this, void 0, void 0, function* () {
52
77
  try {
@@ -66,9 +66,14 @@ let ActorRuntimeService = class ActorRuntimeService {
66
66
  }
67
67
  hasActor(actorType, actorId) {
68
68
  var _a;
69
- const typeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
70
- const actorTypeName = this.actorClient.getActorTypeName(typeName);
71
- return this.hasActorByTypeName(actorTypeName, actorId);
69
+ try {
70
+ const typeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
71
+ const actorTypeName = this.actorClient.getActorTypeName(typeName);
72
+ return this.hasActorByTypeName(actorTypeName, actorId);
73
+ }
74
+ catch (error) {
75
+ return false;
76
+ }
72
77
  }
73
78
  hasActorByTypeName(actorTypeName, actorId) {
74
79
  const manager = this.getActorManager(actorTypeName);
@@ -10,7 +10,9 @@ export declare class DaprActorClient {
10
10
  private prefix;
11
11
  private delimiter;
12
12
  private typeNamePrefix;
13
+ private allowInternalCalls;
13
14
  constructor(moduleRef: ModuleRef, daprClient: DaprClient);
15
+ setAllowInternalCalls(allowInternalCalls: boolean): void;
14
16
  setPrefix(prefix: string, delimiter?: string): void;
15
17
  setTypeNamePrefix(prefix: string): void;
16
18
  register<T>(actorTypeName: string, actorType: Type<T> | Function, daprClient?: DaprClient): void;
@@ -24,6 +24,10 @@ let DaprActorClient = class DaprActorClient {
24
24
  this.prefix = '';
25
25
  this.delimiter = '-';
26
26
  this.typeNamePrefix = '';
27
+ this.allowInternalCalls = process.env.DAPR_ACTOR_ALLOW_INTERNAL_CALLS === 'true';
28
+ }
29
+ setAllowInternalCalls(allowInternalCalls) {
30
+ this.allowInternalCalls = allowInternalCalls;
27
31
  }
28
32
  setPrefix(prefix, delimiter = '-') {
29
33
  this.prefix = prefix;
@@ -34,7 +38,9 @@ let DaprActorClient = class DaprActorClient {
34
38
  }
35
39
  register(actorTypeName, actorType, daprClient) {
36
40
  this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
37
- this.actorProxyBuilders.set(this.formatActorTypeName(actorTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
41
+ const proxyBuilder = new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient);
42
+ proxyBuilder.allowInternalCalls = this.allowInternalCalls;
43
+ this.actorProxyBuilders.set(this.formatActorTypeName(actorTypeName), proxyBuilder);
38
44
  }
39
45
  registerInterface(actorType, interfaceType, daprClient) {
40
46
  var _a, _b;
@@ -42,7 +48,9 @@ let DaprActorClient = class DaprActorClient {
42
48
  const actorTypeName = (_b = actorType.name) !== null && _b !== void 0 ? _b : actorType.constructor.name;
43
49
  this.interfaceToActorTypeNames.set(interfaceTypeName, actorTypeName);
44
50
  this.interfaces.set(this.formatActorTypeName(interfaceTypeName), actorType);
45
- this.actorProxyBuilders.set(this.formatActorTypeName(interfaceTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
51
+ const proxyBuilder = new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient);
52
+ proxyBuilder.allowInternalCalls = this.allowInternalCalls;
53
+ this.actorProxyBuilders.set(this.formatActorTypeName(interfaceTypeName), proxyBuilder);
46
54
  }
47
55
  getActorId(actorId) {
48
56
  var _a;
@@ -6,6 +6,7 @@ export declare class NestActorManager {
6
6
  setup(moduleRef: ModuleRef, options: DaprModuleActorOptions, onActivateFn?: (actorId: ActorId, instance: AbstractActor) => Promise<void>): void;
7
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 runInsideContextIfAvailable;
9
10
  private resolveDependencies;
10
11
  private static extractContext;
11
12
  }
@@ -89,24 +89,34 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
89
89
  ActorManager_1.default.prototype.callActorMethod = function (actorId, methodName, data) {
90
90
  return __awaiter(this, void 0, void 0, function* () {
91
91
  try {
92
- return yield clsService.run(() => __awaiter(this, void 0, void 0, function* () {
93
- var _a;
94
- contextService.setIdIfNotDefined();
95
- const context = NestActorManager_1.extractContext(data);
96
- if (context) {
97
- contextService.set(context);
98
- const correlationId = (_a = context[dapr_context_service_1.DAPR_CORRELATION_ID_KEY]) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)();
99
- if (correlationId) {
100
- contextService.setCorrelationId(correlationId);
101
- }
102
- }
92
+ if (clsService.isActive()) {
103
93
  if (invokeWrapperFn) {
104
94
  return yield invokeWrapperFn(actorId, methodName, data, originalCallActor.bind(this));
105
95
  }
106
96
  else {
107
97
  return yield originalCallActor.bind(this)(actorId, methodName, data);
108
98
  }
109
- }));
99
+ }
100
+ else {
101
+ return yield clsService.run(() => __awaiter(this, void 0, void 0, function* () {
102
+ var _a;
103
+ contextService.setIdIfNotDefined();
104
+ const context = NestActorManager_1.extractContext(data);
105
+ if (context) {
106
+ contextService.set(context);
107
+ const correlationId = (_a = context[dapr_context_service_1.DAPR_CORRELATION_ID_KEY]) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)();
108
+ if (correlationId) {
109
+ contextService.setCorrelationId(correlationId);
110
+ }
111
+ }
112
+ if (invokeWrapperFn) {
113
+ return yield invokeWrapperFn(actorId, methodName, data, originalCallActor.bind(this));
114
+ }
115
+ else {
116
+ return yield originalCallActor.bind(this)(actorId, methodName, data);
117
+ }
118
+ }));
119
+ }
110
120
  }
111
121
  catch (error) {
112
122
  common_1.Logger.error(`Error invoking actor method ${actorId}/${methodName}`);
@@ -116,6 +126,11 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
116
126
  });
117
127
  };
118
128
  }
129
+ runInsideContextIfAvailable() {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ return;
132
+ });
133
+ }
119
134
  resolveDependencies(moduleRef, instance) {
120
135
  return __awaiter(this, void 0, void 0, function* () {
121
136
  const type = instance.constructor;
@@ -146,26 +161,32 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
146
161
  });
147
162
  }
148
163
  static extractContext(data) {
149
- if (!data)
150
- return undefined;
151
- if (Array.isArray(data)) {
152
- const lastItem = data[data.length - 1];
153
- if (lastItem['$t'] === 'ctx') {
154
- data.pop();
155
- return lastItem;
164
+ try {
165
+ if (!data)
166
+ return undefined;
167
+ if (Array.isArray(data) && data.length > 0) {
168
+ const lastItem = data[data.length - 1];
169
+ if (lastItem['$t'] === 'ctx') {
170
+ data.pop();
171
+ return lastItem;
172
+ }
156
173
  }
174
+ if (data['$t'] === 'ctx') {
175
+ const context = Object.assign({}, data);
176
+ data = undefined;
177
+ return context;
178
+ }
179
+ if (data['$ctx']) {
180
+ const context = Object.assign({}, data['$ctx']);
181
+ data['$ctx'] = undefined;
182
+ return context;
183
+ }
184
+ return undefined;
157
185
  }
158
- if (data['$t'] === 'ctx') {
159
- const context = Object.assign({}, data);
160
- data = undefined;
161
- return context;
162
- }
163
- if (data['$ctx']) {
164
- const context = Object.assign({}, data['$ctx']);
165
- data['$ctx'] = undefined;
166
- return context;
186
+ catch (error) {
187
+ console.error(error);
188
+ return undefined;
167
189
  }
168
- return undefined;
169
190
  }
170
191
  };
171
192
  NestActorManager = NestActorManager_1 = __decorate([
@@ -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, _m, _o, _p;
53
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
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');
@@ -63,6 +63,14 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
63
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
64
  this.actorManager.setupReentrancy();
65
65
  }
66
+ if (this.options.actorOptions) {
67
+ this.daprActorClient.setAllowInternalCalls((_e = (_d = this.options.actorOptions) === null || _d === void 0 ? void 0 : _d.allowInternalCalls) !== null && _e !== void 0 ? _e : false);
68
+ this.daprActorClient.setPrefix((_g = (_f = this.options.actorOptions) === null || _f === void 0 ? void 0 : _f.prefix) !== null && _g !== void 0 ? _g : '', (_j = (_h = this.options.actorOptions) === null || _h === void 0 ? void 0 : _h.delimiter) !== null && _j !== void 0 ? _j : '-');
69
+ this.daprActorClient.setTypeNamePrefix((_l = (_k = this.options.actorOptions) === null || _k === void 0 ? void 0 : _k.typeNamePrefix) !== null && _l !== void 0 ? _l : '');
70
+ if ((_m = this.options.actorOptions) === null || _m === void 0 ? void 0 : _m.prefix) {
71
+ this.logger.log(`Actors will be prefixed with ${(_p = (_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.prefix) !== null && _p !== void 0 ? _p : ''} and delimited with ${(_r = (_q = this.options.actorOptions) === null || _q === void 0 ? void 0 : _q.delimiter) !== null && _r !== void 0 ? _r : '-'}`);
72
+ }
73
+ }
66
74
  yield this.daprServer.actor.init();
67
75
  this.loadDaprHandlers();
68
76
  if (this.options.serverPort === '0') {
@@ -76,13 +84,6 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
76
84
  if (resRegisteredActors.length > 0) {
77
85
  this.logger.log(`Registered Actors: ${resRegisteredActors.join(', ')}`);
78
86
  }
79
- if (this.options.actorOptions) {
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 : '-'}`);
84
- }
85
- }
86
87
  });
87
88
  }
88
89
  onApplicationShutdown() {
@@ -16,6 +16,7 @@ export interface DaprModuleActorOptions {
16
16
  prefix?: string;
17
17
  delimiter?: string;
18
18
  typeNamePrefix?: string;
19
+ allowInternalCalls?: boolean;
19
20
  }
20
21
  export declare enum DaprContextProvider {
21
22
  None = "none",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.21",
3
+ "version": "0.9.22",
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",