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

aws-mock-lambda-api-gateway

v5.0.1

Published

AWS Mock Lambda API Gateway

Downloads

12

Readme

AWS Mock Lambda API Gateway

It can often be difficult to run your AWS Lambda Functions locally.

The purpose of this module is to wire a collection of Lambda Functions up to a local http server in a similar, but not identical, way to the API Gateway on AWS.

The purpose of this module is not to reach feature parity with the API Gateway. It is to simply allow you to make HTTP requests against your Lambda functions in a sane way. It does not spin up child processes etc, but runs the Lambda Function on the same event loop as the mock API server.

This is for development use only, do not run in production

API

module.exports.init = function init(opts, cb)

This API Exposes a single function which bootstraps the server. From that point froward, the state of the server should be considered immutable. To put it another way, this module does not support dynamically adding or removing routes.

cb is an error first function

function cb(e, server)

Where server has a single function:

end(cb)

End shuts down the listening server and calls the supplied callback with an error if any

The opts object is required.

opts : {
  routes: [], // Defined below
  listen: []  // Defined below
}

The listen key can be either a single value or an array. It represents the arguments that http.createServer will be called with.

The routes array contains a collection of objects that map an HTTP route to a lambda function. We do not enforce that the values be valid in regards to the API Gateway specification, we simply check http.IncomingMessage for matching values. They should be in the form:

{
  method: 'POST',                       // I.E. 'GET', 'POST', 'PUT', etc (required)
  route: '/metrics/value',              // Only exact matches get routed (required)
  lambda: function (event, context) {}, // Your lambda function (required)
  responses: []                         // Defined below (optional)
}

We do not support timeouts, if your lambda function stalls, the request will hang.

responses is an array of objects that maps the value passed out of your lambda function back to an HTTP method response. This is a combination of the behaviours of Method Response and Integration Response from the API Gateway. 200 is always the default when none of the provided responses map to your Lambda's returned value. Not including this will result in all responses being 200.

The objects of the responses array take the following form:

{
  regex: {}, // A RegExp object or duck type of RegExp
  status: xxx, // HTTP Status Code that will be returned. Will be coersed to an integer.
}

The first matching regex in the array will be used. If you want to change the default of 200, provide a "catch all" regular expression as the last element of the array.

The object passed back in the response will be in the form of:

{
  "errorMessage" : [MESSAGE]
}

Note that double quotes will automatically be escaped in [MESSAGE]

context object

The context object currently has the following keys:

context.succeed(obj): This will return a 200 statusCode context.fail(obj): This will be handled by your responses array context.done(e, obj): Combines behaviour of fail and succeed via e

event object

The event object is an unaltered copy of the contents of the http.incomingMessage 's body passed through JSON.parse. This library does not currently offer a way to modify this object.

If JSON.parse fails, the server will return a 500 statusCode.

Example

var lambdaFunc = require('./index.js')

require('aws-mock-lambda-api-gateway').init({
    routes: [
      {
        method: 'POST',
        route: '/metrics'
        lambda: lambdaFunc
      }
    ],
    listen: [8080,'0.0.0.0']
  }, function (e) {
    if(e) throw e
    console.log('Server Listening...')
})

Development

The officially supported method for developing for this module is to use the included Makefile.

Running make will give you a full list of commands available for development.

make test is the most important. It runs the unit tests everytime the local filesystem changes allowing you to do test driven development.