snowflake-based invite generation
This commit is contained in:
parent
f76b6e4bff
commit
73c5141e05
@ -1,3 +1,5 @@
|
|||||||
|
import { Snowflake } from "@fosscord/util";
|
||||||
|
|
||||||
export function random(length = 6) {
|
export function random(length = 6) {
|
||||||
// Declare all characters
|
// Declare all characters
|
||||||
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
@ -10,3 +12,20 @@ export function random(length = 6) {
|
|||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function snowflakeBasedInvite() {
|
||||||
|
// Declare all characters
|
||||||
|
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
|
||||||
|
let snowflake = Snowflake.generateWorkerProcess();
|
||||||
|
|
||||||
|
// snowflakes hold ~10.75 characters worth of entropy;
|
||||||
|
// safe to generate a 8-char invite out of them
|
||||||
|
let str = "";
|
||||||
|
for (let i=0; i < 10; i++) {
|
||||||
|
str += chars.charAt((snowflake % chars.length));
|
||||||
|
snowflake /= chars.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str.substr(3,8).reverse(); // little-endianise for entropy
|
||||||
|
}
|
||||||
|
@ -21,7 +21,7 @@ export class Snowflake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Twitter snowflake, except the epoch is 2015-01-01T00:00:00.000Z
|
* A Twitter-like snowflake, except the epoch is 2015-01-01T00:00:00.000Z
|
||||||
* ```
|
* ```
|
||||||
* If we have a snowflake '266241948824764416' we can represent it as binary:
|
* If we have a snowflake '266241948824764416' we can represent it as binary:
|
||||||
*
|
*
|
||||||
@ -83,14 +83,17 @@ export class Snowflake {
|
|||||||
return dec;
|
return dec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static generate() {
|
static generateWorkerProcess() { // worker process - returns a number
|
||||||
var time = BigInt(Date.now() - Snowflake.EPOCH) << BigInt(22);
|
var time = BigInt(Date.now() - Snowflake.EPOCH) << BigInt(22);
|
||||||
var worker = Snowflake.workerId << 17n;
|
var worker = Snowflake.workerId << 17n;
|
||||||
var process = Snowflake.processId << 12n;
|
var process = Snowflake.processId << 12n;
|
||||||
var increment = Snowflake.INCREMENT++;
|
var increment = Snowflake.INCREMENT++;
|
||||||
return (time | worker | process | increment).toString();
|
return BigInt(time | worker | process | increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static generate() {
|
||||||
|
return Snowflake.generateWorkerProcess().toString();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* A deconstructed snowflake.
|
* A deconstructed snowflake.
|
||||||
* @typedef {Object} DeconstructedSnowflake
|
* @typedef {Object} DeconstructedSnowflake
|
||||||
|
Loading…
x
Reference in New Issue
Block a user