@ztimson/utils 0.27.10 → 0.27.12
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 +521 -422
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +516 -417
- package/dist/index.mjs.map +1 -1
- package/dist/path-events.d.ts +37 -11
- 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,44 +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
|
-
}
|
|
1930
|
-
let [p, scope, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
|
|
1931
|
-
if (!method) method = scope || "*";
|
|
1932
|
-
if (p == "*" || !p && method == "*") {
|
|
1933
|
-
p = "";
|
|
1934
|
-
method = "*";
|
|
1935
|
-
}
|
|
1936
|
-
let temp = p.split("/").filter((p2) => !!p2);
|
|
1937
|
-
this.module = temp.splice(0, 1)[0] || "";
|
|
1938
|
-
this.path = temp.join("/");
|
|
1939
|
-
this.fullPath = `${this.module}${this.module && this.path ? "/" : ""}${this.path}`;
|
|
1940
|
-
this.name = temp.pop() || "";
|
|
1941
|
-
this.methods = new ASet(method.split(""));
|
|
1942
|
-
_PathEvent.pathEventCache.set(e, this);
|
|
1943
|
-
}
|
|
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;
|
|
1944
1899
|
/** All/Wildcard specified */
|
|
1945
1900
|
get all() {
|
|
1946
1901
|
return this.methods.has("*");
|
|
@@ -1962,6 +1917,13 @@ const _PathEvent = class _PathEvent {
|
|
|
1962
1917
|
set create(v) {
|
|
1963
1918
|
v ? this.methods.delete("n").delete("*").add("c") : this.methods.delete("c");
|
|
1964
1919
|
}
|
|
1920
|
+
/** Execute method specified */
|
|
1921
|
+
get execute() {
|
|
1922
|
+
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("x"));
|
|
1923
|
+
}
|
|
1924
|
+
set execute(v) {
|
|
1925
|
+
v ? this.methods.delete("n").delete("*").add("x") : this.methods.delete("x");
|
|
1926
|
+
}
|
|
1965
1927
|
/** Read method specified */
|
|
1966
1928
|
get read() {
|
|
1967
1929
|
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("r"));
|
|
@@ -1983,9 +1945,107 @@ const _PathEvent = class _PathEvent {
|
|
|
1983
1945
|
set delete(v) {
|
|
1984
1946
|
v ? this.methods.delete("n").delete("*").add("d") : this.methods.delete("d");
|
|
1985
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
|
+
}
|
|
1986
1988
|
/** Clear the cache of all PathEvents */
|
|
1987
1989
|
static clearCache() {
|
|
1988
|
-
|
|
1990
|
+
PathEvent.pathEventCache.clear();
|
|
1991
|
+
}
|
|
1992
|
+
/** Clear the permission cache */
|
|
1993
|
+
static clearPermissionCache() {
|
|
1994
|
+
PathEvent.permissionCache.clear();
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Score a path for specificity ranking (lower = more specific = higher priority)
|
|
1998
|
+
* @private
|
|
1999
|
+
*/
|
|
2000
|
+
static scoreSpecificity(path) {
|
|
2001
|
+
if (path === "**" || path === "") return Number.MAX_SAFE_INTEGER;
|
|
2002
|
+
const segments = path.split("/").filter((p) => !!p);
|
|
2003
|
+
let score = -segments.length;
|
|
2004
|
+
segments.forEach((seg) => {
|
|
2005
|
+
if (seg === "**") score += 0.5;
|
|
2006
|
+
else if (seg === "*") score += 0.25;
|
|
2007
|
+
});
|
|
2008
|
+
return score;
|
|
2009
|
+
}
|
|
2010
|
+
/**
|
|
2011
|
+
* Check if a path matches a glob pattern
|
|
2012
|
+
* @private
|
|
2013
|
+
*/
|
|
2014
|
+
static pathMatchesGlob(path, pattern) {
|
|
2015
|
+
if (pattern === path) return true;
|
|
2016
|
+
const pathParts = path.split("/").filter((p) => !!p);
|
|
2017
|
+
const patternParts = pattern.split("/").filter((p) => !!p);
|
|
2018
|
+
let pathIdx = 0;
|
|
2019
|
+
let patternIdx = 0;
|
|
2020
|
+
while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
|
|
2021
|
+
const patternPart = patternParts[patternIdx];
|
|
2022
|
+
if (patternPart === "**") {
|
|
2023
|
+
if (patternIdx === patternParts.length - 1) {
|
|
2024
|
+
return true;
|
|
2025
|
+
}
|
|
2026
|
+
patternParts[patternIdx + 1];
|
|
2027
|
+
while (pathIdx < pathParts.length) {
|
|
2028
|
+
if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) {
|
|
2029
|
+
return true;
|
|
2030
|
+
}
|
|
2031
|
+
pathIdx++;
|
|
2032
|
+
}
|
|
2033
|
+
return false;
|
|
2034
|
+
} else if (patternPart === "*") {
|
|
2035
|
+
pathIdx++;
|
|
2036
|
+
patternIdx++;
|
|
2037
|
+
} else {
|
|
2038
|
+
if (patternPart !== pathParts[pathIdx]) {
|
|
2039
|
+
return false;
|
|
2040
|
+
}
|
|
2041
|
+
pathIdx++;
|
|
2042
|
+
patternIdx++;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
if (patternIdx < patternParts.length) {
|
|
2046
|
+
return patternParts.slice(patternIdx).every((p) => p === "**");
|
|
2047
|
+
}
|
|
2048
|
+
return pathIdx === pathParts.length;
|
|
1989
2049
|
}
|
|
1990
2050
|
/**
|
|
1991
2051
|
* Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
|
|
@@ -1995,38 +2055,58 @@ const _PathEvent = class _PathEvent {
|
|
|
1995
2055
|
* @return {PathEvent} Final combined permission
|
|
1996
2056
|
*/
|
|
1997
2057
|
static combine(...paths) {
|
|
1998
|
-
|
|
1999
|
-
const
|
|
2000
|
-
const
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
if (
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2058
|
+
const parsed = paths.map((p) => p instanceof PathEvent ? p : new PathEvent(p));
|
|
2059
|
+
const sorted = parsed.toSorted((p1, p2) => {
|
|
2060
|
+
const score1 = PathEvent.scoreSpecificity(p1.fullPath);
|
|
2061
|
+
const score2 = PathEvent.scoreSpecificity(p2.fullPath);
|
|
2062
|
+
return score1 - score2;
|
|
2063
|
+
});
|
|
2064
|
+
let result = null;
|
|
2065
|
+
for (const p of sorted) {
|
|
2066
|
+
if (!result) {
|
|
2067
|
+
result = p;
|
|
2068
|
+
} else {
|
|
2069
|
+
if (result.fullPath.startsWith(p.fullPath)) {
|
|
2070
|
+
if (p.none) {
|
|
2071
|
+
break;
|
|
2072
|
+
}
|
|
2073
|
+
result.methods = new ASet([...result.methods, ...p.methods]);
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
return result || new PathEvent("");
|
|
2011
2078
|
}
|
|
2012
2079
|
/**
|
|
2013
2080
|
* Filter a set of paths based on the target
|
|
2014
2081
|
*
|
|
2015
2082
|
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
|
|
2016
|
-
* @param filter {...PathEvent} Must
|
|
2017
|
-
* @return {
|
|
2083
|
+
* @param filter {...PathEvent} Must contain one of
|
|
2084
|
+
* @return {PathEvent[]} Filtered results
|
|
2018
2085
|
*/
|
|
2019
2086
|
static filter(target, ...filter) {
|
|
2020
|
-
const parsedTarget = makeArray(target).map((pe) => pe instanceof
|
|
2021
|
-
const parsedFilter = makeArray(filter).map((pe) => pe instanceof
|
|
2022
|
-
return parsedTarget.filter((t) =>
|
|
2023
|
-
const
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
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) => {
|
|
2090
|
+
const combined = PathEvent.combine(t);
|
|
2091
|
+
return !!parsedFilter.find((r) => PathEvent.matches(r, combined));
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Check if a filter pattern matches a target path
|
|
2096
|
+
* @private
|
|
2097
|
+
*/
|
|
2098
|
+
static matches(pattern, target) {
|
|
2099
|
+
if (pattern.fullPath === "" || target.fullPath === "") return false;
|
|
2100
|
+
if (pattern.fullPath === "*" || target.fullPath === "*") return pattern.methods.has("*") || target.methods.has("*") || pattern.methods.intersection(target.methods).length > 0;
|
|
2101
|
+
const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
|
|
2102
|
+
if (!methodsMatch) return false;
|
|
2103
|
+
if (!pattern.hasGlob && !target.hasGlob) {
|
|
2104
|
+
return pattern.fullPath === target.fullPath;
|
|
2105
|
+
}
|
|
2106
|
+
if (pattern.hasGlob) {
|
|
2107
|
+
return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
|
|
2108
|
+
}
|
|
2109
|
+
return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
|
|
2030
2110
|
}
|
|
2031
2111
|
/**
|
|
2032
2112
|
* Squash 2 sets of paths & return true if any overlap is found
|
|
@@ -2036,44 +2116,38 @@ const _PathEvent = class _PathEvent {
|
|
|
2036
2116
|
* @return {boolean} Whether there is any overlap
|
|
2037
2117
|
*/
|
|
2038
2118
|
static has(target, ...has) {
|
|
2039
|
-
const parsedTarget = makeArray(target).map((pe) => pe instanceof
|
|
2040
|
-
const parsedRequired = makeArray(has).map((pe) => pe instanceof
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
const p1 = r.fullPath.includes("*") ? r.fullPath.slice(0, r.fullPath.indexOf("*")) : r.fullPath;
|
|
2044
|
-
const p2 = t.fullPath.includes("*") ? t.fullPath.slice(0, t.fullPath.indexOf("*")) : t.fullPath;
|
|
2045
|
-
const scope = p1.startsWith(p2);
|
|
2046
|
-
const methods = r.all || t.all || r.methods.intersection(t.methods).length;
|
|
2047
|
-
return (wildcard || scope) && methods;
|
|
2048
|
-
}));
|
|
2119
|
+
const parsedTarget = makeArray(target).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2120
|
+
const parsedRequired = makeArray(has).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2121
|
+
const effectiveTarget = parsedTarget.length === 1 ? parsedTarget[0] : PathEvent.combine(...parsedTarget);
|
|
2122
|
+
return !!parsedRequired.find((r) => PathEvent.matches(r, effectiveTarget));
|
|
2049
2123
|
}
|
|
2050
2124
|
/**
|
|
2051
2125
|
* Squash 2 sets of paths & return true if the target has all paths
|
|
2052
2126
|
*
|
|
2053
2127
|
* @param {string | PathEvent | (string | PathEvent)[]} target Array of Events as strings or pre-parsed
|
|
2054
2128
|
* @param has Target must have all these paths
|
|
2055
|
-
* @return {boolean} Whether
|
|
2129
|
+
* @return {boolean} Whether all are present
|
|
2056
2130
|
*/
|
|
2057
2131
|
static hasAll(target, ...has) {
|
|
2058
|
-
return has.filter((h) =>
|
|
2132
|
+
return has.filter((h) => PathEvent.has(target, h)).length == has.length;
|
|
2059
2133
|
}
|
|
2060
2134
|
/**
|
|
2061
2135
|
* Same as `has` but raises an error if there is no overlap
|
|
2062
2136
|
*
|
|
2063
|
-
* @param {string | string[]} target Array of Events as strings or pre-parsed
|
|
2137
|
+
* @param {string | PathEvent | (string | PathEvent)[]} target Array of Events as strings or pre-parsed
|
|
2064
2138
|
* @param has Target must have at least one of these path
|
|
2065
2139
|
*/
|
|
2066
2140
|
static hasFatal(target, ...has) {
|
|
2067
|
-
if (!
|
|
2141
|
+
if (!PathEvent.has(target, ...has)) throw new PathError(`Requires one of: ${makeArray(has).join(", ")}`);
|
|
2068
2142
|
}
|
|
2069
2143
|
/**
|
|
2070
2144
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
|
2071
2145
|
*
|
|
2072
|
-
* @param {string | string[]} target Array of Events as strings or pre-parsed
|
|
2146
|
+
* @param {string | PathEvent | (string | PathEvent)[]} target Array of Events as strings or pre-parsed
|
|
2073
2147
|
* @param has Target must have all these paths
|
|
2074
2148
|
*/
|
|
2075
2149
|
static hasAllFatal(target, ...has) {
|
|
2076
|
-
if (!
|
|
2150
|
+
if (!PathEvent.hasAll(target, ...has)) throw new PathError(`Requires all: ${makeArray(has).join(", ")}`);
|
|
2077
2151
|
}
|
|
2078
2152
|
/**
|
|
2079
2153
|
* Create event string from its components
|
|
@@ -2084,8 +2158,8 @@ const _PathEvent = class _PathEvent {
|
|
|
2084
2158
|
*/
|
|
2085
2159
|
static toString(path, methods) {
|
|
2086
2160
|
let p = makeArray(path).filter((p2) => !!p2).join("/");
|
|
2087
|
-
p = p
|
|
2088
|
-
if (methods
|
|
2161
|
+
p = p?.trim().replaceAll(/\/{2,}/g, "/").replaceAll(/(^\/|\/$)/g, "");
|
|
2162
|
+
if (methods?.length) p += `:${makeArray(methods).map((m) => m.toLowerCase()).join("")}`;
|
|
2089
2163
|
return p;
|
|
2090
2164
|
}
|
|
2091
2165
|
/**
|
|
@@ -2095,16 +2169,16 @@ const _PathEvent = class _PathEvent {
|
|
|
2095
2169
|
* @return {boolean} Whether there is any overlap
|
|
2096
2170
|
*/
|
|
2097
2171
|
has(...has) {
|
|
2098
|
-
return
|
|
2172
|
+
return PathEvent.has(this, ...has);
|
|
2099
2173
|
}
|
|
2100
2174
|
/**
|
|
2101
2175
|
* Squash 2 sets of paths & return true if the target has all paths
|
|
2102
2176
|
*
|
|
2103
2177
|
* @param has Target must have all these paths
|
|
2104
|
-
* @return {boolean} Whether
|
|
2178
|
+
* @return {boolean} Whether all are present
|
|
2105
2179
|
*/
|
|
2106
2180
|
hasAll(...has) {
|
|
2107
|
-
return
|
|
2181
|
+
return PathEvent.hasAll(this, ...has);
|
|
2108
2182
|
}
|
|
2109
2183
|
/**
|
|
2110
2184
|
* Same as `has` but raises an error if there is no overlap
|
|
@@ -2112,7 +2186,7 @@ const _PathEvent = class _PathEvent {
|
|
|
2112
2186
|
* @param has Target must have at least one of these path
|
|
2113
2187
|
*/
|
|
2114
2188
|
hasFatal(...has) {
|
|
2115
|
-
return
|
|
2189
|
+
return PathEvent.hasFatal(this, ...has);
|
|
2116
2190
|
}
|
|
2117
2191
|
/**
|
|
2118
2192
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
|
@@ -2120,16 +2194,16 @@ const _PathEvent = class _PathEvent {
|
|
|
2120
2194
|
* @param has Target must have all these paths
|
|
2121
2195
|
*/
|
|
2122
2196
|
hasAllFatal(...has) {
|
|
2123
|
-
return
|
|
2197
|
+
return PathEvent.hasAllFatal(this, ...has);
|
|
2124
2198
|
}
|
|
2125
2199
|
/**
|
|
2126
2200
|
* Filter a set of paths based on this event
|
|
2127
2201
|
*
|
|
2128
2202
|
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
|
|
2129
|
-
* @return {
|
|
2203
|
+
* @return {PathEvent[]} Filtered results
|
|
2130
2204
|
*/
|
|
2131
2205
|
filter(target) {
|
|
2132
|
-
return
|
|
2206
|
+
return PathEvent.filter(target, this);
|
|
2133
2207
|
}
|
|
2134
2208
|
/**
|
|
2135
2209
|
* Create event string from its components
|
|
@@ -2137,29 +2211,37 @@ const _PathEvent = class _PathEvent {
|
|
|
2137
2211
|
* @return {string} String representation of Event
|
|
2138
2212
|
*/
|
|
2139
2213
|
toString() {
|
|
2140
|
-
return
|
|
2214
|
+
return PathEvent.toString(this.fullPath, this.methods);
|
|
2141
2215
|
}
|
|
2142
|
-
}
|
|
2143
|
-
/** Internal cache for PathEvent instances to avoid redundant parsing */
|
|
2144
|
-
__publicField(_PathEvent, "pathEventCache", /* @__PURE__ */ new Map());
|
|
2145
|
-
let PathEvent = _PathEvent;
|
|
2216
|
+
}
|
|
2146
2217
|
class PathEventEmitter {
|
|
2147
2218
|
constructor(prefix = "") {
|
|
2148
|
-
__publicField(this, "listeners", []);
|
|
2149
2219
|
this.prefix = prefix;
|
|
2150
2220
|
}
|
|
2221
|
+
listeners = [];
|
|
2151
2222
|
emit(event, ...args) {
|
|
2152
2223
|
const parsed = event instanceof PathEvent ? event : new PathEvent(`${this.prefix}/${event}`);
|
|
2153
|
-
this.listeners.filter((l) => PathEvent.has(l[0], parsed)).forEach(
|
|
2224
|
+
this.listeners.filter((l) => PathEvent.has(l[0], parsed)).forEach((l) => l[1](parsed, ...args));
|
|
2154
2225
|
}
|
|
2155
2226
|
off(listener) {
|
|
2156
2227
|
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
2157
2228
|
}
|
|
2158
2229
|
on(event, listener) {
|
|
2159
2230
|
makeArray(event).forEach((e) => {
|
|
2160
|
-
|
|
2231
|
+
let fullEvent;
|
|
2232
|
+
if (typeof e === "string") {
|
|
2233
|
+
if (e[0] === ":" && this.prefix) {
|
|
2234
|
+
fullEvent = `${this.prefix}${e}`;
|
|
2235
|
+
} else if (this.prefix) {
|
|
2236
|
+
fullEvent = `${this.prefix}/${e}`;
|
|
2237
|
+
} else {
|
|
2238
|
+
fullEvent = e;
|
|
2239
|
+
}
|
|
2240
|
+
} else {
|
|
2241
|
+
fullEvent = e instanceof PathEvent ? PathEvent.toString(e.fullPath, e.methods) : e;
|
|
2242
|
+
}
|
|
2161
2243
|
this.listeners.push([
|
|
2162
|
-
|
|
2244
|
+
new PathEvent(fullEvent),
|
|
2163
2245
|
listener
|
|
2164
2246
|
]);
|
|
2165
2247
|
});
|
|
@@ -2175,7 +2257,7 @@ class PathEventEmitter {
|
|
|
2175
2257
|
});
|
|
2176
2258
|
}
|
|
2177
2259
|
relayEvents(emitter) {
|
|
2178
|
-
emitter.on("
|
|
2260
|
+
emitter.on("**", (event, ...args) => this.emit(event, ...args));
|
|
2179
2261
|
}
|
|
2180
2262
|
}
|
|
2181
2263
|
function search(rows, search2, regex, transform = (r) => r) {
|
|
@@ -2205,9 +2287,9 @@ function logicTest(target, condition) {
|
|
|
2205
2287
|
case "!=":
|
|
2206
2288
|
return a != b;
|
|
2207
2289
|
case "+=":
|
|
2208
|
-
return a
|
|
2290
|
+
return a?.toString().includes(b);
|
|
2209
2291
|
case "-=":
|
|
2210
|
-
return !
|
|
2292
|
+
return !a?.toString().includes(b);
|
|
2211
2293
|
case ">":
|
|
2212
2294
|
return a > b;
|
|
2213
2295
|
case ">=":
|
|
@@ -2235,176 +2317,193 @@ function logicTest(target, condition) {
|
|
|
2235
2317
|
}).length == and.length;
|
|
2236
2318
|
});
|
|
2237
2319
|
}
|
|
2238
|
-
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
2239
2320
|
var dist = {};
|
|
2240
|
-
var persist
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
__publicField(this, "options");
|
|
2321
|
+
var persist = {};
|
|
2322
|
+
var hasRequiredPersist;
|
|
2323
|
+
function requirePersist() {
|
|
2324
|
+
if (hasRequiredPersist) return persist;
|
|
2325
|
+
hasRequiredPersist = 1;
|
|
2326
|
+
Object.defineProperty(persist, "__esModule", { value: true });
|
|
2327
|
+
persist.persist = persist.Persist = void 0;
|
|
2328
|
+
class Persist {
|
|
2329
|
+
key;
|
|
2330
|
+
options;
|
|
2251
2331
|
/** Backend service to store data, must implement `Storage` interface */
|
|
2252
|
-
|
|
2332
|
+
storage;
|
|
2253
2333
|
/** Listeners which should be notified on changes */
|
|
2254
|
-
|
|
2334
|
+
watches = {};
|
|
2255
2335
|
/** Private value field */
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2336
|
+
_value;
|
|
2337
|
+
/** Current value or default if undefined */
|
|
2338
|
+
get value() {
|
|
2339
|
+
return this._value !== void 0 ? this._value : this.options?.default;
|
|
2340
|
+
}
|
|
2341
|
+
/** Set value with proxy object wrapper to sync future changes */
|
|
2342
|
+
set value(v) {
|
|
2343
|
+
if (v == null || typeof v != "object")
|
|
2344
|
+
this._value = v;
|
|
2345
|
+
else
|
|
2346
|
+
this._value = new Proxy(v, {
|
|
2347
|
+
get: (target, p) => {
|
|
2348
|
+
const f = typeof target[p] == "function";
|
|
2349
|
+
if (!f)
|
|
2350
|
+
return target[p];
|
|
2351
|
+
return (...args) => {
|
|
2352
|
+
const value = target[p](...args);
|
|
2353
|
+
this.save();
|
|
2354
|
+
return value;
|
|
2355
|
+
};
|
|
2356
|
+
},
|
|
2357
|
+
set: (target, p, newValue) => {
|
|
2358
|
+
target[p] = newValue;
|
|
2279
2359
|
this.save();
|
|
2280
|
-
return
|
|
2281
|
-
}
|
|
2360
|
+
return true;
|
|
2361
|
+
}
|
|
2362
|
+
});
|
|
2363
|
+
this.save();
|
|
2364
|
+
}
|
|
2365
|
+
/**
|
|
2366
|
+
* @param {string} key Primary key value will be stored under
|
|
2367
|
+
* @param {PersistOptions<T>} options Configure using {@link PersistOptions}
|
|
2368
|
+
*/
|
|
2369
|
+
constructor(key, options = {}) {
|
|
2370
|
+
this.key = key;
|
|
2371
|
+
this.options = options;
|
|
2372
|
+
this.storage = options.storage || localStorage;
|
|
2373
|
+
this.load();
|
|
2374
|
+
}
|
|
2375
|
+
/** Notify listeners of change */
|
|
2376
|
+
notify(value) {
|
|
2377
|
+
Object.values(this.watches).forEach((watch) => watch(value));
|
|
2378
|
+
}
|
|
2379
|
+
/** Delete value from storage */
|
|
2380
|
+
clear() {
|
|
2381
|
+
this.storage.removeItem(this.key);
|
|
2382
|
+
}
|
|
2383
|
+
/** Save current value to storage */
|
|
2384
|
+
save() {
|
|
2385
|
+
if (this._value === void 0)
|
|
2386
|
+
this.clear();
|
|
2387
|
+
else
|
|
2388
|
+
this.storage.setItem(this.key, JSON.stringify(this._value));
|
|
2389
|
+
this.notify(this.value);
|
|
2390
|
+
}
|
|
2391
|
+
/** Load value from storage */
|
|
2392
|
+
load() {
|
|
2393
|
+
if (this.storage[this.key] != void 0) {
|
|
2394
|
+
let value = JSON.parse(this.storage.getItem(this.key));
|
|
2395
|
+
if (value != null && typeof value == "object" && this.options.type)
|
|
2396
|
+
value.__proto__ = this.options.type.prototype;
|
|
2397
|
+
this.value = value;
|
|
2398
|
+
} else
|
|
2399
|
+
this.value = this.options.default || void 0;
|
|
2400
|
+
}
|
|
2401
|
+
/**
|
|
2402
|
+
* Callback function which is run when there are changes
|
|
2403
|
+
*
|
|
2404
|
+
* @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
|
|
2405
|
+
* @returns {() => void} Function which will unsubscribe the watch/callback when called
|
|
2406
|
+
*/
|
|
2407
|
+
watch(fn2) {
|
|
2408
|
+
const index = Object.keys(this.watches).length;
|
|
2409
|
+
this.watches[index] = fn2;
|
|
2410
|
+
return () => {
|
|
2411
|
+
delete this.watches[index];
|
|
2412
|
+
};
|
|
2413
|
+
}
|
|
2414
|
+
/**
|
|
2415
|
+
* Return value as JSON string
|
|
2416
|
+
*
|
|
2417
|
+
* @returns {string} Stringified object as JSON
|
|
2418
|
+
*/
|
|
2419
|
+
toString() {
|
|
2420
|
+
return JSON.stringify(this.value);
|
|
2421
|
+
}
|
|
2422
|
+
/**
|
|
2423
|
+
* Return current value
|
|
2424
|
+
*
|
|
2425
|
+
* @returns {T} Current value
|
|
2426
|
+
*/
|
|
2427
|
+
valueOf() {
|
|
2428
|
+
return this.value;
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
persist.Persist = Persist;
|
|
2432
|
+
function persist$1(options) {
|
|
2433
|
+
return (target, prop) => {
|
|
2434
|
+
const key = options?.key || `${target.constructor.name}.${prop.toString()}`;
|
|
2435
|
+
const wrapper = new Persist(key, options);
|
|
2436
|
+
Object.defineProperty(target, prop, {
|
|
2437
|
+
get: function() {
|
|
2438
|
+
return wrapper.value;
|
|
2282
2439
|
},
|
|
2283
|
-
set: (
|
|
2284
|
-
|
|
2285
|
-
this.save();
|
|
2286
|
-
return true;
|
|
2440
|
+
set: function(v) {
|
|
2441
|
+
wrapper.value = v;
|
|
2287
2442
|
}
|
|
2288
2443
|
});
|
|
2289
|
-
this.save();
|
|
2290
|
-
}
|
|
2291
|
-
/** Notify listeners of change */
|
|
2292
|
-
notify(value) {
|
|
2293
|
-
Object.values(this.watches).forEach((watch) => watch(value));
|
|
2294
|
-
}
|
|
2295
|
-
/** Delete value from storage */
|
|
2296
|
-
clear() {
|
|
2297
|
-
this.storage.removeItem(this.key);
|
|
2298
|
-
}
|
|
2299
|
-
/** Save current value to storage */
|
|
2300
|
-
save() {
|
|
2301
|
-
if (this._value === void 0)
|
|
2302
|
-
this.clear();
|
|
2303
|
-
else
|
|
2304
|
-
this.storage.setItem(this.key, JSON.stringify(this._value));
|
|
2305
|
-
this.notify(this.value);
|
|
2306
|
-
}
|
|
2307
|
-
/** Load value from storage */
|
|
2308
|
-
load() {
|
|
2309
|
-
if (this.storage[this.key] != void 0) {
|
|
2310
|
-
let value = JSON.parse(this.storage.getItem(this.key));
|
|
2311
|
-
if (value != null && typeof value == "object" && this.options.type)
|
|
2312
|
-
value.__proto__ = this.options.type.prototype;
|
|
2313
|
-
this.value = value;
|
|
2314
|
-
} else
|
|
2315
|
-
this.value = this.options.default || void 0;
|
|
2316
|
-
}
|
|
2317
|
-
/**
|
|
2318
|
-
* Callback function which is run when there are changes
|
|
2319
|
-
*
|
|
2320
|
-
* @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
|
|
2321
|
-
* @returns {() => void} Function which will unsubscribe the watch/callback when called
|
|
2322
|
-
*/
|
|
2323
|
-
watch(fn2) {
|
|
2324
|
-
const index = Object.keys(this.watches).length;
|
|
2325
|
-
this.watches[index] = fn2;
|
|
2326
|
-
return () => {
|
|
2327
|
-
delete this.watches[index];
|
|
2328
2444
|
};
|
|
2329
2445
|
}
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
*
|
|
2333
|
-
* @returns {string} Stringified object as JSON
|
|
2334
|
-
*/
|
|
2335
|
-
toString() {
|
|
2336
|
-
return JSON.stringify(this.value);
|
|
2337
|
-
}
|
|
2338
|
-
/**
|
|
2339
|
-
* Return current value
|
|
2340
|
-
*
|
|
2341
|
-
* @returns {T} Current value
|
|
2342
|
-
*/
|
|
2343
|
-
valueOf() {
|
|
2344
|
-
return this.value;
|
|
2345
|
-
}
|
|
2346
|
-
}
|
|
2347
|
-
persist$1.Persist = Persist;
|
|
2348
|
-
function persist(options) {
|
|
2349
|
-
return (target, prop) => {
|
|
2350
|
-
const key = (options == null ? void 0 : options.key) || `${target.constructor.name}.${prop.toString()}`;
|
|
2351
|
-
const wrapper = new Persist(key, options);
|
|
2352
|
-
Object.defineProperty(target, prop, {
|
|
2353
|
-
get: function() {
|
|
2354
|
-
return wrapper.value;
|
|
2355
|
-
},
|
|
2356
|
-
set: function(v) {
|
|
2357
|
-
wrapper.value = v;
|
|
2358
|
-
}
|
|
2359
|
-
});
|
|
2360
|
-
};
|
|
2446
|
+
persist.persist = persist$1;
|
|
2447
|
+
return persist;
|
|
2361
2448
|
}
|
|
2362
|
-
persist$1.persist = persist;
|
|
2363
2449
|
var memoryStorage = {};
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2450
|
+
var hasRequiredMemoryStorage;
|
|
2451
|
+
function requireMemoryStorage() {
|
|
2452
|
+
if (hasRequiredMemoryStorage) return memoryStorage;
|
|
2453
|
+
hasRequiredMemoryStorage = 1;
|
|
2454
|
+
Object.defineProperty(memoryStorage, "__esModule", { value: true });
|
|
2455
|
+
memoryStorage.MemoryStorage = void 0;
|
|
2456
|
+
class MemoryStorage {
|
|
2457
|
+
get length() {
|
|
2458
|
+
return Object.keys(this).length;
|
|
2459
|
+
}
|
|
2460
|
+
clear() {
|
|
2461
|
+
Object.keys(this).forEach((k) => this.removeItem(k));
|
|
2462
|
+
}
|
|
2463
|
+
getItem(key) {
|
|
2464
|
+
return this[key];
|
|
2465
|
+
}
|
|
2466
|
+
key(index) {
|
|
2467
|
+
return Object.keys(this)[index];
|
|
2468
|
+
}
|
|
2469
|
+
removeItem(key) {
|
|
2470
|
+
delete this[key];
|
|
2471
|
+
}
|
|
2472
|
+
setItem(key, value) {
|
|
2473
|
+
this[key] = value;
|
|
2474
|
+
}
|
|
2384
2475
|
}
|
|
2476
|
+
memoryStorage.MemoryStorage = MemoryStorage;
|
|
2477
|
+
return memoryStorage;
|
|
2478
|
+
}
|
|
2479
|
+
var hasRequiredDist;
|
|
2480
|
+
function requireDist() {
|
|
2481
|
+
if (hasRequiredDist) return dist;
|
|
2482
|
+
hasRequiredDist = 1;
|
|
2483
|
+
(function(exports$1) {
|
|
2484
|
+
var __createBinding = dist && dist.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
2485
|
+
if (k2 === void 0) k2 = k;
|
|
2486
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2487
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2488
|
+
desc = { enumerable: true, get: function() {
|
|
2489
|
+
return m[k];
|
|
2490
|
+
} };
|
|
2491
|
+
}
|
|
2492
|
+
Object.defineProperty(o, k2, desc);
|
|
2493
|
+
}) : (function(o, m, k, k2) {
|
|
2494
|
+
if (k2 === void 0) k2 = k;
|
|
2495
|
+
o[k2] = m[k];
|
|
2496
|
+
}));
|
|
2497
|
+
var __exportStar = dist && dist.__exportStar || function(m, exports$12) {
|
|
2498
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
|
|
2499
|
+
};
|
|
2500
|
+
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
2501
|
+
__exportStar(requirePersist(), exports$1);
|
|
2502
|
+
__exportStar(requireMemoryStorage(), exports$1);
|
|
2503
|
+
})(dist);
|
|
2504
|
+
return dist;
|
|
2385
2505
|
}
|
|
2386
|
-
|
|
2387
|
-
(function(exports$1) {
|
|
2388
|
-
var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
|
|
2389
|
-
if (k2 === void 0) k2 = k;
|
|
2390
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2391
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2392
|
-
desc = { enumerable: true, get: function() {
|
|
2393
|
-
return m[k];
|
|
2394
|
-
} };
|
|
2395
|
-
}
|
|
2396
|
-
Object.defineProperty(o, k2, desc);
|
|
2397
|
-
} : function(o, m, k, k2) {
|
|
2398
|
-
if (k2 === void 0) k2 = k;
|
|
2399
|
-
o[k2] = m[k];
|
|
2400
|
-
});
|
|
2401
|
-
var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports$12) {
|
|
2402
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
|
|
2403
|
-
};
|
|
2404
|
-
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
2405
|
-
__exportStar(persist$1, exports$1);
|
|
2406
|
-
__exportStar(memoryStorage, exports$1);
|
|
2407
|
-
})(dist);
|
|
2506
|
+
requireDist();
|
|
2408
2507
|
export {
|
|
2409
2508
|
ASet,
|
|
2410
2509
|
ArgParser,
|