Skip to content

Commit

Permalink
Fix node detection omfg (nodejs#2341)
Browse files Browse the repository at this point in the history
* fix node detection

* remove comment

* change name

* fix regex for windows paths
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent b230e62 commit 05eef5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
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'
})

0 comments on commit 05eef5d

Please sign in to comment.