-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | Bound mirror reads and writes to the selected role credential. Every request disables redirects.
module Ecluse.Core.Registry.Publish.Internal (
    PublishPlan (..),
    PreparedMirrorVersion (..),
    PreparedMirrorUpload (..),
    PublishCodec (..),
    MirrorCompanion (..),
    fetchVersionList,
    relayPublishRequest,
    MirrorTransport (..),
    MirrorPublish (..),
    newMirrorPublish,
) where

import Data.JsonStream.Lexer (Cursor)
import Network.HTTP.Client (Request)

import Ecluse.Core.Credential (ClientCredential)
import Ecluse.Core.Deadline (Deadlines)
import Ecluse.Core.Package (PackageInfo, PackageName)
import Ecluse.Core.Registry (BodyOutcome, FetchFault (FetchBoundExceeded, FetchUrlUnformable), ParseError, PublishError (PublishError), PublishFault (PublishFetch, PublishRejected), PublishRelayResponse, UrlFormationError)
import Ecluse.Core.Registry.Exchange (accumulatingCharge, boundedExchange, boundedRelay, formThen, withSuccessBody)
import Ecluse.Core.Registry.Exchange.Types (ExchangeClient (ecDeadlines))
import Ecluse.Core.Registry.Json.Walk (Step, readJsonWalk)
import Ecluse.Core.Registry.JsonStream (StreamResult (streamValue))
import Ecluse.Core.Registry.Metadata (Manifest (manifestInfo), MetadataError (..), VersionDoc)
import Ecluse.Core.Registry.Metadata.Fetch.Preparation (PrepareRead, prepareManifest)
import Ecluse.Core.Registry.MirrorUpload (ExpectedMirrorFile, MirrorInventory, MirrorUploadData, VerifiedArtifactBytes)
import Ecluse.Core.Registry.Origin (OriginClient (ocChargeFullRead, ocChargeFullReadWorkspace, ocClient, ocLimits), originClient)
import Ecluse.Core.Registry.Request (sealRequest)
import Ecluse.Core.Registry.VersionList (VersionListState, finishVersionList)
import Ecluse.Core.Security (BodyLimit (MetadataBodyLimit), Limits (progressFloor), maxMetadataBytes)
import Ecluse.Core.Security.Egress (RegistryUrl, registryUrlText)
import Ecluse.Core.Server.Admission.Budget (scaleCharge)
import Ecluse.Core.Server.Admission.Meter (newChargeRegion)
import Ecluse.Core.Telemetry.Span (TracingPort)
import Ecluse.Core.Version (Version)

-- | The release tag the target must carry after publication.
newtype PublishPlan = PublishPlan {PublishPlan -> Version
ppLatest :: Version}
    deriving stock (PublishPlan -> PublishPlan -> Bool
(PublishPlan -> PublishPlan -> Bool)
-> (PublishPlan -> PublishPlan -> Bool) -> Eq PublishPlan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PublishPlan -> PublishPlan -> Bool
== :: PublishPlan -> PublishPlan -> Bool
$c/= :: PublishPlan -> PublishPlan -> Bool
/= :: PublishPlan -> PublishPlan -> Bool
Eq, Int -> PublishPlan -> ShowS
[PublishPlan] -> ShowS
PublishPlan -> String
(Int -> PublishPlan -> ShowS)
-> (PublishPlan -> String)
-> ([PublishPlan] -> ShowS)
-> Show PublishPlan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PublishPlan -> ShowS
showsPrec :: Int -> PublishPlan -> ShowS
$cshow :: PublishPlan -> String
show :: PublishPlan -> String
$cshowList :: [PublishPlan] -> ShowS
showList :: [PublishPlan] -> ShowS
Show)

-- | A complete source snapshot with adapter-owned per-file upload preparation.
data PreparedMirrorVersion = PreparedMirrorVersion
    { PreparedMirrorVersion -> MirrorInventory
pmvInventoryValue :: MirrorInventory
    , PreparedMirrorVersion
-> (Int -> IO ())
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> IO (Either PublishFault PreparedMirrorUpload)
pmvPrepareUploadAction :: (Int -> IO ()) -> ExpectedMirrorFile -> VerifiedArtifactBytes -> IO (Either PublishFault PreparedMirrorUpload)
    }

