module Network.Wai.Source
(
toEnumerator
, toLBS
, fromLBS
) 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
fromLBS :: L.ByteString -> Source
fromLBS =
go . L.toChunks
where
go [] = Source $ return Nothing
go (x:xs) = Source $ return $ Just (x, go xs)