module Network.Wai.Source
(
toEnumerator
, toLBS
) where
import Network.Wai
import qualified Data.ByteString.Lazy as L
import System.IO.Unsafe (unsafeInterleaveIO)
toEnumerator :: Source -> Enumerator
toEnumerator source0 = Enumerator $ helper source0 where
helper source iter a = do
next <- runSource source
case next of
Nothing -> return $ Right a
Just (bs, source') -> do
res <- iter a bs
case res of
Left a' -> return $ Left a'
Right a' -> helper source' iter a'
toLBS :: Source -> IO L.ByteString
toLBS source0 = L.fromChunks `fmap` helper source0 where
helper source = unsafeInterleaveIO $ do
next <- runSource source
case next of
Nothing -> return []
Just (bs, source') -> do
rest <- helper source'
return $ bs : rest