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

@opuscapita/config

v3.1.17

Published

Configuration API connector module for OpusCapita Business Network Portal.

Downloads

2,779

Readme

@opuscapita/config

This library was automatically build and published to npmjs after commit to the master branch but was re-configured for manual build instead.

This module provides easy access to instances of the consul service registry. It helps with accessing key-value stored configuration data, local data encryption and service endpoint discovery with health checking and change notifications.

It provides automatic key prefixing for +key* storage isolation and caching for keys and endpoints. For further details, please have a look at the wiki.

Minimum setup Getting endpoints Automatic change events Requesting multiple keys Minimum setup Keys by prefix Keys by list Data encryption Default configuration Wiki


Minimum setup

First got to your local code directory and run:

npm install @opuscapita/config

To go with the minimum setup, you'll need to have a consul instance up and running. Now go to your code file and put in the minimum setup code.

const config = require('@opuscapita/config');

// You might want to pass a configuration object to the init method. A list of parameters and their default values
// can be found at the .DefaultConfig module property.
await config.init({});
await config.setProperty('my-key', 'my-value');
const value = await config.getProperty('my-key');

console.log(value);

If everything worked as expected, you will get a bluebird promise as result.


Getting endpoints

const config = require('@opuscapita/config');

await config.init();
const endpoint = await config.getEndPoint('service-name');

console.log('Host: ' + endpoint.host);
console.log('Port: ' + endpoint.port);

Automatic change events

The config module provides two different events that are emitted once a service health or a key-value setting has been changed. This is internally used to update the module's cache but it can also be used to write services, that react on changes in the environment. Smartly written, this can be used to change a service's configuration without restarting it.

const config = require('@opuscapita/config');

await config.init();

config.on('endpointChanged', (serviceName, endpoints) => console.log(serviceName, endpoints));
config.on('propertyChanged', (key, value) => console.log(key, value));

Requesting multiple keys

In addition to single-key-requests, the config module provides **two different methods for multi-key-requests.

  1. Key-prefixing (like paths).
  2. List of keys.

Keys by prefix

Prefixing allows you to recursively fetch consul keys and their corresponding values by simply passing a prefix and the recursive parameter to the getProperty() method of the config module. After all keys have been retrieved successfully, the resulting promise contains an object in which the (full) consul keys represent the actual keys and their values represent the actual values inside consul.

const config = require('@opuscapita/config');

await config.init();

await Promise.all([
	config.setProperty("path/to/key1", "value1"),
	config.setProperty("path/to/key2", "value2"),
	config.setProperty("path/to/key3", "value3")
]);

const values = await config.getProperty("path/", true);

console.log(values);

/* Should output:
{ 'path/to/key1': 'value1',
  'path/to/key2': 'value2',
  'path/to/key3': 'value3' }
*/

Keys by list

The config module allows passing an array of keys or prefixes (but not mixed!) to the getProperty() method. After all keys have been retrieved successfully, the resulting promise contains an array of consul values each at the corresponding position of their input keys.

const config = require('@opuscapita/config');

await config.init();

await Promise.all([
	config.setProperty("path/to/key1", "value1"),
	config.setProperty("path/to/key2", "value2"),
	config.setProperty("path/to/key3", "value3")
]);

const values = await config.getProperty([ "key1", "key2", "key3" ]);

console.log(values);

/* Should output:
[ 'value1', 'value2', 'value3' ]
*/

Data encryption

By using the getPassword() and setPassword() methods, the config module is capable of encrypting and decrypting data retrieved and or stored with this methods. It also exposes the encryptData() and decryptData() methods, which can be used to encrypt small amounts of local data.

The file storage of the encryption key can be configured using the serviceSecretPath property of available through the config object .DefaultConfig. This feature is mainly designed to use docker secrets but can of course be adjusted to any other key path.

Notice: If the key path was not found at the configured location, a warning is emitted to the console and the key 'default' is used.


Default configuration

The default configuration object provides hints about what the module's standard behavior is like. It is mostly recommended to leave most settings as they are and treat them more as general conventions to a common structure in order to maintain a common setup across different services. For further details, please have a look at the wiki.

{
	host : 'consul',
	port : 8500,
	retryCount : 50,
	retryTimeout : 1000,
	logger : null,
	serviceSecretPath : `/run/secrets/${ConfigClientBase.serviceName}-consul-key`
}