从 fs.readFile 中获取空字符串 chokidar.watch(path_file).on('change', ...)

Getting empty string from fs.readFile inside chokidar.watch(path_file).on('change', ...)

我有以下非常简单的 Node 项目:

https://github.com/tlg-265/chokidar-issue

$ git clone https://github.com/tlg-265/chokidar-issue
$ cd chokidar-issue
$ npm i
$ npm run watch-changes

主要负责检测文件的更改:

/profiles/bill-gates.json

然后做一个动作。

为此,我有以下文件:

/profile-watcher.js

const fs = require('fs-extra');
const colors = require('colors/safe');
const chokidar = require('chokidar');

const path_file = `profiles/bill-gates.json`;
console.log(`Current Profile: ${colors.red.bgBrightYellow(path_file)}`);

let profile_before = {};

chokidar.watch(path_file).on('change', async (path) => {

  console.log();
  console.log(`${colors.blue.bgYellow(`->`)} Profile changed: ${path}`);

  fs.readFile(path, (err, profile_json) => {
    console.log(`->${profile_json}<-`);
    let profile = JSON.parse(profile_json);
    if (JSON.stringify(profile) != JSON.stringify(profile_before)) {
      console.log('The profile has changed.');
      profile_before = profile;
    }
  });

});

当我 运行 项目时:

$ npm run watch-changes

并对文件进行以下修改:/profiles/bill-gates.json

它工作正常,将这个文件的内容输出到控制台。

但是当我进行下一次修改时:

然后我得到以下错误:

-> Profile changed: profiles\bill-gates.json
-><-
undefined:1

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at fs.readFile (\chokidar-issue\profile-watcher.js:17:24)
    at \chokidar-issue\node_modules\graceful-fs\graceful-fs.js:115:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! chokidar-issue@1.0.0 watch-changes: `node profile-watcher.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the chokidar-issue@1.0.0 watch-changes script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Roaming\npm-cache\_logs20-02-28T23_44_01_038Z-debug.log

/profiles/bill-gates.json(标志:UTF-8 / CRLF

{
  "name": "Bill Gates",
  "email": "bill.gates@microsoft.com",
  "password": "windows",
  "country": "USA"
}

顺便说一句,如果我从 CRLF 更改为 LF 通常我可以在它崩溃之前做更多的修改。

我的印象是,由于某种原因,文件:/profiles/bill-gates.json 在某个时候被锁定,当 Node 试图读取它时 returns 一个空字符串,因为它已锁定。

关于如何在几次尝试后不崩溃的情况下完成这项工作有什么想法吗?

谢谢!

这可能是竞争条件。像这样让你的 JSON.parse 安全:

const path = require('path')

chokidar.watch(path_file).on('change', async (path) => {
  fs.readFile(path, 'utf8', (err, profile_json) => {
    if (!profile_json) {
      console.log(`${path} is an empty file!`)
      return
    }
    const profile = JSON.parse(profile_json);
    if (JSON.stringify(profile) != JSON.stringify(profile_before)) {
      console.log('The profile has changed.');
      profile_before = profile;
    }
  });

});

我可以通过添加一些恢复回退来使其工作:

const fs = require('fs-extra');
const colors = require('colors/safe');
const chokidar = require('chokidar');
const sleep = require('sleep');

const path_file = `profiles/bill-gates.json`;
console.log(`Current Profile: ${colors.red.bgBrightYellow(path_file)}`);

let profile_before = fs.readFileSync(path_file).toString();

chokidar.watch(path_file).on('change', async (path_changed) => {
  let profile = fs.readFileSync(path_changed).toString();
  if (IsValidJson(profile)) {
    if (profile != profile_before) {
      console.log();
      console.log(`Profile changed: ${colors.red.bgBrightYellow(path_changed)}`);
      process_profile(profile);
      profile_before = profile;
    }
  }
  else {
    sleep.msleep(100); // this is necessary
  }
});

function process_profile(profile_json) {
  const profile = JSON.parse(profile_json);
  console.log(`${profile_json}`);
  console.log(profile.name);
}

function IsValidJson(str) {
  try {
    JSON.parse(str);
  } catch (e) {
    return false;
  }
  return true;
}

似乎当您保存文件时(至少在 Windows),有时在文件变得清晰和几毫秒后它得到实际文件之间有一段时间(非常非常短的时间)内容。在这两种情况下, on-change 事件都会被触发。所以,我们只需要验证文件的内容是否为JSON。在那种情况下,我只需要忽略它并等待下一个 on-change 事件。

我遇到了和你一样的问题

“chokidar”中有一个选项可以awaitWriteFinish。它是基于时间的,并检查文件的大小是否在变化。如果没有,那么它将调用回调。

const watcher = chokidar.watch(configPathString, { 
    persistent: true,
    awaitWriteFinish: {
        stabilityThreshold: 500
    } 
});
watcher.on('change', (path, stats) => {

    fs.readFile(configPathString,(err, data)=>{
        if (err) throw err;
        
        //console.log('data',data);

        let receivedData = JSON.parse(data);

        //Do whatever you like
    })
});