-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker_template.js
112 lines (89 loc) · 3.06 KB
/
worker_template.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
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
const path = new URL(event.request.url).pathname.substr(1)
let status = 200
const headers = {
'Content-Type': 'text/html; charset=utf-8',
}
let content = ''
if (path in versions.content) {
headers['Content-Type'] = 'text/javascript'
headers['Access-Control-Allow-Origin'] = '*'
headers['Cache-Control'] = 'max-age=' + (60 * 60 * 24 * 30)
content = versions.content[path]
if (Math.random() <= config.logRequestRate) {
event.waitUntil(logRequest(event.request, path))
}
}
else if (path in scripts) {
headers['Content-Type'] = 'text/javascript'
headers['Cache-Control'] = 'max-age=60'
content = scripts[path]
}
else if (`page__${pagePath(path)}__content` in pages) {
headers['Cache-Control'] = 'max-age=5'
content = generatePage(path)
}
else if (path == 'favicon.ico') {
headers['Content-Type'] = 'image/png'
content = (await fetch('https://cdnstats.instant.page/favicon.ico')).body
}
else if (path == 'twitter_summary_image_v2.png') {
headers['Content-Type'] = 'image/png'
content = (await fetch('https://cdnstats.instant.page/twitter_summary_image_v2.png')).body
}
else if (/\.(png|svg|jpg|gif|ico)$/.test(path)) {
const response = await fetch(`https://assets.instant.page/${path}`)
return response
}
else {
status = 404
content = `404 page not found<br><a href="/">home page</a>`
}
event.waitUntil(getGithubStars())
return new Response(content, {
status,
headers,
})
}
const versions = {}
versions.latest = __VERSIONS_LATEST__
versions.hashes = {}
__VERSIONS_HASHES__
versions.content = {}
__VERSIONS_CONTENT__
const scripts = {}
__SCRIPTS__
const getStylesheet = () => __STYLESHEET__
const pages = {}
__PAGES__
__GENERATE_PAGE__
const config = __CONFIG__
let githubStars
let githubStarsLastFetch = 0
async function getGithubStars() {
if (+new Date - githubStarsLastFetch < 60 * 60 * 1000) {
return
}
githubStarsLastFetch = +new Date
const response = await fetch(`https://api.github.com/repos/instantpage/instant.page`, {
headers: {
'User-Agent': 'instantpage',
'Authorization': `Bearer ${config.githubAccessToken}`,
}
})
const json = await response.json()
githubStars = json.stargazers_count
}
async function logRequest(request, version) {
const key = encodeURIComponent(config.serversidestatsKey)
const method = encodeURIComponent(request.method)
const host = encodeURIComponent(request.headers.get("referer") && new URL(request.headers.get("referer")).host || '')
const userAgent = encodeURIComponent(request.headers.get("user-agent"))
const country = encodeURIComponent(request.headers.get("cf-ipcountry"))
const protocol = new URL(request.url).protocol
const tlsVersion = request.cf.tlsVersion
await fetch(`https://cdnstats.instant.page/?key=${key}&method=${method}&protocol=${protocol}&host=${host}&versionNumber=${version}&userAgent=${userAgent}&country=${country}&tlsVersion=${tlsVersion}`)
}