@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.
@@ -1906,31 +1906,41 @@ function createDynamicRestClient(config) {
1906
1906
  }
1907
1907
 
1908
1908
  // src/lib/ids.ts
1909
+ var CB32 = "0123456789abcdefghjkmnpqrstvwxyz";
1910
+ var CB32_MAP = Object.fromEntries([...CB32].map((c, i) => [c, i]));
1909
1911
  var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1910
1912
  var B58_MAP = Object.fromEntries([...B58].map((c, i) => [c, i]));
1911
- function encodeUserId(uuid) {
1912
- const hex = uuid.replace(/-/g, "");
1913
- if (hex.length !== 32)
1914
- throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1915
- let n = BigInt("0x" + hex);
1916
- let r = "";
1917
- while (n > 0n) {
1918
- r = B58[Number(n % 58n)] + r;
1919
- n /= 58n;
1913
+ function cb32Encode(hex) {
1914
+ const num = BigInt(`0x${hex}`);
1915
+ const chars = [];
1916
+ let n = num;
1917
+ for (let i = 0; i < 26; i++) {
1918
+ chars.unshift(CB32[Number(n & 0x1fn)]);
1919
+ n >>= 5n;
1920
+ }
1921
+ return chars.join("");
1922
+ }
1923
+ function cb32Decode(str) {
1924
+ if (str.length !== 26) return null;
1925
+ let n = 0n;
1926
+ for (const c of str.toLowerCase()) {
1927
+ const idx = CB32_MAP[c];
1928
+ if (idx === void 0) return null;
1929
+ n = n << 5n | BigInt(idx);
1920
1930
  }
1921
- return `user_${r}`;
1931
+ return n.toString(16).padStart(32, "0");
1922
1932
  }
1923
- function decodeUserId(prefixedId) {
1924
- if (!prefixedId.startsWith("user_")) return null;
1925
- const enc = prefixedId.slice(5);
1926
- if (!enc) return null;
1933
+ function b58Decode(str) {
1927
1934
  let n = 0n;
1928
- for (const c of enc) {
1935
+ for (const c of str) {
1929
1936
  const i = B58_MAP[c] ?? -1;
1930
1937
  if (i === -1) return null;
1931
1938
  n = n * 58n + BigInt(i);
1932
1939
  }
1933
1940
  const hex = n.toString(16).padStart(32, "0");
1941
+ return hex.length === 32 ? hex : null;
1942
+ }
1943
+ function hexToUuid(hex) {
1934
1944
  return [
1935
1945
  hex.slice(0, 8),
1936
1946
  hex.slice(8, 12),
@@ -1939,6 +1949,24 @@ function decodeUserId(prefixedId) {
1939
1949
  hex.slice(20)
1940
1950
  ].join("-");
1941
1951
  }
1952
+ function encodeUserId(uuid) {
1953
+ const hex = uuid.replace(/-/g, "");
1954
+ if (hex.length !== 32)
1955
+ throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1956
+ return `user_${cb32Encode(hex)}`;
1957
+ }
1958
+ function decodeUserId(prefixedId) {
1959
+ if (!prefixedId.startsWith("user_")) return null;
1960
+ const enc = prefixedId.slice(5);
1961
+ if (!enc) return null;
1962
+ if (enc.length === 26) {
1963
+ const hex2 = cb32Decode(enc);
1964
+ if (hex2) return hexToUuid(hex2);
1965
+ }
1966
+ const hex = b58Decode(enc);
1967
+ if (hex) return hexToUuid(hex);
1968
+ return null;
1969
+ }
1942
1970
 
1943
1971
  // src/server/ai.ts
1944
1972
  function createAI(options = {}) {