This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (149 loc) · 4.24 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const Constants = require('./constants');
const injectScript = require('raw-loader!./bookmarklet/inject.js');
/** @constant {string} CACHE_TTL Cache Time-To-Live */
const CACHE_TTL = 1 * 24 * 3600;
/** Register worker handler */
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
/** @constant {string} GFONTS_API_KEY */
const GFONTS_API_KEY = Constants.GFONTS_API_KEY;
/** @constant {string} GFONTS_REFERRER */
const GFONTS_REFERRER = Constants.GFONTS_REFERRER;
/** @constant {string} GITHUB_URL */
const GITHUB_URL = `https://github.com/rensatsu/font-tester-worker`;
/**
* Fetch fonts list from Google Fonts.
*
* @returns {(object|null)} JSON list of fonts or null.
*/
async function getFontsList() {
try {
const apiUrl = new URL('https://www.googleapis.com/webfonts/v1/webfonts');
apiUrl.searchParams.append('sort', 'alpha');
apiUrl.searchParams.append('key', GFONTS_API_KEY);
const apiHeaders = new Headers();
apiHeaders.append('referer', GFONTS_REFERRER);
const resp = await fetch(apiUrl, {
headers: apiHeaders,
cf: {
cacheEverything: true,
cacheTtl: CACHE_TTL,
},
});
if (!resp.ok) return null;
return await resp.json();
} catch (e) {
return null;
}
}
/**
* Generate an error response.
*
* @param {number} [code=404] HTTP Status Code
* @param {string} [text='Not found'] Text of the response
* @returns
*/
function errorResponse(code = 404, text = 'Not found') {
const response = new Response(text, {
status: code >= 200 && code <= 599 ? code : 500,
});
response.headers.set('Content-Type', 'text/plain');
addCors(response);
return response;
}
/**
* Add CORS header to the response.
*
* @param {Response} response Response object.
*/
function addCors(response) {
response.headers.set('Access-Control-Allow-Origin', '*');
}
/**
* Handle request to get fonts list.
*
* @returns {Response} Response object.
*/
async function handleGetFonts() {
const gFontsList = await getFontsList();
if (!gFontsList) {
return errorResponse(500, 'Unable to fetch fonts list');
}
if (!('items' in gFontsList)) {
return errorResponse(500, 'Unable to parse fonts list');
}
const fonts = [];
gFontsList.items.forEach(font => {
if (font.subsets.includes('cyrillic')) {
fonts.push(font.family);
}
});
const jsonResp = {
fonts: fonts,
};
const response = new Response(JSON.stringify(jsonResp), { status: 200 });
response.headers.set('Content-Type', 'application/json');
response.headers.set('Cache-Control', `max-age=${CACHE_TTL}`);
addCors(response);
return response;
}
/**
* Handle request to get debug information.
*
* @returns {Response} Response object.
*/
async function handleGetDebug() {
const jsonResp = {
build: 'GIT_COMMIT' in Constants ? Constants.GIT_COMMIT : null,
home: GITHUB_URL,
};
const response = new Response(JSON.stringify(jsonResp), { status: 200 });
response.headers.set('Content-Type', 'application/json');
return response;
}
/**
* Handle request to get information about this app.
*
* @returns {Response} Response object.
*/
function handleGetInfo() {
const response = new Response('Redirecting', { status: 302 });
response.headers.set('Location', GITHUB_URL);
return response;
}
/**
* Handle request to get injector script.
*
* @returns {Response} Response object.
*/
async function handleGetInjectJs() {
const gInjScript = injectScript.default;
if (!gInjScript) {
return errorResponse(500, '// Unable to fetch inject script');
}
const response = new Response(gInjScript, { status: 200 });
response.headers.set('Content-Type', 'application/javascript');
response.headers.set('Cache-Control', `max-age=${CACHE_TTL}`);
addCors(response);
return response;
}
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
const path = new URL(request.url).pathname.replace(/^[\/]{1}/, '');
switch (path) {
case '':
return await handleGetInfo();
case 'fonts':
return await handleGetFonts();
case 'inject.js':
return await handleGetInjectJs();
case 'debug':
return await handleGetDebug();
default:
return errorResponse(404, 'Not found');
}
}