-- | Publication captures checked metadata and verified bytes, so a caller cannot replace the body.
newtype PreparedMirrorUpload = PreparedMirrorUpload
    { PreparedMirrorUpload -> PublishPlan -> IO (Either PublishFault ())
runPreparedUpload :: PublishPlan -> IO (Either PublishFault ())
    }

-- | Native companions retain source metadata under the artifact's existing authority.
data MirrorCompanion = MirrorCompanion
    { MirrorCompanion
-> ExpectedMirrorFile -> Text -> Either PublishFault Text
mcLocation :: ExpectedMirrorFile -> Text -> Either PublishFault Text
    -- ^ Derive a companion location from an authorised artifact location.
    , MirrorCompanion
-> Deadlines
-> Limits
-> (Int -> IO ())
-> MirrorInventory
-> ExpectedMirrorFile
-> ByteString
-> IO (Either PublishFault ByteString)
mcValidate :: Deadlines -> Limits -> (Int -> IO ()) -> MirrorInventory -> ExpectedMirrorFile -> ByteString -> IO (Either PublishFault ByteString)
    -- ^ Validate native facts and return the original bounded bytes.
    }

-- | The codec owns source and upload metadata while the worker owns whole-version orchestration.
data PublishCodec = forall source metadata. PublishCodec
    { ()
pcPrepareSource :: PackageName -> Version -> VersionDoc -> Either PublishFault (MirrorInventory, source)
    , ()
pcPrepareUpload :: Deadlines -> Limits -> (Int -> IO ()) -> MirrorInventory -> source -> ExpectedMirrorFile -> VerifiedArtifactBytes -> IO (Either PublishFault (MirrorUploadData metadata))
    , PublishCodec -> Maybe MirrorCompanion
pcMirrorCompanion :: Maybe MirrorCompanion
    , PublishCodec -> PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete :: PackageName -> Version -> PackageInfo -> Bool
    -- ^ Atomic native inventories may prove completion without retained external source state.
    , PublishCodec -> PrepareRead
pcTargetRead :: PrepareRead
    , PublishCodec -> Int
pcTargetReadPermille :: Int
    -- ^ The adapter's existing full-read heap charge per thousand source bytes.
    , PublishCodec
-> Text
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
pcProbeRequest :: Text -> Maybe ClientCredential -> PackageName -> Either UrlFormationError Request
    , PublishCodec -> Maybe (Limits -> Cursor -> Step VersionListState)
pcVersionListWalk :: Maybe (Limits -> Cursor -> Step VersionListState)
    -- ^ The protocol maintenance caller uses this cheaper inventory where the adapter supplies one.
    , PublishCodec
-> OriginClient
-> RegistryUrl
-> IO (Either MetadataError RegistryUrl)
pcPrepareTarget :: OriginClient -> RegistryUrl -> IO (Either MetadataError RegistryUrl)
    -- ^ Prepare only after the role credential exists. Discovery cannot widen the approved upload root.
    , ()
pcPublishRequest :: Text -> Maybe ClientCredential -> PublishPlan -> MirrorUploadData metadata -> Either PublishFault Request
    , PublishCodec -> Int -> ByteString -> Either PublishFault ()
pcPublishOutcome :: Int -> ByteString -> Either PublishFault ()
    }

-- | Process resources for the target's bounded exchanges.
data MirrorTransport = MirrorTransport
    { MirrorTransport -> ExchangeClient
ptClient :: ExchangeClient
    , MirrorTransport -> IO (Maybe ClientCredential)
ptMintCredential :: IO (Maybe ClientCredential)
    , MirrorTransport -> Limits
ptLimits :: Limits
    , MirrorTransport -> TracingPort
ptTracing :: TracingPort
    }

-- | A target-bound version reader and checked upload preparation capability.
data MirrorPublish = MirrorPublish
    { MirrorPublish -> Maybe MirrorCompanion
mpMirrorCompanion :: Maybe MirrorCompanion
    , MirrorPublish
-> (Int -> IO ())
-> PackageName
-> IO (Either MetadataError PackageInfo)
mpProbeInventory :: (Int -> IO ()) -> PackageName -> IO (Either MetadataError PackageInfo)
    , MirrorPublish -> PackageName -> Version -> PackageInfo -> Bool
mpNativeComplete :: PackageName -> Version -> PackageInfo -> Bool
    , MirrorPublish
-> PackageName
-> Version
-> VersionDoc
-> Either PublishFault PreparedMirrorVersion
mpPrepareVersion :: PackageName -> Version -> VersionDoc -> Either PublishFault PreparedMirrorVersion
    }

