-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfonts.js
executable file
·71 lines (63 loc) · 2.13 KB
/
fonts.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
// Current Version: 1.0.6
// Description: Using Cloudflare Workers to speed up fonts.googleapis.com and fonts.gstatic.com's visiting.
addEventListener( "fetch", event =>
{
event.respondWith( handleRequest( event.request ) )
} )
async function handleRequest ( request )
{
let url = request.url.substr( 8 )
let path = url.split( "/" )[ 0 ]
url = url.substr( url.indexOf( "/" ) + 1 )
const response_css = await fetch( "https://fonts.googleapis.com/" + url )
const response_font = await fetch( "https://fonts.gstatic.com/" + url )
if ( !url || ( response_css.status !== 200 && response_font.status !== 200 ) )
{
return new Response( "404 Not Found", {
status: 404,
headers: {
"Access-Control-Allow-Origin": "*",
"content-type": "text/plain;charset=UTF-8",
},
} )
}
if ( response_css.status === 200 )
{
let css = await response_css.text()
css = css.replace( /fonts\.gstatic\.com/gim, path )
return new Response( css, {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"content-type": "text/css;charset=UTF-8",
},
} )
}
const fontExtMap = {
".collection": "font/collection;charset=UTF-8",
".eot": "application/vnd.ms-fontobject;charset=UTF-8",
".otf": "font/otf;charset=UTF-8",
".sfnt": "font/sfnt;charset=UTF-8",
".svg": "image/svg+xml;charset=UTF-8",
".ttf": "font/ttf;charset=UTF-8",
".woff": "font/woff;charset=UTF-8",
".woff2": "font/woff2;charset=UTF-8",
}
for ( const ext in fontExtMap )
{
if ( url.match( new RegExp( `${ ext }$` ) ) )
{
return new Response( response_font.body, {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"content-type": fontExtMap[ ext ],
},
} )
}
}
return new Response( response_font.body, {
status: 200,
headers: response_font.headers,
} )
}