mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 22:37:04 +08:00
feat(nbstore): add blob sync storage (#10752)
This commit is contained in:
30
packages/common/nbstore/src/storage/blob-sync.ts
Normal file
30
packages/common/nbstore/src/storage/blob-sync.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { Connection } from '../connection';
|
||||
import type { Storage } from './storage';
|
||||
|
||||
export interface BlobSyncStorage extends Storage {
|
||||
readonly storageType: 'blobSync';
|
||||
|
||||
setBlobUploadedAt(
|
||||
peer: string,
|
||||
blobId: string,
|
||||
uploadedAt: Date | null
|
||||
): Promise<void>;
|
||||
|
||||
getBlobUploadedAt(peer: string, blobId: string): Promise<Date | null>;
|
||||
}
|
||||
|
||||
export abstract class BlobSyncStorageBase implements BlobSyncStorage {
|
||||
readonly storageType = 'blobSync';
|
||||
abstract readonly connection: Connection;
|
||||
|
||||
abstract setBlobUploadedAt(
|
||||
peer: string,
|
||||
blobId: string,
|
||||
uploadedAt: Date | null
|
||||
): Promise<void>;
|
||||
|
||||
abstract getBlobUploadedAt(
|
||||
peer: string,
|
||||
blobId: string
|
||||
): Promise<Date | null>;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ export interface ListedBlobRecord {
|
||||
|
||||
export interface BlobStorage extends Storage {
|
||||
readonly storageType: 'blob';
|
||||
readonly isReadonly: boolean;
|
||||
get(key: string, signal?: AbortSignal): Promise<BlobRecord | null>;
|
||||
set(blob: BlobRecord, signal?: AbortSignal): Promise<void>;
|
||||
delete(
|
||||
@@ -31,7 +32,7 @@ export interface BlobStorage extends Storage {
|
||||
export abstract class BlobStorageBase implements BlobStorage {
|
||||
readonly storageType = 'blob';
|
||||
abstract readonly connection: Connection;
|
||||
|
||||
abstract readonly isReadonly: boolean;
|
||||
abstract get(key: string, signal?: AbortSignal): Promise<BlobRecord | null>;
|
||||
abstract set(blob: BlobRecord, signal?: AbortSignal): Promise<void>;
|
||||
abstract delete(
|
||||
|
||||
14
packages/common/nbstore/src/storage/dummy/blob-sync.ts
Normal file
14
packages/common/nbstore/src/storage/dummy/blob-sync.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { type Connection, DummyConnection } from '../../connection';
|
||||
import type { BlobSyncStorage } from '../blob-sync';
|
||||
|
||||
export class DummyBlobSyncStorage implements BlobSyncStorage {
|
||||
storageType = 'blobSync' as const;
|
||||
connection: Connection<any> = new DummyConnection();
|
||||
|
||||
setBlobUploadedAt(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
getBlobUploadedAt(): Promise<Date | null> {
|
||||
return Promise.resolve(new Date());
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from '../blob';
|
||||
|
||||
export class DummyBlobStorage extends BlobStorageBase {
|
||||
override readonly isReadonly = true;
|
||||
override get(
|
||||
_key: string,
|
||||
_signal?: AbortSignal
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from './over-capacity';
|
||||
export * from './over-size';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export class OverCapacityError extends Error {
|
||||
constructor(public originError?: any) {
|
||||
super('Storage over capacity. Origin error: ' + originError);
|
||||
super('Storage over capacity.');
|
||||
}
|
||||
}
|
||||
|
||||
5
packages/common/nbstore/src/storage/errors/over-size.ts
Normal file
5
packages/common/nbstore/src/storage/errors/over-size.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export class OverSizeError extends Error {
|
||||
constructor(public originError?: any) {
|
||||
super('Blob size exceeds the limit.');
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,22 @@ import EventEmitter2 from 'eventemitter2';
|
||||
|
||||
import type { AwarenessStorage } from './awareness';
|
||||
import type { BlobStorage } from './blob';
|
||||
import type { BlobSyncStorage } from './blob-sync';
|
||||
import type { DocStorage } from './doc';
|
||||
import type { DocSyncStorage } from './doc-sync';
|
||||
import { DummyAwarenessStorage } from './dummy/awareness';
|
||||
import { DummyBlobStorage } from './dummy/blob';
|
||||
import { DummyBlobSyncStorage } from './dummy/blob-sync';
|
||||
import { DummyDocStorage } from './dummy/doc';
|
||||
import { DummyDocSyncStorage } from './dummy/doc-sync';
|
||||
import type { StorageType } from './storage';
|
||||
|
||||
type Storages = DocStorage | BlobStorage | DocSyncStorage | AwarenessStorage;
|
||||
type Storages =
|
||||
| DocStorage
|
||||
| BlobStorage
|
||||
| BlobSyncStorage
|
||||
| DocSyncStorage
|
||||
| AwarenessStorage;
|
||||
|
||||
export type SpaceStorageOptions = {
|
||||
[K in StorageType]?: Storages & { storageType: K };
|
||||
@@ -27,8 +34,9 @@ export class SpaceStorage {
|
||||
this.storages = {
|
||||
awareness: storages.awareness ?? new DummyAwarenessStorage(),
|
||||
blob: storages.blob ?? new DummyBlobStorage(),
|
||||
blobSync: storages.blobSync ?? new DummyBlobSyncStorage(),
|
||||
doc: storages.doc ?? new DummyDocStorage(),
|
||||
['docSync']: storages['docSync'] ?? new DummyDocSyncStorage(),
|
||||
docSync: storages.docSync ?? new DummyDocSyncStorage(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -70,6 +78,7 @@ export class SpaceStorage {
|
||||
|
||||
export * from './awareness';
|
||||
export * from './blob';
|
||||
export * from './blob-sync';
|
||||
export * from './doc';
|
||||
export * from './doc-sync';
|
||||
export * from './errors';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Connection } from '../connection';
|
||||
|
||||
export type StorageType = 'blob' | 'doc' | 'docSync' | 'awareness';
|
||||
export type StorageType = 'blob' | 'blobSync' | 'doc' | 'docSync' | 'awareness';
|
||||
|
||||
export interface Storage {
|
||||
readonly storageType: StorageType;
|
||||
|
||||
Reference in New Issue
Block a user