@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,202 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021-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 { extend } from "./util.ts";
|
|
17
|
+
import { defaultPort, getResolveFn } from "./transport.ts";
|
|
18
|
+
import type { Authenticator, ConnectionOptions, ServerInfo } from "./core.ts";
|
|
19
|
+
import { createInbox, DEFAULT_HOST } from "./core.ts";
|
|
20
|
+
import {
|
|
21
|
+
multiAuthenticator,
|
|
22
|
+
noAuthFn,
|
|
23
|
+
tokenAuthenticator,
|
|
24
|
+
usernamePasswordAuthenticator,
|
|
25
|
+
} from "./authenticator.ts";
|
|
26
|
+
import { errors, InvalidArgumentError } from "./errors.ts";
|
|
27
|
+
|
|
28
|
+
export const DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
|
|
29
|
+
export const DEFAULT_JITTER = 100;
|
|
30
|
+
export const DEFAULT_JITTER_TLS = 1000;
|
|
31
|
+
// Ping interval
|
|
32
|
+
export const DEFAULT_PING_INTERVAL = 2 * 60 * 1000; // 2 minutes
|
|
33
|
+
export const DEFAULT_MAX_PING_OUT = 2;
|
|
34
|
+
export const DEFAULT_PING_TIMEOUT = 0;
|
|
35
|
+
|
|
36
|
+
// DISCONNECT Parameters, 2 sec wait, 10 tries
|
|
37
|
+
export const DEFAULT_RECONNECT_TIME_WAIT = 2 * 1000;
|
|
38
|
+
// Upper bound for exponential reconnect-delay backoff. 0 = disabled (legacy
|
|
39
|
+
// linear behavior).
|
|
40
|
+
export const DEFAULT_RECONNECTION_DELAY_MAX = 0;
|
|
41
|
+
// Multiplicative jitter factor [0..1] applied to the computed delay. 0 = no
|
|
42
|
+
// multiplicative jitter (legacy `reconnectJitter` additive jitter still applies).
|
|
43
|
+
export const DEFAULT_RECONNECTION_RANDOMIZATION_FACTOR = 0;
|
|
44
|
+
|
|
45
|
+
export function defaultOptions(): ConnectionOptions {
|
|
46
|
+
return {
|
|
47
|
+
maxPingOut: DEFAULT_MAX_PING_OUT,
|
|
48
|
+
maxReconnectAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
49
|
+
noRandomize: false,
|
|
50
|
+
pedantic: false,
|
|
51
|
+
pingInterval: DEFAULT_PING_INTERVAL,
|
|
52
|
+
reconnect: true,
|
|
53
|
+
reconnectJitter: DEFAULT_JITTER,
|
|
54
|
+
reconnectJitterTLS: DEFAULT_JITTER_TLS,
|
|
55
|
+
reconnectTimeWait: DEFAULT_RECONNECT_TIME_WAIT,
|
|
56
|
+
tls: undefined,
|
|
57
|
+
verbose: false,
|
|
58
|
+
waitOnFirstConnect: false,
|
|
59
|
+
ignoreAuthErrorAbort: false,
|
|
60
|
+
} as ConnectionOptions;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function hasWsProtocol(opts?: ConnectionOptions): boolean {
|
|
64
|
+
if (opts) {
|
|
65
|
+
let { servers } = opts;
|
|
66
|
+
if (typeof servers === "string") {
|
|
67
|
+
servers = [servers];
|
|
68
|
+
}
|
|
69
|
+
if (servers) {
|
|
70
|
+
for (let i = 0; i < servers.length; i++) {
|
|
71
|
+
const s = servers[i].toLowerCase();
|
|
72
|
+
if (s.startsWith("ws://") || s.startsWith("wss://")) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function buildAuthenticator(
|
|
82
|
+
opts: ConnectionOptions,
|
|
83
|
+
): Authenticator {
|
|
84
|
+
const buf: Authenticator[] = [];
|
|
85
|
+
// jwtAuthenticator is created by the user, since it
|
|
86
|
+
// will require possibly reading files which
|
|
87
|
+
// some of the clients are simply unable to do
|
|
88
|
+
if (typeof opts.authenticator === "function") {
|
|
89
|
+
buf.push(opts.authenticator);
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(opts.authenticator)) {
|
|
92
|
+
buf.push(...opts.authenticator);
|
|
93
|
+
}
|
|
94
|
+
if (opts.token) {
|
|
95
|
+
buf.push(tokenAuthenticator(opts.token));
|
|
96
|
+
}
|
|
97
|
+
if (opts.user) {
|
|
98
|
+
buf.push(usernamePasswordAuthenticator(opts.user, opts.pass));
|
|
99
|
+
}
|
|
100
|
+
return buf.length === 0 ? noAuthFn() : multiAuthenticator(buf);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function parseOptions(opts?: ConnectionOptions): ConnectionOptions {
|
|
104
|
+
const dhp = `${DEFAULT_HOST}:${defaultPort()}`;
|
|
105
|
+
opts = opts || { servers: [dhp] };
|
|
106
|
+
opts.servers = opts.servers || [];
|
|
107
|
+
if (typeof opts.servers === "string") {
|
|
108
|
+
opts.servers = [opts.servers];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (opts.servers.length > 0 && opts.port) {
|
|
112
|
+
throw InvalidArgumentError.format(
|
|
113
|
+
["servers", "port"],
|
|
114
|
+
"are mutually exclusive",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (opts.servers.length === 0 && opts.port) {
|
|
119
|
+
opts.servers = [`${DEFAULT_HOST}:${opts.port}`];
|
|
120
|
+
}
|
|
121
|
+
if (opts.servers && opts.servers.length === 0) {
|
|
122
|
+
opts.servers = [dhp];
|
|
123
|
+
}
|
|
124
|
+
const options = extend(defaultOptions(), opts);
|
|
125
|
+
|
|
126
|
+
options.authenticator = buildAuthenticator(options);
|
|
127
|
+
|
|
128
|
+
["reconnectDelayHandler", "authenticator"].forEach((n) => {
|
|
129
|
+
if (options[n] && typeof options[n] !== "function") {
|
|
130
|
+
throw TypeError(`'${n}' must be a function`);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (!options.reconnectDelayHandler) {
|
|
135
|
+
options.reconnectDelayHandler = (attempt = 0) => {
|
|
136
|
+
const base = options.reconnectTimeWait ?? DEFAULT_RECONNECT_TIME_WAIT;
|
|
137
|
+
const max = options.reconnectionDelayMax ?? DEFAULT_RECONNECTION_DELAY_MAX;
|
|
138
|
+
const factor = options.reconnectionRandomizationFactor ??
|
|
139
|
+
DEFAULT_RECONNECTION_RANDOMIZATION_FACTOR;
|
|
140
|
+
|
|
141
|
+
// exponential backoff with cap + multiplicative jitter
|
|
142
|
+
if (max > 0) {
|
|
143
|
+
let d = Math.min(base * Math.pow(2, Math.max(0, attempt)), max);
|
|
144
|
+
if (factor > 0) {
|
|
145
|
+
const lo = d * (1 - factor);
|
|
146
|
+
const hi = d * (1 + factor);
|
|
147
|
+
d = lo + Math.random() * (hi - lo);
|
|
148
|
+
}
|
|
149
|
+
return Math.round(d);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// legacy: constant base + additive jitter
|
|
153
|
+
let extra = options.tls
|
|
154
|
+
? options.reconnectJitterTLS
|
|
155
|
+
: options.reconnectJitter;
|
|
156
|
+
if (extra) {
|
|
157
|
+
extra++;
|
|
158
|
+
extra = Math.floor(Math.random() * extra);
|
|
159
|
+
}
|
|
160
|
+
return base + (extra ?? 0);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (options.inboxPrefix) {
|
|
165
|
+
createInbox(options.inboxPrefix);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// if not set - we set it
|
|
169
|
+
if (options.resolve === undefined) {
|
|
170
|
+
// set a default based on whether the client can resolve or not
|
|
171
|
+
options.resolve = typeof getResolveFn() === "function";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (options.resolve) {
|
|
175
|
+
if (typeof getResolveFn() !== "function") {
|
|
176
|
+
throw InvalidArgumentError.format(
|
|
177
|
+
"resolve",
|
|
178
|
+
"is not supported in the current runtime",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return options;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function checkOptions(info: ServerInfo, options: ConnectionOptions) {
|
|
187
|
+
const { proto, tls_required: tlsRequired, tls_available: tlsAvailable } =
|
|
188
|
+
info;
|
|
189
|
+
if ((proto === undefined || proto < 1) && options.noEcho) {
|
|
190
|
+
throw new errors.ConnectionError(`server does not support 'noEcho'`);
|
|
191
|
+
}
|
|
192
|
+
const tls = tlsRequired || tlsAvailable || false;
|
|
193
|
+
if (options.tls && !tls) {
|
|
194
|
+
throw new errors.ConnectionError(`server does not support 'tls'`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function checkUnsupportedOption(prop: string, v?: unknown) {
|
|
199
|
+
if (v) {
|
|
200
|
+
throw InvalidArgumentError.format(prop, "is not supported");
|
|
201
|
+
}
|
|
202
|
+
}
|