cf workers: TypeError: Cannot read properties of undefined (reading 'eval')
Closed this issue ยท 7 comments
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?
Is this project dead?
@konsumer Nope, it's not. We'll take care of it as soon as possible.
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.
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.
@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.
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.
This issue was closed because it has been stalled for 30 days with no activity.