Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@h4ad/serverless-adapter

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@h4ad/serverless-adapter

This library enables you to utilize any Cloud or Event Source to respond to web and API requests using your existing Node.js application framework.

  • 2.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8K
increased by16.87%
Maintainers
1
Weekly downloads
 
Created
Source

🚀 Serverless Adapter

Install   |    Usage   |    Support   |    Architecture   |    Credits

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Run REST APIs and other web applications using your existing Node.js application framework (Express, Koa, Hapi and Fastify), on top of AWS Lambda, Amazon API Gateway and many other event sources.

This library was a refactored version of @vendia/serverless-express, I created a new way to interact and extend event sources by creating contracts to abstract the integrations between each library layer.

Why you would use this libray instead of @vendia/serverless-express?

  • Better APIs to extend library functionality.
    • You don't need me to release a new version to integrate with the new event source, you can create an adapter and just call the addAdapter method when building your handler.
  • All code can be extended, if you want to modify the current behavior you can.
    • This is important because if you find a bug, you can quickly resolve it by extending the class, and then you can submit a PR to fix the bug.
  • All code was written in Typescript.
  • We have >99% coverage.

Install

Using NPM:

npm install --save @h4ad/serverless-adapter

Using Yarn:

yarn add @h4ad/serverless-adapter

Usage

You can quickly use this library as follows:

import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
import app from './app';

export const handler = ServerlessAdapter.new(app)
  .setFramework(new ExpressFramework())
  .setHandler(new DefaultHandler())
  .setResolver(new PromiseResolver())
  .addAdapter(new AlbAdapter())
  .addAdapter(new SQSAdapter())
  .addAdapter(new SNSAdapter())
  .build();

export { handler };

Too fast? Ok, I can explain.

First, you need to create an instance of the builder with ServerlessAdapter.new(app). The variable app is the instance of your framework (Express, Fastify, Koa, etc).

So you need to specify for this library which framework you will use to handle the request with setFramework, you can only use one. If you use express in our backend, use ExpressFramework , see more in support.

Then you specify which handler you will use to interact with the serverless cloud with setHandler, currently we only have one, the DefaultHandler. In the next releases, maybe we will add support for Azure, Huawei and others, for now, you can use this default.

Then you specify with setResolver which resolver you will use to wait for the library to forward the request to your framework, and then get the response back to your cloud. I recommend you use PromiseResolver, it's the most cloud-agnostic resolver.

By now, you've already added the basic abstractions of this library, now, you can add the adapters that will add support for receiving and processing different sources of addAdapter events. In the example I added AlbAdapter, SQSAdapter and SNSAdapter. With these adapters you can connect your lambda to three different event sources and you can add more if you wish.

Finally, we call build which will assemble your handler that you can expose directly to your cloud.

Final thoughts:

  • You can set the framework only once.
  • You can set the handler only once.
  • You can set the resolver only once.
  • You can have as many adapters as you like, use and extend as you wish.

Support

We are in beta, so some adapters may not work as expected, feel free to create an issue or provide feedback on current behavior.

By design we have these contracts that define the layers of the library: Frameworks, Adapters, Resolvers and Handlers.

If you don't know what each thing means, see Architecture.

Currently, we support these frameworks:

We support these event sources:

We support these resolvers:

We support these handlers:

Architecture

The main purpose of this library is to allow the developer to add support for any cloud and as many event sources as he wants, without having to create an issue to request the feature or copy the library code because the library doesn't expose good APIs for you to extend its functionality

So I refactored @vendia/serverless-express with 4 layers of abstraction: Framework, Handler, Resolver and Adapter.

The FrameworkContract is responsible for forwarding to IncomingMessage and ServerResponse for your application instance. With this abstraction you can implement any framework you want, they just need to accept both parameters and call end in ServerResponse, so the library knows when to continue and return the response.

The HandlerContract is responsible to get the input from the serverless and then manage to call each layer of abstraction to return a response. With this abstraction, you can implement different ways to receive input from your serverless environment. They usually have the same structure, but if you need to deal with a very different cloud, you can use this abstraction to add support for that cloud.

Handler is a good choice for implementing (monsters) ways to receive input. For example, we can create an http server as its handler to test our serverless code without having to launch the framework. Because? I don't know, but you can.

The ResolverContract is responsible for waiting for the framework to handle the request and then returning the response to the cloud. Using AWS for example, you have three ways to wait for the response: returning a promise, calling the callback, and using in-context methods, each option has its own benefits, but generally the promise option will be the better because any good cloud provider will support promises.

Finally, the masterpiece of this library, the AdapterContract is responsible for handling the received event, transforming the request in a way that your application can understand and then transforming the response in a way your cloud can understand.

Well, with these four contracts, you'll be able to add support to any cloud that exists (no more excuses not to use cloud X with NodeJS).

Why you create this library?

The real reason I created this library was because I wanted to add API Gateway and SQS support at the same time to save some money. But, @vendia/serverless-express was not supported, so I created a PR, but until I finished this library, that PR was never accepted.

So I build my own library based on that library with better APIs so I never have to wait for the maintainer to accept my PR just to extend the library's functionality :)

Credits

Honestly, I just refactored all the code that the @vendia team and many other contributors wrote, thanks so much to them for existing and giving us a brilliant library that is the core of my current company.

Keywords

FAQs

Package last updated on 12 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc