@@ -8,7 +8,6 @@ import { AbortError, HttpError, TimeoutError } from "./Errors";
8
8
import { HttpClient , HttpRequest , HttpResponse } from "./HttpClient" ;
9
9
import { ILogger , LogLevel } from "./ILogger" ;
10
10
import { Platform , getGlobalThis , isArrayBuffer } from "./Utils" ;
11
- import { configureAbortController , configureFetch } from "./DynamicImports" ;
12
11
13
12
export class FetchHttpClient extends HttpClient {
14
13
private readonly _abortControllerType : { prototype : AbortController , new ( ) : AbortController } ;
@@ -21,19 +20,38 @@ export class FetchHttpClient extends HttpClient {
21
20
super ( ) ;
22
21
this . _logger = logger ;
23
22
24
- // This is how you do "reference" arguments
25
- const fetchObj = { _fetchType : undefined , _jar : undefined } ;
26
- if ( configureFetch ( fetchObj ) ) {
27
- this . _fetchType = fetchObj . _fetchType ! ;
28
- this . _jar = fetchObj . _jar ;
23
+ // Node added a fetch implementation to the global scope starting in v18.
24
+ // We need to add a cookie jar in node to be able to share cookies with WebSocket
25
+ if ( typeof fetch === "undefined" || Platform . isNode ) {
26
+ // In order to ignore the dynamic require in webpack builds we need to do this magic
27
+ // @ts -ignore: TS doesn't know about these names
28
+ const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
29
+
30
+ // Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
31
+ this . _jar = new ( requireFunc ( "tough-cookie" ) ) . CookieJar ( ) ;
32
+
33
+ if ( typeof fetch === "undefined" ) {
34
+ this . _fetchType = requireFunc ( "node-fetch" ) ;
35
+ } else {
36
+ // Use fetch from Node if available
37
+ this . _fetchType = fetch ;
38
+ }
39
+
40
+ // node-fetch doesn't have a nice API for getting and setting cookies
41
+ // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
42
+ this . _fetchType = requireFunc ( "fetch-cookie" ) ( this . _fetchType , this . _jar ) ;
29
43
} else {
30
44
this . _fetchType = fetch . bind ( getGlobalThis ( ) ) ;
31
45
}
46
+ if ( typeof AbortController === "undefined" ) {
47
+ // In order to ignore the dynamic require in webpack builds we need to do this magic
48
+ // @ts -ignore: TS doesn't know about these names
49
+ const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
32
50
33
- this . _abortControllerType = AbortController ;
34
- const abortObj = { _abortControllerType : this . _abortControllerType } ;
35
- if ( configureAbortController ( abortObj ) ) {
36
- this . _abortControllerType = abortObj . _abortControllerType ;
51
+ // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
52
+ this . _abortControllerType = requireFunc ( "abort-controller" ) ;
53
+ } else {
54
+ this . _abortControllerType = AbortController ;
37
55
}
38
56
}
39
57
0 commit comments