Network.Wai.Enumerator
Description
A collection of utility functions for dealing with Enumerators.
- mapE :: (ByteString -> ByteString) -> Enumerator -> Enumerator
- toLBS :: Enumerator -> IO ByteString
- fromLBS :: ByteString -> Enumerator
- fromLBS' :: IO ByteString -> Enumerator
- toSource :: Enumerator -> IO Source
- fromHandle :: Handle -> Enumerator
- fromFile :: FilePath -> Enumerator
- fromEitherFile :: Either FilePath Enumerator -> Enumerator
Utilities
mapE :: (ByteString -> ByteString) -> Enumerator -> EnumeratorSource
Performs a specified conversion on each ByteString output by an
enumerator.
Conversions
Lazy byte strings
toLBS :: Enumerator -> IO ByteStringSource
This uses unsafeInterleaveIO to lazily read from an enumerator. All
normal lazy I/O warnings apply. In addition, since it is based on
toSource, please observe all precautions for that function.
fromLBS :: ByteString -> EnumeratorSource
This function safely converts a lazy bytestring into an enumerator.
fromLBS' :: IO ByteString -> EnumeratorSource
Same as fromLBS, but the lazy bytestring is in the IO monad. This allows
you to lazily read a file into memory, perform some mapping on the data and
convert it into an enumerator.
Source
toSource :: Enumerator -> IO SourceSource
This function uses another thread to convert an Enumerator to a
Source. In essence, this allows you to write code which "pulls" instead
of code which is pushed to. While this can be a useful technique, some
caveats apply:
- It will be more resource heavy than using the
Enumeratordirectly. - You *must* consume all input. If you do not, then the other thread will be deadlocked.
Handle
fromHandle :: Handle -> EnumeratorSource
Read a chunk of data from the given Handle at a time. We use
defaultChunkSize from the bytestring package to determine the largest
chunk to take.
FilePath
fromFile :: FilePath -> EnumeratorSource
A little wrapper around fromHandle which first opens a file for reading.
fromEitherFile :: Either FilePath Enumerator -> EnumeratorSource
Since the response body is defined as an Either FilePath Enumerator,
this function simply reduces the whole operator to an enumerator. This can
be convenient for server implementations not optimizing file sending.