From d92fb644cf184404c55bbab813a3dc4f255f2fec Mon Sep 17 00:00:00 2001 From: 43081j <43081j@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:24:47 +0100 Subject: [PATCH] feat: move to node:fs/promises Moves to using `node:fs/promises` rather than using `promisify` manually. --- src/index.ts | 5 +---- src/nodefs-handler.ts | 18 ++++++++++-------- test.mjs | 6 ++++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index ea32f8db..918f14c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import fs from 'node:fs'; import { EventEmitter } from 'node:events'; import sysPath from 'node:path'; -import { promisify } from 'node:util'; import readdirp from 'readdirp'; +import {stat, readdir} from 'node:fs/promises'; import NodeFsHandler from './nodefs-handler.js'; import { anymatch, MatchFunction, isMatcherObject, Matcher } from './anymatch.js'; @@ -28,9 +28,6 @@ import { import * as EV from './events.js'; import { EventName } from './events.js'; -const stat = promisify(fs.stat); -const readdir = promisify(fs.readdir); - type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change'; type EmitArgs = [EventName, Path, any?, any?, any?]; diff --git a/src/nodefs-handler.ts b/src/nodefs-handler.ts index a8279844..e35a966e 100644 --- a/src/nodefs-handler.ts +++ b/src/nodefs-handler.ts @@ -1,6 +1,5 @@ import fs from 'fs'; import sysPath from 'path'; -import { promisify } from 'util'; import isBinaryPath from 'is-binary-path'; import { Path, @@ -20,15 +19,15 @@ import { } from './constants.js'; import * as EV from './events.js'; import type { FSWatcher, WatchHelper, FSWInstanceOptions } from './index.js'; +import { + open, + stat, + lstat, + realpath as fsrealpath +} from 'node:fs/promises'; const THROTTLE_MODE_WATCH = 'watch'; -const open = promisify(fs.open); -const stat = promisify(fs.stat); -const lstat = promisify(fs.lstat); -const close = promisify(fs.close); -const fsrealpath = promisify(fs.realpath); - const statMethods = { lstat, stat }; // TODO: emit errors properly. Example: EMFILE on Macos. @@ -191,7 +190,7 @@ const setFsWatchListener = ( if (isWindows && error.code === 'EPERM') { try { const fd = await open(path, 'r'); - await close(fd); + await fd.close(); broadcastErr(error); } catch (err) { // do nothing @@ -373,10 +372,12 @@ export default class NodeFsHandler { if (parent.has(basename)) return; const listener = async (path, newStats) => { + console.log({path, newStats}); if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return; if (!newStats || newStats.mtimeMs === 0) { try { const newStats = await stat(file); + console.log({newStats}); if (this.fsw.closed) return; // Check that change event was not fired because of changed only accessTime. const at = newStats.atimeMs; @@ -392,6 +393,7 @@ export default class NodeFsHandler { prevStats = newStats; } } catch (error) { + console.log({error}); // Fix issues where mtime is null but file is still present this.fsw._remove(dirname, basename); } diff --git a/test.mjs b/test.mjs index 550ba4e2..8e13f163 100644 --- a/test.mjs +++ b/test.mjs @@ -484,7 +484,7 @@ const runTests = (baseopts) => { await waitFor([unlinkSpy.withArgs(testPath)]); unlinkSpy.should.have.been.calledWith(testPath); - await delay(); + await delay(100); await write(testPath, dateNow()); await waitFor([[addSpy.withArgs(testPath), 2]]); addSpy.should.have.been.calledWith(testPath); @@ -1414,15 +1414,17 @@ const runTests = (baseopts) => { }); options2.cwd = getFixturePath('subdir'); const watcher = chokidar_watch(getGlobPath('.'), options); + const watcherEvents = waitForEvents(watcher, 3); const spy1 = await aspy(watcher, EV.ALL); await delay(); const watcher2 = chokidar_watch(currentDir, options2); + const watcher2Events = waitForEvents(watcher2, 5); const spy2 = await aspy(watcher2, EV.ALL); await fs_unlink(getFixturePath('unlink.txt')); await write(getFixturePath('change.txt'), dateNow()); - await waitFor([spy1.withArgs(EV.UNLINK), spy2.withArgs(EV.UNLINK)]); + await Promise.all([watcherEvents, watcher2Events]); spy1.should.have.been.calledWith(EV.CHANGE, 'change.txt'); spy1.should.have.been.calledWith(EV.UNLINK, 'unlink.txt'); spy2.should.have.been.calledWith(EV.ADD, sysPath.join('..', 'change.txt'));