@ava/write-file-atomic

中文文档
2.2.0发布于 2017/06/25ISCCommonJS近 30 天下载 13.6万

Write files in an atomic fashion w/configurable ownership

原始使用文档
文档目录3 节
  1. write-file-atomic
  2. var writeFileAtomic = require('write-file-atomic') writeFil…
  3. var writeFileAtomicSync = require('write-file-atomic').sync…

write-file-atomic

Forked from https://github.com/npm/write-file-atomic to include https://github.com/npm/write-file-atomic/pull/25, for use with AVA.


This is an extension for node's fs.writeFile that makes its operation atomic and allows you set ownership (uid/gid of the file).

var writeFileAtomic = require('write-file-atomic')
writeFileAtomic(filename, data, [options], callback)

  • filename String
  • data String | Buffer
  • options Object
    • chown Object
      • uid Number
      • gid Number
    • encoding String | Null default = 'utf8'
    • fsync Boolean default = true
    • mode Number default = 438 (aka 0666 in Octal) callback Function

Atomically and asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.

The file is initially named filename + "." + murmurhex(__filename, process.pid, ++invocations). If writeFile completes successfully then, if passed the chown option it will change the ownership of the file. Finally it renames the file back to the filename you specified. If it encounters errors at any of these steps it will attempt to unlink the temporary file and then pass the error back to the caller.

If provided, the chown option requires both uid and gid properties or else you'll get an error.

The encoding option is ignored if data is a buffer. It defaults to 'utf8'.

If the fsync option is false, writeFile will skip the final fsync call.

The callback is always invoked with the initial (temporary) filename.

Example:

writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
});

var writeFileAtomicSync = require('write-file-atomic').sync
writeFileAtomicSync(filename, data, [options])

The synchronous version of writeFileAtomic. Returns the initial (temporary) filename.

版本与发布记录

@ava/write-file-atomic 共发布 1 个版本,首次发布于 2017/06/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2017/06/259 年前

关于 @ava/write-file-atomic 的常见问题

@ava/write-file-atomic 是什么?

Write files in an atomic fashion w/configurable ownership

如何安装 @ava/write-file-atomic?

在项目目录运行 npm install @ava/write-file-atomic,或使用 pnpm add / yarn add / bun add。

@ava/write-file-atomic 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ava__write-file-atomic 是否存在。

@ava/write-file-atomic 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ava/write-file-atomic 可以商用吗?

@ava/write-file-atomic 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ava/write-file-atomic 的下载量有多少?

根据 npm 官方下载统计,@ava/write-file-atomic 在 2026/10/07 – 2026/11/05 这 30 天内被下载 136,482 次,日均约 4,549 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ava/write-file-atomic 还在维护吗?

最新版本 2.2.0 发布于 2017/06/25(9 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/06/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。