@@ -16,6 +16,9 @@ import {
16
16
HANDLER_FUNCTION_TITLE ,
17
17
ODB_FUNCTION_TITLE ,
18
18
IMAGE_FUNCTION_TITLE ,
19
+ API_FUNCTION_TITLE ,
20
+ API_FUNCTION_NAME ,
21
+ LAMBDA_WARNING_SIZE ,
19
22
} from '../constants'
20
23
import { getApiHandler } from '../templates/getApiHandler'
21
24
import { getHandler } from '../templates/getHandler'
@@ -31,6 +34,7 @@ import { getFunctionNameForPage } from './utils'
31
34
32
35
export interface ApiRouteConfig {
33
36
functionName : string
37
+ functionTitle ?: string
34
38
route : string
35
39
config : ApiConfig
36
40
compiled : string
@@ -39,6 +43,7 @@ export interface ApiRouteConfig {
39
43
40
44
export interface APILambda {
41
45
functionName : string
46
+ functionTitle : string
42
47
routes : ApiRouteConfig [ ]
43
48
includedFiles : string [ ]
44
49
type ?: ApiRouteType
@@ -60,7 +65,7 @@ export const generateFunctions = async (
60
65
: undefined
61
66
62
67
for ( const apiLambda of apiLambdas ) {
63
- const { functionName, routes, type, includedFiles } = apiLambda
68
+ const { functionName, functionTitle , routes, type, includedFiles } = apiLambda
64
69
65
70
const apiHandlerSource = getApiHandler ( {
66
71
// most api lambdas serve multiple routes, but scheduled functions need to be in separate lambdas.
@@ -102,6 +107,8 @@ export const generateFunctions = async (
102
107
} )
103
108
await writeFile ( join ( functionsDir , functionName , 'pages.js' ) , resolverSource )
104
109
110
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
111
+
105
112
const nfInternalFiles = await glob ( join ( functionsDir , functionName , '**' ) )
106
113
includedFiles . push ( ...nfInternalFiles )
107
114
}
@@ -128,7 +135,7 @@ export const generateFunctions = async (
128
135
join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
129
136
join ( functionsDir , functionName , 'handlerUtils.js' ) ,
130
137
)
131
- writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
138
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
132
139
}
133
140
134
141
await writeHandler ( HANDLER_FUNCTION_NAME , HANDLER_FUNCTION_TITLE , false )
@@ -314,16 +321,14 @@ const getBundleWeight = async (patterns: string[]) => {
314
321
return sum ( sizes . flat ( 1 ) )
315
322
}
316
323
317
- const MB = 1024 * 1024
318
-
319
324
export const getAPILambdas = async (
320
325
publish : string ,
321
326
baseDir : string ,
322
327
pageExtensions : string [ ] ,
323
328
) : Promise < APILambda [ ] > => {
324
329
const commonDependencies = await getAPIPRouteCommonDependencies ( publish )
325
330
326
- const threshold = 50 * MB - ( await getBundleWeight ( commonDependencies ) )
331
+ const threshold = LAMBDA_WARNING_SIZE - ( await getBundleWeight ( commonDependencies ) )
327
332
328
333
const apiRoutes = await getApiRouteConfigs ( publish , baseDir , pageExtensions )
329
334
@@ -334,12 +339,41 @@ export const getAPILambdas = async (
334
339
335
340
const bins = pack ( weighedRoutes , threshold )
336
341
337
- return bins . map ( ( bin , index ) => ( {
338
- functionName : bin . length === 1 ? bin [ 0 ] . functionName : `api-${ index } ` ,
339
- routes : bin ,
340
- includedFiles : [ ...commonDependencies , ...routes . flatMap ( ( route ) => route . includedFiles ) ] ,
341
- type,
342
- } ) )
342
+ return bins . map ( ( bin ) => {
343
+ if ( bin . length === 1 ) {
344
+ const [ func ] = bin
345
+ const { functionName, functionTitle, config, includedFiles } = func
346
+ return {
347
+ functionName,
348
+ functionTitle,
349
+ routes : [ func ] ,
350
+ includedFiles : [ ...commonDependencies , ...includedFiles ] ,
351
+ type : config . type ,
352
+ }
353
+ }
354
+
355
+ const includedFiles = [ ...commonDependencies , ...bin . flatMap ( ( route ) => route . includedFiles ) ]
356
+ const nonSingletonBins = bins . filter ( ( b ) => b . length > 1 )
357
+ if ( nonSingletonBins . length === 1 ) {
358
+ return {
359
+ functionName : API_FUNCTION_NAME ,
360
+ functionTitle : API_FUNCTION_TITLE ,
361
+ includedFiles,
362
+ routes : bin ,
363
+ type,
364
+ }
365
+ }
366
+
367
+ const indexInNonSingletonBins = nonSingletonBins . indexOf ( bin )
368
+
369
+ return {
370
+ functionName : `${ API_FUNCTION_NAME } -${ indexInNonSingletonBins + 1 } ` ,
371
+ functionTitle : `${ API_FUNCTION_TITLE } ${ indexInNonSingletonBins + 1 } /${ nonSingletonBins . length } ` ,
372
+ includedFiles,
373
+ routes : bin ,
374
+ type,
375
+ }
376
+ } )
343
377
}
344
378
345
379
const standardFunctions = apiRoutes . filter (
@@ -366,7 +400,7 @@ export const getAPILambdas = async (
366
400
export const getApiRouteConfigs = async (
367
401
publish : string ,
368
402
appDir : string ,
369
- pageExtensions : string [ ] ,
403
+ pageExtensions ? : string [ ] ,
370
404
) : Promise < Array < ApiRouteConfig > > => {
371
405
const pages = await readJSON ( join ( publish , 'server' , 'pages-manifest.json' ) )
372
406
const apiRoutes = Object . keys ( pages ) . filter ( ( page ) => page . startsWith ( '/api/' ) )
@@ -381,6 +415,7 @@ export const getApiRouteConfigs = async (
381
415
const config = await extractConfigFromFile ( filePath , appDir )
382
416
383
417
const functionName = getFunctionNameForPage ( apiRoute , config . type === ApiRouteType . BACKGROUND )
418
+ const functionTitle = `${ API_FUNCTION_TITLE } ${ apiRoute } `
384
419
385
420
const compiled = pages [ apiRoute ]
386
421
const compiledPath = join ( publish , 'server' , compiled )
@@ -390,6 +425,7 @@ export const getApiRouteConfigs = async (
390
425
391
426
return {
392
427
functionName,
428
+ functionTitle,
393
429
route : apiRoute ,
394
430
config,
395
431
compiled,
@@ -405,7 +441,7 @@ export const getApiRouteConfigs = async (
405
441
export const getExtendedApiRouteConfigs = async (
406
442
publish : string ,
407
443
appDir : string ,
408
- pageExtensions : string [ ] ,
444
+ pageExtensions ? : string [ ] ,
409
445
) : Promise < Array < ApiRouteConfig > > => {
410
446
const settledApiRoutes = await getApiRouteConfigs ( publish , appDir , pageExtensions )
411
447
@@ -415,6 +451,7 @@ export const getExtendedApiRouteConfigs = async (
415
451
416
452
export const packSingleFunction = ( func : ApiRouteConfig ) : APILambda => ( {
417
453
functionName : func . functionName ,
454
+ functionTitle : func . functionTitle ,
418
455
includedFiles : func . includedFiles ,
419
456
routes : [ func ] ,
420
457
type : func . config . type ,
0 commit comments