Skip to content

Commit 39cb1af

Browse files
committed
fix(server): limit rootDoc snapshot size (#12625)
close CLOUD-225 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for reading document blocks without requiring a workspace or root document snapshot. - **Bug Fixes** - Improved handling of large workspace snapshots by skipping them when they exceed 10MB. - **Tests** - Introduced new test cases to cover scenarios where root or workspace snapshots are absent. - Expanded snapshot tests for document block reading. - **Refactor** - Updated several function signatures to make root and workspace snapshot parameters optional for greater flexibility. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 1eb9e62 commit 39cb1af

File tree

8 files changed

+2367
-14
lines changed

8 files changed

+2367
-14
lines changed

packages/backend/server/src/core/utils/__tests__/__snapshots__/blocksute.spec.ts.md

Lines changed: 631 additions & 0 deletions
Large diffs are not rendered by default.

packages/backend/server/src/core/utils/__tests__/blocksute.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,23 @@ test('can read all blocks from doc snapshot', async t => {
4545

4646
const result = await readAllBlocksFromDocSnapshot(
4747
workspace.id,
48-
rootDoc!.blob,
48+
'doc-0',
49+
docSnapshot.blob,
50+
rootDoc!.blob
51+
);
52+
53+
t.snapshot({
54+
...result,
55+
blocks: result!.blocks.map(block => omit(block, ['yblock'])),
56+
});
57+
});
58+
59+
test('can read all blocks from doc snapshot without workspace snapshot', async t => {
60+
const doc = await models.doc.get(workspace.id, docSnapshot.id);
61+
t.truthy(doc);
62+
63+
const result = await readAllBlocksFromDocSnapshot(
64+
workspace.id,
4965
'doc-0',
5066
docSnapshot.blob
5167
);

packages/backend/server/src/core/utils/blocksuite.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,18 @@ export function readAllDocIdsFromWorkspaceSnapshot(snapshot: Uint8Array) {
173173

174174
export async function readAllBlocksFromDocSnapshot(
175175
workspaceId: string,
176-
workspaceSnapshot: Uint8Array,
177176
docId: string,
178177
docSnapshot: Uint8Array,
178+
workspaceSnapshot?: Uint8Array,
179179
maxSummaryLength?: number
180180
) {
181-
const rootYDoc = new YDoc({
182-
guid: workspaceId,
183-
});
184-
applyUpdate(rootYDoc, workspaceSnapshot);
181+
let rootYDoc: YDoc | undefined;
182+
if (workspaceSnapshot) {
183+
rootYDoc = new YDoc({
184+
guid: workspaceId,
185+
});
186+
applyUpdate(rootYDoc, workspaceSnapshot);
187+
}
185188
const ydoc = new YDoc({
186189
guid: docId,
187190
});

packages/backend/server/src/plugins/indexer/service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,14 @@ export class IndexerService {
226226
this.logger.debug(`doc ${workspaceId}/${docId} is empty, skip indexing`);
227227
return;
228228
}
229+
const MAX_WORKSPACE_SNAPSHOT_SIZE = 1024 * 1024 * 10; // 10MB
229230
const result = await readAllBlocksFromDocSnapshot(
230231
workspaceId,
231-
workspaceSnapshot.blob,
232232
docId,
233-
docSnapshot.blob
233+
docSnapshot.blob,
234+
workspaceSnapshot.blob.length < MAX_WORKSPACE_SNAPSHOT_SIZE
235+
? workspaceSnapshot.blob
236+
: undefined
234237
);
235238
if (!result) {
236239
this.logger.warn(

0 commit comments

Comments
 (0)