@certik/skynet 0.22.0 → 0.22.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.
@@ -1,926 +1,63 @@
1
- // @bun
2
- var __create = Object.create;
3
- var __getProtoOf = Object.getPrototypeOf;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __toESM = (mod, isNodeMode, target) => {
9
- target = mod != null ? __create(__getProtoOf(mod)) : {};
10
- const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
11
- for (let key of __getOwnPropNames(mod))
12
- if (!__hasOwnProp.call(to, key))
13
- __defProp(to, key, {
14
- get: () => mod[key],
15
- enumerable: true
16
- });
17
- return to;
18
- };
19
- var __moduleCache = /* @__PURE__ */ new WeakMap;
20
- var __toCommonJS = (from) => {
21
- var entry = __moduleCache.get(from), desc;
22
- if (entry)
23
- return entry;
24
- entry = __defProp({}, "__esModule", { value: true });
25
- if (from && typeof from === "object" || typeof from === "function")
26
- __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
27
- get: () => from[key],
28
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
29
- }));
30
- __moduleCache.set(from, entry);
31
- return entry;
32
- };
33
- var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
34
- var __export = (target, all) => {
35
- for (var name in all)
36
- __defProp(target, name, {
37
- get: all[name],
38
- enumerable: true,
39
- configurable: true,
40
- set: (newValue) => all[name] = () => newValue
1
+ import pThrottle from "p-throttle";
2
+ import pMemoize from "p-memoize";
3
+ import QuickLRU from "quick-lru";
4
+ import { getHash } from "./object-hash";
5
+ export async function wait(time) {
6
+ return new Promise((resolve) => {
7
+ setTimeout(resolve, time);
41
8
  });
42
- };
43
- var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
44
- var __require = import.meta.require;
45
-
46
- // node_modules/@node-rs/xxhash-win32-x64-msvc/xxhash.win32-x64-msvc.node
47
- var require_xxhash_win32_x64_msvc = __commonJS((exports, module) => {
48
- module.exports = __require("./xxhash.win32-x64-msvc-hrdz34v7.node");
49
- });
50
-
51
- // node_modules/@node-rs/xxhash/index.js
52
- var require_xxhash = __commonJS((exports, module) => {
53
- var __filename = "G:\\certik\\certik-skynet\\lib-skynet\\node_modules\\@node-rs\\xxhash\\index.js";
54
- var { createRequire } = __require("module");
55
- __require = createRequire(__filename);
56
- var { readFileSync } = __require("fs");
57
- var nativeBinding = null;
58
- var loadErrors = [];
59
- var isMusl = () => {
60
- let musl = false;
61
- if (process.platform === "linux") {
62
- musl = isMuslFromFilesystem();
63
- if (musl === null) {
64
- musl = isMuslFromReport();
65
- }
66
- if (musl === null) {
67
- musl = isMuslFromChildProcess();
68
- }
69
- }
70
- return musl;
71
- };
72
- var isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-");
73
- var isMuslFromFilesystem = () => {
74
- try {
75
- return readFileSync("/usr/bin/ldd", "utf-8").includes("musl");
76
- } catch {
77
- return null;
78
- }
79
- };
80
- var isMuslFromReport = () => {
81
- const report = typeof process.report.getReport === "function" ? process.report.getReport() : null;
82
- if (!report) {
83
- return null;
84
- }
85
- if (report.header && report.header.glibcVersionRuntime) {
86
- return false;
87
- }
88
- if (Array.isArray(report.sharedObjects)) {
89
- if (report.sharedObjects.some(isFileMusl)) {
90
- return true;
91
- }
92
- }
93
- return false;
94
- };
95
- var isMuslFromChildProcess = () => {
96
- try {
97
- return __require("child_process").execSync("ldd --version", { encoding: "utf8" }).includes("musl");
98
- } catch (e) {
99
- return false;
100
- }
101
- };
102
- function requireNative() {
103
- if (process.platform === "android") {
104
- if (process.arch === "arm64") {
105
- try {
106
- return (()=>{throw new Error("Cannot require module "+"./xxhash.android-arm64.node");})();
107
- } catch (e) {
108
- loadErrors.push(e);
109
- }
110
- try {
111
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-android-arm64");})();
112
- } catch (e) {
113
- loadErrors.push(e);
114
- }
115
- } else if (process.arch === "arm") {
116
- try {
117
- return (()=>{throw new Error("Cannot require module "+"./xxhash.android-arm-eabi.node");})();
118
- } catch (e) {
119
- loadErrors.push(e);
120
- }
121
- try {
122
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-android-arm-eabi");})();
123
- } catch (e) {
124
- loadErrors.push(e);
125
- }
126
- } else {
127
- loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`));
128
- }
129
- } else if (process.platform === "win32") {
130
- if (process.arch === "x64") {
131
- try {
132
- return (()=>{throw new Error("Cannot require module "+"./xxhash.win32-x64-msvc.node");})();
133
- } catch (e) {
134
- loadErrors.push(e);
135
- }
136
- try {
137
- return require_xxhash_win32_x64_msvc();
138
- } catch (e) {
139
- loadErrors.push(e);
140
- }
141
- } else if (process.arch === "ia32") {
142
- try {
143
- return (()=>{throw new Error("Cannot require module "+"./xxhash.win32-ia32-msvc.node");})();
144
- } catch (e) {
145
- loadErrors.push(e);
146
- }
147
- try {
148
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-win32-ia32-msvc");})();
149
- } catch (e) {
150
- loadErrors.push(e);
151
- }
152
- } else if (process.arch === "arm64") {
153
- try {
154
- return (()=>{throw new Error("Cannot require module "+"./xxhash.win32-arm64-msvc.node");})();
155
- } catch (e) {
156
- loadErrors.push(e);
157
- }
158
- try {
159
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-win32-arm64-msvc");})();
160
- } catch (e) {
161
- loadErrors.push(e);
162
- }
163
- } else {
164
- loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`));
165
- }
166
- } else if (process.platform === "darwin") {
167
- try {
168
- return (()=>{throw new Error("Cannot require module "+"./xxhash.darwin-universal.node");})();
169
- } catch (e) {
170
- loadErrors.push(e);
171
- }
172
- try {
173
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-darwin-universal");})();
174
- } catch (e) {
175
- loadErrors.push(e);
176
- }
177
- if (process.arch === "x64") {
178
- try {
179
- return (()=>{throw new Error("Cannot require module "+"./xxhash.darwin-x64.node");})();
180
- } catch (e) {
181
- loadErrors.push(e);
182
- }
183
- try {
184
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-darwin-x64");})();
185
- } catch (e) {
186
- loadErrors.push(e);
187
- }
188
- } else if (process.arch === "arm64") {
189
- try {
190
- return (()=>{throw new Error("Cannot require module "+"./xxhash.darwin-arm64.node");})();
191
- } catch (e) {
192
- loadErrors.push(e);
193
- }
194
- try {
195
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-darwin-arm64");})();
196
- } catch (e) {
197
- loadErrors.push(e);
198
- }
199
- } else {
200
- loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`));
201
- }
202
- } else if (process.platform === "freebsd") {
203
- if (process.arch === "x64") {
204
- try {
205
- return (()=>{throw new Error("Cannot require module "+"./xxhash.freebsd-x64.node");})();
206
- } catch (e) {
207
- loadErrors.push(e);
208
- }
209
- try {
210
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-freebsd-x64");})();
211
- } catch (e) {
212
- loadErrors.push(e);
213
- }
214
- } else if (process.arch === "arm64") {
215
- try {
216
- return (()=>{throw new Error("Cannot require module "+"./xxhash.freebsd-arm64.node");})();
217
- } catch (e) {
218
- loadErrors.push(e);
219
- }
220
- try {
221
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-freebsd-arm64");})();
222
- } catch (e) {
223
- loadErrors.push(e);
224
- }
225
- } else {
226
- loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`));
227
- }
228
- } else if (process.platform === "linux") {
229
- if (process.arch === "x64") {
230
- if (isMusl()) {
231
- try {
232
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-x64-musl.node");})();
233
- } catch (e) {
234
- loadErrors.push(e);
235
- }
236
- try {
237
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-x64-musl");})();
238
- } catch (e) {
239
- loadErrors.push(e);
240
- }
241
- } else {
242
- try {
243
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-x64-gnu.node");})();
244
- } catch (e) {
245
- loadErrors.push(e);
246
- }
247
- try {
248
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-x64-gnu");})();
249
- } catch (e) {
250
- loadErrors.push(e);
251
- }
252
- }
253
- } else if (process.arch === "arm64") {
254
- if (isMusl()) {
255
- try {
256
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-arm64-musl.node");})();
257
- } catch (e) {
258
- loadErrors.push(e);
259
- }
260
- try {
261
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-arm64-musl");})();
262
- } catch (e) {
263
- loadErrors.push(e);
264
- }
265
- } else {
266
- try {
267
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-arm64-gnu.node");})();
268
- } catch (e) {
269
- loadErrors.push(e);
270
- }
271
- try {
272
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-arm64-gnu");})();
273
- } catch (e) {
274
- loadErrors.push(e);
275
- }
276
- }
277
- } else if (process.arch === "arm") {
278
- if (isMusl()) {
279
- try {
280
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-arm-musleabihf.node");})();
281
- } catch (e) {
282
- loadErrors.push(e);
283
- }
284
- try {
285
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-arm-musleabihf");})();
286
- } catch (e) {
287
- loadErrors.push(e);
288
- }
289
- } else {
290
- try {
291
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-arm-gnueabihf.node");})();
292
- } catch (e) {
293
- loadErrors.push(e);
294
- }
295
- try {
296
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-arm-gnueabihf");})();
297
- } catch (e) {
298
- loadErrors.push(e);
299
- }
300
- }
301
- } else if (process.arch === "riscv64") {
302
- if (isMusl()) {
303
- try {
304
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-riscv64-musl.node");})();
305
- } catch (e) {
306
- loadErrors.push(e);
307
- }
308
- try {
309
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-riscv64-musl");})();
310
- } catch (e) {
311
- loadErrors.push(e);
312
- }
313
- } else {
314
- try {
315
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-riscv64-gnu.node");})();
316
- } catch (e) {
317
- loadErrors.push(e);
318
- }
319
- try {
320
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-riscv64-gnu");})();
321
- } catch (e) {
322
- loadErrors.push(e);
323
- }
324
- }
325
- } else if (process.arch === "ppc64") {
326
- try {
327
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-ppc64-gnu.node");})();
328
- } catch (e) {
329
- loadErrors.push(e);
330
- }
331
- try {
332
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-ppc64-gnu");})();
333
- } catch (e) {
334
- loadErrors.push(e);
335
- }
336
- } else if (process.arch === "s390x") {
337
- try {
338
- return (()=>{throw new Error("Cannot require module "+"./xxhash.linux-s390x-gnu.node");})();
339
- } catch (e) {
340
- loadErrors.push(e);
341
- }
342
- try {
343
- return (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-linux-s390x-gnu");})();
344
- } catch (e) {
345
- loadErrors.push(e);
346
- }
347
- } else {
348
- loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`));
349
- }
350
- } else {
351
- loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`));
352
- }
353
- }
354
- nativeBinding = requireNative();
355
- if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
356
- try {
357
- nativeBinding = (()=>{throw new Error("Cannot require module "+"./xxhash.wasi.cjs");})();
358
- } catch (err) {
359
- if (process.env.NAPI_RS_FORCE_WASI) {
360
- loadErrors.push(err);
361
- }
362
- }
363
- if (!nativeBinding) {
364
- try {
365
- nativeBinding = (()=>{throw new Error("Cannot require module "+"@node-rs/xxhash-wasm32-wasi");})();
366
- } catch (err) {
367
- if (process.env.NAPI_RS_FORCE_WASI) {
368
- loadErrors.push(err);
369
- }
370
- }
371
- }
372
- }
373
- if (!nativeBinding) {
374
- if (loadErrors.length > 0) {
375
- throw new Error("Failed to load native binding", { cause: loadErrors });
376
- }
377
- throw new Error(`Failed to load native binding`);
378
- }
379
- exports.Xxh32 = nativeBinding.Xxh32;
380
- exports.Xxh64 = nativeBinding.Xxh64;
381
- exports.xxh32 = nativeBinding.xxh32;
382
- exports.xxh64 = nativeBinding.xxh64;
383
- exports.xxh3 = nativeBinding.xxh3;
384
- });
385
-
386
- // object-hash.ts
387
- var import_xxhash = __toESM(require_xxhash(), 1);
388
- function getHash(obj) {
389
- const xxh3 = import_xxhash.default.xxh3.Xxh3.withSeed();
390
- hash(obj, xxh3);
391
- return xxh3.digest().toString(16);
392
- }
393
- function hash(obj, xxh3) {
394
- if (obj === null) {
395
- xxh3.update("null");
396
- } else if (obj === undefined) {
397
- xxh3.update("undefined");
398
- } else if (typeof obj === "string") {
399
- xxh3.update(obj);
400
- } else if (typeof obj === "number") {
401
- xxh3.update(obj.toString());
402
- } else if (typeof obj === "boolean") {
403
- xxh3.update(obj.toString());
404
- } else if (typeof obj === "bigint") {
405
- xxh3.update(obj.toString());
406
- } else if (obj instanceof Date) {
407
- xxh3.update(obj.toISOString());
408
- } else if (Array.isArray(obj)) {
409
- arrayHash(obj, xxh3);
410
- } else if (obj instanceof Set) {
411
- setHash(obj, xxh3);
412
- } else if (obj instanceof Map) {
413
- mapHash(obj, xxh3);
414
- } else if (typeof obj === "object") {
415
- objectHash(obj, xxh3);
416
- } else {
417
- throw new Error(`Unsupported type: ${obj}`);
418
- }
419
- }
420
- function arrayHash(array, xxh3) {
421
- xxh3.update("[");
422
- for (const obj of array) {
423
- hash(obj, xxh3);
424
- }
425
- xxh3.update("]");
426
- }
427
- function setHash(_set, _xxh3) {
428
- throw new Error("Set hashing not implemented");
429
- }
430
- function mapHash(map, xxh3) {
431
- const array = Array.from(map.entries()).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
432
- for (const [key, value] of array) {
433
- hash(key, xxh3);
434
- hash(value, xxh3);
435
- }
436
- }
437
- function objectHash(obj, xxh3) {
438
- const array = Object.entries(obj).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
439
- for (const [key, value] of array) {
440
- hash(key, xxh3);
441
- hash(value, xxh3);
442
- }
443
9
  }
