-
-
Notifications
You must be signed in to change notification settings - Fork 126
feat(ratelimit): Cloudflare Worker Ratelimit Adapter #1212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ratelimit): Cloudflare Worker Ratelimit Adapter #1212
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughA new Cloudflare Ratelimit Adapter was added to the ratelimit package. Changes include the adapter implementation and unit test, an export added to package.json, and documentation updated to include the Cloudflare adapter and rename the Upstash heading. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CloudflareRatelimiter
participant UnderlyingRatelimit
Client->>CloudflareRatelimiter: limit(key: string)
CloudflareRatelimiter->>UnderlyingRatelimit: limit({ key })
UnderlyingRatelimit-->>CloudflareRatelimiter: Promise<RatelimiterLimitResult>
CloudflareRatelimiter-->>Client: Promise<RatelimiterLimitResult>
Note over CloudflareRatelimiter,UnderlyingRatelimit: Delegation + promise propagation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)packages/ratelimit/src/adapters/cloudflare-ratelimit.ts (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
🔇 Additional comments (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly expands the rate limiting capabilities by introducing a new adapter specifically for Cloudflare Workers. This integration allows applications deployed on Cloudflare to leverage its native rate limiting features seamlessly, providing a robust and efficient solution for managing request traffic directly within the Cloudflare ecosystem. The changes include the adapter's implementation, corresponding documentation, and unit tests. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a new Cloudflare Worker Ratelimit Adapter, including its implementation, tests, and documentation. The changes are well-structured. My review includes a few suggestions to improve the documentation example, refactor the adapter implementation for better readability, and enhance the test suite for clarity and completeness. Overall, this is a great addition.
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/experimental-pino
@orpc/experimental-publisher
@orpc/experimental-ratelimit
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fastify
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new Cloudflare Workers Rate Limit adapter to the @orpc/experimental-ratelimit package, enabling developers to use Cloudflare's native rate limiting bindings in their oRPC applications.
Key Changes:
- Introduces
CloudflareRatelimiterclass as a thin wrapper around Cloudflare Workers Rate Limit API - Adds package exports for the new adapter in both development and production configurations
- Updates documentation with usage examples and integration guidance
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/ratelimit/src/adapters/cloudflare-ratelimit.ts | Implements the CloudflareRatelimiter adapter class as a simple passthrough to Cloudflare's rate limit binding |
| packages/ratelimit/src/adapters/cloudflare-ratelimit.test.ts | Adds basic unit test to verify the adapter correctly forwards limit calls to the underlying Cloudflare rate limiter |
| packages/ratelimit/package.json | Configures package exports to expose the cloudflare-ratelimit adapter as a separate entry point |
| apps/content/docs/helpers/ratelimit.md | Adds documentation section for the Cloudflare adapter with usage example; also updates Upstash section title for consistency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apps/content/docs/helpers/ratelimit.md (1)
106-121: Consider showing actual rate limiting usage in the example.The example demonstrates instantiation but doesn't show how to use the limiter (e.g., calling
limiter.limit('key')). While this follows the pattern of other adapter sections, adding a brief usage snippet would make the example more complete and immediately actionable for users.Example enhancement:
export default { async fetch(request, env) { const limiter = new CloudflareRatelimiter(env.MY_RATE_LIMITER) + + const result = await limiter.limit('user:123') + if (!result.success) { + return new Response('Too many requests', { status: 429 }) + } return new Response(`Hello World!`) } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
apps/content/docs/helpers/ratelimit.md(2 hunks)packages/ratelimit/package.json(1 hunks)packages/ratelimit/src/adapters/cloudflare-ratelimit.test.ts(1 hunks)packages/ratelimit/src/adapters/cloudflare-ratelimit.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
packages/ratelimit/src/adapters/cloudflare-ratelimit.test.ts (1)
packages/ratelimit/src/adapters/cloudflare-ratelimit.ts (1)
CloudflareRatelimiter(3-13)
packages/ratelimit/src/adapters/cloudflare-ratelimit.ts (1)
packages/ratelimit/src/types.ts (2)
Ratelimiter(20-22)RatelimiterLimitResult(1-18)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Agent
- GitHub Check: publish-commit
- GitHub Check: lint
- GitHub Check: test
🔇 Additional comments (4)
apps/content/docs/helpers/ratelimit.md (1)
75-75: Good consistency improvement!Renaming to "Upstash Ratelimit Adapter" aligns with the new "Cloudflare Ratelimit Adapter" heading and improves consistency across adapter sections.
packages/ratelimit/src/adapters/cloudflare-ratelimit.test.ts (1)
3-17: Well-structured test verifying delegation behavior.The test correctly verifies:
- Proper delegation to the underlying ratelimit backend
- Key transformation from string to options object
- Promise pass-through without unnecessary wrapping
- Correct resolution value
packages/ratelimit/package.json (1)
37-42: Package exports correctly configured.The new Cloudflare adapter export follows the exact pattern of existing adapters (memory, redis, upstash-ratelimit) with proper:
- publishConfig.exports for the distributed package
- exports for local development
- Consistent path structure and naming
Also applies to: 49-50
packages/ratelimit/src/adapters/cloudflare-ratelimit.ts (1)
3-13: Implementation verified as compatible with Cloudflare API.The CloudflareRatelimiter correctly adapts the Cloudflare Workers Rate Limit binding. The Cloudflare API's
limitmethod returnsPromise<{ success: boolean }>, which is compatible with theRatelimiterLimitResultinterface wheresuccessis required andlimit,remaining, andresetare optional fields. The adapter properly delegates the key parameter and returns a conforming response.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Summary by CodeRabbit
New Features
Documentation
Tests