Skip to content

Commit

Permalink
feat: custom rate limit rules payload (#6503)
Browse files Browse the repository at this point in the history
* feat: custom rate limit rules payload

* fix: types

* chore: move function before usage

* chore: prettier

* chore: remove unused directive

---------

Co-authored-by: Simon Knott <info@simonknott.de>
  • Loading branch information
paulo and Skn0tt committed Apr 24, 2024
1 parent 31114d9 commit 79e22fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/utils/deploy/deploy-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export const deploySite = async (
}

if (functionsWithNativeModules.length !== 0) {
// @ts-expect-error TS(7031) FIXME: Binding element 'name' implicitly has an 'any' typ... Remove this comment to see the full error message
const functionsWithNativeModulesMessage = functionsWithNativeModules.map(({ name }) => `- ${name}`).join('\n')
warn(`Modules with native dependencies\n
${functionsWithNativeModulesMessage}
Expand Down
50 changes: 42 additions & 8 deletions src/utils/deploy/hash-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path'
import { promisify } from 'util'

import { zipFunctions } from '@netlify/zip-it-and-ship-it'
import type { TrafficRules } from '@netlify/zip-it-and-ship-it/dist/manifest.js'
import type { FunctionResult } from '@netlify/zip-it-and-ship-it/dist/utils/format_result.js'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'from... Remove this comment to see the full error message
import fromArray from 'from2-array'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'pump... Remove this comment to see the full error message
Expand Down Expand Up @@ -37,7 +39,7 @@ const getFunctionZips = async ({
skipFunctionsCache: $TSFixMe
statusCb: $TSFixMe
tmpDir: $TSFixMe
}) => {
}): Promise<(FunctionResult & { buildData?: unknown })[]> => {
statusCb({
type: 'functions-manifest',
msg: 'Looking for a functions cache...',
Expand Down Expand Up @@ -88,6 +90,27 @@ const getFunctionZips = async ({
})
}

const trafficRulesConfig = (trafficRules?: TrafficRules) => {
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,
},
},
}
}

const hashFns = async (
command: BaseCommand,
directories: string[],
Expand Down Expand Up @@ -136,8 +159,17 @@ 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 }) => ({
({
buildData,
displayName,
generator,
invocationMode,
path: functionPath,
priority,
runtime,
runtimeVersion,
trafficRules,
}) => ({
filepath: functionPath,
root: tmpDir,
relname: path.relative(tmpDir, functionPath),
Expand All @@ -152,13 +184,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) => ({
...funcs,
[curr.name]: {
Expand All @@ -167,16 +202,15 @@ const hashFns = async (
routes: curr.routes,
build_data: curr.buildData,
priority: curr.priority,
traffic_rules: trafficRulesConfig(curr.trafficRules),
},
}),
{},
)
const functionSchedules = functionZips
// @ts-expect-error TS(7031) FIXME: Binding element 'name' implicitly has an 'any' typ... Remove this comment to see the full error message
.map(({ name, schedule }) => schedule && { name, cron: schedule })
.filter(Boolean)
const functionsWithNativeModules = functionZips.filter(
// @ts-expect-error TS(7031) FIXME: Binding element 'nativeNodeModules' implicitly has... Remove this comment to see the full error message
({ nativeNodeModules }) => nativeNodeModules !== undefined && Object.keys(nativeNodeModules).length !== 0,
)

Expand Down
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

1 comment on commit 79e22fd

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Dependency count: 1,345
  • Package size: 313 MB
  • Number of ts-expect-error directives: 1,001

Please sign in to comment.