Skip to content

Commit

Permalink
cleanup!: remove deprecated WatchService
Browse files Browse the repository at this point in the history
implementation has race condition bugs. superseded by the official .watch() api.
  • Loading branch information
AviVahl committed Sep 14, 2024
1 parent d425fda commit 09e464d
Show file tree
Hide file tree
Showing 17 changed files with 4 additions and 945 deletions.
1 change: 0 additions & 1 deletion packages/cached/test/cached-fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ describe("createCachedFs", () => {

const testProvider = async () => {
const fs = createCachedFs(createMemoryFs());
fs.watchService.addGlobalListener(({ path }) => fs.invalidate(path));
return {
fs,
dispose: async () => undefined,
Expand Down
54 changes: 0 additions & 54 deletions packages/memory/src/memory-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import {
type IDirectoryContents,
type IDirectoryEntry,
type IFileSystemStats,
type IWatchEvent,
type ReadFileOptions,
type RmOptions,
type StatSyncOptions,
type WatchEventListener,
type WatchChangeEventListener,
} from "@file-services/types";
import { SetMultiMap, createFileSystem, syncToAsyncFs } from "@file-services/utils";
Expand Down Expand Up @@ -65,8 +63,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
const changeListeners = new SetMultiMap<string, WatchChangeEventListener>();
const recursiveChangeListeners = new SetMultiMap<string, WatchChangeEventListener>();
const closeListeners = new SetMultiMap<string, () => void>();
const pathListeners = new SetMultiMap<string, WatchEventListener>();
const globalListeners = new Set<WatchEventListener>();
const textEncoder = new TextEncoder();
realpathSync.native = realpathSync;

Expand All @@ -75,34 +71,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
root,
...posixPath,
resolve: resolvePath,
watchService: {
async watchPath(path, listener) {
const resolvedPath = resolvePath(path);
if (listener) {
pathListeners.add(resolvedPath, listener);
}
},
async unwatchPath(path, listener) {
const resolvedPath = resolvePath(path);
if (listener) {
pathListeners.delete(resolvedPath, listener);
} else {
pathListeners.deleteKey(resolvedPath);
}
},
async unwatchAllPaths() {
pathListeners.clear();
},
addGlobalListener(listener) {
globalListeners.add(listener);
},
removeGlobalListener(listener) {
globalListeners.delete(listener);
},
clearGlobalListeners() {
globalListeners.clear();
},
},
caseSensitive: true,
cwd,
chdir,
Expand Down Expand Up @@ -212,7 +180,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
}
existingNode.entry = { ...existingNode.entry, mtime: new Date() };
existingNode.contents = typeof fileContent === "string" ? fileContent : new Uint8Array(fileContent);
emitWatchEvent({ path: resolvedPath, stats: existingNode.entry });
emitChangeEvent("change", resolvedPath);
} else {
const parentPath = posixPath.dirname(resolvedPath);
Expand All @@ -238,7 +205,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
contents: typeof fileContent === "string" ? fileContent : new Uint8Array(fileContent),
};
parentNode.contents.set(fileName, newFileNode);
emitWatchEvent({ path: resolvedPath, stats: newFileNode.entry });
emitChangeEvent("rename", resolvedPath);
}
}
Expand All @@ -262,7 +228,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
}

parentNode.contents.delete(fileName);
emitWatchEvent({ path: resolvedPath, stats: null });
emitChangeEvent("rename", resolvedPath);
}

Expand Down Expand Up @@ -327,7 +292,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {

const newDirNode: IFsMemDirectoryNode = createMemDirectory(directoryName);
parentNode.contents.set(directoryName, newDirNode);
emitWatchEvent({ path: resolvedPath, stats: newDirNode.entry });
emitChangeEvent("rename", resolvedPath);
}

Expand All @@ -350,7 +314,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
}

parentNode.contents.delete(directoryName);
emitWatchEvent({ path: resolvedPath, stats: null });
emitChangeEvent("rename", resolvedPath);
}

Expand Down Expand Up @@ -382,7 +345,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
}

parentNode.contents.delete(targetName);
emitWatchEvent({ path: resolvedPath, stats: null });
emitChangeEvent("rename", resolvedPath);
}

Expand Down Expand Up @@ -508,8 +470,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
sourceNode.entry = { ...sourceNode.entry, name: destinationName, mtime: new Date() };
destinationParentNode.contents.set(destinationName, sourceNode);

emitWatchEvent({ path: resolvedSourcePath, stats: null });
emitWatchEvent({ path: resolvedDestinationPath, stats: sourceNode.entry });
emitChangeEvent("rename", resolvedSourcePath);
emitChangeEvent("rename", resolvedDestinationPath);
}
Expand Down Expand Up @@ -555,7 +515,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
};
destParentNode.contents.set(targetName, newFileNode);

emitWatchEvent({ path: resolvedDestinationPath, stats: newFileNode.entry });
emitChangeEvent("rename", resolvedDestinationPath);
}

Expand Down Expand Up @@ -591,7 +550,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
};

parentNode.contents.set(fileName, symlinkNode);
emitWatchEvent({ path: resolvedLinkPath, stats: symlinkNode.entry });
emitChangeEvent("rename", resolvedLinkPath);
}

Expand Down Expand Up @@ -655,18 +613,6 @@ export function createBaseMemoryFsSync(): IBaseMemFileSystemSync {
}
}
}

function emitWatchEvent(watchEvent: IWatchEvent): void {
for (const listener of globalListeners) {
listener(watchEvent);
}
const listeners = pathListeners.get(watchEvent.path);
if (listeners) {
for (const listener of listeners) {
listener(watchEvent);
}
}
}
}

function createMemDirectory(name: string): IFsMemDirectoryNode {
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { IFileSystem } from "@file-services/types";
import { createNodeFs } from "./node-fs";

export * from "./node-fs";
export * from "./watch-service";

export const nodeFs: IFileSystem = createNodeFs();
export default nodeFs;
12 changes: 3 additions & 9 deletions packages/node/src/node-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ import { promisify } from "node:util";

import type { IBaseFileSystem, IFileSystem, WatchOptions } from "@file-services/types";
import { createFileSystem } from "@file-services/utils";
import { INodeWatchServiceOptions, NodeWatchService } from "./watch-service";
import { RecursiveFSWatcher } from "./recursive-fs-watcher";

const caseSensitive = !fs.existsSync(argv[0]!.toUpperCase());
const fsPromisesExists = promisify(fs.exists);

export interface ICreateNodeFsOptions {
watchOptions?: INodeWatchServiceOptions;
export function createNodeFs(): IFileSystem {
return createFileSystem(createBaseNodeFs());
}

export function createNodeFs(options?: ICreateNodeFsOptions): IFileSystem {
return createFileSystem(createBaseNodeFs(options));
}

export function createBaseNodeFs(options?: ICreateNodeFsOptions): IBaseFileSystem {
export function createBaseNodeFs(): IBaseFileSystem {
const originalWatch = fs.watch;
const watch = process.platform === "linux" ? wrapWithOwnRecursiveImpl(originalWatch) : originalWatch;
return {
...path,
chdir,
cwd,
watchService: new NodeWatchService(options && options.watchOptions),
caseSensitive,
...fs,
watch,
Expand Down
Loading

0 comments on commit 09e464d

Please sign in to comment.