Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed static.zip in Swift code instead of downloading from remote #334

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions Sources/CartonKit/Model/Entrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,38 +51,15 @@ 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)
try tsc_await {
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)
}
}
7 changes: 3 additions & 4 deletions Sources/CartonKit/Server/StaticArchive.swift

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions Sources/carton-release/HashArchive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down