@camera.ui/rpc 1.0.3 → 1.0.4
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/externals/nats.js/core/src/authenticator.ts +159 -0
- package/externals/nats.js/core/src/bench.ts +426 -0
- package/externals/nats.js/core/src/codec.ts +28 -0
- package/externals/nats.js/core/src/core.ts +1219 -0
- package/externals/nats.js/core/src/databuffer.ts +129 -0
- package/externals/nats.js/core/src/denobuffer.ts +248 -0
- package/externals/nats.js/core/src/encoders.ts +53 -0
- package/externals/nats.js/core/src/errors.ts +300 -0
- package/externals/nats.js/core/src/headers.ts +315 -0
- package/externals/nats.js/core/src/heartbeats.ts +114 -0
- package/externals/nats.js/core/src/idleheartbeat_monitor.ts +140 -0
- package/externals/nats.js/core/src/internal_mod.ts +167 -0
- package/externals/nats.js/core/src/ipparser.ts +215 -0
- package/externals/nats.js/core/src/mod.ts +113 -0
- package/externals/nats.js/core/src/msg.ts +120 -0
- package/externals/nats.js/core/src/muxsubscription.ts +111 -0
- package/externals/nats.js/core/src/nats.ts +650 -0
- package/externals/nats.js/core/src/nkeys.ts +1 -0
- package/externals/nats.js/core/src/nuid.ts +16 -0
- package/externals/nats.js/core/src/options.ts +202 -0
- package/externals/nats.js/core/src/parser.ts +756 -0
- package/externals/nats.js/core/src/protocol.ts +1304 -0
- package/externals/nats.js/core/src/queued_iterator.ts +171 -0
- package/externals/nats.js/core/src/request.ts +177 -0
- package/externals/nats.js/core/src/semver.ts +165 -0
- package/externals/nats.js/core/src/servers.ts +424 -0
- package/externals/nats.js/core/src/transport.ts +117 -0
- package/externals/nats.js/core/src/types.ts +17 -0
- package/externals/nats.js/core/src/util.ts +367 -0
- package/externals/nats.js/core/src/version.ts +2 -0
- package/externals/nats.js/core/src/ws_transport.ts +391 -0
- package/package.json +2 -1
|
@@ -0,0 +1,1219 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 The NATS Authors
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { nuid } from "./nuid.ts";
|
|
17
|
+
import { InvalidArgumentError } from "./errors.ts";
|
|
18
|
+
|
|
19
|
+
export type DisconnectStatus = {
|
|
20
|
+
type: "disconnect";
|
|
21
|
+
server: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type ReconnectStatus = {
|
|
25
|
+
type: "reconnect";
|
|
26
|
+
server: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type ReconnectingStatus = {
|
|
30
|
+
type: "reconnecting";
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type ClusterUpdateStatus = {
|
|
34
|
+
type: "update";
|
|
35
|
+
added?: string[];
|
|
36
|
+
deleted?: string[];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type LDMStatus = {
|
|
40
|
+
type: "ldm";
|
|
41
|
+
server: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type ServerErrorStatus = {
|
|
45
|
+
type: "error";
|
|
46
|
+
error: Error;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ClientPingStatus = {
|
|
50
|
+
type: "ping";
|
|
51
|
+
pendingPings: number;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type StaleConnectionStatus = {
|
|
55
|
+
type: "staleConnection";
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type ForceReconnectStatus = {
|
|
59
|
+
type: "forceReconnect";
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type SlowConsumerStatus = {
|
|
63
|
+
type: "slowConsumer";
|
|
64
|
+
sub: Subscription;
|
|
65
|
+
pending: number;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type CloseStatus = {
|
|
69
|
+
type: "close";
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type Status =
|
|
73
|
+
| DisconnectStatus
|
|
74
|
+
| ReconnectStatus
|
|
75
|
+
| ReconnectingStatus
|
|
76
|
+
| ClusterUpdateStatus
|
|
77
|
+
| LDMStatus
|
|
78
|
+
| ServerErrorStatus
|
|
79
|
+
| ClientPingStatus
|
|
80
|
+
| StaleConnectionStatus
|
|
81
|
+
| SlowConsumerStatus
|
|
82
|
+
| ForceReconnectStatus
|
|
83
|
+
| CloseStatus;
|
|
84
|
+
|
|
85
|
+
export type MsgCallback<T> = (
|
|
86
|
+
err: Error | null,
|
|
87
|
+
msg: T,
|
|
88
|
+
) => void | Promise<never>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Subscription Options
|
|
92
|
+
*/
|
|
93
|
+
export interface SubOpts<T> {
|
|
94
|
+
/**
|
|
95
|
+
* Optional queue name (subscriptions on the same subject that use queues
|
|
96
|
+
* are horizontally load balanced when part of the same queue).
|
|
97
|
+
*/
|
|
98
|
+
queue?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Optional maximum number of messages to deliver to the subscription
|
|
101
|
+
* before it is auto-unsubscribed.
|
|
102
|
+
*/
|
|
103
|
+
max?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Optional maximum number of milliseconds before a timer raises an error. This
|
|
106
|
+
* useful to monitor a subscription that is expected to yield messages.
|
|
107
|
+
* The timer is cancelled when the first message is received by the subscription.
|
|
108
|
+
*/
|
|
109
|
+
timeout?: number;
|
|
110
|
+
/**
|
|
111
|
+
* An optional function that will handle messages. Typically, messages
|
|
112
|
+
* are processed via an async iterator on the subscription. If this
|
|
113
|
+
* option is provided, messages are processed by the specified function.
|
|
114
|
+
* @param err
|
|
115
|
+
* @param msg
|
|
116
|
+
*/
|
|
117
|
+
callback?: MsgCallback<T>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Number of pending messages in a subscription to exceed prior to considering
|
|
121
|
+
* a subscription a Slow Consumer. By default, slow consumer is on a subscription
|
|
122
|
+
* is not accounted for.
|
|
123
|
+
*
|
|
124
|
+
* This is an experimental option.
|
|
125
|
+
*/
|
|
126
|
+
slow?: number;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface DnsResolveFn {
|
|
130
|
+
(h: string): Promise<string[]>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Subscription Options
|
|
135
|
+
*/
|
|
136
|
+
export type SubscriptionOptions = SubOpts<Msg>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* ServerInfo represents information from the connected server
|
|
140
|
+
*/
|
|
141
|
+
export interface ServerInfo {
|
|
142
|
+
/**
|
|
143
|
+
* True if the server requires authentication
|
|
144
|
+
*/
|
|
145
|
+
"auth_required"?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Server-assigned client_id
|
|
148
|
+
*/
|
|
149
|
+
"client_id": number;
|
|
150
|
+
/**
|
|
151
|
+
* The client's IP as seen by the server
|
|
152
|
+
*/
|
|
153
|
+
"client_ip"?: string;
|
|
154
|
+
/**
|
|
155
|
+
* The name or ID of the cluster
|
|
156
|
+
*/
|
|
157
|
+
cluster?: string;
|
|
158
|
+
/**
|
|
159
|
+
* Other servers available on the connected cluster
|
|
160
|
+
*/
|
|
161
|
+
"connect_urls"?: string[];
|
|
162
|
+
/**
|
|
163
|
+
* Git commit information on the built server binary
|
|
164
|
+
*/
|
|
165
|
+
"git_commit"?: string;
|
|
166
|
+
/**
|
|
167
|
+
* Version information on the Go stack used to build the server binary
|
|
168
|
+
*/
|
|
169
|
+
go: string;
|
|
170
|
+
/**
|
|
171
|
+
* True if the server supports headers
|
|
172
|
+
*/
|
|
173
|
+
headers?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Hostname of the connected server
|
|
176
|
+
*/
|
|
177
|
+
host: string;
|
|
178
|
+
/**
|
|
179
|
+
* True if the server supports JetStream
|
|
180
|
+
*/
|
|
181
|
+
jetstream?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* True if the server is in Lame Duck Mode
|
|
184
|
+
*/
|
|
185
|
+
ldm?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Max number of bytes in message that can be sent to the server
|
|
188
|
+
*/
|
|
189
|
+
"max_payload": number;
|
|
190
|
+
/**
|
|
191
|
+
* If the server required nkey or JWT authentication the nonce used during authentication.
|
|
192
|
+
*/
|
|
193
|
+
nonce?: string;
|
|
194
|
+
/**
|
|
195
|
+
* The port where the server is listening
|
|
196
|
+
*/
|
|
197
|
+
port: number;
|
|
198
|
+
/**
|
|
199
|
+
* Version number of the NATS protocol
|
|
200
|
+
*/
|
|
201
|
+
proto: number;
|
|
202
|
+
/**
|
|
203
|
+
* The ID of the server
|
|
204
|
+
*/
|
|
205
|
+
"server_id": string;
|
|
206
|
+
/**
|
|
207
|
+
* The name of the server
|
|
208
|
+
*/
|
|
209
|
+
"server_name": string;
|
|
210
|
+
/**
|
|
211
|
+
* True if TLS is available
|
|
212
|
+
*/
|
|
213
|
+
"tls_available"?: boolean;
|
|
214
|
+
/**
|
|
215
|
+
* True if TLS connections are required
|
|
216
|
+
*/
|
|
217
|
+
"tls_required"?: boolean;
|
|
218
|
+
/**
|
|
219
|
+
* True if TLS client certificate verification is required
|
|
220
|
+
*/
|
|
221
|
+
"tls_verify"?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* The nats-server version
|
|
224
|
+
*/
|
|
225
|
+
version: string;
|
|
226
|
+
/**
|
|
227
|
+
* JetStream API level advertised by the server. Present on servers that
|
|
228
|
+
* support API level negotiation (nats-server 2.11+). Use to gate features
|
|
229
|
+
* gated by API level (e.g. fast ingest requires `api_lvl >= 4`, available
|
|
230
|
+
* in nats-server 2.14+).
|
|
231
|
+
*/
|
|
232
|
+
"api_lvl"?: number;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface Server {
|
|
236
|
+
readonly hostname: string;
|
|
237
|
+
readonly port: number;
|
|
238
|
+
readonly listen: string;
|
|
239
|
+
readonly src: string;
|
|
240
|
+
readonly tlsName: string;
|
|
241
|
+
readonly reconnects: number;
|
|
242
|
+
readonly lastConnect: number;
|
|
243
|
+
readonly gossiped: boolean;
|
|
244
|
+
readonly didConnect: boolean;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* ReconnectToServerHandler is invoked on every connection attempt (initial
|
|
249
|
+
* connect and reconnects). It receives a snapshot of the current server pool,
|
|
250
|
+
* with the server the client would have selected at index 0, and the most
|
|
251
|
+
* recent ServerInfo (null on initial connect, before any INFO has been
|
|
252
|
+
* received).
|
|
253
|
+
*
|
|
254
|
+
* Return one of:
|
|
255
|
+
* - a `Server` from the (possibly mutated) pool: the client will dial it
|
|
256
|
+
* immediately.
|
|
257
|
+
* - `{ server, delay }`: the client will wait `delay` milliseconds (on top
|
|
258
|
+
* of any reconnect backoff already applied) before dialing `server`.
|
|
259
|
+
* Useful for application-level pacing — not for reconnect backoff
|
|
260
|
+
* (configure that via {@link ConnectionOptions.reconnectDelayHandler}).
|
|
261
|
+
* The delay is interruptible by `nc.close()`.
|
|
262
|
+
* - `null`: defer to default selection (pool[0]).
|
|
263
|
+
*
|
|
264
|
+
* The handler must be synchronous and must not throw. If it throws or
|
|
265
|
+
* returns a Server that is not in the pool, the connection is rejected on
|
|
266
|
+
* initial connect or closed on reconnect with a ConnectionError wrapping
|
|
267
|
+
* the underlying cause.
|
|
268
|
+
*
|
|
269
|
+
* Reconnect backoff (reconnectTimeWait, reconnectJitter,
|
|
270
|
+
* reconnectDelayHandler) is applied by the client before invoking this
|
|
271
|
+
* handler — the handler only selects which server to dial, and may
|
|
272
|
+
* optionally request additional pre-dial delay via the `{server, delay}`
|
|
273
|
+
* return shape.
|
|
274
|
+
*/
|
|
275
|
+
export type ReconnectToServerHandler = (
|
|
276
|
+
pool: ReadonlyArray<Server>,
|
|
277
|
+
info: ServerInfo | null,
|
|
278
|
+
) => Server | { server: Server; delay: number } | null;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* ServerChanged records servers in the cluster that were added or deleted.
|
|
282
|
+
*/
|
|
283
|
+
export interface ServersChanged {
|
|
284
|
+
/** list of added servers */
|
|
285
|
+
readonly added: string[];
|
|
286
|
+
/** list of deleted servers */
|
|
287
|
+
readonly deleted: string[];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export const Match = {
|
|
291
|
+
// Exact option is case-sensitive
|
|
292
|
+
Exact: "exact",
|
|
293
|
+
// Case-sensitive, but key is transformed to Canonical MIME representation
|
|
294
|
+
CanonicalMIME: "canonical",
|
|
295
|
+
// Case-insensitive matches
|
|
296
|
+
IgnoreCase: "insensitive",
|
|
297
|
+
} as const;
|
|
298
|
+
|
|
299
|
+
export type Match = typeof Match[keyof typeof Match];
|
|
300
|
+
|
|
301
|
+
export interface MsgHdrs extends Iterable<[string, string[]]> {
|
|
302
|
+
hasError: boolean;
|
|
303
|
+
status: string;
|
|
304
|
+
code: number;
|
|
305
|
+
description: string;
|
|
306
|
+
|
|
307
|
+
get(k: string, match?: Match): string;
|
|
308
|
+
|
|
309
|
+
set(k: string, v: string, match?: Match): void;
|
|
310
|
+
|
|
311
|
+
append(k: string, v: string, match?: Match): void;
|
|
312
|
+
|
|
313
|
+
has(k: string, match?: Match): boolean;
|
|
314
|
+
|
|
315
|
+
keys(): string[];
|
|
316
|
+
|
|
317
|
+
values(k: string, match?: Match): string[];
|
|
318
|
+
|
|
319
|
+
delete(k: string, match?: Match): void;
|
|
320
|
+
|
|
321
|
+
last(k: string, match?: Match): string;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export interface RequestOptions extends TraceOptions {
|
|
325
|
+
/**
|
|
326
|
+
* number of milliseconds before the request will timeout.
|
|
327
|
+
*/
|
|
328
|
+
timeout: number;
|
|
329
|
+
/**
|
|
330
|
+
* MsgHdrs to include with the request.
|
|
331
|
+
*/
|
|
332
|
+
headers?: MsgHdrs;
|
|
333
|
+
/**
|
|
334
|
+
* If true, the request API will create a regular NATS subscription
|
|
335
|
+
* to process the response. Otherwise a shared muxed subscriptions is
|
|
336
|
+
* used. Requires {@link reply}
|
|
337
|
+
*/
|
|
338
|
+
noMux?: boolean;
|
|
339
|
+
/**
|
|
340
|
+
* The subject where the response should be sent to. Requires {@link noMux}
|
|
341
|
+
*/
|
|
342
|
+
reply?: string;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export type RequestStrategy = "timer" | "count" | "stall" | "sentinel";
|
|
346
|
+
|
|
347
|
+
export interface RequestManyOptions extends TraceOptions {
|
|
348
|
+
strategy: RequestStrategy;
|
|
349
|
+
maxWait: number;
|
|
350
|
+
headers?: MsgHdrs;
|
|
351
|
+
maxMessages?: number;
|
|
352
|
+
noMux?: boolean;
|
|
353
|
+
stall?: number;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export interface Stats {
|
|
357
|
+
/**
|
|
358
|
+
* Number of bytes received by the client.
|
|
359
|
+
*/
|
|
360
|
+
inBytes: number;
|
|
361
|
+
/**
|
|
362
|
+
* Number of bytes sent by the client.
|
|
363
|
+
*/
|
|
364
|
+
outBytes: number;
|
|
365
|
+
/**
|
|
366
|
+
* Number of messages received by the client.
|
|
367
|
+
*/
|
|
368
|
+
inMsgs: number;
|
|
369
|
+
/**
|
|
370
|
+
* Number of messages sent by the client.
|
|
371
|
+
*/
|
|
372
|
+
outMsgs: number;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export type Payload = Uint8Array | string;
|
|
376
|
+
|
|
377
|
+
export interface NatsConnection {
|
|
378
|
+
/**
|
|
379
|
+
* ServerInfo to the currently connected server or undefined
|
|
380
|
+
*/
|
|
381
|
+
info?: ServerInfo;
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Returns a promise that can be used to monitor if the client closes.
|
|
385
|
+
* The promise can resolve an Error if the reason for the close was
|
|
386
|
+
* an error. Note that the promise doesn't reject, but rather resolves
|
|
387
|
+
* to the error if there was one.
|
|
388
|
+
*/
|
|
389
|
+
closed(): Promise<void | Error>;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Close will close the connection to the server. This call will terminate
|
|
393
|
+
* all pending requests and subscriptions. The returned promise resolves when
|
|
394
|
+
* the connection closes.
|
|
395
|
+
*/
|
|
396
|
+
close(): Promise<void>;
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* camera.ui fork patch.
|
|
400
|
+
* Synchronous force-close that drops state without awaiting the transport's
|
|
401
|
+
* close handshake. Use when the underlying network is known-dead and the
|
|
402
|
+
* usual {@link close} would hang on a half-open WebSocket. After this call
|
|
403
|
+
* the connection MUST be treated as unusable (same as {@link close}).
|
|
404
|
+
*/
|
|
405
|
+
abortClose(err?: Error): void;
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* camera.ui fork patch.
|
|
409
|
+
* Like {@link reconnect}, but also works when the client is currently
|
|
410
|
+
* disconnected: cancels any pending reconnect delay or stuck dial-handshake
|
|
411
|
+
* race timer so the next dial-loop iteration picks up the current servers
|
|
412
|
+
* list immediately. If no dial loop is running, starts one.
|
|
413
|
+
*/
|
|
414
|
+
forceReconnect(): Promise<void>;
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Publishes the specified data to the specified subject.
|
|
418
|
+
* @param subject
|
|
419
|
+
* @param payload
|
|
420
|
+
* @param options
|
|
421
|
+
*/
|
|
422
|
+
publish(
|
|
423
|
+
subject: string,
|
|
424
|
+
payload?: Payload,
|
|
425
|
+
options?: PublishOptions,
|
|
426
|
+
): void;
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Publishes using the subject of the specified message, specifying the
|
|
430
|
+
* data, headers and reply found in the message if any.
|
|
431
|
+
* @param msg
|
|
432
|
+
*/
|
|
433
|
+
publishMessage(msg: Msg): void;
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Replies using the reply subject of the specified message, specifying the
|
|
437
|
+
* data, headers in the message if any.
|
|
438
|
+
* @param msg
|
|
439
|
+
*/
|
|
440
|
+
respondMessage(msg: Msg): boolean;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Subscribe expresses interest in the specified subject. The subject may
|
|
444
|
+
* have wildcards. Messages are delivered to the {@link SubOpts#callback |SubscriptionOptions callback}
|
|
445
|
+
* if specified. Otherwise, the subscription is an async iterator for {@link Msg}.
|
|
446
|
+
*
|
|
447
|
+
* @param subject
|
|
448
|
+
* @param opts
|
|
449
|
+
*/
|
|
450
|
+
subscribe(subject: string, opts?: SubscriptionOptions): Subscription;
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Publishes a request with specified data in the specified subject expecting a
|
|
454
|
+
* response before {@link RequestOptions#timeout} milliseconds. The api returns a
|
|
455
|
+
* Promise that resolves when the first response to the request is received. If
|
|
456
|
+
* there are no responders (a subscription) listening on the request subject,
|
|
457
|
+
* the request will fail as soon as the server processes it.
|
|
458
|
+
*
|
|
459
|
+
* @param subject
|
|
460
|
+
* @param payload
|
|
461
|
+
* @param opts
|
|
462
|
+
*/
|
|
463
|
+
request(
|
|
464
|
+
subject: string,
|
|
465
|
+
payload?: Payload,
|
|
466
|
+
opts?: RequestOptions,
|
|
467
|
+
): Promise<Msg>;
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Publishes a request expecting multiple responses back. Several strategies
|
|
471
|
+
* to determine when the request should stop gathering responses.
|
|
472
|
+
* @param subject
|
|
473
|
+
* @param payload
|
|
474
|
+
* @param opts
|
|
475
|
+
*/
|
|
476
|
+
requestMany(
|
|
477
|
+
subject: string,
|
|
478
|
+
payload?: Payload,
|
|
479
|
+
opts?: Partial<RequestManyOptions>,
|
|
480
|
+
): Promise<AsyncIterable<Msg>>;
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Returns a Promise that resolves when the client receives a reply from
|
|
484
|
+
* the server. Use of this API is not necessary by clients.
|
|
485
|
+
*/
|
|
486
|
+
flush(): Promise<void>;
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Initiates a drain on the connection and returns a promise that resolves when the
|
|
490
|
+
* drain completes and the connection closes.
|
|
491
|
+
*
|
|
492
|
+
* Drain is an ordered shutdown of the client. Instead of abruptly closing the client,
|
|
493
|
+
* subscriptions are drained, that is messages not yet processed by a subscription are
|
|
494
|
+
* handled before the subscription is closed. After subscriptions are drained it is not
|
|
495
|
+
* possible to create a new subscription. Then all pending outbound messages are
|
|
496
|
+
* sent to the server. Finally, the connection is closed.
|
|
497
|
+
*/
|
|
498
|
+
drain(): Promise<void>;
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Implements the `AsyncDisposable` protocol so the connection can be used
|
|
502
|
+
* with `await using`. Equivalent to calling {@link drain}; if the connection
|
|
503
|
+
* is already closed or draining, resolves without error.
|
|
504
|
+
*/
|
|
505
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Returns true if the client is closed.
|
|
509
|
+
*/
|
|
510
|
+
isClosed(): boolean;
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Returns true if the client is draining.
|
|
514
|
+
*/
|
|
515
|
+
isDraining(): boolean;
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Returns the hostport of the server the client is connected to.
|
|
519
|
+
*/
|
|
520
|
+
getServer(): string;
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Returns an async iterator of {@link Status} that may be
|
|
524
|
+
* useful in understanding when the client looses a connection, or
|
|
525
|
+
* reconnects, or receives an update from the cluster among other things.
|
|
526
|
+
*
|
|
527
|
+
* @return an AsyncIterable<Status>
|
|
528
|
+
*/
|
|
529
|
+
status(): AsyncIterable<Status>;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Returns some metrics such as the number of messages and bytes
|
|
533
|
+
* sent and recieved by the client.
|
|
534
|
+
*/
|
|
535
|
+
stats(): Stats;
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* @return the number of milliseconds it took for a {@link flush}.
|
|
539
|
+
*/
|
|
540
|
+
rtt(): Promise<number>;
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Use of this API is experimental, and it is subject to be removed.
|
|
544
|
+
*
|
|
545
|
+
* reconnect() enables a client to force a reconnect. A reconnect will disconnect
|
|
546
|
+
* the client, and possibly initiate a reconnect to the cluster. Note that all
|
|
547
|
+
* reconnect caveats apply:
|
|
548
|
+
*
|
|
549
|
+
* - If the reconnection policy given to the client doesn't allow reconnects, the
|
|
550
|
+
* connection will close.
|
|
551
|
+
*
|
|
552
|
+
* - Messages that are inbound or outbound could be lost.
|
|
553
|
+
*
|
|
554
|
+
* - All requests that are in flight will be rejected.
|
|
555
|
+
*
|
|
556
|
+
* Note that the returned promise will reject if the client is already closed, or if
|
|
557
|
+
* it is in the process of draining. If the client is currently disconnected,
|
|
558
|
+
* this API has no effect, as the client is already attempting to reconnect.
|
|
559
|
+
*/
|
|
560
|
+
reconnect(): Promise<void>;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Replace the client's server pool with the provided list of `host:port`
|
|
564
|
+
* entries. The new pool is used for subsequent reconnect attempts. The
|
|
565
|
+
* live connection is not dropped — call {@link reconnect} afterwards to
|
|
566
|
+
* force the client to switch onto the new pool immediately.
|
|
567
|
+
*
|
|
568
|
+
* Throws if `servers` is empty or contains invalid entries. Removing all
|
|
569
|
+
* reachable servers from the pool can leave the client unable to reconnect.
|
|
570
|
+
*/
|
|
571
|
+
setServers(servers: string[]): void;
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Returns the current server pool, including gossiped entries received
|
|
575
|
+
* from the cluster. Each entry exposes `gossiped`, `reconnects`,
|
|
576
|
+
* `lastConnect`, and `didConnect` for filtering and diagnostics.
|
|
577
|
+
*
|
|
578
|
+
* To round-trip with {@link setServers}, map to listens:
|
|
579
|
+
* `nc.getServers().map(s => s.listen)`.
|
|
580
|
+
*/
|
|
581
|
+
getServers(): ReadonlyArray<Server>;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* A reviver function
|
|
586
|
+
*/
|
|
587
|
+
//deno-lint-ignore no-explicit-any
|
|
588
|
+
export type ReviverFn = (key: string, value: any) => any;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Represents a message delivered by NATS. This interface is used by
|
|
592
|
+
* Subscribers.
|
|
593
|
+
*/
|
|
594
|
+
export interface Msg {
|
|
595
|
+
/**
|
|
596
|
+
* The subject the message was sent to
|
|
597
|
+
*/
|
|
598
|
+
subject: string;
|
|
599
|
+
/**
|
|
600
|
+
* The subscription ID where the message was dispatched.
|
|
601
|
+
*/
|
|
602
|
+
sid: number;
|
|
603
|
+
/**
|
|
604
|
+
* A possible subject where the recipient may publish a reply (in the cases
|
|
605
|
+
* where the message represents a request).
|
|
606
|
+
*/
|
|
607
|
+
reply?: string;
|
|
608
|
+
/**
|
|
609
|
+
* The message's data (or payload)
|
|
610
|
+
*/
|
|
611
|
+
data: Uint8Array;
|
|
612
|
+
/**
|
|
613
|
+
* Possible headers that may have been set by the server or the publisher.
|
|
614
|
+
*/
|
|
615
|
+
headers?: MsgHdrs;
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Convenience to publish a response to the {@link reply} subject in the
|
|
619
|
+
* message - this is the same as doing `nc.publish(msg.reply, ...)`.
|
|
620
|
+
* @param payload
|
|
621
|
+
* @param opts
|
|
622
|
+
*/
|
|
623
|
+
respond(payload?: Payload, opts?: PublishOptions): boolean;
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Convenience method to parse the message payload as JSON. This method
|
|
627
|
+
* will throw an exception if there's a parsing error;
|
|
628
|
+
* @param reviver a reviver function
|
|
629
|
+
*/
|
|
630
|
+
json<T>(reviver?: ReviverFn): T;
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Convenience method to parse the message payload as string. This method
|
|
634
|
+
* may throw an exception if there's a conversion error
|
|
635
|
+
*/
|
|
636
|
+
string(): string;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
export type SyncIterator<T> = {
|
|
640
|
+
next(): Promise<T | null>;
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* syncIterator is a utility function that allows an AsyncIterator to be triggered
|
|
645
|
+
* by calling next() - the utility will yield null if the underlying iterator is closed.
|
|
646
|
+
* Note it is possibly an error to call use this function on an AsyncIterable that has
|
|
647
|
+
* already been started (Symbol.asyncIterator() has been called) from a looping construct.
|
|
648
|
+
*/
|
|
649
|
+
export function syncIterator<T>(src: AsyncIterable<T>): SyncIterator<T> {
|
|
650
|
+
const iter = src[Symbol.asyncIterator]();
|
|
651
|
+
return {
|
|
652
|
+
async next(): Promise<T | null> {
|
|
653
|
+
const m = await iter.next();
|
|
654
|
+
if (m.done) {
|
|
655
|
+
return Promise.resolve(null);
|
|
656
|
+
}
|
|
657
|
+
return Promise.resolve(m.value);
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Basic interface to a Subscription type
|
|
664
|
+
*/
|
|
665
|
+
export interface Subscription extends AsyncIterable<Msg> {
|
|
666
|
+
/**
|
|
667
|
+
* A promise that resolves when the subscription closes. If the promise
|
|
668
|
+
* resolves to an error, the subscription was closed because of an error
|
|
669
|
+
* typically a permissions error. Note that this promise doesn't reject, but
|
|
670
|
+
* rather resolves to void (no error) or an Error
|
|
671
|
+
*/
|
|
672
|
+
closed: Promise<void | Error>;
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Stop the subscription from receiving messages. You can optionally
|
|
676
|
+
* specify that the subscription should stop after the specified number
|
|
677
|
+
* of messages have been received. Note this count is since the lifetime
|
|
678
|
+
* of the subscription.
|
|
679
|
+
* @param max
|
|
680
|
+
*/
|
|
681
|
+
unsubscribe(max?: number): void;
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Drain the subscription, closing it after processing all messages
|
|
685
|
+
* currently in flight for the client. Returns a promise that resolves
|
|
686
|
+
* when the subscription finished draining.
|
|
687
|
+
*/
|
|
688
|
+
drain(): Promise<void>;
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Implements the `AsyncDisposable` protocol so the subscription can be used
|
|
692
|
+
* with `await using`. Equivalent to calling {@link drain}; if the
|
|
693
|
+
* subscription is already closed or draining, resolves without error.
|
|
694
|
+
*/
|
|
695
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Returns true if the subscription is draining.
|
|
699
|
+
*/
|
|
700
|
+
isDraining(): boolean;
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Returns true if the subscription is closed.
|
|
704
|
+
*/
|
|
705
|
+
isClosed(): boolean;
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* @ignore
|
|
709
|
+
*/
|
|
710
|
+
callback: MsgCallback<Msg>;
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Returns the subject that was used to create the subscription.
|
|
714
|
+
*/
|
|
715
|
+
getSubject(): string;
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Returns the number of messages received by the subscription.
|
|
719
|
+
*/
|
|
720
|
+
getReceived(): number;
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Returns the number of messages that have been processed by the subscription.
|
|
724
|
+
*/
|
|
725
|
+
getProcessed(): number;
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Returns the number of messages that are pending processing. Note that this
|
|
729
|
+
* is method is only valid for iterators.
|
|
730
|
+
*/
|
|
731
|
+
getPending(): number;
|
|
732
|
+
|
|
733
|
+
/** @ignore */
|
|
734
|
+
getID(): number;
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Return the max number of messages before the subscription will unsubscribe.
|
|
738
|
+
*/
|
|
739
|
+
getMax(): number | undefined;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* These options enable message tracing through NATS.
|
|
744
|
+
*/
|
|
745
|
+
export interface TraceOptions {
|
|
746
|
+
/**
|
|
747
|
+
* If set, the server will send events representing the flow of the
|
|
748
|
+
* message as it moves through the system to this subject.
|
|
749
|
+
*/
|
|
750
|
+
traceDestination?: string;
|
|
751
|
+
/**
|
|
752
|
+
* If true, the message will NOT be delivered, and instead will just
|
|
753
|
+
* generate trace information. Note that in the context of requests,
|
|
754
|
+
* this means the service will not be triggered so the operation will
|
|
755
|
+
* timeout or return no responses.
|
|
756
|
+
*/
|
|
757
|
+
traceOnly?: boolean;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export interface PublishOptions extends TraceOptions {
|
|
761
|
+
/**
|
|
762
|
+
* An optional subject where a response should be sent.
|
|
763
|
+
* Note you must have a subscription listening on this subject
|
|
764
|
+
* to receive the response.
|
|
765
|
+
*/
|
|
766
|
+
reply?: string;
|
|
767
|
+
/**
|
|
768
|
+
* Optional headers to include with the message.
|
|
769
|
+
*/
|
|
770
|
+
headers?: MsgHdrs;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// JetStream Server Types
|
|
774
|
+
/**
|
|
775
|
+
* Value expressed as Nanoseconds - use the `nanos(millis)` function
|
|
776
|
+
* to convert millis to nanoseconds. Note that in some environments this
|
|
777
|
+
* could overflow.
|
|
778
|
+
*/
|
|
779
|
+
export type Nanos = number;
|
|
780
|
+
|
|
781
|
+
export type CallbackFn = () => void;
|
|
782
|
+
|
|
783
|
+
export interface Dispatcher<T> {
|
|
784
|
+
push(v: T | CallbackFn): void;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
export interface QueuedIterator<T> {
|
|
788
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
789
|
+
|
|
790
|
+
stop(err?: Error): void;
|
|
791
|
+
|
|
792
|
+
getProcessed(): number;
|
|
793
|
+
|
|
794
|
+
getPending(): number;
|
|
795
|
+
|
|
796
|
+
getReceived(): number;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
export interface Publisher {
|
|
800
|
+
publish(
|
|
801
|
+
subject: string,
|
|
802
|
+
data: Payload,
|
|
803
|
+
options?: { reply?: string; headers?: MsgHdrs },
|
|
804
|
+
): void;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
export function createInbox(prefix = ""): string {
|
|
808
|
+
prefix = prefix || "_INBOX";
|
|
809
|
+
if (typeof prefix !== "string") {
|
|
810
|
+
throw (new TypeError("prefix must be a string"));
|
|
811
|
+
}
|
|
812
|
+
prefix.split(".")
|
|
813
|
+
.forEach((v) => {
|
|
814
|
+
if (v === "*" || v === ">") {
|
|
815
|
+
throw InvalidArgumentError.format(
|
|
816
|
+
"prefix",
|
|
817
|
+
`cannot have wildcards ('${prefix}')`,
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
return `${prefix}.${nuid.next()}`;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
export interface Request {
|
|
825
|
+
token: string;
|
|
826
|
+
requestSubject: string;
|
|
827
|
+
received: number;
|
|
828
|
+
|
|
829
|
+
resolver(err: Error | null, msg: Msg): void;
|
|
830
|
+
|
|
831
|
+
cancel(err?: Error): void;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* @type {}
|
|
836
|
+
*/
|
|
837
|
+
export type NoAuth = void;
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* @type {auth_token: string} the user token
|
|
841
|
+
*/
|
|
842
|
+
export interface TokenAuth {
|
|
843
|
+
"auth_token": string;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* @type {user: string, pass?: string} the username and
|
|
848
|
+
* optional password if the server requires.
|
|
849
|
+
*/
|
|
850
|
+
export interface UserPass {
|
|
851
|
+
user: string;
|
|
852
|
+
pass?: string;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* @type {nkey: string, sig: string} the public nkey for the user,
|
|
857
|
+
* and a base64 encoded string for the calculated signature of the
|
|
858
|
+
* challenge nonce.
|
|
859
|
+
*/
|
|
860
|
+
export interface NKeyAuth {
|
|
861
|
+
nkey: string;
|
|
862
|
+
sig: string;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* @type {jwt: string, nkey?: string, sig?: string} the user JWT,
|
|
867
|
+
* and if not a bearer token also the public nkey for the user,
|
|
868
|
+
* and a base64 encoded string for the calculated signature of the
|
|
869
|
+
* challenge nonce.
|
|
870
|
+
*/
|
|
871
|
+
export interface JwtAuth {
|
|
872
|
+
jwt: string;
|
|
873
|
+
nkey?: string;
|
|
874
|
+
sig?: string;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* @type NoAuth|TokenAuth|UserPass|NKeyAuth|JwtAuth
|
|
879
|
+
*/
|
|
880
|
+
export type Auth = NoAuth | TokenAuth | UserPass | NKeyAuth | JwtAuth;
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Authenticator is an interface that returns credentials.
|
|
884
|
+
* @type function(nonce?: string) => Auth
|
|
885
|
+
*/
|
|
886
|
+
export interface Authenticator {
|
|
887
|
+
(nonce?: string): Auth;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
export interface ConnectionOptions {
|
|
891
|
+
/**
|
|
892
|
+
* When the server requires authentication, set an {@link Authenticator}.
|
|
893
|
+
* An authenticator is created automatically for username/password and token
|
|
894
|
+
* authentication configurations
|
|
895
|
+
* if {@link user} and {@link pass} or the {@link token} options are set.
|
|
896
|
+
*/
|
|
897
|
+
authenticator?: Authenticator | Authenticator[];
|
|
898
|
+
/**
|
|
899
|
+
* When set to `true` the client will print protocol messages that it receives
|
|
900
|
+
* or sends to the server.
|
|
901
|
+
*/
|
|
902
|
+
debug?: boolean;
|
|
903
|
+
/**
|
|
904
|
+
* Sets the maximum count of ping commands that can be awaiting a response
|
|
905
|
+
* before raising a stale connection status {@link StaleConnectionStatus }
|
|
906
|
+
* notification {@link NatsConnection#status} and initiating a reconnect.
|
|
907
|
+
*
|
|
908
|
+
* @see pingInterval
|
|
909
|
+
*/
|
|
910
|
+
maxPingOut?: number;
|
|
911
|
+
/**
|
|
912
|
+
* Sets the maximum count of per-server reconnect attempts before giving up.
|
|
913
|
+
* Set to `-1` to never give up.
|
|
914
|
+
*
|
|
915
|
+
* @default 10
|
|
916
|
+
*/
|
|
917
|
+
maxReconnectAttempts?: number;
|
|
918
|
+
/**
|
|
919
|
+
* Sets the client name. When set, the server monitoring pages will display
|
|
920
|
+
* this name when referring to this client.
|
|
921
|
+
*/
|
|
922
|
+
name?: string;
|
|
923
|
+
/**
|
|
924
|
+
* When set to true, messages published by this client will not match
|
|
925
|
+
* this client's subscriptions, so the client is guaranteed to never
|
|
926
|
+
* receive self-published messages on a subject that it is listening on.
|
|
927
|
+
*/
|
|
928
|
+
noEcho?: boolean;
|
|
929
|
+
/**
|
|
930
|
+
* If set to true, the client will not randomize its server connection list.
|
|
931
|
+
*/
|
|
932
|
+
noRandomize?: boolean;
|
|
933
|
+
/**
|
|
934
|
+
* Sets the password for a client connection. Requires that the {@link user}
|
|
935
|
+
* option be set. See {@link authenticator}.
|
|
936
|
+
*/
|
|
937
|
+
pass?: string;
|
|
938
|
+
/**
|
|
939
|
+
* When set to true, the server may perform additional checks on protocol
|
|
940
|
+
* message requests. This option is only useful for NATS client development
|
|
941
|
+
* and shouldn't be used, as it affects server performance.
|
|
942
|
+
*/
|
|
943
|
+
pedantic?: boolean;
|
|
944
|
+
/**
|
|
945
|
+
* Sets the number of milliseconds between client initiated ping commands.
|
|
946
|
+
* See {@link maxPingOut}.
|
|
947
|
+
* @default 2 minutes.
|
|
948
|
+
*/
|
|
949
|
+
pingInterval?: number;
|
|
950
|
+
/**
|
|
951
|
+
* Sets the maximum number of milliseconds to wait for a PONG response to an
|
|
952
|
+
* individual PING before considering the connection stale and initiating a
|
|
953
|
+
* reconnect.
|
|
954
|
+
*
|
|
955
|
+
* When set to `0` (default) the per-ping timeout is disabled and the client
|
|
956
|
+
* falls back to the legacy behavior, where the connection is only considered
|
|
957
|
+
* stale once {@link maxPingOut} PINGs have accumulated without any PONG
|
|
958
|
+
* arriving (effective worst-case detection time = `pingInterval × maxPingOut`).
|
|
959
|
+
*
|
|
960
|
+
* When set to a positive value, each PING starts its own timer; if no PONG
|
|
961
|
+
* arrives within `pingTimeout` ms, the connection is torn down immediately.
|
|
962
|
+
*
|
|
963
|
+
* @default 0 (disabled)
|
|
964
|
+
*/
|
|
965
|
+
pingTimeout?: number;
|
|
966
|
+
/**
|
|
967
|
+
* Sets the port number on the localhost (127.0.0.1) where the nats-server is running.
|
|
968
|
+
* This option is mutually exclusive with {@link servers}.
|
|
969
|
+
*/
|
|
970
|
+
port?: number;
|
|
971
|
+
/**
|
|
972
|
+
* When set to true, the server will attempt to reconnect so long as
|
|
973
|
+
* {@link maxReconnectAttempts} doesn't prevent it.
|
|
974
|
+
* @default true
|
|
975
|
+
*/
|
|
976
|
+
reconnect?: boolean;
|
|
977
|
+
/**
|
|
978
|
+
* Set a function that dynamically determines the number of milliseconds
|
|
979
|
+
* that the client should wait for the next reconnect attempt.
|
|
980
|
+
*
|
|
981
|
+
* The handler receives `attempt` — the current per-server reconnect attempt
|
|
982
|
+
* count (0 on the initial dial, incremented on each failed dial, reset to 0
|
|
983
|
+
* after a successful reconnect). Existing handlers with arity 0 keep working
|
|
984
|
+
* unchanged.
|
|
985
|
+
*/
|
|
986
|
+
reconnectDelayHandler?: (attempt: number) => number;
|
|
987
|
+
/**
|
|
988
|
+
* Upper bound for the reconnect delay in milliseconds. When set to a value
|
|
989
|
+
* greater than 0, switches the default reconnect-delay handler from the
|
|
990
|
+
* legacy linear scheme to exponential backoff:
|
|
991
|
+
* `min(reconnectTimeWait × 2^attempt, reconnectionDelayMax)`
|
|
992
|
+
* combined with {@link reconnectionRandomizationFactor} for multiplicative
|
|
993
|
+
* jitter. Has no effect when a custom {@link reconnectDelayHandler} is
|
|
994
|
+
* provided.
|
|
995
|
+
*
|
|
996
|
+
* @default 0 (disabled — legacy linear delay)
|
|
997
|
+
*/
|
|
998
|
+
reconnectionDelayMax?: number;
|
|
999
|
+
/**
|
|
1000
|
+
* Multiplicative jitter factor in `[0, 1]` applied to the computed reconnect
|
|
1001
|
+
* delay when {@link reconnectionDelayMax} is active. A factor of `0.5`
|
|
1002
|
+
* yields a final delay uniformly distributed in `[delay × 0.5, delay × 1.5]`.
|
|
1003
|
+
* Ignored when the legacy linear scheme is active — use
|
|
1004
|
+
* {@link reconnectJitter} there instead.
|
|
1005
|
+
*
|
|
1006
|
+
* @default 0 (no multiplicative jitter)
|
|
1007
|
+
*/
|
|
1008
|
+
reconnectionRandomizationFactor?: number;
|
|
1009
|
+
/**
|
|
1010
|
+
* Set the upper bound for a random delay in milliseconds added to
|
|
1011
|
+
* {@link reconnectTimeWait}.
|
|
1012
|
+
*
|
|
1013
|
+
* @default 100 millis
|
|
1014
|
+
*/
|
|
1015
|
+
reconnectJitter?: number;
|
|
1016
|
+
/**
|
|
1017
|
+
* Set the upper bound for a random delay in milliseconds added to
|
|
1018
|
+
* {@link reconnectTimeWait}. This only affects TLS connections
|
|
1019
|
+
*
|
|
1020
|
+
* @default 1000 millis
|
|
1021
|
+
*/
|
|
1022
|
+
reconnectJitterTLS?: number;
|
|
1023
|
+
/**
|
|
1024
|
+
* Set the number of millisecods between reconnect attempts.
|
|
1025
|
+
*
|
|
1026
|
+
* @default 2000 millis
|
|
1027
|
+
*/
|
|
1028
|
+
reconnectTimeWait?: number;
|
|
1029
|
+
/**
|
|
1030
|
+
* Set the hostport(s) where the client should attempt to connect.
|
|
1031
|
+
* This option is mutually exclusive with {@link port}.
|
|
1032
|
+
*
|
|
1033
|
+
* @default 127.0.0.1:4222
|
|
1034
|
+
*/
|
|
1035
|
+
servers?: Array<string> | string;
|
|
1036
|
+
/**
|
|
1037
|
+
* Sets the number of milliseconds the client should wait for a server
|
|
1038
|
+
* handshake to be established.
|
|
1039
|
+
*
|
|
1040
|
+
* @default 20000 millis
|
|
1041
|
+
*/
|
|
1042
|
+
timeout?: number;
|
|
1043
|
+
/**
|
|
1044
|
+
* When set (can be an empty object), the client requires a secure connection.
|
|
1045
|
+
* TlsOptions honored depend on the runtime. Consult the specific NATS javascript
|
|
1046
|
+
* client GitHub repo/documentation. When set to null, the client should fail
|
|
1047
|
+
* should not connect using TLS. In the case where TLS is available on the server
|
|
1048
|
+
* a standard connection will be used. If TLS is required, the connection will fail.
|
|
1049
|
+
*/
|
|
1050
|
+
tls?: TlsOptions | null;
|
|
1051
|
+
/**
|
|
1052
|
+
* Set to a client authentication token. Note that these tokens are
|
|
1053
|
+
* a specific authentication strategy on the nats-server. This option
|
|
1054
|
+
* is mutually exclusive of {@link user} and {@link pass}. See {@link authenticator}.
|
|
1055
|
+
*/
|
|
1056
|
+
token?: string;
|
|
1057
|
+
/**
|
|
1058
|
+
* Sets the username for a client connection. Requires that the {@link pass}
|
|
1059
|
+
* option be set. See {@link authenticator}.
|
|
1060
|
+
*/
|
|
1061
|
+
user?: string;
|
|
1062
|
+
/**
|
|
1063
|
+
* When set to true, the server will send response to all server commands.
|
|
1064
|
+
* This option is only useful for NATS client development and shouldn't
|
|
1065
|
+
* be used, as it affects server performance.
|
|
1066
|
+
*/
|
|
1067
|
+
verbose?: boolean;
|
|
1068
|
+
/**
|
|
1069
|
+
* When set to true {@link maxReconnectAttempts} will not trigger until the client
|
|
1070
|
+
* has established one connection.
|
|
1071
|
+
*/
|
|
1072
|
+
waitOnFirstConnect?: boolean;
|
|
1073
|
+
/**
|
|
1074
|
+
* When set to true, cluster information gossiped by the nats-server will
|
|
1075
|
+
* not augment the lists of server(s) known by the client.
|
|
1076
|
+
*/
|
|
1077
|
+
ignoreClusterUpdates?: boolean;
|
|
1078
|
+
/**
|
|
1079
|
+
* A string prefix (must be a valid subject prefix) prepended to inboxes
|
|
1080
|
+
* generated by client. This allows a client with limited subject permissions
|
|
1081
|
+
* to specify a subject where requests can deliver responses.
|
|
1082
|
+
*/
|
|
1083
|
+
inboxPrefix?: string;
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* By default, NATS clients will abort reconnect if they fail authentication
|
|
1087
|
+
* twice in a row with the same error, regardless of the reconnect policy.
|
|
1088
|
+
* This option should be used with care as it will disable this behaviour when true
|
|
1089
|
+
*/
|
|
1090
|
+
ignoreAuthErrorAbort?: boolean;
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* When true, the client will not augment timeout and other error traces with
|
|
1094
|
+
* additional context as to where the operation was started.
|
|
1095
|
+
*/
|
|
1096
|
+
noAsyncTraces?: boolean;
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* When false, the connect function will not perform any hostname resolution. Note that
|
|
1100
|
+
* by default this option will be true if the client supports hostname resolution.
|
|
1101
|
+
* Note that on clients that don't supported (mainly the websocket client, setting this
|
|
1102
|
+
* option to true, will throw an exception as this option is not available.
|
|
1103
|
+
*/
|
|
1104
|
+
resolve?: boolean;
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* Optional handler invoked on every connection attempt to choose which
|
|
1108
|
+
* server the client should dial next. See {@link ReconnectToServerHandler}.
|
|
1109
|
+
*/
|
|
1110
|
+
reconnectToServer?: ReconnectToServerHandler;
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* camera.ui fork patch.
|
|
1114
|
+
* Optional AbortSignal that cancels an in-flight `connect()`. On abort the
|
|
1115
|
+
* client synchronously runs the same cleanup path as {@link NatsConnection.abortClose},
|
|
1116
|
+
* cancelling any pending dialDelay / raceTimer and breaking the dial loop.
|
|
1117
|
+
* The connect() promise rejects with the signal's reason (or an AbortError).
|
|
1118
|
+
* Use this for race-to-recover patterns where a losing candidate must stop
|
|
1119
|
+
* dialing the moment another candidate wins.
|
|
1120
|
+
*/
|
|
1121
|
+
signal?: AbortSignal;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* TlsOptions that can be specified to a client. Note that
|
|
1126
|
+
* the options are typically runtime specific, so some clients won't support
|
|
1127
|
+
* them at all. In other cases they will match to the runtime's TLS options.
|
|
1128
|
+
*
|
|
1129
|
+
* If no options are specified, but the argument for TlsOptions is an object,
|
|
1130
|
+
* the client is requesting to only use connections that are secured by TLS.
|
|
1131
|
+
*/
|
|
1132
|
+
export interface TlsOptions {
|
|
1133
|
+
/**
|
|
1134
|
+
* handshakeFirst option requires the server to be configured with `handshakeFirst: true`.
|
|
1135
|
+
*/
|
|
1136
|
+
handshakeFirst?: boolean;
|
|
1137
|
+
certFile?: string;
|
|
1138
|
+
cert?: string;
|
|
1139
|
+
caFile?: string;
|
|
1140
|
+
ca?: string;
|
|
1141
|
+
keyFile?: string;
|
|
1142
|
+
key?: string;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
export const DEFAULT_PORT = 4222;
|
|
1146
|
+
export const DEFAULT_HOST = "127.0.0.1";
|
|
1147
|
+
|
|
1148
|
+
export type ConnectFn = (opts: ConnectionOptions) => Promise<NatsConnection>;
|
|
1149
|
+
|
|
1150
|
+
export interface URLParseFn {
|
|
1151
|
+
(u: string, encrypted?: boolean): string;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
export type Context = {
|
|
1155
|
+
server: ContextServer;
|
|
1156
|
+
data: ContextUser;
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
export type ContextServer = {
|
|
1160
|
+
name: string;
|
|
1161
|
+
host: string;
|
|
1162
|
+
id: string;
|
|
1163
|
+
version: string;
|
|
1164
|
+
tags: string[];
|
|
1165
|
+
jetstream: boolean;
|
|
1166
|
+
flags: number;
|
|
1167
|
+
seq: number;
|
|
1168
|
+
time: Date;
|
|
1169
|
+
};
|
|
1170
|
+
|
|
1171
|
+
export type ContextUser = {
|
|
1172
|
+
user: string;
|
|
1173
|
+
account: string;
|
|
1174
|
+
permissions?: {
|
|
1175
|
+
publish?: ContextPermission;
|
|
1176
|
+
subscribe?: ContextPermission;
|
|
1177
|
+
responses?: ContextResponsePermission;
|
|
1178
|
+
};
|
|
1179
|
+
};
|
|
1180
|
+
|
|
1181
|
+
export type ContextPermission = {
|
|
1182
|
+
deny?: string[];
|
|
1183
|
+
allow?: string[];
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
export type ContextResponsePermission = {
|
|
1187
|
+
max: number;
|
|
1188
|
+
ttl: number;
|
|
1189
|
+
};
|
|
1190
|
+
|
|
1191
|
+
export type RequestInfo = {
|
|
1192
|
+
acc: string;
|
|
1193
|
+
rtt: number;
|
|
1194
|
+
start?: Date;
|
|
1195
|
+
host?: string;
|
|
1196
|
+
id?: string;
|
|
1197
|
+
svc?: string;
|
|
1198
|
+
user?: string;
|
|
1199
|
+
name?: string;
|
|
1200
|
+
lang?: string;
|
|
1201
|
+
ver?: string;
|
|
1202
|
+
server?: string;
|
|
1203
|
+
cluster?: string;
|
|
1204
|
+
alts?: string[];
|
|
1205
|
+
stop?: Date;
|
|
1206
|
+
jwt?: string;
|
|
1207
|
+
issuer_key?: string;
|
|
1208
|
+
name_tag?: string;
|
|
1209
|
+
tags?: string[];
|
|
1210
|
+
client_type?: string;
|
|
1211
|
+
client_id?: string;
|
|
1212
|
+
nonce?: string;
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
export type CallbackOptionalErrorFn = (err: Error | void) => void;
|
|
1216
|
+
|
|
1217
|
+
export type ConnectionClosedListener = {
|
|
1218
|
+
connectionClosedCallback: CallbackOptionalErrorFn;
|
|
1219
|
+
};
|