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

colorful-captcha

v0.0.4

Published

Colorful captcha for nodejs

Downloads

279

Readme

colorful-captcha

Traditional graphic verification codes have been considered an unsafe protection method. Modern OCR or AI technology can easily crack various forms of graphic verification codes. This project is not responsible for the security risks brought by the verification code. Please use it with caution.

Why is it called colorful? Because all its noise, obfuscation elements, and verification code text are randomly generated colors, so it looks good. colorful-captcha is a pure JS version of the graphic verification code solution, thanks to skia-canvas

The verification code image is something looks like this:

Install

npm install colorful-captcha

Usage

createCaptchaAsBuffer is the core API exported by colorful-captch. This is an asynchronous function with the following return value type:


// Function signature
createCaptchaAsBuffer(option?: CreateCaptchaOption): Promise<CreateCaptchaReturn>;

// Parameter type
interface CreateCaptchaOption{
  // Verification code width, the default is 240px
  width?: number;
  // Verification code height, the default is 80px
  height?: number;
  // Verification code difficulty, Valid values: "easy" | "normal" | "hard", the default is "normal"
  mode?: DifficultyMode;
  // Spacing between characters, the default is 5px
  spacing?: number;
  // The length of characters, the default is 4
  length: number;
  // Whether characters are allowed to be repeated, the default is false
  noRepeat: boolean;
  // Source of character extraction, excluding 0, o, l, I
  // Default: "123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
  source: string;
}

// Return value type
interface CreateCaptchaReturn {
  // Buffer of verification code image data
  buffer: Buffer;
  // The type of the graphic verification code image, The value is always png.
  type: "png";
  // The text array of the verification code
  chars: string[];
  // The text string of the verification code
  text: string;
  // Width of the verification code image
  width: number;
  // height of the verification code image
  height: number;
  // Verification code image mime type, The value is always image/png
  mime: "image/png",
}

Example:

import { createCaptchaAsBuffer } from "colorful-captcha";
import fs from "node:fs/promises";
import express from "express";

(async () => {
  // Create captcha as buffer, Same as the function call name
  const result = createCaptchaAsBuffer();
  // Create captcha as buffer, With custom parameters
  const result = createCaptchaAsBuffer({
    width: 120,
    height: 80,
    mode: "hard",
    ... // Other parameters of type CreateCaptchaOption
  });

  // Now you can save the buffer into file:
  fs.writeFile("output.png", result.buffer);

  // Or reponse the buffer via http to user's browser
  // Bellow is a example for express.js:
  const app = express();
  app.get("/", function (req, res) {
    res.type(result.type);
    res.send(result.buffer);
  });
})();

Note

The default width and height of the generated graphic verification code image is 240*80. You can customize the width and height through parameters, but if you set the value inappropriately, the text may be cropped. In this case, please try to change a more appropriate ratio yourself