Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 6f9c637

Browse files
committed
chore: ratelimit -> rate_limit
1 parent 8979964 commit 6f9c637

File tree

6 files changed

+46
-52
lines changed

6 files changed

+46
-52
lines changed

node/config.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { bundle } from './bundler.js'
1313
import { FunctionConfig, getFunctionConfig } from './config.js'
1414
import type { Declaration } from './declaration.js'
1515
import { ImportMap } from './import_map.js'
16-
import { RatelimitAction, RatelimitAggregator } from './ratelimit.js'
16+
import { RateLimitAction, RateLimitAggregator } from './rate_limit.js'
1717

1818
const importMapFile = {
1919
baseURL: new URL('file:///some/path/import-map.json'),
@@ -129,7 +129,7 @@ const functions: TestFunctions[] = [
129129
rateLimit: {
130130
windowSize: 10,
131131
windowLimit: 100,
132-
aggregateBy: [RatelimitAggregator.IP, RatelimitAggregator.Domain],
132+
aggregateBy: [RateLimitAggregator.IP, RateLimitAggregator.Domain],
133133
},
134134
},
135135
name: 'func9',
@@ -153,11 +153,11 @@ const functions: TestFunctions[] = [
153153
path: '/rewrite',
154154
name: 'a limit rewrite',
155155
rateLimit: {
156-
action: RatelimitAction.Rewrite,
156+
action: RateLimitAction.Rewrite,
157157
to: '/rewritten',
158158
windowSize: 20,
159159
windowLimit: 200,
160-
aggregateBy: [RatelimitAggregator.IP, RatelimitAggregator.Domain],
160+
aggregateBy: [RateLimitAggregator.IP, RateLimitAggregator.Domain],
161161
},
162162
},
163163
name: 'func9',

node/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { EdgeFunction } from './edge_function.js'
1010
import { ImportMap } from './import_map.js'
1111
import { Logger } from './logger.js'
1212
import { getPackagePath } from './package_json.js'
13-
import { Ratelimit } from './ratelimit.js'
13+
import { RateLimit } from './rate_limit.js'
1414

1515
enum ConfigExitCode {
1616
Success = 0,
@@ -47,7 +47,7 @@ export interface FunctionConfig {
4747
name?: string
4848
generator?: string
4949
method?: HTTPMethod | HTTPMethod[]
50-
rateLimit?: Ratelimit
50+
rateLimit?: RateLimit
5151
}
5252

5353
const getConfigExtractor = () => {

node/manifest.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BundleError } from './bundle_error.js'
99
import { Cache, FunctionConfig } from './config.js'
1010
import { Declaration } from './declaration.js'
1111
import { generateManifest } from './manifest.js'
12-
import { RatelimitAction, RatelimitAggregator } from './ratelimit.js'
12+
import { RateLimitAction, RateLimitAggregator } from './rate_limit.js'
1313

1414
test('Generates a manifest with different bundles', () => {
1515
const bundle1 = {
@@ -533,11 +533,11 @@ test('Generates a manifest with rewrite config', () => {
533533
const userFunctionConfig: Record<string, FunctionConfig> = {
534534
'func-1': {
535535
rateLimit: {
536-
action: RatelimitAction.Rewrite,
536+
action: RateLimitAction.Rewrite,
537537
to: '/new_path',
538538
windowLimit: 100,
539539
windowSize: 60,
540-
aggregateBy: [RatelimitAggregator.Domain, RatelimitAggregator.IP],
540+
aggregateBy: [RateLimitAggregator.Domain, RateLimitAggregator.IP],
541541
},
542542
},
543543
}

node/manifest.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import { EdgeFunction } from './edge_function.js'
99
import { FeatureFlags } from './feature_flags.js'
1010
import { Layer } from './layer.js'
1111
import { getPackageVersion } from './package_json.js'
12-
import {
13-
Ratelimit,
14-
RewriteActionConfig,
15-
RatelimitAction,
16-
RatelimitAlgorithm,
17-
RatelimitAggregator,
18-
} from './ratelimit.js'
12+
import { RateLimit, RateLimitAction, RateLimitAlgorithm, RateLimitAggregator } from './rate_limit.js'
1913
import { nonNullable } from './utils/non_nullable.js'
2014
import { ExtendedURLPattern } from './utils/urlpattern.js'
2115

@@ -239,26 +233,26 @@ const generateManifest = ({
239233
return { declarationsWithoutFunction: [...declarationsWithoutFunction], manifest, unroutedFunctions }
240234
}
241235

242-
const getTrafficRulesConfig = (rl: Ratelimit | undefined) => {
236+
const getTrafficRulesConfig = (rl: RateLimit | undefined) => {
243237
if (rl === undefined) {
244238
return
245239
}
246240

247-
const ratelimitAgg = Array.isArray(rl.aggregateBy) ? rl.aggregateBy : [RatelimitAggregator.Domain]
248-
const rewriteConfig = (rl as RewriteActionConfig).to ? { to: (rl as RewriteActionConfig).to } : undefined
241+
const rateLimitAgg = Array.isArray(rl.aggregateBy) ? rl.aggregateBy : [RateLimitAggregator.Domain]
242+
const rewriteConfig = 'to' in rl && typeof rl.to === 'string' ? { to: rl.to } : undefined
249243

250244
return {
251245
action: {
252-
type: rl.action || RatelimitAction.Limit,
246+
type: rl.action || RateLimitAction.Limit,
253247
config: {
254248
...rewriteConfig,
255249
rate_limit_config: {
256250
window_limit: rl.windowLimit,
257251
window_size: rl.windowSize,
258-
algorithm: RatelimitAlgorithm.SlidingWindow,
252+
algorithm: RateLimitAlgorithm.SlidingWindow,
259253
},
260254
aggregate: {
261-
keys: ratelimitAgg.map((agg) => ({ type: agg })),
255+
keys: rateLimitAgg.map((agg) => ({ type: agg })),
262256
},
263257
},
264258
},

node/rate_limit.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export enum RateLimitAlgorithm {
2+
SlidingWindow = 'sliding_window',
3+
}
4+
5+
export enum RateLimitAggregator {
6+
Domain = 'domain',
7+
IP = 'ip',
8+
}
9+
10+
export enum RateLimitAction {
11+
Limit = 'rate_limit',
12+
Rewrite = 'rewrite',
13+
}
14+
15+
interface SlidingWindow {
16+
windowLimit: number
17+
windowSize: number
18+
}
19+
20+
export type RewriteActionConfig = SlidingWindow & {
21+
to: string
22+
}
23+
24+
interface RateLimitConfig {
25+
action?: RateLimitAction
26+
aggregateBy?: RateLimitAggregator | RateLimitAggregator[]
27+
algorithm?: RateLimitAlgorithm
28+
}
29+
30+
export type RateLimit = RateLimitConfig & (SlidingWindow | RewriteActionConfig)

node/ratelimit.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)