From 90a16da48ef2d0f1f31fc4753f42b347d782da2c Mon Sep 17 00:00:00 2001 From: gregfromstl Date: Fri, 13 Dec 2024 05:48:04 +0000 Subject: [PATCH] [Service Utils] Feature: Accept Request in `authorizeNode` (#5724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR focuses on enhancing the `authorizeNode` function by improving type handling for request headers and refactoring how headers are accessed. It introduces a type guard for distinguishing between header types and updates the way headers are retrieved in various functions. ### Detailed summary - Updated `req` type in `AuthInput` to allow `Request`. - Added `isNodeHeaders` function to differentiate between `IncomingHttpHeaders` and `Headers`. - Refactored `getHeader` function to use `isNodeHeaders` for retrieving headers. - Modified `extractAuthorizationData` to use `getHeader` for constructing the URL. - Updated `logHttpRequest` to use `getHeader` for SDK-related headers. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .changeset/tidy-squids-end.md | 5 ++++ packages/service-utils/src/node/index.ts | 30 ++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 .changeset/tidy-squids-end.md diff --git a/.changeset/tidy-squids-end.md b/.changeset/tidy-squids-end.md new file mode 100644 index 00000000000..4f00a5d5b94 --- /dev/null +++ b/.changeset/tidy-squids-end.md @@ -0,0 +1,5 @@ +--- +"@thirdweb-dev/service-utils": patch +--- + +Accept Request in authorizeNode diff --git a/packages/service-utils/src/node/index.ts b/packages/service-utils/src/node/index.ts index 08be8fe5c82..ec181da4939 100644 --- a/packages/service-utils/src/node/index.ts +++ b/packages/service-utils/src/node/index.ts @@ -18,7 +18,7 @@ export * from "../core/services.js"; type NodeServiceConfig = CoreServiceConfig; export type AuthInput = CoreAuthInput & { - req: IncomingMessage; + req: IncomingMessage | Request; }; /** @@ -66,15 +66,21 @@ export async function authorizeNode( return await authorize(authData, serviceConfig, cacheOptions); } +function isNodeHeaders( + headers: IncomingHttpHeaders | Headers, +): headers is IncomingHttpHeaders { + return typeof headers === "object" && !("get" in headers); +} + function getHeader( - headers: IncomingHttpHeaders, + headers: IncomingHttpHeaders | Headers, headerName: string, ): string | null { - const header = headers[headerName]; - if (Array.isArray(header)) { - return header?.[0] ?? null; - } - return header ?? null; + return isNodeHeaders(headers) + ? Array.isArray(headers[headerName]) + ? (headers[headerName]?.[0] ?? null) + : (headers[headerName] ?? null) + : (headers.get(headerName) ?? null); } export function extractAuthorizationData( @@ -85,7 +91,7 @@ export function extractAuthorizationData( try { requestUrl = new URL( authInput.req.url || "", - `http://${authInput.req.headers.host}`, + `http://${getHeader(authInput.req.headers, "host")}`, ); } catch (error) { console.log("** Node URL Error **", error); @@ -223,10 +229,10 @@ export function logHttpRequest({ isAuthed, status: res.statusCode, statusMessage, - sdkName: headers["x-sdk-name"] ?? undefined, - sdkVersion: headers["x-sdk-version"] ?? undefined, - platform: headers["x-sdk-platform"] ?? undefined, - os: headers["x-sdk-os"] ?? undefined, + sdkName: getHeader(headers, "x-sdk-name"), + sdkVersion: getHeader(headers, "x-sdk-version"), + platform: getHeader(headers, "x-sdk-platform"), + os: getHeader(headers, "x-sdk-os"), latencyMs, }), );