-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce sunflake generation * Update README to reflect Licensing
- Loading branch information
Showing
9 changed files
with
277 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"singleQuote": true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
{ | ||
"files.exclude": { | ||
"lib": true, | ||
"yarn.lock": true, | ||
"yarn-error.log": true, | ||
".prettierrc": true, | ||
".gitignore": true, | ||
"node_modules": true, | ||
".vscode": true, | ||
".github": false, | ||
"coverage": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Adds two arrays for the given base (10 or 16), returning the result. | ||
// This turns out to be the only "primitive" operation we need. | ||
const add = (x: number[], y: number[], base: number) => { | ||
let z = []; | ||
let n = Math.max(x.length, y.length); | ||
let carry = 0; | ||
let i = 0; | ||
while (i < n || carry) { | ||
let xi = i < x.length ? x[i] : 0; | ||
let yi = i < y.length ? y[i] : 0; | ||
let zi = carry + xi + yi; | ||
z.push(zi % base); | ||
carry = Math.floor(zi / base); | ||
i++; | ||
} | ||
|
||
return z; | ||
}; | ||
|
||
// Returns a*x, where x is an array of decimal digits and a is an ordinary | ||
// JavaScript number. base is the number base of the array x. | ||
const multiplyByNumber = (num: number, x: number[], base: number): number[] => { | ||
if (num == 0 || num < 0) return []; | ||
|
||
let result: number[] = []; | ||
let power = x; | ||
|
||
while (num !== 0) { | ||
if (num & 1) { | ||
result = add(result, power, base); | ||
} | ||
|
||
num >>= 1; | ||
|
||
if (num === 0) break; | ||
|
||
power = add(power, power, base); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
const parseToDigitsArray = (str: string, base: number) => { | ||
let digits = str.split(''); | ||
let ary = []; | ||
for (let i = digits.length - 1; i >= 0; i--) { | ||
let n = parseInt(digits[i], base); | ||
|
||
if (isNaN(n)) return null; | ||
|
||
ary.push(n); | ||
} | ||
|
||
return ary; | ||
}; | ||
|
||
const convertBase = (str: string, fromBase: number, toBase: number) => { | ||
let digits = parseToDigitsArray(str, fromBase); | ||
|
||
if (digits === null) return null; | ||
|
||
let outArray: number[] = []; | ||
let power = [1]; | ||
for (let i = 0; i < digits.length; i++) { | ||
// inletiant: at this point, fromBase^i = power | ||
if (digits[i]) { | ||
outArray = add( | ||
outArray, | ||
multiplyByNumber(digits[i] as number, power, toBase), | ||
toBase | ||
); | ||
} | ||
|
||
power = multiplyByNumber(fromBase, power, toBase); | ||
} | ||
|
||
let out = ''; | ||
for (let i = outArray.length - 1; i >= 0; i--) { | ||
out += outArray[i].toString(toBase); | ||
} | ||
|
||
return out; | ||
}; | ||
|
||
export const hexToDec = (hexStr: string) => { | ||
if (hexStr.startsWith('0x')) hexStr = hexStr.substring(2); | ||
|
||
hexStr = hexStr.toLowerCase(); | ||
|
||
return convertBase(hexStr, 16, 10); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
export const Sunflake = {}; | ||
import { hexToDec } from './hex2dec'; | ||
|
||
let lastTime: number = 0; | ||
let seq: number = 0; | ||
|
||
export type SunflakeConfig = { | ||
machineID: number; | ||
epoch: number; | ||
time?: number; | ||
}; | ||
|
||
export const generateSunflake = async (config: SunflakeConfig) => { | ||
let { machineID, epoch, time } = Object.assign< | ||
SunflakeConfig, | ||
Partial<SunflakeConfig> | ||
>( | ||
{ | ||
epoch: 0, | ||
machineID: 0, | ||
time: 1, | ||
}, | ||
config | ||
) as Required<SunflakeConfig>; | ||
|
||
lastTime = time; | ||
machineID = machineID % 1023; | ||
|
||
const bTime = (time - epoch).toString(2); | ||
|
||
// Get the sequence number | ||
if (lastTime == time) { | ||
seq++; | ||
|
||
if (seq > 4095) { | ||
seq = 0; | ||
|
||
// Make system wait till time is been shifted by one millisecond | ||
while (Date.now() <= time) { | ||
await new Promise<void>((acc) => setImmediate(acc)); | ||
} | ||
} | ||
} else { | ||
seq = 0; | ||
} | ||
|
||
lastTime = time; | ||
|
||
let bSeq = seq.toString(2); | ||
let bMid = machineID.toString(2); | ||
|
||
// Create sequence binary bit | ||
while (bSeq.length < 12) bSeq = '0' + bSeq; | ||
while (bMid.length < 10) bMid = '0' + bMid; | ||
|
||
const bid = bTime + bMid + bSeq; | ||
|
||
let id = ''; | ||
for (let i = bid.length; i > 0; i -= 4) { | ||
id = parseInt(bid.substring(i - 4, i), 2).toString(16) + id; | ||
} | ||
|
||
return hexToDec(id); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { generateSunflake } from '../src'; | ||
|
||
const EPOCH: number = 1640988001000; // First second of 2022 | ||
|
||
|
||
it('Exports Sunflake', () => { | ||
expect(generateSunflake); | ||
}); | ||
|
||
it('Generates two snowflake value', async () => { | ||
const flake1 = await generateSunflake({ machineID: 1, epoch: EPOCH }); | ||
const flake2 = await generateSunflake({ machineID: 1, epoch: EPOCH }); | ||
|
||
expect(flake1 != flake2); | ||
}); | ||
|
||
it('Generates two snowflake value in sync', async () => { | ||
const [flake1, flake2] = await Promise.all([ | ||
generateSunflake({ machineID: 1, epoch: EPOCH }), | ||
generateSunflake({ machineID: 1, epoch: EPOCH }), | ||
]); | ||
|
||
expect(flake1 != flake2); | ||
}); | ||
|
||
it('Generates two snowflake value in sync with same time', async () => { | ||
const time = Date.now(); | ||
const [flake1, flake2] = await Promise.all([ | ||
generateSunflake({ machineID: 1, epoch: EPOCH, time }), | ||
generateSunflake({ machineID: 1, epoch: EPOCH, time }), | ||
]); | ||
|
||
expect(flake1 !== flake2); | ||
}); | ||
|
||
it('Generates 500 snowflake value in sync with same time', async () => { | ||
const time = Date.now(); | ||
const hugeList = []; | ||
for (let i = 0; i <= 500; i++) { | ||
hugeList.push(generateSunflake({ machineID: 1, epoch: EPOCH, time })); | ||
} | ||
|
||
const list = await Promise.all(hugeList); | ||
|
||
expect(new Set(list).size === list.length); | ||
}); | ||
|
||
it('Generates 5200 snowflake value in sync with same time', async () => { | ||
const time = Date.now(); | ||
const hugeList = []; | ||
for (let i = 0; i <= 5200; i++) { | ||
hugeList.push(generateSunflake({ machineID: 1, epoch: EPOCH, time })); | ||
} | ||
|
||
const list = await Promise.all(hugeList); | ||
|
||
expect(new Set(list).size === list.length); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters