Skip to content

Commit 2972144

Browse files
author
AffanTheBest
committed
Add worker code
Using worker to avoid ip blocking from Amazon & Flipkart
1 parent 8cd346f commit 2972144

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

worker.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
})

0 commit comments

Comments
 (0)