Skip to content
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: web responses with streaming support #436

Merged
merged 2 commits into from
Jul 10, 2023
Merged

feat: web responses with streaming support #436

merged 2 commits into from
Jul 10, 2023

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jul 10, 2023

πŸ”— Linked issue

#73, #401

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR adds support for directly returning a web Response object from event handlers, leveraging new streaming support for body! (#432)

It is also possible to explicitly use the newly exposed sendWebResponse utility.

Note: The rest is pretty much simple and implementation is already used for event.respondWith (by @danielroe ❀️). I am thinking in next steps, reuse same utility for respondWith as well and deprecating pollyfills we initially made due to runtime incompatibilities. (#119)

Also thanks to @Hebilicious for initial efforts in #395 to push for supporting native Responses ❀️

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Jul 10, 2023

Codecov Report

Merging #436 (ec5ab9f) into main (5c4dcd1) will decrease coverage by 1.74%.
The diff coverage is 78.57%.

❗ Current head ec5ab9f differs from pull request most recent head ea0daa0. Consider uploading reports for the commit ea0daa0 to get more accurate results

@@            Coverage Diff             @@
##             main     #436      +/-   ##
==========================================
- Coverage   77.26%   75.52%   -1.74%     
==========================================
  Files          26       26              
  Lines        2617     2644      +27     
  Branches      376      381       +5     
==========================================
- Hits         2022     1997      -25     
- Misses        595      647      +52     
Impacted Files Coverage Ξ”
src/utils/response.ts 69.04% <72.72%> (+0.29%) ⬆️
src/app.ts 97.54% <100.00%> (+0.06%) ⬆️

... and 3 files with indirect coverage changes

Copy link
Member

@Hebilicious Hebilicious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this ❀️
I have a few questions :

What happens with the response 2nd argument ?

setResponseStatus(event, 208, "hello-status");
return new Response("valid", {
  status: 207,
  statusText: "bye-status" })

Does it works with Transform Streams ?

const response = new Response(`Hello world !`);
const { readable, writable } = new TransformStream();
response.body?.pipeTo(writable);
return new Response(readable, response);

It would be useful to have tests for theses.

@pi0
Copy link
Member Author

pi0 commented Jul 10, 2023

What happens with the response 2nd argument ?

Status code from Response is expected to be picked as it is last operation. Normally we should set it in one place only :)

Does it works with Transform Streams?

Readable streams should be normally chainable. I think we can improve tests for sendStream utility instead as this feature reuses sendStream. PR Welcome to improve tests πŸ’―

@pi0 pi0 merged commit a5c8c9f into main Jul 10, 2023
4 checks passed
@pi0 pi0 deleted the feat/web-response branch July 10, 2023 16:12
@pi0 pi0 mentioned this pull request Aug 4, 2023
"/",
eventHandler(
() =>
new Response("Hello World!", {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document this feature demo in readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants