fluxion-ts 0.3.4 → 0.5.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/dist/index.cjs CHANGED
@@ -6,6 +6,32 @@ var cluster = require('node:cluster');
6
6
  var os = require('node:os');
7
7
  var http = require('node:http');
8
8
  var https = require('node:https');
9
+ var fs$1 = require('fs');
10
+ var promises$1 = require('fs/promises');
11
+ var events = require('events');
12
+ var sysPath = require('path');
13
+ var promises = require('node:fs/promises');
14
+ var node_stream = require('node:stream');
15
+ var os$1 = require('os');
16
+
17
+ function _interopNamespaceDefault(e) {
18
+ var n = Object.create(null);
19
+ if (e) {
20
+ Object.keys(e).forEach(function (k) {
21
+ if (k !== 'default') {
22
+ var d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: function () { return e[k]; }
26
+ });
27
+ }
28
+ });
29
+ }
30
+ n.default = e;
31
+ return Object.freeze(n);
32
+ }
33
+
34
+ var sysPath__namespace = /*#__PURE__*/_interopNamespaceDefault(sysPath);
9
35
 
10
36
  function dtm(dt = new Date()) {
11
37
  const y = dt.getFullYear();
@@ -167,6 +193,35 @@ function createLogger(cx) {
167
193
  };
168
194
  return logger;
169
195
  }
196
+ /**
197
+ * Create a worker logger that prefixes all log messages with the worker PID.
198
+ */
199
+ function createWorkerLogger(baseLogger, pid) {
200
+ const pidPrefix = `[${pid}]`;
201
+ return {
202
+ write(level, event, fields) {
203
+ baseLogger.write(level, `${pidPrefix} ${event}`, fields);
204
+ },
205
+ info(event, fields) {
206
+ baseLogger.info(`${pidPrefix} ${event}`, fields);
207
+ },
208
+ warn(event, fields) {
209
+ baseLogger.warn(`${pidPrefix} ${event}`, fields);
210
+ },
211
+ error(event, fields) {
212
+ baseLogger.error(`${pidPrefix} ${event}`, fields);
213
+ },
214
+ succ(event, fields) {
215
+ baseLogger.succ(`${pidPrefix} ${event}`, fields);
216
+ },
217
+ debug(event, fields) {
218
+ baseLogger.debug(`${pidPrefix} ${event}`, fields);
219
+ },
220
+ verbose(event, fields) {
221
+ baseLogger.verbose(`${pidPrefix} ${event}`, fields);
222
+ },
223
+ };
224
+ }
170
225
  /**
171
226
  * ! Error.isError needs Node.js 24
172
227
  */
