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