Skip to content

Commit d1f53f8

Browse files
authored
Swift SDK Support (#26)
1 parent 83a6855 commit d1f53f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+881
-927
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ on: push
55
jobs:
66
build:
77
runs-on: ubuntu-latest
8-
container: ghcr.io/swiftwasm/swift:5.9
8+
container: swift:6.0
99

1010
steps:
1111
- uses: actions/checkout@v4
1212

1313
- run: swift --version
1414

15-
- run: swift build -c debug --triple wasm32-unknown-wasi
15+
- run: swift sdk install https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.2-RELEASE/swift-wasm-6.0.2-RELEASE-wasm32-unknown-wasi.artifactbundle.zip --checksum 6ffedb055cb9956395d9f435d03d53ebe9f6a8d45106b979d1b7f53358e1dcb4
16+
17+
- run: swift build --swift-sdk wasm32-unknown-wasi

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.0.3

Justfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
format:
2+
swift format --in-place --recursive .
3+
14
build:
2-
swift build -c debug --triple wasm32-unknown-wasi
5+
swift build -c debug --swift-sdk wasm32-unknown-wasi
36

47
demo: build
58
fastly compute serve --skip-build --file ./.build/debug/ComputeDemo.wasm

LICENSE

Lines changed: 20 additions & 373 deletions
Large diffs are not rendered by default.

Package.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
.library(name: "Compute", targets: ["Compute"])
1515
],
1616
dependencies: [
17-
.package(url: "https://github.com/apple/swift-crypto", from: "3.0.0")
17+
.package(url: "https://github.com/apple/swift-crypto", "1.0.0"..<"4.0.0")
1818
],
1919
targets: [
2020
.target(
@@ -35,5 +35,9 @@ let package = Package(
3535
name: "ComputeTests",
3636
dependencies: ["Compute"]
3737
),
38+
],
39+
swiftLanguageVersions: [
40+
.version("6"),
41+
.v5,
3842
]
3943
)

Sources/Compute/Cache.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//
22
// Cache.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 9/13/23.
66
//
77

