fix(mindspace): 修复删除时无法解析 MySQL JSON 导致页面复活
source_snapshot_json 在 mysql2 中已是对象,JSON.parse 失败会使 relative_path 丢失、工作区 HTML 未清理;同步后页面重新出现。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+18
-4
@@ -29,6 +29,16 @@ function asNumber(value) {
|
||||
return Number(value ?? 0);
|
||||
}
|
||||
|
||||
export function parseJsonColumn(value, fallback = {}) {
|
||||
if (value == null || value === '') return { ...fallback };
|
||||
if (typeof value === 'object' && !Buffer.isBuffer(value)) return value;
|
||||
try {
|
||||
return JSON.parse(String(value));
|
||||
} catch {
|
||||
return { ...fallback };
|
||||
}
|
||||
}
|
||||
|
||||
function pageError(message, code, details) {
|
||||
return Object.assign(new Error(message), { code, details });
|
||||
}
|
||||
@@ -730,7 +740,7 @@ export function createPageService(pool, options = {}) {
|
||||
);
|
||||
let snapshot = {};
|
||||
try {
|
||||
snapshot = JSON.parse(rows[0]?.source_snapshot_json ?? '{}');
|
||||
snapshot = parseJsonColumn(rows[0]?.source_snapshot_json);
|
||||
} catch {
|
||||
snapshot = {};
|
||||
}
|
||||
@@ -1084,7 +1094,7 @@ export function createPageService(pool, options = {}) {
|
||||
LIMIT 1`,
|
||||
[pageId, userId],
|
||||
);
|
||||
const snapshot = JSON.parse(rows[0]?.source_snapshot_json ?? '{}');
|
||||
const snapshot = parseJsonColumn(rows[0]?.source_snapshot_json);
|
||||
workspaceHtmlRelativePath = snapshot.relative_path ?? null;
|
||||
} catch {
|
||||
workspaceHtmlRelativePath = null;
|
||||
@@ -1166,7 +1176,10 @@ export function createPageService(pool, options = {}) {
|
||||
publishDir: workspacePublishDir,
|
||||
htmlRelativePath: workspaceHtmlRelativePath,
|
||||
html: page.content ?? '',
|
||||
}).catch(() => ({ removed: [], skipped: [] }));
|
||||
}).catch((error) => {
|
||||
console.warn('[MindSpace] workspace purge failed:', error?.message ?? error);
|
||||
return { removed: [], skipped: [] };
|
||||
});
|
||||
|
||||
return { ...result, workspaceFilesRemoved: purgeResult.removed ?? [] };
|
||||
};
|
||||
@@ -1190,7 +1203,7 @@ export function createPageService(pool, options = {}) {
|
||||
const content = await fs.readFile(storagePath, 'utf8');
|
||||
let snapshot = {};
|
||||
try {
|
||||
snapshot = JSON.parse(row.source_snapshot_json ?? '{}');
|
||||
snapshot = parseJsonColumn(row.source_snapshot_json);
|
||||
} catch {
|
||||
snapshot = {};
|
||||
}
|
||||
@@ -1375,6 +1388,7 @@ export const pageInternals = {
|
||||
escapeHtml,
|
||||
normalizePageInput,
|
||||
normalizeListPageFilters,
|
||||
parseJsonColumn,
|
||||
renderContent,
|
||||
renderPreviewHtml,
|
||||
renderHtmlPreview,
|
||||
|
||||
Reference in New Issue
Block a user