import * as fs from "fs" ; import { Readable } from "readable-stream" ; import { isNode } from "browser-or-node" ; import { getStream } from "./get-stream" ; import { messageError , panic } from "../../index" ; const isURL = require ( "is-url" ) ; const fetch = require ( "isomorphic-fetch" ) ; interface HttpHeaders { [ key ] : string ; } function parseHeaders ( httpHeaders ? ) { if ( ! Array . isArray ( httpHeaders ) ) { return { } ; } return httpHeaders . reduce ( function ( result , httpHeader ) { if ( httpHeader !== undefined && httpHeader . length > 0 ) { const split = httpHeader . indexOf ( ":" ) ; if ( split < 0 ) { return panic ( `Could not parse HTTP header "${ httpHeader }".` ) ; } const key = httpHeader . slice ( 0 , split ) . trim ( ) ; const value = httpHeader . slice ( split + 1 ) . trim ( ) ; result [ key ] = value ; } return result ; } , { } as HttpHeaders ) ; } export async function readableFromFileOrURL ( fileOrURL , httpHeaders ? ) { try { if ( isNode && fileOrURL === "-" ) { return process . stdin as unknown as Readable ; } else if ( isURL ( fileOrURL ) ) { const response = await fetch ( fileOrURL , { headers : parseHeaders ( httpHeaders ) } ) ; return response . body ; } else if ( isNode && fs . existsSync ( fileOrURL ) ) { return fs . createReadStream ( fileOrURL , "utf8" ) as unknown as Readable ; } } catch ( e ) { const message = typeof e . message === "string" ? e . message : "Unknown error" ; return messageError ( "MiscReadError" , { fileOrURL , message } ) ; } return messageError ( "DriverInputFileDoesNotExist" , { filename : fileOrURL } ) ; } export async function readFromFileOrURL ( fileOrURL , httpHeaders ? ) { const readable = await readableFromFileOrURL ( fileOrURL , httpHeaders ) ; try { return await getStream ( readable ) ; } catch ( e ) { const message = typeof e . message === "string" ? e . message : "Unknown error" ; return messageError ( "MiscReadError" , { fileOrURL , message } ) ; } }