diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2023-08-25 21:04:59 +0200 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2023-08-25 21:12:57 +0200 |
commit | 78bbfa42fa1159bce12c2c1d29eeb0bb9a8a2f75 (patch) | |
tree | 041dd938346eddf0a4bcd098403c229555a654cb /src/http/static | |
parent | c4ec9064063f3b15aeb25feb85a3afaaa02008ba (diff) |
Remove mailinglist and proof-of-work functionality
Diffstat (limited to 'src/http/static')
-rw-r--r-- | src/http/static/api.js | 118 | ||||
-rw-r--r-- | src/http/static/solvePow.js | 28 | ||||
-rw-r--r-- | src/http/static/utils.js | 12 |
3 files changed, 0 insertions, 158 deletions
diff --git a/src/http/static/api.js b/src/http/static/api.js deleted file mode 100644 index 55c9ecd..0000000 --- a/src/http/static/api.js +++ /dev/null @@ -1,118 +0,0 @@ -import * as utils from "/static/utils.js"; - -const doFetch = async (req) => { - let res, jsonRes; - try { - res = await fetch(req); - jsonRes = await res.json(); - - } catch (e) { - - if (e instanceof SyntaxError) - e = new Error(`status ${res.status}, empty (or invalid) response body`); - - console.error(`api call ${req.method} ${req.url}: unexpected error:`, e); - throw e; - } - - if (jsonRes.error) { - console.error( - `api call ${req.method} ${req.url}: application error:`, - res.status, - jsonRes.error, - ); - - throw jsonRes.error; - } - - return jsonRes; -} - -// may throw -const solvePow = async () => { - - const res = await call('/api/pow/challenge'); - - const worker = new Worker('/static/solvePow.js'); - - const p = new Promise((resolve, reject) => { - worker.postMessage({seedHex: res.seed, target: res.target}); - worker.onmessage = resolve; - }); - - const powSol = (await p).data; - worker.terminate(); - - return {seed: res.seed, solution: powSol}; -} - -const call = async (route, opts = {}) => { - const { - method = 'POST', - body = {}, - requiresPow = false, - } = opts; - - const reqOpts = { - method, - }; - - if (requiresPow) { - const {seed, solution} = await solvePow(); - body.powSeed = seed; - body.powSolution = solution; - } - - if (Object.keys(body).length > 0) { - const form = new FormData(); - for (const key in body) form.append(key, body[key]); - - reqOpts.body = form; - } - - const req = new Request(route, reqOpts); - return doFetch(req); -} - -const ws = async (route, opts = {}) => { - const { - requiresPow = false, - params = {}, - } = opts; - - const docURL = new URL(document.URL); - const protocol = docURL.protocol == "http:" ? "ws:" : "wss:"; - - const fullParams = new URLSearchParams(params); - - if (requiresPow) { - const {seed, solution} = await solvePow(); - fullParams.set("powSeed", seed); - fullParams.set("powSolution", solution); - } - - const rawConn = new WebSocket(`${protocol}//${docURL.host}${route}?${fullParams.toString()}`); - - const conn = { - next: () => new Promise((resolve, reject) => { - rawConn.onmessage = (m) => { - const mj = JSON.parse(m.data); - resolve(mj); - }; - rawConn.onerror = reject; - rawConn.onclose = reject; - }), - - close: rawConn.close, - }; - - return new Promise((resolve, reject) => { - rawConn.onopen = () => resolve(conn); - rawConn.onerror = reject; - }); -} - -export { - call, - ws -} diff --git a/src/http/static/solvePow.js b/src/http/static/solvePow.js deleted file mode 100644 index 900400c..0000000 --- a/src/http/static/solvePow.js +++ /dev/null @@ -1,28 +0,0 @@ -const fromHexString = hexString => - new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); - -const toHexString = bytes => - bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); - -onmessage = async (e) => { - const seed = fromHexString(e.data.seedHex); - const target = e.data.target; - - const fullBuf = new ArrayBuffer(seed.byteLength*2); - - const fullBufSeed = new Uint8Array(fullBuf, 0, seed.byteLength); - seed.forEach((v, i) => fullBufSeed[i] = v); - - const randBuf = new Uint8Array(fullBuf, seed.byteLength); - - while (true) { - crypto.getRandomValues(randBuf); - const digest = await crypto.subtle.digest('SHA-512', fullBuf); - const digestView = new DataView(digest); - if (digestView.getUint32(0) < target) { - postMessage(toHexString(randBuf)); - return; - } - } - -}; diff --git a/src/http/static/utils.js b/src/http/static/utils.js deleted file mode 100644 index 96a2950..0000000 --- a/src/http/static/utils.js +++ /dev/null @@ -1,12 +0,0 @@ -const cookies = {}; -const cookieKVs = document.cookie - .split(';') - .map(cookie => cookie.trim().split('=', 2)); - -for (const i in cookieKVs) { - cookies[cookieKVs[i][0]] = cookieKVs[i][1]; -} - -export { - cookies, -} |