-
Notifications
You must be signed in to change notification settings - Fork 218
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
feat: Request/Response support, adapter cloudflare and streams #395
Conversation
a2d6a1f
to
458103f
Compare
Codecov Report
@@ Coverage Diff @@
## main #395 +/- ##
==========================================
- Coverage 76.21% 75.72% -0.49%
==========================================
Files 26 29 +3
Lines 2438 2764 +326
Branches 339 403 +64
==========================================
+ Hits 1858 2093 +235
- Misses 580 671 +91
|
c0b8326
to
85ddc29
Compare
WoW lots of amazing stuff. Thank you so much for making a cumulative overview of your work. It is certainly the direction we want to go slowly to prefer web APIs over Node. (#73) However, this amount of changes needs to be slowly adopted. I think fetch adapter would be a really good start. I will keep in contact with you in Discord in upcoming days to have a call and plan on changes. β€οΈ In the meantime closing PR to cleanup for maintenance. Probably also adding some small changes in this direction. |
π Linked issue
fix #394
fix #401
fix #144
β Type of change
π Description
Why ?
I needed these features on one of my project, and I'm contributing them back here.
event.request
alternative APICan be used when h3 is initialised with
createEvent(undefined, undefined, request)
.This API allows you to return native Response objects.
This is more natural in environment that aren't node, such as cloudflare, lagon or deno.
Event handler Response support
Supports Response and RawResponse to override setters values.
Helpers functions are usable instead:
sendResponse
andsendResponseRaw
.This works in node too, the Response() will be converted and sent with
res.write
andres.end
.Streaming support
Through Response, streaming can be achieved with
TransformStream
Nitro
This change makes a lot of sense with Nitro because the majority of the providers are Response based.
Given that Nitro is the main consumer of h3, here's what we could do :
nitro.response()
call that doesn't use unenv/toNodeListener and always returns a Response.The following presets would now supports streaming, be simpler and more natural to use, and the nitro code would simplify as we could always return
nitro.response()
:fetch adapter and cloudflare worker adapter
A new
fetchAdapter
to help use h3 in web standard environments.For example with nitro, it can be used like this :
To illustrate the use case, a cloudflare worker adapter is also provided:
H3 can now be used easily with cloudflare workers, similarly to other frameworks like hono.
The internal API uses
createEvent(undefined, undefined, request)
instead ofcreateEvent(nodeRequest, nodeResponse)
, and provide the alternative API for the platform who aren't node base.Helper Caveats
Almost all of the helpers are supported universally, except these 3 :
writeEarlyHints
sendStream
isStream
They are too node specific and don't make sense for Request/Response platforms.
Every other helper is supported.
Additional thoughts
It looks like the
respondWith
helper could be deprecated assendResponse
replaces it.@pi0 Please let me know if you think this PR should be split, altough it was very natural to do these changes together.
I could trim it by separating out the cloudflare adapter, the new helpers, the Response support and the event.request API.
(Altough it could easily be argued that the cloudflare adapter is just here for the tests)
π Checklist
event.request