Skip to content

Commit

Permalink
feat: move to node:fs/promises
Browse files Browse the repository at this point in the history
Moves to using `node:fs/promises` rather than using `promisify`
manually.
  • Loading branch information
43081j committed Jun 25, 2024
1 parent bd3c8a0 commit d92fb64
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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?];

Expand Down
18 changes: 10 additions & 8 deletions src/nodefs-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import sysPath from 'path';
import { promisify } from 'util';
import isBinaryPath from 'is-binary-path';
import {
Path,
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit d92fb64

Please sign in to comment.