Skip to content

Commit 7bfd4ea

Browse files
Skn0ttbiruwonkodiakhq[bot]
authoredMay 29, 2024··
feat: add timeout config prop for functions (#5683)
Co-authored-by: Antonio Rodríguez <antonio.rodriguez@netlify.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 3aecbfc commit 7bfd4ea

File tree

13 files changed

+165
-3
lines changed

13 files changed

+165
-3
lines changed
 

‎packages/zip-it-and-ship-it/src/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface FunctionConfig {
2222
zipGo?: boolean
2323
name?: string
2424
generator?: string
25+
timeout?: number
2526

2627
// Temporary configuration property, only meant to be used by the deploy
2728
// configuration API. Once we start emitting ESM files for all ESM functions,

‎packages/zip-it-and-ship-it/src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ListedFunction {
2929
schedule?: string
3030
displayName?: string
3131
generator?: string
32+
timeout?: number
3233
inputModuleFormat?: ModuleFormat
3334
}
3435

@@ -151,6 +152,7 @@ const getListedFunction = function ({
151152
displayName: config.name,
152153
extension,
153154
generator: config.generator,
155+
timeout: config.timeout,
154156
mainFile,
155157
name,
156158
runtime: runtime.name,

‎packages/zip-it-and-ship-it/src/manifest.ts

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ interface ManifestFunction {
3838
displayName?: string
3939
bundler?: string
4040
generator?: string
41+
timeout?: number
4142
priority?: number
4243
trafficRules?: TrafficRules
4344
}
@@ -81,11 +82,13 @@ const formatFunctionForManifest = ({
8182
runtimeVersion,
8283
runtimeAPIVersion,
8384
schedule,
85+
timeout,
8486
}: FunctionResult): ManifestFunction => {
8587
const manifestFunction: ManifestFunction = {
8688
bundler,
8789
displayName,
8890
generator,
91+
timeout,
8992
invocationMode,
9093
buildData: { runtimeAPIVersion },
9194
mainFile,

‎packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type ISCValues = {
2525
trafficRules?: TrafficRules
2626
name?: string
2727
generator?: string
28+
timeout?: number
2829
}
2930

3031
export interface StaticAnalysisResult extends ISCValues {
@@ -187,6 +188,10 @@ export const parseSource = (source: string, { functionName }: FindISCDeclaration
187188
result.generator = configExport.generator
188189
}
189190

191+
if (typeof configExport.timeout === 'number') {
192+
result.timeout = configExport.timeout
193+
}
194+
190195
if (configExport.method !== undefined) {
191196
result.methods = normalizeMethods(configExport.method, functionName)
192197
}

‎packages/zip-it-and-ship-it/src/runtimes/node/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ const zipFunction: ZipFunction = async function ({
141141
invocationMode = INVOCATION_MODE.Background
142142
}
143143

144-
const { trafficRules, generator: staticAnalysisGenerator, name: staticAnalysisName } = staticAnalysisResult
144+
const {
145+
trafficRules,
146+
generator: staticAnalysisGenerator,
147+
name: staticAnalysisName,
148+
timeout: staticAnalysisTimeout,
149+
} = staticAnalysisResult
145150

146151
const outputModuleFormat =
147152
extname(finalMainFile) === MODULE_FILE_EXTENSION.MJS ? MODULE_FORMAT.ESM : MODULE_FORMAT.COMMONJS
@@ -154,6 +159,7 @@ const zipFunction: ZipFunction = async function ({
154159
displayName: staticAnalysisName || config?.name,
155160
entryFilename: zipPath.entryFilename,
156161
generator: staticAnalysisGenerator || config?.generator || getInternalValue(isInternal),
162+
timeout: staticAnalysisTimeout || config?.timeout,
157163
inputs,
158164
includedFiles,
159165
staticAnalysisResult,

‎packages/zip-it-and-ship-it/src/runtimes/runtime.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface ZipFunctionResult {
4848
config: FunctionConfig
4949
displayName?: string
5050
generator?: string
51+
timeout?: number
5152
inputs?: string[]
5253
includedFiles?: string[]
5354
invocationMode?: InvocationMode

‎packages/zip-it-and-ship-it/tests/__snapshots__/list_function.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ exports[`listFunction > V2 API > listFunction includes metadata properties when
1212
"runtimeAPIVersion": 2,
1313
"schedule": undefined,
1414
"srcFile": undefined,
15+
"timeout": undefined,
1516
}
1617
`;

‎packages/zip-it-and-ship-it/tests/__snapshots__/list_functions.test.ts.snap

+25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exports[`listFunctions > V2 API > listFunctions includes runtimeAPIVersion when
1212
"runtimeAPIVersion": 2,
1313
"schedule": undefined,
1414
"srcFile": undefined,
15+
"timeout": undefined,
1516
}
1617
`;
1718

@@ -28,6 +29,7 @@ exports[`listFunctions > v1 > Can list function main files from multiple source
2829
"runtimeAPIVersion": undefined,
2930
"schedule": undefined,
3031
"srcFile": undefined,
32+
"timeout": undefined,
3133
},
3234
{
3335
"displayName": undefined,
@@ -40,6 +42,7 @@ exports[`listFunctions > v1 > Can list function main files from multiple source
4042
"runtimeAPIVersion": undefined,
4143
"schedule": undefined,
4244
"srcFile": undefined,
45+
"timeout": undefined,
4346
},
4447
{
4548
"displayName": undefined,
@@ -52,6 +55,7 @@ exports[`listFunctions > v1 > Can list function main files from multiple source
5255
"runtimeAPIVersion": undefined,
5356
"schedule": undefined,
5457
"srcFile": undefined,
58+
"timeout": undefined,
5559
},
5660
{
5761
"displayName": undefined,
@@ -64,6 +68,7 @@ exports[`listFunctions > v1 > Can list function main files from multiple source
6468
"runtimeAPIVersion": undefined,
6569
"schedule": undefined,
6670
"srcFile": undefined,
71+
"timeout": undefined,
6772
},
6873
]
6974
`;
@@ -81,6 +86,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
8186
"runtimeAPIVersion": undefined,
8287
"schedule": undefined,
8388
"srcFile": undefined,
89+
"timeout": undefined,
8490
},
8591
{
8692
"displayName": undefined,
@@ -93,6 +99,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
9399
"runtimeAPIVersion": undefined,
94100
"schedule": undefined,
95101
"srcFile": undefined,
102+
"timeout": undefined,
96103
},
97104
{
98105
"displayName": undefined,
@@ -105,6 +112,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
105112
"runtimeAPIVersion": undefined,
106113
"schedule": undefined,
107114
"srcFile": undefined,
115+
"timeout": undefined,
108116
},
109117
{
110118
"displayName": undefined,
@@ -117,6 +125,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
117125
"runtimeAPIVersion": undefined,
118126
"schedule": undefined,
119127
"srcFile": undefined,
128+
"timeout": undefined,
120129
},
121130
{
122131
"displayName": undefined,
@@ -129,6 +138,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
129138
"runtimeAPIVersion": undefined,
130139
"schedule": undefined,
131140
"srcFile": undefined,
141+
"timeout": undefined,
132142
},
133143
{
134144
"displayName": undefined,
@@ -141,6 +151,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
141151
"runtimeAPIVersion": undefined,
142152
"schedule": undefined,
143153
"srcFile": undefined,
154+
"timeout": undefined,
144155
},
145156
{
146157
"displayName": undefined,
@@ -153,6 +164,7 @@ exports[`listFunctions > v1 > Can list function main files with listFunctions()
153164
"runtimeAPIVersion": undefined,
154165
"schedule": undefined,
155166
"srcFile": undefined,
167+
"timeout": undefined,
156168
},
157169
]
158170
`;
@@ -170,6 +182,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
170182
"runtimeAPIVersion": 1,
171183
"schedule": "@daily",
172184
"srcFile": undefined,
185+
"timeout": undefined,
173186
},
174187
{
175188
"displayName": undefined,
@@ -182,6 +195,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
182195
"runtimeAPIVersion": 1,
183196
"schedule": "@daily",
184197
"srcFile": undefined,
198+
"timeout": undefined,
185199
},
186200
{
187201
"displayName": undefined,
@@ -194,6 +208,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
194208
"runtimeAPIVersion": 1,
195209
"schedule": "@daily",
196210
"srcFile": undefined,
211+
"timeout": undefined,
197212
},
198213
{
199214
"displayName": undefined,
@@ -206,6 +221,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
206221
"runtimeAPIVersion": 1,
207222
"schedule": "@daily",
208223
"srcFile": undefined,
224+
"timeout": undefined,
209225
},
210226
{
211227
"displayName": undefined,
@@ -218,6 +234,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
218234
"runtimeAPIVersion": 1,
219235
"schedule": "@daily",
220236
"srcFile": undefined,
237+
"timeout": undefined,
221238
},
222239
{
223240
"displayName": undefined,
@@ -230,6 +247,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
230247
"runtimeAPIVersion": 1,
231248
"schedule": "@daily",
232249
"srcFile": undefined,
250+
"timeout": undefined,
233251
},
234252
{
235253
"displayName": undefined,
@@ -242,6 +260,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
242260
"runtimeAPIVersion": 1,
243261
"schedule": "@daily",
244262
"srcFile": undefined,
263+
"timeout": undefined,
245264
},
246265
{
247266
"displayName": undefined,
@@ -254,6 +273,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
254273
"runtimeAPIVersion": 1,
255274
"schedule": "@daily",
256275
"srcFile": undefined,
276+
"timeout": undefined,
257277
},
258278
{
259279
"displayName": undefined,
@@ -266,6 +286,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
266286
"runtimeAPIVersion": 1,
267287
"schedule": "@daily",
268288
"srcFile": undefined,
289+
"timeout": undefined,
269290
},
270291
{
271292
"displayName": undefined,
@@ -278,6 +299,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
278299
"runtimeAPIVersion": 1,
279300
"schedule": "@daily",
280301
"srcFile": undefined,
302+
"timeout": undefined,
281303
},
282304
{
283305
"displayName": undefined,
@@ -290,6 +312,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
290312
"runtimeAPIVersion": 1,
291313
"schedule": "@daily",
292314
"srcFile": undefined,
315+
"timeout": undefined,
293316
},
294317
{
295318
"displayName": undefined,
@@ -302,6 +325,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
302325
"runtimeAPIVersion": 1,
303326
"schedule": "@daily",
304327
"srcFile": undefined,
328+
"timeout": undefined,
305329
},
306330
{
307331
"displayName": undefined,
@@ -314,6 +338,7 @@ exports[`listFunctions > v1 > listFunctions includes in-source config declaratio
314338
"runtimeAPIVersion": 1,
315339
"schedule": "@daily",
316340
"srcFile": undefined,
341+
"timeout": undefined,
317342
},
318343
]
319344
`;

‎packages/zip-it-and-ship-it/tests/__snapshots__/list_functions_files.test.ts.snap

+94
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"config": {
33
"name": "should not be respected, ISC takes precedence",
4-
"generator": "should not be respected, ISC takes precedence"
4+
"generator": "should not be respected, ISC takes precedence",
5+
"timeout": 30
56
},
67
"version": 1
78
}

‎packages/zip-it-and-ship-it/tests/fixtures-esm/v2-api-name-generator/.netlify/functions-internal/server.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export default () => new Response('hello world')
33
export const config = {
44
name: 'SSR Function',
55
generator: 'next-runtime@1.2.3',
6+
timeout: 60,
67
}

‎packages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,18 @@ describe('V2 API', () => {
718718
generator: 'bar@1.2.3',
719719
})
720720
})
721+
722+
test('Understands timeout', () => {
723+
const source = `
724+
export default async () => new Response("Hello!")
725+
export const config = { timeout: 60 }`
726+
727+
const isc = parseSource(source, options)
728+
expect(isc).toEqual({
729+
inputModuleFormat: 'esm',
730+
routes: [],
731+
runtimeAPIVersion: 2,
732+
timeout: 60,
733+
})
734+
})
721735
})

‎packages/zip-it-and-ship-it/tests/v2api.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
529529
})
530530
})
531531

532-
test('Name and Generator are taken from ISC and take precedence over deploy config', async () => {
532+
test('Name, Generator and Timeout are taken from ISC and take precedence over deploy config', async () => {
533533
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
534534
const manifestPath = join(tmpDir, 'manifest.json')
535535

@@ -548,7 +548,15 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
548548
})
549549

550550
expect(func.displayName).toBe('SSR Function')
551+
expect(func.timeout).toBe(60)
551552
expect(func.generator).toBe('next-runtime@1.2.3')
553+
554+
const manifestString = await readFile(manifestPath, { encoding: 'utf8' })
555+
const manifest = JSON.parse(manifestString)
556+
expect(manifest.functions).toHaveLength(1)
557+
expect(manifest.functions[0].timeout).toEqual(60)
558+
expect(manifest.functions[0].displayName).toEqual('SSR Function')
559+
expect(manifest.functions[0].generator).toEqual('next-runtime@1.2.3')
552560
})
553561

554562
testMany(

0 commit comments

Comments
 (0)
Please sign in to comment.