Giter Club home page Giter Club logo

Comments (5)

adam-fowler avatar adam-fowler commented on May 27, 2024 1

I'm not sure what you mean about the async apply method being unavailable in HBAsyncMiddleware.

collateBody needs to run on the EventLoop the request was created on but that is all managed internally. I thought I added an async version of it, but if not you can call

let request = try await request.collateBody().get()

V2 of Hummingbird simplifies this by removing all the EventLoop internals. It is a pure Swift Concurrency based solution.

from hummingbird.

upeugene avatar upeugene commented on May 27, 2024
image

from hummingbird.

adam-fowler avatar adam-fowler commented on May 27, 2024

Sorry my error message isn't clear enough.
Before you access the body in middleware you need to collate the stream of buffers into one buffer. The most likely reason you are seeing this when running behind a proxy is because the proxy is breaking up the body into a lot smaller sections.

var request = request
request.collateBody().flatMap {
    /// You can now access 
    authenticate(request)
}

You also you have the issue where you are processing a stream of buffers in two places (the middleware and the route). If you collate the request body stream you need to edit the request to give it the buffer back, otherwise you are sending a request with an empty body to you route. Unfortunately an authenticator doesn't give you a chance to pass the edited request on. So either you'll have to write a Middleware that acts like an Authenticator but passes on the collated request body, or you could stick another middleware in front of your authenticator that does the request body collation

struct CollateBodyMiddleware: HBMiddleware {
    public func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
        return request.collateBody().flatMap { request in
            return next.respond(to: request)
        }
    }
}

from hummingbird.

ronaldmannak avatar ronaldmannak commented on May 27, 2024

Thanks the quick reply and examples. This is a clearly an implementation issue on my side and not a bug so I'll close the this issue.

I do have a question regarding some potential complexities related to asynchronous operations and event loops. For instance, I noticed that collateBody is based on the event loop, and the method apply(to request: HBRequest, next: HBResponder) async throws is not accessible within HBAsyncMiddleware because HBAsyncMiddleware extends HBMiddleware rather than HBAsyncMiddleware. I'm wondering if the upcoming HummingBird 2.0 release might address these issues and simplify the implementation. If that's the case, I might wait for the 2.0 release to resolve this

from hummingbird.

ronaldmannak avatar ronaldmannak commented on May 27, 2024

Thanks Adam, appreciate it.

Re: apply, I meant the async version of apply isn't available in HBAsyncAuthenticator since HBAsyncAuthenticator extends HBAuthenticator and HBAuthenticator extends HBMiddleware, which means HBAsyncAuthenticator does not extend HBAsyncMiddleware where the async version of apply is declared. It would indeed be great to have a single concurrency based version on HB, it will make it a lot easier to develop.

from hummingbird.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.