astuyve / lambda-stream

Implements AWS Lambda Response Streaming in TypeScript for local development and testing
82 stars 9 forks source link

Helper method for headers and status code #9

Open astuyve opened 1 year ago

astuyve commented 1 year ago

As per the readme update, users can add headers and a status code to their streaming responses using a built-in helper method like so:

const { streamifyResponse } = require('lambda-stream')
const stream = require('stream')
module.exports.hello = streamifyResponse(
  async (event, responseStream, context) => {
    const metadata = {
      statusCode: 200,
      headers: {
        "Content-Type": "application/json",
        "CustomHeader": "outerspace"
      }
    };

    // Use global helper to pass metadata and status code
    responseStream = awslambda.HttpResponseStream.from(responseStream, metadata);

    responseStream.write("Streaming with Helper \n");
    await new Promise(r => setTimeout(r, 1000));
    responseStream.write("Hello 0 \n");
    await new Promise(r => setTimeout(r, 1000));
    responseStream.write("Hello 1 \n");
    await new Promise(r => setTimeout(r, 1000));
    responseStream.write("Hello 3 \n");
    responseStream.end();
    // Suggested in Documentation: https://docs.aws.amazon.com/lambda/latest/dg/response-streaming-tutorial.html
    // But NOT defined in node 16 or node 18
    // Only defined in node 14
    await responseStream.finished();
  }
);

Would library users support shimming this method locally? Should we consider wrapping this in Lambda as well? We could simply always return a ResponseStream with helper support. It seems to be a no-op if an empty object is passed as the metadata argument.

Thoughts?

mikeshot commented 1 year ago

Yeah, that would be great as some of us need specific headers & status codes :)

Thanks for this library, it's super helpful!

astuyve commented 1 year ago

Thanks for chiming in @mikeshot!!

silvios commented 12 months ago

yes I would love this support as well

Gr33nLight commented 3 months ago

Keeping an eye on this, are there any updates? Would be really useful @astuyve

astuyve commented 3 months ago

Hi @Gr33nLight!

Thanks for reaching out, I just had a baby so I haven't really put much thought into this API. I'd welcome any PRs though!