nats-io / nuid.js

A Node.js implementation of NUID

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rarely incrementing crypto component of id?

deployable opened this issue · comments

I was having a look through the nats node code and was just wondering what was the reason for the very, very occasional regeneration of the crypto component of the ID?

Nuid.prototype.next = function() {
  this.seq += this.inc;
  if (this.seq > maxSeq) {
    this.setPre();
  }
  this.initSeqAndInc();
  this.fillSeq();
  return (this.buf.toString('ascii'));
};

I saw some reference to the generation in an old issue on node-nats where the generation was moving away from Math.random but it appears to still largely rely on Math.random, except for the very small chance of a number between 33-300 from the largest number being randomly generated when the code will trigger a new crypto prefix.

I guess this.initSeqAndInc() could be in the above if so you have an initial random suffix incrementing by a set random amount until the next random prefix is generated?

Yes, it should be the go implementation has it in the if as well.
Thanks for the spot.

@deployable Thanks for the report!