Skip to content

Helper method for headers and status code #9

Description

@astuyve

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions