-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.ts
362 lines (326 loc) · 9.3 KB
/
bootstrap.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
import type { Server } from "bun"
import main from "./example/main"
type Lambda = {
fetch: (request: Request, server: Server) => Promise<Response | undefined>
error?: (error: unknown) => Promise<Response>
}
let requestId: string | undefined
let traceId: string | undefined
let functionArn: string | undefined
let logger = console.log
function log(level: string, ...args: any[]): void {
if (!args.length) {
return
}
const message = Bun.inspect(args).replace(/\n/g, "\r")
if (requestId === undefined) {
logger(level, message)
} else {
logger(level, `RequestId: ${requestId}`, message)
}
}
console.log = (...args: any[]) => log("INFO", ...args)
console.info = (...args: any[]) => log("INFO", ...args)
console.warn = (...args: any[]) => log("WARN", ...args)
console.error = (...args: any[]) => log("ERROR", ...args)
console.debug = (...args: any[]) => log("DEBUG", ...args)
console.trace = (...args: any[]) => log("TRACE", ...args)
let warnings: Set<string> | undefined
function warnOnce(message: string, ...args: any[]): void {
if (warnings === undefined) {
warnings = new Set()
}
if (warnings.has(message)) {
return
}
warnings.add(message)
console.warn(message, ...args)
}
function reset(): void {
requestId = undefined
traceId = undefined
warnings = undefined
}
function exit(...cause: any[]): never {
console.error(...cause)
process.exit(1)
}
function env(name: string, fallback?: string): string {
const value = process.env[name] ?? fallback ?? null
if (value === null) {
exit(`Runtime failed to find the '${name}' environment variable`)
}
return value
}
const runtimeUrl = new URL(
`http://${env("AWS_LAMBDA_RUNTIME_API")}/2018-06-01/`,
)
async function fetch(url: string, options?: RequestInit): Promise<Response> {
const { href } = new URL(url, runtimeUrl)
const response = await globalThis.fetch(href, {
...options,
timeout: false,
})
if (!response.ok) {
exit(
`Runtime failed to send request to Lambda [status: ${response.status}]`,
)
}
return response
}
type LambdaError = {
readonly errorType: string
readonly errorMessage: string
readonly stackTrace?: string[]
}
function formatError(error: unknown): LambdaError {
if (error instanceof Error) {
return {
errorType: error.name,
errorMessage: error.message,
stackTrace: error.stack
?.split("\n")
.filter((line) => !line.includes(" /opt/runtime.ts")),
}
}
return {
errorType: "Error",
errorMessage: Bun.inspect(error),
}
}
async function sendError(type: string, cause: unknown): Promise<void> {
console.error(cause)
await fetch(
requestId === undefined
? "runtime/init/error"
: `runtime/invocation/${requestId}/error`,
{
method: "POST",
headers: {
"Content-Type": "application/vnd.aws.lambda.error+json",
"Lambda-Runtime-Function-Error-Type": `Bun.${type}`,
},
body: JSON.stringify(formatError(cause)),
},
)
}
async function throwError(type: string, cause: unknown): Promise<never> {
await sendError(type, cause)
exit()
}
type LambdaRequest<E = any> = {
readonly requestId: string
readonly traceId: string
readonly functionArn: string
readonly deadlineMs: number | null
readonly event: E
}
async function receiveRequest(): Promise<LambdaRequest> {
const response = await fetch("runtime/invocation/next")
requestId = response.headers.get("Lambda-Runtime-Aws-Request-Id") ?? undefined
if (requestId === undefined) {
exit("Runtime received a request without a request ID")
}
traceId = response.headers.get("Lambda-Runtime-Trace-Id") ?? undefined
if (traceId === undefined) {
exit("Runtime received a request without a trace ID")
}
process.env["_X_AMZN_TRACE_ID"] = traceId
functionArn =
response.headers.get("Lambda-Runtime-Invoked-Function-Arn") ?? undefined
if (functionArn === undefined) {
exit("Runtime received a request without a function ARN")
}
const deadlineMs =
parseInt(response.headers.get("Lambda-Runtime-Deadline-Ms") ?? "0") || null
let event
try {
event = await response.json()
} catch (cause) {
exit("Runtime received a request with invalid JSON", cause)
}
return {
requestId,
traceId,
functionArn,
deadlineMs,
event,
}
}
type LambdaResponse = {
readonly statusCode: number
readonly headers?: Record<string, string>
readonly encoding?: "base64"
readonly body?: string
}
async function formatResponse(response: Response): Promise<LambdaResponse> {
const statusCode = response.status
const headers = response.headers.toJSON()
const mime = headers["content-type"]
const isBase64Encoded =
!mime || (!mime.startsWith("text/") && !mime.startsWith("application/json"))
const body = isBase64Encoded
? Buffer.from(await response.arrayBuffer()).toString("base64")
: await response.text()
return {
statusCode,
headers,
encoding: isBase64Encoded ? "base64" : undefined,
body,
}
}
async function sendResponse(response: unknown): Promise<void> {
if (requestId === undefined) {
exit("Runtime attempted to send a response without a request ID")
}
await fetch(`runtime/invocation/${requestId}/response`, {
method: "POST",
body: response === null ? null : JSON.stringify(response),
})
}
type VercelEvent = {
readonly body: string
}
type VercelEventParsedBody = {
readonly method: string
readonly headers: any
readonly path: string
readonly body?: string
readonly encoding?: string
}
function formatVercelEvent(event: VercelEvent): Request {
const payload = JSON.parse(event.body) as VercelEventParsedBody
const host = payload.headers["x-forwarded-host"]
return new Request(`https://${host}${payload.path}`, {
method: payload.method,
body: payload.body,
headers: payload.headers,
})
}
function formatRequest(input: LambdaRequest): Request {
const { event, requestId, traceId, functionArn, deadlineMs } = input
let request = formatVercelEvent(event)
request.headers.set("x-amzn-requestid", requestId)
request.headers.set("x-amzn-trace-id", traceId)
request.headers.set("x-amzn-function-arn", functionArn)
if (deadlineMs !== null) {
request.headers.set("x-amzn-deadline-ms", `${deadlineMs}`)
}
// @ts-ignore: Attach the original event to the Request
request.aws = event
return request
}
class LambdaServer implements Server {
#lambda: Lambda
#upgrade: Response | null
pendingRequests: number
pendingWebSockets: number
port: number
hostname: string
development: boolean
id: string
constructor(lambda: Lambda) {
this.#lambda = lambda
this.#upgrade = null
this.pendingRequests = 0
this.pendingWebSockets = 0
this.port = 80
this.hostname = "lambda"
this.development = false
this.id = "lambda"
}
async accept(request: LambdaRequest): Promise<unknown> {
const deadlineMs =
request.deadlineMs === null ? Date.now() + 60_000 : request.deadlineMs
const durationMs = Math.max(1, deadlineMs - Date.now())
let response: unknown
try {
response = await Promise.race([
new Promise<undefined>((resolve) => setTimeout(resolve, durationMs)),
this.#acceptRequest(request),
])
} catch (cause) {
await sendError("RequestError", cause)
return
}
if (response === undefined) {
await sendError("TimeoutError", "Function timed out")
return
}
return response
}
async #acceptRequest(event: LambdaRequest): Promise<unknown> {
const request = formatRequest(event)
const response = await this.fetch(request)
if (response === undefined) {
return {
statusCode: 200,
}
}
if (!request?.headers.has("Host")) {
return response.text()
}
return formatResponse(response)
}
stop(): void {
exit("Runtime exited because Server.stop() was called")
}
reload(options: any): void {
this.#lambda = {
fetch: options.fetch ?? this.#lambda.fetch,
error: options.error ?? this.#lambda.error,
}
this.port =
typeof options.port === "number"
? options.port
: typeof options.port === "string"
? parseInt(options.port)
: this.port
this.hostname = options.hostname ?? this.hostname
this.development = options.development ?? this.development
}
async fetch(request: Request): Promise<Response> {
this.pendingRequests++
try {
let response = await this.#lambda.fetch(request, this as any)
if (response instanceof Response) {
return response
}
if (response === undefined && this.#upgrade !== null) {
return this.#upgrade
}
throw new Error("fetch() did not return a Response")
} catch (cause) {
console.error(cause)
if (this.#lambda.error !== undefined) {
try {
return await this.#lambda.error(cause)
} catch (cause) {
console.error(cause)
}
}
return new Response(null, { status: 500 })
} finally {
this.pendingRequests--
this.#upgrade = null
}
}
upgrade(): boolean {
return false
}
publish(): number {
return 0
}
}
const server = new LambdaServer(main)
while (true) {
try {
const request = await receiveRequest()
const response = await server.accept(request)
if (response !== undefined) {
await sendResponse(response)
}
} finally {
reset()
}
}