Skip to content

Commit

Permalink
feat: custom rate limit rules payload
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo committed Apr 8, 2024
1 parent 9b30807 commit 29f5861
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/utils/deploy/hash-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ const hashFns = async (
})
const fileObjs = functionZips.map(
// @ts-expect-error TS(7031) FIXME: Binding element 'buildData' implicitly has an 'any... Remove this comment to see the full error message

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / verify-docs

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / package-size

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 139 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, *)

Unused '@ts-expect-error' directive.
({ buildData, displayName, generator, invocationMode, path: functionPath, priority, runtime, runtimeVersion }) => ({
({
buildData, // @ts-expect-error TS(7031)

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, *)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / verify-docs

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 2/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 1/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 3/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 3/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / package-size

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 2/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 4/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 4/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, *)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 1/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 2/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 3/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, *)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, *)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 4/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 1/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 3/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 2/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, 18.14.0)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 1/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, *)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 4/4)

Binding element 'buildData' implicitly has an 'any' type.

Check failure on line 141 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, *)

Binding element 'buildData' implicitly has an 'any' type.
displayName, // @ts-expect-error TS(7031)
generator, // @ts-expect-error TS(7031)
invocationMode, // @ts-expect-error TS(7031)
path: functionPath, // @ts-expect-error TS(7031)
priority, // @ts-expect-error TS(7031)
runtime, // @ts-expect-error TS(7031)
runtimeVersion, // @ts-expect-error TS(7031)
trafficRules, // @ts-expect-error TS(7031)

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / verify-docs

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / package-size

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, 18.14.0, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (ubuntu-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (ubuntu-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (macOS-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (macOS-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 3/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 2/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E (macOS-latest, 18.14.0)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 1/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / E2E Windows tests (windows-latest, *)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Integration (windows-latest, *, 4/4)

Unused '@ts-expect-error' directive.

Check failure on line 149 in src/utils/deploy/hash-fns.ts

View workflow job for this annotation

GitHub Actions / Unit (windows-latest, *)

Unused '@ts-expect-error' directive.
}) => ({
filepath: functionPath,
root: tmpDir,
relname: path.relative(tmpDir, functionPath),
Expand All @@ -152,11 +162,16 @@ const hashFns = async (
invocationMode,
buildData,
priority,
trafficRules,
}),
)
const fnConfig = functionZips
// @ts-expect-error TS(7006) FIXME: Parameter 'func' implicitly has an 'any' type.
.filter((func) => Boolean(func.displayName || func.generator || func.routes || func.buildData))
.filter((func) =>
Boolean(
func.displayName || func.generator || func.routes || func.buildData || func.priority || func.trafficRules,
),
)
.reduce(
// @ts-expect-error TS(7006) FIXME: Parameter 'funcs' implicitly has an 'any' type.
(funcs, curr) => ({
Expand All @@ -167,6 +182,7 @@ const hashFns = async (
routes: curr.routes,
build_data: curr.buildData,
priority: curr.priority,
traffic_rules: trafficRulesConfig(curr.trafficRules),
},
}),
{},
Expand Down Expand Up @@ -195,4 +211,25 @@ const hashFns = async (
return { functionSchedules, functions, functionsWithNativeModules, fnShaMap, fnConfig }
}

const trafficRulesConfig = (trafficRules: $TSFixMe) => {
if (!trafficRules) {
return
}

return {
action: {
type: trafficRules?.action?.type,
config: {
rate_limit_config: {
algorithm: trafficRules?.action?.config?.rateLimitConfig?.algorithm,
window_size: trafficRules?.action?.config?.rateLimitConfig?.windowSize,
window_limit: trafficRules?.action?.config?.rateLimitConfig?.windowLimit,
},
aggregate: trafficRules?.action?.config?.aggregate,
to: trafficRules?.action?.config?.to,
},
},
}
}

export default hashFns
9 changes: 8 additions & 1 deletion tests/integration/commands/deploy/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,14 @@ describe.skipIf(process.env.NETLIFY_TEST_DISABLE_LIVE === 'true').concurrent('co
.withContentFile({
content: `
export default async () => new Response("Internal V2 API")
export const config = { path: "/internal-v2-func" }
export const config = {
path: "/internal-v2-func",
rateLimit: {
windowLimit: 60,
windowSize: 50,
aggregateBy: ["ip", "domain"],
}
}
`,
path: '.netlify/functions-internal/func-4.mjs',
})
Expand Down

0 comments on commit 29f5861

Please sign in to comment.