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

totem-otp-storage-redis

v0.0.4

Published

Redis storage implementation for TotemOTP

Readme

totem-otp-storage-redis

Redis storage implementation for TotemOTP framework.

Installation

npm install totem-otp-storage-redis redis

Usage

import { createClient } from 'redis'
import { RedisOTPStorage } from 'totem-otp-storage-redis'
import { TotemOTP } from 'totem-otp'

// Create Redis client (standalone)
const redisClient = createClient({
  socket: {
    host: 'localhost',
    port: 6379
  }
})

await redisClient.connect()

// Create Redis storage instance
const storage = new RedisOTPStorage(redisClient, {
  keyPrefix: 'my-app-otp' // Optional, defaults to 'totem-otp'
})

// Use with TotemOTP
const totem = new TotemOTP({
  storage: () => storage,
  schemas: [/* your schemas */],
  deliveryAgents: [/* your delivery agents */]
})

Redis Cluster Support

The Redis storage is agnostic to Redis deployment mode and works with both standalone and cluster configurations:

import { createCluster } from 'redis'

// Create Redis cluster client
const redisCluster = createCluster({
  rootNodes: [
    { host: 'localhost', port: 7000 },
    { host: 'localhost', port: 7001 },
    { host: 'localhost', port: 7002 }
  ]
})

await redisCluster.connect()

// Use with Redis storage (same API)
const storage = new RedisOTPStorage(redisCluster)

Features

Core Storage Operations

  • store(otp, parentReference, deletableAt) - Stores OTP with automatic expiration
  • fetch(otpReference) - Retrieves OTP with usage metadata
  • markAsSent(otpReference, receiptId) - Marks OTP as sent with receipt ID
  • markAsUsed(otpReference) - Increments usage count using Redis HINCRBY

Automatic Expiration

The storage utilizes Redis's EXPIRE functionality to automatically handle the deletableAt parameter:

const deletableAt = Date.now() + (30 * 60 * 1000) // 30 minutes from now
await storage.store(otp, null, deletableAt)
// OTP will be automatically deleted by Redis after 30 minutes

Usage Count Management

Uses Redis HINCRBY for atomic increment operations:

const usageCount = await storage.markAsUsed('REF123')
console.log(`OTP used ${usageCount} times`)

Key Management

All Redis keys are prefixed to avoid conflicts:

// Default prefix: 'totem-otp:REF123'
const storage = new RedisOTPStorage(redisClient)

// Custom prefix: 'my-app:REF123'
const storage = new RedisOTPStorage(redisClient, { keyPrefix: 'my-app' })

Storage Schema

Each OTP is stored as a Redis hash with the following fields:

totem-otp:REF123 {
  "target_type": "email",
  "target_value": "[email protected]",
  "target_unique_id": "user-123",
  "otp_value": "123456",
  "expires_at_ms": "1640995200000",
  "resend_allowed_at_ms": "1640995080000",
  "parent_reference": "PARENT123",
  "used": "2",
  "receipt_id": "receipt-abc123",
  "created_at": "1640995000000"
}

Additional Utilities

Check OTP Existence

const exists = await storage.exists('REF123')

Get TTL

const ttlSeconds = await storage.getTTL('REF123')

Delete OTP

await storage.delete('REF123')

List All Keys

const allReferences = await storage.getAllKeys()

Error Handling

The storage handles Redis connection errors gracefully:

try {
  await storage.store(otp, null, deletableAt)
} catch (error) {
  console.error('Redis storage error:', error)
  // Handle connection issues, timeouts, etc.
}

Testing

The package includes comprehensive tests using redis-memory-server:

npm test

Configuration Options

interface RedisOTPStorageOptions {
  /**
   * Optional key prefix for Redis keys
   * @default 'totem-otp'
   */
  keyPrefix?: string
}

Requirements

  • Redis 4.0 or higher
  • Node.js 16 or higher
  • TypeScript 4.5 or higher

License

ISC