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-websocket-handler

v1.0.1

Published

This module is created to handle AWS Lambda websocket actions as a one default handler

Downloads

12

Readme

aws-websocket-handler

TypeScript

This module is created to handle AWS Lambda websocket actions as a one default handler. There are multiple benefits of doing so:

  • all actions are handled by one lambda function;
  • no cold start for actions, which are rarely used;
  • the codebase is sharable accross all actions;

How it works?

When you want to use WebSocket API in API Gateway, which are integrated with Lambda functions it is required to specify two mandatory routes $connect and $disconnect. The approach is presented in detail here. Instead of specifing all actions as separate functions we use here a power of $default route, which is invoked every time no matching expresion is found. This package use action parameter from payload.

How to use?

  1. Install the package:
npm i aws-websocket-handler
  1. Use in your lambda handler:
// Typescript
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { Configuration, websocketHandler } from 'aws-websocket-handler';
import { validateToken } from './middlewares/validateToken';
import { simpleAction } from './actions/simple';

const config: Configuration = {
    actions: [
        {
            name: 'simple',
            handler: simpleAction,
        },
    ],
    middlewares: [validateToken],
};

exports.handler = async (
    event: APIGatewayProxyEvent,
    context: Context
): Promise<AWSLambda.APIGatewayProxyResult | AWSLambda.APIGatewayProxyStructuredResultV2> => {
    return websocketHandler(event, context, config);
};

// Javascript
const { websocketHandler } = require("aws-websocket-handler");
const { validateToken } = require("./middlewares/validateToken");
const { simpleAction } = require("./actions/simple");
const config = {
    actions: [
        {
            name: 'simple',
            handler: simpleAction,
        }
    ],
    middlewares: [validateToken],
};
exports.handler = async (event, context) => {
    return websocketHandler(event, context, config);
};

Configuration:

| Param | Description | Required | ------------- | ------------- | ------------- | | actions | Array of ActionsHandlers | optional | middlewares | Array of Middlewares | optional | fallback | Fallback promise to be invoked if no action expression is found | optional | enableLogging | Boolean param to enable simple logging | optional

ActionsHandler example

Name corresponds to Event.body.action, if the expression matches, the handler function is invoked.

const simpleAction = async (event, context, runData) => {
    console.log(runData);
    return {};
};
Middleware example
const validateToken = async (event, _context, runData) => {
    const { token } = JSON.parse(event.body);
    if (!token)
        throw new Error('No token');
    let tokenData;
    try {
        tokenData = JWT.verify(token, config_1.KEY);
    }
    catch (e) {
        throw new Error(e.message);
    }
    return { ...runData, tokenData };
};
Fallback
const fallback = async () => {
  return { statusCode: 200, body: 'fallback' }
},