import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import {
  writeFileSync,
  mkdirSync,
  chmodSync,
  existsSync,
  renameSync,
  unlinkSync
} from "fs";
import { dirname, join } from "path";
import { randomBytes } from "crypto";
function writeFileSecure(filePath, data) {
  const dir = dirname(filePath);
  if (!existsSync(dir)) {
    mkdirSync(dir, { recursive: true, mode: 448 });
  }
  const tempPath = join(dir, `.tmp-${randomBytes(8).toString("hex")}`);
  try {
    writeFileSync(tempPath, data);
    chmodSync(tempPath, 384);
    renameSync(tempPath, filePath);
  } catch (error) {
    try {
      if (existsSync(tempPath)) {
        unlinkSync(tempPath);
      }
    } catch {
    }
    throw error;
  }
}
function ensureSecureDir(dirPath) {
  if (!existsSync(dirPath)) {
    mkdirSync(dirPath, { recursive: true, mode: 448 });
  } else {
    try {
      chmodSync(dirPath, 448);
    } catch {
    }
  }
}
export {
  ensureSecureDir,
  writeFileSecure
};
//# sourceMappingURL=secure-fs.js.map
