-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.ts
More file actions
53 lines (48 loc) · 1.46 KB
/
ping.ts
File metadata and controls
53 lines (48 loc) · 1.46 KB
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
/**
* Ping endpoint for latency measurement
* Cloudflare Pages Function
*/
export async function onRequestGET(context: any) {
const { request } = context;
try {
const startTime = Date.now();
const clientTimestamp = new URL(request.url).searchParams.get('t');
const serverTimestamp = Date.now();
const response = {
timestamp: serverTimestamp,
clientTimestamp: clientTimestamp ? parseInt(clientTimestamp) : null,
serverProcessingTime: serverTimestamp - startTime,
message: 'pong'
};
return new Response(JSON.stringify(response), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
}
});
} catch (error) {
return new Response(JSON.stringify({ error: 'Internal server error' }), {
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
}
}
export async function onRequestOPTIONS() {
return new Response(null, {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
});
}