@rljson/fs-agent 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.tests.md CHANGED
@@ -34,7 +34,7 @@ index is at the end.
34
34
  | tier | what it is | files |
35
35
  | --- | --- | --- |
36
36
  | **mesh** | a real `Server`, real sockets (`createSocketPair`), real watchers, real files on disk, two to four nodes, with cuts, heals, mutes, deafenings and stopped processes | 12 |
37
- | **client–server** | two or three clients and a real hub, each suite run **twice** — once over `SocketMock`, once over a real socket.io server configured at production's 50 MB limit | 6 |
37
+ | **client–server** | two or three clients and a real hub, each suite run **twice** — once over `SocketMock`, once over a real socket.io server configured at production's 50 MB limit | 7 |
38
38
  | **unit / decision** | one agent, or a pure function with no filesystem and no clock | 55 |
39
39
 
40
40
  A decision-tier test proves a RULE. It does not prove the rule is ever
@@ -450,6 +450,7 @@ loop are counted as they run. They sum to the 807 above.
450
450
  | --- | --- | --- |
451
451
  | `client-server/socket-mock.spec.ts` | 35 | the shared suite (`shared-sync-tests.ts`) over `SocketMock` |
452
452
  | `client-server/socket-io.spec.ts` | 35 | the same shared suite over a real socket.io server at production's 50 MB limit |
453
+ | `client-server/large-file.spec.ts` | 2 | a 60 MB file over a real socket.io server at the 50 MB limit: added, changed, deleted, and edited on both sides while apart; counts the bytes the hub carries |
453
454
  | `client-server/advanced-sync.spec.ts` | 15 | three clients, catch-up, teardown and restart, 10 MB, 100 files, rapid overwrites |
454
455
  | `client-server/heals-after-forced-divergence.spec.ts` | 14 | deliberately dropped messages |
455
456
  | `client-server/conflict-sync.spec.ts` | 2 | a real offline divergent edit, both versions preserved |
@@ -34,7 +34,7 @@ index is at the end.
34
34
  | tier | what it is | files |
35
35
  | --- | --- | --- |
36
36
  | **mesh** | a real `Server`, real sockets (`createSocketPair`), real watchers, real files on disk, two to four nodes, with cuts, heals, mutes, deafenings and stopped processes | 12 |
37
- | **client–server** | two or three clients and a real hub, each suite run **twice** — once over `SocketMock`, once over a real socket.io server configured at production's 50 MB limit | 6 |
37
+ | **client–server** | two or three clients and a real hub, each suite run **twice** — once over `SocketMock`, once over a real socket.io server configured at production's 50 MB limit | 7 |
38
38
  | **unit / decision** | one agent, or a pure function with no filesystem and no clock | 55 |
39
39
 
40
40
  A decision-tier test proves a RULE. It does not prove the rule is ever
@@ -450,6 +450,7 @@ loop are counted as they run. They sum to the 807 above.
450
450
  | --- | --- | --- |
451
451
  | `client-server/socket-mock.spec.ts` | 35 | the shared suite (`shared-sync-tests.ts`) over `SocketMock` |
452
452
  | `client-server/socket-io.spec.ts` | 35 | the same shared suite over a real socket.io server at production's 50 MB limit |
453
+ | `client-server/large-file.spec.ts` | 2 | a 60 MB file over a real socket.io server at the 50 MB limit: added, changed, deleted, and edited on both sides while apart; counts the bytes the hub carries |
453
454
  | `client-server/advanced-sync.spec.ts` | 15 | three clients, catch-up, teardown and restart, 10 MB, 100 files, rapid overwrites |
454
455
  | `client-server/heals-after-forced-divergence.spec.ts` | 14 | deliberately dropped messages |
455
456
  | `client-server/conflict-sync.spec.ts` | 2 | a real offline divergent edit, both versions preserved |
