UNPKG

uuniq

Version:
73 lines (70 loc) 2.67 kB
// src/Snowflake.class.ts import _ from "lodash"; // src/defaults/SnowflakeOptions.default.ts var SnowflakeOptionsDefault = { 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 = Object.keys(parts2); for (const key of keys) limits2[key] = Math.pow(2, parts2[key]) - 1; return limits2; }; var calculateShifts = (parts2) => { const shifts2 = {}; let shift = 0; const keys = ["sequence", "place_id", "timestamp"]; for (const key of keys) { shifts2[key] = shift; shift += parts2[key]; } return shifts2; }; var limits = calculateLimits(parts); var shifts = calculateShifts(parts); var Snowflake = class { constructor(options = SnowflakeOptionsDefault) { this.options = _.merge({}, SnowflakeOptionsDefault, options); this.epoch = this.options.epoch instanceof Date ? this.options.epoch.getTime() : typeof this.options.epoch === "string" || typeof this.options.epoch === "number" ? new Date(this.options.epoch).getTime() : (/* @__PURE__ */ new Date("2025-01-01T00:00:00.000Z")).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 ${limits.place_id}`); this.options.place_id = (this.options.place_id ?? 0) & limits.place_id; this.sequence = 0; this.last_timestamp = -1; } currentTimestamp() { return Date.now() - this.epoch; } waitForNextTime(last_timestamp) { let timestamp = this.currentTimestamp(); while (last_timestamp >= timestamp) timestamp = this.currentTimestamp(); return timestamp; } generate() { let timestamp = this.currentTimestamp(); if (timestamp < this.last_timestamp) throw new Error("Clock moved backwards."); if (timestamp === this.last_timestamp) { this.sequence = this.sequence + 1 & limits.sequence; if (this.sequence === 0) timestamp = this.waitForNextTime(this.last_timestamp); } else this.sequence = 0; this.last_timestamp = timestamp; return (BigInt(timestamp) << BigInt(shifts.timestamp) | BigInt(this.options.place_id ?? 0) << BigInt(shifts.place_id) | BigInt(this.sequence)).toString(); } resolve(id) { const bigint_id = BigInt(id); return { created_at: new Date(this.epoch + Number(bigint_id >> BigInt(shifts.timestamp) & BigInt(limits.timestamp))).toISOString(), place_id: Number(bigint_id >> BigInt(shifts.place_id) & BigInt(limits.place_id)), sequence: Number(bigint_id & BigInt(limits.sequence)) }; } }; export { Snowflake };