Skip to content

Commit

Permalink
chore!: Remove rateLimit alias for fixedWindow rule (#964)
Browse files Browse the repository at this point in the history
Closes #191 

This removes the `rateLimit` alias for the `fixedWindow` rate limit rule. We're doing this before beta to ensure we don't need a migration plan for new (fly.io) users.
  • Loading branch information
blaine-arcjet authored Jun 12, 2024
1 parent 8c15961 commit 320d67c
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 157 deletions.
13 changes: 0 additions & 13 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,19 +472,6 @@ export function fixedWindow<
return rules;
}

// This is currently kept for backwards compatibility but should be removed in
// favor of the fixedWindow primitive.
export function rateLimit<const Characteristics extends readonly string[] = []>(
options?: FixedWindowRateLimitOptions<Characteristics>,
...additionalOptions: FixedWindowRateLimitOptions<Characteristics>[]
): Primitive<
Simplify<UnionToIntersection<PropsForCharacteristic<Characteristics[number]>>>
> {
// TODO(#195): We should also have a local rate limit using an in-memory data
// structure if the environment supports it
return fixedWindow(options, ...additionalOptions);
}

export function slidingWindow<
const Characteristics extends readonly string[] = [],
>(
Expand Down
4 changes: 2 additions & 2 deletions arcjet/test/index.edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@jest/globals";

import arcjet, {
rateLimit,
fixedWindow,
tokenBucket,
protectSignup,
Primitive,
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("Arcjet: Env = Edge runtime", () => {
capacity: 1,
},
),
rateLimit({
fixedWindow({
max: 1,
window: "60s",
}),
Expand Down
142 changes: 0 additions & 142 deletions arcjet/test/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import arcjet, {
ArcjetMode,
detectBot,
rateLimit,
ArcjetRule,
validateEmail,
protectSignup,
Expand Down Expand Up @@ -1401,147 +1400,6 @@ describe("Primitive > slidingWindow", () => {
});
});

// The `rateLimit` primitive just proxies to `fixedWindow` and is available for
// backwards compatibility.
// TODO: Remove these tests once `rateLimit` is removed
describe("Primitive > rateLimit", () => {
test("provides no rules if no `options` specified", () => {
const rules = rateLimit();
expect(rules).toHaveLength(0);
});

test("sets mode as `DRY_RUN` if not 'LIVE' or 'DRY_RUN'", async () => {
const [rule] = rateLimit({
// @ts-expect-error
mode: "INVALID",
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
});
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("mode", "DRY_RUN");
});

test("sets mode as `LIVE` if specified", async () => {
const [rule] = rateLimit({
mode: "LIVE",
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
});
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("mode", "LIVE");
});

test("produces a rules based on single `limit` specified", async () => {
const options = {
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
};

const rules = rateLimit(options);
expect(rules).toHaveLength(1);
expect(rules[0].type).toEqual("RATE_LIMIT");
expect(rules[0]).toHaveProperty("mode", "DRY_RUN");
expect(rules[0]).toHaveProperty("match", "/test");
expect(rules[0]).toHaveProperty("characteristics", ["ip.src"]);
expect(rules[0]).toHaveProperty("algorithm", "FIXED_WINDOW");
expect(rules[0]).toHaveProperty("window", 3600);
expect(rules[0]).toHaveProperty("max", 1);
});

test("produces a multiple rules based on multiple `limit` specified", async () => {
const options = [
{
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
},
{
match: "/test-double",
characteristics: ["ip.src"],
window: "2h",
max: 2,
},
];

const rules = rateLimit(...options);
expect(rules).toHaveLength(2);
expect(rules).toEqual([
expect.objectContaining({
type: "RATE_LIMIT",
mode: "DRY_RUN",
match: "/test",
characteristics: ["ip.src"],
algorithm: "FIXED_WINDOW",
window: 3600,
max: 1,
}),
expect.objectContaining({
type: "RATE_LIMIT",
mode: "DRY_RUN",
match: "/test-double",
characteristics: ["ip.src"],
algorithm: "FIXED_WINDOW",
window: 7200,
max: 2,
}),
]);
});

test("does not default `match` and `characteristics` if not specified in single `limit`", async () => {
const options = {
window: "1h",
max: 1,
};

const [rule] = rateLimit(options);
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("match", undefined);
expect(rule).toHaveProperty("characteristics", undefined);
});

test("does not default `match` or `characteristics` if not specified in array `limit`", async () => {
const options = [
{
window: "1h",
max: 1,
},
{
window: "2h",
max: 2,
},
];

const rules = rateLimit(...options);
expect(rules).toEqual([
expect.objectContaining({
type: "RATE_LIMIT",
mode: "DRY_RUN",
match: undefined,
characteristics: undefined,
algorithm: "FIXED_WINDOW",
window: 3600,
max: 1,
}),
expect.objectContaining({
type: "RATE_LIMIT",
mode: "DRY_RUN",
match: undefined,
characteristics: undefined,
algorithm: "FIXED_WINDOW",
window: 7200,
max: 2,
}),
]);
});
});

describe("Primitive > validateEmail", () => {
test("provides a default rule with no options specified", async () => {
const [rule] = validateEmail();
Expand Down

0 comments on commit 320d67c

Please sign in to comment.