Skip to content

Commit c9d0c3e

Browse files
kilimanonurtemizkan
authored andcommitted
Replace object.fromEntries() with version that works with older JS
1 parent bfd4926 commit c9d0c3e

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

packages/remix/src/utils/web-fetch.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ export const normalizeRemixRequest = (request: RemixRequest): Record<string, any
150150
query: parsedURL.query,
151151
href: parsedURL.href,
152152
method: request.method,
153-
// Remix now uses undici, so use standard way to create headers object
154-
headers: Object.fromEntries(headers),
153+
headers: objectFromHeaders(headers),
155154
insecureHTTPParser: request.insecureHTTPParser,
156155
agent,
157156

@@ -164,3 +163,35 @@ export const normalizeRemixRequest = (request: RemixRequest): Record<string, any
164163

165164
return requestOptions;
166165
};
166+
167+
// This function is a `polyfill` for Object.fromEntries()
168+
function objectFromHeaders(headers: Headers): Record<string, string> {
169+
const result: Record<string, string> = {};
170+
let iterator: IterableIterator<[string, string]>;
171+
172+
if (hasIterator(headers)) {
173+
iterator = getIterator(headers) as IterableIterator<[string, string]>;
174+
} else {
175+
return {};
176+
}
177+
178+
for (const [key, value] of iterator) {
179+
result[key] = value;
180+
}
181+
return result;
182+
}
183+
184+
type IterableType<T> = {
185+
[Symbol.iterator]: () => Iterator<T>;
186+
};
187+
188+
function hasIterator<T>(obj: T): obj is T & IterableType<unknown> {
189+
return obj !== null && typeof (obj as IterableType<unknown>)[Symbol.iterator] === 'function';
190+
}
191+
192+
function getIterator<T>(obj: T): Iterator<unknown> {
193+
if (hasIterator(obj)) {
194+
return (obj as IterableType<unknown>)[Symbol.iterator]();
195+
}
196+
throw new Error('Object does not have an iterator');
197+
}

0 commit comments

Comments
 (0)