@@ -4,12 +4,13 @@ import {
44 ResponseContext ,
55 ZstdCompressorCallback ,
66} from "./http" ;
7- import { fetch as crossFetch } from "cross-fetch" ;
87import pako from "pako" ;
98import bufferFrom from "buffer-from" ;
109import { isBrowser , isNode } from "../util" ;
1110import { logger } from "../../../logger" ;
1211
12+ let cachedCrossFetch : any ;
13+
1314export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1415 public debug = false ;
1516 public zstdCompressorCallback : ZstdCompressorCallback | undefined ;
@@ -61,6 +62,10 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
6162 return this . executeRequest ( request , body , 0 , headers ) ;
6263 }
6364
65+ private async importCrossFetch ( ) : Promise < any > {
66+ return cachedCrossFetch ?? ( cachedCrossFetch = ( await import ( "cross-fetch" ) ) . fetch ) ;
67+ }
68+
6469 private async executeRequest (
6570 request : RequestContext ,
6671 body : any ,
@@ -80,7 +85,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
8085 // `cross-fetch` incorrectly assumes all browsers have XHR available.
8186 // See https://github.com/lquixada/cross-fetch/issues/78
8287 // TODO: Remove once once above issue is resolved.
83- ( ! isNode && typeof fetch === "function" ? fetch : crossFetch ) ;
88+ ( ! isNode && typeof fetch === "function" ? fetch : this . importCrossFetch ( ) ) ;
8489
8590 const resp = await fetchFunction ( request . getUrl ( ) , fetchOptions ) ;
8691 const responseHeaders : { [ name : string ] : string } = { } ;
0 commit comments