Skip to content

Commit

Permalink
feat: support proxy headers (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Jan 18, 2024
1 parent 2bb33a3 commit bea6faa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/node_modules/@internal/micro-frame-component/node.marko
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ static async function fetchStream(input, out) {
incomingHeaders = Object.fromEntries(request.headers);
origin = url.origin;
const forwardedProto = request.headers.get("x-forwarded-proto");
const forwardedHost = request.headers.get("x-forwarded-host");
origin = forwardedHost && forwardedProto ? `${forwardedProto}://${forwardedHost}` : url.origin;
} else {
const incomingMessage = out.stream && (out.stream.req || out.stream.request) || out.global.req || out.global.request;
if (!incomingMessage) {
throw new Error("Could not get request from stream/global. Please assign out.global.req with proper request object.");
}
incomingHeaders = incomingMessage.headers;
origin = `${incomingMessage.protocol}://${incomingHeaders.host}`;
const protocol =
incomingHeaders["x-forwarded-proto"] ||
incomingMessage.protocol;
const host =
incomingHeaders["x-forwarded-host"] ||
incomingHeaders.host;
origin = `${protocol}://${host}`;
}
Expand Down
12 changes: 10 additions & 2 deletions src/node_modules/@internal/stream-source-component/node.marko
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ $ const request = async () => {
incomingHeaders = Object.fromEntries(request.headers);
origin = url.origin;
const forwardedProto = request.headers.get("x-forwarded-proto");
const forwardedHost = request.headers.get("x-forwarded-host");
origin = forwardedHost && forwardedProto ? `${forwardedProto}://${forwardedHost}` : url.origin;
} else {
const incomingMessage = out.stream && (out.stream.req || out.stream.request) || out.global.req || out.global.request;
if (!incomingMessage) {
throw new Error("Could not get request from stream/global. Please assign out.global.req with proper request object.");
}
incomingHeaders = incomingMessage.headers;
origin = `${incomingMessage.protocol}://${incomingHeaders.host}`;
const protocol =
incomingHeaders["x-forwarded-proto"] ||
incomingMessage.protocol;
const host =
incomingHeaders["x-forwarded-host"] ||
incomingHeaders.host;
origin = `${protocol}://${host}`;
}
const url = new URL(input.src, origin);
Expand Down

0 comments on commit bea6faa

Please sign in to comment.