Skip to content

Commit

Permalink
Implement fallback inline worker if service worker is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
franzwilding committed Mar 18, 2022
1 parent 2da1dc0 commit 7bb5cbc
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 22 deletions.
13 changes: 9 additions & 4 deletions Resources/assets/js/modules/Captcha.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import FallbackServiceWorker from './FallbackServiceWorker';

/**
* Registers a service worker and let it solve a captcha puzzle
Expand All @@ -18,9 +19,14 @@ export default class Captcha {
return;
}

this.serviceWorker = new Worker(serviceWorkerPath)
try {
this.serviceWorker = new Worker(serviceWorkerPath)
} catch(e) {
this.serviceWorker = new FallbackServiceWorker();
}

this.serviceWorker.addEventListener('message', (e) => {
const { data } = e;
const {data} = e;
switch (data.cmd) {
case 'solution':
this.solveResolve(data.args);
Expand All @@ -29,15 +35,14 @@ export default class Captcha {
this.solveReject(data.args);
break;
case 'progress':
if(this.onProgressCallback) {
if (this.onProgressCallback) {
this.onProgressCallback({
...data.args,
duration: performance.now() - this.solveStartTime
})
}
break;
default:
console.log(data);
break;
}
});
Expand Down
31 changes: 31 additions & 0 deletions Resources/assets/js/modules/FallbackServiceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import PuzzleSolver from "./PuzzleSolver";

export default class FallbackServiceWorker {
constructor() {
this.listeners = {};
const puzzleSolver = new PuzzleSolver(this, true);
this.addEventListener('message', function(e) {
const { data } = e;
switch (data.cmd) {
case 'start':
const {
timestamp,
targetHash,
puzzle,
puzzleStrength,
} = data.args;
puzzleSolver.solve(timestamp, targetHash, puzzle, puzzleStrength);
break;
}
}, false);
}
addEventListener(message, func) {
this.listeners[message] = this.listeners[message] || [];
this.listeners[message].push(func);
}
postMessage(message) {
this.listeners.message.forEach((l) => {
l({data: message});
});
}
}
11 changes: 10 additions & 1 deletion Resources/assets/js/modules/PuzzleSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Sha256 } from '@aws-crypto/sha256-browser'

export default class PuzzleSolver {

constructor(messageBus = null) {
constructor(messageBus = null, wait = false) {
this.messageBus = messageBus || { postMessage() {} };
this.wait = wait;
}

async hash(str) {
Expand Down Expand Up @@ -52,6 +53,14 @@ export default class PuzzleSolver {
const iCandidate = iLeftPuzzleSolution + iBitsMiddle + iBitsToAppend;
const iHash = await this.hash(iCandidate);

if(this.wait && i % 500 === 0) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1);
});
}

if (iHash === pTargetHash) {
this.postMessage('solution', { solution: iCandidate });
return;
Expand Down
2 changes: 1 addition & 1 deletion dist/captcha.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/captcha.js

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions dist/captchaServiceWorker.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions dist/entrypoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"entrypoints": {
"captcha": {
"css": [
"/captcha.css"
],
"js": [
"/captcha.js"
]
},
"captchaServiceWorker": {
"js": [
"/captchaServiceWorker.js"
]
}
}
}
5 changes: 5 additions & 0 deletions dist/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"captcha.css": "/captcha.css",
"captcha.js": "/captcha.js",
"captchaServiceWorker.js": "/captchaServiceWorker.js"
}

0 comments on commit 7bb5cbc

Please sign in to comment.