-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (85 loc) · 3.85 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
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
import Router from 'cloudworker-router';
import config from './config';
import badgen from './helpers/badge';
import * as handlers from './mods';
const router = new Router();
async function routeHandler(ctx, handler) {
const { pathname } = new URL(ctx.request.href);
const badgeOpts = await handler(ctx.params);
const body = badgen(badgeOpts, ctx.query);
// console.log("👉 body:",body)
const req = ctx.event.request;
const country = req.headers.get('CF-IPCountry');
const ip = req.headers.get('CF-Connecting-IP');
const colo = (req.cf || {}).colo;
const http = (req.cf || {}).httpProtocol;
const tls = (req.cf || {}).tlsVersion;
const region = (req.cf || {}).region;
const asn = (req.cf || {}).asn;
const method = req.method;
// console.log("👉 log:",pathname.split('/')[1])
// console.log("👉 href:",(ctx.request.href))
const hhPatch = pathname.split('/')[1]
const _hh = config.hhMap[hhPatch];
ctx.body = body;
ctx.response.headers = {
'content-type': 'image/svg+xml',
'CDN-Cache-Control': `max-age=${_hh ? _hh : config.defaultCacheDurationSecond}, stale-if-error=60`,
'Cache-Control': `max-age=${_hh ? _hh : config.defaultCacheDurationSecond},immutable, s-max-age=${_hh ? _hh : config.defaultCacheDurationSecond}, stale-if-error=60`,
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, HEAD',
'dev-badge': `v.2.0-${country}-${ip}-${colo}-${http}-${tls}-${asn}-${method}`,
'Access-Control-Allow-Origin': '*',
'X-Content-Type-Options': 'nosniff'
};
ctx.status = 200;
// console.log("👉 log:",hhPatch,Object.values(ctx.response.headers).toString())
}
const rc = Math.random().toString(16).slice(-6);
const rc2 = Math.random().toString(16).slice(-6);
const handlerMap = {
'/*': () => ({ subject: "badge", status: 'v:' + "2.0", labelColor: rc2, color: rc }),
'/badge/:label/:status': handlers.badge,
'/badge/:label/:status/:color': handlers.badge,
'/npm/:topic/:pkgName': handlers.npm,
'/github/:topic/:owner/:repo': handlers.github,
'/bundlephobia/:topic/:pkgName': handlers.bundlephobia,
'/packagephobia/:topic/:pkgName': handlers.packagephobia,
'/packagephobia/:topic/:scope/:pkgName': handlers.packagephobia,
'/travis/:user/:repo/:branch': handlers.travis,
'/travis/:user/:repo': handlers.travis,
'/appveyor/:account/:project/:branch': handlers.appveyor,
'/appveyor/:account/:project': handlers.appveyor,
'/vs-marketplace/:topic/:pkgName': handlers.vsmarketplace,
'/docker/:topic/:scope/:name': handlers.docker,
'/docker/:topic/:scope/:name/:tag': handlers.docker,
'/docker/:topic/:scope/:name/:tag/:architecture': handlers.docker,
'/docker/:topic/:scope/:name/:tag/:architecture/:variant': handlers.docker,
'/amo/:topic/:pkgName': handlers.amo,
'/mozilla/:topic/:domain/:method': handlers.mozilla,
'/mozilla/:topic/:domain': handlers.mozilla,
'/jsdelivr/:topic/:period/:type/:name': handlers.jsdelivr,
'/uptimerobot/:topic/:period/:apikey': handlers.uptime,
'/lastfm/:topic/:user': handlers.lastfm,
'/lastfm/:topic/:user/:period': handlers.lastfm,
'/dependabot/:owner/:repo/:branch': handlers.dependabot,
'/spotify/:topic': handlers.spotify,
'/spotify/:topic/:period': handlers.spotify,
'/youtube/:topic/:id': handlers.youtube,
'/calendar/:topic': handlers.calendar,
'/actions/:user/:repo/:workflow/:branch/:event': handlers.actions,
'/actions/:user/:repo/:workflow/:event': handlers.actions,
'/actions/:user/:repo/:workflow': handlers.actions,
'/metrics/:topic/:script/:subtract': handlers.metrics,
};
router.get('/favicon.ico', async (ctx) => {
ctx.status = 204;
ctx.body = (null);
ctx.response.headers = {};
});
Object.entries(handlerMap).forEach(([path, handler]) => {
router.get(path, async (ctx) => routeHandler(ctx, handler));
});
// eslint-disable-next-line
addEventListener("fetch", (event) => {
event.respondWith(router.resolve(event));
});