-- | The read and upload endpoints are explicit because native upload routes need not share the read root.
newMirrorPublish :: MirrorTransport -> RegistryUrl -> RegistryUrl -> PublishCodec -> MirrorPublish
newMirrorPublish :: MirrorTransport
-> RegistryUrl -> RegistryUrl -> PublishCodec -> MirrorPublish
newMirrorPublish MirrorTransport
transport RegistryUrl
readTarget RegistryUrl
uploadTarget PublishCodec{PackageName
-> Version
-> VersionDoc
-> Either PublishFault (MirrorInventory, source)
pcPrepareSource :: ()
pcPrepareSource :: PackageName
-> Version
-> VersionDoc
-> Either PublishFault (MirrorInventory, source)
pcPrepareSource, Deadlines
-> Limits
-> (Int -> IO ())
-> MirrorInventory
-> source
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> IO (Either PublishFault (MirrorUploadData metadata))
pcPrepareUpload :: ()
pcPrepareUpload :: Deadlines
-> Limits
-> (Int -> IO ())
-> MirrorInventory
-> source
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> IO (Either PublishFault (MirrorUploadData metadata))
pcPrepareUpload, Maybe MirrorCompanion
pcMirrorCompanion :: PublishCodec -> Maybe MirrorCompanion
pcMirrorCompanion :: Maybe MirrorCompanion
pcMirrorCompanion, PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete :: PublishCodec -> PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete :: PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete, PrepareRead
pcTargetRead :: PublishCodec -> PrepareRead
pcTargetRead :: PrepareRead
pcTargetRead, Int
pcTargetReadPermille :: PublishCodec -> Int
pcTargetReadPermille :: Int
pcTargetReadPermille, OriginClient
-> RegistryUrl -> IO (Either MetadataError RegistryUrl)
pcPrepareTarget :: PublishCodec
-> OriginClient
-> RegistryUrl
-> IO (Either MetadataError RegistryUrl)
pcPrepareTarget :: OriginClient
-> RegistryUrl -> IO (Either MetadataError RegistryUrl)
pcPrepareTarget, Text
-> Maybe ClientCredential
-> PublishPlan
-> MirrorUploadData metadata
-> Either PublishFault Request
pcPublishRequest :: ()
pcPublishRequest :: Text
-> Maybe ClientCredential
-> PublishPlan
-> MirrorUploadData metadata
-> Either PublishFault Request
pcPublishRequest, Int -> ByteString -> Either PublishFault ()
pcPublishOutcome :: PublishCodec -> Int -> ByteString -> Either PublishFault ()
pcPublishOutcome :: Int -> ByteString -> Either PublishFault ()
pcPublishOutcome} =
    MirrorPublish
        { mpMirrorCompanion :: Maybe MirrorCompanion
mpMirrorCompanion = Maybe MirrorCompanion
pcMirrorCompanion
        , mpProbeInventory :: (Int -> IO ())
-> PackageName -> IO (Either MetadataError PackageInfo)
mpProbeInventory = MirrorTransport
-> RegistryUrl
-> PrepareRead
-> Int
-> (Int -> IO ())
-> PackageName
-> IO (Either MetadataError PackageInfo)
probeInventory MirrorTransport
transport RegistryUrl
readTarget PrepareRead
pcTargetRead Int
pcTargetReadPermille
        , mpNativeComplete :: PackageName -> Version -> PackageInfo -> Bool
mpNativeComplete = PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete
        , mpPrepareVersion :: PackageName
-> Version
-> VersionDoc
-> Either PublishFault PreparedMirrorVersion
mpPrepareVersion = \PackageName
name Version
version VersionDoc
doc -> do
            (inventory, source) <- PackageName
-> Version
-> VersionDoc
-> Either PublishFault (MirrorInventory, source)
pcPrepareSource PackageName
name Version
version VersionDoc
doc
            pure
                PreparedMirrorVersion
                    { pmvInventoryValue = inventory
                    , pmvPrepareUploadAction = \Int -> IO ()
charge ExpectedMirrorFile
file VerifiedArtifactBytes
bytes -> do
                        peak <- Int -> IO (IORef Int)
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef Int
0
                        let pay Int
amount = IORef Int -> (Int -> Int) -> IO ()
forall (m :: * -> *) a. MonadIO m => IORef a -> (a -> a) -> m ()
modifyIORef' IORef Int
peak (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
amount) IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> IO ()
charge Int
amount
                        prepared <- pcPrepareUpload (ecDeadlines (ptClient transport)) (ptLimits transport) pay inventory source file bytes
                        retained <- readIORef peak
                        pure (PreparedMirrorUpload . publishUpload transport readTarget uploadTarget (charge . (+ retained) . scaleCharge pcTargetReadPermille) pcPrepareTarget pcPublishRequest pcPublishOutcome <$> prepared)
                    }
        }

