|
2 | 2 |
|
3 | 3 | const { |
4 | 4 | NumberParseInt, |
| 5 | + ObjectDefineProperties, |
5 | 6 | ObjectDefineProperty, |
6 | 7 | ObjectGetOwnPropertyDescriptor, |
7 | 8 | SafeMap, |
@@ -152,18 +153,46 @@ function setupWarningHandler() { |
152 | 153 | // https://fetch.spec.whatwg.org/ |
153 | 154 | function setupFetch() { |
154 | 155 | if (process.config.variables.node_no_browser_globals || |
155 | | - !getOptionValue('--experimental-fetch')) { |
| 156 | + getOptionValue('--no-experimental-fetch')) { |
156 | 157 | return; |
157 | 158 | } |
158 | 159 |
|
159 | | - emitExperimentalWarning('Fetch'); |
| 160 | + let undici; |
| 161 | + function lazyUndici() { |
| 162 | + if (undici) { |
| 163 | + return undici; |
| 164 | + } |
| 165 | + |
| 166 | + emitExperimentalWarning('The Fetch API'); |
| 167 | + undici = require('internal/deps/undici/undici'); |
| 168 | + return undici; |
| 169 | + } |
| 170 | + |
| 171 | + async function fetch(input, init = undefined) { |
| 172 | + return lazyUndici().fetch(input, init); |
| 173 | + } |
| 174 | + |
| 175 | + defineOperation(globalThis, 'fetch', fetch); |
160 | 176 |
|
161 | | - const undici = require('internal/deps/undici/undici'); |
162 | | - defineOperation(globalThis, 'fetch', undici.fetch); |
163 | | - exposeInterface(globalThis, 'FormData', undici.FormData); |
164 | | - exposeInterface(globalThis, 'Headers', undici.Headers); |
165 | | - exposeInterface(globalThis, 'Request', undici.Request); |
166 | | - exposeInterface(globalThis, 'Response', undici.Response); |
| 177 | + function lazyInterface(name) { |
| 178 | + return { |
| 179 | + configurable: true, |
| 180 | + enumerable: false, |
| 181 | + get() { |
| 182 | + return lazyUndici()[name]; |
| 183 | + }, |
| 184 | + set(value) { |
| 185 | + exposeInterface(globalThis, name, value); |
| 186 | + } |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + ObjectDefineProperties(globalThis, { |
| 191 | + FormData: lazyInterface('FormData'), |
| 192 | + Headers: lazyInterface('Headers'), |
| 193 | + Request: lazyInterface('Request'), |
| 194 | + Response: lazyInterface('Response'), |
| 195 | + }); |
167 | 196 | } |
168 | 197 |
|
169 | 198 | // TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is |
|
0 commit comments