fast-snowflake-id
Advanced tools
Comparing version
@@ -1,17 +0,2 @@ | ||
export interface SnowflakeIdOptions { | ||
epoch?: number; | ||
instanceId?: number; | ||
overflowHandler?: () => void; | ||
} | ||
export class SnowflakeId { | ||
private id: ArrayBuffer; | ||
private idView: DataView; | ||
private lastTime: number; | ||
private epoch: number; | ||
private sequenceId: number; | ||
private instanceId: number; | ||
private isOverflowHandlerFunction: boolean; | ||
private overflowHandler: (() => void) | undefined; | ||
export default class SnowflakeId { | ||
public static getInstanceId(datacenterId: number, workerId: number): number; | ||
@@ -21,8 +6,5 @@ | ||
private generateId(): SnowflakeId; | ||
public generateId(): SnowflakeId; | ||
public getId(): BigInt; | ||
public getBufferId(): Buffer; | ||
} | ||
export default SnowflakeId; | ||
} |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SnowflakeId=void 0;const crypto_1=require("crypto");class SnowflakeId{constructor(e={}){if(this.id=new ArrayBuffer(8),this.idView=new DataView(this.id),this.lastTime=0,this.epoch=0,this.sequenceId=0,this.instanceId=0,this.isOverflowHandlerFunction=!1,Number.isInteger(e.epoch)){if(!(e.epoch<=Date.now()))throw new Error("Options['epoch'] should not exceed current time");this.epoch=e.epoch}if(Number.isInteger(e.instanceId)){if(!(e.instanceId>-1&&e.instanceId<1024))throw new Error("Options['instanceId'] should be an integer between 0 and 1023");this.instanceId=e.instanceId}else this.instanceId=(0,crypto_1.randomInt)(1024);"function"==typeof e.overflowHandler&&(this.overflowHandler=e.overflowHandler,this.isOverflowHandlerFunction=!0),this.instanceId<<=12}generateId(){if(4096!==this.sequenceId){const e=Date.now()-this.epoch;this.lastTime!==e&&(this.sequenceId=0,this.lastTime=e),this.idView.setUint32(0,Math.trunc(e/1024)),this.idView.setUint32(4,(e<<22)+this.instanceId+this.sequenceId++)}else{if(!this.isOverflowHandlerFunction)throw new Error("SnowflakeId['sequenceId'] should not exceed 4095 in given millisecond");this.overflowHandler()}return this}getId(){return this.generateId(),this.idView.getBigUint64(0)}getBufferId(){return this.generateId(),Buffer.from(this.id)}}exports.SnowflakeId=SnowflakeId,exports.default=SnowflakeId; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const crypto_1=require("crypto");class SnowflakeId{static getInstanceId(e,t){if(e>-1&&e<32){if(t>-1&&t<32)return e<<5+t;throw new Error("WorkerId must be an integer between 0 and 32")}throw new Error("DatacenterId must be an integer between 0 and 32")}constructor(e={}){if(this._id=new ArrayBuffer(8),this._idView=new DataView(this._id),this._lastTime=0,this._epoch=0,this._sequenceId=0,this._instanceId=0,Number.isInteger(e.epoch)){if(!(e.epoch<=Date.now()))throw new Error("Options['epoch'] must not exceed current time");this._epoch=e.epoch}if(Number.isInteger(e.instanceId)){if(!(e.instanceId>-1&&e.instanceId<1024))throw new Error("Options['instanceId'] must be an integer between 0 and 1023");this._instanceId=e.instanceId}else this._instanceId=(0,crypto_1.randomInt)(1024);"function"==typeof e.overflowHandler&&(this._overflowHandler=e.overflowHandler),this._instanceId<<=12}generateId(){if(4096!==this._sequenceId){const e=Date.now()-this._epoch;this._lastTime!==e&&(this._sequenceId=0,this._lastTime=e),this._idView.setUint32(0,Math.trunc(e/1024)),this._idView.setUint32(4,(e<<22)+this._instanceId+this._sequenceId++)}else{if("function"!=typeof this._overflowHandler)throw new Error("SnowflakeId['sequenceId'] must not exceed 4095 in given millisecond");this._overflowHandler()}return this}getId(){return this._idView.getBigUint64(0)}getBufferId(){return Buffer.from(this._id)}}exports.default=SnowflakeId; |
{ | ||
"name": "fast-snowflake-id", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Fast snowflake id generator for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "library/index.js", |
@@ -0,0 +0,0 @@ <div align='center'> |
Sorry, the diff of this file is not supported yet
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
5432
-3.59%6
-70%1
Infinity%