wai-0.2.1: Web Application Interface.

Network.Wai.Enumerator

Contents

Description

A collection of utility functions for dealing with Enumerators.

Synopsis

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 Enumerator directly.
  • 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.

fromResponseBody :: ResponseBody -> EnumeratorSource

Since the response body is defined as a ResponseBody, this function simply reduces the whole value to an enumerator. This can be convenient for server implementations not optimizing file sending.