mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-26 01:43:01 +03:00
fix(server): reduce yjs binary size (#8681)
This commit is contained in:
parent
cdaac5602c
commit
63b58aad5c
@ -40,25 +40,26 @@ export class DocStorageOptions implements IDocStorageOptions {
|
||||
) {}
|
||||
|
||||
mergeUpdates = async (updates: Uint8Array[]) => {
|
||||
const doc = await this.recoverDoc(updates);
|
||||
const yjsResult = Buffer.from(Y.encodeStateAsUpdate(doc));
|
||||
|
||||
const useYocto = await this.config.runtime.fetch(
|
||||
'doc/experimentalMergeWithYOcto'
|
||||
);
|
||||
|
||||
if (useYocto) {
|
||||
const doc = await this.recoverDoc(updates);
|
||||
|
||||
metrics.jwst.counter('codec_merge_counter').add(1);
|
||||
const yjsResult = Buffer.from(Y.encodeStateAsUpdate(doc));
|
||||
let log = false;
|
||||
let yoctoResult: Buffer | null = null;
|
||||
try {
|
||||
const yocto = yotcoMergeUpdates(updates.map(Buffer.from));
|
||||
if (!compare(yjsResult, yocto)) {
|
||||
yoctoResult = yotcoMergeUpdates(updates.map(Buffer.from));
|
||||
if (!compare(yjsResult, yoctoResult)) {
|
||||
metrics.jwst.counter('codec_not_match').add(1);
|
||||
this.logger.warn(`yocto codec result doesn't match yjs codec result`);
|
||||
log = true;
|
||||
if (this.config.node.dev) {
|
||||
this.logger.warn(`Expected:\n ${yjsResult.toString('hex')}`);
|
||||
this.logger.warn(`Result:\n ${yocto.toString('hex')}`);
|
||||
this.logger.warn(`Result:\n ${yoctoResult.toString('hex')}`);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@ -73,10 +74,16 @@ export class DocStorageOptions implements IDocStorageOptions {
|
||||
);
|
||||
}
|
||||
|
||||
return yjsResult;
|
||||
} else {
|
||||
return this.simpleMergeUpdates(updates);
|
||||
if (
|
||||
this.config.affine.canary &&
|
||||
yoctoResult &&
|
||||
yoctoResult.length > 2 /* simple test for non-empty yjs binary */
|
||||
) {
|
||||
return yoctoResult;
|
||||
}
|
||||
}
|
||||
|
||||
return yjsResult;
|
||||
};
|
||||
|
||||
historyMaxAge = async (spaceId: string) => {
|
||||
@ -89,11 +96,6 @@ export class DocStorageOptions implements IDocStorageOptions {
|
||||
return this.config.doc.history.interval;
|
||||
};
|
||||
|
||||
@CallMetric('doc', 'yjs_merge_updates')
|
||||
private simpleMergeUpdates(updates: Uint8Array[]) {
|
||||
return Y.mergeUpdates(updates);
|
||||
}
|
||||
|
||||
@CallMetric('doc', 'yjs_recover_updates_to_doc')
|
||||
private recoverDoc(updates: Uint8Array[]): Promise<Y.Doc> {
|
||||
const doc = new Y.Doc();
|
||||
|
Loading…
Reference in New Issue
Block a user