@sylphx/sdk 0.3.3 → 0.3.5

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.
@@ -1338,6 +1338,7 @@ function exponentialBackoff(attempt, baseDelay = BASE_RETRY_DELAY_MS, maxDelay =
1338
1338
  }
1339
1339
 
1340
1340
  // src/key-validation.ts
1341
+ var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
1341
1342
  var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
1342
1343
  var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
1343
1344
  var ENV_PREFIX_MAP = {
@@ -1428,6 +1429,9 @@ function validateKeyForType(key, keyType, pattern, envVarName) {
1428
1429
  issues: [...issues, "invalid-format"]
1429
1430
  };
1430
1431
  }
1432
+ function validatePublicKey(key) {
1433
+ return validateKeyForType(key, "publicKey", PUBLIC_KEY_PATTERN, "NEXT_PUBLIC_SYLPHX_KEY");
1434
+ }
1431
1435
  function validateAppId(key) {
1432
1436
  return validateKeyForType(key, "appId", APP_ID_PATTERN, "NEXT_PUBLIC_SYLPHX_APP_ID");
1433
1437
  }
@@ -1456,22 +1460,23 @@ function validateAndSanitizeSecretKey(key) {
1456
1460
  }
1457
1461
  function detectEnvironment(key) {
1458
1462
  const sanitized = key.trim().toLowerCase();
1463
+ if (sanitized.startsWith("pk_")) {
1464
+ const result = validatePublicKey(sanitized);
1465
+ if (!result.valid) throw new Error(result.error);
1466
+ return result.environment;
1467
+ }
1459
1468
  if (sanitized.startsWith("sk_")) {
1460
1469
  const result = validateSecretKey(sanitized);
1461
- if (!result.valid) {
1462
- throw new Error(result.error);
1463
- }
1470
+ if (!result.valid) throw new Error(result.error);
1464
1471
  return result.environment;
1465
1472
  }
1466
1473
  if (sanitized.startsWith("app_")) {
1467
1474
  const result = validateAppId(sanitized);
1468
- if (!result.valid) {
1469
- throw new Error(result.error);
1470
- }
1475
+ if (!result.valid) throw new Error(result.error);
1471
1476
  return result.environment;
1472
1477
  }
1473
1478
  throw new Error(
1474
- `[Sylphx] Invalid key format. Key must start with 'sk_' (secret) or 'app_' (App ID).`
1479
+ `[Sylphx] Invalid key format. Key must start with 'pk_' (publishable), 'sk_' (secret), or 'app_' (legacy App ID).`
1475
1480
  );
1476
1481
  }
1477
1482
  function isDevelopmentKey(key) {
@@ -1487,6 +1492,7 @@ function getCookieNamespace(secretKey) {
1487
1492
  }
1488
1493
  function detectKeyType(key) {
1489
1494
  const sanitized = key.trim().toLowerCase();
1495
+ if (sanitized.startsWith("pk_")) return "publicKey";
1490
1496
  if (sanitized.startsWith("app_")) return "appId";
1491
1497
  if (sanitized.startsWith("sk_")) return "secret";
1492
1498
  return null;
@@ -1499,6 +1505,9 @@ function isSecretKey(key) {
1499
1505
  }
1500
1506
  function validateKey(key) {
1501
1507
  const keyType = key ? detectKeyType(key) : null;
1508
+ if (keyType === "publicKey") {
1509
+ return validatePublicKey(key);
1510
+ }
1502
1511
  if (keyType === "appId") {
1503
1512
  return validateAppId(key);
1504
1513
  }
@@ -1508,7 +1517,7 @@ function validateKey(key) {
1508
1517
  return {
1509
1518
  valid: false,
1510
1519
  sanitizedKey: "",
1511
- error: key ? `Invalid key format. Keys must start with 'app_' (App ID) or 'sk_' (Secret Key), followed by environment (dev/stg/prod) and identifier. Got: ${key.slice(0, 20)}...` : "API key is required but was not provided.",
1520
+ error: key ? `Invalid key format. Keys must start with 'pk_' (publishable), 'app_' (legacy), or 'sk_' (secret), followed by environment (dev/stg/prod). Got: ${key.slice(0, 20)}...` : "API key is required but was not provided.",
1512
1521
  issues: key ? ["invalid_format"] : ["missing"]
1513
1522
  };
1514
1523
  }
@@ -1906,31 +1915,41 @@ function createDynamicRestClient(config) {
1906
1915
  }
1907
1916
 
1908
1917
  // src/lib/ids.ts
1918
+ var CB32 = "0123456789abcdefghjkmnpqrstvwxyz";
1919
+ var CB32_MAP = Object.fromEntries([...CB32].map((c, i) => [c, i]));
1909
1920
  var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1910
1921
  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;
1922
+ function cb32Encode(hex) {
1923
+ const num = BigInt(`0x${hex}`);
1924
+ const chars = [];
1925
+ let n = num;
1926
+ for (let i = 0; i < 26; i++) {
1927
+ chars.unshift(CB32[Number(n & 0x1fn)]);
1928
+ n >>= 5n;
1929
+ }
1930
+ return chars.join("");
1931
+ }
1932
+ function cb32Decode(str) {
1933
+ if (str.length !== 26) return null;
1934
+ let n = 0n;
1935
+ for (const c of str.toLowerCase()) {
1936
+ const idx = CB32_MAP[c];
1937
+ if (idx === void 0) return null;
1938
+ n = n << 5n | BigInt(idx);
1920
1939
  }
1921
- return `user_${r}`;
1940
+ return n.toString(16).padStart(32, "0");
1922
1941
  }
1923
- function decodeUserId(prefixedId) {
1924
- if (!prefixedId.startsWith("user_")) return null;
1925
- const enc = prefixedId.slice(5);
1926
- if (!enc) return null;
1942
+ function b58Decode(str) {
1927
1943
  let n = 0n;
1928
- for (const c of enc) {
1944
+ for (const c of str) {
1929
1945
  const i = B58_MAP[c] ?? -1;
1930
1946
  if (i === -1) return null;
1931
1947
  n = n * 58n + BigInt(i);
1932
1948
  }
1933
1949
  const hex = n.toString(16).padStart(32, "0");
1950
+ return hex.length === 32 ? hex : null;
1951
+ }
1952
+ function hexToUuid(hex) {
1934
1953
  return [
1935
1954
  hex.slice(0, 8),
1936
1955
  hex.slice(8, 12),
@@ -1939,6 +1958,24 @@ function decodeUserId(prefixedId) {
1939
1958
  hex.slice(20)
1940
1959
  ].join("-");
1941
1960
  }
1961
+ function encodeUserId(uuid) {
1962
+ const hex = uuid.replace(/-/g, "");
1963
+ if (hex.length !== 32)
1964
+ throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1965
+ return `user_${cb32Encode(hex)}`;
1966
+ }
1967
+ function decodeUserId(prefixedId) {
1968
+ if (!prefixedId.startsWith("user_")) return null;
1969
+ const enc = prefixedId.slice(5);
1970
+ if (!enc) return null;
1971
+ if (enc.length === 26) {
1972
+ const hex2 = cb32Decode(enc);
1973
+ if (hex2) return hexToUuid(hex2);
1974
+ }
1975
+ const hex = b58Decode(enc);
1976
+ if (hex) return hexToUuid(hex);
1977
+ return null;
1978
+ }
1942
1979
 
1943
1980
  // src/server/ai.ts
1944
1981
  function createAI(options = {}) {