summaryrefslogtreecommitdiff
path: root/src/index.js
blob: 44812de75e18d752a4413c2374171d4888716a53 (plain) (blame)
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
/*
 ** We support the GET, POST, HEAD, and OPTIONS methods from any origin, and accept the Content-Type header on requests.
 ** These headers must be present on all responses to all CORS requests.
 ** In practice, this means all responses to OPTIONS requests.
 **
 ** Modified by Alejandro Akbal (VoidlessSeven7)
 */

/**
 * Fetches the request content and returns it
 * @param {*} request
 */
async function handleRequest(request) {
	// Initialize url and query
	const url = new URL(request.url);
	const query = url.search.split('?q=')[1];

	/*
	 * Rewrite request to point to API url. This also makes the request mutable
	 * so we can add the correct Origin header to make the API server think
	 * that this request isn't cross-site.
	 */

	request = new Request(query, request);

	/*
	 * Set headers to make the endpoint think it's itself
	 */
	request.headers.set('Host', new URL(query).origin);
	request.headers.set('Referer', new URL(query));
	request.headers.set('Origin', new URL(query));
	request.headers.set('Access-Control-Allow-Credentials', 'true');
	request.headers.delete('X-Content-Type-Options');

	// Fetch it
	let response = await fetch(request);

	// Recreate the response so we can modify the headers
	response = new Response(response.body, response);

	// Set CORS headers
	response.headers.set('Access-Control-Allow-Origin', 'https://due.moe');

	// Append to/Add Vary header so browser will cache response correctly
	response.headers.append('Vary', 'Origin');

	response.headers.set('Cache-Control', 'max-age=300');

	// Return it
	return response;
}

/**
 * Makes sure that the necessary headers are present for this to be a valid pre-flight request
 * @param {*} request
 */
async function handleOptions(request) {
	/*
	 * Handle CORS pre-flight request.
	 * If you want to check the requested method + headers you can do that here.
	 */
	if (
		request.headers.get('Origin') !== null &&
		request.headers.get('Access-Control-Request-Method') !== null &&
		request.headers.get('Access-Control-Request-Headers') !== null
	) {
		return new Response(null, {
			headers: {
				'Access-Control-Allow-Origin': '*',
				'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
				'Access-Control-Allow-Headers': '*',
			},
		});

		/*
		 * Handle standard OPTIONS request.
		 * If you want to allow other HTTP Methods, you can do that here.
		 */
	} else {
		return new Response(null, {
			headers: {
				Allow: 'GET, HEAD, POST, OPTIONS',
			},
		});
	}
}

/*
 ** Event listener for fetching content (What starts everything)
 */
addEventListener('fetch', (event) => {
	// Initialize
	const request = event.request;

	// Handle CORS preflight requests
	switch (request.method) {
		case 'OPTIONS':
			event.respondWith(handleOptions(request));
			break;

		// Handle requests
		case 'GET':
		case 'HEAD':
		case 'POST':
			event.respondWith(handleRequest(request));
			break;

		// If no good option then return error
		default:
			event.respondWith(async () => {
				return new Response(null, {
					status: 405,

					statusText: 'Method Not Allowed',
				});
			});
			break;
	}
});