@upstash/ratelimit
Advanced tools
Comparing version 0.1.2 to 0.1.3-rc.0
@@ -1,2 +0,2 @@ | ||
export { RegionRatelimit as Ratelimit } from "./region.js"; | ||
export { GlobalRatelimit } from "./global.js"; | ||
export { RegionRatelimit as Ratelimit } from "./single.js"; | ||
export { MultiRegionRatelimit } from "./multi.js"; |
@@ -95,3 +95,3 @@ /** | ||
* | ||
* If you want to globally limit your api, you can set a constant string. | ||
* If you want to limit your api across all users, you can set a constant string. | ||
*/ | ||
@@ -98,0 +98,0 @@ identifier, |
@@ -6,3 +6,3 @@ { | ||
"name": "@upstash/ratelimit", | ||
"version": "v0.1.2", | ||
"version": "v0.1.3-rc.0", | ||
"description": "A serverless ratelimiter built on top of Upstash REST API.", | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
# Upstash Redis | ||
# Upstash Ratelimit | ||
@@ -158,11 +158,11 @@ An HTTP/REST based Redis client built on top of Upstash REST API. | ||
## Globally replicated ratelimiting | ||
## MultiRegionly replicated ratelimiting | ||
Using a single redis instance has the downside of providing low latencies to the | ||
part of your userbase closest to the deployed db. That's why we also built | ||
`GlobalRatelimit` which replicates the state across multiple redis databases as | ||
well as offering lower latencies to more of your users. | ||
`MultiRegionRatelimit` which replicates the state across multiple redis | ||
databases as well as offering lower latencies to more of your users. | ||
`GlobalRatelimit` does this by checking the current limit in the closest db and | ||
returning immediately. Only afterwards will the state be asynchronously | ||
`MultiRegionRatelimit` does this by checking the current limit in the closest db | ||
and returning immediately. Only afterwards will the state be asynchronously | ||
replicated to the other datbases leveraging | ||
@@ -179,7 +179,7 @@ [CRDTs](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type). Due | ||
```ts | ||
import { GlobalRatelimit } from "@upstash/ratelimit"; // for deno: see above | ||
import { MultiRegionRatelimit } from "@upstash/ratelimit"; // for deno: see above | ||
import { Redis } from "@upstash/redis"; | ||
// Create a new ratelimiter, that allows 10 requests per 10 seconds | ||
const ratelimit = new GlobalRatelimit({ | ||
const ratelimit = new MultiRegionRatelimit({ | ||
redis: [ | ||
@@ -280,3 +280,3 @@ new Redis({/* auth */}), | ||
_Not yet supported for `GlobalRatelimit`_ | ||
_Not yet supported for `MultiRegionRatelimit`_ | ||
@@ -283,0 +283,0 @@ Consider a bucket filled with `{maxTokens}` tokens that refills constantly at |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GlobalRatelimit = exports.Ratelimit = void 0; | ||
var region_js_1 = require("./region.js"); | ||
Object.defineProperty(exports, "Ratelimit", { enumerable: true, get: function () { return region_js_1.RegionRatelimit; } }); | ||
var global_js_1 = require("./global.js"); | ||
Object.defineProperty(exports, "GlobalRatelimit", { enumerable: true, get: function () { return global_js_1.GlobalRatelimit; } }); | ||
exports.MultiRegionRatelimit = exports.Ratelimit = void 0; | ||
var single_js_1 = require("./single.js"); | ||
Object.defineProperty(exports, "Ratelimit", { enumerable: true, get: function () { return single_js_1.RegionRatelimit; } }); | ||
var multi_js_1 = require("./multi.js"); | ||
Object.defineProperty(exports, "MultiRegionRatelimit", { enumerable: true, get: function () { return multi_js_1.MultiRegionRatelimit; } }); |
@@ -98,3 +98,3 @@ "use strict"; | ||
* | ||
* If you want to globally limit your api, you can set a constant string. | ||
* If you want to limit your api across all users, you can set a constant string. | ||
*/ | ||
@@ -101,0 +101,0 @@ identifier, |
@@ -1,5 +0,5 @@ | ||
export { RegionRatelimit as Ratelimit } from "./region.js"; | ||
export type { RegionRatelimitConfig as RatelimitConfig } from "./region.js"; | ||
export { GlobalRatelimit } from "./global.js"; | ||
export type { GlobalRatelimitConfig } from "./global.js"; | ||
export { RegionRatelimit as Ratelimit } from "./single.js"; | ||
export type { RegionRatelimitConfig as RatelimitConfig } from "./single.js"; | ||
export { MultiRegionRatelimit } from "./multi.js"; | ||
export type { MultiRegionRatelimitConfig } from "./multi.js"; | ||
export type { Algorithm } from "./types.js"; |
@@ -8,6 +8,6 @@ export interface Redis { | ||
}; | ||
export declare type GlobalContext = { | ||
export declare type MultiRegionContext = { | ||
redis: Redis[]; | ||
}; | ||
export declare type Context = RegionContext | GlobalContext; | ||
export declare type Context = RegionContext | MultiRegionContext; | ||
export declare type RatelimitResponse = { | ||
@@ -30,3 +30,26 @@ /** | ||
reset: number; | ||
/** | ||
* For the MultiRegion setup we do some synchronizing in the background, after returning the current limit. | ||
* In most case you can simply ignore this. | ||
* | ||
* On Vercel Edge or Cloudflare workers, you need to explicitely handle the pending Promise like this: | ||
* | ||
* **Vercel Edge:** | ||
* https://nextjs.org/docs/api-reference/next/server#nextfetchevent | ||
* | ||
* ```ts | ||
* const { pending } = await ratelimit.limit("id") | ||
* event.waitUntil(pending) | ||
* ``` | ||
* | ||
* **Cloudflare Worker:** | ||
* https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker | ||
* | ||
* ```ts | ||
* const { pending } = await ratelimit.limit("id") | ||
* context.waitUntil(pending) | ||
* ``` | ||
*/ | ||
pending: Promise<unknown>; | ||
}; | ||
export declare type Algorithm<TContext> = (ctx: TContext, identifier: string) => Promise<RatelimitResponse>; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
68235
1601
2