Fixes to widget, demo

This commit is contained in:
Tiago Rangel
2025-01-13 20:21:14 +00:00
parent 59b18a54e7
commit 72a92c7e55
7 changed files with 82 additions and 22 deletions
+32
View File
@@ -0,0 +1,32 @@
const express = require('express');
const Cap = require('@cap.js/server');
const app = express();
app.use(express.json());
app.use(express.static('public'));
const cap = new Cap({
tokens_store_path: '.data/tokensList.json'
});
app.post('/api/challenge', (req, res) => {
res.json(cap.createChallenge({
challengeCount: 5,
challengeSize: 16,
challengeDifficulty: 3,
expiresMs: 300000
}));
});
app.post('/api/redeem', async (req, res) => {
const { token, solutions } = req.body;
if (!token || !solutions) {
return res.status(400).json({ success: false });
}
res.json(await cap.redeemChallenge({ token, solutions }));
});
app.listen(3000, () => {
console.log('Listening on port 3000');
})
+16
View File
@@ -0,0 +1,16 @@
{
"name": "cap-demo",
"version": "0.0.1",
"description": "Cap demo",
"license": "AGPL-3.0",
"author": "Tiago Rangel",
"type": "commonjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@cap.js/server": "^1.0.1",
"express": "^4.21.2"
}
}
+12
View File
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cap demo</title>
</head>
<body>
<cap-widget id="cap" data-api-endpoint="/api/"></cap-widget>
</body>
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
</html>
+1 -1
View File
@@ -20,7 +20,7 @@ Now, you'll need to change your server code to add the routes that Cap needs to
```js
const express = require('express');
const Cap = require('./index.js');
const Cap = require('@cap.js/server');
const app = express();
app.use(express.json());
+1 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@cap.js/widget",
"version": "0.0.4",
"version": "0.0.5",
"description": "Cap widget",
"keywords": [
"captcha",
@@ -24,7 +24,7 @@
"license": "AGPL-3.0",
"author": "Tiago Rangel",
"type": "commonjs",
"main": "cap.js",
"main": "cap.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"npm:publish": "sudo npm publish --access public"
+18 -18
View File
@@ -1,6 +1,24 @@
(function () {
let workerScript;
const until = (predFn, timeout = 10000) => {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("Initialize timeout"));
}, timeout);
const poll = () => {
if (predFn()) {
clearTimeout(timeoutId);
resolve();
} else {
setTimeout(poll, 500);
}
};
poll();
});
};
class CapBase {
#workerUrl = "";
#el = null;
@@ -9,24 +27,6 @@
#token = null;
async initialize() {
const until = (predFn, timeout = 10000) => {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("Initialize timeout"));
}, timeout);
const poll = () => {
if (predFn()) {
clearTimeout(timeoutId);
resolve();
} else {
setTimeout(poll, 500);
}
};
poll();
});
};
if (this.#workerUrl) {
URL.revokeObjectURL(this.#workerUrl);
}