Skip to content

Commit

Permalink
RequestContext with associatedtype Source (#64)
Browse files Browse the repository at this point in the history
* Update for context source

* Use context-source branch until it is merged

* Get working with collapsed RequestContext

* Fixes for name changes

* Use Humminbird 2.0.0-beta.7
  • Loading branch information
adam-fowler authored Jun 17, 2024
1 parent 9ffcaa8 commit 1352aa2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
.library(name: "HummingbirdWSCompression", targets: ["HummingbirdWSCompression"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.62.0"),
Expand Down
12 changes: 6 additions & 6 deletions Sources/HummingbirdWebSocket/WebSocketRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public struct WebSocketHandlerReference<RequestContext: WebSocketRequestContext>
}

/// Request context protocol requirement for routers that support WebSockets
public protocol WebSocketRequestContext: RequestContext, WebSocketContext {
public protocol WebSocketRequestContext: InitializableFromSource<ApplicationRequestContextSource>, WebSocketContext {
var webSocket: WebSocketHandlerReference<Self> { get }
}

/// Default implementation of a request context that supports WebSockets
public struct BasicWebSocketRequestContext: WebSocketRequestContext {
public var coreContext: CoreRequestContext
public struct BasicWebSocketRequestContext: RequestContext, WebSocketRequestContext {
public var coreContext: CoreRequestContextStorage
public let webSocket: WebSocketHandlerReference<Self>

public init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
public init(source: Source) {
self.coreContext = .init(source: source)
self.webSocket = .init()
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ extension HTTP1WebSocketUpgradeChannel {
let promise = channel.eventLoop.makePromise(of: ShouldUpgradeResult<WebSocketChannelHandler>.self)
promise.completeWithTask {
let request = Request(head: head, body: .init(buffer: .init()))
let context = WSResponder.Context(channel: channel, logger: logger)
let context = WSResponder.Context(source: .init(channel: channel, logger: logger))
do {
let response = try await webSocketResponder.respond(to: request, context: context)
if response.status == .ok, let webSocketHandler = context.webSocket.handler.withLockedValue({ $0 }) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,13 @@ final class HummingbirdWebSocketTests: XCTestCase {

/// Test context from router is passed through to web socket
func testRouterContextUpdate() async throws {
struct MyRequestContext: WebSocketRequestContext {
var coreContext: CoreRequestContext
struct MyRequestContext: RequestContext, WebSocketRequestContext {
var coreContext: CoreRequestContextStorage
var webSocket: WebSocketHandlerReference<MyRequestContext>
var name: String

init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
init(source: Source) {
self.coreContext = .init(source: source)
self.webSocket = .init()
self.name = ""
}
Expand Down

0 comments on commit 1352aa2

Please sign in to comment.