Skip to content
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

Fix node detection omfg #2341

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ async function httpNetworkOrCacheFetch (
// user agents should append `User-Agent`/default `User-Agent` value to
// httpRequest’s header list.
if (!httpRequest.headersList.contains('user-agent')) {
httpRequest.headersList.append('user-agent', __filename.endsWith('index.js') ? 'undici' : 'node')
httpRequest.headersList.append('user-agent', typeof esbuildDetection === 'undefined' ? 'undici' : 'node')
}

// 15. If httpRequest’s cache mode is "default" and httpRequest’s header
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"docs"
],
"scripts": {
"build:node": "npx esbuild@0.14.38 index-fetch.js --bundle --platform=node --outfile=undici-fetch.js",
"build:node": "node scripts/esbuild-build.mjs",
"prebuild:wasm": "node build/wasm.js --prebuild",
"build:wasm": "node build/wasm.js --docker",
"lint": "standard | snazzy",
Expand Down Expand Up @@ -109,6 +109,7 @@
"delay": "^5.0.0",
"dns-packet": "^5.4.0",
"docsify-cli": "^4.4.3",
"esbuild": "^0.19.4",
"form-data": "^4.0.0",
"formdata-node": "^4.3.1",
"https-pem": "^3.0.0",
Expand Down
24 changes: 24 additions & 0 deletions scripts/esbuild-build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as esbuild from 'esbuild'
import fs from 'node:fs'

const bundle = {
name: 'bundle',
setup (build) {
build.onLoad({ filter: /lib(\/|\\)fetch(\/|\\)index.js/ }, async (args) => {
const text = await fs.promises.readFile(args.path, 'utf8')

return {
contents: `var esbuildDetection = 1;${text}`,
loader: 'js'
}
})
}
}

await esbuild.build({
entryPoints: ['index-fetch.js'],
bundle: true,
outfile: 'undici-fetch.js',
plugins: [bundle],
platform: 'node'
})
Loading