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

serverless-wrapper-plugin

v1.2.0

Published

Serverless Wrapper Plugin - Wrap all your functions from one place

Downloads

3

Readme

Serverless Wrapper Plugin

serverless

This goal of this plugin is to provide an easy way to wrap your serverless functions with a wrapper function, without having to edit all the functions themselves.

One use case for this is for example, if you want to override console.warn to prepend a custom string, to make these warning easier to trace in the logs. You could add this to the begining of every handler in your project, or you could write one wrapper function to perform this override and use the serverless-wrapper-plugin to automatically wrap all your handlers.

Another use case might be, you want to write a process to periodically make requests to your functions to keep them hot. You hadn't planned for this when you wrote all your handlers, so what input do you send to avoid causing either errors or unwanted side-effects? With the serverless-wrapper-plugin you could write a wrapper to intercept the input event, and if it is the dummy "wake up" event, then ignore it and return. If it isn't the dummy event then simply pass it through to the handler as normal.

UPDATE 1.1.0

  • Support function specific wrapper function, using wrapper.path option, set using relative wrapper path to the root of the project in the s-function.json file.
  • Support skipping wrapper if function-specific wrapper.path is set to false

Setup

Not compatible with Serverless 1.0.0 and above Support only Node Lambdas

NOTE: If you are using the serverless-webpack-plugin, this plugin must be before the webpack plugin in the list.

Wrapper Function

Firstly you need to write the wrapper function that you want to apply. This wrapper function should have the following form:

function myWrapper(handler, event, context) {
    // Do whatever you like here..
    // ...

    // Call the original handler
    return handler(event, context);
}

module.exports = myWrapper;

The way that this is used is that the handler is transformed to be something like this:

const _handler = require('...path to original handler...');
const _wrapper = require('...path to myWrapper...');

module.exports.handler = function (event, context) {
    return _wrapper(_handler, event, context);
}

Plugin Installation

  • Install the plugin in the root of your Serverless Project:
npm install serverless-wrapper-plugin --save-dev
  • Add the plugin to the plugins array in your Serverless Project's s-project.json, as below.

NOTE: If you are using the serverless-webpack-plugin, this plugin must be before the webpack plugin in the list.

"plugins": [
    "serverless-wrapper-plugin"
]
  • In the custom property of either your s-project.json or s-function.json add a default wrapper property. The path is relative to the project root. This is a fallback if your s-function does not contain a wrapper.path property.
{
    ...
    "custom": {
        "wrapper": {
            "path": "path/relative/to/project-root"
        }
    }
    ...
}
  • To set a custom wrapper for specific function, add the wrapper function's relative path to s-function.json. If you do not want your function to be wrapped by any wrapper function, event the default one, set the wrapper propertry to false
  ...
  "name": "your-function",
  "runtime": "nodejs4.3",
  "custom": {
    "wrapper" {
      "path": "wrapper.js",
    }
  }
  "handler": "handler.handler",
  ...
  ...
  "name": "your-function",
  "runtime": "nodejs4.3",
  "custom": {
    "wrapper": false
  }
  "handler": "handler.handler",
  ...

Development

A brief note about development of the plugin itself. Part of the function of this plugin is to generate code. To do this it uses the template engine doT.js.

There is a template in lib/wrapped-handler.jst this is pre-compiled for runtime efficiency and saved as lib/wrapped-handler.js.

To re-compiled this template, you can use the convenience script in bin/compile-template.js