Skip to content

Conversation

Copy link

Copilot AI commented Nov 29, 2025

Applies code review fixes for unnecessary complexity in string handling, incorrect binary data conversion methods, and inconsistent null/undefined usage.

Changes

  • server/index.js: Replace String.raw template with regular string literal for newline replacement
  • server/index.js: Add length validation (6-32 digits) to normalizePhone() to validate normalized output
  • services/firebaseClient.ts: Remove undefined from type unions, use null consistently for uninitialized state
  • services/geminiService.ts: Replace String.fromCodePoint() with String.fromCharCode() for byte-to-string conversion
  • utils/metadata.ts: Replace codePointAt() with charCodeAt() for base64 binary decoding
  • vite.config.ts: Extract hardcoded base path to VITE_PROD_BASE_PATH env var

Binary data fix

fromCodePoint/codePointAt handle Unicode code points which can exceed byte range. For binary data (0-255), fromCharCode/charCodeAt is correct:

// Before (incorrect for binary)
bytes[i] = binary.codePointAt(i) ?? 0;

// After
bytes[i] = binary.charCodeAt(i);
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...

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…nfiguration

Co-authored-by: morningstarxcdcode <205398826+morningstarxcdcode@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix string literal replacement in private key handling Refactor: Fix string/binary handling and improve type consistency Nov 29, 2025
Copilot finished work on behalf of morningstarxcdcode November 29, 2025 04:04
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