@taybart/corvid 0.0.8 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/dom.d.ts +22 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +324 -80
- package/dist/local_storage.d.ts +2 -1
- package/dist/network.d.ts +31 -1
- package/dist/strings.d.ts +18 -0
- package/dist/utils.d.ts +29 -17
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/dom.d.ts
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
1
|
+
import { logger } from './utils';
|
|
1
2
|
/****************
|
|
2
3
|
* DOM *
|
|
3
4
|
***************/
|
|
4
5
|
/*** window ***/
|
|
5
6
|
/**
|
|
6
|
-
* onDomContentLoaded
|
|
7
|
+
* onDomContentLoaded callback
|
|
7
8
|
*/
|
|
8
9
|
export declare function ready(cb: () => void): void;
|
|
10
|
+
export declare function onKey(key: string, cb: (ev: {
|
|
11
|
+
ctrl: boolean;
|
|
12
|
+
alt: boolean;
|
|
13
|
+
meta: boolean;
|
|
14
|
+
shift: boolean;
|
|
15
|
+
}) => void): void;
|
|
9
16
|
/*** element ***/
|
|
10
17
|
type elOpts = {
|
|
18
|
+
element?: HTMLElement;
|
|
11
19
|
query?: string;
|
|
12
20
|
type?: string;
|
|
13
21
|
content?: any;
|
|
22
|
+
class?: string;
|
|
23
|
+
style?: Object;
|
|
24
|
+
id?: string;
|
|
14
25
|
parent?: HTMLElement | el;
|
|
15
|
-
create?: boolean;
|
|
16
26
|
};
|
|
17
27
|
export declare class el {
|
|
18
28
|
el: HTMLElement | null;
|
|
19
29
|
query: string;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
log: logger;
|
|
31
|
+
constructor(opts: HTMLElement | string | elOpts, verbose?: boolean);
|
|
32
|
+
/*** dom manipulation ***/
|
|
33
|
+
value(update?: string): string | el;
|
|
24
34
|
parent(parent: HTMLElement | el): this;
|
|
25
|
-
appendChild(ch: HTMLElement | el):
|
|
35
|
+
appendChild(ch: HTMLElement | el): this;
|
|
26
36
|
child(ch: HTMLElement | el): this;
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
empty(): this;
|
|
38
|
+
content(content: any, { text }?: {
|
|
29
39
|
text?: boolean;
|
|
30
40
|
}): this;
|
|
31
41
|
src(url: string): this;
|
|
42
|
+
/*** Style ***/
|
|
32
43
|
style(style: Object | string): this;
|
|
33
44
|
addClass(className: string): this;
|
|
34
45
|
removeClass(className: string): this;
|
|
46
|
+
/*** Events ***/
|
|
47
|
+
on(event: string, cb: (ev: Event) => void): this;
|
|
35
48
|
listen(event: string, cb: (ev: Event) => void): this;
|
|
36
49
|
onClick(cb: (ev: Event) => void): this;
|
|
37
50
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -20,10 +20,17 @@ var __webpack_require__ = {};
|
|
|
20
20
|
});
|
|
21
21
|
};
|
|
22
22
|
})();
|
|
23
|
+
var strings_namespaceObject = {};
|
|
24
|
+
__webpack_require__.r(strings_namespaceObject);
|
|
25
|
+
__webpack_require__.d(strings_namespaceObject, {
|
|
26
|
+
bytesToHuman: ()=>bytesToHuman,
|
|
27
|
+
toKebab: ()=>toKebab
|
|
28
|
+
});
|
|
23
29
|
var dom_namespaceObject = {};
|
|
24
30
|
__webpack_require__.r(dom_namespaceObject);
|
|
25
31
|
__webpack_require__.d(dom_namespaceObject, {
|
|
26
|
-
el: ()=>
|
|
32
|
+
el: ()=>dom_el,
|
|
33
|
+
onKey: ()=>onKey,
|
|
27
34
|
ready: ()=>ready
|
|
28
35
|
});
|
|
29
36
|
var style_namespaceObject = {};
|
|
@@ -39,7 +46,9 @@ var network_namespaceObject = {};
|
|
|
39
46
|
__webpack_require__.r(network_namespaceObject);
|
|
40
47
|
__webpack_require__.d(network_namespaceObject, {
|
|
41
48
|
params: ()=>network_params,
|
|
42
|
-
|
|
49
|
+
refresh: ()=>refresh,
|
|
50
|
+
request: ()=>request,
|
|
51
|
+
ws: ()=>ws
|
|
43
52
|
});
|
|
44
53
|
var local_storage_namespaceObject = {};
|
|
45
54
|
__webpack_require__.r(local_storage_namespaceObject);
|
|
@@ -48,6 +57,51 @@ __webpack_require__.d(local_storage_namespaceObject, {
|
|
|
48
57
|
listen: ()=>listen,
|
|
49
58
|
set: ()=>set
|
|
50
59
|
});
|
|
60
|
+
function bytesToHuman(bytes, options = {}) {
|
|
61
|
+
const { useSI = false, decimals = 2, includeUnits = true, targetUnit = null } = options;
|
|
62
|
+
const unit = useSI ? 1000 : 1024;
|
|
63
|
+
const units = useSI ? [
|
|
64
|
+
'B',
|
|
65
|
+
'kB',
|
|
66
|
+
'MB',
|
|
67
|
+
'GB',
|
|
68
|
+
'TB',
|
|
69
|
+
'PB',
|
|
70
|
+
'EB',
|
|
71
|
+
'ZB',
|
|
72
|
+
'YB'
|
|
73
|
+
] : [
|
|
74
|
+
'B',
|
|
75
|
+
'KiB',
|
|
76
|
+
'MiB',
|
|
77
|
+
'GiB',
|
|
78
|
+
'TiB',
|
|
79
|
+
'PiB',
|
|
80
|
+
'EiB',
|
|
81
|
+
'ZiB',
|
|
82
|
+
'YiB'
|
|
83
|
+
];
|
|
84
|
+
if (null === targetUnit) {
|
|
85
|
+
let val = parseInt(bytes, 10);
|
|
86
|
+
if (Math.abs(val) < unit) return `${bytes} B`;
|
|
87
|
+
let u = 0;
|
|
88
|
+
while(Math.abs(val) >= unit && u < units.length - 1){
|
|
89
|
+
val /= unit;
|
|
90
|
+
u++;
|
|
91
|
+
}
|
|
92
|
+
if (includeUnits) return `${val.toFixed(decimals)} ${units[u]}`;
|
|
93
|
+
return `${val.toFixed(decimals)}`;
|
|
94
|
+
}
|
|
95
|
+
const targetUnitIndex = units.indexOf(targetUnit);
|
|
96
|
+
if (-1 === targetUnitIndex) throw new Error(`Invalid unit: ${targetUnit}. Valid units are: ${units.join(', ')}`);
|
|
97
|
+
let val = parseInt(bytes, 10);
|
|
98
|
+
for(let i = 0; i < targetUnitIndex; i++)val /= unit;
|
|
99
|
+
if (includeUnits) return `${val.toFixed(decimals)} ${targetUnit}`;
|
|
100
|
+
return `${val.toFixed(decimals)}`;
|
|
101
|
+
}
|
|
102
|
+
function toKebab(str) {
|
|
103
|
+
return str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, (s, ofs)=>(ofs ? '-' : '') + s.toLowerCase());
|
|
104
|
+
}
|
|
51
105
|
function _define_property(obj, key, value) {
|
|
52
106
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
53
107
|
value: value,
|
|
@@ -58,12 +112,116 @@ function _define_property(obj, key, value) {
|
|
|
58
112
|
else obj[key] = value;
|
|
59
113
|
return obj;
|
|
60
114
|
}
|
|
115
|
+
function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
|
|
116
|
+
return [
|
|
117
|
+
...crypto.getRandomValues(new Uint8Array(len))
|
|
118
|
+
].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
|
|
119
|
+
}
|
|
120
|
+
const clipboard = {
|
|
121
|
+
async copy (text) {
|
|
122
|
+
await navigator.clipboard.writeText(text);
|
|
123
|
+
},
|
|
124
|
+
async copyArbitrary (data) {
|
|
125
|
+
await navigator.clipboard.write(data);
|
|
126
|
+
},
|
|
127
|
+
async read () {
|
|
128
|
+
return await navigator.clipboard.readText();
|
|
129
|
+
},
|
|
130
|
+
async readArbitrary () {
|
|
131
|
+
return await navigator.clipboard.read();
|
|
132
|
+
},
|
|
133
|
+
listen (query, cb) {
|
|
134
|
+
let el;
|
|
135
|
+
el = 'string' == typeof query ? document.querySelector(query) : query;
|
|
136
|
+
if (!el) throw new Error(`no element from query: ${query}`);
|
|
137
|
+
el.addEventListener('copy', (ev)=>{
|
|
138
|
+
const cbEv = ev;
|
|
139
|
+
const selection = document.getSelection();
|
|
140
|
+
if (selection) {
|
|
141
|
+
const text = selection.toString();
|
|
142
|
+
if (text && cbEv.clipboardData) cbEv.clipboardData.setData('text/plain', cb(text));
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var utils_logLevel = /*#__PURE__*/ function(logLevel) {
|
|
148
|
+
logLevel[logLevel["none"] = -1] = "none";
|
|
149
|
+
logLevel[logLevel["error"] = 0] = "error";
|
|
150
|
+
logLevel[logLevel["warn"] = 1] = "warn";
|
|
151
|
+
logLevel[logLevel["info"] = 2] = "info";
|
|
152
|
+
logLevel[logLevel["debug"] = 3] = "debug";
|
|
153
|
+
logLevel[logLevel["trace"] = 4] = "trace";
|
|
154
|
+
return logLevel;
|
|
155
|
+
}({});
|
|
156
|
+
class logger {
|
|
157
|
+
error(...args) {
|
|
158
|
+
if (this.level >= 0) console.error(`[corvid] ${this.prefix}`, ...args);
|
|
159
|
+
}
|
|
160
|
+
warn(...args) {
|
|
161
|
+
if (this.level >= 1) console.warn(`[corvid] ${this.prefix}`, ...args);
|
|
162
|
+
}
|
|
163
|
+
info(...args) {
|
|
164
|
+
if (this.level >= 2) console.info(`[corvid] ${this.prefix}`, ...args);
|
|
165
|
+
}
|
|
166
|
+
debug(...args) {
|
|
167
|
+
if (this.level >= 3) console.debug(`[corvid] ${this.prefix}`, ...args);
|
|
168
|
+
}
|
|
169
|
+
trace(...args) {
|
|
170
|
+
if (this.level >= 4) console.trace(`[corvid] ${this.prefix}`, ...args);
|
|
171
|
+
}
|
|
172
|
+
log(...args) {
|
|
173
|
+
console.log(`[corvid] ${this.prefix}`, ...args);
|
|
174
|
+
}
|
|
175
|
+
constructor(level = 2, prefix){
|
|
176
|
+
_define_property(this, "level", void 0);
|
|
177
|
+
_define_property(this, "prefix", void 0);
|
|
178
|
+
this.level = level;
|
|
179
|
+
this.prefix = prefix ? `(${prefix}):` : ':';
|
|
180
|
+
if (-1 === this.level) [
|
|
181
|
+
'error',
|
|
182
|
+
'warn',
|
|
183
|
+
'info',
|
|
184
|
+
'debug',
|
|
185
|
+
'trace',
|
|
186
|
+
'log'
|
|
187
|
+
].forEach((methodName)=>{
|
|
188
|
+
this[methodName] = ()=>{};
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function dom_define_property(obj, key, value) {
|
|
193
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
194
|
+
value: value,
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
writable: true
|
|
198
|
+
});
|
|
199
|
+
else obj[key] = value;
|
|
200
|
+
return obj;
|
|
201
|
+
}
|
|
61
202
|
function ready(cb) {
|
|
62
203
|
window.addEventListener('DOMContentLoaded', cb);
|
|
63
204
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
205
|
+
function onKey(key, cb) {
|
|
206
|
+
window.addEventListener('keydown', (ev)=>{
|
|
207
|
+
if (ev.key === key) cb({
|
|
208
|
+
ctrl: ev.ctrlKey,
|
|
209
|
+
alt: ev.altKey,
|
|
210
|
+
meta: ev.metaKey,
|
|
211
|
+
shift: ev.shiftKey
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
class dom_el {
|
|
216
|
+
value(update) {
|
|
217
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
218
|
+
if (update) {
|
|
219
|
+
if ('value' in this.el) this.el.value = update;
|
|
220
|
+
if ('src' in this.el) this.el.src = update;
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
if ('value' in this.el) return this.el.value;
|
|
224
|
+
this.log.warn(`element (${this.query}) does not contain value, returning empty string`);
|
|
67
225
|
return '';
|
|
68
226
|
}
|
|
69
227
|
parent(parent) {
|
|
@@ -72,24 +230,26 @@ class el {
|
|
|
72
230
|
return this;
|
|
73
231
|
}
|
|
74
232
|
appendChild(ch) {
|
|
75
|
-
this.child(ch);
|
|
233
|
+
return this.child(ch);
|
|
76
234
|
}
|
|
77
235
|
child(ch) {
|
|
78
236
|
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
79
|
-
if (ch instanceof
|
|
237
|
+
if (ch instanceof dom_el) this.el.appendChild(ch.el);
|
|
80
238
|
else this.el.appendChild(ch);
|
|
81
239
|
return this;
|
|
82
240
|
}
|
|
83
|
-
|
|
241
|
+
empty() {
|
|
242
|
+
if (this.el) this.el.innerHTML = '';
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
content(content, { text = false } = {}) {
|
|
84
246
|
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
85
|
-
if (
|
|
86
|
-
else if (this.el instanceof HTMLInputElement && !force) this.el.value = content;
|
|
87
|
-
else if (text) this.el.textContent = content;
|
|
247
|
+
if (text) this.el.textContent = content;
|
|
88
248
|
else this.el.innerHTML = content;
|
|
89
249
|
return this;
|
|
90
250
|
}
|
|
91
251
|
src(url) {
|
|
92
|
-
if (this.el
|
|
252
|
+
if (this.el && 'src' in this.el) this.el.src = url;
|
|
93
253
|
return this;
|
|
94
254
|
}
|
|
95
255
|
style(style) {
|
|
@@ -97,7 +257,7 @@ class el {
|
|
|
97
257
|
if ('string' == typeof style) this.el.style = style;
|
|
98
258
|
else if ('object' == typeof style) {
|
|
99
259
|
let s = '';
|
|
100
|
-
Object.entries(style).forEach(([k, v])=>s += `${k}:${v};`);
|
|
260
|
+
Object.entries(style).forEach(([k, v])=>s += `${toKebab(k)}:${v};`);
|
|
101
261
|
this.el.style = s;
|
|
102
262
|
}
|
|
103
263
|
}
|
|
@@ -113,32 +273,56 @@ class el {
|
|
|
113
273
|
this.el.classList.remove(className);
|
|
114
274
|
return this;
|
|
115
275
|
}
|
|
116
|
-
|
|
276
|
+
on(event, cb) {
|
|
117
277
|
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
118
278
|
this.el.addEventListener(event, cb);
|
|
119
279
|
return this;
|
|
120
280
|
}
|
|
281
|
+
listen(event, cb) {
|
|
282
|
+
return this.on(event, cb);
|
|
283
|
+
}
|
|
121
284
|
onClick(cb) {
|
|
122
|
-
return this.
|
|
285
|
+
return this.on('click', cb);
|
|
123
286
|
}
|
|
124
|
-
constructor(opts){
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this
|
|
287
|
+
constructor(opts, verbose = false){
|
|
288
|
+
dom_define_property(this, "el", void 0);
|
|
289
|
+
dom_define_property(this, "query", '');
|
|
290
|
+
dom_define_property(this, "log", void 0);
|
|
291
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'element');
|
|
128
292
|
if ('string' == typeof opts) {
|
|
129
293
|
this.query = opts;
|
|
130
294
|
this.el = document.querySelector(opts);
|
|
131
295
|
return;
|
|
132
296
|
}
|
|
133
|
-
const { query, type, content, parent
|
|
297
|
+
const { query, element, type, class: styleClass, style, id, content, parent } = opts;
|
|
134
298
|
if (query) {
|
|
299
|
+
this.log.debug(`using query: ${query}`);
|
|
135
300
|
this.query = query;
|
|
136
301
|
this.el = document.querySelector(query);
|
|
137
|
-
if (!this.el
|
|
138
|
-
} else if (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
302
|
+
if (!this.el) throw new Error(`no element from query: ${query}`);
|
|
303
|
+
} else if (element) {
|
|
304
|
+
this.log.debug(`using existing element: ${element}`);
|
|
305
|
+
this.el = element;
|
|
306
|
+
} else if (type) {
|
|
307
|
+
this.log.debug(`creating element: ${type}`);
|
|
308
|
+
this.el = document.createElement(type);
|
|
309
|
+
} else throw new Error('no query or type provided');
|
|
310
|
+
if (this.el) {
|
|
311
|
+
if (id) {
|
|
312
|
+
this.log.debug(`setting id: ${id}`);
|
|
313
|
+
this.el.id = id;
|
|
314
|
+
}
|
|
315
|
+
if (styleClass) this.el.classList.add(styleClass);
|
|
316
|
+
if (style) this.style(style);
|
|
317
|
+
if (content) {
|
|
318
|
+
this.log.debug(`setting content: ${content}`);
|
|
319
|
+
this.el.innerHTML = content;
|
|
320
|
+
}
|
|
321
|
+
if (parent) {
|
|
322
|
+
this.log.debug("adding to parent");
|
|
323
|
+
parent.appendChild(this.el);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
142
326
|
}
|
|
143
327
|
}
|
|
144
328
|
function cssVar(name) {
|
|
@@ -195,11 +379,15 @@ class network_params {
|
|
|
195
379
|
}
|
|
196
380
|
class request {
|
|
197
381
|
auth(token) {
|
|
198
|
-
|
|
382
|
+
const header = `Bearer ${token}`;
|
|
383
|
+
this.log.debug(`adding auth token header ${header}`);
|
|
384
|
+
this.opts.headers.Authorization = header;
|
|
199
385
|
return this;
|
|
200
386
|
}
|
|
201
387
|
basicAuth(username, password) {
|
|
202
|
-
|
|
388
|
+
const header = `Basic ${btoa(`${username}:${password}`)}`;
|
|
389
|
+
this.log.debug(`adding basic auth header ${header}`);
|
|
390
|
+
this.opts.headers.Authorization = header;
|
|
203
391
|
return this;
|
|
204
392
|
}
|
|
205
393
|
body(body) {
|
|
@@ -214,8 +402,10 @@ class request {
|
|
|
214
402
|
if (path) url = `${this.opts.url}${path}`;
|
|
215
403
|
const params = override.params || this.opts.params;
|
|
216
404
|
if (params) url = `${url}?${params.toString()}`;
|
|
405
|
+
this.log.debug(`${this.opts.method} ${url}`);
|
|
217
406
|
const res = await fetch(url, {
|
|
218
407
|
method: this.opts.method,
|
|
408
|
+
credentials: this.opts.credentials,
|
|
219
409
|
headers: {
|
|
220
410
|
accept: 'application/json',
|
|
221
411
|
'content-type': 'application/json',
|
|
@@ -230,14 +420,109 @@ class request {
|
|
|
230
420
|
}
|
|
231
421
|
return await res.json();
|
|
232
422
|
}
|
|
233
|
-
constructor(opts = {}){
|
|
423
|
+
constructor(opts = {}, verbose = false){
|
|
234
424
|
network_define_property(this, "opts", void 0);
|
|
425
|
+
network_define_property(this, "log", void 0);
|
|
426
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'request');
|
|
235
427
|
if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
|
|
236
428
|
if (!opts.url) throw new Error('must provide url');
|
|
237
429
|
this.opts = opts;
|
|
238
430
|
if (!this.opts.success) this.opts.success = 200;
|
|
239
431
|
if (!this.opts.method) this.opts.method = 'GET';
|
|
240
432
|
if (!this.opts.headers) this.opts.headers = {};
|
|
433
|
+
if (!this.opts.credentials) this.opts.credentials = 'omit';
|
|
434
|
+
this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
class ws {
|
|
438
|
+
setup() {
|
|
439
|
+
this.recursion_level += 1;
|
|
440
|
+
this.ws = new WebSocket(this.url);
|
|
441
|
+
this.backoff = 100;
|
|
442
|
+
this.ws.addEventListener('open', ()=>{
|
|
443
|
+
const rl = this.recursion_level;
|
|
444
|
+
this.log.debug(`on open: reconnected (${rl})`);
|
|
445
|
+
this.is_connected = true;
|
|
446
|
+
for(let key in this.event_listeners)this.event_listeners[key].forEach((cb)=>{
|
|
447
|
+
this.log.debug(`adding listener (${rl}): ${key}`);
|
|
448
|
+
this.ws.addEventListener(key, cb);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
this.ws.addEventListener('close', ()=>{
|
|
452
|
+
this.log.debug('connection closed');
|
|
453
|
+
this.is_connected = false;
|
|
454
|
+
this.backoff = Math.min(2 * this.backoff, this.max_timeout);
|
|
455
|
+
this.log.debug('backoff: ' + this.backoff);
|
|
456
|
+
setTimeout(()=>{
|
|
457
|
+
if (this.should_reconnect) {
|
|
458
|
+
this.ws = null;
|
|
459
|
+
this.setup();
|
|
460
|
+
}
|
|
461
|
+
}, this.backoff + 50 * Math.random());
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
send(data) {
|
|
465
|
+
if (!this.is_connected || !this.ws) throw new Error('not connected');
|
|
466
|
+
this.ws.send(data);
|
|
467
|
+
}
|
|
468
|
+
onMessage(cb) {
|
|
469
|
+
if (!this.ws) throw new Error('ws is null');
|
|
470
|
+
if (!this.event_listeners.message) this.event_listeners.message = [];
|
|
471
|
+
this.event_listeners.message.push((e)=>{
|
|
472
|
+
const rl = this.recursion_level;
|
|
473
|
+
this.log.debug(`message(${rl}): ${e.data}`);
|
|
474
|
+
cb(e.data);
|
|
475
|
+
});
|
|
476
|
+
this.ws.addEventListener('message', (e)=>{
|
|
477
|
+
const rl = this.recursion_level;
|
|
478
|
+
this.log.debug(`message(${rl}): ${e.data}`);
|
|
479
|
+
cb(e.data);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
on(event, cb) {
|
|
483
|
+
if (!this.ws) throw new Error('ws is null');
|
|
484
|
+
if (!this.event_listeners[event]) this.event_listeners[event] = [];
|
|
485
|
+
this.event_listeners[event].push(cb);
|
|
486
|
+
this.ws.addEventListener(event, cb);
|
|
487
|
+
}
|
|
488
|
+
close() {
|
|
489
|
+
if (!this.is_connected || !this.ws) return;
|
|
490
|
+
this.should_reconnect = false;
|
|
491
|
+
this.ws.close();
|
|
492
|
+
}
|
|
493
|
+
constructor(url, verbose = false){
|
|
494
|
+
network_define_property(this, "url", void 0);
|
|
495
|
+
network_define_property(this, "ws", void 0);
|
|
496
|
+
network_define_property(this, "backoff", 50);
|
|
497
|
+
network_define_property(this, "max_timeout", 10000);
|
|
498
|
+
network_define_property(this, "should_reconnect", true);
|
|
499
|
+
network_define_property(this, "is_connected", false);
|
|
500
|
+
network_define_property(this, "recursion_level", 0);
|
|
501
|
+
network_define_property(this, "log", void 0);
|
|
502
|
+
network_define_property(this, "event_listeners", {});
|
|
503
|
+
this.url = url;
|
|
504
|
+
this.ws = null;
|
|
505
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'websocket');
|
|
506
|
+
this.setup();
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
class refresh {
|
|
510
|
+
listen() {
|
|
511
|
+
const socket = new ws(this.url);
|
|
512
|
+
if (socket) {
|
|
513
|
+
socket.on('open', ()=>{
|
|
514
|
+
if (this.should_reload) location.reload();
|
|
515
|
+
});
|
|
516
|
+
socket.on('close', ()=>{
|
|
517
|
+
this.should_reload = true;
|
|
518
|
+
setTimeout(this.listen, 500);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
constructor(url){
|
|
523
|
+
network_define_property(this, "url", void 0);
|
|
524
|
+
network_define_property(this, "should_reload", false);
|
|
525
|
+
this.url = url;
|
|
241
526
|
}
|
|
242
527
|
}
|
|
243
528
|
function get(key) {
|
|
@@ -258,57 +543,16 @@ function set(key, value, broadcast = false) {
|
|
|
258
543
|
}
|
|
259
544
|
function listen(key, cb) {
|
|
260
545
|
document.addEventListener('@corvid/ls-update', (ev)=>{
|
|
261
|
-
if (ev.detail.key === key || '*' === key)
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const units = useSI ? [
|
|
271
|
-
'B',
|
|
272
|
-
'kB',
|
|
273
|
-
'MB',
|
|
274
|
-
'GB',
|
|
275
|
-
'TB',
|
|
276
|
-
'PB',
|
|
277
|
-
'EB',
|
|
278
|
-
'ZB',
|
|
279
|
-
'YB'
|
|
280
|
-
] : [
|
|
281
|
-
'B',
|
|
282
|
-
'KiB',
|
|
283
|
-
'MiB',
|
|
284
|
-
'GiB',
|
|
285
|
-
'TiB',
|
|
286
|
-
'PiB',
|
|
287
|
-
'EiB',
|
|
288
|
-
'ZiB',
|
|
289
|
-
'YiB'
|
|
290
|
-
];
|
|
291
|
-
if (null === targetUnit) {
|
|
292
|
-
let val = parseInt(bytes, 10);
|
|
293
|
-
if (Math.abs(val) < unit) return `${bytes} B`;
|
|
294
|
-
let u = 0;
|
|
295
|
-
while(Math.abs(val) >= unit && u < units.length - 1){
|
|
296
|
-
val /= unit;
|
|
297
|
-
u++;
|
|
546
|
+
if (ev.detail.key === key || '*' === key) {
|
|
547
|
+
if (cb instanceof dom_el) {
|
|
548
|
+
if (ev.detail.key === key) cb.content(ev.detail.value);
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
cb({
|
|
552
|
+
key: ev.detail.key,
|
|
553
|
+
value: ev.detail.value
|
|
554
|
+
});
|
|
298
555
|
}
|
|
299
|
-
|
|
300
|
-
return `${val.toFixed(decimals)}`;
|
|
301
|
-
}
|
|
302
|
-
const targetUnitIndex = units.indexOf(targetUnit);
|
|
303
|
-
if (-1 === targetUnitIndex) throw new Error(`Invalid unit: ${targetUnit}. Valid units are: ${units.join(', ')}`);
|
|
304
|
-
let val = parseInt(bytes, 10);
|
|
305
|
-
for(let i = 0; i < targetUnitIndex; i++)val /= unit;
|
|
306
|
-
if (includeUnits) return `${val.toFixed(decimals)} ${targetUnit}`;
|
|
307
|
-
return `${val.toFixed(decimals)}`;
|
|
308
|
-
}
|
|
309
|
-
function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
|
|
310
|
-
return [
|
|
311
|
-
...crypto.getRandomValues(new Uint8Array(len))
|
|
312
|
-
].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
|
|
556
|
+
});
|
|
313
557
|
}
|
|
314
|
-
export {
|
|
558
|
+
export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, strings_namespaceObject as strings, style_namespaceObject as style };
|
package/dist/local_storage.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { el } from './dom';
|
|
1
2
|
export declare function get(key: string): any;
|
|
2
3
|
export declare function set(key: string, value: any, broadcast?: boolean): void;
|
|
3
4
|
export declare function listen(key: string, cb: (update: {
|
|
4
5
|
key: string;
|
|
5
6
|
value: any;
|
|
6
|
-
}) => void): void;
|
|
7
|
+
}) => void | el): void;
|
package/dist/network.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { logger } from './utils';
|
|
1
2
|
/*** url params ***/
|
|
2
3
|
export declare class params {
|
|
3
4
|
params: URLSearchParams;
|
|
@@ -6,6 +7,7 @@ export declare class params {
|
|
|
6
7
|
toString(): string;
|
|
7
8
|
static render(p: Object): string;
|
|
8
9
|
}
|
|
10
|
+
/*** http request ***/
|
|
9
11
|
export type requestOpts = {
|
|
10
12
|
url?: string;
|
|
11
13
|
type?: 'json';
|
|
@@ -15,10 +17,12 @@ export type requestOpts = {
|
|
|
15
17
|
auth?: string;
|
|
16
18
|
body?: Object;
|
|
17
19
|
success?: number;
|
|
20
|
+
credentials?: RequestCredentials;
|
|
18
21
|
};
|
|
19
22
|
export declare class request {
|
|
20
23
|
opts: requestOpts;
|
|
21
|
-
|
|
24
|
+
log: logger;
|
|
25
|
+
constructor(opts?: requestOpts, verbose?: boolean);
|
|
22
26
|
auth(token: string): this;
|
|
23
27
|
basicAuth(username: string, password: string): this;
|
|
24
28
|
body(body: Object): this;
|
|
@@ -31,3 +35,29 @@ export declare class request {
|
|
|
31
35
|
};
|
|
32
36
|
}): Promise<any>;
|
|
33
37
|
}
|
|
38
|
+
/*** websocket ***/
|
|
39
|
+
export declare class ws {
|
|
40
|
+
url: string;
|
|
41
|
+
ws: WebSocket | null;
|
|
42
|
+
backoff: number;
|
|
43
|
+
max_timeout: number;
|
|
44
|
+
should_reconnect: boolean;
|
|
45
|
+
is_connected: boolean;
|
|
46
|
+
recursion_level: number;
|
|
47
|
+
log: logger;
|
|
48
|
+
event_listeners: Record<string, Array<(data: any) => void>>;
|
|
49
|
+
constructor(url: string, verbose?: boolean);
|
|
50
|
+
setup(): void;
|
|
51
|
+
send(data: any): void;
|
|
52
|
+
onMessage(cb: (data: any) => void): void;
|
|
53
|
+
on(event: string, cb: (data: any) => void): void;
|
|
54
|
+
close(): void;
|
|
55
|
+
}
|
|
56
|
+
/*** refresh ***/
|
|
57
|
+
export declare class refresh {
|
|
58
|
+
url: string;
|
|
59
|
+
should_reload: boolean;
|
|
60
|
+
constructor(url: string);
|
|
61
|
+
listen(): void;
|
|
62
|
+
}
|
|
63
|
+
/*** server sent events ***/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts bytes to a human-readable string representation
|
|
3
|
+
*
|
|
4
|
+
* @param bytes - The number of bytes to convert
|
|
5
|
+
* @param options - Optional configuration
|
|
6
|
+
* @param options.useSI - If true, use SI units (KB, MB, GB) with powers of 1000
|
|
7
|
+
* If false, use binary units (KiB, MiB, GiB) with powers of 1024
|
|
8
|
+
* @param options.decimals - Number of decimal places to include (default: 2)
|
|
9
|
+
* @return Human-readable representation (e.g., "4.2 MB" or "3.7 GiB")
|
|
10
|
+
*/
|
|
11
|
+
export type bytesOptions = {
|
|
12
|
+
useSI?: boolean;
|
|
13
|
+
decimals?: number;
|
|
14
|
+
includeUnits?: boolean;
|
|
15
|
+
targetUnit?: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function bytesToHuman(bytes: string, options?: bytesOptions): string;
|
|
18
|
+
export declare function toKebab(str: string): string;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,23 +1,6 @@
|
|
|
1
1
|
/***************
|
|
2
2
|
* Utils *
|
|
3
3
|
**************/
|
|
4
|
-
/**
|
|
5
|
-
* Converts bytes to a human-readable string representation
|
|
6
|
-
*
|
|
7
|
-
* @param bytes - The number of bytes to convert
|
|
8
|
-
* @param options - Optional configuration
|
|
9
|
-
* @param options.useSI - If true, use SI units (KB, MB, GB) with powers of 1000
|
|
10
|
-
* If false, use binary units (KiB, MiB, GiB) with powers of 1024
|
|
11
|
-
* @param options.decimals - Number of decimal places to include (default: 2)
|
|
12
|
-
* @return Human-readable representation (e.g., "4.2 MB" or "3.7 GiB")
|
|
13
|
-
*/
|
|
14
|
-
export type bytesOptions = {
|
|
15
|
-
useSI?: boolean;
|
|
16
|
-
decimals?: number;
|
|
17
|
-
includeUnits?: boolean;
|
|
18
|
-
targetUnit?: string;
|
|
19
|
-
};
|
|
20
|
-
export declare function bytesToHuman(bytes: string, options?: bytesOptions): string;
|
|
21
4
|
/**
|
|
22
5
|
* Generates a random id of length len using provided alphabet
|
|
23
6
|
* @param len - The length of the string to generate
|
|
@@ -25,3 +8,32 @@ export declare function bytesToHuman(bytes: string, options?: bytesOptions): str
|
|
|
25
8
|
* @return A new random id
|
|
26
9
|
*/
|
|
27
10
|
export declare function genID(len?: number, alphabet?: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Clipboard helper because i can't remember the funciton names
|
|
13
|
+
*/
|
|
14
|
+
export declare const clipboard: {
|
|
15
|
+
copy(text: string): Promise<void>;
|
|
16
|
+
copyArbitrary(data: any): Promise<void>;
|
|
17
|
+
read(): Promise<string>;
|
|
18
|
+
readArbitrary(): Promise<any>;
|
|
19
|
+
listen(query: string | HTMLElement, cb: (contents: string) => string): void;
|
|
20
|
+
};
|
|
21
|
+
export declare enum logLevel {
|
|
22
|
+
none = -1,
|
|
23
|
+
error = 0,
|
|
24
|
+
warn = 1,
|
|
25
|
+
info = 2,
|
|
26
|
+
debug = 3,
|
|
27
|
+
trace = 4
|
|
28
|
+
}
|
|
29
|
+
export declare class logger {
|
|
30
|
+
level: logLevel;
|
|
31
|
+
prefix: string;
|
|
32
|
+
constructor(level?: logLevel, prefix?: string);
|
|
33
|
+
error(...args: any[]): void;
|
|
34
|
+
warn(...args: any[]): void;
|
|
35
|
+
info(...args: any[]): void;
|
|
36
|
+
debug(...args: any[]): void;
|
|
37
|
+
trace(...args: any[]): void;
|
|
38
|
+
log(...args: any[]): void;
|
|
39
|
+
}
|