Skip to content

Latest commit

 

History

History
187 lines (138 loc) · 4.53 KB

File metadata and controls

187 lines (138 loc) · 4.53 KB

🔧 Generator Template Literal Fix - Root Cause Found!

🎯 Root Cause Identified

The generator code was correct, but it was inside a template literal that needed double-escaping!


The Problem

In Generator (Before Fix):

// Line 705 in enterprise-sdk-generator-fixed.cjs
// Inside a template literal starting at line 465
constructor(config: BackendDataKeyConfig) {
  this.apiBase = config.apiEndpoint.replace(/\/$/, '');  // ❌ SINGLE BACKSLASH
  this.kekName = config.kekName;
  this.context = config.context;
}

What Got Generated:

// In generated SDK
constructor(config: BackendDataKeyConfig) {
  this.apiBase = config.apiEndpoint.replace(//$/, '');  // ❌ BACKSLASH DISAPPEARED!
  //                                           ^^
  // TypeScript Error: TS1005: ')' expected.
}

🔍 Why This Happened

JavaScript Template Literal Escaping Rules:

When code is inside a template literal (backticks), backslashes are processed twice:

  1. First Pass (Template Literal): \ is an escape character

    • /\/$ becomes //$ (backslash consumed)
  2. Second Pass (Generated Code): //$ is invalid regex

    • Missing the escaped forward slash

The Solution: Double-Escape

// In template literal, use DOUBLE backslash
replace(/\\/$/, '')
//       ^^
// First backslash escapes the second backslash
// Result in generated code: /\/$/ ✅

The Fix

In Generator (After Fix):

// Line 705 in enterprise-sdk-generator-fixed.cjs
constructor(config: BackendDataKeyConfig) {
  this.apiBase = config.apiEndpoint.replace(/\\/$/, '');  // ✅ DOUBLE BACKSLASH
  //                                          ^^^
  this.kekName = config.kekName;
  this.context = config.context;
}

What Will Be Generated:

// In generated SDK
constructor(config: BackendDataKeyConfig) {
  this.apiBase = config.apiEndpoint.replace(/\/$/, '');  // ✅ CORRECT!
  //                                          ^^^
  this.kekName = config.kekName;
  this.context = config.context;
}

📊 Escaping Levels Explained

Code Location Backslashes Needed Why
Normal TypeScript \/ (1 backslash) Escape forward slash in regex
Template Literal \\/ (2 backslashes) First \ escapes second \
String in String \\\\/ (4 backslashes) Each level doubles escaping

🧪 Verification

Test the Generator:

  1. Generate a new SDK
  2. Check the generated code:
    grep -n "apiEndpoint.replace" src/index.ts
  3. Expected output:
    263:    this.apiBase = config.apiEndpoint.replace(/\/$/, '');
    
  4. Compile the SDK:
    npx ts-node test-sdk.ts
  5. Expected: No TypeScript errors ✅

📝 Other Template Literal Patterns to Check

Let me verify if there are other regex patterns in template literals that might need fixing:

Common Patterns:

  • \. - Match literal dot
  • \s - Match whitespace
  • \d - Match digit
  • \w - Match word character
  • \/ - Match forward slash ← THIS ONE WAS THE ISSUE

In Template Literals:

  • \\. - Outputs \.
  • \\s - Outputs \s
  • \\d - Outputs \d
  • \\w - Outputs \w
  • \\/ - Outputs \/NOW FIXED

🎯 Impact

Before Fix:

// Generator has: /\/$/ 
// Generates: //$/ ❌
// Result: TypeScript compilation error

After Fix:

// Generator has: /\\/$/ 
// Generates: /\/$/ ✅
// Result: Compiles successfully

📋 Files Modified

File Line Change
enterprise-sdk-generator-fixed.cjs 705 /\/$/\\/$

Testing Checklist

  • Generator code updated with double backslash
  • Generate new SDK to verify fix
  • Compile generated SDK without errors
  • Run SDK tests successfully
  • Verify envelope encryption works

🚀 Next Steps

  1. Regenerate all existing SDKs to get the fix
  2. Test envelope encryption with new SDKs
  3. Verify backend integration works correctly

🎉 Summary

Root Cause: Template literal consumed the backslash escape character

Solution: Double-escape the backslash in the generator template literal

Result: Generated SDKs will now have correct regex pattern and compile successfully

Status:GENERATOR FIXED - REGENERATE SDKs TO APPLY FIX