Skip to content

Commit 8fd9c97

Browse files
authored
update registry publishing (#6146)
motivation: update based on recent spec changes: * do not request server requirments, deduce configuration based on use provided flags * fix issues in publishing logic per testing
1 parent e1eba90 commit 8fd9c97

File tree

4 files changed

+191
-332
lines changed

4 files changed

+191
-332
lines changed

Sources/PackageRegistry/RegistryClient.swift

Lines changed: 22 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -648,74 +648,6 @@ public final class RegistryClient: Cancellable {
648648
}
649649
}
650650

651-
public func getPublishRequirements(
652-
registryURL: URL,
653-
timeout: DispatchTimeInterval? = .none,
654-
observabilityScope: ObservabilityScope,
655-
callbackQueue: DispatchQueue,
656-
completion: @escaping (Result<PublishRequirements, Error>) -> Void
657-
) {
658-
let completion = self.makeAsync(completion, on: callbackQueue)
659-
660-
guard var components = URLComponents(url: registryURL, resolvingAgainstBaseURL: true) else {
661-
return completion(.failure(RegistryError.invalidURL(registryURL)))
662-
}
663-
components.appendPathComponents("publish-requirements")
664-
guard let url = components.url else {
665-
return completion(.failure(RegistryError.invalidURL(registryURL)))
666-
}
667-
668-
let request = LegacyHTTPClient.Request(
669-
method: .get,
670-
url: url,
671-
headers: [
672-
"Accept": self.acceptHeader(mediaType: .json),
673-
],
674-
options: self.defaultRequestOptions(timeout: timeout, callbackQueue: callbackQueue)
675-
)
676-
677-
self.httpClient.execute(request, observabilityScope: observabilityScope, progress: nil) { result in
678-
completion(
679-
result.tryMap { response in
680-
switch response.statusCode {
681-
case 200:
682-
let publishRequirements = try response.parseJSON(
683-
Serialization.PublishRequirements.self,
684-
decoder: self.jsonDecoder
685-
)
686-
687-
return PublishRequirements(
688-
metadata: .init(
689-
location: publishRequirements.metadata.location.map {
690-
switch $0 {
691-
case .archive:
692-
return .archive
693-
case .request:
694-
return .request
695-
}
696-
}
697-
),
698-
signing: .init(
699-
required: publishRequirements.signing.required,
700-
acceptedSignatureFormats: publishRequirements.signing.acceptedSignatureFormats.map {
701-
switch $0 {
702-
case .CMS_1_0_0:
703-
return .CMS_1_0_0
704-
}
705-
},
706-
trustedRootCertificates: publishRequirements.signing.trustedRootCertificates
707-
)
708-
)
709-
default:
710-
throw self.unexpectedStatusError(response, expectedStatus: [200])
711-
}
712-
}.mapError {
713-
RegistryError.failedRetrievingRegistryPublishRequirements($0)
714-
}
715-
)
716-
}
717-
}
718-
719651
public func publish(
720652
registryURL: URL,
721653
packageIdentity: PackageIdentity,
@@ -759,43 +691,46 @@ public final class RegistryClient: Cancellable {
759691
}
760692

761693
// TODO: add generic support for upload requests in Basics
762-
var body = Data()
763694
let boundary = UUID().uuidString
695+
var parts = [String]()
764696

765697
// archive field
766-
body.append(contentsOf: """
767-
--\(boundary)
698+
parts.append("""
768699
Content-Disposition: form-data; name=\"source-archive\"\r
769700
Content-Type: application/zip\r
770701
Content-Transfer-Encoding: base64\r
771702
Content-Length: \(packageArchiveContent.count)\r
772703
\r
773-
\(packageArchiveContent.base64EncodedString())
774-
""".utf8)
704+
\(packageArchiveContent.base64EncodedString())\r
705+
""")
775706

776707
if let metadataContent = metadataContent {
777-
// spacer
778-
body.append(contentsOf: "\r\n".utf8)
779-
// metadata fiels
780-
body.append(contentsOf: """
781-
--\(boundary)\r
708+
parts.append("""
782709
Content-Disposition: form-data; name=\"metadata\"\r
783710
Content-Type: application/json\r
784711
Content-Transfer-Encoding: quoted-printable\r
785712
Content-Length: \(metadataContent.count)\r
786713
\r
787-
\(metadataContent)
788-
""".utf8)
714+
\(metadataContent)\r
715+
""")
789716
}
790717

718+
let bodyString = """
719+
--\(boundary)\r
720+
\(parts.joined(separator: "\n--\(boundary)\r\n"))
721+
--\(boundary)--\r\n
722+
"""
723+
791724
let request = LegacyHTTPClient.Request(
792725
method: .put,
793726
url: url,
794727
headers: [
728+
"Content-Type": "multipart/form-data;boundary=\"\(boundary)\"",
795729
"Accept": self.acceptHeader(mediaType: .json),
730+
"Expect": "100-continue",
796731
"Prefer": "respond-async",
797732
],
798-
body: body,
733+
body: Data(bodyString.utf8),
799734
options: self.defaultRequestOptions(timeout: timeout, callbackQueue: callbackQueue)
800735
)
801736

@@ -1017,33 +952,12 @@ extension RegistryClient {
1017952
}
1018953
}
1019954

1020-
extension RegistryClient {
1021-
public enum SignatureFormat {
1022-
case CMS_1_0_0
1023-
}
1024-
}
1025-
1026-
extension RegistryClient {
1027-
public struct PublishRequirements {
1028-
public let metadata: Metadata
1029-
public let signing: Signing
1030-
1031-
public struct Metadata {
1032-
public let location: [MetadataLocation]
1033-
}
1034-
1035-
public enum MetadataLocation {
1036-
case request
1037-
case archive
1038-
}
1039-
1040-
public struct Signing {
1041-
public let required: Bool
1042-
public let acceptedSignatureFormats: [SignatureFormat]
1043-
public let trustedRootCertificates: [String]
1044-
}
1045-
}
1046-
}
955+
/*
956+
extension RegistryClient {
957+
public enum SignatureFormat {
958+
case CMS_1_0_0
959+
}
960+
}*/
1047961

1048962
extension RegistryClient {
1049963
public enum PublishResult: Equatable {

0 commit comments

Comments
 (0)