npm.io
0.2.3 • Published 2d ago

@geek-fun/serverless-adapter

Licence
Apache-2.0
Version
0.2.3
Deps
2
Size
51 kB
Vulns
0
Weekly
42

Serverless-Adapter

Node.js CI release npm version Known Vulnerabilities License codecov

Adapter for web frameworks (Express, Koa, Hono) to run on serverless platforms across multiple cloud providers with automatic provider detection.

Supported Cloud Providers

Provider Service Status Trigger Type
Alibaba Cloud (Aliyun) Function Compute Supported API Gateway
Tencent Cloud Serverless Cloud Function (SCF) Supported API Gateway
Volcengine veFaaS (函数服务) Supported API Gateway

Supported Frameworks

Framework Version Status
Express 4.x Supported
Express 5.x Supported
Koa 2.x Supported
Koa 3.x Supported
Hono 4.x Supported

Note: Hono support requires Node.js >= 18 (for Web API Request/Response globals). Express and Koa continue to support Node.js >= 16.

Quick Start

Prerequisites
  • Node.js >= 16.x
Install
npm install @geek-fun/serverless-adapter
Usage

The adapter automatically detects the cloud provider based on the context object:

import express from 'express';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello World!' });
});

// Auto-detect provider based on context
export const handler = serverlessAdapter(app);
Explicit Provider Selection

You can explicitly specify the provider:

import express from 'express';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello from Tencent Cloud!' });
});

// Explicitly specify Tencent provider
export const main_handler = serverlessAdapter(app, { provider: 'tencent' });
Aliyun Function Compute Example
import express from 'express';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = express();

app.get('/api/users', (req, res) => {
  res.json({ users: [] });
});

// Handler for Aliyun Function Compute
export const handler = serverlessAdapter(app);
Tencent SCF Example
import express from 'express';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = express();

app.get('/api/users', (req, res) => {
  res.json({ users: [] });
});

// Handler for Tencent SCF
export const main_handler = serverlessAdapter(app, { provider: 'tencent' });
Volcengine veFaaS Example
import express from 'express';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = express();

app.get('/api/users', (req, res) => {
  res.json({ users: [] });
});

// Handler for Volcengine veFaaS
export const handler = serverlessAdapter(app, { provider: 'volcengine' });
Hono Example
import { Hono } from 'hono';
import serverlessAdapter from '@geek-fun/serverless-adapter';

const app = new Hono();

app.get('/', (c) => c.json({ message: 'Hello from Hono!' }));

// Auto-detect provider based on context
export const handler = serverlessAdapter(app);

API Reference

serverlessAdapter(app, options?)

Creates a serverless handler for your Express, Koa, or Hono application.

Parameters
Parameter Type Required Description
app Express | Koa | Hono Yes Express, Koa, or Hono application instance
options.provider 'aliyun' | 'tencent' | 'volcengine' No Explicitly specify cloud provider (auto-detected if omitted)
Returns

A function that handles serverless events:

(event: Buffer, context: ProviderContext) =>
  Promise<{
    statusCode: number;
    body: string;
    headers: Record<string, string>;
    isBase64Encoded: boolean;
  }>;

Provider Detection

The adapter automatically detects the cloud provider by examining the context object:

Provider Detection Fields
Aliyun service.name, tracing, logger, function.memory
Tencent tencentcloud_region, tencentcloud_appid, namespace
Volcengine requestId, region, function.memoryMb

License

Apache-2.0

Keywords