-
Notifications
You must be signed in to change notification settings - Fork 34
/
devserver.ts
100 lines (94 loc) · 3.2 KB
/
devserver.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import http from "http";
import https from "https";
import * as path from "path";
import * as fs from "fs";
import { getHandler } from "./lambda/index";
import { APIGatewayProxyEvent, APIGatewayProxyEventHeaders, APIGatewayProxyResult } from "aws-lambda";
import { URL } from "url";
import { buildDi } from "./lambda/utils/di";
import { LogUtil } from "./lambda/utils/log";
import fetch from "node-fetch";
import childProcess from "child_process";
import { localapidomain } from "./src/localdomain";
declare global {
namespace NodeJS {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Global {
__COMMIT_HASH__: string;
__FULL_COMMIT_HASH__: string;
}
}
}
function getBody(req: http.IncomingMessage): Promise<string> {
return new Promise((resolve) => {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
resolve(data);
});
});
}
async function requestToProxyEvent(request: http.IncomingMessage): Promise<APIGatewayProxyEvent> {
const body = await getBody(request);
const url = new URL(request.url || "", "http://www.example.com");
const qs: Partial<Record<string, string>> = {};
url.searchParams.forEach((v, k) => {
qs[k] = v;
});
return {
body: body,
headers: request.headers as APIGatewayProxyEventHeaders,
multiValueHeaders: {},
httpMethod: request.method || "GET",
isBase64Encoded: false,
path: url.pathname,
pathParameters: {},
queryStringParameters: qs,
multiValueQueryStringParameters: {},
stageVariables: {},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
requestContext: {} as any,
resource: "",
};
}
const log = new LogUtil();
const di = buildDi(log, fetch);
const handler = getHandler(di);
const server = https.createServer(
{
key: fs.readFileSync(path.join(process.env.HOME!, `.secrets/live/${localapidomain}.liftosaur.com/privkey.pem`)),
cert: fs.readFileSync(path.join(process.env.HOME!, `.secrets/live/${localapidomain}.liftosaur.com/fullchain.pem`)),
},
async (req, res) => {
try {
const result = (await handler(
await requestToProxyEvent(req),
{ getRemainingTimeInMillis: () => 10000 },
() => undefined
)) as APIGatewayProxyResult;
const body = result.isBase64Encoded ? Buffer.from(result.body, "base64") : result.body;
res.statusCode = result.statusCode;
for (const k of Object.keys(result.headers || {})) {
res.setHeader(k, result.headers![k] as string);
}
res.end(body);
} catch (e) {
if (e instanceof Error) {
console.error(e);
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ name: e.name, error: e.message, stack: e.stack }));
} else {
throw e;
}
}
}
);
// eslint-disable-next-line prefer-const
(global as any).__COMMIT_HASH__ = childProcess.execSync("git rev-parse --short HEAD").toString().trim();
(global as any).__FULL_COMMIT_HASH__ = childProcess.execSync("git rev-parse HEAD").toString().trim();
server.listen(3000, "0.0.0.0", () => {
console.log(`--------- Server is running ----------`);
});