Skip to content

WorkBuddy、CodeBuddy 桌面端增强助手:签到猫猫旅行成长计划查询、多账号独立备份、点切即用;免打扰模式让 AI 无人值守跑长任务;跨账号会话迁移、异常中断自动续接;暂存/快捷提示词;毛玻璃主题与更多实用功能。

1.7 KB
Raw Blame
'use strict';

const crypto = require('crypto');
const fs = require('fs');

const WINDOWS_TRANSIENT_CODES = new Set(['UNKNOWN', 'EPERM', 'EACCES', 'EBUSY', 'EMFILE', 'ENFILE']);

function defaultSleep(ms) {
  if (ms <= 0) return;
  const buffer = new SharedArrayBuffer(4);
  Atomics.wait(new Int32Array(buffer), 0, 0, ms);
}

function defaultSuffix() {
  return `${process.pid}-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`;
}

function replaceFileWithRetry(file, content, mode, options = {}) {
  const fileSystem = options.fs || fs;
  const platform = options.platform || process.platform;
  const sleep = options.sleep || defaultSleep;
  const suffix = options.suffix || defaultSuffix;
  const maxAttempts = platform === 'win32' ? 80 : 1;
  const tmp = `${file}.wbs-tmp-${suffix()}`;
  let tempReady = false;
  let lastError = null;

  for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
    try {
      if (!tempReady) {
        fileSystem.writeFileSync(tmp, content, 'utf8');
        if (mode !== undefined) fileSystem.chmodSync(tmp, mode);
        tempReady = true;
      }
      fileSystem.renameSync(tmp, file);
      return;
    } catch (error) {
      lastError = error;
      const retryable = platform === 'win32' && WINDOWS_TRANSIENT_CODES.has(error && error.code);
      if (!tempReady) {
        try { fileSystem.unlinkSync(tmp); } catch (_) {}
      }
      if (!retryable || attempt === maxAttempts) break;
      if (attempt === 1 && error && error.code === 'EPERM') {
        try { fileSystem.chmodSync(file, 0o666); } catch (_) {}
      }
      sleep(Math.min(250, 50 + attempt * 10));
    }
  }

  try { fileSystem.unlinkSync(tmp); } catch (_) {}
  throw lastError;
}

module.exports = { replaceFileWithRetry, WINDOWS_TRANSIENT_CODES };