Skip to content

Commit

Permalink
Remove EventLoop based functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 1, 2023
1 parent 90ac95d commit 7daf585
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 50 deletions.
57 changes: 9 additions & 48 deletions Sources/Hummingbird/Server/RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Tracing

/// Endpoint path storage
public struct EndpointPath: Sendable {
public init(eventLoop: EventLoop) {
public init() {
self._value = .init(nil)
}

Expand Down Expand Up @@ -65,7 +65,7 @@ public struct HBCoreRequestContext: Sendable {
self.eventLoop = eventLoop
self.allocator = allocator
self.logger = logger
self.endpointPath = .init(eventLoop: eventLoop)
self.endpointPath = .init()
self.parameters = .init()
}

Expand Down Expand Up @@ -126,55 +126,15 @@ extension HBRequestContext {
@inlinable
public var id: String { self.logger[metadataKey: "hb_id"]!.description }

/// Return failed `EventLoopFuture`
@inlinable
public func failure<T>(_ error: Error) -> EventLoopFuture<T> {
return self.eventLoop.makeFailedFuture(error)
}

/// Return failed `EventLoopFuture` with http response status code
@inlinable
public func failure<T>(_ status: HTTPResponseStatus) -> EventLoopFuture<T> {
return self.eventLoop.makeFailedFuture(HBHTTPError(status))
}

/// Return failed `EventLoopFuture` with http response status code and message
@inlinable
public func failure<T>(_ status: HTTPResponseStatus, message: String) -> EventLoopFuture<T> {
return self.eventLoop.makeFailedFuture(HBHTTPError(status, message: message))
}

/// Return succeeded `EventLoopFuture`
@inlinable
public func success<T>(_ value: T) -> EventLoopFuture<T> {
return self.eventLoop.makeSucceededFuture(value)
}

// Return new version of request with collated request body. If you want to process the
// request body in middleware you need to call this to ensure you have the full request
// body. Once this is called the request generated by this should be passed to the nextResponder
@inlinable
public func collateBody(of request: HBRequest) -> EventLoopFuture<HBRequest> {
request.body.consumeBody(
maxSize: self.applicationContext.configuration.maxUploadSize,
on: self.eventLoop
).flatMapThrowing { buffer in
var request = request
request.body = .byteBuffer(buffer)
return request
}
}

// Return new version of request with collated request body. If you want to process the
// request body in middleware you need to call this to ensure you have the full request
// body. Once this is called the request generated by this should be passed to the nextResponder
@inlinable
public func collateBody(of request: HBRequest, maxSize: Int) -> EventLoopFuture<HBRequest> {
request.body.consumeBody(maxSize: maxSize, on: self.eventLoop).flatMapThrowing { buffer in
var request = request
request.body = .byteBuffer(buffer)
return request
}
public func collateBody(of request: HBRequest, maxSize: Int? = nil) async throws -> HBRequest {
let buffer = try await request.body.consumeBody(maxSize: maxSize ?? self.applicationContext.configuration.maxUploadSize)
var request = request
request.body = .byteBuffer(buffer)
return request
}
}

Expand Down Expand Up @@ -204,7 +164,8 @@ public struct HBBasicRequestContext: HBRequestContext, HBRemoteAddressRequestCon
/// Initialize an `HBRequestContext`
/// - Parameters:
/// - applicationContext: Context from Application that instigated the request
/// - channelContext: Context providing source for EventLoop
/// - channel: Channel that generated this request
/// - logger: Logger
public init(
applicationContext: HBApplicationContext,
channel: Channel,
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ final class ApplicationTests: XCTestCase {
func testCollateBody() async throws {
struct CollateMiddleware<Context: HBRequestContext>: HBMiddleware {
func apply(to request: HBRequest, context: Context, next: any HBResponder<Context>) async throws -> HBResponse {
let request = try await context.collateBody(of: request).get()
let request = try await context.collateBody(of: request)
context.logger.info("Buffer size: \(request.body.buffer!.readableBytes)")
return try await next.respond(to: request, context: context)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdTests/AsyncAwaitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class AsyncAwaitTests: XCTestCase {
}

func handle(request: HBRequest, context: HBRequestContext) async throws -> String {
return try await context.success("Hello \(self.name)").get()
return "Hello \(self.name)"
}
}
let router = HBRouterBuilder(context: HBTestRouterContext.self)
Expand Down

0 comments on commit 7daf585

Please sign in to comment.