+36
-2
src/index.ts
+36
-2
src/index.ts
···
6
6
7
7
const SUBDOMAIN_REGEX = new RegExp(`.+(?=\\.${ROOT_DOMAIN}$)`, "gm");
8
8
9
+
function clearCookies(req: Request): Headers {
10
+
const cookie_header = req.headers.get("Cookie");
11
+
// cookies are unset so return empty headers
12
+
if (!cookie_header) return new Headers();
13
+
// get each kv pair and extract the key
14
+
const cookies = cookie_header.split("; ").map((x) => x.split("=")[0]);
15
+
const head = new Headers();
16
+
for (const key of cookies) {
17
+
// max-age <= 0 means instant expiry .: deleted instantly
18
+
head.append("Set-Cookie", `${key}=; Max-Age=-1`);
19
+
}
20
+
return head;
21
+
}
22
+
9
23
Deno.serve({ port: PORT, hostname: ROOT_DOMAIN }, async (req) => {
10
24
const reqUrl = new URL(req.url);
11
25
const subdomain = reqUrl.hostname.match(SUBDOMAIN_REGEX)?.at(0)?.split(".");
···
28
42
// did:web example: `vielle.dev.did-web.ROOT_DOMAIN
29
43
// last segment must be did-plc or did-web
30
44
if (subdomain.at(-1)?.match(/^did-(web|plc)+$/gm)) {
31
-
return user(req, {
45
+
const res = await user(req, {
32
46
did: `did:${subdomain.at(-1) === "did-plc" ? "plc" : "web"}:${subdomain.slice(0, -1).join(".")}`,
33
47
});
48
+
return new Response(res.body, {
49
+
...res,
50
+
headers: {
51
+
...Array.from(res.headers.entries()).reduce(
52
+
(acc, [k, v]) => ({ ...acc, [k]: v }),
53
+
{}
54
+
),
55
+
...clearCookies(req),
56
+
},
57
+
});
34
58
}
35
59
36
60
// ex: vielle.dev.ROOT_DOMAIN
37
61
// cannot contain hyphen in top level domain
38
62
if (!subdomain.at(-1)?.startsWith("did-") && subdomain.length > 1) {
39
-
return await user(req, {
63
+
const res = await user(req, {
40
64
handle: subdomain.join(".") as `${string}.${string}`,
65
+
});
66
+
return new Response(res.body, {
67
+
...res,
68
+
headers: {
69
+
...Array.from(res.headers.entries()).reduce(
70
+
(acc, [k, v]) => ({ ...acc, [k]: v }),
71
+
{}
72
+
),
73
+
...clearCookies(req),
74
+
},
41
75
});
42
76
}
43
77