upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-vercel-kv.vercel.app
MIT License
1.65k stars 33 forks source link

cf workers: TypeError: Cannot read properties of undefined (reading 'eval') #77

Closed konsumer closed 4 months ago

konsumer commented 7 months ago

I am trying to use the Durable Object state as the store, on a CF worker:

import { Ratelimit } from '@upstash/ratelimit'
import { error } from 'itty-router'

import { getClient } from './graphql/index.js'

export default class StoreDurableObject {
  constructor (state, env) {
    this.state = state
    state.cache ||= new Map()
    this.ratelimit = new Ratelimit({
      limiter: Ratelimit.slidingWindow(10, '10 s'),
      ephemeralCache: state.cache
    })
  }

  async fetch (req, env) {
    const { success } = await this.ratelimit.blockUntilReady(req.headers.get('CF-Connecting-IP'), 10_000)
    if (!success) {
      return error(429, 'Rate-limit exceeded.')
    }

    const graphql = await getClient({ req, env, state: this.state })
    const { query, variables = {} } = await req.json()
    return new Response(JSON.stringify(await graphql(query, variables)))
  }
}

And I get this:

✘ [ERROR] Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'eval')

      at null.<anonymous>
  (file:///Users/konsumer/Desktop/store-ideas/node_modules/@upstash/ratelimit/src/single.ts:258:42)
      at limit
  (file:///Users/konsumer/Desktop/store-ideas/node_modules/@upstash/ratelimit/src/ratelimit.ts:134:55)
      at blockUntilReady
  (file:///Users/konsumer/Desktop/store-ideas/node_modules/@upstash/ratelimit/src/ratelimit.ts:221:24)
      at fetch (file:///Users/konsumer/Desktop/store-ideas/src/StoreDurableObject.js:17:46)

Is there a better way to do this?

konsumer commented 7 months ago

Is this project dead?

ogzhanolguncu commented 7 months ago

@konsumer Nope, it's not. We'll take care of it as soon as possible.

ogzhanolguncu commented 7 months ago

Here is a working gist file for you that utilizes ratelimit, redis and durable object: https://gist.github.com/ogzhanolguncu/53b13094c9b123b2e29c9ac8b0078cd8

Also just to make sure I tested this with both local and deployed version both works fine.

github-actions[bot] commented 6 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

konsumer commented 6 months ago

@ogzhanolguncu I appreciate the example. I was trying to use the rate-limiter without redis (directly using DO storage for the cache.) I ended up moving to durable-limiter, which does what I want.

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 4 months ago

This issue was closed because it has been stalled for 30 days with no activity.