snapshot of https://github.com/toeverything/AFFiNE.git (canary, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 4s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Analyze (javascript, affine) (push) Failing after 3s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled

This commit is contained in:
upstream-mirror
2026-09-16 00:31:07 +05:00
commit 50f788e799
7907 changed files with 988785 additions and 0 deletions

View File

@@ -0,0 +1,205 @@
import { type Logger, sha } from '@blocksuite/global/utils';
import type { BlobSource } from './source.js';
export interface BlobStatus {
isStorageOverCapacity: boolean;
}
/**
* # BlobEngine
*
* sync blobs between storages in background.
*
* all operations priority use main, then use shadows.
*/
export class BlobEngine {
private _abort: AbortController | null = null;
get sources() {
return [this.main, ...this.shadows];
}
constructor(
readonly main: BlobSource,
readonly shadows: BlobSource[],
readonly logger: Logger
) {}
async delete(_key: string) {
this.logger.error(
'You are trying to delete a blob. We do not support this feature yet. We need to wait until we implement the indexer, which will inform us which doc is using a particular blob so that we can safely delete it.'
);
}
async get(key: string) {
this.logger.debug('get blob', key);
for (const source of this.sources) {
const data = await source.get(key);
if (data) {
return data;
}
}
return null;
}
async list() {
const blobIdSet = new Set<string>();
for (const source of this.sources) {
const blobs = await source.list();
for (const blob of blobs) {
blobIdSet.add(blob);
}
}
return Array.from(blobIdSet);
}
async set(value: Blob): Promise<string>;
async set(key: string, value: Blob): Promise<string>;
async set(valueOrKey: string | Blob, _value?: Blob) {
if (this.main.readonly) {
throw new Error('main peer is readonly');
}
const key =
typeof valueOrKey === 'string'
? valueOrKey
: await sha(await valueOrKey.arrayBuffer());
const value = typeof valueOrKey === 'string' ? _value : valueOrKey;
if (!value) {
throw new Error('value is empty');
}
// await upload to the main peer
await this.main.set(key, value);
// uploads to other peers in the background
Promise.allSettled(
this.shadows
.filter(r => !r.readonly)
.map(peer =>
peer.set(key, value).catch(err => {
this.logger.error('Error when uploading to peer', err);
})
)
)
.then(result => {
if (result.some(({ status }) => status === 'rejected')) {
this.logger.error(
`blob ${key} update finish, but some peers failed to update`
);
} else {
this.logger.debug(`blob ${key} update finish`);
}
})
.catch(() => {
// Promise.allSettled never reject
});
return key;
}
blobState$(key: string) {
return this.main.blobState$?.(key) ?? null;
}
upload(key: string) {
return this.main.upload?.(key) ?? null;
}
start() {
if (this._abort) {
return;
}
this._abort = new AbortController();
const abortSignal = this._abort.signal;
const sync = () => {
if (abortSignal.aborted) {
return;
}
this.sync()
.catch(error => {
this.logger.error('sync blob error', error);
})
.finally(() => {
// sync every 1 minute
setTimeout(sync, 60000);
});
};
sync();
}
stop() {
this._abort?.abort();
this._abort = null;
}
async sync() {
if (this.main.readonly) {
return;
}
this.logger.debug('start syncing blob...');
for (const shadow of this.shadows) {
let mainList: string[] = [];
let shadowList: string[] = [];
if (!shadow.readonly) {
try {
mainList = await this.main.list();
shadowList = await shadow.list();
} catch (err) {
this.logger.error(`error when sync`, err);
continue;
}
const needUpload = mainList.filter(key => !shadowList.includes(key));
for (const key of needUpload) {
try {
const data = await this.main.get(key);
if (data) {
await shadow.set(key, data);
} else {
this.logger.error(
'data not found when trying upload from main to shadow'
);
}
} catch (err) {
this.logger.error(
`error when sync ${key} from [${this.main.name}] to [${shadow.name}]`,
err
);
}
}
}
const needDownload = shadowList.filter(key => !mainList.includes(key));
for (const key of needDownload) {
try {
const data = await shadow.get(key);
if (data) {
await this.main.set(key, data);
} else {
this.logger.error(
'data not found when trying download from shadow to main'
);
}
} catch (err) {
this.logger.error(
`error when sync ${key} from [${shadow.name}] to [${this.main.name}]`,
err
);
}
}
}
this.logger.debug('finish syncing blob');
}
}

View File

@@ -0,0 +1,2 @@
export * from './indexeddb.js';
export * from './memory.js';

View File

@@ -0,0 +1,39 @@
import { createStore, del, get, keys, set } from 'idb-keyval';
import type { BlobSource } from '../source.js';
export class IndexedDBBlobSource implements BlobSource {
readonly mimeTypeStore = createStore(`${this.name}_blob_mime`, 'blob_mime');
readonly = false;
readonly store = createStore(`${this.name}_blob`, 'blob');
constructor(readonly name: string) {}
async delete(key: string) {
await del(key, this.store);
await del(key, this.mimeTypeStore);
}
async get(key: string) {
const res = await get<ArrayBuffer>(key, this.store);
if (res) {
return new Blob([res], {
type: await get(key, this.mimeTypeStore),
});
}
return null;
}
async list() {
const list = await keys<string>(this.store);
return list;
}
async set(key: string, value: Blob) {
await set(key, await value.arrayBuffer(), this.store);
await set(key, value.type, this.mimeTypeStore);
return key;
}
}

View File

@@ -0,0 +1,27 @@
import type { BlobSource } from '../source.js';
export class MemoryBlobSource implements BlobSource {
readonly map = new Map<string, Blob>();
name = 'memory';
readonly = false;
delete(key: string) {
this.map.delete(key);
return Promise.resolve();
}
get(key: string) {
return Promise.resolve(this.map.get(key) ?? null);
}
list() {
return Promise.resolve(Array.from(this.map.keys()));
}
set(key: string, value: Blob) {
this.map.set(key, value);
return Promise.resolve(key);
}
}

View File

@@ -0,0 +1,3 @@
export * from './engine.js';
export * from './impl/index.js';
export * from './source.js';

View File

@@ -0,0 +1,23 @@
import type { Observable } from 'rxjs';
export interface BlobState {
uploading: boolean;
downloading: boolean;
errorMessage?: string | null;
overSize: boolean;
needUpload: boolean;
needDownload: boolean;
}
export interface BlobSource {
name: string;
readonly: boolean;
get: (key: string) => Promise<Blob | null>;
set: (key: string, value: Blob) => Promise<string>;
delete: (key: string) => Promise<void>;
list: () => Promise<string[]>;
// This state is only available when uploading to the server or downloading from the server.
blobState$?: (key: string) => Observable<BlobState> | null;
// Re-upload to the server.
upload?: (key: string) => Promise<boolean>;
}