Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@h4ad/serverless-adapter

H4ad16kMIT4.4.0TypeScript support: included

Run REST APIs and other web applications using your existing Node.js application framework (NestJS, Express, Koa, Hapi, Fastify and many others), on top of AWS, Azure, Digital Ocean and many other clouds.

aws, serverless, api gateway, sqs, sns, lambda edge, alb, lambda, lambda streaming, response streaming, apollo server, express, koa, hapi, fastify, node.js, http, huawei, functiongraph, trpc, azure, azure functions, http trigger v4, firebase, firebase functions, firebase http events, deepkit, deepkit http, digital ocean, digital ocean functions, digital ocean serverless, gcp, google cloud functions, polka

readme

🚀 Serverless Adapter

Install   |    Usage   |    Support   |    Examples   |    Benchmark   |    Architecture   |    Credits

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly

Run REST APIs and other web applications using your existing Node.js application framework (NestJS, Deepkit, Express (v4 and v5), Koa, Hapi, Fastify, tRPC and Apollo Server), on top of AWS Lambda, Azure, Digital Ocean and many other clouds.

This library was a refactored version of @vendia/serverless-express, I create 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.
  • Well documented, any method, class, or interface has comments to explain the behavior.
  • We have >99% coverage.

Installing

To be able to use, first install the library:

npm i --save @h4ad/serverless-adapter

Usage

To start to use, first you need to know what you need to import, let's start showing the ServerlessAdapter.

import { ServerlessAdapter } from '@h4ad/serverless-adapter';

We need to pass to Serverless Adapter the instance of your api, let's look an example with:

