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

protocall

v2.0.0

Published

Enable use of protocols (such as file:, buffer:, or method:) in configuration files.

Downloads

1,710

Readme

protocall

Npm version Build Status codecov

Use of “Protocall”s in your json configuration

Sometimes JSON just isn't enough for configuration needs. Occasionally it would be nice to use arbitrary types as values, but JSON is necessarily a subset of all available JS types.

protocall enables the use of protocols and handlers to enable identification and special handling of json values.

:warning: This is was initially a fork from shortstop and shortstop-handlers

Basic Usage

Just create a resolver, then use it to resolve your config as objects with resolve or as files with resolveFile. You can either create a resolver with protocall.create() and then customize it, or just use default provided resolver protocall.getDefaultResolver()

const protocall = require('protocall');
const resolver = protocall.getDefaultResolver();
const config = {
    secret: "base64:SGVsbG8sIHdvcmxkIQ==",
    ssl: {
        pfx: "file:foo/bar",
        key: "file:foo/baz.key"
    }
};

resolver.resolve(config, (err, data) => {
    console.log(data);
    // {
    //     "secret": <Buffer ... >,
    //     "ssl" {
    //         "pfx": <Buffer ... >,
    //         "key": <Buffer ... >
    //     }
    // }
});
// Or using the promise returned...
resolver.resolve(config).then(data => {
    // Do something with the 'data
});

(Default) Handlers

A handler can be either a function taking value and a callback, or just take some value and return directly the value or a promise:

const handlerIdentityOne = (value, cb) => cb(null, value);
const handlerIdentityTwo = value => value;
const handlerIdentityThree = value => Promise.resolve(value);

Protocall is shipped with already defined handlers. Here is the default handlers from shortstop-handlers. All these are loaded by the getDefaultResolver

path

protocall.handlers.path([basedir])

  • basedir (String, optional) - The base path used for resolving relative path values. Defaults to caller dirname.

Creates a handler that can be given to protocall to resolve file paths.

file

protocall.handlers.file([basedir], [options])

  • basedir (String, optional) - The base path used for resolving relative path values. Defaults to caller dirname.
  • options (Object, optional) - Options object provided to fs.readFile.

Creates a handler which resolves the provided value to the basedir and returns the contents of the file as a Buffer.

base64

protocall.handlers.base64()

Creates a handler which will return a buffer containing the content of the base64-encoded string.

env

protocall.handlers.env()

Creates a handler which will resolve the provided value as an environment variable, optionally casting the value using the provided filter. Supported filters are |d, |b, and |!b which will cast to Number and Boolean types respectively.

Examples:

{
    "string": "env:HOST",
    "numver": "env:PORT|d",
    "true": "env:ENABLED|b",
    "false": "env:FALSY|b",
    "notFalse": "env:FALSY|!b"
}

require

protocall.handlers.require([basedir])

  • basedir (String, optional) - The base path used for resolving relative path values. Defaults to caller dirname.

Creates a handler which resolves and loads, and returns the specified module.

Examples:

 {
    "path": "require:path",
    "minimist": "require:minimist",
    "mymodule": "require:./mymodule",
    "json": "require:../config/myjson"
}

exec

protocall.handlers.exec([basedir])

  • basedir (String, optional) - The base path used for resolving relative path values. Defaults to caller dirname.

Creates a handler which resolves and loads the specified module, executing the method (if specified) or the module itself, using the return value as the resulting value. The value should have the format {module}(#{method})?. If no function is able to be found this handler will throw with an error.

{
    "functionFromModule": "exec:./mymodule#create",
    "module": "exec:./myothermodule"
};

glob

protocall.handlers.glob([basedir|options])

  • basedir (String or Object, optional) - The base path use for resolving or a glob options object per https://github.com/isaacs/node-glob#options

Creates a handler which match files using the patterns the shell uses.

{
    "files": "glob:**/*.js"
}

Resolver API

Basicly a resolver enable you to register new protocalls/handlers, and to resolve object(resolve) or files(resolveFile)

resolver.use

There is two accepted signatures:

single protocol handler

resolver.use(protocol, handler)

  • protocol (String) - The protocol used to identify a property to be processed, e.g. "file"
  • handler (Function) - The implementation of the given protocol with signature function (value, [callback])

This method returns a function when invoked will remove the handler from the stack for this protocol.

const protocall = require('protocall');

const resolver = protocall.create();
resolver.use('path', protocall.handlers.path());
resolver.use('file', protocall.handlers.file());

multiple protocol handlers

resolver.use(protocolsToHandlers)

  • protocolToHandlers (Object) - An object mapping of protocol name to handler.

This method returns an object mapping protocol to their unsuscribing function

const protocall = require('protocall');

const resolver = protocall.create();
resolver.use({
    path: protocall.handlers.path(),
    file: protocall.handlers.file()
});

resolve.resolve

resolver.resolve(data, [callback])

  • data (Object) - The object, containing protocols in values, to be processed.
  • callback (Function) - Optional callback invoked when the processing is complete with signature function (err, result).

Return a promise that is resolved to the processed data.

resolve.resolveFile

resolver.resolveFile(path, [callback]);

  • path (String) - The path to a file which is, or exports, JSON or a javascript object.
  • callback (Function) - Optional callback invoked when the processing is complete with signature function (err, result).

Return a promise that is resolved to the processed data.

Advanced resolver usage

Multiple handlers

Multiple handlers can be registered for a given protocol. They will be executed in the order registered and the output of one handler will be the input of the next handler in the chain.

const protocall = require('protocall');

const resolver = protocall.create();
resolver.use('path', protocall.handlers.resolve);
resolver.use('file', protocall.handlers.resolve);
resolver.use('file', fs.readFile);

const config = {key: 'file:foo/baz.key', certs: 'path:certs/myapp'};

resolver.resolve(json, function (err, data) {
    console.log(data);
    // {
    //     "key": <Buffer ... >,
    //     "certs": "/path/to/my/certs/myapp"
    // }
});

Removing Handlers

When registered, handlers return an unregister function you can call when you no longer want a handler in the chain.

const path = require('path');
const protocall = require('protocall');

const resolver = protocall.create();
const unusePathProtocall = resolver.use('path', protocall.handlers.resolve);

const config = {key: 'path:foo/baz.key'};

resolver.resolve(config, function (err, data) {
    console.log(data);
    // {   "key": "/path/to/my/foo/baz.key"  }

    unusePathProtocall();

    resolver.resolve(json, function (err, data) {
        console.log(data);
        // { "key": "path:foo/baz.key"  }
    });
});