aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/fs.js
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-06-24 03:24:34 -0300
committerGravatar GitHub <noreply@github.com> 2023-06-23 23:24:34 -0700
commit069b42a7cc1275969859dc60e7c303528ca2dccb (patch)
tree446e9728bb957571c2062d8bd988abc4342dc57c /src/js/node/fs.js
parentceec1afec2bb187fecef0f5006dfe27ee3e1a9a6 (diff)
downloadbun-069b42a7cc1275969859dc60e7c303528ca2dccb.tar.gz
bun-069b42a7cc1275969859dc60e7c303528ca2dccb.tar.zst
bun-069b42a7cc1275969859dc60e7c303528ca2dccb.zip
[feat] fs.watch (#3249)
* initial support * add types * fix comment * fix types * bigfix up * more fixes * fix some encoding support for watch * fix rename event * fixup * fix latin1 * add fs_events, still failing some tests * fixuup * remove unecesary check * readd tests ops * this is necessary? just testing CI/CD weird errors * just use dupe here * cleanup and fix deinit * fix zig upgrade
Diffstat (limited to 'src/js/node/fs.js')
-rw-r--r--src/js/node/fs.js65
1 files changed, 64 insertions, 1 deletions
diff --git a/src/js/node/fs.js b/src/js/node/fs.js
index f117020dd..6b0e3954e 100644
--- a/src/js/node/fs.js
+++ b/src/js/node/fs.js
@@ -1,3 +1,5 @@
+import { EventEmitter } from "stream";
+
// Hardcoded module "node:fs"
var { direct, isPromise, isCallable } = import.meta.primordials;
var promises = import.meta.require("node:fs/promises");
@@ -7,6 +9,63 @@ var NativeReadable = _getNativeReadableStreamPrototype(2, Readable); // 2 means
var fs = Bun.fs();
var debug = process.env.DEBUG ? console.log : () => {};
+
+class FSWatcher extends EventEmitter {
+ #watcher;
+ #listener;
+ constructor(path, options, listener) {
+ super();
+
+ 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 = fs.watch(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();
+ }
+}
export var access = function access(...args) {
callbackify(fs.accessSync, args);
},
@@ -153,6 +212,9 @@ export var access = function access(...args) {
rmdirSync = fs.rmdirSync.bind(fs),
Dirent = fs.Dirent,
Stats = fs.Stats,
+ watch = function watch(path, options, listener) {
+ return new FSWatcher(path, options, listener);
+ },
promises = import.meta.require("node:fs/promises");
function callbackify(fsFunction, args) {
@@ -1002,7 +1064,8 @@ export default {
writeSync,
WriteStream,
ReadStream,
-
+ watch,
+ FSWatcher,
[Symbol.for("::bunternal::")]: {
ReadStreamClass,
WriteStreamClass,