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

How to stream files #93

Closed
maciej-jezierski opened this issue Apr 11, 2022 · 8 comments
Closed

How to stream files #93

maciej-jezierski opened this issue Apr 11, 2022 · 8 comments

Comments

@maciej-jezierski
Copy link

Hi, I am trying to stream files from local disc (for a reason it's not served as static).

I tried to use sendStream or send it directly to pipe but each time I get 404. Documentation is not mentioning too much how to use streams. How to use it correctly with h3?

export default defineEventHandler((event) -> 
     const stream = fs.createReadStream(path);
     stream.pipe(event.res);
)

or

const stream = fs.createReadStream(path);
      sendStream(event, stream);
@zelid
Copy link

zelid commented Apr 12, 2022

I have the same issue with Nuxt 3 and asked for help at https://github.com/nuxt/framework/discussions/4299

Found this thread created 22 hours ago while going to ask the same on h3 repo issues.

@maciej-jezierski
Copy link
Author

I found solution by using playground and checking code. Solution is super simple, sendStream has to be returned directly

const r = "/path/to/file
return sendStream(event, fs.createReadStream(r))

In my case if you have promise first it's working as well

return $fetch(apiUrl, { method: "GET", parseResponse: JSON.parse }).
    .then(r => sendStream(event, fs.createReadStream(r.path)))

KISS, but generally would be good to add it to documentation before some other people run into this issue as streaming files is common exercise.

@atinux
Copy link
Member

atinux commented Apr 16, 2022

Would love to see a PR to https://v3.nuxtjs.org/guide/features/server-routes#handling-requests-with-body to add an example about it 💚

@maciej-jezierski
Copy link
Author

@atinux I've even prepared code for h3 Readme.md and Nuxt repo as well with samples, but apparently I have some access problem. Will try to push it later.

@maciej-jezierski
Copy link
Author

Nuxt PR: nuxt/framework#4401

@pi0
Copy link
Member

pi0 commented Apr 17, 2022

@atinux We should probably start adding examples directly to h3 docs. Otherwise, we will end up with fragmented documentation and outdated info for utils. Also, consider sendStream support is not universal yet.

@fabianwohlfart
Copy link

How do I send a piped file?
sendStream hangs up, and send throws typeError

import PDFDocument from 'pdfkit'
import { sendStream } from 'h3'

export default defineEventHandler(async (event) => {
  const doc = new PDFDocument()
  doc.pipe(event.res)
  doc.text('Test')
  doc.end()
  return sendStream(event, event.res)
})

@manuel-84
Copy link

how to stream text from a variable and not in a file?

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

No branches or pull requests

6 participants