File size: 2,185 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node

/**
 * OmniRoute v3.3 -> v3.4 Environment Migration Script
 * Resolves breaking changes in environment variables format and validation.
 */

import fs from "fs";
import path from "path";
import crypto from "crypto";

const envPath = path.resolve(process.cwd(), ".env");

if (!fs.existsSync(envPath)) {
  console.log("No .env file found. Migration skipped.");
  process.exit(0);
}

let content = fs.readFileSync(envPath, "utf8");
let modified = false;

// 1. Migrate NEXTAUTH_SECRET to JWT_SECRET if missing
const nextAuthMatch = content.match(/^NEXTAUTH_SECRET=(.+)$/m);
const jwtMatch = content.match(/^JWT_SECRET=(.+)$/m);

if (nextAuthMatch && !jwtMatch) {
  console.log("Migrating NEXTAUTH_SECRET to JWT_SECRET...");
  let newJwt = nextAuthMatch[1].trim();

  // Enforce 32 char minimum for secretsValidator.ts
  if (newJwt.length < 32) {
    console.warn(
      `Original NEXTAUTH_SECRET was too short (${newJwt.length} chars). Generating a secure one...`
    );
    newJwt = crypto.randomBytes(48).toString("base64");
  }

  content += `\n# Migrated from NEXTAUTH_SECRET\nJWT_SECRET=${newJwt}\n`;
  modified = true;
} else if (jwtMatch && jwtMatch[1].trim().length < 32) {
  console.warn(
    `JWT_SECRET is too short (${jwtMatch[1].trim().length} chars). Generating a secure one for v3.4.0+...`
  );
  const newJwt = crypto.randomBytes(48).toString("base64");
  content = content.replace(/^JWT_SECRET=(.*)$/m, `JWT_SECRET=${newJwt}`);
  modified = true;
}

// 2. Ensure API_KEY_SECRET exists (required in 3.4.0)
if (!content.match(/^API_KEY_SECRET=/m)) {
  console.log("Adding required API_KEY_SECRET for v3.4.0...");
  const newApiSecret = crypto.randomBytes(32).toString("hex");
  content += `\n# Required for v3.4.0 API Key HMAC\nAPI_KEY_SECRET=${newApiSecret}\n`;
  modified = true;
}

if (modified) {
  // Backup old .env
  fs.writeFileSync(envPath + ".bak", fs.readFileSync(envPath));
  console.log("Created backup at .env.bak");

  // Write new .env
  fs.writeFileSync(envPath, content, "utf8");
  console.log("Successfully migrated .env file for OmniRoute 3.4.x.");
} else {
  console.log(".env file is already compatible with OmniRoute 3.4.x.");
}