zen-fs-config 0.9.6 → 0.9.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/dist/index.js CHANGED
@@ -1879,6 +1879,26 @@ function toBytes(raw) {
1879
1879
  if (typeof raw === "string") return new TextEncoder().encode(raw);
1880
1880
  return new Uint8Array(raw);
1881
1881
  }
1882
+ async function writeFileOverwrite(fs, path, data) {
1883
+ const bytes = toBytes(data);
1884
+ try {
1885
+ await fs.writeFile(path, bytes);
1886
+ return;
1887
+ } catch (err) {
1888
+ if (!isAlreadyExistsError(err)) throw err;
1889
+ }
1890
+ try {
1891
+ await fs.unlink(path);
1892
+ } catch {
1893
+ }
1894
+ await fs.writeFile(path, bytes);
1895
+ }
1896
+ function isAlreadyExistsError(err) {
1897
+ const e = err;
1898
+ if (!e) return false;
1899
+ if (e.code === "EEXIST") return true;
1900
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1901
+ }
1882
1902
  async function ensureParentDir2(fs, filePath) {
1883
1903
  const parts = filePath.split("/").filter(Boolean);
1884
1904
  parts.pop();
@@ -1909,7 +1929,7 @@ async function readText(fs, path) {
1909
1929
  }
1910
1930
  async function writeJson(fs, path, value) {
1911
1931
  await ensureParentDir2(fs, path);
1912
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1932
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1913
1933
  }
1914
1934
  async function existsSafe(fs, path) {
1915
1935
  try {
@@ -2187,14 +2207,14 @@ var TombstoneStore = class {
2187
2207
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2188
2208
  const p = this.pathFor(tombstone.path);
2189
2209
  await ensureParentDir2(this.fs, p);
2190
- await this.fs.writeFile(p, bytes);
2210
+ await writeFileOverwrite(this.fs, p, bytes);
2191
2211
  }
2192
2212
  /** Persist an updated tombstone (confirmations) when it still exists. */
2193
2213
  async save(tombstone) {
2194
2214
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2195
2215
  const p = this.pathFor(tombstone.path);
2196
2216
  if (!await existsSafe(this.fs, p)) return;
2197
- await this.fs.writeFile(p, bytes);
2217
+ await writeFileOverwrite(this.fs, p, bytes);
2198
2218
  }
2199
2219
  /** Delete a tombstone. */
2200
2220
  async remove(tombstone) {
@@ -3392,7 +3412,7 @@ var ConfigRepo = class _ConfigRepo {
3392
3412
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
3393
3413
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
3394
3414
  await this.ensureDir(tombstonePath);
3395
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
3415
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
3396
3416
  }
3397
3417
  /**
3398
3418
  * Delete a file and write a tombstone so the deletion propagates
package/dist/index.mjs CHANGED
@@ -1091,6 +1091,26 @@ function toBytes(raw) {
1091
1091
  if (typeof raw === "string") return new TextEncoder().encode(raw);
1092
1092
  return new Uint8Array(raw);
1093
1093
  }
1094
+ async function writeFileOverwrite(fs, path, data) {
1095
+ const bytes = toBytes(data);
1096
+ try {
1097
+ await fs.writeFile(path, bytes);
1098
+ return;
1099
+ } catch (err) {
1100
+ if (!isAlreadyExistsError(err)) throw err;
1101
+ }
1102
+ try {
1103
+ await fs.unlink(path);
1104
+ } catch {
1105
+ }
1106
+ await fs.writeFile(path, bytes);
1107
+ }
1108
+ function isAlreadyExistsError(err) {
1109
+ const e = err;
1110
+ if (!e) return false;
1111
+ if (e.code === "EEXIST") return true;
1112
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1113
+ }
1094
1114
  async function ensureParentDir2(fs, filePath) {
1095
1115
  const parts = filePath.split("/").filter(Boolean);
1096
1116
  parts.pop();
@@ -1121,7 +1141,7 @@ async function readText(fs, path) {
1121
1141
  }
1122
1142
  async function writeJson(fs, path, value) {
1123
1143
  await ensureParentDir2(fs, path);
1124
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1144
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1125
1145
  }
1126
1146
  async function existsSafe(fs, path) {
1127
1147
  try {
@@ -1399,14 +1419,14 @@ var TombstoneStore = class {
1399
1419
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1400
1420
  const p = this.pathFor(tombstone.path);
1401
1421
  await ensureParentDir2(this.fs, p);
1402
- await this.fs.writeFile(p, bytes);
1422
+ await writeFileOverwrite(this.fs, p, bytes);
1403
1423
  }
1404
1424
  /** Persist an updated tombstone (confirmations) when it still exists. */
1405
1425
  async save(tombstone) {
1406
1426
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1407
1427
  const p = this.pathFor(tombstone.path);
1408
1428
  if (!await existsSafe(this.fs, p)) return;
1409
- await this.fs.writeFile(p, bytes);
1429
+ await writeFileOverwrite(this.fs, p, bytes);
1410
1430
  }
1411
1431
  /** Delete a tombstone. */
1412
1432
  async remove(tombstone) {
@@ -2604,7 +2624,7 @@ var ConfigRepo = class _ConfigRepo {
2604
2624
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
2605
2625
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
2606
2626
  await this.ensureDir(tombstonePath);
2607
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2627
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
2608
2628
  }
2609
2629
  /**
2610
2630
  * Delete a file and write a tombstone so the deletion propagates
@@ -27845,6 +27845,26 @@ var ZenFSConfig = (() => {
27845
27845
  if (typeof raw === "string") return new TextEncoder().encode(raw);
27846
27846
  return new Uint8Array(raw);
27847
27847
  }
27848
+ async function writeFileOverwrite(fs, path, data) {
27849
+ const bytes = toBytes(data);
27850
+ try {
27851
+ await fs.writeFile(path, bytes);
27852
+ return;
27853
+ } catch (err2) {
27854
+ if (!isAlreadyExistsError(err2)) throw err2;
27855
+ }
27856
+ try {
27857
+ await fs.unlink(path);
27858
+ } catch {
27859
+ }
27860
+ await fs.writeFile(path, bytes);
27861
+ }
27862
+ function isAlreadyExistsError(err2) {
27863
+ const e = err2;
27864
+ if (!e) return false;
27865
+ if (e.code === "EEXIST") return true;
27866
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
27867
+ }
27848
27868
  async function ensureParentDir2(fs, filePath) {
27849
27869
  const parts = filePath.split("/").filter(Boolean);
27850
27870
  parts.pop();
@@ -27875,7 +27895,7 @@ var ZenFSConfig = (() => {
27875
27895
  }
27876
27896
  async function writeJson(fs, path, value) {
27877
27897
  await ensureParentDir2(fs, path);
27878
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27898
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27879
27899
  }
27880
27900
  async function existsSafe(fs, path) {
27881
27901
  try {
@@ -28151,14 +28171,14 @@ var ZenFSConfig = (() => {
28151
28171
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28152
28172
  const p = this.pathFor(tombstone.path);
28153
28173
  await ensureParentDir2(this.fs, p);
28154
- await this.fs.writeFile(p, bytes);
28174
+ await writeFileOverwrite(this.fs, p, bytes);
28155
28175
  }
28156
28176
  /** Persist an updated tombstone (confirmations) when it still exists. */
28157
28177
  async save(tombstone) {
28158
28178
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28159
28179
  const p = this.pathFor(tombstone.path);
28160
28180
  if (!await existsSafe(this.fs, p)) return;
28161
- await this.fs.writeFile(p, bytes);
28181
+ await writeFileOverwrite(this.fs, p, bytes);
28162
28182
  }
28163
28183
  /** Delete a tombstone. */
28164
28184
  async remove(tombstone) {
@@ -29341,7 +29361,7 @@ var ZenFSConfig = (() => {
29341
29361
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
29342
29362
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
29343
29363
  await this.ensureDir(tombstonePath);
29344
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
29364
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
29345
29365
  }
29346
29366
  /**
29347
29367
  * Delete a file and write a tombstone so the deletion propagates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",