wai-0.0.1: Web Application Interface.




A collection of utility functions for dealing with Enumerators.



mapE :: (ByteString -> ByteString) -> Enumerator -> EnumeratorSource

Performs a specified conversion on each ByteString output by an enumerator.


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.


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 Enumerator directly.
  • You *must* consume all input. If you do not, then the other thread will be deadlocked.


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.


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.


buffer :: Enumerator -> EnumeratorSource

Buffer chunks until we have a chunk of defaultChunkSize, and then send it.