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

faastcgi

v0.1.2

Published

A FaaS system served via the FastCGI protocol

Downloads

2

Readme

Node FaastCGI

Deploy your Node scripts by simply copying them on a server! FaastCGI is a FaaS (function as a service) system, which is build on top of node-fastcgi, to be able to deploy Node.js scripts by simply placing them in a directory that's accessible by nginx for example. Running the script is then as easy as sending a request to the file you want to run. Basically, this makes running Node.js scripts as easy and convenient as running PHP scripts, but comes with the same tradeoff, i.e. you can't run long running processes with this.

Features

  • Run short Node.js functions without any deployment. Copy the files to a web-accessible directory and you are good to go.
  • Full support for everything Node has to offer, i.e. all local and external modules.
  • Based on express, so native support for middleware.
  • Built-in session system, to get a quick function running even faster.
  • High performance: Each function runs in its own process so nothing gets slowed down by the Node event loop.

Installation

Simply install the module globally by running

npm install -g faastcgi

You can then run faastcgi in your shell to start the CGI broker, but without any configuration this is kind of useless.

Configuration

You can set configration options either in a config file provided as the first parameter to the faastcgi call or in environment variables. The config file can either contain plain JSON or executable code, as long as the necessary options are exported. The environment variables take precedence over the values in the config file.

Configuration options

The following listing contains all configuration options as well as their environment variable counterparts and a small explanation.

{
    "socketPath": String, // SOCKET_PATH, path to a UNIX socket where the CGI server can be bound to. Default: null
    "port": Number, // BIND_PORT, port to bind the CGI server to, Default: 61120
    "address": String, // BIND_ADDRESS, address to bind the CGI server to, Default: "localhost"
    "maxRunners": Number, // MAX_RUNNERS, max number of idle processes waiting for requests, Default: 5
    "session": {
        "dbpath": String, // SESSION_DB_PATH, path to a directory where a sqlite database with session information will be stored, Default: /tmp/
        "secret": String, // SESSION_SECRET, if this is not set to some value the sessions are not persistent across broker restarts, Default: <random>
    },
    "runner": {
        "user": Number, // RUNNER_UID, uid of the user under which the runners will be run, Default: <same as broker>
        "group": Number, // RUNNER_GID, group id of the user under which the runners will be run, Default: <same as broker>
    }
}

Structure of deployed files

The files that can be picked up by the runners are normal Node.js scripts that are require()'d. To provide the scripts functionality to the runner, a few properties need to be exported to user certain features:

main (required)
A function with the signature (req, res), where req and res are standard express.js request and response objects. To finalize the request, simply call res.end() or res.send(), or any of the other available methods. If the request is finalized you have 1 second to terminate, otherwise the runner will be killed.

middleware (optional)
A function with the signature (app, builtins). This function can be used to run some additional express middleware before the main function, like a body parser for example. The builtins object contains a reference to express directly, so enabling a json body parser for example is as simple as defining:

middleware: function(app, builtins) {
    app.use(builtins.express.json());
}

enableSession (optional)
Set this value to a truthy value to enable the built-in session system based on express-session. You could of course run your own session middleware in the middleware function, but the builtin session is a very convenient way to get sessions working without much overhead.

showErrors (optional)
If this is set to true, an a trace of each error occuring in the main function will be sent back as a response to a request. The default behavior will output the error in the console, but only reply with a HTTP 500 error.

sessionOpts (optional)
Additional options to give to the express-session constructor. Use this to change cookie settings for example.

Some examples for function files can be found in the examples/ directory.

nginx configuration

If you have a php-fpm setup already, you can integrate this software pretty easily by simply adding these lines to the definition of your virtual host:

location ~ \.fajs$ {
    fastcgi_pass   unix:/run/faastcgi.sock; # path to your unix socket
    fastcgi_index  index.fajs;
    include        fastcgi_params; # reuse the same params from php
}

To avoid confusion between normal .js files and the ones executed by faastcgi I have decided to use the file extension .fajs to differentiate the two, but you are of course free to use any extension you like. One thing to keep in mind however: If you write some local module code in .js files for inclusion only, don't forget that nginx will serve them out as text by default. It might therefore be advisable to either use another extension for these files (like .jsm) and prevent nginx from serving this filetype or to put your local modules in a lib/ folder and then restricting access to that.