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

@telefonica/architect

v1.0.4

Published

A Simple yet powerful plugin system for node applications

Downloads

145

Readme

Architect

Architect is a simple but powerful structure for Node.js applications. Using Architect, you set up a simple configuration and tell Architect which plugins you want to load. Each plugin registers itself with Architect, so other plugins can use its functions. Plugins can be maintained as NPM packages so they can be dropped in to other Architect apps.

Plugin Interface

// auth.js

/* All plugins must export this public signature.
 * @options is the hash of options the user passes in when creating an instance
 * of the plugin.
 * @imports is a hash of all services this plugin consumes.
 * @register is the callback to be called when the plugin is done initializing.
 */
module.exports = function setup(options, imports, register) {

  // "database" was a service this plugin consumes
  var db = imports.database;

    register(null, {
        // "auth" is a service this plugin provides
        auth: {
            users: function (callback) {
                db.keys(callback);
            },
            authenticate: function (username, password, callback) {
                db.get(username, function (user) {
                    if (!(user && user.password === password)) {
                        return callback();
                    }
                    callback(user);
                });
            }
        },
        // This method will be called after app.destroy()
        onDestroy: function () {
            console.log('Destroying auth...');
        }
    });
};

Each plugin is usually a node module complete with a package.json file. It does not need to be in npm, it can be a simple folder in the code tree.

{
    "name": "auth",
    "version": "0.0.1",
    "main": "auth.js",
    "private": true,
    "plugin": {
        "consumes": ["database"],
        "provides": ["auth"]
    }
}

As shown in simple demo, a plugin could also be a plain javascript file.

function mathPlugin(options, imports, register) {
    register(null, {math: {
        add: function (a,b) { return a + b; },
    }});
}
mathPlugin.provides = ["math"];

module.exports = mathPlugin;

Config Format

The loadConfig function below can read an architect config file. This file can be either JSON or JS (or anything that node's require can read).

The simple calculator app could have a config like this:

module.exports = [
    { packagePath: "architect-http", port: 8080 },
    { packagePath: "architect-http-static", root: "www" },
    "./plugins/calculator",
    "./plugins/db",
    "./plugins/auth"
]

Notice that the config is a list of plugin config options. If the only option in the config is packagePath, then a string can be used in place of the object. If you want to pass other options to the plugin when it's being created, you can put arbitrary properties here.

The plugin section in each plugin's package.json is also merged in as a prototype to the main config. This is where provides and consumes properties are usually set.

Architect main API

The architect module exposes two functions as it's main API.

createApp(config, [callback])

This function starts an architect config. The return value is an Architect instance. The optional callback will listen for both "error" and "ready" on the app object and report on which one happens first.

loadConfig(configPath)

This is a sync function that loads a config file and parses all the plugins into a proper config object for use with createApp. While this uses sync I/O all steps along the way are memoized and I/O only occurs on the first invocation. It's safe to call this in an event loop provided a small set of configPaths are used.

Class: Architect

Inherits from EventEmitter.

The createApp function returns an instance of Architect.

Event: "service" (name, service, plugin)

When a new service is registered, this event is emitted on the app. name is the short name for the service, and service is the actual object with functions, and plugin is the plugins object to which the service belongs.

Event: "plugin" (plugin)

When a plugin registers, this event is emitted.

Event: "ready" (app)

When all plugins are done, the "ready" event is emitted. The value is the Architect instance itself.

Event: "error" (err)

When an error occur, "error" event is emitted. Could be called several times.

Event: "ready-additional" (app)

When all additional plugins are done, the "ready-additional" event is emitted. The value is the Architect instance itself.

Method: loadAdditionalPlugins (additionalConfig, callback)

Method to load plugins after createApp is called with initial config. A new plugin config must be provided in additionalConfig and callback is called with Architect instance as second param, and an event "ready-additional" is emitted.

Method: destroy ()

Method to gracefully destroy all plugins, calling their onDestroy method if provided when registering.