-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.ts
84 lines (72 loc) · 2.53 KB
/
wrapper.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
import { createServer, IncomingMessage, ServerResponse } from "node:http"
import { handler } from "./src/server/index.ts"
import { APIGatewayProxyEventV2 } from "aws-lambda"
function getRequestBody (req: IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let body = ""
req.on("data", chunk => body += chunk)
req.on("end", () => resolve(body))
req.on("error", err => reject(err))
})
}
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
try {
if (typeof(req.url) !== "string") throw new Error(`Invalid url: ${req.url}`)
const body = await getRequestBody(req)
const url = new URL(req.url, `http://${req.headers.host}`)
const date = new Date()
if (typeof req.method === "undefined") {
throw new Error("Req method is undefined")
}
const httpHeaders = Object.entries(req.headers).reduce<APIGatewayProxyEventV2["headers"]>((acc, [key, value]) => {
if (typeof value === "object") {
acc[key] = value.join("; ")
} else {
acc[key] = value
}
return acc
}, {})
const lambdaEvent: APIGatewayProxyEventV2 = {
"version": "local-mock",
"routeKey": "local-mock",
"rawPath": req.url,
"rawQueryString": JSON.stringify(Object.fromEntries(url.searchParams)),
"headers": httpHeaders,
"requestContext": {
"accountId": "local-mock",
"apiId": "local-mock",
"domainName": url.hostname,
"domainPrefix": url.hostname.split(".")[0] ?? url.hostname,
"http": {
"method": req.method,
"path": url.pathname,
"protocol": `HTTP/${req.httpVersion}`,
"sourceIp": req.socket.remoteAddress || "local-mock",
"userAgent": req.headers["user-agent"] || "local-mock"
},
"requestId": "local-mock",
"routeKey": "local-mock",
"stage": "local-mock",
"time": date.toString(),
"timeEpoch": date.getTime()
},
"body": body,
"isBase64Encoded": false
}
const lambdaResponse = await handler(lambdaEvent)
res.statusCode = lambdaResponse.statusCode
if (lambdaResponse.headers) {
Object.entries(lambdaResponse.headers).forEach(([key, value]) => {
res.setHeader(key, value as string)
})
}
res.end(lambdaResponse.body)
} catch (error) {
res.statusCode = 500
res.end("Internal Server Error")
console.error("Error occurred:", error)
}
})
server.listen(3000, () => {
console.log("Running on http://localhost:3000")
})