mumei-dashboard 0.1.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.
@@ -0,0 +1,2040 @@
1
+ import { execFile } from 'child_process';
2
+ import { readFile, readdir, stat, realpath, lstat, open } from 'fs/promises';
3
+ import * as sp2 from 'path';
4
+ import sp2__default, { resolve, join, relative, sep } from 'path';
5
+ import { promisify } from 'util';
6
+ import cors from '@fastify/cors';
7
+ import helmet from '@fastify/helmet';
8
+ import { Type } from '@sinclair/typebox';
9
+ import { EventEmitter } from 'events';
10
+ import { stat as stat$1, unwatchFile, watchFile, watch as watch$1 } from 'fs';
11
+ import { Readable } from 'stream';
12
+ import { type } from 'os';
13
+ import Fastify from 'fastify';
14
+
15
+ // server/index.ts
16
+ var EntryTypes = {
17
+ FILE_TYPE: "files",
18
+ DIR_TYPE: "directories",
19
+ FILE_DIR_TYPE: "files_directories",
20
+ EVERYTHING_TYPE: "all"
21
+ };
22
+ var defaultOptions = {
23
+ root: ".",
24
+ fileFilter: (_entryInfo) => true,
25
+ directoryFilter: (_entryInfo) => true,
26
+ type: EntryTypes.FILE_TYPE,
27
+ lstat: false,
28
+ depth: 2147483648,
29
+ alwaysStat: false,
30
+ highWaterMark: 4096
31
+ };
32
+ Object.freeze(defaultOptions);
33
+ var RECURSIVE_ERROR_CODE = "READDIRP_RECURSIVE_ERROR";
34
+ var NORMAL_FLOW_ERRORS = /* @__PURE__ */ new Set(["ENOENT", "EPERM", "EACCES", "ELOOP", RECURSIVE_ERROR_CODE]);
35
+ var ALL_TYPES = [
36
+ EntryTypes.DIR_TYPE,
37
+ EntryTypes.EVERYTHING_TYPE,
38
+ EntryTypes.FILE_DIR_TYPE,
39
+ EntryTypes.FILE_TYPE
40
+ ];
41
+ var DIR_TYPES = /* @__PURE__ */ new Set([
42
+ EntryTypes.DIR_TYPE,
43
+ EntryTypes.EVERYTHING_TYPE,
44
+ EntryTypes.FILE_DIR_TYPE
45
+ ]);
46
+ var FILE_TYPES = /* @__PURE__ */ new Set([
47
+ EntryTypes.EVERYTHING_TYPE,
48
+ EntryTypes.FILE_DIR_TYPE,
49
+ EntryTypes.FILE_TYPE
50
+ ]);
51
+ var isNormalFlowError = (error) => NORMAL_FLOW_ERRORS.has(error.code);
52
+ var wantBigintFsStats = process.platform === "win32";
53
+ var emptyFn = (_entryInfo) => true;
54
+ var normalizeFilter = (filter) => {
55
+ if (filter === void 0)
56
+ return emptyFn;
57
+ if (typeof filter === "function")
58
+ return filter;
59
+ if (typeof filter === "string") {
60
+ const fl = filter.trim();
61
+ return (entry) => entry.basename === fl;
62
+ }
63
+ if (Array.isArray(filter)) {
64
+ const trItems = filter.map((item) => item.trim());
65
+ return (entry) => trItems.some((f) => entry.basename === f);
66
+ }
67
+ return emptyFn;
68
+ };
69
+ var ReaddirpStream = class extends Readable {
70
+ parents;
71
+ reading;
72
+ parent;
73
+ _stat;
74
+ _maxDepth;
75
+ _wantsDir;
76
+ _wantsFile;
77
+ _wantsEverything;
78
+ _root;
79
+ _isDirent;
80
+ _statsProp;
81
+ _rdOptions;
82
+ _fileFilter;
83
+ _directoryFilter;
84
+ constructor(options = {}) {
85
+ super({
86
+ objectMode: true,
87
+ autoDestroy: true,
88
+ highWaterMark: options.highWaterMark
89
+ });
90
+ const opts = { ...defaultOptions, ...options };
91
+ const { root, type } = opts;
92
+ this._fileFilter = normalizeFilter(opts.fileFilter);
93
+ this._directoryFilter = normalizeFilter(opts.directoryFilter);
94
+ const statMethod = opts.lstat ? lstat : stat;
95
+ if (wantBigintFsStats) {
96
+ this._stat = (path3) => statMethod(path3, { bigint: true });
97
+ } else {
98
+ this._stat = statMethod;
99
+ }
100
+ this._maxDepth = opts.depth != null && Number.isSafeInteger(opts.depth) ? opts.depth : defaultOptions.depth;
101
+ this._wantsDir = type ? DIR_TYPES.has(type) : false;
102
+ this._wantsFile = type ? FILE_TYPES.has(type) : false;
103
+ this._wantsEverything = type === EntryTypes.EVERYTHING_TYPE;
104
+ this._root = resolve(root);
105
+ this._isDirent = !opts.alwaysStat;
106
+ this._statsProp = this._isDirent ? "dirent" : "stats";
107
+ this._rdOptions = { encoding: "utf8", withFileTypes: this._isDirent };
108
+ this.parents = [this._exploreDir(root, 1)];
109
+ this.reading = false;
110
+ this.parent = void 0;
111
+ }
112
+ async _read(batch) {
113
+ if (this.reading)
114
+ return;
115
+ this.reading = true;
116
+ try {
117
+ while (!this.destroyed && batch > 0) {
118
+ const par = this.parent;
119
+ const fil = par && par.files;
120
+ if (fil && fil.length > 0) {
121
+ const { path: path3, depth } = par;
122
+ const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path3));
123
+ const awaited = await Promise.all(slice);
124
+ for (const entry of awaited) {
125
+ if (!entry)
126
+ continue;
127
+ if (this.destroyed)
128
+ return;
129
+ const entryType = await this._getEntryType(entry);
130
+ if (entryType === "directory" && this._directoryFilter(entry)) {
131
+ if (depth <= this._maxDepth) {
132
+ this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
133
+ }
134
+ if (this._wantsDir) {
135
+ this.push(entry);
136
+ batch--;
137
+ }
138
+ } else if ((entryType === "file" || this._includeAsFile(entry)) && this._fileFilter(entry)) {
139
+ if (this._wantsFile) {
140
+ this.push(entry);
141
+ batch--;
142
+ }
143
+ }
144
+ }
145
+ } else {
146
+ const parent = this.parents.pop();
147
+ if (!parent) {
148
+ this.push(null);
149
+ break;
150
+ }
151
+ this.parent = await parent;
152
+ if (this.destroyed)
153
+ return;
154
+ }
155
+ }
156
+ } catch (error) {
157
+ this.destroy(error);
158
+ } finally {
159
+ this.reading = false;
160
+ }
161
+ }
162
+ async _exploreDir(path3, depth) {
163
+ let files;
164
+ try {
165
+ files = await readdir(path3, this._rdOptions);
166
+ } catch (error) {
167
+ this._onError(error);
168
+ }
169
+ return { files, depth, path: path3 };
170
+ }
171
+ async _formatEntry(dirent, path3) {
172
+ let entry;
173
+ const basename3 = this._isDirent ? dirent.name : dirent;
174
+ try {
175
+ const fullPath = resolve(join(path3, basename3));
176
+ entry = { path: relative(this._root, fullPath), fullPath, basename: basename3 };
177
+ entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
178
+ } catch (err) {
179
+ this._onError(err);
180
+ return;
181
+ }
182
+ return entry;
183
+ }
184
+ _onError(err) {
185
+ if (isNormalFlowError(err) && !this.destroyed) {
186
+ this.emit("warn", err);
187
+ } else {
188
+ this.destroy(err);
189
+ }
190
+ }
191
+ async _getEntryType(entry) {
192
+ if (!entry && this._statsProp in entry) {
193
+ return "";
194
+ }
195
+ const stats = entry[this._statsProp];
196
+ if (stats.isFile())
197
+ return "file";
198
+ if (stats.isDirectory())
199
+ return "directory";
200
+ if (stats && stats.isSymbolicLink()) {
201
+ const full = entry.fullPath;
202
+ try {
203
+ const entryRealPath = await realpath(full);
204
+ const entryRealPathStats = await lstat(entryRealPath);
205
+ if (entryRealPathStats.isFile()) {
206
+ return "file";
207
+ }
208
+ if (entryRealPathStats.isDirectory()) {
209
+ const len = entryRealPath.length;
210
+ if (full.startsWith(entryRealPath) && full.substr(len, 1) === sep) {
211
+ const recursiveError = new Error(`Circular symlink detected: "${full}" points to "${entryRealPath}"`);
212
+ recursiveError.code = RECURSIVE_ERROR_CODE;
213
+ return this._onError(recursiveError);
214
+ }
215
+ return "directory";
216
+ }
217
+ } catch (error) {
218
+ this._onError(error);
219
+ return "";
220
+ }
221
+ }
222
+ }
223
+ _includeAsFile(entry) {
224
+ const stats = entry && entry[this._statsProp];
225
+ return stats && this._wantsEverything && !stats.isDirectory();
226
+ }
227
+ };
228
+ function readdirp(root, options = {}) {
229
+ let type = options.entryType || options.type;
230
+ if (type === "both")
231
+ type = EntryTypes.FILE_DIR_TYPE;
232
+ if (type)
233
+ options.type = type;
234
+ if (!root) {
235
+ throw new Error("readdirp: root argument is required. Usage: readdirp(root, options)");
236
+ } else if (typeof root !== "string") {
237
+ throw new TypeError("readdirp: root argument must be a string. Usage: readdirp(root, options)");
238
+ } else if (type && !ALL_TYPES.includes(type)) {
239
+ throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(", ")}`);
240
+ }
241
+ options.root = root;
242
+ return new ReaddirpStream(options);
243
+ }
244
+ var STR_DATA = "data";
245
+ var STR_END = "end";
246
+ var STR_CLOSE = "close";
247
+ var EMPTY_FN = () => {
248
+ };
249
+ var pl = process.platform;
250
+ var isWindows = pl === "win32";
251
+ var isMacos = pl === "darwin";
252
+ var isLinux = pl === "linux";
253
+ var isFreeBSD = pl === "freebsd";
254
+ var isIBMi = type() === "OS400";
255
+ var EVENTS = {
256
+ ALL: "all",
257
+ READY: "ready",
258
+ ADD: "add",
259
+ CHANGE: "change",
260
+ ADD_DIR: "addDir",
261
+ UNLINK: "unlink",
262
+ UNLINK_DIR: "unlinkDir",
263
+ RAW: "raw",
264
+ ERROR: "error"
265
+ };
266
+ var EV = EVENTS;
267
+ var THROTTLE_MODE_WATCH = "watch";
268
+ var statMethods = { lstat: lstat, stat: stat };
269
+ var KEY_LISTENERS = "listeners";
270
+ var KEY_ERR = "errHandlers";
271
+ var KEY_RAW = "rawEmitters";
272
+ var HANDLER_KEYS = [KEY_LISTENERS, KEY_ERR, KEY_RAW];
273
+ var binaryExtensions = /* @__PURE__ */ new Set([
274
+ "3dm",
275
+ "3ds",
276
+ "3g2",
277
+ "3gp",
278
+ "7z",
279
+ "a",
280
+ "aac",
281
+ "adp",
282
+ "afdesign",
283
+ "afphoto",
284
+ "afpub",
285
+ "ai",
286
+ "aif",
287
+ "aiff",
288
+ "alz",
289
+ "ape",
290
+ "apk",
291
+ "appimage",
292
+ "ar",
293
+ "arj",
294
+ "asf",
295
+ "au",
296
+ "avi",
297
+ "bak",
298
+ "baml",
299
+ "bh",
300
+ "bin",
301
+ "bk",
302
+ "bmp",
303
+ "btif",
304
+ "bz2",
305
+ "bzip2",
306
+ "cab",
307
+ "caf",
308
+ "cgm",
309
+ "class",
310
+ "cmx",
311
+ "cpio",
312
+ "cr2",
313
+ "cur",
314
+ "dat",
315
+ "dcm",
316
+ "deb",
317
+ "dex",
318
+ "djvu",
319
+ "dll",
320
+ "dmg",
321
+ "dng",
322
+ "doc",
323
+ "docm",
324
+ "docx",
325
+ "dot",
326
+ "dotm",
327
+ "dra",
328
+ "DS_Store",
329
+ "dsk",
330
+ "dts",
331
+ "dtshd",
332
+ "dvb",
333
+ "dwg",
334
+ "dxf",
335
+ "ecelp4800",
336
+ "ecelp7470",
337
+ "ecelp9600",
338
+ "egg",
339
+ "eol",
340
+ "eot",
341
+ "epub",
342
+ "exe",
343
+ "f4v",
344
+ "fbs",
345
+ "fh",
346
+ "fla",
347
+ "flac",
348
+ "flatpak",
349
+ "fli",
350
+ "flv",
351
+ "fpx",
352
+ "fst",
353
+ "fvt",
354
+ "g3",
355
+ "gh",
356
+ "gif",
357
+ "graffle",
358
+ "gz",
359
+ "gzip",
360
+ "h261",
361
+ "h263",
362
+ "h264",
363
+ "icns",
364
+ "ico",
365
+ "ief",
366
+ "img",
367
+ "ipa",
368
+ "iso",
369
+ "jar",
370
+ "jpeg",
371
+ "jpg",
372
+ "jpgv",
373
+ "jpm",
374
+ "jxr",
375
+ "key",
376
+ "ktx",
377
+ "lha",
378
+ "lib",
379
+ "lvp",
380
+ "lz",
381
+ "lzh",
382
+ "lzma",
383
+ "lzo",
384
+ "m3u",
385
+ "m4a",
386
+ "m4v",
387
+ "mar",
388
+ "mdi",
389
+ "mht",
390
+ "mid",
391
+ "midi",
392
+ "mj2",
393
+ "mka",
394
+ "mkv",
395
+ "mmr",
396
+ "mng",
397
+ "mobi",
398
+ "mov",
399
+ "movie",
400
+ "mp3",
401
+ "mp4",
402
+ "mp4a",
403
+ "mpeg",
404
+ "mpg",
405
+ "mpga",
406
+ "mxu",
407
+ "nef",
408
+ "npx",
409
+ "numbers",
410
+ "nupkg",
411
+ "o",
412
+ "odp",
413
+ "ods",
414
+ "odt",
415
+ "oga",
416
+ "ogg",
417
+ "ogv",
418
+ "otf",
419
+ "ott",
420
+ "pages",
421
+ "pbm",
422
+ "pcx",
423
+ "pdb",
424
+ "pdf",
425
+ "pea",
426
+ "pgm",
427
+ "pic",
428
+ "png",
429
+ "pnm",
430
+ "pot",
431
+ "potm",
432
+ "potx",
433
+ "ppa",
434
+ "ppam",
435
+ "ppm",
436
+ "pps",
437
+ "ppsm",
438
+ "ppsx",
439
+ "ppt",
440
+ "pptm",
441
+ "pptx",
442
+ "psd",
443
+ "pya",
444
+ "pyc",
445
+ "pyo",
446
+ "pyv",
447
+ "qt",
448
+ "rar",
449
+ "ras",
450
+ "raw",
451
+ "resources",
452
+ "rgb",
453
+ "rip",
454
+ "rlc",
455
+ "rmf",
456
+ "rmvb",
457
+ "rpm",
458
+ "rtf",
459
+ "rz",
460
+ "s3m",
461
+ "s7z",
462
+ "scpt",
463
+ "sgi",
464
+ "shar",
465
+ "snap",
466
+ "sil",
467
+ "sketch",
468
+ "slk",
469
+ "smv",
470
+ "snk",
471
+ "so",
472
+ "stl",
473
+ "suo",
474
+ "sub",
475
+ "swf",
476
+ "tar",
477
+ "tbz",
478
+ "tbz2",
479
+ "tga",
480
+ "tgz",
481
+ "thmx",
482
+ "tif",
483
+ "tiff",
484
+ "tlz",
485
+ "ttc",
486
+ "ttf",
487
+ "txz",
488
+ "udf",
489
+ "uvh",
490
+ "uvi",
491
+ "uvm",
492
+ "uvp",
493
+ "uvs",
494
+ "uvu",
495
+ "viv",
496
+ "vob",
497
+ "war",
498
+ "wav",
499
+ "wax",
500
+ "wbmp",
501
+ "wdp",
502
+ "weba",
503
+ "webm",
504
+ "webp",
505
+ "whl",
506
+ "wim",
507
+ "wm",
508
+ "wma",
509
+ "wmv",
510
+ "wmx",
511
+ "woff",
512
+ "woff2",
513
+ "wrm",
514
+ "wvx",
515
+ "xbm",
516
+ "xif",
517
+ "xla",
518
+ "xlam",
519
+ "xls",
520
+ "xlsb",
521
+ "xlsm",
522
+ "xlsx",
523
+ "xlt",
524
+ "xltm",
525
+ "xltx",
526
+ "xm",
527
+ "xmind",
528
+ "xpi",
529
+ "xpm",
530
+ "xwd",
531
+ "xz",
532
+ "z",
533
+ "zip",
534
+ "zipx"
535
+ ]);
536
+ var isBinaryPath = (filePath) => binaryExtensions.has(sp2.extname(filePath).slice(1).toLowerCase());
537
+ var foreach = (val, fn) => {
538
+ if (val instanceof Set) {
539
+ val.forEach(fn);
540
+ } else {
541
+ fn(val);
542
+ }
543
+ };
544
+ var addAndConvert = (main, prop, item) => {
545
+ let container = main[prop];
546
+ if (!(container instanceof Set)) {
547
+ main[prop] = container = /* @__PURE__ */ new Set([container]);
548
+ }
549
+ container.add(item);
550
+ };
551
+ var clearItem = (cont) => (key) => {
552
+ const set = cont[key];
553
+ if (set instanceof Set) {
554
+ set.clear();
555
+ } else {
556
+ delete cont[key];
557
+ }
558
+ };
559
+ var delFromSet = (main, prop, item) => {
560
+ const container = main[prop];
561
+ if (container instanceof Set) {
562
+ container.delete(item);
563
+ } else if (container === item) {
564
+ delete main[prop];
565
+ }
566
+ };
567
+ var isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
568
+ var FsWatchInstances = /* @__PURE__ */ new Map();
569
+ function createFsWatchInstance(path3, options, listener, errHandler, emitRaw) {
570
+ const handleEvent = (rawEvent, evPath) => {
571
+ listener(path3);
572
+ emitRaw(rawEvent, evPath, { watchedPath: path3 });
573
+ if (evPath && path3 !== evPath) {
574
+ fsWatchBroadcast(sp2.resolve(path3, evPath), KEY_LISTENERS, sp2.join(path3, evPath));
575
+ }
576
+ };
577
+ try {
578
+ return watch$1(path3, {
579
+ persistent: options.persistent
580
+ }, handleEvent);
581
+ } catch (error) {
582
+ errHandler(error);
583
+ return void 0;
584
+ }
585
+ }
586
+ var fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
587
+ const cont = FsWatchInstances.get(fullPath);
588
+ if (!cont)
589
+ return;
590
+ foreach(cont[listenerType], (listener) => {
591
+ listener(val1, val2, val3);
592
+ });
593
+ };
594
+ var setFsWatchListener = (path3, fullPath, options, handlers) => {
595
+ const { listener, errHandler, rawEmitter } = handlers;
596
+ let cont = FsWatchInstances.get(fullPath);
597
+ let watcher2;
598
+ if (!options.persistent) {
599
+ watcher2 = createFsWatchInstance(path3, options, listener, errHandler, rawEmitter);
600
+ if (!watcher2)
601
+ return;
602
+ return watcher2.close.bind(watcher2);
603
+ }
604
+ if (cont) {
605
+ addAndConvert(cont, KEY_LISTENERS, listener);
606
+ addAndConvert(cont, KEY_ERR, errHandler);
607
+ addAndConvert(cont, KEY_RAW, rawEmitter);
608
+ } else {
609
+ watcher2 = createFsWatchInstance(
610
+ path3,
611
+ options,
612
+ fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
613
+ errHandler,
614
+ // no need to use broadcast here
615
+ fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
616
+ );
617
+ if (!watcher2)
618
+ return;
619
+ watcher2.on(EV.ERROR, async (error) => {
620
+ const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
621
+ if (cont)
622
+ cont.watcherUnusable = true;
623
+ if (isWindows && error.code === "EPERM") {
624
+ try {
625
+ const fd = await open(path3, "r");
626
+ await fd.close();
627
+ broadcastErr(error);
628
+ } catch (err) {
629
+ }
630
+ } else {
631
+ broadcastErr(error);
632
+ }
633
+ });
634
+ cont = {
635
+ listeners: listener,
636
+ errHandlers: errHandler,
637
+ rawEmitters: rawEmitter,
638
+ watcher: watcher2
639
+ };
640
+ FsWatchInstances.set(fullPath, cont);
641
+ }
642
+ return () => {
643
+ delFromSet(cont, KEY_LISTENERS, listener);
644
+ delFromSet(cont, KEY_ERR, errHandler);
645
+ delFromSet(cont, KEY_RAW, rawEmitter);
646
+ if (isEmptySet(cont.listeners)) {
647
+ cont.watcher.close();
648
+ FsWatchInstances.delete(fullPath);
649
+ HANDLER_KEYS.forEach(clearItem(cont));
650
+ cont.watcher = void 0;
651
+ Object.freeze(cont);
652
+ }
653
+ };
654
+ };
655
+ var FsWatchFileInstances = /* @__PURE__ */ new Map();
656
+ var setFsWatchFileListener = (path3, fullPath, options, handlers) => {
657
+ const { listener, rawEmitter } = handlers;
658
+ let cont = FsWatchFileInstances.get(fullPath);
659
+ const copts = cont && cont.options;
660
+ if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
661
+ unwatchFile(fullPath);
662
+ cont = void 0;
663
+ }
664
+ if (cont) {
665
+ addAndConvert(cont, KEY_LISTENERS, listener);
666
+ addAndConvert(cont, KEY_RAW, rawEmitter);
667
+ } else {
668
+ cont = {
669
+ listeners: listener,
670
+ rawEmitters: rawEmitter,
671
+ options,
672
+ watcher: watchFile(fullPath, options, (curr, prev) => {
673
+ foreach(cont.rawEmitters, (rawEmitter2) => {
674
+ rawEmitter2(EV.CHANGE, fullPath, { curr, prev });
675
+ });
676
+ const currmtime = curr.mtimeMs;
677
+ if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
678
+ foreach(cont.listeners, (listener2) => listener2(path3, curr));
679
+ }
680
+ })
681
+ };
682
+ FsWatchFileInstances.set(fullPath, cont);
683
+ }
684
+ return () => {
685
+ delFromSet(cont, KEY_LISTENERS, listener);
686
+ delFromSet(cont, KEY_RAW, rawEmitter);
687
+ if (isEmptySet(cont.listeners)) {
688
+ FsWatchFileInstances.delete(fullPath);
689
+ unwatchFile(fullPath);
690
+ cont.options = cont.watcher = void 0;
691
+ Object.freeze(cont);
692
+ }
693
+ };
694
+ };
695
+ var NodeFsHandler = class {
696
+ fsw;
697
+ _boundHandleError;
698
+ constructor(fsW) {
699
+ this.fsw = fsW;
700
+ this._boundHandleError = (error) => fsW._handleError(error);
701
+ }
702
+ /**
703
+ * Watch file for changes with fs_watchFile or fs_watch.
704
+ * @param path to file or dir
705
+ * @param listener on fs change
706
+ * @returns closer for the watcher instance
707
+ */
708
+ _watchWithNodeFs(path3, listener) {
709
+ const opts = this.fsw.options;
710
+ const directory = sp2.dirname(path3);
711
+ const basename3 = sp2.basename(path3);
712
+ const parent = this.fsw._getWatchedDir(directory);
713
+ parent.add(basename3);
714
+ const absolutePath = sp2.resolve(path3);
715
+ const options = {
716
+ persistent: opts.persistent
717
+ };
718
+ if (!listener)
719
+ listener = EMPTY_FN;
720
+ let closer;
721
+ if (opts.usePolling) {
722
+ const enableBin = opts.interval !== opts.binaryInterval;
723
+ options.interval = enableBin && isBinaryPath(basename3) ? opts.binaryInterval : opts.interval;
724
+ closer = setFsWatchFileListener(path3, absolutePath, options, {
725
+ listener,
726
+ rawEmitter: this.fsw._emitRaw
727
+ });
728
+ } else {
729
+ closer = setFsWatchListener(path3, absolutePath, options, {
730
+ listener,
731
+ errHandler: this._boundHandleError,
732
+ rawEmitter: this.fsw._emitRaw
733
+ });
734
+ }
735
+ return closer;
736
+ }
737
+ /**
738
+ * Watch a file and emit add event if warranted.
739
+ * @returns closer for the watcher instance
740
+ */
741
+ _handleFile(file, stats, initialAdd) {
742
+ if (this.fsw.closed) {
743
+ return;
744
+ }
745
+ const dirname3 = sp2.dirname(file);
746
+ const basename3 = sp2.basename(file);
747
+ const parent = this.fsw._getWatchedDir(dirname3);
748
+ let prevStats = stats;
749
+ if (parent.has(basename3))
750
+ return;
751
+ const listener = async (path3, newStats) => {
752
+ if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5))
753
+ return;
754
+ if (!newStats || newStats.mtimeMs === 0) {
755
+ try {
756
+ const newStats2 = await stat(file);
757
+ if (this.fsw.closed)
758
+ return;
759
+ const at = newStats2.atimeMs;
760
+ const mt = newStats2.mtimeMs;
761
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
762
+ this.fsw._emit(EV.CHANGE, file, newStats2);
763
+ }
764
+ if ((isMacos || isLinux || isFreeBSD) && prevStats.ino !== newStats2.ino) {
765
+ this.fsw._closeFile(path3);
766
+ prevStats = newStats2;
767
+ const closer2 = this._watchWithNodeFs(file, listener);
768
+ if (closer2)
769
+ this.fsw._addPathCloser(path3, closer2);
770
+ } else {
771
+ prevStats = newStats2;
772
+ }
773
+ } catch (error) {
774
+ this.fsw._remove(dirname3, basename3);
775
+ }
776
+ } else if (parent.has(basename3)) {
777
+ const at = newStats.atimeMs;
778
+ const mt = newStats.mtimeMs;
779
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
780
+ this.fsw._emit(EV.CHANGE, file, newStats);
781
+ }
782
+ prevStats = newStats;
783
+ }
784
+ };
785
+ const closer = this._watchWithNodeFs(file, listener);
786
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
787
+ if (!this.fsw._throttle(EV.ADD, file, 0))
788
+ return;
789
+ this.fsw._emit(EV.ADD, file, stats);
790
+ }
791
+ return closer;
792
+ }
793
+ /**
794
+ * Handle symlinks encountered while reading a dir.
795
+ * @param entry returned by readdirp
796
+ * @param directory path of dir being read
797
+ * @param path of this item
798
+ * @param item basename of this item
799
+ * @returns true if no more processing is needed for this entry.
800
+ */
801
+ async _handleSymlink(entry, directory, path3, item) {
802
+ if (this.fsw.closed) {
803
+ return;
804
+ }
805
+ const full = entry.fullPath;
806
+ const dir = this.fsw._getWatchedDir(directory);
807
+ if (!this.fsw.options.followSymlinks) {
808
+ this.fsw._incrReadyCount();
809
+ let linkPath;
810
+ try {
811
+ linkPath = await realpath(path3);
812
+ } catch (e) {
813
+ this.fsw._emitReady();
814
+ return true;
815
+ }
816
+ if (this.fsw.closed)
817
+ return;
818
+ if (dir.has(item)) {
819
+ if (this.fsw._symlinkPaths.get(full) !== linkPath) {
820
+ this.fsw._symlinkPaths.set(full, linkPath);
821
+ this.fsw._emit(EV.CHANGE, path3, entry.stats);
822
+ }
823
+ } else {
824
+ dir.add(item);
825
+ this.fsw._symlinkPaths.set(full, linkPath);
826
+ this.fsw._emit(EV.ADD, path3, entry.stats);
827
+ }
828
+ this.fsw._emitReady();
829
+ return true;
830
+ }
831
+ if (this.fsw._symlinkPaths.has(full)) {
832
+ return true;
833
+ }
834
+ this.fsw._symlinkPaths.set(full, true);
835
+ }
836
+ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
837
+ directory = sp2.join(directory, "");
838
+ const throttleKey = target ? `${directory}:${target}` : directory;
839
+ throttler = this.fsw._throttle("readdir", throttleKey, 1e3);
840
+ if (!throttler)
841
+ return;
842
+ const previous = this.fsw._getWatchedDir(wh.path);
843
+ const current = /* @__PURE__ */ new Set();
844
+ let stream = this.fsw._readdirp(directory, {
845
+ fileFilter: (entry) => wh.filterPath(entry),
846
+ directoryFilter: (entry) => wh.filterDir(entry)
847
+ });
848
+ if (!stream)
849
+ return;
850
+ stream.on(STR_DATA, async (entry) => {
851
+ if (this.fsw.closed) {
852
+ stream = void 0;
853
+ return;
854
+ }
855
+ const item = entry.path;
856
+ let path3 = sp2.join(directory, item);
857
+ current.add(item);
858
+ if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path3, item)) {
859
+ return;
860
+ }
861
+ if (this.fsw.closed) {
862
+ stream = void 0;
863
+ return;
864
+ }
865
+ if (item === target || !target && !previous.has(item)) {
866
+ this.fsw._incrReadyCount();
867
+ path3 = sp2.join(dir, sp2.relative(dir, path3));
868
+ this._addToNodeFs(path3, initialAdd, wh, depth + 1);
869
+ }
870
+ }).on(EV.ERROR, this._boundHandleError);
871
+ return new Promise((resolve3, reject) => {
872
+ if (!stream)
873
+ return reject();
874
+ stream.once(STR_END, () => {
875
+ if (this.fsw.closed) {
876
+ stream = void 0;
877
+ return;
878
+ }
879
+ const wasThrottled = throttler ? throttler.clear() : false;
880
+ resolve3(void 0);
881
+ previous.getChildren().filter((item) => {
882
+ return item !== directory && !current.has(item);
883
+ }).forEach((item) => {
884
+ this.fsw._remove(directory, item);
885
+ });
886
+ stream = void 0;
887
+ if (wasThrottled)
888
+ this._handleRead(directory, false, wh, target, dir, depth, throttler);
889
+ });
890
+ });
891
+ }
892
+ /**
893
+ * Read directory to add / remove files from `@watched` list and re-read it on change.
894
+ * @param dir fs path
895
+ * @param stats
896
+ * @param initialAdd
897
+ * @param depth relative to user-supplied path
898
+ * @param target child path targeted for watch
899
+ * @param wh Common watch helpers for this path
900
+ * @param realpath
901
+ * @returns closer for the watcher instance.
902
+ */
903
+ async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath2) {
904
+ const parentDir = this.fsw._getWatchedDir(sp2.dirname(dir));
905
+ const tracked = parentDir.has(sp2.basename(dir));
906
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
907
+ this.fsw._emit(EV.ADD_DIR, dir, stats);
908
+ }
909
+ parentDir.add(sp2.basename(dir));
910
+ this.fsw._getWatchedDir(dir);
911
+ let throttler;
912
+ let closer;
913
+ const oDepth = this.fsw.options.depth;
914
+ if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath2)) {
915
+ if (!target) {
916
+ await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
917
+ if (this.fsw.closed)
918
+ return;
919
+ }
920
+ closer = this._watchWithNodeFs(dir, (dirPath, stats2) => {
921
+ if (stats2 && stats2.mtimeMs === 0)
922
+ return;
923
+ this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
924
+ });
925
+ }
926
+ return closer;
927
+ }
928
+ /**
929
+ * Handle added file, directory, or glob pattern.
930
+ * Delegates call to _handleFile / _handleDir after checks.
931
+ * @param path to file or ir
932
+ * @param initialAdd was the file added at watch instantiation?
933
+ * @param priorWh depth relative to user-supplied path
934
+ * @param depth Child path actually targeted for watch
935
+ * @param target Child path actually targeted for watch
936
+ */
937
+ async _addToNodeFs(path3, initialAdd, priorWh, depth, target) {
938
+ const ready = this.fsw._emitReady;
939
+ if (this.fsw._isIgnored(path3) || this.fsw.closed) {
940
+ ready();
941
+ return false;
942
+ }
943
+ const wh = this.fsw._getWatchHelpers(path3);
944
+ if (priorWh) {
945
+ wh.filterPath = (entry) => priorWh.filterPath(entry);
946
+ wh.filterDir = (entry) => priorWh.filterDir(entry);
947
+ }
948
+ try {
949
+ const stats = await statMethods[wh.statMethod](wh.watchPath);
950
+ if (this.fsw.closed)
951
+ return;
952
+ if (this.fsw._isIgnored(wh.watchPath, stats)) {
953
+ ready();
954
+ return false;
955
+ }
956
+ const follow = this.fsw.options.followSymlinks;
957
+ let closer;
958
+ if (stats.isDirectory()) {
959
+ const absPath = sp2.resolve(path3);
960
+ const targetPath = follow ? await realpath(path3) : path3;
961
+ if (this.fsw.closed)
962
+ return;
963
+ closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
964
+ if (this.fsw.closed)
965
+ return;
966
+ if (absPath !== targetPath && targetPath !== void 0) {
967
+ this.fsw._symlinkPaths.set(absPath, targetPath);
968
+ }
969
+ } else if (stats.isSymbolicLink()) {
970
+ const targetPath = follow ? await realpath(path3) : path3;
971
+ if (this.fsw.closed)
972
+ return;
973
+ const parent = sp2.dirname(wh.watchPath);
974
+ this.fsw._getWatchedDir(parent).add(wh.watchPath);
975
+ this.fsw._emit(EV.ADD, wh.watchPath, stats);
976
+ closer = await this._handleDir(parent, stats, initialAdd, depth, path3, wh, targetPath);
977
+ if (this.fsw.closed)
978
+ return;
979
+ if (targetPath !== void 0) {
980
+ this.fsw._symlinkPaths.set(sp2.resolve(path3), targetPath);
981
+ }
982
+ } else {
983
+ closer = this._handleFile(wh.watchPath, stats, initialAdd);
984
+ }
985
+ ready();
986
+ if (closer)
987
+ this.fsw._addPathCloser(path3, closer);
988
+ return false;
989
+ } catch (error) {
990
+ if (this.fsw._handleError(error)) {
991
+ ready();
992
+ return path3;
993
+ }
994
+ }
995
+ }
996
+ };
997
+
998
+ // node_modules/chokidar/index.js
999
+ var SLASH = "/";
1000
+ var SLASH_SLASH = "//";
1001
+ var ONE_DOT = ".";
1002
+ var TWO_DOTS = "..";
1003
+ var STRING_TYPE = "string";
1004
+ var BACK_SLASH_RE = /\\/g;
1005
+ var DOUBLE_SLASH_RE = /\/\//g;
1006
+ var DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
1007
+ var REPLACER_RE = /^\.[/\\]/;
1008
+ function arrify(item) {
1009
+ return Array.isArray(item) ? item : [item];
1010
+ }
1011
+ var isMatcherObject = (matcher) => typeof matcher === "object" && matcher !== null && !(matcher instanceof RegExp);
1012
+ function createPattern(matcher) {
1013
+ if (typeof matcher === "function")
1014
+ return matcher;
1015
+ if (typeof matcher === "string")
1016
+ return (string) => matcher === string;
1017
+ if (matcher instanceof RegExp)
1018
+ return (string) => matcher.test(string);
1019
+ if (typeof matcher === "object" && matcher !== null) {
1020
+ return (string) => {
1021
+ if (matcher.path === string)
1022
+ return true;
1023
+ if (matcher.recursive) {
1024
+ const relative3 = sp2.relative(matcher.path, string);
1025
+ if (!relative3) {
1026
+ return false;
1027
+ }
1028
+ return !relative3.startsWith("..") && !sp2.isAbsolute(relative3);
1029
+ }
1030
+ return false;
1031
+ };
1032
+ }
1033
+ return () => false;
1034
+ }
1035
+ function normalizePath(path3) {
1036
+ if (typeof path3 !== "string")
1037
+ throw new Error("string expected");
1038
+ path3 = sp2.normalize(path3);
1039
+ path3 = path3.replace(/\\/g, "/");
1040
+ let prepend = false;
1041
+ if (path3.startsWith("//"))
1042
+ prepend = true;
1043
+ path3 = path3.replace(DOUBLE_SLASH_RE, "/");
1044
+ if (prepend)
1045
+ path3 = "/" + path3;
1046
+ return path3;
1047
+ }
1048
+ function matchPatterns(patterns, testString, stats) {
1049
+ const path3 = normalizePath(testString);
1050
+ for (let index = 0; index < patterns.length; index++) {
1051
+ const pattern = patterns[index];
1052
+ if (pattern(path3, stats)) {
1053
+ return true;
1054
+ }
1055
+ }
1056
+ return false;
1057
+ }
1058
+ function anymatch(matchers, testString) {
1059
+ if (matchers == null) {
1060
+ throw new TypeError("anymatch: specify first argument");
1061
+ }
1062
+ const matchersArray = arrify(matchers);
1063
+ const patterns = matchersArray.map((matcher) => createPattern(matcher));
1064
+ {
1065
+ return (testString2, stats) => {
1066
+ return matchPatterns(patterns, testString2, stats);
1067
+ };
1068
+ }
1069
+ }
1070
+ var unifyPaths = (paths_) => {
1071
+ const paths = arrify(paths_).flat();
1072
+ if (!paths.every((p) => typeof p === STRING_TYPE)) {
1073
+ throw new TypeError(`Non-string provided as watch path: ${paths}`);
1074
+ }
1075
+ return paths.map(normalizePathToUnix);
1076
+ };
1077
+ var toUnix = (string) => {
1078
+ let str = string.replace(BACK_SLASH_RE, SLASH);
1079
+ let prepend = false;
1080
+ if (str.startsWith(SLASH_SLASH)) {
1081
+ prepend = true;
1082
+ }
1083
+ str = str.replace(DOUBLE_SLASH_RE, SLASH);
1084
+ if (prepend) {
1085
+ str = SLASH + str;
1086
+ }
1087
+ return str;
1088
+ };
1089
+ var normalizePathToUnix = (path3) => toUnix(sp2.normalize(toUnix(path3)));
1090
+ var normalizeIgnored = (cwd = "") => (path3) => {
1091
+ if (typeof path3 === "string") {
1092
+ return normalizePathToUnix(sp2.isAbsolute(path3) ? path3 : sp2.join(cwd, path3));
1093
+ } else {
1094
+ return path3;
1095
+ }
1096
+ };
1097
+ var getAbsolutePath = (path3, cwd) => {
1098
+ if (sp2.isAbsolute(path3)) {
1099
+ return path3;
1100
+ }
1101
+ return sp2.join(cwd, path3);
1102
+ };
1103
+ var EMPTY_SET = Object.freeze(/* @__PURE__ */ new Set());
1104
+ var DirEntry = class {
1105
+ path;
1106
+ _removeWatcher;
1107
+ items;
1108
+ constructor(dir, removeWatcher) {
1109
+ this.path = dir;
1110
+ this._removeWatcher = removeWatcher;
1111
+ this.items = /* @__PURE__ */ new Set();
1112
+ }
1113
+ add(item) {
1114
+ const { items } = this;
1115
+ if (!items)
1116
+ return;
1117
+ if (item !== ONE_DOT && item !== TWO_DOTS)
1118
+ items.add(item);
1119
+ }
1120
+ async remove(item) {
1121
+ const { items } = this;
1122
+ if (!items)
1123
+ return;
1124
+ items.delete(item);
1125
+ if (items.size > 0)
1126
+ return;
1127
+ const dir = this.path;
1128
+ try {
1129
+ await readdir(dir);
1130
+ } catch (err) {
1131
+ if (this._removeWatcher) {
1132
+ this._removeWatcher(sp2.dirname(dir), sp2.basename(dir));
1133
+ }
1134
+ }
1135
+ }
1136
+ has(item) {
1137
+ const { items } = this;
1138
+ if (!items)
1139
+ return;
1140
+ return items.has(item);
1141
+ }
1142
+ getChildren() {
1143
+ const { items } = this;
1144
+ if (!items)
1145
+ return [];
1146
+ return [...items.values()];
1147
+ }
1148
+ dispose() {
1149
+ this.items.clear();
1150
+ this.path = "";
1151
+ this._removeWatcher = EMPTY_FN;
1152
+ this.items = EMPTY_SET;
1153
+ Object.freeze(this);
1154
+ }
1155
+ };
1156
+ var STAT_METHOD_F = "stat";
1157
+ var STAT_METHOD_L = "lstat";
1158
+ var WatchHelper = class {
1159
+ fsw;
1160
+ path;
1161
+ watchPath;
1162
+ fullWatchPath;
1163
+ dirParts;
1164
+ followSymlinks;
1165
+ statMethod;
1166
+ constructor(path3, follow, fsw) {
1167
+ this.fsw = fsw;
1168
+ const watchPath = path3;
1169
+ this.path = path3 = path3.replace(REPLACER_RE, "");
1170
+ this.watchPath = watchPath;
1171
+ this.fullWatchPath = sp2.resolve(watchPath);
1172
+ this.dirParts = [];
1173
+ this.dirParts.forEach((parts) => {
1174
+ if (parts.length > 1)
1175
+ parts.pop();
1176
+ });
1177
+ this.followSymlinks = follow;
1178
+ this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
1179
+ }
1180
+ entryPath(entry) {
1181
+ return sp2.join(this.watchPath, sp2.relative(this.watchPath, entry.fullPath));
1182
+ }
1183
+ filterPath(entry) {
1184
+ const { stats } = entry;
1185
+ if (stats && stats.isSymbolicLink())
1186
+ return this.filterDir(entry);
1187
+ const resolvedPath = this.entryPath(entry);
1188
+ return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
1189
+ }
1190
+ filterDir(entry) {
1191
+ return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
1192
+ }
1193
+ };
1194
+ var FSWatcher = class extends EventEmitter {
1195
+ closed;
1196
+ options;
1197
+ _closers;
1198
+ _ignoredPaths;
1199
+ _throttled;
1200
+ _streams;
1201
+ _symlinkPaths;
1202
+ _watched;
1203
+ _pendingWrites;
1204
+ _pendingUnlinks;
1205
+ _readyCount;
1206
+ _emitReady;
1207
+ _closePromise;
1208
+ _userIgnored;
1209
+ _readyEmitted;
1210
+ _emitRaw;
1211
+ _boundRemove;
1212
+ _nodeFsHandler;
1213
+ // Not indenting methods for history sake; for now.
1214
+ constructor(_opts = {}) {
1215
+ super();
1216
+ this.closed = false;
1217
+ this._closers = /* @__PURE__ */ new Map();
1218
+ this._ignoredPaths = /* @__PURE__ */ new Set();
1219
+ this._throttled = /* @__PURE__ */ new Map();
1220
+ this._streams = /* @__PURE__ */ new Set();
1221
+ this._symlinkPaths = /* @__PURE__ */ new Map();
1222
+ this._watched = /* @__PURE__ */ new Map();
1223
+ this._pendingWrites = /* @__PURE__ */ new Map();
1224
+ this._pendingUnlinks = /* @__PURE__ */ new Map();
1225
+ this._readyCount = 0;
1226
+ this._readyEmitted = false;
1227
+ const awf = _opts.awaitWriteFinish;
1228
+ const DEF_AWF = { stabilityThreshold: 2e3, pollInterval: 100 };
1229
+ const opts = {
1230
+ // Defaults
1231
+ persistent: true,
1232
+ ignoreInitial: false,
1233
+ ignorePermissionErrors: false,
1234
+ interval: 100,
1235
+ binaryInterval: 300,
1236
+ followSymlinks: true,
1237
+ usePolling: false,
1238
+ // useAsync: false,
1239
+ atomic: true,
1240
+ // NOTE: overwritten later (depends on usePolling)
1241
+ ..._opts,
1242
+ // Change format
1243
+ ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
1244
+ awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === "object" ? { ...DEF_AWF, ...awf } : false
1245
+ };
1246
+ if (isIBMi)
1247
+ opts.usePolling = true;
1248
+ if (opts.atomic === void 0)
1249
+ opts.atomic = !opts.usePolling;
1250
+ const envPoll = process.env.CHOKIDAR_USEPOLLING;
1251
+ if (envPoll !== void 0) {
1252
+ const envLower = envPoll.toLowerCase();
1253
+ if (envLower === "false" || envLower === "0")
1254
+ opts.usePolling = false;
1255
+ else if (envLower === "true" || envLower === "1")
1256
+ opts.usePolling = true;
1257
+ else
1258
+ opts.usePolling = !!envLower;
1259
+ }
1260
+ const envInterval = process.env.CHOKIDAR_INTERVAL;
1261
+ if (envInterval)
1262
+ opts.interval = Number.parseInt(envInterval, 10);
1263
+ let readyCalls = 0;
1264
+ this._emitReady = () => {
1265
+ readyCalls++;
1266
+ if (readyCalls >= this._readyCount) {
1267
+ this._emitReady = EMPTY_FN;
1268
+ this._readyEmitted = true;
1269
+ process.nextTick(() => this.emit(EVENTS.READY));
1270
+ }
1271
+ };
1272
+ this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
1273
+ this._boundRemove = this._remove.bind(this);
1274
+ this.options = opts;
1275
+ this._nodeFsHandler = new NodeFsHandler(this);
1276
+ Object.freeze(opts);
1277
+ }
1278
+ _addIgnoredPath(matcher) {
1279
+ if (isMatcherObject(matcher)) {
1280
+ for (const ignored of this._ignoredPaths) {
1281
+ if (isMatcherObject(ignored) && ignored.path === matcher.path && ignored.recursive === matcher.recursive) {
1282
+ return;
1283
+ }
1284
+ }
1285
+ }
1286
+ this._ignoredPaths.add(matcher);
1287
+ }
1288
+ _removeIgnoredPath(matcher) {
1289
+ this._ignoredPaths.delete(matcher);
1290
+ if (typeof matcher === "string") {
1291
+ for (const ignored of this._ignoredPaths) {
1292
+ if (isMatcherObject(ignored) && ignored.path === matcher) {
1293
+ this._ignoredPaths.delete(ignored);
1294
+ }
1295
+ }
1296
+ }
1297
+ }
1298
+ // Public methods
1299
+ /**
1300
+ * Adds paths to be watched on an existing FSWatcher instance.
1301
+ * @param paths_ file or file list. Other arguments are unused
1302
+ */
1303
+ add(paths_, _origAdd, _internal) {
1304
+ const { cwd } = this.options;
1305
+ this.closed = false;
1306
+ this._closePromise = void 0;
1307
+ let paths = unifyPaths(paths_);
1308
+ if (cwd) {
1309
+ paths = paths.map((path3) => {
1310
+ const absPath = getAbsolutePath(path3, cwd);
1311
+ return absPath;
1312
+ });
1313
+ }
1314
+ paths.forEach((path3) => {
1315
+ this._removeIgnoredPath(path3);
1316
+ });
1317
+ this._userIgnored = void 0;
1318
+ if (!this._readyCount)
1319
+ this._readyCount = 0;
1320
+ this._readyCount += paths.length;
1321
+ Promise.all(paths.map(async (path3) => {
1322
+ const res = await this._nodeFsHandler._addToNodeFs(path3, !_internal, void 0, 0, _origAdd);
1323
+ if (res)
1324
+ this._emitReady();
1325
+ return res;
1326
+ })).then((results) => {
1327
+ if (this.closed)
1328
+ return;
1329
+ results.forEach((item) => {
1330
+ if (item)
1331
+ this.add(sp2.dirname(item), sp2.basename(_origAdd || item));
1332
+ });
1333
+ });
1334
+ return this;
1335
+ }
1336
+ /**
1337
+ * Close watchers or start ignoring events from specified paths.
1338
+ */
1339
+ unwatch(paths_) {
1340
+ if (this.closed)
1341
+ return this;
1342
+ const paths = unifyPaths(paths_);
1343
+ const { cwd } = this.options;
1344
+ paths.forEach((path3) => {
1345
+ if (!sp2.isAbsolute(path3) && !this._closers.has(path3)) {
1346
+ if (cwd)
1347
+ path3 = sp2.join(cwd, path3);
1348
+ path3 = sp2.resolve(path3);
1349
+ }
1350
+ this._closePath(path3);
1351
+ this._addIgnoredPath(path3);
1352
+ if (this._watched.has(path3)) {
1353
+ this._addIgnoredPath({
1354
+ path: path3,
1355
+ recursive: true
1356
+ });
1357
+ }
1358
+ this._userIgnored = void 0;
1359
+ });
1360
+ return this;
1361
+ }
1362
+ /**
1363
+ * Close watchers and remove all listeners from watched paths.
1364
+ */
1365
+ close() {
1366
+ if (this._closePromise) {
1367
+ return this._closePromise;
1368
+ }
1369
+ this.closed = true;
1370
+ this.removeAllListeners();
1371
+ const closers = [];
1372
+ this._closers.forEach((closerList) => closerList.forEach((closer) => {
1373
+ const promise = closer();
1374
+ if (promise instanceof Promise)
1375
+ closers.push(promise);
1376
+ }));
1377
+ this._streams.forEach((stream) => stream.destroy());
1378
+ this._userIgnored = void 0;
1379
+ this._readyCount = 0;
1380
+ this._readyEmitted = false;
1381
+ this._watched.forEach((dirent) => dirent.dispose());
1382
+ this._closers.clear();
1383
+ this._watched.clear();
1384
+ this._streams.clear();
1385
+ this._symlinkPaths.clear();
1386
+ this._throttled.clear();
1387
+ this._closePromise = closers.length ? Promise.all(closers).then(() => void 0) : Promise.resolve();
1388
+ return this._closePromise;
1389
+ }
1390
+ /**
1391
+ * Expose list of watched paths
1392
+ * @returns for chaining
1393
+ */
1394
+ getWatched() {
1395
+ const watchList = {};
1396
+ this._watched.forEach((entry, dir) => {
1397
+ const key = this.options.cwd ? sp2.relative(this.options.cwd, dir) : dir;
1398
+ const index = key || ONE_DOT;
1399
+ watchList[index] = entry.getChildren().sort();
1400
+ });
1401
+ return watchList;
1402
+ }
1403
+ emitWithAll(event, args) {
1404
+ this.emit(event, ...args);
1405
+ if (event !== EVENTS.ERROR)
1406
+ this.emit(EVENTS.ALL, event, ...args);
1407
+ }
1408
+ // Common helpers
1409
+ // --------------
1410
+ /**
1411
+ * Normalize and emit events.
1412
+ * Calling _emit DOES NOT MEAN emit() would be called!
1413
+ * @param event Type of event
1414
+ * @param path File or directory path
1415
+ * @param stats arguments to be passed with event
1416
+ * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
1417
+ */
1418
+ async _emit(event, path3, stats) {
1419
+ if (this.closed)
1420
+ return;
1421
+ const opts = this.options;
1422
+ if (isWindows)
1423
+ path3 = sp2.normalize(path3);
1424
+ if (opts.cwd)
1425
+ path3 = sp2.relative(opts.cwd, path3);
1426
+ const args = [path3];
1427
+ if (stats != null)
1428
+ args.push(stats);
1429
+ const awf = opts.awaitWriteFinish;
1430
+ let pw;
1431
+ if (awf && (pw = this._pendingWrites.get(path3))) {
1432
+ pw.lastChange = /* @__PURE__ */ new Date();
1433
+ return this;
1434
+ }
1435
+ if (opts.atomic) {
1436
+ if (event === EVENTS.UNLINK) {
1437
+ this._pendingUnlinks.set(path3, [event, ...args]);
1438
+ setTimeout(() => {
1439
+ this._pendingUnlinks.forEach((entry, path4) => {
1440
+ this.emit(...entry);
1441
+ this.emit(EVENTS.ALL, ...entry);
1442
+ this._pendingUnlinks.delete(path4);
1443
+ });
1444
+ }, typeof opts.atomic === "number" ? opts.atomic : 100);
1445
+ return this;
1446
+ }
1447
+ if (event === EVENTS.ADD && this._pendingUnlinks.has(path3)) {
1448
+ event = EVENTS.CHANGE;
1449
+ this._pendingUnlinks.delete(path3);
1450
+ }
1451
+ }
1452
+ if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
1453
+ const awfEmit = (err, stats2) => {
1454
+ if (err) {
1455
+ event = EVENTS.ERROR;
1456
+ args[0] = err;
1457
+ this.emitWithAll(event, args);
1458
+ } else if (stats2) {
1459
+ if (args.length > 1) {
1460
+ args[1] = stats2;
1461
+ } else {
1462
+ args.push(stats2);
1463
+ }
1464
+ this.emitWithAll(event, args);
1465
+ }
1466
+ };
1467
+ this._awaitWriteFinish(path3, awf.stabilityThreshold, event, awfEmit);
1468
+ return this;
1469
+ }
1470
+ if (event === EVENTS.CHANGE) {
1471
+ const isThrottled = !this._throttle(EVENTS.CHANGE, path3, 50);
1472
+ if (isThrottled)
1473
+ return this;
1474
+ }
1475
+ if (opts.alwaysStat && stats === void 0 && (event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
1476
+ const fullPath = opts.cwd ? sp2.join(opts.cwd, path3) : path3;
1477
+ let stats2;
1478
+ try {
1479
+ stats2 = await stat(fullPath);
1480
+ } catch (err) {
1481
+ }
1482
+ if (!stats2 || this.closed)
1483
+ return;
1484
+ args.push(stats2);
1485
+ }
1486
+ this.emitWithAll(event, args);
1487
+ return this;
1488
+ }
1489
+ /**
1490
+ * Common handler for errors
1491
+ * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
1492
+ */
1493
+ _handleError(error) {
1494
+ const code = error && error.code;
1495
+ if (error && code !== "ENOENT" && code !== "ENOTDIR" && (!this.options.ignorePermissionErrors || code !== "EPERM" && code !== "EACCES")) {
1496
+ this.emit(EVENTS.ERROR, error);
1497
+ }
1498
+ return error || this.closed;
1499
+ }
1500
+ /**
1501
+ * Helper utility for throttling
1502
+ * @param actionType type being throttled
1503
+ * @param path being acted upon
1504
+ * @param timeout duration of time to suppress duplicate actions
1505
+ * @returns tracking object or false if action should be suppressed
1506
+ */
1507
+ _throttle(actionType, path3, timeout) {
1508
+ if (!this._throttled.has(actionType)) {
1509
+ this._throttled.set(actionType, /* @__PURE__ */ new Map());
1510
+ }
1511
+ const action = this._throttled.get(actionType);
1512
+ if (!action)
1513
+ throw new Error("invalid throttle");
1514
+ const actionPath = action.get(path3);
1515
+ if (actionPath) {
1516
+ actionPath.count++;
1517
+ return false;
1518
+ }
1519
+ let timeoutObject;
1520
+ const clear = () => {
1521
+ const item = action.get(path3);
1522
+ const count = item ? item.count : 0;
1523
+ action.delete(path3);
1524
+ clearTimeout(timeoutObject);
1525
+ if (item)
1526
+ clearTimeout(item.timeoutObject);
1527
+ return count;
1528
+ };
1529
+ timeoutObject = setTimeout(clear, timeout);
1530
+ const thr = { timeoutObject, clear, count: 0 };
1531
+ action.set(path3, thr);
1532
+ return thr;
1533
+ }
1534
+ _incrReadyCount() {
1535
+ return this._readyCount++;
1536
+ }
1537
+ /**
1538
+ * Awaits write operation to finish.
1539
+ * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
1540
+ * @param path being acted upon
1541
+ * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
1542
+ * @param event
1543
+ * @param awfEmit Callback to be called when ready for event to be emitted.
1544
+ */
1545
+ _awaitWriteFinish(path3, threshold, event, awfEmit) {
1546
+ const awf = this.options.awaitWriteFinish;
1547
+ if (typeof awf !== "object")
1548
+ return;
1549
+ const pollInterval = awf.pollInterval;
1550
+ let timeoutHandler;
1551
+ let fullPath = path3;
1552
+ if (this.options.cwd && !sp2.isAbsolute(path3)) {
1553
+ fullPath = sp2.join(this.options.cwd, path3);
1554
+ }
1555
+ const now = /* @__PURE__ */ new Date();
1556
+ const writes = this._pendingWrites;
1557
+ function awaitWriteFinishFn(prevStat) {
1558
+ stat$1(fullPath, (err, curStat) => {
1559
+ if (err || !writes.has(path3)) {
1560
+ if (err && err.code !== "ENOENT")
1561
+ awfEmit(err);
1562
+ return;
1563
+ }
1564
+ const now2 = Number(/* @__PURE__ */ new Date());
1565
+ if (prevStat && curStat.size !== prevStat.size) {
1566
+ writes.get(path3).lastChange = now2;
1567
+ }
1568
+ const pw = writes.get(path3);
1569
+ const df = now2 - pw.lastChange;
1570
+ if (df >= threshold) {
1571
+ writes.delete(path3);
1572
+ awfEmit(void 0, curStat);
1573
+ } else {
1574
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
1575
+ }
1576
+ });
1577
+ }
1578
+ if (!writes.has(path3)) {
1579
+ writes.set(path3, {
1580
+ lastChange: now,
1581
+ cancelWait: () => {
1582
+ writes.delete(path3);
1583
+ clearTimeout(timeoutHandler);
1584
+ return event;
1585
+ }
1586
+ });
1587
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
1588
+ }
1589
+ }
1590
+ /**
1591
+ * Determines whether user has asked to ignore this path.
1592
+ */
1593
+ _isIgnored(path3, stats) {
1594
+ if (this.options.atomic && DOT_RE.test(path3))
1595
+ return true;
1596
+ if (!this._userIgnored) {
1597
+ const { cwd } = this.options;
1598
+ const ign = this.options.ignored;
1599
+ const ignored = (ign || []).map(normalizeIgnored(cwd));
1600
+ const ignoredPaths = [...this._ignoredPaths];
1601
+ const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
1602
+ this._userIgnored = anymatch(list);
1603
+ }
1604
+ return this._userIgnored(path3, stats);
1605
+ }
1606
+ _isntIgnored(path3, stat5) {
1607
+ return !this._isIgnored(path3, stat5);
1608
+ }
1609
+ /**
1610
+ * Provides a set of common helpers and properties relating to symlink handling.
1611
+ * @param path file or directory pattern being watched
1612
+ */
1613
+ _getWatchHelpers(path3) {
1614
+ return new WatchHelper(path3, this.options.followSymlinks, this);
1615
+ }
1616
+ // Directory helpers
1617
+ // -----------------
1618
+ /**
1619
+ * Provides directory tracking objects
1620
+ * @param directory path of the directory
1621
+ */
1622
+ _getWatchedDir(directory) {
1623
+ const dir = sp2.resolve(directory);
1624
+ if (!this._watched.has(dir))
1625
+ this._watched.set(dir, new DirEntry(dir, this._boundRemove));
1626
+ return this._watched.get(dir);
1627
+ }
1628
+ // File helpers
1629
+ // ------------
1630
+ /**
1631
+ * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
1632
+ */
1633
+ _hasReadPermissions(stats) {
1634
+ if (this.options.ignorePermissionErrors)
1635
+ return true;
1636
+ return Boolean(Number(stats.mode) & 256);
1637
+ }
1638
+ /**
1639
+ * Handles emitting unlink events for
1640
+ * files and directories, and via recursion, for
1641
+ * files and directories within directories that are unlinked
1642
+ * @param directory within which the following item is located
1643
+ * @param item base path of item/directory
1644
+ */
1645
+ _remove(directory, item, isDirectory) {
1646
+ const path3 = sp2.join(directory, item);
1647
+ const fullPath = sp2.resolve(path3);
1648
+ isDirectory = isDirectory != null ? isDirectory : this._watched.has(path3) || this._watched.has(fullPath);
1649
+ if (!this._throttle("remove", path3, 100))
1650
+ return;
1651
+ if (!isDirectory && this._watched.size === 1) {
1652
+ this.add(directory, item, true);
1653
+ }
1654
+ const wp = this._getWatchedDir(path3);
1655
+ const nestedDirectoryChildren = wp.getChildren();
1656
+ nestedDirectoryChildren.forEach((nested) => this._remove(path3, nested));
1657
+ const parent = this._getWatchedDir(directory);
1658
+ const wasTracked = parent.has(item);
1659
+ parent.remove(item);
1660
+ if (this._symlinkPaths.has(fullPath)) {
1661
+ this._symlinkPaths.delete(fullPath);
1662
+ }
1663
+ let relPath = path3;
1664
+ if (this.options.cwd)
1665
+ relPath = sp2.relative(this.options.cwd, path3);
1666
+ if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
1667
+ const event = this._pendingWrites.get(relPath).cancelWait();
1668
+ if (event === EVENTS.ADD)
1669
+ return;
1670
+ }
1671
+ this._watched.delete(path3);
1672
+ this._watched.delete(fullPath);
1673
+ const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
1674
+ if (wasTracked && !this._isIgnored(path3))
1675
+ this._emit(eventName, path3);
1676
+ this._closePath(path3);
1677
+ }
1678
+ /**
1679
+ * Closes all watchers for a path
1680
+ */
1681
+ _closePath(path3) {
1682
+ this._closeFile(path3);
1683
+ const dir = sp2.dirname(path3);
1684
+ this._getWatchedDir(dir).remove(sp2.basename(path3));
1685
+ }
1686
+ /**
1687
+ * Closes only file-specific watchers
1688
+ */
1689
+ _closeFile(path3) {
1690
+ const closers = this._closers.get(path3);
1691
+ if (!closers)
1692
+ return;
1693
+ closers.forEach((closer) => closer());
1694
+ this._closers.delete(path3);
1695
+ }
1696
+ _addPathCloser(path3, closer) {
1697
+ if (!closer)
1698
+ return;
1699
+ let list = this._closers.get(path3);
1700
+ if (!list) {
1701
+ list = [];
1702
+ this._closers.set(path3, list);
1703
+ }
1704
+ list.push(closer);
1705
+ }
1706
+ _readdirp(root, opts) {
1707
+ if (this.closed)
1708
+ return;
1709
+ const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
1710
+ let stream = readdirp(root, options);
1711
+ this._streams.add(stream);
1712
+ stream.once(STR_CLOSE, () => {
1713
+ stream = void 0;
1714
+ });
1715
+ stream.once(STR_END, () => {
1716
+ if (stream) {
1717
+ this._streams.delete(stream);
1718
+ stream = void 0;
1719
+ }
1720
+ });
1721
+ return stream;
1722
+ }
1723
+ };
1724
+ function watch(paths, options = {}) {
1725
+ const watcher2 = new FSWatcher(options);
1726
+ watcher2.add(paths);
1727
+ return watcher2;
1728
+ }
1729
+ var exec = promisify(execFile);
1730
+ async function listFeatures(projectRoot) {
1731
+ const summaries = [];
1732
+ for (const vehicle of ["spec", "plan"]) {
1733
+ const dir = sp2__default.join(projectRoot, ".mumei", vehicle === "spec" ? "specs" : "plans");
1734
+ const entries = await safeReaddir(dir);
1735
+ for (const entry of entries) {
1736
+ if (entry.isDirectory()) {
1737
+ const summary = await summariseFeature({
1738
+ projectRoot,
1739
+ featureDir: sp2__default.join(dir, entry.name),
1740
+ featureKey: entry.name,
1741
+ vehicle
1742
+ });
1743
+ if (summary) summaries.push(summary);
1744
+ }
1745
+ }
1746
+ }
1747
+ summaries.sort((a, b) => b.last_activity_at.localeCompare(a.last_activity_at));
1748
+ return summaries;
1749
+ }
1750
+ async function summariseFeature(args) {
1751
+ const { projectRoot, featureDir, featureKey, vehicle } = args;
1752
+ const stateRaw = await safeReadFile(sp2__default.join(featureDir, "state.json"));
1753
+ if (!stateRaw) return null;
1754
+ let state;
1755
+ try {
1756
+ state = JSON.parse(stateRaw);
1757
+ } catch {
1758
+ return null;
1759
+ }
1760
+ const requirementsBody = await safeReadFile(sp2__default.join(featureDir, "requirements.md"));
1761
+ const ac_count = requirementsBody ? (requirementsBody.match(/^- REQ-\d+\.\d+/gm) ?? []).length : 0;
1762
+ const tasksBody = await safeReadFile(sp2__default.join(featureDir, "tasks.md"));
1763
+ const wave_count = tasksBody ? (tasksBody.match(/^## Wave \d+:/gm) ?? []).length : 0;
1764
+ const task_total = tasksBody ? (tasksBody.match(/^- \[/gm) ?? []).length : 0;
1765
+ const task_done = tasksBody ? (tasksBody.match(/^- \[x\]/gm) ?? []).length : 0;
1766
+ const last_review_verdict = await latestReviewVerdict(sp2__default.join(featureDir, "reviews"));
1767
+ const cost = await loadCost(projectRoot, featureKey);
1768
+ const lastTouchTs = await latestMtime(featureDir);
1769
+ return {
1770
+ feature: featureKey,
1771
+ id: state.id ?? featureKey,
1772
+ slug: state.slug ?? featureKey,
1773
+ vehicle,
1774
+ phase: state.phase ?? "plan",
1775
+ current_wave: state.current_wave ?? 0,
1776
+ total_waves: wave_count > 0 ? wave_count : null,
1777
+ last_review_verdict,
1778
+ last_activity_at: state.updated_at ?? lastTouchTs ?? (/* @__PURE__ */ new Date()).toISOString(),
1779
+ ac_count,
1780
+ task_total,
1781
+ task_done,
1782
+ cost_input: cost.input,
1783
+ cost_output: cost.output,
1784
+ cost_cache_read: cost.cache_read,
1785
+ cost_cache_create: cost.cache_create,
1786
+ cache_hit_rate: cost.cache_hit_rate
1787
+ };
1788
+ }
1789
+ async function latestReviewVerdict(reviewsDir) {
1790
+ const entries = await safeReaddir(reviewsDir);
1791
+ const reviewFiles = entries.filter((e) => e.isFile() && e.name.endsWith(".json") && !e.name.endsWith("-detectors.json")).map((e) => e.name).sort();
1792
+ const latestName = reviewFiles[reviewFiles.length - 1];
1793
+ if (!latestName) return null;
1794
+ const latestPath = sp2__default.join(reviewsDir, latestName);
1795
+ const body = await safeReadFile(latestPath);
1796
+ if (!body) return null;
1797
+ try {
1798
+ const parsed = JSON.parse(body);
1799
+ return parsed.verdict ?? null;
1800
+ } catch {
1801
+ return null;
1802
+ }
1803
+ }
1804
+ async function loadCost(projectRoot, feature) {
1805
+ try {
1806
+ const { stdout } = await exec("bash", [
1807
+ sp2__default.join(projectRoot, "scripts/aggregate-cost.sh"),
1808
+ "--json",
1809
+ feature
1810
+ ]);
1811
+ const parsed = JSON.parse(stdout);
1812
+ return {
1813
+ input: parsed.totals?.input ?? 0,
1814
+ output: parsed.totals?.output ?? 0,
1815
+ cache_read: parsed.totals?.cache_read ?? 0,
1816
+ cache_create: parsed.totals?.cache_create ?? 0,
1817
+ cache_hit_rate: parsed.cache_hit_rate ?? null
1818
+ };
1819
+ } catch {
1820
+ return { input: 0, output: 0, cache_read: 0, cache_create: 0, cache_hit_rate: null };
1821
+ }
1822
+ }
1823
+ async function safeReaddir(dir) {
1824
+ try {
1825
+ return await readdir(dir, { withFileTypes: true });
1826
+ } catch {
1827
+ return [];
1828
+ }
1829
+ }
1830
+ async function safeReadFile(p) {
1831
+ try {
1832
+ return await readFile(p, "utf8");
1833
+ } catch {
1834
+ return null;
1835
+ }
1836
+ }
1837
+ async function latestMtime(dir) {
1838
+ try {
1839
+ const s = await stat(dir);
1840
+ return s.mtime.toISOString();
1841
+ } catch {
1842
+ return null;
1843
+ }
1844
+ }
1845
+
1846
+ // server/index.ts
1847
+ var exec2 = promisify(execFile);
1848
+ var PROJECT_ROOT = process.cwd();
1849
+ var MUMEI_DIR = sp2__default.join(PROJECT_ROOT, ".mumei");
1850
+ var PORT = Number(process.env.MUMEI_DASHBOARD_PORT ?? "3001");
1851
+ var LOG_LEVEL = process.env.MUMEI_DASHBOARD_LOG_LEVEL ?? "info";
1852
+ var CORS_ORIGINS = process.env.MUMEI_DASHBOARD_CORS_ORIGINS?.split(",").map((s) => s.trim()).filter(Boolean) ?? ["http://localhost:5173"];
1853
+ var app = Fastify({
1854
+ logger: {
1855
+ level: LOG_LEVEL,
1856
+ // Redact sensitive headers in case the proxy ever forwards them
1857
+ // (defensive — local dashboard rarely sees auth headers).
1858
+ redact: {
1859
+ paths: ["req.headers.authorization", "req.headers.cookie", 'req.headers["set-cookie"]'],
1860
+ censor: "[REDACTED]"
1861
+ },
1862
+ transport: LOG_LEVEL === "debug" || LOG_LEVEL === "trace" ? { target: "pino-pretty", options: { colorize: true, translateTime: "HH:MM:ss.l" } } : void 0
1863
+ }
1864
+ });
1865
+ await app.register(helmet, {
1866
+ // Local dev tool — relax frame-src and disable HSTS (dashboard runs
1867
+ // on http://localhost so HTTPS-only headers would be a footgun).
1868
+ contentSecurityPolicy: {
1869
+ directives: {
1870
+ defaultSrc: ["'self'"],
1871
+ connectSrc: ["'self'", "http://localhost:*", "http://127.0.0.1:*"],
1872
+ scriptSrc: ["'self'", "'unsafe-inline'"],
1873
+ styleSrc: ["'self'", "'unsafe-inline'"],
1874
+ imgSrc: ["'self'", "data:"],
1875
+ fontSrc: ["'self'", "data:"]
1876
+ }
1877
+ },
1878
+ hsts: false,
1879
+ crossOriginEmbedderPolicy: false
1880
+ });
1881
+ await app.register(cors, {
1882
+ origin: (origin, cb) => {
1883
+ if (!origin) return cb(null, true);
1884
+ if (CORS_ORIGINS.includes(origin)) return cb(null, true);
1885
+ cb(new Error(`origin not allowed: ${origin}`), false);
1886
+ },
1887
+ credentials: true
1888
+ });
1889
+ var SlugParam = Type.Object({
1890
+ slug: Type.String({ pattern: "^[A-Za-z0-9_-]+$", minLength: 1, maxLength: 100 })
1891
+ });
1892
+ var DocParam = Type.Object({
1893
+ slug: Type.String({ pattern: "^[A-Za-z0-9_-]+$", minLength: 1, maxLength: 100 }),
1894
+ doc: Type.Union([Type.Literal("requirements"), Type.Literal("design"), Type.Literal("tasks")])
1895
+ });
1896
+ var FeatureQuery = Type.Object({
1897
+ feature: Type.String({ pattern: "^[A-Za-z0-9_-]+$", minLength: 1, maxLength: 100 })
1898
+ });
1899
+ app.get("/api/features", async () => {
1900
+ return listFeatures(PROJECT_ROOT);
1901
+ });
1902
+ app.get("/api/cost", { schema: { querystring: FeatureQuery } }, async (req) => {
1903
+ const { feature } = req.query;
1904
+ const { stdout } = await exec2("bash", [
1905
+ sp2__default.join(PROJECT_ROOT, "scripts/aggregate-cost.sh"),
1906
+ "--json",
1907
+ feature
1908
+ ]);
1909
+ return JSON.parse(stdout);
1910
+ });
1911
+ app.get("/api/hook-stats", async () => {
1912
+ const { stdout } = await exec2("bash", [
1913
+ sp2__default.join(PROJECT_ROOT, "scripts/aggregate-hook-stats.sh"),
1914
+ "--json"
1915
+ ]);
1916
+ return JSON.parse(stdout);
1917
+ });
1918
+ app.get("/api/feature/:slug/:doc", { schema: { params: DocParam } }, async (req, reply) => {
1919
+ const { slug, doc } = req.params;
1920
+ const candidates = [
1921
+ sp2__default.join(MUMEI_DIR, "specs", slug, `${doc}.md`),
1922
+ sp2__default.join(MUMEI_DIR, "plans", slug, `${doc}.md`)
1923
+ ];
1924
+ for (const p of candidates) {
1925
+ try {
1926
+ const body = await readFile(p, "utf8");
1927
+ reply.type("text/markdown");
1928
+ return body;
1929
+ } catch {
1930
+ }
1931
+ }
1932
+ reply.code(404);
1933
+ return { error: "not found" };
1934
+ });
1935
+ app.setErrorHandler((err, req, reply) => {
1936
+ req.log.error({ err }, "request handler threw");
1937
+ const fastifyErr = err;
1938
+ if (fastifyErr.validation) {
1939
+ reply.code(400).send({ error: "validation_failed", details: fastifyErr.validation });
1940
+ return;
1941
+ }
1942
+ reply.code(fastifyErr.statusCode ?? 500).send({
1943
+ error: fastifyErr.code ?? "internal_error",
1944
+ message: fastifyErr.message
1945
+ });
1946
+ });
1947
+ var clients = /* @__PURE__ */ new Set();
1948
+ var nextClientId = 1;
1949
+ app.get("/events", (req, reply) => {
1950
+ reply.raw.setHeader("Content-Type", "text/event-stream");
1951
+ reply.raw.setHeader("Cache-Control", "no-cache");
1952
+ reply.raw.setHeader("Connection", "keep-alive");
1953
+ reply.raw.flushHeaders?.();
1954
+ const client = { id: nextClientId++, reply };
1955
+ clients.add(client);
1956
+ app.log.info({ clientId: client.id, total: clients.size }, "sse client connected");
1957
+ reply.raw.write(
1958
+ `data: ${JSON.stringify({ kind: "heartbeat", ts: (/* @__PURE__ */ new Date()).toISOString() })}
1959
+
1960
+ `
1961
+ );
1962
+ req.raw.on("close", () => {
1963
+ clients.delete(client);
1964
+ app.log.info({ clientId: client.id, total: clients.size }, "sse client disconnected");
1965
+ });
1966
+ });
1967
+ function broadcast(event) {
1968
+ const payload = `data: ${JSON.stringify(event)}
1969
+
1970
+ `;
1971
+ for (const c of clients) {
1972
+ try {
1973
+ c.reply.raw.write(payload);
1974
+ } catch (err) {
1975
+ app.log.warn({ err, clientId: c.id }, "sse broadcast failed; dropping client");
1976
+ clients.delete(c);
1977
+ }
1978
+ }
1979
+ }
1980
+ setInterval(() => {
1981
+ broadcast({ kind: "heartbeat", ts: (/* @__PURE__ */ new Date()).toISOString() });
1982
+ }, 25e3);
1983
+ var watcher = watch(MUMEI_DIR, {
1984
+ ignored: (target) => target.includes("/.hook-stats.jsonl.rotate.lock"),
1985
+ persistent: true,
1986
+ ignoreInitial: true,
1987
+ awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 }
1988
+ });
1989
+ watcher.on("all", (event, target) => {
1990
+ const rel = sp2__default.relative(MUMEI_DIR, target);
1991
+ const segments = rel.split(sp2__default.sep);
1992
+ const subroot = segments[0];
1993
+ const featureKey = segments[1];
1994
+ if (!featureKey) return;
1995
+ if (subroot === "specs" || subroot === "plans") {
1996
+ if (segments[2] === "reviews" && /\.json$/.test(target)) {
1997
+ broadcast({
1998
+ kind: "review.added",
1999
+ feature: featureKey,
2000
+ ts: (/* @__PURE__ */ new Date()).toISOString(),
2001
+ verdict: "NEEDS_IMPROVEMENT"
2002
+ // placeholder; client refetches anyway
2003
+ });
2004
+ return;
2005
+ }
2006
+ if (event === "add" || event === "addDir") {
2007
+ broadcast({ kind: "feature.created", feature: featureKey, ts: (/* @__PURE__ */ new Date()).toISOString() });
2008
+ return;
2009
+ }
2010
+ if (event === "unlinkDir") {
2011
+ broadcast({ kind: "feature.archived", feature: featureKey, ts: (/* @__PURE__ */ new Date()).toISOString() });
2012
+ return;
2013
+ }
2014
+ broadcast({ kind: "feature.update", feature: featureKey, ts: (/* @__PURE__ */ new Date()).toISOString() });
2015
+ }
2016
+ });
2017
+ app.listen({ port: PORT, host: "127.0.0.1" }, (err, addr) => {
2018
+ if (err) {
2019
+ app.log.error(err);
2020
+ process.exit(1);
2021
+ }
2022
+ app.log.info({ addr, projectRoot: PROJECT_ROOT, mumei: MUMEI_DIR }, "mumei-dashboard server up");
2023
+ });
2024
+ var shutdown = async (signal) => {
2025
+ app.log.info({ signal }, "shutting down");
2026
+ await watcher.close();
2027
+ await app.close();
2028
+ process.exit(0);
2029
+ };
2030
+ process.on("SIGINT", shutdown);
2031
+ process.on("SIGTERM", shutdown);
2032
+ /*! Bundled license information:
2033
+
2034
+ chokidar/index.js:
2035
+ (*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) *)
2036
+ */
2037
+
2038
+ export { DocParam, FeatureQuery, SlugParam };
2039
+ //# sourceMappingURL=index.js.map
2040
+ //# sourceMappingURL=index.js.map