-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
108 lines (95 loc) · 3 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
import { addRoute } from './router.ts';
addRoute('/psl', async () => {
const pslResponse = await fetch(
'https://publicsuffix.org/list/public_suffix_list.dat',
);
return new Response(pslResponse.body, {
headers: {
...Object.fromEntries(pslResponse.headers),
'Content-Security-Policy': 'sandbox',
'Access-Control-Allow-Origin': '*',
},
});
});
type KeyValue = [key: string, value: string];
interface Details {
preflightHeaders?: KeyValue[];
headers?: KeyValue[];
method?: string;
}
const detailsMap = new Map<string, Details>();
function getDetails(id: string | null): Details {
// Just return a dummy object to make things easier.
if (!id) return {};
if (!detailsMap.has(id)) {
detailsMap.set(id, {});
// Only keep details around for a minute:
setTimeout(() => detailsMap.delete(id), 60_000);
}
return detailsMap.get(id)!;
}
addRoute('/resource', {
OPTIONS(url, request) {
const details = getDetails(url.searchParams.get('id'));
details.preflightHeaders = [...request.headers];
const headers = new Headers();
const status = Number(url.searchParams.get('preflight-status')) || 204;
for (const acHeader of [
'allow-origin',
'allow-credentials',
'allow-methods',
'allow-headers',
]) {
const fullHeader = `access-control-${acHeader}`;
const queryKey = `preflight-${fullHeader}`;
const value = url.searchParams.get(queryKey);
if (value) headers.set(fullHeader, url.searchParams.get(queryKey)!);
}
return new Response(null, { status, headers });
},
all(url, request) {
const details = getDetails(url.searchParams.get('id'));
details.headers = [...request.headers];
details.method = request.method;
const headers = new Headers({
foo: 'bar',
hello: 'world',
'Content-Type': 'text/plain; charset=utf-8',
vary: 'cookie, origin',
});
const status = Number(url.searchParams.get('status')) || 200;
for (const acHeader of [
'allow-origin',
'allow-credentials',
'expose-headers',
]) {
const fullHeader = `access-control-${acHeader}`;
const value = url.searchParams.get(fullHeader);
if (value) headers.set(fullHeader, url.searchParams.get(fullHeader)!);
}
const cookieValues = url.searchParams.getAll('cookie-value');
for (const [i, name] of url.searchParams.getAll('cookie-name').entries()) {
const value = cookieValues[i] || '';
headers.append(
'Set-Cookie',
encodeURIComponent(name) +
'=' +
encodeURIComponent(value) +
'; Max-Age=86400; SameSite=None; Secure',
);
}
return new Response(status === 204 ? null : 'response! 😀', {
status,
headers,
});
},
});
addRoute('/resource-details', (url) => {
const details = getDetails(url.searchParams.get('id'));
return new Response(JSON.stringify(details), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});
});