@zukhruf/fs 0.6.14 → 0.6.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -10
- package/dist/atomic-write.d.ts +3 -1
- package/dist/atomic-write.d.ts.map +1 -1
- package/dist/atomic-write.js +3 -3
- package/dist/atomic-write.js.map +1 -1
- package/dist/create-exclusive.d.ts +4 -1
- package/dist/create-exclusive.d.ts.map +1 -1
- package/dist/create-exclusive.js +5 -5
- package/dist/create-exclusive.js.map +1 -1
- package/dist/durable-write.d.ts +3 -1
- package/dist/durable-write.d.ts.map +1 -1
- package/dist/durable-write.js +3 -3
- package/dist/durable-write.js.map +1 -1
- package/dist/patiently.d.ts +9 -2
- package/dist/patiently.d.ts.map +1 -1
- package/dist/patiently.js +13 -6
- package/dist/patiently.js.map +1 -1
- package/dist/replace-file.d.ts +2 -1
- package/dist/replace-file.d.ts.map +1 -1
- package/dist/replace-file.js +2 -2
- package/dist/replace-file.js.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -11,7 +11,9 @@ A write that changes a file in place can stop halfway. Then a reader sees a part
|
|
|
11
11
|
```ts
|
|
12
12
|
import { atomicWrite } from '@zukhruf/fs';
|
|
13
13
|
|
|
14
|
-
await atomicWrite('/var/lib/app/queue', 'first\nsecond\n'
|
|
14
|
+
await atomicWrite('/var/lib/app/queue', 'first\nsecond\n', {
|
|
15
|
+
clock: globalThis,
|
|
16
|
+
});
|
|
15
17
|
```
|
|
16
18
|
|
|
17
19
|
## Make the replacement durable
|
|
@@ -25,7 +27,7 @@ After a rename, the operating system can keep the new content in memory for some
|
|
|
25
27
|
```ts
|
|
26
28
|
import { durableWrite } from '@zukhruf/fs';
|
|
27
29
|
|
|
28
|
-
await durableWrite('/var/lib/app/counter', '42');
|
|
30
|
+
await durableWrite('/var/lib/app/counter', '42', { clock: globalThis });
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
Use `durableWrite` when a value must never go back after a crash, for example a counter that gives out fencing tokens. A counter that goes back gives the same token two times.
|
|
@@ -37,10 +39,12 @@ On Windows, a process cannot open a directory to sync it. Some file systems refu
|
|
|
37
39
|
`atomicWrite` and `durableWrite` take the same arguments:
|
|
38
40
|
|
|
39
41
|
```ts
|
|
40
|
-
atomicWrite(path: string, content: string | Uint8Array, options
|
|
41
|
-
durableWrite(path: string, content: string | Uint8Array, options
|
|
42
|
+
atomicWrite(path: string, content: string | Uint8Array, options: { clock: Clock; mode?: number })
|
|
43
|
+
durableWrite(path: string, content: string | Uint8Array, options: { clock: Clock; mode?: number })
|
|
42
44
|
```
|
|
43
45
|
|
|
46
|
+
`clock` is the clock that the waits of the write run on. Give `globalThis` in production, and a fake clock in a test. A write waits only on Windows, between two tries of a rename that Windows refuses for now (see "Try again while Windows refuses a file for a moment"). `Clock` is the type of `@zukhruf/async`.
|
|
47
|
+
|
|
44
48
|
The content is a string or bytes. A `Buffer` is a `Uint8Array`, so a `Buffer` is bytes too. The write puts the bytes at the path with no change.
|
|
45
49
|
|
|
46
50
|
`mode` sets the POSIX file mode of the new file, for example `0o600` for a file that only its owner can read. The process umask applies to `mode`, as it does for `writeFile`. The write sets `mode` when it creates the draft, because the rename keeps the mode of the draft. Thus the file at the path never has a different mode, not even for a moment. When you do not give `mode`, the file gets the mode that `writeFile` gives a new file. A write never keeps the mode of the file that it replaces.
|
|
@@ -50,7 +54,10 @@ import { randomBytes } from 'node:crypto';
|
|
|
50
54
|
|
|
51
55
|
import { atomicWrite } from '@zukhruf/fs';
|
|
52
56
|
|
|
53
|
-
await atomicWrite('/var/lib/app/key', randomBytes(32), {
|
|
57
|
+
await atomicWrite('/var/lib/app/key', randomBytes(32), {
|
|
58
|
+
clock: globalThis,
|
|
59
|
+
mode: 0o600,
|
|
60
|
+
});
|
|
54
61
|
```
|
|
55
62
|
|
|
56
63
|
On Windows, a file has no POSIX mode. Node.js uses only the write bit of `mode` there, to make the file read-only.
|
|
@@ -62,7 +69,9 @@ On Windows, a file has no POSIX mode. Node.js uses only the write bit of `mode`
|
|
|
62
69
|
```ts
|
|
63
70
|
import { createExclusive } from '@zukhruf/fs';
|
|
64
71
|
|
|
65
|
-
const created = await createExclusive('/var/lib/app/job.lock', 'pid 4242'
|
|
72
|
+
const created = await createExclusive('/var/lib/app/job.lock', 'pid 4242', {
|
|
73
|
+
clock: globalThis,
|
|
74
|
+
});
|
|
66
75
|
if (!created) console.log('Another process holds the job.');
|
|
67
76
|
```
|
|
68
77
|
|
|
@@ -84,7 +93,9 @@ import { join } from 'node:path';
|
|
|
84
93
|
import { createExclusive, draftSuffixLength, safeFileName } from '@zukhruf/fs';
|
|
85
94
|
|
|
86
95
|
const name = safeFileName('orders/2026', '.lock'.length + draftSuffixLength);
|
|
87
|
-
await createExclusive(join('/var/lib/app/locks', `${name}.lock`), 'pid 4242'
|
|
96
|
+
await createExclusive(join('/var/lib/app/locks', `${name}.lock`), 'pid 4242', {
|
|
97
|
+
clock: globalThis,
|
|
98
|
+
});
|
|
88
99
|
// The file is orders%2F2026.lock
|
|
89
100
|
```
|
|
90
101
|
|
|
@@ -102,12 +113,15 @@ import { readFile } from 'node:fs/promises';
|
|
|
102
113
|
|
|
103
114
|
import { patiently } from '@zukhruf/fs';
|
|
104
115
|
|
|
105
|
-
const content = await patiently(
|
|
106
|
-
readFile('/var/lib/app/job.lock', 'utf8'),
|
|
116
|
+
const content = await patiently(
|
|
117
|
+
() => readFile('/var/lib/app/job.lock', 'utf8'),
|
|
118
|
+
{ clock: globalThis },
|
|
107
119
|
);
|
|
108
120
|
```
|
|
109
121
|
|
|
110
|
-
Each
|
|
122
|
+
Each wait between two tries runs on the clock that you give. The 1 second is a time on that clock too. A test gives a fake clock and moves the time, so that the test does not wait. See [ADR 0005](./docs/adr/0005-a-write-waits-on-the-clock-that-the-caller-gives.md).
|
|
123
|
+
|
|
124
|
+
Each write of this package uses `patiently` for its rename or link, with the clock of the write.
|
|
111
125
|
|
|
112
126
|
## Lock a file
|
|
113
127
|
|
package/dist/atomic-write.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
1
2
|
/**
|
|
2
3
|
* Replaces `path` in one step, so readers see the old or the new content, never a partial write.
|
|
3
4
|
* `mode` is set when the draft is created: the rename keeps the draft's mode,
|
|
4
5
|
* so the file never has another mode at the path.
|
|
5
6
|
*/
|
|
6
|
-
export declare function atomicWrite(path: string, content: string | Uint8Array,
|
|
7
|
+
export declare function atomicWrite(path: string, content: string | Uint8Array, { clock, mode }: {
|
|
8
|
+
clock: Clock;
|
|
7
9
|
mode?: number;
|
|
8
10
|
}): Promise<void>;
|
|
9
11
|
//# sourceMappingURL=atomic-write.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atomic-write.d.ts","sourceRoot":"","sources":["../src/atomic-write.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"atomic-write.d.ts","sourceRoot":"","sources":["../src/atomic-write.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAK5C;;;;GAIG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,iBAWjD"}
|
package/dist/atomic-write.js
CHANGED
|
@@ -6,11 +6,11 @@ import { replaceFile } from "./replace-file.js";
|
|
|
6
6
|
* `mode` is set when the draft is created: the rename keeps the draft's mode,
|
|
7
7
|
* so the file never has another mode at the path.
|
|
8
8
|
*/
|
|
9
|
-
export async function atomicWrite(path, content,
|
|
9
|
+
export async function atomicWrite(path, content, { clock, mode }) {
|
|
10
10
|
const draft = draftOf(path);
|
|
11
11
|
try {
|
|
12
|
-
await writeFile(draft, content, { mode
|
|
13
|
-
await replaceFile(draft, path);
|
|
12
|
+
await writeFile(draft, content, { mode });
|
|
13
|
+
await replaceFile(draft, path, clock);
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
16
|
// A draft that never replaced `path` would only pile up, one per failed write.
|
package/dist/atomic-write.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atomic-write.js","sourceRoot":"","sources":["../src/atomic-write.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"atomic-write.js","sourceRoot":"","sources":["../src/atomic-write.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAIjD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,IAAY,EACZ,OAA4B,EAC5B,EAAE,KAAK,EAAE,IAAI,EAAmC;IAEhD,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,CAAC;QACH,MAAM,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1C,MAAM,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,+EAA+E;QAC/E,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
1
2
|
/**
|
|
2
3
|
* Creates `path` holding `content` unless it already exists. Linking a fully
|
|
3
4
|
* written file makes creation atomic, so the file is never seen empty.
|
|
4
5
|
*/
|
|
5
|
-
export declare function createExclusive(path: string, content: string
|
|
6
|
+
export declare function createExclusive(path: string, content: string, { clock }: {
|
|
7
|
+
clock: Clock;
|
|
8
|
+
}): Promise<boolean>;
|
|
6
9
|
//# sourceMappingURL=create-exclusive.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-exclusive.d.ts","sourceRoot":"","sources":["../src/create-exclusive.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-exclusive.d.ts","sourceRoot":"","sources":["../src/create-exclusive.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAM5C;;;GAGG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,EAAE,KAAK,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,oBAa5B"}
|
package/dist/create-exclusive.js
CHANGED
|
@@ -6,11 +6,11 @@ import { patiently } from "./patiently.js";
|
|
|
6
6
|
* Creates `path` holding `content` unless it already exists. Linking a fully
|
|
7
7
|
* written file makes creation atomic, so the file is never seen empty.
|
|
8
8
|
*/
|
|
9
|
-
export async function createExclusive(path, content) {
|
|
9
|
+
export async function createExclusive(path, content, { clock }) {
|
|
10
10
|
const draft = draftOf(path);
|
|
11
11
|
try {
|
|
12
12
|
await writeFile(draft, content);
|
|
13
|
-
await patiently(() => link(draft, path));
|
|
13
|
+
await patiently(() => link(draft, path), { clock });
|
|
14
14
|
return true;
|
|
15
15
|
}
|
|
16
16
|
catch (error) {
|
|
@@ -19,11 +19,11 @@ export async function createExclusive(path, content) {
|
|
|
19
19
|
throw error;
|
|
20
20
|
}
|
|
21
21
|
finally {
|
|
22
|
-
await removeDraft(draft);
|
|
22
|
+
await removeDraft(draft, clock);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
/** A draft left behind has a unique name, so it never blocks a key; failing to remove it must not change whether `path` was created. */
|
|
26
|
-
async function removeDraft(draft) {
|
|
27
|
-
await patiently(() => unlink(draft)).catch(() => { });
|
|
26
|
+
async function removeDraft(draft, clock) {
|
|
27
|
+
await patiently(() => unlink(draft), { clock }).catch(() => { });
|
|
28
28
|
}
|
|
29
29
|
//# sourceMappingURL=create-exclusive.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-exclusive.js","sourceRoot":"","sources":["../src/create-exclusive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"create-exclusive.js","sourceRoot":"","sources":["../src/create-exclusive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAI3D,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAAY,EACZ,OAAe,EACf,EAAE,KAAK,EAAoB;IAE3B,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,CAAC;QACH,MAAM,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAChC,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACpD,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,OAAO,KAAK,CAAC;QAC3C,MAAM,KAAK,CAAC;IACd,CAAC;YAAS,CAAC;QACT,MAAM,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAClC,CAAC;AACH,CAAC;AAED,wIAAwI;AACxI,KAAK,UAAU,WAAW,CAAC,KAAa,EAAE,KAAY;IACpD,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AAClE,CAAC"}
|
package/dist/durable-write.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
1
2
|
/**
|
|
2
3
|
* Replaces `path` in one step like `atomicWrite`, and returns only once both
|
|
3
4
|
* the new content and the replacement survive a power loss: a counter that
|
|
4
5
|
* goes back after a crash would hand out a token twice.
|
|
5
6
|
*/
|
|
6
|
-
export declare function durableWrite(path: string, content: string | Uint8Array,
|
|
7
|
+
export declare function durableWrite(path: string, content: string | Uint8Array, { clock, mode }: {
|
|
8
|
+
clock: Clock;
|
|
7
9
|
mode?: number;
|
|
8
10
|
}): Promise<void>;
|
|
9
11
|
//# sourceMappingURL=durable-write.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-write.d.ts","sourceRoot":"","sources":["../src/durable-write.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"durable-write.d.ts","sourceRoot":"","sources":["../src/durable-write.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAM5C;;;;GAIG;AACH,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GAAG,UAAU,EAC5B,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,iBAajD"}
|
package/dist/durable-write.js
CHANGED
|
@@ -8,12 +8,12 @@ import { replaceFile } from "./replace-file.js";
|
|
|
8
8
|
* the new content and the replacement survive a power loss: a counter that
|
|
9
9
|
* goes back after a crash would hand out a token twice.
|
|
10
10
|
*/
|
|
11
|
-
export async function durableWrite(path, content,
|
|
11
|
+
export async function durableWrite(path, content, { clock, mode }) {
|
|
12
12
|
const draft = draftOf(path);
|
|
13
13
|
try {
|
|
14
14
|
// The rename keeps the draft's mode, so `mode` is set when the draft is created.
|
|
15
|
-
await writeSynced(draft, content,
|
|
16
|
-
await replaceFile(draft, path);
|
|
15
|
+
await writeSynced(draft, content, mode);
|
|
16
|
+
await replaceFile(draft, path, clock);
|
|
17
17
|
}
|
|
18
18
|
catch (error) {
|
|
19
19
|
// A draft that never replaced `path` would only pile up, one per failed write.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-write.js","sourceRoot":"","sources":["../src/durable-write.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"durable-write.js","sourceRoot":"","sources":["../src/durable-write.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,IAAY,EACZ,OAA4B,EAC5B,EAAE,KAAK,EAAE,IAAI,EAAmC;IAEhD,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,CAAC;QACH,iFAAiF;QACjF,MAAM,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QACxC,MAAM,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,+EAA+E;QAC/E,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,KAAK,CAAC;IACd,CAAC;IACD,MAAM,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;AACrC,CAAC;AAED,KAAK,UAAU,WAAW,CACxB,IAAY,EACZ,OAA4B,EAC5B,IAAwB;IAExB,YAAY,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAClD,MAAM,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAChC,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,aAAa,CAAC,SAAiB;IAC5C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO;QAAE,OAAO;IACzC,YAAY,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAChD,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC;YAAE,MAAM,KAAK,CAAC;IAC3E,CAAC;AACH,CAAC"}
|
package/dist/patiently.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { type Clock } from '@zukhruf/async';
|
|
2
|
+
/**
|
|
3
|
+
* Runs `operation`, and runs it again while Windows refuses the file for a
|
|
4
|
+
* moment; after that, the error reaches the caller. Each wait, and the time
|
|
5
|
+
* that it allows, run on the clock.
|
|
6
|
+
*/
|
|
7
|
+
export declare function patiently<T>(operation: () => Promise<T>, { clock }: {
|
|
8
|
+
clock: Clock;
|
|
9
|
+
}): Promise<T>;
|
|
3
10
|
//# sourceMappingURL=patiently.d.ts.map
|
package/dist/patiently.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"patiently.d.ts","sourceRoot":"","sources":["../src/patiently.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"patiently.d.ts","sourceRoot":"","sources":["../src/patiently.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAS,MAAM,gBAAgB,CAAC;AAoBnD;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,EAAE,KAAK,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,GAC1B,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
|
package/dist/patiently.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { delay } from '@zukhruf/async';
|
|
2
2
|
import { isErrno } from "./errno.js";
|
|
3
3
|
/** How long to retry a file operation that Windows refuses for a moment. */
|
|
4
4
|
const WINDOWS_PATIENCE = 1000;
|
|
@@ -12,18 +12,25 @@ function isRefusedForNow(error) {
|
|
|
12
12
|
return (process.platform === 'win32' &&
|
|
13
13
|
['EPERM', 'EACCES', 'EBUSY'].some((code) => isErrno(error, code)));
|
|
14
14
|
}
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Runs `operation`, and runs it again while Windows refuses the file for a
|
|
17
|
+
* moment; after that, the error reaches the caller. Each wait, and the time
|
|
18
|
+
* that it allows, run on the clock.
|
|
19
|
+
*/
|
|
20
|
+
export async function patiently(operation, { clock }) {
|
|
21
|
+
const started = clock.performance.now();
|
|
18
22
|
for (let attempt = 0;; attempt++) {
|
|
19
23
|
try {
|
|
20
24
|
return await operation();
|
|
21
25
|
}
|
|
22
26
|
catch (error) {
|
|
23
27
|
if (!isRefusedForNow(error) ||
|
|
24
|
-
performance.now() - started > WINDOWS_PATIENCE)
|
|
28
|
+
clock.performance.now() - started > WINDOWS_PATIENCE)
|
|
25
29
|
throw error;
|
|
26
|
-
await delay(Math.min(5 * 2 ** attempt, 100)
|
|
30
|
+
await delay(Math.min(5 * 2 ** attempt, 100), {
|
|
31
|
+
clock,
|
|
32
|
+
signal: undefined,
|
|
33
|
+
});
|
|
27
34
|
}
|
|
28
35
|
}
|
|
29
36
|
}
|
package/dist/patiently.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"patiently.js","sourceRoot":"","sources":["../src/patiently.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"patiently.js","sourceRoot":"","sources":["../src/patiently.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,4EAA4E;AAC5E,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAE9B;;;;;GAKG;AACH,SAAS,eAAe,CAAC,KAAc;IACrC,OAAO,CACL,OAAO,CAAC,QAAQ,KAAK,OAAO;QAC5B,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAClE,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,SAA2B,EAC3B,EAAE,KAAK,EAAoB;IAE3B,MAAM,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;IACxC,KAAK,IAAI,OAAO,GAAG,CAAC,GAAI,OAAO,EAAE,EAAE,CAAC;QAClC,IAAI,CAAC;YACH,OAAO,MAAM,SAAS,EAAE,CAAC;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IACE,CAAC,eAAe,CAAC,KAAK,CAAC;gBACvB,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,OAAO,GAAG,gBAAgB;gBAEpD,MAAM,KAAK,CAAC;YACd,MAAM,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,OAAO,EAAE,GAAG,CAAC,EAAE;gBAC3C,KAAK;gBACL,MAAM,EAAE,SAAS;aAClB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/replace-file.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
1
2
|
/**
|
|
2
3
|
* Moves `draft` over `path` in one step. On Windows, a rename over a file
|
|
3
4
|
* fails while another process has that file open, for example a waiter that
|
|
4
5
|
* reads it at that moment. The refusal is brief, so the rename is tried again;
|
|
5
6
|
* it stays the single commit point.
|
|
6
7
|
*/
|
|
7
|
-
export declare function replaceFile(draft: string, path: string): Promise<void>;
|
|
8
|
+
export declare function replaceFile(draft: string, path: string, clock: Clock): Promise<void>;
|
|
8
9
|
//# sourceMappingURL=replace-file.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replace-file.d.ts","sourceRoot":"","sources":["../src/replace-file.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"replace-file.d.ts","sourceRoot":"","sources":["../src/replace-file.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAI5C;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,iBAEpE"}
|
package/dist/replace-file.js
CHANGED
|
@@ -6,7 +6,7 @@ import { patiently } from "./patiently.js";
|
|
|
6
6
|
* reads it at that moment. The refusal is brief, so the rename is tried again;
|
|
7
7
|
* it stays the single commit point.
|
|
8
8
|
*/
|
|
9
|
-
export function replaceFile(draft, path) {
|
|
10
|
-
return patiently(() => rename(draft, path));
|
|
9
|
+
export function replaceFile(draft, path, clock) {
|
|
10
|
+
return patiently(() => rename(draft, path), { clock });
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=replace-file.js.map
|
package/dist/replace-file.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replace-file.js","sourceRoot":"","sources":["../src/replace-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"replace-file.js","sourceRoot":"","sources":["../src/replace-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI1C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,IAAY,EAAE,KAAY;IACnE,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AACzD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/fs",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.16",
|
|
4
4
|
"description": "File writes a reader never sees half done, a file lock the kernel frees when its holder dies, and a file stamp that tells that a file changed, as git does. Retries Windows' short refusals. Names a file after any key.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -49,7 +49,10 @@
|
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@zukhruf/async": "^0.6.16"
|
|
54
|
+
},
|
|
52
55
|
"devDependencies": {
|
|
53
|
-
"@zukhruf/testing": "^0.6.
|
|
56
|
+
"@zukhruf/testing": "^0.6.16"
|
|
54
57
|
}
|
|
55
58
|
}
|