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

f

v1.4.0

Published

Functional Microservice Request Library

Downloads

7,802

Readme

f

Functional Microservice Request Library

f is a Functional Microservice Request Library. It's a zero-dependency module that wraps HTTP(S) requests, intended for use with stateless, functional microservices. It's built to work out-of-the-box with services created using the stdlib microservice registry, but can be configured to use any gateway (and associated platform or infrastructure provider).

Installation

Node.js

For usage in an existing Node.js project, add it to your dependencies:

$ npm install f

And use it with the following line wherever you need it:

const f = require('f');

Web

Using Bower...

$ bower install poly/f

Or, simply copy web/f.js to wherever you keep your vendor scripts and include it as a script:

<script src="path/to/f.js"></script>

How do I use f?

f creates HTTP(S) requests following a custom specification that maps very closely to function invocation you're used to (as if it were running in a native environment). Let's say we have a microservice running on stdlib that calculates great-circle distances given two sets of coordinates using the Haversine formula.

The call to this service using f, might look like this;

// Calculate distance from Toronto to San Francisco
f('polybit/haversine')({
	from: [43.65, -79.38],
	to: [37.77, -122.42]
}, (err, result) => {

	console.log(result); // logs 3644329 (metres!)

});

Parameters: Arguments and Keyword Arguments

Usually when we make HTTP requests we think of querystring parameters, form-data, urlencoded variables and of course, json. With f, our goal is to standardize the way functional microservices are invoked (and deal with parameters) by referencing familiar concepts; function arguments and keyword arguments.

Arguments (args) are passed to functions as an array of basic JSON types (non-objects); number, boolean, string, null. Keyword arguments are allowed to be anything JSON-serializable (Objects and Arrays). The basic structure for function calls with f is the following;

let fn = f('route/to/function');
fn(arg_1, ..., arg_n, {kwarg_1: val_1, ... kwarg_n: val_n}, callback);

This maps to an HTTP request with the following POST data in the body;

{
	"args": [arg_1, ..., arg_n],
	"kwargs": {
		"kwarg_1": val_1,
		...
		"kwarg_n": val_n
	}
}

Which should be interpreted by a functional microservice (server-side) as;

module.exports = (params, callback) => {
	// params.args == [arg_1, ..., arg_n]
	// params.kwargs == {kwarg_1: val_1, ..., kwarg_n: val_n}
	callback(null, 'Hello World');
};

Note that every parameter is optional. It's up to whoever creates the microservice to lay out the expectation of which arguments / keyword arguments are supported, which can be done using descriptions on services like stdlib or GitHub.

Sending Files

To send raw file (Buffer) data, simply provide 'file' as a string to the second argument when referencing the function:

f('path/to/func', 'file')(new Buffer(0), callback);

This will send POST data with exactly the Buffer contents.

Configuring Gateway

If you don't feel like using stdlib's gateway at https://f.stdlib.com/, simply configure the gateway as follows;

const f = require('f');
f.config.gateway = {
	host: 'my.host',
	port: 8080,
	path: '/'
};

You can, alternatively, pass in custom configuration as a third parameter on a per-function basis.

let fn = f('path/to/func', 'json', {host: 'my.host', port: 8080, path: '/'});
fn(arg0, ..., callback);

Why Use Microservices?

Microservices are tremendously useful for offloading computationally expensive tasks from your core infrastructure, or providing standardized functionality to many different systems (at the cost of a few ms of network latency).

An example would be image processing. Resizing, cropping and editing may not be done frequently on your webserver, but when it does happen, it can slow everything down. Offloading to a scalable, stateless microservice that your application simply calls via the f module is a simple solution.

Another example would be the haversine distance formula given above. You may have found a great npm package, but what if that service functionality needs to be shared across multiple applications written in different languages? Python and Ruby are both capable of making simple HTTP requests to a microservice, but do not share packages in common with the Node ecosystem. Microservices solve this problem.

We plan to have more SDKs out in the coming months. :)

Where can I find Microservices to use?

You can find a list of available microservices on stdlib's search page, where the f team (Polybit Inc.) is hard at work creating a central registry of microservices for the web. Feel free to test drive a basic service or create your own.

You can create microservices using the stdlib CLI tools, but microservice development is out of the scope of the f package directly. It is handled on a platform and infrastructure provider basis.

Thanks!

The f package is © 2016 Polybit Inc. and happily MIT licensed.

Go wild! Contributors welcome, but we ask that PRs don't introduce dependencies and mostly focus on bugfixes and usability.

Sign up for stdlib: A Standard Library for Microservices.

Follow us on Twitter @polybit.