import { ServerlessAdapter } from '@h4ad/serverless-adapter';
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 { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/adapters/aws';

const express = require('express');

const app = express();
export const handler = ServerlessAdapter.new(app)
  .setFramework(new ExpressFramework())
  .setHandler(new DefaultHandler())
  .setResolver(new PromiseResolver())
  .addAdapter(new ApiGatewayV2Adapter())
  // if you need more adapters
  // just append more `addAdapter` calls
  .build();

Documentation

See how to use this library here.

Breaking Changes

I will not consider updating/breaking compatibility of a NodeJS framework as a breaking change, because I had a lot of supported frameworks and if I created a major version for each one it would be a mess.

So if you want predictability, pin the version with ~ instead of ^.

Examples

You can see some examples of how to use this library here.

Benchmark

See the speed comparison between other libraries that have the same purpose in the Benchmark Section.

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.

Sponsors

changelog

Changelog

4.4.0 (2024-12-01)

Features

  • express-v5: added support to express v5 and body-parser v2 (e435b97)
  • fastify-v5: ensure support to fastify v5 (bd09e46)
  • firebase: ensure support to v5 and v6 sdk (bc6886e)

Miscellaneous Chores

  • bump cross-spawn and @swc/cli in /benchmark (0e4db68)
  • bump cross-spawn from 7.0.3 to 7.0.6 in /www (ce1d0cd)
  • bump micromatch from 4.0.5 to 4.0.8 in /benchmark (1ddc2a9)
  • bump webpack from 5.89.0 to 5.96.1 in /www (47392b0)
  • http-deepkit: ensure is working with version 1.0.1-alpha-155 (b1ee6d4)
  • packages: bump lib package versions (15bc6ad)

4.3.2 (2024-11-09)

Continuous Integration

  • coverage: do not fail when fail coverage & fix issue with test (ff9702b)

4.3.1 (2024-11-09)

Miscellaneous Chores

  • bump cookie and express in /www (22d850f)
  • bump serve-static and express in /benchmark (249af0c)
  • codecov: fix issues with upload coverage (4b5b39e)
  • reflect-metadata: allow more general versions for reflect metadata (816cc51)

4.3.0 (2024-09-18)

Features

  • aws-stream-handler: add flag to customize callbackWaitsForEmptyEventLoop (#264) (30a59f9)

4.2.3 (2024-09-09)

Bug Fixes

  • response-stream: improve chunk identification (fixes #260) (2aa474e)

Documentation

  • response-stream: add comments and references explaining implementation (d39db53)

Tests

  • response-stream: test eagerly flushed headers (0f33c29)

4.2.2 (2024-09-06)

Bug Fixes

  • apig-v1-adapter: lowercase request headers (4fbb588)

Documentation

  • fix Apollo Server package name in npm command (4d4cece)
  • getting-started: update npm install command (ee4661f)

Miscellaneous Chores

  • bump express from 4.18.2 to 4.19.2 in /benchmark (98e84d1)

4.2.1 (2024-02-29)

Bug Fixes

  • response-stream: fix response with no content doesn't correctly end the writable stream (bded8cf)

Code Refactoring

  • apollo-server-mutation: better types for adapter (79f3383)
  • response-stream: avoid creating object on log while parsing headers (1effcae)

Tests

  • all: cleaning tests and fixing ts issues (c3dcfff)
  • aws-stream: add tests to cover #206 (c853149)

4.2.0 (2024-01-08)

Features

  • frameworks: added support for polka (39377cb)

Miscellaneous Chores

  • bump @apollo/server from 4.9.5 to 4.10.0 (cf4e1d9)
  • bump @rushstack/node-core-library from 3.62.0 to 3.63.0 (19c88e0)
  • bump @vitest/coverage-v8 from 1.1.0 to 1.1.3 (3e67b23)
  • bump eslint-plugin-prettier from 5.1.1 to 5.1.2 (83fc5e5)
  • bump fastify from 4.25.1 to 4.25.2 (e048b11)
  • bump follow-redirects from 1.15.3 to 1.15.4 in /www (af12bbd)
  • bump koa from 2.14.2 to 2.15.0 (164c97b)
  • bump vite from 5.0.10 to 5.0.11 (0478492)

4.1.0 (2024-01-03)

Features

  • network: support buffering transfer-encoding: chunked (f19ffd1)

4.0.1 (2023-12-26)

Bug Fixes

  • ci: missing build part while releasing new version (5b7d184)

Documentation

  • blog: added note about bug related to missing package files (1d75d91)

4.0.0 (2023-12-26)

⚠ BREAKING CHANGES

  • Now we support dual package publish, and the import can fail.

Features

  • added support for dual package publish (dd0803f)

Bug Fixes

  • dual-package-publish: issue with imports lib when moduleResolution is node (4dac8aa)

Documentation

  • blog: added blogpost about dual package publish (006e8a9)
  • updated documentation for dual package publish (03ee217)

Continuous Integration

  • release: fixed issue with release-please skipping release (8dfb582)

3.2.0 (2023-12-22)

Features

  • firebase: bump supported firebase functions to 4.x (b717240)

Documentation

  • readme: removed semantic release badge (fe85304)

Miscellaneous Chores

  • benchmark: bump package versions (b6aa539)
  • bootstrap releases for path: . (e68506e)
  • bump @apollo/server from 4.7.4 to 4.9.3 (52c8b83)
  • bump semver from 5.7.1 to 5.7.2 in /benchmark (0a6a3e0)
  • bump semver from 5.7.1 to 5.7.2 in /www (49c7baf)
  • bump vite from 4.3.5 to 4.4.9 (ecd1252)
  • bump vite from 4.4.9 to 5.0.10 (8eadf40)
  • bump word-wrap from 1.2.3 to 1.2.4 (218d3a9)
  • docs: update to docusaurus v3 (51a104e)
  • package: bump package versions (fe0a0fc)
  • release-please: set latest version (69110ec)
  • semantic-release: removed unused package (2c60275)

Continuous Integration

  • codeql: run only when changing code files (93d8f1c)
  • codeql: updated configuration (9ffa3e8)
  • docs: only trigger when update workflows of docs (c1e7f8a)
  • pr: only run when update specific files (0085520)
  • pr: stop running the pr on main (7c7a05a)
  • release-please: try fix issues with release please config (46577f2)
  • release: added coverage (57f1e09)
  • release: added provenance during publish (1161e42)
  • release: include all commits on release (9185a0b)
  • release: moved configuration to the correct place (b8c6156)
  • release: use release manager instead of merge-and-release (ef278e6)
  • workflows: bump action versions (647e694)

CHANGES:

3.1.0 (2023-07-01)

Bug Fixes

  • build: disable minify identifiers (0a285a6)
  • package: types not being emitted (2bc1244)

Features

  • aws: added adapter for request lambda edge (b8791da)

3.0.0 (2023-06-09)

Bug Fixes

  • api-gateway-v1: probably missing query string value when multiple (78b9f18)
  • perf(api-gateway-v2)!: single pass when collecting headers and cookies on response (3d65895)
  • refactor(core)!: removed support for regex on binary validation and case-sensitive headers (4fb3a39)
  • chore(nodejs)!: deprecate node 12.x, 14.x and 16.x (4c734d4)

Features

  • trpc: bump support for 10.x (5d3124a)

Performance Improvements

  • api-gateway-v1: faster getRequest (70f7020)
  • api-gateway-v2: faster getRequest method (3b08708)
  • aws: optimized strip base path (f72967a)
  • default-handler: always log using fn (36950b3)
  • headers: use object.keys + reduce instead of entries (41339c6)
  • logger: faster logger (103817c)
  • optional: use strict equal instead of typeof (1fba12c)
  • tsconfig: do not use define because is slower (35ce7c7)

Tests

  • vitest: replaced jest for vitest (7505fad)

BREAKING CHANGES

  • now we don't flatten the headers
  • now regex is not support anymore due the slow performance and we don't lower case all the headers, so the content-encoding and content-type must be lowercase
  • vitest: removed support for fastify 3.0.0 & hapi 20.x & firebase-admin < 11
  • Now we will no longer support old nodejs versions