Skip to content

Commit 0641153

Browse files
committed
Removed IPv4 fallback
1 parent 7bffb08 commit 0641153

File tree

4 files changed

+30
-103
lines changed

4 files changed

+30
-103
lines changed

cppsrc/ipaddress.cpp

Lines changed: 0 additions & 55 deletions
This file was deleted.

cppsrc/ipaddress.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

cppsrc/main.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <chrono>
99
#include <thread>
1010

11-
#include "ipaddress.h"
1211
#include "generate_hash.h"
1312

1413
// ////////////////////////////////////////////////////////////////////////////////////////
@@ -42,21 +41,6 @@ constexpr uint64_t maxSequence = (1 << SEQUENCE_BITS) - 1;
4241

4342
// ////////////////////////////////////////////////////////////////////////////////////////
4443

45-
/**
46-
* Covert the macID string passed as a parameter
47-
* into a hash value and return the bitwise & with maxNodeID
48-
* so that the hashed value is always smaller than maxNodeID
49-
* which is 10 bits in size
50-
*/
51-
int nodeID()
52-
{
53-
char ip[16];
54-
getIP(ip);
55-
return generate_hash(ip, 16) & maxNodeId;
56-
}
57-
58-
// ////////////////////////////////////////////////////////////////////////////////////////
59-
6044
uint64_t getCurrentTime()
6145
{
6246
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
@@ -101,16 +85,12 @@ Napi::Object Snowflake::Init(Napi::Env env, Napi::Object exports)
10185
Snowflake::Snowflake(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Snowflake>(info)
10286
{
10387
auto argLen = info.Length();
88+
if (argLen != 2)
89+
Napi::TypeError::New(env, "Expected two arguments.").ThrowAsJavaScriptException();
90+
10491
this->_CUSTOM_EPOCH = info[0].As<Napi::Number>().Int64Value();
105-
switch (argLen)
106-
{
107-
case 2:
108-
this->_nodeID = info[1].As<Napi::Number>().Int32Value() & maxNodeId;
109-
break;
110-
default:
111-
this->_nodeID = nodeID();
112-
break;
113-
}
92+
// If the number is bigger than maxNodeId than those bits will be fall off
93+
this->_nodeID = info[1].As<Napi::Number>().Int32Value() & maxNodeId;
11494

11595
this->_lastTimestamp = 0;
11696
this->_sequence = 0;

src/generateUniqueID.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { Snowflake } = require('../build/Release/snowflake');
22

33
const CUSTOM_EPOCH = 1546300800000; // 01-01-2019
4+
const MAX_MACHINE_ID = (1 << 12) - 1;
45

56
interface Config {
67
customEpoch?: number;
@@ -18,7 +19,7 @@ const initConfig: Config = {
1819
* of a unique 64 bit time sortable id and a method for retreiving
1920
* time of creation for the ids
2021
*
21-
* @param {config} [customEpoch = 1546300800000] A 32 bit long custom epoch
22+
* @param {config} config
2223
* in ms, defaults to 1546300800000 (01-01-2019)
2324
*
2425
* ```
@@ -30,22 +31,22 @@ const initConfig: Config = {
3031
export class UniqueID {
3132
private _CUSTOM_EPOCH: number;
3233
private _snowflake: any;
33-
private _MACHINE_ID?: number;
34+
private _MACHINE_ID: number;
3435
private returnNumber = true;
3536

3637
constructor(config: Config = initConfig) {
3738
this._CUSTOM_EPOCH = config.customEpoch || CUSTOM_EPOCH;
38-
this._MACHINE_ID = config.machineID;
3939
this.returnNumber = !!config.returnNumber;
4040

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-
}
41+
// A 12 bit machine id, if not passed in then a random id will be used
42+
// Ternary operator was used to make sure "0" isn't considered to be falsy.
43+
this._MACHINE_ID = config.machineID !== undefined ? config.machineID : Math.floor(Math.random() * MAX_MACHINE_ID);
4744

48-
this._snowflake = new Snowflake(this._CUSTOM_EPOCH);
45+
// Check if the number is satisfies all the conditions
46+
if (!Number.isInteger(this._MACHINE_ID)) throw Error("Machine Id should be a decimal number");
47+
if (this._MACHINE_ID > MAX_MACHINE_ID) throw Error("Maximum value of machine id can be 2^12 - 1 (4095)")
48+
49+
this._snowflake = new Snowflake(this._CUSTOM_EPOCH, this._MACHINE_ID);
4950
}
5051

5152
/**
@@ -78,7 +79,21 @@ export class UniqueID {
7879
return this._snowflake.getTimestampFromID(id);
7980
}
8081

82+
/**
83+
* Retrieves the 12 bit machine id where the id was generated,
84+
* irrespective of the machine it was generated on.
85+
* @param id
86+
*/
8187
getMachineIDFromID(id: bigint | string): number {
8288
return this._snowflake.getNodeIDFromID(id);
8389
}
90+
91+
/**
92+
* Machine ID of the current machine. This ID is of 12 bit.
93+
* This can be either provided by the user (preferred) or will be assigned
94+
* randomly.
95+
*/
96+
get machineID() {
97+
return this._MACHINE_ID;
98+
}
8499
}

0 commit comments

Comments
 (0)