npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dodo-captcha

v1.0.0

Published

Simple CAPTCHA based on websocket communication for your backend

Downloads

1

Readme

DodoCaptcha - Simple & customizable self-hosted CAPTCHA

Ready to use text-based CAPTCHA you can implement on your project's backend. Communication between client and server is based on websocket messages so initializing CAPTCHA on your backend will also start the websocket server on port specified in configuration

Installing

npm install dodo-captcha

Examples

Default initialization:

const { DodoCaptcha } = require("dodo-captcha");

const captcha = new DodoCaptcha();
captcha.init();

Custom configuration:

NOTE: Configuration must be set before initialization

// custom websocket server port
captcha.config.port = 8000;

// custom captcha's frame width and generated code position
captcha.config.width = 300;
captcha.config.textPos.x = 150;

// other custom options
captcha.config.[option_name_from_docs] = [value];

Verifying clients:

const express = require("express");
const { DodoCaptcha } = require("dodo-captcha");

const app = express();
app.use(express.json());

const captcha = new DodoCaptcha();
captcha.init();

app.post("/endpoint", (req, res) => {
	const token = req.body.captchaToken;
	if (token) {
		const verified = captcha.verifier.verifyToken(token);
		if (verified) {
			captcha.verifier.removeVerifiedClient(token);
		}
		res.send({status: verified});
	}
});

app.listen(3000);

NOTE: It's recommended to remove the client from the verified list after successful verification because otherwise, they will be removed only when the captcha expires

Client-side

To display the CAPTCHA on the client side you need to initialize a websocket connection with CAPTCHA's backend and handle the messages sent by the backend. You can find a list of all handleable messages along with their descriptions in documentation under "Types" section

Examples (vanilla JS)

Connecting to websocket & handling messages

const messageHandler(evt) => {
	const parsed = JSON.parse(evt.data);
	switch(parsed.type) {
		// handle each message from docs
	}
}
const ws = new WebSocket("ws://localhost:8000");
ws.addEventListener("message", messageHandler);

// some code

ws.removeEventListener("message", messageHandler);

Fetching & displaying CAPTCHA

<html>
	<body>
		<button onclick="fetchCaptcha()">fetch CAPTCHA</button>
		<div id="captcha"></div>
		<script>
		const messageHandler = (evt) => {
			const parsed = JSON.parse(evt.data);
			const captchaDiv = document.getElementById("captcha");
				
			switch (parsed.type) {
				case "captcha.challenge":
					captchaDiv.innerHTML = parsed.params;
					break; 
				// other messages
			}
		}
			
		const ws = new  WebSocket("ws://localhost:1337");
		ws.addEventListener("message", messageHandler);

		const fetchCaptcha = () => {
			ws.send(JSON.stringify({type: "captcha.get.challenge"}));
		}
			
		window.addEventListener("beforeunload", () => {
			ws.removeEventListener("message", messageHandler);
			ws.close();
		})
		</script>
	</body>
</html>

Solving CAPTCHA

<html>
	<body>
		<input id="result" />
		<button onclick="check()">check</button>
		<div id="captcha"></div>
		<script>
		let token;

		const messageHandler = (evt) => {
			const parsed = JSON.parse(evt.data);
			const captchaDiv = document.getElementById("captcha");

			switch (parsed.type) {
				case "captcha.challenge":
					captchaDiv.innerHTML = parsed.params;
					break;
				case "captcha.verified":
					token = parsed.params;
					break;
				// other messages
			}
		}

		const ws = new WebSocket("ws://localhost:1337");

		const check = () => {
			const result = document.getElementById("result");
			ws.send(JSON.stringify({type: "captcha.check.result", params: result.value}));
		}

		ws.addEventListener("message", messageHandler);
		ws.addEventListener("open", () => {
			ws.send(JSON.stringify({type: "captcha.get.challenge"}));
		}, {once: true});

		window.addEventListener("beforeunload", () => {
			ws.removeEventListener("message", messageHandler);
			ws.close();
		})
		</script>
	</body>
</html>

License

MIT