feat: native sync state (#14190)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Added indexed clock management capabilities for documents, enabling
get, set, and clear operations across Android, iOS, Electron, and web
platforms.

* **Refactor**
* Improved storage architecture to dynamically select platform-specific
implementations (SQLite for Electron, IndexedDB for others).

* **Bug Fixes**
* Enhanced document operations to properly maintain and clean up indexer
synchronization state during document lifecycle changes.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky
2025-12-31 04:09:32 +08:00
committed by GitHub
parent 95ef04f3e0
commit 99332228da
18 changed files with 375 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
import type { CrawlResult } from '@affine/nbstore';
import type { CrawlResult, DocIndexedClock } from '@affine/nbstore';
export interface Blob {
key: string;
@@ -184,7 +184,21 @@ export interface NbStorePlugin {
indexName: string;
docId: string;
query: string;
}) => Promise<{ matches: Array<{ start: number; end: number }> }>;
}) => Promise<{ matches: { start: number; end: number }[] }>;
ftsFlushIndex: (options: { id: string }) => Promise<void>;
ftsIndexVersion: () => Promise<{ indexVersion: number }>;
getDocIndexedClock: (options: {
id: string;
docId: string;
}) => Promise<DocIndexedClock | null>;
setDocIndexedClock: (options: {
id: string;
docId: string;
indexedClock: number;
indexerVersion: number;
}) => Promise<void>;
clearDocIndexedClock: (options: {
id: string;
docId: string;
}) => Promise<void>;
}

View File

