From 67996f6d0afa8956048c2a0824ba77472970b6ec Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 13 May 2026 16:53:47 +1000 Subject: [PATCH] cli: fix stale stat.size in NodeVaultAdapter causing corrupted file errors chokidar stats are captured at poll time and may not reflect the file's final byte length by the time vault.read() is called. The downstream integrity check compares stat.size to content length; a mismatch causes other LiveSync clients to reject the file as corrupted. Fix by updating file.stat.size from the actual content in read() and readBinary(). Co-authored-by: Joysimple --- src/apps/cli/adapters/NodeVaultAdapter.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/apps/cli/adapters/NodeVaultAdapter.ts b/src/apps/cli/adapters/NodeVaultAdapter.ts index 947ad01..89e6911 100644 --- a/src/apps/cli/adapters/NodeVaultAdapter.ts +++ b/src/apps/cli/adapters/NodeVaultAdapter.ts @@ -15,7 +15,12 @@ export class NodeVaultAdapter implements IVaultAdapter { } async read(file: NodeFile): Promise { - return await fs.readFile(this.resolvePath(file.path), "utf-8"); + const content = await fs.readFile(this.resolvePath(file.path), "utf-8"); + // Correct stale stat.size — chokidar stats may be from a poll before the final write. + // The downstream document integrity check compares stat.size to content length, so + // they must agree or other clients reject the file as corrupted. + file.stat.size = Buffer.byteLength(content, "utf-8"); + return content; } async cachedRead(file: NodeFile): Promise { @@ -25,6 +30,8 @@ export class NodeVaultAdapter implements IVaultAdapter { async readBinary(file: NodeFile): Promise { const buffer = await fs.readFile(this.resolvePath(file.path)); + // Same correction as read() — ensure stat.size matches actual byte length. + file.stat.size = buffer.length; return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength) as ArrayBuffer; }