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 🙏

© 2026 – Pkg Stats / Ryan Hefner

r2c-url-shortener

v1.0.5

Published

Quick, Reusable, Reliable, Collision-free URL shortener with Redis and Postgres support

Downloads

24

Readme

🧩 r2c-url-shortener

A lightweight, pluggable Node.js utility for shortening URLs using PostgreSQL, Redis, and SHA-256 hashing. Perfect for building your own short URL service or integrating into existing backend microservices.


🚀 Features

  • 🔗 Generates unique short URLs with collision handling
  • 🧠 Uses Redis for fast lookup caching
  • 🗄 Persists URLs in PostgreSQL
  • ⚙️ Configurable via user-defined options
  • ❌ Handles edge cases with custom error handling

📦 Installation

npm install r2c-url-shortener

🧰 Usage

1. Initialize PostgreSQL and Redis connections:

const { initPostgres, initRedis } = require('r2c-url-shortener');

initPostgres({
  username: 'postgres',
  password: 'your_password',
  host: 'localhost',
  database: 'url-shortener',
  port: 5432,
});

initRedis({
  url: 'redis://localhost:6379',
});

2. Compress a Long URL

const { compress } = require('r2c-url-shortener');

const func = async() => {
  const urlObject = {
    longUrl: "https://www.youtube.com/watch?v=h4i7aRqIeRA&ab_channel=FarawayVillage"
  };
  const options = {
    baseUrl: "your_url", // Your URL e.g. http://abc.com
    maxCollisionAttempts: 50,  // optional
    urlCachingInMinutes: 45    // optional
  };
  const response = await compress(urlObject, options);
  console.log("Compressed URL: ", response.shortUrl);
  // Example output URL: https://abc.com/5f75f76
};

3. Redirect the short URL generated using above compress()

const { redirect } = require('r2c-url-shortener');

const func = async() => {
  await redirect({ shortUrl: "https://{your_domain}/5f75f76" });
};

🗃️ Dependencies

  • pg for PostgreSQL connection
  • redis for in-memory key-value caching
  • uuid and moment for ID & timestamp management
  • Node’s native crypto (WebCrypto API) for hashing

📁 Database Schema

You must have a urls table in PostgreSQL:

CREATE TABLE urls (
  id UUID PRIMARY KEY,
  short_code TEXT NOT NULL UNIQUE,
  long_url TEXT NOT NULL,
  created_at TIMESTAMP NOT NULL
);

🛡 Error Handling

All errors are wrapped in a custom AppError class with statusCode and message. You can handle them in your main app like this:

try {
  await compress(...);
} catch (err) {
  console.error(err.statusCode, err.message);
}

📜 License

MIT © Almonds Technology Corporation 📧 [email protected]

Comments, suggestions and bug reports are welcomed