import { map , share } from 'rxjs/operators' ; import { inflateResponse } from '.' ; import { fromStreamingXhr } from './from_streaming_xhr' ; import { split } from './split' ; import { appendQueryParam } from '../../common' ; export interface FetchStreamingParams { url : string ; headers ? : Record < string , string > ; method ? : 'GET' | 'POST' ; body ? : string ; signal ? : AbortSignal ; getIsCompressionDisabled ? : ( ) => boolean ; } /**
 * Sends an AJAX request to the server, and processes the result as a
 * streaming HTTP/1 response. Streams data as text through observable.
 */ export function fetchStreaming ( { url , headers = { } , method = 'POST' , body = '' , signal , getIsCompressionDisabled = ( ) => false , } ) { const xhr = new window . XMLHttpRequest ( ) ; const isCompressionDisabled = getIsCompressionDisabled ( ) ; if ( ! isCompressionDisabled ) { url = appendQueryParam ( url , 'compress' , 'true' ) ; } xhr . open ( method , url ) ; xhr . withCredentials = true ; Object . entries ( headers ) . forEach ( ( [ k , v ] ) => xhr . setRequestHeader ( k , v ) ) ; const stream = fromStreamingXhr ( xhr , signal ) ; xhr . send ( body ) ; const stream$ = stream . pipe ( split ( '\n' ) , map ( ( msg ) => { return isCompressionDisabled ? msg : inflateResponse ( msg ) ; } ) , share ( ) ) ; return { xhr , stream : stream$ , } ; }