@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.
@@ -4724,7 +4724,7 @@ interface components {
4724
4724
  OrgInvitation: {
4725
4725
  /**
4726
4726
  * @description Invitation ID
4727
- * @example ivt_3Zb83qVQxkHMJPZ8VrJfQ2
4727
+ * @example inv_3Zb83qVQxkHMJPZ8VrJfQ2
4728
4728
  */
4729
4729
  id: string
4730
4730
  /**
@@ -4794,7 +4794,7 @@ interface components {
4794
4794
  /**
4795
4795
  * Format: uuid
4796
4796
  * @description Invitation token (ID)
4797
- * @example ivt_3Zb83qVQxkHMJPZ8VrJfQ2
4797
+ * @example inv_3Zb83qVQxkHMJPZ8VrJfQ2
4798
4798
  */
4799
4799
  token: string
4800
4800
  }
@@ -6053,33 +6053,36 @@ declare const SESSION_TOKEN_LIFETIME_MS: number;
6053
6053
  * Platform ID utilities for the Sylphx SDK
6054
6054
  *
6055
6055
  * Converts between raw UUIDs (used internally / in JWT sub claims) and
6056
- * prefixed base58 IDs (used in all API responses and JWT pid claims).
6056
+ * prefixed TypeID strings (used in all API responses and JWT pid claims).
6057
6057
  *
6058
- * Same alphabet and encoding logic as the platform server.
6059
- * No external dependencies pure TypeScript.
6058
+ * Uses TypeID spec v0.3.0 (Crockford base32, case-insensitive).
6059
+ * Also accepts legacy base58 format for backward compatibility.
6060
+ *
6061
+ * No external dependencies — pure TypeScript implementation of Crockford base32.
6060
6062
  *
6061
6063
  * @example
6062
6064
  * ```ts
6063
6065
  * import { encodeUserId, decodeUserId } from '@sylphx/sdk/nextjs'
6064
6066
  *
6065
6067
  * const prefixed = encodeUserId('018f4a3b-1c2d-7000-9abc-def012345678')
6066
- * // => 'user_3Zb83qVQxkHMJPZ8VrJfQ2'
6068
+ * // => 'user_01h2xcejqtf2nbrexx3vqjhp41'
6067
6069
  *
6068
- * const uuid = decodeUserId('user_3Zb83qVQxkHMJPZ8VrJfQ2')
6070
+ * const uuid = decodeUserId('user_01h2xcejqtf2nbrexx3vqjhp41')
6069
6071
  * // => '018f4a3b-1c2d-7000-9abc-def012345678'
6070
6072
  * ```
6071
6073
  */
6072
6074
  /**
6073
- * Encode a raw UUID as a prefixed base58 user ID.
6075
+ * Encode a raw UUID as a prefixed TypeID user ID.
6074
6076
  *
6075
6077
  * @param uuid - Raw UUID string (with or without dashes)
6076
- * @returns Prefixed platform ID: `user_<base58>`
6078
+ * @returns Prefixed TypeID: `user_<crockford_base32>`
6077
6079
  */
6078
6080
  declare function encodeUserId(uuid: string): string;
6079
6081
  /**
6080
- * Decode a prefixed base58 user ID back to a raw UUID.
6082
+ * Decode a prefixed user ID back to a raw UUID.
6083
+ * Accepts both TypeID (current) and base58 (legacy) formats.
6081
6084
  *
6082
- * @param prefixedId - Prefixed platform ID: `user_<base58>`
6085
+ * @param prefixedId - Prefixed user ID: `user_<encoded>`
6083
6086
  * @returns Raw UUID string, or null if invalid
6084
6087
  */
6085
6088
  declare function decodeUserId(prefixedId: string): string | null;
@@ -4724,7 +4724,7 @@ interface components {
4724
4724
  OrgInvitation: {
4725
4725
  /**
4726
4726
  * @description Invitation ID
4727
- * @example ivt_3Zb83qVQxkHMJPZ8VrJfQ2
4727
+ * @example inv_3Zb83qVQxkHMJPZ8VrJfQ2
4728
4728
  */
4729
4729
  id: string
4730
4730
  /**
@@ -4794,7 +4794,7 @@ interface components {
4794
4794
  /**
4795
4795
  * Format: uuid
4796
4796
  * @description Invitation token (ID)
4797
- * @example ivt_3Zb83qVQxkHMJPZ8VrJfQ2
4797
+ * @example inv_3Zb83qVQxkHMJPZ8VrJfQ2
4798
4798
  */
4799
4799
  token: string
4800
4800
  }
@@ -6053,33 +6053,36 @@ declare const SESSION_TOKEN_LIFETIME_MS: number;
6053
6053
  * Platform ID utilities for the Sylphx SDK
6054
6054
  *
6055
6055
  * Converts between raw UUIDs (used internally / in JWT sub claims) and
6056
- * prefixed base58 IDs (used in all API responses and JWT pid claims).
6056
+ * prefixed TypeID strings (used in all API responses and JWT pid claims).
6057
6057
  *
6058
- * Same alphabet and encoding logic as the platform server.
6059
- * No external dependencies pure TypeScript.
6058
+ * Uses TypeID spec v0.3.0 (Crockford base32, case-insensitive).
6059
+ * Also accepts legacy base58 format for backward compatibility.
6060
+ *
6061
+ * No external dependencies — pure TypeScript implementation of Crockford base32.
6060
6062
  *
6061
6063
  * @example
6062
6064
  * ```ts
6063
6065
  * import { encodeUserId, decodeUserId } from '@sylphx/sdk/nextjs'
6064
6066
  *
6065
6067
  * const prefixed = encodeUserId('018f4a3b-1c2d-7000-9abc-def012345678')
6066
- * // => 'user_3Zb83qVQxkHMJPZ8VrJfQ2'
6068
+ * // => 'user_01h2xcejqtf2nbrexx3vqjhp41'
6067
6069
  *
6068
- * const uuid = decodeUserId('user_3Zb83qVQxkHMJPZ8VrJfQ2')
6070
+ * const uuid = decodeUserId('user_01h2xcejqtf2nbrexx3vqjhp41')
6069
6071
  * // => '018f4a3b-1c2d-7000-9abc-def012345678'
6070
6072
  * ```
6071
6073
  */
6072
6074
  /**
6073
- * Encode a raw UUID as a prefixed base58 user ID.
6075
+ * Encode a raw UUID as a prefixed TypeID user ID.
6074
6076
  *
6075
6077
  * @param uuid - Raw UUID string (with or without dashes)
6076
- * @returns Prefixed platform ID: `user_<base58>`
6078
+ * @returns Prefixed TypeID: `user_<crockford_base32>`
6077
6079
  */
6078
6080
  declare function encodeUserId(uuid: string): string;
6079
6081
  /**
6080
- * Decode a prefixed base58 user ID back to a raw UUID.
6082
+ * Decode a prefixed user ID back to a raw UUID.
6083
+ * Accepts both TypeID (current) and base58 (legacy) formats.
6081
6084
  *
6082
- * @param prefixedId - Prefixed platform ID: `user_<base58>`
6085
+ * @param prefixedId - Prefixed user ID: `user_<encoded>`
6083
6086
  * @returns Raw UUID string, or null if invalid
6084
6087
  */
6085
6088
  declare function decodeUserId(prefixedId: string): string | null;
@@ -85,6 +85,7 @@ var JWK_CACHE_TTL_MS = 60 * 60 * 1e3;
85
85
  var ETAG_CACHE_TTL_MS = 5 * 60 * 1e3;
86
86
 
87
87
  // src/key-validation.ts
88
+ var PUBLIC_KEY_PATTERN = /^pk_(dev|stg|prod)_[a-z0-9]{12}_[a-f0-9]{32}$/;
88
89
  var APP_ID_PATTERN = /^app_(dev|stg|prod)_[a-z0-9_-]+$/;
89
90
  var SECRET_KEY_PATTERN = /^sk_(dev|stg|prod)_[a-z0-9_-]+$/;
90
91
  var ENV_PREFIX_MAP = {
@@ -175,6 +176,9 @@ function validateKeyForType(key, keyType, pattern, envVarName) {
175
176
  issues: [...issues, "invalid-format"]
176
177
  };
177
178
  }
179
+ function validatePublicKey(key) {
180
+ return validateKeyForType(key, "publicKey", PUBLIC_KEY_PATTERN, "NEXT_PUBLIC_SYLPHX_KEY");
181
+ }
178
182
  function validateAppId(key) {
179
183
  return validateKeyForType(key, "appId", APP_ID_PATTERN, "NEXT_PUBLIC_SYLPHX_APP_ID");
180
184
  }
@@ -203,22 +207,23 @@ function validateAndSanitizeSecretKey(key) {
203
207
  }
204
208
  function detectEnvironment(key) {
205
209
  const sanitized = key.trim().toLowerCase();
210
+ if (sanitized.startsWith("pk_")) {
211
+ const result = validatePublicKey(sanitized);
212
+ if (!result.valid) throw new Error(result.error);
213
+ return result.environment;
214
+ }
206
215
  if (sanitized.startsWith("sk_")) {
207
216
  const result = validateSecretKey(sanitized);
208
- if (!result.valid) {
209
- throw new Error(result.error);
210
- }
217
+ if (!result.valid) throw new Error(result.error);
211
218
  return result.environment;
212
219
  }
213
220
  if (sanitized.startsWith("app_")) {
214
221
  const result = validateAppId(sanitized);
215
- if (!result.valid) {
216
- throw new Error(result.error);
217
- }
222
+ if (!result.valid) throw new Error(result.error);
218
223
  return result.environment;
219
224
  }
220
225
  throw new Error(
221
- `[Sylphx] Invalid key format. Key must start with 'sk_' (secret) or 'app_' (App ID).`
226
+ `[Sylphx] Invalid key format. Key must start with 'pk_' (publishable), 'sk_' (secret), or 'app_' (legacy App ID).`
222
227
  );
223
228
  }
224
229
  function getCookieNamespace(secretKey) {
@@ -1730,31 +1735,41 @@ async function jwtVerify(jwt, key, options) {
1730
1735
  }
1731
1736
 
1732
1737
  // src/lib/ids.ts
1738
+ var CB32 = "0123456789abcdefghjkmnpqrstvwxyz";
1739
+ var CB32_MAP = Object.fromEntries([...CB32].map((c, i) => [c, i]));
1733
1740
  var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1734
1741
  var B58_MAP = Object.fromEntries([...B58].map((c, i) => [c, i]));
1735
- function encodeUserId(uuid) {
1736
- const hex = uuid.replace(/-/g, "");
1737
- if (hex.length !== 32)
1738
- throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1739
- let n = BigInt("0x" + hex);
1740
- let r = "";
1741
- while (n > 0n) {
1742
- r = B58[Number(n % 58n)] + r;
1743
- n /= 58n;
1742
+ function cb32Encode(hex) {
1743
+ const num = BigInt(`0x${hex}`);
1744
+ const chars = [];
1745
+ let n = num;
1746
+ for (let i = 0; i < 26; i++) {
1747
+ chars.unshift(CB32[Number(n & 0x1fn)]);
1748
+ n >>= 5n;
1749
+ }
1750
+ return chars.join("");
1751
+ }
1752
+ function cb32Decode(str) {
1753
+ if (str.length !== 26) return null;
1754
+ let n = 0n;
1755
+ for (const c of str.toLowerCase()) {
1756
+ const idx = CB32_MAP[c];
1757
+ if (idx === void 0) return null;
1758
+ n = n << 5n | BigInt(idx);
1744
1759
  }
1745
- return `user_${r}`;
1760
+ return n.toString(16).padStart(32, "0");
1746
1761
  }
1747
- function decodeUserId(prefixedId) {
1748
- if (!prefixedId.startsWith("user_")) return null;
1749
- const enc = prefixedId.slice(5);
1750
- if (!enc) return null;
1762
+ function b58Decode(str) {
1751
1763
  let n = 0n;
1752
- for (const c of enc) {
1764
+ for (const c of str) {
1753
1765
  const i = B58_MAP[c] ?? -1;
1754
1766
  if (i === -1) return null;
1755
1767
  n = n * 58n + BigInt(i);
1756
1768
  }
1757
1769
  const hex = n.toString(16).padStart(32, "0");
1770
+ return hex.length === 32 ? hex : null;
1771
+ }
1772
+ function hexToUuid(hex) {
1758
1773
  return [
1759
1774
  hex.slice(0, 8),
1760
1775
  hex.slice(8, 12),
@@ -1763,6 +1778,24 @@ function decodeUserId(prefixedId) {
1763
1778
  hex.slice(20)
1764
1779
  ].join("-");
1765
1780
  }
1781
+ function encodeUserId(uuid) {
1782
+ const hex = uuid.replace(/-/g, "");
1783
+ if (hex.length !== 32)
1784
+ throw new Error("Invalid UUID: expected 32 hex chars after stripping dashes");
1785
+ return `user_${cb32Encode(hex)}`;
1786
+ }
1787
+ function decodeUserId(prefixedId) {
1788
+ if (!prefixedId.startsWith("user_")) return null;
1789
+ const enc = prefixedId.slice(5);
1790
+ if (!enc) return null;
1791
+ if (enc.length === 26) {
1792
+ const hex2 = cb32Decode(enc);
1793
+ if (hex2) return hexToUuid(hex2);
1794
+ }
1795
+ const hex = b58Decode(enc);
1796
+ if (hex) return hexToUuid(hex);
1797
+ return null;
1798
+ }
1766
1799
 
1767
1800
  // src/server/index.ts
1768
1801
  function isJwksResponse(data) {