{"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import md5 from \"blueimp-md5\";\n\nimport type { SipInfo } from \"./types.js\";\n\n// Private counter kept alive in the closure\nlet counter = 0;\nexport const uuid = () => {\n  // 1. Increment counter safely (reset if it gets dangerously high, though unlikely)\n  counter = (counter + 1) % Number.MAX_SAFE_INTEGER;\n\n  // 2. Get current time in Base36 (alphanumeric)\n  const timePart = Date.now().toString(36);\n\n  // 3. Get the counter in Base36\n  const countPart = counter.toString(36);\n\n  // 4. Add a bit of Math.random for good measure (Base36, stripping '0.')\n  const randomPart = Math.random().toString(36).substring(2, 8);\n\n  // Combine them. Output example: \"lq5z8f9x1a2b3c\"\n  return `${timePart}${countPart}${randomPart}`;\n};\n\nexport const branch = () => \"z9hG4bK-\" + uuid();\n\nconst generateResponse = (\n  sipInfo: SipInfo,\n  endpoint: string,\n  nonce: string,\n) => {\n  const ha1 = md5(\n    `${sipInfo.authorizationId}:${sipInfo.domain}:${sipInfo.password}`,\n  );\n  const ha2 = md5(endpoint);\n  const response = md5(`${ha1}:${nonce}:${ha2}`);\n  return response;\n};\n\nexport const generateAuthorization = (\n  sipInfo: SipInfo,\n  nonce: string,\n  method: \"REGISTER\" | \"INVITE\",\n) => {\n  const authObj = {\n    \"Digest algorithm\": \"MD5\",\n    username: sipInfo.authorizationId,\n    realm: sipInfo.domain,\n    nonce,\n    uri: `sip:${sipInfo.domain}`,\n    response: generateResponse(\n      sipInfo,\n      `${method}:sip:${sipInfo.domain}`,\n      nonce,\n    ),\n  };\n  return Object.entries(authObj)\n    .map(([key, value]) => `${key}=\"${value}\"`)\n    .join(\", \");\n};\n\nexport const withoutTag = (s: string) => s.replace(/;tag=.*$/, \"\");\nexport const extractAddress = (s: string) => s.match(/<(sip:.+?)>/)![1];\nexport const extractNumber = (s: string) => s.match(/<sip:(.+?)@/)![1];\nexport const extractTag = (peer: string) => peer.match(/;tag=(.*)/)![1];\n\nexport const fakeDomain = uuid() + \".invalid\";\nexport const fakeEmail = uuid() + \"@\" + fakeDomain;\n"],"mappings":";;AAKA,IAAI,UAAU;AACd,MAAa,aAAa;AAExB,YAAW,UAAU,KAAK,OAAO;AAYjC,QAAO,GATU,KAAK,KAAK,CAAC,SAAS,GASnB,GANA,QAAQ,SAAS,GAML,GAHX,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,UAAU,GAAG,EAGhB;;AAG7C,MAAa,eAAe,aAAa,MAAM;AAE/C,MAAM,oBACJ,SACA,UACA,UACG;AAMH,QADiB,IAAI,GAJT,IACV,GAAG,QAAQ,gBAAgB,GAAG,QAAQ,OAAO,GAAG,QAAQ,WAG/B,CAAC,GAAG,MAAM,GADzB,IAAI,SAC2B,GAC5B;;AAGjB,MAAa,yBACX,SACA,OACA,WACG;CACH,MAAM,UAAU;EACd,oBAAoB;EACpB,UAAU,QAAQ;EAClB,OAAO,QAAQ;EACf;EACA,KAAK,OAAO,QAAQ;EACpB,UAAU,iBACR,SACA,GAAG,OAAO,OAAO,QAAQ,UACzB,MACD;EACF;AACD,QAAO,OAAO,QAAQ,QAAQ,CAC3B,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,IAAI,MAAM,GAAG,CAC1C,KAAK,KAAK;;AAGf,MAAa,cAAc,MAAc,EAAE,QAAQ,YAAY,GAAG;AAClE,MAAa,kBAAkB,MAAc,EAAE,MAAM,cAAc,CAAE;AACrE,MAAa,iBAAiB,MAAc,EAAE,MAAM,cAAc,CAAE;AACpE,MAAa,cAAc,SAAiB,KAAK,MAAM,YAAY,CAAE;AAErE,MAAa,aAAa,MAAM,GAAG;AACnC,MAAa,YAAY,MAAM,GAAG,MAAM"}