@taybart/corvid 0.0.8 → 0.1.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/README.md +5 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/dom.d.ts +22 -9
- package/dist/index.js +279 -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/crypto.d.ts
ADDED
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.js
CHANGED
|
@@ -23,7 +23,8 @@ var __webpack_require__ = {};
|
|
|
23
23
|
var dom_namespaceObject = {};
|
|
24
24
|
__webpack_require__.r(dom_namespaceObject);
|
|
25
25
|
__webpack_require__.d(dom_namespaceObject, {
|
|
26
|
-
el: ()=>
|
|
26
|
+
el: ()=>dom_el,
|
|
27
|
+
onKey: ()=>onKey,
|
|
27
28
|
ready: ()=>ready
|
|
28
29
|
});
|
|
29
30
|
var style_namespaceObject = {};
|
|
@@ -39,7 +40,9 @@ var network_namespaceObject = {};
|
|
|
39
40
|
__webpack_require__.r(network_namespaceObject);
|
|
40
41
|
__webpack_require__.d(network_namespaceObject, {
|
|
41
42
|
params: ()=>network_params,
|
|
42
|
-
|
|
43
|
+
refresh: ()=>refresh,
|
|
44
|
+
request: ()=>request,
|
|
45
|
+
ws: ()=>ws
|
|
43
46
|
});
|
|
44
47
|
var local_storage_namespaceObject = {};
|
|
45
48
|
__webpack_require__.r(local_storage_namespaceObject);
|
|
@@ -48,6 +51,9 @@ __webpack_require__.d(local_storage_namespaceObject, {
|
|
|
48
51
|
listen: ()=>listen,
|
|
49
52
|
set: ()=>set
|
|
50
53
|
});
|
|
54
|
+
function toKebab(str) {
|
|
55
|
+
return str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, (s, ofs)=>(ofs ? '-' : '') + s.toLowerCase());
|
|
56
|
+
}
|
|
51
57
|
function _define_property(obj, key, value) {
|
|
52
58
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
53
59
|
value: value,
|
|
@@ -58,12 +64,119 @@ function _define_property(obj, key, value) {
|
|
|
58
64
|
else obj[key] = value;
|
|
59
65
|
return obj;
|
|
60
66
|
}
|
|
67
|
+
function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
|
|
68
|
+
return [
|
|
69
|
+
...crypto.getRandomValues(new Uint8Array(len))
|
|
70
|
+
].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
|
|
71
|
+
}
|
|
72
|
+
const clipboard = {
|
|
73
|
+
async copy (text) {
|
|
74
|
+
await navigator.clipboard.writeText(text);
|
|
75
|
+
},
|
|
76
|
+
async copyArbitrary (data) {
|
|
77
|
+
await navigator.clipboard.write(data);
|
|
78
|
+
},
|
|
79
|
+
async read () {
|
|
80
|
+
return await navigator.clipboard.readText();
|
|
81
|
+
},
|
|
82
|
+
async readArbitrary () {
|
|
83
|
+
return await navigator.clipboard.read();
|
|
84
|
+
},
|
|
85
|
+
listen (query, cb) {
|
|
86
|
+
let el;
|
|
87
|
+
el = 'string' == typeof query ? document.querySelector(query) : query;
|
|
88
|
+
if (!el) throw new Error(`no element from query: ${query}`);
|
|
89
|
+
el.addEventListener('copy', (ev)=>{
|
|
90
|
+
const cbEv = ev;
|
|
91
|
+
const selection = document.getSelection();
|
|
92
|
+
if (selection) {
|
|
93
|
+
const text = selection.toString();
|
|
94
|
+
if (text && cbEv.clipboardData) cbEv.clipboardData.setData('text/plain', cb(text));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
var utils_logLevel = /*#__PURE__*/ function(logLevel) {
|
|
100
|
+
logLevel[logLevel["none"] = -1] = "none";
|
|
101
|
+
logLevel[logLevel["error"] = 0] = "error";
|
|
102
|
+
logLevel[logLevel["warn"] = 1] = "warn";
|
|
103
|
+
logLevel[logLevel["info"] = 2] = "info";
|
|
104
|
+
logLevel[logLevel["debug"] = 3] = "debug";
|
|
105
|
+
logLevel[logLevel["trace"] = 4] = "trace";
|
|
106
|
+
return logLevel;
|
|
107
|
+
}({});
|
|
108
|
+
class logger {
|
|
109
|
+
error(...args) {
|
|
110
|
+
if (this.level >= 0) console.error(`[corvid] ${this.prefix}`, ...args);
|
|
111
|
+
}
|
|
112
|
+
warn(...args) {
|
|
113
|
+
if (this.level >= 1) console.warn(`[corvid] ${this.prefix}`, ...args);
|
|
114
|
+
}
|
|
115
|
+
info(...args) {
|
|
116
|
+
if (this.level >= 2) console.info(`[corvid] ${this.prefix}`, ...args);
|
|
117
|
+
}
|
|
118
|
+
debug(...args) {
|
|
119
|
+
if (this.level >= 3) console.debug(`[corvid] ${this.prefix}`, ...args);
|
|
120
|
+
}
|
|
121
|
+
trace(...args) {
|
|
122
|
+
if (this.level >= 4) console.trace(`[corvid] ${this.prefix}`, ...args);
|
|
123
|
+
}
|
|
124
|
+
log(...args) {
|
|
125
|
+
console.log(`[corvid] ${this.prefix}`, ...args);
|
|
126
|
+
}
|
|
127
|
+
constructor(level = 2, prefix){
|
|
128
|
+
_define_property(this, "level", void 0);
|
|
129
|
+
_define_property(this, "prefix", void 0);
|
|
130
|
+
this.level = level;
|
|
131
|
+
this.prefix = prefix ? `(${prefix}):` : ':';
|
|
132
|
+
if (-1 === this.level) {
|
|
133
|
+
const methodsToMock = [
|
|
134
|
+
'error',
|
|
135
|
+
'warn',
|
|
136
|
+
'info',
|
|
137
|
+
'debug',
|
|
138
|
+
'trace',
|
|
139
|
+
'log'
|
|
140
|
+
];
|
|
141
|
+
methodsToMock.forEach((methodName)=>{
|
|
142
|
+
this[methodName] = ()=>{};
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function dom_define_property(obj, key, value) {
|
|
148
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
149
|
+
value: value,
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true,
|
|
152
|
+
writable: true
|
|
153
|
+
});
|
|
154
|
+
else obj[key] = value;
|
|
155
|
+
return obj;
|
|
156
|
+
}
|
|
61
157
|
function ready(cb) {
|
|
62
158
|
window.addEventListener('DOMContentLoaded', cb);
|
|
63
159
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
160
|
+
function onKey(key, cb) {
|
|
161
|
+
window.addEventListener('keydown', (ev)=>{
|
|
162
|
+
if (ev.key === key) cb({
|
|
163
|
+
ctrl: ev.ctrlKey,
|
|
164
|
+
alt: ev.altKey,
|
|
165
|
+
meta: ev.metaKey,
|
|
166
|
+
shift: ev.shiftKey
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
class dom_el {
|
|
171
|
+
value(update) {
|
|
172
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
173
|
+
if (update) {
|
|
174
|
+
if ('value' in this.el) this.el.value = update;
|
|
175
|
+
if ('src' in this.el) this.el.src = update;
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
if ('value' in this.el) return this.el.value;
|
|
179
|
+
this.log.warn(`element (${this.query}) does not contain value, returning empty string`);
|
|
67
180
|
return '';
|
|
68
181
|
}
|
|
69
182
|
parent(parent) {
|
|
@@ -72,24 +185,26 @@ class el {
|
|
|
72
185
|
return this;
|
|
73
186
|
}
|
|
74
187
|
appendChild(ch) {
|
|
75
|
-
this.child(ch);
|
|
188
|
+
return this.child(ch);
|
|
76
189
|
}
|
|
77
190
|
child(ch) {
|
|
78
191
|
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
79
|
-
if (ch instanceof
|
|
192
|
+
if (ch instanceof dom_el) this.el.appendChild(ch.el);
|
|
80
193
|
else this.el.appendChild(ch);
|
|
81
194
|
return this;
|
|
82
195
|
}
|
|
83
|
-
|
|
196
|
+
empty() {
|
|
197
|
+
if (this.el) this.el.innerHTML = '';
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
content(content, { text = false } = {}) {
|
|
84
201
|
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;
|
|
202
|
+
if (text) this.el.textContent = content;
|
|
88
203
|
else this.el.innerHTML = content;
|
|
89
204
|
return this;
|
|
90
205
|
}
|
|
91
206
|
src(url) {
|
|
92
|
-
if (this.el
|
|
207
|
+
if (this.el && 'src' in this.el) this.el.src = url;
|
|
93
208
|
return this;
|
|
94
209
|
}
|
|
95
210
|
style(style) {
|
|
@@ -97,7 +212,7 @@ class el {
|
|
|
97
212
|
if ('string' == typeof style) this.el.style = style;
|
|
98
213
|
else if ('object' == typeof style) {
|
|
99
214
|
let s = '';
|
|
100
|
-
Object.entries(style).forEach(([k, v])=>s += `${k}:${v};`);
|
|
215
|
+
Object.entries(style).forEach(([k, v])=>s += `${toKebab(k)}:${v};`);
|
|
101
216
|
this.el.style = s;
|
|
102
217
|
}
|
|
103
218
|
}
|
|
@@ -113,32 +228,56 @@ class el {
|
|
|
113
228
|
this.el.classList.remove(className);
|
|
114
229
|
return this;
|
|
115
230
|
}
|
|
116
|
-
|
|
231
|
+
on(event, cb) {
|
|
117
232
|
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
118
233
|
this.el.addEventListener(event, cb);
|
|
119
234
|
return this;
|
|
120
235
|
}
|
|
236
|
+
listen(event, cb) {
|
|
237
|
+
return this.on(event, cb);
|
|
238
|
+
}
|
|
121
239
|
onClick(cb) {
|
|
122
|
-
return this.
|
|
240
|
+
return this.on('click', cb);
|
|
123
241
|
}
|
|
124
|
-
constructor(opts){
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this
|
|
242
|
+
constructor(opts, verbose = false){
|
|
243
|
+
dom_define_property(this, "el", void 0);
|
|
244
|
+
dom_define_property(this, "query", '');
|
|
245
|
+
dom_define_property(this, "log", void 0);
|
|
246
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'element');
|
|
128
247
|
if ('string' == typeof opts) {
|
|
129
248
|
this.query = opts;
|
|
130
249
|
this.el = document.querySelector(opts);
|
|
131
250
|
return;
|
|
132
251
|
}
|
|
133
|
-
const { query, type, content, parent
|
|
252
|
+
const { query, element, type, class: styleClass, style, id, content, parent } = opts;
|
|
134
253
|
if (query) {
|
|
254
|
+
this.log.debug(`using query: ${query}`);
|
|
135
255
|
this.query = query;
|
|
136
256
|
this.el = document.querySelector(query);
|
|
137
|
-
if (!this.el
|
|
138
|
-
} else if (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
257
|
+
if (!this.el) throw new Error(`no element from query: ${query}`);
|
|
258
|
+
} else if (element) {
|
|
259
|
+
this.log.debug(`using existing element: ${element}`);
|
|
260
|
+
this.el = element;
|
|
261
|
+
} else if (type) {
|
|
262
|
+
this.log.debug(`creating element: ${type}`);
|
|
263
|
+
this.el = document.createElement(type);
|
|
264
|
+
} else throw new Error('no query or type provided');
|
|
265
|
+
if (this.el) {
|
|
266
|
+
if (id) {
|
|
267
|
+
this.log.debug(`setting id: ${id}`);
|
|
268
|
+
this.el.id = id;
|
|
269
|
+
}
|
|
270
|
+
if (styleClass) this.el.classList.add(styleClass);
|
|
271
|
+
if (style) this.style(style);
|
|
272
|
+
if (content) {
|
|
273
|
+
this.log.debug(`setting content: ${content}`);
|
|
274
|
+
this.el.innerHTML = content;
|
|
275
|
+
}
|
|
276
|
+
if (parent) {
|
|
277
|
+
this.log.debug("adding to parent");
|
|
278
|
+
parent.appendChild(this.el);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
142
281
|
}
|
|
143
282
|
}
|
|
144
283
|
function cssVar(name) {
|
|
@@ -195,11 +334,15 @@ class network_params {
|
|
|
195
334
|
}
|
|
196
335
|
class request {
|
|
197
336
|
auth(token) {
|
|
198
|
-
|
|
337
|
+
const header = `Bearer ${token}`;
|
|
338
|
+
this.log.debug(`adding auth token header ${header}`);
|
|
339
|
+
this.opts.headers.Authorization = header;
|
|
199
340
|
return this;
|
|
200
341
|
}
|
|
201
342
|
basicAuth(username, password) {
|
|
202
|
-
|
|
343
|
+
const header = `Basic ${btoa(`${username}:${password}`)}`;
|
|
344
|
+
this.log.debug(`adding basic auth header ${header}`);
|
|
345
|
+
this.opts.headers.Authorization = header;
|
|
203
346
|
return this;
|
|
204
347
|
}
|
|
205
348
|
body(body) {
|
|
@@ -214,8 +357,10 @@ class request {
|
|
|
214
357
|
if (path) url = `${this.opts.url}${path}`;
|
|
215
358
|
const params = override.params || this.opts.params;
|
|
216
359
|
if (params) url = `${url}?${params.toString()}`;
|
|
360
|
+
this.log.debug(`${this.opts.method} ${url}`);
|
|
217
361
|
const res = await fetch(url, {
|
|
218
362
|
method: this.opts.method,
|
|
363
|
+
credentials: this.opts.credentials,
|
|
219
364
|
headers: {
|
|
220
365
|
accept: 'application/json',
|
|
221
366
|
'content-type': 'application/json',
|
|
@@ -230,14 +375,109 @@ class request {
|
|
|
230
375
|
}
|
|
231
376
|
return await res.json();
|
|
232
377
|
}
|
|
233
|
-
constructor(opts = {}){
|
|
378
|
+
constructor(opts = {}, verbose = false){
|
|
234
379
|
network_define_property(this, "opts", void 0);
|
|
380
|
+
network_define_property(this, "log", void 0);
|
|
381
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'request');
|
|
235
382
|
if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
|
|
236
383
|
if (!opts.url) throw new Error('must provide url');
|
|
237
384
|
this.opts = opts;
|
|
238
385
|
if (!this.opts.success) this.opts.success = 200;
|
|
239
386
|
if (!this.opts.method) this.opts.method = 'GET';
|
|
240
387
|
if (!this.opts.headers) this.opts.headers = {};
|
|
388
|
+
if (!this.opts.credentials) this.opts.credentials = 'omit';
|
|
389
|
+
this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
class ws {
|
|
393
|
+
setup() {
|
|
394
|
+
this.recursion_level += 1;
|
|
395
|
+
this.ws = new WebSocket(this.url);
|
|
396
|
+
this.backoff = 100;
|
|
397
|
+
this.ws.addEventListener('open', ()=>{
|
|
398
|
+
const rl = this.recursion_level;
|
|
399
|
+
this.log.debug(`on open: reconnected (${rl})`);
|
|
400
|
+
this.is_connected = true;
|
|
401
|
+
for(let key in this.event_listeners)this.event_listeners[key].forEach((cb)=>{
|
|
402
|
+
this.log.debug(`adding listener (${rl}): ${key}`);
|
|
403
|
+
this.ws.addEventListener(key, cb);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
this.ws.addEventListener('close', ()=>{
|
|
407
|
+
this.log.debug('connection closed');
|
|
408
|
+
this.is_connected = false;
|
|
409
|
+
this.backoff = Math.min(2 * this.backoff, this.max_timeout);
|
|
410
|
+
this.log.debug('backoff: ' + this.backoff);
|
|
411
|
+
setTimeout(()=>{
|
|
412
|
+
if (this.should_reconnect) {
|
|
413
|
+
this.ws = null;
|
|
414
|
+
this.setup();
|
|
415
|
+
}
|
|
416
|
+
}, this.backoff + 50 * Math.random());
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
send(data) {
|
|
420
|
+
if (!this.is_connected || !this.ws) throw new Error('not connected');
|
|
421
|
+
this.ws.send(data);
|
|
422
|
+
}
|
|
423
|
+
onMessage(cb) {
|
|
424
|
+
if (!this.ws) throw new Error('ws is null');
|
|
425
|
+
if (!this.event_listeners.message) this.event_listeners.message = [];
|
|
426
|
+
this.event_listeners.message.push((e)=>{
|
|
427
|
+
const rl = this.recursion_level;
|
|
428
|
+
this.log.debug(`message(${rl}): ${e.data}`);
|
|
429
|
+
cb(e.data);
|
|
430
|
+
});
|
|
431
|
+
this.ws.addEventListener('message', (e)=>{
|
|
432
|
+
const rl = this.recursion_level;
|
|
433
|
+
this.log.debug(`message(${rl}): ${e.data}`);
|
|
434
|
+
cb(e.data);
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
on(event, cb) {
|
|
438
|
+
if (!this.ws) throw new Error('ws is null');
|
|
439
|
+
if (!this.event_listeners[event]) this.event_listeners[event] = [];
|
|
440
|
+
this.event_listeners[event].push(cb);
|
|
441
|
+
this.ws.addEventListener(event, cb);
|
|
442
|
+
}
|
|
443
|
+
close() {
|
|
444
|
+
if (!this.is_connected || !this.ws) return;
|
|
445
|
+
this.should_reconnect = false;
|
|
446
|
+
this.ws.close();
|
|
447
|
+
}
|
|
448
|
+
constructor(url, verbose = false){
|
|
449
|
+
network_define_property(this, "url", void 0);
|
|
450
|
+
network_define_property(this, "ws", void 0);
|
|
451
|
+
network_define_property(this, "backoff", 50);
|
|
452
|
+
network_define_property(this, "max_timeout", 10000);
|
|
453
|
+
network_define_property(this, "should_reconnect", true);
|
|
454
|
+
network_define_property(this, "is_connected", false);
|
|
455
|
+
network_define_property(this, "recursion_level", 0);
|
|
456
|
+
network_define_property(this, "log", void 0);
|
|
457
|
+
network_define_property(this, "event_listeners", {});
|
|
458
|
+
this.url = url;
|
|
459
|
+
this.ws = null;
|
|
460
|
+
this.log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'websocket');
|
|
461
|
+
this.setup();
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
class refresh {
|
|
465
|
+
listen() {
|
|
466
|
+
const socket = new ws(this.url);
|
|
467
|
+
if (socket) {
|
|
468
|
+
socket.on('open', ()=>{
|
|
469
|
+
if (this.should_reload) location.reload();
|
|
470
|
+
});
|
|
471
|
+
socket.on('close', ()=>{
|
|
472
|
+
this.should_reload = true;
|
|
473
|
+
setTimeout(this.listen, 500);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
constructor(url){
|
|
478
|
+
network_define_property(this, "url", void 0);
|
|
479
|
+
network_define_property(this, "should_reload", false);
|
|
480
|
+
this.url = url;
|
|
241
481
|
}
|
|
242
482
|
}
|
|
243
483
|
function get(key) {
|
|
@@ -258,57 +498,16 @@ function set(key, value, broadcast = false) {
|
|
|
258
498
|
}
|
|
259
499
|
function listen(key, cb) {
|
|
260
500
|
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++;
|
|
501
|
+
if (ev.detail.key === key || '*' === key) {
|
|
502
|
+
if (cb instanceof dom_el) {
|
|
503
|
+
if (ev.detail.key === key) cb.content(ev.detail.value);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
cb({
|
|
507
|
+
key: ev.detail.key,
|
|
508
|
+
value: ev.detail.value
|
|
509
|
+
});
|
|
298
510
|
}
|
|
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('');
|
|
511
|
+
});
|
|
313
512
|
}
|
|
314
|
-
export {
|
|
513
|
+
export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, 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
|
+
}
|