@@ -611,17 +611,6 @@ export declare class FsAgent {
611
611
  * @param err - The error value caught
612
612
  */
613
613
  _writeSyncError(context: string, err: unknown): void;
614
- /**
615
- * Writes a file through a staging file and a rename.
616
- *
617
- * Kept as a thin named wrapper because the restore path reads as a pair with
618
- * {@link FsAgent._atomicWriteStream}. The rule, and the byte-level
619
- * corruption that made it a rule on every platform rather than on Windows
620
- * only, are in `fs-atomic-write.ts`.
621
- * @param filePath - Destination path
622
- * @param content - Bytes to write
623
- */
624
- private static _atomicWriteFile;
625
614
  /**
626
615
  * Whether this error came from reading the blob rather than writing the file.
627
616
  *
@@ -639,8 +628,9 @@ export declare class FsAgent {
639
628
  /**
640
629
  * Writes a file from a stream, holding one chunk at a time.
641
630
  *
642
- * The twin of {@link FsAgent._atomicWriteFile} — temp and rename on every
643
- * platform, for the same reasons — but never materialising the whole file. A
631
+ * Temp and rename on every platform — the rule, and the byte-level
632
+ * corruption that made it a rule, are in `fs-atomic-write.ts` — and never
633
+ * materialising the whole file. A
644
634
  * 500 MB file used to cost 500 MB of Buffer on the receiving agent, another
645
635
  * copy in the socket parser, and — on the serving hub — the same again. That
646
636
  * is the shape that exhausted a cloud relay: memory that is work in flight
package/dist/fs-agent.js CHANGED
@@ -2040,6 +2040,10 @@ var FsScanner = class FsScanner {
2040
2040
  _missedChangesDuringPause = false;
2041
2041
  /** Periodic full-rescan timer that catches events the native watcher drops. */
2042
2042
  _safetyTimer = null;
2043
+ /** Whether a safety rescan is running; a tick meanwhile is skipped. */
2044
+ _safetyRescanRunning = false;
2045
+ /** Files that could not be opened, reported once until they can be. */
2046
+ _locked = /* @__PURE__ */ new Set();
2043
2047
  /** When the current pause began, or null when not paused. */
2044
2048
  _pausedAt = null;
2045
2049
  /** Releases a pause whose resume never arrived (see {@link pauseWatch}). */
@@ -2294,8 +2298,14 @@ var FsScanner = class FsScanner {
2294
2298
  handle = await open(childPath, "r");
2295
2299
  } catch (error) {
2296
2300
  if (FsScanner._isVanished(error)) throw error;
2301
+ if (this._watcher !== null && FsScanner._isLocked(error)) {
2302
+ this._reportLocked(childRelPath, error);
2303
+ this._keepPrevious(childRelPath, trees, childTrees, childRefs);
2304
+ continue;
2305
+ }
2297
2306
  throw new Error(`Failed to read file "${childRelPath}": ${error instanceof Error ? error.message : String(error)}`);
2298
2307
  }
2308
+ this._locked.delete(childRelPath);
2299
2309
  let blobProps;
2300
2310
  try {
2301
2311
  blobProps = await storeFileAsBlob(this._bs, handle, childStats.size);
@@ -2371,7 +2381,7 @@ var FsScanner = class FsScanner {
2371
2381
  try {
2372
2382
  const raw = await readFile(this._scanCachePath, "utf8");
2373
2383
  const parsed = JSON.parse(raw);
2374
- if (Array.isArray(parsed.entries)) this._blobCache = new Map(parsed.entries);
2384
+ if (parsed.version === 2 && Array.isArray(parsed.entries)) this._blobCache = new Map(parsed.entries);
2375
2385
  } catch {}
2376
2386
  }
2377
2387
  /**
@@ -2382,7 +2392,7 @@ var FsScanner = class FsScanner {
2382
2392
  /* v8 ignore next -- @preserve: only called when scanCachePath is set */
2383
2393
  if (!this._scanCachePath) return;
2384
2394
  const body = JSON.stringify({
2385
- version: 1,
2395
+ version: 2,
2386
2396
  entries: [...this._blobCache.entries()]
2387
2397
  });
2388
2398
  const tmp = `${this._scanCachePath}.${Date.now().toString(36)}.tmp`;
@@ -2518,6 +2528,17 @@ var FsScanner = class FsScanner {
2518
2528
  */
2519
2529
  _reuse(relativePath, trees, childTrees, childRefs) {
2520
2530
  this._unsettledDuringScan.push(relativePath);
2531
+ this._keepPrevious(relativePath, trees, childTrees, childRefs);
2532
+ }
2533
+ /**
2534
+ * Puts the node the last scan had for a path into this scan's tree, when
2535
+ * there was one.
2536
+ * @param relativePath - The path.
2537
+ * @param trees - The tree map being built.
2538
+ * @param childTrees - The parent's child nodes.
2539
+ * @param childRefs - The parent's child hashes.
2540
+ */
2541
+ _keepPrevious(relativePath, trees, childTrees, childRefs) {
2521
2542
  const previous = this.getTreeByPath(relativePath);
2522
2543
  if (!previous) return;
2523
2544
  const hash = previous._hash;
@@ -2544,7 +2565,26 @@ var FsScanner = class FsScanner {
2544
2565
  }
2545
2566
  /* v8 ignore stop -- @preserve */
2546
2567
  }
2568
+ /**
2569
+ * Runs one safety rescan, unless one is still running.
2570
+ *
2571
+ * The timer fires every {@link SAFETY_RESCAN_INTERVAL_MS} whatever a scan
2572
+ * costs, and a scan that waits on a large file being copied takes far
2573
+ * longer. Every tick then joined the same scan and, when it ended, each
2574
+ * reported the same drift — seven times in one millisecond on the lab
2575
+ * during a 400 MB copy, and seven notifications for one change. The tick
2576
+ * that finds a rescan running is covered by it.
2577
+ */
2547
2578
  async _runSafetyRescan() {
2579
+ if (this._safetyRescanRunning) return;
2580
+ this._safetyRescanRunning = true;
2581
+ try {
2582
+ await this._safetyRescan();
2583
+ } finally {
2584
+ this._safetyRescanRunning = false;
2585
+ }
2586
+ }
2587
+ async _safetyRescan() {
2548
2588
  if (this._stopRequested) return;
2549
2589
  if (this._paused && !this._pauseLooksStuck({
2550
2590
  type: "safety-rescan",
@@ -2622,6 +2662,26 @@ var FsScanner = class FsScanner {
2622
2662
  * @param err - The caught value.
2623
2663
  * @returns `true` for ENOENT / ENOTDIR.
2624
2664
  */
2665
+ /**
2666
+ * Whether an open failed because the file is held by another program, or
2667
+ * may not be read: Windows answers a copy in progress with EBUSY.
2668
+ * @param err - What the open threw.
2669
+ * @returns True for EBUSY, EPERM and EACCES.
2670
+ */
2671
+ static _isLocked(err) {
2672
+ const code = err?.code;
2673
+ return code === "EBUSY" || code === "EPERM" || code === "EACCES";
2674
+ }
2675
+ /**
2676
+ * Reports a file that could not be opened, once until it can be again.
2677
+ * @param relativePath - The file.
2678
+ * @param err - Why it could not be opened.
2679
+ */
2680
+ _reportLocked(relativePath, err) {
2681
+ if (this._locked.has(relativePath)) return;
2682
+ this._locked.add(relativePath);
2683
+ console.warn(`[fs-scanner] "${relativePath}" is locked (${FsScanner._errMessage(err)}) — keeping its last version, read again on the next scan`);
2684
+ }
2625
2685
  static _isVanished(err) {
2626
2686
  const code = err?.code;
2627
2687
  return code === "ENOENT" || code === "ENOTDIR";
@@ -3436,19 +3496,6 @@ var FsAgent = class FsAgent {
3436
3496
  } catch {}
3437
3497
  }
3438
3498
  /**
3439
- * Writes a file through a staging file and a rename.
3440
- *
3441
- * Kept as a thin named wrapper because the restore path reads as a pair with
3442
- * {@link FsAgent._atomicWriteStream}. The rule, and the byte-level
3443
- * corruption that made it a rule on every platform rather than on Windows
3444
- * only, are in `fs-atomic-write.ts`.
3445
- * @param filePath - Destination path
3446
- * @param content - Bytes to write
3447
- */
3448
- static async _atomicWriteFile(filePath, content) {
3449
- return atomicWriteFile(filePath, content);
3450
- }
3451
- /**
3452
3499
  * Whether this error came from reading the blob rather than writing the file.
3453
3500
  *
3454
3501
  * The two have different answers. A blob that cannot be fetched costs one
@@ -3467,8 +3514,9 @@ var FsAgent = class FsAgent {
3467
3514
  /**
3468
3515
  * Writes a file from a stream, holding one chunk at a time.
3469
3516
  *
3470
- * The twin of {@link FsAgent._atomicWriteFile} — temp and rename on every
3471
- * platform, for the same reasons — but never materialising the whole file. A
3517
+ * Temp and rename on every platform — the rule, and the byte-level
3518
+ * corruption that made it a rule, are in `fs-atomic-write.ts` — and never
3519
+ * materialising the whole file. A
3472
3520
  * 500 MB file used to cost 500 MB of Buffer on the receiving agent, another
3473
3521
  * copy in the socket parser, and — on the serving hub — the same again. That
3474
3522
  * is the shape that exhausted a cloud relay: memory that is work in flight
@@ -5159,7 +5207,7 @@ var FsAgent = class FsAgent {
5159
5207
  const blob = outcome.merged.get(path);
5160
5208
  const full = join(this._rootPath, ...path.split("/"));
5161
5209
  await mkdir(dirname(full), { recursive: true });
5162
- await FsAgent._atomicWriteFile(full, await this._adapter.getFileContent(blob));
5210
+ await FsAgent._atomicWriteStream(full, await this._bs.getBlobStream(blob));
5163
5211
  const st = await stat(full);
5164
5212
  this._scanner.noteWrite(path, blob, st.size, st.mtime.getTime());
5165
5213
  }
@@ -76,6 +76,16 @@ export type FsChangeCallback = (change: FsChange) => void | Promise<void>;
76
76
  * already covered.
77
77
  */
78
78
  export declare const DEFAULT_SETTLE_MS = 100;
79
+ /**
80
+ * The format of a persisted scan cache. A cache of any other version is
81
+ * discarded, and the next scan reads and stores every file once.
82
+ *
83
+ * A cached blobId is only worth reusing while the blob store still holds that
84
+ * blob. Version 2 came with a blob store that keeps blobs as blocks in a new
85
+ * layout: a node coming up with an older cache would announce blobs its store
86
+ * no longer holds, and nobody could fetch them until each file changed again.
87
+ */
88
+ export declare const SCAN_CACHE_VERSION = 2;
79
89
  export interface FsScanOptions {
80
90
  /** Patterns to ignore (glob patterns) */
81
91
  ignore?: string[];
@@ -140,6 +150,10 @@ export declare class FsScanner {
140
150
  private _missedChangesDuringPause;
141
151
  /** Periodic full-rescan timer that catches events the native watcher drops. */
142
152
  private _safetyTimer;
153
+ /** Whether a safety rescan is running; a tick meanwhile is skipped. */
154
+ private _safetyRescanRunning;
155
+ /** Files that could not be opened, reported once until they can be. */
156
+ private readonly _locked;
143
157
  /** When the current pause began, or null when not paused. */
144
158
  private _pausedAt;
145
159
  /** Releases a pause whose resume never arrived (see {@link pauseWatch}). */
@@ -335,8 +349,28 @@ export declare class FsScanner {
335
349
  * @param childRefs - The parent's child hashes.
336
350
  */
337
351
  private _reuse;
352
+ /**
353
+ * Puts the node the last scan had for a path into this scan's tree, when
354
+ * there was one.
355
+ * @param relativePath - The path.
356
+ * @param trees - The tree map being built.
357
+ * @param childTrees - The parent's child nodes.
358
+ * @param childRefs - The parent's child hashes.
359
+ */
360
+ private _keepPrevious;
338
361
  private _rescanAfterSettle;
362
+ /**
363
+ * Runs one safety rescan, unless one is still running.
364
+ *
365
+ * The timer fires every {@link SAFETY_RESCAN_INTERVAL_MS} whatever a scan
366
+ * costs, and a scan that waits on a large file being copied takes far
367
+ * longer. Every tick then joined the same scan and, when it ended, each
368
+ * reported the same drift — seven times in one millisecond on the lab
369
+ * during a 400 MB copy, and seven notifications for one change. The tick
370
+ * that finds a rescan running is covered by it.
371
+ */
339
372
  private _runSafetyRescan;
373
+ private _safetyRescan;
340
374
  /**
341
375
  * Path+blobId content fingerprint used by the safety rescan to detect drift
342
376
  * the native watcher missed (mtime-independent, same idea as the agent's
@@ -372,6 +406,19 @@ export declare class FsScanner {
372
406
  * @param err - The caught value.
373
407
  * @returns `true` for ENOENT / ENOTDIR.
374
408
  */
409
+ /**
410
+ * Whether an open failed because the file is held by another program, or
411
+ * may not be read: Windows answers a copy in progress with EBUSY.
412
+ * @param err - What the open threw.
413
+ * @returns True for EBUSY, EPERM and EACCES.
414
+ */
415
+ private static _isLocked;
416
+ /**
417
+ * Reports a file that could not be opened, once until it can be again.
418
+ * @param relativePath - The file.
419
+ * @param err - Why it could not be opened.
420
+ */
421
+ private _reportLocked;
375
422
  private static _isVanished;
376
423
  /** Whether the host is Windows — gates Windows-specific watcher hardening. */
377
424
  private static get _isWindows();
@@ -1,2 +1,2 @@
1
1
  /** The version of the `@rljson/fs-agent` package. */
2
- export declare const fsAgentVersion = "0.2.6";
2
+ export declare const fsAgentVersion = "0.2.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/fs-agent",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Rljson fs-agent description",
5
5
  "homepage": "https://github.com/rljson/fs-agent",
6
6
  "bugs": "https://github.com/rljson/fs-agent/issues",
@@ -21,7 +21,7 @@
21
21
  "type": "module",
22
22
  "devDependencies": {
23
23
  "@rljson/dna-rljson": "^0.3.2",
24
- "@rljson/server": "0.1.0",
24
+ "@rljson/server": "0.2.0",
25
25
  "@tssuite/helix-js": "0.5.1",
26
26
  "@types/node": "^26.6.6",
27
27
  "@typescript-eslint/eslint-plugin": "^8.72.1",
@@ -29,7 +29,7 @@
29
29
  "@vitest/coverage-v8": "^5.1.0",
30
30
  "cross-env": "^10.1.0",
31
31
  "eslint": "~10.14.0",
32
- "eslint-plugin-jsdoc": "^65.4.2",
32
+ "eslint-plugin-jsdoc": "^65.4.3",
33
33
  "eslint-plugin-tsdoc": "^0.5.4",
34
34
  "globals": "^17.14.0",
35
35
  "jsdoc": "^4.0.5",
@@ -44,7 +44,7 @@
44
44
  "vitest-dom": "^0.1.1"
45
45
  },
46
46
  "dependencies": {
47
- "@rljson/bs": "0.0.32",
47
+ "@rljson/bs": "0.1.1",
48
48
  "@rljson/db": "0.1.4",
49
49
  "@rljson/hash": "0.0.21",
50
50
  "@rljson/io": "0.0.88",