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

@logzio-node-toolbox/consul

v0.0.32

Published

Consul easy use for json configs and service discovery

Downloads

10,284

Readme

consul

easy to use warper for consul to register to service discovery and get, merge and watch keys

Usage

import { Consul } from '@logzio-node-toolbox/consul';

const consul = new Consul({ port: 18500, host: '127.0.0.1', baseUrl = 'some_base_url' });

initialized params:

  1. port - consul port to connect default 8500 (default 8500)
  2. host - consul host to connect default 8500 (default localhost)
  3. baseUrl - some default path to load configs from, will prefix the path to each get request (default '')
  4. validateOptions - object with defaults { fail: true, timeout: 5000, retries: 6, factor: 2, onRetry: null } can override each one of the defaults.
  5. defaultWatchOptions - object with defaults { backoffFactor: 100, backoffMax: 30000, maxAttempts: 10000 } can override each one of the defaults.
  6. defaultRegisterRetryOptions - object with defaults { factor: 2, retries: 6, onRetry: null } can override each one of the defaults.

methods

validateConnected - make use connected to consul service (will retry (connectMaxRetries) of times);

receive same params as validateOptions and will merge with th one passed to the initializer

  await consul.validateConnected(validateOptions)

get - will get a key from consul service, (if initlized with base path it will prefix it)

  const { key, value } = await consul.get('somepath/config.json')
  // if have base path will fetch 'some_base_url/somepath/config.json'

set - will set a value to consul, (if initlized with basebath it will prefix it)

   await consul.set({key: 'somepath/config.json', value: {some: "value"}})
  // if have base path will set to 'some_base_url/somepath/config.json'

keys - list of keys in path

   await consul.keys('somepath/config.json')

merge - deepmerge current values with new values

it will fetch current values will deep merge all keys and set it

   await consul.merge({key: 'somepath/config.json', value: {toOverride: "newValue" }})

watch - listen to key change and invoke handler

options - same watchOptions object as the initializer ( will merge them together)

   await consul.watch({
   key: 'somepath/config.json',
   onChange: ({key, value}) => {
       console.log(key)
       console.log(value) // new values
   },
   onError:(err) => {
    console.log(err)
   },
   options
   })

register - will register service to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   options
   })

registerInterval - will create interval to validate service always register to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   interval: 3000,
   onError:(err) => {
    console.log(err)
   },
   options
   })

close - deregister and close all watchers

options - same as registerRetryOptions object as the initializer ( will merge them together)

   await consul.close(options)

multiConsul

extend consul to work on multiple keys and merged config

import { MultiConsul } from '@logzio-node-toolbox/consul';
const consul = new MultiConsul({ port: 18500, paths:['config1', 'config2', 'config3'] });
/*
config1 in consul:
{
  "key1": "value1"
  key2": "value1"
  key3": "value1"
}

config2 in consul:
{
  key2": "value2"
}
 // no config 3
*/

const values = await consul.getAll() // {key1: value1, key2: value2, key3: value1};

watchAll(({ key, value, changedValue }) => {
  /* settings new consul file config3
    { key: value3 }
    will invoke watch with
    key: config3
    changedValue: { key: value3 }
    values: {key1: value1, key2: value2, key3: value3};
  */
})