-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
64 lines (52 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const CUSTOM_DOMAINS = {
'npm.example.com': 'https://registry.npmjs.org',
'yarn.example.com': 'https://registry.yarnpkg.com',
// 你可以添加更多的镜像地址
};
const DEFAULT_UPSTREAM = CUSTOM_DOMAINS['npm.example.com'];
const json = (data = {}, code = 200, headers = {}) => {
return new Response(JSON.stringify(data), {
status: code,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'X-Powered-By': 'X-Powered-By cloudflare npm proxy',
...headers,
},
});
};
// 路由函数,根据请求的主机名选择相应的上游地址
function routeByHosts(host) {
return CUSTOM_DOMAINS[host] || DEFAULT_UPSTREAM;
}
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const upstream = routeByHosts(url.hostname);
if (request.method !== 'GET') {
return Response.redirect(`${upstream}${url.pathname}`, 302);
}
if (url.pathname === '/') {
return new Response(`Hello ${url.hostname}`);
}
const fetchUrl = `${upstream}${url.pathname}`;
const response = await fetch(fetchUrl);
if (url.pathname.endsWith('.tgz')) {
const contentType = response.headers.get('content-type');
if (contentType && contentType.indexOf('application/json') !== -1) {
const errorData = await response.json();
return json({ error: `Fetch failed: ${errorData.message}` }, 500);
} else {
return response;
}
}
const data = await response.json();
if (data.error) {
return json(data, 500);
}
if (data && data.dist && data.dist.tarball) {
data.dist.tarball = data.dist.tarball.replace(upstream, url.hostname);
}
return json(data);
},
};