-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
50 lines (44 loc) · 1.29 KB
/
index.js
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
// Note: change this URL to point to the OHTTP target.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Handle relayed requests.
* @param {Request} request
*/
async function handleRequest(request) {
const url = new URL(request.url);
// Check to see if metadata should be returned
if (url.pathname.startsWith("/metadata")) {
const data = request.cf !== undefined ?
request.cf :
{ error: "Metadata unavailable" }
return new Response(JSON.stringify(data, null, 2), {
headers: {
"content-type": "application/json;charset=UTF-8"
}
})
}
// Handle all other requests as relay requests
return handleRelayRequest(request)
}
async function handleRelayRequest(request) {
// Check method type and Content-Type header
if (request.method != 'POST') {
return new Response('Invalid request', { status: 400 })
}
const { headers } = request
const contentType = headers.get("content-type") || ""
if (!contentType.includes("message/ohttp-req")) {
return new Response('Invalid request', { status: 400 })
}
// Forward the request
const response = await fetch(TARGET, {
method: 'POST',
headers: {
"content-type": "message/ohttp-req",
},
body: request.body,
})
return response
}