feat(nbstore): add blob sync (#8996)

This commit is contained in:
EYHN
2024-12-07 08:05:03 +00:00
parent f54f6e88cb
commit 7225f59138
7 changed files with 214 additions and 19 deletions

View File

@@ -3,11 +3,15 @@ import 'fake-indexeddb/auto';
import { expect, test } from 'vitest';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { IndexedDBDocStorage, IndexedDBSyncStorage } from '../impls/idb';
import {
IndexedDBBlobStorage,
IndexedDBDocStorage,
IndexedDBSyncStorage,
} from '../impls/idb';
import { SpaceStorage } from '../storage';
import { SyncEngine } from '../sync';
test('sync', async () => {
test('doc', async () => {
const doc = new YDoc();
doc.getMap('test').set('hello', 'world');
const update = encodeStateAsUpdate(doc);
@@ -83,3 +87,69 @@ test('sync', async () => {
expect(c?.bin).toEqual(update2);
}
});
test('blob', async () => {
const a = new IndexedDBBlobStorage({
id: 'ws1',
peer: 'a',
type: 'workspace',
});
const b = new IndexedDBBlobStorage({
id: 'ws1',
peer: 'b',
type: 'workspace',
});
const c = new IndexedDBBlobStorage({
id: 'ws1',
peer: 'c',
type: 'workspace',
});
await a.set({
key: 'test',
data: new Uint8Array([1, 2, 3, 4]),
mime: 'text/plain',
createdAt: new Date(100),
});
await c.set({
key: 'test2',
data: new Uint8Array([4, 3, 2, 1]),
mime: 'text/plain',
createdAt: new Date(100),
});
const peerA = new SpaceStorage([a]);
const peerB = new SpaceStorage([b]);
const peerC = new SpaceStorage([c]);
await peerA.connect();
await peerB.connect();
await peerC.connect();
const sync = new SyncEngine(peerA, [peerB, peerC]);
const abort = new AbortController();
sync.run(abort.signal);
await new Promise(resolve => setTimeout(resolve, 1000));
{
const a = await peerA.get('blob').get('test');
expect(a).not.toBeNull();
expect(a?.data).toEqual(new Uint8Array([1, 2, 3, 4]));
}
{
const b = await peerB.get('blob').get('test');
expect(b).not.toBeNull();
expect(b?.data).toEqual(new Uint8Array([1, 2, 3, 4]));
}
{
const c = await peerC.get('blob').get('test2');
expect(c).not.toBeNull();
expect(c?.data).toEqual(new Uint8Array([4, 3, 2, 1]));
}
});