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

@adenin/cf-provider

v2.8.10

Published

Detect current serverless function environment and return correct middleware

Downloads

146

Readme

Cloud Function Provider

This module returns entrypoints for FaaS providers Microsoft Azure, AWS Lambda, and Google Cloud functions.

It detects execution environment at runtime before returning the appropriate entrypoint for that environment, therefore enabling you to abstract away provider-specific code and implement only the business logic in your project repository, which will work for all 3 providers without modification.

By default, if neither of the 3 providers are detected, it will return a Koa middleware that allows you to execute your cloud functions in a local environment by calling the returned function from a Koa server.

Installation

Use npm:

npm install @adenin/cf-provider

Usage

The module assumes your function repo to take the following structure:

.
├── activities
|   ├── common/
|   ├── myactivity.js
|   ├── anotheractivity.js
|   └── function.json
└── index.js

Each .js file within the /activities folder will then be accessible through the exported cloud function middleware by name. Helper scripts used by these files should reside in the subfolder /common, to avoid inadvertently exposing them to the function endpoint also.

The function's function.json file is an Azure function configuration which will require the following properties to be set (in addition to the bindings and other required properties):

{
    "disabled": false,
    "bindings": [
        {
            "authLevel": "anonymous",
            "type": "httpTrigger",
            "direction": "in",
            "name": "req",
            "methods": [
                "post"
            ],
            "route": "{activity}"
        },
        {
            "type": "http",
            "direction": "out",
            "name": "res"
        }
    ],
    "scriptFile": "../index.js",
    "entryPoint": "activities"
}

Within index.js, you then simply need to do the following:

require('@adenin/cf-provider')(exports);

Your functions repo will now be exporting an entrypoint to access all the scripts contained in /activities, which will be correct for the current execution environment detected. The activity to be executed is specified by a mandatory path parameter /:activity to be appended to the endpoint of the deployed cloud function.

You can use the following function entry points for deployment to cloud providers:

GCP: activities

AWS: index.activities

Azure function entrypoint is already configured in function.json.

To call functions from a Koa server we can just provide the following route:

const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');

global.logger = require('@adenin/cf-logger');

const app = new Koa();
const router = new Router();

const index = require('./index');

router.post('/:activity', async (ctx) => {
    await index.activities(ctx);
});

// set up koa app
app
    .use(bodyParser())
    .use(router.routes())
    .use(router.allowedMethods())
    .listen(3000);

The exported middleware accepts POST requests, and the request body will be passed into each activity function - this object will automatically become the response body when function execution ends (no need to return it).

The activity script should export a single async function similar to the following:

module.exports = async (activity) => {
    if (activity.Request.Data === 'Say hello') {
        activity.Response.Data = 'Hello world!';
    }
}

The module will enforce that the body has the valid structure for an adenin activity object, that is that it contains the following root nodes:

{
    "Request": {},
    "Context": {}
}

It also allows for authentication with an API key if an environment variable API_KEYS, containing a ; delimited set of keys, is set in the execution environment. The key can then be provided in the x-api-key request header.

If neither this header nor the environment variable are set, authentication will not be required, unless otherwise configured within your cloud service's internal settings. Requests made when authorization is disabled will however be logged.

Logging is provided via @adenin/cf-logger, therefore any information logged while execution is within cf-provider code, is dependent on the cf-logger's LOG_FILTER environment variable.