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

@cloudomium/middleware

v0.2.0

Published

a set of tools for easier serverless development on AWS

Downloads

221

Readme

Cloudomium Middleware

This project is a set of tools for easier serverless development on AWS Lambda.

Middleware Engine for AWS Lambda

The middleware engine CloudomiumLambda provides methods for adding set of functions in front of the handler, as well as the afterwards.

Before / After The Lambda Handler

There are methods called before and after in CloudomiumLambda. These method adds middleware functions to be called right before or right after executing the handler function. Usually, the middlewares which added to be called before the handler are for processing the request. On the other hand, the middlewares which added to be called after the handler are for processing the response.

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | middleware | function | true | a middleware function that implements Middleware interface | | onError | function | false | a function that implements ErrorHandler interface |

To define the handler function, there is a method called execute

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | handler | function | true | a handler function that implements Handler interface | | onError | function | false | a function that implements ErrorHandler interface |

const handler = new CloudomiumLambda()
    .before(await middlewareA())
    .after(await middlewareB())
    .execute(async (event, context) => {
        return { statusCode: 204 }
    })

Built-in Middlewares

There are various built-in middlewares. Also, you can easily build your own middlewares by creating a function that implements Middleware interface.

Authentication

This middleware adds an authentication and authorization layer in front of the Lambda handler function. While the jwt token is for authentication part, check function is for authorization.

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | jwt | object | true | jsonwebtoken instance | | jwtOptions | object | false | official options for jsonwebtoken instance | | secret | secret | true | the secret value for signing and verifying tokens | | header | string | false | header key for tokens, default value is authorization | | mustSignIn | boolean | false | flag to determine authentication requirement | | check | function | false | function for validating claims inside the token |

import jwt from 'jsonwebtoken'
import { CloudomiumLambda } from '@cloudomium/middleware'

const handler = new CloudomiumLambda()
    .before(await authMiddleware({ jwt, secret: 'my secret', mustSignedIn: true }), (error, type) => console.error(type, error))
    .execute(async (event, context) => {
        return { statusCode: 204 }
    })

CORS

This middleware adds a simple CORS support to the response. It's better (also cheaper) to solve CORS in front of Lambda, instead of invoking one

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | origins | string[] | false | allowed origins. default value is "*" | | headers | string[] | false | allowed headers | | methods | string[] | false | allowed methods | | credentials | boolean | false | flag to allow credentials |

import { CloudomiumLambda } from '@cloudomium/middleware'

const handler = new CloudomiumLambda()
    .before(await corsMiddleware({ origins: ['http://localhost:9000'] }), (error, type) => console.error(type, error))
    .execute(async (event, context) => {
        return { statusCode: 204 }
    })

JSON Body Parser

This middleware adds a support for JSON payload.

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | base64 | boolean | false | flag for base64 encoding | | compressed | boolean | false | flag for gzip compression |

import { CloudomiumLambda } from '@cloudomium/middleware'

const handler = new CloudomiumLambda()
    .before(await jsonMiddleware(), (error, type) => console.error(type, error))
    .execute(async (event, context) => {
        return { statusCode: 204 }
    })

HTTP Validator

This middleware adds a validation support for input and / or output data

| Parameter | Type | Required | Description | | --------- | ---- | -------- | ----------- | | body | object | false | ZodError model for body | | queryString | object | false | ZodError model for queryString | | pathParameters | object | false | ZodError model for pathParameters | | headers | object | false | ZodError model for headers | | onError | function | false | Error handling function |

import { CloudomiumLambda } from '@cloudomium/middleware'

const handler = new CloudomiumLambda()
    .before(await validate({ body: z.object({ name: z.string() }) }), (error, type) => console.error(type, error))
    .execute(async (event, context) => {
        return { statusCode: 204 }
    })