@lakutata/nats 2.3.0 → 3.0.0-beta.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.
Files changed (121) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +288 -25
  3. package/dist/cjs/codecs/JSONCodec.d.ts +32 -0
  4. package/dist/cjs/codecs/JSONCodec.js +44 -0
  5. package/dist/cjs/codecs/MessagePackCodec.d.ts +22 -0
  6. package/dist/cjs/codecs/MessagePackCodec.js +35 -0
  7. package/dist/cjs/components/NATS.d.ts +376 -0
  8. package/dist/cjs/components/NATS.js +772 -0
  9. package/dist/cjs/entrypoints/NatsServiceEntrypoint.d.ts +102 -0
  10. package/dist/cjs/entrypoints/NatsServiceEntrypoint.js +127 -0
  11. package/dist/cjs/exceptions/NatsBadRequestException.d.ts +11 -0
  12. package/dist/cjs/exceptions/NatsBadRequestException.js +18 -0
  13. package/dist/cjs/exceptions/NatsBulkException.d.ts +19 -0
  14. package/dist/cjs/exceptions/NatsBulkException.js +26 -0
  15. package/dist/cjs/exceptions/NatsForbiddenException.d.ts +11 -0
  16. package/dist/cjs/exceptions/NatsForbiddenException.js +18 -0
  17. package/dist/cjs/exceptions/NatsInternalServerException.d.ts +12 -0
  18. package/dist/cjs/exceptions/NatsInternalServerException.js +19 -0
  19. package/dist/cjs/exceptions/NatsNoRespondersAvailableException.d.ts +12 -0
  20. package/dist/cjs/exceptions/NatsNoRespondersAvailableException.js +19 -0
  21. package/dist/cjs/exceptions/NatsNotFoundException.d.ts +11 -0
  22. package/dist/cjs/exceptions/NatsNotFoundException.js +18 -0
  23. package/dist/cjs/exceptions/NatsRequestTimeoutException.d.ts +11 -0
  24. package/dist/cjs/exceptions/NatsRequestTimeoutException.js +18 -0
  25. package/dist/cjs/exports/Nats.d.ts +18 -0
  26. package/dist/cjs/exports/Nats.js +39 -0
  27. package/dist/cjs/interfaces/NatsClientOptions.d.ts +106 -0
  28. package/dist/{lib → cjs/lib}/ServiceEventCodec.d.ts +0 -1
  29. package/dist/{lib → cjs/lib}/ServiceEventCodec.js +19 -9
  30. package/dist/cjs/package.json +1 -0
  31. package/dist/cjs/providers/ServiceProxy.d.ts +210 -0
  32. package/dist/cjs/providers/ServiceProxy.js +274 -0
  33. package/dist/cjs/types/SubscribeOptions.d.ts +33 -0
  34. package/dist/esm/codecs/JSONCodec.js +41 -0
  35. package/dist/esm/codecs/MessagePackCodec.js +32 -0
  36. package/dist/esm/components/NATS.js +767 -0
  37. package/dist/esm/entrypoints/NatsServiceEntrypoint.js +123 -0
  38. package/dist/esm/exceptions/NatsBadRequestException.js +14 -0
  39. package/dist/esm/exceptions/NatsBulkException.js +22 -0
  40. package/dist/esm/exceptions/NatsForbiddenException.js +14 -0
  41. package/dist/esm/exceptions/NatsInternalServerException.js +15 -0
  42. package/dist/esm/exceptions/NatsNoRespondersAvailableException.js +15 -0
  43. package/dist/esm/exceptions/NatsNotFoundException.js +14 -0
  44. package/dist/esm/exceptions/NatsRequestTimeoutException.js +14 -0
  45. package/dist/esm/exports/Nats.js +15 -0
  46. package/dist/esm/interfaces/NatsClientOptions.js +1 -0
  47. package/dist/esm/lib/ServiceEventCodec.js +21 -0
  48. package/dist/esm/providers/ServiceProxy.js +270 -0
  49. package/dist/esm/types/SubscribeOptions.js +1 -0
  50. package/dist/types/codecs/JSONCodec.d.ts +32 -0
  51. package/dist/types/codecs/MessagePackCodec.d.ts +22 -0
  52. package/dist/types/components/NATS.d.ts +376 -0
  53. package/dist/types/entrypoints/NatsServiceEntrypoint.d.ts +102 -0
  54. package/dist/types/exceptions/NatsBadRequestException.d.ts +11 -0
  55. package/dist/types/exceptions/NatsBulkException.d.ts +19 -0
  56. package/dist/types/exceptions/NatsForbiddenException.d.ts +11 -0
  57. package/dist/types/exceptions/NatsInternalServerException.d.ts +12 -0
  58. package/dist/types/exceptions/NatsNoRespondersAvailableException.d.ts +12 -0
  59. package/dist/types/exceptions/NatsNotFoundException.d.ts +11 -0
  60. package/dist/types/exceptions/NatsRequestTimeoutException.d.ts +11 -0
  61. package/dist/types/exports/Nats.d.ts +18 -0
  62. package/dist/types/interfaces/NatsClientOptions.d.ts +106 -0
  63. package/dist/types/lib/ServiceEventCodec.d.ts +9 -0
  64. package/dist/types/providers/ServiceProxy.d.ts +210 -0
  65. package/dist/types/types/SubscribeOptions.d.ts +33 -0
  66. package/package.json +42 -33
  67. package/CHANGELOG.md +0 -288
  68. package/dist/CommonExports.d.ts +0 -19
  69. package/dist/CommonExports.d.ts.map +0 -1
  70. package/dist/CommonExports.js +0 -35
  71. package/dist/codecs/JSONCodec.d.ts +0 -3
  72. package/dist/codecs/JSONCodec.d.ts.map +0 -1
  73. package/dist/codecs/JSONCodec.js +0 -15
  74. package/dist/codecs/MessagePackCodec.d.ts +0 -3
  75. package/dist/codecs/MessagePackCodec.d.ts.map +0 -1
  76. package/dist/codecs/MessagePackCodec.js +0 -16
  77. package/dist/components/NATS.d.ts +0 -252
  78. package/dist/components/NATS.d.ts.map +0 -1
  79. package/dist/components/NATS.js +0 -564
  80. package/dist/entrypoints/SetupNatsServiceEntrypoint.d.ts +0 -8
  81. package/dist/entrypoints/SetupNatsServiceEntrypoint.d.ts.map +0 -1
  82. package/dist/entrypoints/SetupNatsServiceEntrypoint.js +0 -41
  83. package/dist/exceptions/NatsBadRequestException.d.ts +0 -7
  84. package/dist/exceptions/NatsBadRequestException.d.ts.map +0 -1
  85. package/dist/exceptions/NatsBadRequestException.js +0 -13
  86. package/dist/exceptions/NatsBulkException.d.ts +0 -11
  87. package/dist/exceptions/NatsBulkException.d.ts.map +0 -1
  88. package/dist/exceptions/NatsBulkException.js +0 -17
  89. package/dist/exceptions/NatsForbiddenException.d.ts +0 -7
  90. package/dist/exceptions/NatsForbiddenException.d.ts.map +0 -1
  91. package/dist/exceptions/NatsForbiddenException.js +0 -13
  92. package/dist/exceptions/NatsInternalServerException.d.ts +0 -7
  93. package/dist/exceptions/NatsInternalServerException.d.ts.map +0 -1
  94. package/dist/exceptions/NatsInternalServerException.js +0 -13
  95. package/dist/exceptions/NatsNoRespondersAvailableException.d.ts +0 -7
  96. package/dist/exceptions/NatsNoRespondersAvailableException.d.ts.map +0 -1
  97. package/dist/exceptions/NatsNoRespondersAvailableException.js +0 -13
  98. package/dist/exceptions/NatsNotFoundException.d.ts +0 -7
  99. package/dist/exceptions/NatsNotFoundException.d.ts.map +0 -1
  100. package/dist/exceptions/NatsNotFoundException.js +0 -13
  101. package/dist/exceptions/NatsRequestTimeoutException.d.ts +0 -7
  102. package/dist/exceptions/NatsRequestTimeoutException.d.ts.map +0 -1
  103. package/dist/exceptions/NatsRequestTimeoutException.js +0 -13
  104. package/dist/exceptions/ServiceInvokeException.d.ts +0 -6
  105. package/dist/exceptions/ServiceInvokeException.d.ts.map +0 -1
  106. package/dist/exceptions/ServiceInvokeException.js +0 -11
  107. package/dist/interfaces/NatsClientOptions.d.ts +0 -26
  108. package/dist/interfaces/NatsClientOptions.d.ts.map +0 -1
  109. package/dist/lib/ServiceEventCodec.d.ts.map +0 -1
  110. package/dist/lib/ServiceResponseCodec.d.ts +0 -22
  111. package/dist/lib/ServiceResponseCodec.d.ts.map +0 -1
  112. package/dist/lib/ServiceResponseCodec.js +0 -50
  113. package/dist/providers/ServiceProxy.d.ts +0 -85
  114. package/dist/providers/ServiceProxy.d.ts.map +0 -1
  115. package/dist/providers/ServiceProxy.js +0 -124
  116. package/dist/types/SubscribeOptions.d.ts +0 -12
  117. package/dist/types/SubscribeOptions.d.ts.map +0 -1
  118. package/docs/large-payload-design.md +0 -199
  119. package/tsconfig.json +0 -8
  120. /package/dist/{interfaces → cjs/interfaces}/NatsClientOptions.js +0 -0
  121. /package/dist/{types → cjs/types}/SubscribeOptions.js +0 -0
