UNPKG

uuniq

Version:
185 lines (177 loc) 7.1 kB
"use strict"; 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 __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { Increment: () => Increment, Snowflake: () => Snowflake }); module.exports = __toCommonJS(main_exports); // src/Snowflake.class.ts var import_any_base = __toESM(require("any-base")); var import_lodash = __toESM(require("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 = (0, import_lodash.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.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 = (0, import_any_base.default)(import_any_base.default.DEC, this.options.charset ?? ""); this.anybase_decode = (0, import_any_base.default)(this.options.charset ?? "", import_any_base.default.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 var import_any_base2 = __toESM(require("any-base")); var import_lodash2 = __toESM(require("lodash.merge")); var import_lodash3 = __toESM(require("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 = (0, import_lodash2.default)({}, 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 = (0, import_any_base2.default)(import_any_base2.default.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 = (0, import_lodash3.default)( () => { 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(); }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Increment, Snowflake });