Skip to content

Commit

Permalink
docs(cloudflare): updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MathurAditya724 committed Jun 25, 2024
1 parent ea8aa8c commit b5c2bd9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 22 deletions.
115 changes: 109 additions & 6 deletions packages/cloudflare/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,114 @@
# cloudflare
# <div align="center">🔥`@hono-rate-limiter/cloudflare`🔥</div>

This library was generated with [Nx](https://nx.dev).
<div align="center">

## Building
[![tests](https://img.shields.io/github/actions/workflow/status/rhinobase/hono-rate-limiter/test.yml)](https://github.com/rhinobase/hono-rate-limiter/actions/workflows/test.yml)
[![npm version](https://img.shields.io/npm/v/@hono-rate-limiter/cloudflare.svg)](https://npmjs.org/package/@hono-rate-limiter/cloudflare "View this project on NPM")
[![npm downloads](https://img.shields.io/npm/dm/@hono-rate-limiter/cloudflare)](https://www.npmjs.com/package/@hono-rate-limiter/cloudflare)
[![license](https://img.shields.io/npm/l/@hono-rate-limiter/cloudflare)](LICENSE)

Run `nx build cloudflare` to build the library.
</div>

## Running unit tests
This package include [`WorkersKV`](https://developers.cloudflare.com/kv/) store for the [`hono-rate-limiter`](https://github.com/rhinobase/hono-rate-limiter) middleware and `cloudflareRateLimiter` func for [Workers Rate Limiting](https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/) API.

Run `nx test cloudflare` to execute the unit tests via [Vitest](https://vitest.dev/).
## Installation

```sh
# Using npm/yarn/pnpm/bun
npm add @hono-rate-limiter/cloudflare
```

## Usage

### Examples

#### Using `cloudflareRateLimiter`

```toml
# wrangler.toml
# The rate limiting API is in open beta.
[[unsafe.bindings]]
name = "MY_RATE_LIMITER"
type = "ratelimit"
# An identifier you define, that is unique to your Cloudflare account.
# Must be an integer.
namespace_id = "1001"

# Limit: the number of tokens allowed within a given period in a single
# Cloudflare location
# Period: the duration of the period, in seconds. Must be either 10 or 60
simple = { limit = 100, period = 60 }
```

For more info on setting up your Workers Rate Limiting API you can check out [Rate Limiting Guide](https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/) on cloudflare.

```ts
import { cloudflareRateLimiter, RateLimitBinding } from "@hono-rate-limiter/cloudflare";

type Bindings = {
RATE_LIMITER: RateLimitBinding;
};

// Apply the rate limiting middleware to all requests.
app.use((c: Context, next: Next) =>
rateLimiter({
rateLimitBinding: c.env.RATE_LIMITER,
keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "", // Method to generate custom identifiers for clients.
// store: ... , // Redis, MemoryStore, etc.
})(c, next)
);
```

#### Using `WorkersKVStore`

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

For more info on setting up your `WorkersKV` you can check out [Get Started Guide](https://developers.cloudflare.com/kv/get-started) on cloudflare.

```ts
// index.ts
import { WorkersKVStore } from "@hono-rate-limiter/cloudflare";
import { rateLimiter } from "hono-rate-limiter";
import { Context, Next } from "hono";
import { KVNamespace } from "cloudflare:worker";

// Add this in Hono app
interface Env {
CACHE: KVNamespace;
// ... other binding types
}

// Apply the rate limiting middleware to all requests.
app.use((c: Context, next: Next) =>
rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "", // Method to generate custom identifiers for clients.
store: new WorkersKVStore({ namespace: c.env.CACHE }), // Here CACHE is your WorkersKV Binding.
})(c, next)
);
```

### Configuration

#### `namespace`

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

#### `prefix`

The text to prepend to the key in Redis.

Defaults to `hrl:`.

## Contributing

We would love to have more contributors involved!

To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-rate-limiter/blob/main/CONTRIBUTING.md).
7 changes: 0 additions & 7 deletions packages/cloudflare/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ export class WorkersKVStore implements Store {
*/
prefix: string;

/**
* Whether to reset the expiry for a particular key whenever its hit count
* changes.
*/
resetExpiryOnChange: boolean;

/**
* The KV namespace to use.
*/
Expand All @@ -36,7 +30,6 @@ export class WorkersKVStore implements Store {
constructor(options: Options) {
this.namespace = options.namespace;
this.prefix = options.prefix ?? "hrl:";
this.resetExpiryOnChange = options.resetExpiryOnChange ?? false;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions packages/cloudflare/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export type RateLimitExceededEventHandler<
I extends Input = Input,
> = (c: Context<E, P, I>, next: Next, optionsUsed: ConfigType<E, P, I>) => void;

export type RateLimitBinding = {
limit: (options: { key: string }) => Promise<{ success: boolean }>;
};

/**
* The configuration options for the rate limiter.
*/
Expand Down Expand Up @@ -45,9 +49,7 @@ export interface ConfigType<
/**
* Method to generate custom identifiers for clients.
*/
rateLimitBinding: {
limit: (options: { key: string }) => Promise<{ success: boolean }>;
};
rateLimitBinding: RateLimitBinding;

/**
* Method to generate custom identifiers for clients.
Expand Down Expand Up @@ -84,10 +86,4 @@ export type Options = {
* The text to prepend to the key in Redis.
*/
readonly prefix?: string;

/**
* Whether to reset the expiry for a particular key whenever its hit count
* changes.
*/
readonly resetExpiryOnChange?: boolean;
};

0 comments on commit b5c2bd9

Please sign in to comment.