pi-fabric 0.92.1 → 0.92.3

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.
@@ -28,8 +28,9 @@ import {
28
28
  import {
29
29
  DEFAULT_FABRIC_CONFIG,
30
30
  MAX_AGENT_TIMEOUT_MS,
31
- MIN_AGENT_TIMEOUT_MS
32
- } from "./chunk-CHNDPTCW.js";
31
+ MIN_AGENT_TIMEOUT_MS,
32
+ quarantineDamagedFile
33
+ } from "./chunk-OKDKF6L5.js";
33
34
  import {
34
35
  writeFileAtomic,
35
36
  writeJsonAtomic
@@ -154,25 +155,30 @@ var recoverConcatenatedState = (serialized) => {
154
155
  }
155
156
  return start < 0 && documents > 1 ? snapshots.at(-1) : void 0;
156
157
  };
158
+ var emptyState = () => ({ format: 1, entries: {} });
157
159
  var readState = (filePath, maxBytes) => {
160
+ let serialized;
158
161
  try {
159
162
  const stat = fs.statSync(filePath);
160
163
  if (stat.size > maxBytes) throw new Error(`state exceeds ${maxBytes} bytes`);
161
- const serialized = fs.readFileSync(filePath, "utf8");
162
- try {
163
- const parsed = JSON.parse(serialized);
164
- if (isMeshStateFile(parsed)) return parsed;
165
- throw new Error("invalid state format");
166
- } catch (error) {
167
- const recovered = recoverConcatenatedState(serialized);
168
- if (recovered) return recovered;
169
- throw error;
170
- }
164
+ if (stat.size === 0) return emptyState();
165
+ serialized = fs.readFileSync(filePath, "utf8");
171
166
  } catch (error) {
172
- if (errorCode(error) === "ENOENT") return { format: 1, entries: {} };
167
+ if (errorCode(error) === "ENOENT") return emptyState();
173
168
  const message = error instanceof Error ? error.message : String(error);
174
169
  throw new Error(`Failed to read Fabric mesh state: ${message}`);
175
170
  }
171
+ if (!serialized.trim()) return emptyState();
172
+ try {
173
+ const parsed = JSON.parse(serialized);
174
+ if (isMeshStateFile(parsed)) return parsed;
175
+ throw new Error("invalid state format");
176
+ } catch (error) {
177
+ const recovered = recoverConcatenatedState(serialized);
178
+ if (recovered) return recovered;
179
+ quarantineDamagedFile(filePath);
180
+ return emptyState();
181
+ }
176
182
  };
177
183
  var atomicWrite = (filePath, value, maxBytes = Number.POSITIVE_INFINITY) => {
178
184
  const serialized = JSON.stringify(value, null, 2);
@@ -550,7 +556,7 @@ var MeshStore = class {
550
556
  }
551
557
  } catch (error) {
552
558
  this.#stateCache = void 0;
553
- if (errorCode(error) === "ENOENT") return { format: 1, entries: {} };
559
+ if (errorCode(error) === "ENOENT") return emptyState();
554
560
  throw error;
555
561
  }
556
562
  const state = readState(this.#statePath, this.#maxStateBytes);
@@ -6642,4 +6648,4 @@ export {
6642
6648
  agentParticipantRecords,
6643
6649
  actorParticipantRecord
6644
6650
  };
6645
- //# sourceMappingURL=chunk-OJX2V36R.js.map
6651
+ //# sourceMappingURL=chunk-2ECGHE2U.js.map