Refactor worker.js to handle requests and modify response

This commit is contained in:
严浩
2024-08-10 14:22:07 +08:00
parent b796f01443
commit 96d10ab6a0
3 changed files with 29 additions and 13 deletions

5
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"recommendations": [
"humao.rest-client"
]
}

10
src/request.http Normal file
View File

@ -0,0 +1,10 @@
###
GET http://localhost:8787/
###
GET http://localhost:8787/install.sh
###
GET http://localhost:8787/install.ps1

View File

@ -1,16 +1,17 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run "npm run dev" in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run "npm run deploy" to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
const url = new URL(request.url);
console.log('url :>> ', url);
if ("/" === url.pathname) {
return new Response(`curl -fsSL ${url.origin}/install.sh | sh -
iwr ${url.origin}/install.ps1 -useb | iex`);
}
let response = await fetch(`http://get.pnpm.io${url.pathname}`);
let text = await response.text();
text = text.replace(/https:\/\/github.com/g, "https://gh-cf.oo1.dev/https://github.com");
text=text.replace('Downloading pnpm from GitHub','Downloading pnpm from GitHub (via Cloudflare)');
text=text.replace('Downloading pnpm','Downloading pnpm (via Cloudflare)');
return new Response(text);
},
};