@netlify/build 27.5.0 → 27.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/build",
3
- "version": "27.5.0",
3
+ "version": "27.6.0",
4
4
  "description": "Netlify build module",
5
5
  "type": "module",
6
6
  "exports": "./src/core/main.js",
@@ -66,6 +66,8 @@
66
66
  "@netlify/zip-it-and-ship-it": "5.13.2",
67
67
  "@sindresorhus/slugify": "^2.0.0",
68
68
  "@types/node": "^16.0.0",
69
+ "ajv": "^8.11.0",
70
+ "ajv-errors": "^3.0.0",
69
71
  "ansi-escapes": "^5.0.0",
70
72
  "chalk": "^5.0.0",
71
73
  "clean-stack": "^4.0.0",
@@ -23,8 +23,12 @@ const getBuildCommandLocation = function ({ buildCommand, buildCommandOrigin })
23
23
  ${buildCommand}`
24
24
  }
25
25
 
26
- const getFunctionsBundlingLocation = function ({ functionName }) {
27
- return `While bundling Function "${functionName}"`
26
+ const getFunctionsBundlingLocation = function ({ functionName, functionType }) {
27
+ if (functionType === 'edge') {
28
+ return 'While bundling edge function'
29
+ }
30
+
31
+ return `While bundling function "${functionName}"`
28
32
  }
29
33
 
30
34
  const getCoreStepLocation = function ({ coreStepName }) {
package/src/error/type.js CHANGED
@@ -81,7 +81,13 @@ const TYPES = {
81
81
 
82
82
  // User error during Functions bundling
83
83
  functionsBundling: {
84
- title: ({ location: { functionName } }) => `Bundling of Function "${functionName}" failed`,
84
+ title: ({ location: { functionName, functionType } }) => {
85
+ if (functionType === 'edge') {
86
+ return 'Bundling of edge function failed'
87
+ }
88
+
89
+ return `Bundling of function "${functionName}" failed`
90
+ },
85
91
  stackType: 'none',
86
92
  locationType: 'functionsBundling',
87
93
  severity: 'info',
@@ -7,12 +7,15 @@ import { pathExists } from 'path-exists'
7
7
  import { logFunctionsToBundle } from '../../log/messages/core_steps.js'
8
8
  import { logEdgeManifest } from '../../log/messages/edge_manifest.js'
9
9
 
10
+ import { tagBundlingError } from './lib/error.js'
10
11
  import { parseManifest } from './lib/internal_manifest.js'
12
+ import { validateEdgeFunctionsManifest } from './validate_manifest/validate_edge_functions_manifest.js'
11
13
 
12
14
  // TODO: Replace this with a custom cache directory.
13
15
  const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli'
14
16
  const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json'
15
17
 
18
+ // eslint-disable-next-line complexity, max-statements
16
19
  const coreStep = async function ({
17
20
  buildDir,
18
21
  constants: {
@@ -46,18 +49,27 @@ const coreStep = async function ({
46
49
  // Edge Bundler expects the dist directory to exist.
47
50
  await fs.mkdir(distPath, { recursive: true })
48
51
 
49
- const { manifest } = await bundle(sourcePaths, distPath, declarations, {
50
- cacheDirectory,
51
- debug,
52
- distImportMapPath,
53
- featureFlags,
54
- importMaps: [importMap].filter(Boolean),
55
- })
56
-
57
- if (debug) {
58
- logEdgeManifest({ manifest, logs, debug })
52
+ try {
53
+ const { manifest } = await bundle(sourcePaths, distPath, declarations, {
54
+ basePath: buildDir,
55
+ cacheDirectory,
56
+ debug,
57
+ distImportMapPath,
58
+ featureFlags,
59
+ importMaps: [importMap].filter(Boolean),
60
+ })
61
+
62
+ if (debug) {
63
+ logEdgeManifest({ manifest, logs, debug })
64
+ }
65
+ } catch (error) {
66
+ tagBundlingError(error)
67
+
68
+ throw error
59
69
  }
60
70
 
71
+ await validateEdgeFunctionsManifest({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory } })
72
+
61
73
  return {}
62
74
  }
63
75
 
@@ -0,0 +1,21 @@
1
+ import { CUSTOM_ERROR_KEY, getErrorInfo, isBuildError } from '../../../error/info.js'
2
+
3
+ // If we have a custom error tagged with `functionsBundling` (which happens if
4
+ // there is an issue with user code), we tag it as coming from an edge function
5
+ // so that we can adjust the downstream error messages accordingly.
6
+ export const tagBundlingError = (error) => {
7
+ if (!isBuildError(error)) {
8
+ return
9
+ }
10
+
11
+ const [errorInfo = {}] = getErrorInfo(error)
12
+
13
+ if (errorInfo.type !== 'functionsBundling') {
14
+ return
15
+ }
16
+
17
+ error[CUSTOM_ERROR_KEY].location = {
18
+ ...error[CUSTOM_ERROR_KEY].location,
19
+ functionType: 'edge',
20
+ }
21
+ }
@@ -0,0 +1,89 @@
1
+ import { promises as fs } from 'fs'
2
+ import { join, resolve } from 'path'
3
+
4
+ import Ajv from 'ajv'
5
+ import ajvErrors from 'ajv-errors'
6
+
7
+ import { addErrorInfo } from '../../../error/info.js'
8
+
9
+ const ajv = new Ajv({ allErrors: true })
10
+ ajvErrors(ajv)
11
+
12
+ // regex pattern for manifest route pattern
13
+ // checks if the pattern string starts with ^ and ends with $
14
+ // we define this format in edge-bundler:
15
+ // https://github.com/netlify/edge-bundler/blob/main/src/manifest.ts#L66
16
+ const normalizedPatternRegex = /^\^.*\$$/
17
+ ajv.addFormat('regexPattern', {
18
+ async: true,
19
+ validate: (data) => normalizedPatternRegex.test(data),
20
+ })
21
+
22
+ const bundlesSchema = {
23
+ $async: true,
24
+ type: 'object',
25
+ required: ['asset', 'format'],
26
+ properties: {
27
+ asset: { type: 'string' },
28
+ format: { type: 'string' },
29
+ },
30
+ additionalProperties: false,
31
+ }
32
+
33
+ const routesSchema = {
34
+ $async: true,
35
+ type: 'object',
36
+ required: ['function', 'pattern'],
37
+ properties: {
38
+ function: { type: 'string' },
39
+ pattern: { type: 'string', format: 'regexPattern', errorMessage: `must match format ${normalizedPatternRegex}` },
40
+ },
41
+ additionalProperties: false,
42
+ }
43
+
44
+ const edgeManifestSchema = {
45
+ $async: true,
46
+ type: 'object',
47
+ required: ['bundles', 'routes', 'bundler_version'],
48
+ properties: {
49
+ bundles: {
50
+ type: 'array',
51
+ items: bundlesSchema,
52
+ },
53
+ routes: {
54
+ type: 'array',
55
+ items: routesSchema,
56
+ },
57
+ bundler_version: { type: 'string' },
58
+ },
59
+ additionalProperties: false,
60
+ errorMessage: "Couldn't validate Edge Functions manifest.json",
61
+ }
62
+
63
+ const validateManifest = async (manifestData) => {
64
+ const validate = ajv.compile(edgeManifestSchema)
65
+
66
+ await validate(manifestData)
67
+ }
68
+
69
+ export const validateEdgeFunctionsManifest = async function ({
70
+ buildDir,
71
+ constants: { EDGE_FUNCTIONS_DIST: distDirectory },
72
+ }) {
73
+ try {
74
+ const edgeFunctionsDistPath = resolve(buildDir, distDirectory)
75
+ const manifestPath = join(edgeFunctionsDistPath, 'manifest.json')
76
+ const data = await fs.readFile(manifestPath)
77
+ const manifestData = JSON.parse(data)
78
+
79
+ await validateManifest(manifestData)
80
+ } catch (error) {
81
+ const isValidationErr = error instanceof Ajv.ValidationError
82
+ const parsedErr = isValidationErr ? error.errors : error
83
+
84
+ addErrorInfo(parsedErr, { type: 'coreStep' })
85
+ throw new Error(isValidationErr ? JSON.stringify(parsedErr, null, 2) : parsedErr)
86
+ }
87
+
88
+ return {}
89
+ }