-
Notifications
You must be signed in to change notification settings - Fork 507
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
Sending stream responses (experimental) #1327
Comments
I'm experiencing problems detecting when the request is closed to clean up processing. event.node.req.once("close", () => {
cleanupFunction() // <-- this is never called
}) @pi0 can you update the example of streams to handle this? I don't know if this is a bug. |
So how does one set this up for Nuxt ? |
For node.js, you could pass a cleanup function to your sendStream handler, and run it as part of the close() callback of WritableStream. function sendStream(event: H3Event, stream: ReadableStream, cleanup: any) {
// Mark to prevent h3 handling response
event._handled = true;
// Workers (unenv)
(event.node.res as unknown as { _data: BodyInit })._data = stream;
// Node.js
if (event.node.res.socket) {
stream.pipeTo(
new WritableStream({
write(chunk) {
event.node.res.write(chunk);
},
close() {
cleanup()
event.node.res.end();
},
})
);
}
} |
@Hebilicious |
Update: Web Stream Support directly landing in H3 (unjs/h3#432). We still might need to tweak some presets for full compatibility and experiment worker support. |
Can we use this yet in Nuxt or not yet? Confused. |
@dosstx h3 changes are still not released and being tested. But very soon you would be able to try it via nuxt or nitro edge channels. |
@pi0 OK thank you very much. Sorry to waste your time looking at these kinds of questions. Looking forward to the release! |
You can already use it with a few workarounds: vercel/ai#295 |
how can i intercept the streaming content to save it to db on cf worker I have tried this:
} |
streaming support should be more stable now. please report any issues if you encounter so we can improve 🙏🏼 |
Moving from (#19)
Coming with h3@1.7.0 and new
event.handled
flag it is possible to take over h3 response mechanism and send streams. For Workers, we can checkres._body
property existence. Direct fetch in unenv (src) passes the body as is.This way we can implement
sendStream
with the minimum possible changes. Once tested enough and working for common presets, I plan to add this as a built-in (nitro/h3) utility.Example usage: https://github.com/unjs/nitro-deploys/blob/main/routes/stream.ts
Working deployments:
Screen.Recording.2023-06-21.at.02.56.44.mov
The text was updated successfully, but these errors were encountered: