-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
177 lines (144 loc) Β· 4.52 KB
/
index.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
import { Serve } from "bun";
import { getFormData } from "./getFormData";
import { submitForm } from "./submitForm";
const CACHE = new Map();
setInterval(() => {
for (const [id, { expiry }] of CACHE.entries()) {
if (expiry < new Date()) {
CACHE.delete(id);
}
}
}, 10_000);
const errorResponse = () => {
return new Response(JSON.stringify({
error: true,
message: "An unexpected error occurred. Please contact eliot <at> eliot <dot> sh with your form ID and any other relevant information.",
}), {
status: 500,
statusText: "Internal Server Error",
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*",
}
});
}
const serveOptions: Serve = {
fetch(req) {
switch (req.method) {
case "GET":
return getHandler(req)
.catch((err) => {
console.error(err);
return errorResponse();
});
case "POST":
return postHandler(req)
.catch((err) => {
console.error(err);
return errorResponse();
});
default:
const response = {
error: true,
message: `Invalid method: ${req.method}`,
}
return new Response(JSON.stringify(response), {
status: 405,
statusText: "Method Not Allowed",
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*", // CORS
}
});
}
},
};
const getHandler = async (req: Request) => {
const url = new URL(req.url);
const id = url.pathname.split("/")[1];
if (!id) {
return Response.redirect("https://github.com/eiiot/openform", 301);
}
console.log(`${id} - GET`);
if (CACHE.has(id)) {
const data = CACHE.get(id).data;
console.log(`${id} - CACHE HIT`);
console.log(`${id} - RETURNING RESPONSE WITH STATUS ${data.error ? 400 : 200}`);
return new Response(JSON.stringify(data), {
status: data.error ? 400 : 200,
headers: {
"content-type": "application/json;charset=UTF-8",
"x-cache": "HIT",
"access-control-allow-origin": "*",
},
});
}
console.log(`${id} - CACHE MISS`);
const data = await getFormData(id);
CACHE.set(id, {
expiry: new Date(Date.now() + 30_000),
data,
});
console.log(`${id} - CACHE SET`);
console.log(`${id} - RETURNING RESPONSE WITH STATUS ${data.error ? 400 : 200}`);
return new Response(JSON.stringify(data), {
status: data.error ? 400 : 200,
headers: {
"content-type": "application/json;charset=UTF-8",
"x-cache": "MISS",
"access-control-allow-origin": "*",
},
});
};
const postHandler = async (req: Request) => {
const url = new URL(req.url);
const id = url.pathname.split("/")[1];
if (!id) {
return Response.redirect("https://github.com/eiiot/openform", 301);
}
console.log(`${id} - POST`);
const contentType = req.headers.get("content-type");
if (contentType?.includes("application/x-www-form-urlencoded")) {
const formData = await req.formData();
const data = Object.fromEntries(formData.entries()) as { [key: string]: string };
const submitted = await submitForm(id, data);
console.log(`${id} - RETURNING RESPONSE WITH STATUS ${submitted.error ? 400 : 200}`);
return new Response(JSON.stringify(submitted), {
status: submitted.error ? 400 : 200,
statusText: submitted.error ? "Bad Request" : "OK",
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*",
},
});
} else {
try {
const json = await req.json();
const submitted = await submitForm(id, json);
console.log(`${id} - RETURNING RESPONSE WITH STATUS ${submitted.error ? 400 : 200}`);
return new Response(JSON.stringify(submitted), {
status: submitted.error ? 400 : 200,
statusText: submitted.error ? "Bad Request" : "OK",
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*",
},
});
} catch (err) {
console.error(err);
return new Response(JSON.stringify({
error: true,
message: "Invalid JSON",
}), {
status: 400,
statusText: "Bad Request",
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*",
},
});
}
}
};
Bun.serve(serveOptions);
console.log("Started server on port", process.env.PORT || 3000);