-
Notifications
You must be signed in to change notification settings - Fork 28.5k
misc: remove vendored node-fetch usages #75741
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
Changes from all commits
dabbab2
ff55812
6fa0960
3afacf7
477b8b1
076aec7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,24 @@ | ||
// @ts-ignore | ||
import fetch from 'next/dist/compiled/node-fetch' | ||
import { getProxyAgent } from './get-proxy-agent' | ||
import fs from 'node:fs' | ||
import { retry } from './retry' | ||
import { fetchResource } from './fetch-resource' | ||
|
||
/** | ||
* Fetch the url and return a buffer with the font file. | ||
* Fetches a font file and returns its contents as a Buffer. | ||
* If NEXT_FONT_GOOGLE_MOCKED_RESPONSES is set, we handle mock data logic. | ||
*/ | ||
export async function fetchFontFile(url: string, isDev: boolean) { | ||
// Check if we're using mocked data | ||
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) { | ||
// If it's an absolute path, read the file from the filesystem | ||
if (url.startsWith('/')) { | ||
return require('fs').readFileSync(url) | ||
return fs.readFileSync(url) | ||
} | ||
// Otherwise just return a unique buffer | ||
return Buffer.from(url) | ||
} | ||
|
||
return await retry(async () => { | ||
const controller = new AbortController() | ||
const timeoutId = setTimeout(() => controller.abort(), 3000) | ||
const arrayBuffer = await fetch(url, { | ||
agent: getProxyAgent(), | ||
// Add a timeout in dev | ||
signal: isDev ? controller.signal : undefined, | ||
}) | ||
.then((r: any) => r.arrayBuffer()) | ||
.finally(() => { | ||
clearTimeout(timeoutId) | ||
}) | ||
return Buffer.from(arrayBuffer) | ||
return fetchResource( | ||
url, | ||
isDev, | ||
`Failed to fetch font file from \`${url}\`.` | ||
) | ||
}, 3) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import http from 'node:http' | ||
import https from 'node:https' | ||
import { getProxyAgent } from './get-proxy-agent' | ||
|
||
/** | ||
* Makes a simple GET request and returns the entire response as a Buffer. | ||
* - Throws if the response status is not 200. | ||
* - Applies a 3000 ms timeout when `isDev` is `true`. | ||
*/ | ||
export function fetchResource( | ||
url: string, | ||
isDev: boolean, | ||
errorMessage?: string | ||
): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
const { protocol } = new URL(url) | ||
const client = protocol === 'https:' ? https : http | ||
const timeout = isDev ? 3000 : undefined | ||
|
||
const req = client.request( | ||
url, | ||
{ | ||
agent: getProxyAgent(), | ||
headers: { | ||
// The file format is based off of the user agent, make sure woff2 files are fetched | ||
'User-Agent': | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ' + | ||
'AppleWebKit/537.36 (KHTML, like Gecko) ' + | ||
'Chrome/104.0.0.0 Safari/537.36', | ||
}, | ||
}, | ||
(res) => { | ||
if (res.statusCode !== 200) { | ||
reject( | ||
new Error( | ||
errorMessage || | ||
`Request failed: ${url} (status: ${res.statusCode})` | ||
) | ||
) | ||
return | ||
} | ||
const chunks: Buffer[] = [] | ||
res.on('data', (chunk) => chunks.push(Buffer.from(chunk))) | ||
res.on('end', () => resolve(Buffer.concat(chunks))) | ||
} | ||
) | ||
|
||
if (timeout) { | ||
req.setTimeout(timeout, () => { | ||
req.destroy(new Error(`Request timed out after ${timeout}ms`)) | ||
}) | ||
} | ||
|
||
req.on('error', (err) => reject(err)) | ||
req.end() | ||
}) | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ const externals = { | |
'terser-webpack-plugin': | ||
'next/dist/build/webpack/plugins/terser-webpack-plugin/src', | ||
|
||
punycode: 'punycode/', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how come this is the only one with a trailing slash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trailing slash enforces the use of external punycode library, instead of the node-embedded one |
||
// TODO: Add @swc/helpers to externals once @vercel/ncc switch to swc-loader | ||
} | ||
// eslint-disable-next-line camelcase | ||
|
Uh oh!
There was an error while loading. Please reload this page.