npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vdtn359/lambda-stream

v0.5.0

Published

awslambda.streamifyResponse, but locally and with typescript

Downloads

33

Readme

Lambda-Stream

This library adds types and local support for AWS Lambda Response Streaming. Locally, it'll buffer and return the entire response.

In AWS Lambda, it'll simply use the global awslambda.streamifyResponse.

This library exposes a ResponseStream class, the streamifyResponse method, and isInAWS method. Types are also included.

Why?

AWS added Lambda Response Streaming directly into the NodeJS runtime as a global method, instead of providing it with the AWS-SDK.

This makes functions leveraging Lambda Response Streaming difficult to test, run locally, or even write Lambda functions using a typical editor. Magic, runtime-only methods are a drag on developer experience!

So I wrote this library to fill that gap

End of Life

This library shouldn't exist.

If or when AWS decides to release and support a library which makes AWS Lambda Streaming Responses usable and testable locally, I'll happily archive this project.

Examples

Works like this:

import { APIGatewayProxyEventV2 } from 'aws-lambda'
import { streamifyResponse, ResponseStream } from 'lambda-stream'

export const handler = streamifyResponse(myHandler)

async function myHandler(
  event: APIGatewayProxyEventV2,
  responseStream: ResponseStream
): Promise<void> {
  console.log('Handler got event:', event)
  return new Promise((resolve, reject) => {
    responseStream.setContentType('text/plain')
    responseStream.write('Hello')
    setTimeout(() => {
        responseStream.write('World')
        responseStream.end()
        resolve()
    }, 1000)
  })
}

Or in commonjs:

'use strict'
const { streamifyResponse } = require('lambda-stream')

module.exports.hello = streamifyResponse(
  async (event, responseStream, context) => {
    responseStream.setContentType('text/plain')
    responseStream.write('Hello, world!')
    responseStream.end()
  }
)

Pipelining is also supported:

const pipeline = require('util').promisify(require('stream').pipeline)
const { Readable } = require('stream')
const { streamifyResponse } = require('lambda-stream')

const handler = async (event, responseStream, _context) => {
  // As an example, convert event to a readable stream.
  requestStream = Readable.from(Buffer.from(JSON.stringify({ hello: 'world' })))

  await pipeline(requestStream, zlib.createGzip(), responseStream)
}

module.exports.gzip = streamifyResponse(handler)

Headers, Status Code

AWS provides a helper function which allows you to specify additional headers, as well as craft the status code.

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();
  }
);

We are considering shimming our own helper to support types as well.

Contributors

AJ Stuyvenberg

Luka Matic

Paulo Ricardo Xavier Giusti