Skip to content

Commit

Permalink
Switch to unframed entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Feb 4, 2025
1 parent 55fd99b commit b87bbbb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions unison-cli/src/Unison/Share/SyncV2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,25 @@ type SyncAPI = ("ucm" Servant.:> "v2" Servant.:> "sync" Servant.:> SyncV2.API)
syncAPI :: Proxy SyncAPI
syncAPI = Proxy @SyncAPI

downloadEntitiesStreamClientM :: SyncV2.DownloadEntitiesRequest -> Servant.ClientM (Servant.SourceT IO SyncV2.DownloadEntitiesChunk)
downloadEntitiesStreamClientM :: SyncV2.DownloadEntitiesRequest -> Servant.ClientM (Servant.SourceT IO (CBORBytes SyncV2.DownloadEntitiesChunk))
SyncV2.Routes
{ downloadEntitiesStream = downloadEntitiesStreamClientM
} = Servant.client syncAPI

-- | Helper for running clientM that returns a stream of entities.
-- You MUST consume the stream within the callback, it will be closed when the callback returns.
withConduit :: forall r. Servant.ClientEnv -> (Stream () SyncV2.DownloadEntitiesChunk -> StreamM r) -> Servant.ClientM (Servant.SourceIO SyncV2.DownloadEntitiesChunk) -> StreamM r
withConduit :: forall r. Servant.ClientEnv -> (Stream () (SyncV2.DownloadEntitiesChunk) -> StreamM r) -> Servant.ClientM (Servant.SourceIO (CBORBytes SyncV2.DownloadEntitiesChunk)) -> StreamM r
withConduit clientEnv callback clientM = do
ExceptT $ withRunInIO \runInIO -> do
Servant.withClientM clientM clientEnv $ \case
Left err -> pure . Left . TransportError $ (handleClientError clientEnv err)
Right sourceT -> do
conduit <- liftIO $ Servant.fromSourceIO sourceT
(runInIO . runExceptT $ callback conduit)
(runInIO . runExceptT $ callback (conduit C..| unpackCBORBytesStream))

unpackCBORBytesStream :: Stream (CBORBytes SyncV2.DownloadEntitiesChunk) SyncV2.DownloadEntitiesChunk
unpackCBORBytesStream =
C.map (BL.toStrict . coerce @_ @BL.ByteString) C..| decodeUnframedEntities

handleClientError :: Servant.ClientEnv -> Servant.ClientError -> CodeserverTransportError
handleClientError clientEnv err =
Expand Down
2 changes: 1 addition & 1 deletion unison-share-api/src/Unison/SyncV2/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type API = NamedRoutes Routes
type DownloadEntitiesStream =
-- | The causal hash the client needs. The server should provide it and all of its dependencies
ReqBody '[CBOR, JSON] DownloadEntitiesRequest
:> StreamPost NetstringFraming CBOR (SourceIO DownloadEntitiesChunk)
:> StreamPost NoFraming CBOR (SourceIO (CBORBytes DownloadEntitiesChunk))

data Routes mode = Routes
{ downloadEntitiesStream :: mode :- "entities" :> "download" :> DownloadEntitiesStream
Expand Down

0 comments on commit b87bbbb

Please sign in to comment.