aboutsummaryrefslogtreecommitdiff
path: root/cmd/anubis/static/js
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2025-01-25 15:32:51 -0500
committerXe Iaso <me@xeiaso.net>2025-01-25 15:32:51 -0500
commit58811194f0bced9b578937cdeb36d00243ac7df1 (patch)
treee8406ed18f7fa8c4a99ff4e228f76149fbafc268 /cmd/anubis/static/js
parent84a2cb246c5c6172c2499f57a43cbc898d4e2c3f (diff)
downloadx-58811194f0bced9b578937cdeb36d00243ac7df1.tar.xz
x-58811194f0bced9b578937cdeb36d00243ac7df1.zip
cmd/anubis: minify JS, add video element test
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/anubis/static/js')
-rw-r--r--cmd/anubis/static/js/main.mjs51
-rw-r--r--cmd/anubis/static/js/proof-of-work.mjs62
2 files changed, 1 insertions, 112 deletions
diff --git a/cmd/anubis/static/js/main.mjs b/cmd/anubis/static/js/main.mjs
index f111bb4..4d8c60c 100644
--- a/cmd/anubis/static/js/main.mjs
+++ b/cmd/anubis/static/js/main.mjs
@@ -1,50 +1 @@
-import { process } from './proof-of-work.mjs';
-
-// from Xeact
-const u = (url = "", params = {}) => {
- let result = new URL(url, window.location.href);
- Object.entries(params).forEach((kv) => {
- let [k, v] = kv;
- result.searchParams.set(k, v);
- });
- return result.toString();
-};
-
-(async () => {
- const status = document.getElementById('status');
- const image = document.getElementById('image');
- const title = document.getElementById('title');
- const spinner = document.getElementById('spinner');
-
- status.innerHTML = 'Calculating...';
-
- const { challenge, difficulty } = await fetch("/.within.website/x/cmd/anubis/api/make-challenge", { method: "POST" })
- .then(r => {
- if (!r.ok) {
- throw new Error("Failed to fetch config");
- }
- return r.json();
- })
- .catch(err => {
- status.innerHTML = `Failed to fetch config: ${err.message}`;
- image.src = "/.within.website/x/cmd/anubis/static/img/sad.webp";
- throw err;
- });
-
- status.innerHTML = `Calculating...<br/>Difficulty: ${difficulty}`;
-
- const t0 = Date.now();
- const { hash, nonce } = await process(challenge, difficulty);
- const t1 = Date.now();
-
- title.innerHTML = "Success!";
- status.innerHTML = `Done! Took ${t1 - t0}ms, ${nonce} iterations`;
- image.src = "/.within.website/x/cmd/anubis/static/img/happy.webp";
- spinner.innerHTML = "";
- spinner.style.display = "none";
-
- setTimeout(() => {
- const redir = window.location.href;
- window.location.href = u("/.within.website/x/cmd/anubis/api/pass-challenge", { response: hash, nonce, redir, elapsedTime: t1 - t0, difficulty });
- }, 2000);
-})(); \ No newline at end of file
+(()=>{function w(e,i=5){return new Promise((n,t)=>{let o=URL.createObjectURL(new Blob(["(",y(),")()"],{type:"application/javascript"})),s=new Worker(o);s.onmessage=r=>{s.terminate(),n(r.data)},s.onerror=r=>{s.terminate(),t()},s.postMessage({data:e,difficulty:i}),URL.revokeObjectURL(o)})}function y(){return function(){let e=i=>{let n=new TextEncoder().encode(i);return crypto.subtle.digest("SHA-256",n.buffer).then(t=>Array.from(new Uint8Array(t)).map(o=>o.toString(16).padStart(2,"0")).join(""))};addEventListener("message",async i=>{let n=i.data.data,t=i.data.difficulty,o,s=0;do o=await e(n+s++);while(o.substring(0,t)!==Array(t+1).join("0"));s-=1,postMessage({hash:o,data:n,difficulty:t,nonce:s})})}.toString()}var g='<video id="videotest" width="0" height="0" src="/.within.website/x/cmd/anubis/static/testdata/black.mp4"></video>',h=async e=>(e.innerHTML=g,await new Promise(i=>{let n=document.getElementById("videotest");n.oncanplay=()=>{e.style.display="none",i(!0)},n.onerror=t=>{e.style.display="none",i(!1)}}));var f=(e="",i={})=>{let n=new URL(e,window.location.href);return Object.entries(i).forEach(t=>{let[o,s]=t;n.searchParams.set(o,s)}),n.toString()},d=e=>`/.within.website/x/cmd/anubis/static/img/${e}.webp`;(async()=>{let e=document.getElementById("status"),i=document.getElementById("image"),n=document.getElementById("title"),t=document.getElementById("spinner"),o=document.getElementById("testarea");if(!await h(o)){n.innerHTML="Oh no!",e.innerHTML="Checks failed. Please check your browser's settings and try again.",i.src=d("sad"),t.innerHTML="",t.style.display="none";return}e.innerHTML="Calculating...";let{challenge:r,difficulty:c}=await fetch("/.within.website/x/cmd/anubis/api/make-challenge",{method:"POST"}).then(a=>{if(!a.ok)throw new Error("Failed to fetch config");return a.json()}).catch(a=>{throw n.innerHTML="Oh no!",e.innerHTML=`Failed to fetch config: ${a.message}`,i.src=d("sad"),t.innerHTML="",t.style.display="none",a});e.innerHTML=`Calculating...<br/>Difficulty: ${c}`;let l=Date.now(),{hash:p,nonce:m}=await w(r,c),u=Date.now();n.innerHTML="Success!",e.innerHTML=`Done! Took ${u-l}ms, ${m} iterations`,i.src=d("happy"),t.innerHTML="",t.style.display="none",setTimeout(()=>{let a=window.location.href;window.location.href=f("/.within.website/x/cmd/anubis/api/pass-challenge",{response:p,nonce:m,redir:a,elapsedTime:u-l,difficulty:c})},2e3)})();})();
diff --git a/cmd/anubis/static/js/proof-of-work.mjs b/cmd/anubis/static/js/proof-of-work.mjs
deleted file mode 100644
index d71d2db..0000000
--- a/cmd/anubis/static/js/proof-of-work.mjs
+++ /dev/null
@@ -1,62 +0,0 @@
-// https://dev.to/ratmd/simple-proof-of-work-in-javascript-3kgm
-
-export function process(data, difficulty = 5) {
- return new Promise((resolve, reject) => {
- let webWorkerURL = URL.createObjectURL(new Blob([
- '(', processTask(), ')()'
- ], { type: 'application/javascript' }));
-
- let worker = new Worker(webWorkerURL);
-
- worker.onmessage = (event) => {
- worker.terminate();
- resolve(event.data);
- };
-
- worker.onerror = (event) => {
- worker.terminate();
- reject();
- };
-
- worker.postMessage({
- data,
- difficulty
- });
-
- URL.revokeObjectURL(webWorkerURL);
- });
-}
-
-function processTask() {
- return function () {
- const sha256 = (text) => {
- const encoded = new TextEncoder().encode(text);
- return crypto.subtle.digest("SHA-256", encoded.buffer).then((result) =>
- Array.from(new Uint8Array(result))
- .map((c) => c.toString(16).padStart(2, "0"))
- .join(""),
- );
- };
-
- addEventListener('message', async (event) => {
- let data = event.data.data;
- let difficulty = event.data.difficulty;
-
- let hash;
- let nonce = 0;
- do {
- hash = await sha256(data + nonce++);
- } while (hash.substring(0, difficulty) !== Array(difficulty + 1).join('0'));
-
- nonce -= 1; // last nonce was post-incremented
-
- postMessage({
- hash,
- data,
- difficulty,
- nonce,
- });
- });
- }.toString();
-}
-