UNPKG

@eristack/entity-id

Version:

UUID v7 entity identifiers — sortable, parseable, Drizzle column helper

103 lines (96 loc) • 3.31 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/zod/index.ts var zod_exports = {}; __export(zod_exports, { entityIdSchema: () => entityIdSchema }); module.exports = __toCommonJS(zod_exports); // src/zod/schemas.ts var import_zod = require("zod"); // src/core/errors.ts var ENTITY_ID_PARSE_CODE = "ENTITY_ID_PARSE"; var EntityIdParseError = class extends Error { code = ENTITY_ID_PARSE_CODE; constructor(message) { super(message); this.name = "EntityIdParseError"; } }; // src/core/types.ts var ENTITY_ID_VERSION = 7; // src/core/internal.ts var HYPHENATED = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; function canonicalUuidToBytes(id) { const compact = id.replace(/-/g, "").toLowerCase(); if (compact.length !== 32 || !/^[0-9a-f]+$/.test(compact)) { throw new EntityIdParseError("Entity id must be 32 hex digits (optional hyphens)"); } const bytes = new Uint8Array(16); for (let i = 0; i < 16; i++) { bytes[i] = Number.parseInt(compact.slice(i * 2, i * 2 + 2), 16); } return bytes; } function assertEntityIdV7(bytes) { const version = bytes[6] >> 4 & 15; if (version !== ENTITY_ID_VERSION) { throw new EntityIdParseError( `Entity id must be UUID version ${ENTITY_ID_VERSION}, got version ${version}` ); } const variant = bytes[8] >> 6 & 3; if (variant !== 2) { throw new EntityIdParseError("Entity id must use RFC 4122 variant (10xxxxxx)"); } } function isHyphenatedUuidLower(value) { return HYPHENATED.test(value); } // src/core/entity-id.ts function normalizeEntityId(value) { const trimmed = value.trim().toLowerCase(); const canonical = trimmed.includes("-") ? trimmed : `${trimmed.slice(0, 8)}-${trimmed.slice(8, 12)}-${trimmed.slice(12, 16)}-${trimmed.slice(16, 20)}-${trimmed.slice(20)}`; if (!isHyphenatedUuidLower(canonical)) { throw new EntityIdParseError("Entity id must be a lowercase UUID string"); } const bytes = canonicalUuidToBytes(canonical); assertEntityIdV7(bytes); return canonical; } function parseEntityId(value) { return normalizeEntityId(value); } // src/zod/schemas.ts var entityIdSchema = import_zod.z.string().transform((value, ctx) => { try { return parseEntityId(value); } catch (err) { ctx.addIssue({ code: "custom", message: err instanceof Error ? err.message : "Invalid entity id" }); return import_zod.z.NEVER; } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { entityIdSchema }); //# sourceMappingURL=index.cjs.map