probeInventory :: MirrorTransport -> RegistryUrl -> PrepareRead -> Int -> (Int -> IO ()) -> PackageName -> IO (Either MetadataError PackageInfo)
probeInventory :: MirrorTransport
-> RegistryUrl
-> PrepareRead
-> Int
-> (Int -> IO ())
-> PackageName
-> IO (Either MetadataError PackageInfo)
probeInventory MirrorTransport
transport RegistryUrl
target PrepareRead
targetRead Int
factor Int -> IO ()
charge PackageName
name = do
    pay <- (Int -> IO ()) -> IO (Int -> IO ())
accumulatingCharge Int -> IO ()
charge
    workspace <- newChargeRegion pay
    credential <- ptMintCredential transport
    -- The initial and final target snapshots can overlap during completion.
    let origin = (Limits
-> ExchangeClient
-> RegistryUrl
-> Maybe ClientCredential
-> OriginClient
originClient (MirrorTransport -> Limits
ptLimits MirrorTransport
transport) (MirrorTransport -> ExchangeClient
ptClient MirrorTransport
transport) RegistryUrl
target Maybe ClientCredential
credential){ocChargeFullRead = pay . scaleCharge (2 * factor), ocChargeFullReadWorkspace = workspace}
    fmap manifestInfo <$> prepareManifest targetRead (ptTracing transport) origin name

-- | Preserve the native publication response under the origin's metadata and progress limits.
relayPublishRequest :: OriginClient -> Either UrlFormationError Request -> IO (Either FetchFault PublishRelayResponse)
relayPublishRequest :: OriginClient
-> Either UrlFormationError Request
-> IO (Either FetchFault PublishRelayResponse)
relayPublishRequest OriginClient
origin =
    (UrlFormationError -> FetchFault)
-> (Request -> IO (Either FetchFault PublishRelayResponse))
-> Either UrlFormationError Request
-> IO (Either FetchFault PublishRelayResponse)
forall fault a.
(UrlFormationError -> fault)
-> (Request -> IO (Either fault a))
-> Either UrlFormationError Request
-> IO (Either fault a)
formThen
        UrlFormationError -> FetchFault
FetchUrlUnformable
        (ExchangeClient
-> ProgressFloor
-> BodyLimit
-> Request
-> IO (Either FetchFault PublishRelayResponse)
boundedRelay (OriginClient -> ExchangeClient
ocClient OriginClient
origin) (Limits -> ProgressFloor
progressFloor (OriginClient -> Limits
ocLimits OriginClient
origin)) (Int -> BodyLimit
MetadataBodyLimit (Limits -> Int
maxMetadataBytes (OriginClient -> Limits
ocLimits OriginClient
origin))))

-- | Read bounded version identifiers for the protocol maintenance caller.
fetchVersionList :: ExchangeClient -> Limits -> (Cursor -> Step VersionListState) -> Request -> IO (Either FetchFault (BodyOutcome (Either ParseError [Version])))
fetchVersionList :: ExchangeClient
-> Limits
-> (Cursor -> Step VersionListState)
-> Request
-> IO
     (Either FetchFault (BodyOutcome (Either ParseError [Version])))
fetchVersionList ExchangeClient
client Limits
limits Cursor -> Step VersionListState
walk Request
request =
    (BodyOutcome (StreamResult VersionListState)
 -> BodyOutcome (Either ParseError [Version]))
