@rayondigital/nest-dapr 0.9.4 → 0.9.5
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.
- package/dist/actors/actor-proxy-builder.d.ts +11 -0
- package/dist/actors/actor-proxy-builder.js +47 -0
- package/dist/actors/dapr-actor-client.service.d.ts +4 -0
- package/dist/actors/dapr-actor-client.service.js +35 -8
- package/dist/actors/nest-actor-manager.d.ts +2 -1
- package/dist/actors/nest-actor-manager.js +5 -2
- package/dist/dapr.loader.js +28 -11
- package/dist/dapr.module.d.ts +6 -4
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ActorId, CommunicationProtocolEnum, DaprClient } from '@dapr/dapr';
|
|
2
|
+
import ActorClient from '@dapr/dapr/actors/client/ActorClient/ActorClient';
|
|
3
|
+
import Class from '@dapr/dapr/types/Class';
|
|
4
|
+
import { DaprClientOptions } from '@dapr/dapr/types/DaprClientOptions';
|
|
5
|
+
export declare class ActorProxyBuilder<T> {
|
|
6
|
+
actorClient: ActorClient;
|
|
7
|
+
actorTypeClass: Class<T>;
|
|
8
|
+
constructor(actorTypeClass: Class<T>, daprClient: DaprClient);
|
|
9
|
+
constructor(actorTypeClass: Class<T>, host: string, port: string, communicationProtocol: CommunicationProtocolEnum, clientOptions: DaprClientOptions);
|
|
10
|
+
build(actorId: ActorId, actorTypeName?: string): T;
|
|
11
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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.ActorProxyBuilder = void 0;
|
|
16
|
+
const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorClient/ActorClient"));
|
|
17
|
+
class ActorProxyBuilder {
|
|
18
|
+
constructor(actorTypeClass, ...args) {
|
|
19
|
+
this.actorTypeClass = actorTypeClass;
|
|
20
|
+
if (args.length == 1) {
|
|
21
|
+
const [daprClient] = args;
|
|
22
|
+
this.actorClient = new ActorClient_1.default(daprClient.options.daprHost, daprClient.options.daprPort, daprClient.options.communicationProtocol, daprClient.options);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const [host, port, communicationProtocol, clientOptions] = args;
|
|
26
|
+
this.actorClient = new ActorClient_1.default(host, port, communicationProtocol, clientOptions);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
build(actorId, actorTypeName) {
|
|
30
|
+
const actorTypeClassName = actorTypeName !== null && actorTypeName !== void 0 ? actorTypeName : this.actorTypeClass.name;
|
|
31
|
+
const actorClient = this.actorClient;
|
|
32
|
+
const handler = {
|
|
33
|
+
get(_target, propKey, _receiver) {
|
|
34
|
+
return function (...args) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const body = args.length > 0 ? args : null;
|
|
37
|
+
const res = yield actorClient.actor.invoke(actorTypeClassName, actorId, propKey, body);
|
|
38
|
+
return res;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
const proxy = new Proxy(this.actorTypeClass, handler);
|
|
44
|
+
return proxy;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ActorProxyBuilder = ActorProxyBuilder;
|
|
@@ -4,15 +4,19 @@ export declare class DaprActorClient {
|
|
|
4
4
|
private readonly daprClient;
|
|
5
5
|
private actorClients;
|
|
6
6
|
private interfaces;
|
|
7
|
+
private interfaceToActorTypeNames;
|
|
7
8
|
private prefix;
|
|
8
9
|
private delimiter;
|
|
10
|
+
private typeNamePrefix;
|
|
9
11
|
constructor(daprClient: DaprClient);
|
|
10
12
|
setPrefix(prefix: string, delimiter?: string): void;
|
|
13
|
+
setTypeNamePrefix(prefix: string): void;
|
|
11
14
|
register<T>(actorTypeName: string, actorType: Type<T> | Function, daprClient?: DaprClient): void;
|
|
12
15
|
registerInterface<T>(actorType: Type<T> | Function, interfaceType: Type<T> | Function, daprClient?: DaprClient): void;
|
|
13
16
|
getActorId(actorId: string): ActorId;
|
|
14
17
|
getActor<TActorInterface>(actorType: Type<TActorInterface> | Function, actorId: string): TActorInterface;
|
|
15
18
|
getActorByTypeName<TActorInterface>(actorTypeName: string, actorId: string): TActorInterface;
|
|
19
|
+
getActorTypeName(typeName: string): string;
|
|
16
20
|
contains(actorTypeName: string): boolean;
|
|
17
21
|
private formatActorTypeName;
|
|
18
22
|
private getActorClient;
|
|
@@ -12,27 +12,35 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.DaprActorClient = void 0;
|
|
13
13
|
const dapr_1 = require("@dapr/dapr");
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
|
+
const actor_proxy_builder_1 = require("./actor-proxy-builder");
|
|
15
16
|
let DaprActorClient = class DaprActorClient {
|
|
16
17
|
constructor(daprClient) {
|
|
17
18
|
this.daprClient = daprClient;
|
|
18
19
|
this.actorClients = new Map();
|
|
19
20
|
this.interfaces = new Map();
|
|
21
|
+
this.interfaceToActorTypeNames = new Map();
|
|
20
22
|
this.prefix = '';
|
|
21
23
|
this.delimiter = '-';
|
|
24
|
+
this.typeNamePrefix = '';
|
|
22
25
|
}
|
|
23
26
|
setPrefix(prefix, delimiter = '-') {
|
|
24
27
|
this.prefix = prefix;
|
|
25
28
|
this.delimiter = delimiter;
|
|
26
29
|
}
|
|
30
|
+
setTypeNamePrefix(prefix) {
|
|
31
|
+
this.typeNamePrefix = prefix;
|
|
32
|
+
}
|
|
27
33
|
register(actorTypeName, actorType, daprClient) {
|
|
28
34
|
this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
|
|
29
|
-
this.actorClients.set(this.formatActorTypeName(actorTypeName), new
|
|
35
|
+
this.actorClients.set(this.formatActorTypeName(actorTypeName), new actor_proxy_builder_1.ActorProxyBuilder(actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
|
|
30
36
|
}
|
|
31
37
|
registerInterface(actorType, interfaceType, daprClient) {
|
|
32
|
-
var _a;
|
|
38
|
+
var _a, _b;
|
|
33
39
|
const interfaceTypeName = (_a = interfaceType.name) !== null && _a !== void 0 ? _a : interfaceType.constructor.name;
|
|
40
|
+
const actorTypeName = (_b = actorType.name) !== null && _b !== void 0 ? _b : actorType.constructor.name;
|
|
41
|
+
this.interfaceToActorTypeNames.set(interfaceTypeName, actorTypeName);
|
|
34
42
|
this.interfaces.set(this.formatActorTypeName(interfaceTypeName), actorType);
|
|
35
|
-
this.actorClients.set(this.formatActorTypeName(interfaceTypeName), new
|
|
43
|
+
this.actorClients.set(this.formatActorTypeName(interfaceTypeName), new actor_proxy_builder_1.ActorProxyBuilder(actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
|
|
36
44
|
}
|
|
37
45
|
getActorId(actorId) {
|
|
38
46
|
var _a;
|
|
@@ -43,27 +51,46 @@ let DaprActorClient = class DaprActorClient {
|
|
|
43
51
|
}
|
|
44
52
|
getActor(actorType, actorId) {
|
|
45
53
|
var _a;
|
|
46
|
-
const
|
|
54
|
+
const typeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
|
|
55
|
+
const actorTypeName = this.getActorTypeName(typeName);
|
|
47
56
|
if (!actorTypeName) {
|
|
48
57
|
throw new Error(`Actor type name must be provided`);
|
|
49
58
|
}
|
|
50
59
|
if (!this.contains(actorTypeName)) {
|
|
51
60
|
throw new Error(`Actor ${actorTypeName} not found`);
|
|
52
61
|
}
|
|
53
|
-
const actorClient = this.getActorClient(actorTypeName);
|
|
54
62
|
const fullActorId = this.getActorId(actorId);
|
|
55
|
-
|
|
63
|
+
const actorClient = this.getActorClient(actorTypeName);
|
|
64
|
+
return actorClient.build(fullActorId, actorTypeName);
|
|
56
65
|
}
|
|
57
66
|
getActorByTypeName(actorTypeName, actorId) {
|
|
67
|
+
if (this.interfaceToActorTypeNames.has(actorTypeName)) {
|
|
68
|
+
actorTypeName = this.interfaceToActorTypeNames.get(actorTypeName);
|
|
69
|
+
}
|
|
58
70
|
if (!actorTypeName) {
|
|
59
71
|
throw new Error(`Actor type name must be provided`);
|
|
60
72
|
}
|
|
61
73
|
if (!this.contains(actorTypeName)) {
|
|
62
74
|
throw new Error(`Actor ${actorTypeName} not found`);
|
|
63
75
|
}
|
|
64
|
-
const actorClient = this.getActorClient(actorTypeName);
|
|
65
76
|
const fullActorId = this.getActorId(actorId);
|
|
66
|
-
|
|
77
|
+
const actorClient = this.getActorClient(actorTypeName);
|
|
78
|
+
return actorClient.build(fullActorId, actorTypeName);
|
|
79
|
+
}
|
|
80
|
+
getActorTypeName(typeName) {
|
|
81
|
+
if (this.interfaceToActorTypeNames.has(typeName)) {
|
|
82
|
+
const actorTypeName = this.interfaceToActorTypeNames.get(typeName);
|
|
83
|
+
if (this.actorClients.has(actorTypeName)) {
|
|
84
|
+
return actorTypeName;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
return `${this.typeNamePrefix}${actorTypeName}`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (this.typeNamePrefix) {
|
|
91
|
+
return `${this.typeNamePrefix}${typeName}`;
|
|
92
|
+
}
|
|
93
|
+
return typeName;
|
|
67
94
|
}
|
|
68
95
|
contains(actorTypeName) {
|
|
69
96
|
return this.actorClients.has(this.formatActorTypeName(actorTypeName));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ActorId } from '@dapr/dapr';
|
|
2
2
|
import { ModuleRef } from '@nestjs/core';
|
|
3
|
-
|
|
3
|
+
import { DaprModuleActorOptions } from '../dapr.module';
|
|
4
|
+
export declare function patchActorManagerForNest(moduleRef: ModuleRef, options: DaprModuleActorOptions, invokeWrapperFn?: (actorId: ActorId, methodName: string, data: any, method: (actorId: ActorId, methodName: string, data: any) => Promise<any>) => Promise<any>): void;
|
|
4
5
|
export interface ActorMethodInvocation {
|
|
5
6
|
actorId: ActorId;
|
|
6
7
|
method: string;
|
|
@@ -16,12 +16,15 @@ exports.patchActorManagerForNest = void 0;
|
|
|
16
16
|
const ActorManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorManager"));
|
|
17
17
|
const common_1 = require("@nestjs/common");
|
|
18
18
|
const instance_wrapper_1 = require("@nestjs/core/injector/instance-wrapper");
|
|
19
|
-
function patchActorManagerForNest(moduleRef, invokeWrapperFn) {
|
|
19
|
+
function patchActorManagerForNest(moduleRef, options, invokeWrapperFn) {
|
|
20
20
|
const originalCreateActor = ActorManager_1.default.prototype.createActor;
|
|
21
21
|
const originalCallActor = ActorManager_1.default.prototype.callActorMethod;
|
|
22
22
|
ActorManager_1.default.prototype.createActor = function (actorId) {
|
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const instance = yield originalCreateActor.bind(this)(actorId);
|
|
24
|
+
const instance = (yield originalCreateActor.bind(this)(actorId));
|
|
25
|
+
if (options === null || options === void 0 ? void 0 : options.typeNamePrefix) {
|
|
26
|
+
instance['actorType'] = `${options.typeNamePrefix}${instance.actorType}`;
|
|
27
|
+
}
|
|
25
28
|
try {
|
|
26
29
|
yield resolveDependencies(moduleRef, instance);
|
|
27
30
|
}
|
package/dist/dapr.loader.js
CHANGED
|
@@ -20,10 +20,15 @@ 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
|
+
};
|
|
23
26
|
var DaprLoader_1;
|
|
24
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
28
|
exports.DaprLoader = void 0;
|
|
26
29
|
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"));
|
|
27
32
|
const common_1 = require("@nestjs/common");
|
|
28
33
|
const core_1 = require("@nestjs/core");
|
|
29
34
|
const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
|
|
@@ -42,23 +47,23 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
|
|
|
42
47
|
this.logger = new common_1.Logger(DaprLoader_1.name);
|
|
43
48
|
}
|
|
44
49
|
onApplicationBootstrap() {
|
|
45
|
-
var _a, _b, _c, _d;
|
|
50
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
46
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
|
|
48
|
-
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 : '-');
|
|
49
|
-
}
|
|
50
|
-
(0, nest_actor_manager_1.patchActorManagerForNest)(this.moduleRef);
|
|
52
|
+
(0, nest_actor_manager_1.patchActorManagerForNest)(this.moduleRef, this.options.actorOptions);
|
|
51
53
|
yield this.daprServer.actor.init();
|
|
52
54
|
this.loadDaprHandlers();
|
|
53
55
|
this.logger.log('Starting Dapr server');
|
|
54
56
|
yield this.daprServer.start();
|
|
55
|
-
this.daprServer.invoker.listen('test', (data) => __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
console.log('data', data);
|
|
57
|
-
return { status: 200, data: { message: 'ok' } };
|
|
58
|
-
}), { method: dapr_1.HttpMethod.POST });
|
|
59
57
|
this.logger.log('Dapr server started');
|
|
60
58
|
const resRegisteredActors = yield this.daprServer.actor.getRegisteredActors();
|
|
61
59
|
this.logger.log(`Registered Actors: ${resRegisteredActors.join(', ')}`);
|
|
60
|
+
if (this.options.actorOptions) {
|
|
61
|
+
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 : '-');
|
|
62
|
+
this.daprActorClient.setTypeNamePrefix((_f = (_e = this.options.actorOptions) === null || _e === void 0 ? void 0 : _e.typeNamePrefix) !== null && _f !== void 0 ? _f : '');
|
|
63
|
+
if ((_g = this.options.actorOptions) === null || _g === void 0 ? void 0 : _g.prefix) {
|
|
64
|
+
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 : '-'}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
62
67
|
});
|
|
63
68
|
}
|
|
64
69
|
onApplicationShutdown() {
|
|
@@ -136,11 +141,23 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
|
|
|
136
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
137
142
|
if (!actorType)
|
|
138
143
|
return;
|
|
139
|
-
|
|
144
|
+
let actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
|
|
140
145
|
const daprActorMetadata = this.daprMetadataAccessor.getDaprActorMetadata(actorType);
|
|
141
146
|
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;
|
|
147
|
+
if (this.options.actorOptions.typeNamePrefix) {
|
|
148
|
+
actorTypeName = this.options.actorOptions.typeNamePrefix + actorTypeName;
|
|
149
|
+
}
|
|
142
150
|
this.logger.log(`Registering Dapr Actor: ${actorTypeName} of type ${interfaceTypeName !== null && interfaceTypeName !== void 0 ? interfaceTypeName : 'unknown'}`);
|
|
143
|
-
|
|
151
|
+
try {
|
|
152
|
+
const actorManager = ActorRuntime_1.default.getInstanceByDaprClient(this.daprServer.client);
|
|
153
|
+
const managers = actorManager['actorManagers'];
|
|
154
|
+
if (!managers.has(actorTypeName)) {
|
|
155
|
+
managers.set(actorTypeName, new ActorManager_1.default(actorType, this.daprServer.client));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
yield this.daprServer.actor.registerActor(actorType);
|
|
160
|
+
}
|
|
144
161
|
this.daprActorClient.register(actorTypeName, actorType, this.daprServer.client);
|
|
145
162
|
if (daprActorMetadata.interfaceType) {
|
|
146
163
|
this.daprActorClient.registerInterface(actorType, daprActorMetadata.interfaceType, this.daprServer.client);
|
package/dist/dapr.module.d.ts
CHANGED
|
@@ -8,10 +8,12 @@ export interface DaprModuleOptions {
|
|
|
8
8
|
communicationProtocol?: CommunicationProtocolEnum;
|
|
9
9
|
clientOptions?: DaprClientOptions;
|
|
10
10
|
onError?: (name: string, topicName: string, error: any) => DaprPubSubStatusEnum;
|
|
11
|
-
actorOptions?:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
actorOptions?: DaprModuleActorOptions;
|
|
12
|
+
}
|
|
13
|
+
export interface DaprModuleActorOptions {
|
|
14
|
+
prefix?: string;
|
|
15
|
+
delimiter?: string;
|
|
16
|
+
typeNamePrefix?: string;
|
|
15
17
|
}
|
|
16
18
|
export interface DaprModuleOptionsFactory {
|
|
17
19
|
createDaprModuleOptions(): Promise<DaprModuleOptions> | DaprModuleOptions;
|