@rayondigital/nest-dapr 0.9.17 → 0.9.19

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.
@@ -0,0 +1,36 @@
1
+ import ActorId from '@dapr/dapr/actors/ActorId';
2
+ import ActorStateManager from '@dapr/dapr/actors/runtime/ActorStateManager';
3
+ import StateProvider from '@dapr/dapr/actors/runtime/StateProvider';
4
+ import DaprClient from '@dapr/dapr/implementation/Client/DaprClient';
5
+ import { Temporal } from '@js-temporal/polyfill';
6
+ export declare abstract class AbstractActor {
7
+ private readonly stateManager;
8
+ private readonly id;
9
+ private readonly daprClient;
10
+ private readonly actorClient;
11
+ private readonly daprStateProvider;
12
+ private readonly actorType;
13
+ private readonly daprLogger;
14
+ constructor(daprClient: DaprClient, id: ActorId);
15
+ registerActorReminder<_Type>(reminderName: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise<void>;
16
+ unregisterActorReminder(reminderName: string): Promise<void>;
17
+ registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise<void>;
18
+ unregisterActorTimer(timerName: string): Promise<void>;
19
+ onActivateInternal(): Promise<void>;
20
+ onDeactivateInternal(): Promise<void>;
21
+ onActorMethodPreInternal(): Promise<void>;
22
+ onActorMethodPostInternal(): Promise<void>;
23
+ resetStateInternal(): Promise<void>;
24
+ saveStateInternal(): Promise<void>;
25
+ onActivate(): Promise<void>;
26
+ onDeactivate(): Promise<void>;
27
+ onActorMethodPre(): Promise<void>;
28
+ onActorMethodPost(): Promise<void>;
29
+ receiveReminder(_data: string): Promise<void>;
30
+ getDaprClient(): DaprClient;
31
+ getStateProvider(): StateProvider;
32
+ getStateManager<T>(): ActorStateManager<T>;
33
+ getActorId(): ActorId;
34
+ getActorType(): any;
35
+ getId(): string;
36
+ }
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AbstractActor = void 0;
16
+ const ActorStateManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorStateManager"));
17
+ const Logger_1 = require("@dapr/dapr/logger/Logger");
18
+ const polyfill_1 = require("@js-temporal/polyfill");
19
+ const client_cache_1 = require("./client-cache");
20
+ class AbstractActor {
21
+ constructor(daprClient, id) {
22
+ this.daprClient = daprClient;
23
+ this.actorClient = client_cache_1.DaprClientCache.getOrCreateActorClientFromOptions(daprClient.options);
24
+ this.daprLogger = new Logger_1.Logger('Actors', 'AbstractActor', daprClient.options.logger);
25
+ this.id = id;
26
+ this.stateManager = new ActorStateManager_1.default(this);
27
+ this.daprStateProvider = client_cache_1.DaprClientCache.getOrCreateStateProviderFromOptions(daprClient.options);
28
+ this.actorType = this.constructor.name;
29
+ }
30
+ registerActorReminder(reminderName, dueTime, period, ttl, state) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ yield this.actorClient.actor.registerActorReminder(this.actorType, this.id, reminderName, {
33
+ period: period !== null && period !== void 0 ? period : polyfill_1.Temporal.Duration.from({ hours: 1 }),
34
+ dueTime,
35
+ ttl,
36
+ data: state,
37
+ });
38
+ });
39
+ }
40
+ unregisterActorReminder(reminderName) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ yield this.actorClient.actor.unregisterActorReminder(this.actorType, this.id, reminderName);
43
+ });
44
+ }
45
+ registerActorTimer(timerName, callback, dueTime, period, ttl, state) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return yield this.actorClient.actor.registerActorTimer(this.actorType, this.id, timerName, {
48
+ period: period !== null && period !== void 0 ? period : polyfill_1.Temporal.Duration.from({ hours: 12 }),
49
+ dueTime,
50
+ ttl,
51
+ data: state,
52
+ callback,
53
+ });
54
+ });
55
+ }
56
+ unregisterActorTimer(timerName) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ yield this.actorClient.actor.unregisterActorTimer(this.actorType, this.id, timerName);
59
+ });
60
+ }
61
+ onActivateInternal() {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ yield this.resetStateInternal();
64
+ yield this.onActivate();
65
+ yield this.saveStateInternal();
66
+ });
67
+ }
68
+ onDeactivateInternal() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ yield this.resetStateInternal();
71
+ yield this.onDeactivate();
72
+ yield this.saveStateInternal();
73
+ });
74
+ }
75
+ onActorMethodPreInternal() {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ yield this.onActorMethodPre();
78
+ });
79
+ }
80
+ onActorMethodPostInternal() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ yield this.onActorMethodPost();
83
+ yield this.saveStateInternal();
84
+ });
85
+ }
86
+ resetStateInternal() {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ yield this.stateManager.clearCache();
89
+ });
90
+ }
91
+ saveStateInternal() {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ yield this.stateManager.saveState();
94
+ });
95
+ }
96
+ onActivate() {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ return;
99
+ });
100
+ }
101
+ onDeactivate() {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ return;
104
+ });
105
+ }
106
+ onActorMethodPre() {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ return;
109
+ });
110
+ }
111
+ onActorMethodPost() {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ return;
114
+ });
115
+ }
116
+ receiveReminder(_data) {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ this.daprLogger.warn(JSON.stringify({
119
+ error: 'ACTOR_METHOD_NOT_IMPLEMENTED',
120
+ errorMsg: `A reminder was created for the actor with id: ${this.id} but the method 'receiveReminder' was not implemented`,
121
+ }));
122
+ });
123
+ }
124
+ getDaprClient() {
125
+ return this.daprClient;
126
+ }
127
+ getStateProvider() {
128
+ return this.daprStateProvider;
129
+ }
130
+ getStateManager() {
131
+ return this.stateManager;
132
+ }
133
+ getActorId() {
134
+ return this.id;
135
+ }
136
+ getActorType() {
137
+ return this.actorType;
138
+ }
139
+ getId() {
140
+ return this.getActorId().getId();
141
+ }
142
+ }
143
+ exports.AbstractActor = AbstractActor;
@@ -0,0 +1,12 @@
1
+ import ActorManager from '@dapr/dapr/actors/runtime/ActorManager';
2
+ import ActorRuntime from '@dapr/dapr/actors/runtime/ActorRuntime';
3
+ export declare class ActorRuntimeService {
4
+ getRuntime(): ActorRuntime;
5
+ getActorManager<TActorInterface>(actorTypeName: string): ActorManager<TActorInterface>;
6
+ getAllActors(): Map<string, any>;
7
+ getActors<TActorInterface>(actorTypeName: string): Map<string, TActorInterface>;
8
+ getActor<TActorInterface>(actorTypeName: string, actorId: string): TActorInterface;
9
+ hasActor(actorTypeName: string, actorId: string): boolean;
10
+ invoke<TActorInterface, TResult>(actorTypeName: string, actorId: string, methodName: string, payload: any): Promise<TResult>;
11
+ removeInstance(actorTypeName: string, actorId: string): Promise<boolean>;
12
+ }
@@ -0,0 +1,75 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.ActorRuntimeService = void 0;
22
+ const dapr_1 = require("@dapr/dapr");
23
+ const ActorRuntime_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorRuntime"));
24
+ const common_1 = require("@nestjs/common");
25
+ let ActorRuntimeService = class ActorRuntimeService {
26
+ getRuntime() {
27
+ return ActorRuntime_1.default['instance'];
28
+ }
29
+ getActorManager(actorTypeName) {
30
+ return this.getRuntime().getActorManager(actorTypeName);
31
+ }
32
+ getAllActors() {
33
+ const runtime = this.getRuntime();
34
+ const actorManagers = runtime['actorManagers'];
35
+ const actors = new Map();
36
+ for (const item of actorManagers) {
37
+ const managerId = item[0];
38
+ const manager = item[1];
39
+ for (const actor of manager.actors) {
40
+ const actorId = `${managerId}:${actor[0]}`;
41
+ actors.set(actorId, actor[1]);
42
+ }
43
+ }
44
+ return actors;
45
+ }
46
+ getActors(actorTypeName) {
47
+ const manager = this.getActorManager(actorTypeName);
48
+ return manager.actors;
49
+ }
50
+ getActor(actorTypeName, actorId) {
51
+ const manager = this.getActorManager(actorTypeName);
52
+ return manager.actors.get(actorId);
53
+ }
54
+ hasActor(actorTypeName, actorId) {
55
+ const manager = this.getActorManager(actorTypeName);
56
+ return manager.actors.has(actorId);
57
+ }
58
+ invoke(actorTypeName, actorId, methodName, payload) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const manager = this.getActorManager(actorTypeName);
61
+ const requestBody = JSON.stringify(payload);
62
+ return yield manager.invoke(new dapr_1.ActorId(actorId), methodName, Buffer.from(requestBody));
63
+ });
64
+ }
65
+ removeInstance(actorTypeName, actorId) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const manager = this.getActorManager(actorTypeName);
68
+ return manager.actors.delete(actorId);
69
+ });
70
+ }
71
+ };
72
+ ActorRuntimeService = __decorate([
73
+ (0, common_1.Injectable)()
74
+ ], ActorRuntimeService);
75
+ exports.ActorRuntimeService = ActorRuntimeService;
@@ -5,6 +5,7 @@ export declare class DaprClientCache {
5
5
  private static clients;
6
6
  private static actorClients;
7
7
  private static stateProviders;
8
+ static getDaprClient(): DaprClient | undefined;
8
9
  static getAllClients(): DaprClient[];
9
10
  static getAllActorClients(): ActorClient[];
10
11
  static getAllStateProviders(): StateProvider[];
@@ -15,6 +15,11 @@ const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorCli
15
15
  const StateProvider_1 = __importDefault(require("@dapr/dapr/actors/runtime/StateProvider"));
16
16
  const common_1 = require("@nestjs/common");
17
17
  let DaprClientCache = class DaprClientCache {
18
+ static getDaprClient() {
19
+ if (this.clients.size === 0)
20
+ return undefined;
21
+ return this.clients.entries().next().value[1];
22
+ }
18
23
  static getAllClients() {
19
24
  return Array.from(this.clients.values());
20
25
  }
@@ -1,4 +1,4 @@
1
- import { AbstractActor } from '@dapr/dapr';
1
+ import { AbstractActor } from './abstract-actor';
2
2
  export interface IState {
3
3
  fromJSON(json: any): this;
4
4
  toJSON(): any;
@@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.StatefulActor = void 0;
13
- const dapr_1 = require("@dapr/dapr");
14
13
  const constants_1 = require("../constants");
15
- class StatefulActor extends dapr_1.AbstractActor {
14
+ const abstract_actor_1 = require("./abstract-actor");
15
+ class StatefulActor extends abstract_actor_1.AbstractActor {
16
16
  setState(stateName, value) {
17
17
  return __awaiter(this, void 0, void 0, function* () {
18
18
  yield this.getStateManager().setState(stateName, value);
@@ -20,6 +20,7 @@ exports.DaprModule = exports.createOptionsProvider = exports.DaprContextProvider
20
20
  const dapr_1 = require("@dapr/dapr");
21
21
  const common_1 = require("@nestjs/common");
22
22
  const core_1 = require("@nestjs/core");
23
+ const actor_runtime_service_1 = require("./actors/actor-runtime.service");
23
24
  const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
24
25
  const nest_actor_manager_1 = require("./actors/nest-actor-manager");
25
26
  const dapr_context_service_1 = require("./dapr-context-service");
@@ -56,6 +57,7 @@ let DaprModule = DaprModule_1 = class DaprModule {
56
57
  createOptionsProvider(options),
57
58
  {
58
59
  provide: dapr_1.DaprServer,
60
+ scope: common_1.Scope.DEFAULT,
59
61
  useValue: new dapr_1.DaprServer({
60
62
  serverHost: options.serverHost,
61
63
  serverPort: options.serverPort,
@@ -65,6 +67,7 @@ let DaprModule = DaprModule_1 = class DaprModule {
65
67
  },
66
68
  {
67
69
  provide: dapr_1.DaprClient,
70
+ scope: common_1.Scope.DEFAULT,
68
71
  useFactory: (daprServer) => daprServer.client,
69
72
  inject: [dapr_1.DaprServer],
70
73
  },
@@ -85,7 +88,8 @@ let DaprModule = DaprModule_1 = class DaprModule {
85
88
  ...this.createAsyncProviders(options),
86
89
  {
87
90
  provide: dapr_1.DaprServer,
88
- useFactory: ({ serverHost, serverPort, communicationProtocol, clientOptions, }) => new dapr_1.DaprServer({
91
+ scope: common_1.Scope.DEFAULT,
92
+ useFactory: ({ serverHost, serverPort, communicationProtocol, clientOptions }) => new dapr_1.DaprServer({
89
93
  serverHost,
90
94
  serverPort,
91
95
  clientOptions,
@@ -95,6 +99,7 @@ let DaprModule = DaprModule_1 = class DaprModule {
95
99
  },
96
100
  {
97
101
  provide: dapr_1.DaprClient,
102
+ scope: common_1.Scope.DEFAULT,
98
103
  useFactory: (daprServer) => daprServer.client,
99
104
  inject: [dapr_1.DaprServer],
100
105
  },
@@ -137,7 +142,7 @@ let DaprModule = DaprModule_1 = class DaprModule {
137
142
  DaprModule = DaprModule_1 = __decorate([
138
143
  (0, common_1.Module)({
139
144
  providers: [dapr_actor_client_service_1.DaprActorClient, nest_actor_manager_1.NestActorManager, dapr_context_service_1.DaprContextService],
140
- exports: [dapr_actor_client_service_1.DaprActorClient, dapr_context_service_1.DaprContextService],
145
+ exports: [dapr_actor_client_service_1.DaprActorClient, dapr_context_service_1.DaprContextService, actor_runtime_service_1.ActorRuntimeService],
141
146
  })
142
147
  ], DaprModule);
143
148
  exports.DaprModule = DaprModule;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ActorRuntimeService } from './actors/actor-runtime.service';
1
2
  import { DaprClientCache } from './actors/client-cache';
2
3
  import { DaprActorClient } from './actors/dapr-actor-client.service';
3
4
  import { StatefulActorOf } from './actors/stateful-actor-of';
@@ -11,4 +12,4 @@ import { DaprMetadataAccessor } from './dapr-metadata.accessor';
11
12
  import { DaprPubSub, DaprPubSubMetadata } from './dapr-pubsub.decorator';
12
13
  import { DaprLoader } from './dapr.loader';
13
14
  import { DaprModule } from './dapr.module';
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, };
15
+ 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, ActorRuntimeService, 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.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;
3
+ exports.StatefulActorOf = exports.StatefulActor = exports.DaprClientCache = exports.ActorRuntimeService = 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 actor_runtime_service_1 = require("./actors/actor-runtime.service");
5
+ Object.defineProperty(exports, "ActorRuntimeService", { enumerable: true, get: function () { return actor_runtime_service_1.ActorRuntimeService; } });
4
6
  const client_cache_1 = require("./actors/client-cache");
5
7
  Object.defineProperty(exports, "DaprClientCache", { enumerable: true, get: function () { return client_cache_1.DaprClientCache; } });
6
8
  const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.17",
3
+ "version": "0.9.19",
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",