Repository navigation
Expand file tree
/
Copy pathwebcontainer-setup.js
More file actions
99 lines (78 loc) · 3.12 KB
/
Copy pathwebcontainer-setup.js
File metadata and controls
99 lines (78 loc) · 3.12 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Web Container Setup Script
// This script sets up environment variables needed for the application
// to run in web containers like StackBlitz and CodeSandbox
import { writeFileSync, existsSync, readFileSync } from 'fs';
import { join } from 'path';
import crypto from 'crypto';
// Generate a strong random string for secrets - safe for web containers
const generateSecret = (length = 32) => {
// Generate random bytes in a single call (more efficient)
const randomBytes = crypto.randomBytes(length);
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=';
let result = '';
// Map each byte to a character
for (let i = 0; i < length; i++) {
result += chars.charAt(randomBytes[i] % chars.length);
}
return result;
};
// Set up .env file with authentication secrets
const setupEnv = () => {
try {
const envPath = join(process.cwd(), '.env');
console.log('🔧 Setting up environment for web container...');
// Generate secure secrets
const jwtSecret = generateSecret();
// Default environment content
let envContent = '';
// Read existing .env file if it exists
if (existsSync(envPath)) {
console.log('📄 Reading existing .env file...');
envContent = readFileSync(envPath, 'utf8');
// Split into lines for easier manipulation
const lines = envContent.split('\n');
let jwtSet = false;
// Process each line to find and replace JWT_SECRET and COOKIE_SECRET
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('JWT_SECRET=')) {
// Replace line with the new JWT_SECRET
lines[i] = `JWT_SECRET=${jwtSecret}`;
jwtSet = true;
console.log('✅ Updated JWT_SECRET with a secure value');
}
}
// Add secrets if they don't exist in the file
if (!jwtSet) {
lines.push('\n# Authentication secrets');
lines.push(`JWT_SECRET=${jwtSecret}`);
console.log('✅ Added JWT_SECRET to .env file');
}
// Reassemble the .env content
envContent = lines.join('\n');
} else {
console.log('📄 Creating new .env file with authentication secrets...');
envContent = `# Generated by webcontainer-setup.js
# Database configuration
DATABASE_CLIENT=sqlite3
# Authentication secrets
JWT_SECRET=${jwtSecret}
`;
}
// Write the updated .env file
writeFileSync(envPath, envContent);
console.log('✅ Environment setup complete!');
console.log('🔑 JWT_SECRET has been generated');
// Also set these in process.env for immediate use
process.env.JWT_SECRET = jwtSecret;
// Optional: Force log the values for debugging
console.log(`JWT_SECRET set to: ${jwtSecret.substring(0, 3)}...${jwtSecret.substring(jwtSecret.length-3)}`);
console.log('COOKIE_SECRET has been removed.');
} catch (error) {
console.error('❌ Error setting up environment variables:', error.message);
process.exit(1); // Exit with error code
}
};
// Run the setup
setupEnv();
// Export the setup function for use in other files
export default setupEnv;