diff --git a/src/__tests__/transmitter.test.ts b/src/__tests__/transmitter.test.ts index d91b04db..fd8cb1c4 100644 --- a/src/__tests__/transmitter.test.ts +++ b/src/__tests__/transmitter.test.ts @@ -267,7 +267,8 @@ describe("Transmitter", () => { nock("http://example.invalid") .get("/301") .reply(301, undefined, { - Location: "http://example.invalid/302" + Location: "http://example.invalid/302", + FooHeader: "Location" }) .get("/302") .reply(302, undefined, { diff --git a/src/transmitter.ts b/src/transmitter.ts index e3665ebd..62e56273 100644 --- a/src/transmitter.ts +++ b/src/transmitter.ts @@ -137,7 +137,7 @@ export class Transmitter { return stream => { const responseStatus = stream.statusCode ?? 999 const isRedirect = REDIRECT_STATUS_CODES.indexOf(responseStatus) !== -1 - const newURL = stream.headers?.location + const newURL = this.getLocationHeader(stream.rawHeaders || []) if (isRedirect && typeof newURL !== "undefined") { const redirectCount = callback[REDIRECT_COUNT] ?? 0 @@ -169,6 +169,22 @@ export class Transmitter { } } + // Temporary fix to deal with the header setter removal in Node.js 19 + // https://github.com/nodejs/node/issues/45510 + private getLocationHeader(rawHeaders: Array): string | undefined { + let location + rawHeaders.forEach((element, index) => { + // Skip odd indices as rawHeaders are represented as an array of pairs (key, value) + if (Math.abs(index % 2) == 1) return + + if (element == "Location") { + location = rawHeaders[index + 1] + } + }) + + return location + } + private configParams(): URLSearchParams { const config_data = this.#config.data