question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending streams as response

See original GitHub issue

Hi there,

Is it possible to send streams somehow? Roughly, I’m trying to do this:

@Get("/*")
public async processRequest(@Req() request, @Res() response) {
        ...
        const readableStream = await myStorage.getBlobAsStream();
        readableStream.pipe(response);
       ...
}

…and receive the error like this:

Application has thrown an uncaught exception and is terminated:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at write_ (_http_outgoing.js:572:17)
    at ServerResponse.write (_http_outgoing.js:567:10)
    at RetriableReadableStream.ondata (_stream_readable.js:666:20)
    at RetriableReadableStream.emit (events.js:182:13)
    at RetriableReadableStream.Readable.read (_stream_readable.js:486:10)
    at flow (_stream_readable.js:922:34)
    at resume_ (_stream_readable.js:904:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Could you please advise?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
wsmdcommented, Feb 17, 2021

Hey all - routing-controllers does support returning streams directly from a controller method (via https://github.com/typestack/routing-controllers/pull/285), but I’ve found it a bit tricky at first when it comes to piping streams (i.e. readable.pipe(response)).

I’ve managed to get piping streams to work by using either of the two following methods:

1. Piping the readable to the response and returning a Promise

@Get('/pipe_with_promise')
promise(@Res() res: Response) {
  return new Promise<Response>((resolve, reject) => {
    const readable = getReadableStreamSomehow();
    readable.pipe(res);
    readable.on('end', () => resolve(res));
    readable.on('error', (error) => reject(error));
  });
}

2. Piping the readable to a PassThrough stream and returning it

import { PassThrough } from 'stream';

// ...

@Get('/pipe_with_passthrough')
passthrough() {
  const stream = new PassThrough();
  getReadableStreamSomehow().pipe(stream);
  return stream;
}

Note that with the Passthrough approach, I’ve encountered some issues with the content-type not being inferred automatically, so you may need to use @ContentType as well, depending on your use case.

0reactions
github-actions[bot]commented, Mar 20, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to stream to an HTTP response - Mario Kandut
Send a stream back to the client as a response to an HTTP request · 1. Initialize project and install dependencies · 2....
Read more >
How do I stream response in express? - Stack Overflow
I've been trying to get a express app to send the response as stream. var Readable = require('stream'). Readable; var rs = Readable();...
Read more >
Using readable streams - Web APIs | MDN
In our Simple stream pump example, we consume the custom readable stream by passing it into a Response constructor call, after which we...
Read more >
Stream to an HTTP Response with Node.js - YouTube
In this tutorial we will use streams to efficiently send a large ... a large file into memory before sending it back as...
Read more >
Implement HTTP Streaming with Node.js and Fetch API
Your API should use HTTP streaming to send its response. Your webapp should use the Fetch API to make the request so that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found