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

somod-websocket-extension

v1.0.0

Published

SOMOD middleware to intercept and validate Lambda event for AWS Websocket APIGateway

Downloads

13

Readme

SOMOD WebSocket Extension


The SOMOD Extension Configure WebSocket routes and validate incoming WebSocket Messages in Serverless Functions.

The middlware in this extension works with Functions of type WebSocket Only.

Install

Install as an npm package in somod modules

npm install somod-websocket-extension

Usage

Attach the middleware to the Serverless Function

Resources:
  MyWebSocketMessageHandler:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri:
        SOMOD::Function:
          # ...
          middlewares:
            - module: somod-websocket-extension
              resource: SomodWebSocketMiddleware
      # ...

Refer to SOMOD's Middleware reference for more information

Configure Routes and Schemas

Routes configuration for each serverless function can be provided using a yaml file at serverless/functions/<function_name>.websocket.yaml.

Example:

# serverless/functions/chat.websocket.yaml

$default:
  body:
    parser: json
    schema:
      type: object
      required: [name]
      properties:
        name:
          type: string
          maxLength: 32
        email:
          type: string
          format: email

Access Sanitized Message

The sanitized Message can be accessed using the middleware's context using the somod-websocket-message key.

Example:

// serverless/functions/chat.ts

const ChatHandler = event => {
  const message = event.somodMiddlewareContext.get("somod-websocket-message");
  // use message to read the data from the incoming websocket message
};

export default ChatHandler;

This module also provides a utility library to create Serverless Functions with multiple routes easily. Refer to RouteBuilder for more details

Specification

Structure of Routes configuration file

In general, routes configuration in <function_name>.websocket.yaml file follows the below structure

<routeKey>:
  body:
    parser?: <"text"|"json"|"formdata">. If not provided, automatically choosen based on the Content-Type Header (text is considered if automatic detection fails)
    schema: <json schema>

The complete JSONSchema is available here

Type of the Message object

The message object accessed using event.somodMiddlewareContext.get("somod-websocket-message") has this type.

type Message<T = unknown> = {
  routeKey: string;
  body: T;
};

The Message Type is available from this module to use (import as shown below)

import { Message } from "somod-websocket-extension";

Error Handling

The middleware immediates ends the Lambda execution and returns appropriate message when Validation fails

RouteBuilder

The RouteBuilder is a wrapper javascript utility library to create serverless functions with multiple routes.

Using the RouteBuilder

// serverless/function/user.ts
import { RouteBuilder } from "somod-websocket-extension";

const builder = new RouteBuilder();

builder.add("$default", defaultMessageHandler);

export default builder.getHandler();

RouteBuilder Specification

RouteBuilder has 2 methods

  • add

    function add(
      routeKey: string,
      handler: (message: Message, event: RawEvent) => Promise<Response>
    ): void {
      //
    }

    The handler receives the sanitized message object and the raw event from AWS. The handler has to return a promise which resolves to the Response object.

    The Raw Event type and Response type is documented here in the AWS specification.

  • getHandler

    function getHandler(): (event: RawEvent) => Promise<Response> {
      //
    }

    handle function returns the function which is a lambda function handler

Issues

The project issues, features, and milestones are maintained in this GitHub repo.

Create issues or feature requests at https://github.com/somod-dev/somod-websocket-extension/issues

Contributions

Please read our CONTRIBUTING guide before contributing to this project.