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

nodefaas

v0.0.3

Published

Node OpenFaas connector

Downloads

1

Readme

nodefaas

Node OpenFaas connector that utilises async/await and node-fetch

This library is inspired by the work of Austin Frey.

Installation

npm i nodefaas

Basic Usage

const faas = new NodeFaas();

console.log(await faas.invoke('figlet', 'Hello World'));

Advanced Usage

const FAAS_HOST = 'http://127.0.0.1:8080';

const FAAS_OPTIONS = {
    username: '{your admin username}',
    password: '{your admin password}',
    agentSettings: {  // Settings as per Node HTTP/HTTPS agent, e.g.:
        keepAlive: true,
        keepAliveMsecs: 1500,
    }; 
};

const faas = new NodeFaas(FAAS_HOST, FAAS_OPTIONS);

let response;
try {
    response = await faas.deploy('my-figlet', 'functions/figlet:latest');
    console.log(await faas.invoke('my-figlet', 'Hello World'));
    console.log(await faas.inspect('my-figlet'));
    response = await faas.remove('my-figlet');
} catch (e) {
    console.error(e);
}

Error Handling

All errors are thrown with a NodeFaasError object that extends the node Error object:

class NodeFaasError extends Error {
    constructor(message, status, statusText) {
        super(message);
        this.name = "NodeFaasError";
        this.status = status;                   // HTML Status codes
        this.statusText = statusText;
    }
}

Available Functions

invoke
faas.invoke(funcName, data, {options});
  • funcName = the name of the FAAS function you want to invoke
  • data = a string or object that you want to pass to the function
  • options = an object that defines the return:
    • isJson: true | false - Sets expected return type as JSON
    • isBinaryResponse: true | false - Sets expected return type as Binary

Returns the data from the FAAS function

list
faas.list();

Returns a list of the available FAAS functions in a JSON object

inspect
faas.inspect(funcName);
  • funcName - the name of the FAAS function you want to inspect

Returns statistics about a specific function as a JSON object

deploy
faas.deploy(funcName, image, {options});
  • funcName - the name of the function you want to deploy (e.g. 'figlet-test')
  • image - the name of the image you want to deploy (e.g. 'functions/figlet:latest')
  • options {optional}
    • network - defaults to 'func_functions'

Returns deployment status

remove
faas.remove(funcName);
  • the name of the function you want to remove (e.g. 'figlet-test')

Returns removal status


Tests

I have yet to create any unit tests for this project!

In the root of the project there is a file called

test-figlet.js

This file deploys a figlet to your server, runs some speed tests against it, and then removes it.

To execute this file, you will need to add an .env file to the root of the project (sample below)

FAASHOST = http://127.0.0.1:8080
FAASUSERNAME = admin
FAASPASSWORD = adminpasswordadminpassword
FAASITERATIONS = 1000

Once run, it will output something similar to the following:

...
Running test: 999
Time to run 1 figlet: 5.67ms
Time to run 10 figlets: 48.61ms
{
  name: 'nodefaas-figlet-99d9f6e3-e881-4131-9267-505c29766b98',
  image: 'functions/figlet:latest',
  invocationCount: 10803,
  replicas: 20,
  envProcess: '',
  availableReplicas: 1,
  labels: {
    faas_function: 'nodefaas-figlet-99d9f6e3-e881-4131-9267-505c29766b98'
  },
  annotations: { 'prometheus.io.scrape': 'false' },
  namespace: 'openfaas-fn'
}
Average time to run 1000 iterations:
Executing one: 22.76ms
Executing 10: 60.22ms
Removing test figlet
{ status: 202, statusText: 'Accepted', body: '' }