-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathfunctions.ts
629 lines (535 loc) · 20.9 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
import bridgeFile from '@vercel/node-bridge'
import chalk from 'chalk'
import destr from 'destr'
import { copyFile, ensureDir, existsSync, readJSON, writeFile, writeJSON, stat } from 'fs-extra'
import { PrerenderManifest } from 'next/dist/build'
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
import { outdent } from 'outdent'
import { join, relative, resolve, dirname, basename, extname } from 'pathe'
import glob from 'tiny-glob'
import {
HANDLER_FUNCTION_NAME,
ODB_FUNCTION_NAME,
IMAGE_FUNCTION_NAME,
DEFAULT_FUNCTIONS_SRC,
HANDLER_FUNCTION_TITLE,
ODB_FUNCTION_TITLE,
IMAGE_FUNCTION_TITLE,
API_FUNCTION_TITLE,
API_FUNCTION_NAME,
LAMBDA_WARNING_SIZE,
} from '../constants'
import { getApiHandler } from '../templates/getApiHandler'
import { getHandler } from '../templates/getHandler'
import { getResolverForPages, getResolverForSourceFiles } from '../templates/getPageResolver'
import { ApiConfig, extractConfigFromFile, isEdgeConfig } from './analysis'
import { getRequiredServerFiles } from './config'
import { getDependenciesOfFile, getServerFile, getSourceFileForPage } from './files'
import { writeFunctionConfiguration } from './functionsMetaData'
import { pack } from './pack'
import { ApiRouteType } from './types'
import { getFunctionNameForPage } from './utils'
export interface RouteConfig {
functionName: string
functionTitle?: string
route: string
compiled: string
includedFiles: string[]
}
export interface ApiRouteConfig extends RouteConfig {
config: ApiConfig
}
export interface SSRLambda {
functionName: string
functionTitle: string
routes: RouteConfig[]
includedFiles: string[]
}
export interface APILambda extends SSRLambda {
routes: ApiRouteConfig[]
type?: ApiRouteType
}
export const generateFunctions = async (
{
INTERNAL_FUNCTIONS_SRC,
PUBLISH_DIR,
PACKAGE_PATH = '',
FUNCTIONS_SRC = join(PACKAGE_PATH, DEFAULT_FUNCTIONS_SRC),
}: NetlifyPluginConstants,
appDir: string,
apiLambdas: APILambda[],
ssrLambdas: SSRLambda[],
): Promise<void> => {
const publish = resolve(PUBLISH_DIR)
const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC)
const functionDir = join(functionsDir, HANDLER_FUNCTION_NAME)
const publishDir = relative(functionDir, publish)
const nextServerModuleAbsoluteLocation = getServerFile(appDir, false)
const nextServerModuleRelativeLocation = nextServerModuleAbsoluteLocation
? relative(functionDir, nextServerModuleAbsoluteLocation)
: undefined
for (const apiLambda of apiLambdas) {
const { functionName, functionTitle, routes, type, includedFiles } = apiLambda
const apiHandlerSource = getApiHandler({
// most api lambdas serve multiple routes, but scheduled functions need to be in separate lambdas.
// so routes[0] is safe to access.
schedule: type === ApiRouteType.SCHEDULED ? routes[0].config.schedule : undefined,
publishDir,
appDir: relative(functionDir, appDir),
nextServerModuleRelativeLocation,
})
await ensureDir(join(functionsDir, functionName))
// write main API handler file
await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource)
// copy handler dependencies (VercelNodeBridge, NetlifyNextServer, etc.)
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js'))
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'server.js'),
join(functionsDir, functionName, 'server.js'),
)
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'requireHooks.js'),
join(functionsDir, functionName, 'requireHooks.js'),
)
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'),
join(functionsDir, functionName, 'handlerUtils.js'),
)
const resolveSourceFile = (file: string) => join(publish, 'server', file)
// TODO: this should be unneeded once we use the `none` bundler everywhere
const resolverSource = await getResolverForSourceFiles({
functionsDir,
// These extra pages are always included by Next.js
sourceFiles: [
...routes.map((route) => route.compiled),
'pages/_app.js',
'pages/_document.js',
'pages/_error.js',
].map(resolveSourceFile),
})
await writeFile(join(functionsDir, functionName, 'pages.js'), resolverSource)
await writeFunctionConfiguration({ functionName, functionTitle, functionsDir })
const nfInternalFiles = await glob(join(functionsDir, functionName, '**'))
includedFiles.push(...nfInternalFiles)
}
const writeHandler = async (functionName: string, functionTitle: string, isODB: boolean) => {
const handlerSource = getHandler({
isODB,
publishDir,
appDir: relative(functionDir, appDir),
nextServerModuleRelativeLocation,
})
await ensureDir(join(functionsDir, functionName))
// write main handler file (standard or ODB)
await writeFile(join(functionsDir, functionName, `${functionName}.js`), handlerSource)
// copy handler dependencies (VercelNodeBridge, NetlifyNextServer, etc.)
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js'))
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'server.js'),
join(functionsDir, functionName, 'server.js'),
)
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'requireHooks.js'),
join(functionsDir, functionName, 'requireHooks.js'),
)
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'),
join(functionsDir, functionName, 'handlerUtils.js'),
)
await writeFunctionConfiguration({ functionName, functionTitle, functionsDir })
const nfInternalFiles = await glob(join(functionsDir, functionName, '**'))
const lambda = ssrLambdas.find((l) => l.functionName === functionName)
if (lambda) {
lambda.includedFiles.push(...nfInternalFiles)
}
}
await writeHandler(HANDLER_FUNCTION_NAME, HANDLER_FUNCTION_TITLE, false)
await writeHandler(ODB_FUNCTION_NAME, ODB_FUNCTION_TITLE, true)
}
/**
* Writes a file in each function directory that contains references to every page entrypoint.
* This is just so that the nft bundler knows about them. We'll eventually do this better.
*/
export const generatePagesResolver = async ({
INTERNAL_FUNCTIONS_SRC,
PUBLISH_DIR,
PACKAGE_PATH = '',
FUNCTIONS_SRC = join(PACKAGE_PATH, DEFAULT_FUNCTIONS_SRC),
}: NetlifyPluginConstants): Promise<void> => {
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
const jsSource = await getResolverForPages(PUBLISH_DIR, PACKAGE_PATH)
await writeFile(join(functionsPath, ODB_FUNCTION_NAME, 'pages.js'), jsSource)
await writeFile(join(functionsPath, HANDLER_FUNCTION_NAME, 'pages.js'), jsSource)
}
// Move our next/image function into the correct functions directory
export const setupImageFunction = async ({
constants: {
IS_LOCAL,
INTERNAL_FUNCTIONS_SRC,
PACKAGE_PATH = '',
FUNCTIONS_SRC = join(PACKAGE_PATH, DEFAULT_FUNCTIONS_SRC),
},
imageconfig = {},
netlifyConfig,
basePath,
remotePatterns,
responseHeaders,
}: {
constants: NetlifyPluginConstants
netlifyConfig: NetlifyConfig
basePath: string
imageconfig: Partial<ImageConfigComplete>
remotePatterns: RemotePattern[]
responseHeaders?: Record<string, string>
}): Promise<void> => {
const imagePath = imageconfig.path || '/_next/image'
if (destr(process.env.DISABLE_IPX)) {
// If no image loader is specified, need to redirect to a 404 page since there's no
// backing loader to serve local site images once deployed to Netlify
if (!IS_LOCAL && imageconfig.loader === 'default') {
netlifyConfig.redirects.push({
from: `${imagePath}*`,
query: { url: ':url', w: ':width', q: ':quality' },
to: '/404.html',
status: 404,
force: true,
})
}
} else {
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
const functionName = `${IMAGE_FUNCTION_NAME}.js`
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME)
await ensureDir(functionDirectory)
await writeJSON(join(functionDirectory, 'imageconfig.json'), {
...imageconfig,
basePath: [basePath, IMAGE_FUNCTION_NAME].join('/'),
remotePatterns,
responseHeaders,
})
await copyFile(join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), join(functionDirectory, functionName))
writeFunctionConfiguration({
functionName: IMAGE_FUNCTION_NAME,
functionTitle: IMAGE_FUNCTION_TITLE,
functionsDir: functionsPath,
})
// If we have edge functions then the request will have already been rewritten
// so this won't match. This is matched if edge is disabled or unavailable.
netlifyConfig.redirects.push({
from: `${imagePath}*`,
query: { url: ':url', w: ':width', q: ':quality' },
to: `${basePath}/${IMAGE_FUNCTION_NAME}/w_:width,q_:quality/:url`,
status: 301,
})
netlifyConfig.redirects.push({
from: `${basePath}/${IMAGE_FUNCTION_NAME}/*`,
to: `/.netlify/builders/${IMAGE_FUNCTION_NAME}`,
status: 200,
})
}
if (basePath) {
// next/image generates image static URLs that still point at the site root
netlifyConfig.redirects.push({
from: '/_next/static/image/*',
to: '/static/image/:splat',
status: 200,
})
}
}
const traceRequiredServerFiles = async (publish: string): Promise<string[]> => {
const requiredServerFiles = await getRequiredServerFiles(publish)
let appDirRoot = requiredServerFiles.appDir ?? join(publish, '..')
if (requiredServerFiles.relativeAppDir && requiredServerFiles.config?.experimental.outputFileTracingRoot) {
appDirRoot = join(requiredServerFiles.config.experimental.outputFileTracingRoot, requiredServerFiles.relativeAppDir)
}
const files = requiredServerFiles.files ?? []
const absoluteFiles = files.map((file) => join(appDirRoot, file))
absoluteFiles.push(join(publish, 'required-server-files.json'))
return absoluteFiles
}
const traceNextServer = async (publish: string): Promise<string[]> => {
const nextServerDeps = await getDependenciesOfFile(join(publish, 'next-server.js'))
// during testing, i've seen `next-server` contain only one line.
// this is a sanity check to make sure we're getting all the deps.
if (nextServerDeps.length < 10) {
console.error(nextServerDeps)
throw new Error("next-server.js.nft.json didn't contain all dependencies.")
}
const filtered = nextServerDeps.filter((f) => {
// NFT detects a bunch of large development files that we don't need.
if (f.endsWith('.development.js')) return false
// not needed for API Routes!
if (f.endsWith('node_modules/sass/sass.dart.js')) return false
return true
})
return filtered
}
export const traceNPMPackage = async (packageName: string, publish: string) => {
try {
return await glob(join(dirname(require.resolve(packageName, { paths: [__dirname, publish] })), '**', '*'), {
absolute: true,
})
} catch (error) {
if (process.env.NODE_ENV === 'test') {
return []
}
throw error
}
}
export const getCommonDependencies = async (publish: string) => {
const deps = await Promise.all([
traceRequiredServerFiles(publish),
traceNextServer(publish),
// used by our own bridge.js
traceNPMPackage('follow-redirects', publish),
// using package.json because otherwise, we'd find some /dist/... path
traceNPMPackage('@netlify/functions/package.json', publish),
traceNPMPackage('is-promise', publish),
])
return deps.flat(1)
}
const sum = (arr: number[]) => arr.reduce((v, current) => v + current, 0)
// TODO: cache results
const getBundleWeight = async (patterns: string[]) => {
const sizes = await Promise.all(
patterns.flatMap(async (pattern) => {
const files = await glob(pattern)
return Promise.all(
files.map(async (file) => {
const fStat = await stat(file)
if (fStat.isFile()) {
return fStat.size
}
return 0
}),
)
}),
)
return sum(sizes.flat(1))
}
const changeExtension = (file: string, extension: string) => {
const base = basename(file, extname(file))
return join(dirname(file), base + extension)
}
const getSSRDependencies = async (publish: string): Promise<string[]> => {
const prerenderManifest: PrerenderManifest = await readJSON(join(publish, 'prerender-manifest.json'))
return [
...Object.entries(prerenderManifest.routes).flatMap(([route, ssgRoute]) => {
if (ssgRoute.initialRevalidateSeconds === false) {
return []
}
if (ssgRoute.dataRoute.endsWith('.rsc')) {
return [
join(publish, 'server', 'app', ssgRoute.dataRoute),
join(publish, 'server', 'app', changeExtension(ssgRoute.dataRoute, '.html')),
]
}
const trimmedPath = route === '/' ? 'index' : route.slice(1)
return [
join(publish, 'server', 'pages', `${trimmedPath}.html`),
join(publish, 'server', 'pages', `${trimmedPath}.json`),
]
}),
join(publish, '**', '*.html'),
join(publish, 'static-manifest.json'),
]
}
export const getSSRLambdas = async (publish: string): Promise<SSRLambda[]> => {
const commonDependencies = await getCommonDependencies(publish)
const ssrRoutes = await getSSRRoutes(publish)
// TODO: for now, they're the same - but we should separate them
const nonOdbRoutes = ssrRoutes
const odbRoutes = ssrRoutes
const ssrDependencies = await getSSRDependencies(publish)
return [
{
functionName: HANDLER_FUNCTION_NAME,
functionTitle: HANDLER_FUNCTION_TITLE,
includedFiles: [
...commonDependencies,
...ssrDependencies,
...nonOdbRoutes.flatMap((route) => route.includedFiles),
],
routes: nonOdbRoutes,
},
{
functionName: ODB_FUNCTION_NAME,
functionTitle: ODB_FUNCTION_TITLE,
includedFiles: [...commonDependencies, ...ssrDependencies, ...odbRoutes.flatMap((route) => route.includedFiles)],
routes: odbRoutes,
},
]
}
const getSSRRoutes = async (publish: string): Promise<RouteConfig[]> => {
const pageManifest = (await readJSON(join(publish, 'server', 'pages-manifest.json'))) as Record<string, string>
const pageManifestRoutes = Object.entries(pageManifest).filter(
([page, compiled]) => !page.startsWith('/api/') && !compiled.endsWith('.html'),
)
const appPathsManifest: Record<string, string> = await readJSON(
join(publish, 'server', 'app-paths-manifest.json'),
).catch(() => ({}))
const appRoutes = Object.entries(appPathsManifest)
const routes = [...pageManifestRoutes, ...appRoutes]
return await Promise.all(
routes.map(async ([route, compiled]) => {
const functionName = getFunctionNameForPage(route)
const compiledPath = join(publish, 'server', compiled)
const routeDependencies = await getDependenciesOfFile(compiledPath)
const includedFiles = [compiledPath, ...routeDependencies]
return {
functionName,
route,
compiled,
includedFiles,
}
}),
)
}
export const getAPILambdas = async (
publish: string,
baseDir: string,
pageExtensions: string[],
): Promise<APILambda[]> => {
const commonDependencies = await getCommonDependencies(publish)
const threshold = LAMBDA_WARNING_SIZE - (await getBundleWeight(commonDependencies))
const apiRoutes = await getApiRouteConfigs(publish, baseDir, pageExtensions)
const packFunctions = async (routes: ApiRouteConfig[], type?: ApiRouteType): Promise<APILambda[]> => {
const weighedRoutes = await Promise.all(
routes.map(async (route) => ({ value: route, weight: await getBundleWeight(route.includedFiles) })),
)
const bins = pack(weighedRoutes, threshold)
return bins.map((bin) => {
if (bin.length === 1) {
const [func] = bin
const { functionName, functionTitle, config, includedFiles } = func
return {
functionName,
functionTitle,
routes: [func],
includedFiles: [...commonDependencies, ...includedFiles],
type: config.type,
}
}
const includedFiles = [...commonDependencies, ...bin.flatMap((route) => route.includedFiles)]
const nonSingletonBins = bins.filter((b) => b.length > 1)
if (nonSingletonBins.length === 1) {
return {
functionName: API_FUNCTION_NAME,
functionTitle: API_FUNCTION_TITLE,
includedFiles,
routes: bin,
type,
}
}
const indexInNonSingletonBins = nonSingletonBins.indexOf(bin)
return {
functionName: `${API_FUNCTION_NAME}-${indexInNonSingletonBins + 1}`,
functionTitle: `${API_FUNCTION_TITLE} ${indexInNonSingletonBins + 1}/${nonSingletonBins.length}`,
includedFiles,
routes: bin,
type,
}
})
}
const standardFunctions = apiRoutes.filter(
(route) =>
!isEdgeConfig(route.config.runtime) &&
route.config.type !== ApiRouteType.BACKGROUND &&
route.config.type !== ApiRouteType.SCHEDULED,
)
const scheduledFunctions = apiRoutes.filter((route) => route.config.type === ApiRouteType.SCHEDULED)
const backgroundFunctions = apiRoutes.filter((route) => route.config.type === ApiRouteType.BACKGROUND)
const scheduledLambdas: APILambda[] = scheduledFunctions.map(packSingleFunction)
const [standardLambdas, backgroundLambdas] = await Promise.all([
packFunctions(standardFunctions),
packFunctions(backgroundFunctions, ApiRouteType.BACKGROUND),
])
return [...standardLambdas, ...backgroundLambdas, ...scheduledLambdas]
}
/**
* Look for API routes, and extract the config from the source file.
*/
export const getApiRouteConfigs = async (
publish: string,
appDir: string,
pageExtensions?: string[],
): Promise<Array<ApiRouteConfig>> => {
const pages = await readJSON(join(publish, 'server', 'pages-manifest.json'))
const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/'))
// two possible places
// Ref: https://nextjs.org/docs/advanced-features/src-directory
const pagesDir = join(appDir, 'pages')
const srcPagesDir = join(appDir, 'src', 'pages')
return await Promise.all(
apiRoutes.map(async (apiRoute) => {
const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir], pageExtensions)
const config = await extractConfigFromFile(filePath, appDir)
const functionName = getFunctionNameForPage(apiRoute, config.type === ApiRouteType.BACKGROUND)
const functionTitle = `${API_FUNCTION_TITLE} ${apiRoute}`
const compiled = pages[apiRoute]
const compiledPath = join(publish, 'server', compiled)
const routeDependencies = await getDependenciesOfFile(compiledPath)
const includedFiles = [compiledPath, ...routeDependencies]
return {
functionName,
functionTitle,
route: apiRoute,
config,
compiled,
includedFiles,
}
}),
)
}
/**
* Looks for extended API routes (background and scheduled functions) and extract the config from the source file.
*/
export const getExtendedApiRouteConfigs = async (
publish: string,
appDir: string,
pageExtensions?: string[],
): Promise<Array<ApiRouteConfig>> => {
const settledApiRoutes = await getApiRouteConfigs(publish, appDir, pageExtensions)
// We only want to return the API routes that are background or scheduled functions
return settledApiRoutes.filter((apiRoute) => apiRoute.config.type !== undefined)
}
export const packSingleFunction = (func: ApiRouteConfig): APILambda => ({
functionName: func.functionName,
functionTitle: func.functionTitle,
includedFiles: func.includedFiles,
routes: [func],
type: func.config.type,
})
interface FunctionsManifest {
functions: Array<{ name: string; schedule?: string }>
}
/**
* Warn the user of the caveats if they're using background or scheduled API routes
*/
export const warnOnApiRoutes = async ({
FUNCTIONS_DIST,
}: Pick<NetlifyPluginConstants, 'FUNCTIONS_DIST'>): Promise<void> => {
const functionsManifestPath = join(FUNCTIONS_DIST, 'manifest.json')
if (!existsSync(functionsManifestPath)) {
return
}
const { functions }: FunctionsManifest = await readJSON(functionsManifestPath)
if (functions.some((func) => func.name.endsWith('-background'))) {
console.warn(
outdent`
${chalk.yellowBright`Using background API routes`}
If your account type does not support background functions, the deploy will fail.
During local development, background API routes will run as regular API routes, but in production they will immediately return an empty "202 Accepted" response.
`,
)
}
if (functions.some((func) => func.schedule)) {
console.warn(
outdent`
${chalk.yellowBright`Using scheduled API routes`}
These are run on a schedule when deployed to production.
You can test them locally by loading them in your browser but this will not be available when deployed, and any returned value is ignored.
`,
)
}
}