Embed static.zip in Swift code instead of downloading from remote (#334)
* Embed static.zip in Swift code instead of downloading from remote * Make base64 decode logic failure * Add rationale for the use of base64
This commit is contained in:
parent
cd7377fb49
commit
ad9f73ceb1
|
@ -18,23 +18,7 @@ import Foundation
|
|||
import TSCBasic
|
||||
import TSCUtility
|
||||
|
||||
/** The `static.zip` archive is always uploaded to release assets of a previous release
|
||||
instead of the forthcoming release, because the corresponding new release tag doesn't exist yet.
|
||||
*/
|
||||
private let staticArchiveURL =
|
||||
"https://github.com/swiftwasm/carton/releases/download/0.14.1/static.zip"
|
||||
|
||||
private let verifyHash = Equality<ByteString, String> {
|
||||
"""
|
||||
Expected SHA256 of \($2), which is
|
||||
\($0.hexadecimalRepresentation)
|
||||
to equal
|
||||
\($1.hexadecimalRepresentation)
|
||||
"""
|
||||
}
|
||||
|
||||
public enum EntrypointError: Error {
|
||||
case downloadFailed(url: String)
|
||||
}
|
||||
|
||||
public struct Entrypoint {
|
||||
|
@ -67,27 +51,9 @@ public struct Entrypoint {
|
|||
try fileSystem.removeFileTree(staticDir)
|
||||
try fileSystem.removeFileTree(archiveFile)
|
||||
|
||||
let client = HTTPClient(eventLoopGroupProvider: .createNew)
|
||||
let request = try HTTPClient.Request.get(url: staticArchiveURL)
|
||||
let response: HTTPClient.Response = try tsc_await {
|
||||
client.execute(request: request).whenComplete($0)
|
||||
}
|
||||
try client.syncShutdown()
|
||||
|
||||
guard
|
||||
var body = response.body,
|
||||
let bytes = body.readBytes(length: body.readableBytes)
|
||||
else { throw EntrypointError.downloadFailed(url: staticArchiveURL) }
|
||||
|
||||
terminal.logLookup("Polyfills archive successfully downloaded from ", staticArchiveURL)
|
||||
|
||||
let downloadedArchive = ByteString(bytes)
|
||||
|
||||
let downloadedHash = SHA256().hash(downloadedArchive)
|
||||
try verifyHash(downloadedHash, staticArchiveHash, context: staticArchiveURL)
|
||||
|
||||
let staticArchiveBytes = Data(base64Encoded: staticArchiveContents)!
|
||||
try fileSystem.createDirectory(cartonDir, recursive: true)
|
||||
try fileSystem.writeFileContents(archiveFile, bytes: downloadedArchive)
|
||||
try fileSystem.writeFileContents(archiveFile, bytes: ByteString(staticArchiveBytes))
|
||||
terminal.logLookup("Unpacking the archive: ", archiveFile)
|
||||
|
||||
try fileSystem.createDirectory(staticDir)
|
||||
|
@ -95,10 +61,5 @@ public struct Entrypoint {
|
|||
ZipArchiver().extract(from: archiveFile, to: staticDir, completion: $0)
|
||||
}
|
||||
}
|
||||
|
||||
let unpackedEntrypointHash = try SHA256().hash(fileSystem.readFileContents(filePath))
|
||||
// Nothing we can do after the hash doesn't match after unpacking
|
||||
try verifyHash(unpackedEntrypointHash, sha256, context: filePath.pathString)
|
||||
terminal.logLookup("Entrypoint integrity verified: ", filePath)
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -69,13 +69,12 @@ struct HashArchive: AsyncParsableCommand {
|
|||
|
||||
try await Process.run(["zip", "-j", "static.zip"] + archiveSources, terminal)
|
||||
|
||||
let archiveHash = try SHA256().hash(
|
||||
localFileSystem.readFileContents(AbsolutePath(
|
||||
localFileSystem.currentWorkingDirectory!,
|
||||
RelativePath("static.zip")
|
||||
))
|
||||
).hexadecimalRepresentation.uppercased()
|
||||
let staticArchiveContents = try localFileSystem.readFileContents(AbsolutePath(
|
||||
localFileSystem.currentWorkingDirectory!,
|
||||
RelativePath("static.zip")
|
||||
))
|
||||
|
||||
// Base64 is not an efficient way, but too long byte array literal breaks type-checker
|
||||
let hashesFileContent = """
|
||||
import TSCBasic
|
||||
|
||||
|
@ -87,10 +86,9 @@ struct HashArchive: AsyncParsableCommand {
|
|||
|
||||
|
||||
"""
|
||||
}.joined())public let staticArchiveHash = ByteString([
|
||||
\(arrayString(from: archiveHash)),
|
||||
])
|
||||
}.joined())
|
||||
|
||||
public let staticArchiveContents = "\(staticArchiveContents.withData { $0.base64EncodedString() })"
|
||||
"""
|
||||
|
||||
try localFileSystem.writeFileContents(
|
||||
|
|
Loading…
Reference in New Issue