diff --git a/web/js/algorithms/fast.mjs b/web/js/algorithms/fast.ts similarity index 72% rename from web/js/algorithms/fast.mjs rename to web/js/algorithms/fast.ts index ee08a19f..6f3483b4 100644 --- a/web/js/algorithms/fast.mjs +++ b/web/js/algorithms/fast.ts @@ -1,11 +1,18 @@ +type ProgressCallback = (nonce: number) => void; + +interface ProcessOptions { + basePrefix: string; + version: string; +} + export default function process( - { basePrefix, version }, - data, - difficulty = 5, - signal = null, - progressCallback = null, - threads = Math.trunc(Math.max(navigator.hardwareConcurrency / 2, 1)), -) { + options: ProcessOptions, + data: string, + difficulty: number = 5, + signal: AbortSignal | null = null, + progressCallback?: ProgressCallback, + threads: number = Math.trunc(Math.max(navigator.hardwareConcurrency / 2, 1)), +): Promise { console.debug("fast algo"); let workerMethod = window.crypto !== undefined ? "webcrypto" : "purejs"; @@ -16,13 +23,17 @@ export default function process( } return new Promise((resolve, reject) => { - let webWorkerURL = `${basePrefix}/.within.website/x/cmd/anubis/static/js/worker/sha256-${workerMethod}.mjs?cacheBuster=${version}`; + let webWorkerURL = `${options.basePrefix}/.within.website/x/cmd/anubis/static/js/worker/sha256-${workerMethod}.mjs?cacheBuster=${options.version}`; - console.log(webWorkerURL); - - const workers = []; + const workers: Worker[] = []; let settled = false; + const onAbort = () => { + console.log("PoW aborted"); + cleanup(); + reject(new DOMException("Aborted", "AbortError")); + }; + const cleanup = () => { if (settled) { return; @@ -34,12 +45,6 @@ export default function process( } }; - const onAbort = () => { - console.log("PoW aborted"); - cleanup(); - reject(new DOMException("Aborted", "AbortError")); - }; - if (signal != null) { if (signal.aborted) { return onAbort(); diff --git a/web/js/algorithms/index.mjs b/web/js/algorithms/index.ts similarity index 80% rename from web/js/algorithms/index.mjs rename to web/js/algorithms/index.ts index cc1ae5d5..5b571837 100644 --- a/web/js/algorithms/index.mjs +++ b/web/js/algorithms/index.ts @@ -1,4 +1,4 @@ -import fast from "./fast.mjs"; +import fast from "./fast"; export default { fast: fast,