Skip to content

Commit 7bcb80c

Browse files
authored
Fix node detection omfg (#2341)
* fix node detection * remove comment * change name * fix regex for windows paths
1 parent 69ea7b9 commit 7bcb80c

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/fetch/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ async function httpNetworkOrCacheFetch (
13441344
// user agents should append `User-Agent`/default `User-Agent` value to
13451345
// httpRequest’s header list.
13461346
if (!httpRequest.headersList.contains('user-agent')) {
1347-
httpRequest.headersList.append('user-agent', __filename.endsWith('index.js') ? 'undici' : 'node')
1347+
httpRequest.headersList.append('user-agent', typeof esbuildDetection === 'undefined' ? 'undici' : 'node')
13481348
}
13491349

13501350
// 15. If httpRequest’s cache mode is "default" and httpRequest’s header

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"docs"
7171
],
7272
"scripts": {
73-
"build:node": "npx esbuild@0.14.38 index-fetch.js --bundle --platform=node --outfile=undici-fetch.js",
73+
"build:node": "node scripts/esbuild-build.mjs",
7474
"prebuild:wasm": "node build/wasm.js --prebuild",
7575
"build:wasm": "node build/wasm.js --docker",
7676
"lint": "standard | snazzy",
@@ -109,6 +109,7 @@
109109
"delay": "^5.0.0",
110110
"dns-packet": "^5.4.0",
111111
"docsify-cli": "^4.4.3",
112+
"esbuild": "^0.19.4",
112113
"form-data": "^4.0.0",
113114
"formdata-node": "^4.3.1",
114115
"https-pem": "^3.0.0",

scripts/esbuild-build.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as esbuild from 'esbuild'
2+
import fs from 'node:fs'
3+
4+
const bundle = {
5+
name: 'bundle',
6+
setup (build) {
7+
build.onLoad({ filter: /lib(\/|\\)fetch(\/|\\)index.js/ }, async (args) => {
8+
const text = await fs.promises.readFile(args.path, 'utf8')
9+
10+
return {
11+
contents: `var esbuildDetection = 1;${text}`,
12+
loader: 'js'
13+
}
14+
})
15+
}
16+
}
17+
18+
await esbuild.build({
19+
entryPoints: ['index-fetch.js'],
20+
bundle: true,
21+
outfile: 'undici-fetch.js',
22+
plugins: [bundle],
23+
platform: 'node'
24+
})

0 commit comments

Comments
 (0)