import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url);
var sessionResourceCleanups=new Set;function registerSessionResourceCleanup(cleanup){return sessionResourceCleanups.add(cleanup),()=>{sessionResourceCleanups.delete(cleanup)}}function cleanupSessionResources(sessionId){let errors=[];for(let cleanup of sessionResourceCleanups)try{cleanup(sessionId)}catch(error){errors.push(error)}if(errors.length>0)throw new AggregateError(errors,"Failed to cleanup session resources")}var lastTimestamp=-1/0,sequence=0;function fillRandomBytes(bytes){if(globalThis.crypto?.getRandomValues){globalThis.crypto.getRandomValues(bytes);return}for(let i=0;i<bytes.length;i++)bytes[i]=Math.floor(Math.random()*256)}function uuidv7(){let random=new Uint8Array(16);fillRandomBytes(random);let timestamp=Date.now();timestamp>lastTimestamp?(sequence=random[6]*16777216+random[7]*65536+random[8]*256+random[9],lastTimestamp=timestamp):(sequence=sequence+1>>>0,sequence===0&&lastTimestamp++);let bytes=new Uint8Array(16);bytes[0]=lastTimestamp/1099511627776&255,bytes[1]=lastTimestamp/4294967296&255,bytes[2]=lastTimestamp/16777216&255,bytes[3]=lastTimestamp/65536&255,bytes[4]=lastTimestamp/256&255,bytes[5]=lastTimestamp&255,bytes[6]=112|sequence>>>28&15,bytes[7]=sequence>>>20&255,bytes[8]=128|sequence>>>14&63,bytes[9]=sequence>>>6&255,bytes[10]=(sequence&63)<<2|random[10]&3,bytes[11]=random[11],bytes[12]=random[12],bytes[13]=random[13],bytes[14]=random[14],bytes[15]=random[15];let hex=Array.from(bytes,byte=>byte.toString(16).padStart(2,"0"));return`${hex.slice(0,4).join("")}-${hex.slice(4,6).join("")}-${hex.slice(6,8).join("")}-${hex.slice(8,10).join("")}-${hex.slice(10,16).join("")}`}export{registerSessionResourceCleanup,cleanupSessionResources,uuidv7};
