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

@builtwithai/serverless-redis-vercel

v1.0.0

Published

Vercel Edge Functions integration for Serverless Redis Client

Downloads

8

Readme

@scaler/serverless-redis-vercel

Vercel Edge Functions integration for the Serverless Redis Client, optimized for Vercel's edge runtime and deployment model.

Installation

npm install @scaler/serverless-redis-vercel

Quick Start

Edge Functions

// api/redis-example.ts
import { withRedis } from '@scaler/serverless-redis-vercel';

export default withRedis(async (redis, request) => {
  const url = new URL(request.url);
  const key = url.searchParams.get('key') || 'default';
  
  if (request.method === 'GET') {
    const value = await redis.get(key);
    return { key, value };
  }
  
  if (request.method === 'POST') {
    const { value } = await request.json();
    await redis.set(key, value);
    return { success: true, key, value };
  }
  
  return new Response('Method not allowed', { status: 405 });
});

export const config = {
  runtime: 'edge',
};

API Routes (Node.js)

// api/users/[id].ts
import { getServerlessRedis } from '@scaler/serverless-redis-vercel';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const redis = getServerlessRedis();
  const { id } = req.query;
  
  if (req.method === 'GET') {
    const user = await redis.hgetall(`user:${id}`);
    res.json(user);
  } else if (req.method === 'PUT') {
    await redis.hset(`user:${id}`, ...Object.entries(req.body).flat());
    res.json({ success: true });
  }
}

Features

  • 🚀 Edge Runtime Optimized - Works seamlessly in Vercel Edge Functions
  • Auto-Configuration - Detects runtime and applies optimal settings
  • 🌍 Environment Detection - Automatically configures for production/preview/development
  • 🔧 TypeScript First - Full type safety for Vercel environments
  • 📦 Zero Config - Works with standard Vercel environment variables
  • 🛡️ CORS Helpers - Built-in CORS handling utilities

Configuration

Environment Variables

Set these in your Vercel project settings or .env.local:

REDIS_PROXY_URL=https://your-redis-proxy.example.com
REDIS_TOKEN=your-api-key-or-jwt
REDIS_TIMEOUT=3000  # Optional: timeout in ms (default: 3000 for edge, 5000 for nodejs)
REDIS_RETRIES=1     # Optional: retry attempts (default: 1 for edge, 3 for nodejs)
REDIS_DB=0          # Optional: Redis database number

Programmatic Configuration

import { createServerlessRedis, VercelRedisUtils } from '@scaler/serverless-redis-vercel';

const redis = createServerlessRedis({
  url: 'https://your-proxy.example.com',
  token: 'your-api-key',
  timeout: 3000,
  retries: 1,
  compression: true,
  headers: {
    'X-Custom-Header': 'value'
  }
});

// Or use optimized configuration for current environment
const optimizedRedis = createServerlessRedis(
  VercelRedisUtils.getOptimizedConfig({
    url: process.env.REDIS_PROXY_URL,
    token: process.env.REDIS_TOKEN,
  })
);

API Reference

Functions

createServerlessRedis(config?)

Creates a new Redis client instance optimized for Vercel.

const redis = createServerlessRedis({
  url: 'https://your-proxy.example.com',
  token: 'your-api-key',
  timeout: 3000,    // Shorter timeout for edge functions
  retries: 1,       // Fewer retries for edge functions
  compression: true // Enable compression
});

getServerlessRedis(config?)

Gets or creates a global Redis client instance (recommended for performance).

const redis = getServerlessRedis();

withRedis(handler)

Wraps an Edge Function handler with Redis client injection and error handling.

export default withRedis(async (redis, request) => {
  const data = await redis.get('key');
  return { data };
});

createRedisMiddleware(config?)

Creates middleware that adds Redis client to request objects.

const { redis, middleware } = createRedisMiddleware();

Utilities

VercelRedisUtils

Utility functions for Vercel-specific functionality:

import { VercelRedisUtils } from '@scaler/serverless-redis-vercel';

// Runtime detection
VercelRedisUtils.isEdgeRuntime();    // true if running in Edge Runtime
VercelRedisUtils.isNodeRuntime();    // true if running in Node.js runtime

