chaimi-keep-mcp 3.3.0-beta.4 → 3.3.0-beta.5

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.
Files changed (2) hide show
  1. package/oauth.js +31 -4
  2. package/package.json +1 -1
package/oauth.js CHANGED
@@ -448,20 +448,37 @@ 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
- this.filePath,
457
+ tempFilePath,
453
458
  content,
454
459
  { mode: 0o600 }
455
460
  );
456
461
 
457
- // 验证写入是否成功
462
+ // 验证临时文件写入是否成功
463
+ const tempStats = await this.fs.stat(tempFilePath);
464
+ console.error('🔍 [DEBUG] 临时文件写入成功,大小:', tempStats.size, '字节');
465
+
466
+ if (tempStats.size === 0) {
467
+ throw new Error('临时文件写入后大小为 0,写入失败');
468
+ }
469
+
470
+ // 原子重命名(覆盖目标文件)
471
+ await this.fs.rename(tempFilePath, this.filePath);
472
+ console.error('🔍 [DEBUG] 文件重命名成功');
473
+
474
+ // 验证最终文件
458
475
  const stats = await this.fs.stat(this.filePath);
459
- console.error('🔍 [DEBUG] Token 文件写入成功');
476
+ console.error('🔍 [DEBUG] Token 文件保存成功');
460
477
  console.error('🔍 [DEBUG] 文件路径:', this.filePath);
461
478
  console.error('🔍 [DEBUG] 文件大小:', stats.size, '字节');
462
479
 
463
480
  if (stats.size === 0) {
464
- throw new Error('Token 文件写入后大小为 0,写入失败');
481
+ throw new Error('Token 文件大小为 0,写入失败');
465
482
  }
466
483
 
467
484
  // 立即读取验证
@@ -474,6 +491,16 @@ class FileTokenStorage extends TokenStorage {
474
491
  } catch (error) {
475
492
  console.error('❌ Token 保存失败:', error.message);
476
493
  console.error('❌ 错误详情:', error);
494
+
495
+ // 清理临时文件
496
+ try {
497
+ const tempFilePath = this.filePath + '.tmp';
498
+ await this.fs.unlink(tempFilePath);
499
+ console.error('🔍 [DEBUG] 已清理临时文件');
500
+ } catch (cleanupError) {
501
+ // 忽略清理错误
502
+ }
503
+
477
504
  throw error;
478
505
  }
479
506
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chaimi-keep-mcp",
3
- "version": "3.3.0-beta.4",
3
+ "version": "3.3.0-beta.5",
4
4
  "description": "柴米记账 MCP Server - 支持 Claude、Cursor、OpenClaw、WorkBuddy 等 AI 工具直接记账",
5
5
  "main": "server.js",
6
6
  "bin": {