fix(server): avoid saving invalid data (#4859)

This commit is contained in:
liuyi 2023-11-07 16:27:17 +08:00 committed by GitHub
parent ac0a3aab3e
commit 01d7fe4597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,14 @@ function compare(yBinary: Buffer, jwstBinary: Buffer, strict = false): boolean {
return compare(yBinary, yBinary2, true);
}
function isEmptyBuffer(buf: Buffer): boolean {
return (
buf.length == 0 ||
// 0x0000
(buf.length === 2 && buf[0] === 0 && buf[1] === 0)
);
}
const MAX_SEQ_NUM = 0x3fffffff; // u31
/**
@ -377,6 +385,11 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
) {
const blob = Buffer.from(encodeStateAsUpdate(doc));
const state = Buffer.from(encodeStateVector(doc));
if (isEmptyBuffer(blob)) {
return null;
}
return this.db.snapshot.upsert({
where: {
id_workspaceId: {