Skip to content

Commit

Permalink
Proxy no longer rewrites hostname, instead uses worker origin request…
Browse files Browse the repository at this point in the history
… forwarding
  • Loading branch information
ericallam committed Oct 24, 2024
1 parent 212f853 commit af427aa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
3 changes: 2 additions & 1 deletion apps/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev"
"dev": "wrangler dev",
"dry-run:staging": "wrangler deploy --dry-run --outdir=dist --env staging"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240512.0",
Expand Down
22 changes: 2 additions & 20 deletions apps/proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ export interface Env {

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (!env.REWRITE_HOSTNAME) throw new Error("Missing REWRITE_HOSTNAME");
console.log("url", request.url);

if (!queueingIsEnabled(env)) {
console.log("Missing AWS credentials. Passing through to the origin.");
return redirectToOrigin(request, env);
return fetch(request);
}

const url = new URL(request.url);
Expand All @@ -42,25 +39,10 @@ export default {
}

//the same request but with the hostname (and port) changed
return redirectToOrigin(request, env);
return fetch(request);
},
};

function redirectToOrigin(request: Request, env: Env) {
const newUrl = new URL(request.url);
newUrl.hostname = env.REWRITE_HOSTNAME;
newUrl.port = env.REWRITE_PORT || newUrl.port;

const requestInit: RequestInit = {
method: request.method,
headers: request.headers,
body: request.body,
};

console.log("rewritten url", newUrl.toString());
return fetch(newUrl.toString(), requestInit);
}

function queueingIsEnabled(env: Env) {
return (
env.AWS_SQS_ACCESS_KEY_ID &&
Expand Down

0 comments on commit af427aa

Please sign in to comment.