Skip to content

Commit b80a3e6

Browse files
committed
Add NoFraming strategy
1 parent 0ba09c9 commit b80a3e6

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

servant-client/test/Servant/StreamSpec.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import Test.QuickCheck
4141
import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
4242
NetstringFraming, NewlineFraming,
4343
OctetStream, ResultStream (..),
44-
StreamGenerator (..), StreamGet)
44+
StreamGenerator (..), StreamGet,
45+
NoFraming)
4546
import Servant.Client
4647
import Servant.ClientSpec (Person (..))
4748
import qualified Servant.ClientSpec as CS
@@ -55,7 +56,7 @@ spec = describe "Servant.Stream" $ do
5556
type StreamApi f =
5657
"streamGetNewline" :> StreamGet NewlineFraming JSON (f Person)
5758
:<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person)
58-
:<|> "streamALot" :> StreamGet NewlineFraming OctetStream (f BS.ByteString)
59+
:<|> "streamALot" :> StreamGet NoFraming OctetStream (f BS.ByteString)
5960

6061

6162
capi :: Proxy (StreamApi ResultStream)

servant/src/Servant/API.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ import Servant.API.Stream
117117
(BoundaryStrategy (..), BuildFromStream (..),
118118
ByteStringParser (..), FramingRender (..),
119119
FramingUnrender (..), NetstringFraming, NewlineFraming,
120-
ResultStream (..), Stream, StreamGenerator (..), StreamGet,
121-
StreamPost, ToStreamGenerator (..))
120+
NoFraming, ResultStream (..), Stream, StreamGenerator (..),
121+
StreamGet, StreamPost, ToStreamGenerator (..))
122122
import Servant.API.Sub
123123
((:>))
124124
import Servant.API.Vault

servant/src/Servant/API/Stream.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ data ByteStringParser a = ByteStringParser {
8181
class FramingUnrender strategy a where
8282
unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString))
8383

84+
-- | A framing strategy that does not do any framing at all, it just passes the input data
85+
-- This will be used most of the time with binary data, such as files
86+
data NoFraming
87+
88+
instance FramingRender NoFraming a where
89+
header _ _ = empty
90+
boundary _ _ = BoundaryStrategyGeneral id
91+
trailer _ _ = empty
92+
93+
instance FramingUnrender NoFraming a where
94+
unrenderFrames _ _ = ByteStringParser (Just . (go,)) (go,)
95+
where go = ByteStringParser (Just . (, empty) . Right) ((, empty) . Right)
8496

8597
-- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame.
8698
-- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).

0 commit comments

Comments
 (0)