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

cascading-service-config

v1.1.2

Published

dry configuration for a multi-service system

Downloads

14

Readme

Cascading Service Config

is a format for a versatile configuration file that configures more than one http service in a single system.

What does it do?

  • Extract a configuration for a single service from a global configuration file.
  • Supply access points to each of the other services in the system, that are configured in the global configuration file.
  • Inherit configurations of modules that are shared between services.

Install

npm install cascading-service-config

Example

Lets say we have an example config with two services and an authentication module (which requires a config) that both services depend on, as well as some shared settings. Our config might look like this:

var configSource = {
  logmethod: "syslog", // a shared setting
  auth: {
    cookie: "authKey",
    storage: {
      type: "redis",
      location: "localhost:6789"
    }
  },
  service1: {
    port: 8402,
    logmethod: "loggly",
    wibblewobbles: true
  },
  service2: {
    port: 8403
  }
}

Now we can give this to CSC, as well as a domain - the domain is just the name of the service which is currently reading the config.

var config = require('cascading-service-config')(configSource, 'service1');

Now we have our config object. The properties which you have access to look like this (but this is different to what you'll get if you console.log it, as we are making using of prototypical inheritance):

{ 
  logmethod: "loggly", // note that we overrode the shared setting here
  auth: { 
    service1: "http://localhost:8402",
    service2: "http://localhost:8403",
    logmethod: "syslog",
    auth: [circular reference],
    cookie: "authKey", 
    storage: { type: "redis", location: "localhost:6789" } 
  },
  service1: "http://localhost:8402",
  service2: "http://localhost:8403",
  wibblewobbles: true,
  port: 8402
}

So what's happening here?

Because we specified service1 as our domain, that's is our top-level config object. All properties in the service1 object of the config source are passed verbatim to the final config.

However, under this, we have two objects which we inherit.

  1. { service1: "http://localhost:8402", service2: "http://localhost:8403" } - CSC looks through each of the objects on the root level of the config. If an object has a port setting, it is assumed to be a service. This object consists of assembled access points for each of the other services in your domain, so that they're easily accessible on your config.
  2. The original config. This is the base prototype.

Also note that each other object on the root config also gains the same prototype chain, so in our example, auth also gained the service locations.

In our example, the prototype chain looks something like this:

      { wibblewobbles: true, logmethod: "loggly", port: 8402 }
//                          inherits...
  { service1: "http://localhost:8402", service2: "http://localhost:8403" }
//                          inherits...
  { auth: { ... }, logmethod: "syslog", service1: { ... }, service2: { ... } }

My other services aren't running on http or localhost

No problem! There's two special config options you can use to configure this: internalServiceHostname and internalServiceProtocol. You can configure this globally or per-object. In our example, we could set:

{
  logmethod: "syslog"
  internalServiceHostname: "amazing.service.com",
  internalServiceProtocol: "https",
  ...

Which, in our final config, would result in...

console.log(config.service2);
// -> 'https://amazing.service.com:8403'

But what if service1 is running on http, not https? Well, we can override the root internalServiceProtocol in service1's config:

  ...
  service1: {
    internalServiceProtocol: "http",
    port: 8402,
    ...

Resulting in...

console.log(config.service1);
// -> 'http://amazing.service.com:8402'
console.log(config.service2);
// -> 'https://amazing.service.com:8403'

Can I access the config of other services?

It's possible, but very much discouraged (as you'll see from the syntax). Config formats change, and depending upon another service's config format is usually a pretty bad idea.

However, if you absolutely must do it, there are two ways.

You could access the source config object by traversing the prototype chain:

console.log(config.__proto__.__proto__.service2.port);
// -> 8403

Or you could just call CSC again and specify the domain of the other service:

var service2config = require('cascading-service-config')(configSource, 'service2');
console.log(service2config.port);
// -> 8403

csc(configSourceObject, [domain])

Reads a cascading service config file and returns a config for the given domain. If the domain does not exist, the root config is returned.