-> Either FetchFault (BodyOutcome (StreamResult VersionListState))
-> Either FetchFault (BodyOutcome (Either ParseError [Version]))
forall a b. (a -> b) -> Either FetchFault a -> Either FetchFault b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((StreamResult VersionListState -> Either ParseError [Version])
-> BodyOutcome (StreamResult VersionListState)
-> BodyOutcome (Either ParseError [Version])
forall a b. (a -> b) -> BodyOutcome a -> BodyOutcome b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StreamResult VersionListState -> Either ParseError [Version]
versions) (Either FetchFault (BodyOutcome (StreamResult VersionListState))
 -> Either FetchFault (BodyOutcome (Either ParseError [Version])))
-> IO
     (Either FetchFault (BodyOutcome (StreamResult VersionListState)))
-> IO
     (Either FetchFault (BodyOutcome (Either ParseError [Version])))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExchangeClient
-> ProgressFloor
-> (IO ByteString
    -> IO (Either LimitError (StreamResult VersionListState)))
-> Request
-> IO
     (Either FetchFault (BodyOutcome (StreamResult VersionListState)))
forall a.
ExchangeClient
-> ProgressFloor
-> (IO ByteString -> IO (Either LimitError a))
-> Request
-> IO (Either FetchFault (BodyOutcome a))
withSuccessBody ExchangeClient
client (Limits -> ProgressFloor
progressFloor Limits
limits) (BodyLimit
-> (Cursor -> Step VersionListState)
-> IO ByteString
-> IO (Either LimitError (StreamResult VersionListState))
forall (m :: * -> *) s.
Monad m =>
BodyLimit
-> (Cursor -> Step s)
-> m ByteString
-> m (Either LimitError (StreamResult s))
readJsonWalk (Int -> BodyLimit
MetadataBodyLimit (Limits -> Int
maxMetadataBytes Limits
limits)) Cursor -> Step VersionListState
walk) Request
request
  where
    versions :: StreamResult VersionListState -> Either ParseError [Version]
versions StreamResult VersionListState
streamed = StreamResult VersionListState -> Either ParseError VersionListState
forall a. StreamResult a -> Either ParseError a
streamValue StreamResult VersionListState
streamed Either ParseError VersionListState
-> (VersionListState -> Either ParseError [Version])
-> Either ParseError [Version]
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= VersionListState -> Either ParseError [Version]
finishVersionList

publishUpload :: MirrorTransport -> RegistryUrl -> RegistryUrl -> (Int -> IO ()) -> (OriginClient -> RegistryUrl -> IO (Either MetadataError RegistryUrl)) -> (Text -> Maybe ClientCredential -> PublishPlan -> MirrorUploadData metadata -> Either PublishFault Request) -> (Int -> ByteString -> Either PublishFault ()) -> MirrorUploadData metadata -> PublishPlan -> IO (Either PublishFault ())
publishUpload :: forall metadata.
MirrorTransport
-> RegistryUrl
-> RegistryUrl
-> (Int -> IO ())
-> (OriginClient
    -> RegistryUrl -> IO (Either MetadataError RegistryUrl))
-> (Text
    -> Maybe ClientCredential
    -> PublishPlan
    -> MirrorUploadData metadata
    -> Either PublishFault Request)
-> (Int -> ByteString -> Either PublishFault ())
-> MirrorUploadData metadata
-> PublishPlan
-> IO (Either PublishFault ())
publishUpload MirrorTransport
transport RegistryUrl
readTarget RegistryUrl
target Int -> IO ()
charge OriginClient
-> RegistryUrl -> IO (Either MetadataError RegistryUrl)
prepare Text
-> Maybe ClientCredential
-> PublishPlan
-> MirrorUploadData metadata
-> Either PublishFault Request
request Int -> ByteString -> Either PublishFault ()
outcome MirrorUploadData metadata
upload PublishPlan
plan = do
    credential <- MirrorTransport -> IO (Maybe ClientCredential)
ptMintCredential MirrorTransport
transport
    pay <- accumulatingCharge charge
    let origin = (Limits
-> ExchangeClient
-> RegistryUrl
-> Maybe ClientCredential
-> OriginClient
originClient (MirrorTransport -> Limits
ptLimits MirrorTransport
transport) (MirrorTransport -> ExchangeClient
ptClient MirrorTransport
transport) RegistryUrl
readTarget Maybe ClientCredential
credential){ocChargeFullRead = pay}
    prepared <- prepare origin target
    case prepared of
        Left MetadataError
fault -> Either PublishFault () -> IO (Either PublishFault ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublishFault -> Either PublishFault ()
forall a b. a -> Either a b
Left (MetadataError -> PublishFault
publishTargetFault MetadataError
fault))
        Right RegistryUrl