88
public struct Cache: Sendable {
99

10-
public static func getOrSet(_ key: String, _ handler: () async throws -> (FetchResponse, CachePolicy)) async throws -> Entry {
10+
public static func getOrSet(
11+
_ key: String, _ handler: () async throws -> (FetchResponse, CachePolicy)
12+
) async throws -> Entry {
1113
let trx = try await Fastly.Cache.getOrSet(key) {
1214
let (res, cachePolicy) = try await handler()
1315
let length = res.headers[.contentLength].flatMap(Int.init)
@@ -16,31 +18,39 @@ public struct Cache: Sendable {
1618
return try .init(trx)
1719
}
1820

19-
public static func getOrSet(_ key: String, _ handler: () async throws -> ([UInt8], CachePolicy)) async throws -> Entry {
21+
public static func getOrSet(_ key: String, _ handler: () async throws -> ([UInt8], CachePolicy))
22+
async throws -> Entry
23+
{
2024
let trx = try await Fastly.Cache.getOrSet(key) {
2125
let (bytes, cachePolicy) = try await handler()
2226
return (.bytes(bytes), cachePolicy)
2327
}
2428
return try .init(trx)
2529
}
2630

27-
public static func getOrSet(_ key: String, _ handler: () async throws -> (String, CachePolicy)) async throws -> Entry {
31+
public static func getOrSet(_ key: String, _ handler: () async throws -> (String, CachePolicy))
32+
async throws -> Entry
33+
{
2834
let trx = try await Fastly.Cache.getOrSet(key) {
2935
let (text, cachePolicy) = try await handler()
3036
return (.bytes(.init(text.utf8)), cachePolicy)
3137
}
3238
return try .init(trx)
3339
}
3440

35-
public static func getOrSet(_ key: String, _ handler: () async throws -> (Data, CachePolicy)) async throws -> Entry {
41+
public static func getOrSet(_ key: String, _ handler: () async throws -> (Data, CachePolicy))
42+
async throws -> Entry
43+
{
3644
let trx = try await Fastly.Cache.getOrSet(key) {
3745
let (data, cachePolicy) = try await handler()
3846
return (.bytes(data.bytes), cachePolicy)
3947
}
4048
return try .init(trx)
4149
}
4250

43-
public static func getOrSet(_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)) async throws -> Entry {
51+
public static func getOrSet(
52+
_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)
53+
) async throws -> Entry {
4454
let trx = try await Fastly.Cache.getOrSet(key) {
4555
let (json, cachePolicy) = try await handler()
4656
let data = try JSONSerialization.data(withJSONObject: json)
@@ -49,7 +59,9 @@ public struct Cache: Sendable {
4959
return try .init(trx)
5060
}
5161

52-
public static func getOrSet(_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)) async throws -> Entry {
62+
public static func getOrSet(
63+
_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)
64+
) async throws -> Entry {
5365
let trx = try await Fastly.Cache.getOrSet(key) {
5466
let (json, cachePolicy) = try await handler()
5567
let data = try JSONSerialization.data(withJSONObject: json)

Sources/Compute/Concurrency.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Concurrency.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 11/27/22.
66
//
@@ -10,7 +10,7 @@ extension Data: @unchecked Sendable {}
1010
extension URL: @unchecked Sendable {}
1111

1212
#if !arch(wasm32)
13-
extension HTTPURLResponse: @unchecked Sendable {}
13+
extension HTTPURLResponse: @unchecked Sendable {}
1414
#endif
1515

1616
extension Task where Success == Never, Failure == Never {

Sources/Compute/ConfigStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ConfigStore.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 11/27/22.
66
//

Sources/Compute/Console.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Console.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 3/23/22.
66
//
@@ -35,7 +35,7 @@ public struct Console: Sendable {
3535
}
3636
}
3737

38-
fileprivate struct StandardErrorOutputStream: TextOutputStream {
38+
private struct StandardErrorOutputStream: TextOutputStream {
3939

4040
private let stderr = FileHandle.standardError
4141

Sources/Compute/Crypto.swift

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Crypto.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 2/8/23.
66
//
@@ -13,15 +13,18 @@ public enum Crypto {}
1313

1414
extension Crypto {
1515

16-
public static func hash<T>(_ input: String, using hash: T.Type) -> T.Digest where T: HashFunction {
16+
public static func hash<T>(_ input: String, using hash: T.Type) -> T.Digest
17+
where T: HashFunction {
1718
return T.hash(data: Data(input.utf8))
1819
}
1920

20-
public static func hash<T>(_ input: [UInt8], using hash: T.Type) -> T.Digest where T: HashFunction {
21+
public static func hash<T>(_ input: [UInt8], using hash: T.Type) -> T.Digest
22+
where T: HashFunction {
2123
return T.hash(data: Data(input))
2224
}
2325

24-
public static func hash<T>(_ input: Data, using hash: T.Type) -> T.Digest where T: HashFunction {
26+
public static func hash<T>(_ input: Data, using hash: T.Type) -> T.Digest
27+
where T: HashFunction {
2528
return T.hash(data: input)
2629
}
2730

@@ -92,7 +95,9 @@ extension Crypto {
9295
}
9396
}
9497

95-
public static func verify(_ input: String, signature: Data, secret: String, using hash: Hash) -> Bool {
98+
public static func verify(
99+
_ input: String, signature: Data, secret: String, using hash: Hash
100+
) -> Bool {
96101
let computed = code(for: input, secret: secret, using: hash)
97102
return computed.toHexString() == signature.toHexString()
98103
}
@@ -109,7 +114,9 @@ extension Crypto {
109114
case p521
110115
}
111116

112-
public static func signature(for input: String, secret: String, using algorithm: Algorithm) throws -> Data {
117+
public static func signature(for input: String, secret: String, using algorithm: Algorithm)
118+
throws -> Data
119+
{
113120
switch algorithm {
114121
case .p256:
115122
let pk = try P256.Signing.PrivateKey(pemRepresentation: secret)
@@ -123,7 +130,9 @@ extension Crypto {
123130
}
124131
}
125132

126-
public static func verify(_ input: String, signature: Data, key: String, using algorithm: Algorithm) throws -> Bool {
133+
public static func verify(
134+
_ input: String, signature: Data, key: String, using algorithm: Algorithm
135+
) throws -> Bool {
127136
switch algorithm {
128137
case .p256:
129138
let publicKey = try P256.Signing.PublicKey(pemRepresentation: key)
@@ -154,7 +163,7 @@ extension DataProtocol {
154163
}
155164

156165
public func toHexString() -> String {
157-
return reduce("") {$0 + String(format: "%02x", $1)}
166+
return reduce("") { $0 + String(format: "%02x", $1) }
158167
}
159168
}
160169

@@ -168,7 +177,7 @@ extension Digest {
168177
}
169178

170179
public func toHexString() -> String {
171-
return reduce("") {$0 + String(format: "%02x", $1)}
180+
return reduce("") { $0 + String(format: "%02x", $1) }
172181
}
173182
}
174183

@@ -182,7 +191,7 @@ extension HashedAuthenticationCode {
182191
}
183192

184193
public func toHexString() -> String {
185-
return reduce("") {$0 + String(format: "%02x", $1)}
194+
return reduce("") { $0 + String(format: "%02x", $1) }
186195
}
187196
}
188197

@@ -192,6 +201,6 @@ extension Array where Element == UInt8 {
192201
}
193202

194203
public func toHexString() -> String {
195-
return reduce("") {$0 + String(format: "%02x", $1)}
204+
return reduce("") { $0 + String(format: "%02x", $1) }
196205
}
197206
}

0 commit comments

Comments
 (0)