Skip to content

Commit

Permalink
Embed static.zip in Swift code instead of downloading from remote (#334)
Browse files Browse the repository at this point in the history
* Embed static.zip in Swift code instead of downloading from remote

* Make base64 decode logic failure

* Add rationale for the use of base64
  • Loading branch information
kateinoigakukun authored May 11, 2022
1 parent cd7377f commit ad9f73c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 54 deletions.
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

0 comments on commit ad9f73c

Please sign in to comment.