-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.ts
179 lines (149 loc) · 5.01 KB
/
config.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
import { promises as fs } from 'fs'
import { join } from 'path'
import { pathToFileURL } from 'url'
import tmp from 'tmp-promise'
import { DenoBridge } from './bridge.js'
import { BundleError } from './bundle_error.js'
import { EdgeFunction } from './edge_function.js'
import { ImportMap } from './import_map.js'
import { Logger } from './logger.js'
import { getPackagePath } from './package_json.js'
import { RateLimit } from './rate_limit.js'
enum ConfigExitCode {
Success = 0,
UnhandledError = 1,
ImportError,
NoConfig,
InvalidExport,
SerializationError,
InvalidDefaultExport,
}
export const enum Cache {
Off = 'off',
Manual = 'manual',
}
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
export type Path = `/${string}`
export type OnError = 'fail' | 'bypass' | Path
export const isValidOnError = (value: unknown): value is OnError => {
if (typeof value === 'undefined') return true
if (typeof value !== 'string') return false
return value === 'fail' || value === 'bypass' || value.startsWith('/')
}
export interface FunctionConfig {
cache?: Cache
path?: Path | Path[]
excludedPath?: Path | Path[]
onError?: OnError
name?: string
generator?: string
method?: HTTPMethod | HTTPMethod[]
rateLimit?: RateLimit
}
const getConfigExtractor = () => {
const packagePath = getPackagePath()
const configExtractorPath = join(packagePath, 'deno', 'config.ts')
return configExtractorPath
}
export const getFunctionConfig = async ({
func,
importMap,
deno,
log,
}: {
func: EdgeFunction
importMap: ImportMap
deno: DenoBridge
log: Logger
}) => {
// The extractor is a Deno script that will import the function and run its
// `config` export, if one exists.
const extractorPath = getConfigExtractor()
// We need to collect the output of the config function, which should be a
// JSON object. Rather than printing it to stdout, the extractor will write
// it to a temporary file, which we then read in the Node side. This allows
// the config function to write to stdout and stderr without that interfering
// with the extractor.
const collector = await tmp.file()
// The extractor will use its exit code to signal different error scenarios,
// based on the list of exit codes we send as an argument. We then capture
// the exit code to know exactly what happened and guide people accordingly.
const { exitCode, stderr, stdout } = await deno.run(
[
'run',
'--allow-env',
'--allow-net',
'--allow-read',
`--allow-write=${collector.path}`,
'--quiet',
`--import-map=${importMap.toDataURL()}`,
extractorPath,
pathToFileURL(func.path).href,
pathToFileURL(collector.path).href,
JSON.stringify(ConfigExitCode),
],
{ rejectOnExitCode: false },
)
if (exitCode !== ConfigExitCode.Success) {
handleConfigError(func, exitCode, stderr, log)
return {}
}
if (stdout !== '') {
log.user(stdout)
}
let collectorData: FunctionConfig = {}
try {
const collectorDataJSON = await fs.readFile(collector.path, 'utf8')
collectorData = JSON.parse(collectorDataJSON) as FunctionConfig
} catch {
handleConfigError(func, ConfigExitCode.UnhandledError, stderr, log)
} finally {
await collector.cleanup()
}
if (!isValidOnError(collectorData.onError)) {
throw new BundleError(
new Error(
`The 'onError' configuration property in edge function at '${func.path}' must be one of 'fail', 'bypass', or a path starting with '/'. Got '${collectorData.onError}'. More on the Edge Functions API at https://ntl.fyi/edge-api.`,
),
)
}
return collectorData
}
const handleConfigError = (func: EdgeFunction, exitCode: number, stderr: string, log: Logger) => {
switch (exitCode) {
case ConfigExitCode.ImportError:
log.user(stderr)
throw new BundleError(
new Error(
`Could not load edge function at '${func.path}'. More on the Edge Functions API at https://ntl.fyi/edge-api.`,
),
)
break
case ConfigExitCode.NoConfig:
log.system(`No in-source config found for edge function at '${func.path}'`)
break
case ConfigExitCode.InvalidExport:
throw new BundleError(
new Error(
`The 'config' export in edge function at '${func.path}' must be an object. More on the Edge Functions API at https://ntl.fyi/edge-api.`,
),
)
break
case ConfigExitCode.SerializationError:
throw new BundleError(
new Error(
`The 'config' object in the edge function at '${func.path}' must contain primitive values only. More on the Edge Functions API at https://ntl.fyi/edge-api.`,
),
)
break
case ConfigExitCode.InvalidDefaultExport:
throw new BundleError(
new Error(
`Default export in '${func.path}' must be a function. More on the Edge Functions API at https://ntl.fyi/edge-api.`,
),
)
default:
log.user(`Could not load configuration for edge function at '${func.path}'`)
log.user(stderr)
}
}