tfrere HF Staff commited on
Commit
c49b156
·
1 Parent(s): b6e0911

fix: flush Hocuspocus document to disk before publish

Browse files

openDirectConnection retrieves the live Y.Doc from Hocuspocus and
writes it to data/<doc>.yjs so the publisher always finds the file,
even when Hocuspocus hasn't triggered a store callback yet.

Made-with: Cursor

Files changed (1) hide show
  1. backend/src/server.ts +11 -0
backend/src/server.ts CHANGED
@@ -8,6 +8,7 @@ import { join, dirname } from "path";
8
  import { fileURLToPath } from "url";
9
  import { randomUUID } from "crypto";
10
  import multer from "multer";
 
11
  import { handleChat } from "./agent/chat.js";
12
  import { citationsRouter } from "./citations.js";
13
  import {
@@ -168,6 +169,16 @@ app.post("/api/publish", async (req, res) => {
168
  }
169
 
170
  try {
 
 
 
 
 
 
 
 
 
 
171
  const result = await publishDocument(DEFAULT_DOC_NAME);
172
  if (!result.success) {
173
  res.status(500).json({ error: result.error || "Publish failed" });
 
8
  import { fileURLToPath } from "url";
9
  import { randomUUID } from "crypto";
10
  import multer from "multer";
11
+ import * as Y from "yjs";
12
  import { handleChat } from "./agent/chat.js";
13
  import { citationsRouter } from "./citations.js";
14
  import {
 
169
  }
170
 
171
  try {
172
+ // Flush live Hocuspocus document to disk so the publisher can read it
173
+ const conn = await hocuspocus.openDirectConnection(DEFAULT_DOC_NAME);
174
+ if (conn.document) {
175
+ writeFileSync(
176
+ docPath(DEFAULT_DOC_NAME),
177
+ Buffer.from(Y.encodeStateAsUpdate(conn.document)),
178
+ );
179
+ }
180
+ await conn.disconnect();
181
+
182
  const result = await publishDocument(DEFAULT_DOC_NAME);
183
  if (!result.success) {
184
  res.status(500).json({ error: result.error || "Publish failed" });