packages feed

datastar-hs-brotli (empty) → 1.0.0.0

raw patch · 6 files changed

+237/−0 lines, 6 filesdep +basedep +brotlidep +bytestring

Dependencies added: base, brotli, bytestring, datastar-hs, datastar-hs-brotli, hspec, wai

Files

+ CHANGELOG.md view
@@ -0,0 +1,6 @@+# Revision history for datastar-hs-brotli++## 1.0.0.0 -- 2026-07-04++* First release. `Hypermedia.Datastar.Compression.Brotli`, split out of+  `datastar-hs` 1.0.x so the core package has no system-library dependencies.
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright © Star Federation++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.
+ datastar-hs-brotli.cabal view
@@ -0,0 +1,70 @@+cabal-version:   3.0+name:            datastar-hs-brotli+version:         1.0.0.0+synopsis:        Brotli compressor for datastar-hs+description:+  @br@ (Brotli) @Content-Encoding@ compressor for+  <https://hackage.haskell.org/package/datastar-hs datastar-hs> SSE streams.+  Brotli compresses extremely well on SSE streams that repeatedly patch+  similar HTML fragments.+  .+  Links against the system Brotli C library: @brew install brotli@ on macOS,+  @apt-get install libbrotli-dev pkg-config@ on Debian/Ubuntu.+homepage:        https://github.com/starfederation/datastar-haskell+bug-reports:     https://github.com/starfederation/datastar-haskell/issues+license:         MIT+license-file:    LICENSE+author:          Carlo Hamalainen+maintainer:      carlo@carlo-hamalainen.net+category:        Web, Hypermedia+build-type:      Simple+extra-doc-files: CHANGELOG.md++source-repository head+  type:     git+  location: https://github.com/starfederation/datastar-haskell.git++common warnings+  ghc-options:+    -Wall+    -Wcompat+    -Wunused-packages+    -Wredundant-constraints+    -Wincomplete-uni-patterns+    -Wincomplete-record-updates++library+  import: warnings+  exposed-modules:+    Hypermedia.Datastar.Compression.Brotli+  hs-source-dirs:  src+  default-language: Haskell2010+  default-extensions:+    ImportQualifiedPost+    LambdaCase+    OverloadedStrings+  build-depends:+    , base       >= 4.14  && < 5+    , brotli     >= 0.0.0.3 && < 1+    , bytestring >= 0.10.12 && < 1+    , datastar-hs >= 1.1 && < 1.2++test-suite datastar-hs-brotli-test+  import: warnings+  type:             exitcode-stdio-1.0+  hs-source-dirs:   test+  main-is:          Main.hs+  other-modules:+    Hypermedia.Datastar.Compression.BrotliSpec+  build-depends:+    , base+    , brotli+    , bytestring+    , datastar-hs+    , datastar-hs-brotli+    , hspec+    , wai+  default-language: Haskell2010+  default-extensions:+    ImportQualifiedPost+    OverloadedStrings
+ src/Hypermedia/Datastar/Compression/Brotli.hs view
@@ -0,0 +1,67 @@+module Hypermedia.Datastar.Compression.Brotli+  ( brotli+  , brotliWith+  , defaultBrotliParams+  )+where++import Codec.Compression.Brotli qualified as B+import Control.Concurrent.MVar+import Data.ByteString qualified as BS+import Data.ByteString.Builder qualified as BSB+import Data.ByteString.Lazy qualified as BL++import Hypermedia.Datastar.WAI (Compressor (..))++brotli :: Compressor+brotli = brotliWith defaultBrotliParams++brotliWith :: B.CompressParams -> Compressor+brotliWith params = Compressor "br" wrap+ where+  wrap rawWrite rawFlush = do+    csM <- B.compressIO params >>= newMVar++    let write builder = do+          let bs = BL.toStrict $ BSB.toLazyByteString builder+          if BS.null bs+            then pure ()+            else modifyMVar_ csM $ \case+              B.CompressInputRequired _ supply ->+                supply bs >>= doRawWrites rawWrite+              other -> pure other++        flush = do+          modifyMVar_ csM $ \case+            B.CompressInputRequired doFlush _ ->+              doFlush >>= doRawWrites rawWrite+            other -> pure other+          rawFlush++        finish = do+          modifyMVar_ csM $ \case+            B.CompressInputRequired _ supply ->+              supply BS.empty >>= doRawWrites rawWrite+            other -> pure other+          rawFlush++    return (write, flush, finish)++doRawWrites+  :: (BSB.Builder -> IO ())+  -> B.CompressStream IO+  -> IO (B.CompressStream IO)+doRawWrites rawWrite cs = case cs of+  B.CompressOutputAvailable chunk nextAction -> do+    rawWrite $ BSB.byteString chunk+    cs' <- nextAction+    doRawWrites rawWrite cs'+  _ -> pure cs++defaultBrotliParams :: B.CompressParams+defaultBrotliParams =+  B.defaultCompressParams+    { B.compressMode = B.CompressionModeText+    , B.compressLevel = B.CompressionLevel5+    , B.compressWindowSize = B.CompressionWindowBits24+    }
+ test/Hypermedia/Datastar/Compression/BrotliSpec.hs view
@@ -0,0 +1,79 @@+module Hypermedia.Datastar.Compression.BrotliSpec (spec) where++import Test.Hspec++import Codec.Compression.Brotli qualified as B+import Data.ByteString.Builder qualified as BSB+import Data.ByteString.Lazy qualified as BL+import Data.IORef++import Network.Wai (defaultRequest, requestHeaders)+import Network.Wai.Internal (Response (..))++import Hypermedia.Datastar+import Hypermedia.Datastar.Compression.Brotli (brotli)+import Hypermedia.Datastar.Logger (nullLogger)+import Hypermedia.Datastar.WAI (compressorWrap)++{- | Drive a streaming WAI response to completion, returning its response headers+and the full raw body.+-}+runStream (ResponseStream _status headers body) = do+  ref <- newIORef mempty+  body (\chunk -> modifyIORef' ref (<> chunk)) (pure ())+  bytes <- BSB.toLazyByteString <$> readIORef ref+  pure (headers, bytes)+runStream _ = error "expected a streaming response"++spec :: Spec+spec = describe "Hypermedia.Datastar.Compression.Brotli" $ do+  let sendEvents gen = do+        sendPatchElements gen (patchElements "<div id=\"a\">1</div>")+        sendPatchElements gen (patchElements "<div id=\"b\">2</div>")+        sendPatchSignals gen (patchSignals "{\"count\":42}")+      withAccept enc = defaultRequest{requestHeaders = [("Accept-Encoding", enc)]}++  it "round-trips: the br stream decompresses to the uncompressed stream" $ do+    (_, reference) <- runStream (sseResponse nullLogger sendEvents)+    (headers, compressed) <-+      runStream (sseResponseWith nullLogger [brotli] (withAccept "br") sendEvents)++    headers `shouldSatisfy` elem ("Content-Encoding", "br")+    B.decompress compressed `shouldBe` reference++  it "declines compression when the client does not accept br" $ do+    (_, reference) <- runStream (sseResponse nullLogger sendEvents)+    (headers, body) <-+      runStream (sseResponseWith nullLogger [brotli] (withAccept "gzip") sendEvents)++    filter ((== "Content-Encoding") . fst) headers `shouldBe` []+    body `shouldBe` reference++  it "emits output incrementally on flush, not buffered until finish" $ do+    ref <- newIORef mempty+    let rawWrite c = modifyIORef' ref (<> c)+        sizeSoFar = fromIntegral . BL.length . BSB.toLazyByteString <$> readIORef ref+    (write, flush, finish) <- compressorWrap brotli rawWrite (pure ())++    write (BSB.byteString "event: datastar-patch-elements\ndata: elements <div>1</div>\n\n")+    flush+    afterFirst <- sizeSoFar++    write (BSB.byteString "event: datastar-patch-elements\ndata: elements <div>2</div>\n\n")+    flush+    afterSecond <- sizeSoFar++    finish+    afterFinish <- sizeSoFar++    -- Each flush must push bytes onto the wire; a compressor that buffered+    -- everything until finish would leave afterFirst == afterSecond == 0.+    afterFirst `shouldSatisfy` (> (0 :: Int))+    afterSecond `shouldSatisfy` (> afterFirst)+    afterFinish `shouldSatisfy` (>= afterSecond)++    -- And the accumulated stream is still a valid, complete brotli stream.+    whole <- BSB.toLazyByteString <$> readIORef ref+    B.decompress whole+      `shouldBe` "event: datastar-patch-elements\ndata: elements <div>1</div>\n\n\+                 \event: datastar-patch-elements\ndata: elements <div>2</div>\n\n"
+ test/Main.hs view
@@ -0,0 +1,8 @@+module Main where++import Test.Hspec++import Hypermedia.Datastar.Compression.BrotliSpec qualified++main :: IO ()+main = hspec Hypermedia.Datastar.Compression.BrotliSpec.spec