@sylphx/sdk 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1676,31 +1676,41 @@ async function jwtVerify(jwt, key, options) {
1676
1676
  }
1677
1677
 
1678
1678
  // src/lib/ids.ts
1679
+ var CB32 = "0123456789abcdefghjkmnpqrstvwxyz";
1680
+ var CB32_MAP = Object.fromEntries([...CB32].map((c, i) => [c, i]));
1679
1681
  var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1680
1682
  var B58_MAP = Object.fromEntries([...B58].map((c, i) => [c, i]));
1681
- function encodeUserId(uuid) {
1682
- const hex = uuid.replace(/-/g, "");
1683
- if (hex.length !== 32)
1684
- throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1685
- let n = BigInt("0x" + hex);
1686
- let r = "";
1687
- while (n > 0n) {
1688
- r = B58[Number(n % 58n)] + r;
1689
- n /= 58n;
1683
+ function cb32Encode(hex) {
1684
+ const num = BigInt(`0x${hex}`);
1685
+ const chars = [];
1686
+ let n = num;
1687
+ for (let i = 0; i < 26; i++) {
1688
+ chars.unshift(CB32[Number(n & 0x1fn)]);
1689
+ n >>= 5n;
1690
+ }
1691
+ return chars.join("");
1692
+ }
1693
+ function cb32Decode(str) {
1694
+ if (str.length !== 26) return null;
1695
+ let n = 0n;
1696
+ for (const c of str.toLowerCase()) {
1697
+ const idx = CB32_MAP[c];
1698
+ if (idx === void 0) return null;
1699
+ n = n << 5n | BigInt(idx);
1690
1700
  }
1691
- return `user_${r}`;
1701
+ return n.toString(16).padStart(32, "0");
1692
1702
  }
1693
- function decodeUserId(prefixedId) {
1694
- if (!prefixedId.startsWith("user_")) return null;
1695
- const enc = prefixedId.slice(5);
1696
- if (!enc) return null;
1703
+ function b58Decode(str) {
1697
1704
  let n = 0n;
1698
- for (const c of enc) {
1705
+ for (const c of str) {
1699
1706
  const i = B58_MAP[c] ?? -1;
1700
1707
  if (i === -1) return null;
1701
1708
  n = n * 58n + BigInt(i);
1702
1709
  }
1703
1710
  const hex = n.toString(16).padStart(32, "0");
1711
+ return hex.length === 32 ? hex : null;
1712
+ }
1713
+ function hexToUuid(hex) {
1704
1714
  return [
1705
1715
  hex.slice(0, 8),
1706
1716
  hex.slice(8, 12),
@@ -1709,6 +1719,24 @@ function decodeUserId(prefixedId) {
1709
1719
  hex.slice(20)
1710
1720
  ].join("-");
1711
1721
  }
1722
+ function encodeUserId(uuid) {
1723
+ const hex = uuid.replace(/-/g, "");
1724
+ if (hex.length !== 32)
1725
+ throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1726
+ return `user_${cb32Encode(hex)}`;
1727
+ }
1728
+ function decodeUserId(prefixedId) {
1729
+ if (!prefixedId.startsWith("user_")) return null;
1730
+ const enc = prefixedId.slice(5);
1731
+ if (!enc) return null;
1732
+ if (enc.length === 26) {
1733
+ const hex2 = cb32Decode(enc);
1734
+ if (hex2) return hexToUuid(hex2);
1735
+ }
1736
+ const hex = b58Decode(enc);
1737
+ if (hex) return hexToUuid(hex);
1738
+ return null;
1739
+ }
1712
1740
 
1713
1741
  // src/server/index.ts
1714
1742
  function isJwksResponse(data) {