Skip to content

Commit

Permalink
chore!: Remove match option from rate limit rules (#1815)
Browse files Browse the repository at this point in the history
This removes the `match` option in the SDK so we don't need a request to have a `path`. It is not removed in the Decide service to support older SDKs that still might supply it.

Closes #1810
  • Loading branch information
blaine-arcjet authored Oct 2, 2024
1 parent 39db93e commit 853119d
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 74 deletions.
12 changes: 0 additions & 12 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ const validateTokenBucketOptions = createValidator({
required: false,
validate: validateMode,
},
{ key: "match", required: false, validate: validateString },
{
key: "characteristics",
validate: validateStringArray,
Expand All @@ -380,7 +379,6 @@ const validateFixedWindowOptions = createValidator({
rule: "fixedWindow",
validations: [
{ key: "mode", required: false, validate: validateMode },
{ key: "match", required: false, validate: validateString },
{
key: "characteristics",
validate: validateStringArray,
Expand All @@ -395,7 +393,6 @@ const validateSlidingWindowOptions = createValidator({
rule: "slidingWindow",
validations: [
{ key: "mode", required: false, validate: validateMode },
{ key: "match", required: false, validate: validateString },
{
key: "characteristics",
validate: validateStringArray,
Expand Down Expand Up @@ -447,7 +444,6 @@ const validateShieldOptions = createValidator({

type TokenBucketRateLimitOptions<Characteristics extends readonly string[]> = {
mode?: ArcjetMode;
match?: string;
characteristics?: Characteristics;
refillRate: number;
interval: string | number;
Expand All @@ -456,7 +452,6 @@ type TokenBucketRateLimitOptions<Characteristics extends readonly string[]> = {

type FixedWindowRateLimitOptions<Characteristics extends readonly string[]> = {
mode?: ArcjetMode;
match?: string;
characteristics?: Characteristics;
window: string | number;
max: number;
Expand All @@ -465,7 +460,6 @@ type FixedWindowRateLimitOptions<Characteristics extends readonly string[]> = {
type SlidingWindowRateLimitOptions<Characteristics extends readonly string[]> =
{
mode?: ArcjetMode;
match?: string;
characteristics?: Characteristics;
interval: string | number;
max: number;
Expand Down Expand Up @@ -643,7 +637,6 @@ export function tokenBucket<
validateTokenBucketOptions(options);

const mode = options.mode === "LIVE" ? "LIVE" : "DRY_RUN";
const match = options.match;
const characteristics = options.characteristics;

const refillRate = options.refillRate;
Expand All @@ -655,7 +648,6 @@ export function tokenBucket<
type: "RATE_LIMIT",
priority: Priority.RateLimit,
mode,
match,
characteristics,
algorithm: "TOKEN_BUCKET",
refillRate,
Expand All @@ -673,7 +665,6 @@ export function fixedWindow<
validateFixedWindowOptions(options);

const mode = options.mode === "LIVE" ? "LIVE" : "DRY_RUN";
const match = options.match;
const characteristics = Array.isArray(options.characteristics)
? options.characteristics
: undefined;
Expand All @@ -686,7 +677,6 @@ export function fixedWindow<
type: "RATE_LIMIT",
priority: Priority.RateLimit,
mode,
match,
characteristics,
algorithm: "FIXED_WINDOW",
max,
Expand All @@ -703,7 +693,6 @@ export function slidingWindow<
validateSlidingWindowOptions(options);

const mode = options.mode === "LIVE" ? "LIVE" : "DRY_RUN";
const match = options.match;
const characteristics = Array.isArray(options.characteristics)
? options.characteristics
: undefined;
Expand All @@ -716,7 +705,6 @@ export function slidingWindow<
type: "RATE_LIMIT",
priority: Priority.RateLimit,
mode,
match,
characteristics,
algorithm: "SLIDING_WINDOW",
max,
Expand Down
59 changes: 3 additions & 56 deletions arcjet/test/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,6 @@ describe("Primitive > tokenBucket", () => {
);
});

test("validates `match` option if it is set", async () => {
expect(() => {
tokenBucket({
// @ts-expect-error
match: /foobar/,
refillRate: 1,
interval: 1,
capacity: 1,
});
}).toThrow(
"`tokenBucket` options error: invalid type for `match` - expected string",
);
});

test("validates `characteristics` items are strings if it is set", async () => {
expect(() => {
tokenBucket({
Expand Down Expand Up @@ -741,7 +727,6 @@ describe("Primitive > tokenBucket", () => {
test("sets mode as `LIVE` if specified", async () => {
const [rule] = tokenBucket({
mode: "LIVE",
match: "/test",
characteristics: ["ip.src"],
refillRate: 1,
interval: 1,
Expand Down Expand Up @@ -816,7 +801,6 @@ describe("Primitive > tokenBucket", () => {

test("produces a rules based on configuration specified", async () => {
const options = {
match: "/test",
characteristics: ["ip.src"],
refillRate: 1,
interval: 1,
Expand All @@ -827,15 +811,14 @@ describe("Primitive > tokenBucket", () => {
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", "TOKEN_BUCKET");
expect(rules[0]).toHaveProperty("refillRate", 1);
expect(rules[0]).toHaveProperty("interval", 1);
expect(rules[0]).toHaveProperty("capacity", 1);
});

test("does not default `match` and `characteristics` if not specified", async () => {
test("does not default `characteristics` if not specified", async () => {
const options = {
refillRate: 1,
interval: 1,
Expand All @@ -844,7 +827,6 @@ describe("Primitive > tokenBucket", () => {

const [rule] = tokenBucket(options);
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("match", undefined);
expect(rule).toHaveProperty("characteristics", undefined);
});
});
Expand All @@ -863,19 +845,6 @@ describe("Primitive > fixedWindow", () => {
);
});

test("validates `match` option if it is set", async () => {
expect(() => {
fixedWindow({
// @ts-expect-error
match: /foobar/,
window: "1h",
max: 1,
});
}).toThrow(
"`fixedWindow` options error: invalid type for `match` - expected string",
);
});

test("validates `window` option is required", async () => {
expect(() => {
fixedWindow(
Expand Down Expand Up @@ -925,7 +894,6 @@ describe("Primitive > fixedWindow", () => {
test("sets mode as `LIVE` if specified", async () => {
const [rule] = fixedWindow({
mode: "LIVE",
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
Expand Down Expand Up @@ -990,7 +958,6 @@ describe("Primitive > fixedWindow", () => {

test("produces a rules based on configuration specified", async () => {
const options = {
match: "/test",
characteristics: ["ip.src"],
window: "1h",
max: 1,
Expand All @@ -1000,22 +967,20 @@ describe("Primitive > fixedWindow", () => {
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("does not default `match` and `characteristics` if not specified", async () => {
test("does not default `characteristics` if not specified", async () => {
const options = {
window: "1h",
max: 1,
};

const [rule] = fixedWindow(options);
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("match", undefined);
expect(rule).toHaveProperty("characteristics", undefined);
});
});
Expand All @@ -1034,19 +999,6 @@ describe("Primitive > slidingWindow", () => {
);
});

test("validates `match` option if it is set", async () => {
expect(() => {
slidingWindow({
// @ts-expect-error
match: /foobar/,
interval: 3600,
max: 1,
});
}).toThrow(
"`slidingWindow` options error: invalid type for `match` - expected string",
);
});

test("validates `interval` option is required", async () => {
expect(() => {
slidingWindow(
Expand Down Expand Up @@ -1096,7 +1048,6 @@ describe("Primitive > slidingWindow", () => {
test("sets mode as `LIVE` if specified", async () => {
const [rule] = slidingWindow({
mode: "LIVE",
match: "/test",
characteristics: ["ip.src"],
interval: 3600,
max: 1,
Expand Down Expand Up @@ -1161,7 +1112,6 @@ describe("Primitive > slidingWindow", () => {

test("produces a rules based on configuration specified", async () => {
const options = {
match: "/test",
characteristics: ["ip.src"],
interval: 3600,
max: 1,
Expand All @@ -1171,22 +1121,20 @@ describe("Primitive > slidingWindow", () => {
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", "SLIDING_WINDOW");
expect(rules[0]).toHaveProperty("interval", 3600);
expect(rules[0]).toHaveProperty("max", 1);
});

test("does not default `match` and `characteristics` if not specified", async () => {
test("does not default `characteristics` if not specified", async () => {
const options = {
interval: 3600,
max: 1,
};

const [rule] = slidingWindow(options);
expect(rule.type).toEqual("RATE_LIMIT");
expect(rule).toHaveProperty("match", undefined);
expect(rule).toHaveProperty("characteristics", undefined);
});
});
Expand Down Expand Up @@ -2200,7 +2148,6 @@ describe("Products > protectSignup", () => {
const rules = protectSignup({
rateLimit: {
mode: ArcjetMode.DRY_RUN,
match: "/test",
characteristics: ["ip.src"],
interval: 60 /* minutes */ * 60 /* seconds */,
max: 1,
Expand Down
3 changes: 0 additions & 3 deletions protocol/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ export function ArcjetRuleToProtocol<Props extends { [key: string]: unknown }>(
case: "rateLimit",
value: {
mode: ArcjetModeToProtocol(rule.mode),
match: rule.match,
characteristics: rule.characteristics,
algorithm: RateLimitAlgorithm.TOKEN_BUCKET,
refillRate: rule.refillRate,
Expand All @@ -589,7 +588,6 @@ export function ArcjetRuleToProtocol<Props extends { [key: string]: unknown }>(
case: "rateLimit",
value: {
mode: ArcjetModeToProtocol(rule.mode),
match: rule.match,
characteristics: rule.characteristics,
algorithm: RateLimitAlgorithm.FIXED_WINDOW,
max: rule.max,
Expand All @@ -605,7 +603,6 @@ export function ArcjetRuleToProtocol<Props extends { [key: string]: unknown }>(
case: "rateLimit",
value: {
mode: ArcjetModeToProtocol(rule.mode),
match: rule.match,
characteristics: rule.characteristics,
algorithm: RateLimitAlgorithm.SLIDING_WINDOW,
max: rule.max,
Expand Down
3 changes: 0 additions & 3 deletions protocol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,6 @@ export interface ArcjetTokenBucketRateLimitRule<Props extends {}>
extends ArcjetRateLimitRule<Props> {
algorithm: "TOKEN_BUCKET";

match?: string;
refillRate: number;
interval: number;
capacity: number;
Expand All @@ -731,7 +730,6 @@ export interface ArcjetFixedWindowRateLimitRule<Props extends {}>
extends ArcjetRateLimitRule<Props> {
algorithm: "FIXED_WINDOW";

match?: string;
max: number;
window: number;
}
Expand All @@ -740,7 +738,6 @@ export interface ArcjetSlidingWindowRateLimitRule<Props extends {}>
extends ArcjetRateLimitRule<Props> {
algorithm: "SLIDING_WINDOW";

match?: string;
max: number;
interval: number;
}
Expand Down

0 comments on commit 853119d

Please sign in to comment.