-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
122 lines (104 loc) · 3.91 KB
/
worker.js
File metadata and controls
122 lines (104 loc) · 3.91 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
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
120
121
122
// api.wulong.dev — API Gateway Worker
// Routes requests to upstream data sources
// Use GitHub raw content instead of Pages to avoid redirect issues
const UPSTREAM = "https://raw.githubusercontent.com/oolong-tea-2026/arena-ai-leaderboards/main/data";
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
function jsonResponse(data, status = 200) {
return new Response(JSON.stringify(data, null, 2), {
status,
headers: { "Content-Type": "application/json", ...CORS_HEADERS },
});
}
function errorResponse(message, status) {
return jsonResponse({ error: message }, status);
}
async function fetchJSON(url) {
const resp = await fetch(url, {
headers: { "User-Agent": "api-gateway-worker" },
});
if (!resp.ok) return { ok: false, status: resp.status };
return { ok: true, data: await resp.json() };
}
// GET /arena-ai-leaderboards/v1/leaderboards — list all leaderboards
// GET /arena-ai-leaderboards/v1/leaderboard?name=text — single leaderboard (latest)
// GET /arena-ai-leaderboards/v1/leaderboard?name=text&date=2026-03-19 — historical
async function handleArena(path, params) {
if (path === "/" || path === "") {
return jsonResponse({
service: "arena-ai-leaderboards",
version: "v1",
endpoints: [
"GET /arena-ai-leaderboards/v1/leaderboards",
"GET /arena-ai-leaderboards/v1/leaderboard?name={slug}",
"GET /arena-ai-leaderboards/v1/leaderboard?name={slug}&date={YYYY-MM-DD}",
],
});
}
if (path === "/leaderboards") {
const date = params.get("date");
if (date) {
const result = await fetchJSON(`${UPSTREAM}/${date}/_index.json`);
if (!result.ok) return errorResponse(`Index not found for date: ${date}`, 404);
return jsonResponse(result.data);
}
// Latest: resolve date from latest.json, then return _index.json
const latest = await fetchJSON(`${UPSTREAM}/latest.json`);
if (!latest.ok) return errorResponse("Failed to fetch latest index", 502);
const dateDir = latest.data.date || latest.data.latest;
const result = await fetchJSON(`${UPSTREAM}/${dateDir}/_index.json`);
if (!result.ok) return errorResponse("Index not found", 404);
return jsonResponse(result.data);
}
if (path === "/leaderboard") {
const name = params.get("name");
if (!name) return errorResponse("Missing required parameter: name", 400);
const date = params.get("date");
let dateDir;
if (date) {
dateDir = date;
} else {
const latest = await fetchJSON(`${UPSTREAM}/latest.json`);
if (!latest.ok) return errorResponse("Failed to fetch latest index", 502);
dateDir = latest.data.date || latest.data.latest;
}
const result = await fetchJSON(`${UPSTREAM}/${dateDir}/${name}.json`);
if (!result.ok) return errorResponse(`Leaderboard not found: ${name}`, 404);
return jsonResponse(result.data);
}
return null;
}
export default {
async fetch(request) {
const url = new URL(request.url);
if (request.method === "OPTIONS") {
return new Response(null, { headers: CORS_HEADERS });
}
if (request.method !== "GET") {
return errorResponse("Method not allowed", 405);
}
// Root — service directory
if (url.pathname === "/" || url.pathname === "") {
return jsonResponse({
name: "api.wulong.dev",
services: {
"arena-ai-leaderboards": {
version: "v1",
base: "/arena-ai-leaderboards/v1",
},
},
});
}
// Route: /arena-ai-leaderboards/v1/...
const arenaPrefix = "/arena-ai-leaderboards/v1";
if (url.pathname.startsWith(arenaPrefix)) {
const subpath = url.pathname.slice(arenaPrefix.length) || "/";
const result = await handleArena(subpath, url.searchParams);
if (result) return result;
}
return errorResponse("Not found", 404);
},
};