Skip to content

Commit 24867aa

Browse files
committed
Removed macid and added feature to retrieve the machine id from the generated ids, irrespective of the machine the id was generated on
1 parent c737e99 commit 24867aa

12 files changed

+80
-115
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.vscode
3-
build
3+
build
4+
log

lib/generateUniqueID.d.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
interface Config {
22
customEpoch?: number;
33
returnNumber?: boolean;
4+
machineID?: number;
45
}
56
/**
67
* Constructs a UniqueID object which stores method for generation
78
* of a unique 64 bit time sortable id and a method for retreiving
89
* time of creation for the ids
910
*
10-
* @param {config} [customEpoch = 1546300800000] A 42 bit long custom epoch
11+
* @param {config} [customEpoch = 1546300800000] A 32 bit long custom epoch
1112
* in ms, defaults to 1546300800000 (01-01-2019)
1213
*
1314
* ```
1415
* const uid = new UniqueID();
1516
* const ID = uid.getUniqueID();
1617
* const IDCreateAt = uid.getTimestampFromID(ID);
17-
* const currentMacID = uid.getMacID();
1818
* ```
1919
*/
2020
export declare class UniqueID {
2121
private _CUSTOM_EPOCH;
22-
private _MACID;
23-
private _FORMATTEDMACID;
2422
private _snowflake;
25-
private _nextID;
23+
private _MACHINE_ID?;
24+
private returnNumber;
2625
constructor(config?: Config);
2726
/**
2827
* Generates a unique time sortable 64 bit number using native code
@@ -42,10 +41,7 @@ export declare class UniqueID {
4241
* @returns {number} timestamp of id creations
4342
*/
4443
getTimestampFromID(id: bigint | string): number;
45-
/**
46-
* @returns MAC address being used internally
47-
*/
48-
get macID(): string;
44+
getMachineIDFromID(id: bigint | string): number;
4945
}
5046
export {};
5147
//# sourceMappingURL=generateUniqueID.d.ts.map

lib/generateUniqueID.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

lib/generateUniqueID.js

+21-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/generateUniqueID.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test.js

+13-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-snowflake",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "Generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/generateUniqueID.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const { Snowflake } = require('../build/Release/snowflake');
2-
import getMacID from './getMacID';
32

43
const CUSTOM_EPOCH = 1546300800000; // 01-01-2019
54

65
interface Config {
76
customEpoch?: number;
8-
returnNumber?: boolean
7+
returnNumber?: boolean;
8+
machineID?: number;
99
}
1010

1111
const initConfig: Config = {
@@ -18,41 +18,45 @@ const initConfig: Config = {
1818
* of a unique 64 bit time sortable id and a method for retreiving
1919
* time of creation for the ids
2020
*
21-
* @param {config} [customEpoch = 1546300800000] A 42 bit long custom epoch
21+
* @param {config} [customEpoch = 1546300800000] A 32 bit long custom epoch
2222
* in ms, defaults to 1546300800000 (01-01-2019)
2323
*
2424
* ```
2525
* const uid = new UniqueID();
2626
* const ID = uid.getUniqueID();
2727
* const IDCreateAt = uid.getTimestampFromID(ID);
28-
* const currentMacID = uid.getMacID();
2928
* ```
3029
*/
3130
export class UniqueID {
3231
private _CUSTOM_EPOCH: number;
33-
private _MACID: string = '';
34-
private _FORMATTEDMACID: string = '';
3532
private _snowflake: any;
36-
private _nextID: Function;
33+
private _MACHINE_ID?: number;
34+
private returnNumber = true;
3735

3836
constructor(config: Config = initConfig) {
3937
this._CUSTOM_EPOCH = config.customEpoch || CUSTOM_EPOCH;
40-
const { macIDString, macID } = getMacID();
41-
this._MACID = macIDString;
42-
this._FORMATTEDMACID = macID;
43-
if (!this._MACID) throw new Error('No MAC ADDRESS found to initialise');
44-
this._snowflake = new Snowflake(this._MACID, this._CUSTOM_EPOCH);
45-
this._nextID = config.returnNumber
46-
? () => this._snowflake.getUniqueIDBigInt()
47-
: () => this._snowflake.getUniqueID()
38+
this._MACHINE_ID = config.machineID;
39+
this.returnNumber = !!config.returnNumber;
40+
41+
if ((this._MACHINE_ID !== undefined) && !isNaN(this._MACHINE_ID)) {
42+
if (!Number.isInteger(this._MACHINE_ID)) throw Error("Machine Id should be a decimal number");
43+
if (this._MACHINE_ID > (1 << 12) - 1) throw Error("Maximum value of machine id can be 2^12 - 1 (4095)")
44+
this._snowflake = new Snowflake(this._CUSTOM_EPOCH, this._MACHINE_ID);
45+
return;
46+
}
47+
48+
this._snowflake = new Snowflake(this._CUSTOM_EPOCH);
4849
}
4950

5051
/**
5152
* Generates a unique time sortable 64 bit number using native code
5253
* @returns {string | bigint} the unique id
5354
*/
5455
getUniqueID(): string | bigint {
55-
return this._nextID();
56+
const val = this._snowflake.getUniqueID()
57+
if (!this.returnNumber)
58+
return val.toString();
59+
return val
5660
}
5761

5862
/**
@@ -71,13 +75,10 @@ export class UniqueID {
7175
* @returns {number} timestamp of id creations
7276
*/
7377
getTimestampFromID(id: bigint | string): number {
74-
return this._snowflake.getTimestampFromID(id) + this._CUSTOM_EPOCH;
78+
return this._snowflake.getTimestampFromID(id);
7579
}
7680

77-
/**
78-
* @returns MAC address being used internally
79-
*/
80-
get macID(): string {
81-
return this._FORMATTEDMACID;
81+
getMachineIDFromID(id: bigint | string): number {
82+
return this._snowflake.getNodeIDFromID(id);
8283
}
8384
}

src/getMacID.ts

-40
This file was deleted.

src/test.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import UniqueID from './index';
22
import NanoTimer from 'nanotimer';
33
import parser from 'minimist';
44

5-
const { type, time, interval, v: verbose } = parser(process.argv.slice(2));
5+
const { type, time, interval, v: verbose, metadata, demoonly } = parser(process.argv.slice(2));
66
const timer = new NanoTimer();
77
const uid = new UniqueID({
8-
returnNumber: type === "number" || type === "num"
8+
returnNumber: type === "number" || type === "num",
9+
machineID: Number(process.env.MACHINE_ID)
910
});
1011

1112
const benchmark = (totalTime: string, Function: Function) => {
1213
console.log('[STARTING]')
1314
let times = 0;
1415
timer.setInterval(() => {
15-
times++;
16+
times += 1;
1617
Function();
1718
}, '', interval || '1u');
1819
timer.setTimeout((timer: any) => {
@@ -26,4 +27,10 @@ const benchmarkFunction = () => {
2627
else return () => uid.getUniqueID()
2728
}
2829

29-
benchmark(time || '1s', benchmarkFunction());
30+
if (demoonly) {
31+
console.log(uid.getUniqueID())
32+
} else if (metadata) {
33+
console.log(uid.getMachineIDFromID(metadata), uid.getTimestampFromID(metadata))
34+
} else {
35+
benchmark(time || '1s', benchmarkFunction());
36+
}

0 commit comments

Comments
 (0)