1'use strict'; 2 3// Flags: --expose-internals 4 5const common = require('../common'); 6 7const assert = require('assert'); 8const { BigIntStats } = require('internal/fs/utils'); 9const fs = require('fs'); 10const path = require('path'); 11 12const tmpdir = require('../common/tmpdir'); 13 14const enoentFile = path.join(tmpdir.path, 'non-existent-file'); 15const expectedStatObject = new BigIntStats( 16 0n, // dev 17 0n, // mode 18 0n, // nlink 19 0n, // uid 20 0n, // gid 21 0n, // rdev 22 0n, // blksize 23 0n, // ino 24 0n, // size 25 0n, // blocks 26 0n, // atimeMs 27 0n, // mtimeMs 28 0n, // ctimeMs 29 0n, // birthtimeMs 30 0n, // atimeNs 31 0n, // mtimeNs 32 0n, // ctimeNs 33 0n // birthtimeNs 34); 35 36tmpdir.refresh(); 37 38// If the file initially didn't exist, and gets created at a later point of 39// time, the callback should be invoked again with proper values in stat object 40let fileExists = false; 41const options = { interval: 0, bigint: true }; 42 43const watcher = 44 fs.watchFile(enoentFile, options, common.mustCall((curr, prev) => { 45 if (!fileExists) { 46 // If the file does not exist, all the fields should be zero and the date 47 // fields should be UNIX EPOCH time 48 assert.deepStrictEqual(curr, expectedStatObject); 49 assert.deepStrictEqual(prev, expectedStatObject); 50 // Create the file now, so that the callback will be called back once the 51 // event loop notices it. 52 fs.closeSync(fs.openSync(enoentFile, 'w')); 53 fileExists = true; 54 } else { 55 // If the ino (inode) value is greater than zero, it means that the file 56 // is present in the filesystem and it has a valid inode number. 57 assert(curr.ino > 0n); 58 // As the file just got created, previous ino value should be lesser than 59 // or equal to zero (non-existent file). 60 assert(prev.ino <= 0n); 61 // Stop watching the file 62 fs.unwatchFile(enoentFile); 63 watcher.stop(); // Stopping a stopped watcher should be a noop 64 } 65 }, 2)); 66 67// 'stop' should only be emitted once - stopping a stopped watcher should 68// not trigger a 'stop' event. 69watcher.on('stop', common.mustCall(function onStop() {})); 70