59 lines
2.2 KiB
Diff
59 lines
2.2 KiB
Diff
|
From b285b94aa4a9f4a03524a3af71865666b4683e6d Mon Sep 17 00:00:00 2001
|
|||
|
From: Zephyr Lykos <git@mochaa.ws>
|
|||
|
Date: Wed, 31 Jan 2024 15:26:16 +0800
|
|||
|
Subject: [PATCH] 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.
|
|||
|
---
|
|||
|
build/wasm.js | 2 ++
|
|||
|
lib/fetch/index.js | 8 ++++++--
|
|||
|
2 files changed, 8 insertions(+), 2 deletions(-)
|
|||
|
|
|||
|
diff --git a/build/wasm.js b/build/wasm.js
|
|||
|
index 1104cfec..cd227256 100644
|
|||
|
--- a/build/wasm.js
|
|||
|
+++ b/build/wasm.js
|
|||
|
@@ -101,6 +101,8 @@ if (EXTERNAL_PATH) {
|
|||
|
writeFileSync(join(ROOT, 'loader.js'), `
|
|||
|
'use strict'
|
|||
|
|
|||
|
+globalThis.__UNDICI_IS_NODE__ = true
|
|||
|
module.exports = require('node:module').createRequire('${EXTERNAL_PATH}/loader.js')('./index-fetch.js')
|
|||
|
+delete globalThis.__UNDICI_IS_NODE__
|
|||
|
`)
|
|||
|
}
|
|||
|
diff --git a/lib/fetch/index.js b/lib/fetch/index.js
|
|||
|
index dea20696..52129884 100644
|
|||
|
--- a/lib/fetch/index.js
|
|||
|
+++ b/lib/fetch/index.js
|
|||
|
@@ -64,6 +64,10 @@ const { webidl } = require('./webidl')
|
|||
|
const { STATUS_CODES } = require('http')
|
|||
|
const GET_OR_HEAD = ['GET', 'HEAD']
|
|||
|
|
|||
|
+const defaultUserAgent = typeof __UNDICI_IS_NODE__ !== 'undefined' || typeof esbuildDetection !== 'undefined'
|
|||
|
+ ? 'node'
|
|||
|
+ : 'undici'
|
|||
|
+
|
|||
|
/** @type {import('buffer').resolveObjectURL} */
|
|||
|
let resolveObjectURL
|
|||
|
let ReadableStream = globalThis.ReadableStream
|
|||
|
@@ -1352,8 +1356,8 @@ async function httpNetworkOrCacheFetch (
|
|||
|
// 14. If httpRequest’s header list does not contain `User-Agent`, then
|
|||
|
// 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', typeof esbuildDetection === 'undefined' ? 'undici' : 'node')
|
|||
|
+ if (!httpRequest.headersList.contains('user-agent', true)) {
|
|||
|
+ httpRequest.headersList.append('user-agent', defaultUserAgent)
|
|||
|
}
|
|||
|
|
|||
|
// 15. If httpRequest’s cache mode is "default" and httpRequest’s header
|
|||
|
--
|
|||
|
2.43.0
|
|||
|
|