widget: fixes #90 (0.1.26)

This commit is contained in:
Tiago
2025-08-26 12:14:25 +01:00
parent dc5605f0cc
commit 32eb43184c
3 changed files with 64 additions and 56 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cap.js/widget",
"version": "0.1.25",
"version": "0.1.26",
"description": "Client-side widget for Cap, a lightweight, modern open-source CAPTCHA alternative designed using SHA-256 PoW.",
"keywords": [
"security",
+62 -54
View File
@@ -1,69 +1,71 @@
(() => {
const solveFallback = async ({ salt, target }) => {
let nonce = 0;
const batchSize = 50000;
const encoder = new TextEncoder();
const targetBytes = new Uint8Array(target.length / 2);
for (let k = 0; k < targetBytes.length; k++) {
targetBytes[k] = parseInt(target.substring(k * 2, k * 2 + 2), 16);
}
const targetBytesLength = targetBytes.length;
while (true) {
try {
for (let i = 0; i < batchSize; i++) {
const inputString = salt + nonce;
const inputBytes = encoder.encode(inputString);
const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
let matches = true;
for (let k = 0; k < targetBytesLength; k++) {
if (hashBytes[k] !== targetBytes[k]) {
matches = false;
break;
}
}
if (matches) {
self.postMessage({ nonce, found: true });
return;
}
nonce++;
}
} catch (error) {
console.error("[cap worker]", error);
self.postMessage({
found: false,
error: error.message,
});
return;
}
}
};
if (
typeof WebAssembly !== "object" ||
typeof WebAssembly?.instantiate !== "function"
) {
// fallback worker for environments without wasm
// this is much slower than the wasm version
self.onmessage = async ({ data: { salt, target } }) => {
let nonce = 0;
const batchSize = 50000;
const encoder = new TextEncoder();
const targetBytes = new Uint8Array(target.length / 2);
for (let k = 0; k < targetBytes.length; k++) {
targetBytes[k] = parseInt(target.substring(k * 2, k * 2 + 2), 16);
}
const targetBytesLength = targetBytes.length;
while (true) {
try {
for (let i = 0; i < batchSize; i++) {
const inputString = salt + nonce;
const inputBytes = encoder.encode(inputString);
const hashBuffer = await crypto.subtle.digest(
"SHA-256",
inputBytes,
);
const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
let matches = true;
for (let k = 0; k < targetBytesLength; k++) {
if (hashBytes[k] !== targetBytes[k]) {
matches = false;
break;
}
}
if (matches) {
self.postMessage({ nonce, found: true });
return;
}
nonce++;
}
} catch (error) {
console.error("[cap worker]", error);
self.postMessage({
found: false,
error: error.message,
});
return;
}
}
};
return console.warn(
console.warn(
"[cap worker] wasm not supported, falling back to alternative solver. this will be significantly slower.",
);
self.onmessage = async ({ data: { salt, target } }) => {
return solveFallback({ salt, target });
};
return;
}
let wasmCacheUrl, solve_pow_function;
self.onmessage = async ({ data: { salt, target, wasmUrl } }) => {
let fb;
if (wasmCacheUrl !== wasmUrl) {
wasmCacheUrl = wasmUrl;
await import(wasmUrl)
@@ -76,7 +78,12 @@
})
.catch((e) => {
console.error("[cap worker] using fallback solver due to error:", e);
fb = true;
return solveFallback({ salt, target });
});
if (fb) return;
}
try {
@@ -91,6 +98,7 @@
});
} catch (error) {
console.error("[cap worker]", error);
self.postMessage({
found: false,
error: error.message || String(error),