Socket
Socket
Sign inDemoInstall

@trpc-limiter/memory

Package Overview
Dependencies
3
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trpc-limiter/memory

Memory store adapter for tRPC limiter.


Version published
Weekly downloads
1.2K
increased by13.5%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@trpc-limiter/memory

Memory store adapter for tRPC limiter.

Install

npm install @trpc-limiter/memory@latest

Usage

import { initTRPC, TRPCError } from '@trpc/server'
import type { IContext } from './context'
import {
  createTRPCStoreLimiter,
  defaultFingerPrint,
} from '@trpc-limiter/memory'

type IContext = {
  req: Request // your request type
}

export const root = initTRPC.context<IContext>().create()

const limiter = createTRPCStoreLimiter<typeof root>({
  fingerprint: (ctx) => defaultFingerPrint(ctx.req),
  windowMs: 20000,
  message: (retryAfter) =>
    `Too many requests, please try again later. ${retryAfter}`,
  max: 15,
  onLimit: (retryAfter, _ctx, fingerprint) => {
    console.log(retryAfter, fingerprint)
    throw new TRPCError({
      code: 'INTERNAL_SERVER_ERROR',
      message: 'Too many requests unique',
    })
  },
})

export const rateLimitedProcedure = root.procedure.use(limiter)
import { initTRPC } from '@trpc/server'
import { type NextApiRequest } from 'next'
import {
  createTRPCStoreLimiter,
  defaultFingerPrint,
} from '@trpc-limiter/memory'

type Context = {
  req: NextApiRequest
}

const t = initTRPC.context<Context>().create()

const rateLimiter = createTRPCStoreLimiter<typeof t>({
  fingerprint: (ctx) => defaultFingerPrint(ctx.req),
  message: (hitInfo) =>
    `Too many requests, please try again later. ${Math.ceil(
      (hitInfo.reset - Date.now()) / 1000
    )}`,
  max: 15,
  windowMs: 10000,
})

export const rateLimitedProcedure = t.procedure.use(rateLimiter)

Keywords

FAQs

Last updated on 16 Apr 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc