fastify / aws-lambda-fastify

Insipired by aws-serverless-express to work with Fastify with inject functionality.
MIT License
501 stars 32 forks source link

Support for response streaming #154

Open serg06 opened 1 year ago

serg06 commented 1 year ago

Prerequisites

πŸš€ Feature Proposal

AWS just released the ability to stream a Lambda function's response: https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/

It would be great if this framework supported it.

It requires wrapping the handler in streamifyResponse().

Motivation

This is an extremely useful feature which greatly decreases TTFB.

Example

// API which returns chunked data.
// Locally TTFB is 0, but on Lambda TTFB is 5s.
// Presumably because the handler is not wrapped in streamifyResponse.
fastify_streamtest.get('/chunked', (request, reply) => {
  // Create a buffer to hold the response chunks
  var buffer = new stream.Readable();
  buffer._read = () => {};

  // Generate 5 chunks with 1 second interval
  var count = 5;
  var emit = () => {
    var data = `Hello World ${count}`;
    console.log(`sending "${data}"`);
    buffer.push(data);

    count--;
    if (count > 0) {
      setTimeout(emit, 1000);
    } else {
      console.log('end sending.');
      buffer.push(null);
    }
  };

  emit();
  void reply.type('text/html').send(buffer);
});
adrai commented 1 year ago

image

serg06 commented 1 year ago

It should still work with direct Lambda URLs, no?

IMG_2489

adrai commented 1 year ago

Might work with Lambda function URLs, but...

beside that I don't know if the fastify inject function is able to handle chunked responses in a way to pass them to the awslambda.streamifyResponse πŸ€·β€β™‚οΈ Maybe @mcollina can confirm?

mcollina commented 1 year ago

Given the current limitations I don't see this as such a big deal. It will definitely help Vercel.

I think it's possible to make a few modifications to inject to support this. However I don't have time to work on them right now.

adrai commented 1 year ago

thank you for the info

Deanfei commented 1 month ago

Does this library support the lambda streaming response yet? That's the only main blocker to use it.

adrai commented 1 month ago

The Fastify inject function (and maybe also light-my-request), needs to be modified first.

Deanfei commented 1 month ago

Any native support for the streaming now?