Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom rate limit rules payload #6503

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/utils/deploy/hash-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,26 @@ const hashFns = async (
tmpDir,
})
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
({ buildData, displayName, generator, invocationMode, path: functionPath, priority, runtime, runtimeVersion }) => ({
({
// @ts-expect-error TS(7031) FIXME: Binding element 'buildData' implicitly has an 'any... Remove this comment to see the full error message
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone knows a way to apply this to the whole block, please do share 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix is to write some proper types for this :D Let me see what I can do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 8f4a538!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah, thank you so much!

buildData,
// @ts-expect-error TS(7031)
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,
}) => ({
filepath: functionPath,
root: tmpDir,
relname: path.relative(tmpDir, functionPath),
Expand All @@ -152,11 +170,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 +190,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 +219,25 @@ const hashFns = async (
return { functionSchedules, functions, functionsWithNativeModules, fnShaMap, fnConfig }
}

const trafficRulesConfig = (trafficRules: $TSFixMe) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be nice to import the type that we'll expose to users and use it here, but we can do that as a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 8f4a538, i've imported the TrafficRules type from the ZISI internals. Is that what you had in mind? Also, do we consider importing those internals criminal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly! I don't think it's criminal, but I think it would be good to properly export them so we're not relying on internal file paths.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. i'll make the zisi PR and replace the import in a followup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any assertions added. I'm assuming you used this for testing things locally? Fine by me, i'm wondering if there's a good way of asserting that the ratelimit config was actually applied 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I tested it out locally

windowLimit: 60,
windowSize: 50,
aggregateBy: ["ip", "domain"],
}
}
`,
path: '.netlify/functions-internal/func-4.mjs',
})
Expand Down
Loading