-
Notifications
You must be signed in to change notification settings - Fork 52
/
validate_edge_functions_manifest.js
90 lines (77 loc) · 2.37 KB
/
validate_edge_functions_manifest.js
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
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import Ajv from 'ajv'
import ajvErrors from 'ajv-errors'
import { addErrorInfo } from '../../../error/info.js'
const ajv = new Ajv({ allErrors: true })
ajvErrors(ajv)
// regex pattern for manifest route pattern
// checks if the pattern string starts with ^ and ends with $
// we define this format in edge-bundler:
// https://github.com/netlify/edge-bundler/blob/main/src/manifest.ts#L66
const normalizedPatternRegex = /^\^.*\$$/
ajv.addFormat('regexPattern', {
async: true,
validate: (data) => normalizedPatternRegex.test(data),
})
const bundlesSchema = {
$async: true,
type: 'object',
required: ['asset', 'format'],
properties: {
asset: { type: 'string' },
format: { type: 'string' },
},
additionalProperties: false,
}
const routesSchema = {
$async: true,
type: 'object',
required: ['function', 'pattern'],
properties: {
name: { type: 'string' },
function: { type: 'string' },
pattern: { type: 'string', format: 'regexPattern', errorMessage: `must match format ${normalizedPatternRegex}` },
},
additionalProperties: false,
}
const edgeManifestSchema = {
$async: true,
type: 'object',
required: ['bundles', 'routes', 'bundler_version'],
properties: {
bundles: {
type: 'array',
items: bundlesSchema,
},
routes: {
type: 'array',
items: routesSchema,
},
bundler_version: { type: 'string' },
},
additionalProperties: false,
errorMessage: "Couldn't validate Edge Functions manifest.json",
}
const validateManifest = async (manifestData) => {
const validate = ajv.compile(edgeManifestSchema)
await validate(manifestData)
}
export const validateEdgeFunctionsManifest = async function ({
buildDir,
constants: { EDGE_FUNCTIONS_DIST: distDirectory },
}) {
try {
const edgeFunctionsDistPath = resolve(buildDir, distDirectory)
const manifestPath = join(edgeFunctionsDistPath, 'manifest.json')
const data = await fs.readFile(manifestPath)
const manifestData = JSON.parse(data)
await validateManifest(manifestData)
} catch (error) {
const isValidationErr = error instanceof Ajv.ValidationError
const parsedErr = isValidationErr ? error.errors : error
addErrorInfo(parsedErr, { type: 'coreStep' })
throw new Error(isValidationErr ? JSON.stringify(parsedErr, null, 2) : parsedErr)
}
return {}
}