@@ -287,7 +342,7 @@ function normalizeHttpsOptions(https, moduleDir) {
287
342
  */
288
343
  function normalizeOptions(options) {
289
344
  expect.isObject(options, 'FluxionOptions must be an object');
290
- let { dir, host, port, metaPort, injections = [], moduleDir = process.cwd(), workerOptions = {}, maxRequestBytes = 8_000_000, reloadDelay = 300, include = ['**/*'], apiInclude = ['**/*.ts'], exclude = [
345
+ let { dir, host, port, metaPort, injections = [], moduleDir = process.cwd(), workerOptions = {}, maxRequestBytes = 8_000_000, reloadDelay = 500, include = ['**/*'], apiInclude = ['**/*.ts'], exclude = [
291
346
  '**/node_modules/**',
292
347
  '**/.git/**',
293
348
  '**/dist/**',
@@ -957,7 +1012,7 @@ const inject = async (cx) => {
957
1012
  const instance = await PromiseTry(factory);
958
1013
  o[injection.name] = instance;
959
1014
  }
960
- cx.logger.info(`[worker ${process.pid}] injections loaded`, Object.keys(o));
1015
+ cx.logger.info('injections loaded', Object.keys(o));
961
1016
  };
962
1017
  const startStatsReporter = () => {
963
1018
  let previousCpuUsage = process.cpuUsage();
@@ -1022,6 +1077,1663 @@ function initWorker(cx) {
1022
1077
  });
1023
1078
  }
1024
1079
 
1080
+ const EntryTypes = {
1081
+ FILE_TYPE: 'files',
1082
+ DIR_TYPE: 'directories',
1083
+ FILE_DIR_TYPE: 'files_directories',
1084
+ EVERYTHING_TYPE: 'all',
1085
+ };
1086
+ const defaultOptions = {
1087
+ root: '.',
1088
+ fileFilter: (_entryInfo) => true,
1089
+ directoryFilter: (_entryInfo) => true,
1090
+ type: EntryTypes.FILE_TYPE,
1091
+ lstat: false,
1092
+ depth: 2147483648,
1093
+ alwaysStat: false,
1094
+ highWaterMark: 4096,
1095
+ };
1096
+ Object.freeze(defaultOptions);
1097
+ const RECURSIVE_ERROR_CODE = 'READDIRP_RECURSIVE_ERROR';
1098
+ const NORMAL_FLOW_ERRORS = new Set(['ENOENT', 'EPERM', 'EACCES', 'ELOOP', RECURSIVE_ERROR_CODE]);
1099
+ const ALL_TYPES = [
1100
+ EntryTypes.DIR_TYPE,
1101
+ EntryTypes.EVERYTHING_TYPE,
1102
+ EntryTypes.FILE_DIR_TYPE,
1103
+ EntryTypes.FILE_TYPE,
1104
+ ];
1105
+ const DIR_TYPES = new Set([
1106
+ EntryTypes.DIR_TYPE,
1107
+ EntryTypes.EVERYTHING_TYPE,
1108
+ EntryTypes.FILE_DIR_TYPE,
1109
+ ]);
1110
+ const FILE_TYPES = new Set([
1111
+ EntryTypes.EVERYTHING_TYPE,
1112
+ EntryTypes.FILE_DIR_TYPE,
1113
+ EntryTypes.FILE_TYPE,
1114
+ ]);
1115
+ const isNormalFlowError = (error) => NORMAL_FLOW_ERRORS.has(error.code);
1116
+ const wantBigintFsStats = process.platform === 'win32';
1117
+ const emptyFn = (_entryInfo) => true;
1118
+ const normalizeFilter = (filter) => {
1119
+ if (filter === undefined)
1120
+ return emptyFn;
1121
+ if (typeof filter === 'function')
1122
+ return filter;
1123
+ if (typeof filter === 'string') {
1124
+ const fl = filter.trim();
1125
+ return (entry) => entry.basename === fl;
1126
+ }
1127
+ if (Array.isArray(filter)) {
1128
+ const trItems = filter.map((item) => item.trim());
1129
+ return (entry) => trItems.some((f) => entry.basename === f);
1130
+ }
1131
+ return emptyFn;
1132
+ };
1133
+ /** Readable readdir stream, emitting new files as they're being listed. */
1134
+ class ReaddirpStream extends node_stream.Readable {
1135
+ constructor(options = {}) {
1136
+ super({
1137
+ objectMode: true,
1138
+ autoDestroy: true,
1139
+ highWaterMark: options.highWaterMark,
1140
+ });
1141
+ const opts = { ...defaultOptions, ...options };
1142
+ const { root, type } = opts;
1143
+ this._fileFilter = normalizeFilter(opts.fileFilter);
1144
+ this._directoryFilter = normalizeFilter(opts.directoryFilter);
1145
+ const statMethod = opts.lstat ? promises.lstat : promises.stat;
1146
+ // Use bigint stats if it's windows and stat() supports options (node 10+).
1147
+ if (wantBigintFsStats) {
1148
+ this._stat = (path) => statMethod(path, { bigint: true });
1149
+ }
1150
+ else {
1151
+ this._stat = statMethod;
1152
+ }
1153
+ this._maxDepth = opts.depth ?? defaultOptions.depth;
1154
+ this._wantsDir = type ? DIR_TYPES.has(type) : false;
1155
+ this._wantsFile = type ? FILE_TYPES.has(type) : false;
1156
+ this._wantsEverything = type === EntryTypes.EVERYTHING_TYPE;
1157
+ this._root = path$1.resolve(root);
1158
+ this._isDirent = !opts.alwaysStat;
1159
+ this._statsProp = this._isDirent ? 'dirent' : 'stats';
1160
+ this._rdOptions = { encoding: 'utf8', withFileTypes: this._isDirent };
1161
+ // Launch stream with one parent, the root dir.
1162
+ this.parents = [this._exploreDir(root, 1)];
1163
+ this.reading = false;
1164
+ this.parent = undefined;
1165
+ }
1166
+ async _read(batch) {
1167
+ if (this.reading)
1168
+ return;
1169
+ this.reading = true;
1170
+ try {
1171
+ while (!this.destroyed && batch > 0) {
1172
+ const par = this.parent;
1173
+ const fil = par && par.files;
1174
+ if (fil && fil.length > 0) {
1175
+ const { path, depth } = par;
1176
+ const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path));
1177
+ const awaited = await Promise.all(slice);
1178
+ for (const entry of awaited) {
1179
+ if (!entry)
1180
+ continue;
1181
+ if (this.destroyed)
1182
+ return;
1183
+ const entryType = await this._getEntryType(entry);
1184
+ if (entryType === 'directory' && this._directoryFilter(entry)) {
1185
+ if (depth <= this._maxDepth) {
1186
+ this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
1187
+ }
1188
+ if (this._wantsDir) {
1189
+ this.push(entry);
1190
+ batch--;
1191
+ }
1192
+ }
1193
+ else if ((entryType === 'file' || this._includeAsFile(entry)) &&
1194
+ this._fileFilter(entry)) {
1195
+ if (this._wantsFile) {
1196
+ this.push(entry);
1197
+ batch--;
1198
+ }
1199
+ }
1200
+ }
1201
+ }
1202
+ else {
1203
+ const parent = this.parents.pop();
1204
+ if (!parent) {
1205
+ this.push(null);
1206
+ break;
1207
+ }
1208
+ this.parent = await parent;
1209
+ if (this.destroyed)
1210
+ return;
1211
+ }
1212
+ }
1213
+ }
1214
+ catch (error) {
1215
+ this.destroy(error);
1216
+ }
1217
+ finally {
1218
+ this.reading = false;
1219
+ }
1220
+ }
1221
+ async _exploreDir(path, depth) {
1222
+ let files;
1223
+ try {
1224
+ files = await promises.readdir(path, this._rdOptions);
1225
+ }
1226
+ catch (error) {
1227
+ this._onError(error);
1228
+ }
1229
+ return { files, depth, path };
1230
+ }
1231
+ async _formatEntry(dirent, path) {
1232
+ let entry;
1233
+ const basename = this._isDirent ? dirent.name : dirent;
1234
+ try {
1235
+ const fullPath = path$1.resolve(path$1.join(path, basename));
1236
+ entry = { path: path$1.relative(this._root, fullPath), fullPath, basename };
1237
+ entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
1238
+ }
1239
+ catch (err) {
1240
+ this._onError(err);
1241
+ return;
1242
+ }
1243
+ return entry;
1244
+ }
1245
+ _onError(err) {
1246
+ if (isNormalFlowError(err) && !this.destroyed) {
1247
+ this.emit('warn', err);
1248
+ }
1249
+ else {
1250
+ this.destroy(err);
1251
+ }
1252
+ }
1253
+ async _getEntryType(entry) {
1254
+ // entry may be undefined, because a warning or an error were emitted
1255
+ // and the statsProp is undefined
1256
+ if (!entry && this._statsProp in entry) {
1257
+ return '';
1258
+ }
1259
+ const stats = entry[this._statsProp];
1260
+ if (stats.isFile())
1261
+ return 'file';
1262
+ if (stats.isDirectory())
1263
+ return 'directory';
1264
+ if (stats && stats.isSymbolicLink()) {
1265
+ const full = entry.fullPath;
1266
+ try {
1267
+ const entryRealPath = await promises.realpath(full);
1268
+ const entryRealPathStats = await promises.lstat(entryRealPath);
1269
+ if (entryRealPathStats.isFile()) {
1270
+ return 'file';
1271
+ }
1272
+ if (entryRealPathStats.isDirectory()) {
1273
+ const len = entryRealPath.length;
1274
+ if (full.startsWith(entryRealPath) && full.substr(len, 1) === path$1.sep) {
1275
+ const recursiveError = new Error(`Circular symlink detected: "${full}" points to "${entryRealPath}"`);
1276
+ // @ts-ignore
1277
+ recursiveError.code = RECURSIVE_ERROR_CODE;
1278
+ return this._onError(recursiveError);
1279
+ }
1280
+ return 'directory';
1281
+ }
1282
+ }
1283
+ catch (error) {
1284
+ this._onError(error);
1285
+ return '';
1286
+ }
1287
+ }
1288
+ }
1289
+ _includeAsFile(entry) {
1290
+ const stats = entry && entry[this._statsProp];
1291
+ return stats && this._wantsEverything && !stats.isDirectory();
1292
+ }
1293
+ }
1294
+ /**
1295
+ * Streaming version: Reads all files and directories in given root recursively.
1296
+ * Consumes ~constant small amount of RAM.
1297
+ * @param root Root directory
1298
+ * @param options Options to specify root (start directory), filters and recursion depth
1299
+ */
1300
+ function readdirp(root, options = {}) {
1301
+ // @ts-ignore
1302
+ let type = options.entryType || options.type;
1303
+ if (type === 'both')
1304
+ type = EntryTypes.FILE_DIR_TYPE; // backwards-compatibility
1305
+ if (type)
1306
+ options.type = type;
1307
+ if (!root) {
1308
+ throw new Error('readdirp: root argument is required. Usage: readdirp(root, options)');
1309
+ }
1310
+ else if (typeof root !== 'string') {
1311
+ throw new TypeError('readdirp: root argument must be a string. Usage: readdirp(root, options)');
1312
+ }
1313
+ else if (type && !ALL_TYPES.includes(type)) {
1314
+ throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(', ')}`);
1315
+ }
1316
+ options.root = root;
1317
+ return new ReaddirpStream(options);
1318
+ }
1319
+
1320
+ const STR_DATA = 'data';
1321
+ const STR_END = 'end';
1322
+ const STR_CLOSE = 'close';
1323
+ const EMPTY_FN = () => { };
1324
+ const pl = process.platform;
1325
+ const isWindows = pl === 'win32';
1326
+ const isMacos = pl === 'darwin';
1327
+ const isLinux = pl === 'linux';
1328
+ const isFreeBSD = pl === 'freebsd';
1329
+ const isIBMi = os$1.type() === 'OS400';
1330
+ const EVENTS = {
1331
+ ALL: 'all',
1332
+ READY: 'ready',
1333
+ ADD: 'add',
1334
+ CHANGE: 'change',
1335
+ ADD_DIR: 'addDir',
1336
+ UNLINK: 'unlink',
1337
+ UNLINK_DIR: 'unlinkDir',
1338
+ RAW: 'raw',
1339
+ ERROR: 'error',
1340
+ };
1341
+ const EV = EVENTS;
1342
+ const THROTTLE_MODE_WATCH = 'watch';
1343
+ const statMethods = { lstat: promises$1.lstat, stat: promises$1.stat };
1344
+ const KEY_LISTENERS = 'listeners';
1345
+ const KEY_ERR = 'errHandlers';
1346
+ const KEY_RAW = 'rawEmitters';
1347
+ const HANDLER_KEYS = [KEY_LISTENERS, KEY_ERR, KEY_RAW];
1348
+ // prettier-ignore
1349
+ const binaryExtensions = new Set([
1350
+ '3dm', '3ds', '3g2', '3gp', '7z', 'a', 'aac', 'adp', 'afdesign', 'afphoto', 'afpub', 'ai',
1351
+ 'aif', 'aiff', 'alz', 'ape', 'apk', 'appimage', 'ar', 'arj', 'asf', 'au', 'avi',
1352
+ 'bak', 'baml', 'bh', 'bin', 'bk', 'bmp', 'btif', 'bz2', 'bzip2',
1353
+ 'cab', 'caf', 'cgm', 'class', 'cmx', 'cpio', 'cr2', 'cur', 'dat', 'dcm', 'deb', 'dex', 'djvu',
1354
+ 'dll', 'dmg', 'dng', 'doc', 'docm', 'docx', 'dot', 'dotm', 'dra', 'DS_Store', 'dsk', 'dts',
1355
+ 'dtshd', 'dvb', 'dwg', 'dxf',
1356
+ 'ecelp4800', 'ecelp7470', 'ecelp9600', 'egg', 'eol', 'eot', 'epub', 'exe',
1357
+ 'f4v', 'fbs', 'fh', 'fla', 'flac', 'flatpak', 'fli', 'flv', 'fpx', 'fst', 'fvt',
1358
+ 'g3', 'gh', 'gif', 'graffle', 'gz', 'gzip',
1359
+ 'h261', 'h263', 'h264', 'icns', 'ico', 'ief', 'img', 'ipa', 'iso',
1360
+ 'jar', 'jpeg', 'jpg', 'jpgv', 'jpm', 'jxr', 'key', 'ktx',
1361
+ 'lha', 'lib', 'lvp', 'lz', 'lzh', 'lzma', 'lzo',
1362
+ 'm3u', 'm4a', 'm4v', 'mar', 'mdi', 'mht', 'mid', 'midi', 'mj2', 'mka', 'mkv', 'mmr', 'mng',
1363
+ 'mobi', 'mov', 'movie', 'mp3',
1364
+ 'mp4', 'mp4a', 'mpeg', 'mpg', 'mpga', 'mxu',
1365
+ 'nef', 'npx', 'numbers', 'nupkg',
1366
+ 'o', 'odp', 'ods', 'odt', 'oga', 'ogg', 'ogv', 'otf', 'ott',
1367
+ 'pages', 'pbm', 'pcx', 'pdb', 'pdf', 'pea', 'pgm', 'pic', 'png', 'pnm', 'pot', 'potm',
1368
+ 'potx', 'ppa', 'ppam',
1369
+ 'ppm', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx', 'psd', 'pya', 'pyc', 'pyo', 'pyv',
1370
+ 'qt',
1371
+ 'rar', 'ras', 'raw', 'resources', 'rgb', 'rip', 'rlc', 'rmf', 'rmvb', 'rpm', 'rtf', 'rz',
1372
+ 's3m', 's7z', 'scpt', 'sgi', 'shar', 'snap', 'sil', 'sketch', 'slk', 'smv', 'snk', 'so',
1373
+ 'stl', 'suo', 'sub', 'swf',
1374
+ 'tar', 'tbz', 'tbz2', 'tga', 'tgz', 'thmx', 'tif', 'tiff', 'tlz', 'ttc', 'ttf', 'txz',
1375
+ 'udf', 'uvh', 'uvi', 'uvm', 'uvp', 'uvs', 'uvu',
1376
+ 'viv', 'vob',
1377
+ 'war', 'wav', 'wax', 'wbmp', 'wdp', 'weba', 'webm', 'webp', 'whl', 'wim', 'wm', 'wma',
1378
+ 'wmv', 'wmx', 'woff', 'woff2', 'wrm', 'wvx',
1379
+ 'xbm', 'xif', 'xla', 'xlam', 'xls', 'xlsb', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx', 'xm',
1380
+ 'xmind', 'xpi', 'xpm', 'xwd', 'xz',
1381
+ 'z', 'zip', 'zipx',
1382
+ ]);
1383
+ const isBinaryPath = (filePath) => binaryExtensions.has(sysPath__namespace.extname(filePath).slice(1).toLowerCase());
1384
+ // TODO: emit errors properly. Example: EMFILE on Macos.
1385
+ const foreach = (val, fn) => {
1386
+ if (val instanceof Set) {
1387
+ val.forEach(fn);
1388
+ }
1389
+ else {
1390
+ fn(val);
1391
+ }
1392
+ };
1393
+ const addAndConvert = (main, prop, item) => {
1394
+ let container = main[prop];
1395
+ if (!(container instanceof Set)) {
1396
+ main[prop] = container = new Set([container]);
1397
+ }
1398
+ container.add(item);
1399
+ };
1400
+ const clearItem = (cont) => (key) => {
1401
+ const set = cont[key];
1402
+ if (set instanceof Set) {
1403
+ set.clear();
1404
+ }
1405
+ else {
1406
+ delete cont[key];
1407
+ }
1408
+ };
1409
+ const delFromSet = (main, prop, item) => {
1410
+ const container = main[prop];
1411
+ if (container instanceof Set) {
1412
+ container.delete(item);
1413
+ }
1414
+ else if (container === item) {
1415
+ delete main[prop];
1416
+ }
1417
+ };
1418
+ const isEmptySet = (val) => (val instanceof Set ? val.size === 0 : !val);
1419
+ const FsWatchInstances = new Map();
1420
+ /**
1421
+ * Instantiates the fs_watch interface
1422
+ * @param path to be watched
1423
+ * @param options to be passed to fs_watch
1424
+ * @param listener main event handler
1425
+ * @param errHandler emits info about errors
1426
+ * @param emitRaw emits raw event data
1427
+ * @returns {NativeFsWatcher}
1428
+ */
1429
+ function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
1430
+ const handleEvent = (rawEvent, evPath) => {
1431
+ listener(path);
1432
+ emitRaw(rawEvent, evPath, { watchedPath: path });
1433
+ // emit based on events occurring for files from a directory's watcher in
1434
+ // case the file's watcher misses it (and rely on throttling to de-dupe)
1435
+ if (evPath && path !== evPath) {
1436
+ fsWatchBroadcast(sysPath__namespace.resolve(path, evPath), KEY_LISTENERS, sysPath__namespace.join(path, evPath));
1437
+ }
1438
+ };
1439
+ try {
1440
+ return fs$1.watch(path, {
1441
+ persistent: options.persistent,
1442
+ }, handleEvent);
1443
+ }
1444
+ catch (error) {
1445
+ errHandler(error);
1446
+ return undefined;
1447
+ }
1448
+ }
1449
+ /**
1450
+ * Helper for passing fs_watch event data to a collection of listeners
1451
+ * @param fullPath absolute path bound to fs_watch instance
1452
+ */
1453
+ const fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
1454
+ const cont = FsWatchInstances.get(fullPath);
1455
+ if (!cont)
1456
+ return;
1457
+ foreach(cont[listenerType], (listener) => {
1458
+ listener(val1, val2, val3);
1459
+ });
1460
+ };
1461
+ /**
1462
+ * Instantiates the fs_watch interface or binds listeners
1463
+ * to an existing one covering the same file system entry
1464
+ * @param path
1465
+ * @param fullPath absolute path
1466
+ * @param options to be passed to fs_watch
1467
+ * @param handlers container for event listener functions
1468
+ */
1469
+ const setFsWatchListener = (path, fullPath, options, handlers) => {
1470
+ const { listener, errHandler, rawEmitter } = handlers;
1471
+ let cont = FsWatchInstances.get(fullPath);
1472
+ let watcher;
1473
+ if (!options.persistent) {
1474
+ watcher = createFsWatchInstance(path, options, listener, errHandler, rawEmitter);
1475
+ if (!watcher)
1476
+ return;
1477
+ return watcher.close.bind(watcher);
1478
+ }
1479
+ if (cont) {
1480
+ addAndConvert(cont, KEY_LISTENERS, listener);
1481
+ addAndConvert(cont, KEY_ERR, errHandler);
1482
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1483
+ }
1484
+ else {
1485
+ watcher = createFsWatchInstance(path, options, fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS), errHandler, // no need to use broadcast here
1486
+ fsWatchBroadcast.bind(null, fullPath, KEY_RAW));
1487
+ if (!watcher)
1488
+ return;
1489
+ watcher.on(EV.ERROR, async (error) => {
1490
+ const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
1491
+ if (cont)
1492
+ cont.watcherUnusable = true; // documented since Node 10.4.1
1493
+ // Workaround for https://github.com/joyent/node/issues/4337
1494
+ if (isWindows && error.code === 'EPERM') {
1495
+ try {
1496
+ const fd = await promises$1.open(path, 'r');
1497
+ await fd.close();
1498
+ broadcastErr(error);
1499
+ }
1500
+ catch (err) {
1501
+ // do nothing
1502
+ }
1503
+ }
1504
+ else {
1505
+ broadcastErr(error);
1506
+ }
1507
+ });
1508
+ cont = {
1509
+ listeners: listener,
1510
+ errHandlers: errHandler,
1511
+ rawEmitters: rawEmitter,
1512
+ watcher,
1513
+ };
1514
+ FsWatchInstances.set(fullPath, cont);
1515
+ }
1516
+ // const index = cont.listeners.indexOf(listener);
1517
+ // removes this instance's listeners and closes the underlying fs_watch
1518
+ // instance if there are no more listeners left
1519
+ return () => {
1520
+ delFromSet(cont, KEY_LISTENERS, listener);
1521
+ delFromSet(cont, KEY_ERR, errHandler);
1522
+ delFromSet(cont, KEY_RAW, rawEmitter);
1523
+ if (isEmptySet(cont.listeners)) {
1524
+ // Check to protect against issue gh-730.
1525
+ // if (cont.watcherUnusable) {
1526
+ cont.watcher.close();
1527
+ // }
1528
+ FsWatchInstances.delete(fullPath);
1529
+ HANDLER_KEYS.forEach(clearItem(cont));
1530
+ // @ts-ignore
1531
+ cont.watcher = undefined;
1532
+ Object.freeze(cont);
1533
+ }
1534
+ };
1535
+ };
1536
+ // fs_watchFile helpers
1537
+ // object to hold per-process fs_watchFile instances
1538
+ // (may be shared across chokidar FSWatcher instances)
1539
+ const FsWatchFileInstances = new Map();
1540
+ /**
1541
+ * Instantiates the fs_watchFile interface or binds listeners
1542
+ * to an existing one covering the same file system entry
1543
+ * @param path to be watched
1544
+ * @param fullPath absolute path
1545
+ * @param options options to be passed to fs_watchFile
1546
+ * @param handlers container for event listener functions
1547
+ * @returns closer
1548
+ */
1549
+ const setFsWatchFileListener = (path, fullPath, options, handlers) => {
1550
+ const { listener, rawEmitter } = handlers;
1551
+ let cont = FsWatchFileInstances.get(fullPath);
1552
+ // let listeners = new Set();
1553
+ // let rawEmitters = new Set();
1554
+ const copts = cont && cont.options;
1555
+ if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
1556
+ // "Upgrade" the watcher to persistence or a quicker interval.
1557
+ // This creates some unlikely edge case issues if the user mixes
1558
+ // settings in a very weird way, but solving for those cases
1559
+ // doesn't seem worthwhile for the added complexity.
1560
+ // listeners = cont.listeners;
1561
+ // rawEmitters = cont.rawEmitters;
1562
+ fs$1.unwatchFile(fullPath);
1563
+ cont = undefined;
1564
+ }
1565
+ if (cont) {
1566
+ addAndConvert(cont, KEY_LISTENERS, listener);
1567
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1568
+ }
1569
+ else {
1570
+ // TODO
1571
+ // listeners.add(listener);
1572
+ // rawEmitters.add(rawEmitter);
1573
+ cont = {
1574
+ listeners: listener,
1575
+ rawEmitters: rawEmitter,
1576
+ options,
1577
+ watcher: fs$1.watchFile(fullPath, options, (curr, prev) => {
1578
+ foreach(cont.rawEmitters, (rawEmitter) => {
1579
+ rawEmitter(EV.CHANGE, fullPath, { curr, prev });
1580
+ });
1581
+ const currmtime = curr.mtimeMs;
1582
+ if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
1583
+ foreach(cont.listeners, (listener) => listener(path, curr));
1584
+ }
1585
+ }),
1586
+ };
1587
+ FsWatchFileInstances.set(fullPath, cont);
1588
+ }
1589
+ // const index = cont.listeners.indexOf(listener);
1590
+ // Removes this instance's listeners and closes the underlying fs_watchFile
1591
+ // instance if there are no more listeners left.
1592
+ return () => {
1593
+ delFromSet(cont, KEY_LISTENERS, listener);
1594
+ delFromSet(cont, KEY_RAW, rawEmitter);
1595
+ if (isEmptySet(cont.listeners)) {
1596
+ FsWatchFileInstances.delete(fullPath);
1597
+ fs$1.unwatchFile(fullPath);
1598
+ cont.options = cont.watcher = undefined;
1599
+ Object.freeze(cont);
1600
+ }
1601
+ };
1602
+ };
1603
+ /**
1604
+ * @mixin
1605
+ */
1606
+ class NodeFsHandler {
1607
+ constructor(fsW) {
1608
+ this.fsw = fsW;
1609
+ this._boundHandleError = (error) => fsW._handleError(error);
1610
+ }
1611
+ /**
1612
+ * Watch file for changes with fs_watchFile or fs_watch.
1613
+ * @param path to file or dir
1614
+ * @param listener on fs change
1615
+ * @returns closer for the watcher instance
1616
+ */
1617
+ _watchWithNodeFs(path, listener) {
1618
+ const opts = this.fsw.options;
1619
+ const directory = sysPath__namespace.dirname(path);
1620
+ const basename = sysPath__namespace.basename(path);
1621
+ const parent = this.fsw._getWatchedDir(directory);
1622
+ parent.add(basename);
1623
+ const absolutePath = sysPath__namespace.resolve(path);
1624
+ const options = {
1625
+ persistent: opts.persistent,
1626
+ };
1627
+ if (!listener)
1628
+ listener = EMPTY_FN;
1629
+ let closer;
1630
+ if (opts.usePolling) {
1631
+ const enableBin = opts.interval !== opts.binaryInterval;
1632
+ options.interval = enableBin && isBinaryPath(basename) ? opts.binaryInterval : opts.interval;
1633
+ closer = setFsWatchFileListener(path, absolutePath, options, {
1634
+ listener,
1635
+ rawEmitter: this.fsw._emitRaw,
1636
+ });
1637
+ }
1638
+ else {
1639
+ closer = setFsWatchListener(path, absolutePath, options, {
1640
+ listener,
1641
+ errHandler: this._boundHandleError,
1642
+ rawEmitter: this.fsw._emitRaw,
1643
+ });
1644
+ }
1645
+ return closer;
1646
+ }
1647
+ /**
1648
+ * Watch a file and emit add event if warranted.
1649
+ * @returns closer for the watcher instance
1650
+ */
1651
+ _handleFile(file, stats, initialAdd) {
1652
+ if (this.fsw.closed) {
1653
+ return;
1654
+ }
1655
+ const dirname = sysPath__namespace.dirname(file);
1656
+ const basename = sysPath__namespace.basename(file);
1657
+ const parent = this.fsw._getWatchedDir(dirname);
1658
+ // stats is always present
1659
+ let prevStats = stats;
1660
+ // if the file is already being watched, do nothing
1661
+ if (parent.has(basename))
1662
+ return;
1663
+ const listener = async (path, newStats) => {
1664
+ if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5))
1665
+ return;
1666
+ if (!newStats || newStats.mtimeMs === 0) {
1667
+ try {
1668
+ const newStats = await promises$1.stat(file);
1669
+ if (this.fsw.closed)
1670
+ return;
1671
+ // Check that change event was not fired because of changed only accessTime.
1672
+ const at = newStats.atimeMs;
1673
+ const mt = newStats.mtimeMs;
1674
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1675
+ this.fsw._emit(EV.CHANGE, file, newStats);
1676
+ }
1677
+ if ((isMacos || isLinux || isFreeBSD) && prevStats.ino !== newStats.ino) {
1678
+ this.fsw._closeFile(path);
1679
+ prevStats = newStats;
1680
+ const closer = this._watchWithNodeFs(file, listener);
1681
+ if (closer)
1682
+ this.fsw._addPathCloser(path, closer);
1683
+ }
1684
+ else {
1685
+ prevStats = newStats;
1686
+ }
1687
+ }
1688
+ catch (error) {
1689
+ // Fix issues where mtime is null but file is still present
1690
+ this.fsw._remove(dirname, basename);
1691
+ }
1692
+ // add is about to be emitted if file not already tracked in parent
1693
+ }
1694
+ else if (parent.has(basename)) {
1695
+ // Check that change event was not fired because of changed only accessTime.
1696
+ const at = newStats.atimeMs;
1697
+ const mt = newStats.mtimeMs;
1698
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1699
+ this.fsw._emit(EV.CHANGE, file, newStats);
1700
+ }
1701
+ prevStats = newStats;
1702
+ }
1703
+ };
1704
+ // kick off the watcher
1705
+ const closer = this._watchWithNodeFs(file, listener);
1706
+ // emit an add event if we're supposed to
1707
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
1708
+ if (!this.fsw._throttle(EV.ADD, file, 0))
1709
+ return;
1710
+ this.fsw._emit(EV.ADD, file, stats);
1711
+ }
1712
+ return closer;
1713
+ }
1714
+ /**
1715
+ * Handle symlinks encountered while reading a dir.
1716
+ * @param entry returned by readdirp
1717
+ * @param directory path of dir being read
1718
+ * @param path of this item
1719
+ * @param item basename of this item
1720
+ * @returns true if no more processing is needed for this entry.
1721
+ */
1722
+ async _handleSymlink(entry, directory, path, item) {
1723
+ if (this.fsw.closed) {
1724
+ return;
1725
+ }
1726
+ const full = entry.fullPath;
1727
+ const dir = this.fsw._getWatchedDir(directory);
1728
+ if (!this.fsw.options.followSymlinks) {
1729
+ // watch symlink directly (don't follow) and detect changes
1730
+ this.fsw._incrReadyCount();
1731
+ let linkPath;
1732
+ try {
1733
+ linkPath = await promises$1.realpath(path);
1734
+ }
1735
+ catch (e) {
1736
+ this.fsw._emitReady();
1737
+ return true;
1738
+ }
1739
+ if (this.fsw.closed)
1740
+ return;
1741
+ if (dir.has(item)) {
1742
+ if (this.fsw._symlinkPaths.get(full) !== linkPath) {
1743
+ this.fsw._symlinkPaths.set(full, linkPath);
1744
+ this.fsw._emit(EV.CHANGE, path, entry.stats);
1745
+ }
1746
+ }
1747
+ else {
1748
+ dir.add(item);
1749
+ this.fsw._symlinkPaths.set(full, linkPath);
1750
+ this.fsw._emit(EV.ADD, path, entry.stats);
1751
+ }
1752
+ this.fsw._emitReady();
1753
+ return true;
1754
+ }
1755
+ // don't follow the same symlink more than once
1756
+ if (this.fsw._symlinkPaths.has(full)) {
1757
+ return true;
1758
+ }
1759
+ this.fsw._symlinkPaths.set(full, true);
1760
+ }
1761
+ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
1762
+ // Normalize the directory name on Windows
1763
+ directory = sysPath__namespace.join(directory, '');
1764
+ throttler = this.fsw._throttle('readdir', directory, 1000);
1765
+ if (!throttler)
1766
+ return;
1767
+ const previous = this.fsw._getWatchedDir(wh.path);
1768
+ const current = new Set();
1769
+ let stream = this.fsw._readdirp(directory, {
1770
+ fileFilter: (entry) => wh.filterPath(entry),
1771
+ directoryFilter: (entry) => wh.filterDir(entry),
1772
+ });
1773
+ if (!stream)
1774
+ return;
1775
+ stream
1776
+ .on(STR_DATA, async (entry) => {
1777
+ if (this.fsw.closed) {
1778
+ stream = undefined;
1779
+ return;
1780
+ }
1781
+ const item = entry.path;
1782
+ let path = sysPath__namespace.join(directory, item);
1783
+ current.add(item);
1784
+ if (entry.stats.isSymbolicLink() &&
1785
+ (await this._handleSymlink(entry, directory, path, item))) {
1786
+ return;
1787
+ }
1788
+ if (this.fsw.closed) {
1789
+ stream = undefined;
1790
+ return;
1791
+ }
1792
+ // Files that present in current directory snapshot
1793
+ // but absent in previous are added to watch list and
1794
+ // emit `add` event.
1795
+ if (item === target || (!target && !previous.has(item))) {
1796
+ this.fsw._incrReadyCount();
1797
+ // ensure relativeness of path is preserved in case of watcher reuse
1798
+ path = sysPath__namespace.join(dir, sysPath__namespace.relative(dir, path));
1799
+ this._addToNodeFs(path, initialAdd, wh, depth + 1);
1800
+ }
1801
+ })
1802
+ .on(EV.ERROR, this._boundHandleError);
1803
+ return new Promise((resolve, reject) => {
1804
+ if (!stream)
1805
+ return reject();
1806
+ stream.once(STR_END, () => {
1807
+ if (this.fsw.closed) {
1808
+ stream = undefined;
1809
+ return;
1810
+ }
1811
+ const wasThrottled = throttler ? throttler.clear() : false;
1812
+ resolve(undefined);
1813
+ // Files that absent in current directory snapshot
1814
+ // but present in previous emit `remove` event
1815
+ // and are removed from @watched[directory].
1816
+ previous
1817
+ .getChildren()
1818
+ .filter((item) => {
1819
+ return item !== directory && !current.has(item);
1820
+ })
1821
+ .forEach((item) => {
1822
+ this.fsw._remove(directory, item);
1823
+ });
1824
+ stream = undefined;
1825
+ // one more time for any missed in case changes came in extremely quickly
1826
+ if (wasThrottled)
1827
+ this._handleRead(directory, false, wh, target, dir, depth, throttler);
1828
+ });
1829
+ });
1830
+ }
1831
+ /**
1832
+ * Read directory to add / remove files from `@watched` list and re-read it on change.
1833
+ * @param dir fs path
1834
+ * @param stats
1835
+ * @param initialAdd
1836
+ * @param depth relative to user-supplied path
1837
+ * @param target child path targeted for watch
1838
+ * @param wh Common watch helpers for this path
1839
+ * @param realpath
1840
+ * @returns closer for the watcher instance.
1841
+ */
1842
+ async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
1843
+ const parentDir = this.fsw._getWatchedDir(sysPath__namespace.dirname(dir));
1844
+ const tracked = parentDir.has(sysPath__namespace.basename(dir));
1845
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
1846
+ this.fsw._emit(EV.ADD_DIR, dir, stats);
1847
+ }
1848
+ // ensure dir is tracked (harmless if redundant)
1849
+ parentDir.add(sysPath__namespace.basename(dir));
1850
+ this.fsw._getWatchedDir(dir);
1851
+ let throttler;
1852
+ let closer;
1853
+ const oDepth = this.fsw.options.depth;
1854
+ if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
1855
+ if (!target) {
1856
+ await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
1857
+ if (this.fsw.closed)
1858
+ return;
1859
+ }
1860
+ closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
1861
+ // if current directory is removed, do nothing
1862
+ if (stats && stats.mtimeMs === 0)
1863
+ return;
1864
+ this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
1865
+ });
1866
+ }
1867
+ return closer;
1868
+ }
1869
+ /**
1870
+ * Handle added file, directory, or glob pattern.
1871
+ * Delegates call to _handleFile / _handleDir after checks.
1872
+ * @param path to file or ir
1873
+ * @param initialAdd was the file added at watch instantiation?
1874
+ * @param priorWh depth relative to user-supplied path
1875
+ * @param depth Child path actually targeted for watch
1876
+ * @param target Child path actually targeted for watch
1877
+ */
1878
+ async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
1879
+ const ready = this.fsw._emitReady;
1880
+ if (this.fsw._isIgnored(path) || this.fsw.closed) {
1881
+ ready();
1882
+ return false;
1883
+ }
1884
+ const wh = this.fsw._getWatchHelpers(path);
1885
+ if (priorWh) {
1886
+ wh.filterPath = (entry) => priorWh.filterPath(entry);
1887
+ wh.filterDir = (entry) => priorWh.filterDir(entry);
1888
+ }
1889
+ // evaluate what is at the path we're being asked to watch
1890
+ try {
1891
+ const stats = await statMethods[wh.statMethod](wh.watchPath);
1892
+ if (this.fsw.closed)
1893
+ return;
1894
+ if (this.fsw._isIgnored(wh.watchPath, stats)) {
1895
+ ready();
1896
+ return false;
1897
+ }
1898
+ const follow = this.fsw.options.followSymlinks;
1899
+ let closer;
1900
+ if (stats.isDirectory()) {
1901
+ const absPath = sysPath__namespace.resolve(path);
1902
+ const targetPath = follow ? await promises$1.realpath(path) : path;
1903
+ if (this.fsw.closed)
1904
+ return;
1905
+ closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
1906
+ if (this.fsw.closed)
1907
+ return;
1908
+ // preserve this symlink's target path
1909
+ if (absPath !== targetPath && targetPath !== undefined) {
1910
+ this.fsw._symlinkPaths.set(absPath, targetPath);
1911
+ }
1912
+ }
1913
+ else if (stats.isSymbolicLink()) {
1914
+ const targetPath = follow ? await promises$1.realpath(path) : path;
1915
+ if (this.fsw.closed)
1916
+ return;
1917
+ const parent = sysPath__namespace.dirname(wh.watchPath);
1918
+ this.fsw._getWatchedDir(parent).add(wh.watchPath);
1919
+ this.fsw._emit(EV.ADD, wh.watchPath, stats);
1920
+ closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
1921
+ if (this.fsw.closed)
1922
+ return;
1923
+ // preserve this symlink's target path
1924
+ if (targetPath !== undefined) {
1925
+ this.fsw._symlinkPaths.set(sysPath__namespace.resolve(path), targetPath);
1926
+ }
1927
+ }
1928
+ else {
1929
+ closer = this._handleFile(wh.watchPath, stats, initialAdd);
1930
+ }
1931
+ ready();
1932
+ if (closer)
1933
+ this.fsw._addPathCloser(path, closer);
1934
+ return false;
1935
+ }
1936
+ catch (error) {
1937
+ if (this.fsw._handleError(error)) {
1938
+ ready();
1939
+ return path;
1940
+ }
1941
+ }
1942
+ }
1943
+ }
1944
+
1945
+ /*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) */
1946
+ const SLASH = '/';
1947
+ const SLASH_SLASH = '//';
1948
+ const ONE_DOT = '.';
1949
+ const TWO_DOTS = '..';
1950
+ const STRING_TYPE = 'string';
1951
+ const BACK_SLASH_RE = /\\/g;
1952
+ const DOUBLE_SLASH_RE = /\/\//;
1953
+ const DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
1954
+ const REPLACER_RE = /^\.[/\\]/;
1955
+ function arrify(item) {
1956
+ return Array.isArray(item) ? item : [item];
1957
+ }
1958
+ const isMatcherObject = (matcher) => typeof matcher === 'object' && matcher !== null && !(matcher instanceof RegExp);
1959
+ function createPattern(matcher) {
1960
+ if (typeof matcher === 'function')
1961
+ return matcher;
1962
+ if (typeof matcher === 'string')
1963
+ return (string) => matcher === string;
1964
+ if (matcher instanceof RegExp)
1965
+ return (string) => matcher.test(string);
1966
+ if (typeof matcher === 'object' && matcher !== null) {
1967
+ return (string) => {
1968
+ if (matcher.path === string)
1969
+ return true;
1970
+ if (matcher.recursive) {
1971
+ const relative = sysPath__namespace.relative(matcher.path, string);
1972
+ if (!relative) {
1973
+ return false;
1974
+ }
1975
+ return !relative.startsWith('..') && !sysPath__namespace.isAbsolute(relative);
1976
+ }
1977
+ return false;
1978
+ };
1979
+ }
1980
+ return () => false;
1981
+ }
1982
+ function normalizePath(path) {
1983
+ if (typeof path !== 'string')
1984
+ throw new Error('string expected');
1985
+ path = sysPath__namespace.normalize(path);
1986
+ path = path.replace(/\\/g, '/');
1987
+ let prepend = false;
1988
+ if (path.startsWith('//'))
1989
+ prepend = true;
1990
+ const DOUBLE_SLASH_RE = /\/\//;
1991
+ while (path.match(DOUBLE_SLASH_RE))
1992
+ path = path.replace(DOUBLE_SLASH_RE, '/');
1993
+ if (prepend)
1994
+ path = '/' + path;
1995
+ return path;
1996
+ }
1997
+ function matchPatterns(patterns, testString, stats) {
1998
+ const path = normalizePath(testString);
1999
+ for (let index = 0; index < patterns.length; index++) {
2000
+ const pattern = patterns[index];
2001
+ if (pattern(path, stats)) {
2002
+ return true;
2003
+ }
2004
+ }
2005
+ return false;
2006
+ }
2007
+ function anymatch(matchers, testString) {
2008
+ if (matchers == null) {
2009
+ throw new TypeError('anymatch: specify first argument');
2010
+ }
2011
+ // Early cache for matchers.
2012
+ const matchersArray = arrify(matchers);
2013
+ const patterns = matchersArray.map((matcher) => createPattern(matcher));
2014
+ {
2015
+ return (testString, stats) => {
2016
+ return matchPatterns(patterns, testString, stats);
2017
+ };
2018
+ }
2019
+ }
2020
+ const unifyPaths = (paths_) => {
2021
+ const paths = arrify(paths_).flat();
2022
+ if (!paths.every((p) => typeof p === STRING_TYPE)) {
2023
+ throw new TypeError(`Non-string provided as watch path: ${paths}`);
2024
+ }
2025
+ return paths.map(normalizePathToUnix);
2026
+ };
2027
+ // If SLASH_SLASH occurs at the beginning of path, it is not replaced
2028
+ // because "//StoragePC/DrivePool/Movies" is a valid network path
2029
+ const toUnix = (string) => {
2030
+ let str = string.replace(BACK_SLASH_RE, SLASH);
2031
+ let prepend = false;
2032
+ if (str.startsWith(SLASH_SLASH)) {
2033
+ prepend = true;
2034
+ }
2035
+ while (str.match(DOUBLE_SLASH_RE)) {
2036
+ str = str.replace(DOUBLE_SLASH_RE, SLASH);
2037
+ }
2038
+ if (prepend) {
2039
+ str = SLASH + str;
2040
+ }
2041
+ return str;
2042
+ };
2043
+ // Our version of upath.normalize
2044
+ // TODO: this is not equal to path-normalize module - investigate why
2045
+ const normalizePathToUnix = (path) => toUnix(sysPath__namespace.normalize(toUnix(path)));
2046
+ // TODO: refactor
2047
+ const normalizeIgnored = (cwd = '') => (path) => {
2048
+ if (typeof path === 'string') {
2049
+ return normalizePathToUnix(sysPath__namespace.isAbsolute(path) ? path : sysPath__namespace.join(cwd, path));
2050
+ }
2051
+ else {
2052
+ return path;
2053
+ }
2054
+ };
2055
+ const getAbsolutePath = (path, cwd) => {
2056
+ if (sysPath__namespace.isAbsolute(path)) {
2057
+ return path;
2058
+ }
2059
+ return sysPath__namespace.join(cwd, path);
2060
+ };
2061
+ const EMPTY_SET = Object.freeze(new Set());
2062
+ /**
2063
+ * Directory entry.
2064
+ */
2065
+ class DirEntry {
2066
+ constructor(dir, removeWatcher) {
2067
+ this.path = dir;
2068
+ this._removeWatcher = removeWatcher;
2069
+ this.items = new Set();
2070
+ }
2071
+ add(item) {
2072
+ const { items } = this;
2073
+ if (!items)
2074
+ return;
2075
+ if (item !== ONE_DOT && item !== TWO_DOTS)
2076
+ items.add(item);
2077
+ }
2078
+ async remove(item) {
2079
+ const { items } = this;
2080
+ if (!items)
2081
+ return;
2082
+ items.delete(item);
2083
+ if (items.size > 0)
2084
+ return;
2085
+ const dir = this.path;
2086
+ try {
2087
+ await promises$1.readdir(dir);
2088
+ }
2089
+ catch (err) {
2090
+ if (this._removeWatcher) {
2091
+ this._removeWatcher(sysPath__namespace.dirname(dir), sysPath__namespace.basename(dir));
2092
+ }
2093
+ }
2094
+ }
2095
+ has(item) {
2096
+ const { items } = this;
2097
+ if (!items)
2098
+ return;
2099
+ return items.has(item);
2100
+ }
2101
+ getChildren() {
2102
+ const { items } = this;
2103
+ if (!items)
2104
+ return [];
2105
+ return [...items.values()];
2106
+ }
2107
+ dispose() {
2108
+ this.items.clear();
2109
+ this.path = '';
2110
+ this._removeWatcher = EMPTY_FN;
2111
+ this.items = EMPTY_SET;
2112
+ Object.freeze(this);
2113
+ }
2114
+ }
2115
+ const STAT_METHOD_F = 'stat';
2116
+ const STAT_METHOD_L = 'lstat';
2117
+ class WatchHelper {
2118
+ constructor(path, follow, fsw) {
2119
+ this.fsw = fsw;
2120
+ const watchPath = path;
2121
+ this.path = path = path.replace(REPLACER_RE, '');
2122
+ this.watchPath = watchPath;
2123
+ this.fullWatchPath = sysPath__namespace.resolve(watchPath);
2124
+ this.dirParts = [];
2125
+ this.dirParts.forEach((parts) => {
2126
+ if (parts.length > 1)
2127
+ parts.pop();
2128
+ });
2129
+ this.followSymlinks = follow;
2130
+ this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
2131
+ }
2132
+ entryPath(entry) {
2133
+ return sysPath__namespace.join(this.watchPath, sysPath__namespace.relative(this.watchPath, entry.fullPath));
2134
+ }
2135
+ filterPath(entry) {
2136
+ const { stats } = entry;
2137
+ if (stats && stats.isSymbolicLink())
2138
+ return this.filterDir(entry);
2139
+ const resolvedPath = this.entryPath(entry);
2140
+ // TODO: what if stats is undefined? remove !
2141
+ return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
2142
+ }
2143
+ filterDir(entry) {
2144
+ return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
2145
+ }
2146
+ }
2147
+ /**
2148
+ * Watches files & directories for changes. Emitted events:
2149
+ * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
2150
+ *
2151
+ * new FSWatcher()
2152
+ * .add(directories)
2153
+ * .on('add', path => log('File', path, 'was added'))
2154
+ */
2155
+ class FSWatcher extends events.EventEmitter {
2156
+ // Not indenting methods for history sake; for now.
2157
+ constructor(_opts = {}) {
2158
+ super();
2159
+ this.closed = false;
2160
+ this._closers = new Map();
2161
+ this._ignoredPaths = new Set();
2162
+ this._throttled = new Map();
2163
+ this._streams = new Set();
2164
+ this._symlinkPaths = new Map();
2165
+ this._watched = new Map();
2166
+ this._pendingWrites = new Map();
2167
+ this._pendingUnlinks = new Map();
2168
+ this._readyCount = 0;
2169
+ this._readyEmitted = false;
2170
+ const awf = _opts.awaitWriteFinish;
2171
+ const DEF_AWF = { stabilityThreshold: 2000, pollInterval: 100 };
2172
+ const opts = {
2173
+ // Defaults
2174
+ persistent: true,
2175
+ ignoreInitial: false,
2176
+ ignorePermissionErrors: false,
2177
+ interval: 100,
2178
+ binaryInterval: 300,
2179
+ followSymlinks: true,
2180
+ usePolling: false,
2181
+ // useAsync: false,
2182
+ atomic: true, // NOTE: overwritten later (depends on usePolling)
2183
+ ..._opts,
2184
+ // Change format
2185
+ ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
2186
+ awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === 'object' ? { ...DEF_AWF, ...awf } : false,
2187
+ };
2188
+ // Always default to polling on IBM i because fs.watch() is not available on IBM i.
2189
+ if (isIBMi)
2190
+ opts.usePolling = true;
2191
+ // Editor atomic write normalization enabled by default with fs.watch
2192
+ if (opts.atomic === undefined)
2193
+ opts.atomic = !opts.usePolling;
2194
+ // opts.atomic = typeof _opts.atomic === 'number' ? _opts.atomic : 100;
2195
+ // Global override. Useful for developers, who need to force polling for all
2196
+ // instances of chokidar, regardless of usage / dependency depth
2197
+ const envPoll = process.env.CHOKIDAR_USEPOLLING;
2198
+ if (envPoll !== undefined) {
2199
+ const envLower = envPoll.toLowerCase();
2200
+ if (envLower === 'false' || envLower === '0')
2201
+ opts.usePolling = false;
2202
+ else if (envLower === 'true' || envLower === '1')
2203
+ opts.usePolling = true;
2204
+ else
2205
+ opts.usePolling = !!envLower;
2206
+ }
2207
+ const envInterval = process.env.CHOKIDAR_INTERVAL;
2208
+ if (envInterval)
2209
+ opts.interval = Number.parseInt(envInterval, 10);
2210
+ // This is done to emit ready only once, but each 'add' will increase that?
2211
+ let readyCalls = 0;
2212
+ this._emitReady = () => {
2213
+ readyCalls++;
2214
+ if (readyCalls >= this._readyCount) {
2215
+ this._emitReady = EMPTY_FN;
2216
+ this._readyEmitted = true;
2217
+ // use process.nextTick to allow time for listener to be bound
2218
+ process.nextTick(() => this.emit(EVENTS.READY));
2219
+ }
2220
+ };
2221
+ this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
2222
+ this._boundRemove = this._remove.bind(this);
2223
+ this.options = opts;
2224
+ this._nodeFsHandler = new NodeFsHandler(this);
2225
+ // You’re frozen when your heart’s not open.
2226
+ Object.freeze(opts);
2227
+ }
2228
+ _addIgnoredPath(matcher) {
2229
+ if (isMatcherObject(matcher)) {
2230
+ // return early if we already have a deeply equal matcher object
2231
+ for (const ignored of this._ignoredPaths) {
2232
+ if (isMatcherObject(ignored) &&
2233
+ ignored.path === matcher.path &&
2234
+ ignored.recursive === matcher.recursive) {
2235
+ return;
2236
+ }
2237
+ }
2238
+ }
2239
+ this._ignoredPaths.add(matcher);
2240
+ }
2241
+ _removeIgnoredPath(matcher) {
2242
+ this._ignoredPaths.delete(matcher);
2243
+ // now find any matcher objects with the matcher as path
2244
+ if (typeof matcher === 'string') {
2245
+ for (const ignored of this._ignoredPaths) {
2246
+ // TODO (43081j): make this more efficient.
2247
+ // probably just make a `this._ignoredDirectories` or some
2248
+ // such thing.
2249
+ if (isMatcherObject(ignored) && ignored.path === matcher) {
2250
+ this._ignoredPaths.delete(ignored);
2251
+ }
2252
+ }
2253
+ }
2254
+ }
2255
+ // Public methods
2256
+ /**
2257
+ * Adds paths to be watched on an existing FSWatcher instance.
2258
+ * @param paths_ file or file list. Other arguments are unused
2259
+ */
2260
+ add(paths_, _origAdd, _internal) {
2261
+ const { cwd } = this.options;
2262
+ this.closed = false;
2263
+ this._closePromise = undefined;
2264
+ let paths = unifyPaths(paths_);
2265
+ if (cwd) {
2266
+ paths = paths.map((path) => {
2267
+ const absPath = getAbsolutePath(path, cwd);
2268
+ // Check `path` instead of `absPath` because the cwd portion can't be a glob
2269
+ return absPath;
2270
+ });
2271
+ }
2272
+ paths.forEach((path) => {
2273
+ this._removeIgnoredPath(path);
2274
+ });
2275
+ this._userIgnored = undefined;
2276
+ if (!this._readyCount)
2277
+ this._readyCount = 0;
2278
+ this._readyCount += paths.length;
2279
+ Promise.all(paths.map(async (path) => {
2280
+ const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, undefined, 0, _origAdd);
2281
+ if (res)
2282
+ this._emitReady();
2283
+ return res;
2284
+ })).then((results) => {
2285
+ if (this.closed)
2286
+ return;
2287
+ results.forEach((item) => {
2288
+ if (item)
2289
+ this.add(sysPath__namespace.dirname(item), sysPath__namespace.basename(_origAdd || item));
2290
+ });
2291
+ });
2292
+ return this;
2293
+ }
2294
+ /**
2295
+ * Close watchers or start ignoring events from specified paths.
2296
+ */
2297
+ unwatch(paths_) {
2298
+ if (this.closed)
2299
+ return this;
2300
+ const paths = unifyPaths(paths_);
2301
+ const { cwd } = this.options;
2302
+ paths.forEach((path) => {
2303
+ // convert to absolute path unless relative path already matches
2304
+ if (!sysPath__namespace.isAbsolute(path) && !this._closers.has(path)) {
2305
+ if (cwd)
2306
+ path = sysPath__namespace.join(cwd, path);
2307
+ path = sysPath__namespace.resolve(path);
2308
+ }
2309
+ this._closePath(path);
2310
+ this._addIgnoredPath(path);
2311
+ if (this._watched.has(path)) {
2312
+ this._addIgnoredPath({
2313
+ path,
2314
+ recursive: true,
2315
+ });
2316
+ }
2317
+ // reset the cached userIgnored anymatch fn
2318
+ // to make ignoredPaths changes effective
2319
+ this._userIgnored = undefined;
2320
+ });
2321
+ return this;
2322
+ }
2323
+ /**
2324
+ * Close watchers and remove all listeners from watched paths.
2325
+ */
2326
+ close() {
2327
+ if (this._closePromise) {
2328
+ return this._closePromise;
2329
+ }
2330
+ this.closed = true;
2331
+ // Memory management.
2332
+ this.removeAllListeners();
2333
+ const closers = [];
2334
+ this._closers.forEach((closerList) => closerList.forEach((closer) => {
2335
+ const promise = closer();
2336
+ if (promise instanceof Promise)
2337
+ closers.push(promise);
2338
+ }));
2339
+ this._streams.forEach((stream) => stream.destroy());
2340
+ this._userIgnored = undefined;
2341
+ this._readyCount = 0;
2342
+ this._readyEmitted = false;
2343
+ this._watched.forEach((dirent) => dirent.dispose());
2344
+ this._closers.clear();
2345
+ this._watched.clear();
2346
+ this._streams.clear();
2347
+ this._symlinkPaths.clear();
2348
+ this._throttled.clear();
2349
+ this._closePromise = closers.length
2350
+ ? Promise.all(closers).then(() => undefined)
2351
+ : Promise.resolve();
2352
+ return this._closePromise;
2353
+ }
2354
+ /**
2355
+ * Expose list of watched paths
2356
+ * @returns for chaining
2357
+ */
2358
+ getWatched() {
2359
+ const watchList = {};
2360
+ this._watched.forEach((entry, dir) => {
2361
+ const key = this.options.cwd ? sysPath__namespace.relative(this.options.cwd, dir) : dir;
2362
+ const index = key || ONE_DOT;
2363
+ watchList[index] = entry.getChildren().sort();
2364
+ });
2365
+ return watchList;
2366
+ }
2367
+ emitWithAll(event, args) {
2368
+ this.emit(event, ...args);
2369
+ if (event !== EVENTS.ERROR)
2370
+ this.emit(EVENTS.ALL, event, ...args);
2371
+ }
2372
+ // Common helpers
2373
+ // --------------
2374
+ /**
2375
+ * Normalize and emit events.
2376
+ * Calling _emit DOES NOT MEAN emit() would be called!
2377
+ * @param event Type of event
2378
+ * @param path File or directory path
2379
+ * @param stats arguments to be passed with event
2380
+ * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2381
+ */
2382
+ async _emit(event, path, stats) {
2383
+ if (this.closed)
2384
+ return;
2385
+ const opts = this.options;
2386
+ if (isWindows)
2387
+ path = sysPath__namespace.normalize(path);
2388
+ if (opts.cwd)
2389
+ path = sysPath__namespace.relative(opts.cwd, path);
2390
+ const args = [path];
2391
+ if (stats != null)
2392
+ args.push(stats);
2393
+ const awf = opts.awaitWriteFinish;
2394
+ let pw;
2395
+ if (awf && (pw = this._pendingWrites.get(path))) {
2396
+ pw.lastChange = new Date();
2397
+ return this;
2398
+ }
2399
+ if (opts.atomic) {
2400
+ if (event === EVENTS.UNLINK) {
2401
+ this._pendingUnlinks.set(path, [event, ...args]);
2402
+ setTimeout(() => {
2403
+ this._pendingUnlinks.forEach((entry, path) => {
2404
+ this.emit(...entry);
2405
+ this.emit(EVENTS.ALL, ...entry);
2406
+ this._pendingUnlinks.delete(path);
2407
+ });
2408
+ }, typeof opts.atomic === 'number' ? opts.atomic : 100);
2409
+ return this;
2410
+ }
2411
+ if (event === EVENTS.ADD && this._pendingUnlinks.has(path)) {
2412
+ event = EVENTS.CHANGE;
2413
+ this._pendingUnlinks.delete(path);
2414
+ }
2415
+ }
2416
+ if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
2417
+ const awfEmit = (err, stats) => {
2418
+ if (err) {
2419
+ event = EVENTS.ERROR;
2420
+ args[0] = err;
2421
+ this.emitWithAll(event, args);
2422
+ }
2423
+ else if (stats) {
2424
+ // if stats doesn't exist the file must have been deleted
2425
+ if (args.length > 1) {
2426
+ args[1] = stats;
2427
+ }
2428
+ else {
2429
+ args.push(stats);
2430
+ }
2431
+ this.emitWithAll(event, args);
2432
+ }
2433
+ };
2434
+ this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
2435
+ return this;
2436
+ }
2437
+ if (event === EVENTS.CHANGE) {
2438
+ const isThrottled = !this._throttle(EVENTS.CHANGE, path, 50);
2439
+ if (isThrottled)
2440
+ return this;
2441
+ }
2442
+ if (opts.alwaysStat &&
2443
+ stats === undefined &&
2444
+ (event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
2445
+ const fullPath = opts.cwd ? sysPath__namespace.join(opts.cwd, path) : path;
2446
+ let stats;
2447
+ try {
2448
+ stats = await promises$1.stat(fullPath);
2449
+ }
2450
+ catch (err) {
2451
+ // do nothing
2452
+ }
2453
+ // Suppress event when fs_stat fails, to avoid sending undefined 'stat'
2454
+ if (!stats || this.closed)
2455
+ return;
2456
+ args.push(stats);
2457
+ }
2458
+ this.emitWithAll(event, args);
2459
+ return this;
2460
+ }
2461
+ /**
2462
+ * Common handler for errors
2463
+ * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2464
+ */
2465
+ _handleError(error) {
2466
+ const code = error && error.code;
2467
+ if (error &&
2468
+ code !== 'ENOENT' &&
2469
+ code !== 'ENOTDIR' &&
2470
+ (!this.options.ignorePermissionErrors || (code !== 'EPERM' && code !== 'EACCES'))) {
2471
+ this.emit(EVENTS.ERROR, error);
2472
+ }
2473
+ return error || this.closed;
2474
+ }
2475
+ /**
2476
+ * Helper utility for throttling
2477
+ * @param actionType type being throttled
2478
+ * @param path being acted upon
2479
+ * @param timeout duration of time to suppress duplicate actions
2480
+ * @returns tracking object or false if action should be suppressed
2481
+ */
2482
+ _throttle(actionType, path, timeout) {
2483
+ if (!this._throttled.has(actionType)) {
2484
+ this._throttled.set(actionType, new Map());
2485
+ }
2486
+ const action = this._throttled.get(actionType);
2487
+ if (!action)
2488
+ throw new Error('invalid throttle');
2489
+ const actionPath = action.get(path);
2490
+ if (actionPath) {
2491
+ actionPath.count++;
2492
+ return false;
2493
+ }
2494
+ // eslint-disable-next-line prefer-const
2495
+ let timeoutObject;
2496
+ const clear = () => {
2497
+ const item = action.get(path);
2498
+ const count = item ? item.count : 0;
2499
+ action.delete(path);
2500
+ clearTimeout(timeoutObject);
2501
+ if (item)
2502
+ clearTimeout(item.timeoutObject);
2503
+ return count;
2504
+ };
2505
+ timeoutObject = setTimeout(clear, timeout);
2506
+ const thr = { timeoutObject, clear, count: 0 };
2507
+ action.set(path, thr);
2508
+ return thr;
2509
+ }
2510
+ _incrReadyCount() {
2511
+ return this._readyCount++;
2512
+ }
2513
+ /**
2514
+ * Awaits write operation to finish.
2515
+ * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
2516
+ * @param path being acted upon
2517
+ * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
2518
+ * @param event
2519
+ * @param awfEmit Callback to be called when ready for event to be emitted.
2520
+ */
2521
+ _awaitWriteFinish(path, threshold, event, awfEmit) {
2522
+ const awf = this.options.awaitWriteFinish;
2523
+ if (typeof awf !== 'object')
2524
+ return;
2525
+ const pollInterval = awf.pollInterval;
2526
+ let timeoutHandler;
2527
+ let fullPath = path;
2528
+ if (this.options.cwd && !sysPath__namespace.isAbsolute(path)) {
2529
+ fullPath = sysPath__namespace.join(this.options.cwd, path);
2530
+ }
2531
+ const now = new Date();
2532
+ const writes = this._pendingWrites;
2533
+ function awaitWriteFinishFn(prevStat) {
2534
+ fs$1.stat(fullPath, (err, curStat) => {
2535
+ if (err || !writes.has(path)) {
2536
+ if (err && err.code !== 'ENOENT')
2537
+ awfEmit(err);
2538
+ return;
2539
+ }
2540
+ const now = Number(new Date());
2541
+ if (prevStat && curStat.size !== prevStat.size) {
2542
+ writes.get(path).lastChange = now;
2543
+ }
2544
+ const pw = writes.get(path);
2545
+ const df = now - pw.lastChange;
2546
+ if (df >= threshold) {
2547
+ writes.delete(path);
2548
+ awfEmit(undefined, curStat);
2549
+ }
2550
+ else {
2551
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
2552
+ }
2553
+ });
2554
+ }
2555
+ if (!writes.has(path)) {
2556
+ writes.set(path, {
2557
+ lastChange: now,
2558
+ cancelWait: () => {
2559
+ writes.delete(path);
2560
+ clearTimeout(timeoutHandler);
2561
+ return event;
2562
+ },
2563
+ });
2564
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
2565
+ }
2566
+ }
2567
+ /**
2568
+ * Determines whether user has asked to ignore this path.
2569
+ */
2570
+ _isIgnored(path, stats) {
2571
+ if (this.options.atomic && DOT_RE.test(path))
2572
+ return true;
2573
+ if (!this._userIgnored) {
2574
+ const { cwd } = this.options;
2575
+ const ign = this.options.ignored;
2576
+ const ignored = (ign || []).map(normalizeIgnored(cwd));
2577
+ const ignoredPaths = [...this._ignoredPaths];
2578
+ const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
2579
+ this._userIgnored = anymatch(list);
2580
+ }
2581
+ return this._userIgnored(path, stats);
2582
+ }
2583
+ _isntIgnored(path, stat) {
2584
+ return !this._isIgnored(path, stat);
2585
+ }
2586
+ /**
2587
+ * Provides a set of common helpers and properties relating to symlink handling.
2588
+ * @param path file or directory pattern being watched
2589
+ */
2590
+ _getWatchHelpers(path) {
2591
+ return new WatchHelper(path, this.options.followSymlinks, this);
2592
+ }
2593
+ // Directory helpers
2594
+ // -----------------
2595
+ /**
2596
+ * Provides directory tracking objects
2597
+ * @param directory path of the directory
2598
+ */
2599
+ _getWatchedDir(directory) {
2600
+ const dir = sysPath__namespace.resolve(directory);
2601
+ if (!this._watched.has(dir))
2602
+ this._watched.set(dir, new DirEntry(dir, this._boundRemove));
2603
+ return this._watched.get(dir);
2604
+ }
2605
+ // File helpers
2606
+ // ------------
2607
+ /**
2608
+ * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
2609
+ */
2610
+ _hasReadPermissions(stats) {
2611
+ if (this.options.ignorePermissionErrors)
2612
+ return true;
2613
+ return Boolean(Number(stats.mode) & 0o400);
2614
+ }
2615
+ /**
2616
+ * Handles emitting unlink events for
2617
+ * files and directories, and via recursion, for
2618
+ * files and directories within directories that are unlinked
2619
+ * @param directory within which the following item is located
2620
+ * @param item base path of item/directory
2621
+ */
2622
+ _remove(directory, item, isDirectory) {
2623
+ // if what is being deleted is a directory, get that directory's paths
2624
+ // for recursive deleting and cleaning of watched object
2625
+ // if it is not a directory, nestedDirectoryChildren will be empty array
2626
+ const path = sysPath__namespace.join(directory, item);
2627
+ const fullPath = sysPath__namespace.resolve(path);
2628
+ isDirectory =
2629
+ isDirectory != null ? isDirectory : this._watched.has(path) || this._watched.has(fullPath);
2630
+ // prevent duplicate handling in case of arriving here nearly simultaneously
2631
+ // via multiple paths (such as _handleFile and _handleDir)
2632
+ if (!this._throttle('remove', path, 100))
2633
+ return;
2634
+ // if the only watched file is removed, watch for its return
2635
+ if (!isDirectory && this._watched.size === 1) {
2636
+ this.add(directory, item, true);
2637
+ }
2638
+ // This will create a new entry in the watched object in either case
2639
+ // so we got to do the directory check beforehand
2640
+ const wp = this._getWatchedDir(path);
2641
+ const nestedDirectoryChildren = wp.getChildren();
2642
+ // Recursively remove children directories / files.
2643
+ nestedDirectoryChildren.forEach((nested) => this._remove(path, nested));
2644
+ // Check if item was on the watched list and remove it
2645
+ const parent = this._getWatchedDir(directory);
2646
+ const wasTracked = parent.has(item);
2647
+ parent.remove(item);
2648
+ // Fixes issue #1042 -> Relative paths were detected and added as symlinks
2649
+ // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
2650
+ // but never removed from the map in case the path was deleted.
2651
+ // This leads to an incorrect state if the path was recreated:
2652
+ // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
2653
+ if (this._symlinkPaths.has(fullPath)) {
2654
+ this._symlinkPaths.delete(fullPath);
2655
+ }
2656
+ // If we wait for this file to be fully written, cancel the wait.
2657
+ let relPath = path;
2658
+ if (this.options.cwd)
2659
+ relPath = sysPath__namespace.relative(this.options.cwd, path);
2660
+ if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
2661
+ const event = this._pendingWrites.get(relPath).cancelWait();
2662
+ if (event === EVENTS.ADD)
2663
+ return;
2664
+ }
2665
+ // The Entry will either be a directory that just got removed
2666
+ // or a bogus entry to a file, in either case we have to remove it
2667
+ this._watched.delete(path);
2668
+ this._watched.delete(fullPath);
2669
+ const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
2670
+ if (wasTracked && !this._isIgnored(path))
2671
+ this._emit(eventName, path);
2672
+ // Avoid conflicts if we later create another file with the same name
2673
+ this._closePath(path);
2674
+ }
2675
+ /**
2676
+ * Closes all watchers for a path
2677
+ */
2678
+ _closePath(path) {
2679
+ this._closeFile(path);
2680
+ const dir = sysPath__namespace.dirname(path);
2681
+ this._getWatchedDir(dir).remove(sysPath__namespace.basename(path));
2682
+ }
2683
+ /**
2684
+ * Closes only file-specific watchers
2685
+ */
2686
+ _closeFile(path) {
2687
+ const closers = this._closers.get(path);
2688
+ if (!closers)
2689
+ return;
2690
+ closers.forEach((closer) => closer());
2691
+ this._closers.delete(path);
2692
+ }
2693
+ _addPathCloser(path, closer) {
2694
+ if (!closer)
2695
+ return;
2696
+ let list = this._closers.get(path);
2697
+ if (!list) {
2698
+ list = [];
2699
+ this._closers.set(path, list);
2700
+ }
2701
+ list.push(closer);
2702
+ }
2703
+ _readdirp(root, opts) {
2704
+ if (this.closed)
2705
+ return;
2706
+ const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
2707
+ let stream = readdirp(root, options);
2708
+ this._streams.add(stream);
2709
+ stream.once(STR_CLOSE, () => {
2710
+ stream = undefined;
2711
+ });
2712
+ stream.once(STR_END, () => {
2713
+ if (stream) {
2714
+ this._streams.delete(stream);
2715
+ stream = undefined;
2716
+ }
2717
+ });
2718
+ return stream;
2719
+ }
2720
+ }
2721
+ /**
2722
+ * Instantiates watcher with paths to be tracked.
2723
+ * @param paths file / directory paths
2724
+ * @param options opts, such as `atomic`, `awaitWriteFinish`, `ignored`, and others
2725
+ * @returns an instance of FSWatcher for chaining.
2726
+ * @example
2727
+ * const watcher = watch('.').on('all', (event, path) => { console.log(event, path); });
2728
+ * watch('.', { atomic: true, awaitWriteFinish: true, ignored: (f, stats) => stats?.isFile() && !f.endsWith('.js') })
2729
+ */
2730
+ function watch(paths, options = {}) {
2731
+ const watcher = new FSWatcher(options);
2732
+ watcher.add(paths);
2733
+ return watcher;
2734
+ }
2735
+ var chokidar = { watch, FSWatcher };
2736
+
1025
2737
  class FluxionWatcher {
1026
2738
  constructor(cx) {
1027
2739
  this.timer = null;
@@ -1064,18 +2776,41 @@ class FluxionWatcher {
1064
2776
  return this;
1065
2777
  }
1066
2778
  /**
1067
- * Since all actions are mapped to `rename` and `change` (WatchEventType).
2779
+ * Start watching files with chokidar.
1068
2780
  *
1069
- * We could only record every file and reload them all.
2781
+ * Using chokidar provides:
2782
+ * - Cross-platform recursive watch support (including Linux/CentOS)
2783
+ * - Better event handling and stability
2784
+ * - Automatic resource management
1070
2785
  */
1071
2786
  start() {
1072
2787
  this.init();
1073
- this.watcher = fs
1074
- .watch(this.cx.options.dir, { recursive: true }, (_eventType, filename) => {
2788
+ const dirPath = path$1.isAbsolute(this.cx.options.dir)
2789
+ ? this.cx.options.dir
2790
+ : path$1.join(process.cwd(), this.cx.options.dir);
2791
+ this.watcher = chokidar
2792
+ .watch(dirPath, {
2793
+ // Ignore dotfiles and common ignore patterns
2794
+ ignored: /(^|[\/\\])\../,
2795
+ // Keep the process running
2796
+ persistent: true,
2797
+ // Don't emit 'add' events for initial scan
2798
+ ignoreInitial: true,
2799
+ // Use polling as fallback (helps with some network drives)
2800
+ usePolling: false,
2801
+ // Atomic writes handling
2802
+ awaitWriteFinish: {
2803
+ stabilityThreshold: 100,
2804
+ pollInterval: 50,
2805
+ },
2806
+ })
2807
+ .on('all', (_event, filename) => {
1075
2808
  if (!filename) {
1076
2809
  return;
1077
2810
  }
1078
- this.filesChanged.add(filename);
2811
+ // Calculate relative path
2812
+ const relativePath = path$1.relative(dirPath, filename);
2813
+ this.filesChanged.add(relativePath);
1079
2814
  if (!this.timer) {
1080
2815
  this.timer = setTimeout(() => {
1081
2816
  this.filesChanged.forEach((p, _, s) => {
@@ -1094,9 +2829,13 @@ class FluxionWatcher {
1094
2829
  }
1095
2830
  })
1096
2831
  .on('error', (err) => {
1097
- this.cx.logger.error(`Watcher error: ${err.message}`);
2832
+ const error = err instanceof Error ? err : new Error(String(err));
2833
+ this.cx.logger.error(`Watcher error: ${error.message}`);
1098
2834
  this.cx.logger.error(`Restarting watcher...`);
1099
2835
  this.stop().start();
2836
+ })
2837
+ .on('ready', () => {
2838
+ this.cx.logger.info(`Watcher ready and watching directory: ${this.cx.options.dir}`);
1100
2839
  });
1101
2840
  this.cx.logger.info(`Watcher started on directory: ${this.cx.options.dir}`);
1102
2841
  return this;
@@ -3636,6 +5375,8 @@ async function fluxion(options) {
3636
5375
  initPrimary(context);
3637
5376
  }
3638
5377
  else {
5378
+ // Replace logger with worker logger that prefixes PID
5379
+ context.logger = createWorkerLogger(context.logger, process.pid);
3639
5380
  // Only worker creates the watcher
3640
5381
  context.watcher = new FluxionWatcher(context).start();
3641
5382
  initWorker(context);