@@ -0,0 +1,102 @@
1
+ import { ServiceContext, type EntrypointAdapter, type MiddlewareDefinition } from '@lakutata/entrypoint';
2
+ /**
3
+ * The options of {@link natsServiceEntrypoint}.
4
+ */
5
+ export type NatsServiceEntrypointOptions = {
6
+ /**
7
+ * The adapter name, to tell apart several service adapters of one entrypoint.
8
+ */
9
+ name?: string;
10
+ /**
11
+ * The subject the requests are received on: the `serviceId` the service proxies use. The events of
12
+ * `Entrypoint.broadcast()` are published under it; the ones of `NATS.emitServiceEvent()` under the application id.
13
+ * @default the application id (its `id` option)
14
+ */
15
+ subject?: string;
16
+ /**
17
+ * The queue group sharing the requests between the instances of the service: each request is handled by one of
18
+ * them.
19
+ * @default `{subject}.serviceQueue`
20
+ */
21
+ queue?: string;
22
+ /**
23
+ * The middlewares of this adapter's actions, run after the entrypoint's global middlewares.
24
+ */
25
+ middlewares?: MiddlewareDefinition[];
26
+ };
27
+ /**
28
+ * Create a service adapter of an entrypoint served over NATS: the requests sent to the application's id (by the
29
+ * {@link ServiceProxy} components of the other applications, or any NATS client) are dispatched to the controllers'
30
+ * service actions, through the {@link NATS} component registered under `natsComponentName`.
31
+ *
32
+ * A request's payload is the message matched against the `@ServiceAction` patterns: the action's pattern and its input
33
+ * in one object. It is answered with a service response `{success, error, payload}` (see `ServiceResponseCodec`):
34
+ * `payload` is the action's result, or `error` the properties of the error when the action failed (no matching action,
35
+ * validation, access denied…). A request whose payload cannot be decoded, or whose response cannot be sent, is answered
36
+ * with such an error too, so the requester does not wait for its timeout. A published message (without reply subject)
37
+ * runs the action without answer.
38
+ *
39
+ * The instances of the service share the requests through a queue group: each request is handled by one of them. Large
40
+ * requests and responses go through the NATS component's bulk channel. `Entrypoint.broadcast()` publishes its events
41
+ * under the entrypoint's subject, to the proxies of the service (it counts them 0: NATS does not tell how many receive
42
+ * them); `Entrypoint.notify()` does not reach it (NATS has no client ids). When the entrypoint stops, the subscription is drained: no request is received any more
43
+ * (the callers get no responder and retry), the requests in progress are answered.
44
+ * @param natsComponentName The name of the NATS component, as registered in the components; the entrypoint fails to
45
+ * start when it is not registered.
46
+ * @param options The subject, the queue group, the adapter name and its middlewares.
47
+ * @returns The adapter, to give to the `service` option of `buildEntrypoints`.
48
+ * @example
49
+ * ```typescript
50
+ * import {Application, DTO} from 'lakutata'
51
+ * import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
52
+ * import {ServiceAction} from 'lakutata/decorator/ctrl'
53
+ * import {Expect} from 'lakutata/decorator/dto'
54
+ * import {buildNatsClientOptions, natsServiceEntrypoint} from '@lakutata/nats'
55
+ *
56
+ * class GetOrderDTO extends DTO {
57
+ * @Expect(DTO.Number().integer().required())
58
+ * public id!: number
59
+ * }
60
+ *
61
+ * class OrderController extends Controller {
62
+ * //Invoked by orderService.invoke({cmd: 'order.get', id: 1}) in the other applications
63
+ * @ServiceAction({cmd: 'order.get'}, GetOrderDTO)
64
+ * public async get(input: GetOrderDTO): Promise<{ id: number, total: number }> {
65
+ * return {id: input.id, total: 42}
66
+ * }
67
+ * }
68
+ *
69
+ * Application.run({
70
+ * //The subject of the requests: the serviceId of the proxies
71
+ * id: 'order.service',
72
+ * name: 'OrderService',
73
+ * components: {
74
+ * nats: buildNatsClientOptions({servers: 'nats://127.0.0.1:4222'}),
75
+ * entrypoint: buildEntrypoints({
76
+ * controllers: [OrderController],
77
+ * service: natsServiceEntrypoint('nats')
78
+ * })
79
+ * },
80
+ * bootstrap: ['entrypoint']
81
+ * })
82
+ * ```
83
+ */
84
+ export declare function natsServiceEntrypoint(natsComponentName: string, options?: NatsServiceEntrypointOptions): EntrypointAdapter<ServiceContext>;
85
+ /**
86
+ * The former name of {@link natsServiceEntrypoint}, kept for the applications written for 2.x; it behaves the same.
87
+ * @param natsComponentName The name of the NATS component, as registered in the components.
88
+ * @param options The subject, the queue group, the adapter name and its middlewares.
89
+ * @returns The adapter, to give to the `service` option of `buildEntrypoints`.
90
+ * @example
91
+ * ```typescript
92
+ * import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
93
+ * import {setupNatsServiceEntrypoint} from '@lakutata/nats'
94
+ *
95
+ * class OrderController extends Controller {
96
+ * }
97
+ *
98
+ * //The same as natsServiceEntrypoint('nats')
99
+ * export const entrypoint = buildEntrypoints({controllers: [OrderController], service: setupNatsServiceEntrypoint('nats')})
100
+ * ```
101
+ */
102
+ export declare const setupNatsServiceEntrypoint: (natsComponentName: string, options?: NatsServiceEntrypointOptions) => EntrypointAdapter<ServiceContext>;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupNatsServiceEntrypoint = void 0;
4
+ exports.natsServiceEntrypoint = natsServiceEntrypoint;
5
+ const core_1 = require("@lakutata/core");
6
+ const entrypoint_1 = require("@lakutata/entrypoint");
7
+ const channel_1 = require("@lakutata/entrypoint/channel");
8
+ const ServiceEventCodec_js_1 = require("../lib/ServiceEventCodec.js");
9
+ /**
10
+ * Create a service adapter of an entrypoint served over NATS: the requests sent to the application's id (by the
11
+ * {@link ServiceProxy} components of the other applications, or any NATS client) are dispatched to the controllers'
12
+ * service actions, through the {@link NATS} component registered under `natsComponentName`.
13
+ *
14
+ * A request's payload is the message matched against the `@ServiceAction` patterns: the action's pattern and its input
15
+ * in one object. It is answered with a service response `{success, error, payload}` (see `ServiceResponseCodec`):
16
+ * `payload` is the action's result, or `error` the properties of the error when the action failed (no matching action,
17
+ * validation, access denied…). A request whose payload cannot be decoded, or whose response cannot be sent, is answered
18
+ * with such an error too, so the requester does not wait for its timeout. A published message (without reply subject)
19
+ * runs the action without answer.
20
+ *
21
+ * The instances of the service share the requests through a queue group: each request is handled by one of them. Large
22
+ * requests and responses go through the NATS component's bulk channel. `Entrypoint.broadcast()` publishes its events
23
+ * under the entrypoint's subject, to the proxies of the service (it counts them 0: NATS does not tell how many receive
24
+ * them); `Entrypoint.notify()` does not reach it (NATS has no client ids). When the entrypoint stops, the subscription is drained: no request is received any more
25
+ * (the callers get no responder and retry), the requests in progress are answered.
26
+ * @param natsComponentName The name of the NATS component, as registered in the components; the entrypoint fails to
27
+ * start when it is not registered.
28
+ * @param options The subject, the queue group, the adapter name and its middlewares.
29
+ * @returns The adapter, to give to the `service` option of `buildEntrypoints`.
30
+ * @example
31
+ * ```typescript
32
+ * import {Application, DTO} from 'lakutata'
33
+ * import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
34
+ * import {ServiceAction} from 'lakutata/decorator/ctrl'
35
+ * import {Expect} from 'lakutata/decorator/dto'
36
+ * import {buildNatsClientOptions, natsServiceEntrypoint} from '@lakutata/nats'
37
+ *
38
+ * class GetOrderDTO extends DTO {
39
+ * @Expect(DTO.Number().integer().required())
40
+ * public id!: number
41
+ * }
42
+ *
43
+ * class OrderController extends Controller {
44
+ * //Invoked by orderService.invoke({cmd: 'order.get', id: 1}) in the other applications
45
+ * @ServiceAction({cmd: 'order.get'}, GetOrderDTO)
46
+ * public async get(input: GetOrderDTO): Promise<{ id: number, total: number }> {
47
+ * return {id: input.id, total: 42}
48
+ * }
49
+ * }
50
+ *
51
+ * Application.run({
52
+ * //The subject of the requests: the serviceId of the proxies
53
+ * id: 'order.service',
54
+ * name: 'OrderService',
55
+ * components: {
56
+ * nats: buildNatsClientOptions({servers: 'nats://127.0.0.1:4222'}),
57
+ * entrypoint: buildEntrypoints({
58
+ * controllers: [OrderController],
59
+ * service: natsServiceEntrypoint('nats')
60
+ * })
61
+ * },
62
+ * bootstrap: ['entrypoint']
63
+ * })
64
+ * ```
65
+ */
66
+ function natsServiceEntrypoint(natsComponentName, options = {}) {
67
+ let subscription;
68
+ let nats;
69
+ let subject;
70
+ return {
71
+ channel: 'service',
72
+ definition: channel_1.ServiceChannel,
73
+ name: options.name,
74
+ middlewares: options.middlewares,
75
+ start: async (runtime) => {
76
+ nats = await runtime.module.getObject(natsComponentName);
77
+ subject = options.subject ?? core_1.Application.appId;
78
+ subscription = nats.subscribe(subject, async (payload) => {
79
+ try {
80
+ return channel_1.ServiceResponseCodec.encode(await runtime.dispatch(new entrypoint_1.ServiceContext({ data: payload })), false);
81
+ }
82
+ catch (e) {
83
+ return channel_1.ServiceResponseCodec.encodeError(e);
84
+ }
85
+ }, {
86
+ queue: options.queue ?? `${subject}.serviceQueue`,
87
+ //The failures outside of the dispatch (a payload which cannot be decoded, a response which cannot be encoded)
88
+ //are answered too, so the requester does not wait for its timeout
89
+ errorResponse: (error) => channel_1.ServiceResponseCodec.encodeError(error)
90
+ });
91
+ },
92
+ //The events published under the subject of the entrypoint, to the proxies of its service (their serviceId); NATS
93
+ //does not tell how many receive them: counted 0. NATS has no client ids: notify() pushes to none
94
+ broadcast: (event, args) => {
95
+ if (nats && subject)
96
+ nats.publish(ServiceEventCodec_js_1.ServiceEventCodec.formatSubject(subject, event), ServiceEventCodec_js_1.ServiceEventCodec.encode(args));
97
+ return 0;
98
+ },
99
+ stop: async () => {
100
+ //No request is received any more, the requests in progress are awaited by the entrypoint
101
+ try {
102
+ await subscription?.drain();
103
+ }
104
+ catch {
105
+ //The connection is already closed
106
+ }
107
+ }
108
+ };
109
+ }
110
+ /**
111
+ * The former name of {@link natsServiceEntrypoint}, kept for the applications written for 2.x; it behaves the same.
112
+ * @param natsComponentName The name of the NATS component, as registered in the components.
113
+ * @param options The subject, the queue group, the adapter name and its middlewares.
114
+ * @returns The adapter, to give to the `service` option of `buildEntrypoints`.
115
+ * @example
116
+ * ```typescript
117
+ * import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
118
+ * import {setupNatsServiceEntrypoint} from '@lakutata/nats'
119
+ *
120
+ * class OrderController extends Controller {
121
+ * }
122
+ *
123
+ * //The same as natsServiceEntrypoint('nats')
124
+ * export const entrypoint = buildEntrypoints({controllers: [OrderController], service: setupNatsServiceEntrypoint('nats')})
125
+ * ```
126
+ */
127
+ exports.setupNatsServiceEntrypoint = natsServiceEntrypoint;
@@ -0,0 +1,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
4
+ * whose code is `400` (a bad request). The client reports such codes for some JetStream operations, not for the core
5
+ * requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_BAD_REQUEST`, its `statusCode` 400.
6
+ */
7
+ export declare class NatsBadRequestException extends Exception {
8
+ errno: string | number;
9
+ message: string;
10
+ statusCode: number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsBadRequestException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
7
+ * whose code is `400` (a bad request). The client reports such codes for some JetStream operations, not for the core
8
+ * requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_BAD_REQUEST`, its `statusCode` 400.
9
+ */
10
+ class NatsBadRequestException extends core_1.Exception {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.errno = 'E_NATS_BAD_REQUEST';
14
+ this.message = 'Bad Request';
15
+ this.statusCode = 400;
16
+ }
17
+ }
18
+ exports.NatsBadRequestException = NatsBadRequestException;
@@ -0,0 +1,19 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * A failure of the bulk channel, which relays the large payloads through a JetStream object store; its message tells
4
+ * the cause:
5
+ * - `bulk channel is not enabled`: `putBulk()` or `getBulk()` called, or a reference received, while the channel is
6
+ * disabled (the `bulk` option is `false`, or the channel failed to start);
7
+ * - `bulk put failed: …` / `bulk get failed: …`: the object store rejected the write or the read;
8
+ * - `bulk object not found: <id>`: the referenced object is gone (its time to live passed) or is in another bucket
9
+ * (the peers do not share their `bulkBucket`).
10
+ *
11
+ * `NATS.request()` and `ServiceProxy.invoke()` throw it when the large request cannot be stored or the large response
12
+ * cannot be read; `ServiceProxy.invoke()` never retries it, since the service may have processed the request. On the
13
+ * service side, the failure is answered to the requester, which receives a `ServiceInvokeException` whose `errno` is
14
+ * `E_NATS_BULK`. Its `errno` is `E_NATS_BULK`, its `statusCode` 502.
15
+ */
16
+ export declare class NatsBulkException extends Exception {
17
+ errno: string | number;
18
+ statusCode: number;
19
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsBulkException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * A failure of the bulk channel, which relays the large payloads through a JetStream object store; its message tells
7
+ * the cause:
8
+ * - `bulk channel is not enabled`: `putBulk()` or `getBulk()` called, or a reference received, while the channel is
9
+ * disabled (the `bulk` option is `false`, or the channel failed to start);
10
+ * - `bulk put failed: …` / `bulk get failed: …`: the object store rejected the write or the read;
11
+ * - `bulk object not found: <id>`: the referenced object is gone (its time to live passed) or is in another bucket
12
+ * (the peers do not share their `bulkBucket`).
13
+ *
14
+ * `NATS.request()` and `ServiceProxy.invoke()` throw it when the large request cannot be stored or the large response
15
+ * cannot be read; `ServiceProxy.invoke()` never retries it, since the service may have processed the request. On the
16
+ * service side, the failure is answered to the requester, which receives a `ServiceInvokeException` whose `errno` is
17
+ * `E_NATS_BULK`. Its `errno` is `E_NATS_BULK`, its `statusCode` 502.
18
+ */
19
+ class NatsBulkException extends core_1.Exception {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.errno = 'E_NATS_BULK';
23
+ this.statusCode = 502;
24
+ }
25
+ }
26
+ exports.NatsBulkException = NatsBulkException;
@@ -0,0 +1,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
4
+ * whose code is `403` (a forbidden request). The client reports such codes for some JetStream operations, not for the
5
+ * core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_FORBIDDEN`, its `statusCode` 403.
6
+ */
7
+ export declare class NatsForbiddenException extends Exception {
8
+ errno: string | number;
9
+ message: string;
10
+ statusCode: number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsForbiddenException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
7
+ * whose code is `403` (a forbidden request). The client reports such codes for some JetStream operations, not for the
8
+ * core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_FORBIDDEN`, its `statusCode` 403.
9
+ */
10
+ class NatsForbiddenException extends core_1.Exception {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.errno = 'E_NATS_FORBIDDEN';
14
+ this.message = 'Forbidden';
15
+ this.statusCode = 403;
16
+ }
17
+ }
18
+ exports.NatsForbiddenException = NatsForbiddenException;
@@ -0,0 +1,12 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
4
+ * whose code is `500` (an internal error of the server). The client reports such codes for some JetStream operations,
5
+ * not for the core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_INTERNAL_SERVER`, its
6
+ * `statusCode` 500.
7
+ */
8
+ export declare class NatsInternalServerException extends Exception {
9
+ errno: string | number;
10
+ message: string;
11
+ statusCode: number;
12
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsInternalServerException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
7
+ * whose code is `500` (an internal error of the server). The client reports such codes for some JetStream operations,
8
+ * not for the core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_INTERNAL_SERVER`, its
9
+ * `statusCode` 500.
10
+ */
11
+ class NatsInternalServerException extends core_1.Exception {
12
+ constructor() {
13
+ super(...arguments);
14
+ this.errno = 'E_NATS_INTERNAL_SERVER';
15
+ this.message = 'Internal Server Error';
16
+ this.statusCode = 500;
17
+ }
18
+ }
19
+ exports.NatsInternalServerException = NatsInternalServerException;
@@ -0,0 +1,12 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` when no subscription receives the request: nobody listens on the subject (the service is
4
+ * not running, or is stopping). It is thrown at once, without waiting for the timeout, and the request was not
5
+ * processed: `ServiceProxy.invoke()` retries it (`maxRetries` times) before throwing it. Its `errno` is
6
+ * `E_NATS_NO_RESPONDERS_AVAILABLE`, its `statusCode` 503.
7
+ */
8
+ export declare class NatsNoRespondersAvailableException extends Exception {
9
+ errno: string | number;
10
+ message: string;
11
+ statusCode: number;
12
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsNoRespondersAvailableException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` when no subscription receives the request: nobody listens on the subject (the service is
7
+ * not running, or is stopping). It is thrown at once, without waiting for the timeout, and the request was not
8
+ * processed: `ServiceProxy.invoke()` retries it (`maxRetries` times) before throwing it. Its `errno` is
9
+ * `E_NATS_NO_RESPONDERS_AVAILABLE`, its `statusCode` 503.
10
+ */
11
+ class NatsNoRespondersAvailableException extends core_1.Exception {
12
+ constructor() {
13
+ super(...arguments);
14
+ this.errno = 'E_NATS_NO_RESPONDERS_AVAILABLE';
15
+ this.message = 'No Responders Available';
16
+ this.statusCode = 503;
17
+ }
18
+ }
19
+ exports.NatsNoRespondersAvailableException = NatsNoRespondersAvailableException;
@@ -0,0 +1,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
4
+ * whose code is `404` (a subject not found). The client reports such codes for some JetStream operations, not for the
5
+ * core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_NOT_FOUND`, its `statusCode` 404.
6
+ */
7
+ export declare class NatsNotFoundException extends Exception {
8
+ errno: string | number;
9
+ message: string;
10
+ statusCode: number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsNotFoundException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when the request fails with an error of the NATS client
7
+ * whose code is `404` (a subject not found). The client reports such codes for some JetStream operations, not for the
8
+ * core requests the component sends, so it is rarely seen. Its `errno` is `E_NATS_NOT_FOUND`, its `statusCode` 404.
9
+ */
10
+ class NatsNotFoundException extends core_1.Exception {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.errno = 'E_NATS_NOT_FOUND';
14
+ this.message = 'Subject Not Found';
15
+ this.statusCode = 404;
16
+ }
17
+ }
18
+ exports.NatsNotFoundException = NatsNotFoundException;
@@ -0,0 +1,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when no response arrives within the request's timeout.
4
+ * The request may have been received and processed, its response lost or late: it is never retried. Its `errno` is
5
+ * `E_NATS_REQUEST_TIMEOUT`, its `statusCode` 408.
6
+ */
7
+ export declare class NatsRequestTimeoutException extends Exception {
8
+ errno: string | number;
9
+ message: string;
10
+ statusCode: number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsRequestTimeoutException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown by `NATS.request()` (and so `ServiceProxy.invoke()`) when no response arrives within the request's timeout.
7
+ * The request may have been received and processed, its response lost or late: it is never retried. Its `errno` is
8
+ * `E_NATS_REQUEST_TIMEOUT`, its `statusCode` 408.
9
+ */
10
+ class NatsRequestTimeoutException extends core_1.Exception {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.errno = 'E_NATS_REQUEST_TIMEOUT';
14
+ this.message = 'Request Timeout';
15
+ this.statusCode = 408;
16
+ }
17
+ }
18
+ exports.NatsRequestTimeoutException = NatsRequestTimeoutException;
@@ -0,0 +1,18 @@
1
+ export { natsServiceEntrypoint, setupNatsServiceEntrypoint, type NatsServiceEntrypointOptions } from '../entrypoints/NatsServiceEntrypoint.js';
2
+ export { ServiceProxy, buildServiceProxy, type BuildServiceProxyOptions } from '../providers/ServiceProxy.js';
3
+ export { RetentionPolicy } from 'nats';
4
+ export { NATS, buildNatsClientOptions } from '../components/NATS.js';
5
+ export { jsonCodec } from '../codecs/JSONCodec.js';
6
+ export { messagePackCodec } from '../codecs/MessagePackCodec.js';
7
+ export { StringCodec } from 'nats';
8
+ export type { SubscribeOptions } from '../types/SubscribeOptions.js';
9
+ export type { Subscription, Codec } from 'nats';
10
+ export type { NatsClientOptions } from '../interfaces/NatsClientOptions.js';
11
+ export { NatsBadRequestException } from '../exceptions/NatsBadRequestException.js';
12
+ export { NatsForbiddenException } from '../exceptions/NatsForbiddenException.js';
13
+ export { NatsInternalServerException } from '../exceptions/NatsInternalServerException.js';
14
+ export { NatsNoRespondersAvailableException } from '../exceptions/NatsNoRespondersAvailableException.js';
15
+ export { NatsNotFoundException } from '../exceptions/NatsNotFoundException.js';
16
+ export { NatsRequestTimeoutException } from '../exceptions/NatsRequestTimeoutException.js';
17
+ export { ServiceInvokeException } from '@lakutata/entrypoint';
18
+ export { NatsBulkException } from '../exceptions/NatsBulkException.js';
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsBulkException = exports.ServiceInvokeException = exports.NatsRequestTimeoutException = exports.NatsNotFoundException = exports.NatsNoRespondersAvailableException = exports.NatsInternalServerException = exports.NatsForbiddenException = exports.NatsBadRequestException = exports.StringCodec = exports.messagePackCodec = exports.jsonCodec = exports.buildNatsClientOptions = exports.NATS = exports.RetentionPolicy = exports.buildServiceProxy = exports.ServiceProxy = exports.setupNatsServiceEntrypoint = exports.natsServiceEntrypoint = void 0;
4
+ var NatsServiceEntrypoint_js_1 = require("../entrypoints/NatsServiceEntrypoint.js");
5
+ Object.defineProperty(exports, "natsServiceEntrypoint", { enumerable: true, get: function () { return NatsServiceEntrypoint_js_1.natsServiceEntrypoint; } });
6
+ Object.defineProperty(exports, "setupNatsServiceEntrypoint", { enumerable: true, get: function () { return NatsServiceEntrypoint_js_1.setupNatsServiceEntrypoint; } });
7
+ var ServiceProxy_js_1 = require("../providers/ServiceProxy.js");
8
+ Object.defineProperty(exports, "ServiceProxy", { enumerable: true, get: function () { return ServiceProxy_js_1.ServiceProxy; } });
9
+ Object.defineProperty(exports, "buildServiceProxy", { enumerable: true, get: function () { return ServiceProxy_js_1.buildServiceProxy; } });
10
+ var nats_1 = require("nats");
11
+ Object.defineProperty(exports, "RetentionPolicy", { enumerable: true, get: function () { return nats_1.RetentionPolicy; } });
12
+ var NATS_js_1 = require("../components/NATS.js");
13
+ Object.defineProperty(exports, "NATS", { enumerable: true, get: function () { return NATS_js_1.NATS; } });
14
+ Object.defineProperty(exports, "buildNatsClientOptions", { enumerable: true, get: function () { return NATS_js_1.buildNatsClientOptions; } });
15
+ var JSONCodec_js_1 = require("../codecs/JSONCodec.js");
16
+ Object.defineProperty(exports, "jsonCodec", { enumerable: true, get: function () { return JSONCodec_js_1.jsonCodec; } });
17
+ var MessagePackCodec_js_1 = require("../codecs/MessagePackCodec.js");
18
+ Object.defineProperty(exports, "messagePackCodec", { enumerable: true, get: function () { return MessagePackCodec_js_1.messagePackCodec; } });
19
+ var nats_2 = require("nats");
20
+ Object.defineProperty(exports, "StringCodec", { enumerable: true, get: function () { return nats_2.StringCodec; } });
21
+ var NatsBadRequestException_js_1 = require("../exceptions/NatsBadRequestException.js");
22
+ Object.defineProperty(exports, "NatsBadRequestException", { enumerable: true, get: function () { return NatsBadRequestException_js_1.NatsBadRequestException; } });
23
+ var NatsForbiddenException_js_1 = require("../exceptions/NatsForbiddenException.js");
24
+ Object.defineProperty(exports, "NatsForbiddenException", { enumerable: true, get: function () { return NatsForbiddenException_js_1.NatsForbiddenException; } });
25
+ var NatsInternalServerException_js_1 = require("../exceptions/NatsInternalServerException.js");
26
+ Object.defineProperty(exports, "NatsInternalServerException", { enumerable: true, get: function () { return NatsInternalServerException_js_1.NatsInternalServerException; } });
27
+ var NatsNoRespondersAvailableException_js_1 = require("../exceptions/NatsNoRespondersAvailableException.js");
28
+ Object.defineProperty(exports, "NatsNoRespondersAvailableException", { enumerable: true, get: function () { return NatsNoRespondersAvailableException_js_1.NatsNoRespondersAvailableException; } });
29
+ var NatsNotFoundException_js_1 = require("../exceptions/NatsNotFoundException.js");
30
+ Object.defineProperty(exports, "NatsNotFoundException", { enumerable: true, get: function () { return NatsNotFoundException_js_1.NatsNotFoundException; } });
31
+ var NatsRequestTimeoutException_js_1 = require("../exceptions/NatsRequestTimeoutException.js");
32
+ Object.defineProperty(exports, "NatsRequestTimeoutException", { enumerable: true, get: function () { return NatsRequestTimeoutException_js_1.NatsRequestTimeoutException; } });
33
+ var entrypoint_1 = require("@lakutata/entrypoint");
34
+ Object.defineProperty(exports, "ServiceInvokeException", { enumerable: true, get: function () { return entrypoint_1.ServiceInvokeException; } });
35
+ var NatsBulkException_js_1 = require("../exceptions/NatsBulkException.js");
36
+ Object.defineProperty(exports, "NatsBulkException", { enumerable: true, get: function () { return NatsBulkException_js_1.NatsBulkException; } });
37
+
38
+ //The default of the entry: the entry itself (scripts/package-build/cjs-package.mjs)
39
+ if (!Object.prototype.hasOwnProperty.call(exports, 'default')) Object.defineProperty(exports, 'default', {value: exports})
@@ -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
+ }
@@ -7,4 +7,3 @@ export declare class ServiceEventCodec {
7
7
  static decode(eventData: ServiceEventData): any[];
8
8
  static formatSubject(serviceId: string, eventName: string): string;
9
9
  }
10
- //# sourceMappingURL=ServiceEventCodec.d.ts.map