Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/js/internal/fs/FSWatcher-lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var FSWatcher;

var load = () => {
FSWatcher = require("internal/fs/FSWatcher").FSWatcher;
load = () => {};
return FSWatcher;
};

export default {
FSWatcherPropertyDescriptor: {
enumerable: true,
get() {
load();
return FSWatcher;
},
set() {},
},
watch: function watch(...args) {
load();
return new FSWatcher(...args);
},
};
71 changes: 71 additions & 0 deletions src/js/internal/fs/FSWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var watchNodeFS: typeof import("node:fs").watch;

const EventEmitter = require("node:events");
class FSWatcher extends EventEmitter {
#watcher;
#listener;
constructor(path, options, listener) {
super();

if (!watchNodeFS) {
const nodeFS = Bun.fs();
watchNodeFS = nodeFS.watch.bind(nodeFS) as any;
}

if (typeof options === "function") {
listener = options;
options = {};
} else if (typeof options === "string") {
options = { encoding: options };
}

if (typeof listener !== "function") {
listener = () => {};
}

this.#listener = listener;
try {
this.#watcher = watchNodeFS(path, options || {}, this.#onEvent.bind(this));
} catch (e) {
if (!e.message?.startsWith("FileNotFound")) {
throw e;
}
const notFound = new Error(`ENOENT: no such file or directory, watch '${path}'`);
notFound.code = "ENOENT";
notFound.errno = -2;
notFound.path = path;
notFound.syscall = "watch";
notFound.filename = path;
throw notFound;
}
}

#onEvent(eventType, filenameOrError) {
if (eventType === "error" || eventType === "close") {
this.emit(eventType, filenameOrError);
} else {
this.emit("change", eventType, filenameOrError);
this.#listener(eventType, filenameOrError);
}
}

close() {
this.#watcher?.close();
this.#watcher = null;
}

ref() {
this.#watcher?.ref();
}

unref() {
this.#watcher?.unref();
}

// https://github.com/nodejs/node/blob/9f51c55a47702dc6a0ca3569853dd7ba022bf7bb/lib/internal/fs/watchers.js#L259-L263
start() {}
}

export default {
FSWatcher,
};
20 changes: 20 additions & 0 deletions src/js/internal/fs/ReadStream-lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var ReadStream;
function load() {
return require("internal/fs/ReadStream").ReadStream;
}

export default {
ReadStreamPropertyDescriptor: {
enumerable: true,

get() {
return (ReadStream ??= load());
},

set() {},
},

createReadStream(path, options) {
return new (ReadStream ??= load())(path, options);
},
};
Loading