// Environment detection
VercelRedisUtils.isProduction();     // true if VERCEL_ENV=production
VercelRedisUtils.isPreview();        // true if VERCEL_ENV=preview
VercelRedisUtils.isDevelopment();    // true if VERCEL_ENV=development

// Get current Vercel URL
const url = VercelRedisUtils.getVercelUrl();

// Get optimized config for current environment
const config = VercelRedisUtils.getOptimizedConfig({
  url: process.env.REDIS_PROXY_URL,
  token: process.env.REDIS_TOKEN,
});

// Create config from environment variables
const envConfig = VercelRedisUtils.createConfigFromEnvironment();

CORS Helpers

import { corsResponse, handleCors } from '@scaler/serverless-redis-vercel';

export default async function handler(request: Request) {
  // Handle CORS preflight
  const corsResult = handleCors(request);
  if (corsResult) return corsResult;
  
  // Your logic here
  const data = { message: 'Hello' };
  
  // Return CORS-enabled response
  return corsResponse(data);
}

Examples

User Session Management

// api/session.ts
import { withRedis } from '@scaler/serverless-redis-vercel';

export default withRedis(async (redis, request) => {
  const { searchParams } = new URL(request.url);
  const sessionId = searchParams.get('sessionId');
  
  if (!sessionId) {
    return new Response('Session ID required', { status: 400 });
  }
  
  if (request.method === 'GET') {
    const sessionData = await redis.hgetall(`session:${sessionId}`);
    return { sessionData };
  }
  
  if (request.method === 'POST') {
    const data = await request.json();
    await redis.hset(`session:${sessionId}`, ...Object.entries(data).flat());
    await redis.expire(`session:${sessionId}`, 3600); // 1 hour TTL
    return { success: true };
  }
  
  if (request.method === 'DELETE') {
    await redis.del(`session:${sessionId}`);
    return { success: true };
  }
});

export const config = { runtime: 'edge' };

Rate Limiting

// api/rate-limit.ts
import { withRedis } from '@scaler/serverless-redis-vercel';

export default withRedis(async (redis, request) => {
  const clientIP = request.headers.get('x-forwarded-for') || 'unknown';
  const key = `rate_limit:${clientIP}`;
  
  const current = await redis.incr(key);
  
  if (current === 1) {
    await redis.expire(key, 60); // 1 minute window
  }
  
  if (current > 10) { // 10 requests per minute
    return new Response('Rate limit exceeded', { 
      status: 429,
      headers: {
        'Retry-After': '60'
      }
    });
  }
  
  return {
    message: 'API call successful',
    remaining: 10 - current,
    resetIn: await redis.ttl(key)
  };
});

export const config = { runtime: 'edge' };

Cached API Proxy

// api/proxy/[...path].ts
import { withRedis } from '@scaler/serverless-redis-vercel';

export default withRedis(async (redis, request) => {
  const url = new URL(request.url);
  const path = url.pathname.replace('/api/proxy/', '');
  const cacheKey = `proxy:${path}:${url.search}`;
  
  // Try cache first
  const cached = await redis.get(cacheKey);
  if (cached) {
    return JSON.parse(cached);
  }
  
  // Fetch from upstream API
  const response = await fetch(`https://api.example.com/${path}${url.search}`);
  const data = await response.json();
  
  // Cache for 5 minutes
  await redis.setex(cacheKey, 300, JSON.stringify(data));
  
  return data;
});

export const config = { runtime: 'edge' };

Runtime Optimization

The package automatically optimizes settings based on the Vercel runtime:

Edge Runtime

  • Shorter timeouts (3000ms default)
  • Fewer retries (1 default)
  • Compression enabled
  • Minimal memory footprint

Node.js Runtime

  • Standard timeouts (5000ms default)
  • Standard retries (3 default)
  • Full feature set available

Deployment

  1. Install the package:

    npm install @scaler/serverless-redis-vercel
  2. Set environment variables in Vercel dashboard:

    • REDIS_PROXY_URL
    • REDIS_TOKEN
  3. Deploy your functions:

    vercel deploy

The package handles the rest automatically!

License

MIT License - see LICENSE for details.