@@ -3,18 +3,17 @@ module Unison.Share.SyncV3
33 )
44where
55
6+ import Network.Socket (withSocketsDo )
67import Control.Arrow ((&&&) )
78import Control.Monad.Reader
89import Data.Set qualified as Set
9- import Data.Set.Lens qualified as Lens
10+ import Data.Text.Encoding as Text
1011import GHC.Natural
1112import Ki qualified
1213import Network.WebSockets qualified as WS
1314import U.Codebase.HashTags
1415import U.Codebase.Sqlite.DbId
15- import U.Codebase.Sqlite.Entity qualified as Entity
1616import U.Codebase.Sqlite.Queries qualified as Q
17- import U.Codebase.Sqlite.TempEntity (TempEntity )
1817import U.Codebase.Sqlite.V2.HashHandle (v2HashHandle )
1918import Unison.Cli.Monad
2019import Unison.Cli.Monad qualified as Cli
@@ -27,9 +26,11 @@ import Unison.Server.Orphans ()
2726import Unison.Share.API.Hash qualified as Share
2827import Unison.Share.Codeserver qualified as Codeserver
2928import Unison.Share.Sync.Types qualified as Sync
29+ import Unison.Share.Types
3030import Unison.Sync.Common qualified as Sync
3131import Unison.SyncV3.Types
3232import Unison.SyncV3.Types as SyncV3
33+ import Unison.SyncV3.Utils (tempEntityDependencies )
3334import Unison.Util.Servant.CBOR qualified as CBOR
3435import Unison.Util.Websockets (Queues (.. ), withQueues )
3536import UnliftIO.STM
@@ -58,14 +59,16 @@ syncFromCodeserver ::
5859 Share. HashJWT ->
5960 Cli (Either (Sync. SyncError SyncV3. SyncError ) (CausalHash , CausalHashId ))
6061syncFromCodeserver _shouldValidate codeserver branchRef hashJwt = do
61- Cli. Env {codebase} <- ask
62+ Cli. Env {codebase, tokenProvider } <- ask
6263 let host = Codeserver. codeserverRegName codeserver
6364 let syncV3Path = " /ucm/v3/sync/download"
6465 let rootCausalHash = Share. hashJWTHash hashJwt
6566 -- Enable compression
6667 let connectionOptions = WS. defaultConnectionOptions {WS. connectionCompressionOptions = WS. PermessageDeflateCompression WS. defaultPermessageDeflate}
67- -- TODO: Add authentication headers manually.
68- let headers = []
68+ headers <-
69+ (liftIO (tokenProvider (codeserverIdFromCodeserverURI codeserver))) <&> \ case
70+ Left {} -> []
71+ Right token -> [(" Authorization" , " Bearer " <> Text. encodeUtf8 token)]
6972 let runner = case Codeserver. codeserverScheme codeserver of
7073 Codeserver. Https ->
7174 let tlsPort = 443
@@ -76,7 +79,7 @@ syncFromCodeserver _shouldValidate codeserver branchRef hashJwt = do
7679 port = maybe tlsPort id $ (Codeserver. codeserverPort) codeserver
7780 in WS. runClientWith host port
7881 Debug. debugLogM Debug. Temp " Obtaining Connection"
79- liftIO $ (runner syncV3Path connectionOptions headers) \ conn -> do
82+ liftIO $ withSocketsDo $ (runner syncV3Path connectionOptions headers) \ conn -> do
8083 Debug. debugLogM Debug. Temp " Obtained Connection"
8184 withQueues inputBuffer outputBuffer conn $ \ queues@ Queues {send} -> do
8285 Debug. debugLogM Debug. Temp " Obtained Queues"
@@ -216,16 +219,3 @@ flushTemp codebase rootCausalHash = do
216219 loop
217220 loop
218221 Q. expectCausalHashIdByCausalHash (Sync. hash32ToCausalHash rootCausalHash)
219-
220- tempEntityDependencies :: TempEntity -> Set (EntityKind , Hash32 )
221- tempEntityDependencies entity = do
222- let componentDeps = Lens. setOf Entity. defns_ entity
223- patchDeps = Lens. setOf Entity. patches_ entity
224- branchHashes = Lens. setOf Entity. branchHashes_ entity <> Lens. setOf Entity. branches_ entity
225- causalHashes = Lens. setOf Entity. causalHashes_ entity
226- in Set. unions
227- [ Set. map (DefnComponentEntity ,) componentDeps,
228- Set. map (PatchEntity ,) patchDeps,
229- Set. map (NamespaceEntity ,) branchHashes,
230- Set. map (CausalEntity ,) causalHashes
231- ]
0 commit comments