Skip to content

Conversation

Copy link

Copilot AI commented Nov 29, 2025

Applies code quality fixes: simplifies unnecessary String.raw usage, corrects binary data encoding/decoding methods, removes type ambiguity, and extracts hardcoded config.

String handling

  • server/index.js: Replace String.raw\\n`with'\n'` for private key newline replacement
  • services/geminiService.ts: Use String.fromCharCode() instead of String.fromCodePoint() for byte-to-binary conversion
  • utils/metadata.ts: Use charCodeAt() instead of codePointAt() for binary string decoding

Validation

  • server/index.js: normalizePhone() now validates digit count (6-32) before returning

Type consistency

  • services/firebaseClient.ts: Remove undefined from type union, use explicit null initialization with firebaseInitialized flag to track state

Configuration

  • vite.config.ts: Extract base path to VITE_PROD_BASE_PATH env var with fallback
// Before
const privateKey = rawPrivateKey.replaceAll(String.raw`\n`, '\n');

// After
const privateKey = rawPrivateKey.replaceAll('\\n', '\n');
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"Using String.raw template literal for a simple string replacement is unnecessarily complex. Use a regular string literal instead: rawPrivateKey.replaceAll('\\\\n', '\\n').","fixFiles":[{"filePath":"server/index.js","diff":"diff --git a/server/index.js b/server/index.js\n--- a/server/index.js\n+++ b/server/index.js\n@@ -124,7 +124,7 @@\n   const projectId = process.env.FIREBASE_PROJECT_ID;\n   const clientEmail = process.env.FIREBASE_CLIENT_EMAIL;\n   const rawPrivateKey = process.env.FIREBASE_PRIVATE_KEY;\n-  const privateKey = rawPrivateKey ? rawPrivateKey.replaceAll(String.raw`\\n`, '\\n') : undefined;\n+  const privateKey = rawPrivateKey ? rawPrivateKey.replaceAll('\\\\n', '\\n') : undefined;\n \n   if (projectId && clientEmail && privateKey) {\n     if (!admin.apps.length) {\n"}]},{"message":"The phone normalization only removes non-digits but doesn't validate the result. Consider adding validation to ensure the normalized phone number has a reasonable length and format before processing.","fixFiles":[{"filePath":"server/index.js","diff":"diff --git a/server/index.js b/server/index.js\n--- a/server/index.js\n+++ b/server/index.js\n@@ -179,7 +179,15 @@\n });\n \n const normalizeEmail = (email) => email.trim().toLowerCase();\n-const normalizePhone = (phone) => phone.replaceAll(/\\D/g, '');\n+const normalizePhone = (phone) => {\n+  const digits = phone.replaceAll(/\\D/g, '');\n+  const minLen = 6;\n+  const maxLen = 32;\n+  if (digits.length < minLen || digits.length > maxLen) {\n+    throw new Error('Normalized phone number has invalid length.');\n+  }\n+  return digits;\n+};\n \n const normalizeAddress = (address) => {\n   if (typeof address !== 'string' || !address.startsWith('0x')) {\n"}]},{"message":"Using both null and undefined in the type union creates ambiguity. Choose either null or undefined consistently for uninitialized state to improve code clarity and reduce confusion.","fixFiles":[{"filePath":"services/firebaseClient.ts","diff":"diff --git a/services/firebaseClient.ts b/services/firebaseClient.ts\n--- a/services/firebaseClient.ts\n+++ b/services/firebaseClient.ts\n@@ -22,8 +22,8 @@\n   type UserCredential,\n } from 'firebase/auth';\n \n-let firebaseApp: FirebaseApp | null | undefined;\n-let firebaseAuth: Auth | null | undefined;\n+let firebaseApp: FirebaseApp | null;\n+let firebaseAuth: Auth | null;\n \n const loadFirebaseConfig = () => {\n   const apiKey = import.meta.env.VITE_FIREBASE_API_KEY;\n@@ -50,7 +50,7 @@\n };\n \n const ensureFirebase = (): { app: FirebaseApp; auth: Auth } | null => {\n-  if (firebaseAuth !== undefined && firebaseApp !== undefined) {\n+  if (firebaseAuth !== null && firebaseApp !== null) {\n     if (firebaseApp && firebaseAuth) {\n       return { app: firebaseApp, auth: firebaseAuth };\n     }\n"}]},{"message":"Using both null and undefined in the type union creates ambiguity. Choose either null or undefined consistently for uninitialized state to improve code clarity and reduce confusion.","fixFiles":[{"filePath":"services/firebaseClient.ts","diff":"diff --git a/services/firebaseClient.ts b/services/firebaseClient.ts\n--- a/services/firebaseClient.ts\n+++ b/services/firebaseClient.ts\n@@ -22,8 +22,8 @@\n   type UserCredential,\n } from 'firebase/auth';\n \n-let firebaseApp: FirebaseApp | null | undefined;\n-let firebaseAuth: Auth | null | undefined;\n+let firebaseApp: FirebaseApp | null;\n+let firebaseAuth: Auth | null;\n \n const loadFirebaseConfig = () => {\n   const apiKey = import.meta.env.VITE_FIREBASE_API_KEY;\n@@ -50,7 +50,7 @@\n };\n \n const ensureFirebase = (): { app: FirebaseApp; auth: Auth } | null => {\n-  if (firebaseAuth !== undefined && firebaseApp !== undefined) {\n+  if (firebaseAuth !== null && firebaseApp !== null) {\n     if (firebaseApp && firebaseAuth) {\n       return { app: firebaseApp, auth: firebaseAuth };\n     }\n"}]},{"message":"Using String.fromCodePoint() for binary data conversion is incorrect. For converting bytes to a binary string for btoa(), use String.fromCharCode() instead, as fromCodePoint() is designed for Unicode code points, not raw byte values.","fixFiles":[{"filePath":"services/geminiService.ts","diff":"diff --git a/services/geminiService.ts b/services/geminiService.ts\n--- a/services/geminiService.ts\n+++ b/services/geminiService.ts\n@@ -263,7 +263,7 @@\n         const bytes = new Uint8Array(result);\n         let binary = '';\n         for (let i = 0; i < bytes.byteLength; i++) {\n-          binary += String.fromCodePoint(bytes[i]);\n+          binary += String.fromCharCode(bytes[i]);\n         }\n         resolve(btoa(binary));\n       } else {\n"}]},{"message":"Using codePointAt() for binary data conversion is incorrect. Binary strings from atob() contain byte values 0-255, but codePointAt() returns Unicode code points which can be much lar...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 29, 2025 04:00
…dation, improve type consistency

Co-authored-by: morningstarxcdcode <205398826+morningstarxcdcode@users.noreply.github.com>
Co-authored-by: morningstarxcdcode <205398826+morningstarxcdcode@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix string replacement method in server/index.js Refactor: simplify string handling, fix binary encoding, improve type consistency Nov 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants