Skip to content

Commit 904bfc2

Browse files
committed
chore: refine esbuild & node detection
When using the loader for external builtins, `esbuildDetection` is undefined. This commit defines `__UNDICI_IS_NODE__` on `globalThis` in the loader and deletes it after loading Undici. `esbuildDetection` has also been extracted as a variable at the top level of the module, to support deleting `__UNDICI_IS_NODE__` on `globalThis` to avoid polluting the global namespace.
1 parent 0808a72 commit 904bfc2

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

build/wasm.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ if (EXTERNAL_PATH) {
101101
writeFileSync(join(ROOT, 'loader.js'), `
102102
'use strict'
103103
104+
globalThis.__UNDICI_IS_NODE__ = true
104105
module.exports = require('node:module').createRequire('${EXTERNAL_PATH}/loader.js')('./index-fetch.js')
106+
delete globalThis.__UNDICI_IS_NODE__
105107
`)
106108
}

lib/fetch/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ const { webidl } = require('./webidl')
6666
const { STATUS_CODES } = require('node:http')
6767
const GET_OR_HEAD = ['GET', 'HEAD']
6868

69+
const defaultUserAgent = typeof __UNDICI_IS_NODE__ !== 'undefined' || typeof esbuildDetection !== 'undefined'
70+
? 'node'
71+
: 'undici'
72+
6973
/** @type {import('buffer').resolveObjectURL} */
7074
let resolveObjectURL
7175

@@ -1473,7 +1477,7 @@ async function httpNetworkOrCacheFetch (
14731477
// user agents should append `User-Agent`/default `User-Agent` value to
14741478
// httpRequest’s header list.
14751479
if (!httpRequest.headersList.contains('user-agent', true)) {
1476-
httpRequest.headersList.append('user-agent', typeof esbuildDetection === 'undefined' ? 'undici' : 'node', true)
1480+
httpRequest.headersList.append('user-agent', defaultUserAgent)
14771481
}
14781482

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

0 commit comments

Comments
 (0)