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 🙏

© 2025 – Pkg Stats / Ryan Hefner

cloudfront-express

v1.0.5

Published

A simple library for Lambda@Edge that allows conversion between CloudFront events and Express req/res objects.

Downloads

5

Readme

cloudfront-express

A simple (~100 LOC) library for Lambda@Edge that allows conversion between CloudFront events, Express req, and generic response objects.

It's on npm as @hbkapps/cloudfront-express.

Why would I want do to this?

Our use case for Lambda@Edge is to make middleware that allows a SPA hosted on S3 to do a couple different things that need to be done server-side (like redirects and some dynamic CSS generation). But that makes it hard to test these functions or to run the SPA locally with these in place.

With this library, I can write Lambda@Edge functions right into Webpack dev server's middleware. I'll have more examples of doing that soon.

Plus Cloudfront's event objects are unreasonably obtuse and confusing. By flattening them into something Expressy, they become much easier to work with. On the other side, we can use a common generic response object to send down the appropriate res, since Express' res object is not a static object.

For example, in our modules, we have a simple populateResponse() function, and two handlers that simply call that function, one for express and one for Lambda. This allows the greatest possible abstraction and the ability to work in both environments:

function populateResponse(req, cb){
	let response = {};

	response.status = 200;

	response.headers = {
		'Cache-Control': 'no-cache',
		'Content-Type': 'text/css',
		'Content-Encoding': 'UTF-8'
	};

	doStuffToGenerateBody(req, function(err, body){
		if(err){
			console.log(err);
		}

		response.body = body;

		cb(response);
	});
}

// lambda
module.exports.handler = (event, context, callback) => {
	var req = cloudfrontExpress.fromCf(event);

	cloudfrontExpress.toCf(populateResponse(req));
};

// express
module.exports.middleware = (req, res, next) => {
	cloudfrontExpress.sendExpressResponse(populateResponse(req), req, res, next);
};

Because all the logic happens on a generic response object, it becomes very easy to translate between the two execution environments (Lambda and Express/Webpack).

What is a generic response object?

A generic response object looks like this. Not terribly complicated. ;-)

{
    headers: {
        'Blah': 'blah' //required, or blank
    },
    status: 200, // required
    body: "blah" // optional
}

API

There are three exported functions. They are described in much greater detail in the JSDoc comments, but from a high-level, it's just:

fromCf(cfEvent)

This accepts a Cloudfront viewer request event and transforms it into an Express-like req object. Because Lambda@Edge is so limited, the only properties on the returned Express-like req object will be:

  • headers
  • cookies
  • method
  • url

That's still more than enough to do what can be done with Lambda@Edge

toCf(response)

This does this opposite: it accepts a generic response object and transforms it into a Cloudfront viewer response object. You can then pass this into your Lambda@Edge callback to return a response!

sendExpressResponse(response, req, res, next)

This sends down an Express response given a generic response object. The headers, body, and status will all be appopriately set based on that properties of that object.

Author

Brett Neese [email protected]