@eternaljs/captcha
Version:
This npm package generates secure and customizable CAPTCHA images for web applications, preventing automated abuse with ease.
56 lines (55 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const canvas_1 = require("canvas");
function generateCaptcha(options) {
const canvas = (0, canvas_1.createCanvas)(options['width'], options['height']);
const ctx = canvas.getContext('2d');
const captchaText = generateCaptchaText(options['length'] ?? 6);
ctx.font = `${options['font_size']}px Arial`;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
const textX = options['width'] / 2;
const textY = options['height'] / 2;
ctx.fillText(captchaText, textX, textY);
ctx.beginPath();
ctx.moveTo(textX - ctx.measureText(captchaText).width / 2, textY);
ctx.lineTo(textX + ctx.measureText(captchaText).width / 2, textY);
ctx.strokeStyle = 'rgba(0,0,0)';
ctx.stroke();
const lines = options['lines'] ?? 6;
for (let i = 0; i < lines; i++) {
ctx.beginPath();
ctx.moveTo(0, Math.random() * options['height']);
ctx.lineTo(options['width'], Math.random() * options['height']);
ctx.strokeStyle = 'rgba(0,0,0,0.8)';
ctx.stroke();
}
return { text: captchaText, image: canvas, };
}
function generateCaptchaText(length) {
const charSet = {
digit: '0123456789',
lowercase: 'abcdefghijklmnopqrstuvwxyz',
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
};
let captchaText = '';
for (const type in charSet) {
captchaText += charSet[type].charAt(Math.floor(Math.random() * charSet[type].length));
}
const remaining = length - captchaText.length;
if (!remaining)
return captchaText;
for (let i = 0; i < remaining; i++) {
if (i === 0) {
captchaText += charSet["uppercase"].charAt(Math.floor(Math.random() * charSet["uppercase"].length));
}
else if (i === 1) {
captchaText += charSet["lowercase"].charAt(Math.floor(Math.random() * charSet["lowercase"].length));
}
else {
captchaText += charSet["digit"].charAt(Math.floor(Math.random() * charSet["digit"].length));
}
}
return captchaText.split('').sort(() => Math.random() - 0.5).join('');
}
exports.default = generateCaptcha;