approved ->
            (PublishFault -> IO (Either PublishFault ()))
-> (Request -> IO (Either PublishFault ()))
-> Either PublishFault Request
-> IO (Either PublishFault ())
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
                (Either PublishFault () -> IO (Either PublishFault ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either PublishFault () -> IO (Either PublishFault ()))
-> (PublishFault -> Either PublishFault ())
-> PublishFault
-> IO (Either PublishFault ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublishFault -> Either PublishFault ()
forall a b. a -> Either a b
Left)
                (MirrorTransport
-> (Int -> ByteString -> Either PublishFault ())
-> Request
-> IO (Either PublishFault ())
writeUpload MirrorTransport
transport Int -> ByteString -> Either PublishFault ()
outcome (Request -> IO (Either PublishFault ()))
-> (Request -> Request) -> Request -> IO (Either PublishFault ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> Request
sealRequest)
                (Text
-> Maybe ClientCredential
-> PublishPlan
-> MirrorUploadData metadata
-> Either PublishFault Request
request (RegistryUrl -> Text
registryUrlText RegistryUrl
approved) Maybe ClientCredential
credential PublishPlan
plan MirrorUploadData metadata
upload)

publishTargetFault :: MetadataError -> PublishFault
publishTargetFault :: MetadataError -> PublishFault
publishTargetFault = \case
    MetadataFetch FetchFault
fault -> FetchFault -> PublishFault
PublishFetch FetchFault
fault
    MetadataBoundExceeded LimitError
fault -> FetchFault -> PublishFault
PublishFetch (LimitError -> FetchFault
FetchBoundExceeded LimitError
fault)
    MetadataAuthorisationFailure Int
status -> Int -> PublishFault
rejected Int
status
    MetadataHttpFailure Int
status Maybe RetryAfter
_ -> Int -> PublishFault
rejected Int
status
    MetadataError
MetadataAbsent -> Int -> PublishFault
rejected Int
404
    MetadataError
MetadataUndecodable -> PublishError -> PublishFault
PublishRejected (Text -> PublishError
PublishError Text
"the target discovery is malformed or names an unapproved API root")
    MetadataNameMismatch Text
_ -> PublishError -> PublishFault
PublishRejected (Text -> PublishError
PublishError Text
"the target discovery has an inconsistent identity")
  where
    rejected :: Int -> PublishFault
    rejected :: Int -> PublishFault
rejected Int
status = PublishError -> PublishFault
PublishRejected (Text -> PublishError
PublishError (Text
"target discovery failed with HTTP status " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
status))

writeUpload :: MirrorTransport -> (Int -> ByteString -> Either PublishFault ()) -> Request -> IO (Either PublishFault ())
writeUpload :: MirrorTransport
-> (Int -> ByteString -> Either PublishFault ())
-> Request
-> IO (Either PublishFault ())
writeUpload MirrorTransport
transport Int -> ByteString -> Either PublishFault ()
outcome Request
request =
    (Int -> Int -> ByteString -> (Int, ByteString))
-> ExchangeClient
-> ProgressFloor
-> BodyLimit
-> Request
-> IO (Either FetchFault (Int, ByteString))
forall a.
(Int -> Int -> ByteString -> a)
-> ExchangeClient
-> ProgressFloor
-> BodyLimit
-> Request
-> IO (Either FetchFault a)
boundedExchange (\Int
status Int
_ ByteString
body -> (Int
status, ByteString
body)) (MirrorTransport -> ExchangeClient
ptClient MirrorTransport
transport) (Limits -> ProgressFloor
progressFloor (MirrorTransport -> Limits
ptLimits MirrorTransport
transport)) (Int -> BodyLimit
MetadataBodyLimit (Limits -> Int
maxMetadataBytes (MirrorTransport -> Limits
ptLimits MirrorTransport
transport))) Request
request
        IO (Either FetchFault (Int, ByteString))
-> (Either FetchFault (Int, ByteString) -> Either PublishFault ())
-> IO (Either PublishFault ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
            Left FetchFault
fault -> PublishFault -> Either PublishFault ()
forall a b. a -> Either a b
Left (FetchFault -> PublishFault
PublishFetch FetchFault
fault)
            Right (Int
status, ByteString
body) -> Int -> ByteString -> Either PublishFault ()
outcome Int
status ByteString
body