UNPKG

uuniq

Version:
172 lines (171 loc) 6.26 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); //#region \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let any_base = require("any-base"); any_base = __toESM(any_base); let lodash_merge = require("lodash.merge"); lodash_merge = __toESM(lodash_merge); let lodash_throttle = require("lodash.throttle"); lodash_throttle = __toESM(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 = (0, lodash_merge.default)({}, 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 = (0, any_base.default)(any_base.default.DEC, this.options.charset ?? ""); this.anybase_decode = (0, any_base.default)(this.options.charset ?? "", any_base.default.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 = (0, lodash_merge.default)({}, 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 = (0, any_base.default)(any_base.default.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 = (0, lodash_throttle.default)(() => { 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 exports.Increment = Increment; exports.Snowflake = Snowflake;