flake-id-gen.js
function (cb) {
          /**
           * This callback receives generated id
           * @callback callback
           * @param {Error} error - Error occurred during id generation
           * @param {Buffer} id - Generated id
           */

            var id = new Buffer(8), time = Date.now() - this.epoch;
            id.fill(0);

            // Generates id in the same millisecond as the previous id
            if (time === this.lastTime) {

                // If all sequence values (4096 unique values including 0) have been used
                // to generate ids in the current millisecond (overflow is true) wait till next millisecond
                if (this.overflow) {
                    overflowCond(this, cb);
                    return;
                }

                // Increase sequence counter
                /*jslint bitwise: true */
                this.seq = (this.seq + 1) & this.seqMask;

                // sequence counter exceeded its max value (4095)
                // - set overflow flag and wait till next millisecond
                if (this.seq === 0) {
                    this.overflow = true;
                    overflowCond(this, cb);
                    return;
                }
            } else {
                this.overflow = false;
                this.seq = 0;
            }
            this.lastTime = time;

            id.writeUInt32BE(((time & 0x3) << 22) | this.id | this.seq, 4);
            id.writeUInt8(Math.floor(time / 4) & 0xFF, 4);
            id.writeUInt16BE(Math.floor(time / FlakeId.POW10) & 0xFFFF, 2);
            id.writeUInt16BE(Math.floor(time / FlakeId.POW26) & 0xFFFF, 0);

            if (cb) {
                process.nextTick(cb.bind(null, null, id));
            }
            else {
                return id;
            }
        }
function (cb) {
          /**
           * This callback receives generated id
           * @callback callback
           * @param {Error} error - Error occurred during id generation
           * @param {Buffer} id - Generated id
           */

            var id = new Buffer(8), time = Date.now() - this.epoch;
            id.fill(0);

            // Generates id in the same millisecond as the previous id
            if (time === this.lastTime) {

                // If all sequence values (4096 unique values including 0) have been used
                // to generate ids in the current millisecond (overflow is true) wait till next millisecond
                if (this.overflow) {
                    overflowCond(this, cb);
                    return;
                }

                // Increase sequence counter
                /*jslint bitwise: true */
                this.seq = (this.seq + 1) & this.seqMask;

                // sequence counter exceeded its max value (4095)
                // - set overflow flag and wait till next millisecond
                if (this.seq === 0) {
                    this.overflow = true;
                    overflowCond(this, cb);
                    return;
                }
            } else {
                this.overflow = false;
                this.seq = 0;
            }
            this.lastTime = time;

            id.writeUInt32BE(((time & 0x3) << 22) | this.id | this.seq, 4);
            id.writeUInt8(Math.floor(time / 4) & 0xFF, 4);
            id.writeUInt16BE(Math.floor(time / FlakeId.POW10) & 0xFFFF, 2);
            id.writeUInt16BE(Math.floor(time / FlakeId.POW26) & 0xFFFF, 0);

            if (cb) {
                process.nextTick(cb.bind(null, null, id));
            }
            else {
                return id;
            }
        }
Generates conflice-free id
@param {cb=} callback The callback that handles the response.
@returns {Buffer} Generated id if callback is not provided
@exception if a sequence exceeded its maximum value and a callback function is not provided