(request)
| 1 | export default { |
| 2 | async fetch(request) { |
| 3 | const url = new URL(request.url); |
| 4 | const path = url.pathname.substring(1); |
| 5 | |
| 6 | if (path === "locations") { |
| 7 | return locations_cn(request); |
| 8 | } |
| 9 | |
| 10 | let bytes = 100000000; // Default 100MB |
| 11 | if (path) { |
| 12 | const match = path.match(/^(\d+)([a-z]{0,2})$/i); |
| 13 | if (!match) { |
| 14 | return new Response("路径格式不正确", { status: 400 }); |
| 15 | } |
| 16 | |
| 17 | const size = parseInt(match[1], 10); |
| 18 | const unit = match[2].toLowerCase(); |
| 19 | const multipliers = { |
| 20 | 'k': 1000, 'kb': 1000, |
| 21 | 'm': 1000000, 'mb': 1000000, |
| 22 | 'g': 1000000000, 'gb': 1000000000 |
| 23 | }; |
| 24 | bytes = size * (multipliers[unit] || 1); |
| 25 | } |
| 26 | |
| 27 | const targetUrl = `https://speed.cloudflare.com/__down?bytes=${bytes}`; |
| 28 | const headers = new Headers(request.headers); |
| 29 | headers.set('referer', 'https://speed.cloudflare.com/'); |
| 30 | |
| 31 | return fetch(new Request(targetUrl, { |
| 32 | method: request.method, |
| 33 | headers: headers, |
| 34 | body: request.body |
| 35 | })); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | async function locations_cn(request) { |
no test coverage detected