-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
831 lines (763 loc) · 25.2 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*jshint esversion: 6 */
//lucid.app/documents/embeddedchart/e19ef36e-5abb-40fe-95d9-84cd4e140947#
const uid = Date.now();
const server = location.host.indexOf("httprelay") === -1;
sm = {
proxy_url: "//demo.httprelay.io/proxy/" + uid,
to: 2048 * 2,
users: 6,
log: {
c: { uid: uid, auth: {}, users: 0 },
[uid]: {
c: { uid: uid },
e: []
}
},
SERVE: function () {
// merge push with master, prune master, return revlist
sm.proxy.routes.addGet("/log", "log", (ctx) => {
let params = ctx.request.url.searchParams.get("log") || "{}";
let log = JSON.parse(decodeURIComponent(params));
//console.log("log", log);
// empty or malformed
if (!log || !log.c || !log.c.uid) {
return {};
}
// globals
const time = Date.now();
const uid = log.c.uid;
let branch = sm.log[uid];
let cfg = sm.log.c;
// access user
let init = log.e.length && log.e[0].init;
let auth, uac, is_full;
const daemon = function (access, user = uid) {
// access server
if (access) {
// user with auth denied may retain session
let branch = sm.log[user];
if (access == "deny" && branch) {
cfg.users -= 1;
branch.c.time = "Infinity";
branch.c.hint = 0;
// prune user
if (branch.e.length <= 1 || sm.purge) {
delete sm.log[user];
//delete cfg.auth[user];
}
} else if (access == "allow") {
cfg.users += 1;
}
cfg.auth[user] = access;
}
// update access user, unless deny
if (user == uid) {
auth = cfg.auth[user];
}
uac = auth != "allow";
is_full = uac && cfg.users >= sm.users;
};
// uac
let unload = log.c.time == Infinity ? "deny" : false;
daemon(unload);
if (init || uac) {
// user access control
if (!auth) {
// queue or allow
let access = is_full ? log.e[0].time : "allow";
daemon(access);
} else if (isFinite(auth) && !is_full) {
// allow oldest queuer
let initiate = uid;
Object.keys(cfg.auth).forEach(function (user) {
let depth = cfg.auth[user];
if (isFinite(depth) && depth < cfg.auth[initiate]) {
initiate = user;
}
});
daemon("allow", initiate);
//console.log("allow from queue", uid);
}
// abort, no permission
if (uac || is_full) {
//log.e = [];
const queue = function () {
// queuer timestamp depth
let wait = 0;
Object.keys(cfg.auth).forEach(function (user) {
let depth = cfg.auth[user];
if (isFinite(depth) && depth <= auth) {
wait++;
}
});
return -wait;
};
let wait = auth == "deny" ? "Infinity" : queue();
return { c: { time: wait, hint: -1 } };
} else if (init) {
// new master branch, bump access time
log.e[0].time = log.c.time = time;
branch = sm.log[uid] = log;
branch.c.auth = {};
}
}
let events = branch.e;
// last fetch ( multi-part 0 ) minus two deviation
let multi_idx = 0;
let multi_tip = branch.c.time - sm.to * 2;
for (let i = events.length - 1; i >= 0; i--) {
if (events[i].time < multi_tip) {
// index to sort multi-part timestamps
multi_idx = i;
break;
}
}
// push log events to master branch
if (!init) {
for (let i = 0; i < log.e.length; i++) {
let event = log.e[i];
// image share route and thumbnail
if (event.id && event.id.toString().indexOf("i__") === 0) {
let de = sm.lzw.de(event.value);
// serve route image
sm.img(de, event.id, { serve: event, pass: 1 / 64 });
}
if (uid !== cfg.uid) {
events.push(event);
}
}
}
// sort events by time
function multipart(a, b) {
if ("init" in a || "init" in b) {
return 0;
} else {
if (a.time < b.time) return -1;
if (a.time > b.time) return 1;
return 0;
}
}
// live events list is async
let sorted = events.slice(multi_idx).sort(multipart);
branch.e = events.slice(0, multi_idx).concat(sorted);
// multi-part early abort. should uac/daemon?
if (log.c.hint > 1) {
return { c: { time: "Infinity", hint: log.c.hint } };
}
// push branch cfg to master
Object.keys(log.c).forEach((meta) => {
if (init || (meta != "time" && meta != "auth")) {
branch.c[meta] = log.c[meta];
}
});
// latency
// server: [...AIMD poll intervals] (revlists use -interval)
// client: compression factor
let delta = (time - log.c.time) / sm.to;
delta = branch.c.hint = Math.max(1 - delta, 0).toFixed(3);
// events: prune server old, sent client new
let revlist = { c: { time: time, hint: delta } };
let users = 0;
Object.keys(sm.log).forEach((peer) => {
if (peer !== "c") {
let denied = cfg.auth[peer] == "deny";
let user = sm.log[peer];
if (peer != uid) {
let u = (revlist[peer] = { c: user.c });
// peers init, round-trip, events
let pull_hard =
branch.c.auth[peer] === "init" &&
branch.e[0].init === false &&
user.e[0].init === false &&
user.e.length > 1;
for (let i = user.e.length - 1; i >= 0; i--) {
let event = user.e[i];
// commits to revlist, unless own user
//if (peer != uid) {
let tip_push = event.time >= branch.c.time - sm.to;
let tip_pull = event.time >= user.c.time - sm.to;
// auth new init local
let HEAD = (tip_push || tip_pull) && event.time <= time;
if ((HEAD && !denied) || pull_hard) {
// || init
u[event.time] = event;
}
//}
// prune before HEAD (basic test)
if (event.time < branch.c.time) {
for (let j = i - 1; j >= 0; j--) {
let eventPre = user.e[j];
if (
event.id == eventPre.id &&
event.value == eventPre.value
) {
user.e.splice(j, 1);
i--;
}
}
}
}
// peer init, full clone singleton
if (pull_hard) {
branch.c.auth[peer] = "done";
//delete branch.c.auth[peer];
}
// peer init, handshake
if (user.c.auth && !user.c.auth[uid] && !denied) {
user.c.auth[uid] = "init";
}
}
// user access control
const is_server = peer == cfg.uid;
if (!is_server && (!denied || sm.purge)) {
// server as user skip: count, events, unload...
let active = user.e && user.e.length > 1;
let recent = user.e && 30000 >= time - user.e[0].time;
const is_unload = user.c.time == Infinity || (!active && !recent);
if (is_unload) {
// user access
daemon("deny", user.c.uid);
} else {
users++;
}
}
}
// client revlist doesn't use auth...?
//u.c.auth = {};
});
// real meta (not uac daemon, pre-revlist)
cfg.users = users;
branch.c.time = log.c.time;
// server/user auth handshake
if (init) {
log.e[0].init = false;
}
let large = JSON.stringify(revlist).length;
if (large >= 19200) {
console.error("LARGE", revlist, large);
}
return revlist;
});
},
GET: function (cfg) {
let branch = sm.log[uid];
let last = branch.c.time;
// local HEAD time per response hint
branch.c.time =
isFinite(cfg.time) && cfg.hint >= 0
? Date.now()
: branch.c.time || cfg.time;
// init event
let auth = branch.e[0];
if (!isFinite(last) && isFinite(branch.c.time)) {
// final handshake could utilize cfg.hint 1.000 >= 0
auth.init = false;
auth.time = cfg.time;
}
// if URL parameter exceeds boundary, multi-part fetch (like FormData)
const body = function (boundary) {
// push local cfg
let params = { c: { uid: branch.c.uid, time: branch.c.time }, e: [] };
//Object.keys(branch.c).forEach(function (k) {
// multi-part cfg is minimal
// if (boundary === 0 || k == "uid" || k == "time" || k == "hint") {
// params.c[k] = branch.c[k];
// }
//});
// multi-part fetch returns revlist once
params.c.hint = boundary + 1;
return params;
};
let multi = [body(0)];
let part = multi[multi.length - 1];
// proxy parameters
if (auth.init) {
// uac daemon
part.e.unshift(auth);
} else {
// local user events since last GET
for (let i = branch.e.length - 1; i > 0; i--) {
let event = branch.e[i];
if (event.time > last) {
if (JSON.stringify(part).length >= 20480) {
let boundary = multi.length;
part = body(boundary);
multi.unshift(part);
}
// init sends false once more
part.e.unshift(event);
} else {
break;
}
}
}
console.log("multipart", multi);
// route has 20-minute cache ( max ~2048KB * 10 per second )
let revlogs = document.getElementById("revlist");
for (let i = 0; i < multi.length; i++) {
let part = multi[i];
// part-specific cfg
if (i == 0) {
part.c = branch.c;
}
part.c.hint = i + 1;
let params = encodeURIComponent(JSON.stringify(part));
if (part.c.time == "Infinity") {
// user unload, no delayed multi-part
fetch(sm.proxy_url + "/log?log=" + params, { keepalive: true });
continue;
}
let RPS = i * 200;
setTimeout(() => {
let toast = document.createElement("article");
let status = "";
fetch(sm.proxy_url + "/log?log=" + params, { keepalive: true })
.then((response) => {
// response > 19200 may be truncated
if (!response.ok || response.status !== 200) {
// == 400
// error, reconnect?
status = response.status;
throw new Error(response.status);
}
return response.text();
})
.then((res) => {
// JSON, not JSON, or nothing?
try {
res = JSON.parse(res);
} catch {
res = res;
}
return res;
})
.then((revlist) => {
// max revlist: ~2048KB*users
console.log("revlist", revlist);
let cfg = revlist && revlist.c ? revlist.c : { hint: -1 };
// cfg callback: uac daemon
if (cfg.hint === -1) {
status += "access error";
if (cfg.time == "Infinity") {
// uid blocked, reconnect?
document.getElementById("client").disabled = false;
status += ": expired";
} else if (cfg.time < 0) {
// -Infinity or -queue
status += ": max user" + cfg.time;
}
// undefined... reconnect?
} else if (cfg.hint > 1 && cfg.time == "Infinity") {
status = "multi-part: " + cfg.hint;
return;
}
// reconnect loop
clearTimeout(sm.sto);
if (cfg.time != "Infinity") {
sm.sto = setTimeout(function () {
sm.GET(cfg);
}, sm.to);
}
// local logs
document.getElementById("local").innerText = JSON.stringify(
sm.log,
null,
2
);
// revlist cards
let fragment = new DocumentFragment();
Object.keys(revlist).forEach(function (key) {
let merge = revlist[key];
let card = document.createElement("section");
if (key == "c") {
if (!server) {
// pull revlist cfg unless server or error
// hint is latency (client-specific)
sm.log.c.hint = merge.hint;
}
card.style.backgroundColor = "#efefef";
} else {
// user style
let color =
merge.color || (merge.c && merge.c.color) || "initial";
card.style.backgroundColor = color;
// text legible
color = color.replace("#", "").replace("initial", "FFFFFF");
color =
Number(color.charAt(0)) +
Number(color.charAt(2)) +
Number(color.charAt(4));
if (isFinite(color) && color <= 27) {
card.style.color = "#fff";
}
// latency
let hint = (90 * (merge.c && merge.c.hint)) | 0;
hint = "hsl(" + hint + ", 100%, 50%)";
card.style.boxShadow = "inset 0.25rem 0 " + hint;
}
// prettify
let string = JSON.stringify({ [key]: merge }, null, 2);
string = string.slice(1);
string = string.slice(0, -1);
card.innerText = string;
// output
fragment.append(card);
});
toast.append(fragment);
// revlist old remove
let articles = revlogs.querySelectorAll("article[data-time]");
for (let i = articles.length - 1; i >= 0; i--) {
let article = articles[i];
let time = article.getAttribute("data-time");
if (Date.now() - time > sm.to * 20) {
article.parentElement.removeChild(article);
}
}
})
.catch((error) => {
status = status || -1;
//error.message == "Failed to fetch"
console.error("revlist", error);
})
.finally(() => {
if (typeof status == "number") {
// server error: status (-1, 401)
// possibly a multi-part
status = "serve error: " + status;
document.getElementById("client").disabled = false;
clearTimeout(sm.sto);
}
// output toast ui
toast.setAttribute("data-time", Date.now());
toast.prepend(status);
revlogs.appendChild(toast);
toast.scrollIntoView();
});
}, RPS);
}
},
proxy_init: function () {
// proto template served to client
let template = document.getElementById("template").cloneNode(true);
// proxy role
let role = server ? "server" : "client";
document.getElementById(role).disabled = false;
// share links
document.getElementById("logs").classList.remove("hide");
document
.getElementById("logLink")
.setAttribute("href", sm.proxy_url + "/log");
document
.getElementById("pageLink")
.setAttribute("href", sm.proxy_url + "/page");
if (server) {
// create name server
// note: multiple servers prevents fetch response
sm.proxy = new HttpRelay(new URL("https://demo.httprelay.io")).proxy(
sm.log.c.uid
);
// dependency
let globals = document.createElement("script");
globals.textContent += "let sm;";
globals.textContent += "const serverId = " + sm.log.c.uid + ";";
let js = document.createElement("script");
js.src = "//codepen.io/kpachinger/pen/VwzmKJV.js";
sm.proxy.routes.addGet("/page", "page", () => {
// route clients to landing page
let doc = document.implementation.createHTMLDocument(
"HTTPRelay-js Client"
);
doc.head.appendChild(globals);
doc.body.appendChild(template);
doc.body.appendChild(js);
return doc;
});
sm.proxy.routes.addGet("/download/:id", "download", (ctx) => {
// route downloads
//let img = document.getElementById(ctx.routeParams[0]);
//let blob = new Blob([img], {type:"image/jpeg"})
return ctx.respond({
body: document.getElementById(ctx.routeParams[0])["file"],
//body: blob,
download: true
});
});
sm.proxy.start();
setInterval(function () {
// poll to maintain server
let err = sm.proxy.errRetry;
if (err > 0 && err % 4 == 0) {
// interrupt: lost internet, browser asleep...
console.error("error");
sm.proxy.stop();
document.getElementById("server").disabled = false;
} else {
// silent interrupt: fetch error, second server...
}
}, 15000);
//
}
// click events
let ui = document.querySelectorAll("fieldset [id]");
for (let i = ui.length - 1; i >= 0; i--) {
let el = ui[i];
switch (el.id) {
case "client":
case "server":
el.addEventListener("click", button);
break;
default:
el.addEventListener("change", button);
}
}
function button(e) {
let target = e.target,
id = target.id;
if (id == "server" || id == "client") {
target.disabled = true;
if (id == "server") {
if (sm.proxy.abortSig.aborted) {
// reset fetch
// ... also errRetry, users...?
sm.proxy.abortCtrl = new AbortController();
sm.proxy.abortSig = sm.proxy.abortCtrl.signal;
sm.proxy.start();
return;
}
// route client event logs
sm.SERVE();
document.getElementById("client").disabled = false;
document.getElementById("sessions").classList.remove("hide");
} else {
// vanity username
let user = sm.log[uid];
const rand = function (arr) {
return arr[Math.floor(Math.random() * arr.length)];
};
if (!user.c.user) {
let username = "HOST";
if (!server) {
username = ["Delta", "Gamma", "Vega", "Theta"];
username = rand(username);
username += "_" + Math.floor(Math.random() * 100);
}
user.c.user = username;
document.getElementById("user").value = username;
}
if (!user.c.color) {
let color = "#c0c0c0";
if (!server) {
color = ["c0", "cf", "ff"];
color = "#" + rand(color) + rand(color) + rand(color);
}
user.c.color = color;
document.getElementById("color").value = color;
}
// init event and GET loop
if (!user.e.length || !("init" in user.e[0])) {
user.e.unshift({
time: Date.now(),
init: true
});
} else {
// button re-enabled (no server, timeout...)
if (!server && user.e[0].init !== true) {
// attempt reconnect with convoluted uid
user.c.uid += "c";
user.e[0].init = true;
user.c.time = "-Infinity";
}
}
sm.GET({ time: "-Infinity" });
}
} else {
switch (id) {
case "users":
// users may only increase
if (target.value > sm.users) {
sm[id]++;
} else {
target.value = sm.users;
}
break;
default:
sm[id] = target.checked;
}
}
}
},
proxy_add: function (val, type, id = 123) {
if (type == "file") {
let files = val.files;
if (FileReader && files && files.length) {
for (let i = 0; i < files.length; i++) {
let file = files[i];
let fr = new FileReader();
fr.onloadend = function () {
let tokenReg = /[^A-Za-z0-9_-]/g;
let token = "i__" + file.name.replace(tokenReg, "__");
sm.img(fr.result, token, { type: file.type });
};
file && fr.readAsDataURL(file);
}
}
} else if (!type) {
sm.log[uid].e.push({ time: Date.now(), value: val, id: id });
} else {
sm.log[uid].c[type] = val;
}
},
img: function (img, token, opts = {}) {
let type = opts.type || "image/jpeg";
let pass = opts.pass || 1 / 2;
// pass multiple (latency, archive)
// load image source
let output = {};
let image = document.createElement("img");
image.id = token;
image.onload = function () {
convert();
};
image.src = img;
const convert = function () {
// pass max dimensions
let MAX = 512 * pass;
let width = image.width;
let height = image.height;
if (width > height) {
if (width > MAX) {
height = height * (MAX / width);
width = MAX;
}
} else {
if (height > MAX) {
width = width * (MAX / height);
height = MAX;
}
}
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
const resize = function (type, inc) {
// thumbnail resize and base64
canvas.width = width * inc;
canvas.height = height * inc;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
return canvas.toDataURL(type, inc);
};
// reduce source to max
output.thumb = resize(type, 1);
// reduce output to min
let file,
inc = 0.1;
let types = ["image/jpeg", "image/png", "image/webp"];
for (let i = 0; i < types.length; i++) {
let type = types[i];
for (let j = 1; j >= 0; j -= inc) {
// increment (dimensions/quality) until within max filesize
let thumb = resize(type, j);
if (thumb.length <= 9_600) {
// use additional iteration if > 75% reduction
let thumberer = resize(type, j - inc);
thumb = thumberer.length / thumb.length < 0.75 ? thumberer : thumb;
// use minimum size file of types
file = thumb.length < output.thumb.length ? thumb : file;
break;
}
}
}
// transfer minimum
let en = sm.lzw.en(file || output.thumb);
if (opts.serve) {
// crunch thumbnail for revlist
opts.serve.value = en;
// route file download
async function dataUrlToFile() {
type = img.slice(img.indexOf("image/"), img.indexOf(";base64"));
// create
const res = await fetch(img);
const blob = await res.blob();
let ext = token.lastIndexOf("__");
//type.slice(type.indexOf("/")+1)
ext = token.slice(0, ext) + "." + type.slice(type.indexOf("/") + 1);
output.file = new File([blob], ext, { type: type });
// append
image["file"] = output.file;
let link = document.createElement("a");
link.href = sm.proxy_url + "/download/" + token;
link.target = "_blank";
link.append(image);
document.getElementById("images").append(link);
return;
}
dataUrlToFile();
// serve thumbnail and file
} else {
// client compress and send
sm.proxy_add(en, false, token);
}
return output;
};
},
lzw: {
en: function (c) {
var x = "charCodeAt",
b,
e = {},
f = c.split(""),
d = [],
a = f[0],
g = 256;
for (b = 1; b < f.length; b++)
(c = f[b]),
null != e[a + c]
? (a += c)
: (d.push(1 < a.length ? e[a] : a[x](0)),
(e[a + c] = g),
g++,
(a = c));
d.push(1 < a.length ? e[a] : a[x](0));
for (b = 0; b < d.length; b++) d[b] = String.fromCharCode(d[b]);
return d.join("");
},
de: function (b) {
let f, o;
var a,
e = {},
d = b.split(""),
c = (f = d[0]),
g = [c],
h = (o = 256);
for (b = 1; b < d.length; b++)
(a = d[b].charCodeAt(0)),
(a = h > a ? d[b] : e[a] ? e[a] : f + c),
g.push(a),
(c = a.charAt(0)),
(e[o] = f + c),
o++,
(f = a);
return g.join("");
}
}
};
// loaded?
if (server) {
// server: load proxy & SERVE
let script = document.createElement("script");
script.src = "//unpkg.com/httprelay@0.0.44/lib/non-mod/httprelay.js";
script.onload = function () {
sm.proxy_init();
};
document.head.appendChild(script);
} else {
// client: load proxy & GET
sm.log.c.uid = serverId;
sm.proxy_url = "https://demo.httprelay.io/proxy/" + sm.log.c.uid;
sm.proxy_init();
window.onbeforeunload = function () {
// unload stricter
sm.log[uid].c.time = "Infinity";
sm.GET({
time: "Infinity"
});
};
}