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

@supercharge/hapi-aws-lambda

v2.0.0

Published

Run your hapi server in an AWS Lambda function

Downloads

8

Readme


Introduction

Serverless is becoming popular and widely accepted in the developer community. Going serverless requires a mindset shift. Going serverless requires you to think stateless.

This @supercharge/hapi-aws-lambda package let’s you use your hapi.js HTTP server on AWS Lambda.

This package wraps your hapi server and transforms an incoming event from Lambda and API Gateway into a request. The request will be injected into your hapi server and the resulting response transformed into an API-Gateway-compatible format.

It’s basically a “done for you” package to run your hapi server in a serverless function AWS Lambda.

Requirements

hapi v19 (or later) and Node.js v12 (or newer)

This plugin requires hapi v19 (or later) and Node.js v12 or newer.

Compatibility

| Major Release | hapi.js version | Node.js version | | --- | --- | --- | | v2 | >=19 @hapi/hapi | >=12 | | v1 | >=18 hapi | >=8 |

Installation

npm i @supercharge/hapi-aws-lambda

Usage

Using @supercharge/hapi-aws-lambda is a two-step process:

  1. Implement the function’s entrypoint
  2. Configure Binary Media Types for your API Gateway

The Lambda Function

Using @supercharge/hapi-aws-lambda is pretty straightforward:

'use strict'

const Hapi = require('@hapi/hapi')
const LambdaHandler = require('@supercharge/hapi-aws-lambda')

// this `handler` will be used as a cached instance
// a warm Lambda function will reuse the handler for incoming events
let handler

module.exports.handler = async event => {
  if (!handler) {
     // First, compose your hapi server with all the plugins and dependencies
    server = new Hapi.Server()

    await server.register({
      plugin: require('@hapi/vision')
    })

    // Second, create a handler instance for your server which will
    // transform the Lambda/API Gateway event to a request, send
    // the request through your hapi server and then create
    // an API Gateay compatible response
    handler = LambdaHandler.for(server)
  }

  return handler.proxy(event)
}

Configure Binary Media Types

Serving images from an HTTP server running in a Lambda function won’t work out of the box. When neccessary, @supercharge/hapi-aws-lambda Base64-encodes the response data so that AWS API Gateway can handle the response body.

You need to explicitely configure binary media types in your the API Gateway that is responsible for your Lambda function. Here’s a screenshot of the */* configuration we use:

AWS API Gateway: Binary Media Types configuration

Deployment Example

There’s a deployment example in the superchargejs/playground-aws-lambda repository.

We used the Serverless framework to deploy the Supercharge app in the playground-aws-lambda repository. The Serverless CLI is sweet. Here’s the sample serverless.yml used to deploy the app:

service: supercharge-aws-lambda

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-central-1

functions:
  app:
    handler: server.handler
    memorySize: 384 # default is 1024 MB
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

plugins:
  - serverless-offline

Contributing

Do you miss a string function? We very much appreciate your contribution! Please send in a pull request 😊

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 🚀

License

MIT © Supercharge


superchargejs.com  ·  GitHub @superchargejs  ·  Twitter @superchargejs