Skip to content

Commit d311cae

Browse files
committed
Swift 6
1 parent c162d6a commit d311cae

File tree

7 files changed

+47
-28
lines changed

7 files changed

+47
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
/.build
33
/.swiftpm
4+
/.index-build
45
/Packages
56
/*.xcodeproj
67
xcuserdata/

Package.resolved

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.9
1+
// swift-tools-version:6.0
22

33
import PackageDescription
44

@@ -22,16 +22,10 @@ let package = Package(
2222
dependencies: [
2323
"ComputeRuntime",
2424
.product(name: "Crypto", package: "swift-crypto"),
25-
],
26-
swiftSettings: [
27-
.enableExperimentalFeature("StrictConcurrency")
2825
]
2926
),
3027
.target(
31-
name: "ComputeRuntime",
32-
swiftSettings: [
33-
.enableExperimentalFeature("StrictConcurrency")
34-
]
28+
name: "ComputeRuntime"
3529
),
3630
.executableTarget(
3731
name: "ComputeDemo",

Sources/Compute/Compute.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
//
77

88
@_exported import Foundation
9+
910
#if canImport(FoundationNetworking)
10-
@_exported import FoundationNetworking
11+
@_exported import FoundationNetworking
1112
#endif
1213

13-
public func onIncomingRequest(_ handler: @escaping (_ req: IncomingRequest, _ res: OutgoingResponse) async throws -> Void) async throws {
14+
@MainActor
15+
public func onIncomingRequest(
16+
_ handler: @escaping (_ req: IncomingRequest, _ res: OutgoingResponse) async throws -> Void
17+
) async throws {
1418
// Initialize ABI
1519
try Fastly.ABI.initialize()
1620

Sources/Compute/OutgoingResponse.swift

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

8-
public final class OutgoingResponse {
8+
public actor OutgoingResponse {
99

1010
internal private(set) var response: Fastly.Response
1111

@@ -222,7 +222,8 @@ extension OutgoingResponse {
222222
extension OutgoingResponse {
223223

224224
@discardableResult
225-
public func write<T>(_ value: T, encoder: JSONEncoder = .init()) async throws -> Self where T: Encodable & Sendable {
225+
public func write<T>(_ value: T, encoder: JSONEncoder = .init()) async throws -> Self
226+
where T: Encodable & Sendable {
226227
try await sendAndStream()
227228
try await body.write(value, encoder: encoder)
228229
return self
@@ -295,7 +296,7 @@ extension OutgoingResponse {
295296

296297
private let invalidProxyHeaders: Set<String> = [
297298
HTTPHeader.altSvc.rawValue,
298-
HTTPHeader.transferEncoding.rawValue
299+
HTTPHeader.transferEncoding.rawValue,
299300
]
300301

301302
// MARK: - CORS
@@ -313,9 +314,11 @@ extension OutgoingResponse {
313314
) -> Self {
314315
headers[.accessControlAllowOrigin] = origin
315316
headers[.accessControlAllowMethods] = methods.map { $0.rawValue }.joined(separator: ", ")
316-
headers[.accessControlAllowHeaders] = allowHeaders?.map { $0.stringValue }.joined(separator: ", ") ?? "*"
317+
headers[.accessControlAllowHeaders] =
318+
allowHeaders?.map { $0.stringValue }.joined(separator: ", ") ?? "*"
317319
headers[.accessControlAllowCredentials] = allowCredentials?.description
318-
headers[.accessControlExposeHeaders] = exposeHeaders?.map { $0.stringValue }.joined(separator: ", ")
320+
headers[.accessControlExposeHeaders] = exposeHeaders?.map { $0.stringValue }.joined(
321+
separator: ", ")
319322
headers[.accessControlMaxAge] = String(maxAge)
320323
return self
321324
}
@@ -338,7 +341,8 @@ extension OutgoingResponse {
338341

339342
@discardableResult
340343
public func upgradeToHTTP3(maxAge: Int = 86400) -> Self {
341-
headers[.altSvc] = #"h3=":443";ma=\#(maxAge),h3-29=":443";ma=\#(maxAge),h3-27=":443";ma=\#(maxAge)"#
344+
headers[.altSvc] =
345+
#"h3=":443";ma=\#(maxAge),h3-29=":443";ma=\#(maxAge),h3-27=":443";ma=\#(maxAge)"#
342346
return self
343347
}
344348
}
@@ -412,8 +416,10 @@ extension OutgoingResponse {
412416
_ value: String,
413417
_ options: [CookieOption]
414418
) -> Self {
415-
let encodedName = name.addingPercentEncoding(withAllowedCharacters: .javascriptURLAllowed) ?? name
416-
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .javascriptURLAllowed) ?? value
419+
let encodedName =
420+
name.addingPercentEncoding(withAllowedCharacters: .javascriptURLAllowed) ?? name
421+
let encodedValue =
422+
value.addingPercentEncoding(withAllowedCharacters: .javascriptURLAllowed) ?? value
417423
let parts = ["\(encodedName)=\(encodedValue)"] + options.map(\.value)
418424
let header = parts.joined(separator: "; ")
419425
headers.append(.setCookie, header)

Sources/Compute/Router/Router.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//
22
// Router.swift
3-
//
3+
//
44
//
55
// Created by Andrew Barba on 4/4/22.
66
//
77

8+
@MainActor
89
public final class Router {
910

1011
public typealias Handler = (IncomingRequest, OutgoingResponse) async throws -> Void
@@ -31,15 +32,18 @@ public final class Router {
3132

3233
private func handler(for req: inout IncomingRequest) -> Handler? {
3334
let pathComponents = req.url.pathComponents.dropFirst()
34-
return router.route(path: [req.method.rawValue] + pathComponents, parameters: &req.pathParams)
35+
return router.route(
36+
path: [req.method.rawValue] + pathComponents, parameters: &req.pathParams)
3537
}
3638
}
3739

3840
extension Router {
3941

4042
@discardableResult
4143
public func listen() async throws -> Self {
42-
try await onIncomingRequest(self.run)
44+
try await onIncomingRequest { req, res in
45+
try await self.run(req, res)
46+
}
4347
return self
4448
}
4549
}
@@ -58,7 +62,7 @@ extension Router {
5862
}
5963

6064
@discardableResult
61-
public func put (_ path: String, _ handler: @escaping Handler) -> Self {
65+
public func put(_ path: String, _ handler: @escaping Handler) -> Self {
6266
return add(method: .put, path: path, handler: handler)
6367
}
6468

@@ -109,7 +113,7 @@ extension Router {
109113
}
110114

111115
// Check if response was already sent
112-
guard res.didSend == false else {
116+
guard await res.didSend == false else {
113117
return
114118
}
115119

Sources/ComputeDemo/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ try await onIncomingRequest { req, res in
1717
try await res.send([
1818
"verified": verified,
1919
"signature": jwt.signature.toHexString(),
20-
"jwt": JWT(claims: ["a": "b"], secret: "hello-world").token
20+
"jwt": JWT(claims: ["a": "b"], secret: "hello-world").token,
2121
])
2222
}

0 commit comments

Comments
 (0)