Repository navigation
feat: Swap node fetch with impit #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import type { RequestInit as ImpitRequestInit } from 'impit'; | ||
| import { DEFAULT_USER_AGENT } from '../store/config.js'; | ||
| import type { FetchLike } from './client.js'; | ||
|
|
||
| type HeadersInit = ConstructorParameters<typeof Headers>[0]; | ||
|
|
||
| export function toHeaders(init: HeadersInit | undefined): Headers { | ||
| const headers = new Headers(); | ||
| if (init) { | ||
| const source = | ||
| init instanceof Headers ? init : new Headers(init as HeadersInit); | ||
| source.forEach((v, k) => headers.set(k, v)); | ||
| } | ||
| return headers; | ||
| } | ||
|
|
||
| function secFetchSite(url: string, headers: Headers): string { | ||
| const source = headers.get('origin') ?? headers.get('referer'); | ||
| if (!source) return 'none'; | ||
| try { | ||
| return new URL(source).origin === new URL(url).origin | ||
| ? 'same-origin' | ||
| : 'cross-site'; | ||
| } catch { | ||
| return 'same-origin'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adjusts only context-dependent headers (`Sec-Fetch-*` and custom | ||
| * `User-Agent` overrides) while leaving static browser headers (`Accept`, | ||
| * `Accept-Encoding`, `Accept-Language`, `Priority`, `Sec-CH-UA*`, `User-Agent`) | ||
| * to impit's browser profile. | ||
| */ | ||
| function adaptHeaders( | ||
| url: string, | ||
| method: string, | ||
| init: HeadersInit | undefined, | ||
| ): Headers { | ||
| const headers = toHeaders(init); | ||
| const ua = headers.get('user-agent'); | ||
| if (ua === DEFAULT_USER_AGENT) { | ||
| headers.delete('user-agent'); | ||
| } else if (ua) { | ||
| for (const h of ['sec-ch-ua', 'sec-ch-ua-mobile', 'sec-ch-ua-platform']) { | ||
| if (!headers.has(h)) headers.set(h, ''); | ||
| } | ||
| } | ||
|
|
||
| if (!headers.has('sec-fetch-site')) { | ||
| headers.set('sec-fetch-site', secFetchSite(url, headers)); | ||
| } | ||
|
|
||
| const accept = headers.get('accept'); | ||
| const isImage = method === 'GET' && !!accept?.startsWith('image/'); | ||
| const isXhr = | ||
| !isImage && | ||
| (method !== 'GET' || | ||
| headers.has('x-requested-with') || | ||
| headers.has('origin') || | ||
| headers.has('content-type') || | ||
| headers.has('authorization') || | ||
| (accept !== null && !accept.includes('text/html'))); | ||
|
|
||
| if (isImage) { | ||
| if (!headers.has('sec-fetch-mode')) | ||
| headers.set('sec-fetch-mode', 'no-cors'); | ||
| if (!headers.has('sec-fetch-dest')) headers.set('sec-fetch-dest', 'image'); | ||
| if (!headers.has('sec-fetch-user')) headers.set('sec-fetch-user', ''); | ||
| if (!headers.has('upgrade-insecure-requests')) | ||
| headers.set('upgrade-insecure-requests', ''); | ||
| } else if (isXhr) { | ||
| if (!headers.has('accept')) headers.set('accept', '*/*'); | ||
| if (!headers.has('sec-fetch-mode')) headers.set('sec-fetch-mode', 'cors'); | ||
| if (!headers.has('sec-fetch-dest')) headers.set('sec-fetch-dest', 'empty'); | ||
| if (!headers.has('sec-fetch-user')) headers.set('sec-fetch-user', ''); | ||
| if (!headers.has('upgrade-insecure-requests')) | ||
| headers.set('upgrade-insecure-requests', ''); | ||
| } | ||
| return headers; | ||
| } | ||
|
|
||
| const FALLBACK_HEADERS: Record<string, string> = { | ||
| Accept: '*/*', | ||
| 'Accept-Language': 'en-US,en;q=0.9', | ||
| 'Sec-Fetch-Mode': 'cors', | ||
| }; | ||
|
|
||
| export function createImpitFetch( | ||
| load: () => Promise<typeof import('impit')> = () => import('impit'), | ||
| ): FetchLike { | ||
| let implPromise: Promise<FetchLike> | undefined; | ||
| const resolveImpl = (): Promise<FetchLike> => | ||
| (implPromise ??= (async (): Promise<FetchLike> => { | ||
| try { | ||
| const { Impit } = await load(); | ||
| const impit = new Impit({ browser: 'chrome151', timeout: null }); | ||
| return (input, init = {}) => { | ||
| const method = (init.method ?? 'GET').toUpperCase(); | ||
| return impit.fetch(input, { | ||
| ...(init as ImpitRequestInit), | ||
| headers: adaptHeaders(input, method, init.headers), | ||
| }) as unknown as Promise<Response>; | ||
| }; | ||
| } catch { | ||
| return (input, init = {}) => { | ||
| const headers = toHeaders(init.headers); | ||
| for (const [k, v] of Object.entries(FALLBACK_HEADERS)) { | ||
| if (!headers.has(k)) headers.set(k, v); | ||
| } | ||
| return globalThis.fetch(input, { ...init, headers }); | ||
| }; | ||
| } | ||
| })()); | ||
|
|
||
| return async (input, init) => (await resolveImpl())(input, init); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.