Skip to content

Commit c7b190a

Browse files
Fix Cloudflare Workers usage re:Headers weirdness (#214)
Ensure the headers has a raw() method before calling it
1 parent 81c9650 commit c7b190a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

.changeset/smart-tools-occur.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@apollo/datasource-rest': patch
3+
---
4+
5+
Fix bug in Cloudflare Worker usage where we try to call the `.raw()` method on its response headers object when it doesn't exist.
6+
7+
For some reason, the Cloudflare Worker's global `fetch` `HeadersList` object is passing the instanceof check against `node-fetch`'s `Headers` class, but it doesn't have the `.raw()` method we expect on it. To be sure, we can just make sure it's there before we call it.

cspell-dict.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ direnv
1414
Fakeable
1515
falsey
1616
httpcache
17+
instanceof
1718
isplainobject
1819
keyvaluecache
1920
overridable

src/HTTPCache.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,12 @@ function policyResponseFrom(response: FetcherResponse) {
295295
return {
296296
status: response.status,
297297
headers:
298-
response.headers instanceof NodeFetchHeaders
298+
response.headers instanceof NodeFetchHeaders &&
299+
// https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers/issues/37
300+
// For some reason, Cloudflare Workers' `response.headers` is passing
301+
// the instanceof check here but doesn't have the `raw()` method that
302+
// node-fetch's headers have.
303+
'raw' in response.headers
299304
? nodeFetchHeadersToCachePolicyHeaders(response.headers)
300305
: Object.fromEntries(response.headers),
301306
};

0 commit comments

Comments
 (0)