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

lalalambda

v1.3.0

Published

Serverless functions powered by hapijs

Downloads

11

Readme

lalalambda

Serverless functions powered by hapijs

Build Status Coverage Status

Lead Maintainer - Devin Ivy

Usage

See also the API Reference

Note

Lalalambda is intended for use with hapi v17+ and nodejs v8+. Currently only deployments to AWS Lambda are supported, but we are open to expand support with your help!

You can skip down the page if you're looking for installation instructions.

Lalalambda offers three core features integrating hapi with the Serverless framework:

  1. :mount_fuji: The ability to deploy an entire hapi server as a lambda function.

  2. :sunrise_over_mountains: The ability to deploy individual hapi routes as lambda functions.

  3. :sunrise: The ability to deploy arbitrary lambda functions triggered by cloud events, authored similarly to how you'd create a standard hapi route.

Let's take a quick look at a code example for each of these features.

Examples

Lambda-ify an entire server

server.js
'use strict';

const Hapi = require('@hapi/hapi');
const Lalalambda = require('lalalambda');

exports.deployment = async () => {

    const server = Hapi.server();

    await server.register({
        plugin: Lalalambda,
        options: {
            lambdaify: true // Creates a lambda named "server" by default
        }
    });

    server.route({
        method: 'get',
        path: '/hello/{name?}',
        handler: ({ params }) => {

            return {
                hello: params.name || 'world'
            };
        }
    });

    return server;
};

Assuming you've already followed installation, now just deploy to get a URL to your hapi server deployed as a lambda function!

npx serverless deploy

Lambda-ify a single hapi route

server.js
'use strict';

const Hapi = require('@hapi/hapi');
const Lalalambda = require('lalalambda');

exports.deployment = async () => {

    const server = Hapi.server();

    await server.register(Lalalambda);

    server.route({
        method: 'get',
        path: '/hello/{name?}',
        options: {
            // By default the route id is used to name your lambda
            id: 'hello',
            plugins: {
                lalalambda: true
            },
            handler: ({ params }) => {

                return {
                    hello: params.name || 'world'
                };
            }
        }
    });

    return server;
};

Assuming you've already followed installation, now just deploy to get a URL to your hapi route deployed as a lambda function!

npx serverless deploy

Create an arbitrary lambda function

Here we'll create a lambda that is scheduled to log the most recent earthquake on Earth each minute.

server.js
'use strict';

const Hapi = require('@hapi/hapi');
const Wreck = require('@hapi/wreck');
const Lalalambda = require('lalalambda');

exports.deployment = async () => {

    const server = Hapi.server({
        debug: {
            // These hapi server logs will show-up in your lambda's logs
            log: ['earthquake']
        }
    });

    await server.register(Lalalambda);

    // Just as simple as configuring a route!

    server.lambda({
        id: 'earthquakes',
        options: {
            events: [{
                schedule: 'rate(1 minute)'
            }],
            handler: async (event, context) => {

                const { payload } = await Wreck.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');

                const { features: [earthquake] } = JSON.parse(payload.toString());

                if (earthquake) {
                    // You have access to the server on the event's context
                    context.server.log('earthquake', earthquake.properties.title);
                }
            }
        }
    });

    return server;
};

Assuming you've already followed installation, now just deploy to start logging earthquake data! You can then view these logs in realtime from your terminal.

npx serverless deploy
npx serverless logs --tail --function earthquakes

Installation

Lalalambda is one package that doubles as 1. a hapi plugin and 2. a Serverless framework plugin. These two plugins work together to allow you to define lambda functions in hapi that can be packaged and deployed using the Serverless framework to AWS Lambda. A basic installation has just a few steps.

  1. Install the lalalambda and serverless packages from npm.

    npm install lalalambda
    npm install --save-dev serverless
  2. Setup a Serverless config in the root of your project including lalalambda as a plugin.

    # serverless.yml
    service: my-service
    
    provider:
      name: aws
      runtime: nodejs12.x
    
    plugins:
      - lalalambda

    There is also an optional configuration for declaring the path to the server file.

    # serverless.yml
    
    custom:
      lalalambda:
        serverPath: 'src/my-server.js'  # This is always relative to the serverless.yml file.
  3. Register lalalambda to your hapi server.

    If you're using the pal boilerplate then simply add lalalambda to your manifest's plugins section.

    await server.register(require('lalalambda'));
  4. Ensure server.js or server/index.js exports a function named deployment that returns your configured hapi server.

    If you're using the pal boilerplate then you can skip this step!

    Below is a very simple example of boilerplate code to configure a hapi server, and is not necessarily "production-ready." For a more complete setup, consider using the pal boilerplate, or check-out its approach as seen here.

    // server.js
    'use strict';
    
    const Hapi = require('@hapi/hapi');
    const Lalalambda = require('lalalambda');
    const AppPlugin = require('./app');
    
    // Lalalambda will look for and use exports.deployment()
    // as defined below to obtain a hapi server
    
    exports.deployment = async (start) => {
    
        const server = Hapi.server();
    
        await server.register(Lalalambda);
    
        // Assuming your application (its routes, lambdas, etc.) live in a plugin
        await server.register(AppPlugin);
    
        if (start) {
            await server.start();
            console.log(`Server started at ${server.info.uri}`);
        }
    
        return server;
    };
    
    // Start the server only when this file is
    // run directly from the CLI, i.e. "node ./server"
    
    if (!module.parent) {
        exports.deployment(true);
    }

Now you have the full expressiveness of the Serverless and hapi frameworks at your fingertips!

The rest of your setup depends on however you want to further configure Serverless and hapi. We suggest checking-out the Serverless AWS Guide, continuing by taking a peek at the Credentials section so that the Serverless CLI will have access to deploy to your AWS account. Going forward you can invoke the Serverless CLI for deployment, etc.:

npx serverless --help
# or, shorthand:
npx sls --help