瀏覽代碼

fix playground (#2153)

Seefs 1 月之前
父節點
當前提交
fb610e62a0
共有 4 個文件被更改,包括 67 次插入3 次删除
  1. 56 0
      web/src/helpers/base64.js
  2. 1 0
      web/src/helpers/index.js
  3. 8 2
      web/src/hooks/tokens/useTokensData.jsx
  4. 2 1
      web/src/pages/Playground/index.jsx

+ 56 - 0
web/src/helpers/base64.js

@@ -0,0 +1,56 @@
+/*
+Copyright (C) 2025 QuantumNous
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+For commercial licensing, please contact [email protected]
+*/
+
+const toBinaryString = (text) => {
+  if (typeof TextEncoder !== 'undefined') {
+    const bytes = new TextEncoder().encode(text);
+    let binary = '';
+
+    bytes.forEach((byte) => {
+      binary += String.fromCharCode(byte);
+    });
+
+    return binary;
+  }
+
+  return encodeURIComponent(text).replace(/%([0-9A-F]{2})/g, (_, hex) =>
+    String.fromCharCode(parseInt(hex, 16)),
+  );
+};
+
+export const encodeToBase64 = (value) => {
+  const input = value == null ? '' : String(value);
+
+  if (typeof window === 'undefined') {
+    if (typeof Buffer !== 'undefined') {
+      return Buffer.from(input, 'utf-8').toString('base64');
+    }
+    if (
+      typeof globalThis !== 'undefined' &&
+      typeof globalThis.btoa === 'function'
+    ) {
+      return globalThis.btoa(toBinaryString(input));
+    }
+    throw new Error(
+      'Base64 encoding is unavailable in the current environment',
+    );
+  }
+
+  return window.btoa(toBinaryString(input));
+};

+ 1 - 0
web/src/helpers/index.js

@@ -20,6 +20,7 @@ For commercial licensing, please contact [email protected]
 export * from './history';
 export * from './auth';
 export * from './utils';
+export * from './base64';
 export * from './api';
 export * from './render';
 export * from './log';

+ 8 - 2
web/src/hooks/tokens/useTokensData.jsx

@@ -20,7 +20,13 @@ For commercial licensing, please contact [email protected]
 import { useState, useEffect } from 'react';
 import { useTranslation } from 'react-i18next';
 import { Modal } from '@douyinfe/semi-ui';
-import { API, copy, showError, showSuccess } from '../../helpers';
+import {
+  API,
+  copy,
+  showError,
+  showSuccess,
+  encodeToBase64,
+} from '../../helpers';
 import { ITEMS_PER_PAGE } from '../../constants';
 import { useTableCompactMode } from '../common/useTableCompactMode';
 
@@ -136,7 +142,7 @@ export const useTokensData = (openFluentNotification) => {
         apiKey: 'sk-' + record.key,
       };
       let encodedConfig = encodeURIComponent(
-        btoa(JSON.stringify(cherryConfig)),
+        encodeToBase64(JSON.stringify(cherryConfig)),
       );
       url = url.replaceAll('{cherryConfig}', encodedConfig);
     } else {

+ 2 - 1
web/src/pages/Playground/index.jsx

@@ -47,6 +47,7 @@ import {
   createLoadingAssistantMessage,
   getTextContent,
   buildApiPayload,
+  encodeToBase64,
 } from '../../helpers';
 
 // Components
@@ -72,7 +73,7 @@ const generateAvatarDataUrl = (username) => {
       <text x="50%" y="50%" dominant-baseline="central" text-anchor="middle" font-size="16" fill="#ffffff" font-family="sans-serif">${firstLetter}</text>
     </svg>
   `;
-  return `data:image/svg+xml;base64,${btoa(svg)}`;
+  return `data:image/svg+xml;base64,${encodeToBase64(svg)}`;
 };
 
 const Playground = () => {