46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const filePath = path.join(__dirname, 'src/core/security/SecurityHardeningService.ts');
|
||
|
|
|
||
|
|
// 使用 Buffer 读取文件,确保不改变编码
|
||
|
|
const buffer = fs.readFileSync(filePath);
|
||
|
|
let content = buffer.toString('utf8');
|
||
|
|
|
||
|
|
console.log('Original lines:', content.split('\n').length);
|
||
|
|
console.log('Original bytes:', buffer.length);
|
||
|
|
|
||
|
|
// 1. 替换导入语句
|
||
|
|
content = content.replace(
|
||
|
|
"import { RedisService } from '../cache/RedisService';",
|
||
|
|
"import RedisService from '../../services/RedisService';"
|
||
|
|
);
|
||
|
|
|
||
|
|
// 2. 替换 securityCheckInterval 属性
|
||
|
|
content = content.replace(
|
||
|
|
'private securityCheckInterval: NodeJS.Timeout;',
|
||
|
|
'private securityCheckInterval!: NodeJS.Timeout;'
|
||
|
|
);
|
||
|
|
|
||
|
|
// 3. 移除构造函数中的 redisService 参数
|
||
|
|
content = content.replace(
|
||
|
|
/constructor\(\s*private readonly configService: ConfigService,\s*private readonly redisService: RedisService,\s*\) \{\}/,
|
||
|
|
'constructor(\n private readonly configService: ConfigService,\n ) {}'
|
||
|
|
);
|
||
|
|
|
||
|
|
// 4. 替换所有 this.redisService. 为 RedisService.
|
||
|
|
content = content.replace(/this\.redisService\./g, 'RedisService.');
|
||
|
|
|
||
|
|
// 5. 修复 RedisService.set 调用 - 移除 'EX' 参数
|
||
|
|
// 只替换 , 'EX', 数字 的模式(包括换行)
|
||
|
|
content = content.replace(/,\s*'EX',\s*(\d+)/g, ', $1');
|
||
|
|
|
||
|
|
console.log('After replacement lines:', content.split('\n').length);
|
||
|
|
console.log('After replacement bytes:', Buffer.byteLength(content, 'utf8'));
|
||
|
|
|
||
|
|
// 使用 Buffer 写入文件,确保不添加 BOM
|
||
|
|
const newBuffer = Buffer.from(content, 'utf8');
|
||
|
|
fs.writeFileSync(filePath, newBuffer);
|
||
|
|
|
||
|
|
console.log('File updated successfully');
|