File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // Copy this whole code into cloudflare worker and add WORKER_URL in environment variable.
2+
3+ async function gatherResponse ( response ) {
4+ const { headers } = response
5+ const contentType = headers . get ( "content-type" ) || ""
6+ if ( contentType . includes ( "application/json" ) ) {
7+ return JSON . stringify ( await response . json ( ) )
8+ }
9+ else if ( contentType . includes ( "application/text" ) ) {
10+ return response . text ( )
11+ }
12+ else if ( contentType . includes ( "text/html" ) ) {
13+ return response . text ( )
14+ }
15+ else {
16+ return response . text ( )
17+ }
18+ }
19+
20+ async function handleRequest ( request ) {
21+
22+ let method = request . method ;
23+ let request_headers = request . headers ;
24+ let new_request_headers = new Headers ( request_headers ) ;
25+ let url = new URL ( request . url )
26+ let params = new URLSearchParams ( url . search . substring ( 1 ) ) ;
27+
28+ const path = decodeURIComponent ( url . searchParams . get ( 'url' ) ) ;
29+ const response = await fetch ( path , {
30+ method : method ,
31+ headers : new_request_headers
32+ } )
33+ const results = await gatherResponse ( response )
34+ return new Response ( results , {
35+ headers : response . headers
36+ } )
37+ }
38+
39+ addEventListener ( "fetch" , async event => {
40+ event . respondWith ( handleRequest ( event . request ) )
41+ } )
You can’t perform that action at this time.
0 commit comments