Skip to content
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
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on: push
jobs:
build:
runs-on: ubuntu-latest
container: ghcr.io/swiftwasm/swift:5.9
container: swift:6.0

steps:
- uses: actions/checkout@v4

- run: swift --version

- run: swift build -c debug --triple wasm32-unknown-wasi
- 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

- run: swift build --swift-sdk wasm32-unknown-wasi
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.0.3
5 changes: 4 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
format:
swift format --in-place --recursive .

build:
swift build -c debug --triple wasm32-unknown-wasi
swift build -c debug --swift-sdk wasm32-unknown-wasi

demo: build
fastly compute serve --skip-build --file ./.build/debug/ComputeDemo.wasm
393 changes: 20 additions & 373 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
.library(name: "Compute", targets: ["Compute"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto", from: "3.0.0")
.package(url: "https://github.com/apple/swift-crypto", "1.0.0"..<"4.0.0")
],
targets: [
.target(
Expand All @@ -35,5 +35,9 @@ let package = Package(
name: "ComputeTests",
dependencies: ["Compute"]
),
],
swiftLanguageVersions: [
.version("6"),
.v5,
]
)
26 changes: 19 additions & 7 deletions Sources/Compute/Cache.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//
// Cache.swift
//
//
//
// Created by Andrew Barba on 9/13/23.
//

public struct Cache: Sendable {

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

public static func getOrSet(_ key: String, _ handler: () async throws -> ([UInt8], CachePolicy)) async throws -> Entry {
public static func getOrSet(_ key: String, _ handler: () async throws -> ([UInt8], CachePolicy))
async throws -> Entry
{
let trx = try await Fastly.Cache.getOrSet(key) {
let (bytes, cachePolicy) = try await handler()
return (.bytes(bytes), cachePolicy)
}
return try .init(trx)
}

public static func getOrSet(_ key: String, _ handler: () async throws -> (String, CachePolicy)) async throws -> Entry {
public static func getOrSet(_ key: String, _ handler: () async throws -> (String, CachePolicy))
async throws -> Entry
{
let trx = try await Fastly.Cache.getOrSet(key) {
let (text, cachePolicy) = try await handler()
return (.bytes(.init(text.utf8)), cachePolicy)
}
return try .init(trx)
}

public static func getOrSet(_ key: String, _ handler: () async throws -> (Data, CachePolicy)) async throws -> Entry {
public static func getOrSet(_ key: String, _ handler: () async throws -> (Data, CachePolicy))
async throws -> Entry
{
let trx = try await Fastly.Cache.getOrSet(key) {
let (data, cachePolicy) = try await handler()
return (.bytes(data.bytes), cachePolicy)
}
return try .init(trx)
}

public static func getOrSet(_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)) async throws -> Entry {
public static func getOrSet(
_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)
) async throws -> Entry {
let trx = try await Fastly.Cache.getOrSet(key) {
let (json, cachePolicy) = try await handler()
let data = try JSONSerialization.data(withJSONObject: json)
Expand All @@ -49,7 +59,9 @@ public struct Cache: Sendable {
return try .init(trx)
}

public static func getOrSet(_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)) async throws -> Entry {
public static func getOrSet(
_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)
) async throws -> Entry {
let trx = try await Fastly.Cache.getOrSet(key) {
let (json, cachePolicy) = try await handler()
let data = try JSONSerialization.data(withJSONObject: json)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Compute/Concurrency.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Concurrency.swift
//
//
//
// Created by Andrew Barba on 11/27/22.
//
Expand All @@ -10,7 +10,7 @@ extension Data: @unchecked Sendable {}
extension URL: @unchecked Sendable {}

#if !arch(wasm32)
extension HTTPURLResponse: @unchecked Sendable {}
extension HTTPURLResponse: @unchecked Sendable {}
#endif

extension Task where Success == Never, Failure == Never {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/ConfigStore.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ConfigStore.swift
//
//
//
// Created by Andrew Barba on 11/27/22.
//
Expand Down
4 changes: 2 additions & 2 deletions Sources/Compute/Console.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Console.swift
//
//
//
// Created by Andrew Barba on 3/23/22.
//
Expand Down Expand Up @@ -35,7 +35,7 @@ public struct Console: Sendable {
}
}

fileprivate struct StandardErrorOutputStream: TextOutputStream {
private struct StandardErrorOutputStream: TextOutputStream {

private let stderr = FileHandle.standardError

Expand Down
31 changes: 20 additions & 11 deletions Sources/Compute/Crypto.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Crypto.swift
//
//
//
// Created by Andrew Barba on 2/8/23.
//
Expand All @@ -13,15 +13,18 @@ public enum Crypto {}

extension Crypto {

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

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

public static func hash<T>(_ input: Data, using hash: T.Type) -> T.Digest where T: HashFunction {
public static func hash<T>(_ input: Data, using hash: T.Type) -> T.Digest
where T: HashFunction {
return T.hash(data: input)
}

Expand Down Expand Up @@ -92,7 +95,9 @@ extension Crypto {
}
}

public static func verify(_ input: String, signature: Data, secret: String, using hash: Hash) -> Bool {
public static func verify(
_ input: String, signature: Data, secret: String, using hash: Hash
) -> Bool {
let computed = code(for: input, secret: secret, using: hash)
return computed.toHexString() == signature.toHexString()
}
Expand All @@ -109,7 +114,9 @@ extension Crypto {
case p521
}

public static func signature(for input: String, secret: String, using algorithm: Algorithm) throws -> Data {
public static func signature(for input: String, secret: String, using algorithm: Algorithm)
throws -> Data
{
switch algorithm {
case .p256:
let pk = try P256.Signing.PrivateKey(pemRepresentation: secret)
Expand All @@ -123,7 +130,9 @@ extension Crypto {
}
}

public static func verify(_ input: String, signature: Data, key: String, using algorithm: Algorithm) throws -> Bool {
public static func verify(
_ input: String, signature: Data, key: String, using algorithm: Algorithm
) throws -> Bool {
switch algorithm {
case .p256:
let publicKey = try P256.Signing.PublicKey(pemRepresentation: key)
Expand Down Expand Up @@ -154,7 +163,7 @@ extension DataProtocol {
}

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

Expand All @@ -168,7 +177,7 @@ extension Digest {
}

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

Expand All @@ -182,7 +191,7 @@ extension HashedAuthenticationCode {
}

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

Expand All @@ -192,6 +201,6 @@ extension Array where Element == UInt8 {
}

public func toHexString() -> String {
return reduce("") {$0 + String(format: "%02x", $1)}
return reduce("") { $0 + String(format: "%02x", $1) }
}
}
27 changes: 18 additions & 9 deletions Sources/Compute/Fanout/FanoutClient.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// FanoutClient.swift
//
//
//
// Created by Andrew Barba on 2/1/23.
//
Expand All @@ -19,19 +19,24 @@ public struct FanoutClient: Sendable {
"https://\(hostname)/service/\(service)/publish/"
}

public init(service: String = Fastly.Environment.serviceId, token: String, hostname: String = "api.fastly.com") {
public init(
service: String = Fastly.Environment.serviceId, token: String,
hostname: String = "api.fastly.com"
) {
self.service = service
self.token = token
self.hostname = hostname
}

@discardableResult
public func publish(_ message: PublishMessage) async throws -> FetchResponse {
return try await fetch(publishEndpoint, .options(
method: .post,
body: .json(message),
headers: ["Fastly-Key": token]
))
return try await fetch(
publishEndpoint,
.options(
method: .post,
body: .json(message),
headers: ["Fastly-Key": token]
))
}

@discardableResult
Expand All @@ -43,7 +48,9 @@ public struct FanoutClient: Sendable {
}

@discardableResult
public func publish<T: Encodable>(_ value: T, encoder: JSONEncoder = .init(), to channel: String) async throws -> FetchResponse {
public func publish<T: Encodable>(
_ value: T, encoder: JSONEncoder = .init(), to channel: String
) async throws -> FetchResponse {
let content = try encoder.encode(value)
return try await publish(content, to: channel)
}
Expand All @@ -56,7 +63,9 @@ public struct FanoutClient: Sendable {
}

@discardableResult
public func publish(_ jsonObject: [String: Any], to channel: String) async throws -> FetchResponse {
public func publish(_ jsonObject: [String: Any], to channel: String) async throws
-> FetchResponse
{
let data = try JSONSerialization.data(withJSONObject: jsonObject)
let content = String(data: data, encoding: .utf8)
return try await publish(content, to: channel)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/Fanout/FanoutMessage.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// FanoutMessage.swift
//
//
//
// Created by Andrew Barba on 1/31/23.
//
Expand Down
16 changes: 9 additions & 7 deletions Sources/Compute/Fanout/IncomingRequest+Fanout.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// IncomingRequest+Fanout.swift
//
//
//
// Created by Andrew Barba on 2/1/23.
//
Expand Down Expand Up @@ -37,14 +37,16 @@ extension IncomingRequest {
}

public func verifyFanoutRequest() throws {
guard let token = headers[.gripSig] else {
throw FanoutRequestError.invalidSignature
}
let jwt = try JWT(token: token)
try jwt.verify(key: fanoutPublicKey)
guard let token = headers[.gripSig] else {
throw FanoutRequestError.invalidSignature
}
let jwt = try JWT(token: token)
try jwt.verify(key: fanoutPublicKey)
}

public func upgradeWebsocket(to destination: UpgradeWebsocketDestination, hostname: String = "localhost") throws {
public func upgradeWebsocket(
to destination: UpgradeWebsocketDestination, hostname: String = "localhost"
) throws {
switch destination {
case .proxy:
try request.redirectToWebsocketProxy(backend: hostname)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/Fanout/OutgoingResponse+Fanout.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// OutgoingResponse+Fanout.swift
//
//
//
// Created by Andrew Barba on 2/2/23.
//
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/Fastly/Fastly.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Fastly.swift
//
//
//
// Created by Andrew Barba on 11/27/22.
//
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/Fastly/FastlyABI.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ABI.swift
//
//
//
// Created by Andrew Barba on 1/12/22.
//
Expand Down
Loading
Loading