@cardanvil/frame-kit 0.3.0 → 0.3.1

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.
@@ -5,7 +5,8 @@
5
5
  * written — Card Anvil re-reads a bundle the moment it changes. Writing in
6
6
  * place would hand it a truncated zip whenever the timing was unlucky, so
7
7
  * every output is written to a temp file and renamed into position, which is
8
- * atomic on every platform this runs on.
8
+ * atomic on every platform this runs on — see {@link renameWhenFree} for the
9
+ * one thing Windows asks for in return.
9
10
  */
10
11
  export declare function writeFileAtomic(file: string, data: string | Uint8Array): Promise<void>;
11
12
  /** Streams to `file` atomically. Returns the number of bytes written. */
@@ -2,6 +2,7 @@ import { createWriteStream } from "node:fs";
2
2
  import { copyFile, rename, rm, writeFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { pipeline } from "node:stream/promises";
5
+ import { setTimeout as delay } from "node:timers/promises";
5
6
  /**
6
7
  * A temp name beside the destination.
7
8
  *
@@ -15,6 +16,49 @@ function tempFor(file) {
15
16
  async function discard(temp) {
16
17
  await rm(temp, { force: true });
17
18
  }
19
+ /**
20
+ * What Windows reports when something else has the destination open.
21
+ *
22
+ * `EBUSY` and `EACCES` for the same situation on network shares and some
23
+ * antivirus filters.
24
+ */
25
+ const HELD_OPEN = new Set(["EPERM", "EACCES", "EBUSY"]);
26
+ /** Backoff between rename attempts. */
27
+ const RETRY_DELAYS_MS = [10, 20, 40, 80, 160, 320, 640, 1000];
28
+ const TOTAL_WAIT_MS = RETRY_DELAYS_MS.reduce((a, b) => a + b, 0);
29
+ /**
30
+ * Renames, waiting out anything that has the destination open.
31
+ *
32
+ * POSIX lets a file be replaced while it is being read. Windows does not: a
33
+ * rename onto a path some process holds open fails outright. A linked folder
34
+ * exists precisely to be read — Card Anvil re-reads a bundle whenever it
35
+ * changes — so a rebuild that lands mid-read hits this, and so do antivirus
36
+ * and search indexers, which open files briefly on their own schedule.
37
+ *
38
+ * Waiting is the whole fix: whatever holds the file is finishing a read that
39
+ * takes milliseconds. A real permission problem still fails, two seconds later.
40
+ */
41
+ async function renameWhenFree(temp, file) {
42
+ for (let attempt = 0;; attempt++) {
43
+ try {
44
+ await rename(temp, file);
45
+ return;
46
+ }
47
+ catch (cause) {
48
+ const code = cause.code ?? "";
49
+ if (!HELD_OPEN.has(code)) {
50
+ throw cause;
51
+ }
52
+ const wait = RETRY_DELAYS_MS[attempt];
53
+ if (wait === undefined) {
54
+ // `EPERM: operation not permitted, rename` says nothing about what to
55
+ // do. Whatever is holding the file is the thing to go and close.
56
+ throw new Error(`could not replace ${path.basename(file)}: something still has it open after ${String(TOTAL_WAIT_MS)}ms (${code}). On Windows a file cannot be replaced while another process holds it — close whatever is reading it, or point the build somewhere else.`, { cause });
57
+ }
58
+ await delay(wait);
59
+ }
60
+ }
61
+ }
18
62
  /**
19
63
  * Writes `file` so that a reader never sees a partial one.
20
64
  *
@@ -22,13 +66,14 @@ async function discard(temp) {
22
66
  * written — Card Anvil re-reads a bundle the moment it changes. Writing in
23
67
  * place would hand it a truncated zip whenever the timing was unlucky, so
24
68
  * every output is written to a temp file and renamed into position, which is
25
- * atomic on every platform this runs on.
69
+ * atomic on every platform this runs on — see {@link renameWhenFree} for the
70
+ * one thing Windows asks for in return.
26
71
  */
27
72
  export async function writeFileAtomic(file, data) {
28
73
  const temp = tempFor(file);
29
74
  try {
30
75
  await writeFile(temp, data);
31
- await rename(temp, file);
76
+ await renameWhenFree(temp, file);
32
77
  }
33
78
  catch (cause) {
34
79
  await discard(temp);
@@ -47,7 +92,7 @@ stream) {
47
92
  });
48
93
  try {
49
94
  await pipeline(stream, createWriteStream(temp));
50
- await rename(temp, file);
95
+ await renameWhenFree(temp, file);
51
96
  return bytes;
52
97
  }
53
98
  catch (cause) {
@@ -60,7 +105,7 @@ export async function copyFileAtomic(source, dest) {
60
105
  const temp = tempFor(dest);
61
106
  try {
62
107
  await copyFile(source, temp);
63
- await rename(temp, dest);
108
+ await renameWhenFree(temp, dest);
64
109
  }
65
110
  catch (cause) {
66
111
  await discard(temp);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cardanvil/frame-kit",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "The contract Card Anvil frames are authored against: schemas, types, authoring helpers, and the distributable manifest format.",
5
5
  "license": "MIT",
6
6
  "type": "module",