module Ecluse.Core.Registry.RubyGems.Mirror (
rubyMirrorCompanion,
rubyGemsMirrorDocuments,
rubyGemsStateFormat,
mirrorRow,
) where
import Data.Aeson (Value (Object))
import Data.ByteString qualified as BS
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Ecluse.Core.Deadline (inspectionDeadline)
import Ecluse.Core.Mirror.Documents (MirrorDocuments (..), restoreFragments)
import Ecluse.Core.Mirror.State (MirrorVersion, recordPackage)
import Ecluse.Core.Mirror.State.Format (MirrorStateFormat (..), noProjectEnvelope)
import Ecluse.Core.Mirror.State.Weight (fragmentShellAllowance)
import Ecluse.Core.Package (Artifact (..), PackageDetails (pkgArtifacts, pkgPublishedAt))
import Ecluse.Core.Package.Entry (EntryKey (RecordEntry))
import Ecluse.Core.Registry (ParseError (parseErrorMessage), PublishFault (PublishSourceUnavailable))
import Ecluse.Core.Registry.CachedDocument (rubyGemsCached)
import Ecluse.Core.Registry.Metadata (VersionDoc (..))
import Ecluse.Core.Registry.MirrorUpload
import Ecluse.Core.Registry.Publish (MirrorCompanion (..))
import Ecluse.Core.Registry.RubyGems.CompactSpec (compactSpecBytes, validateCompactSpec)
import Ecluse.Core.Registry.RubyGems.Document (GemDocument (..), GemRow (..))
import Ecluse.Core.Registry.RubyGems.Project (coordinateFilename, gemFileStem)
import Ecluse.Core.Registry.RubyGems.Request (compactSpecPath, gemPath)
import Ecluse.Core.Registry.RubyGems.SourceState (gemSourceCompanions, gemSourceDecodeCharge, gemSourcePublishedAt, gemSourceValue, parseGemSource)
import Ecluse.Core.Server.Cache.VersionWeight.Fields (retainedRawWeight)
import Ecluse.Core.Server.Path (unFilename)
rubyMirrorCompanion :: MirrorCompanion
rubyMirrorCompanion :: MirrorCompanion
rubyMirrorCompanion =
MirrorCompanion
{ mcLocation :: ExpectedMirrorFile -> Text -> Either PublishFault Text
mcLocation = \ExpectedMirrorFile
file Text
source -> do
let filename :: Text
filename = Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file)
base <- PublishFault -> Maybe Text -> Either PublishFault Text
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishSourceUnavailable Text
"RubyGems artifact URL disagrees with its native filename") (Text -> Text -> Maybe Text
T.stripSuffix (Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
gemPath Text
filename) Text
source)
stem <- maybeToRight (PublishSourceUnavailable "RubyGems artifact lacks its native extension") (gemFileStem filename)
pure (base <> "/" <> compactSpecPath stem)
, mcValidate :: Deadlines
-> Limits
-> (Int -> IO ())
-> MirrorInventory
-> ExpectedMirrorFile
-> ByteString
-> IO (Either PublishFault ByteString)
mcValidate = \Deadlines
deadlines Limits
limits Int -> IO ()
charge MirrorInventory
inventory ExpectedMirrorFile
file ByteString
bytes -> Deadlines
-> PublishFault
-> IO (Either PublishFault ByteString)
-> IO (Either PublishFault ByteString)
forall e a. Deadlines -> e -> IO (Either e a) -> IO (Either e a)
inspectionDeadline Deadlines
deadlines (Text -> PublishFault
PublishSourceUnavailable Text
"compact specification agreement timed out") (IO (Either PublishFault ByteString)
-> IO (Either PublishFault ByteString))
-> IO (Either PublishFault ByteString)
-> IO (Either PublishFault ByteString)
forall a b. (a -> b) -> a -> b
$ case MirrorInventory -> ExpectedMirrorFile -> Either PublishFault GemRow
mirrorRow MirrorInventory
inventory ExpectedMirrorFile
file of
Left PublishFault
fault -> Either PublishFault ByteString
-> IO (Either PublishFault ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublishFault -> Either PublishFault ByteString
forall a b. a -> Either a b
Left PublishFault
fault)
Right GemRow
row -> (Either ParseError CompactSpec -> Either PublishFault ByteString)
-> IO (Either ParseError CompactSpec)
-> IO (Either PublishFault ByteString)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ParseError -> PublishFault)
-> Either ParseError ByteString -> Either PublishFault ByteString
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> PublishFault
PublishSourceUnavailable (Text -> PublishFault)
-> (ParseError -> Text) -> ParseError -> PublishFault
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Text
parseErrorMessage) (Either ParseError ByteString -> Either PublishFault ByteString)
-> (Either ParseError CompactSpec -> Either ParseError ByteString)
-> Either ParseError CompactSpec
-> Either PublishFault ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CompactSpec -> ByteString)
-> Either ParseError CompactSpec -> Either ParseError ByteString
forall a b. (a -> b) -> Either ParseError a -> Either ParseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CompactSpec -> ByteString
compactSpecBytes) (Limits
-> (Int -> IO ())
-> GemCoordinate
-> GemRow
-> ByteString
-> IO (Either ParseError CompactSpec)
validateCompactSpec Limits
limits Int -> IO ()
charge (GemRow -> GemCoordinate
grCoordinate GemRow
row) GemRow
row ByteString
bytes)
}
rubyGemsMirrorDocuments :: MirrorDocuments
rubyGemsMirrorDocuments :: MirrorDocuments
rubyGemsMirrorDocuments =
MirrorDocuments
{ mdRestore :: MirrorVersion -> [Artifact] -> Maybe VersionDoc
mdRestore = MirrorVersion -> [Artifact] -> Maybe VersionDoc
restoreGemVersion
, mdOrder :: [(Text, VersionDoc)] -> [(Text, VersionDoc)]
mdOrder = [(Text, VersionDoc)] -> [(Text, VersionDoc)]
forall a. a -> a
id
, mdEntryKey :: Int -> EntryKey
mdEntryKey = Int -> EntryKey
RecordEntry
, mdRekey :: (EntryKey -> EntryKey) -> CachedDoc -> Maybe CachedDoc
mdRekey = \EntryKey -> EntryKey
rekey CachedDoc
raw -> do
native <- (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached CachedDoc
raw
pure (fst rubyGemsCached native{gemRows = [row{grEntryKey = rekey (grEntryKey row)} | row <- gemRows native]})
, mdAssemble :: ProjectMirrorRecord -> [VersionDoc] -> CachedDoc
mdAssemble = \ProjectMirrorRecord
record [VersionDoc]
docs ->
let documents :: [GemDocument]
documents = [GemDocument
document | VersionDoc
doc <- [VersionDoc]
docs, Just CachedDoc
raw <- [VersionDoc -> Maybe CachedDoc
vdRaw VersionDoc
doc], Just GemDocument
document <- [(GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached CachedDoc
raw]]
in (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> GemDocument -> CachedDoc
forall a b. (a, b) -> a
fst (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached (PackageName -> ByteString -> [GemRow] -> GemDocument
GemDocument (ProjectMirrorRecord -> PackageName
recordPackage ProjectMirrorRecord
record) (ByteString
-> (GemDocument -> ByteString) -> Maybe GemDocument -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ByteString
forall a. Monoid a => a
mempty GemDocument -> ByteString
gemHeader ([GemDocument] -> Maybe GemDocument
forall a. [a] -> Maybe a
listToMaybe [GemDocument]
documents)) ((GemDocument -> [GemRow]) -> [GemDocument] -> [GemRow]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GemDocument -> [GemRow]
gemRows [GemDocument]
documents))
}
rubyGemsStateFormat :: MirrorStateFormat
rubyGemsStateFormat :: MirrorStateFormat
rubyGemsStateFormat =
MirrorStateFormat
{ msfKind :: Text
msfKind = Text
"rubygems"
, msfEncodeSource :: MirrorReadData -> Maybe Value
msfEncodeSource = \case
RubyGemsMirrorRead GemDocument
document -> Value -> Maybe Value
forall a. a -> Maybe a
Just (GemDocument -> Value
gemSourceValue GemDocument
document)
MirrorReadData
_ -> Maybe Value
forall a. Maybe a
Nothing
, msfDecodeSource :: Object -> Parser MirrorReadData
msfDecodeSource = (GemDocument -> MirrorReadData)
-> Parser GemDocument -> Parser MirrorReadData
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GemDocument -> MirrorReadData
RubyGemsMirrorRead (Parser GemDocument -> Parser MirrorReadData)
-> (Object -> Parser GemDocument)
-> Object
-> Parser MirrorReadData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser GemDocument
parseGemSource (Value -> Parser GemDocument)
-> (Object -> Value) -> Object -> Parser GemDocument
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> Value
Object
, msfDecodeCharge :: Value -> Integer
msfDecodeCharge = Value -> Integer
gemSourceDecodeCharge
, msfForceSource :: MirrorReadData -> ()
msfForceSource = \case
RubyGemsMirrorRead GemDocument
document -> (() -> GemRow -> ()) -> () -> [GemRow] -> ()
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\() GemRow
row -> ByteString -> Int
BS.length (GemRow -> ByteString
grSource GemRow
row) Int -> () -> ()
forall a b. a -> b -> b
`seq` ()) () (GemDocument -> [GemRow]
gemRows GemDocument
document)
MirrorReadData
_ -> ()
, msfEnvelope :: ProjectEnvelope
msfEnvelope = Text -> ProjectEnvelope
noProjectEnvelope Text
"RubyGems"
, msfFragmentShell :: PackageDetails -> Integer
msfFragmentShell = Integer -> PackageDetails -> Integer
forall a b. a -> b -> a
const Integer
fragmentShellAllowance
, msfSourceWeight :: MirrorReadData -> Integer
msfSourceWeight = \case
RubyGemsMirrorRead GemDocument
document -> CachedDoc -> Integer
retainedRawWeight ((GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> GemDocument -> CachedDoc
forall a b. (a, b) -> a
fst (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached GemDocument
document)
MirrorReadData
_ -> Integer
0
, msfCompanions :: CompanionRequirement
msfCompanions = CompanionRequirement
gemSourceCompanions
}
restoreGemVersion :: MirrorVersion -> [Artifact] -> Maybe VersionDoc
restoreGemVersion :: MirrorVersion -> [Artifact] -> Maybe VersionDoc
restoreGemVersion MirrorVersion
mirror [Artifact]
current = do
fragments@((firstDetails, firstSource, _) :| _) <- (Int -> EntryKey)
-> (MirrorReadData -> Maybe (GemDocument, Map EntryKey GemRow))
-> MirrorVersion
-> [Artifact]
-> Maybe
(NonEmpty
(PackageDetails, GemDocument, NonEmpty (Artifact, GemRow)))
forall source entry.
(Int -> EntryKey)
-> (MirrorReadData -> Maybe (source, Map EntryKey entry))
-> MirrorVersion
-> [Artifact]
-> Maybe
(NonEmpty (PackageDetails, source, NonEmpty (Artifact, entry)))
restoreFragments Int -> EntryKey
RecordEntry MirrorReadData -> Maybe (GemDocument, Map EntryKey GemRow)
gemEntries MirrorVersion
mirror [Artifact]
current
let pairs = ((PackageDetails, GemDocument, NonEmpty (Artifact, GemRow))
-> [(Artifact, GemRow)])
-> NonEmpty
(PackageDetails, GemDocument, NonEmpty (Artifact, GemRow))
-> [(Artifact, GemRow)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(PackageDetails
_, GemDocument
_, NonEmpty (Artifact, GemRow)
restored) -> NonEmpty (Artifact, GemRow) -> [(Artifact, GemRow)]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty (Artifact, GemRow)
restored) NonEmpty (PackageDetails, GemDocument, NonEmpty (Artifact, GemRow))
fragments
source = GemDocument
firstSource{gemRows = [row{grEntryKey = artEntryKey artifact} | (artifact, row) <- pairs]}
artifacts <- nonEmpty (map fst pairs)
pure (VersionDoc firstDetails{pkgArtifacts = artifacts, pkgPublishedAt = gemSourcePublishedAt source} (Just (fst rubyGemsCached source)))
where
gemEntries :: MirrorReadData -> Maybe (GemDocument, Map EntryKey GemRow)
gemEntries = \case
RubyGemsMirrorRead GemDocument
source -> (GemDocument, Map EntryKey GemRow)
-> Maybe (GemDocument, Map EntryKey GemRow)
forall a. a -> Maybe a
Just (GemDocument
source, [(EntryKey, GemRow)] -> Map EntryKey GemRow
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(GemRow -> EntryKey
grEntryKey GemRow
row, GemRow
row) | GemRow
row <- GemDocument -> [GemRow]
gemRows GemDocument
source])
MirrorReadData
_ -> Maybe (GemDocument, Map EntryKey GemRow)
forall a. Maybe a
Nothing
mirrorRow :: MirrorInventory -> ExpectedMirrorFile -> Either PublishFault GemRow
mirrorRow :: MirrorInventory -> ExpectedMirrorFile -> Either PublishFault GemRow
mirrorRow MirrorInventory
inventory ExpectedMirrorFile
file = case MirrorInventory -> MirrorReadData
inventoryReadData MirrorInventory
inventory of
RubyGemsMirrorRead GemDocument
source -> case (GemRow -> Bool) -> [GemRow] -> [GemRow]
forall a. (a -> Bool) -> [a] -> [a]
filter ((EntryKey -> EntryKey -> Bool
forall a. Eq a => a -> a -> Bool
== Artifact -> EntryKey
artEntryKey (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file)) (EntryKey -> Bool) -> (GemRow -> EntryKey) -> GemRow -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GemRow -> EntryKey
grEntryKey) (GemDocument -> [GemRow]
gemRows GemDocument
source) of
[GemRow
row] | Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename (GemRow -> GemCoordinate
grCoordinate GemRow
row)) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file) -> GemRow -> Either PublishFault GemRow
forall a b. b -> Either a b
Right GemRow
row
[GemRow]
_ -> PublishFault -> Either PublishFault GemRow
forall a b. a -> Either a b
Left (Text -> PublishFault
PublishSourceUnavailable Text
"RubyGems upload has no unique paired source row")
MirrorReadData
_ -> PublishFault -> Either PublishFault GemRow
forall a b. a -> Either a b
Left (Text -> PublishFault
PublishSourceUnavailable Text
"RubyGems upload has no native source document")