@lakutata/nats 1.2.0 → 1.3.1
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/CHANGELOG.md +23 -0
- package/dist/CommonExports.d.ts +1 -0
- package/dist/CommonExports.d.ts.map +1 -1
- package/dist/CommonExports.js +4 -1
- package/dist/components/NATS.d.ts +39 -1
- package/dist/components/NATS.d.ts.map +1 -1
- package/dist/components/NATS.js +84 -0
- package/dist/entrypoints/SetupNatsServiceEntrypoint.d.ts.map +1 -1
- package/dist/entrypoints/SetupNatsServiceEntrypoint.js +12 -4
- package/dist/exceptions/ServiceInvokeException.d.ts +5 -0
- package/dist/exceptions/ServiceInvokeException.d.ts.map +1 -0
- package/dist/exceptions/ServiceInvokeException.js +11 -0
- package/dist/lib/ServiceEventCodec.d.ts +10 -0
- package/dist/lib/ServiceEventCodec.d.ts.map +1 -0
- package/dist/lib/ServiceEventCodec.js +48 -0
- package/dist/lib/ServiceResponseCodec.d.ts +21 -0
- package/dist/lib/ServiceResponseCodec.d.ts.map +1 -0
- package/dist/lib/ServiceResponseCodec.js +35 -0
- package/dist/providers/ServiceProxy.d.ts +71 -0
- package/dist/providers/ServiceProxy.d.ts.map +1 -0
- package/dist/providers/ServiceProxy.js +98 -0
- package/dist/tests/NatsTest.spec.js +42 -8
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,29 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.3.1](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.3.0...@lakutata/nats@1.3.1) (2025-11-10)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **nats:** add default timeout for service proxy invocation ([d3f13ec](https://github.com/lakutata/lakutata-packages/commit/d3f13ec0884083e6ae162d1e89241cfad3a3680a))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [1.3.0](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.2.0...@lakutata/nats@1.3.0) (2025-10-16)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* **nats:** add service event system with pub/sub capabilities ([0dbc85a](https://github.com/lakutata/lakutata-packages/commit/0dbc85af81a37ddf21912fc520023928224ab0ee))
|
|
23
|
+
* **nats:** implement service proxy and error handling ([4b9c806](https://github.com/lakutata/lakutata-packages/commit/4b9c8068c3071c9ccc0076d581f82d418cd9711f))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
6
29
|
# [1.2.0](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.1.0...@lakutata/nats@1.2.0) (2025-10-14)
|
|
7
30
|
|
|
8
31
|
|
package/dist/CommonExports.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { SetupNatsServiceEntrypoint } from './entrypoints/SetupNatsServiceEntrypoint';
|
|
2
|
+
export { ServiceProxy, BuildServiceProxy, type BuildServiceProxyOptions } from './providers/ServiceProxy';
|
|
2
3
|
export { NATS, buildNatsClientOptions } from './components/NATS';
|
|
3
4
|
export { JSONCodec } from './codecs/JSONCodec';
|
|
4
5
|
export { StringCodec } from 'nats';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommonExports.d.ts","sourceRoot":"","sources":["../src/CommonExports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,0BAA0B,EAAC,MAAM,0CAA0C,CAAA;AACnF,OAAO,EAAC,IAAI,EAAE,sBAAsB,EAAC,MAAM,mBAAmB,CAAA;AAC9D,OAAO,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAC,WAAW,EAAC,MAAM,MAAM,CAAA;AAChC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C,MAAM,iDAAiD,CAAA;AAClG,OAAO,EAAC,qBAAqB,EAAC,MAAM,oCAAoC,CAAA;AACxE,OAAO,EAAC,2BAA2B,EAAC,MAAM,0CAA0C,CAAA"}
|
package/dist/CommonExports.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NatsRequestTimeoutException = exports.NatsNotFoundException = exports.NatsNoRespondersAvailableException = exports.NatsInternalServerException = exports.NatsForbiddenException = exports.NatsBadRequestException = exports.StringCodec = exports.JSONCodec = exports.buildNatsClientOptions = exports.NATS = exports.SetupNatsServiceEntrypoint = void 0;
|
|
3
|
+
exports.NatsRequestTimeoutException = exports.NatsNotFoundException = exports.NatsNoRespondersAvailableException = exports.NatsInternalServerException = exports.NatsForbiddenException = exports.NatsBadRequestException = exports.StringCodec = exports.JSONCodec = exports.buildNatsClientOptions = exports.NATS = exports.BuildServiceProxy = exports.ServiceProxy = exports.SetupNatsServiceEntrypoint = void 0;
|
|
4
4
|
var SetupNatsServiceEntrypoint_1 = require("./entrypoints/SetupNatsServiceEntrypoint");
|
|
5
5
|
Object.defineProperty(exports, "SetupNatsServiceEntrypoint", { enumerable: true, get: function () { return SetupNatsServiceEntrypoint_1.SetupNatsServiceEntrypoint; } });
|
|
6
|
+
var ServiceProxy_1 = require("./providers/ServiceProxy");
|
|
7
|
+
Object.defineProperty(exports, "ServiceProxy", { enumerable: true, get: function () { return ServiceProxy_1.ServiceProxy; } });
|
|
8
|
+
Object.defineProperty(exports, "BuildServiceProxy", { enumerable: true, get: function () { return ServiceProxy_1.BuildServiceProxy; } });
|
|
6
9
|
var NATS_1 = require("./components/NATS");
|
|
7
10
|
Object.defineProperty(exports, "NATS", { enumerable: true, get: function () { return NATS_1.NATS; } });
|
|
8
11
|
Object.defineProperty(exports, "buildNatsClientOptions", { enumerable: true, get: function () { return NATS_1.buildNatsClientOptions; } });
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { Component, ComponentOptionsBuilder } from 'lakutata';
|
|
1
|
+
import { Application, Component, ComponentOptionsBuilder } from 'lakutata';
|
|
2
2
|
import { type Codec, Subscription } from 'nats';
|
|
3
3
|
import { SubscribeOptions } from '../types/SubscribeOptions';
|
|
4
4
|
import { NatsClientOptions } from '../interfaces/NatsClientOptions';
|
|
5
5
|
export declare const buildNatsClientOptions: ComponentOptionsBuilder<NatsClientOptions>;
|
|
6
6
|
export declare class NATS extends Component {
|
|
7
7
|
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Application instance
|
|
10
|
+
* @protected
|
|
11
|
+
*/
|
|
12
|
+
protected readonly app: Application;
|
|
8
13
|
/**
|
|
9
14
|
* NATS servers
|
|
10
15
|
* @protected
|
|
@@ -89,6 +94,11 @@ export declare class NATS extends Component {
|
|
|
89
94
|
* @protected
|
|
90
95
|
*/
|
|
91
96
|
protected readonly reconnect?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Service event subscription map
|
|
99
|
+
* @protected
|
|
100
|
+
*/
|
|
101
|
+
protected readonly serviceEventSubscriptionMap: Map<string, Map<(...args: any[]) => void, Subscription>>;
|
|
92
102
|
/**
|
|
93
103
|
* Initializer
|
|
94
104
|
* @protected
|
|
@@ -125,5 +135,33 @@ export declare class NATS extends Component {
|
|
|
125
135
|
* @param subscribeOptions
|
|
126
136
|
*/
|
|
127
137
|
subscribe(subject: string, callback: (data: any) => any | Promise<any>, subscribeOptions?: SubscribeOptions): Subscription;
|
|
138
|
+
/**
|
|
139
|
+
* Emit service event
|
|
140
|
+
* @param eventName
|
|
141
|
+
* @param args
|
|
142
|
+
*/
|
|
143
|
+
emitServiceEvent(eventName: string, ...args: any[]): this;
|
|
144
|
+
/**
|
|
145
|
+
* On service event
|
|
146
|
+
* @param serviceId
|
|
147
|
+
* @param eventName
|
|
148
|
+
* @param listener
|
|
149
|
+
* @param onlySingleClientReceived
|
|
150
|
+
*/
|
|
151
|
+
onServiceEvent(serviceId: string, eventName: string, listener: (...args: any[]) => void, onlySingleClientReceived?: boolean): this;
|
|
152
|
+
/**
|
|
153
|
+
* Once service event
|
|
154
|
+
* @param serviceId
|
|
155
|
+
* @param eventName
|
|
156
|
+
* @param listener
|
|
157
|
+
*/
|
|
158
|
+
onceServiceEvent(serviceId: string, eventName: string, listener: (...args: any[]) => void): this;
|
|
159
|
+
/**
|
|
160
|
+
* Off server events
|
|
161
|
+
* @param serviceId
|
|
162
|
+
* @param eventName
|
|
163
|
+
* @param listener
|
|
164
|
+
*/
|
|
165
|
+
offServiceEvent(serviceId: string, eventName: string, listener?: (...args: any[]) => void): this;
|
|
128
166
|
}
|
|
129
167
|
//# sourceMappingURL=NATS.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NATS.d.ts","sourceRoot":"","sources":["../../src/components/NATS.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,SAAS,EAET,uBAAuB,EAE1B,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAC,KAAK,KAAK,EAA6C,YAAY,EAAC,MAAM,MAAM,CAAA;AACxF,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAA;AAO1D,OAAO,EAAC,iBAAiB,EAAC,MAAM,iCAAiC,CAAA;
|
|
1
|
+
{"version":3,"file":"NATS.d.ts","sourceRoot":"","sources":["../../src/components/NATS.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,SAAS,EAET,uBAAuB,EAE1B,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAC,KAAK,KAAK,EAA6C,YAAY,EAAC,MAAM,MAAM,CAAA;AACxF,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAA;AAO1D,OAAO,EAAC,iBAAiB,EAAC,MAAM,iCAAiC,CAAA;AAIjE,eAAO,MAAM,sBAAsB,EAAE,uBAAuB,CAAC,iBAAiB,CAkB7E,CAAA;AAED,qBAAa,IAAK,SAAQ,SAAS;;IAC/B;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GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;CAW1G"}
|
package/dist/components/NATS.js
CHANGED
|
@@ -20,6 +20,7 @@ const NatsRequestTimeoutException_1 = require("../exceptions/NatsRequestTimeoutE
|
|
|
20
20
|
const NatsNoRespondersAvailableException_1 = require("../exceptions/NatsNoRespondersAvailableException");
|
|
21
21
|
const NatsInternalServerException_1 = require("../exceptions/NatsInternalServerException");
|
|
22
22
|
const JSONCodec_1 = require("../codecs/JSONCodec");
|
|
23
|
+
const ServiceEventCodec_1 = require("../lib/ServiceEventCodec");
|
|
23
24
|
const buildNatsClientOptions = (options) => {
|
|
24
25
|
return {
|
|
25
26
|
class: NATS,
|
|
@@ -41,6 +42,14 @@ const buildNatsClientOptions = (options) => {
|
|
|
41
42
|
};
|
|
42
43
|
exports.buildNatsClientOptions = buildNatsClientOptions;
|
|
43
44
|
class NATS extends lakutata_1.Component {
|
|
45
|
+
constructor() {
|
|
46
|
+
super(...arguments);
|
|
47
|
+
/**
|
|
48
|
+
* Service event subscription map
|
|
49
|
+
* @protected
|
|
50
|
+
*/
|
|
51
|
+
this.serviceEventSubscriptionMap = new Map();
|
|
52
|
+
}
|
|
44
53
|
/**
|
|
45
54
|
* NATS client instance
|
|
46
55
|
* @private
|
|
@@ -154,8 +163,83 @@ class NATS extends lakutata_1.Component {
|
|
|
154
163
|
}
|
|
155
164
|
return subscription;
|
|
156
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Emit service event
|
|
168
|
+
* @param eventName
|
|
169
|
+
* @param args
|
|
170
|
+
*/
|
|
171
|
+
emitServiceEvent(eventName, ...args) {
|
|
172
|
+
const eventSubject = ServiceEventCodec_1.ServiceEventCodec.formatSubject(this.app.appId, eventName);
|
|
173
|
+
this.publish(eventSubject, ServiceEventCodec_1.ServiceEventCodec.encode(args));
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* On service event
|
|
178
|
+
* @param serviceId
|
|
179
|
+
* @param eventName
|
|
180
|
+
* @param listener
|
|
181
|
+
* @param onlySingleClientReceived
|
|
182
|
+
*/
|
|
183
|
+
onServiceEvent(serviceId, eventName, listener, onlySingleClientReceived = false) {
|
|
184
|
+
const eventSubject = ServiceEventCodec_1.ServiceEventCodec.formatSubject(serviceId, eventName);
|
|
185
|
+
if (!this.serviceEventSubscriptionMap.has(eventSubject))
|
|
186
|
+
this.serviceEventSubscriptionMap.set(eventSubject, new Map());
|
|
187
|
+
if (!this.serviceEventSubscriptionMap.get(eventSubject)?.has(listener)) {
|
|
188
|
+
const subscription = this.subscribe(eventSubject, (eventData) => {
|
|
189
|
+
listener(...ServiceEventCodec_1.ServiceEventCodec.decode(eventData));
|
|
190
|
+
}, onlySingleClientReceived ? { queue: eventSubject } : void (0));
|
|
191
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.set(listener, subscription);
|
|
192
|
+
}
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Once service event
|
|
197
|
+
* @param serviceId
|
|
198
|
+
* @param eventName
|
|
199
|
+
* @param listener
|
|
200
|
+
*/
|
|
201
|
+
onceServiceEvent(serviceId, eventName, listener) {
|
|
202
|
+
const eventSubject = ServiceEventCodec_1.ServiceEventCodec.formatSubject(serviceId, eventName);
|
|
203
|
+
if (!this.serviceEventSubscriptionMap.has(eventSubject))
|
|
204
|
+
this.serviceEventSubscriptionMap.set(eventSubject, new Map());
|
|
205
|
+
if (!this.serviceEventSubscriptionMap.get(eventSubject)?.has(listener)) {
|
|
206
|
+
const subscription = this.subscribe(eventSubject, (eventData) => {
|
|
207
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.delete(listener);
|
|
208
|
+
listener(...ServiceEventCodec_1.ServiceEventCodec.decode(eventData));
|
|
209
|
+
}, { max: 1 });
|
|
210
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.set(listener, subscription);
|
|
211
|
+
}
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Off server events
|
|
216
|
+
* @param serviceId
|
|
217
|
+
* @param eventName
|
|
218
|
+
* @param listener
|
|
219
|
+
*/
|
|
220
|
+
offServiceEvent(serviceId, eventName, listener) {
|
|
221
|
+
const eventSubject = ServiceEventCodec_1.ServiceEventCodec.formatSubject(serviceId, eventName);
|
|
222
|
+
if (listener) {
|
|
223
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.get(listener)?.unsubscribe();
|
|
224
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.delete(listener);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.serviceEventSubscriptionMap.get(eventSubject)?.forEach((subscription) => subscription.unsubscribe());
|
|
228
|
+
this.serviceEventSubscriptionMap.delete(eventSubject);
|
|
229
|
+
}
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
157
232
|
}
|
|
158
233
|
exports.NATS = NATS;
|
|
234
|
+
__decorate([
|
|
235
|
+
(0, di_1.Inject)(lakutata_1.Application),
|
|
236
|
+
__metadata("design:type", lakutata_1.Application
|
|
237
|
+
/**
|
|
238
|
+
* NATS servers
|
|
239
|
+
* @protected
|
|
240
|
+
*/
|
|
241
|
+
)
|
|
242
|
+
], NATS.prototype, "app", void 0);
|
|
159
243
|
__decorate([
|
|
160
244
|
(0, di_1.Configurable)(lakutata_1.DTO.Alternatives(lakutata_1.DTO.String(), lakutata_1.DTO.Array(lakutata_1.DTO.String())).required()),
|
|
161
245
|
__metadata("design:type", Object)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SetupNatsServiceEntrypoint.d.ts","sourceRoot":"","sources":["../../src/entrypoints/SetupNatsServiceEntrypoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,iBAAiB,EAEpB,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"SetupNatsServiceEntrypoint.d.ts","sourceRoot":"","sources":["../../src/entrypoints/SetupNatsServiceEntrypoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,iBAAiB,EAEpB,MAAM,yBAAyB,CAAA;AAKhC;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,iBAAiB,EAAE,MAAM,GAAG,iBAAiB,CAevF"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SetupNatsServiceEntrypoint = void 0;
|
|
4
4
|
const entrypoint_1 = require("lakutata/com/entrypoint");
|
|
5
|
+
const ServiceResponseCodec_1 = require("../lib/ServiceResponseCodec");
|
|
5
6
|
/**
|
|
6
7
|
* Setup service based on NATS
|
|
7
8
|
* @param natsComponentName
|
|
@@ -11,10 +12,17 @@ function SetupNatsServiceEntrypoint(natsComponentName) {
|
|
|
11
12
|
return (0, entrypoint_1.BuildServiceEntrypoint)(async (module, handler) => {
|
|
12
13
|
const nats = await module.getObject(natsComponentName);
|
|
13
14
|
nats.subscribe(module.appId, async (incomeRequestPayload) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
try {
|
|
16
|
+
return ServiceResponseCodec_1.ServiceResponseCodec.encode(await handler(new entrypoint_1.ServiceContext({
|
|
17
|
+
data: incomeRequestPayload
|
|
18
|
+
})), false);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
const errorObject = {};
|
|
22
|
+
Object.getOwnPropertyNames(e).forEach((prop) => errorObject[prop] = e[prop]);
|
|
23
|
+
return ServiceResponseCodec_1.ServiceResponseCodec.encode(errorObject, true);
|
|
24
|
+
}
|
|
25
|
+
}, { queue: `${module.appId}.serviceQueue` });
|
|
18
26
|
});
|
|
19
27
|
}
|
|
20
28
|
exports.SetupNatsServiceEntrypoint = SetupNatsServiceEntrypoint;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceInvokeException.d.ts","sourceRoot":"","sources":["../../src/exceptions/ServiceInvokeException.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAA;AAElC,qBAAa,sBAAuB,SAAQ,SAAS;IAC1C,KAAK,EAAE,MAAM,GAAG,MAAM,CAAqB;CACrD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceInvokeException = void 0;
|
|
4
|
+
const lakutata_1 = require("lakutata");
|
|
5
|
+
class ServiceInvokeException extends lakutata_1.Exception {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.errno = 'E_SERVICE_INVOKE';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ServiceInvokeException = ServiceInvokeException;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface ServiceEventData {
|
|
2
|
+
readonly args: any[];
|
|
3
|
+
}
|
|
4
|
+
export declare class ServiceEventCodec {
|
|
5
|
+
protected static hashEventName(serviceId: string, eventName: string): string;
|
|
6
|
+
static encode(args: any[]): ServiceEventData;
|
|
7
|
+
static decode(eventData: ServiceEventData): any[];
|
|
8
|
+
static formatSubject(serviceId: string, eventName: string): string;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=ServiceEventCodec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceEventCodec.d.ts","sourceRoot":"","sources":["../../src/lib/ServiceEventCodec.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,CAAA;CACvB;AAED,qBAAa,iBAAiB;IAC1B,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;WAO9D,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,gBAAgB;WAMrC,MAAM,CAAC,SAAS,EAAE,gBAAgB,GAAG,GAAG,EAAE;WAI1C,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;CAG5E"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ServiceEventCodec = void 0;
|
|
27
|
+
const helper_1 = require("lakutata/helper");
|
|
28
|
+
const querystring = __importStar(require("node:querystring"));
|
|
29
|
+
class ServiceEventCodec {
|
|
30
|
+
static hashEventName(serviceId, eventName) {
|
|
31
|
+
return (0, helper_1.MD5)(querystring.encode({
|
|
32
|
+
serviceId: serviceId,
|
|
33
|
+
eventName: eventName
|
|
34
|
+
})).toString('hex');
|
|
35
|
+
}
|
|
36
|
+
static encode(args) {
|
|
37
|
+
return {
|
|
38
|
+
args: args
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
static decode(eventData) {
|
|
42
|
+
return eventData.args;
|
|
43
|
+
}
|
|
44
|
+
static formatSubject(serviceId, eventName) {
|
|
45
|
+
return `framework.internal.service.event.${this.hashEventName(serviceId, eventName)}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.ServiceEventCodec = ServiceEventCodec;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Exception } from 'lakutata';
|
|
2
|
+
interface ServiceResponse {
|
|
3
|
+
readonly success: boolean;
|
|
4
|
+
readonly error: Error | Exception | null;
|
|
5
|
+
readonly payload: any | null;
|
|
6
|
+
}
|
|
7
|
+
export declare class ServiceResponseCodec {
|
|
8
|
+
/**
|
|
9
|
+
* Encode service response
|
|
10
|
+
* @param data
|
|
11
|
+
* @param isError
|
|
12
|
+
*/
|
|
13
|
+
static encode(data: any, isError: boolean): ServiceResponse;
|
|
14
|
+
/**
|
|
15
|
+
* Decode service response
|
|
16
|
+
* @param response
|
|
17
|
+
*/
|
|
18
|
+
static decode(response: ServiceResponse): any;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=ServiceResponseCodec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceResponseCodec.d.ts","sourceRoot":"","sources":["../../src/lib/ServiceResponseCodec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAA;AAGlC,UAAU,eAAe;IACrB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,IAAI,CAAA;IACxC,QAAQ,CAAC,OAAO,EAAE,GAAG,GAAG,IAAI,CAAA;CAC/B;AAED,qBAAa,oBAAoB;IAC7B;;;;OAIG;WACW,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,GAAG,eAAe;IAYlE;;;OAGG;WACW,MAAM,CAAC,QAAQ,EAAE,eAAe,GAAG,GAAG;CAQvD"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceResponseCodec = void 0;
|
|
4
|
+
const ServiceInvokeException_1 = require("../exceptions/ServiceInvokeException");
|
|
5
|
+
class ServiceResponseCodec {
|
|
6
|
+
/**
|
|
7
|
+
* Encode service response
|
|
8
|
+
* @param data
|
|
9
|
+
* @param isError
|
|
10
|
+
*/
|
|
11
|
+
static encode(data, isError) {
|
|
12
|
+
return isError ? {
|
|
13
|
+
success: false,
|
|
14
|
+
error: data,
|
|
15
|
+
payload: null
|
|
16
|
+
} : {
|
|
17
|
+
success: true,
|
|
18
|
+
error: null,
|
|
19
|
+
payload: data
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Decode service response
|
|
24
|
+
* @param response
|
|
25
|
+
*/
|
|
26
|
+
static decode(response) {
|
|
27
|
+
if (!response.success) {
|
|
28
|
+
const remoteError = response.error;
|
|
29
|
+
const serviceInvokeException = new ServiceInvokeException_1.ServiceInvokeException(remoteError.message || 'Unknown Error');
|
|
30
|
+
throw Object.assign(serviceInvokeException, response.error);
|
|
31
|
+
}
|
|
32
|
+
return response.payload;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.ServiceResponseCodec = ServiceResponseCodec;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ActionPattern, Application, Provider, ProviderOptionsBuilder } from 'lakutata';
|
|
2
|
+
import { NATS } from '../components/NATS';
|
|
3
|
+
/**
|
|
4
|
+
* Build service proxy options
|
|
5
|
+
*/
|
|
6
|
+
export type BuildServiceProxyOptions = {
|
|
7
|
+
readonly natsComponentName: string;
|
|
8
|
+
readonly serviceId: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Build service proxy
|
|
12
|
+
* @param options
|
|
13
|
+
* @constructor
|
|
14
|
+
*/
|
|
15
|
+
export declare const BuildServiceProxy: ProviderOptionsBuilder<BuildServiceProxyOptions>;
|
|
16
|
+
/**
|
|
17
|
+
* Service proxy provider
|
|
18
|
+
*/
|
|
19
|
+
export declare class ServiceProxy extends Provider {
|
|
20
|
+
/**
|
|
21
|
+
* Inject application instance
|
|
22
|
+
* @protected
|
|
23
|
+
*/
|
|
24
|
+
protected readonly app: Application;
|
|
25
|
+
/**
|
|
26
|
+
* NATS registered component name
|
|
27
|
+
* @protected
|
|
28
|
+
*/
|
|
29
|
+
protected readonly natsComponentName: string;
|
|
30
|
+
/**
|
|
31
|
+
* Service ID
|
|
32
|
+
* @protected
|
|
33
|
+
*/
|
|
34
|
+
protected readonly serviceId: string;
|
|
35
|
+
/**
|
|
36
|
+
* NATS component instance
|
|
37
|
+
* @protected
|
|
38
|
+
*/
|
|
39
|
+
protected nats: NATS;
|
|
40
|
+
/**
|
|
41
|
+
* Initializer
|
|
42
|
+
* @protected
|
|
43
|
+
*/
|
|
44
|
+
protected init(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Invoke service registered method by pattern
|
|
47
|
+
* @param input
|
|
48
|
+
* @param timeout
|
|
49
|
+
*/
|
|
50
|
+
invoke<T extends Record<string, any>>(input: ActionPattern<T>, timeout?: number): Promise<any>;
|
|
51
|
+
/**
|
|
52
|
+
* On service event
|
|
53
|
+
* @param eventName
|
|
54
|
+
* @param listener
|
|
55
|
+
* @param onlySingleClientReceived
|
|
56
|
+
*/
|
|
57
|
+
on(eventName: string, listener: (...args: any[]) => void, onlySingleClientReceived?: boolean): this;
|
|
58
|
+
/**
|
|
59
|
+
* Once service event
|
|
60
|
+
* @param eventName
|
|
61
|
+
* @param listener
|
|
62
|
+
*/
|
|
63
|
+
once(eventName: string, listener: (...args: any[]) => void): this;
|
|
64
|
+
/**
|
|
65
|
+
* Off service events
|
|
66
|
+
* @param eventName
|
|
67
|
+
* @param listener
|
|
68
|
+
*/
|
|
69
|
+
off(eventName: string, listener?: (...args: any[]) => void): this;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=ServiceProxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceProxy.d.ts","sourceRoot":"","sources":["../../src/providers/ServiceProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,WAAW,EAAO,QAAQ,EAAE,sBAAsB,EAAC,MAAM,UAAU,CAAA;AAE1F,OAAO,EAAC,IAAI,EAAC,MAAM,oBAAoB,CAAA;AAGvC;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACnC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAA;IAClC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;CAC7B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,sBAAsB,CAAC,wBAAwB,CAM9E,CAAA;AAED;;GAEG;AACH,qBACa,YAAa,SAAQ,QAAQ;IACtC;;;OAGG;IAEH,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;IAEnC;;;OAGG;IAEH,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAA;IAE5C;;;OAGG;IAEH,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAEpC;;;OAGG;IACH,SAAS,CAAC,IAAI,EAAE,IAAI,CAAA;IAEpB;;;OAGG;cACa,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrC;;;;OAIG;IACU,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,GAAE,MAAuB,GAAG,OAAO,CAAC,GAAG,CAAC;IAK3H;;;;;OAKG;IACI,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB,GAAE,OAAe,GAAG,IAAI;IAKjH;;;;OAIG;IACI,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAKxE;;;;OAIG;IACI,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;CAI3E"}
|
|
@@ -0,0 +1,98 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ServiceProxy = exports.BuildServiceProxy = void 0;
|
|
13
|
+
const lakutata_1 = require("lakutata");
|
|
14
|
+
const di_1 = require("lakutata/decorator/di");
|
|
15
|
+
const ServiceResponseCodec_1 = require("../lib/ServiceResponseCodec");
|
|
16
|
+
/**
|
|
17
|
+
* Build service proxy
|
|
18
|
+
* @param options
|
|
19
|
+
* @constructor
|
|
20
|
+
*/
|
|
21
|
+
const BuildServiceProxy = (options) => {
|
|
22
|
+
return {
|
|
23
|
+
class: ServiceProxy,
|
|
24
|
+
natsComponentName: options.natsComponentName,
|
|
25
|
+
serviceId: options.serviceId
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
exports.BuildServiceProxy = BuildServiceProxy;
|
|
29
|
+
/**
|
|
30
|
+
* Service proxy provider
|
|
31
|
+
*/
|
|
32
|
+
let ServiceProxy = class ServiceProxy extends lakutata_1.Provider {
|
|
33
|
+
/**
|
|
34
|
+
* Initializer
|
|
35
|
+
* @protected
|
|
36
|
+
*/
|
|
37
|
+
async init() {
|
|
38
|
+
this.nats = await this.app.getObject(this.natsComponentName);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Invoke service registered method by pattern
|
|
42
|
+
* @param input
|
|
43
|
+
* @param timeout
|
|
44
|
+
*/
|
|
45
|
+
async invoke(input, timeout = 60 * 60 * 1000) {
|
|
46
|
+
const response = await this.nats.request(this.serviceId, input, timeout);
|
|
47
|
+
return ServiceResponseCodec_1.ServiceResponseCodec.decode(response);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* On service event
|
|
51
|
+
* @param eventName
|
|
52
|
+
* @param listener
|
|
53
|
+
* @param onlySingleClientReceived
|
|
54
|
+
*/
|
|
55
|
+
on(eventName, listener, onlySingleClientReceived = false) {
|
|
56
|
+
this.nats.onServiceEvent(this.serviceId, eventName, listener, onlySingleClientReceived);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Once service event
|
|
61
|
+
* @param eventName
|
|
62
|
+
* @param listener
|
|
63
|
+
*/
|
|
64
|
+
once(eventName, listener) {
|
|
65
|
+
this.nats.onceServiceEvent(this.serviceId, eventName, listener);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Off service events
|
|
70
|
+
* @param eventName
|
|
71
|
+
* @param listener
|
|
72
|
+
*/
|
|
73
|
+
off(eventName, listener) {
|
|
74
|
+
this.nats.offServiceEvent(this.serviceId, eventName, listener);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
exports.ServiceProxy = ServiceProxy;
|
|
79
|
+
__decorate([
|
|
80
|
+
(0, di_1.Inject)(lakutata_1.Application),
|
|
81
|
+
__metadata("design:type", lakutata_1.Application
|
|
82
|
+
/**
|
|
83
|
+
* NATS registered component name
|
|
84
|
+
* @protected
|
|
85
|
+
*/
|
|
86
|
+
)
|
|
87
|
+
], ServiceProxy.prototype, "app", void 0);
|
|
88
|
+
__decorate([
|
|
89
|
+
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
|
|
90
|
+
__metadata("design:type", String)
|
|
91
|
+
], ServiceProxy.prototype, "natsComponentName", void 0);
|
|
92
|
+
__decorate([
|
|
93
|
+
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
|
|
94
|
+
__metadata("design:type", String)
|
|
95
|
+
], ServiceProxy.prototype, "serviceId", void 0);
|
|
96
|
+
exports.ServiceProxy = ServiceProxy = __decorate([
|
|
97
|
+
(0, di_1.Singleton)()
|
|
98
|
+
], ServiceProxy);
|
|
@@ -14,7 +14,7 @@ const entrypoint_1 = require("lakutata/com/entrypoint");
|
|
|
14
14
|
const di_1 = require("lakutata/decorator/di");
|
|
15
15
|
const CommonExports_1 = require("../CommonExports");
|
|
16
16
|
const ctrl_1 = require("lakutata/decorator/ctrl");
|
|
17
|
-
const
|
|
17
|
+
const ServiceProxy_1 = require("../providers/ServiceProxy");
|
|
18
18
|
class TestComponent extends lakutata_1.Component {
|
|
19
19
|
async init() {
|
|
20
20
|
// this.nats.subscribe('test-invoke', (msg) => {
|
|
@@ -29,13 +29,38 @@ class TestComponent extends lakutata_1.Component {
|
|
|
29
29
|
// this.nats.publish('test', 1234)
|
|
30
30
|
// console.log('res:', await this.nats.request('test-invoke', JSON.stringify({haha: true})), 1000000)
|
|
31
31
|
// }, 1)
|
|
32
|
-
console.log(await this.nats.request(this.app.appId, {
|
|
33
|
-
this.nats.subscribe('test', async (inp) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}, {
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
// console.log(await this.nats.request(this.app.appId, {test: true}))
|
|
33
|
+
// this.nats.subscribe('test', async (inp) => {
|
|
34
|
+
// console.log(inp)
|
|
35
|
+
// await Delay(1000)
|
|
36
|
+
// }, {iterator: true})
|
|
37
|
+
//
|
|
38
|
+
// for (let i = 0; i < 1000; i++) {
|
|
39
|
+
// this.nats.publish('test', i)
|
|
40
|
+
// }
|
|
41
|
+
const handler1 = async (data1, data2) => {
|
|
42
|
+
console.log('data1:', data1, data2);
|
|
43
|
+
// this.nats.offServiceEvent(this.app.appId, 'testEvt', handler1)
|
|
44
|
+
// this.self.off('testEvt', handler1)
|
|
45
|
+
};
|
|
46
|
+
const handler2 = async (data1, data2) => {
|
|
47
|
+
console.log('data2:', data1, data2);
|
|
48
|
+
// this.nats.offServiceEvent(this.app.appId, 'testEvt', handler1)
|
|
49
|
+
// this.nats.offServiceEvent(this.app.appId, 'testEvt')
|
|
50
|
+
};
|
|
51
|
+
this.self.on('testEvt', handler1);
|
|
52
|
+
this.self.on('testEvt', handler2);
|
|
53
|
+
// this.nats.onServiceEvent(this.app.appId, 'testEvt', handler1)
|
|
54
|
+
// this.nats.onServiceEvent(this.app.appId, 'testEvt', handler2)
|
|
55
|
+
setInterval(() => {
|
|
56
|
+
this.nats.emitServiceEvent('testEvt', 123, 456);
|
|
57
|
+
}, 1000);
|
|
58
|
+
try {
|
|
59
|
+
console.log(await this.self.invoke({ test: true }));
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
// console.error(JSON.parse(JSON.stringify(e)))
|
|
63
|
+
console.error(e);
|
|
39
64
|
}
|
|
40
65
|
}
|
|
41
66
|
}
|
|
@@ -47,8 +72,14 @@ __decorate([
|
|
|
47
72
|
(0, di_1.Inject)('nats'),
|
|
48
73
|
__metadata("design:type", CommonExports_1.NATS)
|
|
49
74
|
], TestComponent.prototype, "nats", void 0);
|
|
75
|
+
__decorate([
|
|
76
|
+
(0, di_1.Inject)('self'),
|
|
77
|
+
__metadata("design:type", ServiceProxy_1.ServiceProxy)
|
|
78
|
+
], TestComponent.prototype, "self", void 0);
|
|
50
79
|
class TestController extends entrypoint_1.Controller {
|
|
51
80
|
async test(inp) {
|
|
81
|
+
// throw new Error('fuck')
|
|
82
|
+
// throw new NatsForbiddenException('fuck')
|
|
52
83
|
// return 'hahahah'
|
|
53
84
|
return {
|
|
54
85
|
test: true,
|
|
@@ -86,6 +117,9 @@ lakutata_1.Application.run({
|
|
|
86
117
|
class: TestComponent
|
|
87
118
|
}
|
|
88
119
|
},
|
|
120
|
+
providers: {
|
|
121
|
+
self: (0, CommonExports_1.BuildServiceProxy)({ serviceId: 'test.app', natsComponentName: 'nats' })
|
|
122
|
+
},
|
|
89
123
|
bootstrap: [
|
|
90
124
|
'entrypoint',
|
|
91
125
|
'test'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lakutata/nats",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Lakutata NATS Client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lakutata",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"nats": "^2.29.3"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "9c0e81cd717624113ac0133ecff79cf2d64b7322"
|
|
35
35
|
}
|