wai-eventsource-1.3.0.4/0000755000000000000000000000000012110452471013160 5ustar0000000000000000wai-eventsource-1.3.0.4/wai-eventsource.cabal0000644000000000000000000000215012110452471017262 0ustar0000000000000000Name: wai-eventsource Version: 1.3.0.4 Synopsis: WAI support for server-sent events Description: WAI support for server-sent events License: MIT Author: Chris Smith, Mathias Biilmann Christensen Maintainer: greg@gregweber.info Stability: Experimental Category: Web Build-type: Simple Cabal-version: >=1.8 homepage: http://www.yesodweb.com/book/web-application-interface license-file: LICENSE Library Build-depends: base >= 4.3 && < 5 , bytestring >= 0.9.1.4 , blaze-builder >= 0.3 && < 0.4 , conduit >= 0.5 && < 1.1 , http-types >= 0.7 , wai >= 1.3 && < 1.5 , warp >= 1.3 && < 1.4 , transformers ghc-options: -Wall hs-source-dirs: src exposed-modules: Network.Wai.EventSource Network.Wai.EventSource.EventStream source-repository head type: git location: git://github.com/yesodweb/wai.git wai-eventsource-1.3.0.4/LICENSE0000644000000000000000000000207512110452471014171 0ustar0000000000000000Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. wai-eventsource-1.3.0.4/Setup.lhs0000644000000000000000000000016212110452471014767 0ustar0000000000000000#!/usr/bin/env runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain wai-eventsource-1.3.0.4/src/0000755000000000000000000000000012110452471013747 5ustar0000000000000000wai-eventsource-1.3.0.4/src/Network/0000755000000000000000000000000012110452471015400 5ustar0000000000000000wai-eventsource-1.3.0.4/src/Network/Wai/0000755000000000000000000000000012110452471016120 5ustar0000000000000000wai-eventsource-1.3.0.4/src/Network/Wai/EventSource.hs0000644000000000000000000000432412110452471020721 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-| A WAI adapter to the HTML5 Server-Sent Events API. -} module Network.Wai.EventSource ( ServerEvent(..), eventSourceAppChan, eventSourceAppSource, eventSourceAppIO, sourceToSource ) where import Blaze.ByteString.Builder (Builder) import Control.Concurrent.Chan (Chan, dupChan, readChan) import Control.Monad.IO.Class (liftIO) import Data.Conduit import qualified Data.Conduit.List as CL import Network.HTTP.Types (status200) import Network.Wai (Application, Response(..)) import Network.Wai.EventSource.EventStream -- | Make a new WAI EventSource application reading events from -- the given channel. eventSourceAppChan :: Chan ServerEvent -> Application eventSourceAppChan chan _ = do chan' <- liftIO $ dupChan chan return $ response chanToSource chan' -- | Make a new WAI EventSource application reading events from -- the given source. eventSourceAppSource :: Source (ResourceT IO) ServerEvent -> Application eventSourceAppSource src _ = return $ response sourceToSource src -- | Make a new WAI EventSource application reading events from -- the given IO action. eventSourceAppIO :: IO ServerEvent -> Application eventSourceAppIO act _ = return $ response ioToSource act response :: (a -> Source (ResourceT IO) (Flush Builder)) -> a -> Response response f a = ResponseSource status200 [("Content-Type", "text/event-stream")] $ f a chanToSource :: Chan ServerEvent -> Source (ResourceT IO) (Flush Builder) chanToSource = ioToSource . readChan ioToSource :: IO ServerEvent -> Source (ResourceT IO) (Flush Builder) ioToSource act = loop where loop = do x <- liftIO act case eventToBuilder x of Nothing -> return () Just y -> do yield $ Chunk y yield Flush loop -- | Convert a ServerEvent source into a Builder source of serialized -- events. sourceToSource :: Monad m => Source m ServerEvent -> Source m (Flush Builder) sourceToSource src = src $= CL.concatMap eventToFlushBuilder where eventToFlushBuilder event = case eventToBuilder event of Nothing -> [] Just x -> [Chunk x, Flush] wai-eventsource-1.3.0.4/src/Network/Wai/EventSource/0000755000000000000000000000000012110452471020362 5ustar0000000000000000wai-eventsource-1.3.0.4/src/Network/Wai/EventSource/EventStream.hs0000644000000000000000000000370612110452471023161 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {- code adapted by Mathias Billman originaly from Chris Smith https://github.com/cdsmith/gloss-web -} {-| Internal module, usually you don't need to use it. -} module Network.Wai.EventSource.EventStream ( ServerEvent(..), eventToBuilder ) where import Blaze.ByteString.Builder import Blaze.ByteString.Builder.Char8 import Data.Monoid {-| Type representing a communication over an event stream. This can be an actual event, a comment, a modification to the retry timer, or a special "close" event indicating the server should close the connection. -} data ServerEvent = ServerEvent { eventName :: Maybe Builder, eventId :: Maybe Builder, eventData :: [Builder] } | CommentEvent { eventComment :: Builder } | RetryEvent { eventRetry :: Int } | CloseEvent {-| Newline as a Builder. -} nl :: Builder nl = fromChar '\n' {-| Field names as Builder -} nameField, idField, dataField, retryField, commentField :: Builder nameField = fromString "event:" idField = fromString "id:" dataField = fromString "data:" retryField = fromString "retry:" commentField = fromChar ':' {-| Wraps the text as a labeled field of an event stream. -} field :: Builder -> Builder -> Builder field l b = l `mappend` b `mappend` nl {-| Converts a 'ServerEvent' to its wire representation as specified by the @text/event-stream@ content type. -} eventToBuilder :: ServerEvent -> Maybe Builder eventToBuilder (CommentEvent txt) = Just $ field commentField txt eventToBuilder (RetryEvent n) = Just $ field retryField (fromShow n) eventToBuilder (CloseEvent) = Nothing eventToBuilder (ServerEvent n i d)= Just $ (name n $ evid i $ mconcat (map (field dataField) d)) `mappend` nl where name Nothing = id name (Just n') = mappend (field nameField n') evid Nothing = id evid (Just i') = mappend (field idField i')