module Network.Wai.Streaming ( Flush(..)
, streamingResponse
, streamingBody
, streamingResponseF
, streamingBodyF
) where
import Streaming
import Network.Wai
import Data.ByteString (ByteString)
import Data.ByteString.Builder (byteString, Builder)
import Network.HTTP.Types.Header
import Network.HTTP.Types.Status
import Streaming.Prelude as S
data Flush a = Chunk !a
| Flush
deriving (Show, Functor)
streamingResponse :: Stream (Of ByteString) IO r
-> Status
-> ResponseHeaders
-> Response
streamingResponse src status headers =
responseStream status headers (streamingBody src)
streamingBody :: Stream (Of ByteString) IO r -> StreamingBody
streamingBody src write flush = S.foldM_ writer flush return src
where
writer _ a = do
write (byteString a)
flush
streamingResponseF :: Stream (Of (Flush Builder)) IO r
-> Status
-> ResponseHeaders
-> Response
streamingResponseF src status headers =
responseStream status headers (streamingBodyF src)
streamingBodyF :: Stream (Of (Flush Builder)) IO r -> StreamingBody
streamingBodyF src write flush = S.foldM_ writer flush return src
where
writer _ (Chunk a) = write a
writer _ Flush = flush