fstalk 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -3
- package/backend.js +81 -0
- package/index.js +89 -0
- package/package.json +29 -6
- package/runtime.js +24 -0
- package/tree.js +53 -0
- package/watchfile.js +35 -0
package/README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# fstalk
|
|
2
|
+
|
|
3
|
+
Minimal fast cross-platform file watcher. ~5KB of flat JS, zero dependencies.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install fstalk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { watch } from 'fstalk';
|
|
15
|
+
|
|
16
|
+
const w = watch('./src', { recursive: true, debounce: 50 });
|
|
17
|
+
|
|
18
|
+
w.on('add', p => console.log('added', p));
|
|
19
|
+
w.on('change', p => console.log('changed', p));
|
|
20
|
+
w.on('unlink', p => console.log('removed', p));
|
|
21
|
+
w.on('addDir', p => console.log('dir+', p));
|
|
22
|
+
w.on('unlinkDir', p => console.log('dir-', p));
|
|
23
|
+
w.on('ready', () => console.log('initial scan done'));
|
|
24
|
+
|
|
25
|
+
w.close();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Single file:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { watchFile } from 'fstalk/watchfile';
|
|
32
|
+
|
|
33
|
+
const w = watchFile('./config.json', () => reload());
|
|
34
|
+
w.close();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
| option | default | description |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `recursive` | `true` | watch subdirectories |
|
|
42
|
+
| `ignoreInitial` | `false` | don't emit `add`/`addDir` for existing files |
|
|
43
|
+
| `debounce` | `0` | ms; coalesces event bursts per path |
|
|
44
|
+
| `ignored` | — | RegExp, function, or substring to skip paths |
|
|
45
|
+
|
|
46
|
+
## Events
|
|
47
|
+
|
|
48
|
+
`add` `change` `unlink` `addDir` `unlinkDir` `ready` `error`
|
|
49
|
+
|
|
50
|
+
## Design
|
|
51
|
+
|
|
52
|
+
- `backend.js` — platform layer: native `fs.watch(recursive)` on Windows/macOS, per-directory watchers elsewhere
|
|
53
|
+
- `runtime.js` — debounce/coalesce scheduler
|
|
54
|
+
- `tree.js` — known file/dir sets + initial scan + rename disambiguation
|
|
55
|
+
- `index.js` — `FSWatcher` API
|
|
56
|
+
- `watchfile.js` — single-file watch helper
|
|
57
|
+
|
|
58
|
+
Known-file `change` events cost a `Set` lookup — no stat, no alloc.
|
package/backend.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// fsw backend — platform watcher katmanı
|
|
2
|
+
// win32/darwin: tek recursive fs.watch
|
|
3
|
+
// linux/diğer: her dizin için ayrı watcher (manuel traversal)
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const NATIVE_RECURSIVE = process.platform === 'win32' || process.platform === 'darwin';
|
|
8
|
+
|
|
9
|
+
export function createWatcher(root, onEvent, opts = {}) {
|
|
10
|
+
const recursive = opts.recursive !== false;
|
|
11
|
+
const filter = opts.filter || (() => true);
|
|
12
|
+
|
|
13
|
+
if (!recursive || !NATIVE_RECURSIVE) {
|
|
14
|
+
if (!recursive) {
|
|
15
|
+
const w = fs.watch(root, (ev, name) => onEvent(ev, name ? path.join(root, name) : root));
|
|
16
|
+
w.on('error', () => {});
|
|
17
|
+
return { close: () => w.close() };
|
|
18
|
+
}
|
|
19
|
+
return createManual(root, onEvent, filter);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const w = fs.watch(root, { recursive: true }, (ev, name) => {
|
|
23
|
+
onEvent(ev, name ? path.join(root, name) : root);
|
|
24
|
+
});
|
|
25
|
+
w.on('error', () => {});
|
|
26
|
+
return { close: () => w.close() };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---- linux/diğer: manuel recursive ----
|
|
30
|
+
function createManual(root, onEvent, filter) {
|
|
31
|
+
const watchers = new Map(); // dir -> FSWatcher
|
|
32
|
+
const queue = [root];
|
|
33
|
+
|
|
34
|
+
function addWatch(dir) {
|
|
35
|
+
if (watchers.has(dir) || !filter(dir)) return;
|
|
36
|
+
let w;
|
|
37
|
+
try {
|
|
38
|
+
w = fs.watch(dir, (ev, name) => {
|
|
39
|
+
if (name) onEvent(ev, path.join(dir, name));
|
|
40
|
+
});
|
|
41
|
+
} catch { return; }
|
|
42
|
+
watchers.set(dir, w);
|
|
43
|
+
w.on('error', () => {});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function scan(dir) {
|
|
47
|
+
let ents;
|
|
48
|
+
try { ents = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
49
|
+
for (const e of ents) {
|
|
50
|
+
if (!e.isDirectory()) continue;
|
|
51
|
+
const sub = path.join(dir, e.name);
|
|
52
|
+
queue.push(sub);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function drain() {
|
|
57
|
+
while (queue.length) {
|
|
58
|
+
const d = queue.pop();
|
|
59
|
+
if (watchers.has(d)) continue;
|
|
60
|
+
addWatch(d);
|
|
61
|
+
scan(d);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
drain();
|
|
66
|
+
|
|
67
|
+
// rename event'lerinde yeni dizinleri yakala
|
|
68
|
+
const raw = onEvent;
|
|
69
|
+
onEvent = function(ev, full) {
|
|
70
|
+
raw(ev, full);
|
|
71
|
+
if (ev !== 'rename' || watchers.has(full)) return;
|
|
72
|
+
try {
|
|
73
|
+
if (fs.statSync(full).isDirectory()) { queue.push(full); drain(); }
|
|
74
|
+
} catch {}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
close() { for (const w of watchers.values()) w.close(); watchers.clear(); },
|
|
79
|
+
kind: 'manual'
|
|
80
|
+
};
|
|
81
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// fsw — minimal cross-platform file watcher
|
|
2
|
+
// 'rename' event'lerini stat ile add/change/unlink/addDir/unlinkDir'e çözer.
|
|
3
|
+
// Hot path: bilinen dosyada 'change' → Set lookup + emit. Sıfır stat, sıfır alloc.
|
|
4
|
+
import { EventEmitter } from 'node:events';
|
|
5
|
+
import fsp from 'node:fs/promises';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { createWatcher } from './backend.js';
|
|
8
|
+
import { Scheduler } from './runtime.js';
|
|
9
|
+
import { Tree, scan, ignoredFn } from './tree.js';
|
|
10
|
+
|
|
11
|
+
export class FSWatcher extends EventEmitter {
|
|
12
|
+
constructor(paths, opts = {}) {
|
|
13
|
+
super();
|
|
14
|
+
this.opts = opts;
|
|
15
|
+
this.recursive = opts.recursive !== false;
|
|
16
|
+
this.ignoreInitial = !!opts.ignoreInitial;
|
|
17
|
+
this._ignored = ignoredFn(opts.ignored);
|
|
18
|
+
this.tree = new Tree();
|
|
19
|
+
this.sched = new Scheduler(opts.debounce ?? 0);
|
|
20
|
+
this.backends = [];
|
|
21
|
+
this.closed = false;
|
|
22
|
+
this._init(Array.isArray(paths) ? paths : [paths]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_init(roots) {
|
|
26
|
+
const scans = [];
|
|
27
|
+
for (const root of roots) {
|
|
28
|
+
const abs = path.resolve(root);
|
|
29
|
+
this.backends.push(createWatcher(abs, (ev, full) => this._onRaw(ev, full), {
|
|
30
|
+
recursive: this.recursive,
|
|
31
|
+
filter: (d) => !this._ignored(d)
|
|
32
|
+
}));
|
|
33
|
+
scans.push(scan(abs, {
|
|
34
|
+
recursive: this.recursive,
|
|
35
|
+
ignored: this.opts.ignored,
|
|
36
|
+
tree: this.tree
|
|
37
|
+
}, (type, p) => { if (!this.ignoreInitial) this._emitNow(type, p); }));
|
|
38
|
+
}
|
|
39
|
+
Promise.all(scans).then(() => { if (!this.closed) this.emit('ready'); });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_onRaw(ev, full) {
|
|
43
|
+
if (this.closed || this._ignored(full)) return;
|
|
44
|
+
if (this.tree.isDir(full) && ev === 'change') return; // dizin metadata change'i anlamsız
|
|
45
|
+
if (ev === 'change' && this.tree.isFile(full)) {
|
|
46
|
+
return this.sched.run('c:' + full, () => this._emitNow('change', full));
|
|
47
|
+
}
|
|
48
|
+
// 'rename' veya bilinmeyen 'change' → stat ile çöz (async, nadir event)
|
|
49
|
+
fsp.stat(full).then(
|
|
50
|
+
(st) => {
|
|
51
|
+
if (this.closed) return;
|
|
52
|
+
if (st.isDirectory()) {
|
|
53
|
+
if (!this.tree.isDir(full)) {
|
|
54
|
+
this.tree.addDir(full);
|
|
55
|
+
this.sched.run('aD:' + full, () => this._emitNow('addDir', full));
|
|
56
|
+
}
|
|
57
|
+
} else if (st.isFile()) {
|
|
58
|
+
const isNew = !this.tree.isFile(full);
|
|
59
|
+
this.tree.addFile(full);
|
|
60
|
+
this.sched.run((isNew ? 'a:' : 'c:') + full, () =>
|
|
61
|
+
this._emitNow(isNew ? 'add' : 'change', full));
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
() => {
|
|
65
|
+
if (this.closed) return;
|
|
66
|
+
const type = this.tree.remove(full);
|
|
67
|
+
if (type) this.sched.run(type + ':' + full, () => this._emitNow(type, full));
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_emitNow(type, p) {
|
|
73
|
+
if (!this.closed) this.emit(type, p);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
close() {
|
|
77
|
+
if (this.closed) return;
|
|
78
|
+
this.closed = true;
|
|
79
|
+
this.sched.clear();
|
|
80
|
+
for (const b of this.backends) b.close();
|
|
81
|
+
this.backends = [];
|
|
82
|
+
this.tree.files.clear();
|
|
83
|
+
this.tree.dirs.clear();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function watch(paths, opts) {
|
|
88
|
+
return new FSWatcher(paths, opts);
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "fstalk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "fstalk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Minimal fast cross-platform file watcher — tiny, flat, low-overhead",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./watchfile": "./watchfile.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"backend.js",
|
|
14
|
+
"runtime.js",
|
|
15
|
+
"tree.js",
|
|
16
|
+
"watchfile.js"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"fs",
|
|
24
|
+
"watch",
|
|
25
|
+
"watcher",
|
|
26
|
+
"file",
|
|
27
|
+
"recursive"
|
|
28
|
+
]
|
|
29
|
+
}
|
package/runtime.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// fsw runtime — debounce/coalesce scheduler
|
|
2
|
+
// ms=0 → direkt çalışır (sıfır overhead). ms>0 → path başına tek timer, burst'ler birleşir.
|
|
3
|
+
export class Scheduler {
|
|
4
|
+
constructor(ms = 0) {
|
|
5
|
+
this.ms = ms;
|
|
6
|
+
this.timers = new Map();
|
|
7
|
+
this.closed = false;
|
|
8
|
+
}
|
|
9
|
+
run(key, fn) {
|
|
10
|
+
if (this.closed) return;
|
|
11
|
+
if (!this.ms) return fn();
|
|
12
|
+
const old = this.timers.get(key);
|
|
13
|
+
if (old !== undefined) clearTimeout(old);
|
|
14
|
+
this.timers.set(key, setTimeout(() => {
|
|
15
|
+
this.timers.delete(key);
|
|
16
|
+
if (!this.closed) fn();
|
|
17
|
+
}, this.ms));
|
|
18
|
+
}
|
|
19
|
+
clear() {
|
|
20
|
+
this.closed = true;
|
|
21
|
+
for (const t of this.timers.values()) clearTimeout(t);
|
|
22
|
+
this.timers.clear();
|
|
23
|
+
}
|
|
24
|
+
}
|
package/tree.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// fsw tree — bilinen dosya/dizin set'leri + initial scan
|
|
2
|
+
// rename event'lerinin add/change/unlink/addDir/unlinkDir'e çevrilmesinde kullanılır.
|
|
3
|
+
import fsp from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
export class Tree {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.files = new Set();
|
|
9
|
+
this.dirs = new Set();
|
|
10
|
+
}
|
|
11
|
+
has(p) { return this.files.has(p) || this.dirs.has(p); }
|
|
12
|
+
isDir(p) { return this.dirs.has(p); }
|
|
13
|
+
isFile(p) { return this.files.has(p); }
|
|
14
|
+
addFile(p) { this.files.add(p); }
|
|
15
|
+
addDir(p) { this.dirs.add(p); }
|
|
16
|
+
remove(p) {
|
|
17
|
+
if (this.dirs.delete(p)) return 'unlinkDir';
|
|
18
|
+
if (this.files.delete(p)) return 'unlink';
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ignored spec → predicate. regex | function | substring string | falsy
|
|
24
|
+
export function ignoredFn(spec) {
|
|
25
|
+
if (!spec) return () => false;
|
|
26
|
+
if (spec instanceof RegExp) return (p) => spec.test(p);
|
|
27
|
+
if (typeof spec === 'function') return spec;
|
|
28
|
+
return (p) => p.includes(spec);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Mevcut ağacı tara; her girdi için onEntry(type, path) çağır.
|
|
32
|
+
// Set'ler her zaman dolar; onEntry çağrısını ignoreInitial FSWatcher tarafında atlar.
|
|
33
|
+
export async function scan(root, opts, onEntry) {
|
|
34
|
+
const recursive = opts.recursive !== false;
|
|
35
|
+
const skip = ignoredFn(opts.ignored);
|
|
36
|
+
const walk = async (dir) => {
|
|
37
|
+
let ents;
|
|
38
|
+
try { ents = await fsp.readdir(dir, { withFileTypes: true }); } catch { return; }
|
|
39
|
+
for (const e of ents) {
|
|
40
|
+
const full = path.join(dir, e.name);
|
|
41
|
+
if (skip(full)) continue;
|
|
42
|
+
if (e.isDirectory()) {
|
|
43
|
+
opts.tree.addDir(full);
|
|
44
|
+
onEntry('addDir', full);
|
|
45
|
+
if (recursive) await walk(full);
|
|
46
|
+
} else if (e.isFile()) {
|
|
47
|
+
opts.tree.addFile(full);
|
|
48
|
+
onEntry('add', full);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
await walk(root);
|
|
53
|
+
}
|
package/watchfile.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// fsw watchfile — tek-dosya watch (parent dir + basename filtre)
|
|
2
|
+
// Dosya silinip yeniden yaratılsa bile çalışır. mtime+size imzası aynıysa ateşlemez.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { Scheduler } from './runtime.js';
|
|
6
|
+
|
|
7
|
+
function sig(file) {
|
|
8
|
+
try {
|
|
9
|
+
const st = fs.statSync(file);
|
|
10
|
+
return st.mtimeMs + ':' + st.size;
|
|
11
|
+
} catch { return ''; }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function watchFile(file, onChange, opts = {}) {
|
|
15
|
+
const abs = path.resolve(file);
|
|
16
|
+
const dir = path.dirname(abs);
|
|
17
|
+
const base = path.basename(abs);
|
|
18
|
+
let lastSig = sig(abs), closed = false;
|
|
19
|
+
const sched = new Scheduler(opts.debounce ?? 50);
|
|
20
|
+
|
|
21
|
+
const w = fs.watch(dir, (ev, name) => {
|
|
22
|
+
if (closed || name !== base) return;
|
|
23
|
+
sched.run('f', () => {
|
|
24
|
+
const s = sig(abs);
|
|
25
|
+
if (s === lastSig) return;
|
|
26
|
+
lastSig = s;
|
|
27
|
+
onChange(abs);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
w.on('error', () => {});
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
close() { closed = true; sched.clear(); w.close(); }
|
|
34
|
+
};
|
|
35
|
+
}
|