chaimi-keep-mcp 3.3.0-beta.4 → 3.3.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/oauth.js +35 -5
- package/package.json +1 -1
package/oauth.js
CHANGED
|
@@ -448,20 +448,40 @@ class FileTokenStorage extends TokenStorage {
|
|
|
448
448
|
const content = JSON.stringify(data, null, 2);
|
|
449
449
|
console.error('🔍 [DEBUG] 准备写入内容长度:', content.length, '字节');
|
|
450
450
|
|
|
451
|
+
// 【修复】使用原子写入:先写临时文件,再重命名
|
|
452
|
+
// 这样可以避免首次创建文件时的竞态条件
|
|
453
|
+
const tempFilePath = this.filePath + '.tmp';
|
|
454
|
+
|
|
455
|
+
// 写入临时文件
|
|
451
456
|
await this.fs.writeFile(
|
|
452
|
-
|
|
457
|
+
tempFilePath,
|
|
453
458
|
content,
|
|
454
|
-
|
|
459
|
+
'utf8'
|
|
455
460
|
);
|
|
456
461
|
|
|
457
|
-
//
|
|
462
|
+
// 设置文件权限(仅所有者可读写)
|
|
463
|
+
await this.fs.chmod(tempFilePath, 0o600);
|
|
464
|
+
|
|
465
|
+
// 验证临时文件写入是否成功
|
|
466
|
+
const tempStats = await this.fs.stat(tempFilePath);
|
|
467
|
+
console.error('🔍 [DEBUG] 临时文件写入成功,大小:', tempStats.size, '字节');
|
|
468
|
+
|
|
469
|
+
if (tempStats.size === 0) {
|
|
470
|
+
throw new Error('临时文件写入后大小为 0,写入失败');
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// 原子重命名(覆盖目标文件)
|
|
474
|
+
await this.fs.rename(tempFilePath, this.filePath);
|
|
475
|
+
console.error('🔍 [DEBUG] 文件重命名成功');
|
|
476
|
+
|
|
477
|
+
// 验证最终文件
|
|
458
478
|
const stats = await this.fs.stat(this.filePath);
|
|
459
|
-
console.error('🔍 [DEBUG] Token
|
|
479
|
+
console.error('🔍 [DEBUG] Token 文件保存成功');
|
|
460
480
|
console.error('🔍 [DEBUG] 文件路径:', this.filePath);
|
|
461
481
|
console.error('🔍 [DEBUG] 文件大小:', stats.size, '字节');
|
|
462
482
|
|
|
463
483
|
if (stats.size === 0) {
|
|
464
|
-
throw new Error('Token
|
|
484
|
+
throw new Error('Token 文件大小为 0,写入失败');
|
|
465
485
|
}
|
|
466
486
|
|
|
467
487
|
// 立即读取验证
|
|
@@ -474,6 +494,16 @@ class FileTokenStorage extends TokenStorage {
|
|
|
474
494
|
} catch (error) {
|
|
475
495
|
console.error('❌ Token 保存失败:', error.message);
|
|
476
496
|
console.error('❌ 错误详情:', error);
|
|
497
|
+
|
|
498
|
+
// 清理临时文件
|
|
499
|
+
try {
|
|
500
|
+
const tempFilePath = this.filePath + '.tmp';
|
|
501
|
+
await this.fs.unlink(tempFilePath);
|
|
502
|
+
console.error('🔍 [DEBUG] 已清理临时文件');
|
|
503
|
+
} catch (cleanupError) {
|
|
504
|
+
// 忽略清理错误
|
|
505
|
+
}
|
|
506
|
+
|
|
477
507
|
throw error;
|
|
478
508
|
}
|
|
479
509
|
}
|