Skip to content

Commit 993d9f6

Browse files
committed
Replace object.fromEntries() with version that works with older JS
1 parent d911002 commit 993d9f6

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
@@ -147,8 +147,7 @@ export const normalizeRemixRequest = (request: RemixRequest): Record<string, any
147147
query: parsedURL.query,
148148
href: parsedURL.href,
149149
method: request.method,
150-
// Remix now uses undici, so use standard way to create headers object
151-
headers: Object.fromEntries(headers),
150+
headers: objectFromHeaders(headers),
152151
insecureHTTPParser: request.insecureHTTPParser,
153152
agent,
154153

@@ -161,3 +160,35 @@ export const normalizeRemixRequest = (request: RemixRequest): Record<string, any
161160

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

0 commit comments

Comments
 (0)