Skip to content

Commit

Permalink
Add fallback for Request.url.protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
witoszekdev committed Jan 10, 2025
1 parent 1b87494 commit d5e8785
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/handlers/fetch-api/create-manifest-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type CreateManifestHandlerOptions = {
export const createManifestHandler =
(options: CreateManifestHandlerOptions) => async (request: Request) => {
const { schemaVersion } = getSaleorHeadersFetchAPI(request.headers);
const baseURL = getBaseUrlFetchAPI(request.headers);
const baseURL = getBaseUrlFetchAPI(request);

const manifest = await options.manifestFactory({
appBaseUrl: baseURL,
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/fetch-api/process-protected-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ProtectedHandlerError extends Error {
}

interface ProcessSaleorProtectedHandlerArgs {
request: Pick<Request, "headers">;
request: Request;
apl: APL;
requiredPermissions?: Permission[];
}
Expand Down Expand Up @@ -71,7 +71,7 @@ export const processSaleorProtectedHandler: ProcessAsyncSaleorProtectedHandler =
request.headers
);

const baseUrl = getBaseUrlFetchAPI(request.headers);
const baseUrl = getBaseUrlFetchAPI(request);

span.setAttribute("saleorApiUrl", saleorApiUrl ?? "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const processSaleorWebhook = async <T>({
}

const { event, signature, saleorApiUrl } = getSaleorHeadersFetchAPI(req.headers);
const baseUrl = getBaseUrlFetchAPI(req.headers);
const baseUrl = getBaseUrlFetchAPI(req);

if (!baseUrl) {
debug("Missing host header");
Expand Down
27 changes: 22 additions & 5 deletions src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,29 @@ export const getBaseUrl = (headers: { [name: string]: string | string[] | undefi
return `${protocol}://${host}`;
};

export const getBaseUrlFetchAPI = (headers: Headers) => {
const host = headers.get("host");
const xForwardedProto = headers.get("x-forwarded-proto") || "http";
export const getBaseUrlFetchAPI = (request: Request) => {
let url: URL | undefined;
try {
url = new URL(request.url);
} catch (e) {
// no-op
}

const protocols = xForwardedProto.split(",").map((value) => value.trimStart());
const protocol = protocols.find((el) => el === "https") || protocols[0];
const host = request.headers.get("host");
const xForwardedProto = request.headers.get("x-forwarded-proto");

let protocol: string;
if (xForwardedProto) {
const xForwardedForProtocols = xForwardedProto.split(",").map((value) => value.trimStart());
protocol = xForwardedForProtocols.find((el) => el === "https") || xForwardedForProtocols[0];
} else if (url) {
// Some providers (e.g. Deno Deploy)
// do not set x-forwarded-for header when handling request
// try to get it from URL
protocol = url.protocol.replace(":", "");
} else {
protocol = "http";
}

return `${protocol}://${host}`;
};

0 comments on commit d5e8785

Please sign in to comment.