@ztimson/momentum 0.47.0 → 0.48.0
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/actions.d.ts +2 -1
- package/dist/actions.d.ts.map +1 -1
- package/dist/auth.d.ts +9 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/client.d.ts +9 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +816 -565
- package/dist/index.mjs +816 -565
- package/dist/momentum.d.ts +2 -0
- package/dist/momentum.d.ts.map +1 -1
- package/dist/users.d.ts +4 -3
- package/dist/users.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,40 +1,49 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
function
|
|
8
|
-
if (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
4
|
+
var __defProp2 = Object.defineProperty;
|
|
5
|
+
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
function clean(obj, undefinedOnly = false) {
|
|
8
|
+
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
9
|
+
if (Array.isArray(obj)) {
|
|
10
|
+
obj = obj.filter((o) => o != null);
|
|
11
|
+
} else {
|
|
12
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
13
|
+
if (undefinedOnly && value === void 0 || !undefinedOnly && value == null) delete obj[key];
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return obj;
|
|
12
17
|
}
|
|
13
|
-
function
|
|
14
|
-
const
|
|
15
|
-
|
|
18
|
+
function isEqual(a, b) {
|
|
19
|
+
const ta = typeof a, tb = typeof b;
|
|
20
|
+
if (ta != "object" || a == null || (tb != "object" || b == null))
|
|
21
|
+
return ta == "function" && tb == "function" ? a.toString() == b.toString() : a === b;
|
|
22
|
+
const keys = Object.keys(a);
|
|
23
|
+
if (keys.length != Object.keys(b).length) return false;
|
|
24
|
+
return Object.keys(a).every((key) => isEqual(a[key], b[key]));
|
|
16
25
|
}
|
|
17
|
-
function
|
|
26
|
+
function JSONAttemptParse(json) {
|
|
18
27
|
try {
|
|
19
|
-
return JSON.parse(
|
|
28
|
+
return JSON.parse(json);
|
|
20
29
|
} catch {
|
|
21
|
-
return
|
|
30
|
+
return json;
|
|
22
31
|
}
|
|
23
32
|
}
|
|
24
|
-
function
|
|
25
|
-
let
|
|
26
|
-
return JSON.stringify(
|
|
27
|
-
if (typeof
|
|
28
|
-
if (
|
|
29
|
-
|
|
33
|
+
function JSONSanitize(obj, space) {
|
|
34
|
+
let cache = [];
|
|
35
|
+
return JSON.stringify(obj, (key, value) => {
|
|
36
|
+
if (typeof value === "object" && value !== null) {
|
|
37
|
+
if (cache.includes(value)) return;
|
|
38
|
+
cache.push(value);
|
|
30
39
|
}
|
|
31
|
-
return
|
|
32
|
-
},
|
|
40
|
+
return value;
|
|
41
|
+
}, space);
|
|
33
42
|
}
|
|
34
|
-
function
|
|
35
|
-
return Array.isArray(
|
|
43
|
+
function makeArray(value) {
|
|
44
|
+
return Array.isArray(value) ? value : [value];
|
|
36
45
|
}
|
|
37
|
-
class
|
|
46
|
+
class ASet extends Array {
|
|
38
47
|
/** Number of elements in set */
|
|
39
48
|
get size() {
|
|
40
49
|
return this.length;
|
|
@@ -43,118 +52,144 @@ class S extends Array {
|
|
|
43
52
|
* Array to create set from, duplicate values will be removed
|
|
44
53
|
* @param {T[]} elements Elements which will be added to set
|
|
45
54
|
*/
|
|
46
|
-
constructor(
|
|
47
|
-
super()
|
|
55
|
+
constructor(elements = []) {
|
|
56
|
+
super();
|
|
57
|
+
if (!!(elements == null ? void 0 : elements["forEach"]))
|
|
58
|
+
elements.forEach((el) => this.add(el));
|
|
48
59
|
}
|
|
49
60
|
/**
|
|
50
61
|
* Add elements to set if unique
|
|
51
62
|
* @param items
|
|
52
63
|
*/
|
|
53
|
-
add(...
|
|
54
|
-
|
|
64
|
+
add(...items) {
|
|
65
|
+
items.filter((el) => !this.has(el)).forEach((el) => this.push(el));
|
|
66
|
+
return this;
|
|
55
67
|
}
|
|
56
68
|
/**
|
|
57
69
|
* Delete elements from set
|
|
58
70
|
* @param items Elements that will be deleted
|
|
59
71
|
*/
|
|
60
|
-
delete(...
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
72
|
+
delete(...items) {
|
|
73
|
+
items.forEach((el) => {
|
|
74
|
+
const index = this.indexOf(el);
|
|
75
|
+
if (index != -1) this.splice(index, 1);
|
|
64
76
|
});
|
|
77
|
+
return this;
|
|
65
78
|
}
|
|
66
79
|
/**
|
|
67
80
|
* Create list of elements this set has which the comparison set does not
|
|
68
81
|
* @param {ASet<T>} set Set to compare against
|
|
69
82
|
* @return {ASet<T>} Different elements
|
|
70
83
|
*/
|
|
71
|
-
difference(
|
|
72
|
-
return new
|
|
84
|
+
difference(set) {
|
|
85
|
+
return new ASet(this.filter((el) => !set.has(el)));
|
|
73
86
|
}
|
|
74
87
|
/**
|
|
75
88
|
* Check if set includes element
|
|
76
89
|
* @param {T} el Element to look for
|
|
77
90
|
* @return {boolean} True if element was found, false otherwise
|
|
78
91
|
*/
|
|
79
|
-
has(
|
|
80
|
-
return this.indexOf(
|
|
92
|
+
has(el) {
|
|
93
|
+
return this.indexOf(el) != -1;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Find index number of element, or -1 if it doesn't exist. Matches by equality not reference
|
|
97
|
+
*
|
|
98
|
+
* @param {T} search Element to find
|
|
99
|
+
* @param {number} fromIndex Starting index position
|
|
100
|
+
* @return {number} Element index number or -1 if missing
|
|
101
|
+
*/
|
|
102
|
+
indexOf(search, fromIndex) {
|
|
103
|
+
return super.findIndex((el) => isEqual(el, search), fromIndex);
|
|
81
104
|
}
|
|
82
105
|
/**
|
|
83
106
|
* Create list of elements this set has in common with the comparison set
|
|
84
107
|
* @param {ASet<T>} set Set to compare against
|
|
85
108
|
* @return {boolean} Set of common elements
|
|
86
109
|
*/
|
|
87
|
-
intersection(
|
|
88
|
-
return new
|
|
110
|
+
intersection(set) {
|
|
111
|
+
return new ASet(this.filter((el) => set.has(el)));
|
|
89
112
|
}
|
|
90
113
|
/**
|
|
91
114
|
* Check if this set has no elements in common with the comparison set
|
|
92
115
|
* @param {ASet<T>} set Set to compare against
|
|
93
116
|
* @return {boolean} True if nothing in common, false otherwise
|
|
94
117
|
*/
|
|
95
|
-
isDisjointFrom(
|
|
96
|
-
return this.intersection(
|
|
118
|
+
isDisjointFrom(set) {
|
|
119
|
+
return this.intersection(set).size == 0;
|
|
97
120
|
}
|
|
98
121
|
/**
|
|
99
122
|
* Check if all elements in this set are included in the comparison set
|
|
100
123
|
* @param {ASet<T>} set Set to compare against
|
|
101
124
|
* @return {boolean} True if all elements are included, false otherwise
|
|
102
125
|
*/
|
|
103
|
-
isSubsetOf(
|
|
104
|
-
return this.findIndex((
|
|
126
|
+
isSubsetOf(set) {
|
|
127
|
+
return this.findIndex((el) => !set.has(el)) == -1;
|
|
105
128
|
}
|
|
106
129
|
/**
|
|
107
130
|
* Check if all elements from comparison set are included in this set
|
|
108
131
|
* @param {ASet<T>} set Set to compare against
|
|
109
132
|
* @return {boolean} True if all elements are included, false otherwise
|
|
110
133
|
*/
|
|
111
|
-
isSuperset(
|
|
112
|
-
return
|
|
134
|
+
isSuperset(set) {
|
|
135
|
+
return set.findIndex((el) => !this.has(el)) == -1;
|
|
113
136
|
}
|
|
114
137
|
/**
|
|
115
138
|
* Create list of elements that are only in one set but not both (XOR)
|
|
116
139
|
* @param {ASet<T>} set Set to compare against
|
|
117
140
|
* @return {ASet<T>} New set of unique elements
|
|
118
141
|
*/
|
|
119
|
-
symmetricDifference(
|
|
120
|
-
return new
|
|
142
|
+
symmetricDifference(set) {
|
|
143
|
+
return new ASet([...this.difference(set), ...set.difference(this)]);
|
|
121
144
|
}
|
|
122
145
|
/**
|
|
123
146
|
* Create joined list of elements included in this & the comparison set
|
|
124
147
|
* @param {ASet<T>} set Set join
|
|
125
148
|
* @return {ASet<T>} New set of both previous sets combined
|
|
126
149
|
*/
|
|
127
|
-
union(
|
|
128
|
-
return new
|
|
150
|
+
union(set) {
|
|
151
|
+
return new ASet([...this, ...set]);
|
|
129
152
|
}
|
|
130
153
|
}
|
|
131
|
-
class
|
|
154
|
+
class Cache {
|
|
132
155
|
/**
|
|
133
156
|
* Create new cache
|
|
134
157
|
*
|
|
135
158
|
* @param {keyof T} key Default property to use as primary key
|
|
136
159
|
* @param options
|
|
137
160
|
*/
|
|
138
|
-
constructor(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
161
|
+
constructor(key, options = {}) {
|
|
162
|
+
__publicField2(this, "store", {});
|
|
163
|
+
__publicField2(this, "complete", false);
|
|
164
|
+
__publicField2(this, "values", this.all());
|
|
165
|
+
this.key = key;
|
|
166
|
+
this.options = options;
|
|
167
|
+
if (options.storageKey && !options.storage && typeof Storage !== "undefined")
|
|
168
|
+
options.storage = localStorage;
|
|
169
|
+
if (options.storageKey && options.storage) {
|
|
170
|
+
const stored = options.storage.getItem(options.storageKey);
|
|
171
|
+
if (stored) {
|
|
145
172
|
try {
|
|
146
|
-
Object.assign(this.store, JSON.parse(
|
|
173
|
+
Object.assign(this.store, JSON.parse(stored));
|
|
147
174
|
} catch {
|
|
148
175
|
}
|
|
176
|
+
}
|
|
149
177
|
}
|
|
150
178
|
return new Proxy(this, {
|
|
151
|
-
get: (
|
|
152
|
-
|
|
179
|
+
get: (target, prop) => {
|
|
180
|
+
if (prop in target) return target[prop];
|
|
181
|
+
return target.store[prop];
|
|
182
|
+
},
|
|
183
|
+
set: (target, prop, value) => {
|
|
184
|
+
if (prop in target) target[prop] = value;
|
|
185
|
+
else target.store[prop] = value;
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
153
188
|
});
|
|
154
189
|
}
|
|
155
|
-
getKey(
|
|
190
|
+
getKey(value) {
|
|
156
191
|
if (!this.key) throw new Error("No key defined");
|
|
157
|
-
return
|
|
192
|
+
return value[this.key];
|
|
158
193
|
}
|
|
159
194
|
/**
|
|
160
195
|
* Get all cached items
|
|
@@ -171,9 +206,10 @@ class Tt {
|
|
|
171
206
|
* @param {number | undefined} ttl Override default expiry
|
|
172
207
|
* @return {this}
|
|
173
208
|
*/
|
|
174
|
-
add(
|
|
175
|
-
const
|
|
176
|
-
|
|
209
|
+
add(value, ttl = this.ttl) {
|
|
210
|
+
const key = this.getKey(value);
|
|
211
|
+
this.set(key, value, ttl);
|
|
212
|
+
return this;
|
|
177
213
|
}
|
|
178
214
|
/**
|
|
179
215
|
* Add several rows to the cache
|
|
@@ -182,8 +218,10 @@ class Tt {
|
|
|
182
218
|
* @param complete Mark cache as complete & reliable, defaults to true
|
|
183
219
|
* @return {this}
|
|
184
220
|
*/
|
|
185
|
-
addAll(
|
|
186
|
-
|
|
221
|
+
addAll(rows, complete = true) {
|
|
222
|
+
rows.forEach((r) => this.add(r));
|
|
223
|
+
this.complete = complete;
|
|
224
|
+
return this;
|
|
187
225
|
}
|
|
188
226
|
/**
|
|
189
227
|
* Remove all keys from cache
|
|
@@ -196,8 +234,10 @@ class Tt {
|
|
|
196
234
|
*
|
|
197
235
|
* @param {K} key Item's primary key
|
|
198
236
|
*/
|
|
199
|
-
delete(
|
|
200
|
-
delete this.store[
|
|
237
|
+
delete(key) {
|
|
238
|
+
delete this.store[key];
|
|
239
|
+
if (this.options.storageKey && this.options.storage)
|
|
240
|
+
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
|
|
201
241
|
}
|
|
202
242
|
/**
|
|
203
243
|
* Return cache as an array of key-value pairs
|
|
@@ -211,8 +251,8 @@ class Tt {
|
|
|
211
251
|
* @param {K} key Key to lookup
|
|
212
252
|
* @return {T} Cached item
|
|
213
253
|
*/
|
|
214
|
-
get(
|
|
215
|
-
return this.store[
|
|
254
|
+
get(key) {
|
|
255
|
+
return this.store[key];
|
|
216
256
|
}
|
|
217
257
|
/**
|
|
218
258
|
* Get a list of cached keys
|
|
@@ -238,326 +278,389 @@ class Tt {
|
|
|
238
278
|
* @param {number | undefined} ttl Override default expiry in seconds
|
|
239
279
|
* @return {this}
|
|
240
280
|
*/
|
|
241
|
-
set(
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
281
|
+
set(key, value, ttl = this.options.ttl) {
|
|
282
|
+
this.store[key] = value;
|
|
283
|
+
if (this.options.storageKey && this.options.storage)
|
|
284
|
+
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
|
|
285
|
+
if (ttl) setTimeout(() => {
|
|
286
|
+
this.complete = false;
|
|
287
|
+
this.delete(key);
|
|
288
|
+
}, ttl * 1e3);
|
|
289
|
+
return this;
|
|
245
290
|
}
|
|
246
291
|
}
|
|
247
|
-
class
|
|
248
|
-
constructor(
|
|
249
|
-
super((
|
|
250
|
-
(
|
|
251
|
-
(
|
|
252
|
-
(
|
|
292
|
+
class PromiseProgress extends Promise {
|
|
293
|
+
constructor(executor) {
|
|
294
|
+
super((resolve, reject) => executor(
|
|
295
|
+
(value) => resolve(value),
|
|
296
|
+
(reason) => reject(reason),
|
|
297
|
+
(progress) => this.progress = progress
|
|
253
298
|
));
|
|
254
|
-
|
|
255
|
-
|
|
299
|
+
__publicField2(this, "listeners", []);
|
|
300
|
+
__publicField2(this, "_progress", 0);
|
|
256
301
|
}
|
|
257
302
|
get progress() {
|
|
258
303
|
return this._progress;
|
|
259
304
|
}
|
|
260
|
-
set progress(
|
|
261
|
-
|
|
305
|
+
set progress(p) {
|
|
306
|
+
if (p == this._progress) return;
|
|
307
|
+
this._progress = p;
|
|
308
|
+
this.listeners.forEach((l) => l(p));
|
|
262
309
|
}
|
|
263
|
-
static from(
|
|
264
|
-
|
|
310
|
+
static from(promise) {
|
|
311
|
+
if (promise instanceof PromiseProgress) return promise;
|
|
312
|
+
return new PromiseProgress((res, rej) => promise.then((...args) => res(...args)).catch((...args) => rej(...args)));
|
|
265
313
|
}
|
|
266
|
-
from(
|
|
267
|
-
const
|
|
268
|
-
|
|
314
|
+
from(promise) {
|
|
315
|
+
const newPromise = PromiseProgress.from(promise);
|
|
316
|
+
this.onProgress((p) => newPromise.progress = p);
|
|
317
|
+
return newPromise;
|
|
269
318
|
}
|
|
270
|
-
onProgress(
|
|
271
|
-
|
|
319
|
+
onProgress(callback) {
|
|
320
|
+
this.listeners.push(callback);
|
|
321
|
+
return this;
|
|
272
322
|
}
|
|
273
|
-
then(
|
|
274
|
-
const
|
|
275
|
-
return this.from(
|
|
323
|
+
then(res, rej) {
|
|
324
|
+
const resp = super.then(res, rej);
|
|
325
|
+
return this.from(resp);
|
|
276
326
|
}
|
|
277
|
-
catch(
|
|
278
|
-
return this.from(super.catch(
|
|
327
|
+
catch(rej) {
|
|
328
|
+
return this.from(super.catch(rej));
|
|
279
329
|
}
|
|
280
|
-
finally(
|
|
281
|
-
return this.from(super.finally(
|
|
330
|
+
finally(res) {
|
|
331
|
+
return this.from(super.finally(res));
|
|
282
332
|
}
|
|
283
333
|
}
|
|
284
|
-
function
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
334
|
+
function downloadFile(blob, name) {
|
|
335
|
+
if (!(blob instanceof Blob)) blob = new Blob(makeArray(blob));
|
|
336
|
+
const url = URL.createObjectURL(blob);
|
|
337
|
+
downloadUrl(url, name);
|
|
338
|
+
URL.revokeObjectURL(url);
|
|
288
339
|
}
|
|
289
|
-
function
|
|
290
|
-
const
|
|
291
|
-
|
|
340
|
+
function downloadUrl(href, name) {
|
|
341
|
+
const a = document.createElement("a");
|
|
342
|
+
a.href = href;
|
|
343
|
+
a.download = name || href.split("/").pop();
|
|
344
|
+
document.body.appendChild(a);
|
|
345
|
+
a.click();
|
|
346
|
+
document.body.removeChild(a);
|
|
292
347
|
}
|
|
293
|
-
function
|
|
294
|
-
return new Promise((
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
348
|
+
function fileBrowser(options = {}) {
|
|
349
|
+
return new Promise((res) => {
|
|
350
|
+
const input = document.createElement("input");
|
|
351
|
+
input.type = "file";
|
|
352
|
+
input.accept = options.accept || "*";
|
|
353
|
+
input.style.display = "none";
|
|
354
|
+
input.multiple = !!options.multiple;
|
|
355
|
+
input.onblur = input.onchange = async () => {
|
|
356
|
+
res(Array.from(input.files));
|
|
357
|
+
input.remove();
|
|
358
|
+
};
|
|
359
|
+
document.body.appendChild(input);
|
|
360
|
+
input.click();
|
|
299
361
|
});
|
|
300
362
|
}
|
|
301
|
-
function
|
|
302
|
-
(typeof
|
|
303
|
-
const
|
|
304
|
-
return
|
|
363
|
+
function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
|
|
364
|
+
if (typeof date == "number" || typeof date == "string") date = new Date(date);
|
|
365
|
+
const timestamp = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}_${date.getHours().toString().padStart(2, "0")}-${date.getMinutes().toString().padStart(2, "0")}-${date.getSeconds().toString().padStart(2, "0")}`;
|
|
366
|
+
return timestamp;
|
|
305
367
|
}
|
|
306
|
-
function
|
|
307
|
-
return new
|
|
308
|
-
const
|
|
309
|
-
|
|
368
|
+
function uploadWithProgress(options) {
|
|
369
|
+
return new PromiseProgress((res, rej, prog) => {
|
|
370
|
+
const xhr = new XMLHttpRequest();
|
|
371
|
+
const formData2 = new FormData();
|
|
372
|
+
options.files.forEach((f) => formData2.append("file", f));
|
|
373
|
+
xhr.withCredentials = !!options.withCredentials;
|
|
374
|
+
xhr.upload.addEventListener("progress", (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
|
375
|
+
xhr.addEventListener("loadend", () => res(JSONAttemptParse(xhr.responseText)));
|
|
376
|
+
xhr.addEventListener("error", () => rej(JSONAttemptParse(xhr.responseText)));
|
|
377
|
+
xhr.addEventListener("timeout", () => rej({ error: "Request timed out" }));
|
|
378
|
+
xhr.open("POST", options.url);
|
|
379
|
+
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
380
|
+
xhr.send(formData2);
|
|
310
381
|
});
|
|
311
382
|
}
|
|
312
|
-
class
|
|
383
|
+
class TypedEmitter {
|
|
313
384
|
constructor() {
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
static emit(t, ...e) {
|
|
317
|
-
(this.listeners["*"] || []).forEach((n) => n(t, ...e)), (this.listeners[t.toString()] || []).forEach((n) => n(...e));
|
|
385
|
+
__publicField2(this, "listeners", {});
|
|
318
386
|
}
|
|
319
|
-
static
|
|
320
|
-
|
|
321
|
-
this.listeners[
|
|
387
|
+
static emit(event, ...args) {
|
|
388
|
+
(this.listeners["*"] || []).forEach((l) => l(event, ...args));
|
|
389
|
+
(this.listeners[event.toString()] || []).forEach((l) => l(...args));
|
|
322
390
|
}
|
|
323
|
-
static
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return this.listeners[n] || (this.listeners[n] = []), (s = this.listeners[n]) == null || s.push(e), () => this.off(t, e);
|
|
391
|
+
static off(event, listener) {
|
|
392
|
+
const e = event.toString();
|
|
393
|
+
this.listeners[e] = (this.listeners[e] || []).filter((l) => l === listener);
|
|
327
394
|
}
|
|
328
|
-
static
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
395
|
+
static on(event, listener) {
|
|
396
|
+
var _a;
|
|
397
|
+
const e = event.toString();
|
|
398
|
+
if (!this.listeners[e]) this.listeners[e] = [];
|
|
399
|
+
(_a = this.listeners[e]) == null ? void 0 : _a.push(listener);
|
|
400
|
+
return () => this.off(event, listener);
|
|
401
|
+
}
|
|
402
|
+
static once(event, listener) {
|
|
403
|
+
return new Promise((res) => {
|
|
404
|
+
const unsubscribe = this.on(event, (...args) => {
|
|
405
|
+
res(args.length == 1 ? args[0] : args);
|
|
406
|
+
if (listener) listener(...args);
|
|
407
|
+
unsubscribe();
|
|
332
408
|
});
|
|
333
409
|
});
|
|
334
410
|
}
|
|
335
|
-
emit(
|
|
336
|
-
(this.listeners["*"] || []).forEach((
|
|
337
|
-
|
|
338
|
-
off(t, e) {
|
|
339
|
-
this.listeners[t] = (this.listeners[t] || []).filter((n) => n === e);
|
|
411
|
+
emit(event, ...args) {
|
|
412
|
+
(this.listeners["*"] || []).forEach((l) => l(event, ...args));
|
|
413
|
+
(this.listeners[event] || []).forEach((l) => l(...args));
|
|
340
414
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
return this.listeners[t] || (this.listeners[t] = []), (n = this.listeners[t]) == null || n.push(e), () => this.off(t, e);
|
|
415
|
+
off(event, listener) {
|
|
416
|
+
this.listeners[event] = (this.listeners[event] || []).filter((l) => l === listener);
|
|
344
417
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
418
|
+
on(event, listener) {
|
|
419
|
+
var _a;
|
|
420
|
+
if (!this.listeners[event]) this.listeners[event] = [];
|
|
421
|
+
(_a = this.listeners[event]) == null ? void 0 : _a.push(listener);
|
|
422
|
+
return () => this.off(event, listener);
|
|
423
|
+
}
|
|
424
|
+
once(event, listener) {
|
|
425
|
+
return new Promise((res) => {
|
|
426
|
+
const unsubscribe = this.on(event, (...args) => {
|
|
427
|
+
res(args.length == 1 ? args[0] : args);
|
|
428
|
+
if (listener) listener(...args);
|
|
429
|
+
unsubscribe();
|
|
349
430
|
});
|
|
350
431
|
});
|
|
351
432
|
}
|
|
352
433
|
}
|
|
353
|
-
|
|
354
|
-
class
|
|
355
|
-
constructor(
|
|
356
|
-
super(
|
|
357
|
-
|
|
358
|
-
|
|
434
|
+
__publicField2(TypedEmitter, "listeners", {});
|
|
435
|
+
class CustomError extends Error {
|
|
436
|
+
constructor(message, code) {
|
|
437
|
+
super(message);
|
|
438
|
+
__publicField2(this, "_code");
|
|
439
|
+
if (code != null) this._code = code;
|
|
359
440
|
}
|
|
360
441
|
get code() {
|
|
361
442
|
return this._code || this.constructor.code;
|
|
362
443
|
}
|
|
363
|
-
set code(
|
|
364
|
-
this._code =
|
|
444
|
+
set code(c) {
|
|
445
|
+
this._code = c;
|
|
365
446
|
}
|
|
366
|
-
static from(
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
447
|
+
static from(err) {
|
|
448
|
+
const code = Number(err.statusCode) ?? Number(err.code);
|
|
449
|
+
const newErr = new this(err.message || err.toString());
|
|
450
|
+
return Object.assign(newErr, {
|
|
451
|
+
stack: err.stack,
|
|
452
|
+
...err,
|
|
453
|
+
code: code ?? void 0
|
|
372
454
|
});
|
|
373
455
|
}
|
|
374
|
-
static instanceof(
|
|
375
|
-
return
|
|
456
|
+
static instanceof(err) {
|
|
457
|
+
return err.constructor.code != void 0;
|
|
376
458
|
}
|
|
377
459
|
toString() {
|
|
378
460
|
return this.message || super.toString();
|
|
379
461
|
}
|
|
380
462
|
}
|
|
381
|
-
|
|
382
|
-
class
|
|
383
|
-
constructor(
|
|
384
|
-
super(
|
|
463
|
+
__publicField2(CustomError, "code", 500);
|
|
464
|
+
class BadRequestError extends CustomError {
|
|
465
|
+
constructor(message = "Bad Request") {
|
|
466
|
+
super(message);
|
|
385
467
|
}
|
|
386
|
-
static instanceof(
|
|
387
|
-
return
|
|
468
|
+
static instanceof(err) {
|
|
469
|
+
return err.constructor.code == this.code;
|
|
388
470
|
}
|
|
389
471
|
}
|
|
390
|
-
|
|
391
|
-
class
|
|
392
|
-
constructor(
|
|
393
|
-
super(
|
|
472
|
+
__publicField2(BadRequestError, "code", 400);
|
|
473
|
+
class UnauthorizedError extends CustomError {
|
|
474
|
+
constructor(message = "Unauthorized") {
|
|
475
|
+
super(message);
|
|
394
476
|
}
|
|
395
|
-
static instanceof(
|
|
396
|
-
return
|
|
477
|
+
static instanceof(err) {
|
|
478
|
+
return err.constructor.code == this.code;
|
|
397
479
|
}
|
|
398
480
|
}
|
|
399
|
-
|
|
400
|
-
class
|
|
401
|
-
constructor(
|
|
402
|
-
super(
|
|
481
|
+
__publicField2(UnauthorizedError, "code", 401);
|
|
482
|
+
class PaymentRequiredError extends CustomError {
|
|
483
|
+
constructor(message = "Payment Required") {
|
|
484
|
+
super(message);
|
|
403
485
|
}
|
|
404
|
-
static instanceof(
|
|
405
|
-
return
|
|
486
|
+
static instanceof(err) {
|
|
487
|
+
return err.constructor.code == this.code;
|
|
406
488
|
}
|
|
407
489
|
}
|
|
408
|
-
|
|
409
|
-
class
|
|
410
|
-
constructor(
|
|
411
|
-
super(
|
|
490
|
+
__publicField2(PaymentRequiredError, "code", 402);
|
|
491
|
+
class ForbiddenError extends CustomError {
|
|
492
|
+
constructor(message = "Forbidden") {
|
|
493
|
+
super(message);
|
|
412
494
|
}
|
|
413
|
-
static instanceof(
|
|
414
|
-
return
|
|
495
|
+
static instanceof(err) {
|
|
496
|
+
return err.constructor.code == this.code;
|
|
415
497
|
}
|
|
416
498
|
}
|
|
417
|
-
|
|
418
|
-
class
|
|
419
|
-
constructor(
|
|
420
|
-
super(
|
|
499
|
+
__publicField2(ForbiddenError, "code", 403);
|
|
500
|
+
class NotFoundError extends CustomError {
|
|
501
|
+
constructor(message = "Not Found") {
|
|
502
|
+
super(message);
|
|
421
503
|
}
|
|
422
|
-
static instanceof(
|
|
423
|
-
return
|
|
504
|
+
static instanceof(err) {
|
|
505
|
+
return err.constructor.code == this.code;
|
|
424
506
|
}
|
|
425
507
|
}
|
|
426
|
-
|
|
427
|
-
class
|
|
428
|
-
constructor(
|
|
429
|
-
super(
|
|
508
|
+
__publicField2(NotFoundError, "code", 404);
|
|
509
|
+
class MethodNotAllowedError extends CustomError {
|
|
510
|
+
constructor(message = "Method Not Allowed") {
|
|
511
|
+
super(message);
|
|
430
512
|
}
|
|
431
|
-
static instanceof(
|
|
432
|
-
return
|
|
513
|
+
static instanceof(err) {
|
|
514
|
+
return err.constructor.code == this.code;
|
|
433
515
|
}
|
|
434
516
|
}
|
|
435
|
-
|
|
436
|
-
class
|
|
437
|
-
constructor(
|
|
438
|
-
super(
|
|
517
|
+
__publicField2(MethodNotAllowedError, "code", 405);
|
|
518
|
+
class NotAcceptableError extends CustomError {
|
|
519
|
+
constructor(message = "Not Acceptable") {
|
|
520
|
+
super(message);
|
|
439
521
|
}
|
|
440
|
-
static instanceof(
|
|
441
|
-
return
|
|
522
|
+
static instanceof(err) {
|
|
523
|
+
return err.constructor.code == this.code;
|
|
442
524
|
}
|
|
443
525
|
}
|
|
444
|
-
|
|
445
|
-
class
|
|
446
|
-
constructor(
|
|
447
|
-
super(
|
|
526
|
+
__publicField2(NotAcceptableError, "code", 406);
|
|
527
|
+
class InternalServerError extends CustomError {
|
|
528
|
+
constructor(message = "Internal Server Error") {
|
|
529
|
+
super(message);
|
|
448
530
|
}
|
|
449
|
-
static instanceof(
|
|
450
|
-
return
|
|
531
|
+
static instanceof(err) {
|
|
532
|
+
return err.constructor.code == this.code;
|
|
451
533
|
}
|
|
452
534
|
}
|
|
453
|
-
|
|
454
|
-
class
|
|
455
|
-
constructor(
|
|
456
|
-
super(
|
|
535
|
+
__publicField2(InternalServerError, "code", 500);
|
|
536
|
+
class NotImplementedError extends CustomError {
|
|
537
|
+
constructor(message = "Not Implemented") {
|
|
538
|
+
super(message);
|
|
457
539
|
}
|
|
458
|
-
static instanceof(
|
|
459
|
-
return
|
|
540
|
+
static instanceof(err) {
|
|
541
|
+
return err.constructor.code == this.code;
|
|
460
542
|
}
|
|
461
543
|
}
|
|
462
|
-
|
|
463
|
-
class
|
|
464
|
-
constructor(
|
|
465
|
-
super(
|
|
544
|
+
__publicField2(NotImplementedError, "code", 501);
|
|
545
|
+
class BadGatewayError extends CustomError {
|
|
546
|
+
constructor(message = "Bad Gateway") {
|
|
547
|
+
super(message);
|
|
466
548
|
}
|
|
467
|
-
static instanceof(
|
|
468
|
-
return
|
|
549
|
+
static instanceof(err) {
|
|
550
|
+
return err.constructor.code == this.code;
|
|
469
551
|
}
|
|
470
552
|
}
|
|
471
|
-
|
|
472
|
-
class
|
|
473
|
-
constructor(
|
|
474
|
-
super(
|
|
553
|
+
__publicField2(BadGatewayError, "code", 502);
|
|
554
|
+
class ServiceUnavailableError extends CustomError {
|
|
555
|
+
constructor(message = "Service Unavailable") {
|
|
556
|
+
super(message);
|
|
475
557
|
}
|
|
476
|
-
static instanceof(
|
|
477
|
-
return
|
|
558
|
+
static instanceof(err) {
|
|
559
|
+
return err.constructor.code == this.code;
|
|
478
560
|
}
|
|
479
561
|
}
|
|
480
|
-
|
|
481
|
-
class
|
|
482
|
-
constructor(
|
|
483
|
-
super(
|
|
562
|
+
__publicField2(ServiceUnavailableError, "code", 503);
|
|
563
|
+
class GatewayTimeoutError extends CustomError {
|
|
564
|
+
constructor(message = "Gateway Timeout") {
|
|
565
|
+
super(message);
|
|
484
566
|
}
|
|
485
|
-
static instanceof(
|
|
486
|
-
return
|
|
567
|
+
static instanceof(err) {
|
|
568
|
+
return err.constructor.code == this.code;
|
|
487
569
|
}
|
|
488
570
|
}
|
|
489
|
-
|
|
490
|
-
const
|
|
491
|
-
constructor(
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
this.url =
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
571
|
+
__publicField2(GatewayTimeoutError, "code", 504);
|
|
572
|
+
const _Http = class _Http2 {
|
|
573
|
+
constructor(defaults = {}) {
|
|
574
|
+
__publicField2(this, "interceptors", {});
|
|
575
|
+
__publicField2(this, "headers", {});
|
|
576
|
+
__publicField2(this, "url");
|
|
577
|
+
this.url = defaults.url ?? null;
|
|
578
|
+
this.headers = defaults.headers || {};
|
|
579
|
+
if (defaults.interceptors) {
|
|
580
|
+
defaults.interceptors.forEach((i) => _Http2.addInterceptor(i));
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
static addInterceptor(fn) {
|
|
584
|
+
const key = Object.keys(_Http2.interceptors).length.toString();
|
|
585
|
+
_Http2.interceptors[key] = fn;
|
|
586
|
+
return () => {
|
|
587
|
+
_Http2.interceptors[key] = null;
|
|
501
588
|
};
|
|
502
589
|
}
|
|
503
|
-
addInterceptor(
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
590
|
+
addInterceptor(fn) {
|
|
591
|
+
const key = Object.keys(this.interceptors).length.toString();
|
|
592
|
+
this.interceptors[key] = fn;
|
|
593
|
+
return () => {
|
|
594
|
+
this.interceptors[key] = null;
|
|
507
595
|
};
|
|
508
596
|
}
|
|
509
|
-
request(
|
|
510
|
-
var
|
|
511
|
-
if (!this.url && !
|
|
512
|
-
let
|
|
513
|
-
if (
|
|
514
|
-
|
|
515
|
-
|
|
597
|
+
request(opts = {}) {
|
|
598
|
+
var _a;
|
|
599
|
+
if (!this.url && !opts.url) throw new Error("URL needs to be set");
|
|
600
|
+
let url = (((_a = opts.url) == null ? void 0 : _a.startsWith("http")) ? opts.url : (this.url || "") + (opts.url || "")).replace(/([^:]\/)\/+/g, "$1");
|
|
601
|
+
if (opts.fragment) url.includes("#") ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : url += "#" + opts.fragment;
|
|
602
|
+
if (opts.query) {
|
|
603
|
+
const q = Array.isArray(opts.query) ? opts.query : Object.keys(opts.query).map((k) => ({ key: k, value: opts.query[k] }));
|
|
604
|
+
url += (url.includes("?") ? "&" : "?") + q.map((q2) => `${q2.key}=${q2.value}`).join("&");
|
|
516
605
|
}
|
|
517
|
-
const
|
|
518
|
-
"Content-Type":
|
|
519
|
-
...
|
|
606
|
+
const headers = clean({
|
|
607
|
+
"Content-Type": !opts.body ? void 0 : opts.body instanceof FormData ? "multipart/form-data" : "application/json",
|
|
608
|
+
..._Http2.headers,
|
|
520
609
|
...this.headers,
|
|
521
|
-
...
|
|
610
|
+
...opts.headers
|
|
522
611
|
});
|
|
523
|
-
|
|
612
|
+
if (typeof opts.body == "object" && opts.body != null && headers["Content-Type"] == "application/json")
|
|
613
|
+
opts.body = JSON.stringify(opts.body);
|
|
614
|
+
return new PromiseProgress((res, rej, prog) => {
|
|
524
615
|
try {
|
|
525
|
-
fetch(
|
|
526
|
-
headers
|
|
527
|
-
method:
|
|
528
|
-
body:
|
|
529
|
-
}).then(async (
|
|
530
|
-
var
|
|
531
|
-
for (let
|
|
532
|
-
await new Promise((
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
616
|
+
fetch(url, {
|
|
617
|
+
headers,
|
|
618
|
+
method: opts.method || (opts.body ? "POST" : "GET"),
|
|
619
|
+
body: opts.body
|
|
620
|
+
}).then(async (resp) => {
|
|
621
|
+
var _a2, _b;
|
|
622
|
+
for (let fn of [...Object.values(_Http2.interceptors), ...Object.values(this.interceptors)]) {
|
|
623
|
+
await new Promise((res2) => fn(resp, () => res2()));
|
|
624
|
+
}
|
|
625
|
+
const contentLength = resp.headers.get("Content-Length");
|
|
626
|
+
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
|
627
|
+
let loaded = 0;
|
|
628
|
+
const reader = (_a2 = resp.body) == null ? void 0 : _a2.getReader();
|
|
629
|
+
const stream = new ReadableStream({
|
|
630
|
+
start(controller) {
|
|
631
|
+
function push() {
|
|
632
|
+
reader == null ? void 0 : reader.read().then((event) => {
|
|
633
|
+
if (event.done) return controller.close();
|
|
634
|
+
loaded += event.value.byteLength;
|
|
635
|
+
prog(loaded / total);
|
|
636
|
+
controller.enqueue(event.value);
|
|
637
|
+
push();
|
|
638
|
+
}).catch((error) => controller.error(error));
|
|
542
639
|
}
|
|
543
|
-
|
|
640
|
+
push();
|
|
544
641
|
}
|
|
545
642
|
});
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
643
|
+
resp.data = new Response(stream);
|
|
644
|
+
if (opts.decode == null || opts.decode) {
|
|
645
|
+
const content = (_b = resp.headers.get("Content-Type")) == null ? void 0 : _b.toLowerCase();
|
|
646
|
+
if (content == null ? void 0 : content.includes("form")) resp.data = await resp.data.formData();
|
|
647
|
+
else if (content == null ? void 0 : content.includes("json")) resp.data = await resp.data.json();
|
|
648
|
+
else if (content == null ? void 0 : content.includes("text")) resp.data = await resp.data.text();
|
|
649
|
+
else if (content == null ? void 0 : content.includes("application")) resp.data = await resp.data.blob();
|
|
549
650
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
651
|
+
if (resp.ok) res(resp);
|
|
652
|
+
else rej(resp);
|
|
653
|
+
}).catch((err) => rej(err));
|
|
654
|
+
} catch (err) {
|
|
655
|
+
rej(err);
|
|
554
656
|
}
|
|
555
657
|
});
|
|
556
658
|
}
|
|
557
659
|
};
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
660
|
+
__publicField2(_Http, "interceptors", {});
|
|
661
|
+
__publicField2(_Http, "headers", {});
|
|
662
|
+
let Http = _Http;
|
|
663
|
+
const CliEffects = {
|
|
561
664
|
CLEAR: "\x1B[0m",
|
|
562
665
|
BRIGHT: "\x1B[1m",
|
|
563
666
|
DIM: "\x1B[2m",
|
|
@@ -565,7 +668,8 @@ const R = {
|
|
|
565
668
|
BLINK: "\x1B[5m",
|
|
566
669
|
REVERSE: "\x1B[7m",
|
|
567
670
|
HIDDEN: "\x1B[8m"
|
|
568
|
-
}
|
|
671
|
+
};
|
|
672
|
+
const CliForeground = {
|
|
569
673
|
BLACK: "\x1B[30m",
|
|
570
674
|
RED: "\x1B[31m",
|
|
571
675
|
GREEN: "\x1B[32m",
|
|
@@ -583,80 +687,138 @@ const R = {
|
|
|
583
687
|
LIGHT_CYAN: "\x1B[96m",
|
|
584
688
|
WHITE: "\x1B[97m"
|
|
585
689
|
};
|
|
586
|
-
var
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
if (
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
690
|
+
var LOG_LEVEL = /* @__PURE__ */ ((LOG_LEVEL2) => {
|
|
691
|
+
LOG_LEVEL2[LOG_LEVEL2["ERROR"] = 0] = "ERROR";
|
|
692
|
+
LOG_LEVEL2[LOG_LEVEL2["WARN"] = 1] = "WARN";
|
|
693
|
+
LOG_LEVEL2[LOG_LEVEL2["INFO"] = 2] = "INFO";
|
|
694
|
+
LOG_LEVEL2[LOG_LEVEL2["LOG"] = 3] = "LOG";
|
|
695
|
+
LOG_LEVEL2[LOG_LEVEL2["DEBUG"] = 4] = "DEBUG";
|
|
696
|
+
return LOG_LEVEL2;
|
|
697
|
+
})(LOG_LEVEL || {});
|
|
698
|
+
const _Logger = class _Logger2 extends TypedEmitter {
|
|
699
|
+
constructor(namespace) {
|
|
700
|
+
super();
|
|
701
|
+
this.namespace = namespace;
|
|
702
|
+
}
|
|
703
|
+
format(...text) {
|
|
704
|
+
const now = /* @__PURE__ */ new Date();
|
|
705
|
+
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")}`;
|
|
706
|
+
return `${timestamp}${this.namespace ? ` [${this.namespace}]` : ""} ${text.map((t) => typeof t == "string" ? t : JSONSanitize(t, 2)).join(" ")}`;
|
|
707
|
+
}
|
|
708
|
+
debug(...args) {
|
|
709
|
+
if (_Logger2.LOG_LEVEL < 4) return;
|
|
710
|
+
const str = this.format(...args);
|
|
711
|
+
_Logger2.emit(4, str);
|
|
712
|
+
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
|
|
713
|
+
}
|
|
714
|
+
log(...args) {
|
|
715
|
+
if (_Logger2.LOG_LEVEL < 3) return;
|
|
716
|
+
const str = this.format(...args);
|
|
717
|
+
_Logger2.emit(3, str);
|
|
718
|
+
console.log(CliEffects.CLEAR + str);
|
|
719
|
+
}
|
|
720
|
+
info(...args) {
|
|
721
|
+
if (_Logger2.LOG_LEVEL < 2) return;
|
|
722
|
+
const str = this.format(...args);
|
|
723
|
+
_Logger2.emit(2, str);
|
|
724
|
+
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
|
|
725
|
+
}
|
|
726
|
+
warn(...args) {
|
|
727
|
+
if (_Logger2.LOG_LEVEL < 1) return;
|
|
728
|
+
const str = this.format(...args);
|
|
729
|
+
_Logger2.emit(1, str);
|
|
730
|
+
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
|
|
731
|
+
}
|
|
732
|
+
error(...args) {
|
|
733
|
+
if (_Logger2.LOG_LEVEL < 0) return;
|
|
734
|
+
const str = this.format(...args);
|
|
735
|
+
_Logger2.emit(0, str);
|
|
736
|
+
console.error(CliForeground.RED + str + CliEffects.CLEAR);
|
|
625
737
|
}
|
|
626
738
|
};
|
|
627
|
-
|
|
628
|
-
function
|
|
629
|
-
const
|
|
630
|
-
for (let
|
|
631
|
-
|
|
632
|
-
|
|
739
|
+
__publicField2(_Logger, "LOG_LEVEL", 4);
|
|
740
|
+
function PE(str, ...args) {
|
|
741
|
+
const combined = [];
|
|
742
|
+
for (let i = 0; i < str.length || i < args.length; i++) {
|
|
743
|
+
if (str[i]) combined.push(str[i]);
|
|
744
|
+
if (args[i]) combined.push(args[i]);
|
|
745
|
+
}
|
|
746
|
+
return new PathEvent(combined.join(""));
|
|
747
|
+
}
|
|
748
|
+
function PES(str, ...args) {
|
|
749
|
+
let combined = [];
|
|
750
|
+
for (let i = 0; i < str.length || i < args.length; i++) {
|
|
751
|
+
if (str[i]) combined.push(str[i]);
|
|
752
|
+
if (args[i]) combined.push(args[i]);
|
|
753
|
+
}
|
|
754
|
+
const [paths, methods] = combined.join("").split(":");
|
|
755
|
+
return PathEvent.toString(paths, methods == null ? void 0 : methods.split(""));
|
|
633
756
|
}
|
|
634
|
-
|
|
635
|
-
let e = [];
|
|
636
|
-
for (let i = 0; i < r.length || i < t.length; i++)
|
|
637
|
-
r[i] && e.push(r[i]), t[i] && e.push(t[i]);
|
|
638
|
-
const [n, s] = e.join("").split(":");
|
|
639
|
-
return y.toString(n, s == null ? void 0 : s.split(""));
|
|
757
|
+
class PathError extends Error {
|
|
640
758
|
}
|
|
641
|
-
class
|
|
642
|
-
constructor(
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
let
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
this.
|
|
759
|
+
class PathEvent {
|
|
760
|
+
constructor(Event) {
|
|
761
|
+
__publicField2(this, "module");
|
|
762
|
+
__publicField2(this, "fullPath");
|
|
763
|
+
__publicField2(this, "path");
|
|
764
|
+
__publicField2(this, "name");
|
|
765
|
+
__publicField2(this, "methods");
|
|
766
|
+
var _a;
|
|
767
|
+
if (typeof Event == "object") return Object.assign(this, Event);
|
|
768
|
+
let [p, scope, method] = Event.split(":");
|
|
769
|
+
if (!method) method = scope || "*";
|
|
770
|
+
if (p == "*" || !p && method == "*") {
|
|
771
|
+
p = "";
|
|
772
|
+
method = "*";
|
|
773
|
+
}
|
|
774
|
+
let temp = p.split("/").filter((p2) => !!p2);
|
|
775
|
+
this.module = ((_a = temp.splice(0, 1)[0]) == null ? void 0 : _a.toLowerCase()) || "";
|
|
776
|
+
this.fullPath = p;
|
|
777
|
+
this.path = temp.join("/");
|
|
778
|
+
this.name = temp.pop() || "";
|
|
779
|
+
this.methods = new ASet(method.split(""));
|
|
780
|
+
}
|
|
781
|
+
/** All/Wildcard specified */
|
|
782
|
+
get all() {
|
|
783
|
+
return this.methods.has("*");
|
|
784
|
+
}
|
|
785
|
+
set all(v) {
|
|
786
|
+
v ? new ASet(["*"]) : this.methods.delete("*");
|
|
787
|
+
}
|
|
788
|
+
/** None specified */
|
|
789
|
+
get none() {
|
|
790
|
+
return this.methods.has("n");
|
|
791
|
+
}
|
|
792
|
+
set none(v) {
|
|
793
|
+
v ? this.methods = new ASet(["n"]) : this.methods.delete("n");
|
|
794
|
+
}
|
|
795
|
+
/** Create method specified */
|
|
796
|
+
get create() {
|
|
797
|
+
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("c"));
|
|
798
|
+
}
|
|
799
|
+
set create(v) {
|
|
800
|
+
v ? this.methods.delete("n").add("c") : this.methods.delete("c");
|
|
801
|
+
}
|
|
802
|
+
/** Read method specified */
|
|
803
|
+
get read() {
|
|
804
|
+
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("r"));
|
|
805
|
+
}
|
|
806
|
+
set read(v) {
|
|
807
|
+
v ? this.methods.delete("n").add("r") : this.methods.delete("r");
|
|
808
|
+
}
|
|
809
|
+
/** Update method specified */
|
|
810
|
+
get update() {
|
|
811
|
+
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("u"));
|
|
812
|
+
}
|
|
813
|
+
set update(v) {
|
|
814
|
+
v ? this.methods.delete("n").add("u") : this.methods.delete("u");
|
|
815
|
+
}
|
|
816
|
+
/** Delete method specified */
|
|
817
|
+
get delete() {
|
|
818
|
+
return !this.methods.has("n") && (this.methods.has("*") || this.methods.has("d"));
|
|
819
|
+
}
|
|
820
|
+
set delete(v) {
|
|
821
|
+
v ? this.methods.delete("n").add("d") : this.methods.delete("d");
|
|
660
822
|
}
|
|
661
823
|
/**
|
|
662
824
|
* Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
|
|
@@ -665,13 +827,35 @@ class y {
|
|
|
665
827
|
* @param {string | PathEvent} paths Events as strings or pre-parsed
|
|
666
828
|
* @return {PathEvent} Final combined permission
|
|
667
829
|
*/
|
|
668
|
-
static combine(
|
|
669
|
-
let
|
|
670
|
-
const
|
|
671
|
-
const
|
|
672
|
-
return
|
|
673
|
-
}).reduce((
|
|
674
|
-
|
|
830
|
+
static combine(...paths) {
|
|
831
|
+
let hitNone = false;
|
|
832
|
+
const combined = paths.map((p) => new PathEvent(p)).toSorted((p1, p2) => {
|
|
833
|
+
const l1 = p1.fullPath.length, l2 = p2.fullPath.length;
|
|
834
|
+
return l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
|
|
835
|
+
}).reduce((acc, p) => {
|
|
836
|
+
if (p.none) hitNone = true;
|
|
837
|
+
if (!acc) return p;
|
|
838
|
+
if (hitNone) return acc;
|
|
839
|
+
acc.methods = [...acc.methods, ...p.methods];
|
|
840
|
+
return acc;
|
|
841
|
+
}, null);
|
|
842
|
+
combined.methods = new ASet(combined.methods);
|
|
843
|
+
return combined;
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Filter a set of paths based on the target
|
|
847
|
+
*
|
|
848
|
+
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
|
|
849
|
+
* @param filter {...PathEvent} Must container one of
|
|
850
|
+
* @return {boolean} Whether there is any overlap
|
|
851
|
+
*/
|
|
852
|
+
static filter(target, ...filter) {
|
|
853
|
+
const parsedTarget = makeArray(target).map((pe) => new PathEvent(pe));
|
|
854
|
+
const parsedFind = makeArray(filter).map((pe) => new PathEvent(pe));
|
|
855
|
+
return parsedTarget.filter((t) => {
|
|
856
|
+
if (!t.fullPath && t.all) return true;
|
|
857
|
+
return !!parsedFind.find((f) => t.fullPath.startsWith(f.fullPath) && (f.all || t.all || t.methods.intersection(f.methods).length));
|
|
858
|
+
});
|
|
675
859
|
}
|
|
676
860
|
/**
|
|
677
861
|
* Squash 2 sets of paths & return true if any overlap is found
|
|
@@ -680,14 +864,15 @@ class y {
|
|
|
680
864
|
* @param has Target must have at least one of these path
|
|
681
865
|
* @return {boolean} Whether there is any overlap
|
|
682
866
|
*/
|
|
683
|
-
static has(
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
867
|
+
static has(target, ...has) {
|
|
868
|
+
const parsedRequired = makeArray(has).map((pe) => new PathEvent(pe));
|
|
869
|
+
const parsedTarget = makeArray(target).map((pe) => new PathEvent(pe));
|
|
870
|
+
return !!parsedRequired.find((r) => {
|
|
871
|
+
if (!r.fullPath && r.all) return true;
|
|
872
|
+
const filtered = parsedTarget.filter((p) => r.fullPath.startsWith(p.fullPath));
|
|
873
|
+
if (!filtered.length) return false;
|
|
874
|
+
const combined = PathEvent.combine(...filtered);
|
|
875
|
+
return !combined.none && (combined.all || r.all) || combined.methods.intersection(r.methods).length;
|
|
691
876
|
});
|
|
692
877
|
}
|
|
693
878
|
/**
|
|
@@ -697,8 +882,8 @@ class y {
|
|
|
697
882
|
* @param has Target must have all these paths
|
|
698
883
|
* @return {boolean} Whether there is any overlap
|
|
699
884
|
*/
|
|
700
|
-
static hasAll(
|
|
701
|
-
return
|
|
885
|
+
static hasAll(target, ...has) {
|
|
886
|
+
return has.filter((h) => PathEvent.has(target, h)).length == has.length;
|
|
702
887
|
}
|
|
703
888
|
/**
|
|
704
889
|
* Same as `has` but raises an error if there is no overlap
|
|
@@ -706,8 +891,8 @@ class y {
|
|
|
706
891
|
* @param {string | string[]} target Array of Events as strings or pre-parsed
|
|
707
892
|
* @param has Target must have at least one of these path
|
|
708
893
|
*/
|
|
709
|
-
static hasFatal(
|
|
710
|
-
if (!
|
|
894
|
+
static hasFatal(target, ...has) {
|
|
895
|
+
if (!PathEvent.has(target, ...has)) throw new PathError(`Requires one of: ${makeArray(has).join(", ")}`);
|
|
711
896
|
}
|
|
712
897
|
/**
|
|
713
898
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
|
@@ -715,8 +900,8 @@ class y {
|
|
|
715
900
|
* @param {string | string[]} target Array of Events as strings or pre-parsed
|
|
716
901
|
* @param has Target must have all these paths
|
|
717
902
|
*/
|
|
718
|
-
static hasAllFatal(
|
|
719
|
-
if (!
|
|
903
|
+
static hasAllFatal(target, ...has) {
|
|
904
|
+
if (!PathEvent.hasAll(target, ...has)) throw new PathError(`Requires all: ${makeArray(has).join(", ")}`);
|
|
720
905
|
}
|
|
721
906
|
/**
|
|
722
907
|
* Create event string from its components
|
|
@@ -725,9 +910,20 @@ class y {
|
|
|
725
910
|
* @param {Method} methods Event method
|
|
726
911
|
* @return {string} String representation of Event
|
|
727
912
|
*/
|
|
728
|
-
static toString(
|
|
729
|
-
let
|
|
730
|
-
|
|
913
|
+
static toString(path, methods) {
|
|
914
|
+
let p = makeArray(path).filter((p2) => p2 != null).join("/");
|
|
915
|
+
p = p == null ? void 0 : p.trim().replaceAll(/\/{2,}/g, "/").replaceAll(/(^\/|\/$)/g, "");
|
|
916
|
+
if (methods == null ? void 0 : methods.length) p += `:${makeArray(methods).map((m) => m.toLowerCase()).join("")}`;
|
|
917
|
+
return p;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Filter a set of paths based on this event
|
|
921
|
+
*
|
|
922
|
+
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
|
|
923
|
+
* @return {boolean} Whether there is any overlap
|
|
924
|
+
*/
|
|
925
|
+
filter(target) {
|
|
926
|
+
return PathEvent.filter(target, this);
|
|
731
927
|
}
|
|
732
928
|
/**
|
|
733
929
|
* Create event string from its components
|
|
@@ -735,38 +931,41 @@ class y {
|
|
|
735
931
|
* @return {string} String representation of Event
|
|
736
932
|
*/
|
|
737
933
|
toString() {
|
|
738
|
-
return
|
|
934
|
+
return PathEvent.toString(this.fullPath, this.methods);
|
|
739
935
|
}
|
|
740
936
|
}
|
|
741
|
-
class
|
|
937
|
+
class PathEventEmitter {
|
|
742
938
|
constructor() {
|
|
743
|
-
|
|
939
|
+
__publicField2(this, "listeners", []);
|
|
744
940
|
}
|
|
745
|
-
emit(
|
|
746
|
-
const
|
|
747
|
-
this.listeners.filter((
|
|
941
|
+
emit(event, ...args) {
|
|
942
|
+
const parsed = new PathEvent(event);
|
|
943
|
+
this.listeners.filter((l) => PathEvent.has(l[0], event)).forEach(async (l) => l[1](parsed, ...args));
|
|
748
944
|
}
|
|
749
|
-
off(
|
|
750
|
-
this.listeners = this.listeners.filter((
|
|
945
|
+
off(listener) {
|
|
946
|
+
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
751
947
|
}
|
|
752
|
-
on(
|
|
753
|
-
|
|
948
|
+
on(event, listener) {
|
|
949
|
+
makeArray(event).forEach((e) => this.listeners.push([new PathEvent(e), listener]));
|
|
950
|
+
return () => this.off(listener);
|
|
754
951
|
}
|
|
755
|
-
once(
|
|
756
|
-
return new Promise((
|
|
757
|
-
const
|
|
758
|
-
|
|
952
|
+
once(event, listener) {
|
|
953
|
+
return new Promise((res) => {
|
|
954
|
+
const unsubscribe = this.on(event, (event2, ...args) => {
|
|
955
|
+
res(args.length < 2 ? args[0] : args);
|
|
956
|
+
if (listener) listener(event2, ...args);
|
|
957
|
+
unsubscribe();
|
|
759
958
|
});
|
|
760
959
|
});
|
|
761
960
|
}
|
|
762
|
-
relayEvents(
|
|
763
|
-
|
|
961
|
+
relayEvents(emitter) {
|
|
962
|
+
emitter.on("*", (event, ...args) => this.emit(event, ...args));
|
|
764
963
|
}
|
|
765
964
|
}
|
|
766
|
-
class Api extends
|
|
965
|
+
class Api extends Http {
|
|
767
966
|
constructor(url = location.origin, opts = {}) {
|
|
768
967
|
super({ ...(opts == null ? void 0 : opts.http) || {}, url });
|
|
769
|
-
__publicField(this, "emitter", new
|
|
968
|
+
__publicField(this, "emitter", new PathEventEmitter());
|
|
770
969
|
__publicField(this, "pending", {});
|
|
771
970
|
__publicField(this, "storageKey");
|
|
772
971
|
__publicField(this, "host");
|
|
@@ -796,27 +995,27 @@ class Api extends F {
|
|
|
796
995
|
if (token) localStorage.setItem(this.storageKey, token);
|
|
797
996
|
else localStorage.removeItem(this.storageKey);
|
|
798
997
|
}
|
|
799
|
-
this.emit(
|
|
998
|
+
this.emit(PES`api/token:${token ? "u" : "d"}`, token);
|
|
800
999
|
}
|
|
801
1000
|
healthcheck() {
|
|
802
1001
|
return this.request({ url: "/api/healthcheck" }).then((resp) => {
|
|
803
|
-
this.emit(
|
|
1002
|
+
this.emit(PES`api/healthcheck:r`, resp);
|
|
804
1003
|
return resp;
|
|
805
1004
|
});
|
|
806
1005
|
}
|
|
807
1006
|
request(options) {
|
|
808
|
-
const key =
|
|
1007
|
+
const key = JSONSanitize(options);
|
|
809
1008
|
const method = options.method == "GET" ? "r" : options.method == "POST" ? "c" : options.method == "DELETE" ? "d" : "u";
|
|
810
1009
|
if (this.pending[key] != null) return this.pending[key];
|
|
811
1010
|
this.pending[key] = super.request(options).then((response) => {
|
|
812
|
-
this.emit(
|
|
1011
|
+
this.emit(PES`api/response:${method}`, { request: options, response });
|
|
813
1012
|
return response.data;
|
|
814
1013
|
}).catch((err) => {
|
|
815
1014
|
const e = (err == null ? void 0 : err.data) || err;
|
|
816
|
-
this.emit(
|
|
1015
|
+
this.emit(PES`api/error:${method}`, { request: options, error: e });
|
|
817
1016
|
throw e;
|
|
818
1017
|
}).finally(() => delete this.pending[key]);
|
|
819
|
-
this.emit(
|
|
1018
|
+
this.emit(PES`api/request:${method}`, { request: options, response: this.pending[key] });
|
|
820
1019
|
return this.pending[key];
|
|
821
1020
|
}
|
|
822
1021
|
}
|
|
@@ -830,59 +1029,60 @@ var ActionType = /* @__PURE__ */ ((ActionType2) => {
|
|
|
830
1029
|
ActionType2[ActionType2["PUT"] = 6] = "PUT";
|
|
831
1030
|
return ActionType2;
|
|
832
1031
|
})(ActionType || {});
|
|
833
|
-
class Actions extends
|
|
1032
|
+
class Actions extends PathEventEmitter {
|
|
834
1033
|
constructor(api) {
|
|
835
1034
|
super();
|
|
836
1035
|
__publicField(this, "api");
|
|
837
|
-
__publicField(this, "cache", new
|
|
1036
|
+
__publicField(this, "cache", new Cache("_id"));
|
|
838
1037
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
839
1038
|
}
|
|
840
|
-
all() {
|
|
841
|
-
|
|
1039
|
+
async all(reload) {
|
|
1040
|
+
if (!reload && this.cache.complete) return this.cache.all();
|
|
1041
|
+
return this.api.request({ url: `/api/` + PES`actions` }).then((resp) => {
|
|
842
1042
|
this.cache.addAll(resp);
|
|
843
|
-
this.emit(
|
|
1043
|
+
this.emit(PES`actions:r`, resp || []);
|
|
844
1044
|
return resp;
|
|
845
1045
|
});
|
|
846
1046
|
}
|
|
847
1047
|
delete(id) {
|
|
848
1048
|
if (!id) throw new Error("Cannot delete action, missing ID");
|
|
849
|
-
return this.api.request({ url: `/api/` +
|
|
1049
|
+
return this.api.request({ url: `/api/` + PES`actions/${id}`, method: "DELETE" }).then(() => {
|
|
850
1050
|
this.cache.delete(id);
|
|
851
|
-
this.emit(
|
|
1051
|
+
this.emit(PES`actions/${id}:d`, id);
|
|
852
1052
|
});
|
|
853
1053
|
}
|
|
854
1054
|
read(id, reload = false) {
|
|
855
1055
|
if (!id) throw new Error("Cannot read action, missing ID");
|
|
856
1056
|
const cached = this.cache.get(id);
|
|
857
1057
|
if (!reload && cached) return Promise.resolve(cached);
|
|
858
|
-
return this.api.request({ url: `/api/` +
|
|
1058
|
+
return this.api.request({ url: `/api/` + PES`actions/${id}` }).then((action) => {
|
|
859
1059
|
if (action) this.cache.add(action);
|
|
860
|
-
this.emit(
|
|
1060
|
+
this.emit(PES`actions/${id}:r`, action);
|
|
861
1061
|
return action;
|
|
862
1062
|
});
|
|
863
1063
|
}
|
|
864
1064
|
run(path, opts = {}) {
|
|
865
1065
|
if (!path) throw new Error("Cannot run action, missing path");
|
|
866
|
-
return this.api.request({ url: `/api/` +
|
|
1066
|
+
return this.api.request({ url: `/api/` + PES`actions/run/${path}`, ...opts });
|
|
867
1067
|
}
|
|
868
1068
|
runById(action, opts = {}) {
|
|
869
1069
|
const id = typeof action == "string" ? action : action == null ? void 0 : action._id;
|
|
870
1070
|
if (!id) throw new Error("Cannot run action, missing ID");
|
|
871
|
-
return this.api.request({ url: "/api/" +
|
|
1071
|
+
return this.api.request({ url: "/api/" + PES`actions/run-by-id/${id}`, method: "POST", ...opts });
|
|
872
1072
|
}
|
|
873
1073
|
update(action) {
|
|
874
1074
|
return this.api.request({
|
|
875
|
-
url: `/api/` +
|
|
1075
|
+
url: `/api/` + PES`actions/${action._id}`,
|
|
876
1076
|
method: "POST",
|
|
877
1077
|
body: action
|
|
878
1078
|
}).then((action2) => {
|
|
879
1079
|
if (action2) this.cache.add(action2);
|
|
880
|
-
this.emit(
|
|
1080
|
+
this.emit(PES`actions/${action2._id}:u`, action2);
|
|
881
1081
|
return action2;
|
|
882
1082
|
});
|
|
883
1083
|
}
|
|
884
1084
|
}
|
|
885
|
-
class Ai extends
|
|
1085
|
+
class Ai extends PathEventEmitter {
|
|
886
1086
|
constructor(api) {
|
|
887
1087
|
super();
|
|
888
1088
|
__publicField(this, "api");
|
|
@@ -890,19 +1090,19 @@ class Ai extends re {
|
|
|
890
1090
|
}
|
|
891
1091
|
ask(question, context) {
|
|
892
1092
|
if (!question) throw new Error("Cannot ask AI, missing question");
|
|
893
|
-
return this.api.request({ url: `/api/` +
|
|
1093
|
+
return this.api.request({ url: `/api/` + PES`ai`, method: "POST", body: {
|
|
894
1094
|
question,
|
|
895
1095
|
context
|
|
896
1096
|
} }).then((response) => {
|
|
897
|
-
this.emit(
|
|
1097
|
+
this.emit(PES`ai:c`, { question, context, response });
|
|
898
1098
|
return response;
|
|
899
1099
|
});
|
|
900
1100
|
}
|
|
901
1101
|
clear() {
|
|
902
|
-
return this.api.request({ url: "/api/" +
|
|
1102
|
+
return this.api.request({ url: "/api/" + PES`ai`, method: "DELETE" }).then(() => this.emit(PES`ai:d`, this.api.token));
|
|
903
1103
|
}
|
|
904
1104
|
}
|
|
905
|
-
class Analytics extends
|
|
1105
|
+
class Analytics extends PathEventEmitter {
|
|
906
1106
|
constructor(api) {
|
|
907
1107
|
super();
|
|
908
1108
|
__publicField(this, "api");
|
|
@@ -910,19 +1110,27 @@ class Analytics extends re {
|
|
|
910
1110
|
}
|
|
911
1111
|
ipTrace(ip) {
|
|
912
1112
|
if (!ip) throw new Error("Cannot trace, missing IP");
|
|
913
|
-
return this.api.request({ url: `/api/` +
|
|
914
|
-
this.emit(
|
|
1113
|
+
return this.api.request({ url: `/api/` + PES`analytics/trace/${ip}` }).then((resp) => {
|
|
1114
|
+
this.emit(PES`analytics/trace/${ip}:r`, resp);
|
|
915
1115
|
return resp;
|
|
916
1116
|
});
|
|
917
1117
|
}
|
|
918
1118
|
}
|
|
919
|
-
class Auth extends
|
|
1119
|
+
class Auth extends PathEventEmitter {
|
|
920
1120
|
constructor(api, opts = {}) {
|
|
921
1121
|
super();
|
|
922
1122
|
__publicField(this, "api");
|
|
1123
|
+
__publicField(this, "_permissions", []);
|
|
923
1124
|
__publicField(this, "_user");
|
|
1125
|
+
// Permission helpers
|
|
1126
|
+
__publicField(this, "filter", (...events) => PathEvent.filter(this.permissions, ...events));
|
|
1127
|
+
__publicField(this, "has", (...events) => PathEvent.has(this.permissions, ...events));
|
|
1128
|
+
__publicField(this, "hasAll", (...events) => PathEvent.hasAll(this.permissions, ...events));
|
|
1129
|
+
__publicField(this, "hasFatal", (...events) => PathEvent.hasFatal(this.permissions, ...events));
|
|
1130
|
+
__publicField(this, "hasAllFatal", (...events) => PathEvent.hasAllFatal(this.permissions, ...events));
|
|
924
1131
|
__publicField(this, "enableTotp", this.resetTotp);
|
|
925
1132
|
this.opts = opts;
|
|
1133
|
+
window.PathEvent = PathEvent;
|
|
926
1134
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
927
1135
|
this.opts = {
|
|
928
1136
|
loginUrl: this.api.url + "/ui/#/login",
|
|
@@ -935,7 +1143,7 @@ class Auth extends re {
|
|
|
935
1143
|
"/api/auth/totp"
|
|
936
1144
|
];
|
|
937
1145
|
if (resp.status == 401 && !blacklist.find((url) => resp.url.includes(url)))
|
|
938
|
-
this.emit(
|
|
1146
|
+
this.emit(PES`auth/session-expired:d`, this.api.token);
|
|
939
1147
|
next();
|
|
940
1148
|
});
|
|
941
1149
|
if (this.api.token) this.session(this.api.token, true);
|
|
@@ -946,13 +1154,20 @@ class Auth extends re {
|
|
|
946
1154
|
else this.user = null;
|
|
947
1155
|
});
|
|
948
1156
|
}
|
|
1157
|
+
get permissions() {
|
|
1158
|
+
return this._permissions;
|
|
1159
|
+
}
|
|
1160
|
+
set permissions(perms) {
|
|
1161
|
+
this._permissions = perms;
|
|
1162
|
+
this.emit(PES`auth/permissions:u`, this._permissions);
|
|
1163
|
+
}
|
|
949
1164
|
get user() {
|
|
950
1165
|
return this._user;
|
|
951
1166
|
}
|
|
952
1167
|
set user(user) {
|
|
953
|
-
if (!
|
|
1168
|
+
if (!isEqual(this.user, user)) {
|
|
954
1169
|
this._user = user ? user : null;
|
|
955
|
-
this.emit(
|
|
1170
|
+
this.emit(PES`auth/user:u`, this._user);
|
|
956
1171
|
}
|
|
957
1172
|
}
|
|
958
1173
|
knownHost(host = location.origin) {
|
|
@@ -973,7 +1188,7 @@ class Auth extends re {
|
|
|
973
1188
|
}).then(async (resp) => {
|
|
974
1189
|
this.api.token = (resp == null ? void 0 : resp.token) || null;
|
|
975
1190
|
const user = await this.once("auth/user");
|
|
976
|
-
this.emit(
|
|
1191
|
+
this.emit(PES`auth/login/${username}:u`, user);
|
|
977
1192
|
return user;
|
|
978
1193
|
});
|
|
979
1194
|
}
|
|
@@ -994,7 +1209,7 @@ class Auth extends re {
|
|
|
994
1209
|
});
|
|
995
1210
|
}
|
|
996
1211
|
logout() {
|
|
997
|
-
this.emit(
|
|
1212
|
+
this.emit(PES`auth/logout:d`, this.user);
|
|
998
1213
|
this.api.token = null;
|
|
999
1214
|
this.user = null;
|
|
1000
1215
|
}
|
|
@@ -1003,7 +1218,7 @@ class Auth extends re {
|
|
|
1003
1218
|
if (!u.username || !u.password) throw new Error("Cannot register user, missing username or password");
|
|
1004
1219
|
const user = await this.api.request({ url: "/api/auth/register", body: { ...u } });
|
|
1005
1220
|
if ((_a = user == null ? void 0 : user.image) == null ? void 0 : _a.startsWith("/")) user.image = `${this.api.url}${user.image}?token=${this.api.token}`;
|
|
1006
|
-
this.emit(
|
|
1221
|
+
this.emit(PES`auth/register:c`, user);
|
|
1007
1222
|
return user;
|
|
1008
1223
|
}
|
|
1009
1224
|
reset(emailOrPass, token) {
|
|
@@ -1016,7 +1231,7 @@ class Auth extends re {
|
|
|
1016
1231
|
password: token ? emailOrPass : void 0
|
|
1017
1232
|
}
|
|
1018
1233
|
}).then(() => {
|
|
1019
|
-
this.emit(
|
|
1234
|
+
this.emit(PES`auth/reset:${token ? "u" : "c"}`, token || emailOrPass);
|
|
1020
1235
|
});
|
|
1021
1236
|
}
|
|
1022
1237
|
async session(token, set = false) {
|
|
@@ -1025,12 +1240,13 @@ class Auth extends re {
|
|
|
1025
1240
|
url: "/api/auth/session",
|
|
1026
1241
|
headers: token ? { "Authorization": `Bearer ${token}` } : void 0
|
|
1027
1242
|
});
|
|
1028
|
-
this.emit(
|
|
1243
|
+
this.emit(PES`auth/session:r`, session);
|
|
1029
1244
|
if (set) {
|
|
1030
1245
|
this.api.token = token;
|
|
1031
1246
|
if (session == null ? void 0 : session.user) session.user.image = `${this.api.url}${session.user.image}?token=${this.api.token}`;
|
|
1032
1247
|
this.user = (session == null ? void 0 : session.user) || null;
|
|
1033
|
-
|
|
1248
|
+
this.permissions = (session == null ? void 0 : session.permissions) || [];
|
|
1249
|
+
if (session) this.emit(PES`auth/login:c`, session.user);
|
|
1034
1250
|
}
|
|
1035
1251
|
return session;
|
|
1036
1252
|
}
|
|
@@ -1040,7 +1256,7 @@ class Auth extends re {
|
|
|
1040
1256
|
url: "/api/auth/password",
|
|
1041
1257
|
body: { username, password, oldPassword }
|
|
1042
1258
|
}).then((resp) => {
|
|
1043
|
-
this.emit(
|
|
1259
|
+
this.emit(PES`auth/reset:u`, resp == null ? void 0 : resp.token);
|
|
1044
1260
|
if (resp == null ? void 0 : resp.token) this.api.token = resp.token;
|
|
1045
1261
|
});
|
|
1046
1262
|
}
|
|
@@ -1052,22 +1268,32 @@ class Auth extends re {
|
|
|
1052
1268
|
return this.api.request({ url: `/api/auth/totp/${username}`, method: "POST" });
|
|
1053
1269
|
}
|
|
1054
1270
|
setupTotp(username, method = "app", totp) {
|
|
1055
|
-
return this.api.request({ url: `/api/auth/totp/${username}`, body:
|
|
1271
|
+
return this.api.request({ url: `/api/auth/totp/${username}`, body: clean({
|
|
1056
1272
|
method,
|
|
1057
1273
|
totp
|
|
1058
1274
|
}) });
|
|
1059
1275
|
}
|
|
1060
1276
|
}
|
|
1061
|
-
class Client extends
|
|
1062
|
-
constructor(settings) {
|
|
1277
|
+
class Client extends PathEventEmitter {
|
|
1278
|
+
constructor(api, settings) {
|
|
1063
1279
|
super();
|
|
1280
|
+
__publicField(this, "_notifications", false);
|
|
1064
1281
|
__publicField(this, "_platform");
|
|
1065
1282
|
__publicField(this, "_pwa");
|
|
1283
|
+
this.api = api;
|
|
1066
1284
|
this.settings = settings;
|
|
1285
|
+
this.pushSubscription.then((resp) => this.notifications = !!resp);
|
|
1067
1286
|
}
|
|
1068
1287
|
get mobile() {
|
|
1069
1288
|
return ["android", "ios"].includes(this.platform);
|
|
1070
1289
|
}
|
|
1290
|
+
get notifications() {
|
|
1291
|
+
return this._notifications;
|
|
1292
|
+
}
|
|
1293
|
+
set notifications(enabled) {
|
|
1294
|
+
this._notifications = enabled;
|
|
1295
|
+
this.emit(PES`client/notifications:${enabled ? "c" : "d"}`, enabled);
|
|
1296
|
+
}
|
|
1071
1297
|
get platform() {
|
|
1072
1298
|
if (!this._platform) {
|
|
1073
1299
|
const userAgent = navigator.userAgent || navigator.vendor;
|
|
@@ -1080,6 +1306,10 @@ class Client extends re {
|
|
|
1080
1306
|
}
|
|
1081
1307
|
return this._platform;
|
|
1082
1308
|
}
|
|
1309
|
+
get pushSubscription() {
|
|
1310
|
+
if (!navigator.serviceWorker.controller) return Promise.resolve(null);
|
|
1311
|
+
return navigator.serviceWorker.ready.then((sw) => sw.pushManager.getSubscription());
|
|
1312
|
+
}
|
|
1083
1313
|
get pwa() {
|
|
1084
1314
|
if (this._pwa == null)
|
|
1085
1315
|
this._pwa = window.matchMedia("(display-mode: standalone)").matches || (navigator == null ? void 0 : navigator.standalone) || document.referrer.includes("android-app://");
|
|
@@ -1161,7 +1391,7 @@ class Client extends re {
|
|
|
1161
1391
|
if (!dismissed && !this.pwa && this.mobile) this.pwaPrompt();
|
|
1162
1392
|
}, 500);
|
|
1163
1393
|
}
|
|
1164
|
-
this.emit(
|
|
1394
|
+
this.emit(PES`client/inject:c`, this.platform);
|
|
1165
1395
|
}
|
|
1166
1396
|
pwaPrompt(platform) {
|
|
1167
1397
|
const url = this.settings.api.url;
|
|
@@ -1278,16 +1508,34 @@ class Client extends re {
|
|
|
1278
1508
|
setTimeout(() => {
|
|
1279
1509
|
prompt.remove();
|
|
1280
1510
|
backdrop.remove();
|
|
1281
|
-
this.emit(
|
|
1511
|
+
this.emit(PES`client/pwa:d`, platform);
|
|
1282
1512
|
}, 500);
|
|
1283
1513
|
};
|
|
1284
1514
|
prompt.append(close);
|
|
1285
1515
|
backdrop.append(prompt);
|
|
1286
1516
|
document.body.append(backdrop);
|
|
1287
|
-
this.emit(
|
|
1517
|
+
this.emit(PES`client/pwa:c`, platform);
|
|
1518
|
+
}
|
|
1519
|
+
async enableNotifications() {
|
|
1520
|
+
const granted = await Notification.requestPermission();
|
|
1521
|
+
if (!granted) return null;
|
|
1522
|
+
const sw = await navigator.serviceWorker.ready;
|
|
1523
|
+
const subscription = (await sw.pushManager.subscribe({
|
|
1524
|
+
userVisibleOnly: true,
|
|
1525
|
+
applicationServerKey: this.settings.cache["push_public_key"]
|
|
1526
|
+
})).toJSON();
|
|
1527
|
+
return this.api.request({ url: "/api/notifications", body: subscription }).then(() => this.notifications = true);
|
|
1528
|
+
}
|
|
1529
|
+
async disableNotifications() {
|
|
1530
|
+
var _a;
|
|
1531
|
+
const subscription = await this.pushSubscription;
|
|
1532
|
+
subscription == null ? void 0 : subscription.unsubscribe();
|
|
1533
|
+
return this.api.request({ url: "/api/notifications", method: "DELETE", body: {
|
|
1534
|
+
p256dh: (_a = subscription == null ? void 0 : subscription.toJSON().keys) == null ? void 0 : _a["p256dh"]
|
|
1535
|
+
} }).then(() => this.notifications = false);
|
|
1288
1536
|
}
|
|
1289
1537
|
}
|
|
1290
|
-
class Data extends
|
|
1538
|
+
class Data extends PathEventEmitter {
|
|
1291
1539
|
constructor(api) {
|
|
1292
1540
|
super();
|
|
1293
1541
|
__publicField(this, "api");
|
|
@@ -1296,25 +1544,25 @@ class Data extends re {
|
|
|
1296
1544
|
create(collection, document2) {
|
|
1297
1545
|
if (!collection || !document2) throw new Error("Cannot create document, missing collection or document");
|
|
1298
1546
|
return this.api.request({
|
|
1299
|
-
url: `/api/` +
|
|
1547
|
+
url: `/api/` + PES`data/${collection}`,
|
|
1300
1548
|
method: "POST",
|
|
1301
1549
|
body: document2
|
|
1302
1550
|
}).then((resp) => {
|
|
1303
|
-
this.emit(
|
|
1551
|
+
this.emit(PES`data/${collection}:c`, resp);
|
|
1304
1552
|
return resp;
|
|
1305
1553
|
});
|
|
1306
1554
|
}
|
|
1307
1555
|
delete(collection, id) {
|
|
1308
1556
|
if (!collection || !id) throw new Error("Cannot delete document, missing collection or ID");
|
|
1309
1557
|
return this.api.request({
|
|
1310
|
-
url: `/api/` +
|
|
1558
|
+
url: `/api/` + PES`data/${collection}/${id}`,
|
|
1311
1559
|
method: "DELETE"
|
|
1312
|
-
}).then(() => this.emit(
|
|
1560
|
+
}).then(() => this.emit(PES`data/${collection}/${id}:d`, id));
|
|
1313
1561
|
}
|
|
1314
1562
|
read(collection, id) {
|
|
1315
1563
|
if (!collection) throw new Error("Cannot read documents, missing collection");
|
|
1316
|
-
return this.api.request({ url: `/api/` +
|
|
1317
|
-
this.emit(
|
|
1564
|
+
return this.api.request({ url: `/api/` + PES`data/${collection}/${id}` }).then((resp) => {
|
|
1565
|
+
this.emit(PES`data/${collection}/${id}:r`, collection, resp);
|
|
1318
1566
|
return resp;
|
|
1319
1567
|
});
|
|
1320
1568
|
}
|
|
@@ -1322,42 +1570,42 @@ class Data extends re {
|
|
|
1322
1570
|
if (!collection || !document2) throw new Error("Cannot update document, missing collection or document");
|
|
1323
1571
|
if (!document2._id) return this.create(collection, document2);
|
|
1324
1572
|
return this.api.request({
|
|
1325
|
-
url: `/api/` +
|
|
1573
|
+
url: `/api/` + PES`data/${collection}/${document2._id}`,
|
|
1326
1574
|
method: append ? "PATCH" : "PUT",
|
|
1327
1575
|
body: document2
|
|
1328
1576
|
}).then((resp) => {
|
|
1329
|
-
this.emit(
|
|
1577
|
+
this.emit(PES`data/${collection}/${document2._id}:u`, resp);
|
|
1330
1578
|
return resp;
|
|
1331
1579
|
});
|
|
1332
1580
|
}
|
|
1333
1581
|
raw(collection, query) {
|
|
1334
1582
|
if (!collection || !query) throw new Error("Cannot execute raw query, missing collection or query");
|
|
1335
1583
|
const mode = query.operand.startsWith("find") ? "r" : query.operand == "insert" ? "c" : query.operand.startsWith("delete") ? "d" : "u";
|
|
1336
|
-
return this.api.request({ url: `/api/` +
|
|
1337
|
-
this.emit(
|
|
1584
|
+
return this.api.request({ url: `/api/` + PES`data/${collection}` + "?raw", body: query }).then((resp) => {
|
|
1585
|
+
this.emit(PES`data/${collection}:${mode}`, resp);
|
|
1338
1586
|
return resp;
|
|
1339
1587
|
});
|
|
1340
1588
|
}
|
|
1341
1589
|
// Schema ==========================================================================================================
|
|
1342
1590
|
deleteSchema(path) {
|
|
1343
1591
|
if (!path) throw new Error("Cannot delete schema, missing collection path");
|
|
1344
|
-
return this.api.request({ url: `/api/` +
|
|
1592
|
+
return this.api.request({ url: `/api/` + PES`schema/${path}`, method: "DELETE" }).then(() => this.emit(PES`schema/${path}:d`, path));
|
|
1345
1593
|
}
|
|
1346
1594
|
readSchema(pathOrTree) {
|
|
1347
|
-
return this.api.request({ url: "/api/" +
|
|
1348
|
-
this.emit(
|
|
1595
|
+
return this.api.request({ url: "/api/" + PES`schema/${typeof pathOrTree == "string" ? pathOrTree : ""}` + (pathOrTree === true ? `?tree=${pathOrTree}` : "") }).then((resp) => {
|
|
1596
|
+
this.emit(PES`schema/${typeof pathOrTree == "string" ? pathOrTree : ""}:r`, resp);
|
|
1349
1597
|
return resp;
|
|
1350
1598
|
});
|
|
1351
1599
|
}
|
|
1352
1600
|
updateSchema(schema) {
|
|
1353
1601
|
if (!schema.path) throw new Error("Cannot update schema, missing collection path");
|
|
1354
|
-
return this.api.request({ url: "/api/" +
|
|
1355
|
-
this.emit(
|
|
1602
|
+
return this.api.request({ url: "/api/" + PES`schema/${schema.path}`, body: schema }).then((resp) => {
|
|
1603
|
+
this.emit(PES`schema/${schema.path}:${schema._id ? "u" : "c"}`, resp);
|
|
1356
1604
|
return resp;
|
|
1357
1605
|
});
|
|
1358
1606
|
}
|
|
1359
1607
|
}
|
|
1360
|
-
class Email extends
|
|
1608
|
+
class Email extends PathEventEmitter {
|
|
1361
1609
|
constructor(api) {
|
|
1362
1610
|
super();
|
|
1363
1611
|
__publicField(this, "api");
|
|
@@ -1366,73 +1614,73 @@ class Email extends re {
|
|
|
1366
1614
|
send(email) {
|
|
1367
1615
|
var _a;
|
|
1368
1616
|
if (!email.to && !email.bcc || !email.body) throw new Error("Cannot send email, missing address or body");
|
|
1369
|
-
return this.api.request({ url: "/api/" +
|
|
1617
|
+
return this.api.request({ url: "/api/" + PES`email/${(_a = email.body) == null ? void 0 : _a.template}`, body: email }).then((response) => {
|
|
1370
1618
|
var _a2;
|
|
1371
|
-
this.emit(
|
|
1619
|
+
this.emit(PES`email/${(_a2 = email.body) == null ? void 0 : _a2.template}:c`, { email, response });
|
|
1372
1620
|
return response;
|
|
1373
1621
|
});
|
|
1374
1622
|
}
|
|
1375
1623
|
}
|
|
1376
|
-
class Groups extends
|
|
1624
|
+
class Groups extends PathEventEmitter {
|
|
1377
1625
|
constructor(api) {
|
|
1378
1626
|
super();
|
|
1379
1627
|
__publicField(this, "api");
|
|
1380
|
-
__publicField(this, "cache", new
|
|
1628
|
+
__publicField(this, "cache", new Cache("name"));
|
|
1381
1629
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1382
1630
|
}
|
|
1383
1631
|
async all(reload) {
|
|
1384
1632
|
if (!reload && this.cache.complete) return this.cache.all();
|
|
1385
|
-
return this.api.request({ url: `/api/` +
|
|
1633
|
+
return this.api.request({ url: `/api/` + PES`groups` }).then((resp) => {
|
|
1386
1634
|
this.cache.addAll(resp);
|
|
1387
|
-
this.emit(
|
|
1635
|
+
this.emit(PES`groups:r`, resp || []);
|
|
1388
1636
|
return resp;
|
|
1389
1637
|
});
|
|
1390
1638
|
}
|
|
1391
1639
|
delete(name) {
|
|
1392
1640
|
if (!name) throw new Error("Cannot delete group, missing name");
|
|
1393
1641
|
return this.api.request({
|
|
1394
|
-
url: `/api/` +
|
|
1642
|
+
url: `/api/` + PES`groups/${name}`,
|
|
1395
1643
|
method: "DELETE"
|
|
1396
1644
|
}).then(() => {
|
|
1397
1645
|
this.cache.delete(name);
|
|
1398
|
-
this.emit(
|
|
1646
|
+
this.emit(PES`groups/${name}:d`);
|
|
1399
1647
|
});
|
|
1400
1648
|
}
|
|
1401
1649
|
create(group) {
|
|
1402
1650
|
if (!group.name) throw new Error("Cannot create group, missing name");
|
|
1403
1651
|
return this.api.request({
|
|
1404
|
-
url: `/api/` +
|
|
1652
|
+
url: `/api/` + PES`groups/${group.name}`,
|
|
1405
1653
|
method: "POST",
|
|
1406
1654
|
body: group
|
|
1407
1655
|
}).then((resp) => {
|
|
1408
1656
|
this.cache.add(resp);
|
|
1409
|
-
this.emit(
|
|
1657
|
+
this.emit(PES`groups/${group.name}:c`, resp);
|
|
1410
1658
|
return resp;
|
|
1411
1659
|
});
|
|
1412
1660
|
}
|
|
1413
1661
|
async read(name, reload) {
|
|
1414
1662
|
if (!name) throw new Error("Cannot read group, missing name");
|
|
1415
1663
|
if (!reload && this.cache.get(name)) return this.cache.get(name);
|
|
1416
|
-
return this.api.request({ url: `/api/` +
|
|
1664
|
+
return this.api.request({ url: `/api/` + PES`groups/${name}` }).then((resp) => {
|
|
1417
1665
|
this.cache.add(resp);
|
|
1418
|
-
this.emit(
|
|
1666
|
+
this.emit(PES`groups/${name}:r`, resp);
|
|
1419
1667
|
return resp;
|
|
1420
1668
|
});
|
|
1421
1669
|
}
|
|
1422
1670
|
update(group) {
|
|
1423
1671
|
if (!group.name) throw new Error("Cannot update group, missing name");
|
|
1424
1672
|
return this.api.request({
|
|
1425
|
-
url: `/api/` +
|
|
1673
|
+
url: `/api/` + PES`groups/${group.name}`,
|
|
1426
1674
|
method: "PATCH",
|
|
1427
1675
|
body: group
|
|
1428
1676
|
}).then((resp) => {
|
|
1429
1677
|
this.cache.add(resp);
|
|
1430
|
-
this.emit(
|
|
1678
|
+
this.emit(PES`groups/${group.name}:u`, resp);
|
|
1431
1679
|
return resp;
|
|
1432
1680
|
});
|
|
1433
1681
|
}
|
|
1434
1682
|
}
|
|
1435
|
-
class Logger extends
|
|
1683
|
+
class Logger extends PathEventEmitter {
|
|
1436
1684
|
constructor(api, channel, logLevel) {
|
|
1437
1685
|
super();
|
|
1438
1686
|
__publicField(this, "api");
|
|
@@ -1447,7 +1695,7 @@ class Logger extends re {
|
|
|
1447
1695
|
if (channel.toLowerCase() == "server") throw new Error('"Server" namespace is reserved');
|
|
1448
1696
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1449
1697
|
if (logLevel != null && logLevel && logLevel != "NONE") {
|
|
1450
|
-
if (
|
|
1698
|
+
if (LOG_LEVEL[logLevel] >= 0) {
|
|
1451
1699
|
console.error = (...args) => {
|
|
1452
1700
|
this.console.error(...args);
|
|
1453
1701
|
this.error(...args);
|
|
@@ -1462,25 +1710,25 @@ ${log}`;
|
|
|
1462
1710
|
((_c = event.reason) == null ? void 0 : _c.code) == null || ((_d = event.reason) == null ? void 0 : _d.code) >= 500 ? this.error(log) : this.warn(log);
|
|
1463
1711
|
});
|
|
1464
1712
|
}
|
|
1465
|
-
if (
|
|
1713
|
+
if (LOG_LEVEL[logLevel] >= 1) {
|
|
1466
1714
|
console.warn = (...args) => {
|
|
1467
1715
|
this.console.warn(...args);
|
|
1468
1716
|
this.warn(...args);
|
|
1469
1717
|
};
|
|
1470
1718
|
}
|
|
1471
|
-
if (
|
|
1719
|
+
if (LOG_LEVEL[logLevel] >= 2) {
|
|
1472
1720
|
console.info = (...args) => {
|
|
1473
1721
|
this.console.info(...args);
|
|
1474
1722
|
this.info(...args);
|
|
1475
1723
|
};
|
|
1476
1724
|
}
|
|
1477
|
-
if (
|
|
1725
|
+
if (LOG_LEVEL[logLevel] >= 3) {
|
|
1478
1726
|
console.log = (...args) => {
|
|
1479
1727
|
this.console.log(...args);
|
|
1480
1728
|
this.log(...args);
|
|
1481
1729
|
};
|
|
1482
1730
|
}
|
|
1483
|
-
if (
|
|
1731
|
+
if (LOG_LEVEL[logLevel] >= 4) {
|
|
1484
1732
|
console.debug = (...args) => {
|
|
1485
1733
|
this.console.debug(...args);
|
|
1486
1734
|
this.debug(...args);
|
|
@@ -1503,39 +1751,39 @@ ${log}`;
|
|
|
1503
1751
|
}
|
|
1504
1752
|
create(log, channel = this.channel) {
|
|
1505
1753
|
if (channel.toLowerCase() == "server") throw new Error('"Server" namespace is reserved');
|
|
1506
|
-
return this.api.request({ url: `/api/` +
|
|
1754
|
+
return this.api.request({ url: `/api/` + PES`logs/${channel}`, body: log }).then(() => this.emit(PES`logs/${channel}:c`, log)).catch(() => {
|
|
1507
1755
|
});
|
|
1508
1756
|
}
|
|
1509
1757
|
channels() {
|
|
1510
|
-
return this.api.request({ url: "/api/" +
|
|
1758
|
+
return this.api.request({ url: "/api/" + PES`logs/channels` });
|
|
1511
1759
|
}
|
|
1512
1760
|
delete(channel = this.channel) {
|
|
1513
|
-
return this.api.request({ url: `/api/` +
|
|
1761
|
+
return this.api.request({ url: `/api/` + PES`logs/${channel}`, method: "DELETE" }).then(() => this.emit(PES`logs/${channel}:d`));
|
|
1514
1762
|
}
|
|
1515
1763
|
read(channel = this.channel) {
|
|
1516
|
-
return this.api.request({ url: `/api/` +
|
|
1517
|
-
this.emit(
|
|
1764
|
+
return this.api.request({ url: `/api/` + PES`logs/${channel}` }).then((logs) => {
|
|
1765
|
+
this.emit(PES`logs/${channel}:r`, logs);
|
|
1518
1766
|
return logs;
|
|
1519
1767
|
});
|
|
1520
1768
|
}
|
|
1521
1769
|
// Console =========================================================================================================
|
|
1522
1770
|
debug(log, channel = this.channel) {
|
|
1523
|
-
return this.create(this.buildLog(
|
|
1771
|
+
return this.create(this.buildLog(LOG_LEVEL.DEBUG, log), channel);
|
|
1524
1772
|
}
|
|
1525
1773
|
log(log, channel = this.channel) {
|
|
1526
|
-
return this.create(this.buildLog(
|
|
1774
|
+
return this.create(this.buildLog(LOG_LEVEL.LOG, log), channel);
|
|
1527
1775
|
}
|
|
1528
1776
|
info(log, channel = this.channel) {
|
|
1529
|
-
return this.create(this.buildLog(
|
|
1777
|
+
return this.create(this.buildLog(LOG_LEVEL.INFO, log), channel);
|
|
1530
1778
|
}
|
|
1531
1779
|
warn(log, channel = this.channel) {
|
|
1532
|
-
return this.create(this.buildLog(
|
|
1780
|
+
return this.create(this.buildLog(LOG_LEVEL.WARN, log), channel);
|
|
1533
1781
|
}
|
|
1534
1782
|
error(log, channel = this.channel) {
|
|
1535
|
-
return this.create(this.buildLog(
|
|
1783
|
+
return this.create(this.buildLog(LOG_LEVEL.ERROR, log), channel);
|
|
1536
1784
|
}
|
|
1537
1785
|
}
|
|
1538
|
-
class Payments extends
|
|
1786
|
+
class Payments extends PathEventEmitter {
|
|
1539
1787
|
constructor(api, secret) {
|
|
1540
1788
|
super();
|
|
1541
1789
|
__publicField(this, "api");
|
|
@@ -1558,7 +1806,7 @@ class Payments extends re {
|
|
|
1558
1806
|
amount,
|
|
1559
1807
|
custom
|
|
1560
1808
|
} });
|
|
1561
|
-
this.emit(
|
|
1809
|
+
this.emit(PES`payments:c`, request.data.clientSecret);
|
|
1562
1810
|
return request.data.clientSecret;
|
|
1563
1811
|
}
|
|
1564
1812
|
async createForm(element, amount, custom) {
|
|
@@ -1572,28 +1820,28 @@ class Payments extends re {
|
|
|
1572
1820
|
});
|
|
1573
1821
|
}
|
|
1574
1822
|
async history(username) {
|
|
1575
|
-
const history = await this.api.request({ url: `/api/` +
|
|
1576
|
-
this.emit(
|
|
1823
|
+
const history = await this.api.request({ url: `/api/` + PES`payments/${username}` });
|
|
1824
|
+
this.emit(PES`payments/${username}:r`, history);
|
|
1577
1825
|
return history;
|
|
1578
1826
|
}
|
|
1579
1827
|
}
|
|
1580
|
-
class Pdf extends
|
|
1828
|
+
class Pdf extends PathEventEmitter {
|
|
1581
1829
|
constructor(api) {
|
|
1582
1830
|
super();
|
|
1583
1831
|
__publicField(this, "api");
|
|
1584
1832
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1585
1833
|
}
|
|
1586
1834
|
createPdf(body, options) {
|
|
1587
|
-
return this.api.request({ url: `/api/` +
|
|
1835
|
+
return this.api.request({ url: `/api/` + PES`pdf`, body: { ...body, options }, decode: false }).then(async (resp) => {
|
|
1588
1836
|
const blob = await resp.blob();
|
|
1589
1837
|
if (options == null ? void 0 : options.download) {
|
|
1590
|
-
let filename = (options == null ? void 0 : options.filename) ||
|
|
1838
|
+
let filename = (options == null ? void 0 : options.filename) || timestampFilename();
|
|
1591
1839
|
if (!filename.endsWith(".pdf")) filename += ".pdf";
|
|
1592
1840
|
const url = URL.createObjectURL(blob);
|
|
1593
|
-
|
|
1841
|
+
downloadUrl(url, filename);
|
|
1594
1842
|
URL.revokeObjectURL(url);
|
|
1595
1843
|
}
|
|
1596
|
-
this.emit(
|
|
1844
|
+
this.emit(PES`pdf:c`, blob);
|
|
1597
1845
|
return blob;
|
|
1598
1846
|
});
|
|
1599
1847
|
}
|
|
@@ -1660,7 +1908,7 @@ const _Socket = class _Socket {
|
|
|
1660
1908
|
};
|
|
1661
1909
|
__publicField(_Socket, "pollingSpeed", 3e4);
|
|
1662
1910
|
let Socket = _Socket;
|
|
1663
|
-
|
|
1911
|
+
let Storage$1 = class Storage2 extends PathEventEmitter {
|
|
1664
1912
|
constructor(api) {
|
|
1665
1913
|
super();
|
|
1666
1914
|
__publicField(this, "api");
|
|
@@ -1668,119 +1916,119 @@ class Storage extends re {
|
|
|
1668
1916
|
}
|
|
1669
1917
|
copy(source, destination) {
|
|
1670
1918
|
if (!source || !destination) throw new Error("Cannot copy file or folder, missing source or destination");
|
|
1671
|
-
return this.api.request({ url: "/api/" +
|
|
1672
|
-
this.emit(
|
|
1919
|
+
return this.api.request({ url: "/api/" + PES`storage/${destination}`, body: { from: source } }).then((response) => {
|
|
1920
|
+
this.emit(PES`storage/${destination}:c`, response);
|
|
1673
1921
|
return response;
|
|
1674
1922
|
});
|
|
1675
1923
|
}
|
|
1676
1924
|
delete(path) {
|
|
1677
1925
|
if (!path) throw new Error("Cannot delete file or folder, missing path");
|
|
1678
|
-
return this.api.request({ url: "/api/" +
|
|
1679
|
-
this.emit(
|
|
1926
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}`, method: "DELETE" }).then(() => {
|
|
1927
|
+
this.emit(PES`storage/${path}:d`, path);
|
|
1680
1928
|
});
|
|
1681
1929
|
}
|
|
1682
1930
|
download(path, opts = {}) {
|
|
1683
1931
|
if (!path) throw new Error("Cannot download file, missing path");
|
|
1684
|
-
return this.api.request({ ...opts, url: "/api/" +
|
|
1932
|
+
return this.api.request({ ...opts, url: "/api/" + PES`storage/${path}`, decode: false }).then(async (response) => {
|
|
1685
1933
|
const blob = await response.blob();
|
|
1686
1934
|
const name = opts.downloadAs || path.split("/").pop();
|
|
1687
|
-
this.emit(
|
|
1688
|
-
|
|
1935
|
+
this.emit(PES`storage/${path}:r`, blob);
|
|
1936
|
+
downloadFile(blob, name);
|
|
1689
1937
|
return response;
|
|
1690
1938
|
});
|
|
1691
1939
|
}
|
|
1692
1940
|
list(path) {
|
|
1693
1941
|
if (!path) path = "/";
|
|
1694
|
-
return this.api.request({ url: "/api/" +
|
|
1695
|
-
this.emit(
|
|
1942
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}` + "?list" }).then((resp) => {
|
|
1943
|
+
this.emit(PES`storage/${path}:r`, resp);
|
|
1696
1944
|
return resp;
|
|
1697
1945
|
});
|
|
1698
1946
|
}
|
|
1699
1947
|
open(path, target = "_blank") {
|
|
1700
1948
|
if (!path) throw new Error("Cannot download file, missing path");
|
|
1701
|
-
const link = `${this.api.url}/api/` +
|
|
1949
|
+
const link = `${this.api.url}/api/` + PES`storage/${path}` + (this.api.token ? `?token=${this.api.token}` : "");
|
|
1702
1950
|
if (!target) return link;
|
|
1703
|
-
this.emit(
|
|
1951
|
+
this.emit(PES`storage/${path}:r`, path);
|
|
1704
1952
|
return window.open(link, target);
|
|
1705
1953
|
}
|
|
1706
1954
|
mkdir(path) {
|
|
1707
1955
|
if (!path) throw new Error("Cannot make directory, missing path");
|
|
1708
|
-
return this.api.request({ url: "/api/" +
|
|
1709
|
-
this.emit(
|
|
1956
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}`, body: { directory: true } }).then((resp) => {
|
|
1957
|
+
this.emit(PES`storage/${path}:c`, resp);
|
|
1710
1958
|
return resp;
|
|
1711
1959
|
});
|
|
1712
1960
|
}
|
|
1713
1961
|
move(source, destination) {
|
|
1714
1962
|
if (!source || !destination) throw new Error("Cannot move file or folder, missing source or destination");
|
|
1715
1963
|
if (source == destination) return this.list(destination);
|
|
1716
|
-
return this.api.request({ url: "/api/" +
|
|
1717
|
-
this.emit(
|
|
1964
|
+
return this.api.request({ url: "/api/" + PES`storage/${source}`, method: "PATCH", body: { move: destination } }).then((response) => {
|
|
1965
|
+
this.emit(PES`storage/${source}:u`, response);
|
|
1718
1966
|
return response;
|
|
1719
1967
|
});
|
|
1720
1968
|
}
|
|
1721
1969
|
upload(files, opts) {
|
|
1722
|
-
return new
|
|
1723
|
-
if (!files) files = await
|
|
1970
|
+
return new PromiseProgress(async (res, rej, prog) => {
|
|
1971
|
+
if (!files) files = await fileBrowser(typeof opts == "object" ? opts : void 0);
|
|
1724
1972
|
if (!files || Array.isArray(files) && !files.length) return [];
|
|
1725
1973
|
const path = (opts && typeof opts == "object" ? opts == null ? void 0 : opts.path : opts) || "/";
|
|
1726
|
-
return
|
|
1727
|
-
url: `${this.api.url}/api/` +
|
|
1728
|
-
files:
|
|
1974
|
+
return uploadWithProgress({
|
|
1975
|
+
url: `${this.api.url}/api/` + PES`storage/${path}`,
|
|
1976
|
+
files: makeArray(files),
|
|
1729
1977
|
headers: this.api.headers
|
|
1730
1978
|
}).onProgress((p) => {
|
|
1731
1979
|
prog(p);
|
|
1732
1980
|
}).then((resp) => {
|
|
1733
|
-
this.emit(
|
|
1981
|
+
this.emit(PES`storage/${path}:c`, resp);
|
|
1734
1982
|
res(resp);
|
|
1735
1983
|
}).catch((err) => rej(err));
|
|
1736
1984
|
});
|
|
1737
1985
|
}
|
|
1738
|
-
}
|
|
1739
|
-
class Users extends
|
|
1986
|
+
};
|
|
1987
|
+
class Users extends PathEventEmitter {
|
|
1740
1988
|
constructor(api) {
|
|
1741
1989
|
super();
|
|
1742
1990
|
__publicField(this, "api");
|
|
1743
|
-
__publicField(this, "cache", new
|
|
1991
|
+
__publicField(this, "cache", new Cache("username"));
|
|
1744
1992
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1745
1993
|
}
|
|
1746
1994
|
async all(reload) {
|
|
1747
1995
|
if (!reload && this.cache.complete) return this.cache.all();
|
|
1748
|
-
return this.api.request({ url: "/api/" +
|
|
1996
|
+
return this.api.request({ url: "/api/" + PES`users` }).then((resp) => {
|
|
1749
1997
|
resp == null ? void 0 : resp.forEach((r) => {
|
|
1750
1998
|
r.image = this.api.url + r.image + `?token=${this.api.token}`;
|
|
1751
1999
|
return r;
|
|
1752
2000
|
});
|
|
1753
2001
|
this.cache.addAll(resp);
|
|
1754
|
-
this.emit(
|
|
2002
|
+
this.emit(PES`users:r`, resp || []);
|
|
1755
2003
|
return resp;
|
|
1756
2004
|
});
|
|
1757
2005
|
}
|
|
1758
2006
|
delete(username) {
|
|
1759
2007
|
if (!username) throw new Error("Cannot delete user, missing username");
|
|
1760
2008
|
return this.api.request({
|
|
1761
|
-
url: "/api/" +
|
|
2009
|
+
url: "/api/" + PES`users/${username}`,
|
|
1762
2010
|
method: "DELETE"
|
|
1763
2011
|
}).then(() => {
|
|
1764
2012
|
this.cache.delete(username);
|
|
1765
|
-
this.emit(
|
|
2013
|
+
this.emit(PES`users/${username}:d`, username);
|
|
1766
2014
|
});
|
|
1767
2015
|
}
|
|
1768
2016
|
async read(username, reload) {
|
|
1769
2017
|
if (!username) throw new Error("Cannot read user, missing username");
|
|
1770
2018
|
if (!reload && this.cache.get(username)) return this.cache.get(username);
|
|
1771
|
-
return this.api.request({ url: "/api/" +
|
|
2019
|
+
return this.api.request({ url: "/api/" + PES`users/${username}` }).then((resp) => {
|
|
1772
2020
|
if (resp) {
|
|
1773
2021
|
resp.image = this.api.url + resp.image + `?token=${this.api.token}`;
|
|
1774
2022
|
this.cache.add(resp);
|
|
1775
2023
|
}
|
|
1776
|
-
this.emit(
|
|
2024
|
+
this.emit(PES`users/${username}:r`, resp);
|
|
1777
2025
|
return resp;
|
|
1778
2026
|
});
|
|
1779
2027
|
}
|
|
1780
2028
|
update(user) {
|
|
1781
2029
|
if (!user.username) throw new Error("Cannot update user, missing username");
|
|
1782
2030
|
return this.api.request({
|
|
1783
|
-
url: `/api/` +
|
|
2031
|
+
url: `/api/` + PES`users/${user.username}`,
|
|
1784
2032
|
method: "PATCH",
|
|
1785
2033
|
body: user
|
|
1786
2034
|
}).then((resp) => {
|
|
@@ -1788,64 +2036,64 @@ class Users extends re {
|
|
|
1788
2036
|
resp.image = this.api.url + resp.image + `?token=${this.api.token}`;
|
|
1789
2037
|
this.cache.add(resp);
|
|
1790
2038
|
}
|
|
1791
|
-
this.emit(
|
|
2039
|
+
this.emit(PES`users/${user.username}:${resp._id ? "u" : "c"}`, resp);
|
|
1792
2040
|
return resp;
|
|
1793
2041
|
});
|
|
1794
2042
|
}
|
|
1795
2043
|
uploadImage(username, file) {
|
|
1796
2044
|
if (!username || !file) throw new Error("Cannot update user image, missing username or file");
|
|
1797
|
-
return
|
|
1798
|
-
url: this.api.url + `/api/` +
|
|
2045
|
+
return uploadWithProgress({
|
|
2046
|
+
url: this.api.url + `/api/` + PES`users/${username}/image`,
|
|
1799
2047
|
files: [file],
|
|
1800
2048
|
headers: this.api.headers
|
|
1801
2049
|
});
|
|
1802
2050
|
}
|
|
1803
2051
|
}
|
|
1804
|
-
class Settings extends
|
|
2052
|
+
class Settings extends PathEventEmitter {
|
|
1805
2053
|
constructor(api) {
|
|
1806
2054
|
super();
|
|
1807
2055
|
__publicField(this, "api");
|
|
1808
2056
|
__publicField(this, "cache");
|
|
1809
2057
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1810
|
-
this.cache = new
|
|
2058
|
+
this.cache = new Cache("key", { storageKey: `momentum:settings:${this.api.host}` });
|
|
1811
2059
|
this.api.on("api/token", () => this.all(false, true));
|
|
1812
2060
|
}
|
|
1813
2061
|
async all(detailed, reload) {
|
|
1814
2062
|
if (!reload && !detailed && this.cache.complete) return this.cache;
|
|
1815
|
-
return this.api.request({ url: `/api/` +
|
|
2063
|
+
return this.api.request({ url: `/api/` + PES`settings` + (detailed ? "?detailed" : "") }).then((resp) => {
|
|
1816
2064
|
this.cache.clear();
|
|
1817
2065
|
if (resp) Object.keys(resp).forEach((key) => this.cache.set(key, detailed ? resp[key].value : resp[key]));
|
|
1818
2066
|
this.cache.complete = true;
|
|
1819
|
-
this.emit(
|
|
2067
|
+
this.emit(PES`settings:r`, resp || []);
|
|
1820
2068
|
return resp;
|
|
1821
2069
|
});
|
|
1822
2070
|
}
|
|
1823
2071
|
delete(key) {
|
|
1824
2072
|
if (!key) throw new Error("Cannot delete setting, missing key");
|
|
1825
|
-
return this.api.request({ url: `/api/` +
|
|
2073
|
+
return this.api.request({ url: `/api/` + PES`settings/${key}`, method: "DELETE" }).then(() => {
|
|
1826
2074
|
this.cache.delete(key);
|
|
1827
|
-
this.emit(
|
|
2075
|
+
this.emit(PES`settings/${key}:d`, key);
|
|
1828
2076
|
});
|
|
1829
2077
|
}
|
|
1830
2078
|
read(key, reload = false) {
|
|
1831
2079
|
if (!key) throw new Error("Cannot read setting, missing key");
|
|
1832
2080
|
if (!reload && this.cache.get(key)) return this.cache.get(key);
|
|
1833
|
-
return this.api.request({ url: `/api/` +
|
|
2081
|
+
return this.api.request({ url: `/api/` + PES`settings/${key}` }).then((variable) => {
|
|
1834
2082
|
if (variable) this.cache.set(variable.key, variable.value);
|
|
1835
|
-
this.emit(
|
|
2083
|
+
this.emit(PES`settings/${key}:r`, variable);
|
|
1836
2084
|
return variable;
|
|
1837
2085
|
});
|
|
1838
2086
|
}
|
|
1839
2087
|
update(variable) {
|
|
1840
2088
|
if (!variable.key) throw new Error("Cannot update setting, missing key");
|
|
1841
|
-
return this.api.request({ url: `/api/` +
|
|
2089
|
+
return this.api.request({ url: `/api/` + PES`settings/${variable.key}`, body: variable }).then((variable2) => {
|
|
1842
2090
|
if (variable2) this.cache.set(variable2.key, variable2.value);
|
|
1843
|
-
this.emit(`/api/` +
|
|
2091
|
+
this.emit(`/api/` + PES`settings/${variable2.key}:${variable2._id ? "u" : "c"}`, variable2);
|
|
1844
2092
|
return variable2;
|
|
1845
2093
|
});
|
|
1846
2094
|
}
|
|
1847
2095
|
}
|
|
1848
|
-
class Static extends
|
|
2096
|
+
class Static extends PathEventEmitter {
|
|
1849
2097
|
constructor(api) {
|
|
1850
2098
|
super();
|
|
1851
2099
|
__publicField(this, "api");
|
|
@@ -1853,27 +2101,27 @@ class Static extends re {
|
|
|
1853
2101
|
}
|
|
1854
2102
|
delete(path) {
|
|
1855
2103
|
if (!path) throw new Error("Cannot delete static asset, missing path");
|
|
1856
|
-
return this.api.request({ url: `/api/` +
|
|
1857
|
-
this.emit(
|
|
2104
|
+
return this.api.request({ url: `/api/` + PES`static/${path}`, method: "DELETE" }).then(() => {
|
|
2105
|
+
this.emit(PES`static/${path}:d`, path);
|
|
1858
2106
|
});
|
|
1859
2107
|
}
|
|
1860
2108
|
upload(files, path = "/") {
|
|
1861
2109
|
if (!files) throw new Error("Cannot upload static assets, missing file");
|
|
1862
|
-
return new
|
|
1863
|
-
return
|
|
1864
|
-
url: this.api.url + "/api/" +
|
|
1865
|
-
files:
|
|
2110
|
+
return new PromiseProgress(async (res, rej, prog) => {
|
|
2111
|
+
return uploadWithProgress({
|
|
2112
|
+
url: this.api.url + "/api/" + PES`static/${path}`,
|
|
2113
|
+
files: makeArray(files),
|
|
1866
2114
|
headers: this.api.headers
|
|
1867
2115
|
}).onProgress((p) => {
|
|
1868
2116
|
prog(p);
|
|
1869
2117
|
}).then((resp) => {
|
|
1870
|
-
this.emit(
|
|
2118
|
+
this.emit(PES`static/${path}:c`, resp);
|
|
1871
2119
|
res(resp);
|
|
1872
2120
|
}).catch((err) => rej(err));
|
|
1873
2121
|
});
|
|
1874
2122
|
}
|
|
1875
2123
|
}
|
|
1876
|
-
class Momentum extends
|
|
2124
|
+
class Momentum extends PathEventEmitter {
|
|
1877
2125
|
constructor(url, opts) {
|
|
1878
2126
|
super();
|
|
1879
2127
|
__publicField(this, "api");
|
|
@@ -1907,8 +2155,8 @@ class Momentum extends re {
|
|
|
1907
2155
|
this.settings = new Settings(this.api);
|
|
1908
2156
|
if (opts == null ? void 0 : opts.socket) this.socket = new Socket(this.api);
|
|
1909
2157
|
this.static = new Static(this.api);
|
|
1910
|
-
this.storage = new Storage(this.api);
|
|
1911
|
-
this.client = new Client(this.settings);
|
|
2158
|
+
this.storage = new Storage$1(this.api);
|
|
2159
|
+
this.client = new Client(this.api, this.settings);
|
|
1912
2160
|
this.users = new Users(this.api);
|
|
1913
2161
|
this.relayEvents(this.actions);
|
|
1914
2162
|
this.relayEvents(this.ai);
|
|
@@ -1931,6 +2179,9 @@ class Momentum extends re {
|
|
|
1931
2179
|
const cached = this.users.cache.get(this.auth.user.username);
|
|
1932
2180
|
if (cached) this.auth.user = cached;
|
|
1933
2181
|
});
|
|
2182
|
+
if (!(opts == null ? void 0 : opts.disableWorker) && "serviceWorker" in navigator) {
|
|
2183
|
+
navigator.serviceWorker.register("/momentum-worker.js");
|
|
2184
|
+
}
|
|
1934
2185
|
}
|
|
1935
2186
|
}
|
|
1936
2187
|
export {
|
|
@@ -1946,15 +2197,15 @@ export {
|
|
|
1946
2197
|
Groups,
|
|
1947
2198
|
Logger,
|
|
1948
2199
|
Momentum,
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
2200
|
+
PE,
|
|
2201
|
+
PES,
|
|
2202
|
+
PathEvent,
|
|
2203
|
+
PathEventEmitter,
|
|
1953
2204
|
Payments,
|
|
1954
2205
|
Pdf,
|
|
1955
2206
|
Settings,
|
|
1956
2207
|
Socket,
|
|
1957
2208
|
Static,
|
|
1958
|
-
Storage,
|
|
2209
|
+
Storage$1 as Storage,
|
|
1959
2210
|
Users
|
|
1960
2211
|
};
|