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

vnode-hashring

v1.0.1

Published

A typescript implementation of consistent hash algorithm based on virtual nodes for Node.js

Downloads

4

Readme

vnode-hashring

A typescript implementation of consistent hash algorithm based on virtual nodes for Node.js

NPM version

Installation

# npx pnpm install vnode-hashring
npm install vnode-hashring

Usage

const Hashring = require('vnode-hashring')
const servers = [
  '127.0.0.1',
  '127.0.0.1:4000',
  '127.0.0.1:5000',
]
const cons = new Hashring(servers)
const codes = [
  '04615', '00133', '29599', '00195', '61532', '42529', '80009', '79087', '94467', '96384', '09874', '72902', '91466', '10546', '97845', '43680', '65013', '80230', '53824', '47539'
]
const nodes: Record<string, Array<string>> = {}

codes.forEach((code) => {
  const node = cons.getNode(code)
  if (node) {
    if (nodes[node.address]) { nodes[node.address].push(code) }
    else {
      nodes[node.address] = []
      nodes[node.address].push(code)
    }
  }
})
// nodes:
// {
//   "127.0.0.1": [
//     "00195", "61532", "96384", "91466", "43680", "65013", "47539"
//   ],
//   "127.0.0.1:4000": [
//     "04615", "29599", "94467", "09874", "72902", "97845", "53824",
//   ],
//   "127.0.0.1:5000": [
//     "00133", "42529", "80009", "79087", "10546", "80230",
//   ],
// }
const statistics: Record<string, number> = {}
Object.keys(nodes).forEach((node) => {
  statistics[node] = nodes[node].length
})
// statistics:
// {
//   "127.0.0.1": 7,
//   "127.0.0.1:4000": 7,
//   "127.0.0.1:5000": 6,
// }

nodes

the first parameter is the server array which is being add to hash ring.

item can be strings(server address) or objects including address(server address) and vnodes(virtual node count)

vnodes: global vnodes count will be overwritten for this server node if you set the vnodes

new Hashring([
  '127.0.0.1', // 127.0.0.1: defatult 100
  {
    address: '127.0.0.1:4000',
    vnodes: 50
  }, // 127.0.0.1:4000: 50
])

options

  • vnodes The amount of virtual nodes per server, defaults to 100
  • algorithm you can configure the algorithm that is used to hash the keys. It defaults to md5 and can only contain values that are accepted in Node's crypto API.
new Hashring(['127.0.0.1'], {
  vnodes: 200,
  algorithm: 'sha256'
})

API

getNode(key)

Find the correct node for which the key is closest to the point after what the given key hashes to.

  • key {string}: Random key that needs to be searched in the hash ring

return {string}: The matching server address

cons.getNode('00195') // 127.0.0.1
cons.getNode('04615') // 127.0.0.1:4000

getNodes()

Get all the server address.

return {string[]}: server address array

cons.getNodes() // ['127.0.0.1', '127.0.0.1:4000', '127.0.0.1:5000']

addNode(node)

Add a new server to ring without having to re-initialize the hashring.

return {Hashring}: Hashring instance for call chaining

cons.addNode('127.0.0.1:6000')
// ['127.0.0.1', '127.0.0.1:4000', '127.0.0.1:5000', '127.0.0.1:6000']
// vnodes count: 100 + 100 + 100 + 100 = 400
cons.addNode({
  address: '127.0.0.1:7000',
  vnodes: 50
})
// ['127.0.0.1', '127.0.0.1:4000', '127.0.0.1:5000', '127.0.0.1:6000', '127.0.0.1:7000']
// vnodes count: 100 + 100 + 100 + 100 + 50 = 450

removeNode(node)

Remove a server from the hash ring.

  • node {string}: Server address that need to be removed from the ring.

return {Hashring}: Hashring instance for call chaining

cons.removeNode('127.0.0.1:6000')
// ['127.0.0.1', '127.0.0.1:4000', '127.0.0.1:5000', '127.0.0.1:7000']
// vnodes count: 100 + 100 + 100 + 50 = 350
cons.addNode('127.0.0.1:7000')
// ['127.0.0.1', '127.0.0.1:4000', '127.0.0.1:5000']
// vnodes count: 100 + 100 + 100 = 300

getRingVNodeCount()

Get the virtual node count from the hash ring.

return {number}: the count of virtual nodes

cons.getRingVNodeCount('127.0.0.1:6000') // vnodes count: 100 + 100 + 100 = 300

License

MIT License © 2022 su9er