fix playground - #2153
Conversation
WalkthroughIntroduces a cross-environment Base64 encoding utility module that properly handles non-ASCII characters. Replaces direct Changes
Sequence DiagramsequenceDiagram
participant App as App Component
participant Helper as encodeToBase64()
participant Env as Runtime Environment
App->>Helper: encodeToBase64(value)
Helper->>Env: Detect environment
alt Node.js with Buffer
Env-->>Helper: Buffer available
Helper->>Helper: Convert string → Buffer
Helper->>Helper: Encode to Base64
else Browser with btoa
Env-->>Helper: window.btoa available
Helper->>Helper: toBinaryString(value)
Helper->>Helper: btoa(binary)
else globalThis.btoa
Env-->>Helper: globalThis.btoa available
Helper->>Helper: toBinaryString(value)
Helper->>Helper: globalThis.btoa(binary)
else No encoding method
Env-->>Helper: ✗ No method
Helper-->>App: throw Error
end
Helper-->>App: Base64 encoded string
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
web/src/helpers/base64.js (1)
37-56: Well-designed cross-environment Base64 encoding.The implementation correctly handles Unicode characters across different environments:
- Node.js: Uses
Buffer.from(input, 'utf-8')for direct UTF-8 encoding- Browser: Uses
window.btoa(toBinaryString(input))to handle Unicode via binary string conversion- Fallback: Checks
globalThis.btoafor other environments- Error handling: Throws descriptive error if no encoding method available
This properly fixes the root cause of issue #2150 where
btoa()failed on Chinese characters.Consider adding JSDoc documentation to clarify the Unicode handling behavior:
+/** + * Encodes a value to Base64 with full Unicode support. + * Handles non-ASCII characters (e.g., Chinese, emoji) by converting to UTF-8 bytes. + * @param {*} value - Value to encode (will be converted to string) + * @returns {string} Base64-encoded string + * @throws {Error} If Base64 encoding is unavailable in the current environment + */ export const encodeToBase64 = (value) => {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
web/src/helpers/base64.js(1 hunks)web/src/helpers/index.js(1 hunks)web/src/hooks/tokens/useTokensData.jsx(2 hunks)web/src/pages/Playground/index.jsx(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
web/src/pages/Playground/index.jsx (1)
web/src/helpers/base64.js (2)
encodeToBase64(37-56)encodeToBase64(37-56)
web/src/hooks/tokens/useTokensData.jsx (1)
web/src/helpers/base64.js (2)
encodeToBase64(37-56)encodeToBase64(37-56)
🔇 Additional comments (6)
web/src/helpers/index.js (1)
23-23: LGTM! Base64 utilities properly exposed.The re-export correctly makes
encodeToBase64available through the central helpers index, maintaining consistency with the existing module structure.web/src/pages/Playground/index.jsx (2)
50-50: Good addition of Unicode-safe encoding utility.The import of
encodeToBase64enables proper handling of non-ASCII characters in the avatar generation.
64-77: Critical fix: This resolves the Chinese username bug.Replacing
btoawithencodeToBase64correctly handles Unicode characters in usernames. The nativebtoa()throws errors with Chinese characters because it only accepts Latin1 (0-255) code points, whileencodeToBase64properly converts Unicode to UTF-8 bytes before Base64 encoding.This directly fixes issue #2150 where Chinese usernames caused blank playground pages and console errors.
web/src/hooks/tokens/useTokensData.jsx (2)
23-29: Good addition of encoding utility to imports.The
encodeToBase64import complements existing helper utilities and enables Unicode-safe encoding.
124-155: Consistent Unicode-safe encoding for config data.Replacing
btoawithencodeToBase64ensures thatcherryConfigJSON can be safely encoded even if it contains non-ASCII characters. While the current config fields (baseUrl, apiKey, id) are typically ASCII, this change provides robustness and consistency with the encoding approach used elsewhere in the PR.web/src/helpers/base64.js (1)
20-35: Solid Unicode-to-binary-string conversion.The
toBinaryStringhelper correctly converts Unicode strings to binary strings suitable forbtoa:
- Primary path uses
TextEncoderto produce UTF-8 bytes, then converts each byte to a character- Fallback uses
encodeURIComponentwith regex replacement to achieve the same resultBoth paths correctly handle non-ASCII characters by representing UTF-8 bytes as a binary string (where each character has code point 0-255).
* main: (77 commits) refactor(adaptor): Comment out enable_thinking logic for clarity and future adjustments fix GetChannelKey AdminAuth -> RootAuth fix GetChannelKey AdminAuth -> RootAuth feat: vidu reference2video only viduq2 feat: vidu specify reference2video via metadata action 同步多语言README文档 chore: Update README.md for improved structure and clarity, including new sections for partners, acknowledgments, and deployment instructions feat: replicate channel flux model feat: ShouldPreserveThinkingSuffix (#2189) fix(channel): 当没有可用密钥时返回错误而不是第一个密钥 fix: update tag normalization regex feat: restrict automatic channel testing to master node only feat: EditTagModal header && param (#2159) add custom tool (#2157) fix playground (#2153) feat: add TASK_PRICE_PATCH environment variable for per-task billing configuration feat: EditTokenModal 中针对用户创建的 token 默认无限额度 feat: add environment variable switch for critical rate limit feat: enhance Ali video request processing with resolution mapping and size validation fix: logger ...
fix #2150
Summary by CodeRabbit