-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WorkerRequest/WorkerResponse traits #530
Conversation
* WorkerRequest * WorkerResponse
Works like a charm! One notable difference with #527 is that this PR requires the error type to implement |
You have very good points, probably it might make sense to split into different packages, to try to make things clearer for users
The point of the http feature was to be temporary, now we are discussing something more permanent. My only issue, is that I believe we should try to maintain http, as much as possible, as there are other libraries targeting that crate. I'm not even sure that axum is best suited for the current worker model, and most likely other frameworks will soon follow, now that http support is out there... |
Maybe! I'm not so familiar with how Rustaceans tend to approach this (separate packages vs. feature-gating). Either way makes sense to me. My opinionated vote would be:
Afaict, if a path like this is approved, it isn't blocked by the PR as it stands now, could build on top of it |
I believe it always depends on the crate's maintainers, although small, specific crates seem to be a common practice. A good rule of thumb seems to be if its an extension or a layer on top. 2 -> I believe they should be separate crates, even probably also D1, R2, AI. 3 -> Tests should be specific and internal for each crate, as they should be as self contained as possible. Using a layered approach you can even unit test them without hitting the workerd, this makes it easier to simulate error conditions, for instance. 4 -> Normally its the upper layer that re-exports the lower layer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes a lot of sense to use this trait based approach, where http
is just one option we support. I think it could also split http functionality into a new crate and make it additive.
As far as the other suggestions, I'm a little worried about bikeshedding / making drastic changes here. I would rather focus on implementing missing APIs and make small modularity improvements over time.
* and a bit of cleanup to clarify previous naming conflict of WorkerResponse
FWIW, I tried your fork on one of our larger projects with success. Is there anything left to do here? |
Of the top of my head I would like to finish the swing on more generic http Body support, more generic Error type support, and possibly better errors from the macro when these traits do not line up. Are you using web_sys::Response as well or do you have a different use case? |
@kflansburg didn't quite follow, are you asking for more work to be done on this PR for "more generic [...] support"? If so, feel free to direct me to what you'd like to see done here (if it's work outside of the scope of this PR, then I suppose someone else like yourself would take it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose error types and macro errors can be landed in a separate PR, I was hoping to do it all in one release to minimize disruption. For now, I think there is one place this PR could be made slightly more generic.
worker/src/response.rs
Outdated
} | ||
|
||
#[cfg(feature = "http")] | ||
impl IntoResponse for crate::HttpResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be more generic?
impl IntoResponse for crate::HttpResponse { | |
impl IntoResponse for http::Response<http_body::Body> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the thought on impl
for the concrete types rather than the alias?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kflansburg made more generic in 370af17 (as far as crate::http::response::to_wasm()
allows: impl http_body::Body<Data = Bytes> + 'static
)
These seems to cover/conflict with the concrete axum impl too (so I removed that) :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the thought on
impl
for the concrete types rather than the alias?
I don't understand the question. http_body::Body
is a trait and will be more generic than the type alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you're right, I just read that as http::body::Body
which isn't a trait.
Opening this more as a discussion point, though the PR is fully baked afaict and ready to be merged if the idea is sound.
Motivation: it seems to me that there's currently a few conflicting ideas at play:
http
feature forces addingdep:axum
too, even though they are theoretically decoupled.The idea of the change in this PR is to instead nip the problem at its core, there only needs to be a single WorkerRequest and WorkerResponse traits, which go from and to web_sys respectively.
Out of the box, this trait is implemented for:
http
feature is enabledI've also added a proof of concept in the
examples
directory showing that this also allows downstream users to use any Request/Response type they want, just need to impl the traits.If approved and merged, I don't think anything would break, and this paves the way for solidifying what the http API looks like... for that, I'd suggest next steps:
http
feature - instead call it what it really seems to be, theaxum
feature, and doing this should enable axum to work as smoothly as possible out of the box