444
-
445
- // node_modules/p-throttle/index.js
446
- var registry = new FinalizationRegistry(({ signal, aborted }) => {
447
- signal?.removeEventListener("abort", aborted);
448
- });
449
- function pThrottle({ limit, interval, strict, signal, onDelay }) {
450
- if (!Number.isFinite(limit)) {
451
- throw new TypeError("Expected `limit` to be a finite number");
452
- }
453
- if (!Number.isFinite(interval)) {
454
- throw new TypeError("Expected `interval` to be a finite number");
455
- }
456
- const queue = new Map;
457
- let currentTick = 0;
458
- let activeCount = 0;
459
- function windowedDelay() {
460
- const now = Date.now();
461
- if (now - currentTick > interval) {
462
- activeCount = 1;
463
- currentTick = now;
464
- return 0;
465
- }
466
- if (activeCount < limit) {
467
- activeCount++;
468
- } else {
469
- currentTick += interval;
470
- activeCount = 1;
471
- }
472
- return currentTick - now;
473
- }
474
- const strictTicks = [];
475
- function strictDelay() {
476
- const now = Date.now();
477
- if (strictTicks.length > 0 && now - strictTicks.at(-1) > interval) {
478
- strictTicks.length = 0;
479
- }
480
- if (strictTicks.length < limit) {
481
- strictTicks.push(now);
482
- return 0;
483
- }
484
- const nextExecutionTime = strictTicks[0] + interval;
485
- strictTicks.shift();
486
- strictTicks.push(nextExecutionTime);
487
- return Math.max(0, nextExecutionTime - now);
488
- }
489
- const getDelay = strict ? strictDelay : windowedDelay;
490
- return (function_) => {
491
- const throttled = function(...arguments_) {
492
- if (!throttled.isEnabled) {
493
- return (async () => function_.apply(this, arguments_))();
494
- }
495
- let timeoutId;
496
- return new Promise((resolve, reject) => {
497
- const execute = () => {
498
- resolve(function_.apply(this, arguments_));
499
- queue.delete(timeoutId);
500
- };
501
- const delay = getDelay();
502
- if (delay > 0) {
503
- timeoutId = setTimeout(execute, delay);
504
- queue.set(timeoutId, reject);
505
- onDelay?.(...arguments_);
506
- } else {
507
- execute();
10
+ export async function exponentialRetry(func, { maxRetry, initialDuration, growFactor, test, verbose, }) {
11
+ let retries = maxRetry;
12
+ let duration = initialDuration || 5000;
13
+ const growFactorFinal = growFactor || 2;
14
+ let result = await func();
15
+ while (!test(result) && retries > 0) {
16
+ if (verbose) {
17
+ console.log("failed attempt result", result);
18
+ console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
508
19
  }
509
- });
510
- };
511
- const aborted = () => {
512
- for (const timeout of queue.keys()) {
513
- clearTimeout(timeout);
514
- queue.get(timeout)(signal.reason);
515
- }
516
- queue.clear();
517
- strictTicks.splice(0, strictTicks.length);
518
- };
519
- registry.register(throttled, { signal, aborted });
520
- signal?.throwIfAborted();
521
- signal?.addEventListener("abort", aborted, { once: true });
522
- throttled.isEnabled = true;
523
- Object.defineProperty(throttled, "queueSize", {
524
- get() {
525
- return queue.size;
526
- }
527
- });
528
- return throttled;
529
- };
530
- }
531
-
532
- // node_modules/mimic-fn/index.js
533
- var copyProperty = (to, from, property, ignoreNonConfigurable) => {
534
- if (property === "length" || property === "prototype") {
535
- return;
536
- }
537
- if (property === "arguments" || property === "caller") {
538
- return;
539
- }
540
- const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
541
- const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
542
- if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
543
- return;
544
- }
545
- Object.defineProperty(to, property, fromDescriptor);
546
- };
547
- var canCopyProperty = function(toDescriptor, fromDescriptor) {
548
- return toDescriptor === undefined || toDescriptor.configurable || toDescriptor.writable === fromDescriptor.writable && toDescriptor.enumerable === fromDescriptor.enumerable && toDescriptor.configurable === fromDescriptor.configurable && (toDescriptor.writable || toDescriptor.value === fromDescriptor.value);
549
- };
550
- var changePrototype = (to, from) => {
551
- const fromPrototype = Object.getPrototypeOf(from);
552
- if (fromPrototype === Object.getPrototypeOf(to)) {
553
- return;
554
- }
555
- Object.setPrototypeOf(to, fromPrototype);
556
- };
557
- var wrappedToString = (withName, fromBody) => `/* Wrapped ${withName}*/
558
- ${fromBody}`;
559
- var toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, "toString");
560
- var toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, "name");
561
- var changeToString = (to, from, name) => {
562
- const withName = name === "" ? "" : `with ${name.trim()}() `;
563
- const newToString = wrappedToString.bind(null, withName, from.toString());
564
- Object.defineProperty(newToString, "name", toStringName);
565
- Object.defineProperty(to, "toString", { ...toStringDescriptor, value: newToString });
566
- };
567
- function mimicFunction(to, from, { ignoreNonConfigurable = false } = {}) {
568
- const { name } = to;
569
- for (const property of Reflect.ownKeys(from)) {
570
- copyProperty(to, from, property, ignoreNonConfigurable);
571
- }
572
- changePrototype(to, from);
573
- changeToString(to, from, name);
574
- return to;
575
- }
576
-
577
- // node_modules/p-memoize/dist/index.js
578
- var cacheStore = new WeakMap;
579
- function pMemoize(fn, { cacheKey = ([firstArgument]) => firstArgument, cache = new Map } = {}) {
580
- const promiseCache = new Map;
581
- const memoized = function(...arguments_) {
582
- const key = cacheKey(arguments_);
583
- if (promiseCache.has(key)) {
584
- return promiseCache.get(key);
20
+ retries = retries - 1;
21
+ await wait(duration);
22
+ result = await func();
23
+ duration = duration * growFactorFinal;
585
24
  }
586
- const promise = (async () => {
587
- try {
588
- if (cache && await cache.has(key)) {
589
- return await cache.get(key);
590
- }
591
- const promise2 = fn.apply(this, arguments_);
592
- const result = await promise2;
593
- try {
594
- return result;
595
- } finally {
596
- if (cache) {
597
- await cache.set(key, result);
598
- }
599
- }
600
- } finally {
601
- promiseCache.delete(key);
602
- }
603
- })();
604
- promiseCache.set(key, promise);
605
- return promise;
606
- };
607
- mimicFunction(memoized, fn, {
608
- ignoreNonConfigurable: true
609
- });
610
- cacheStore.set(memoized, cache);
611
- return memoized;
25
+ if (verbose) {
26
+ console.log(`function to retry ends with status ${test(result)}, number of retries done: ${maxRetry - retries}}`);
27
+ }
28
+ return result;
29
+ }
30
+ export function withRetry(func, options) {
31
+ let retries = options?.maxRetry || 3;
32
+ let duration = options?.initialDuration || 500;
33
+ const growFactorFinal = options?.growFactor || 2;
34
+ return (async (...args) => {
35
+ do {
36
+ try {
37
+ return await func(...args);
38
+ }
39
+ catch (error) {
40
+ retries = retries - 1;
41
+ if (retries <= 0) {
42
+ throw error;
43
+ }
44
+ await wait(duration);
45
+ duration = duration * growFactorFinal;
46
+ }
47
+ } while (retries > 0);
48
+ throw new Error("unreachable");
49
+ });
612
50
  }
