@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.
@@ -31,6 +31,7 @@ var JWK_CACHE_TTL_MS = 60 * 60 * 1e3;
31
31
  var ETAG_CACHE_TTL_MS = 5 * 60 * 1e3;
32
32
 
33
33
  // src/key-validation.ts
34
+ var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
34
35
  var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
35
36
  var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
36
37
  var ENV_PREFIX_MAP = {
@@ -121,6 +122,9 @@ function validateKeyForType(key, keyType, pattern, envVarName) {
121
122
  issues: [...issues, "invalid-format"]
122
123
  };
123
124
  }
125
+ function validatePublicKey(key) {
126
+ return validateKeyForType(key, "publicKey", PUBLIC_KEY_PATTERN, "NEXT_PUBLIC_SYLPHX_KEY");
127
+ }
124
128
  function validateAppId(key) {
125
129
  return validateKeyForType(key, "appId", APP_ID_PATTERN, "NEXT_PUBLIC_SYLPHX_APP_ID");
126
130
  }
@@ -149,22 +153,23 @@ function validateAndSanitizeSecretKey(key) {
149
153
  }
150
154
  function detectEnvironment(key) {
151
155
  const sanitized = key.trim().toLowerCase();
156
+ if (sanitized.startsWith("pk_")) {
157
+ const result = validatePublicKey(sanitized);
158
+ if (!result.valid) throw new Error(result.error);
159
+ return result.environment;
160
+ }
152
161
  if (sanitized.startsWith("sk_")) {
153
162
  const result = validateSecretKey(sanitized);
154
- if (!result.valid) {
155
- throw new Error(result.error);
156
- }
163
+ if (!result.valid) throw new Error(result.error);
157
164
  return result.environment;
158
165
  }
159
166
  if (sanitized.startsWith("app_")) {
160
167
  const result = validateAppId(sanitized);
161
- if (!result.valid) {
162
- throw new Error(result.error);
163
- }
168
+ if (!result.valid) throw new Error(result.error);
164
169
  return result.environment;
165
170
  }
166
171
  throw new Error(
167
- `[Sylphx] Invalid key format. Key must start with 'sk_' (secret) or 'app_' (App ID).`
172
+ `[Sylphx] Invalid key format. Key must start with 'pk_' (publishable), 'sk_' (secret), or 'app_' (legacy App ID).`
168
173
  );
169
174
  }
170
175
  function getCookieNamespace(secretKey) {
@@ -1676,31 +1681,41 @@ async function jwtVerify(jwt, key, options) {
1676
1681
  }
1677
1682
 
1678
1683
  // src/lib/ids.ts
1684
+ var CB32 = "0123456789abcdefghjkmnpqrstvwxyz";
1685
+ var CB32_MAP = Object.fromEntries([...CB32].map((c, i) => [c, i]));
1679
1686
  var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1680
1687
  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;
1688
+ function cb32Encode(hex) {
1689
+ const num = BigInt(`0x${hex}`);
1690
+ const chars = [];
1691
+ let n = num;
1692
+ for (let i = 0; i < 26; i++) {
1693
+ chars.unshift(CB32[Number(n & 0x1fn)]);
1694
+ n >>= 5n;
1695
+ }
1696
+ return chars.join("");
1697
+ }
1698
+ function cb32Decode(str) {
1699
+ if (str.length !== 26) return null;
1700
+ let n = 0n;
1701
+ for (const c of str.toLowerCase()) {
1702
+ const idx = CB32_MAP[c];
1703
+ if (idx === void 0) return null;
1704
+ n = n << 5n | BigInt(idx);
1690
1705
  }
1691
- return `user_${r}`;
1706
+ return n.toString(16).padStart(32, "0");
1692
1707
  }
1693
- function decodeUserId(prefixedId) {
1694
- if (!prefixedId.startsWith("user_")) return null;
1695
- const enc = prefixedId.slice(5);
1696
- if (!enc) return null;
1708
+ function b58Decode(str) {
1697
1709
  let n = 0n;
1698
- for (const c of enc) {
1710
+ for (const c of str) {
1699
1711
  const i = B58_MAP[c] ?? -1;
1700
1712
  if (i === -1) return null;
1701
1713
  n = n * 58n + BigInt(i);
1702
1714
  }
1703
1715
  const hex = n.toString(16).padStart(32, "0");
1716
+ return hex.length === 32 ? hex : null;
1717
+ }
1718
+ function hexToUuid(hex) {
1704
1719
  return [
1705
1720
  hex.slice(0, 8),
1706
1721
  hex.slice(8, 12),
@@ -1709,6 +1724,24 @@ function decodeUserId(prefixedId) {
1709
1724
  hex.slice(20)
1710
1725
  ].join("-");
1711
1726
  }
1727
+ function encodeUserId(uuid) {
1728
+ const hex = uuid.replace(/-/g, "");
1729
+ if (hex.length !== 32)
1730
+ throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1731
+ return `user_${cb32Encode(hex)}`;
1732
+ }
1733
+ function decodeUserId(prefixedId) {
1734
+ if (!prefixedId.startsWith("user_")) return null;
1735
+ const enc = prefixedId.slice(5);
1736
+ if (!enc) return null;
1737
+ if (enc.length === 26) {
1738
+ const hex2 = cb32Decode(enc);
1739
+ if (hex2) return hexToUuid(hex2);
1740
+ }
1741
+ const hex = b58Decode(enc);
1742
+ if (hex) return hexToUuid(hex);
1743
+ return null;
1744
+ }
1712
1745
 
1713
1746
  // src/server/index.ts
1714
1747
  function isJwksResponse(data) {