-
Notifications
You must be signed in to change notification settings - Fork 53
/
server.ts
242 lines (208 loc) · 6.01 KB
/
server.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
import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef } from '../bridge.js'
import { getFunctionConfig, FunctionConfig } from '../config.js'
import type { EdgeFunction } from '../edge_function.js'
import type { FeatureFlags } from '../feature_flags.js'
import { generateStage2 } from '../formats/javascript.js'
import { ImportMap } from '../import_map.js'
import { getLogger, LogFunction, Logger } from '../logger.js'
import { vendorNPMSpecifiers } from '../npm_dependencies.js'
import { ensureLatestTypes } from '../types.js'
import { killProcess, waitForServer } from './util.js'
export type FormatFunction = (name: string) => string
interface PrepareServerOptions {
basePath: string
bootstrapURL: string
deno: DenoBridge
distDirectory: string
distImportMapPath?: string
entryPoint?: string
featureFlags?: FeatureFlags
flags: string[]
formatExportTypeError?: FormatFunction
formatImportError?: FormatFunction
importMap: ImportMap
logger: Logger
port: number
}
interface StartServerOptions {
getFunctionsConfig?: boolean
}
const prepareServer = ({
basePath,
bootstrapURL,
deno,
distDirectory,
distImportMapPath,
featureFlags,
flags: denoFlags,
formatExportTypeError,
formatImportError,
importMap: baseImportMap,
logger,
port,
}: PrepareServerOptions) => {
const processRef: ProcessRef = {}
const startServer = async (
functions: EdgeFunction[],
env: NodeJS.ProcessEnv = {},
options: StartServerOptions = {},
) => {
if (processRef?.ps !== undefined) {
await killProcess(processRef.ps)
}
let graph
const stage2Path = await generateStage2({
bootstrapURL,
distDirectory,
fileName: 'dev.js',
functions,
formatExportTypeError,
formatImportError,
})
const features: Record<string, boolean> = {}
const importMap = baseImportMap.clone()
const npmSpecifiersWithExtraneousFiles: string[] = []
if (featureFlags?.edge_functions_npm_modules) {
const vendor = await vendorNPMSpecifiers({
basePath,
directory: distDirectory,
functions: functions.map(({ path }) => path),
importMap,
logger,
referenceTypes: true,
})
if (vendor) {
features.npmModules = true
importMap.add(vendor.importMap)
npmSpecifiersWithExtraneousFiles.push(...vendor.npmSpecifiersWithExtraneousFiles)
}
}
try {
// This command will print a JSON object with all the modules found in
// the `stage2Path` file as well as all of their dependencies.
// Consumers such as the CLI can use this information to watch all the
// relevant files and issue an isolate restart when one of them changes.
const { stdout } = await deno.run(['info', '--json', stage2Path])
graph = JSON.parse(stdout)
} catch {
// no-op
}
const extraDenoFlags = [`--import-map=${importMap.toDataURL()}`]
const applicationFlags = ['--port', port.toString()]
// We set `extendEnv: false` to avoid polluting the edge function context
// with variables from the user's system, since those will not be available
// in the production environment.
await deno.runInBackground(['run', ...denoFlags, ...extraDenoFlags, stage2Path, ...applicationFlags], processRef, {
pipeOutput: true,
env,
extendEnv: false,
})
let functionsConfig: FunctionConfig[] = []
if (options.getFunctionsConfig) {
functionsConfig = await Promise.all(
functions.map((func) => getFunctionConfig({ func, importMap, deno, log: logger })),
)
}
if (distImportMapPath) {
await importMap.writeToFile(distImportMapPath)
}
const success = await waitForServer(port, processRef.ps)
return {
features,
functionsConfig,
graph,
npmSpecifiersWithExtraneousFiles,
success,
}
}
return startServer
}
interface InspectSettings {
// Inspect mode enabled
enabled: boolean
// Pause on breakpoints (i.e. "--brk")
pause: boolean
// Host/port override (optional)
address?: string
}
interface ServeOptions {
basePath: string
bootstrapURL: string
certificatePath?: string
debug?: boolean
distImportMapPath?: string
featureFlags?: FeatureFlags
inspectSettings?: InspectSettings
importMapPaths?: string[]
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
formatExportTypeError?: FormatFunction
formatImportError?: FormatFunction
port: number
servePath: string
userLogger?: LogFunction
systemLogger?: LogFunction
}
export const serve = async ({
basePath,
bootstrapURL,
certificatePath,
debug,
distImportMapPath,
inspectSettings,
featureFlags,
formatExportTypeError,
formatImportError,
importMapPaths = [],
onAfterDownload,
onBeforeDownload,
port,
servePath,
userLogger,
systemLogger,
}: ServeOptions) => {
const logger = getLogger(systemLogger, userLogger, debug)
const deno = new DenoBridge({
debug,
logger,
onAfterDownload,
onBeforeDownload,
})
// Wait for the binary to be downloaded if needed.
await deno.getBinaryPath()
// Downloading latest types if needed.
await ensureLatestTypes(deno, logger)
const flags = ['--allow-all', '--no-config']
if (certificatePath) {
flags.push(`--cert=${certificatePath}`)
}
if (debug) {
flags.push('--log-level=debug')
} else {
flags.push('--quiet')
}
if (inspectSettings && inspectSettings.enabled) {
if (inspectSettings.pause) {
flags.push(inspectSettings.address ? `--inspect-brk=${inspectSettings.address}` : '--inspect-brk')
} else {
flags.push(inspectSettings.address ? `--inspect=${inspectSettings.address}` : '--inspect')
}
}
const importMap = new ImportMap()
await importMap.addFiles(importMapPaths, logger)
const server = prepareServer({
basePath,
bootstrapURL,
deno,
distDirectory: servePath,
distImportMapPath,
featureFlags,
flags,
formatExportTypeError,
formatImportError,
importMap,
logger,
port,
})
return server
}