fix: lazy-restore published article using OAuth token
Browse filesThe startup pull fails when there's no HF_TOKEN env var (common in
Spaces using OAuth only). Now the restore is attempted:
1. At startup (works if HF_TOKEN secret is set)
2. On first authenticated request (auth/status with user token)
3. On first visit to the public page (if visitor has a cookie)
Uses a flag to ensure the pull only runs once successfully.
Made-with: Cursor
- backend/src/server.ts +39 -10
backend/src/server.ts
CHANGED
|
@@ -33,6 +33,35 @@ const PORT = parseInt(process.env.PORT || "8080", 10);
|
|
| 33 |
/** Single collab document; URL `?doc=` is not supported. */
|
| 34 |
const DEFAULT_DOC_NAME = "default";
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
mkdirSync(DATA_DIR, { recursive: true });
|
| 37 |
|
| 38 |
function docPath(name: string) {
|
|
@@ -133,6 +162,8 @@ app.get("/api/auth/status", async (req, res) => {
|
|
| 133 |
// Cache the user's OAuth token for HF API calls (dataset storage)
|
| 134 |
if (user.canEdit && token) {
|
| 135 |
setUserToken(token);
|
|
|
|
|
|
|
| 136 |
}
|
| 137 |
|
| 138 |
res.json({
|
|
@@ -392,6 +423,12 @@ if (existsSync(staticDir)) {
|
|
| 392 |
return;
|
| 393 |
}
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
// In production with OAuth: serve the published article if it exists
|
| 396 |
const publishedPath = getPublishedPath(DEFAULT_DOC_NAME);
|
| 397 |
|
|
@@ -411,16 +448,8 @@ const server = httpServer.listen(PORT, async () => {
|
|
| 411 |
console.log(`[server] running on http://localhost:${PORT}`);
|
| 412 |
console.log(`[server] collab websocket at ws://localhost:${PORT}/collab/:doc`);
|
| 413 |
|
| 414 |
-
//
|
| 415 |
-
|
| 416 |
-
try {
|
| 417 |
-
const found = await pullPublishedAssets(DEFAULT_DOC_NAME, DATA_DIR);
|
| 418 |
-
if (found) console.log("[server] restored published article from HF dataset");
|
| 419 |
-
else console.log("[server] no published article found in HF dataset");
|
| 420 |
-
} catch (err) {
|
| 421 |
-
console.warn("[server] failed to restore published article:", (err as Error).message);
|
| 422 |
-
}
|
| 423 |
-
}
|
| 424 |
});
|
| 425 |
|
| 426 |
// Graceful shutdown: flush pending docs to HF
|
|
|
|
| 33 |
/** Single collab document; URL `?doc=` is not supported. */
|
| 34 |
const DEFAULT_DOC_NAME = "default";
|
| 35 |
|
| 36 |
+
/**
|
| 37 |
+
* Lazy restore of published assets from HF dataset.
|
| 38 |
+
* Called at startup (may fail without token) and again on first auth.
|
| 39 |
+
* Only runs the actual pull once successfully.
|
| 40 |
+
*/
|
| 41 |
+
let _publishedRestored = false;
|
| 42 |
+
async function ensurePublishedRestored(token?: string): Promise<void> {
|
| 43 |
+
if (_publishedRestored) return;
|
| 44 |
+
if (!getDatasetId()) return;
|
| 45 |
+
|
| 46 |
+
const publishedPath = join(DATA_DIR, "published", DEFAULT_DOC_NAME.replace(/[^a-zA-Z0-9_-]/g, "_"), "index.html");
|
| 47 |
+
if (existsSync(publishedPath)) {
|
| 48 |
+
_publishedRestored = true;
|
| 49 |
+
return;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (token) setUserToken(token);
|
| 53 |
+
|
| 54 |
+
try {
|
| 55 |
+
const found = await pullPublishedAssets(DEFAULT_DOC_NAME, DATA_DIR);
|
| 56 |
+
if (found) {
|
| 57 |
+
_publishedRestored = true;
|
| 58 |
+
console.log("[server] restored published article from HF dataset");
|
| 59 |
+
}
|
| 60 |
+
} catch (err) {
|
| 61 |
+
console.warn("[server] failed to restore published:", (err as Error).message);
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
mkdirSync(DATA_DIR, { recursive: true });
|
| 66 |
|
| 67 |
function docPath(name: string) {
|
|
|
|
| 162 |
// Cache the user's OAuth token for HF API calls (dataset storage)
|
| 163 |
if (user.canEdit && token) {
|
| 164 |
setUserToken(token);
|
| 165 |
+
// Lazy-restore published article on first authenticated request
|
| 166 |
+
ensurePublishedRestored(token).catch(() => {});
|
| 167 |
}
|
| 168 |
|
| 169 |
res.json({
|
|
|
|
| 423 |
return;
|
| 424 |
}
|
| 425 |
|
| 426 |
+
// Try lazy-restore if we have a cookie token (visitor may be logged in)
|
| 427 |
+
const visitorToken = extractToken(req.headers.cookie) ?? undefined;
|
| 428 |
+
if (visitorToken) {
|
| 429 |
+
await ensurePublishedRestored(visitorToken);
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
// In production with OAuth: serve the published article if it exists
|
| 433 |
const publishedPath = getPublishedPath(DEFAULT_DOC_NAME);
|
| 434 |
|
|
|
|
| 448 |
console.log(`[server] running on http://localhost:${PORT}`);
|
| 449 |
console.log(`[server] collab websocket at ws://localhost:${PORT}/collab/:doc`);
|
| 450 |
|
| 451 |
+
// Try eager restore (works if HF_TOKEN env var is set)
|
| 452 |
+
await ensurePublishedRestored();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
});
|
| 454 |
|
| 455 |
// Graceful shutdown: flush pending docs to HF
|