uuniq
Version:
Short yet unique IDs.
147 lines (141 loc) • 5.18 kB
JavaScript
// src/Snowflake.class.ts
import Anybase from "any-base";
import merge from "lodash.merge";
// src/defaults/SnowflakeOptions.default.ts
var SnowflakeOptionsDefault = {
format: "numeric",
charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
epoch: "2025-01-01T00:00:00.000Z",
place_id: 0
};
// src/Snowflake.class.ts
var parts = {
timestamp: 53,
place_id: 4,
sequence: 10
};
var calculateLimits = (parts2) => {
const limits2 = {};
const keys = ["sequence", "place_id", "timestamp"];
for (const key of keys) limits2[key] = (BigInt(1) << BigInt(parts2[key])) - BigInt(1);
return limits2;
};
var calculateShifts = (parts2) => {
const shifts2 = {};
const keys = ["sequence", "place_id", "timestamp"];
let shift = 0;
for (const key of keys) {
shifts2[key] = shift;
shift += parts2[key];
}
return shifts2;
};
var limits = calculateLimits(parts);
var shifts = calculateShifts(parts);
var place_ids_used = /* @__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.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.sequence = 0;
this.last_timestamp = -1;
this.anybase_encode = Anybase(Anybase.DEC, this.options.charset ?? "");
this.anybase_decode = Anybase(this.options.charset ?? "", Anybase.DEC);
}
currentTimestamp() {
return Date.now() - this.epoch;
}
waitForNextTime(last_timestamp) {
let current_timestamp = this.currentTimestamp();
while (last_timestamp >= current_timestamp) current_timestamp = this.currentTimestamp();
return current_timestamp;
}
generate() {
let current_timestamp = this.currentTimestamp();
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.waitForNextTime(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)
};
}
};
// src/Increment.class.ts
import Anybase2 from "any-base";
import merge2 from "lodash.merge";
import throttle from "lodash.throttle";
// src/defaults/IncrementOptions.default.ts
var IncrementOptionsDefault = {
format: "numeric",
initial: 10000001,
charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
place_id: 0,
store: /* @__PURE__ */ new Map()
};
// src/Increment.class.ts
var place_ids_used2 = /* @__PURE__ */ new Set();
var Increment = class {
options;
store;
sequence = null;
anybase_encode;
constructor(options) {
this.options = merge2({}, IncrementOptionsDefault, options);
this.options.place_id = this.options.place_id ?? 0;
if (place_ids_used2.has(this.options.place_id)) throw new Error(`Place ID ${String(this.options.place_id)} already in use`);
place_ids_used2.add(this.options.place_id);
this.store = options.store;
this.anybase_encode = Anybase2(Anybase2.DEC, this.options.charset ?? "");
void this.initial();
}
async initial() {
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);
}
syncSequence = throttle(
() => {
if (this.sequence === null) return;
void 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.syncSequence();
let id = String(this.sequence);
if (this.options.format === "symbolic") id = this.anybase_encode(id);
resolve(id);
};
wait();
});
}
};
export {
Increment,
Snowflake
};