zen-fs-config 0.9.6 → 0.9.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/dist/index.js CHANGED
@@ -1879,6 +1879,34 @@ 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, options = {}) {
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
+ try {
1895
+ await fs.writeFile(path, bytes);
1896
+ } catch (err) {
1897
+ if (isAlreadyExistsError(err) && options.ignorePersistentEexist) {
1898
+ log3.warn(`[zen-fs-config] writeFileOverwrite: \u540C\u540D\u6587\u4EF6\u65E0\u6CD5\u8986\u76D6\uFF0C\u6309\u5DF2\u5B58\u5728\u5904\u7406 ${path}`);
1899
+ return;
1900
+ }
1901
+ throw err;
1902
+ }
1903
+ }
1904
+ function isAlreadyExistsError(err) {
1905
+ const e = err;
1906
+ if (!e) return false;
1907
+ if (e.code === "EEXIST") return true;
1908
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1909
+ }
1882
1910
  async function ensureParentDir2(fs, filePath) {
1883
1911
  const parts = filePath.split("/").filter(Boolean);
1884
1912
  parts.pop();
@@ -1909,7 +1937,7 @@ async function readText(fs, path) {
1909
1937
  }
1910
1938
  async function writeJson(fs, path, value) {
1911
1939
  await ensureParentDir2(fs, path);
1912
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1940
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1913
1941
  }
1914
1942
  async function existsSafe(fs, path) {
1915
1943
  try {
@@ -2187,14 +2215,14 @@ var TombstoneStore = class {
2187
2215
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2188
2216
  const p = this.pathFor(tombstone.path);
2189
2217
  await ensureParentDir2(this.fs, p);
2190
- await this.fs.writeFile(p, bytes);
2218
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
2191
2219
  }
2192
2220
  /** Persist an updated tombstone (confirmations) when it still exists. */
2193
2221
  async save(tombstone) {
2194
2222
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2195
2223
  const p = this.pathFor(tombstone.path);
2196
2224
  if (!await existsSafe(this.fs, p)) return;
2197
- await this.fs.writeFile(p, bytes);
2225
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
2198
2226
  }
2199
2227
  /** Delete a tombstone. */
2200
2228
  async remove(tombstone) {
@@ -3392,7 +3420,7 @@ var ConfigRepo = class _ConfigRepo {
3392
3420
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
3393
3421
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
3394
3422
  await this.ensureDir(tombstonePath);
3395
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
3423
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
3396
3424
  }
3397
3425
  /**
3398
3426
  * Delete a file and write a tombstone so the deletion propagates
package/dist/index.mjs CHANGED
@@ -1091,6 +1091,34 @@ 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, options = {}) {
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
+ try {
1107
+ await fs.writeFile(path, bytes);
1108
+ } catch (err) {
1109
+ if (isAlreadyExistsError(err) && options.ignorePersistentEexist) {
1110
+ log3.warn(`[zen-fs-config] writeFileOverwrite: \u540C\u540D\u6587\u4EF6\u65E0\u6CD5\u8986\u76D6\uFF0C\u6309\u5DF2\u5B58\u5728\u5904\u7406 ${path}`);
1111
+ return;
1112
+ }
1113
+ throw err;
1114
+ }
1115
+ }
1116
+ function isAlreadyExistsError(err) {
1117
+ const e = err;
1118
+ if (!e) return false;
1119
+ if (e.code === "EEXIST") return true;
1120
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1121
+ }
1094
1122
  async function ensureParentDir2(fs, filePath) {
1095
1123
  const parts = filePath.split("/").filter(Boolean);
1096
1124
  parts.pop();
@@ -1121,7 +1149,7 @@ async function readText(fs, path) {
1121
1149
  }
1122
1150
  async function writeJson(fs, path, value) {
1123
1151
  await ensureParentDir2(fs, path);
1124
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1152
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1125
1153
  }
1126
1154
  async function existsSafe(fs, path) {
1127
1155
  try {
@@ -1399,14 +1427,14 @@ var TombstoneStore = class {
1399
1427
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1400
1428
  const p = this.pathFor(tombstone.path);
1401
1429
  await ensureParentDir2(this.fs, p);
1402
- await this.fs.writeFile(p, bytes);
1430
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
1403
1431
  }
1404
1432
  /** Persist an updated tombstone (confirmations) when it still exists. */
1405
1433
  async save(tombstone) {
1406
1434
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1407
1435
  const p = this.pathFor(tombstone.path);
1408
1436
  if (!await existsSafe(this.fs, p)) return;
1409
- await this.fs.writeFile(p, bytes);
1437
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
1410
1438
  }
1411
1439
  /** Delete a tombstone. */
1412
1440
  async remove(tombstone) {
@@ -2604,7 +2632,7 @@ var ConfigRepo = class _ConfigRepo {
2604
2632
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
2605
2633
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
2606
2634
  await this.ensureDir(tombstonePath);
2607
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2635
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
2608
2636
  }
2609
2637
  /**
2610
2638
  * Delete a file and write a tombstone so the deletion propagates
@@ -27845,6 +27845,34 @@ 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, options = {}) {
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
+ try {
27861
+ await fs.writeFile(path, bytes);
27862
+ } catch (err2) {
27863
+ if (isAlreadyExistsError(err2) && options.ignorePersistentEexist) {
27864
+ log7.warn(`[zen-fs-config] writeFileOverwrite: \u540C\u540D\u6587\u4EF6\u65E0\u6CD5\u8986\u76D6\uFF0C\u6309\u5DF2\u5B58\u5728\u5904\u7406 ${path}`);
27865
+ return;
27866
+ }
27867
+ throw err2;
27868
+ }
27869
+ }
27870
+ function isAlreadyExistsError(err2) {
27871
+ const e = err2;
27872
+ if (!e) return false;
27873
+ if (e.code === "EEXIST") return true;
27874
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
27875
+ }
27848
27876
  async function ensureParentDir2(fs, filePath) {
27849
27877
  const parts = filePath.split("/").filter(Boolean);
27850
27878
  parts.pop();
@@ -27875,7 +27903,7 @@ var ZenFSConfig = (() => {
27875
27903
  }
27876
27904
  async function writeJson(fs, path, value) {
27877
27905
  await ensureParentDir2(fs, path);
27878
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27906
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27879
27907
  }
27880
27908
  async function existsSafe(fs, path) {
27881
27909
  try {
@@ -28151,14 +28179,14 @@ var ZenFSConfig = (() => {
28151
28179
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28152
28180
  const p = this.pathFor(tombstone.path);
28153
28181
  await ensureParentDir2(this.fs, p);
28154
- await this.fs.writeFile(p, bytes);
28182
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
28155
28183
  }
28156
28184
  /** Persist an updated tombstone (confirmations) when it still exists. */
28157
28185
  async save(tombstone) {
28158
28186
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28159
28187
  const p = this.pathFor(tombstone.path);
28160
28188
  if (!await existsSafe(this.fs, p)) return;
28161
- await this.fs.writeFile(p, bytes);
28189
+ await writeFileOverwrite(this.fs, p, bytes, { ignorePersistentEexist: true });
28162
28190
  }
28163
28191
  /** Delete a tombstone. */
28164
28192
  async remove(tombstone) {
@@ -29341,7 +29369,7 @@ var ZenFSConfig = (() => {
29341
29369
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
29342
29370
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
29343
29371
  await this.ensureDir(tombstonePath);
29344
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
29372
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
29345
29373
  }
29346
29374
  /**
29347
29375
  * 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.8",
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",