Skip to content

Commit 6e52aa3

Browse files
committed
fix(sync): Only send recovery step in case of SyncStep1
Signed-off-by: Jonas <jonas@freesources.org>
1 parent 4ddfd75 commit 6e52aa3

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/services/WebSocketPolyfill.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { decodeArrayBuffer } from '../helpers/base64.js'
6+
import { decodeArrayBuffer, encodeArrayBuffer } from '../helpers/base64.js'
77
import { logger } from '../helpers/logger.js'
88
import getNotifyBus from './NotifyService'
99
import type { Step, SyncService } from './SyncService.js'
@@ -78,16 +78,24 @@ export default function initWebSocketPolyfill(
7878
send(step: Uint8Array<ArrayBufferLike>) {
7979
// Useful for debugging what steps are sent and how they were initiated
8080
// logStep(step)
81-
if (this.#processingVersion) {
82-
// this is a direct response while processing the step
83-
console.error(`Failed to process step ${this.#processingVersion}.`, {
84-
lastSuccessfullyProcessed: syncService.version,
85-
sendingSyncStep1: step,
86-
})
87-
// Do not increase the syncService.version for the current steps
88-
// as we failed to process them.
89-
this.#processingVersion = 0
81+
82+
const encoded = encodeArrayBuffer(step)
83+
const isSyncStep1 = encoded < 'AAE'
84+
if (!this.#processingVersion || !isSyncStep1) {
85+
syncService.sendStep(step)
86+
return
9087
}
88+
89+
// If `this.#processingVersion` is set, we're in the middle of applying steps of one version.
90+
// If `isSyncStep1`, Yjs failed to integrate a message due to pending structs.
91+
// Log and ask for recovery due to a not applied/missing step.
92+
console.error(`Failed to process step ${this.#processingVersion}.`, {
93+
lastSuccessfullyProcessed: syncService.version,
94+
sendingSyncStep1: step,
95+
})
96+
// Do not increase the syncService.version for the current steps
97+
// as we failed to process them.
98+
this.#processingVersion = 0
9199
syncService.sendRecoveryStep(step)
92100
}
93101

0 commit comments

Comments
 (0)