mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 01:52:42 +00:00
Merge pull request #4329 from naturalcrit/AdditionalPatchLogging
On patch failure, compare client and server text bytewise
This commit is contained in:
@@ -11,7 +11,7 @@ import { nanoid } from 'nanoid';
|
|||||||
import {makePatches, applyPatches, stringifyPatches, parsePatch} from '@sanity/diff-match-patch';
|
import {makePatches, applyPatches, stringifyPatches, parsePatch} from '@sanity/diff-match-patch';
|
||||||
import { md5 } from 'hash-wasm';
|
import { md5 } from 'hash-wasm';
|
||||||
import { splitTextStyleAndMetadata,
|
import { splitTextStyleAndMetadata,
|
||||||
brewSnippetsToJSON } from '../shared/helpers.js';
|
brewSnippetsToJSON, debugTextMismatch } from '../shared/helpers.js';
|
||||||
import checkClientVersion from './middleware/check-client-version.js';
|
import checkClientVersion from './middleware/check-client-version.js';
|
||||||
|
|
||||||
|
|
||||||
@@ -354,7 +354,7 @@ const api = {
|
|||||||
|
|
||||||
if(brewFromServer?.hash !== brewFromClient?.hash) {
|
if(brewFromServer?.hash !== brewFromClient?.hash) {
|
||||||
console.log(`Hash mismatch on brew ${brewFromClient.editId}`);
|
console.log(`Hash mismatch on brew ${brewFromClient.editId}`);
|
||||||
|
debugTextMismatch(brewFromClient.text, brewFromServer.text, `edit/${brewFromClient.editId}`);
|
||||||
res.setHeader('Content-Type', 'application/json');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
return res.status(409).send(JSON.stringify({ message: `The server copy is out of sync with the saved brew. Please save your changes elsewhere, refresh, and try again.` }));
|
return res.status(409).send(JSON.stringify({ message: `The server copy is out of sync with the saved brew. Please save your changes elsewhere, refresh, and try again.` }));
|
||||||
}
|
}
|
||||||
@@ -369,6 +369,7 @@ const api = {
|
|||||||
const patchedResult = applyPatches(patches, brewFromServer.text)[0];
|
const patchedResult = applyPatches(patches, brewFromServer.text)[0];
|
||||||
// brew.text = applyPatches(patches, brewFromServer.text)[0];
|
// brew.text = applyPatches(patches, brewFromServer.text)[0];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
debugTextMismatch(brewFromClient.text, brewFromServer.text, `edit/${brewFromClient.editId}`);
|
||||||
console.error('Failed to apply patches:', {
|
console.error('Failed to apply patches:', {
|
||||||
patches : brewFromClient.patches,
|
patches : brewFromClient.patches,
|
||||||
brewId : brew.editId || 'unknown',
|
brewId : brew.editId || 'unknown',
|
||||||
|
|||||||
@@ -139,9 +139,45 @@ const fetchThemeBundle = async (obj, renderer, theme)=>{
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const debugTextMismatch = (clientTextRaw, serverTextRaw, label) => {
|
||||||
|
const clientText = clientTextRaw?.normalize('NFC') || '';
|
||||||
|
const serverText = serverTextRaw?.normalize('NFC') || '';
|
||||||
|
|
||||||
|
const clientBuffer = Buffer.from(clientText, 'utf8');
|
||||||
|
const serverBuffer = Buffer.from(serverText, 'utf8');
|
||||||
|
|
||||||
|
if (clientBuffer.equals(serverBuffer)) {
|
||||||
|
console.log(`✅ ${label} text matches byte-for-byte.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn(`❗${label} text mismatch detected.`);
|
||||||
|
console.log(`Client length: ${clientBuffer.length}`);
|
||||||
|
console.log(`Server length: ${serverBuffer.length}`);
|
||||||
|
|
||||||
|
// Byte-level diff
|
||||||
|
for (let i = 0; i < Math.min(clientBuffer.length, serverBuffer.length); i++) {
|
||||||
|
if (clientBuffer[i] !== serverBuffer[i]) {
|
||||||
|
console.log(`Byte mismatch at offset ${i}: client=0x${clientBuffer[i].toString(16)} server=0x${serverBuffer[i].toString(16)}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Char-level diff
|
||||||
|
for (let i = 0; i < Math.min(clientText.length, serverText.length); i++) {
|
||||||
|
if (clientText[i] !== serverText[i]) {
|
||||||
|
console.log(`Char mismatch at index ${i}:`);
|
||||||
|
console.log(` Client: '${clientText[i]}' (U+${clientText.charCodeAt(i).toString(16).toUpperCase()})`);
|
||||||
|
console.log(` Server: '${serverText[i]}' (U+${serverText.charCodeAt(i).toString(16).toUpperCase()})`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
splitTextStyleAndMetadata,
|
splitTextStyleAndMetadata,
|
||||||
printCurrentBrew,
|
printCurrentBrew,
|
||||||
fetchThemeBundle,
|
fetchThemeBundle,
|
||||||
brewSnippetsToJSON
|
brewSnippetsToJSON,
|
||||||
|
debugTextMismatch
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user