-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathfunctions.ts
460 lines (391 loc) · 15.5 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
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 type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
import { outdent } from 'outdent'
import { join, relative, resolve, dirname } 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,
} 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 ApiRouteConfig {
functionName: string
route: string
config: ApiConfig
compiled: string
includedFiles: string[]
}
export interface APILambda {
functionName: string
routes: ApiRouteConfig[]
includedFiles: string[]
type?: ApiRouteType
}
export const generateFunctions = async (
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
appDir: string,
apiLambdas: APILambda[],
): 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, 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', '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)
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', 'handlerUtils.js'),
join(functionsDir, functionName, 'handlerUtils.js'),
)
writeFunctionConfiguration({ functionName, functionTitle, functionsDir })
}
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,
FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC,
PUBLISH_DIR,
}: NetlifyPluginConstants): Promise<void> => {
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
const jsSource = await getResolverForPages(PUBLISH_DIR)
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: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, IS_LOCAL },
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 {
files,
relativeAppDir,
config: {
experimental: { outputFileTracingRoot },
},
} = await getRequiredServerFiles(publish)
const appDirRoot = join(outputFileTracingRoot, relativeAppDir)
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 getAPIPRouteCommonDependencies = async (publish: string) => {
const deps = await Promise.all([
traceRequiredServerFiles(publish),
traceNextServer(publish),
// used by our own bridge.js
traceNPMPackage('follow-redirects', 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 MB = 1024 * 1024
export const getAPILambdas = async (
publish: string,
baseDir: string,
pageExtensions: string[],
): Promise<APILambda[]> => {
const commonDependencies = await getAPIPRouteCommonDependencies(publish)
const threshold = 50 * MB - (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, index) => ({
functionName: bin.length === 1 ? bin[0].functionName : `api-${index}`,
routes: bin,
includedFiles: [...commonDependencies, ...routes.flatMap((route) => route.includedFiles)],
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 compiled = pages[apiRoute]
const compiledPath = join(publish, 'server', compiled)
const routeDependencies = await getDependenciesOfFile(compiledPath)
const includedFiles = [compiledPath, ...routeDependencies]
return {
functionName,
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,
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.
`,
)
}
}