@ztimson/utils 0.27.11 → 0.27.13
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/dist/index.cjs +399 -417
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +394 -412
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
1
|
class ArgParser {
|
|
5
2
|
/**
|
|
6
3
|
* Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
|
|
@@ -11,19 +8,12 @@ class ArgParser {
|
|
|
11
8
|
* @param {string[]} examples Additional examples to display
|
|
12
9
|
*/
|
|
13
10
|
constructor(name, desc, argList = [], examples = []) {
|
|
14
|
-
__publicField(this, "commands", []);
|
|
15
|
-
__publicField(this, "args", []);
|
|
16
|
-
__publicField(this, "flags", []);
|
|
17
|
-
__publicField(this, "defaults");
|
|
18
11
|
this.name = name;
|
|
19
12
|
this.desc = desc;
|
|
20
13
|
this.argList = argList;
|
|
21
14
|
this.examples = examples;
|
|
22
15
|
this.commands = argList.filter((arg) => arg instanceof ArgParser);
|
|
23
|
-
this.args = argList.filter((arg) =>
|
|
24
|
-
var _a;
|
|
25
|
-
return !(arg instanceof ArgParser) && !((_a = arg.flags) == null ? void 0 : _a.length);
|
|
26
|
-
});
|
|
16
|
+
this.args = argList.filter((arg) => !(arg instanceof ArgParser) && !arg.flags?.length);
|
|
27
17
|
this.flags = [
|
|
28
18
|
...argList.filter((arg) => !(arg instanceof ArgParser) && arg.flags && arg.flags.length),
|
|
29
19
|
{ name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false }
|
|
@@ -36,6 +26,10 @@ class ArgParser {
|
|
|
36
26
|
`--help ${this.commands.length ? "[COMMAND]" : ""}`
|
|
37
27
|
].filter((e) => !!e);
|
|
38
28
|
}
|
|
29
|
+
commands = [];
|
|
30
|
+
args = [];
|
|
31
|
+
flags = [];
|
|
32
|
+
defaults;
|
|
39
33
|
/**
|
|
40
34
|
* Parse an array into an arguments dictionary using the configuration.
|
|
41
35
|
*
|
|
@@ -43,7 +37,6 @@ class ArgParser {
|
|
|
43
37
|
* @returns {object} Dictionary of arguments with defaults applied
|
|
44
38
|
*/
|
|
45
39
|
parse(args) {
|
|
46
|
-
var _a;
|
|
47
40
|
let extras = [], parsed = { ...this.defaults, "_error": [] }, queue = [...args];
|
|
48
41
|
while (queue.length) {
|
|
49
42
|
let arg = queue.splice(0, 1)[0];
|
|
@@ -53,10 +46,7 @@ class ArgParser {
|
|
|
53
46
|
arg = `-${arg[1]}`;
|
|
54
47
|
}
|
|
55
48
|
const combined = arg.split("=");
|
|
56
|
-
const argDef = this.flags.find((flag) =>
|
|
57
|
-
var _a2;
|
|
58
|
-
return (_a2 = flag.flags) == null ? void 0 : _a2.includes(combined[0] || arg);
|
|
59
|
-
});
|
|
49
|
+
const argDef = this.flags.find((flag) => flag.flags?.includes(combined[0] || arg));
|
|
60
50
|
if (argDef == null) {
|
|
61
51
|
extras.push(arg);
|
|
62
52
|
continue;
|
|
@@ -84,7 +74,7 @@ class ArgParser {
|
|
|
84
74
|
if (!arg.optional && !extras.length) parsed["_error"].push(`Argument missing: ${arg.name.toUpperCase()}`);
|
|
85
75
|
if (extras.length) parsed[arg.name] = extras.splice(0, 1)[0];
|
|
86
76
|
});
|
|
87
|
-
const extraKey =
|
|
77
|
+
const extraKey = this.args.find((arg) => arg.extras)?.name || "_extra";
|
|
88
78
|
parsed[extraKey] = extras;
|
|
89
79
|
return parsed;
|
|
90
80
|
}
|
|
@@ -107,8 +97,7 @@ ${opts.message || this.desc}`;
|
|
|
107
97
|
msg += "\n\nUsage: " + this.examples.map((ex) => `${this.name} ${ex}`).join("\n ");
|
|
108
98
|
if (this.args.length) msg += "\n\n " + this.args.map((arg) => `${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join("\n ");
|
|
109
99
|
msg += "\n\nOptions:\n " + this.flags.map((flag) => {
|
|
110
|
-
|
|
111
|
-
const flags = ((_a = flag.flags) == null ? void 0 : _a.join(", ")) || "";
|
|
100
|
+
const flags = flag.flags?.join(", ") || "";
|
|
112
101
|
return `${flags}${spacer(flags)}${flag.desc}`;
|
|
113
102
|
}).join("\n ");
|
|
114
103
|
if (this.commands.length) msg += "\n\nCommands:\n " + this.commands.map((command) => `${command.name}${spacer(command.name)}${command.desc}`).join("\n ");
|
|
@@ -151,7 +140,7 @@ function applyDeltas(base, ...deltas) {
|
|
|
151
140
|
}
|
|
152
141
|
return result;
|
|
153
142
|
}
|
|
154
|
-
for (const d of deltas.flat()) base = applyDelta(base,
|
|
143
|
+
for (const d of deltas.flat()) base = applyDelta(base, d?.delta ?? d);
|
|
155
144
|
return base;
|
|
156
145
|
}
|
|
157
146
|
function calcDelta(old, updated) {
|
|
@@ -159,8 +148,8 @@ function calcDelta(old, updated) {
|
|
|
159
148
|
const delta = {};
|
|
160
149
|
const isObj = (v) => v && typeof v === "object" && !Array.isArray(v);
|
|
161
150
|
for (const key of /* @__PURE__ */ new Set([...old ? Object.keys(old) : [], ...updated ? Object.keys(updated) : []])) {
|
|
162
|
-
const oldVal = old
|
|
163
|
-
const newVal = updated
|
|
151
|
+
const oldVal = old?.[key];
|
|
152
|
+
const newVal = updated?.[key];
|
|
164
153
|
if (isObj(oldVal) && isObj(newVal)) {
|
|
165
154
|
const nested = calcDelta(oldVal, newVal);
|
|
166
155
|
if (nested !== null && Object.keys(nested).length > 0) delta[key] = nested;
|
|
@@ -208,7 +197,7 @@ function dotNotation(obj, prop, set) {
|
|
|
208
197
|
if (obj == null || !prop) return void 0;
|
|
209
198
|
return prop.split(/[.[\]]/g).filter((prop2) => prop2.length).reduce((obj2, prop2, i, arr) => {
|
|
210
199
|
if (prop2[0] == '"' || prop2[0] == "'") prop2 = prop2.slice(1, -1);
|
|
211
|
-
if (!
|
|
200
|
+
if (!obj2?.hasOwnProperty(prop2)) {
|
|
212
201
|
if (set == void 0) return void 0;
|
|
213
202
|
obj2[prop2] = {};
|
|
214
203
|
}
|
|
@@ -305,7 +294,7 @@ class ASet extends Array {
|
|
|
305
294
|
*/
|
|
306
295
|
constructor(elements = []) {
|
|
307
296
|
super();
|
|
308
|
-
if (!!
|
|
297
|
+
if (!!elements?.["forEach"])
|
|
309
298
|
elements.forEach((el) => this.add(el));
|
|
310
299
|
}
|
|
311
300
|
/**
|
|
@@ -458,23 +447,12 @@ class Cache {
|
|
|
458
447
|
* @param options
|
|
459
448
|
*/
|
|
460
449
|
constructor(key, options = {}) {
|
|
461
|
-
__publicField(this, "_loading");
|
|
462
|
-
__publicField(this, "store", /* @__PURE__ */ new Map());
|
|
463
|
-
__publicField(this, "timers", /* @__PURE__ */ new Map());
|
|
464
|
-
__publicField(this, "lruOrder", []);
|
|
465
|
-
/** Whether cache is complete */
|
|
466
|
-
__publicField(this, "complete", false);
|
|
467
|
-
/** Await initial loading */
|
|
468
|
-
__publicField(this, "loading", new Promise((r) => this._loading = r));
|
|
469
|
-
/** Get all cached items */
|
|
470
|
-
__publicField(this, "values", this.all);
|
|
471
|
-
var _a, _b, _c, _d;
|
|
472
450
|
this.key = key;
|
|
473
451
|
this.options = options;
|
|
474
452
|
if (this.options.persistentStorage != null) {
|
|
475
453
|
if (typeof this.options.persistentStorage == "string")
|
|
476
454
|
this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
|
|
477
|
-
if (
|
|
455
|
+
if (this.options.persistentStorage?.storage?.database != void 0) {
|
|
478
456
|
(async () => {
|
|
479
457
|
const persists = this.options.persistentStorage;
|
|
480
458
|
const table = await persists.storage.createTable({ name: persists.key, key: this.key });
|
|
@@ -482,7 +460,7 @@ class Cache {
|
|
|
482
460
|
for (const row of rows) this.store.set(this.getKey(row), row);
|
|
483
461
|
this._loading();
|
|
484
462
|
})();
|
|
485
|
-
} else if (
|
|
463
|
+
} else if (this.options.persistentStorage?.storage?.getItem != void 0) {
|
|
486
464
|
const { storage, key: key2 } = this.options.persistentStorage;
|
|
487
465
|
const stored = storage.getItem(key2);
|
|
488
466
|
if (stored != null) {
|
|
@@ -509,6 +487,14 @@ class Cache {
|
|
|
509
487
|
}
|
|
510
488
|
});
|
|
511
489
|
}
|
|
490
|
+
_loading;
|
|
491
|
+
store = /* @__PURE__ */ new Map();
|
|
492
|
+
timers = /* @__PURE__ */ new Map();
|
|
493
|
+
lruOrder = [];
|
|
494
|
+
/** Whether cache is complete */
|
|
495
|
+
complete = false;
|
|
496
|
+
/** Await initial loading */
|
|
497
|
+
loading = new Promise((r) => this._loading = r);
|
|
512
498
|
getKey(value) {
|
|
513
499
|
if (!this.key) throw new Error("No key defined");
|
|
514
500
|
if (value[this.key] === void 0) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
|
|
@@ -516,10 +502,9 @@ class Cache {
|
|
|
516
502
|
}
|
|
517
503
|
/** Save item to storage */
|
|
518
504
|
save(key) {
|
|
519
|
-
var _a, _b;
|
|
520
505
|
const persists = this.options.persistentStorage;
|
|
521
|
-
if (!!
|
|
522
|
-
if (
|
|
506
|
+
if (!!persists?.storage) {
|
|
507
|
+
if (persists.storage?.database != void 0) {
|
|
523
508
|
persists.storage.createTable({ name: persists.key, key: this.key }).then((table) => {
|
|
524
509
|
if (key !== void 0) {
|
|
525
510
|
const value = this.get(key, true);
|
|
@@ -530,7 +515,7 @@ class Cache {
|
|
|
530
515
|
this.all(true).forEach((row) => table.add(row));
|
|
531
516
|
}
|
|
532
517
|
});
|
|
533
|
-
} else if (
|
|
518
|
+
} else if (persists.storage?.setItem != void 0) {
|
|
534
519
|
const obj = {};
|
|
535
520
|
for (const [k, v] of this.store.entries()) obj[k] = v;
|
|
536
521
|
persists.storage.setItem(persists.key, JSONSanitize(obj));
|
|
@@ -562,7 +547,7 @@ class Cache {
|
|
|
562
547
|
const out = [];
|
|
563
548
|
for (const v of this.store.values()) {
|
|
564
549
|
const val = v;
|
|
565
|
-
if (expired || !
|
|
550
|
+
if (expired || !val?._expired) out.push(deepCopy(val));
|
|
566
551
|
}
|
|
567
552
|
return out;
|
|
568
553
|
}
|
|
@@ -607,7 +592,7 @@ class Cache {
|
|
|
607
592
|
const out = [];
|
|
608
593
|
for (const [k, v] of this.store.entries()) {
|
|
609
594
|
const val = v;
|
|
610
|
-
if (expired || !
|
|
595
|
+
if (expired || !val?._expired) out.push([k, deepCopy(val)]);
|
|
611
596
|
}
|
|
612
597
|
return out;
|
|
613
598
|
}
|
|
@@ -637,7 +622,7 @@ class Cache {
|
|
|
637
622
|
const raw = this.store.get(key);
|
|
638
623
|
if (raw == null) return null;
|
|
639
624
|
this.touchLRU(key);
|
|
640
|
-
const isExpired = raw
|
|
625
|
+
const isExpired = raw?._expired;
|
|
641
626
|
if (expired || !isExpired) return deepCopy(raw);
|
|
642
627
|
return null;
|
|
643
628
|
}
|
|
@@ -646,7 +631,7 @@ class Cache {
|
|
|
646
631
|
const out = [];
|
|
647
632
|
for (const [k, v] of this.store.entries()) {
|
|
648
633
|
const val = v;
|
|
649
|
-
if (expired || !
|
|
634
|
+
if (expired || !val?._expired) out.push(k);
|
|
650
635
|
}
|
|
651
636
|
return out;
|
|
652
637
|
}
|
|
@@ -655,7 +640,7 @@ class Cache {
|
|
|
655
640
|
const copy = {};
|
|
656
641
|
for (const [k, v] of this.store.entries()) {
|
|
657
642
|
const val = v;
|
|
658
|
-
if (expired || !
|
|
643
|
+
if (expired || !val?._expired) copy[k] = deepCopy(val);
|
|
659
644
|
}
|
|
660
645
|
return copy;
|
|
661
646
|
}
|
|
@@ -675,10 +660,12 @@ class Cache {
|
|
|
675
660
|
}
|
|
676
661
|
return this;
|
|
677
662
|
}
|
|
663
|
+
/** Get all cached items */
|
|
664
|
+
values = this.all;
|
|
678
665
|
}
|
|
679
666
|
function contrast(background) {
|
|
680
|
-
const exploded = background
|
|
681
|
-
if (!exploded ||
|
|
667
|
+
const exploded = background?.match(background.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
|
668
|
+
if (!exploded || exploded?.length < 3) return "black";
|
|
682
669
|
const [r, g, b] = exploded.map((hex) => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
|
683
670
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
684
671
|
return luminance > 0.5 ? "black" : "white";
|
|
@@ -784,7 +771,7 @@ function parseUrl(url) {
|
|
|
784
771
|
"(?:(?<protocol>[\\w\\d]+)\\:\\/\\/)?(?:(?<user>.+)\\@)?(?<host>(?<domain>[^:\\/\\?#@\\n]+)(?:\\:(?<port>\\d*))?)(?<path>\\/.*?)?(?:\\?(?<query>.*?))?(?:#(?<fragment>.*?))?$",
|
|
785
772
|
"gm"
|
|
786
773
|
).exec(url);
|
|
787
|
-
const groups =
|
|
774
|
+
const groups = processed?.groups ?? {};
|
|
788
775
|
const domains = groups.domain.split(".");
|
|
789
776
|
if (groups["port"] != null) groups.port = Number(groups.port);
|
|
790
777
|
if (domains.length > 2) {
|
|
@@ -856,7 +843,6 @@ function validateEmail(email) {
|
|
|
856
843
|
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
|
|
857
844
|
}
|
|
858
845
|
function fromCsv(csv, hasHeaders = true) {
|
|
859
|
-
var _a;
|
|
860
846
|
function parseLine(line) {
|
|
861
847
|
const columns = [];
|
|
862
848
|
let current = "", inQuotes2 = false;
|
|
@@ -887,7 +873,7 @@ function fromCsv(csv, hasHeaders = true) {
|
|
|
887
873
|
}
|
|
888
874
|
if (currentRow) rows.push(currentRow.trim());
|
|
889
875
|
let headers = hasHeaders ? rows.splice(0, 1)[0] : null;
|
|
890
|
-
if (headers) headers =
|
|
876
|
+
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h) => h.trim());
|
|
891
877
|
return rows.map((r) => {
|
|
892
878
|
const props = parseLine(r);
|
|
893
879
|
const h = headers || Array(props.length).fill(null).map((_, i) => {
|
|
@@ -940,11 +926,10 @@ function dayOfYear(date) {
|
|
|
940
926
|
return Math.ceil((date.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
|
|
941
927
|
}
|
|
942
928
|
function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz = "local") {
|
|
943
|
-
var _a;
|
|
944
929
|
if (typeof date === "number" || typeof date === "string") date = new Date(date);
|
|
945
930
|
if (isNaN(date.getTime())) throw new Error("Invalid date input");
|
|
946
931
|
const numericTz = typeof tz === "number";
|
|
947
|
-
const localTz = tz === "local" || !numericTz &&
|
|
932
|
+
const localTz = tz === "local" || !numericTz && tz.toLowerCase?.() === "local";
|
|
948
933
|
const tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? "UTC" : tz;
|
|
949
934
|
if (!numericTz && tzName !== "UTC") {
|
|
950
935
|
try {
|
|
@@ -1005,7 +990,6 @@ function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(
|
|
|
1005
990
|
return n + (s[(v - 20) % 10] || s[v] || s[0]);
|
|
1006
991
|
}
|
|
1007
992
|
function getTZOffset() {
|
|
1008
|
-
var _a2, _b;
|
|
1009
993
|
if (numericTz) {
|
|
1010
994
|
const total = tz * 60;
|
|
1011
995
|
const hours = Math.floor(Math.abs(total) / 60);
|
|
@@ -1013,17 +997,16 @@ function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(
|
|
|
1013
997
|
return `${tz >= 0 ? "+" : "-"}${String(hours).padStart(2, "0")}:${String(mins).padStart(2, "0")}`;
|
|
1014
998
|
}
|
|
1015
999
|
try {
|
|
1016
|
-
const offset =
|
|
1000
|
+
const offset = new Intl.DateTimeFormat("en-US", { timeZone: tzName, timeZoneName: "longOffset", hour: "2-digit", minute: "2-digit" }).formatToParts(date).find((p) => p.type === "timeZoneName")?.value.match(/([+-]\d{2}:\d{2})/)?.[1];
|
|
1017
1001
|
if (offset) return offset;
|
|
1018
1002
|
} catch {
|
|
1019
1003
|
}
|
|
1020
1004
|
return "+00:00";
|
|
1021
1005
|
}
|
|
1022
1006
|
function getTZAbbr() {
|
|
1023
|
-
var _a2;
|
|
1024
1007
|
if (numericTz && tz === 0) return "UTC";
|
|
1025
1008
|
try {
|
|
1026
|
-
return
|
|
1009
|
+
return new Intl.DateTimeFormat("en-US", { timeZone: tzName, timeZoneName: "short" }).formatToParts(date).find((p) => p.type === "timeZoneName")?.value || "";
|
|
1027
1010
|
} catch {
|
|
1028
1011
|
return tzName;
|
|
1029
1012
|
}
|
|
@@ -1086,10 +1069,7 @@ function timezoneOffset(tz, date = /* @__PURE__ */ new Date()) {
|
|
|
1086
1069
|
second: "2-digit"
|
|
1087
1070
|
});
|
|
1088
1071
|
const parts = dtf.formatToParts(date);
|
|
1089
|
-
const get = (type) =>
|
|
1090
|
-
var _a;
|
|
1091
|
-
return Number((_a = parts.find((v) => v.type === type)) == null ? void 0 : _a.value);
|
|
1092
|
-
};
|
|
1072
|
+
const get = (type) => Number(parts.find((v) => v.type === type)?.value);
|
|
1093
1073
|
const y = get("year");
|
|
1094
1074
|
const mo = get("month");
|
|
1095
1075
|
const d = get("day");
|
|
@@ -1101,9 +1081,7 @@ function timezoneOffset(tz, date = /* @__PURE__ */ new Date()) {
|
|
|
1101
1081
|
return Math.round((asLocal - asUTC) / 6e4);
|
|
1102
1082
|
}
|
|
1103
1083
|
class AsyncLock {
|
|
1104
|
-
|
|
1105
|
-
__publicField(this, "p", Promise.resolve());
|
|
1106
|
-
}
|
|
1084
|
+
p = Promise.resolve();
|
|
1107
1085
|
run(fn2) {
|
|
1108
1086
|
const res = this.p.then(fn2, fn2);
|
|
1109
1087
|
this.p = res.then(() => {
|
|
@@ -1114,11 +1092,6 @@ class AsyncLock {
|
|
|
1114
1092
|
}
|
|
1115
1093
|
class Database {
|
|
1116
1094
|
constructor(database, tables, version) {
|
|
1117
|
-
__publicField(this, "schemaLock", new AsyncLock());
|
|
1118
|
-
__publicField(this, "upgrading", false);
|
|
1119
|
-
__publicField(this, "connection");
|
|
1120
|
-
__publicField(this, "tables");
|
|
1121
|
-
__publicField(this, "waitForUpgrade", () => sleepWhile(() => this.upgrading));
|
|
1122
1095
|
this.database = database;
|
|
1123
1096
|
this.version = version;
|
|
1124
1097
|
this.connection = new Promise((resolve, reject) => {
|
|
@@ -1179,8 +1152,8 @@ class Database {
|
|
|
1179
1152
|
desired.difference(existingTables).forEach((name) => {
|
|
1180
1153
|
const t = this.tables.find(findByProp("name", name));
|
|
1181
1154
|
db.createObjectStore(name, {
|
|
1182
|
-
keyPath: t
|
|
1183
|
-
autoIncrement:
|
|
1155
|
+
keyPath: t?.key,
|
|
1156
|
+
autoIncrement: t?.autoIncrement || !t?.key
|
|
1184
1157
|
});
|
|
1185
1158
|
});
|
|
1186
1159
|
}
|
|
@@ -1189,9 +1162,14 @@ class Database {
|
|
|
1189
1162
|
};
|
|
1190
1163
|
});
|
|
1191
1164
|
}
|
|
1165
|
+
schemaLock = new AsyncLock();
|
|
1166
|
+
upgrading = false;
|
|
1167
|
+
connection;
|
|
1168
|
+
tables;
|
|
1192
1169
|
get ready() {
|
|
1193
1170
|
return !this.upgrading;
|
|
1194
1171
|
}
|
|
1172
|
+
waitForUpgrade = () => sleepWhile(() => this.upgrading);
|
|
1195
1173
|
async createTable(table) {
|
|
1196
1174
|
return this.schemaLock.run(async () => {
|
|
1197
1175
|
if (typeof table == "string") table = { name: table };
|
|
@@ -1227,9 +1205,6 @@ class Database {
|
|
|
1227
1205
|
}
|
|
1228
1206
|
class Table {
|
|
1229
1207
|
constructor(database, name, key = "id") {
|
|
1230
|
-
__publicField(this, "all", this.getAll);
|
|
1231
|
-
__publicField(this, "create", this.add);
|
|
1232
|
-
__publicField(this, "update", this.set);
|
|
1233
1208
|
this.database = database;
|
|
1234
1209
|
this.name = name;
|
|
1235
1210
|
this.key = key;
|
|
@@ -1251,12 +1226,14 @@ class Table {
|
|
|
1251
1226
|
add(value, key) {
|
|
1252
1227
|
return this.tx(this.name, (store) => store.add(value, key));
|
|
1253
1228
|
}
|
|
1229
|
+
all = this.getAll;
|
|
1254
1230
|
clear() {
|
|
1255
1231
|
return this.tx(this.name, (store) => store.clear());
|
|
1256
1232
|
}
|
|
1257
1233
|
count() {
|
|
1258
1234
|
return this.tx(this.name, (store) => store.count(), true);
|
|
1259
1235
|
}
|
|
1236
|
+
create = this.add;
|
|
1260
1237
|
delete(key) {
|
|
1261
1238
|
return this.tx(this.name, (store) => store.delete(key));
|
|
1262
1239
|
}
|
|
@@ -1283,17 +1260,11 @@ class Table {
|
|
|
1283
1260
|
if (!value[this.key]) return this.add(value);
|
|
1284
1261
|
return this.put(value);
|
|
1285
1262
|
}
|
|
1263
|
+
update = this.set;
|
|
1286
1264
|
}
|
|
1287
1265
|
class PromiseProgress extends Promise {
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
(value) => resolve(value),
|
|
1291
|
-
(reason) => reject(reason),
|
|
1292
|
-
(progress) => this.progress = progress
|
|
1293
|
-
));
|
|
1294
|
-
__publicField(this, "listeners", []);
|
|
1295
|
-
__publicField(this, "_progress", 0);
|
|
1296
|
-
}
|
|
1266
|
+
listeners = [];
|
|
1267
|
+
_progress = 0;
|
|
1297
1268
|
get progress() {
|
|
1298
1269
|
return this._progress;
|
|
1299
1270
|
}
|
|
@@ -1302,6 +1273,13 @@ class PromiseProgress extends Promise {
|
|
|
1302
1273
|
this._progress = p;
|
|
1303
1274
|
this.listeners.forEach((l) => l(p));
|
|
1304
1275
|
}
|
|
1276
|
+
constructor(executor) {
|
|
1277
|
+
super((resolve, reject) => executor(
|
|
1278
|
+
(value) => resolve(value),
|
|
1279
|
+
(reason) => reject(reason),
|
|
1280
|
+
(progress) => this.progress = progress
|
|
1281
|
+
));
|
|
1282
|
+
}
|
|
1305
1283
|
static from(promise) {
|
|
1306
1284
|
if (promise instanceof PromiseProgress) return promise;
|
|
1307
1285
|
return new PromiseProgress((res, rej) => promise.then((...args) => res(...args)).catch((...args) => rej(...args)));
|
|
@@ -1384,9 +1362,8 @@ function uploadWithProgress(options) {
|
|
|
1384
1362
|
});
|
|
1385
1363
|
}
|
|
1386
1364
|
class TypedEmitter {
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
}
|
|
1365
|
+
static listeners = {};
|
|
1366
|
+
listeners = {};
|
|
1390
1367
|
static emit(event, ...args) {
|
|
1391
1368
|
(this.listeners["*"] || []).forEach((l) => l(event, ...args));
|
|
1392
1369
|
(this.listeners[event.toString()] || []).forEach((l) => l(...args));
|
|
@@ -1396,19 +1373,18 @@ class TypedEmitter {
|
|
|
1396
1373
|
this.listeners[e] = (this.listeners[e] || []).filter((l) => l != listener);
|
|
1397
1374
|
}
|
|
1398
1375
|
static on(event, listener) {
|
|
1399
|
-
var _a;
|
|
1400
1376
|
const e = event.toString();
|
|
1401
1377
|
if (!this.listeners[e]) this.listeners[e] = [];
|
|
1402
|
-
|
|
1378
|
+
this.listeners[e]?.push(listener);
|
|
1403
1379
|
return () => this.off(event, listener);
|
|
1404
1380
|
}
|
|
1405
1381
|
static once(event, listener) {
|
|
1406
1382
|
return new Promise((res) => {
|
|
1407
|
-
const unsubscribe = this.on(event, (...args) => {
|
|
1383
|
+
const unsubscribe = this.on(event, ((...args) => {
|
|
1408
1384
|
res(args.length == 1 ? args[0] : args);
|
|
1409
1385
|
if (listener) listener(...args);
|
|
1410
1386
|
unsubscribe();
|
|
1411
|
-
});
|
|
1387
|
+
}));
|
|
1412
1388
|
});
|
|
1413
1389
|
}
|
|
1414
1390
|
emit(event, ...args) {
|
|
@@ -1419,34 +1395,33 @@ class TypedEmitter {
|
|
|
1419
1395
|
this.listeners[event] = (this.listeners[event] || []).filter((l) => l != listener);
|
|
1420
1396
|
}
|
|
1421
1397
|
on(event, listener) {
|
|
1422
|
-
var _a;
|
|
1423
1398
|
if (!this.listeners[event]) this.listeners[event] = [];
|
|
1424
|
-
|
|
1399
|
+
this.listeners[event]?.push(listener);
|
|
1425
1400
|
return () => this.off(event, listener);
|
|
1426
1401
|
}
|
|
1427
1402
|
once(event, listener) {
|
|
1428
1403
|
return new Promise((res) => {
|
|
1429
|
-
const unsubscribe = this.on(event, (...args) => {
|
|
1404
|
+
const unsubscribe = this.on(event, ((...args) => {
|
|
1430
1405
|
res(args.length == 1 ? args[0] : args);
|
|
1431
1406
|
if (listener) listener(...args);
|
|
1432
1407
|
unsubscribe();
|
|
1433
|
-
});
|
|
1408
|
+
}));
|
|
1434
1409
|
});
|
|
1435
1410
|
}
|
|
1436
1411
|
}
|
|
1437
|
-
__publicField(TypedEmitter, "listeners", {});
|
|
1438
1412
|
class CustomError extends Error {
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
__publicField(this, "_code");
|
|
1442
|
-
if (code != null) this._code = code;
|
|
1443
|
-
}
|
|
1413
|
+
static code = 500;
|
|
1414
|
+
_code;
|
|
1444
1415
|
get code() {
|
|
1445
1416
|
return this._code || this.constructor.code;
|
|
1446
1417
|
}
|
|
1447
1418
|
set code(c) {
|
|
1448
1419
|
this._code = c;
|
|
1449
1420
|
}
|
|
1421
|
+
constructor(message, code) {
|
|
1422
|
+
super(message);
|
|
1423
|
+
if (code != null) this._code = code;
|
|
1424
|
+
}
|
|
1450
1425
|
static from(err) {
|
|
1451
1426
|
const code = Number(err.statusCode) ?? Number(err.code);
|
|
1452
1427
|
const newErr = new this(err.message || err.toString());
|
|
@@ -1463,8 +1438,8 @@ class CustomError extends Error {
|
|
|
1463
1438
|
return this.message || super.toString();
|
|
1464
1439
|
}
|
|
1465
1440
|
}
|
|
1466
|
-
__publicField(CustomError, "code", 500);
|
|
1467
1441
|
class BadRequestError extends CustomError {
|
|
1442
|
+
static code = 400;
|
|
1468
1443
|
constructor(message = "Bad Request") {
|
|
1469
1444
|
super(message);
|
|
1470
1445
|
}
|
|
@@ -1472,8 +1447,8 @@ class BadRequestError extends CustomError {
|
|
|
1472
1447
|
return err.constructor.code == this.code;
|
|
1473
1448
|
}
|
|
1474
1449
|
}
|
|
1475
|
-
__publicField(BadRequestError, "code", 400);
|
|
1476
1450
|
class UnauthorizedError extends CustomError {
|
|
1451
|
+
static code = 401;
|
|
1477
1452
|
constructor(message = "Unauthorized") {
|
|
1478
1453
|
super(message);
|
|
1479
1454
|
}
|
|
@@ -1481,8 +1456,8 @@ class UnauthorizedError extends CustomError {
|
|
|
1481
1456
|
return err.constructor.code == this.code;
|
|
1482
1457
|
}
|
|
1483
1458
|
}
|
|
1484
|
-
__publicField(UnauthorizedError, "code", 401);
|
|
1485
1459
|
class PaymentRequiredError extends CustomError {
|
|
1460
|
+
static code = 402;
|
|
1486
1461
|
constructor(message = "Payment Required") {
|
|
1487
1462
|
super(message);
|
|
1488
1463
|
}
|
|
@@ -1490,8 +1465,8 @@ class PaymentRequiredError extends CustomError {
|
|
|
1490
1465
|
return err.constructor.code == this.code;
|
|
1491
1466
|
}
|
|
1492
1467
|
}
|
|
1493
|
-
__publicField(PaymentRequiredError, "code", 402);
|
|
1494
1468
|
class ForbiddenError extends CustomError {
|
|
1469
|
+
static code = 403;
|
|
1495
1470
|
constructor(message = "Forbidden") {
|
|
1496
1471
|
super(message);
|
|
1497
1472
|
}
|
|
@@ -1499,8 +1474,8 @@ class ForbiddenError extends CustomError {
|
|
|
1499
1474
|
return err.constructor.code == this.code;
|
|
1500
1475
|
}
|
|
1501
1476
|
}
|
|
1502
|
-
__publicField(ForbiddenError, "code", 403);
|
|
1503
1477
|
class NotFoundError extends CustomError {
|
|
1478
|
+
static code = 404;
|
|
1504
1479
|
constructor(message = "Not Found") {
|
|
1505
1480
|
super(message);
|
|
1506
1481
|
}
|
|
@@ -1508,8 +1483,8 @@ class NotFoundError extends CustomError {
|
|
|
1508
1483
|
return err.constructor.code == this.code;
|
|
1509
1484
|
}
|
|
1510
1485
|
}
|
|
1511
|
-
__publicField(NotFoundError, "code", 404);
|
|
1512
1486
|
class MethodNotAllowedError extends CustomError {
|
|
1487
|
+
static code = 405;
|
|
1513
1488
|
constructor(message = "Method Not Allowed") {
|
|
1514
1489
|
super(message);
|
|
1515
1490
|
}
|
|
@@ -1517,8 +1492,8 @@ class MethodNotAllowedError extends CustomError {
|
|
|
1517
1492
|
return err.constructor.code == this.code;
|
|
1518
1493
|
}
|
|
1519
1494
|
}
|
|
1520
|
-
__publicField(MethodNotAllowedError, "code", 405);
|
|
1521
1495
|
class NotAcceptableError extends CustomError {
|
|
1496
|
+
static code = 406;
|
|
1522
1497
|
constructor(message = "Not Acceptable") {
|
|
1523
1498
|
super(message);
|
|
1524
1499
|
}
|
|
@@ -1526,8 +1501,8 @@ class NotAcceptableError extends CustomError {
|
|
|
1526
1501
|
return err.constructor.code == this.code;
|
|
1527
1502
|
}
|
|
1528
1503
|
}
|
|
1529
|
-
__publicField(NotAcceptableError, "code", 406);
|
|
1530
1504
|
class InternalServerError extends CustomError {
|
|
1505
|
+
static code = 500;
|
|
1531
1506
|
constructor(message = "Internal Server Error") {
|
|
1532
1507
|
super(message);
|
|
1533
1508
|
}
|
|
@@ -1535,8 +1510,8 @@ class InternalServerError extends CustomError {
|
|
|
1535
1510
|
return err.constructor.code == this.code;
|
|
1536
1511
|
}
|
|
1537
1512
|
}
|
|
1538
|
-
__publicField(InternalServerError, "code", 500);
|
|
1539
1513
|
class NotImplementedError extends CustomError {
|
|
1514
|
+
static code = 501;
|
|
1540
1515
|
constructor(message = "Not Implemented") {
|
|
1541
1516
|
super(message);
|
|
1542
1517
|
}
|
|
@@ -1544,8 +1519,8 @@ class NotImplementedError extends CustomError {
|
|
|
1544
1519
|
return err.constructor.code == this.code;
|
|
1545
1520
|
}
|
|
1546
1521
|
}
|
|
1547
|
-
__publicField(NotImplementedError, "code", 501);
|
|
1548
1522
|
class BadGatewayError extends CustomError {
|
|
1523
|
+
static code = 502;
|
|
1549
1524
|
constructor(message = "Bad Gateway") {
|
|
1550
1525
|
super(message);
|
|
1551
1526
|
}
|
|
@@ -1553,8 +1528,8 @@ class BadGatewayError extends CustomError {
|
|
|
1553
1528
|
return err.constructor.code == this.code;
|
|
1554
1529
|
}
|
|
1555
1530
|
}
|
|
1556
|
-
__publicField(BadGatewayError, "code", 502);
|
|
1557
1531
|
class ServiceUnavailableError extends CustomError {
|
|
1532
|
+
static code = 503;
|
|
1558
1533
|
constructor(message = "Service Unavailable") {
|
|
1559
1534
|
super(message);
|
|
1560
1535
|
}
|
|
@@ -1562,8 +1537,8 @@ class ServiceUnavailableError extends CustomError {
|
|
|
1562
1537
|
return err.constructor.code == this.code;
|
|
1563
1538
|
}
|
|
1564
1539
|
}
|
|
1565
|
-
__publicField(ServiceUnavailableError, "code", 503);
|
|
1566
1540
|
class GatewayTimeoutError extends CustomError {
|
|
1541
|
+
static code = 504;
|
|
1567
1542
|
constructor(message = "Gateway Timeout") {
|
|
1568
1543
|
super(message);
|
|
1569
1544
|
}
|
|
@@ -1571,7 +1546,6 @@ class GatewayTimeoutError extends CustomError {
|
|
|
1571
1546
|
return err.constructor.code == this.code;
|
|
1572
1547
|
}
|
|
1573
1548
|
}
|
|
1574
|
-
__publicField(GatewayTimeoutError, "code", 504);
|
|
1575
1549
|
function errorFromCode(code, message) {
|
|
1576
1550
|
switch (code) {
|
|
1577
1551
|
case 400:
|
|
@@ -1603,6 +1577,11 @@ function errorFromCode(code, message) {
|
|
|
1603
1577
|
}
|
|
1604
1578
|
}
|
|
1605
1579
|
class HttpResponse extends Response {
|
|
1580
|
+
data;
|
|
1581
|
+
ok;
|
|
1582
|
+
redirected;
|
|
1583
|
+
type;
|
|
1584
|
+
url;
|
|
1606
1585
|
constructor(resp, stream) {
|
|
1607
1586
|
const body = [204, 205, 304].includes(resp.status) ? null : stream;
|
|
1608
1587
|
super(body, {
|
|
@@ -1610,33 +1589,30 @@ class HttpResponse extends Response {
|
|
|
1610
1589
|
status: resp.status,
|
|
1611
1590
|
statusText: resp.statusText
|
|
1612
1591
|
});
|
|
1613
|
-
__publicField(this, "data");
|
|
1614
|
-
__publicField(this, "ok");
|
|
1615
|
-
__publicField(this, "redirected");
|
|
1616
|
-
__publicField(this, "type");
|
|
1617
|
-
__publicField(this, "url");
|
|
1618
1592
|
this.ok = resp.ok;
|
|
1619
1593
|
this.redirected = resp.redirected;
|
|
1620
1594
|
this.type = resp.type;
|
|
1621
1595
|
this.url = resp.url;
|
|
1622
1596
|
}
|
|
1623
1597
|
}
|
|
1624
|
-
|
|
1598
|
+
class Http {
|
|
1599
|
+
static interceptors = {};
|
|
1600
|
+
static headers = {};
|
|
1601
|
+
interceptors = {};
|
|
1602
|
+
headers = {};
|
|
1603
|
+
url;
|
|
1625
1604
|
constructor(defaults = {}) {
|
|
1626
|
-
__publicField(this, "interceptors", {});
|
|
1627
|
-
__publicField(this, "headers", {});
|
|
1628
|
-
__publicField(this, "url");
|
|
1629
1605
|
this.url = defaults.url ?? null;
|
|
1630
1606
|
this.headers = defaults.headers || {};
|
|
1631
1607
|
if (defaults.interceptors) {
|
|
1632
|
-
defaults.interceptors.forEach((i) =>
|
|
1608
|
+
defaults.interceptors.forEach((i) => Http.addInterceptor(i));
|
|
1633
1609
|
}
|
|
1634
1610
|
}
|
|
1635
1611
|
static addInterceptor(fn2) {
|
|
1636
|
-
const key = Object.keys(
|
|
1637
|
-
|
|
1612
|
+
const key = Object.keys(Http.interceptors).length.toString();
|
|
1613
|
+
Http.interceptors[key] = fn2;
|
|
1638
1614
|
return () => {
|
|
1639
|
-
|
|
1615
|
+
Http.interceptors[key] = null;
|
|
1640
1616
|
};
|
|
1641
1617
|
}
|
|
1642
1618
|
addInterceptor(fn2) {
|
|
@@ -1647,9 +1623,8 @@ const _Http = class _Http {
|
|
|
1647
1623
|
};
|
|
1648
1624
|
}
|
|
1649
1625
|
request(opts = {}) {
|
|
1650
|
-
var _a;
|
|
1651
1626
|
if (!this.url && !opts.url) throw new Error("URL needs to be set");
|
|
1652
|
-
let url =
|
|
1627
|
+
let url = opts.url?.startsWith("http") ? opts.url : (this.url || "") + (opts.url || "");
|
|
1653
1628
|
url = url.replaceAll(/([^:]\/)\/+/g, "$1");
|
|
1654
1629
|
if (opts.fragment) url.includes("#") ? url.replace(/#.*([?\n])/g, (match, arg1) => `#${opts.fragment}${arg1}`) : `${url}#${opts.fragment}`;
|
|
1655
1630
|
if (opts.query) {
|
|
@@ -1658,7 +1633,7 @@ const _Http = class _Http {
|
|
|
1658
1633
|
}
|
|
1659
1634
|
const headers = clean({
|
|
1660
1635
|
"Content-Type": !opts.body ? void 0 : opts.body instanceof FormData ? "multipart/form-data" : "application/json",
|
|
1661
|
-
...
|
|
1636
|
+
...Http.headers,
|
|
1662
1637
|
...this.headers,
|
|
1663
1638
|
...opts.headers
|
|
1664
1639
|
});
|
|
@@ -1671,18 +1646,17 @@ const _Http = class _Http {
|
|
|
1671
1646
|
method: opts.method || (opts.body ? "POST" : "GET"),
|
|
1672
1647
|
body: opts.body
|
|
1673
1648
|
}).then(async (resp) => {
|
|
1674
|
-
|
|
1675
|
-
for (let fn2 of [...Object.values(_Http.interceptors), ...Object.values(this.interceptors)]) {
|
|
1649
|
+
for (let fn2 of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
|
|
1676
1650
|
await new Promise((res2) => fn2(resp, () => res2()));
|
|
1677
1651
|
}
|
|
1678
1652
|
const contentLength = resp.headers.get("Content-Length");
|
|
1679
1653
|
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
|
1680
1654
|
let loaded = 0;
|
|
1681
|
-
const reader =
|
|
1655
|
+
const reader = resp.body?.getReader();
|
|
1682
1656
|
const stream = new ReadableStream({
|
|
1683
1657
|
start(controller) {
|
|
1684
1658
|
function push() {
|
|
1685
|
-
reader
|
|
1659
|
+
reader?.read().then((event) => {
|
|
1686
1660
|
if (event.done) return controller.close();
|
|
1687
1661
|
loaded += event.value.byteLength;
|
|
1688
1662
|
prog(loaded / total);
|
|
@@ -1695,11 +1669,11 @@ const _Http = class _Http {
|
|
|
1695
1669
|
});
|
|
1696
1670
|
resp = new HttpResponse(resp, stream);
|
|
1697
1671
|
if (opts.decode !== false) {
|
|
1698
|
-
const content =
|
|
1699
|
-
if (content
|
|
1700
|
-
else if (content
|
|
1701
|
-
else if (content
|
|
1702
|
-
else if (content
|
|
1672
|
+
const content = resp.headers.get("Content-Type")?.toLowerCase();
|
|
1673
|
+
if (content?.includes("form")) resp.data = await resp.formData();
|
|
1674
|
+
else if (content?.includes("json")) resp.data = await resp.json();
|
|
1675
|
+
else if (content?.includes("text")) resp.data = await resp.text();
|
|
1676
|
+
else if (content?.includes("application")) resp.data = await resp.blob();
|
|
1703
1677
|
}
|
|
1704
1678
|
if (resp.ok) res(resp);
|
|
1705
1679
|
else rej(resp);
|
|
@@ -1709,10 +1683,7 @@ const _Http = class _Http {
|
|
|
1709
1683
|
}
|
|
1710
1684
|
});
|
|
1711
1685
|
}
|
|
1712
|
-
}
|
|
1713
|
-
__publicField(_Http, "interceptors", {});
|
|
1714
|
-
__publicField(_Http, "headers", {});
|
|
1715
|
-
let Http = _Http;
|
|
1686
|
+
}
|
|
1716
1687
|
function createJwt(payload, signature = "unsigned") {
|
|
1717
1688
|
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
|
|
1718
1689
|
const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
@@ -1770,49 +1741,48 @@ var LOG_LEVEL = /* @__PURE__ */ ((LOG_LEVEL2) => {
|
|
|
1770
1741
|
LOG_LEVEL2[LOG_LEVEL2["DEBUG"] = 4] = "DEBUG";
|
|
1771
1742
|
return LOG_LEVEL2;
|
|
1772
1743
|
})(LOG_LEVEL || {});
|
|
1773
|
-
|
|
1744
|
+
class Logger extends TypedEmitter {
|
|
1774
1745
|
constructor(namespace) {
|
|
1775
1746
|
super();
|
|
1776
1747
|
this.namespace = namespace;
|
|
1777
1748
|
}
|
|
1749
|
+
static LOG_LEVEL = 4;
|
|
1778
1750
|
format(...text) {
|
|
1779
1751
|
const now = /* @__PURE__ */ new Date();
|
|
1780
1752
|
const timestamp = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}.${now.getMilliseconds().toString().padEnd(3, "0")}`;
|
|
1781
1753
|
return `${timestamp}${this.namespace ? ` [${this.namespace}]` : ""} ${text.map((t) => typeof t == "string" ? t : JSONSanitize(t, 2)).join(" ")}`;
|
|
1782
1754
|
}
|
|
1783
1755
|
debug(...args) {
|
|
1784
|
-
if (
|
|
1756
|
+
if (Logger.LOG_LEVEL < 4) return;
|
|
1785
1757
|
const str = this.format(...args);
|
|
1786
|
-
|
|
1758
|
+
Logger.emit(4, str);
|
|
1787
1759
|
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
|
|
1788
1760
|
}
|
|
1789
1761
|
log(...args) {
|
|
1790
|
-
if (
|
|
1762
|
+
if (Logger.LOG_LEVEL < 3) return;
|
|
1791
1763
|
const str = this.format(...args);
|
|
1792
|
-
|
|
1764
|
+
Logger.emit(3, str);
|
|
1793
1765
|
console.log(CliEffects.CLEAR + str);
|
|
1794
1766
|
}
|
|
1795
1767
|
info(...args) {
|
|
1796
|
-
if (
|
|
1768
|
+
if (Logger.LOG_LEVEL < 2) return;
|
|
1797
1769
|
const str = this.format(...args);
|
|
1798
|
-
|
|
1770
|
+
Logger.emit(2, str);
|
|
1799
1771
|
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
|
|
1800
1772
|
}
|
|
1801
1773
|
warn(...args) {
|
|
1802
|
-
if (
|
|
1774
|
+
if (Logger.LOG_LEVEL < 1) return;
|
|
1803
1775
|
const str = this.format(...args);
|
|
1804
|
-
|
|
1776
|
+
Logger.emit(1, str);
|
|
1805
1777
|
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
|
|
1806
1778
|
}
|
|
1807
1779
|
error(...args) {
|
|
1808
|
-
if (
|
|
1780
|
+
if (Logger.LOG_LEVEL < 0) return;
|
|
1809
1781
|
const str = this.format(...args);
|
|
1810
|
-
|
|
1782
|
+
Logger.emit(0, str);
|
|
1811
1783
|
console.error(CliForeground.RED + str + CliEffects.CLEAR);
|
|
1812
1784
|
}
|
|
1813
|
-
}
|
|
1814
|
-
__publicField(_Logger, "LOG_LEVEL", 4);
|
|
1815
|
-
let Logger = _Logger;
|
|
1785
|
+
}
|
|
1816
1786
|
function dec2Frac(num, maxDen = 1e3) {
|
|
1817
1787
|
let sign = Math.sign(num);
|
|
1818
1788
|
num = Math.abs(num);
|
|
@@ -1847,22 +1817,22 @@ function compareVersions(target, vs) {
|
|
|
1847
1817
|
}
|
|
1848
1818
|
function consoleInterceptor(out = console, map) {
|
|
1849
1819
|
const logs = { debug: [], log: [], info: [], warn: [], error: [], stderr: [], stdout: [] };
|
|
1850
|
-
const cWrapper = (type) => (...args) => {
|
|
1820
|
+
const cWrapper = (type) => ((...args) => {
|
|
1851
1821
|
if (out) out[type](...args);
|
|
1852
1822
|
logs[type].push(...args);
|
|
1853
1823
|
if (type == "error") logs.stderr.push(...args);
|
|
1854
1824
|
else logs.stdout.push(...args);
|
|
1855
|
-
};
|
|
1825
|
+
});
|
|
1856
1826
|
return {
|
|
1857
|
-
debug:
|
|
1827
|
+
debug: map?.debug != "none" ? cWrapper(map?.debug || "debug") : () => {
|
|
1858
1828
|
},
|
|
1859
|
-
log:
|
|
1829
|
+
log: map?.log != "none" ? cWrapper(map?.log || "log") : () => {
|
|
1860
1830
|
},
|
|
1861
|
-
info:
|
|
1831
|
+
info: map?.info != "none" ? cWrapper(map?.info || "info") : () => {
|
|
1862
1832
|
},
|
|
1863
|
-
warn:
|
|
1833
|
+
warn: map?.warn != "none" ? cWrapper(map?.warn || "warn") : () => {
|
|
1864
1834
|
},
|
|
1865
|
-
error:
|
|
1835
|
+
error: map?.error != "none" ? cWrapper(map?.error || "error") : () => {
|
|
1866
1836
|
},
|
|
1867
1837
|
output: logs
|
|
1868
1838
|
};
|
|
@@ -1903,63 +1873,29 @@ function PES(str, ...args) {
|
|
|
1903
1873
|
if (args[i]) combined.push(args[i]);
|
|
1904
1874
|
}
|
|
1905
1875
|
const [paths, methods] = combined.join("/").split(":");
|
|
1906
|
-
return PathEvent.toString(paths, methods
|
|
1876
|
+
return PathEvent.toString(paths, methods?.split(""));
|
|
1907
1877
|
}
|
|
1908
1878
|
class PathError extends Error {
|
|
1909
1879
|
}
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
Object.assign(this, _PathEvent.pathEventCache.get(e));
|
|
1930
|
-
return;
|
|
1931
|
-
}
|
|
1932
|
-
let [p, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
|
|
1933
|
-
if (!method) method = "*";
|
|
1934
|
-
if (p === "" || p === void 0) {
|
|
1935
|
-
this.module = "";
|
|
1936
|
-
this.path = "";
|
|
1937
|
-
this.fullPath = "";
|
|
1938
|
-
this.name = "";
|
|
1939
|
-
this.methods = new ASet(["n"]);
|
|
1940
|
-
this.hasGlob = false;
|
|
1941
|
-
_PathEvent.pathEventCache.set(e, this);
|
|
1942
|
-
return;
|
|
1943
|
-
}
|
|
1944
|
-
if (p === "*") {
|
|
1945
|
-
this.module = "";
|
|
1946
|
-
this.path = "";
|
|
1947
|
-
this.fullPath = "**";
|
|
1948
|
-
this.name = "";
|
|
1949
|
-
this.methods = new ASet(["*"]);
|
|
1950
|
-
this.hasGlob = true;
|
|
1951
|
-
_PathEvent.pathEventCache.set(e, this);
|
|
1952
|
-
return;
|
|
1953
|
-
}
|
|
1954
|
-
let temp = p.split("/").filter((p2) => !!p2);
|
|
1955
|
-
this.module = temp.splice(0, 1)[0] || "";
|
|
1956
|
-
this.path = temp.join("/");
|
|
1957
|
-
this.fullPath = `${this.module}${this.module && this.path ? "/" : ""}${this.path}`;
|
|
1958
|
-
this.name = temp.pop() || "";
|
|
1959
|
-
this.hasGlob = this.fullPath.includes("*");
|
|
1960
|
-
this.methods = new ASet(method.split(""));
|
|
1961
|
-
_PathEvent.pathEventCache.set(e, this);
|
|
1962
|
-
}
|
|
1880
|
+
class PathEvent {
|
|
1881
|
+
/** First directory in path */
|
|
1882
|
+
module;
|
|
1883
|
+
/** Entire path, including the module & name */
|
|
1884
|
+
fullPath;
|
|
1885
|
+
/** Path including the name, excluding the module */
|
|
1886
|
+
path;
|
|
1887
|
+
/** Last segment of path */
|
|
1888
|
+
name;
|
|
1889
|
+
/** List of methods */
|
|
1890
|
+
methods;
|
|
1891
|
+
/** Whether this path contains glob patterns */
|
|
1892
|
+
hasGlob;
|
|
1893
|
+
/** Internal cache for PathEvent instances to avoid redundant parsing */
|
|
1894
|
+
static pathEventCache = /* @__PURE__ */ new Map();
|
|
1895
|
+
/** Cache for compiled permissions (path + required permissions → result) */
|
|
1896
|
+
static permissionCache = /* @__PURE__ */ new Map();
|
|
1897
|
+
/** Max size for permission cache before LRU eviction */
|
|
1898
|
+
static MAX_PERMISSION_CACHE_SIZE = 1e3;
|
|
1963
1899
|
/** All/Wildcard specified */
|
|
1964
1900
|
get all() {
|
|
1965
1901
|
return this.methods.has("*");
|
|
@@ -2009,13 +1945,53 @@ const _PathEvent = class _PathEvent {
|
|
|
2009
1945
|
set delete(v) {
|
|
2010
1946
|
v ? this.methods.delete("n").delete("*").add("d") : this.methods.delete("d");
|
|
2011
1947
|
}
|
|
1948
|
+
constructor(e) {
|
|
1949
|
+
if (typeof e == "object") {
|
|
1950
|
+
Object.assign(this, e);
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
if (PathEvent.pathEventCache.has(e)) {
|
|
1954
|
+
Object.assign(this, PathEvent.pathEventCache.get(e));
|
|
1955
|
+
return;
|
|
1956
|
+
}
|
|
1957
|
+
let [p, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
|
|
1958
|
+
if (!method) method = "*";
|
|
1959
|
+
if (p === "" || p === void 0) {
|
|
1960
|
+
this.module = "";
|
|
1961
|
+
this.path = "";
|
|
1962
|
+
this.fullPath = "";
|
|
1963
|
+
this.name = "";
|
|
1964
|
+
this.methods = new ASet(["n"]);
|
|
1965
|
+
this.hasGlob = false;
|
|
1966
|
+
PathEvent.pathEventCache.set(e, this);
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
if (p === "*") {
|
|
1970
|
+
this.module = "";
|
|
1971
|
+
this.path = "";
|
|
1972
|
+
this.fullPath = "**";
|
|
1973
|
+
this.name = "";
|
|
1974
|
+
this.methods = new ASet(["*"]);
|
|
1975
|
+
this.hasGlob = true;
|
|
1976
|
+
PathEvent.pathEventCache.set(e, this);
|
|
1977
|
+
return;
|
|
1978
|
+
}
|
|
1979
|
+
let temp = p.split("/").filter((p2) => !!p2);
|
|
1980
|
+
this.module = temp.splice(0, 1)[0] || "";
|
|
1981
|
+
this.path = temp.join("/");
|
|
1982
|
+
this.fullPath = `${this.module}${this.module && this.path ? "/" : ""}${this.path}`;
|
|
1983
|
+
this.name = temp.pop() || "";
|
|
1984
|
+
this.hasGlob = this.fullPath.includes("*");
|
|
1985
|
+
this.methods = new ASet(method.split(""));
|
|
1986
|
+
PathEvent.pathEventCache.set(e, this);
|
|
1987
|
+
}
|
|
2012
1988
|
/** Clear the cache of all PathEvents */
|
|
2013
1989
|
static clearCache() {
|
|
2014
|
-
|
|
1990
|
+
PathEvent.pathEventCache.clear();
|
|
2015
1991
|
}
|
|
2016
1992
|
/** Clear the permission cache */
|
|
2017
1993
|
static clearPermissionCache() {
|
|
2018
|
-
|
|
1994
|
+
PathEvent.permissionCache.clear();
|
|
2019
1995
|
}
|
|
2020
1996
|
/**
|
|
2021
1997
|
* Score a path for specificity ranking (lower = more specific = higher priority)
|
|
@@ -2049,7 +2025,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2049
2025
|
}
|
|
2050
2026
|
patternParts[patternIdx + 1];
|
|
2051
2027
|
while (pathIdx < pathParts.length) {
|
|
2052
|
-
if (
|
|
2028
|
+
if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) {
|
|
2053
2029
|
return true;
|
|
2054
2030
|
}
|
|
2055
2031
|
pathIdx++;
|
|
@@ -2079,10 +2055,10 @@ const _PathEvent = class _PathEvent {
|
|
|
2079
2055
|
* @return {PathEvent} Final combined permission
|
|
2080
2056
|
*/
|
|
2081
2057
|
static combine(...paths) {
|
|
2082
|
-
const parsed = paths.map((p) => p instanceof
|
|
2058
|
+
const parsed = paths.map((p) => p instanceof PathEvent ? p : new PathEvent(p));
|
|
2083
2059
|
const sorted = parsed.toSorted((p1, p2) => {
|
|
2084
|
-
const score1 =
|
|
2085
|
-
const score2 =
|
|
2060
|
+
const score1 = PathEvent.scoreSpecificity(p1.fullPath);
|
|
2061
|
+
const score2 = PathEvent.scoreSpecificity(p2.fullPath);
|
|
2086
2062
|
return score1 - score2;
|
|
2087
2063
|
});
|
|
2088
2064
|
let result = null;
|
|
@@ -2098,7 +2074,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2098
2074
|
}
|
|
2099
2075
|
}
|
|
2100
2076
|
}
|
|
2101
|
-
return result || new
|
|
2077
|
+
return result || new PathEvent("");
|
|
2102
2078
|
}
|
|
2103
2079
|
/**
|
|
2104
2080
|
* Filter a set of paths based on the target
|
|
@@ -2108,12 +2084,9 @@ const _PathEvent = class _PathEvent {
|
|
|
2108
2084
|
* @return {PathEvent[]} Filtered results
|
|
2109
2085
|
*/
|
|
2110
2086
|
static filter(target, ...filter) {
|
|
2111
|
-
const parsedTarget = makeArray(target).map((pe) => pe instanceof
|
|
2112
|
-
const parsedFilter = makeArray(filter).map((pe) => pe instanceof
|
|
2113
|
-
return parsedTarget.filter((t) =>
|
|
2114
|
-
const combined = _PathEvent.combine(t);
|
|
2115
|
-
return !!parsedFilter.find((r) => _PathEvent.matches(r, combined));
|
|
2116
|
-
});
|
|
2087
|
+
const parsedTarget = makeArray(target).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2088
|
+
const parsedFilter = makeArray(filter).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2089
|
+
return parsedTarget.filter((t) => !!parsedFilter.find((r) => PathEvent.matches(r, t)));
|
|
2117
2090
|
}
|
|
2118
2091
|
/**
|
|
2119
2092
|
* Check if a filter pattern matches a target path
|
|
@@ -2140,10 +2113,9 @@ const _PathEvent = class _PathEvent {
|
|
|
2140
2113
|
* @return {boolean} Whether there is any overlap
|
|
2141
2114
|
*/
|
|
2142
2115
|
static has(target, ...has) {
|
|
2143
|
-
const parsedTarget = makeArray(target).map((pe) => pe instanceof
|
|
2144
|
-
const parsedRequired = makeArray(has).map((pe) => pe instanceof
|
|
2145
|
-
|
|
2146
|
-
return !!parsedRequired.find((r) => _PathEvent.matches(r, effectiveTarget));
|
|
2116
|
+
const parsedTarget = makeArray(target).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2117
|
+
const parsedRequired = makeArray(has).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2118
|
+
return !!parsedRequired.find((r) => !!parsedTarget.find((t) => PathEvent.matches(r, t)));
|
|
2147
2119
|
}
|
|
2148
2120
|
/**
|
|
2149
2121
|
* Squash 2 sets of paths & return true if the target has all paths
|
|
@@ -2153,7 +2125,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2153
2125
|
* @return {boolean} Whether all are present
|
|
2154
2126
|
*/
|
|
2155
2127
|
static hasAll(target, ...has) {
|
|
2156
|
-
return has.filter((h) =>
|
|
2128
|
+
return has.filter((h) => PathEvent.has(target, h)).length == has.length;
|
|
2157
2129
|
}
|
|
2158
2130
|
/**
|
|
2159
2131
|
* Same as `has` but raises an error if there is no overlap
|
|
@@ -2162,7 +2134,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2162
2134
|
* @param has Target must have at least one of these path
|
|
2163
2135
|
*/
|
|
2164
2136
|
static hasFatal(target, ...has) {
|
|
2165
|
-
if (!
|
|
2137
|
+
if (!PathEvent.has(target, ...has)) throw new PathError(`Requires one of: ${makeArray(has).join(", ")}`);
|
|
2166
2138
|
}
|
|
2167
2139
|
/**
|
|
2168
2140
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
|
@@ -2171,7 +2143,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2171
2143
|
* @param has Target must have all these paths
|
|
2172
2144
|
*/
|
|
2173
2145
|
static hasAllFatal(target, ...has) {
|
|
2174
|
-
if (!
|
|
2146
|
+
if (!PathEvent.hasAll(target, ...has)) throw new PathError(`Requires all: ${makeArray(has).join(", ")}`);
|
|
2175
2147
|
}
|
|
2176
2148
|
/**
|
|
2177
2149
|
* Create event string from its components
|
|
@@ -2182,8 +2154,8 @@ const _PathEvent = class _PathEvent {
|
|
|
2182
2154
|
*/
|
|
2183
2155
|
static toString(path, methods) {
|
|
2184
2156
|
let p = makeArray(path).filter((p2) => !!p2).join("/");
|
|
2185
|
-
p = p
|
|
2186
|
-
if (methods
|
|
2157
|
+
p = p?.trim().replaceAll(/\/{2,}/g, "/").replaceAll(/(^\/|\/$)/g, "");
|
|
2158
|
+
if (methods?.length) p += `:${makeArray(methods).map((m) => m.toLowerCase()).join("")}`;
|
|
2187
2159
|
return p;
|
|
2188
2160
|
}
|
|
2189
2161
|
/**
|
|
@@ -2193,7 +2165,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2193
2165
|
* @return {boolean} Whether there is any overlap
|
|
2194
2166
|
*/
|
|
2195
2167
|
has(...has) {
|
|
2196
|
-
return
|
|
2168
|
+
return PathEvent.has(this, ...has);
|
|
2197
2169
|
}
|
|
2198
2170
|
/**
|
|
2199
2171
|
* Squash 2 sets of paths & return true if the target has all paths
|
|
@@ -2202,7 +2174,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2202
2174
|
* @return {boolean} Whether all are present
|
|
2203
2175
|
*/
|
|
2204
2176
|
hasAll(...has) {
|
|
2205
|
-
return
|
|
2177
|
+
return PathEvent.hasAll(this, ...has);
|
|
2206
2178
|
}
|
|
2207
2179
|
/**
|
|
2208
2180
|
* Same as `has` but raises an error if there is no overlap
|
|
@@ -2210,7 +2182,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2210
2182
|
* @param has Target must have at least one of these path
|
|
2211
2183
|
*/
|
|
2212
2184
|
hasFatal(...has) {
|
|
2213
|
-
return
|
|
2185
|
+
return PathEvent.hasFatal(this, ...has);
|
|
2214
2186
|
}
|
|
2215
2187
|
/**
|
|
2216
2188
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
|
@@ -2218,7 +2190,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2218
2190
|
* @param has Target must have all these paths
|
|
2219
2191
|
*/
|
|
2220
2192
|
hasAllFatal(...has) {
|
|
2221
|
-
return
|
|
2193
|
+
return PathEvent.hasAllFatal(this, ...has);
|
|
2222
2194
|
}
|
|
2223
2195
|
/**
|
|
2224
2196
|
* Filter a set of paths based on this event
|
|
@@ -2227,7 +2199,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2227
2199
|
* @return {PathEvent[]} Filtered results
|
|
2228
2200
|
*/
|
|
2229
2201
|
filter(target) {
|
|
2230
|
-
return
|
|
2202
|
+
return PathEvent.filter(target, this);
|
|
2231
2203
|
}
|
|
2232
2204
|
/**
|
|
2233
2205
|
* Create event string from its components
|
|
@@ -2235,21 +2207,14 @@ const _PathEvent = class _PathEvent {
|
|
|
2235
2207
|
* @return {string} String representation of Event
|
|
2236
2208
|
*/
|
|
2237
2209
|
toString() {
|
|
2238
|
-
return
|
|
2210
|
+
return PathEvent.toString(this.fullPath, this.methods);
|
|
2239
2211
|
}
|
|
2240
|
-
}
|
|
2241
|
-
/** Internal cache for PathEvent instances to avoid redundant parsing */
|
|
2242
|
-
__publicField(_PathEvent, "pathEventCache", /* @__PURE__ */ new Map());
|
|
2243
|
-
/** Cache for compiled permissions (path + required permissions → result) */
|
|
2244
|
-
__publicField(_PathEvent, "permissionCache", /* @__PURE__ */ new Map());
|
|
2245
|
-
/** Max size for permission cache before LRU eviction */
|
|
2246
|
-
__publicField(_PathEvent, "MAX_PERMISSION_CACHE_SIZE", 1e3);
|
|
2247
|
-
let PathEvent = _PathEvent;
|
|
2212
|
+
}
|
|
2248
2213
|
class PathEventEmitter {
|
|
2249
2214
|
constructor(prefix = "") {
|
|
2250
|
-
__publicField(this, "listeners", []);
|
|
2251
2215
|
this.prefix = prefix;
|
|
2252
2216
|
}
|
|
2217
|
+
listeners = [];
|
|
2253
2218
|
emit(event, ...args) {
|
|
2254
2219
|
const parsed = event instanceof PathEvent ? event : new PathEvent(`${this.prefix}/${event}`);
|
|
2255
2220
|
this.listeners.filter((l) => PathEvent.has(l[0], parsed)).forEach((l) => l[1](parsed, ...args));
|
|
@@ -2318,9 +2283,9 @@ function logicTest(target, condition) {
|
|
|
2318
2283
|
case "!=":
|
|
2319
2284
|
return a != b;
|
|
2320
2285
|
case "+=":
|
|
2321
|
-
return a
|
|
2286
|
+
return a?.toString().includes(b);
|
|
2322
2287
|
case "-=":
|
|
2323
|
-
return !
|
|
2288
|
+
return !a?.toString().includes(b);
|
|
2324
2289
|
case ">":
|
|
2325
2290
|
return a > b;
|
|
2326
2291
|
case ">=":
|
|
@@ -2348,176 +2313,193 @@ function logicTest(target, condition) {
|
|
|
2348
2313
|
}).length == and.length;
|
|
2349
2314
|
});
|
|
2350
2315
|
}
|
|
2351
|
-
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
2352
2316
|
var dist = {};
|
|
2353
|
-
var persist
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
__publicField(this, "options");
|
|
2317
|
+
var persist = {};
|
|
2318
|
+
var hasRequiredPersist;
|
|
2319
|
+
function requirePersist() {
|
|
2320
|
+
if (hasRequiredPersist) return persist;
|
|
2321
|
+
hasRequiredPersist = 1;
|
|
2322
|
+
Object.defineProperty(persist, "__esModule", { value: true });
|
|
2323
|
+
persist.persist = persist.Persist = void 0;
|
|
2324
|
+
class Persist {
|
|
2325
|
+
key;
|
|
2326
|
+
options;
|
|
2364
2327
|
/** Backend service to store data, must implement `Storage` interface */
|
|
2365
|
-
|
|
2328
|
+
storage;
|
|
2366
2329
|
/** Listeners which should be notified on changes */
|
|
2367
|
-
|
|
2330
|
+
watches = {};
|
|
2368
2331
|
/** Private value field */
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2332
|
+
_value;
|
|
2333
|
+
/** Current value or default if undefined */
|
|
2334
|
+
get value() {
|
|
2335
|
+
return this._value !== void 0 ? this._value : this.options?.default;
|
|
2336
|
+
}
|
|
2337
|
+
/** Set value with proxy object wrapper to sync future changes */
|
|
2338
|
+
set value(v) {
|
|
2339
|
+
if (v == null || typeof v != "object")
|
|
2340
|
+
this._value = v;
|
|
2341
|
+
else
|
|
2342
|
+
this._value = new Proxy(v, {
|
|
2343
|
+
get: (target, p) => {
|
|
2344
|
+
const f = typeof target[p] == "function";
|
|
2345
|
+
if (!f)
|
|
2346
|
+
return target[p];
|
|
2347
|
+
return (...args) => {
|
|
2348
|
+
const value = target[p](...args);
|
|
2349
|
+
this.save();
|
|
2350
|
+
return value;
|
|
2351
|
+
};
|
|
2352
|
+
},
|
|
2353
|
+
set: (target, p, newValue) => {
|
|
2354
|
+
target[p] = newValue;
|
|
2392
2355
|
this.save();
|
|
2393
|
-
return
|
|
2394
|
-
}
|
|
2356
|
+
return true;
|
|
2357
|
+
}
|
|
2358
|
+
});
|
|
2359
|
+
this.save();
|
|
2360
|
+
}
|
|
2361
|
+
/**
|
|
2362
|
+
* @param {string} key Primary key value will be stored under
|
|
2363
|
+
* @param {PersistOptions<T>} options Configure using {@link PersistOptions}
|
|
2364
|
+
*/
|
|
2365
|
+
constructor(key, options = {}) {
|
|
2366
|
+
this.key = key;
|
|
2367
|
+
this.options = options;
|
|
2368
|
+
this.storage = options.storage || localStorage;
|
|
2369
|
+
this.load();
|
|
2370
|
+
}
|
|
2371
|
+
/** Notify listeners of change */
|
|
2372
|
+
notify(value) {
|
|
2373
|
+
Object.values(this.watches).forEach((watch) => watch(value));
|
|
2374
|
+
}
|
|
2375
|
+
/** Delete value from storage */
|
|
2376
|
+
clear() {
|
|
2377
|
+
this.storage.removeItem(this.key);
|
|
2378
|
+
}
|
|
2379
|
+
/** Save current value to storage */
|
|
2380
|
+
save() {
|
|
2381
|
+
if (this._value === void 0)
|
|
2382
|
+
this.clear();
|
|
2383
|
+
else
|
|
2384
|
+
this.storage.setItem(this.key, JSON.stringify(this._value));
|
|
2385
|
+
this.notify(this.value);
|
|
2386
|
+
}
|
|
2387
|
+
/** Load value from storage */
|
|
2388
|
+
load() {
|
|
2389
|
+
if (this.storage[this.key] != void 0) {
|
|
2390
|
+
let value = JSON.parse(this.storage.getItem(this.key));
|
|
2391
|
+
if (value != null && typeof value == "object" && this.options.type)
|
|
2392
|
+
value.__proto__ = this.options.type.prototype;
|
|
2393
|
+
this.value = value;
|
|
2394
|
+
} else
|
|
2395
|
+
this.value = this.options.default || void 0;
|
|
2396
|
+
}
|
|
2397
|
+
/**
|
|
2398
|
+
* Callback function which is run when there are changes
|
|
2399
|
+
*
|
|
2400
|
+
* @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
|
|
2401
|
+
* @returns {() => void} Function which will unsubscribe the watch/callback when called
|
|
2402
|
+
*/
|
|
2403
|
+
watch(fn2) {
|
|
2404
|
+
const index = Object.keys(this.watches).length;
|
|
2405
|
+
this.watches[index] = fn2;
|
|
2406
|
+
return () => {
|
|
2407
|
+
delete this.watches[index];
|
|
2408
|
+
};
|
|
2409
|
+
}
|
|
2410
|
+
/**
|
|
2411
|
+
* Return value as JSON string
|
|
2412
|
+
*
|
|
2413
|
+
* @returns {string} Stringified object as JSON
|
|
2414
|
+
*/
|
|
2415
|
+
toString() {
|
|
2416
|
+
return JSON.stringify(this.value);
|
|
2417
|
+
}
|
|
2418
|
+
/**
|
|
2419
|
+
* Return current value
|
|
2420
|
+
*
|
|
2421
|
+
* @returns {T} Current value
|
|
2422
|
+
*/
|
|
2423
|
+
valueOf() {
|
|
2424
|
+
return this.value;
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
persist.Persist = Persist;
|
|
2428
|
+
function persist$1(options) {
|
|
2429
|
+
return (target, prop) => {
|
|
2430
|
+
const key = options?.key || `${target.constructor.name}.${prop.toString()}`;
|
|
2431
|
+
const wrapper = new Persist(key, options);
|
|
2432
|
+
Object.defineProperty(target, prop, {
|
|
2433
|
+
get: function() {
|
|
2434
|
+
return wrapper.value;
|
|
2395
2435
|
},
|
|
2396
|
-
set: (
|
|
2397
|
-
|
|
2398
|
-
this.save();
|
|
2399
|
-
return true;
|
|
2436
|
+
set: function(v) {
|
|
2437
|
+
wrapper.value = v;
|
|
2400
2438
|
}
|
|
2401
2439
|
});
|
|
2402
|
-
this.save();
|
|
2403
|
-
}
|
|
2404
|
-
/** Notify listeners of change */
|
|
2405
|
-
notify(value) {
|
|
2406
|
-
Object.values(this.watches).forEach((watch) => watch(value));
|
|
2407
|
-
}
|
|
2408
|
-
/** Delete value from storage */
|
|
2409
|
-
clear() {
|
|
2410
|
-
this.storage.removeItem(this.key);
|
|
2411
|
-
}
|
|
2412
|
-
/** Save current value to storage */
|
|
2413
|
-
save() {
|
|
2414
|
-
if (this._value === void 0)
|
|
2415
|
-
this.clear();
|
|
2416
|
-
else
|
|
2417
|
-
this.storage.setItem(this.key, JSON.stringify(this._value));
|
|
2418
|
-
this.notify(this.value);
|
|
2419
|
-
}
|
|
2420
|
-
/** Load value from storage */
|
|
2421
|
-
load() {
|
|
2422
|
-
if (this.storage[this.key] != void 0) {
|
|
2423
|
-
let value = JSON.parse(this.storage.getItem(this.key));
|
|
2424
|
-
if (value != null && typeof value == "object" && this.options.type)
|
|
2425
|
-
value.__proto__ = this.options.type.prototype;
|
|
2426
|
-
this.value = value;
|
|
2427
|
-
} else
|
|
2428
|
-
this.value = this.options.default || void 0;
|
|
2429
|
-
}
|
|
2430
|
-
/**
|
|
2431
|
-
* Callback function which is run when there are changes
|
|
2432
|
-
*
|
|
2433
|
-
* @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
|
|
2434
|
-
* @returns {() => void} Function which will unsubscribe the watch/callback when called
|
|
2435
|
-
*/
|
|
2436
|
-
watch(fn2) {
|
|
2437
|
-
const index = Object.keys(this.watches).length;
|
|
2438
|
-
this.watches[index] = fn2;
|
|
2439
|
-
return () => {
|
|
2440
|
-
delete this.watches[index];
|
|
2441
2440
|
};
|
|
2442
2441
|
}
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
*
|
|
2446
|
-
* @returns {string} Stringified object as JSON
|
|
2447
|
-
*/
|
|
2448
|
-
toString() {
|
|
2449
|
-
return JSON.stringify(this.value);
|
|
2450
|
-
}
|
|
2451
|
-
/**
|
|
2452
|
-
* Return current value
|
|
2453
|
-
*
|
|
2454
|
-
* @returns {T} Current value
|
|
2455
|
-
*/
|
|
2456
|
-
valueOf() {
|
|
2457
|
-
return this.value;
|
|
2458
|
-
}
|
|
2459
|
-
}
|
|
2460
|
-
persist$1.Persist = Persist;
|
|
2461
|
-
function persist(options) {
|
|
2462
|
-
return (target, prop) => {
|
|
2463
|
-
const key = (options == null ? void 0 : options.key) || `${target.constructor.name}.${prop.toString()}`;
|
|
2464
|
-
const wrapper = new Persist(key, options);
|
|
2465
|
-
Object.defineProperty(target, prop, {
|
|
2466
|
-
get: function() {
|
|
2467
|
-
return wrapper.value;
|
|
2468
|
-
},
|
|
2469
|
-
set: function(v) {
|
|
2470
|
-
wrapper.value = v;
|
|
2471
|
-
}
|
|
2472
|
-
});
|
|
2473
|
-
};
|
|
2442
|
+
persist.persist = persist$1;
|
|
2443
|
+
return persist;
|
|
2474
2444
|
}
|
|
2475
|
-
persist$1.persist = persist;
|
|
2476
2445
|
var memoryStorage = {};
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2446
|
+
var hasRequiredMemoryStorage;
|
|
2447
|
+
function requireMemoryStorage() {
|
|
2448
|
+
if (hasRequiredMemoryStorage) return memoryStorage;
|
|
2449
|
+
hasRequiredMemoryStorage = 1;
|
|
2450
|
+
Object.defineProperty(memoryStorage, "__esModule", { value: true });
|
|
2451
|
+
memoryStorage.MemoryStorage = void 0;
|
|
2452
|
+
class MemoryStorage {
|
|
2453
|
+
get length() {
|
|
2454
|
+
return Object.keys(this).length;
|
|
2455
|
+
}
|
|
2456
|
+
clear() {
|
|
2457
|
+
Object.keys(this).forEach((k) => this.removeItem(k));
|
|
2458
|
+
}
|
|
2459
|
+
getItem(key) {
|
|
2460
|
+
return this[key];
|
|
2461
|
+
}
|
|
2462
|
+
key(index) {
|
|
2463
|
+
return Object.keys(this)[index];
|
|
2464
|
+
}
|
|
2465
|
+
removeItem(key) {
|
|
2466
|
+
delete this[key];
|
|
2467
|
+
}
|
|
2468
|
+
setItem(key, value) {
|
|
2469
|
+
this[key] = value;
|
|
2470
|
+
}
|
|
2497
2471
|
}
|
|
2472
|
+
memoryStorage.MemoryStorage = MemoryStorage;
|
|
2473
|
+
return memoryStorage;
|
|
2474
|
+
}
|
|
2475
|
+
var hasRequiredDist;
|
|
2476
|
+
function requireDist() {
|
|
2477
|
+
if (hasRequiredDist) return dist;
|
|
2478
|
+
hasRequiredDist = 1;
|
|
2479
|
+
(function(exports$1) {
|
|
2480
|
+
var __createBinding = dist && dist.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
2481
|
+
if (k2 === void 0) k2 = k;
|
|
2482
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2483
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2484
|
+
desc = { enumerable: true, get: function() {
|
|
2485
|
+
return m[k];
|
|
2486
|
+
} };
|
|
2487
|
+
}
|
|
2488
|
+
Object.defineProperty(o, k2, desc);
|
|
2489
|
+
}) : (function(o, m, k, k2) {
|
|
2490
|
+
if (k2 === void 0) k2 = k;
|
|
2491
|
+
o[k2] = m[k];
|
|
2492
|
+
}));
|
|
2493
|
+
var __exportStar = dist && dist.__exportStar || function(m, exports$12) {
|
|
2494
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
|
|
2495
|
+
};
|
|
2496
|
+
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
2497
|
+
__exportStar(requirePersist(), exports$1);
|
|
2498
|
+
__exportStar(requireMemoryStorage(), exports$1);
|
|
2499
|
+
})(dist);
|
|
2500
|
+
return dist;
|
|
2498
2501
|
}
|
|
2499
|
-
|
|
2500
|
-
(function(exports$1) {
|
|
2501
|
-
var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
|
|
2502
|
-
if (k2 === void 0) k2 = k;
|
|
2503
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2504
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2505
|
-
desc = { enumerable: true, get: function() {
|
|
2506
|
-
return m[k];
|
|
2507
|
-
} };
|
|
2508
|
-
}
|
|
2509
|
-
Object.defineProperty(o, k2, desc);
|
|
2510
|
-
} : function(o, m, k, k2) {
|
|
2511
|
-
if (k2 === void 0) k2 = k;
|
|
2512
|
-
o[k2] = m[k];
|
|
2513
|
-
});
|
|
2514
|
-
var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports$12) {
|
|
2515
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
|
|
2516
|
-
};
|
|
2517
|
-
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
2518
|
-
__exportStar(persist$1, exports$1);
|
|
2519
|
-
__exportStar(memoryStorage, exports$1);
|
|
2520
|
-
})(dist);
|
|
2502
|
+
requireDist();
|
|
2521
2503
|
export {
|
|
2522
2504
|
ASet,
|
|
2523
2505
|
ArgParser,
|