@rayondigital/nest-dapr 0.9.15 → 0.9.17

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.
@@ -8,14 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.ActorProxyBuilder = void 0;
16
- const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClient"));
17
- const dapr_context_service_1 = require("../dapr-context-service");
18
13
  const common_1 = require("@nestjs/common");
14
+ const dapr_context_service_1 = require("../dapr-context-service");
15
+ const client_cache_1 = require("./client-cache");
19
16
  class ActorProxyBuilder {
20
17
  constructor(moduleRef, actorTypeClass, ...args) {
21
18
  this.moduleRef = moduleRef;
@@ -25,11 +22,12 @@ class ActorProxyBuilder {
25
22
  this.actorTypeClass = actorTypeClass;
26
23
  if (args.length == 1) {
27
24
  const [daprClient] = args;
28
- this.actorClient = new ActorClient_1.default(daprClient.options.daprHost, daprClient.options.daprPort, daprClient.options.communicationProtocol, daprClient.options);
25
+ this.actorClient = client_cache_1.DaprClientCache.getOrCreateActorClientFromClient(daprClient);
29
26
  }
30
27
  else {
31
28
  const [host, port, communicationProtocol, clientOptions] = args;
32
- this.actorClient = new ActorClient_1.default(host, port, communicationProtocol, clientOptions);
29
+ const options = Object.assign({ daprHost: host, daprPort: port, communicationProtocol: communicationProtocol }, clientOptions);
30
+ this.actorClient = client_cache_1.DaprClientCache.getOrCreateActorClientFromOptions(options);
33
31
  }
34
32
  }
35
33
  build(actorId, actorTypeName) {
@@ -0,0 +1,18 @@
1
+ import { DaprClient, DaprClientOptions } from '@dapr/dapr';
2
+ import ActorClient from '@dapr/dapr/actors/client/ActorClient/ActorClient';
3
+ import StateProvider from '@dapr/dapr/actors/runtime/StateProvider';
4
+ export declare class DaprClientCache {
5
+ private static clients;
6
+ private static actorClients;
7
+ private static stateProviders;
8
+ static getAllClients(): DaprClient[];
9
+ static getAllActorClients(): ActorClient[];
10
+ static getAllStateProviders(): StateProvider[];
11
+ static getClientByHost(host: string): DaprClient | undefined;
12
+ static getActorClientByHost(host: string): ActorClient | undefined;
13
+ static getStateProviderByHost(host: string): StateProvider | undefined;
14
+ static getOrCreateActorClientFromClient(daprClient: DaprClient): ActorClient;
15
+ static getOrCreateStateProviderFromOptions(options: DaprClientOptions): StateProvider;
16
+ static getOrCreateActorClientFromOptions(options: DaprClientOptions): ActorClient;
17
+ static getOrCreateClientFromOptions(options: DaprClientOptions): DaprClient;
18
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DaprClientCache = void 0;
13
+ const dapr_1 = require("@dapr/dapr");
14
+ const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClient"));
15
+ const StateProvider_1 = __importDefault(require("@dapr/dapr/actors/runtime/StateProvider"));
16
+ const common_1 = require("@nestjs/common");
17
+ let DaprClientCache = class DaprClientCache {
18
+ static getAllClients() {
19
+ return Array.from(this.clients.values());
20
+ }
21
+ static getAllActorClients() {
22
+ return Array.from(this.actorClients.values());
23
+ }
24
+ static getAllStateProviders() {
25
+ return Array.from(this.stateProviders.values());
26
+ }
27
+ static getClientByHost(host) {
28
+ return this.clients.get(host);
29
+ }
30
+ static getActorClientByHost(host) {
31
+ return this.actorClients.get(host);
32
+ }
33
+ static getStateProviderByHost(host) {
34
+ return this.stateProviders.get(host);
35
+ }
36
+ static getOrCreateActorClientFromClient(daprClient) {
37
+ if (this.actorClients.has(daprClient.options.daprHost)) {
38
+ return this.actorClients.get(daprClient.options.daprHost);
39
+ }
40
+ if (!this.clients.has(daprClient.options.daprHost)) {
41
+ this.clients.set(daprClient.options.daprHost, daprClient);
42
+ }
43
+ return this.getOrCreateActorClientFromOptions(daprClient.options);
44
+ }
45
+ static getOrCreateStateProviderFromOptions(options) {
46
+ if (this.stateProviders.has(options.daprHost)) {
47
+ return this.stateProviders.get(options.daprHost);
48
+ }
49
+ const actorClient = this.getOrCreateActorClientFromOptions(options);
50
+ const stateProvider = new StateProvider_1.default(actorClient);
51
+ this.stateProviders.set(options.daprHost, stateProvider);
52
+ return stateProvider;
53
+ }
54
+ static getOrCreateActorClientFromOptions(options) {
55
+ if (this.actorClients.has(options.daprHost)) {
56
+ return this.actorClients.get(options.daprHost);
57
+ }
58
+ const client = new ActorClient_1.default(options.daprHost, options.daprPort, options.communicationProtocol, options);
59
+ this.actorClients.set(options.daprHost, client);
60
+ return client;
61
+ }
62
+ static getOrCreateClientFromOptions(options) {
63
+ if (this.clients.has(options.daprHost)) {
64
+ return this.clients.get(options.daprHost);
65
+ }
66
+ const client = new dapr_1.DaprClient(options);
67
+ this.clients.set(options.daprHost, client);
68
+ return client;
69
+ }
70
+ };
71
+ DaprClientCache.clients = new Map();
72
+ DaprClientCache.actorClients = new Map();
73
+ DaprClientCache.stateProviders = new Map();
74
+ DaprClientCache = __decorate([
75
+ (0, common_1.Injectable)()
76
+ ], DaprClientCache);
77
+ exports.DaprClientCache = DaprClientCache;
@@ -4,7 +4,7 @@ import { ModuleRef } from '@nestjs/core';
4
4
  export declare class DaprActorClient {
5
5
  private readonly moduleRef;
6
6
  private readonly daprClient;
7
- private actorClients;
7
+ private actorProxyBuilders;
8
8
  private interfaces;
9
9
  private interfaceToActorTypeNames;
10
10
  private prefix;
@@ -21,5 +21,5 @@ export declare class DaprActorClient {
21
21
  getActorTypeName(typeName: string): string;
22
22
  contains(actorTypeName: string): boolean;
23
23
  private formatActorTypeName;
24
- private getActorClient;
24
+ private getActorClientProxyBuilder;
25
25
  }
@@ -18,7 +18,7 @@ let DaprActorClient = class DaprActorClient {
18
18
  constructor(moduleRef, daprClient) {
19
19
  this.moduleRef = moduleRef;
20
20
  this.daprClient = daprClient;
21
- this.actorClients = new Map();
21
+ this.actorProxyBuilders = new Map();
22
22
  this.interfaces = new Map();
23
23
  this.interfaceToActorTypeNames = new Map();
24
24
  this.prefix = '';
@@ -34,7 +34,7 @@ let DaprActorClient = class DaprActorClient {
34
34
  }
35
35
  register(actorTypeName, actorType, daprClient) {
36
36
  this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
37
- this.actorClients.set(this.formatActorTypeName(actorTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
37
+ this.actorProxyBuilders.set(this.formatActorTypeName(actorTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
38
38
  }
39
39
  registerInterface(actorType, interfaceType, daprClient) {
40
40
  var _a, _b;
@@ -42,7 +42,7 @@ let DaprActorClient = class DaprActorClient {
42
42
  const actorTypeName = (_b = actorType.name) !== null && _b !== void 0 ? _b : actorType.constructor.name;
43
43
  this.interfaceToActorTypeNames.set(interfaceTypeName, actorTypeName);
44
44
  this.interfaces.set(this.formatActorTypeName(interfaceTypeName), actorType);
45
- this.actorClients.set(this.formatActorTypeName(interfaceTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
45
+ this.actorProxyBuilders.set(this.formatActorTypeName(interfaceTypeName), new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
46
46
  }
47
47
  getActorId(actorId) {
48
48
  var _a;
@@ -62,7 +62,7 @@ let DaprActorClient = class DaprActorClient {
62
62
  throw new Error(`Actor ${actorTypeName} not found`);
63
63
  }
64
64
  const fullActorId = this.getActorId(actorId);
65
- const actorClient = this.getActorClient(actorTypeName);
65
+ const actorClient = this.getActorClientProxyBuilder(actorTypeName);
66
66
  return actorClient.build(fullActorId, actorTypeName);
67
67
  }
68
68
  getActorByTypeName(actorTypeName, actorId) {
@@ -76,13 +76,13 @@ let DaprActorClient = class DaprActorClient {
76
76
  throw new Error(`Actor ${actorTypeName} not found`);
77
77
  }
78
78
  const fullActorId = this.getActorId(actorId);
79
- const actorClient = this.getActorClient(actorTypeName);
79
+ const actorClient = this.getActorClientProxyBuilder(actorTypeName);
80
80
  return actorClient.build(fullActorId, actorTypeName);
81
81
  }
82
82
  getActorTypeName(typeName) {
83
83
  if (this.interfaceToActorTypeNames.has(typeName)) {
84
84
  const actorTypeName = this.interfaceToActorTypeNames.get(typeName);
85
- if (this.actorClients.has(actorTypeName)) {
85
+ if (this.actorProxyBuilders.has(actorTypeName)) {
86
86
  return actorTypeName;
87
87
  }
88
88
  else {
@@ -95,18 +95,17 @@ let DaprActorClient = class DaprActorClient {
95
95
  return typeName;
96
96
  }
97
97
  contains(actorTypeName) {
98
- return this.actorClients.has(this.formatActorTypeName(actorTypeName));
98
+ return this.actorProxyBuilders.has(this.formatActorTypeName(actorTypeName));
99
99
  }
100
100
  formatActorTypeName(actorTypeName) {
101
101
  return actorTypeName.toLowerCase();
102
102
  }
103
- getActorClient(actorTypeName) {
104
- return this.actorClients.get(this.formatActorTypeName(actorTypeName));
103
+ getActorClientProxyBuilder(actorTypeName) {
104
+ return this.actorProxyBuilders.get(this.formatActorTypeName(actorTypeName));
105
105
  }
106
106
  };
107
107
  DaprActorClient = __decorate([
108
108
  (0, common_1.Injectable)(),
109
- __metadata("design:paramtypes", [core_1.ModuleRef,
110
- dapr_1.DaprClient])
109
+ __metadata("design:paramtypes", [core_1.ModuleRef, dapr_1.DaprClient])
111
110
  ], DaprActorClient);
112
111
  exports.DaprActorClient = DaprActorClient;
@@ -4,7 +4,6 @@ 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
- setupRemindersAndTimers(): void;
8
7
  setupReentrancy(): void;
9
8
  setupCSLWrapper(contextService: DaprContextService, invokeWrapperFn?: (actorId: ActorId, methodName: string, data: any, method: (actorId: ActorId, methodName: string, data: any) => Promise<any>) => Promise<any>): void;
10
9
  private resolveDependencies;
@@ -23,8 +23,6 @@ exports.NestActorManager = void 0;
23
23
  const crypto_1 = require("crypto");
24
24
  const ActorClientHTTP_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClientHTTP"));
25
25
  const ActorManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorManager"));
26
- const ActorReminderData_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorReminderData"));
27
- const ActorTimerData_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorTimerData"));
28
26
  const common_1 = require("@nestjs/common");
29
27
  const instance_wrapper_1 = require("@nestjs/core/injector/instance-wrapper");
30
28
  const dapr_context_service_1 = require("../dapr-context-service");
@@ -51,40 +49,18 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
51
49
  return instance;
52
50
  });
53
51
  };
54
- }
55
- setupRemindersAndTimers() {
56
- const originalFireReminder = ActorManager_1.default.prototype.fireReminder;
57
- const originalFireTimer = ActorManager_1.default.prototype.fireTimer;
58
- ActorManager_1.default.prototype.fireReminder = function (actorId, reminderName, requestBody) {
59
- var _a;
52
+ const originalDeactivateActor = ActorManager_1.default.prototype.deactivateActor;
53
+ ActorManager_1.default.prototype.deactivateActor = function (actorId) {
60
54
  return __awaiter(this, void 0, void 0, function* () {
61
55
  try {
62
- const requestBodyDeserialized = this.serializer.deserialize(requestBody || Buffer.from(''));
63
- const reminderData = ActorReminderData_1.default.fromObject(reminderName, requestBodyDeserialized);
64
- const reminderState = reminderData.state;
65
- let methodName = 'receiveReminder';
66
- if (reminderState !== undefined && typeof reminderState === 'object') {
67
- methodName = (_a = reminderState === null || reminderState === void 0 ? void 0 : reminderState.callback) !== null && _a !== void 0 ? _a : reminderState.method;
56
+ if (!this.actors.has(actorId.getId())) {
57
+ return;
68
58
  }
69
- return yield this.callActorMethod(actorId, methodName !== null && methodName !== void 0 ? methodName : 'receiveReminder', [reminderState, reminderName]);
59
+ yield originalDeactivateActor.bind(this)(actorId);
70
60
  }
71
61
  catch (error) {
72
- common_1.Logger.error(`Error firing reminder ${actorId}/${reminderName}`);
73
- throw error;
74
- }
75
- });
76
- };
77
- ActorManager_1.default.prototype.fireTimer = function (actorId, timerName, requestBody) {
78
- var _a;
79
- return __awaiter(this, void 0, void 0, function* () {
80
- try {
81
- const requestBodyDeserialized = this.serializer.deserialize(requestBody || Buffer.from(''));
82
- const timerData = ActorTimerData_1.default.fromObject(timerName, requestBodyDeserialized);
83
- return yield this.callActorMethod(actorId, (_a = timerData.callback) !== null && _a !== void 0 ? _a : 'handleTimer', [timerData.state, timerName]);
84
- }
85
- catch (error) {
86
- common_1.Logger.error(`Error firing timer ${actorId}/${timerName}`);
87
- throw error;
62
+ common_1.Logger.error(`Error deactivating actor ${actorId}`);
63
+ common_1.Logger.error(error);
88
64
  }
89
65
  });
90
66
  };
@@ -13,5 +13,5 @@ export declare class StatefulActor extends AbstractActor {
13
13
  getAllState(): Promise<void>;
14
14
  setAllStateFromProperties(): Promise<void>;
15
15
  getState<T>(stateName: string, defaultValue?: T): Promise<T | undefined>;
16
- private createStatePropertyInstance;
16
+ private createInstance;
17
17
  }
@@ -59,12 +59,18 @@ class StatefulActor extends dapr_1.AbstractActor {
59
59
  this[property.key] = property.defaultValue;
60
60
  }
61
61
  else {
62
- this[property.key] = this.createStatePropertyInstance(property);
62
+ this[property.key] = this.createInstance(property.type);
63
63
  }
64
64
  }
65
65
  else {
66
66
  if (property.serializable) {
67
- const instance = this.createStatePropertyInstance(property);
67
+ let instance;
68
+ if (typeof property.defaultValue === 'function') {
69
+ instance = property.defaultValue();
70
+ }
71
+ else {
72
+ instance = this.createInstance(property.type);
73
+ }
68
74
  this[property.key] = (_a = instance.fromJSON(rawValue)) !== null && _a !== void 0 ? _a : instance;
69
75
  }
70
76
  else {
@@ -104,18 +110,9 @@ class StatefulActor extends dapr_1.AbstractActor {
104
110
  }
105
111
  });
106
112
  }
107
- createStatePropertyInstance(property) {
113
+ createInstance(type) {
108
114
  try {
109
- if (property.defaultValue !== undefined) {
110
- if (property.defaultValue instanceof Function || typeof property.defaultValue === 'function') {
111
- return property.defaultValue();
112
- }
113
- return property.defaultValue;
114
- }
115
- if (property.type || property.serializable) {
116
- const type = property.type;
117
- return new type();
118
- }
115
+ return new type();
119
116
  }
120
117
  catch (e) {
121
118
  return undefined;
@@ -3,10 +3,10 @@ export interface StateProperty {
3
3
  key: string | symbol;
4
4
  name: string;
5
5
  type: Function | Type<any>;
6
- defaultValue?: (() => any) | Function | Type<any>;
6
+ defaultValue?: any;
7
7
  serializable?: boolean;
8
8
  }
9
9
  export declare function State(options?: {
10
- defaultValue?: (() => any) | Function | Type<any>;
10
+ defaultValue?: any;
11
11
  name?: string;
12
12
  }): PropertyDecorator;
@@ -57,7 +57,6 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
57
57
  return;
58
58
  }
59
59
  this.actorManager.setup(this.moduleRef, this.options.actorOptions);
60
- this.actorManager.setupRemindersAndTimers();
61
60
  if (this.options.contextProvider !== dapr_module_1.DaprContextProvider.None) {
62
61
  this.actorManager.setupCSLWrapper(this.contextService);
63
62
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { DaprClientCache } from './actors/client-cache';
1
2
  import { DaprActorClient } from './actors/dapr-actor-client.service';
2
3
  import { StatefulActorOf } from './actors/stateful-actor-of';
3
4
  import { IState, StatefulActor } from './actors/stateful.actor';
@@ -10,4 +11,4 @@ import { DaprMetadataAccessor } from './dapr-metadata.accessor';
10
11
  import { DaprPubSub, DaprPubSubMetadata } from './dapr-pubsub.decorator';
11
12
  import { DaprLoader } from './dapr.loader';
12
13
  import { DaprModule } from './dapr.module';
13
- export { DAPR_BINDING_METADATA, DAPR_PUBSUB_METADATA, DAPR_ACTOR_METADATA, DAPR_ACTOR_STATE_METADATA, DaprMetadataAccessor, DaprBindingMetadata, DaprBinding, DaprPubSubMetadata, DaprPubSub, DaprActorMetadata, State, DaprActor, DaprLoader, DaprModule, DaprActorClient, DaprContextService, StatefulActor, StatefulActorOf, IState, };
14
+ export { DAPR_BINDING_METADATA, DAPR_PUBSUB_METADATA, DAPR_ACTOR_METADATA, DAPR_ACTOR_STATE_METADATA, DaprMetadataAccessor, DaprBindingMetadata, DaprBinding, DaprPubSubMetadata, DaprPubSub, DaprActorMetadata, State, DaprActor, DaprLoader, DaprModule, DaprActorClient, DaprContextService, DaprClientCache, StatefulActor, StatefulActorOf, IState, };
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StatefulActorOf = exports.StatefulActor = exports.DaprContextService = exports.DaprActorClient = exports.DaprModule = exports.DaprLoader = exports.DaprActor = exports.State = exports.DaprPubSub = exports.DaprBinding = exports.DaprMetadataAccessor = exports.DAPR_ACTOR_STATE_METADATA = exports.DAPR_ACTOR_METADATA = exports.DAPR_PUBSUB_METADATA = exports.DAPR_BINDING_METADATA = void 0;
3
+ exports.StatefulActorOf = exports.StatefulActor = exports.DaprClientCache = exports.DaprContextService = exports.DaprActorClient = exports.DaprModule = exports.DaprLoader = exports.DaprActor = exports.State = exports.DaprPubSub = exports.DaprBinding = exports.DaprMetadataAccessor = exports.DAPR_ACTOR_STATE_METADATA = exports.DAPR_ACTOR_METADATA = exports.DAPR_PUBSUB_METADATA = exports.DAPR_BINDING_METADATA = void 0;
4
+ const client_cache_1 = require("./actors/client-cache");
5
+ Object.defineProperty(exports, "DaprClientCache", { enumerable: true, get: function () { return client_cache_1.DaprClientCache; } });
4
6
  const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
5
7
  Object.defineProperty(exports, "DaprActorClient", { enumerable: true, get: function () { return dapr_actor_client_service_1.DaprActorClient; } });
6
8
  const stateful_actor_of_1 = require("./actors/stateful-actor-of");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.15",
3
+ "version": "0.9.17",
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",