From 05eef5d442de5997c153e37549c602f1b0c53912 Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 11 Oct 2023 14:24:46 -0400 Subject: [PATCH] Fix node detection omfg (#2341) * fix node detection * remove comment * change name * fix regex for windows paths --- lib/fetch/index.js | 2 +- package.json | 3 ++- scripts/esbuild-build.mjs | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 scripts/esbuild-build.mjs diff --git a/lib/fetch/index.js b/lib/fetch/index.js index 8c98a646a0e..c89c9b7ffcb 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -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 diff --git a/package.json b/package.json index a780bc10376..3436db5fe86 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/scripts/esbuild-build.mjs b/scripts/esbuild-build.mjs new file mode 100644 index 00000000000..ca5886c1a2b --- /dev/null +++ b/scripts/esbuild-build.mjs @@ -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' +})