1- import type { Fetcher , Request } from "@cloudflare/workers-types" ;
2- import { minimatch } from 'minimatch'
1+ import { type Fetcher , type Request } from "@cloudflare/workers-types" ;
32
43interface Env {
54 ASSETS : Fetcher ;
@@ -13,12 +12,12 @@ interface Redirect {
1312
1413type Redirects = ReadonlyArray < Redirect > ;
1514
16- type Headers = Record < string , Record < string , string > > ;
15+ type HeaderPatterns = Record < string , Record < string , string > > ;
1716
18- const headerPatterns : Headers = {
17+ const headerPatterns : HeaderPatterns = {
1918 // Headers which are common to all paths. This includes most security
2019 // headers.
21- "/*" : {
20+ "/. *" : {
2221 "X-Content-Type-Options" : "nosniff" ,
2322 "X-Frame-Options" : "DENY" ,
2423 "Content-Security-Policy" : "default-src 'self'; connect-src *; img-src 'self' data:; script-src 'self' 'sha256-dNGbdYMnwBYenrRGOHR0l33DkR37uJKlpRHFVeG85Lk=' https://umami.acearchive.lgbt; style-src 'self' 'unsafe-inline'; base-uri 'self'; frame-ancestors 'none';" ,
@@ -30,34 +29,34 @@ const headerPatterns: Headers = {
3029 // Path-specific caching headers. We want to employ a different caching
3130 // strategy with files that have cache-busting filenames (e.g. JS/CSS) vs
3231 // those that do not (e.g. HTML).
33- "/js/* .js" : {
32+ "/js/[^/]+\\ .js" : {
3433 "Cache-Control" : "public, immutable, max-age=31536000" ,
3534 } ,
36- "/js/vendor/* " : {
35+ "/js/vendor/.+ " : {
3736 "Cache-Control" : "no-cache" ,
3837 } ,
39- "/js/vendor/bootstrap/dist/js/* .js" : {
38+ "/js/vendor/bootstrap/dist/js/[^/]+\\ .js" : {
4039 "Cache-Control" : "public, immutable, max-age=31536000" ,
4140 } ,
42- "/css/* .css" : {
41+ "/css/[^/]+\\ .css" : {
4342 "Cache-Control" : "public, immutable, max-age=31536000" ,
4443 } ,
45- "/css/vendor/* " : {
44+ "/css/vendor/.+ " : {
4645 "Cache-Control" : "no-cache" ,
4746 } ,
48- "/icons/* " : {
47+ "/icons/.+ " : {
4948 "Cache-Control" : "public, immutable, max-age=31536000" ,
5049 } ,
51- "/fonts/* " : {
50+ "/fonts/.+ " : {
5251 "Cache-Control" : "public, immutable, max-age=31536000" ,
5352 } ,
5453 // Headers specific to Stoplight Elements, the app we embed for rendering the
5554 // OpenAPI docs.
56- "/docs/api/* " : {
55+ "/docs/api/.+ " : {
5756 "Content-Security-Policy" : "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; connect-src https://raw.githubusercontent.com https://api.acearchive.lgbt; frame-ancestors 'none';" ,
5857 } ,
5958 // File-specific header overrides.
60- "/cute.gif" : {
59+ "/cute\\ .gif" : {
6160 "Content-Type" : "image/webp" ,
6261 }
6362} ;
@@ -66,35 +65,31 @@ export default {
6665 async fetch ( request : Request , env : Env ) {
6766 const requestUrl = new URL ( request . url ) ;
6867
69- console . log ( `Request URL: ${ requestUrl . pathname } ` ) ;
70-
7168 const redirectsUrl = new URL ( request . url ) ;
7269 redirectsUrl . pathname = "/redirects.json" ;
7370
7471 const redirectsResponse = await env . ASSETS . fetch ( redirectsUrl ) ;
7572 const redirects : Redirects = await redirectsResponse . json ( ) ;
7673
7774 for ( const redirect of redirects ) {
78- console . log ( `Checking redirect: ${ redirect . from } -> ${ redirect . to } (${ redirect . status } )` ) ;
79-
80- if ( minimatch ( requestUrl . pathname , redirect . from ) ) {
81- console . log ( `Redirecting: ${ redirect . from } -> ${ redirect . to } (${ redirect . status } )` ) ;
75+ if ( requestUrl . pathname === redirect . from ) {
8276 const url = new URL ( request . url ) ;
8377 url . pathname = redirect . to ;
8478 return Response . redirect ( url . toString ( ) , redirect . status ) ;
8579 }
8680 }
8781
88- const responseHeaders = new Headers ( ) ;
82+ const response = await env . ASSETS . fetch ( request ) ;
83+ const newResponse = new Response ( response . body , response ) ;
8984
9085 for ( const [ pattern , patternHeaders ] of Object . entries ( headerPatterns ) ) {
91- if ( minimatch ( requestUrl . pathname , pattern ) ) {
86+ if ( new RegExp ( pattern ) . test ( requestUrl . pathname ) ) {
9287 for ( const [ key , value ] of Object . entries ( patternHeaders ) ) {
93- responseHeaders . set ( key , value ) ;
88+ newResponse . headers . set ( key , value ) ;
9489 }
9590 }
9691 }
9792
98- return env . ASSETS . fetch ( request , { headers : responseHeaders } ) ;
93+ return newResponse ;
9994 } ,
10095} ;
0 commit comments