613
-
614
- // node_modules/quick-lru/index.js
615
- class QuickLRU extends Map {
616
- #size = 0;
617
- #cache = new Map;
618
- #oldCache = new Map;
619
- #maxSize;
620
- #maxAge;
621
- #onEviction;
622
- constructor(options = {}) {
623
- super();
624
- if (!(options.maxSize && options.maxSize > 0)) {
625
- throw new TypeError("`maxSize` must be a number greater than 0");
626
- }
627
- if (typeof options.maxAge === "number" && options.maxAge === 0) {
628
- throw new TypeError("`maxAge` must be a number greater than 0");
629
- }
630
- this.#maxSize = options.maxSize;
631
- this.#maxAge = options.maxAge || Number.POSITIVE_INFINITY;
632
- this.#onEviction = options.onEviction;
633
- }
634
- get __oldCache() {
635
- return this.#oldCache;
636
- }
637
- #emitEvictions(cache) {
638
- if (typeof this.#onEviction !== "function") {
639
- return;
640
- }
641
- for (const [key, item] of cache) {
642
- this.#onEviction(key, item.value);
643
- }
644
- }
645
- #deleteIfExpired(key, item) {
646
- if (typeof item.expiry === "number" && item.expiry <= Date.now()) {
647
- if (typeof this.#onEviction === "function") {
648
- this.#onEviction(key, item.value);
649
- }
650
- return this.delete(key);
651
- }
652
- return false;
653
- }
654
- #getOrDeleteIfExpired(key, item) {
655
- const deleted = this.#deleteIfExpired(key, item);
656
- if (deleted === false) {
657
- return item.value;
658
- }
659
- }
660
- #getItemValue(key, item) {
661
- return item.expiry ? this.#getOrDeleteIfExpired(key, item) : item.value;
662
- }
663
- #peek(key, cache) {
664
- const item = cache.get(key);
665
- return this.#getItemValue(key, item);
666
- }
667
- #set(key, value) {
668
- this.#cache.set(key, value);
669
- this.#size++;
670
- if (this.#size >= this.#maxSize) {
671
- this.#size = 0;
672
- this.#emitEvictions(this.#oldCache);
673
- this.#oldCache = this.#cache;
674
- this.#cache = new Map;
675
- }
676
- }
677
- #moveToRecent(key, item) {
678
- this.#oldCache.delete(key);
679
- this.#set(key, item);
680
- }
681
- *#entriesAscending() {
682
- for (const item of this.#oldCache) {
683
- const [key, value] = item;
684
- if (!this.#cache.has(key)) {
685
- const deleted = this.#deleteIfExpired(key, value);
686
- if (deleted === false) {
687
- yield item;
688
- }
689
- }
690
- }
691
- for (const item of this.#cache) {
692
- const [key, value] = item;
693
- const deleted = this.#deleteIfExpired(key, value);
694
- if (deleted === false) {
695
- yield item;
696
- }
697
- }
698
- }
699
- get(key) {
700
- if (this.#cache.has(key)) {
701
- const item = this.#cache.get(key);
702
- return this.#getItemValue(key, item);
703
- }
704
- if (this.#oldCache.has(key)) {
705
- const item = this.#oldCache.get(key);
706
- if (this.#deleteIfExpired(key, item) === false) {
707
- this.#moveToRecent(key, item);
708
- return item.value;
709
- }
710
- }
711
- }
712
- set(key, value, { maxAge = this.#maxAge } = {}) {
713
- const expiry = typeof maxAge === "number" && maxAge !== Number.POSITIVE_INFINITY ? Date.now() + maxAge : undefined;
714
- if (this.#cache.has(key)) {
715
- this.#cache.set(key, {
716
- value,
717
- expiry
718
- });
719
- } else {
720
- this.#set(key, { value, expiry });
721
- }
722
- return this;
723
- }
724
- has(key) {
725
- if (this.#cache.has(key)) {
726
- return !this.#deleteIfExpired(key, this.#cache.get(key));
727
- }
728
- if (this.#oldCache.has(key)) {
729
- return !this.#deleteIfExpired(key, this.#oldCache.get(key));
730
- }
731
- return false;
732
- }
733
- peek(key) {
734
- if (this.#cache.has(key)) {
735
- return this.#peek(key, this.#cache);
736
- }
737
- if (this.#oldCache.has(key)) {
738
- return this.#peek(key, this.#oldCache);
51
+ export function memoize(func, options) {
52
+ if (!options) {
53
+ options = {};
739
54
  }
740
- }
741
- delete(key) {
742
- const deleted = this.#cache.delete(key);
743
- if (deleted) {
744
- this.#size--;
55
+ if (!options.cache) {
56
+ options.cache = new QuickLRU({ maxSize: options.lruMaxSize || 10000 });
745
57
  }
746
- return this.#oldCache.delete(key) || deleted;
747
- }
748
- clear() {
749
- this.#cache.clear();
750
- this.#oldCache.clear();
751
- this.#size = 0;
752
- }
753
- resize(newSize) {
754
- if (!(newSize && newSize > 0)) {
755
- throw new TypeError("`maxSize` must be a number greater than 0");
58
+ if (!options.cacheKey) {
59
+ options.cacheKey = (args) => getHash(args);
756
60
  }
757
- const items = [...this.#entriesAscending()];
758
- const removeCount = items.length - newSize;
759
- if (removeCount < 0) {
760
- this.#cache = new Map(items);
761
- this.#oldCache = new Map;
762
- this.#size = items.length;
763
- } else {
764
- if (removeCount > 0) {
765
- this.#emitEvictions(items.slice(0, removeCount));
766
- }
767
- this.#oldCache = new Map(items.slice(removeCount));
768
- this.#cache = new Map;
769
- this.#size = 0;
770
- }
771
- this.#maxSize = newSize;
772
- }
773
- *keys() {
774
- for (const [key] of this) {
775
- yield key;
776
- }
777
- }
778
- *values() {
779
- for (const [, value] of this) {
780
- yield value;
781
- }
782
- }
783
- *[Symbol.iterator]() {
784
- for (const item of this.#cache) {
785
- const [key, value] = item;
786
- const deleted = this.#deleteIfExpired(key, value);
787
- if (deleted === false) {
788
- yield [key, value.value];
789
- }
790
- }
791
- for (const item of this.#oldCache) {
792
- const [key, value] = item;
793
- if (!this.#cache.has(key)) {
794
- const deleted = this.#deleteIfExpired(key, value);
795
- if (deleted === false) {
796
- yield [key, value.value];
797
- }
798
- }
799
- }
800
- }
801
- *entriesDescending() {
802
- let items = [...this.#cache];
803
- for (let i = items.length - 1;i >= 0; --i) {
804
- const item = items[i];
805
- const [key, value] = item;
806
- const deleted = this.#deleteIfExpired(key, value);
807
- if (deleted === false) {
808
- yield [key, value.value];
809
- }
810
- }
811
- items = [...this.#oldCache];
812
- for (let i = items.length - 1;i >= 0; --i) {
813
- const item = items[i];
814
- const [key, value] = item;
815
- if (!this.#cache.has(key)) {
816
- const deleted = this.#deleteIfExpired(key, value);
817
- if (deleted === false) {
818
- yield [key, value.value];
819
- }
820
- }
821
- }
822
- }
823
- *entriesAscending() {
824
- for (const [key, value] of this.#entriesAscending()) {
825
- yield [key, value.value];
826
- }
827
- }
828
- get size() {
829
- if (!this.#size) {
830
- return this.#oldCache.size;
831
- }
832
- let oldCacheSize = 0;
833
- for (const key of this.#oldCache.keys()) {
834
- if (!this.#cache.has(key)) {
835
- oldCacheSize++;
836
- }
837
- }
838
- return Math.min(this.#size + oldCacheSize, this.#maxSize);
839
- }
840
- get maxSize() {
841
- return this.#maxSize;
842
- }
843
- entries() {
844
- return this.entriesAscending();
845
- }
846
- forEach(callbackFunction, thisArgument = this) {
847
- for (const [key, value] of this.entriesAscending()) {
848
- callbackFunction.call(thisArgument, value, key, this);
849
- }
850
- }
851
- get [Symbol.toStringTag]() {
852
- return JSON.stringify([...this.entriesAscending()]);
853
- }
854
- }
855
-
856
- // availability.ts
857
- async function wait(time) {
858
- return new Promise((resolve) => {
859
- setTimeout(resolve, time);
860
- });
861
- }
862
- async function exponentialRetry(func, {
863
- maxRetry,
864
- initialDuration,
865
- growFactor,
866
- test,
867
- verbose
868
- }) {
869
- let retries = maxRetry;
870
- let duration = initialDuration || 5000;
871
- const growFactorFinal = growFactor || 2;
872
- let result = await func();
873
- while (!test(result) && retries > 0) {
874
- if (verbose) {
875
- console.log("failed attempt result", result);
876
- console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
877
- }
878
- retries = retries - 1;
879
- await wait(duration);
880
- result = await func();
881
- duration = duration * growFactorFinal;
882
- }
883
- if (verbose) {
884
- console.log(`function to retry ends with status ${test(result)}, number of retries done: ${maxRetry - retries}}`);
885
- }
886
- return result;
887
- }
888
- function withRetry(func, options) {
889
- let retries = options?.maxRetry || 3;
890
- let duration = options?.initialDuration || 500;
891
- const growFactorFinal = options?.growFactor || 2;
892
- return async (...args) => {
893
- do {
894
- try {
895
- return await func(...args);
896
- } catch (error) {
897
- retries = retries - 1;
898
- if (retries <= 0) {
899
- throw error;
900
- }
901
- await wait(duration);
902
- duration = duration * growFactorFinal;
903
- }
904
- } while (retries > 0);
905
- throw new Error("unreachable");
906
- };
907
- }
908
- function memoize(func, options) {
909
- if (!options) {
910
- options = {};
911
- }
912
- if (!options.cache) {
913
- options.cache = new QuickLRU({ maxSize: options.lruMaxSize || 1e4 });
914
- }
915
- if (!options.cacheKey) {
916
- options.cacheKey = (args) => getHash(args);
917
- }
918
- return pMemoize(func, options);
61
+ return pMemoize(func, options);
919
62
  }
920
- export {
921
- withRetry,
922
- wait,
923
- pThrottle as throttle,
924
- memoize,
925
- exponentialRetry
926
- };
63
+ export { pThrottle as throttle };