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