zen-fs-config 0.7.7 → 0.7.9

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.
@@ -49,6 +49,134 @@ var ZenFSConfig = (() => {
49
49
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
50
50
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
51
51
 
52
+ // src/idb-repair.ts
53
+ function idb() {
54
+ return globalThis.indexedDB;
55
+ }
56
+ function openDB(name) {
57
+ const factory = idb();
58
+ if (!factory) return Promise.resolve(null);
59
+ return new Promise((resolve4) => {
60
+ let req;
61
+ try {
62
+ req = factory.open(name);
63
+ } catch {
64
+ resolve4(null);
65
+ return;
66
+ }
67
+ req.onupgradeneeded = () => {
68
+ try {
69
+ req.transaction?.abort();
70
+ } catch {
71
+ }
72
+ };
73
+ req.onsuccess = () => resolve4(req.result);
74
+ req.onerror = () => resolve4(null);
75
+ req.onblocked = () => resolve4(null);
76
+ });
77
+ }
78
+ function isDecodableListing(bytes) {
79
+ if (bytes.length === 0) return false;
80
+ if (bytes[0] !== LEGACY_LISTING_MAGIC) return true;
81
+ let text;
82
+ try {
83
+ text = new TextDecoder().decode(bytes);
84
+ } catch {
85
+ return false;
86
+ }
87
+ let parsed;
88
+ try {
89
+ parsed = JSON.parse(text);
90
+ } catch {
91
+ return false;
92
+ }
93
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return false;
94
+ return Object.values(parsed).every(
95
+ (v) => typeof v === "number" || typeof v === "string" && /^\d+$/.test(v)
96
+ );
97
+ }
98
+ function isDirectoryInode(bytes, key) {
99
+ if (bytes.byteLength !== INODE_SIZE) return null;
100
+ const view2 = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
101
+ const ino = view2.getUint32(58, true);
102
+ if (ino !== key) return null;
103
+ const mode = view2.getUint16(12, true);
104
+ if ((mode & S_IFMT) !== S_IFDIR) return null;
105
+ const mtime = view2.getFloat64(42, true);
106
+ if (!Number.isFinite(mtime) || mtime < 1e11 || mtime > 1e13) return null;
107
+ return view2.getUint32(0, true);
108
+ }
109
+ async function repairIndexedDBStore(storeName) {
110
+ if (!idb()) return null;
111
+ const db = await openDB(storeName);
112
+ if (!db) return null;
113
+ const objectStoreName = Array.from(
114
+ db.objectStoreNames
115
+ )[0];
116
+ if (!objectStoreName) {
117
+ db.close?.();
118
+ return null;
119
+ }
120
+ const result = { scanned: 0, repaired: 0, details: [] };
121
+ try {
122
+ const tx = db.transaction(objectStoreName, "readwrite");
123
+ const store = tx.objectStore(objectStoreName);
124
+ const keys = await request(store.getAllKeys());
125
+ const values = await request(store.getAll());
126
+ const byKey = /* @__PURE__ */ new Map();
127
+ for (let i = 0; i < keys.length; i++) {
128
+ const value = values[i];
129
+ if (value instanceof Uint8Array) byKey.set(Number(keys[i]), value);
130
+ }
131
+ for (let i = 0; i < keys.length; i++) {
132
+ const value = values[i];
133
+ if (!(value instanceof Uint8Array)) continue;
134
+ const key = Number(keys[i]);
135
+ if (!Number.isFinite(key)) continue;
136
+ result.scanned++;
137
+ const dataSlot = isDirectoryInode(value, key);
138
+ if (dataSlot === null) continue;
139
+ const listing = byKey.get(dataSlot);
140
+ if (listing && isDecodableListing(listing)) continue;
141
+ const reason = listing ? "listing slot holds non-listing bytes" : "listing slot missing";
142
+ result.repaired++;
143
+ result.details.push({ key, data: dataSlot, reason });
144
+ await request(store.put(new TextEncoder().encode("{}"), dataSlot));
145
+ }
146
+ await new Promise((resolve4, reject) => {
147
+ tx.oncomplete = () => resolve4();
148
+ tx.onerror = () => reject(tx.error);
149
+ tx.onabort = () => reject(tx.error);
150
+ });
151
+ } catch {
152
+ try {
153
+ db.close?.();
154
+ } catch {
155
+ }
156
+ return null;
157
+ } finally {
158
+ try {
159
+ db.close?.();
160
+ } catch {
161
+ }
162
+ }
163
+ return result;
164
+ }
165
+ var INODE_SIZE, S_IFMT, S_IFDIR, LEGACY_LISTING_MAGIC, request;
166
+ var init_idb_repair = __esm({
167
+ "src/idb-repair.ts"() {
168
+ "use strict";
169
+ INODE_SIZE = 4096;
170
+ S_IFMT = 61440;
171
+ S_IFDIR = 16384;
172
+ LEGACY_LISTING_MAGIC = 123;
173
+ request = (req) => new Promise((resolve4, reject) => {
174
+ req.onsuccess = () => resolve4(req.result);
175
+ req.onerror = () => reject(req.error);
176
+ });
177
+ }
178
+ });
179
+
52
180
  // node_modules/utilium/dist/array.js
53
181
  var init_array = __esm({
54
182
  "node_modules/utilium/dist/array.js"() {
@@ -1478,10 +1606,10 @@ var ZenFSConfig = (() => {
1478
1606
  R_OK: () => R_OK,
1479
1607
  S_IFBLK: () => S_IFBLK,
1480
1608
  S_IFCHR: () => S_IFCHR,
1481
- S_IFDIR: () => S_IFDIR,
1609
+ S_IFDIR: () => S_IFDIR2,
1482
1610
  S_IFIFO: () => S_IFIFO,
1483
1611
  S_IFLNK: () => S_IFLNK,
1484
- S_IFMT: () => S_IFMT,
1612
+ S_IFMT: () => S_IFMT2,
1485
1613
  S_IFREG: () => S_IFREG,
1486
1614
  S_IFSOCK: () => S_IFSOCK,
1487
1615
  S_IRGRP: () => S_IRGRP,
@@ -1504,7 +1632,7 @@ var ZenFSConfig = (() => {
1504
1632
  X_OK: () => X_OK,
1505
1633
  size_max: () => size_max
1506
1634
  });
1507
- var F_OK, R_OK, W_OK, X_OK, COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE, O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND, O_DIRECTORY, O_NOATIME, O_NOFOLLOW, O_SYNC, O_DSYNC, O_SYMLINK, O_DIRECT, O_NONBLOCK, S_IFMT, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFDIR, S_IFCHR, S_IFIFO, S_ISUID, S_ISGID, S_ISVTX, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH, UV_FS_O_FILEMAP, size_max;
1635
+ var F_OK, R_OK, W_OK, X_OK, COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE, O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND, O_DIRECTORY, O_NOATIME, O_NOFOLLOW, O_SYNC, O_DSYNC, O_SYMLINK, O_DIRECT, O_NONBLOCK, S_IFMT2, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFDIR2, S_IFCHR, S_IFIFO, S_ISUID, S_ISGID, S_ISVTX, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH, UV_FS_O_FILEMAP, size_max;
1508
1636
  var init_constants = __esm({
1509
1637
  "node_modules/@zenfs/core/dist/constants.js"() {
1510
1638
  "use strict";
@@ -1531,12 +1659,12 @@ var ZenFSConfig = (() => {
1531
1659
  O_SYMLINK = 32768;
1532
1660
  O_DIRECT = 16384;
1533
1661
  O_NONBLOCK = 2048;
1534
- S_IFMT = 61440;
1662
+ S_IFMT2 = 61440;
1535
1663
  S_IFSOCK = 49152;
1536
1664
  S_IFLNK = 40960;
1537
1665
  S_IFREG = 32768;
1538
1666
  S_IFBLK = 24576;
1539
- S_IFDIR = 16384;
1667
+ S_IFDIR2 = 16384;
1540
1668
  S_IFCHR = 8192;
1541
1669
  S_IFIFO = 4096;
1542
1670
  S_ISUID = 2048;
@@ -2906,7 +3034,7 @@ var ZenFSConfig = (() => {
2906
3034
  this.size = this._convert(size ?? 0);
2907
3035
  this.ino = this._convert(ino ?? 0);
2908
3036
  this.mode = this._convert(mode ?? 420 & S_IFREG);
2909
- if ((this.mode & S_IFMT) == 0) {
3037
+ if ((this.mode & S_IFMT2) == 0) {
2910
3038
  this.mode = this.mode | this._convert(S_IFREG);
2911
3039
  }
2912
3040
  Object.assign(this, rest);
@@ -2944,25 +3072,25 @@ var ZenFSConfig = (() => {
2944
3072
  this.birthtimeMs = this._convert(value.getTime());
2945
3073
  }
2946
3074
  isFile() {
2947
- return (this.mode & S_IFMT) === S_IFREG;
3075
+ return (this.mode & S_IFMT2) === S_IFREG;
2948
3076
  }
2949
3077
  isDirectory() {
2950
- return (this.mode & S_IFMT) === S_IFDIR;
3078
+ return (this.mode & S_IFMT2) === S_IFDIR2;
2951
3079
  }
2952
3080
  isSymbolicLink() {
2953
- return (this.mode & S_IFMT) === S_IFLNK;
3081
+ return (this.mode & S_IFMT2) === S_IFLNK;
2954
3082
  }
2955
3083
  isSocket() {
2956
- return (this.mode & S_IFMT) === S_IFSOCK;
3084
+ return (this.mode & S_IFMT2) === S_IFSOCK;
2957
3085
  }
2958
3086
  isBlockDevice() {
2959
- return (this.mode & S_IFMT) === S_IFBLK;
3087
+ return (this.mode & S_IFMT2) === S_IFBLK;
2960
3088
  }
2961
3089
  isCharacterDevice() {
2962
- return (this.mode & S_IFMT) === S_IFCHR;
3090
+ return (this.mode & S_IFMT2) === S_IFCHR;
2963
3091
  }
2964
3092
  isFIFO() {
2965
- return (this.mode & S_IFMT) === S_IFIFO;
3093
+ return (this.mode & S_IFMT2) === S_IFIFO;
2966
3094
  }
2967
3095
  toJSON() {
2968
3096
  return pick(this, _inode_fields);
@@ -3042,25 +3170,25 @@ var ZenFSConfig = (() => {
3042
3170
 
3043
3171
  // node_modules/@zenfs/core/dist/internal/inode.js
3044
3172
  function isFile2(metadata) {
3045
- return (metadata.mode & S_IFMT) === S_IFREG;
3173
+ return (metadata.mode & S_IFMT2) === S_IFREG;
3046
3174
  }
3047
3175
  function isDirectory2(metadata) {
3048
- return (metadata.mode & S_IFMT) === S_IFDIR;
3176
+ return (metadata.mode & S_IFMT2) === S_IFDIR2;
3049
3177
  }
3050
3178
  function isSymbolicLink(metadata) {
3051
- return (metadata.mode & S_IFMT) === S_IFLNK;
3179
+ return (metadata.mode & S_IFMT2) === S_IFLNK;
3052
3180
  }
3053
3181
  function isSocket(metadata) {
3054
- return (metadata.mode & S_IFMT) === S_IFSOCK;
3182
+ return (metadata.mode & S_IFMT2) === S_IFSOCK;
3055
3183
  }
3056
3184
  function isBlockDevice(metadata) {
3057
- return (metadata.mode & S_IFMT) === S_IFBLK;
3185
+ return (metadata.mode & S_IFMT2) === S_IFBLK;
3058
3186
  }
3059
3187
  function isCharacterDevice(metadata) {
3060
- return (metadata.mode & S_IFMT) === S_IFCHR;
3188
+ return (metadata.mode & S_IFMT2) === S_IFCHR;
3061
3189
  }
3062
3190
  function isFIFO(metadata) {
3063
- return (metadata.mode & S_IFMT) === S_IFIFO;
3191
+ return (metadata.mode & S_IFMT2) === S_IFIFO;
3064
3192
  }
3065
3193
  function hasAccess($, inode, access3) {
3066
3194
  const { credentials } = contextOf($);
@@ -3849,7 +3977,7 @@ var ZenFSConfig = (() => {
3849
3977
  const node = this.get(path);
3850
3978
  if (!node)
3851
3979
  throw withErrno("ENOENT");
3852
- if ((node.mode & S_IFMT) != S_IFDIR)
3980
+ if ((node.mode & S_IFMT2) != S_IFDIR2)
3853
3981
  throw withErrno("ENOTDIR");
3854
3982
  const entries2 = {};
3855
3983
  for (const entry of this.keys()) {
@@ -3873,7 +4001,7 @@ var ZenFSConfig = (() => {
3873
4001
  directories() {
3874
4002
  const dirs = /* @__PURE__ */ new Map();
3875
4003
  for (const [path, node] of this) {
3876
- if ((node.mode & S_IFMT) != S_IFDIR)
4004
+ if ((node.mode & S_IFMT2) != S_IFDIR2)
3877
4005
  continue;
3878
4006
  const entries2 = {};
3879
4007
  for (const entry of this.keys()) {
@@ -4562,7 +4690,7 @@ var ZenFSConfig = (() => {
4562
4690
  const [path, ino] = queue.shift();
4563
4691
  const inode = new Inode(await tx.get(ino));
4564
4692
  index.set(path, inode);
4565
- if (inode.mode & S_IFDIR) {
4693
+ if (inode.mode & S_IFDIR2) {
4566
4694
  const dir = decodeDirListing(await tx.get(inode.data) ?? _throw(withErrno("ENODATA")));
4567
4695
  for (const [name, id] of Object.entries(dir)) {
4568
4696
  queue.push([join(path, name), id]);
@@ -4591,7 +4719,7 @@ var ZenFSConfig = (() => {
4591
4719
  const [path, ino] = queue.shift();
4592
4720
  const inode = new Inode(tx.getSync(ino));
4593
4721
  index.set(path, inode);
4594
- if (inode.mode & S_IFDIR) {
4722
+ if (inode.mode & S_IFDIR2) {
4595
4723
  const dir = decodeDirListing(tx.getSync(inode.data) ?? _throw(withErrno("ENODATA")));
4596
4724
  for (const [name, id] of Object.entries(dir)) {
4597
4725
  queue.push([join(path, name), id]);
@@ -4767,10 +4895,10 @@ var ZenFSConfig = (() => {
4767
4895
  this.removeSync(path, true);
4768
4896
  }
4769
4897
  async mkdir(path, options) {
4770
- return await this.commitNew(path, { ...options, mode: options.mode | S_IFDIR }, encodeUTF8("{}"));
4898
+ return await this.commitNew(path, { ...options, mode: options.mode | S_IFDIR2 }, encodeUTF8("{}"));
4771
4899
  }
4772
4900
  mkdirSync(path, options) {
4773
- return this.commitNewSync(path, { ...options, mode: options.mode | S_IFDIR }, encodeUTF8("{}"));
4901
+ return this.commitNewSync(path, { ...options, mode: options.mode | S_IFDIR2 }, encodeUTF8("{}"));
4774
4902
  }
4775
4903
  async readdir(path) {
4776
4904
  const env_11 = { stack: [], error: void 0, hasError: false };
@@ -4956,7 +5084,7 @@ var ZenFSConfig = (() => {
4956
5084
  const tx = __addDisposableResource(env_19, this.transaction(), true);
4957
5085
  if (await tx.get(rootIno))
4958
5086
  return;
4959
- const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR });
5087
+ const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR2 });
4960
5088
  await tx.set(inode.data, encodeUTF8("{}"));
4961
5089
  this._add(rootIno, "/");
4962
5090
  await tx.set(rootIno, inode);
@@ -4979,7 +5107,7 @@ var ZenFSConfig = (() => {
4979
5107
  const tx = __addDisposableResource(env_20, this.transaction(), false);
4980
5108
  if (tx.getSync(rootIno))
4981
5109
  return;
4982
- const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR });
5110
+ const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR2 });
4983
5111
  tx.setSync(inode.data, encodeUTF8("{}"));
4984
5112
  this._add(rootIno, "/");
4985
5113
  tx.setSync(rootIno, inode);
@@ -5006,7 +5134,7 @@ var ZenFSConfig = (() => {
5006
5134
  const rootData = await tx.get(rootIno);
5007
5135
  if (!rootData) {
5008
5136
  notice("Store does not have a root inode");
5009
- const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR });
5137
+ const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR2 });
5010
5138
  await tx.set(inode.data, encodeUTF8("{}"));
5011
5139
  this._add(rootIno, "/");
5012
5140
  await tx.set(rootIno, inode);
@@ -5034,7 +5162,7 @@ var ZenFSConfig = (() => {
5034
5162
  continue;
5035
5163
  }
5036
5164
  const inode = new Inode(inodeData);
5037
- if ((inode.mode & S_IFDIR) != S_IFDIR || visitedDirectories.has(ino)) {
5165
+ if ((inode.mode & S_IFDIR2) != S_IFDIR2 || visitedDirectories.has(ino)) {
5038
5166
  continue;
5039
5167
  }
5040
5168
  visitedDirectories.add(ino);
@@ -5070,7 +5198,7 @@ var ZenFSConfig = (() => {
5070
5198
  const rootData = tx.getSync(rootIno);
5071
5199
  if (!rootData) {
5072
5200
  notice("Store does not have a root inode");
5073
- const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR });
5201
+ const inode = new Inode({ ino: rootIno, data: 1, mode: 511 | S_IFDIR2 });
5074
5202
  tx.setSync(inode.data, encodeUTF8("{}"));
5075
5203
  this._add(rootIno, "/");
5076
5204
  tx.setSync(rootIno, inode);
@@ -5098,7 +5226,7 @@ var ZenFSConfig = (() => {
5098
5226
  continue;
5099
5227
  }
5100
5228
  const inode = new Inode(inodeData);
5101
- if ((inode.mode & S_IFDIR) != S_IFDIR || visitedDirectories.has(ino)) {
5229
+ if ((inode.mode & S_IFDIR2) != S_IFDIR2 || visitedDirectories.has(ino)) {
5102
5230
  continue;
5103
5231
  }
5104
5232
  visitedDirectories.add(ino);
@@ -8504,8 +8632,8 @@ var ZenFSConfig = (() => {
8504
8632
  throw UV("EBADF", "chmod", this.path);
8505
8633
  const _ = __addDisposableResource3(env_5, this.vnode.lockSync("rw"), false);
8506
8634
  this.vnode.metadataDirty = true;
8507
- this.inode.mode = this.inode.mode & (mode > S_IFMT ? ~S_IFMT : S_IFMT) | mode;
8508
- if (this._isSync || mode > S_IFMT)
8635
+ this.inode.mode = this.inode.mode & (mode > S_IFMT2 ? ~S_IFMT2 : S_IFMT2) | mode;
8636
+ if (this._isSync || mode > S_IFMT2)
8509
8637
  this.vnode.syncSync();
8510
8638
  } catch (e_5) {
8511
8639
  env_5.error = e_5;
@@ -8702,8 +8830,8 @@ var ZenFSConfig = (() => {
8702
8830
  throw UV("EBADF", "chmod", this.path);
8703
8831
  const _ = __addDisposableResource3(env_12, await this.vnode.lock("rw"), false);
8704
8832
  this.vnode.metadataDirty = true;
8705
- this.inode.mode = this.inode.mode & (mode > S_IFMT ? ~S_IFMT : S_IFMT) | mode;
8706
- if (this._isSync || mode > S_IFMT)
8833
+ this.inode.mode = this.inode.mode & (mode > S_IFMT2 ? ~S_IFMT2 : S_IFMT2) | mode;
8834
+ if (this._isSync || mode > S_IFMT2)
8707
8835
  await this.vnode.sync();
8708
8836
  } catch (e_12) {
8709
8837
  env_12.error = e_12;
@@ -9114,7 +9242,7 @@ var ZenFSConfig = (() => {
9114
9242
  if (!isDirectory2(parentStats)) {
9115
9243
  throw UV("ENOTDIR", "open", path);
9116
9244
  }
9117
- if (!opt.allowDirectory && mode & S_IFDIR)
9245
+ if (!opt.allowDirectory && mode & S_IFDIR2)
9118
9246
  throw UV("EISDIR", "open", path);
9119
9247
  const _ = __addDisposableResource4(env_1, lockPathSync(fs, parentPath, "rw", parentStats), false);
9120
9248
  const { euid: uid, egid: gid } = contextOf(this).credentials;
@@ -9137,7 +9265,7 @@ var ZenFSConfig = (() => {
9137
9265
  }
9138
9266
  if (flag & O_EXCL)
9139
9267
  throw UV("EEXIST", "open", path);
9140
- if (!opt.allowDirectory && stats.mode & S_IFDIR)
9268
+ if (!opt.allowDirectory && stats.mode & S_IFDIR2)
9141
9269
  throw UV("EISDIR", "open", path);
9142
9270
  const file = new Handle(this, path, resolved, flag, cacheOf(fs).ref(resolved, stats));
9143
9271
  if (flag & O_TRUNC)
@@ -9953,8 +10081,8 @@ var ZenFSConfig = (() => {
9953
10081
  }
9954
10082
  if (!stats)
9955
10083
  return;
9956
- switch (stats.mode & S_IFMT) {
9957
- case S_IFDIR:
10084
+ switch (stats.mode & S_IFMT2) {
10085
+ case S_IFDIR2:
9958
10086
  if (options?.recursive) {
9959
10087
  for (const entry of readdirSync.call(this, path)) {
9960
10088
  rmSync.call(this, join(path, entry), options);
@@ -10020,8 +10148,8 @@ var ZenFSConfig = (() => {
10020
10148
  const srcStats = lstatSync.call(this, source);
10021
10149
  if (opts?.errorOnExist && existsSync.call(this, destination))
10022
10150
  throw UV("EEXIST", "cp", destination);
10023
- switch (srcStats.mode & S_IFMT) {
10024
- case S_IFDIR:
10151
+ switch (srcStats.mode & S_IFMT2) {
10152
+ case S_IFDIR2:
10025
10153
  if (!opts?.recursive)
10026
10154
  throw UV("EISDIR", "cp", source);
10027
10155
  mkdirSync.call(this, destination, { recursive: true });
@@ -10373,7 +10501,7 @@ var ZenFSConfig = (() => {
10373
10501
  throw UV("EACCES", "open", dirname(path));
10374
10502
  if (!isDirectory2(parentStats))
10375
10503
  throw UV("ENOTDIR", "open", dirname(path));
10376
- if (!opt.allowDirectory && mode & S_IFDIR)
10504
+ if (!opt.allowDirectory && mode & S_IFDIR2)
10377
10505
  throw UV("EISDIR", "open", path);
10378
10506
  const _ = __addDisposableResource6(env_1, await lockPath(fs, parentPath, "rw", parentStats), false);
10379
10507
  stats = cacheOf(fs).get(resolved)?.inode ?? await fs.stat(resolved).catch(() => void 0);
@@ -10398,7 +10526,7 @@ var ZenFSConfig = (() => {
10398
10526
  throw UV("EACCES", $ex);
10399
10527
  if (flag & O_EXCL)
10400
10528
  throw UV("EEXIST", $ex);
10401
- if (!opt.allowDirectory && mode & S_IFDIR)
10529
+ if (!opt.allowDirectory && mode & S_IFDIR2)
10402
10530
  throw UV("EISDIR", "open", path);
10403
10531
  const handle = new Handle($, path, resolved, flag, cacheOf(fs).ref(resolved, stats));
10404
10532
  if (flag & O_TRUNC)
@@ -17963,8 +18091,8 @@ var ZenFSConfig = (() => {
17963
18091
  });
17964
18092
  if (!stats)
17965
18093
  return;
17966
- switch (stats.mode & S_IFMT) {
17967
- case S_IFDIR:
18094
+ switch (stats.mode & S_IFMT2) {
18095
+ case S_IFDIR2:
17968
18096
  if (options?.recursive) {
17969
18097
  for (const entry of await readdir2.call(this, path)) {
17970
18098
  await rm.call(this, join(path, entry), options);
@@ -18014,8 +18142,8 @@ var ZenFSConfig = (() => {
18014
18142
  const srcStats = await lstat.call(this, source);
18015
18143
  if (opts?.errorOnExist && await exists.call(this, destination))
18016
18144
  throw UV("EEXIST", "cp", destination);
18017
- switch (srcStats.mode & S_IFMT) {
18018
- case S_IFDIR: {
18145
+ switch (srcStats.mode & S_IFMT2) {
18146
+ case S_IFDIR2: {
18019
18147
  if (!opts?.recursive)
18020
18148
  throw UV("EISDIR", "cp", source);
18021
18149
  const [entries2] = await Promise.all(
@@ -19445,7 +19573,7 @@ var ZenFSConfig = (() => {
19445
19573
  for (const { to, inode } of toRename) {
19446
19574
  const data = contents.get(to);
19447
19575
  this.index.set(to, inode);
19448
- if ((inode.mode & S_IFMT) == S_IFDIR)
19576
+ if ((inode.mode & S_IFMT2) == S_IFDIR2)
19449
19577
  await this._mkdir?.(to, inode);
19450
19578
  else
19451
19579
  await this.write(to, data, 0);
@@ -19469,7 +19597,7 @@ var ZenFSConfig = (() => {
19469
19597
  for (const { to, inode } of toRename) {
19470
19598
  const data = contents.get(to);
19471
19599
  this.index.set(to, inode);
19472
- if ((inode.mode & S_IFMT) == S_IFDIR)
19600
+ if ((inode.mode & S_IFMT2) == S_IFDIR2)
19473
19601
  this._mkdirSync?.(to, inode);
19474
19602
  else
19475
19603
  this.writeSync(to, data, 0);
@@ -19499,7 +19627,7 @@ var ZenFSConfig = (() => {
19499
19627
  const inode = this.index.get(path);
19500
19628
  if (!inode)
19501
19629
  throw withErrno("ENOENT");
19502
- const isDir2 = (inode.mode & S_IFMT) == S_IFDIR;
19630
+ const isDir2 = (inode.mode & S_IFMT2) == S_IFDIR2;
19503
19631
  if (!isDir2 && !isUnlink)
19504
19632
  throw withErrno("ENOTDIR");
19505
19633
  if (isDir2 && isUnlink)
@@ -19556,13 +19684,13 @@ var ZenFSConfig = (() => {
19556
19684
  return this.create(path, { ...options, mode: options.mode | S_IFREG });
19557
19685
  }
19558
19686
  async mkdir(path, options) {
19559
- options = { ...options, mode: options.mode | S_IFDIR };
19687
+ options = { ...options, mode: options.mode | S_IFDIR2 };
19560
19688
  const inode = this.create(path, options);
19561
19689
  await this._mkdir?.(path, options);
19562
19690
  return inode;
19563
19691
  }
19564
19692
  mkdirSync(path, options) {
19565
- options = { ...options, mode: options.mode | S_IFDIR };
19693
+ options = { ...options, mode: options.mode | S_IFDIR2 };
19566
19694
  const inode = this.create(path, options);
19567
19695
  this._mkdirSync?.(path, options);
19568
19696
  return inode;
@@ -20048,7 +20176,7 @@ var ZenFSConfig = (() => {
20048
20176
  handleResponse: () => handleResponse,
20049
20177
  isMessage: () => isMessage,
20050
20178
  isPort: () => isPort,
20051
- request: () => request
20179
+ request: () => request2
20052
20180
  });
20053
20181
  function isPort(port) {
20054
20182
  return port != null && typeof port == "object" && "channel" in port && "send" in port && "addHandler" in port && "removeHandler" in port;
@@ -20147,7 +20275,7 @@ var ZenFSConfig = (() => {
20147
20275
  }
20148
20276
  executors.delete(id);
20149
20277
  }
20150
- function request(request2, { port, timeout: ms = 1e3, fs }) {
20278
+ function request2(request3, { port, timeout: ms = 1e3, fs }) {
20151
20279
  const stack = "\n" + new Error().stack.slice("Error:".length);
20152
20280
  if (!port)
20153
20281
  throw err(withErrno("EINVAL", "Can not make an RPC request without a port"));
@@ -20161,7 +20289,7 @@ var ZenFSConfig = (() => {
20161
20289
  }, ms);
20162
20290
  const executor = { resolve: resolve4, reject, promise, fs, timeout };
20163
20291
  executors.set(id, executor);
20164
- port.send({ ...request2, _zenfs: true, id, stack });
20292
+ port.send({ ...request3, _zenfs: true, id, stack });
20165
20293
  return promise;
20166
20294
  }
20167
20295
  function __requestMethod(req) {
@@ -20208,21 +20336,21 @@ var ZenFSConfig = (() => {
20208
20336
  return async function(fs) {
20209
20337
  detach(port, handler);
20210
20338
  for (const event of events) {
20211
- const request2 = "data" in event ? event.data : event;
20212
- await handleRequest(port, fs, request2);
20339
+ const request3 = "data" in event ? event.data : event;
20340
+ await handleRequest(port, fs, request3);
20213
20341
  }
20214
20342
  };
20215
20343
  }
20216
- async function handleRequest(port, fs, request2) {
20217
- if (!isMessage(request2))
20344
+ async function handleRequest(port, fs, request3) {
20345
+ if (!isMessage(request3))
20218
20346
  return;
20219
20347
  let value, error;
20220
20348
  const transferList = [];
20221
20349
  try {
20222
- switch (request2.method) {
20350
+ switch (request3.method) {
20223
20351
  case "read": {
20224
- __requestMethod(request2);
20225
- const [path, buffer, start, end] = request2.args;
20352
+ __requestMethod(request3);
20353
+ const [path, buffer, start, end] = request3.args;
20226
20354
  await fs.read(path, buffer, start, end);
20227
20355
  value = buffer;
20228
20356
  break;
@@ -20230,26 +20358,26 @@ var ZenFSConfig = (() => {
20230
20358
  case "stat":
20231
20359
  case "createFile":
20232
20360
  case "mkdir": {
20233
- __requestMethod(request2);
20234
- const md = await fs[request2.method](...request2.args);
20361
+ __requestMethod(request3);
20362
+ const md = await fs[request3.method](...request3.args);
20235
20363
  const inode = md instanceof Inode ? md : new Inode(md);
20236
20364
  value = new Uint8Array(inode.buffer, inode.byteOffset, inode.byteLength);
20237
20365
  break;
20238
20366
  }
20239
20367
  case "touch": {
20240
- __requestMethod(request2);
20241
- const [path, metadata] = request2.args;
20368
+ __requestMethod(request3);
20369
+ const [path, metadata] = request3.args;
20242
20370
  await fs.touch(path, new Inode(metadata));
20243
20371
  value = void 0;
20244
20372
  break;
20245
20373
  }
20246
20374
  default:
20247
- value = await fs[request2.method](...request2.args);
20375
+ value = await fs[request3.method](...request3.args);
20248
20376
  }
20249
20377
  } catch (e) {
20250
20378
  error = e instanceof Exception ? e.toJSON() : pick(e, "message", "stack");
20251
20379
  }
20252
- port.send({ _zenfs: true, ...pick(request2, "id", "method", "stack"), error, value }, transferList);
20380
+ port.send({ _zenfs: true, ...pick(request3, "id", "method", "stack"), error, value }, transferList);
20253
20381
  }
20254
20382
  var encodingVersion, executors;
20255
20383
  var init_rpc = __esm({
@@ -20516,11 +20644,11 @@ var ZenFSConfig = (() => {
20516
20644
  // node_modules/@zenfs/core/dist/backends/port.js
20517
20645
  function attachFS(channel, fs) {
20518
20646
  const port = from(channel);
20519
- attach(port, (request2) => handleRequest(port, fs, request2));
20647
+ attach(port, (request3) => handleRequest(port, fs, request3));
20520
20648
  }
20521
20649
  function detachFS(channel, fs) {
20522
20650
  const port = from(channel);
20523
- detach(port, (request2) => handleRequest(port, fs, request2));
20651
+ detach(port, (request3) => handleRequest(port, fs, request3));
20524
20652
  }
20525
20653
  async function resolveRemoteMount(channel, config, _depth = 0) {
20526
20654
  const port = from(channel);
@@ -20562,7 +20690,7 @@ var ZenFSConfig = (() => {
20562
20690
  attach(this.port, handleResponse);
20563
20691
  }
20564
20692
  rpc(method, ...args) {
20565
- return request({ method, args }, {
20693
+ return request2({ method, args }, {
20566
20694
  port: this.port,
20567
20695
  timeout: this.timeout,
20568
20696
  fs: this
@@ -23898,12 +24026,12 @@ var ZenFSConfig = (() => {
23898
24026
  });
23899
24027
 
23900
24028
  // node_modules/@zenfs/dom/dist/IndexedDB.js
23901
- function wrap2(request2) {
24029
+ function wrap2(request3) {
23902
24030
  return new Promise((resolve4, reject) => {
23903
- request2.onsuccess = () => resolve4(request2.result);
23904
- request2.onerror = (e) => {
24031
+ request3.onsuccess = () => resolve4(request3.result);
24032
+ request3.onerror = (e) => {
23905
24033
  e.preventDefault();
23906
- reject(convertException(request2.error));
24034
+ reject(convertException(request3.error));
23907
24035
  };
23908
24036
  });
23909
24037
  }
@@ -24580,6 +24708,7 @@ var ZenFSConfig = (() => {
24580
24708
  var init_backend_registry = __esm({
24581
24709
  "src/backend-registry.ts"() {
24582
24710
  "use strict";
24711
+ init_idb_repair();
24583
24712
  registry = /* @__PURE__ */ new Map();
24584
24713
  metadataRegistry = /* @__PURE__ */ new Map();
24585
24714
  inMemoryCounter = 0;
@@ -24604,6 +24733,12 @@ var ZenFSConfig = (() => {
24604
24733
  const { IndexedDB: IndexedDB2 } = await Promise.resolve().then(() => (init_dist5(), dist_exports2));
24605
24734
  const storeName = options.storeName ?? "zen-fs-config";
24606
24735
  const label = options.label ?? storeName;
24736
+ const repaired = await repairIndexedDBStore(storeName);
24737
+ if (repaired?.repaired) {
24738
+ console.warn(
24739
+ `[Backend:IndexedDB] ${storeName}: reset ${repaired.repaired} corrupted directory listing(s): ` + repaired.details.map((d) => `ino ${d.key}\u2192slot ${d.data} (${d.reason})`).join(", ")
24740
+ );
24741
+ }
24607
24742
  return wrapZenFSFileSystem({ backend: IndexedDB2, storeName, label });
24608
24743
  }, {
24609
24744
  type: "IndexedDB",
@@ -27125,8 +27260,8 @@ var ZenFSConfig = (() => {
27125
27260
  }
27126
27261
  function deleteDataGroupLocalStore(appId, groupId, options = {}) {
27127
27262
  const storeName = dataGroupLocalStoreName(appId, groupId);
27128
- const idb = globalThis.indexedDB;
27129
- if (!idb || typeof idb.deleteDatabase !== "function") {
27263
+ const idb2 = globalThis.indexedDB;
27264
+ if (!idb2 || typeof idb2.deleteDatabase !== "function") {
27130
27265
  return Promise.resolve({
27131
27266
  storeName,
27132
27267
  deleted: false,
@@ -27146,21 +27281,21 @@ var ZenFSConfig = (() => {
27146
27281
  if (timer) clearTimeout(timer);
27147
27282
  resolve4(result);
27148
27283
  };
27149
- let request2;
27284
+ let request3;
27150
27285
  try {
27151
- request2 = idb.deleteDatabase(storeName);
27286
+ request3 = idb2.deleteDatabase(storeName);
27152
27287
  } catch (err2) {
27153
27288
  finish({ storeName, deleted: false, blocked: false, error: summarizeError(err2).message });
27154
27289
  return;
27155
27290
  }
27156
- request2.onsuccess = () => finish({ storeName, deleted: true, blocked: false });
27157
- request2.onerror = () => finish({
27291
+ request3.onsuccess = () => finish({ storeName, deleted: true, blocked: false });
27292
+ request3.onerror = () => finish({
27158
27293
  storeName,
27159
27294
  deleted: false,
27160
27295
  blocked: false,
27161
- error: summarizeError(request2.error).message
27296
+ error: summarizeError(request3.error).message
27162
27297
  });
27163
- request2.onblocked = () => {
27298
+ request3.onblocked = () => {
27164
27299
  blocked = true;
27165
27300
  };
27166
27301
  timer = setTimeout(() => {
@@ -28246,9 +28381,9 @@ var ZenFSConfig = (() => {
28246
28381
  function isMtimeSidecar(fileName) {
28247
28382
  return fileName.endsWith(".mtime");
28248
28383
  }
28249
- var S_IFDIR2 = 16384;
28384
+ var S_IFDIR3 = 16384;
28250
28385
  function isDirectory3(stat5) {
28251
- return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR2) === S_IFDIR2;
28386
+ return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR3) === S_IFDIR3;
28252
28387
  }
28253
28388
  function joinPath(dir, entry) {
28254
28389
  if (dir === "/" || dir === "") return `/${entry}`;
@@ -28300,10 +28435,10 @@ var ZenFSConfig = (() => {
28300
28435
  function isKeepFile(fileName) {
28301
28436
  return fileName === ".keep";
28302
28437
  }
28303
- var S_IFDIR3 = 16384;
28438
+ var S_IFDIR4 = 16384;
28304
28439
  var DEFAULT_PROTECTED_DIRS = ["/.meta/backends/.keep"];
28305
28440
  function isDirectory4(stat5) {
28306
- return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR3) === S_IFDIR3;
28441
+ return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR4) === S_IFDIR4;
28307
28442
  }
28308
28443
  function joinPath2(dir, entry) {
28309
28444
  if (dir === "/" || dir === "") return `/${entry}`;
@@ -28361,9 +28496,9 @@ var ZenFSConfig = (() => {
28361
28496
  }
28362
28497
 
28363
28498
  // src/version-migration.ts
28364
- var S_IFDIR4 = 16384;
28499
+ var S_IFDIR5 = 16384;
28365
28500
  function isDir(stat5) {
28366
- return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR4) === S_IFDIR4;
28501
+ return typeof stat5.mode === "number" && (stat5.mode & S_IFDIR5) === S_IFDIR5;
28367
28502
  }
28368
28503
  function joinPath3(dir, entry) {
28369
28504
  if (dir === "/" || dir === "") return `/${entry}`;
@@ -28963,6 +29098,40 @@ var ZenFSConfig = (() => {
28963
29098
  invalidateTombstoneCache() {
28964
29099
  this.tombstoneCache = null;
28965
29100
  }
29101
+ /**
29102
+ * 写入成功即撤销该路径(及其被删祖先目录)上的墓碑。
29103
+ *
29104
+ * 墓碑表达的是「删除意图」,而同一路径被重新写入是更强的意图。只靠
29105
+ * `isPathRecreatedAfterTombstone` 的 mtime 判定要等到下一轮 tombstone pass 才
29106
+ * 解除排除表:这段窗口里新写入既进不了同步,又可能在 process 里被再删一次。
29107
+ * 因此在写入路径上即时撤销,并立即刷新排除表。
29108
+ */
29109
+ async clearTombstonesForWrite(filePath) {
29110
+ let tombstones;
29111
+ try {
29112
+ tombstones = await this.readTombstones();
29113
+ } catch {
29114
+ return;
29115
+ }
29116
+ const normalized = normalizeTombstonePath(filePath);
29117
+ const affected = tombstones.filter((t) => {
29118
+ if (!t.path) return false;
29119
+ if (t.path === normalized) return true;
29120
+ return isDirectoryTombstone(t) && normalized.startsWith(`${t.path}/`);
29121
+ });
29122
+ if (affected.length === 0) return;
29123
+ for (const tombstone of affected) {
29124
+ try {
29125
+ await this.cachedFS.unlink(tombstonePathFor(tombstone.path, TOMBSTONES_DIR));
29126
+ } catch {
29127
+ }
29128
+ }
29129
+ this.invalidateTombstoneCache();
29130
+ await this.readTombstones();
29131
+ log9.log(
29132
+ `[ConfigRepo] cleared ${affected.length} tombstone(s) on write: ${normalized}`
29133
+ );
29134
+ }
28966
29135
  /**
28967
29136
  * 判定墓碑目标路径是否在删除之后被重新创建。
28968
29137
  *
@@ -29731,6 +29900,7 @@ var ZenFSConfig = (() => {
29731
29900
  async persistConfig(fullPath, bytes) {
29732
29901
  await this.ensureDir(fullPath);
29733
29902
  await this.cachedFS.writeFile(fullPath, bytes);
29903
+ await this.clearTombstonesForWrite(fullPath);
29734
29904
  const author = `${this.appId}/${this.nodeId}`;
29735
29905
  const version2 = await incrementVersion(this.fullFS, fullPath, bytes, author);
29736
29906
  await this.writeVersionSidecar(fullPath, version2);
@@ -29888,6 +30058,7 @@ var ZenFSConfig = (() => {
29888
30058
  const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
29889
30059
  await this.ensureDir(p);
29890
30060
  await this.cachedFS.writeFile(p, bytes);
30061
+ await this.clearTombstonesForWrite(p);
29891
30062
  const author = `${this.appId}/${this.nodeId}`;
29892
30063
  try {
29893
30064
  const version2 = await incrementVersion(this.fullFS, p, bytes, author);
@@ -31119,7 +31290,10 @@ var ZenFSConfig = (() => {
31119
31290
  }
31120
31291
  this.fs = createChrootFS(this.localFS, "/", {
31121
31292
  hiddenPrefixes: [META_DIR, CONFIG_DIR],
31122
- onWrite: (path, data) => this.recordVersion(path, data),
31293
+ onWrite: async (path, data) => {
31294
+ await this.clearTombstonesForWrite(path);
31295
+ await this.recordVersion(path, data);
31296
+ },
31123
31297
  onUnlink: (path) => this.deleteFile(path),
31124
31298
  onRmdir: (path) => this.deleteFile(path)
31125
31299
  });
@@ -31275,6 +31449,34 @@ var ZenFSConfig = (() => {
31275
31449
  this.tombstoneExcludePrefixes.push(tombstone.path, `${tombstone.path}/`);
31276
31450
  }
31277
31451
  }
31452
+ /**
31453
+ * 写入成功即撤销该路径(及其被删祖先目录)上的墓碑。
31454
+ *
31455
+ * 与配置组同一语义:墓碑是删除意图,重新写入是更强的意图;只靠 mtime 判定
31456
+ * 要等下一轮 tombstone pass 才解除排除,期间写入既不参与同步又可能被再删。
31457
+ */
31458
+ async clearTombstonesForWrite(filePath) {
31459
+ try {
31460
+ const tombstones = await this.tombstones.list();
31461
+ const normalized = normalizeTombstonePath(filePath);
31462
+ const affected = tombstones.filter((t) => {
31463
+ if (!t.path) return false;
31464
+ if (t.path === normalized) return true;
31465
+ return isDirectoryTombstone(t) && normalized.startsWith(`${t.path}/`);
31466
+ });
31467
+ if (affected.length === 0) return;
31468
+ for (const tombstone of affected) await this.tombstones.remove(tombstone);
31469
+ this.refreshSyncExclusions(await this.tombstones.list());
31470
+ log9.log(
31471
+ `[AppDataGroup:${this.groupId}] cleared ${affected.length} tombstone(s) on write: ${normalized}`
31472
+ );
31473
+ } catch (err2) {
31474
+ log9.warn(
31475
+ `[AppDataGroup:${this.groupId}] clear tombstones on write failed for ${filePath}:`,
31476
+ err2
31477
+ );
31478
+ }
31479
+ }
31278
31480
  async confirmTombstones() {
31279
31481
  try {
31280
31482
  await this.tombstones.confirm(this.tombstoneTargets());