packages feed

pipes-network-ws (empty) → 0.1

raw patch · 6 files changed

+145/−0 lines, 6 filesdep +basedep +bytestringdep +network-simple-wssetup-changed

Dependencies added: base, bytestring, network-simple-ws, pipes

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Renzo Carbonara <renλren.zone>++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Renzo Carbonara nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,10 @@+# pipes-network-ws++Utilities to deal with WebSocket connections using **pipes**.++Check the source or rendered Haddocks for extensive documentation.++This code is licensed under the terms of the so called **3-clause BSD+license**. Read the file named ``LICENSE`` found in this same directory+for details.+
+ Setup.hs view
@@ -0,0 +1,4 @@+#! /usr/bin/env nix-shell+#! nix-shell ./shell.nix -i runghc+import Distribution.Simple+main = defaultMain
+ changelog.md view
@@ -0,0 +1,3 @@+# Version 0.1++* First release.
+ pipes-network-ws.cabal view
@@ -0,0 +1,40 @@+name: pipes-network-ws+version: 0.1+license: BSD3+license-file: LICENSE+copyright: Copyright (c) Renzo Carbonara 2018+author: Renzo Carbonara+maintainer: renλren.zone+stability: Experimental+homepage: https://github.com/k0001/pipes-network-ws+bug-reports: https://github.com/k0001/pipes-network-ws/issues+category: Pipes, Network+build-type: Simple+synopsis: WebSockets support for pipes.+cabal-version: >=1.8+extra-source-files: changelog.md README.md+description:+  Use WebSockets with the @pipes@ ecosystem.+  .+  This package is organized using the following namespaces:+  .+  See the @changelog@ file in the source distribution to learn about any+  important changes between version.++source-repository head+    type: git+    location: https://github.com/k0001/pipes-network-ws++library+    hs-source-dirs: src+    ghc-options: -Wall -O2+    build-depends:+        base (==4.*),+        bytestring,+        network-simple-ws,+        pipes+    exposed-modules:+        Pipes.Network.WS+++
+ src/Pipes/Network/WS.hs view
@@ -0,0 +1,58 @@+{-# LANGUAGE RankNTypes #-}++-- | This module offers tools to stream from and to WebSocket+-- 'W.Connection's using "Pipes".+--+-- See the+-- [network-simple-wss](https://hackage.haskell.org/package/network-simple-wss),+-- [network-simple-ws](https://hackage.haskell.org/package/network-simple-ws) or+-- and [websockets](https://hackage.haskell.org/package/websockets)+-- libraries as well, for lower level support.+module Pipes.Network.WS+ ( fromConnection+ , toConnection+ ) where++import Control.Monad.IO.Class (MonadIO)+import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as BL+import Data.Function (fix)+import qualified Pipes as P++import qualified Network.Simple.WS as WS++--------------------------------------------------------------------------------++-- | Receives bytes from the remote end and sends them downstream.+--+-- The obtailed 'B.ByteString's are never 'B.empty'.+--+-- This producer returns when the conection is closed gracefully.++-- Note: The WebSockets protocol supports the silly idea of sending text, rather+-- than bytes, over the socket. We don't support that. If necessary, users can+-- find support for this in the `websockets` library.+fromConnection+  :: MonadIO m+  => WS.Connection+  -> P.Producer' B.ByteString m ()  -- ^+{-# INLINABLE fromConnection #-}+fromConnection conn =+  fix $ \k -> do+     bs <- WS.recv conn+     case B.null bs of+        False -> P.yield bs >> k+        True -> pure ()++-- | Send bytes from upstream to the remote end.++-- Note: The WebSockets protocol supports the silly idea of sending text, rather+-- than bytes, over the socket. We don't support that. If necessary, users can+-- find support for this in the `websockets` library.+toConnection+  :: MonadIO m+  => WS.Connection+  -> P.Consumer' B.ByteString m ()  -- ^+{-# INLINABLE toConnection #-}+toConnection conn = P.for P.cat (WS.send conn . BL.fromStrict)+