JSPM

  • Created
  • Published
  • Downloads 38
  • Score
    100M100P100Q53236F
  • License AGPL-3.0-only

✚ Easily persist your own autonomous Page View Counter for free (no VPS or database plans required).

Package Exports

  • countty
  • countty/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (countty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Countty ✚

Easily persist your own autonomous Page View Counter for free.

NPM Version NPM Downloads


  • Ready for use via Cloudflare Workers and Durable Objects ⛅️
  • No hosting, domains, VPS or database plans required 💸
  • No need to configure servers or databases ✨
  • Countty can be used both as a plug-in and a self-contained Worker app ⚡️
  • CLI helper to simplify the creation, backup, and maintenance of your counter 🛠️

📦 Create your App

In an empty directory, run:

npx -y countty@latest init
  • This will automatically create all the necessary files and install the development dependencies 💫

[!TIP]

  • You can test it locally, even if you don't have a Cloudflare account.
  • Change your Worker app name using the name property in wrangler.jsonc.
    • Default is countty.

⛅️ Countty Standalone

To use Countty as a self-contained Worker, modify the worker.ts file content to:

import { createCountty } from 'countty';

const { Worker, Countty } = createCountty();

// Worker App
export default Worker;

// Durable Object (SQLite)
export { Countty };

🏁 Run

npm run dev

Example:

[!IMPORTANT]

By default, the token for private routes is "123456" and the URL is "http://localhost:8787". You can change it in the .env file, for example:

COUNTTY_URL='https://countty.<your-subdomain>.workers.dev'
COUNTTY_TOKEN='your-secret-token'

[!TIP]

You can have an .env file for development and another for production.


🔗 API Routes

/create

npm run create <slug>
  • Creates a new counter for the specified slug.
  • Type: private.

/views

npm run views <slug>
  • Increments a view and returns the number of views for the specified slug.
  • Returns 0 when the slug does not exist.
  • Type: public.

/remove

npm run remove <slug>
  • Permanently removes the specified slug.
  • Type: private.

/backup

npm run backup
  • Performs a complete backup of the Countty Durable Object and returns the SQL dump as plain text.
  • Type: private.

/reset

npm run reset
  • ⚠️ Permanently reset the Countty Durable Object.
  • Type: private.

🔐 Production Usage

For production use, you will need a Cloudflare account to proceed.

⛅️ Create your worker

npm run deploy

🔑 Personalize your Token

To safe use your token in production without uploading .env files, you can create a secret:

npx wrangler secret put COUNTTY_TOKEN # Then press `Enter` to insert your token

🛠️ CLI Helper

Countty includes a CLI helper to simplify the creation, backup, and maintenance of your counter directly from the terminal.

[!NOTE]

It's not possible to use custom routes with the Counter CLI.

🧩 Available Commands

  • create <slug>: Create a new counter.
  • views <slug>: View counter statistics.
  • remove <slug>: Remove permanently an existing counter.
  • backup: Backup the Countty Durable Object to ./backups/ directory.
  • reset: Reset all counters permanently.

🕹️ Options

  • --help - Show help message.
  • --env <path> - Specify a custom .env file path.

[!TIP]

The CLI automatically loads environment variables from .env file in your current directory. Use --env flag to specify a different location.


✚ Countty Options

You can customize your Countty using the options available at the time of creation:

import type { CounttyOptions } from 'countty';
import { createCountty } from 'countty';

const options: CounttyOptions = {
  // Specifies the table name in the SQLite Durable Object.
  table: 'countty',
};

const { Worker, Countty } = createCountty(options);

// ...

[!IMPORTANT]

Changing the table name won't migrate data from a previous table.


⚖️ Restrictions on the free plan: