@rljson/fs-agent 0.2.7 → 0.2.8
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.api.md +6 -0
- package/README.architecture.md +14 -2
- package/README.md +2 -2
- package/README.tests.md +9 -9
- package/dist/README.api.md +6 -0
- package/dist/README.architecture.md +14 -2
- package/dist/README.md +2 -2
- package/dist/README.tests.md +9 -9
- package/dist/fs-agent.js +109 -4
- package/dist/fs-atomic-write.d.ts +25 -0
- package/dist/fs-scanner.d.ts +25 -0
- package/dist/fs_agent_version.d.ts +1 -1
- package/package.json +11 -11
package/README.api.md
CHANGED
|
@@ -446,6 +446,12 @@ produce bytes nobody wrote — measured at 1 520 blended pairs in 2 000.
|
|
|
446
446
|
|
|
447
447
|
Writes to a temporary file beside the target and renames it into place.
|
|
448
448
|
|
|
449
|
+
On Windows, a rename onto a target that another rename or a program holds
|
|
450
|
+
(EPERM, EBUSY, EACCES) is tried again for about half a second before the error
|
|
451
|
+
reaches the caller. A name the filesystem refuses — on Windows one over 255
|
|
452
|
+
characters or with `?`, `*`, `:` — throws EINVAL, not the ENOENT Windows
|
|
453
|
+
reports for it.
|
|
454
|
+
|
|
449
455
|
### `atomicWriteFileSync(path, content)`
|
|
450
456
|
|
|
451
457
|
The same, synchronously — for a small file that must be on disk before the
|
package/README.architecture.md
CHANGED
|
@@ -751,11 +751,23 @@ an mtime to the nearest millisecond, `Date.now()` truncates — so the scan
|
|
|
751
751
|
reuses the version before it, and a receiver authors that older version as an
|
|
752
752
|
edit, which reverts the writer's own copy everywhere.
|
|
753
753
|
|
|
754
|
+
**A file that refuses to open is kept — unless it is being deleted.** Windows
|
|
755
|
+
refuses an open while a program copies or holds a file (EBUSY, EPERM, EACCES).
|
|
756
|
+
The scan keeps the version it read last and reads the file again on the next
|
|
757
|
+
scan, so one held file does not stop the rest of the folder. The same EPERM
|
|
758
|
+
answers an open in the moment between a delete marking a file and its name
|
|
759
|
+
going, so a refusal is looked at again after `LOCK_RECHECK_MS` (10, 20 and
|
|
760
|
+
40 ms): a deleted file is gone by then, and the scan treats it as vanished —
|
|
761
|
+
a partial picture, which is not published — instead of keeping a version
|
|
762
|
+
nobody holds. Keeping it was field defect F6: the deletion was never authored,
|
|
763
|
+
and the next apply wrote the file back on its own node.
|
|
764
|
+
|
|
754
765
|
What no rule over `stat` can decide is a writer that pauses for a long time, and
|
|
755
766
|
`README.public.md` → "Known constraints" says what that costs.
|
|
756
767
|
|
|
757
|
-
**Tested by** `test/fs-clock-skew.spec.ts` (6) for the clock half
|
|
758
|
-
`test/fs-slow-copy.spec.ts` (3) for the deferral itself
|
|
768
|
+
**Tested by** `test/fs-clock-skew.spec.ts` (6) for the clock half,
|
|
769
|
+
`test/fs-slow-copy.spec.ts` (3) for the deferral itself, and
|
|
770
|
+
`test/fs-scanner-vanish.spec.ts` for the locked file and the one being deleted.
|
|
759
771
|
|
|
760
772
|
## Ignore Matching (`src/fs-ignore.ts`)
|
|
761
773
|
|
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ decision everything else follows from.
|
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
22
|
npm install @rljson/fs-agent
|
|
23
|
-
pnpm test #
|
|
23
|
+
pnpm test # 71 files, 823 scenarios, 100 % coverage, then eslint
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## Where to start
|
|
@@ -28,7 +28,7 @@ pnpm test # 70 files, 807 scenarios, 100 % coverage, then eslint
|
|
|
28
28
|
| if you are | read |
|
|
29
29
|
| --- | --- |
|
|
30
30
|
| **using the package** | [README.public.md](README.public.md) — install, the production configuration, every option and what it costs, troubleshooting |
|
|
31
|
-
| **deciding whether to trust it** | [README.tests.md](README.tests.md) — what the
|
|
31
|
+
| **deciding whether to trust it** | [README.tests.md](README.tests.md) — what the 823 scenarios actually prove, and what they cannot |
|
|
32
32
|
| **changing the sync** | [README.architecture.md](README.architecture.md) — the mechanism, and why each rule exists |
|
|
33
33
|
| **calling the API** | [README.api.md](README.api.md) — every export, grouped by module, with what it costs |
|
|
34
34
|
| **setting the repo up** | [README.contributors.md](README.contributors.md) — run, debug, build, publish |
|
package/README.tests.md
CHANGED
|
@@ -8,8 +8,8 @@ found in the LICENSE file in the root of this package.
|
|
|
8
8
|
|
|
9
9
|
# The test suite, as a document
|
|
10
10
|
|
|
11
|
-
**
|
|
12
|
-
lines.**
|
|
11
|
+
**71 files, 823 scenarios, 100 % coverage on statements, branches, functions and
|
|
12
|
+
lines.** 822 must pass; one is committed inverted, and §2.11 says which and why.
|
|
13
13
|
One command runs all of it:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
@@ -23,7 +23,7 @@ failures. Four different tests once failed
|
|
|
23
23
|
across four CI runs of the same commit, each passing on re-run — not four bugs,
|
|
24
24
|
one scheduler.
|
|
25
25
|
|
|
26
|
-
This document says what the suite actually proves, because the number
|
|
26
|
+
This document says what the suite actually proves, because the number 823 does
|
|
27
27
|
not. It is organised by the question answered rather than by file; the file
|
|
28
28
|
index is at the end.
|
|
29
29
|
|
|
@@ -296,7 +296,7 @@ visible files all vanished is refused however many it carries.
|
|
|
296
296
|
| `fs-filenames.spec.ts` (5) | a German filename round-tripping byte for byte, two Unicode spellings of one name not destroying each other, a case-only rename |
|
|
297
297
|
| `fs-hostile-tree.spec.ts` (7) | a tree from a peer is not trusted with paths — no escaping the sync root |
|
|
298
298
|
| `fs-filetype-changes.spec.ts` (6) | a path that changes what it IS: file → directory, directory → file, symlinks |
|
|
299
|
-
| `fs-scanner-vanish.spec.ts` (
|
|
299
|
+
| `fs-scanner-vanish.spec.ts` (17) | an entry deleted between `readdir` and `stat`, a locked file, and a file refused while it is being deleted |
|
|
300
300
|
| `fs-disk-full.spec.ts` (3) | ENOSPC — a clean abort with a message, not a silent standstill |
|
|
301
301
|
| `fs-slow-copy.spec.ts` (3) | a file still being written: never hashed half-done, and one unsettled file does not stop the folder |
|
|
302
302
|
| `fs-clock-skew.spec.ts` (6) | a machine whose clock is wrong — a file is visible whatever its timestamp says |
|
|
@@ -425,7 +425,7 @@ Open items are in
|
|
|
425
425
|
## 5. File index
|
|
426
426
|
|
|
427
427
|
Counts are what `pnpm test` reports per file, so suites that generate cases in a
|
|
428
|
-
loop are counted as they run. They sum to the
|
|
428
|
+
loop are counted as they run. They sum to the 823 above.
|
|
429
429
|
|
|
430
430
|
### Mesh tier
|
|
431
431
|
|
|
@@ -461,8 +461,8 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
461
461
|
| file | n | what it covers |
|
|
462
462
|
| --- | --- | --- |
|
|
463
463
|
| `fs-agent.spec.ts` | 81 | the agent's surface |
|
|
464
|
-
| `fs-scanner.spec.ts` |
|
|
465
|
-
| `fs-agent-degradation.spec.ts` |
|
|
464
|
+
| `fs-scanner.spec.ts` | 65 | scanning, watching, the scan cache, the agent's own writes |
|
|
465
|
+
| `fs-agent-degradation.spec.ts` | 16 | what the agent does when a dependency fails, and the shapes no scenario reaches |
|
|
466
466
|
| `fs-agent-receive-degradation.spec.ts` | 16 | receiving when a part of it fails — a tree it cannot read, a stamp or state it cannot file, a folder that moves under the apply, a join |
|
|
467
467
|
| `fs-receive.spec.ts` | 11 | what a node does with a head that arrived |
|
|
468
468
|
| `fs-relate.spec.ts` | 11 | same / behind / ahead / fork / incomplete |
|
|
@@ -479,7 +479,7 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
479
479
|
| `fs-agent-authorship.spec.ts` | 4 | a node claims only what it changed |
|
|
480
480
|
| `fs-divergence-is-content.spec.ts` | 2 | a divergence is a difference in CONTENT |
|
|
481
481
|
| `fs-ref-vs-content.spec.ts` | 6 | ref identity vs content identity |
|
|
482
|
-
| `fs-agent-concurrent-write.spec.ts` |
|
|
482
|
+
| `fs-agent-concurrent-write.spec.ts` | 17 | two writes of one path must not blend — 200 rounds per assertion — the synchronous writer the bookkeeping uses, and a rename that waits out a held file and names a refused name |
|
|
483
483
|
| `fs-agent-incremental-restore.spec.ts` | 8 | a restore writes only what changed |
|
|
484
484
|
| `fs-agent-locked-file.spec.ts` | 8 | a file held open by another process |
|
|
485
485
|
| `fs-agent-blob-stream.spec.ts` | 3 | a restore streams the blob |
|
|
@@ -492,7 +492,7 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
492
492
|
| `fs-blob-adapter.spec.ts` | 26 | file ↔ blob |
|
|
493
493
|
| `fs-db-adapter.spec.ts` | 6 | storing a tree with its predecessors |
|
|
494
494
|
| `blob-io.spec.ts` | 5 | storing a file as a blob |
|
|
495
|
-
| `fs-scanner-vanish.spec.ts` |
|
|
495
|
+
| `fs-scanner-vanish.spec.ts` | 17 | an entry that vanishes mid-scan, a locked file, one being deleted |
|
|
496
496
|
| `fs-scanner-scan-coalescing.spec.ts` | 4 | a burst collapses into at most two passes |
|
|
497
497
|
| `fs-hostile-tree.spec.ts` | 7 | a peer's tree is not trusted with paths |
|
|
498
498
|
| `fs-impossible-filename.spec.ts` | 4 | a path the filesystem refuses |
|
package/dist/README.api.md
CHANGED
|
@@ -446,6 +446,12 @@ produce bytes nobody wrote — measured at 1 520 blended pairs in 2 000.
|
|
|
446
446
|
|
|
447
447
|
Writes to a temporary file beside the target and renames it into place.
|
|
448
448
|
|
|
449
|
+
On Windows, a rename onto a target that another rename or a program holds
|
|
450
|
+
(EPERM, EBUSY, EACCES) is tried again for about half a second before the error
|
|
451
|
+
reaches the caller. A name the filesystem refuses — on Windows one over 255
|
|
452
|
+
characters or with `?`, `*`, `:` — throws EINVAL, not the ENOENT Windows
|
|
453
|
+
reports for it.
|
|
454
|
+
|
|
449
455
|
### `atomicWriteFileSync(path, content)`
|
|
450
456
|
|
|
451
457
|
The same, synchronously — for a small file that must be on disk before the
|
|
@@ -751,11 +751,23 @@ an mtime to the nearest millisecond, `Date.now()` truncates — so the scan
|
|
|
751
751
|
reuses the version before it, and a receiver authors that older version as an
|
|
752
752
|
edit, which reverts the writer's own copy everywhere.
|
|
753
753
|
|
|
754
|
+
**A file that refuses to open is kept — unless it is being deleted.** Windows
|
|
755
|
+
refuses an open while a program copies or holds a file (EBUSY, EPERM, EACCES).
|
|
756
|
+
The scan keeps the version it read last and reads the file again on the next
|
|
757
|
+
scan, so one held file does not stop the rest of the folder. The same EPERM
|
|
758
|
+
answers an open in the moment between a delete marking a file and its name
|
|
759
|
+
going, so a refusal is looked at again after `LOCK_RECHECK_MS` (10, 20 and
|
|
760
|
+
40 ms): a deleted file is gone by then, and the scan treats it as vanished —
|
|
761
|
+
a partial picture, which is not published — instead of keeping a version
|
|
762
|
+
nobody holds. Keeping it was field defect F6: the deletion was never authored,
|
|
763
|
+
and the next apply wrote the file back on its own node.
|
|
764
|
+
|
|
754
765
|
What no rule over `stat` can decide is a writer that pauses for a long time, and
|
|
755
766
|
`README.public.md` → "Known constraints" says what that costs.
|
|
756
767
|
|
|
757
|
-
**Tested by** `test/fs-clock-skew.spec.ts` (6) for the clock half
|
|
758
|
-
`test/fs-slow-copy.spec.ts` (3) for the deferral itself
|
|
768
|
+
**Tested by** `test/fs-clock-skew.spec.ts` (6) for the clock half,
|
|
769
|
+
`test/fs-slow-copy.spec.ts` (3) for the deferral itself, and
|
|
770
|
+
`test/fs-scanner-vanish.spec.ts` for the locked file and the one being deleted.
|
|
759
771
|
|
|
760
772
|
## Ignore Matching (`src/fs-ignore.ts`)
|
|
761
773
|
|
package/dist/README.md
CHANGED
|
@@ -20,7 +20,7 @@ decision everything else follows from.
|
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
22
|
npm install @rljson/fs-agent
|
|
23
|
-
pnpm test #
|
|
23
|
+
pnpm test # 71 files, 823 scenarios, 100 % coverage, then eslint
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## Where to start
|
|
@@ -28,7 +28,7 @@ pnpm test # 70 files, 807 scenarios, 100 % coverage, then eslint
|
|
|
28
28
|
| if you are | read |
|
|
29
29
|
| --- | --- |
|
|
30
30
|
| **using the package** | [README.public.md](README.public.md) — install, the production configuration, every option and what it costs, troubleshooting |
|
|
31
|
-
| **deciding whether to trust it** | [README.tests.md](README.tests.md) — what the
|
|
31
|
+
| **deciding whether to trust it** | [README.tests.md](README.tests.md) — what the 823 scenarios actually prove, and what they cannot |
|
|
32
32
|
| **changing the sync** | [README.architecture.md](README.architecture.md) — the mechanism, and why each rule exists |
|
|
33
33
|
| **calling the API** | [README.api.md](README.api.md) — every export, grouped by module, with what it costs |
|
|
34
34
|
| **setting the repo up** | [README.contributors.md](README.contributors.md) — run, debug, build, publish |
|
package/dist/README.tests.md
CHANGED
|
@@ -8,8 +8,8 @@ found in the LICENSE file in the root of this package.
|
|
|
8
8
|
|
|
9
9
|
# The test suite, as a document
|
|
10
10
|
|
|
11
|
-
**
|
|
12
|
-
lines.**
|
|
11
|
+
**71 files, 823 scenarios, 100 % coverage on statements, branches, functions and
|
|
12
|
+
lines.** 822 must pass; one is committed inverted, and §2.11 says which and why.
|
|
13
13
|
One command runs all of it:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
@@ -23,7 +23,7 @@ failures. Four different tests once failed
|
|
|
23
23
|
across four CI runs of the same commit, each passing on re-run — not four bugs,
|
|
24
24
|
one scheduler.
|
|
25
25
|
|
|
26
|
-
This document says what the suite actually proves, because the number
|
|
26
|
+
This document says what the suite actually proves, because the number 823 does
|
|
27
27
|
not. It is organised by the question answered rather than by file; the file
|
|
28
28
|
index is at the end.
|
|
29
29
|
|
|
@@ -296,7 +296,7 @@ visible files all vanished is refused however many it carries.
|
|
|
296
296
|
| `fs-filenames.spec.ts` (5) | a German filename round-tripping byte for byte, two Unicode spellings of one name not destroying each other, a case-only rename |
|
|
297
297
|
| `fs-hostile-tree.spec.ts` (7) | a tree from a peer is not trusted with paths — no escaping the sync root |
|
|
298
298
|
| `fs-filetype-changes.spec.ts` (6) | a path that changes what it IS: file → directory, directory → file, symlinks |
|
|
299
|
-
| `fs-scanner-vanish.spec.ts` (
|
|
299
|
+
| `fs-scanner-vanish.spec.ts` (17) | an entry deleted between `readdir` and `stat`, a locked file, and a file refused while it is being deleted |
|
|
300
300
|
| `fs-disk-full.spec.ts` (3) | ENOSPC — a clean abort with a message, not a silent standstill |
|
|
301
301
|
| `fs-slow-copy.spec.ts` (3) | a file still being written: never hashed half-done, and one unsettled file does not stop the folder |
|
|
302
302
|
| `fs-clock-skew.spec.ts` (6) | a machine whose clock is wrong — a file is visible whatever its timestamp says |
|
|
@@ -425,7 +425,7 @@ Open items are in
|
|
|
425
425
|
## 5. File index
|
|
426
426
|
|
|
427
427
|
Counts are what `pnpm test` reports per file, so suites that generate cases in a
|
|
428
|
-
loop are counted as they run. They sum to the
|
|
428
|
+
loop are counted as they run. They sum to the 823 above.
|
|
429
429
|
|
|
430
430
|
### Mesh tier
|
|
431
431
|
|
|
@@ -461,8 +461,8 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
461
461
|
| file | n | what it covers |
|
|
462
462
|
| --- | --- | --- |
|
|
463
463
|
| `fs-agent.spec.ts` | 81 | the agent's surface |
|
|
464
|
-
| `fs-scanner.spec.ts` |
|
|
465
|
-
| `fs-agent-degradation.spec.ts` |
|
|
464
|
+
| `fs-scanner.spec.ts` | 65 | scanning, watching, the scan cache, the agent's own writes |
|
|
465
|
+
| `fs-agent-degradation.spec.ts` | 16 | what the agent does when a dependency fails, and the shapes no scenario reaches |
|
|
466
466
|
| `fs-agent-receive-degradation.spec.ts` | 16 | receiving when a part of it fails — a tree it cannot read, a stamp or state it cannot file, a folder that moves under the apply, a join |
|
|
467
467
|
| `fs-receive.spec.ts` | 11 | what a node does with a head that arrived |
|
|
468
468
|
| `fs-relate.spec.ts` | 11 | same / behind / ahead / fork / incomplete |
|
|
@@ -479,7 +479,7 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
479
479
|
| `fs-agent-authorship.spec.ts` | 4 | a node claims only what it changed |
|
|
480
480
|
| `fs-divergence-is-content.spec.ts` | 2 | a divergence is a difference in CONTENT |
|
|
481
481
|
| `fs-ref-vs-content.spec.ts` | 6 | ref identity vs content identity |
|
|
482
|
-
| `fs-agent-concurrent-write.spec.ts` |
|
|
482
|
+
| `fs-agent-concurrent-write.spec.ts` | 17 | two writes of one path must not blend — 200 rounds per assertion — the synchronous writer the bookkeeping uses, and a rename that waits out a held file and names a refused name |
|
|
483
483
|
| `fs-agent-incremental-restore.spec.ts` | 8 | a restore writes only what changed |
|
|
484
484
|
| `fs-agent-locked-file.spec.ts` | 8 | a file held open by another process |
|
|
485
485
|
| `fs-agent-blob-stream.spec.ts` | 3 | a restore streams the blob |
|
|
@@ -492,7 +492,7 @@ loop are counted as they run. They sum to the 807 above.
|
|
|
492
492
|
| `fs-blob-adapter.spec.ts` | 26 | file ↔ blob |
|
|
493
493
|
| `fs-db-adapter.spec.ts` | 6 | storing a tree with its predecessors |
|
|
494
494
|
| `blob-io.spec.ts` | 5 | storing a file as a blob |
|
|
495
|
-
| `fs-scanner-vanish.spec.ts` |
|
|
495
|
+
| `fs-scanner-vanish.spec.ts` | 17 | an entry that vanishes mid-scan, a locked file, one being deleted |
|
|
496
496
|
| `fs-scanner-scan-coalescing.spec.ts` | 4 | a burst collapses into at most two passes |
|
|
497
497
|
| `fs-hostile-tree.spec.ts` | 7 | a peer's tree is not trusted with paths |
|
|
498
498
|
| `fs-impossible-filename.spec.ts` | 4 | a path the filesystem refuses |
|
package/dist/fs-agent.js
CHANGED
|
@@ -222,6 +222,74 @@ var atomicTmpPath = (filePath) => {
|
|
|
222
222
|
return join(dirname(filePath), `${ATOMIC_TMP_PREFIX}${rnd}`);
|
|
223
223
|
};
|
|
224
224
|
/**
|
|
225
|
+
* How often a rename onto a held file is tried again on this platform.
|
|
226
|
+
*
|
|
227
|
+
* Windows refuses to replace a file while another rename onto it is under way,
|
|
228
|
+
* or while a program holds it open, with EPERM, EBUSY or EACCES: two writes
|
|
229
|
+
* racing on one path then failed instead of one of them winning. Ten retries
|
|
230
|
+
* wait about half a second in all, long enough for a colliding rename and
|
|
231
|
+
* short enough that a real lock still reaches the caller soon. POSIX replaces
|
|
232
|
+
* the target regardless, so a refusal there is final and not retried.
|
|
233
|
+
* @returns The number of retries.
|
|
234
|
+
*/
|
|
235
|
+
var renameRetries = () => {
|
|
236
|
+
/* v8 ignore start -- @preserve Windows-only arm; CI runs on Linux */
|
|
237
|
+
return process.platform === "win32" ? 10 : 0;
|
|
238
|
+
/* v8 ignore stop -- @preserve */
|
|
239
|
+
};
|
|
240
|
+
var isHeld = (err) => {
|
|
241
|
+
const code = err?.code;
|
|
242
|
+
return code === "EPERM" || code === "EBUSY" || code === "EACCES";
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* The error for a rename the filesystem refused because of the destination
|
|
246
|
+
* NAME, coded EINVAL, the code of an invalid argument.
|
|
247
|
+
*
|
|
248
|
+
* Windows reports an invalid name — a component over 255 characters, `?`,
|
|
249
|
+
* `*`, `:` and the other reserved characters — as ENOENT, the code of a
|
|
250
|
+
* missing file. A staged file renamed within its own directory is not missing,
|
|
251
|
+
* though: when it and the directory both still exist after the refusal, the
|
|
252
|
+
* name is what was refused. Anything else stays the ENOENT it was.
|
|
253
|
+
* @param err - What the rename threw.
|
|
254
|
+
* @param from - The staged file.
|
|
255
|
+
* @param to - The destination.
|
|
256
|
+
* @returns The EINVAL error, or `undefined` when the name is not the cause.
|
|
257
|
+
*/
|
|
258
|
+
var nameRefused = async (err, from, to) => {
|
|
259
|
+
if (err?.code !== "ENOENT") return;
|
|
260
|
+
try {
|
|
261
|
+
await stat(from);
|
|
262
|
+
await stat(dirname(to));
|
|
263
|
+
} catch {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
return Object.assign(/* @__PURE__ */ new Error(`EINVAL: the filesystem refuses the name '${to}'`), {
|
|
267
|
+
code: "EINVAL",
|
|
268
|
+
syscall: "rename",
|
|
269
|
+
path: from,
|
|
270
|
+
dest: to,
|
|
271
|
+
cause: err
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Renames `from` onto `to`, trying again while the target is held.
|
|
276
|
+
* @param from - The staged file.
|
|
277
|
+
* @param to - The destination.
|
|
278
|
+
* @param retries - How often to try again after a held-file refusal.
|
|
279
|
+
* @param doRename - The rename to call.
|
|
280
|
+
*/
|
|
281
|
+
var renameOver = async (from, to, retries = renameRetries(), doRename = rename) => {
|
|
282
|
+
for (let attempt = 0;; attempt++) try {
|
|
283
|
+
return await doRename(from, to);
|
|
284
|
+
} catch (err) {
|
|
285
|
+
if (attempt < retries && isHeld(err)) {
|
|
286
|
+
await new Promise((r) => setTimeout(r, 10 * (attempt + 1)));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
throw await nameRefused(err, from, to) ?? err;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
225
293
|
* Writes bytes to `filePath` through a staging file.
|
|
226
294
|
*
|
|
227
295
|
* We do not `fsync` the temp: it adds significant per-file latency under
|
|
@@ -234,7 +302,7 @@ var atomicWriteFile = async (filePath, content) => {
|
|
|
234
302
|
const tmp = atomicTmpPath(filePath);
|
|
235
303
|
try {
|
|
236
304
|
await writeFile(tmp, content);
|
|
237
|
-
await
|
|
305
|
+
await renameOver(tmp, filePath);
|
|
238
306
|
} catch (err) {
|
|
239
307
|
try {
|
|
240
308
|
await unlink(tmp);
|
|
@@ -302,7 +370,7 @@ var atomicWriteStream = async (filePath, stream, wrapReadError) => {
|
|
|
302
370
|
}
|
|
303
371
|
await handle.close();
|
|
304
372
|
try {
|
|
305
|
-
await
|
|
373
|
+
await renameOver(tmp, filePath);
|
|
306
374
|
} catch (err) {
|
|
307
375
|
await unlink(tmp).catch(() => {});
|
|
308
376
|
throw err;
|
|
@@ -360,7 +428,7 @@ var FsBlobAdapter = class FsBlobAdapter {
|
|
|
360
428
|
await handle.close();
|
|
361
429
|
}
|
|
362
430
|
return {
|
|
363
|
-
name: filePath.split(
|
|
431
|
+
name: filePath.split(/[\\/]/).pop(),
|
|
364
432
|
blobId: blobProps.blobId,
|
|
365
433
|
size: stats.size,
|
|
366
434
|
mtime: stats.mtime.getTime(),
|
|
@@ -2018,6 +2086,23 @@ var STUCK_PAUSE_MS = 15e3;
|
|
|
2018
2086
|
/** How many of this process's own writes the scanner remembers. */
|
|
2019
2087
|
var OWN_WRITES_MAX = 1e4;
|
|
2020
2088
|
/**
|
|
2089
|
+
* The pauses, in ms, before the scanner opens a refused file again.
|
|
2090
|
+
*
|
|
2091
|
+
* Windows deletes a file in two steps: the deleting call opens the file, marks
|
|
2092
|
+
* it and closes it, and the name goes with the close. An open in between is
|
|
2093
|
+
* refused with EPERM, the code of a file another program holds. Taken for a
|
|
2094
|
+
* lock, the deleted file kept the version the last scan read — so its deletion
|
|
2095
|
+
* was never authored, and the next apply wrote it back (field defect F6). The
|
|
2096
|
+
* window lasts microseconds; a look after these pauses finds a deleted file
|
|
2097
|
+
* gone and a held one still refused. Seventy milliseconds in all, paid only
|
|
2098
|
+
* by a file that refuses.
|
|
2099
|
+
*/
|
|
2100
|
+
var LOCK_RECHECK_MS = [
|
|
2101
|
+
10,
|
|
2102
|
+
20,
|
|
2103
|
+
40
|
|
2104
|
+
];
|
|
2105
|
+
/**
|
|
2021
2106
|
* Scans and watches file system changes, extracting RLJSON tree structure
|
|
2022
2107
|
*/
|
|
2023
2108
|
var FsScanner = class FsScanner {
|
|
@@ -2295,7 +2380,7 @@ var FsScanner = class FsScanner {
|
|
|
2295
2380
|
else {
|
|
2296
2381
|
let handle;
|
|
2297
2382
|
try {
|
|
2298
|
-
handle = await
|
|
2383
|
+
handle = await FsScanner._openToRead(childPath);
|
|
2299
2384
|
} catch (error) {
|
|
2300
2385
|
if (FsScanner._isVanished(error)) throw error;
|
|
2301
2386
|
if (this._watcher !== null && FsScanner._isLocked(error)) {
|
|
@@ -2673,6 +2758,26 @@ var FsScanner = class FsScanner {
|
|
|
2673
2758
|
return code === "EBUSY" || code === "EPERM" || code === "EACCES";
|
|
2674
2759
|
}
|
|
2675
2760
|
/**
|
|
2761
|
+
* Opens a file to read it, and opens it again after each of
|
|
2762
|
+
* {@link LOCK_RECHECK_MS} while it is refused as held.
|
|
2763
|
+
*
|
|
2764
|
+
* A file refused because it is being deleted is gone at the next look, and
|
|
2765
|
+
* that open throws ENOENT: the scan then treats it as vanished, not as
|
|
2766
|
+
* locked. A file a program really holds is refused every time, and the last
|
|
2767
|
+
* refusal is thrown as before.
|
|
2768
|
+
* @param path - The file.
|
|
2769
|
+
* @returns The open handle.
|
|
2770
|
+
*/
|
|
2771
|
+
static async _openToRead(path) {
|
|
2772
|
+
for (const pause of LOCK_RECHECK_MS) try {
|
|
2773
|
+
return await open(path, "r");
|
|
2774
|
+
} catch (error) {
|
|
2775
|
+
if (!FsScanner._isLocked(error)) throw error;
|
|
2776
|
+
await new Promise((r) => setTimeout(r, pause));
|
|
2777
|
+
}
|
|
2778
|
+
return open(path, "r");
|
|
2779
|
+
}
|
|
2780
|
+
/**
|
|
2676
2781
|
* Reports a file that could not be opened, once until it can be again.
|
|
2677
2782
|
* @param relativePath - The file.
|
|
2678
2783
|
* @param err - Why it could not be opened.
|
|
@@ -18,6 +18,31 @@ export declare const ATOMIC_TMP_PREFIX = ".fsagent-tmp-";
|
|
|
18
18
|
* @returns A path to stage into.
|
|
19
19
|
*/
|
|
20
20
|
export declare const atomicTmpPath: (filePath: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* The pause before the first retry of a refused rename, in ms; each further
|
|
23
|
+
* pause is one step longer.
|
|
24
|
+
*/
|
|
25
|
+
export declare const RENAME_RETRY_STEP_MS = 10;
|
|
26
|
+
/**
|
|
27
|
+
* How often a rename onto a held file is tried again on this platform.
|
|
28
|
+
*
|
|
29
|
+
* Windows refuses to replace a file while another rename onto it is under way,
|
|
30
|
+
* or while a program holds it open, with EPERM, EBUSY or EACCES: two writes
|
|
31
|
+
* racing on one path then failed instead of one of them winning. Ten retries
|
|
32
|
+
* wait about half a second in all, long enough for a colliding rename and
|
|
33
|
+
* short enough that a real lock still reaches the caller soon. POSIX replaces
|
|
34
|
+
* the target regardless, so a refusal there is final and not retried.
|
|
35
|
+
* @returns The number of retries.
|
|
36
|
+
*/
|
|
37
|
+
export declare const renameRetries: () => number;
|
|
38
|
+
/**
|
|
39
|
+
* Renames `from` onto `to`, trying again while the target is held.
|
|
40
|
+
* @param from - The staged file.
|
|
41
|
+
* @param to - The destination.
|
|
42
|
+
* @param retries - How often to try again after a held-file refusal.
|
|
43
|
+
* @param doRename - The rename to call.
|
|
44
|
+
*/
|
|
45
|
+
export declare const renameOver: (from: string, to: string, retries?: number, doRename?: (from: string, to: string) => Promise<void>) => Promise<void>;
|
|
21
46
|
/**
|
|
22
47
|
* Writes bytes to `filePath` through a staging file.
|
|
23
48
|
*
|
package/dist/fs-scanner.d.ts
CHANGED
|
@@ -76,6 +76,19 @@ 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 pauses, in ms, before the scanner opens a refused file again.
|
|
81
|
+
*
|
|
82
|
+
* Windows deletes a file in two steps: the deleting call opens the file, marks
|
|
83
|
+
* it and closes it, and the name goes with the close. An open in between is
|
|
84
|
+
* refused with EPERM, the code of a file another program holds. Taken for a
|
|
85
|
+
* lock, the deleted file kept the version the last scan read — so its deletion
|
|
86
|
+
* was never authored, and the next apply wrote it back (field defect F6). The
|
|
87
|
+
* window lasts microseconds; a look after these pauses finds a deleted file
|
|
88
|
+
* gone and a held one still refused. Seventy milliseconds in all, paid only
|
|
89
|
+
* by a file that refuses.
|
|
90
|
+
*/
|
|
91
|
+
export declare const LOCK_RECHECK_MS: readonly number[];
|
|
79
92
|
/**
|
|
80
93
|
* The format of a persisted scan cache. A cache of any other version is
|
|
81
94
|
* discarded, and the next scan reads and stores every file once.
|
|
@@ -413,6 +426,18 @@ export declare class FsScanner {
|
|
|
413
426
|
* @returns True for EBUSY, EPERM and EACCES.
|
|
414
427
|
*/
|
|
415
428
|
private static _isLocked;
|
|
429
|
+
/**
|
|
430
|
+
* Opens a file to read it, and opens it again after each of
|
|
431
|
+
* {@link LOCK_RECHECK_MS} while it is refused as held.
|
|
432
|
+
*
|
|
433
|
+
* A file refused because it is being deleted is gone at the next look, and
|
|
434
|
+
* that open throws ENOENT: the scan then treats it as vanished, not as
|
|
435
|
+
* locked. A file a program really holds is refused every time, and the last
|
|
436
|
+
* refusal is thrown as before.
|
|
437
|
+
* @param path - The file.
|
|
438
|
+
* @returns The open handle.
|
|
439
|
+
*/
|
|
440
|
+
private static _openToRead;
|
|
416
441
|
/**
|
|
417
442
|
* Reports a file that could not be opened, once until it can be again.
|
|
418
443
|
* @param relativePath - The file.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The version of the `@rljson/fs-agent` package. */
|
|
2
|
-
export declare const fsAgentVersion = "0.2.
|
|
2
|
+
export declare const fsAgentVersion = "0.2.8";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rljson/fs-agent",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
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",
|
|
@@ -22,25 +22,25 @@
|
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@rljson/dna-rljson": "^0.3.2",
|
|
24
24
|
"@rljson/server": "0.2.0",
|
|
25
|
-
"@tssuite/helix-js": "0.
|
|
26
|
-
"@types/node": "^26.6.
|
|
27
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
28
|
-
"@typescript-eslint/parser": "^8.
|
|
29
|
-
"@vitest/coverage-v8": "^5.1.
|
|
25
|
+
"@tssuite/helix-js": "0.7.0",
|
|
26
|
+
"@types/node": "^26.6.11",
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^8.74.0",
|
|
28
|
+
"@typescript-eslint/parser": "^8.74.0",
|
|
29
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
30
30
|
"cross-env": "^10.1.0",
|
|
31
|
-
"eslint": "~10.
|
|
32
|
-
"eslint-plugin-jsdoc": "^65.
|
|
31
|
+
"eslint": "~10.15.0",
|
|
32
|
+
"eslint-plugin-jsdoc": "^65.5.5",
|
|
33
33
|
"eslint-plugin-tsdoc": "^0.5.4",
|
|
34
34
|
"globals": "^17.14.0",
|
|
35
35
|
"jsdoc": "^4.0.5",
|
|
36
36
|
"read-pkg": "^10.1.0",
|
|
37
37
|
"typescript": "~6.0.3",
|
|
38
|
-
"typescript-eslint": "^8.
|
|
39
|
-
"vite": "^8.3.
|
|
38
|
+
"typescript-eslint": "^8.74.0",
|
|
39
|
+
"vite": "^8.3.8",
|
|
40
40
|
"vite-node": "^6.0.0",
|
|
41
41
|
"vite-plugin-dts": "^5.1.2",
|
|
42
42
|
"vite-tsconfig-paths": "^6.1.1",
|
|
43
|
-
"vitest": "^5.1.
|
|
43
|
+
"vitest": "^5.1.2",
|
|
44
44
|
"vitest-dom": "^0.1.1"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|