Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@cfworker/json-schema": "^4.0.3",
"@hono/node-server": "^1.3.3",
"@hono/node-server": "^1.19.5",
"@hono/node-ws": "^1.2.0",
"@portkey-ai/mustache": "^2.1.3",
"@smithy/signature-v4": "^2.1.1",
Expand Down
13 changes: 12 additions & 1 deletion src/handlers/services/responseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export class ResponseService {
));
}

this.updateHeaders(finalMappedResponse, cache.cacheStatus, retryAttempt);
finalMappedResponse = this.updateHeaders(
finalMappedResponse,
cache.cacheStatus,
retryAttempt
);

return {
response: finalMappedResponse,
Expand Down Expand Up @@ -99,6 +103,13 @@ export class ResponseService {
cacheStatus: string | undefined,
retryAttempt: number
) {
// Clone response and headers to work around `immutable` header guard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expensive operation, have to see if there are better ways to solve this, dont want to. be slowing down the fastest gateway xd

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood. i attempted to access the header guard to modify it in place, but undici hides it in a private field (link) and deletes the getters and setters (link).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should work:

// Clone headers to work around `immutable` header guard
const kHeaders = Object.getOwnPropertySymbols(response).find((symbol) => symbol.toString() === 'Symbol(headers)');
Object.defineProperty(response, kHeaders, {
  value: new Headers(response.headers),
  writable: true,
  enumerable: true,
  configurable: true,
});

do we like that better? the benefit is that we don't need to construct a new response, but it is making some unsafe assumptions about internals.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another option:

// Capture internal `getHeadersGuard` and `setHeadersGuard`
let getHeadersGuard, setHeadersGuard
const deleteProperty = Reflect.deleteProperty
Reflect.deleteProperty = function (...args) {
  if (args[1] === 'getHeadersGuard') getHeadersGuard = args[0].getHeadersGuard
  if (args[1] === 'setHeadersGuard') setHeadersGuard = args[0].setHeadersGuard
  return deleteProperty.call(this, ...args)
}

// then
setHeadersGuard(response.headers, 'response')

response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: new Headers(response.headers),
});

// Append headers directly
response.headers.append(
RESPONSE_HEADER_KEYS.LAST_USED_OPTION_INDEX,
Expand Down
20 changes: 12 additions & 8 deletions tests/unit/src/handlers/services/responseService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe('ResponseService', () => {
});

it('should add required headers', () => {
responseService.updateHeaders(mockResponse, 'HIT', 2);
mockResponse = responseService.updateHeaders(mockResponse, 'HIT', 2);

expect(
mockResponse.headers.get(RESPONSE_HEADER_KEYS.LAST_USED_OPTION_INDEX)
Expand All @@ -411,14 +411,14 @@ describe('ResponseService', () => {
});

it('should remove problematic headers', () => {
responseService.updateHeaders(mockResponse, undefined, 0);
mockResponse = responseService.updateHeaders(mockResponse, undefined, 0);

expect(mockResponse.headers.get('content-length')).toBeNull();
expect(mockResponse.headers.get('transfer-encoding')).toBeNull();
});

it('should remove brotli encoding', () => {
responseService.updateHeaders(mockResponse, undefined, 0);
mockResponse = responseService.updateHeaders(mockResponse, undefined, 0);

expect(mockResponse.headers.get('content-encoding')).toBeNull();
});
Expand All @@ -429,7 +429,7 @@ describe('ResponseService', () => {
headers: { 'content-encoding': 'gzip' },
});

responseService.updateHeaders(response, undefined, 0);
mockResponse = responseService.updateHeaders(response, undefined, 0);

expect(response.headers.get('content-encoding')).toBeNull();
});
Expand All @@ -440,13 +440,13 @@ describe('ResponseService', () => {
headers: { 'content-encoding': 'gzip' },
});

responseService.updateHeaders(response, undefined, 0);
mockResponse = responseService.updateHeaders(response, undefined, 0);

expect(response.headers.get('content-encoding')).toBe('gzip');
});

it('should not add cache status header when undefined', () => {
responseService.updateHeaders(mockResponse, undefined, 0);
mockResponse = responseService.updateHeaders(mockResponse, undefined, 0);

expect(
mockResponse.headers.get(RESPONSE_HEADER_KEYS.CACHE_STATUS)
Expand All @@ -466,7 +466,7 @@ describe('ResponseService', () => {
mockLogsService
);

serviceWithPortkey.updateHeaders(mockResponse, 'MISS', 0);
mockResponse = serviceWithPortkey.updateHeaders(mockResponse, 'MISS', 0);

expect(mockResponse.headers.get(HEADER_KEYS.PROVIDER)).toBeNull();
});
Expand All @@ -484,7 +484,11 @@ describe('ResponseService', () => {
mockLogsService
);

serviceWithEmptyProvider.updateHeaders(mockResponse, 'MISS', 0);
mockResponse = serviceWithEmptyProvider.updateHeaders(
mockResponse,
'MISS',
0
);

expect(mockResponse.headers.get(HEADER_KEYS.PROVIDER)).toBeNull();
});
Expand Down