@@ -4,7 +4,9 @@ import {
} from '@affine/core/modules/workspace-engine';
import {
type BlobRecord,
type CrawlResult,
type DocClock,
type DocIndexedClock,
type DocRecord,
type ListedBlobRecord,
parseUniversalId,
@@ -321,7 +323,7 @@ export const NbStoreNativeDBApis: NativeDBApis = {
peer,
blobId,
});
return result?.uploadedAt ? new Date(result.uploadedAt) : null;
return result.uploadedAt ? new Date(result.uploadedAt) : null;
},
setBlobUploadedAt: async function (
id: string,
@@ -336,8 +338,11 @@ export const NbStoreNativeDBApis: NativeDBApis = {
uploadedAt: uploadedAt ? uploadedAt.getTime() : null,
});
},
crawlDocData: async function (id: string, docId: string) {
return NbStore.crawlDocData({ id, docId });
crawlDocData: async function (
id: string,
docId: string
): Promise<CrawlResult> {
return await NbStore.crawlDocData({ id, docId });
},
ftsAddDocument: async function (
id: string,
@@ -411,4 +416,29 @@ export const NbStoreNativeDBApis: NativeDBApis = {
ftsIndexVersion: function (): Promise<number> {
return NbStore.ftsIndexVersion().then(res => res.indexVersion);
},
getDocIndexedClock: function (
id: string,
docId: string
): Promise<DocIndexedClock | null> {
return NbStore.getDocIndexedClock({ id, docId });
},
setDocIndexedClock: function (
id: string,
docId: string,
indexedClock: Date,
indexerVersion: number
): Promise<void> {
return NbStore.setDocIndexedClock({
id,
docId,
indexedClock: indexedClock.getTime(),
indexerVersion,
});
},
clearDocIndexedClock: function (id: string, docId: string): Promise<void> {
return NbStore.clearDocIndexedClock({
id,
docId,
});
},
};

View File

@@ -3,7 +3,6 @@ import '@affine/core/bootstrap/electron';
import { apis } from '@affine/electron-api';
import { broadcastChannelStorages } from '@affine/nbstore/broadcast-channel';
import { cloudStorages } from '@affine/nbstore/cloud';
import { idbStoragesIndexerOnly } from '@affine/nbstore/idb';
import { bindNativeDBApis, sqliteStorages } from '@affine/nbstore/sqlite';
import {
bindNativeDBV1Apis,
@@ -21,7 +20,6 @@ bindNativeDBApis(apis!.nbstore);
bindNativeDBV1Apis(apis!.db);
const storeManager = new StoreManagerConsumer([
...idbStoragesIndexerOnly,
...sqliteStorages,
...sqliteV1Storages,
...broadcastChannelStorages,

View File

@@ -30,6 +30,9 @@ export const nbstoreHandlers: NativeDBApis = {
deleteDoc: POOL.deleteDoc.bind(POOL),
getDocClocks: POOL.getDocClocks.bind(POOL),
getDocClock: POOL.getDocClock.bind(POOL),
getDocIndexedClock: POOL.getDocIndexedClock.bind(POOL),
setDocIndexedClock: POOL.setDocIndexedClock.bind(POOL),
clearDocIndexedClock: POOL.clearDocIndexedClock.bind(POOL),
getBlob: POOL.getBlob.bind(POOL),
setBlob: POOL.setBlob.bind(POOL),
deleteBlob: POOL.deleteBlob.bind(POOL),

View File

@@ -127,6 +127,12 @@ const needRefererDomains = [
/^(?:[a-zA-Z0-9-]+\.)*googlevideo\.com$/,
];
const defaultReferer = 'https://client.affine.local/';
const affineDomains = [
/^(?:[a-z0-9-]+\.)*usercontent\.affine\.pro$/i,
/^(?:[a-z0-9-]+\.)*affine\.pro$/i,
/^(?:[a-z0-9-]+\.)*affine\.fail$/i,
/^(?:[a-z0-9-]+\.)*affine\.run$/i,
];
function setHeader(
headers: Record<string, string[]>,
@@ -166,6 +172,17 @@ function ensureFrameAncestors(
});
}
function allowCors(headers: Record<string, string[]>) {
// Signed blob URLs redirect to *.usercontent.affine.pro without CORS headers.
setHeader(headers, 'Access-Control-Allow-Origin', '*');
setHeader(headers, 'Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
setHeader(
headers,
'Access-Control-Allow-Headers',
'*, Authorization, Content-Type, Range'
);
}
export function registerProtocol() {
protocol.handle('assets', request => {
return handleFileRequest(request);
@@ -211,9 +228,9 @@ export function registerProtocol() {
}
}
const { protocol } = new URL(url);
const { protocol, hostname } = new URL(url);
// Only adjust CORS for assets responses; leave remote http(s) headers intact
// Adjust CORS for assets responses and allow blob redirects on affine domains
if (protocol === 'assets:') {
delete responseHeaders['access-control-allow-origin'];
delete responseHeaders['access-control-allow-headers'];
@@ -221,6 +238,11 @@ export function registerProtocol() {
delete responseHeaders['Access-Control-Allow-Headers'];
setHeader(responseHeaders, 'X-Frame-Options', 'SAMEORIGIN');
ensureFrameAncestors(responseHeaders, "'self'");
} else if (
(protocol === 'http:' || protocol === 'https:') &&
affineDomains.some(regex => regex.test(hostname))
) {
allowCors(responseHeaders);
}
}
})()

View File

@@ -1,4 +1,4 @@
import type { CrawlResult } from '@affine/nbstore';
import type { CrawlResult, DocIndexedClock } from '@affine/nbstore';
export interface Blob {
key: string;
@@ -187,4 +187,18 @@ export interface NbStorePlugin {
}) => Promise<{ matches: { start: number; end: number }[] }>;
ftsFlushIndex: (options: { id: string }) => Promise<void>;
ftsIndexVersion: () => Promise<{ indexVersion: number }>;
getDocIndexedClock: (options: {
id: string;
docId: string;
}) => Promise<DocIndexedClock | null>;
setDocIndexedClock: (options: {
id: string;
docId: string;
indexedClock: number;
indexerVersion: number;
}) => Promise<void>;
clearDocIndexedClock: (options: {
id: string;
docId: string;
}) => Promise<void>;
}

View File

@@ -6,6 +6,7 @@ import {
type BlobRecord,
type CrawlResult,
type DocClock,
type DocIndexedClock,
type DocRecord,
type ListedBlobRecord,
parseUniversalId,
@@ -415,4 +416,29 @@ export const NbStoreNativeDBApis: NativeDBApis = {
ftsIndexVersion: function (): Promise<number> {
return NbStore.ftsIndexVersion().then(res => res.indexVersion);
},
getDocIndexedClock: function (
id: string,
docId: string
): Promise<DocIndexedClock | null> {
return NbStore.getDocIndexedClock({ id, docId });
},
setDocIndexedClock: function (
id: string,
docId: string,
indexedClock: Date,
indexerVersion: number
): Promise<void> {
return NbStore.setDocIndexedClock({
id,
docId,
indexedClock: indexedClock.getTime(),
indexerVersion,
});
},
clearDocIndexedClock: function (id: string, docId: string): Promise<void> {
return NbStore.clearDocIndexedClock({
id,
docId,
});
},
};