@lakutata/nats 2.3.0 → 3.0.0-beta.0
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/LICENSE +23 -0
- package/README.md +288 -25
- package/dist/cjs/codecs/JSONCodec.d.ts +32 -0
- package/dist/cjs/codecs/JSONCodec.js +44 -0
- package/dist/cjs/codecs/MessagePackCodec.d.ts +22 -0
- package/dist/cjs/codecs/MessagePackCodec.js +35 -0
- package/dist/cjs/components/NATS.d.ts +376 -0
- package/dist/cjs/components/NATS.js +772 -0
- package/dist/cjs/entrypoints/NatsServiceEntrypoint.d.ts +102 -0
- package/dist/cjs/entrypoints/NatsServiceEntrypoint.js +127 -0
- package/dist/cjs/exceptions/NatsBadRequestException.d.ts +11 -0
- package/dist/cjs/exceptions/NatsBadRequestException.js +18 -0
- package/dist/cjs/exceptions/NatsBulkException.d.ts +19 -0
- package/dist/cjs/exceptions/NatsBulkException.js +26 -0
- package/dist/cjs/exceptions/NatsForbiddenException.d.ts +11 -0
- package/dist/cjs/exceptions/NatsForbiddenException.js +18 -0
- package/dist/cjs/exceptions/NatsInternalServerException.d.ts +12 -0
- package/dist/cjs/exceptions/NatsInternalServerException.js +19 -0
- package/dist/cjs/exceptions/NatsNoRespondersAvailableException.d.ts +12 -0
- package/dist/cjs/exceptions/NatsNoRespondersAvailableException.js +19 -0
- package/dist/cjs/exceptions/NatsNotFoundException.d.ts +11 -0
- package/dist/cjs/exceptions/NatsNotFoundException.js +18 -0
- package/dist/cjs/exceptions/NatsRequestTimeoutException.d.ts +11 -0
- package/dist/cjs/exceptions/NatsRequestTimeoutException.js +18 -0
- package/dist/cjs/exports/Nats.d.ts +18 -0
- package/dist/cjs/exports/Nats.js +39 -0
- package/dist/cjs/interfaces/NatsClientOptions.d.ts +106 -0
- package/dist/{lib → cjs/lib}/ServiceEventCodec.d.ts +0 -1
- package/dist/{lib → cjs/lib}/ServiceEventCodec.js +19 -9
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/providers/ServiceProxy.d.ts +210 -0
- package/dist/cjs/providers/ServiceProxy.js +274 -0
- package/dist/cjs/types/SubscribeOptions.d.ts +33 -0
- package/dist/esm/codecs/JSONCodec.js +41 -0
- package/dist/esm/codecs/MessagePackCodec.js +32 -0
- package/dist/esm/components/NATS.js +767 -0
- package/dist/esm/entrypoints/NatsServiceEntrypoint.js +123 -0
- package/dist/esm/exceptions/NatsBadRequestException.js +14 -0
- package/dist/esm/exceptions/NatsBulkException.js +22 -0
- package/dist/esm/exceptions/NatsForbiddenException.js +14 -0
- package/dist/esm/exceptions/NatsInternalServerException.js +15 -0
- package/dist/esm/exceptions/NatsNoRespondersAvailableException.js +15 -0
- package/dist/esm/exceptions/NatsNotFoundException.js +14 -0
- package/dist/esm/exceptions/NatsRequestTimeoutException.js +14 -0
- package/dist/esm/exports/Nats.js +15 -0
- package/dist/esm/interfaces/NatsClientOptions.js +1 -0
- package/dist/esm/lib/ServiceEventCodec.js +21 -0
- package/dist/esm/providers/ServiceProxy.js +270 -0
- package/dist/esm/types/SubscribeOptions.js +1 -0
- package/dist/types/codecs/JSONCodec.d.ts +32 -0
- package/dist/types/codecs/MessagePackCodec.d.ts +22 -0
- package/dist/types/components/NATS.d.ts +376 -0
- package/dist/types/entrypoints/NatsServiceEntrypoint.d.ts +102 -0
- package/dist/types/exceptions/NatsBadRequestException.d.ts +11 -0
- package/dist/types/exceptions/NatsBulkException.d.ts +19 -0
- package/dist/types/exceptions/NatsForbiddenException.d.ts +11 -0
- package/dist/types/exceptions/NatsInternalServerException.d.ts +12 -0
- package/dist/types/exceptions/NatsNoRespondersAvailableException.d.ts +12 -0
- package/dist/types/exceptions/NatsNotFoundException.d.ts +11 -0
- package/dist/types/exceptions/NatsRequestTimeoutException.d.ts +11 -0
- package/dist/types/exports/Nats.d.ts +18 -0
- package/dist/types/interfaces/NatsClientOptions.d.ts +106 -0
- package/dist/types/lib/ServiceEventCodec.d.ts +9 -0
- package/dist/types/providers/ServiceProxy.d.ts +210 -0
- package/dist/types/types/SubscribeOptions.d.ts +33 -0
- package/package.json +42 -33
- package/CHANGELOG.md +0 -288
- package/dist/CommonExports.d.ts +0 -19
- package/dist/CommonExports.d.ts.map +0 -1
- package/dist/CommonExports.js +0 -35
- package/dist/codecs/JSONCodec.d.ts +0 -3
- package/dist/codecs/JSONCodec.d.ts.map +0 -1
- package/dist/codecs/JSONCodec.js +0 -15
- package/dist/codecs/MessagePackCodec.d.ts +0 -3
- package/dist/codecs/MessagePackCodec.d.ts.map +0 -1
- package/dist/codecs/MessagePackCodec.js +0 -16
- package/dist/components/NATS.d.ts +0 -252
- package/dist/components/NATS.d.ts.map +0 -1
- package/dist/components/NATS.js +0 -564
- package/dist/entrypoints/SetupNatsServiceEntrypoint.d.ts +0 -8
- package/dist/entrypoints/SetupNatsServiceEntrypoint.d.ts.map +0 -1
- package/dist/entrypoints/SetupNatsServiceEntrypoint.js +0 -41
- package/dist/exceptions/NatsBadRequestException.d.ts +0 -7
- package/dist/exceptions/NatsBadRequestException.d.ts.map +0 -1
- package/dist/exceptions/NatsBadRequestException.js +0 -13
- package/dist/exceptions/NatsBulkException.d.ts +0 -11
- package/dist/exceptions/NatsBulkException.d.ts.map +0 -1
- package/dist/exceptions/NatsBulkException.js +0 -17
- package/dist/exceptions/NatsForbiddenException.d.ts +0 -7
- package/dist/exceptions/NatsForbiddenException.d.ts.map +0 -1
- package/dist/exceptions/NatsForbiddenException.js +0 -13
- package/dist/exceptions/NatsInternalServerException.d.ts +0 -7
- package/dist/exceptions/NatsInternalServerException.d.ts.map +0 -1
- package/dist/exceptions/NatsInternalServerException.js +0 -13
- package/dist/exceptions/NatsNoRespondersAvailableException.d.ts +0 -7
- package/dist/exceptions/NatsNoRespondersAvailableException.d.ts.map +0 -1
- package/dist/exceptions/NatsNoRespondersAvailableException.js +0 -13
- package/dist/exceptions/NatsNotFoundException.d.ts +0 -7
- package/dist/exceptions/NatsNotFoundException.d.ts.map +0 -1
- package/dist/exceptions/NatsNotFoundException.js +0 -13
- package/dist/exceptions/NatsRequestTimeoutException.d.ts +0 -7
- package/dist/exceptions/NatsRequestTimeoutException.d.ts.map +0 -1
- package/dist/exceptions/NatsRequestTimeoutException.js +0 -13
- package/dist/exceptions/ServiceInvokeException.d.ts +0 -6
- package/dist/exceptions/ServiceInvokeException.d.ts.map +0 -1
- package/dist/exceptions/ServiceInvokeException.js +0 -11
- package/dist/interfaces/NatsClientOptions.d.ts +0 -26
- package/dist/interfaces/NatsClientOptions.d.ts.map +0 -1
- package/dist/lib/ServiceEventCodec.d.ts.map +0 -1
- package/dist/lib/ServiceResponseCodec.d.ts +0 -22
- package/dist/lib/ServiceResponseCodec.d.ts.map +0 -1
- package/dist/lib/ServiceResponseCodec.js +0 -50
- package/dist/providers/ServiceProxy.d.ts +0 -85
- package/dist/providers/ServiceProxy.d.ts.map +0 -1
- package/dist/providers/ServiceProxy.js +0 -124
- package/dist/types/SubscribeOptions.d.ts +0 -12
- package/dist/types/SubscribeOptions.d.ts.map +0 -1
- package/docs/large-payload-design.md +0 -199
- package/tsconfig.json +0 -8
- /package/dist/{interfaces → cjs/interfaces}/NatsClientOptions.js +0 -0
- /package/dist/{types → cjs/types}/SubscribeOptions.js +0 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Codec } from 'nats';
|
|
2
|
+
/**
|
|
3
|
+
* The options of the {@link NATS} component, given to {@link buildNatsClientOptions}. They are validated when the
|
|
4
|
+
* component is created: an invalid value fails it.
|
|
5
|
+
*/
|
|
6
|
+
export interface NatsClientOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The NATS server, or the servers of a cluster, to connect to: `host:port` or a URL (`nats://user:pass@host:4222`).
|
|
9
|
+
* The client connects to one of them (in random order unless `noRandomize`) and moves to another on a disconnect.
|
|
10
|
+
*/
|
|
11
|
+
servers: string | string[];
|
|
12
|
+
/**
|
|
13
|
+
* The codec encoding and decoding every payload: {@link messagePackCodec}, {@link jsonCodec}, or any object with
|
|
14
|
+
* `encode(value)` and `decode(bytes)`. All the applications exchanging messages on a subject must use the same one.
|
|
15
|
+
* @default messagePackCodec()
|
|
16
|
+
*/
|
|
17
|
+
codec?: Codec<unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* The time to wait for the connection to a server to be established, in milliseconds.
|
|
20
|
+
* @default 20000
|
|
21
|
+
*/
|
|
22
|
+
timeout?: number;
|
|
23
|
+
/**
|
|
24
|
+
* The token of the server's token authentication.
|
|
25
|
+
* @default none
|
|
26
|
+
*/
|
|
27
|
+
token?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The user name of the server's user/password authentication.
|
|
30
|
+
* @default none
|
|
31
|
+
*/
|
|
32
|
+
user?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The password of the server's user/password authentication, with `user`.
|
|
35
|
+
* @default none
|
|
36
|
+
*/
|
|
37
|
+
pass?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Whether the client prints the protocol messages it sends and receives (to debug a connection).
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
debug?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The number of pings left unanswered after which the connection is considered stale and the client reconnects.
|
|
45
|
+
* @default 2
|
|
46
|
+
*/
|
|
47
|
+
maxPingOut?: number;
|
|
48
|
+
/**
|
|
49
|
+
* The number of reconnection attempts to each server before giving up, `-1` never giving up. Once given up, the
|
|
50
|
+
* connection is closed for good: the calls fail with the NATS client error `CONNECTION_CLOSED` until the application
|
|
51
|
+
* restarts.
|
|
52
|
+
* @default 10
|
|
53
|
+
*/
|
|
54
|
+
maxReconnectAttempts?: number;
|
|
55
|
+
/**
|
|
56
|
+
* The client name shown by the server's monitoring. It also names the default object store bucket of the bulk
|
|
57
|
+
* channel (`lkt-bulk-{name}`), and the bulk connection (`{name}-bulk`).
|
|
58
|
+
* @default none
|
|
59
|
+
*/
|
|
60
|
+
name?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Whether the messages this client publishes are never delivered to its own subscriptions.
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
65
|
+
noEcho?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Whether the servers are tried in the order given, instead of a random order.
|
|
68
|
+
* @default false
|
|
69
|
+
*/
|
|
70
|
+
noRandomize?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* The interval between the pings the client sends to check the connection, in milliseconds.
|
|
73
|
+
* @default 120000
|
|
74
|
+
*/
|
|
75
|
+
pingInterval?: number;
|
|
76
|
+
/**
|
|
77
|
+
* Whether the client reconnects after losing its connection (see `maxReconnectAttempts`). While it reconnects, the
|
|
78
|
+
* outgoing messages are buffered and the requests wait, failing when their timeout passes.
|
|
79
|
+
* @default true
|
|
80
|
+
*/
|
|
81
|
+
reconnect?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Whether the bulk channel is enabled: the payloads too large for the server's `max_payload` are relayed through a
|
|
84
|
+
* JetStream object store, using a second connection. It needs JetStream on the server; when the channel cannot
|
|
85
|
+
* start (no JetStream, bucket rejected…), a warning is printed and the component starts without it.
|
|
86
|
+
* @default true
|
|
87
|
+
*/
|
|
88
|
+
bulk?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* The object store bucket of the bulk channel, created when missing. The applications exchanging large payloads
|
|
91
|
+
* must use the same bucket: a reference to an object of another bucket fails with {@link NatsBulkException}.
|
|
92
|
+
* @default `lkt-bulk-{name}`, `lkt-bulk-default` without `name`
|
|
93
|
+
*/
|
|
94
|
+
bulkBucket?: string;
|
|
95
|
+
/**
|
|
96
|
+
* The time to live of the objects of the bulk channel, in milliseconds. An object is deleted once read; this removes
|
|
97
|
+
* the ones never read (a requester gone, a response lost). It applies when the bucket is created.
|
|
98
|
+
* @default 300000 (5 minutes)
|
|
99
|
+
*/
|
|
100
|
+
bulkTTL?: number;
|
|
101
|
+
/**
|
|
102
|
+
* The number of replicas of the bulk bucket in a JetStream cluster, applied when the bucket is created.
|
|
103
|
+
* @default none: the server's default (one replica)
|
|
104
|
+
*/
|
|
105
|
+
bulkReplicas?: number;
|
|
106
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { ActionPattern, Application, Provider, type ProviderOptionsBuilder } from '@lakutata/core';
|
|
2
|
+
import { NATS } from '../components/NATS.js';
|
|
3
|
+
/**
|
|
4
|
+
* The options of a {@link ServiceProxy}, given to {@link buildServiceProxy}.
|
|
5
|
+
*/
|
|
6
|
+
export type BuildServiceProxyOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the {@link NATS} component the proxy sends its requests through, as registered in the components.
|
|
9
|
+
*/
|
|
10
|
+
readonly natsComponentName: string;
|
|
11
|
+
/**
|
|
12
|
+
* The id of the remote service: the subject its {@link natsServiceEntrypoint} listens on, which is its application
|
|
13
|
+
* id (its `id` option) unless the entrypoint's `subject` option changes it. Its events are listened to under this id
|
|
14
|
+
* too, which only matches the application id.
|
|
15
|
+
*/
|
|
16
|
+
readonly serviceId: string;
|
|
17
|
+
/**
|
|
18
|
+
* The number of times a request nobody received (no responder: the service is not running or is restarting) is
|
|
19
|
+
* sent again before {@link NatsNoRespondersAvailableException} is thrown. The timeouts and the failures of the
|
|
20
|
+
* service are never retried, since the request may have been processed.
|
|
21
|
+
* @default 3
|
|
22
|
+
*/
|
|
23
|
+
readonly maxRetries?: number;
|
|
24
|
+
/**
|
|
25
|
+
* The delay before each retry, in milliseconds.
|
|
26
|
+
* @default 100
|
|
27
|
+
*/
|
|
28
|
+
readonly retryDelay?: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Build the provider options of a {@link ServiceProxy}, to register it in the `providers` of an application under the
|
|
32
|
+
* name of your choice (as in 2.x); declare one per remote service. The options are validated when the proxy is created,
|
|
33
|
+
* not here.
|
|
34
|
+
* @param options The NATS component to use, the id of the remote service, and the retries of the requests nobody
|
|
35
|
+
* received.
|
|
36
|
+
* @returns The provider options (`{class: ServiceProxy, ...options}`).
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import {Application} from 'lakutata'
|
|
40
|
+
* import {buildNatsClientOptions, buildServiceProxy, type ServiceProxy} from '@lakutata/nats'
|
|
41
|
+
*
|
|
42
|
+
* Application
|
|
43
|
+
* .run({
|
|
44
|
+
* id: 'shop.app',
|
|
45
|
+
* name: 'Shop',
|
|
46
|
+
* components: {
|
|
47
|
+
* nats: buildNatsClientOptions({servers: 'nats://127.0.0.1:4222'})
|
|
48
|
+
* },
|
|
49
|
+
* providers: {
|
|
50
|
+
* //The actions and the events of the application whose id is "order.service"
|
|
51
|
+
* orderService: buildServiceProxy({natsComponentName: 'nats', serviceId: 'order.service', maxRetries: 5})
|
|
52
|
+
* }
|
|
53
|
+
* })
|
|
54
|
+
* .onLaunched(async (app: Application): Promise<void> => {
|
|
55
|
+
* const orderService: ServiceProxy = await app.getObject('orderService')
|
|
56
|
+
* console.log(await orderService.invoke({cmd: 'order.get', id: 1}))
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare const buildServiceProxy: ProviderOptionsBuilder<BuildServiceProxyOptions>;
|
|
61
|
+
/**
|
|
62
|
+
* The proxy of a remote service served by {@link natsServiceEntrypoint}: it invokes the service's actions and listens to
|
|
63
|
+
* its events, over a {@link NATS} component. Register it in the `providers` of an application with
|
|
64
|
+
* {@link buildServiceProxy} (as in 2.x), one per remote service, and get it with `getObject()` or `@Inject`; it is a
|
|
65
|
+
* singleton provider, shared by the scopes. It resolves its NATS component when it is created (which fails when that
|
|
66
|
+
* component is not registered).
|
|
67
|
+
*
|
|
68
|
+
* An invocation is a NATS request to the service id, answered by one instance of the service (its queue group): the
|
|
69
|
+
* action's result is returned, the action's failure thrown as a `ServiceInvokeException` carrying the remote error's
|
|
70
|
+
* properties (`errno`, `statusCode`, `message`…). A request nobody received is sent again (`maxRetries`, `retryDelay`);
|
|
71
|
+
* a timeout is never retried, since the action may have run. Large requests and responses go through the bulk channel
|
|
72
|
+
* of the NATS component (see {@link NATS}).
|
|
73
|
+
*
|
|
74
|
+
* The event listeners registered through the proxy are removed when it is destroyed.
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* import {Application, Component} from 'lakutata'
|
|
78
|
+
* import {Inject} from 'lakutata/decorator/di'
|
|
79
|
+
* import {buildNatsClientOptions, buildServiceProxy, ServiceInvokeException, type ServiceProxy} from '@lakutata/nats'
|
|
80
|
+
*
|
|
81
|
+
* type Order = {id: number, total: number}
|
|
82
|
+
*
|
|
83
|
+
* class Billing extends Component {
|
|
84
|
+
* @Inject('orderService')
|
|
85
|
+
* protected readonly orderService: ServiceProxy
|
|
86
|
+
*
|
|
87
|
+
* public async total(id: number): Promise<number | undefined> {
|
|
88
|
+
* try {
|
|
89
|
+
* //The action of the order service matching {cmd: 'order.get'}, its input {id}; 30 seconds at most
|
|
90
|
+
* const order: Order = await this.orderService.invoke({cmd: 'order.get', id: id}, 30000)
|
|
91
|
+
* return order.total
|
|
92
|
+
* } catch (error) {
|
|
93
|
+
* //The action failed: the remote error's errno and statusCode are copied
|
|
94
|
+
* if (error instanceof ServiceInvokeException && error.statusCode === 404) return undefined
|
|
95
|
+
* //NatsNoRespondersAvailableException (not running, after the retries), NatsRequestTimeoutException…
|
|
96
|
+
* throw error
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* Application
|
|
102
|
+
* .run({
|
|
103
|
+
* id: 'billing.app',
|
|
104
|
+
* name: 'Billing',
|
|
105
|
+
* components: {
|
|
106
|
+
* nats: buildNatsClientOptions({servers: 'nats://127.0.0.1:4222'}),
|
|
107
|
+
* billing: {class: Billing}
|
|
108
|
+
* },
|
|
109
|
+
* providers: {
|
|
110
|
+
* orderService: buildServiceProxy({natsComponentName: 'nats', serviceId: 'order.service'})
|
|
111
|
+
* }
|
|
112
|
+
* })
|
|
113
|
+
* .onLaunched(async (app: Application): Promise<void> => {
|
|
114
|
+
* const orderService: ServiceProxy = await app.getObject('orderService')
|
|
115
|
+
* //The events emitted by the order service with NATS.emitServiceEvent('orderPaid', id, total)
|
|
116
|
+
* orderService.on('orderPaid', (id: number, total: number): void => console.log(`Order ${id} paid: ${total}`))
|
|
117
|
+
* const billing: Billing = await app.getObject('billing')
|
|
118
|
+
* console.log(await billing.total(1))
|
|
119
|
+
* })
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare class ServiceProxy extends Provider {
|
|
123
|
+
/**
|
|
124
|
+
* The application the proxy belongs to, to resolve its NATS component.
|
|
125
|
+
*/
|
|
126
|
+
protected readonly app: Application;
|
|
127
|
+
/**
|
|
128
|
+
* The name of the NATS component the proxy uses.
|
|
129
|
+
*/
|
|
130
|
+
protected readonly natsComponentName: string;
|
|
131
|
+
/**
|
|
132
|
+
* The id of the remote service: the subject of its requests and the id of its events.
|
|
133
|
+
*/
|
|
134
|
+
protected readonly serviceId: string;
|
|
135
|
+
/**
|
|
136
|
+
* The number of times a request nobody received is sent again; the timeouts and the failures of the service are
|
|
137
|
+
* never retried.
|
|
138
|
+
* @default 3
|
|
139
|
+
*/
|
|
140
|
+
protected readonly maxRetries: number;
|
|
141
|
+
/**
|
|
142
|
+
* The delay before each retry, in milliseconds.
|
|
143
|
+
* @default 100
|
|
144
|
+
*/
|
|
145
|
+
protected readonly retryDelay: number;
|
|
146
|
+
/**
|
|
147
|
+
* The NATS component, resolved when the proxy is created.
|
|
148
|
+
*/
|
|
149
|
+
protected nats: NATS;
|
|
150
|
+
/**
|
|
151
|
+
* The event listeners registered through this proxy, by event name, removed when it is destroyed.
|
|
152
|
+
*/
|
|
153
|
+
protected readonly eventListeners: Map<string, Map<(...args: any[]) => void, (...args: any[]) => unknown>>;
|
|
154
|
+
/**
|
|
155
|
+
* Resolve the NATS component.
|
|
156
|
+
* @throws {Error} When no component is registered under `natsComponentName`.
|
|
157
|
+
*/
|
|
158
|
+
protected init(): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* Remove the event listeners registered through this proxy.
|
|
161
|
+
*/
|
|
162
|
+
protected destroy(): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Record an event listener registered through this proxy, to remove it when the proxy is destroyed.
|
|
165
|
+
* @param eventName The event name.
|
|
166
|
+
* @param listener The listener.
|
|
167
|
+
*/
|
|
168
|
+
protected keepListener(eventName: string, listener: (...args: any[]) => void): ((...args: any[]) => unknown) | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* Invoke an action of the remote service and return its result.
|
|
171
|
+
* @param input The message: the action's pattern (as declared by its `@ServiceAction`) and its input, in one object
|
|
172
|
+
* (`{cmd: 'order.get', id: 1}`). It is encoded with the NATS component's codec (plain JSON data).
|
|
173
|
+
* @param timeout The time to wait for the response of each attempt, in milliseconds. A missing service fails at once
|
|
174
|
+
* (no responder), so it only bounds a request received and processed too slowly; `0` waits 600000 (10 minutes).
|
|
175
|
+
* @returns The action's result (the data of a `StatusResult`).
|
|
176
|
+
* @throws {ServiceInvokeException} When the action fails, or no action matches: its message is the remote error's, its
|
|
177
|
+
* `errno`, `statusCode` and other properties are copied from it, its `service` is the service id.
|
|
178
|
+
* @throws {NatsNoRespondersAvailableException} When nobody received the request, after `maxRetries` retries.
|
|
179
|
+
* @throws {NatsRequestTimeoutException} When no response arrives in time (never retried: the action may have run).
|
|
180
|
+
* @throws {NatsBulkException} When a large request cannot be stored, or a large response cannot be read.
|
|
181
|
+
* @throws {Error} The NATS client error `MAX_PAYLOAD_EXCEEDED` when a request too large is not relayed (see
|
|
182
|
+
* {@link NATS.request}), `CONNECTION_CLOSED` once the connection is closed.
|
|
183
|
+
*/
|
|
184
|
+
invoke<T extends Record<string, any>>(input: ActionPattern<T>, timeout?: number): Promise<any>;
|
|
185
|
+
/**
|
|
186
|
+
* Listen to an event of the remote service, emitted by its NATS component with `emitServiceEvent()`. A listener
|
|
187
|
+
* registered again for the same event on the same NATS component is ignored.
|
|
188
|
+
* @param eventName The event name.
|
|
189
|
+
* @param listener The listener, receiving the event's arguments.
|
|
190
|
+
* @param onlySingleClientReceived Whether each event goes to one of the listeners registered with `true` for it, across
|
|
191
|
+
* all the applications (a queue group, such as the instances of one service), instead of to each of them.
|
|
192
|
+
* @returns The proxy, for chaining.
|
|
193
|
+
*/
|
|
194
|
+
on(eventName: string, listener: (...args: any[]) => void, onlySingleClientReceived?: boolean): this;
|
|
195
|
+
/**
|
|
196
|
+
* Listen to the next occurrence of an event of the remote service only.
|
|
197
|
+
* @param eventName The event name.
|
|
198
|
+
* @param listener The listener, receiving the event's arguments.
|
|
199
|
+
* @returns The proxy, for chaining.
|
|
200
|
+
*/
|
|
201
|
+
once(eventName: string, listener: (...args: any[]) => void): this;
|
|
202
|
+
/**
|
|
203
|
+
* Stop listening to an event of the remote service.
|
|
204
|
+
* @param eventName The event name.
|
|
205
|
+
* @param listener The listener to remove; without it, every listener of the event added through this proxy (the
|
|
206
|
+
* listeners added through the other proxies or `NATS.onServiceEvent()` are kept).
|
|
207
|
+
* @returns The proxy, for chaining.
|
|
208
|
+
*/
|
|
209
|
+
off(eventName: string, listener?: (...args: any[]) => void): this;
|
|
210
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The options of {@link NATS.subscribe}.
|
|
3
|
+
*/
|
|
4
|
+
export type SubscribeOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* The queue group of the subscription: the subscriptions of one queue group on one subject share its messages, each
|
|
7
|
+
* message delivered to one of them only (load balancing between the instances of a service).
|
|
8
|
+
* @default none: every subscription receives every message
|
|
9
|
+
*/
|
|
10
|
+
queue?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The number of messages after which the subscription ends by itself (unsubscribed automatically).
|
|
13
|
+
* @default none: no limit
|
|
14
|
+
*/
|
|
15
|
+
max?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the messages are handled one after the other, each callback awaited before the next message, instead of
|
|
18
|
+
* concurrently (a callback started for each message as it arrives).
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
iterator?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Build the response to a request (a message with a reply subject) whose handling failed: the callback threw, the
|
|
24
|
+
* payload could not be decoded or read from the object store, or the response could not be encoded or sent. Its
|
|
25
|
+
* return value is encoded with the codec and sent (never relayed through the object store); `undefined` answers
|
|
26
|
+
* nothing, and the requester waits for its timeout. Without it, a failed request is not answered either.
|
|
27
|
+
* It lets an upper layer (such as the service entrypoint) answer with its own error protocol.
|
|
28
|
+
* @param error The error of the handling.
|
|
29
|
+
* @returns The payload of the response, or `undefined` to answer nothing.
|
|
30
|
+
* @default none: a failed request is not answered
|
|
31
|
+
*/
|
|
32
|
+
errorResponse?: (error: any) => any;
|
|
33
|
+
};
|
package/package.json
CHANGED
|
@@ -1,42 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lakutata/nats",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"url": "git+https://github.com/lakutata/lakutata-packages.git"
|
|
3
|
+
"version": "3.0.0-beta.0",
|
|
4
|
+
"description": "The NATS component of the lakutata framework: messaging, service proxies, service entrypoint and large payloads.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/cjs/exports/Nats.js",
|
|
7
|
+
"types": "./dist/cjs/exports/Nats.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/types/exports/Nats.d.ts",
|
|
12
|
+
"default": "./dist/esm/exports/Nats.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/cjs/exports/Nats.d.ts",
|
|
16
|
+
"default": "./dist/cjs/exports/Nats.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
21
20
|
},
|
|
22
21
|
"scripts": {
|
|
23
|
-
"clean": "
|
|
24
|
-
"build": "tsc",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
22
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', {recursive: true, force: true})\"",
|
|
23
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
24
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
25
|
+
"build:cjs-pkg": "node ../../scripts/package-build/cjs-package.mjs",
|
|
26
|
+
"build:js": "npm run build:cjs && npm run build:esm && npm run build:cjs-pkg",
|
|
27
|
+
"rebuild": "npm run clean && npm run build:js",
|
|
28
|
+
"compile": "npm run rebuild",
|
|
29
|
+
"test:unit": "npm run compile && node --test \"dist/esm/tests/unit/*.spec.js\"",
|
|
30
|
+
"test:integration": "npm run compile && node --test --test-concurrency=1 \"dist/esm/tests/integration/*.spec.js\""
|
|
32
31
|
},
|
|
33
32
|
"dependencies": {
|
|
34
|
-
"lakutata": "^2.0.121",
|
|
35
33
|
"nats": "^2.29.3"
|
|
36
34
|
},
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
}
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@lakutata/core": "3.0.0-beta.0",
|
|
37
|
+
"@lakutata/entrypoint": "3.0.0-beta.0",
|
|
38
|
+
"@lakutata/utils": "3.0.0-beta.0"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"!dist/*/tests",
|
|
46
|
+
"*.md",
|
|
47
|
+
"LICENSE",
|
|
48
|
+
"package.json",
|
|
49
|
+
"THIRD_PARTY_NOTICES.md"
|
|
50
|
+
]
|
|
42
51
|
}
|