import * as fs from "fs" ; import { URL } from "url" ; const node_fetch = require ( "node-fetch" ) ; declare const global ; class AbortSignal { aborted = false ; } class AbortController { signal = new AbortSignal ( ) ; abort ( ) { this . signal . aborted = true ; } } if ( global . fetch === undefined ) { global . fetch = fetchWithFileSupport ; global . Response = node_fetch . Response ; global . Headers = node_fetch . Headers ; global . Request = node_fetch . Request ; global . AbortController = AbortController ; global . AbortSignal = AbortSignal ; } export type FetchFunction = typeof fetch ; see function fetchWithFileSupport ( input , init ? ) { const url = typeof input === "object" ? input . url : input ; const parentUrl = `file://${ process . cwd ( ) }/` ; const actualUrl = new URL ( url , parentUrl ) ; if ( actualUrl . protocol === "file:" ) { return new Promise < Response > ( ( resolve , reject ) => { fs . readFile ( actualUrl , ( error , buffer ) => { if ( error ) { reject ( new Error ( `failed to read file ${ actualUrl }: ${ error }` ) ) ; } const response = { headers : new Headers ( ) , ok : true , status : 200 , statusText : "OK" , type : "basic" , url : actualUrl . toString ( ) , size : buffer . byteLength , timeout : 0 , body : null as any , bodyUsed : false , buffer ( ) { return Promise . resolve ( buffer ) ; } , arrayBuffer ( ) { return Promise . resolve ( buffer . buffer as ArrayBuffer ) ; } , json ( ) { return Promise . resolve ( JSON . parse ( buffer . toString ( "utf-8" ) ) ) ; } , text ( ) { return Promise . resolve ( buffer . toString ( "utf-8" ) ) ; } , clone ( ) { return Object . assign ( { } , this ) ; } } ; resolve ( ( response as any ) as Response ) ; } ) ; } ) ; } else { return node_fetch ( url , init ) ; } }