Skip to content

Commit 2dc064d

Browse files
authored
Remove usages of deprecated registry methods (#8335)
### Motivation: Fix the warnings around using the sync versions of PackageRegistry methods. ### Modifications: Convert the remainder of the PackageRegistry code to be async first, and move existing usages of the old sync methods in PackageRegistryCommand over to the new async alternatives. Clean up unnecessary plumbing of callbackQueues through the async methods. ### Result: All asynchronous methods are `async` based inside Sources/PackageRegistry, and warnings about using deprecated methods have been fixed.
1 parent 23f32be commit 2dc064d

16 files changed

+765
-1055
lines changed

Sources/PackageMetadata/PackageMetadata.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public struct PackageSearchClient {
174174
package: package,
175175
version: version,
176176
fileSystem: self.fileSystem,
177-
observabilityScope: observabilityScope,
178-
callbackQueue: DispatchQueue.sharedConcurrent)
177+
observabilityScope: observabilityScope
178+
)
179179

180180
return Metadata(
181181
licenseURL: metadata.licenseURL,
@@ -285,7 +285,10 @@ public struct PackageSearchClient {
285285
}
286286
let metadata: RegistryClient.PackageMetadata
287287
do {
288-
metadata = try await self.registryClient.getPackageMetadata(package: identity, observabilityScope: observabilityScope, callbackQueue: DispatchQueue.sharedConcurrent)
288+
metadata = try await self.registryClient.getPackageMetadata(
289+
package: identity,
290+
observabilityScope: observabilityScope
291+
)
289292
} catch {
290293
return try await fetchStandalonePackageByURL(error)
291294
}
@@ -403,7 +406,7 @@ extension Package.Author {
403406
url: author.url
404407
)
405408
}
406-
409+
407410
fileprivate init(_ author: PackageCollectionsModel.Package.Author) {
408411
self.init(
409412
name: author.username,

Sources/PackageRegistry/ChecksumTOFU.swift

+57-73
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,26 @@ struct PackageVersionChecksumTOFU {
4343
version: Version,
4444
checksum: String,
4545
timeout: DispatchTimeInterval?,
46-
observabilityScope: ObservabilityScope,
47-
callbackQueue: DispatchQueue
46+
observabilityScope: ObservabilityScope
4847
) async throws {
49-
try await withCheckedThrowingContinuation { continuation in
50-
self.validateSourceArchive(
51-
registry: registry,
52-
package: package,
53-
version: version,
54-
checksum: checksum,
55-
timeout: timeout,
56-
observabilityScope: observabilityScope,
57-
callbackQueue: callbackQueue,
58-
completion: {
59-
continuation.resume(with: $0)
60-
}
61-
)
48+
let expectedChecksum = try await self.getExpectedChecksum(
49+
registry: registry,
50+
package: package,
51+
version: version,
52+
timeout: timeout,
53+
observabilityScope: observabilityScope
54+
)
55+
56+
if checksum != expectedChecksum {
57+
switch self.fingerprintCheckingMode {
58+
case .strict:
59+
throw RegistryError.invalidChecksum(expected: expectedChecksum, actual: checksum)
60+
case .warn:
61+
observabilityScope
62+
.emit(
63+
warning: "the checksum \(checksum) for source archive of \(package) \(version) does not match previously recorded value \(expectedChecksum)"
64+
)
65+
}
6266
}
6367
}
6468

@@ -73,28 +77,14 @@ struct PackageVersionChecksumTOFU {
7377
callbackQueue: DispatchQueue,
7478
completion: @escaping (Result<Void, Error>) -> Void
7579
) {
76-
self.getExpectedChecksum(
77-
registry: registry,
78-
package: package,
79-
version: version,
80-
timeout: timeout,
81-
observabilityScope: observabilityScope,
82-
callbackQueue: callbackQueue
83-
) { result in
84-
completion(
85-
result.tryMap { expectedChecksum in
86-
if checksum != expectedChecksum {
87-
switch self.fingerprintCheckingMode {
88-
case .strict:
89-
throw RegistryError.invalidChecksum(expected: expectedChecksum, actual: checksum)
90-
case .warn:
91-
observabilityScope
92-
.emit(
93-
warning: "the checksum \(checksum) for source archive of \(package) \(version) does not match previously recorded value \(expectedChecksum)"
94-
)
95-
}
96-
}
97-
}
80+
callbackQueue.asyncResult(completion) {
81+
try await self.validateSourceArchive(
82+
registry: registry,
83+
package: package,
84+
version: version,
85+
checksum: checksum,
86+
timeout: timeout,
87+
observabilityScope: observabilityScope
9888
)
9989
}
10090
}
@@ -104,54 +94,48 @@ struct PackageVersionChecksumTOFU {
10494
package: PackageIdentity.RegistryIdentity,
10595
version: Version,
10696
timeout: DispatchTimeInterval?,
107-
observabilityScope: ObservabilityScope,
108-
callbackQueue: DispatchQueue,
109-
completion: @escaping (Result<String, Error>) -> Void
110-
) {
97+
observabilityScope: ObservabilityScope
98+
) async throws -> String {
11199
// We either use a previously recorded checksum, or fetch it from the registry.
112100
if let savedChecksum = try? self.readFromStorage(package: package, version: version, contentType: .sourceCode, observabilityScope: observabilityScope) {
113-
return completion(.success(savedChecksum))
101+
return savedChecksum
114102
}
115103

116104
// Try fetching checksum from registry if:
117105
// - No storage available
118106
// - Checksum not found in storage
119107
// - Reading from storage resulted in error
120-
Task {
121-
do {
122-
let versionMetadata = try await self.versionMetadataProvider(package, version)
123-
guard let sourceArchiveResource = versionMetadata.sourceArchive else {
124-
throw RegistryError.missingSourceArchive
125-
}
126-
guard let checksum = sourceArchiveResource.checksum else {
127-
throw RegistryError.sourceArchiveMissingChecksum(
128-
registry: registry,
129-
package: package.underlying,
130-
version: version
131-
)
132-
}
133-
do {
134-
try self.writeToStorage(
135-
registry: registry,
136-
package: package,
137-
version: version,
138-
checksum: checksum,
139-
contentType: .sourceCode,
140-
observabilityScope: observabilityScope
141-
)
142-
completion(.success(checksum))
143-
} catch {
144-
completion(.failure(error))
145-
}
146-
} catch {
147-
completion(.failure(RegistryError.failedRetrievingReleaseChecksum(
108+
var checksum: String
109+
do {
110+
let versionMetadata = try await self.versionMetadataProvider(package, version)
111+
guard let sourceArchiveResource = versionMetadata.sourceArchive else {
112+
throw RegistryError.missingSourceArchive
113+
}
114+
guard let archiveChecksum = sourceArchiveResource.checksum else {
115+
throw RegistryError.sourceArchiveMissingChecksum(
148116
registry: registry,
149117
package: package.underlying,
150-
version: version,
151-
error: error
152-
)))
118+
version: version
119+
)
153120
}
121+
checksum = archiveChecksum
122+
} catch {
123+
throw RegistryError.failedRetrievingReleaseChecksum(
124+
registry: registry,
125+
package: package.underlying,
126+
version: version,
127+
error: error
128+
)
154129
}
130+
try self.writeToStorage(
131+
registry: registry,
132+
package: package,
133+
version: version,
134+
checksum: checksum,
135+
contentType: .sourceCode,
136+
observabilityScope: observabilityScope
137+
)
138+
return checksum
155139
}
156140

157141
func validateManifest(

0 commit comments

Comments
 (0)