@@ -8,7 +8,6 @@ import { AbortError, HttpError, TimeoutError } from "./Errors";
88import { HttpClient , HttpRequest , HttpResponse } from "./HttpClient" ;
99import { ILogger , LogLevel } from "./ILogger" ;
1010import { Platform , getGlobalThis , isArrayBuffer } from "./Utils" ;
11- import { configureAbortController , configureFetch } from "./DynamicImports" ;
1211
1312export class FetchHttpClient extends HttpClient {
1413 private readonly _abortControllerType : { prototype : AbortController , new ( ) : AbortController } ;
@@ -21,19 +20,38 @@ export class FetchHttpClient extends HttpClient {
2120 super ( ) ;
2221 this . _logger = logger ;
2322
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 ) ;
2943 } else {
3044 this . _fetchType = fetch . bind ( getGlobalThis ( ) ) ;
3145 }
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 ;
3250
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 ;
3755 }
3856 }
3957
0 commit comments