@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,367 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2018-2024 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
|
+
// deno-lint-ignore-file no-explicit-any
|
|
16
|
+
import { TD } from "./encoders.ts";
|
|
17
|
+
import type { Nanos } from "./core.ts";
|
|
18
|
+
import { TimeoutError } from "./errors.ts";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Allows derived type structures to show through
|
|
22
|
+
*/
|
|
23
|
+
export type Prettify<T> = {
|
|
24
|
+
[K in keyof T]: T[K];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* WithRequired is a utility Type allows a type to specify required fields
|
|
29
|
+
*/
|
|
30
|
+
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
|
|
31
|
+
|
|
32
|
+
export type ValueResult<T> = {
|
|
33
|
+
isError: false;
|
|
34
|
+
value: T;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ErrorResult = {
|
|
38
|
+
isError: true;
|
|
39
|
+
error: Error;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Result is a value that may have resulted in an error.
|
|
44
|
+
*/
|
|
45
|
+
export type Result<T> = ValueResult<T> | ErrorResult;
|
|
46
|
+
|
|
47
|
+
export function extend(a: any, ...b: any[]): any {
|
|
48
|
+
for (let i = 0; i < b.length; i++) {
|
|
49
|
+
const o = b[i];
|
|
50
|
+
Object.keys(o).forEach(function (k) {
|
|
51
|
+
a[k] = o[k];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return a;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Pending {
|
|
58
|
+
pending: number;
|
|
59
|
+
write: (c: number) => void;
|
|
60
|
+
wrote: (c: number) => void;
|
|
61
|
+
err: (err: Error) => void;
|
|
62
|
+
close: () => void;
|
|
63
|
+
promise: () => Promise<any>;
|
|
64
|
+
resolved: boolean;
|
|
65
|
+
done: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function render(frame: Uint8Array): string {
|
|
69
|
+
const cr = "␍";
|
|
70
|
+
const lf = "␊";
|
|
71
|
+
return TD.decode(frame)
|
|
72
|
+
.replace(/\n/g, lf)
|
|
73
|
+
.replace(/\r/g, cr);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface Timeout<T> extends Promise<T> {
|
|
77
|
+
cancel: () => void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function timeout<T>(ms: number, asyncTraces = true): Timeout<T> {
|
|
81
|
+
// by generating the stack here to help identify what timed out
|
|
82
|
+
const err = asyncTraces ? new TimeoutError() : null;
|
|
83
|
+
let methods;
|
|
84
|
+
let timer: number;
|
|
85
|
+
const p = new Promise((_resolve, reject) => {
|
|
86
|
+
const cancel = (): void => {
|
|
87
|
+
if (timer) {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
methods = { cancel };
|
|
92
|
+
// @ts-ignore: node is not a number
|
|
93
|
+
timer = setTimeout(() => {
|
|
94
|
+
if (err === null) {
|
|
95
|
+
reject(new TimeoutError());
|
|
96
|
+
} else {
|
|
97
|
+
reject(err);
|
|
98
|
+
}
|
|
99
|
+
}, ms);
|
|
100
|
+
});
|
|
101
|
+
// noinspection JSUnusedAssignment
|
|
102
|
+
return Object.assign(p, methods) as Timeout<T>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface Delay extends Promise<void> {
|
|
106
|
+
cancel: () => void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function delay(ms = 0): Delay {
|
|
110
|
+
let methods;
|
|
111
|
+
const p = new Promise<void>((resolve) => {
|
|
112
|
+
const timer = setTimeout(() => {
|
|
113
|
+
resolve();
|
|
114
|
+
}, ms);
|
|
115
|
+
const cancel = (): void => {
|
|
116
|
+
if (timer) {
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
resolve();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
methods = { cancel };
|
|
122
|
+
});
|
|
123
|
+
return Object.assign(p, methods) as Delay;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function deadline<T>(p: Promise<T>, millis = 1000): Promise<T> {
|
|
127
|
+
const d = deferred<never>();
|
|
128
|
+
const timer = setTimeout(
|
|
129
|
+
() => {
|
|
130
|
+
d.reject(new TimeoutError());
|
|
131
|
+
},
|
|
132
|
+
millis,
|
|
133
|
+
);
|
|
134
|
+
try {
|
|
135
|
+
return await Promise.race([p, d]);
|
|
136
|
+
} finally {
|
|
137
|
+
clearTimeout(timer);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface Deferred<T> extends Promise<T> {
|
|
142
|
+
/**
|
|
143
|
+
* Resolves the Deferred to a value T
|
|
144
|
+
* @param value
|
|
145
|
+
*/
|
|
146
|
+
resolve: (value?: T | PromiseLike<T>) => void;
|
|
147
|
+
//@ts-ignore: tsc guard
|
|
148
|
+
/**
|
|
149
|
+
* Rejects the Deferred
|
|
150
|
+
* @param reason
|
|
151
|
+
*/
|
|
152
|
+
reject: (reason?: any) => void;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Returns a Promise that has a resolve/reject methods that can
|
|
157
|
+
* be used to resolve and defer the Deferred.
|
|
158
|
+
*/
|
|
159
|
+
export function deferred<T>(): Deferred<T> {
|
|
160
|
+
let methods = {};
|
|
161
|
+
const p = new Promise<T>((resolve, reject): void => {
|
|
162
|
+
methods = { resolve, reject };
|
|
163
|
+
});
|
|
164
|
+
return Object.assign(p, methods) as Deferred<T>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function debugDeferred<T>(): Deferred<T> {
|
|
168
|
+
let methods = {};
|
|
169
|
+
const p = new Promise<T>((resolve, reject): void => {
|
|
170
|
+
methods = {
|
|
171
|
+
resolve: (v: T) => {
|
|
172
|
+
console.trace("resolve", v);
|
|
173
|
+
resolve(v);
|
|
174
|
+
},
|
|
175
|
+
reject: (err?: Error) => {
|
|
176
|
+
console.trace("reject");
|
|
177
|
+
reject(err);
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
return Object.assign(p, methods) as Deferred<T>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function shuffle<T>(a: T[]): T[] {
|
|
185
|
+
for (let i = a.length - 1; i > 0; i--) {
|
|
186
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
187
|
+
[a[i], a[j]] = [a[j], a[i]];
|
|
188
|
+
}
|
|
189
|
+
return a;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function collect<T>(iter: AsyncIterable<T>): Promise<T[]> {
|
|
193
|
+
const buf: T[] = [];
|
|
194
|
+
for await (const v of iter) {
|
|
195
|
+
buf.push(v);
|
|
196
|
+
}
|
|
197
|
+
return buf;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export class Perf {
|
|
201
|
+
timers: Map<string, number>;
|
|
202
|
+
measures: Map<string, number>;
|
|
203
|
+
|
|
204
|
+
constructor() {
|
|
205
|
+
this.timers = new Map();
|
|
206
|
+
this.measures = new Map();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
mark(key: string) {
|
|
210
|
+
this.timers.set(key, performance.now());
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
measure(key: string, startKey: string, endKey: string) {
|
|
214
|
+
const s = this.timers.get(startKey);
|
|
215
|
+
if (s === undefined) {
|
|
216
|
+
throw new Error(`${startKey} is not defined`);
|
|
217
|
+
}
|
|
218
|
+
const e = this.timers.get(endKey);
|
|
219
|
+
if (e === undefined) {
|
|
220
|
+
throw new Error(`${endKey} is not defined`);
|
|
221
|
+
}
|
|
222
|
+
this.measures.set(key, e - s);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
getEntries(): { name: string; duration: number }[] {
|
|
226
|
+
const values: { name: string; duration: number }[] = [];
|
|
227
|
+
this.measures.forEach((v, k) => {
|
|
228
|
+
values.push({ name: k, duration: v });
|
|
229
|
+
});
|
|
230
|
+
return values;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export class SimpleMutex {
|
|
235
|
+
max: number;
|
|
236
|
+
current: number;
|
|
237
|
+
waiting: Deferred<void>[];
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @param max number of concurrent operations
|
|
241
|
+
*/
|
|
242
|
+
constructor(max = 1) {
|
|
243
|
+
this.max = max;
|
|
244
|
+
this.current = 0;
|
|
245
|
+
this.waiting = [];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Returns a promise that resolves when the mutex is acquired
|
|
250
|
+
*/
|
|
251
|
+
lock(): Promise<void> {
|
|
252
|
+
// increment the count
|
|
253
|
+
this.current++;
|
|
254
|
+
// if we have runners, resolve it
|
|
255
|
+
if (this.current <= this.max) {
|
|
256
|
+
return Promise.resolve();
|
|
257
|
+
}
|
|
258
|
+
// otherwise defer it
|
|
259
|
+
const d = deferred<void>();
|
|
260
|
+
this.waiting.push(d);
|
|
261
|
+
return d;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Release an acquired mutex - must be called
|
|
266
|
+
*/
|
|
267
|
+
unlock(): void {
|
|
268
|
+
// decrement the count
|
|
269
|
+
this.current--;
|
|
270
|
+
// if we have deferred, resolve one
|
|
271
|
+
const d = this.waiting.pop();
|
|
272
|
+
d?.resolve();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Returns a new number between .5*n and 1.5*n.
|
|
278
|
+
* If the n is 0, returns 0.
|
|
279
|
+
* @param n
|
|
280
|
+
*/
|
|
281
|
+
export function jitter(n: number): number {
|
|
282
|
+
if (n === 0) {
|
|
283
|
+
return 0;
|
|
284
|
+
}
|
|
285
|
+
return Math.floor(n / 2 + Math.random() * n);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export interface Backoff {
|
|
289
|
+
backoff(attempt: number): number;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Returns a Backoff with the specified interval policy set.
|
|
294
|
+
* @param policy
|
|
295
|
+
*/
|
|
296
|
+
export function backoff(policy = [0, 250, 250, 500, 500, 3000, 5000]): Backoff {
|
|
297
|
+
if (!Array.isArray(policy)) {
|
|
298
|
+
policy = [0, 250, 250, 500, 500, 3000, 5000];
|
|
299
|
+
}
|
|
300
|
+
const max = policy.length - 1;
|
|
301
|
+
return {
|
|
302
|
+
backoff(attempt: number): number {
|
|
303
|
+
return jitter(attempt > max ? policy[max] : policy[attempt]);
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Converts the specified millis into Nanos
|
|
310
|
+
* @param millis
|
|
311
|
+
*/
|
|
312
|
+
export function nanos(millis: number): Nanos {
|
|
313
|
+
return millis * 1000000;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Convert the specified Nanos into millis
|
|
318
|
+
* @param ns
|
|
319
|
+
*/
|
|
320
|
+
export function millis(ns: Nanos): number {
|
|
321
|
+
return Math.floor(ns / 1000000);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const tokenDigits =
|
|
325
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
326
|
+
const tokenDigitCodes = new Uint8Array(62);
|
|
327
|
+
for (let i = 0; i < 62; i++) tokenDigitCodes[i] = tokenDigits.charCodeAt(i);
|
|
328
|
+
|
|
329
|
+
// 62^8 = 218,340,105,584,896 — fits safely in Number.MAX_SAFE_INTEGER (2^53-1).
|
|
330
|
+
const tokenSpace = 218340105584896;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Returns an 8-char base62 token from `Math.random()`. Used as the per-request
|
|
334
|
+
* suffix on a shared `_INBOX.<nuid>.*` mux subscription — mirrors the
|
|
335
|
+
* `replySuffixLen=8` token generated by nats.go in `(nc *Conn).newRespInbox`.
|
|
336
|
+
*
|
|
337
|
+
* Not cryptographically random and does not check for collisions. Collision
|
|
338
|
+
* avoidance relies on (a) the per-connection nuid prefix isolating namespace
|
|
339
|
+
* across connections, and (b) the in-flight token set being small relative
|
|
340
|
+
* to the 62^8 ≈ 2.18e14 space.
|
|
341
|
+
*/
|
|
342
|
+
export function randomToken(): string {
|
|
343
|
+
let n = Math.floor(Math.random() * tokenSpace);
|
|
344
|
+
let d = n % 62;
|
|
345
|
+
const c0 = tokenDigitCodes[d];
|
|
346
|
+
n = (n - d) / 62;
|
|
347
|
+
d = n % 62;
|
|
348
|
+
const c1 = tokenDigitCodes[d];
|
|
349
|
+
n = (n - d) / 62;
|
|
350
|
+
d = n % 62;
|
|
351
|
+
const c2 = tokenDigitCodes[d];
|
|
352
|
+
n = (n - d) / 62;
|
|
353
|
+
d = n % 62;
|
|
354
|
+
const c3 = tokenDigitCodes[d];
|
|
355
|
+
n = (n - d) / 62;
|
|
356
|
+
d = n % 62;
|
|
357
|
+
const c4 = tokenDigitCodes[d];
|
|
358
|
+
n = (n - d) / 62;
|
|
359
|
+
d = n % 62;
|
|
360
|
+
const c5 = tokenDigitCodes[d];
|
|
361
|
+
n = (n - d) / 62;
|
|
362
|
+
d = n % 62;
|
|
363
|
+
const c6 = tokenDigitCodes[d];
|
|
364
|
+
n = (n - d) / 62;
|
|
365
|
+
const c7 = tokenDigitCodes[n];
|
|
366
|
+
return String.fromCharCode(c0, c1, c2, c3, c4, c5, c6, c7);
|
|
367
|
+
}
|