Skip to content

Commit

Permalink
Update Swift version and dependencies (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel committed Dec 4, 2023
1 parent f409dca commit 32de0d7
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Select Xcode 14
run: sudo xcode-select -s /Applications/Xcode_14.3.1.app
- name: Select Xcode 15
run: sudo xcode-select -s /Applications/Xcode_15.0.app
- name: Test
run: swift test
2 changes: 1 addition & 1 deletion .swift-version
@@ -1,2 +1,2 @@
5.8.0
5.9.0

42 changes: 30 additions & 12 deletions Package.resolved
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/shareup/async-extensions.git",
"state" : {
"revision" : "f8dec7a227bbbe15fd4df90c787d4c73e91451ba",
"version" : "4.2.1"
"revision" : "7e727e3b9009a5de429393691f9f499aedb7a109",
"version" : "4.3.0"
}
},
{
Expand All @@ -23,35 +23,53 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "6c89474e62719ddcc1e9614989fff2f68208fe10",
"version" : "1.1.0"
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
"revision" : "a902f1823a7ff3c9ab2fba0f992396b948eda307",
"version" : "1.0.5"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "1827dc94bdab2eb5f2fc804e9b0cb43574282566",
"version" : "1.0.2"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "cf281631ff10ec6111f2761052aa81896a83a007",
"version" : "2.58.0"
"revision" : "702cd7c56d5d44eeba73fdf83918339b26dc855c",
"version" : "2.62.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "0e0d0aab665ff1a0659ce75ac003081f2b1c8997",
"version" : "1.19.0"
"revision" : "798c962495593a23fdea0c0c63fd55571d8dff51",
"version" : "1.20.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "3bd9004b9d685ed6b629760fc84903e48efec806",
"version" : "1.29.0"
}
},
{
Expand All @@ -68,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "e7403c35ca6bb539a7ca353b91cc2d8ec0362d58",
"version" : "1.19.0"
"revision" : "ebf8b9c365a6ce043bf6e6326a04b15589bd285e",
"version" : "1.20.0"
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
@@ -1,4 +1,4 @@
// swift-tools-version:5.8
// swift-tools-version:5.9

import PackageDescription

Expand All @@ -16,19 +16,19 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/shareup/async-extensions.git",
from: "4.1.0"
from: "4.3.0"
),
.package(
url: "https://github.com/shareup/dispatch-timer.git",
from: "3.0.0"
),
.package(
url: "https://github.com/vapor/websocket-kit.git",
from: "2.6.1"
from: "2.14.0"
),
.package(
url: "https://github.com/apple/swift-nio.git",
from: "2.0.0"
from: "2.62.0"
),
],
targets: [
Expand Down
22 changes: 11 additions & 11 deletions Sources/WebSocket/SystemWebSocket.swift
@@ -1,6 +1,6 @@
import AsyncExtensions
@preconcurrency import Combine
@preconcurrency import Foundation
import Foundation
import os.log
import Synchronized

Expand Down Expand Up @@ -292,28 +292,28 @@ private extension SystemWebSocket {
var ws: URLSessionWebSocketTask? {
switch self {
case let .connecting(ws), let .open(ws):
return ws
ws

case .unopened, .closed:
return nil
nil
}
}

var description: String {
switch self {
case .unopened: return "unopened"
case .connecting: return "connecting"
case .open: return "open"
case .closed: return "closed"
case .unopened: "unopened"
case .connecting: "connecting"
case .open: "open"
case .closed: "closed"
}
}

var debugDescription: String {
switch self {
case .unopened: return "unopened"
case let .connecting(ws): return "connecting(\(String(reflecting: ws)))"
case let .open(ws): return "open(\(String(reflecting: ws)))"
case let .closed(error): return "closed(\(error.description))"
case .unopened: "unopened"
case let .connecting(ws): "connecting(\(String(reflecting: ws)))"
case let .open(ws): "open(\(String(reflecting: ws)))"
case let .closed(error): "closed(\(error.description))"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/WebSocket/WebSocketClose.swift
Expand Up @@ -15,15 +15,15 @@ public struct WebSocketClose: Hashable, CustomStringConvertible, Sendable {
public extension WebSocketClose {
var isNormal: Bool {
switch code {
case .normalClosure: return true
default: return false
case .normalClosure: true
default: false
}
}

var isCancelled: Bool {
switch code {
case .cancelled: return true
default: return false
case .cancelled: true
default: false
}
}
}
68 changes: 34 additions & 34 deletions Sources/WebSocket/WebSocketCloseCode.swift
Expand Up @@ -79,23 +79,23 @@ public enum WebSocketCloseCode: Int, CaseIterable, Sendable {
extension WebSocketCloseCode: CustomStringConvertible {
public var description: String {
switch self {
case .invalid: return "invalid"
case .normalClosure: return "normalClosure"
case .goingAway: return "goingAway"
case .protocolError: return "protocolError"
case .unsupportedData: return "unsupportedData"
case .noStatusReceived: return "noStatusReceived"
case .abnormalClosure: return "abnormalClosure"
case .invalidFramePayloadData: return "invalidFramePayloadData"
case .policyViolation: return "policyViolation"
case .messageTooBig: return "messageTooBig"
case .mandatoryExtensionMissing: return "mandatoryExtensionMissing"
case .internalServerError: return "internalServerError"
case .tlsHandshakeFailure: return "tlsHandshakeFailure"
case .cancelled: return "cancelled"
case .alreadyClosed: return "alreadyClosed"
case .timeout: return "timeout"
case .unknown: return "unknown"
case .invalid: "invalid"
case .normalClosure: "normalClosure"
case .goingAway: "goingAway"
case .protocolError: "protocolError"
case .unsupportedData: "unsupportedData"
case .noStatusReceived: "noStatusReceived"
case .abnormalClosure: "abnormalClosure"
case .invalidFramePayloadData: "invalidFramePayloadData"
case .policyViolation: "policyViolation"
case .messageTooBig: "messageTooBig"
case .mandatoryExtensionMissing: "mandatoryExtensionMissing"
case .internalServerError: "internalServerError"
case .tlsHandshakeFailure: "tlsHandshakeFailure"
case .cancelled: "cancelled"
case .alreadyClosed: "alreadyClosed"
case .timeout: "timeout"
case .unknown: "unknown"
}
}
}
Expand Down Expand Up @@ -136,23 +136,23 @@ extension WebSocketCloseCode {

var wsCloseCode: URLSessionWebSocketTask.CloseCode? {
switch self {
case .invalid: return .invalid
case .normalClosure: return .normalClosure
case .goingAway: return .goingAway
case .protocolError: return .protocolError
case .unsupportedData: return .unsupportedData
case .noStatusReceived: return .noStatusReceived
case .abnormalClosure: return .abnormalClosure
case .invalidFramePayloadData: return .invalidFramePayloadData
case .policyViolation: return .policyViolation
case .messageTooBig: return .messageTooBig
case .mandatoryExtensionMissing: return .mandatoryExtensionMissing
case .internalServerError: return .internalServerError
case .tlsHandshakeFailure: return .tlsHandshakeFailure
case .cancelled: return nil
case .alreadyClosed: return nil
case .timeout: return nil
case .unknown: return nil
case .invalid: .invalid
case .normalClosure: .normalClosure
case .goingAway: .goingAway
case .protocolError: .protocolError
case .unsupportedData: .unsupportedData
case .noStatusReceived: .noStatusReceived
case .abnormalClosure: .abnormalClosure
case .invalidFramePayloadData: .invalidFramePayloadData
case .policyViolation: .policyViolation
case .messageTooBig: .messageTooBig
case .mandatoryExtensionMissing: .mandatoryExtensionMissing
case .internalServerError: .internalServerError
case .tlsHandshakeFailure: .tlsHandshakeFailure
case .cancelled: nil
case .alreadyClosed: nil
case .timeout: nil
case .unknown: nil
}
}
}
12 changes: 6 additions & 6 deletions Sources/WebSocket/WebSocketMessage.swift
Expand Up @@ -11,8 +11,8 @@ public enum WebSocketMessage: CustomStringConvertible, Hashable, Sendable {

public var description: String {
switch self {
case let .data(data): return String(decoding: data.prefix(100), as: UTF8.self)
case let .text(text): return text
case let .data(data): String(decoding: data.prefix(100), as: UTF8.self)
case let .text(text): text
}
}
}
Expand All @@ -21,10 +21,10 @@ public extension WebSocketMessage {
var stringValue: String? {
switch self {
case let .data(data):
return String(data: data, encoding: .utf8)
String(data: data, encoding: .utf8)

case let .text(text):
return text
text
}
}
}
Expand All @@ -45,8 +45,8 @@ extension WebSocketMessage {

var wsMessage: URLSessionWebSocketTask.Message {
switch self {
case let .data(data): return .data(data)
case let .text(text): return .string(text)
case let .data(data): .data(data)
case let .text(text): .string(text)
}
}
}
28 changes: 19 additions & 9 deletions Tests/WebSocketTests/Server/WebSocketServer.swift
Expand Up @@ -15,7 +15,9 @@ enum WebSocketServerOutput: Hashable {
private typealias WS = WebSocketKit.WebSocket

final class WebSocketServer {
let port: UInt16
var port: Int { _port! }
private var _port: Int?

let maximumMessageSize: Int

// Publisher provided by consumers of `WebSocketServer` to provide the output
Expand All @@ -30,12 +32,9 @@ final class WebSocketServer {
private var channel: Channel?

init<P: Publisher>(
port: UInt16,
outputPublisher: P,
usesTLS _: Bool = false,
maximumMessageSize: Int = 1024 * 1024
) throws where P.Output == WebSocketServerOutput, P.Failure == Error {
self.port = port
self.outputPublisher = outputPublisher.eraseToAnyPublisher()
self.maximumMessageSize = maximumMessageSize

Expand All @@ -45,26 +44,37 @@ final class WebSocketServer {
on: eventLoopGroup,
onUpgrade: onWebSocketUpgrade
)
.bind(host: "127.0.0.1", port: Int(port))
.bind(to: SocketAddress(
ipAddress: "127.0.0.1",
port: 0 // random port
))
.wait()

if let port = channel?.localAddress?.port {
_port = port
}
}

private func makeWebSocket(
on eventLoopGroup: EventLoopGroup,
onUpgrade: @escaping (HTTPRequestHead, WS) -> Void
) -> ServerBootstrap {
ServerBootstrap(group: eventLoopGroup)
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.childChannelInitializer { (channel: Channel) in
.serverChannelOption(
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
value: 1
)
.childChannelInitializer { channel in
let ws = NIOWebSocketServerUpgrader(
shouldUpgrade: { channel, _ in
channel.eventLoop.makeSucceededFuture([:])
},
upgradePipelineHandler: { channel, req in
WS.server(on: channel) { onUpgrade(req, $0) }
WebSocket.server(on: channel) { ws in
onUpgrade(req, ws)
}
}
)

return channel.pipeline.configureHTTPServerPipeline(
withServerUpgrade: (
upgraders: [ws],
Expand Down

0 comments on commit 32de0d7

Please sign in to comment.