Merge pull request #147 from mitallast/master

Fix: ensuring worker is ready
This commit is contained in:
Aravinth Manivannan
2024-03-12 19:41:49 +05:30
committed by GitHub
5 changed files with 28 additions and 7 deletions

View File

@@ -21,7 +21,7 @@ SPDX-License-Identifier: MIT OR Apache-2.0
<label class="widget__verification-container" for="widget__verification-checkbox"> <label class="widget__verification-container" for="widget__verification-checkbox">
<span id="widget__verification-text" <span id="widget__verification-text"
>I'm not a robot</span> >I'm not a robot</span>
<input <input disabled
id="widget__verification-checkbox" id="widget__verification-checkbox"
aria-valuenow="I'm not a robot" aria-valuenow="I'm not a robot"
aria-checked="false" aria-checked="false"

View File

@@ -12,7 +12,17 @@ import * as CONST from "./const";
import "./main.scss"; import "./main.scss";
let LOCK = false; let LOCK = false;
const workerPromise = new Promise<Worker>((res) => {
const worker = new Worker("/bench.js"); const worker = new Worker("/bench.js");
worker.onmessage = (event: MessageEvent) => {
const message: ServiceWorkerMessage = event.data;
if(message.type === "ready") {
console.log("worker ready");
res(worker);
}
};
});
/** add mcaptcha widget element to DOM */ /** add mcaptcha widget element to DOM */
export const registerVerificationEventHandler = (): void => { export const registerVerificationEventHandler = (): void => {
@@ -20,10 +30,14 @@ export const registerVerificationEventHandler = (): void => {
document.querySelector(".widget__verification-container") document.querySelector(".widget__verification-container")
); );
verificationContainer.style.display = "flex"; verificationContainer.style.display = "flex";
CONST.btn().addEventListener("click", (e) => solveCaptchaRunner(e)); workerPromise.then((worker: Worker) => {
const btn = CONST.btn();
btn.disabled = false;
btn.addEventListener("click", (e) => solveCaptchaRunner(worker, e));
});
}; };
export const solveCaptchaRunner = async (e: Event): Promise<void> => { export const solveCaptchaRunner = async (worker: Worker, e: Event): Promise<void> => {
const PROGRESS_FILL = <HTMLElement>document.querySelector(".progress__fill"); const PROGRESS_FILL = <HTMLElement>document.querySelector(".progress__fill");
const setWidth = (width: number) => { const setWidth = (width: number) => {
@@ -94,7 +108,7 @@ export const solveCaptchaRunner = async (e: Event): Promise<void> => {
} }
if (resp.type === "progress") { if (resp.type === "progress") {
if (width < 80) { if (width < 80) {
width = Number(resp.nonce / max_recorded_nonce) * 100; width = resp.nonce / max_recorded_nonce * 100;
setWidth(width); setWidth(width);
} }
console.log(`received nonce ${resp.nonce}`); console.log(`received nonce ${resp.nonce}`);

View File

@@ -30,7 +30,7 @@ const prove = async (
config.string, config.string,
config.difficulty_factor, config.difficulty_factor,
STEPS, STEPS,
progress (nonce: BigInt | number) => progress(Number(nonce))
); );
const t1 = performance.now(); const t1 = performance.now();
time = t1 - t0; time = t1 - t0;

View File

@@ -9,6 +9,12 @@ import prove from "./prove";
import { PoWConfig, ServiceWorkerMessage, ServiceWorkerWork } from "./types"; import { PoWConfig, ServiceWorkerMessage, ServiceWorkerWork } from "./types";
log.log("worker registered"); log.log("worker registered");
const ready: ServiceWorkerMessage = {
type: "ready",
};
postMessage(ready);
onmessage = async (e) => { onmessage = async (e) => {
console.debug("message received at worker"); console.debug("message received at worker");
const config: PoWConfig = e.data; const config: PoWConfig = e.data;

View File

@@ -40,5 +40,6 @@ export type Token = {
}; };
export type ServiceWorkerMessage = export type ServiceWorkerMessage =
| { type: "ready" }
| { type: "work"; value: ServiceWorkerWork } | { type: "work"; value: ServiceWorkerWork }
| { type: "progress"; nonce: number }; | { type: "progress"; nonce: number };