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 🙏

© 2025 – Pkg Stats / Ryan Hefner

perfow-captcha-client

v0.1.0

Published

Client library for interacting with perfow-captcha-server

Readme

perfow-captcha-client

npm version Build Status

A frontend JavaScript/TypeScript library for interacting with a perfow-captcha-client-server (or compatible) instance.

This library handles fetching Proof-of-Work (PoW) challenges, solving them efficiently using Web Workers without blocking the main thread, and verifying the solution with the server.

Features

  • Fetches CAPTCHA challenges from the server.
  • Solves SHA-256 based PoW challenges in a Web Worker.
  • Verifies the found solution (nonce) with the server.
  • Provides a simple Promise-based API.
  • Written in TypeScript, providing type definitions.

Installation

npm install perfow-captcha-client
# or
yarn add perfow-captcha-client
# or
pnpm add perfow-captcha-client

Usage

The easiest way to use the library is via the solveAndVerifyCaptcha convenience function.

import { solveAndVerifyCaptcha } from "perfow-captcha-client";

// Assuming you have an async function context
async function handleFormSubmit() {
  const captchaServerUrl = "http://localhost:8080"; // Your CAPTCHA server URL

  try {
    console.log("Starting CAPTCHA process...");
    const result = await solveAndVerifyCaptcha(captchaServerUrl);

    if (result.success) {
      console.log("CAPTCHA verification successful!");
      // Proceed with form submission or other action
    } else {
      console.error("CAPTCHA verification failed:", result.message);
      // Handle verification failure (e.g., show error to user)
    }
  } catch (error) {
    console.error("An error occurred during the CAPTCHA process:", error);
    // Handle errors (network, solving timeout, etc.)
  }
}

// Example of triggering it
// const submitButton = document.getElementById('submit-button');
// submitButton.addEventListener('click', handleFormSubmit);

Using the Class directly

For more control over the process (e.g., displaying progress), you can use the PerfowCaptchaClient class directly:

import { PerfowCaptchaClient } from "perfow-captcha-client";
// Note: The standalone solveChallenge function is not exported by default,
// but you could modify index.ts to export it if needed for advanced scenarios.

async function solveManually() {
  const captchaServerUrl = "http://localhost:8080";
  const client = new PerfowCaptchaClient(captchaServerUrl);

  try {
    console.log("Fetching challenge...");
    const challengeData = await client.fetchChallenge();
    console.log("Challenge data received:", challengeData);

    // You might want to import and use solveChallenge directly here if exposed
    // Or implement your own worker interaction logic if needed.
    // For this example, we assume solveAndVerifyCaptcha's internal logic
    // which uses the unexported solveChallenge.
    // A practical direct usage might involve getting challenge data
    // then passing it to a UI component that handles solving.

    // Hypothetical example if solveChallenge was exported:
    /*
        import { solveChallenge } from 'perfow-captcha-client';
        console.log('Solving challenge...');
        const solutionNonce = await solveChallenge(challengeData);
        console.log('Solution found:', solutionNonce);

        console.log('Verifying solution...');
        const verificationResult = await client.verifySolution(challengeData, solutionNonce);
        console.log('Verification result:', verificationResult);
        */
    alert(
      "Manual solving example is conceptual. Use solveAndVerifyCaptcha for standard use."
    );
  } catch (error) {
    console.error("Error in manual CAPTCHA flow:", error);
  }
}

Important: CORS Configuration

For this library to work in a browser, the perfow-captcha-client-server must be configured to accept requests from the origin where your frontend application is hosted.

This is done by adding your frontend's origin (e.g., http://localhost:3000, https://yourdomain.com) to the allowed_origins setting in the server's configuration (e.g., .env or config.toml file) and restarting the server.

Failure to configure CORS correctly will result in fetch errors in the browser's console.

API

solveAndVerifyCaptcha(serverBaseUrl: string): Promise<VerificationResult>

  • serverBaseUrl: The base URL of the perfow-captcha-client-server.
  • Returns: A Promise that resolves with a VerificationResult object ({ success: boolean; message?: string }).
  • Throws: An error if fetching, solving, or verification fails for any reason (network, server error, timeout, etc.).

PerfowCaptchaClient

constructor(serverBaseUrl: string)

  • Creates a new client instance.
  • serverBaseUrl: The base URL of the perfow-captcha-client-server.

async fetchChallenge(): Promise<ChallengeData>

  • Fetches a new challenge from the server.
  • Returns: A Promise resolving with ChallengeData ({ salt: string; challenge: string; difficultyBits: number; maxNumber: number; signature: string; expires: number; }).
  • Throws: Error on network or server issues.

async verifySolution(originalChallenge: ChallengeData, solution: number): Promise<VerificationResult>

  • Verifies a found solution with the server.
  • originalChallenge: The ChallengeData object received from fetchChallenge.
  • solution: The nonce (number) found by the solver.
  • Returns: A Promise resolving with VerificationResult.
  • Throws: Error on network or server issues, or if the server returns a non-OK status.

Development

  1. Clone the repository.
  2. Install dependencies: npm install
  3. Build the library: npm run build
  4. Run tests: npm test
  5. Lint code: npm run lint
  6. Format code: npm run format

License

MIT