uuniq
Version:
Short yet unique IDs.
145 lines (144 loc) • 4.92 kB
JavaScript
import Anybase from "any-base";
import merge from "lodash.merge";
import throttle from "lodash.throttle";
//#region src/defaults/snowflake_options.ts
const SnowflakeOptionsDefault = {
format: "numeric",
charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
epoch: "2025-01-01T00:00:00.000Z",
place_id: 0
};
//#endregion
//#region src/snowflake.ts
const parts = {
timestamp: 53,
place_id: 4,
sequence: 10
};
const calculate_limits = (parts) => {
const limits = {};
for (const key of [
"sequence",
"place_id",
"timestamp"
]) limits[key] = (BigInt(1) << BigInt(parts[key])) - BigInt(1);
return limits;
};
const calculate_shifts = (parts) => {
const shifts = {};
const keys = [
"sequence",
"place_id",
"timestamp"
];
let shift = 0;
for (const key of keys) {
shifts[key] = shift;
shift += parts[key];
}
return shifts;
};
const limits = calculate_limits(parts);
const shifts = calculate_shifts(parts);
const place_ids_used$1 = /* @__PURE__ */ new Set();
var Snowflake = class {
options;
epoch;
sequence;
last_timestamp;
anybase_encode;
anybase_decode;
constructor(options = SnowflakeOptionsDefault) {
this.options = merge({}, SnowflakeOptionsDefault, options);
this.epoch = new Date(this.options.epoch ?? "").getTime();
if ((this.options.place_id ?? 0) < 0 || (this.options.place_id ?? 0) > limits.place_id) throw new Error(`Field place_id must be between 0 and ${String(limits.place_id)}`);
this.options.place_id = (this.options.place_id ?? 0) & Number(limits.place_id);
if (place_ids_used$1.has(this.options.place_id)) throw new Error(`Place ID ${String(this.options.place_id)} already in use`);
place_ids_used$1.add(this.options.place_id);
this.sequence = 0;
this.last_timestamp = -1;
this.anybase_encode = Anybase(Anybase.DEC, this.options.charset ?? "");
this.anybase_decode = Anybase(this.options.charset ?? "", Anybase.DEC);
}
current_timestamp() {
return Date.now() - this.epoch;
}
wait_for_next_time(last_timestamp) {
let current_timestamp = this.current_timestamp();
while (last_timestamp >= current_timestamp) current_timestamp = this.current_timestamp();
return current_timestamp;
}
generate() {
let current_timestamp = this.current_timestamp();
if (current_timestamp < this.last_timestamp) throw new Error("Clock moved backwards");
if (current_timestamp === this.last_timestamp) {
this.sequence = this.sequence + 1 & Number(limits.sequence);
if (this.sequence === 0) current_timestamp = this.wait_for_next_time(this.last_timestamp);
} else this.sequence = 0;
this.last_timestamp = current_timestamp;
let id = String(BigInt(current_timestamp) << BigInt(shifts.timestamp) | BigInt(this.options.place_id ?? 0) << BigInt(shifts.place_id) | BigInt(this.sequence));
if (this.options.format === "symbolic") id = this.anybase_encode(id);
return id;
}
resolve(id) {
if (this.options.format === "symbolic") id = this.anybase_decode(id);
const bigint_id = BigInt(id);
return {
created_at: new Date(this.epoch + Number(bigint_id >> BigInt(shifts.timestamp) & limits.timestamp)).toISOString(),
place_id: Number(bigint_id >> BigInt(shifts.place_id) & limits.place_id),
sequence: Number(bigint_id & limits.sequence)
};
}
};
//#endregion
//#region src/defaults/increment_options.ts
const IncrementOptionsDefault = {
format: "numeric",
initial: 10000001,
charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
place_id: 0,
store: /* @__PURE__ */ new Map()
};
//#endregion
//#region src/increment.ts
const place_ids_used = /* @__PURE__ */ new Set();
var Increment = class {
options;
store;
sequence = null;
anybase_encode;
constructor(options) {
this.options = merge({}, IncrementOptionsDefault, options);
this.options.place_id = this.options.place_id ?? 0;
if (place_ids_used.has(this.options.place_id)) throw new Error(`Place ID ${String(this.options.place_id)} already in use`);
place_ids_used.add(this.options.place_id);
this.store = options.store;
this.anybase_encode = Anybase(Anybase.DEC, this.options.charset ?? "");
this.init();
}
async init() {
this.sequence = await this.store.get(`increment_sequence--place_id:${String(this.options.place_id)}`) ?? (this.options.initial !== void 0 ? this.options.initial - 1 : 0);
}
sync_sequence = throttle(() => {
if (this.sequence === null) return;
this.store.set(`increment_sequence--place_id:${String(this.options.place_id)}`, this.sequence);
}, 1e3, {
leading: true,
trailing: true
});
generate() {
return new Promise((resolve) => {
const wait = () => {
if (this.sequence === null) return setTimeout(() => wait(), 1e3);
this.sequence++;
this.sync_sequence();
let id = String(this.sequence);
if (this.options.format === "symbolic") id = this.anybase_encode(id);
resolve(id);
};
wait();
});
}
};
//#endregion
export { Increment, Snowflake };