🚨 Latest Research:Tanstack npm Packages Compromised in Ongoing Mini Shai-Hulud Supply-Chain Attack.Learn More →
Socket
Book a DemoSign in
Socket

@trpc-rate-limiter/cloudflare

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trpc-rate-limiter/cloudflare

Cloudflare stores for trpc-rate-limiter.

latest
Source
npmnpm
Version
0.1.3
Version published
Weekly downloads
6
-40%
Maintainers
1
Weekly downloads
 
Created
Source

@trpc-rate-limiter/cloudflare

This package includes WorkersKV and Durable Object store for the @trpc-rate-limiter.

Installation

# Using npm/yarn/pnpm/bun
npm add @trpc-rate-limiter/cloudflare

Usage

Examples

Using WorkersKVStore

# wrangler.toml
[[kv_namespaces]]
binding = "CACHE"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

For more info on setting up your WorkersKV you can check out Get Started Guide on cloudflare.

// index.ts
import { WorkersKVStore } from "@trpc-rate-limiter/cloudflare";
import { trpcRateLimiter } from "@trpc-rate-limiter/hono";
import { KVNamespace } from "cloudflare:worker";

// Add this in Hono app
type Bindings = {
  CACHE: KVNamespace;
  // ... other binding types
};

const app = new Hono<{ Bindings: Bindings }>();

const t = initTRPC.context<TRPCContext>().create();

// Apply the rate limiter to the tRPC middleware
const rateLimiterMiddleware = t.middleware(async ({ ctx, next }) => {
  const { c } = ctx;

  await trpcRateLimiter<AppRouter>({
    config: {
      "auth.signIn": {
        windowMs: 5 * 60 * 1000,
        limit: 5,
      },
      default: { windowMs: 60 * 1000, limit: 5 },
    },
    store: new WorkersKVStore({ namespace: c.env.CACHE }), // Here CACHE is your WorkersKV Binding.
  })(c);

  return next();
});

Using DurableObjectStore

# wrangler.toml
[[durable_objects.bindings]]
name = "CACHE"
class_name = "DurableObjectRateLimiter"

[[migrations]]
tag = "v1" # Should be unique for each entry
new_classes = ["DurableObjectRateLimiter"]

For more info on setting up your Durable Objects you can check out Get Started Guide on cloudflare.

// index.ts
import { DurableObjectStore, DurableObjectRateLimiter } from "@trpc-rate-limiter/cloudflare";
import { trpcRateLimiter } from "@trpc-rate-limiter/hono";
import { Context, Next } from "hono";
import { DurableObjectNamespace } from "cloudflare:worker";

// Add this in Hono app
type Bindings = {
  CACHE: DurableObjectNamespace<DurableObjectRateLimiter>;
  // ... other binding types
};

const app = new Hono<{ Bindings: Bindings }>();

const t = initTRPC.context<TRPCContext>().create();

// Apply the rate limiter to the tRPC middleware
const rateLimiterMiddleware = t.middleware(async ({ ctx, next }) => {
  const { c } = ctx;

  await trpcRateLimiter<AppRouter>({
    config: {
      "auth.signIn": {
        windowMs: 5 * 60 * 1000,
        limit: 5,
      },
      default: { windowMs: 60 * 1000, limit: 5 },
    },
    store: new DurableObjectStore({ namespace: c.env.CACHE }), // Here CACHE is your Durable Object Binding.
  })(c);

  return next();
});

// Export DurableObjectRateLimiter in the index file of your Worker
export { DurableObjectRateLimiter };

export default app;

Configuration Props of WorkersKVStore and DurableObjectStore

namespace

The KV / Durable Object namespace to use. The value you set for <BINDING_NAME> will be used to reference this database / durable object in your Worker.

prefix

The text to prepend to the key in the KV / Durable Object namespace.

Defaults to hrl:.

Credits

The @trpc-rate-limiter/cloudflare is based on the @hono-rate-limiter/cloudflare store.

Keywords

trpc

FAQs

Package last updated on 12 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts