@dxos/echo-pipeline 0.4.9 → 0.4.10-main.05b9ab6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/{chunk-RTEEJ723.mjs → chunk-KMWJLYEQ.mjs} +77 -57
- package/dist/lib/browser/chunk-KMWJLYEQ.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +617 -217
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +10 -2
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-7VZVCCNF.cjs → chunk-YZA42CKA.cjs} +82 -62
- package/dist/lib/node/chunk-YZA42CKA.cjs.map +7 -0
- package/dist/lib/node/index.cjs +635 -237
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +20 -13
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/types/src/automerge/automerge-doc-loader.d.ts +66 -0
- package/dist/types/src/automerge/automerge-doc-loader.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-doc-loader.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-doc-loader.test.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-host.d.ts +21 -18
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-repo.test.d.ts.map +1 -0
- package/dist/types/src/automerge/index.d.ts +4 -0
- package/dist/types/src/automerge/index.d.ts.map +1 -1
- package/dist/types/src/automerge/level.test.d.ts +2 -0
- package/dist/types/src/automerge/level.test.d.ts.map +1 -0
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +30 -0
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -0
- package/dist/types/src/automerge/local-host-network-adapter.d.ts +8 -1
- package/dist/types/src/automerge/local-host-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/migrations.d.ts +7 -0
- package/dist/types/src/automerge/migrations.d.ts.map +1 -0
- package/dist/types/src/automerge/reference.d.ts +15 -0
- package/dist/types/src/automerge/reference.d.ts.map +1 -0
- package/dist/types/src/automerge/storage-adapter.test.d.ts +2 -0
- package/dist/types/src/automerge/storage-adapter.test.d.ts.map +1 -0
- package/dist/types/src/automerge/types.d.ts +73 -0
- package/dist/types/src/automerge/types.d.ts.map +1 -0
- package/dist/types/src/metadata/metadata-store.d.ts +2 -1
- package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
- package/dist/types/src/space/control-pipeline.d.ts +3 -1
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +3 -1
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +6 -9
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/level.d.ts +3 -0
- package/dist/types/src/testing/level.d.ts.map +1 -0
- package/dist/types/src/testing/test-agent-builder.d.ts +2 -2
- package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
- package/package.json +33 -30
- package/src/automerge/automerge-doc-loader.test.ts +97 -0
- package/src/automerge/automerge-doc-loader.ts +244 -0
- package/src/automerge/automerge-host.test.ts +22 -8
- package/src/automerge/automerge-host.ts +66 -118
- package/src/automerge/automerge-repo.test.ts +29 -0
- package/src/automerge/index.ts +4 -0
- package/src/automerge/level.test.ts +82 -0
- package/src/automerge/leveldb-storage-adapter.ts +117 -0
- package/src/automerge/local-host-network-adapter.ts +19 -13
- package/src/automerge/migrations.ts +41 -0
- package/src/automerge/reference.ts +31 -0
- package/src/automerge/storage-adapter.test.ts +90 -0
- package/src/automerge/types.ts +86 -0
- package/src/db-host/data-service.ts +1 -1
- package/src/metadata/metadata-store.ts +17 -8
- package/src/space/control-pipeline.ts +11 -1
- package/src/space/space-manager.ts +4 -0
- package/src/space/space.test.ts +7 -7
- package/src/space/space.ts +17 -22
- package/src/testing/index.ts +1 -0
- package/src/testing/level.ts +11 -0
- package/src/testing/test-agent-builder.ts +1 -0
- package/dist/lib/browser/chunk-RTEEJ723.mjs.map +0 -7
- package/dist/lib/node/chunk-7VZVCCNF.cjs.map +0 -7
|
@@ -2,78 +2,82 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
type PeerId,
|
|
9
|
-
type DocumentId,
|
|
10
|
-
type StorageKey,
|
|
11
|
-
type DocHandle,
|
|
12
|
-
type DocHandleChangePayload,
|
|
13
|
-
} from '@dxos/automerge/automerge-repo';
|
|
14
|
-
import { IndexedDBStorageAdapter } from '@dxos/automerge/automerge-repo-storage-indexeddb';
|
|
5
|
+
import { asyncTimeout } from '@dxos/async';
|
|
6
|
+
import { next as automerge } from '@dxos/automerge/automerge';
|
|
7
|
+
import { Repo, type DocumentId, type PeerId, type StorageAdapterInterface } from '@dxos/automerge/automerge-repo';
|
|
15
8
|
import { type Stream } from '@dxos/codec-protobuf';
|
|
16
|
-
import { Context } from '@dxos/context';
|
|
9
|
+
import { Context, type Lifecycle } from '@dxos/context';
|
|
17
10
|
import { PublicKey } from '@dxos/keys';
|
|
18
11
|
import { log } from '@dxos/log';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
import {
|
|
13
|
+
type FlushRequest,
|
|
14
|
+
type HostInfo,
|
|
15
|
+
type SyncRepoRequest,
|
|
16
|
+
type SyncRepoResponse,
|
|
17
|
+
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
18
|
+
import { type Directory } from '@dxos/random-access-storage';
|
|
22
19
|
import { type AutomergeReplicator } from '@dxos/teleport-extension-automerge-replicator';
|
|
23
20
|
import { trace } from '@dxos/tracing';
|
|
24
21
|
import { ComplexMap, ComplexSet, defaultMap, mapValues } from '@dxos/util';
|
|
25
22
|
|
|
26
|
-
import {
|
|
27
|
-
import { AutomergeStorageWrapper } from './automerge-storage–wrapper';
|
|
23
|
+
import { LevelDBStorageAdapter, type StorageCallbacks } from './leveldb-storage-adapter';
|
|
28
24
|
import { LocalHostNetworkAdapter } from './local-host-network-adapter';
|
|
29
25
|
import { MeshNetworkAdapter } from './mesh-network-adapter';
|
|
26
|
+
import { levelMigration } from './migrations';
|
|
27
|
+
import { type SubLevelDB } from './types';
|
|
30
28
|
|
|
29
|
+
// TODO: Remove
|
|
31
30
|
export type { DocumentId };
|
|
32
31
|
|
|
33
|
-
export interface MetadataMethods {
|
|
34
|
-
markDirty(idToLastHash: Map<string, string>): Promise<void>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
32
|
export type AutomergeHostParams = {
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
db: SubLevelDB;
|
|
34
|
+
/**
|
|
35
|
+
* For migration purposes.
|
|
36
|
+
*/
|
|
37
|
+
directory?: Directory;
|
|
38
|
+
storageCallbacks?: StorageCallbacks;
|
|
40
39
|
};
|
|
41
40
|
|
|
42
41
|
@trace.resource()
|
|
43
42
|
export class AutomergeHost {
|
|
44
43
|
private readonly _ctx = new Context();
|
|
45
|
-
private readonly
|
|
46
|
-
private readonly
|
|
47
|
-
private readonly
|
|
48
|
-
|
|
44
|
+
private readonly _directory?: Directory;
|
|
45
|
+
private readonly _db: SubLevelDB;
|
|
46
|
+
private readonly _storageCallbacks?: StorageCallbacks;
|
|
47
|
+
|
|
48
|
+
private _repo!: Repo;
|
|
49
|
+
private _meshNetwork!: MeshNetworkAdapter;
|
|
50
|
+
private _clientNetwork!: LocalHostNetworkAdapter;
|
|
51
|
+
private _storage!: StorageAdapterInterface & Lifecycle;
|
|
49
52
|
|
|
50
53
|
@trace.info()
|
|
51
|
-
private
|
|
54
|
+
private _peerId!: string;
|
|
52
55
|
|
|
53
56
|
/**
|
|
54
57
|
* spaceKey -> deviceKey[]
|
|
55
58
|
*/
|
|
56
59
|
private readonly _authorizedDevices = new ComplexMap<PublicKey, ComplexSet<PublicKey>>(PublicKey.hash);
|
|
57
60
|
|
|
58
|
-
private readonly _updatingMetadata = new Map<string, Promise<void>>();
|
|
59
|
-
private readonly _metadata?: MetadataMethods;
|
|
60
|
-
|
|
61
61
|
public _requestedDocs = new Set<string>();
|
|
62
62
|
|
|
63
|
-
constructor({ directory,
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
66
|
-
this.
|
|
63
|
+
constructor({ directory, db, storageCallbacks }: AutomergeHostParams) {
|
|
64
|
+
this._directory = directory;
|
|
65
|
+
this._db = db;
|
|
66
|
+
this._storageCallbacks = storageCallbacks;
|
|
67
|
+
}
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
callbacks: { beforeSave: (params) => this._beforeSave(params) },
|
|
69
|
+
async open() {
|
|
70
|
+
// TODO(mykola): remove this before 0.6 release.
|
|
71
|
+
this._directory && (await levelMigration({ db: this._db, directory: this._directory }));
|
|
72
|
+
this._storage = new LevelDBStorageAdapter({
|
|
73
|
+
db: this._db,
|
|
74
|
+
callbacks: this._storageCallbacks,
|
|
75
75
|
});
|
|
76
|
+
await this._storage.open?.();
|
|
76
77
|
this._peerId = `host-${PublicKey.random().toHex()}` as PeerId;
|
|
78
|
+
|
|
79
|
+
this._meshNetwork = new MeshNetworkAdapter();
|
|
80
|
+
this._clientNetwork = new LocalHostNetworkAdapter();
|
|
77
81
|
this._repo = new Repo({
|
|
78
82
|
peerId: this._peerId as PeerId,
|
|
79
83
|
network: [this._clientNetwork, this._meshNetwork],
|
|
@@ -133,61 +137,17 @@ export class AutomergeHost {
|
|
|
133
137
|
this._clientNetwork.ready();
|
|
134
138
|
this._meshNetwork.ready();
|
|
135
139
|
|
|
136
|
-
|
|
137
|
-
const listener = ({ handle }: { handle: DocHandle<any> }) => this._onDocument(handle);
|
|
138
|
-
this._repo.on('document', listener);
|
|
139
|
-
this._ctx.onDispose(() => {
|
|
140
|
-
this._repo.off('document', listener);
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
get repo(): Repo {
|
|
146
|
-
return this._repo;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private async _beforeSave(path: StorageKey) {
|
|
150
|
-
const id = path[0];
|
|
151
|
-
if (this._updatingMetadata.has(id)) {
|
|
152
|
-
return this._updatingMetadata.get(id);
|
|
153
|
-
}
|
|
140
|
+
await this._clientNetwork.whenConnected();
|
|
154
141
|
}
|
|
155
142
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
this._ctx.
|
|
160
|
-
handle.off('change', listener);
|
|
161
|
-
});
|
|
143
|
+
async close() {
|
|
144
|
+
await this._storage.close?.();
|
|
145
|
+
await this._clientNetwork.close();
|
|
146
|
+
await this._ctx.dispose();
|
|
162
147
|
}
|
|
163
148
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const objectIds = getInlineChanges(event);
|
|
170
|
-
if (objectIds.length === 0) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const heads = getHeads(event.doc);
|
|
175
|
-
const lastAvailableHash = heads.join('');
|
|
176
|
-
if (!lastAvailableHash) {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const encodedIds = objectIds.map((objectId) => idCodec.encode({ documentId: event.handle.documentId, objectId }));
|
|
181
|
-
const idToLastHash = new Map(encodedIds.map((id) => [id, lastAvailableHash]));
|
|
182
|
-
const markingDirtyPromise = this._metadata
|
|
183
|
-
.markDirty(idToLastHash)
|
|
184
|
-
.then(() => {
|
|
185
|
-
this._updatingMetadata.delete(event.handle.documentId);
|
|
186
|
-
})
|
|
187
|
-
.catch((err: Error) => {
|
|
188
|
-
this._ctx.disposed && log.catch(err);
|
|
189
|
-
});
|
|
190
|
-
this._updatingMetadata.set(event.handle.documentId, markingDirtyPromise);
|
|
149
|
+
get repo(): Repo {
|
|
150
|
+
return this._repo;
|
|
191
151
|
}
|
|
192
152
|
|
|
193
153
|
@trace.info({ depth: null })
|
|
@@ -197,8 +157,8 @@ export class AutomergeHost {
|
|
|
197
157
|
hasDoc: !!handle.docSync(),
|
|
198
158
|
heads: handle.docSync() ? automerge.getHeads(handle.docSync()) : null,
|
|
199
159
|
data:
|
|
200
|
-
handle.docSync()
|
|
201
|
-
mapValues(handle.docSync()
|
|
160
|
+
handle.docSync() &&
|
|
161
|
+
mapValues(handle.docSync(), (value, key) => {
|
|
202
162
|
try {
|
|
203
163
|
switch (key) {
|
|
204
164
|
case 'access':
|
|
@@ -221,15 +181,21 @@ export class AutomergeHost {
|
|
|
221
181
|
return this._repo.peers;
|
|
222
182
|
}
|
|
223
183
|
|
|
224
|
-
async close() {
|
|
225
|
-
await this._storage.close();
|
|
226
|
-
await this._clientNetwork.close();
|
|
227
|
-
await this._ctx.dispose();
|
|
228
|
-
}
|
|
229
|
-
|
|
230
184
|
//
|
|
231
185
|
// Methods for client-services.
|
|
232
186
|
//
|
|
187
|
+
@trace.span({ showInBrowserTimeline: true })
|
|
188
|
+
async flush({ documentIds }: FlushRequest): Promise<void> {
|
|
189
|
+
// Note: Wait for all requested documents to be loaded/synced from thin-client.
|
|
190
|
+
await Promise.all(documentIds?.map((id) => this._repo.find(id as DocumentId).whenReady()) ?? []);
|
|
191
|
+
|
|
192
|
+
// TODO(dmaretskyi): Workaround until the flush issue gets resolved.
|
|
193
|
+
try {
|
|
194
|
+
await asyncTimeout(this._repo.flush(documentIds as DocumentId[]), 500);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
log.warn('flush error', { documentIds, err });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
233
199
|
|
|
234
200
|
syncRepo(request: SyncRepoRequest): Stream<SyncRepoResponse> {
|
|
235
201
|
return this._clientNetwork.syncRepo(request);
|
|
@@ -257,24 +223,6 @@ export class AutomergeHost {
|
|
|
257
223
|
}
|
|
258
224
|
}
|
|
259
225
|
|
|
260
|
-
// TODO(mykola): Reconcile with `getInlineAndLinkChanges` in AutomergeDB.
|
|
261
|
-
const getInlineChanges = (event: DocHandleChangePayload<any>) => {
|
|
262
|
-
const inlineChangedObjectIds = new Set<string>();
|
|
263
|
-
for (const { path } of event.patches) {
|
|
264
|
-
if (path.length < 2) {
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
switch (path[0]) {
|
|
268
|
-
case 'objects':
|
|
269
|
-
if (path.length >= 2) {
|
|
270
|
-
inlineChangedObjectIds.add(path[1]);
|
|
271
|
-
}
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
return [...inlineChangedObjectIds];
|
|
276
|
-
};
|
|
277
|
-
|
|
278
226
|
export const getSpaceKeyFromDoc = (doc: any): string | null => {
|
|
279
227
|
// experimental_spaceKey is set on old documents, new ones are created with doc.access.spaceKey
|
|
280
228
|
const rawSpaceKey = doc.access?.spaceKey ?? doc.experimental_spaceKey;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Repo } from '@dxos/automerge/automerge-repo';
|
|
6
|
+
import { randomBytes } from '@dxos/crypto';
|
|
7
|
+
import { StorageType, createStorage } from '@dxos/random-access-storage';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
9
|
+
|
|
10
|
+
import { AutomergeStorageAdapter } from './automerge-storage-adapter';
|
|
11
|
+
|
|
12
|
+
describe('AutomergeRepo', () => {
|
|
13
|
+
// Currently failing
|
|
14
|
+
test.skip('flush', async () => {
|
|
15
|
+
const repo = new Repo({
|
|
16
|
+
network: [],
|
|
17
|
+
storage: new AutomergeStorageAdapter(createStorage({ type: StorageType.NODE }).createDirectory()),
|
|
18
|
+
});
|
|
19
|
+
const handle = repo.create<{ field?: string }>();
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < 10; i++) {
|
|
22
|
+
const p = repo.flush([handle.documentId]);
|
|
23
|
+
handle.change((doc: any) => {
|
|
24
|
+
doc.field += randomBytes(1024).toString('hex');
|
|
25
|
+
});
|
|
26
|
+
await p;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
package/src/automerge/index.ts
CHANGED
|
@@ -4,5 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
export * from './automerge-host';
|
|
6
6
|
export * from './automerge-storage-adapter';
|
|
7
|
+
export * from './automerge-doc-loader';
|
|
8
|
+
export * from './leveldb-storage-adapter';
|
|
7
9
|
export * from './local-host-network-adapter';
|
|
8
10
|
export * from './mesh-network-adapter';
|
|
11
|
+
export * from './types';
|
|
12
|
+
export * from './reference';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import { Level } from 'level';
|
|
7
|
+
|
|
8
|
+
import { PublicKey } from '@dxos/keys';
|
|
9
|
+
import { describe, openAndClose, test } from '@dxos/test';
|
|
10
|
+
|
|
11
|
+
import { type SubLevelDB } from './types';
|
|
12
|
+
import { createTestLevel } from '../testing';
|
|
13
|
+
|
|
14
|
+
describe('Level', () => {
|
|
15
|
+
test('missing keys', async () => {
|
|
16
|
+
const level = createTestLevel();
|
|
17
|
+
await openAndClose(level);
|
|
18
|
+
|
|
19
|
+
expect(() => level.get('missing')).to.throw;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('data persistance after reload', async () => {
|
|
23
|
+
const path = `/tmp/dxos-${PublicKey.random().toHex()}`;
|
|
24
|
+
const level = new Level<string, string>(path);
|
|
25
|
+
await level.open();
|
|
26
|
+
|
|
27
|
+
const key = 'name';
|
|
28
|
+
const value = 'Rich';
|
|
29
|
+
{
|
|
30
|
+
await level.put(key, value);
|
|
31
|
+
expect(await level.get(key)).to.equal(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await level.close();
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
const level = new Level<string, string>(path);
|
|
38
|
+
await level.open();
|
|
39
|
+
expect(await level.get(key)).to.equal(value);
|
|
40
|
+
await level.clear();
|
|
41
|
+
expect(() => level.get(key)).to.throw;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('batch different sublevels', async () => {
|
|
46
|
+
const level = createTestLevel();
|
|
47
|
+
await openAndClose(level);
|
|
48
|
+
|
|
49
|
+
const first: SubLevelDB = level.sublevel('first');
|
|
50
|
+
const second: SubLevelDB = level.sublevel('second');
|
|
51
|
+
|
|
52
|
+
const batch = first.batch();
|
|
53
|
+
|
|
54
|
+
const key = 'key';
|
|
55
|
+
const value = 'first-level-value';
|
|
56
|
+
batch.put(key, value, { sublevel: second });
|
|
57
|
+
await batch.write();
|
|
58
|
+
|
|
59
|
+
expect(() => first.get(key)).to.throw;
|
|
60
|
+
expect(await level.sublevel('second').get(key)).to.equal(value);
|
|
61
|
+
expect(await second.get(key)).to.equal(value);
|
|
62
|
+
expect(await second.iterator().all()).to.deep.equal([[key, value]]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('sublevel prefixes', () => {
|
|
66
|
+
const level = createTestLevel();
|
|
67
|
+
expect(level.prefixKey('', 'utf8')).to.equal('');
|
|
68
|
+
|
|
69
|
+
{
|
|
70
|
+
const sublevel = level.sublevel('sublevel');
|
|
71
|
+
expect(sublevel.prefixKey('', 'utf8')).to.equal('!sublevel!');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
{
|
|
75
|
+
const sublevel = level.sublevel('');
|
|
76
|
+
expect(sublevel.prefixKey('', 'utf8')).to.equal('!!');
|
|
77
|
+
|
|
78
|
+
const sublevel2 = sublevel.sublevel('');
|
|
79
|
+
expect(sublevel2.prefixKey('', 'utf8')).to.equal('!!!!');
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
// s
|
|
4
|
+
|
|
5
|
+
import { type MixedEncoding } from 'level-transcoder';
|
|
6
|
+
|
|
7
|
+
import { type StorageAdapterInterface, type Chunk, type StorageKey } from '@dxos/automerge/automerge-repo';
|
|
8
|
+
import { LifecycleState, Resource } from '@dxos/context';
|
|
9
|
+
import { type MaybePromise } from '@dxos/util';
|
|
10
|
+
|
|
11
|
+
import { type BatchLevel, type SubLevelDB } from './types';
|
|
12
|
+
|
|
13
|
+
export type LevelDBStorageAdapterParams = {
|
|
14
|
+
db: SubLevelDB;
|
|
15
|
+
callbacks?: StorageCallbacks;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type BeforeSaveParams = { path: StorageKey; batch: BatchLevel };
|
|
19
|
+
|
|
20
|
+
export interface StorageCallbacks {
|
|
21
|
+
beforeSave(params: BeforeSaveParams): MaybePromise<void>;
|
|
22
|
+
afterSave(path: StorageKey): MaybePromise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class LevelDBStorageAdapter extends Resource implements StorageAdapterInterface {
|
|
26
|
+
constructor(private readonly _params: LevelDBStorageAdapterParams) {
|
|
27
|
+
super();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async load(keyArray: StorageKey): Promise<Uint8Array | undefined> {
|
|
31
|
+
try {
|
|
32
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
33
|
+
// TODO(mykola): this should be an error.
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return await this._params.db.get<StorageKey, Uint8Array>(keyArray, { ...encodingOptions });
|
|
37
|
+
} catch (err: any) {
|
|
38
|
+
if (isLevelDbNotFoundError(err)) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async save(keyArray: StorageKey, binary: Uint8Array): Promise<void> {
|
|
46
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const batch = this._params.db.batch();
|
|
50
|
+
|
|
51
|
+
await this._params.callbacks?.beforeSave?.({ path: keyArray, batch });
|
|
52
|
+
batch.put<StorageKey, Uint8Array>(keyArray, Buffer.from(binary), {
|
|
53
|
+
...encodingOptions,
|
|
54
|
+
});
|
|
55
|
+
await batch.write();
|
|
56
|
+
|
|
57
|
+
await this._params.callbacks?.afterSave?.(keyArray);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async remove(keyArray: StorageKey): Promise<void> {
|
|
61
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
await this._params.db.del<StorageKey>(keyArray, { ...encodingOptions });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async loadRange(keyPrefix: StorageKey): Promise<Chunk[]> {
|
|
68
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
const result: Chunk[] = [];
|
|
72
|
+
for await (const [key, value] of this._params.db.iterator<StorageKey, Uint8Array>({
|
|
73
|
+
gte: keyPrefix,
|
|
74
|
+
lte: [...keyPrefix, '\uffff'],
|
|
75
|
+
...encodingOptions,
|
|
76
|
+
})) {
|
|
77
|
+
result.push({
|
|
78
|
+
key,
|
|
79
|
+
data: value,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async removeRange(keyPrefix: StorageKey): Promise<void> {
|
|
86
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const batch = this._params.db.batch();
|
|
90
|
+
|
|
91
|
+
for await (const [key] of this._params.db.iterator<StorageKey, Uint8Array>({
|
|
92
|
+
gte: keyPrefix,
|
|
93
|
+
lte: [...keyPrefix, '\uffff'],
|
|
94
|
+
...encodingOptions,
|
|
95
|
+
})) {
|
|
96
|
+
batch.del<StorageKey>(key, { ...encodingOptions });
|
|
97
|
+
}
|
|
98
|
+
await batch.write();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const keyEncoder: MixedEncoding<StorageKey, Uint8Array, StorageKey> = {
|
|
103
|
+
encode: (key: StorageKey): Uint8Array =>
|
|
104
|
+
Buffer.from(key.map((k) => k.replaceAll('%', '%25').replaceAll('-', '%2D')).join('-')),
|
|
105
|
+
decode: (key: Uint8Array): StorageKey =>
|
|
106
|
+
Buffer.from(key)
|
|
107
|
+
.toString()
|
|
108
|
+
.split('-')
|
|
109
|
+
.map((k) => k.replaceAll('%2D', '-').replaceAll('%25', '%')),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const encodingOptions = {
|
|
113
|
+
keyEncoding: keyEncoder,
|
|
114
|
+
valueEncoding: 'buffer',
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const isLevelDbNotFoundError = (err: any): boolean => err.code === 'LEVEL_NOT_FOUND';
|
|
@@ -6,7 +6,6 @@ import { Trigger } from '@dxos/async';
|
|
|
6
6
|
import { NetworkAdapter, type Message, type PeerId, cbor } from '@dxos/automerge/automerge-repo';
|
|
7
7
|
import { Stream } from '@dxos/codec-protobuf';
|
|
8
8
|
import { invariant } from '@dxos/invariant';
|
|
9
|
-
import { log } from '@dxos/log';
|
|
10
9
|
import { type HostInfo, type SyncRepoRequest, type SyncRepoResponse } from '@dxos/protocols/proto/dxos/echo/service';
|
|
11
10
|
|
|
12
11
|
type ClientSyncState = {
|
|
@@ -32,10 +31,17 @@ export class LocalHostNetworkAdapter extends NetworkAdapter {
|
|
|
32
31
|
});
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
private _connected = new Trigger();
|
|
34
|
+
private readonly _connected = new Trigger();
|
|
35
|
+
private _isConnected: boolean = false;
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Called by `Repo` to connect to the network.
|
|
39
|
+
*
|
|
40
|
+
* @param peerId Our peer Id.
|
|
41
|
+
*/
|
|
37
42
|
override connect(peerId: PeerId): void {
|
|
38
43
|
this.peerId = peerId;
|
|
44
|
+
this._isConnected = true;
|
|
39
45
|
this._connected.wake();
|
|
40
46
|
// No-op. Client always connects first
|
|
41
47
|
}
|
|
@@ -56,6 +62,10 @@ export class LocalHostNetworkAdapter extends NetworkAdapter {
|
|
|
56
62
|
// No-op
|
|
57
63
|
}
|
|
58
64
|
|
|
65
|
+
async whenConnected(): Promise<void> {
|
|
66
|
+
await this._connected.wait({ timeout: 10_000 });
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
syncRepo({ id, syncMessage }: SyncRepoRequest): Stream<SyncRepoResponse> {
|
|
60
70
|
const peerId = this._getPeerId(id);
|
|
61
71
|
|
|
@@ -77,26 +87,22 @@ export class LocalHostNetworkAdapter extends NetworkAdapter {
|
|
|
77
87
|
},
|
|
78
88
|
});
|
|
79
89
|
|
|
80
|
-
this.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
peerId,
|
|
86
|
-
});
|
|
87
|
-
})
|
|
88
|
-
.catch((err) => log.catch(err));
|
|
90
|
+
invariant(this._isConnected);
|
|
91
|
+
this.emit('peer-candidate', {
|
|
92
|
+
peerMetadata: {},
|
|
93
|
+
peerId,
|
|
94
|
+
});
|
|
89
95
|
});
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
async sendSyncMessage({ id, syncMessage }: SyncRepoRequest): Promise<void> {
|
|
93
|
-
|
|
99
|
+
invariant(this._isConnected);
|
|
94
100
|
const message = cbor.decode(syncMessage!) as Message;
|
|
95
101
|
this.emit('message', message);
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
async getHostInfo(): Promise<HostInfo> {
|
|
99
|
-
|
|
105
|
+
invariant(this._isConnected);
|
|
100
106
|
invariant(this.peerId, 'Peer id not set.');
|
|
101
107
|
return {
|
|
102
108
|
peerId: this.peerId,
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type StorageKey } from '@dxos/automerge/automerge-repo';
|
|
6
|
+
import { IndexedDBStorageAdapter } from '@dxos/automerge/automerge-repo-storage-indexeddb';
|
|
7
|
+
import { log } from '@dxos/log';
|
|
8
|
+
import { StorageType, type Directory } from '@dxos/random-access-storage';
|
|
9
|
+
|
|
10
|
+
import { AutomergeStorageAdapter } from './automerge-storage-adapter';
|
|
11
|
+
import { encodingOptions } from './leveldb-storage-adapter';
|
|
12
|
+
import { type SubLevelDB } from './types';
|
|
13
|
+
|
|
14
|
+
export const levelMigration = async ({ db, directory }: { db: SubLevelDB; directory: Directory }) => {
|
|
15
|
+
// Note: Make automigration from previous storage to leveldb here.
|
|
16
|
+
const isNewLevel = !(await db
|
|
17
|
+
.iterator<StorageKey, Uint8Array>({
|
|
18
|
+
...encodingOptions,
|
|
19
|
+
})
|
|
20
|
+
.next());
|
|
21
|
+
|
|
22
|
+
if (!isNewLevel) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const oldStorageAdapter =
|
|
27
|
+
directory.type === StorageType.IDB
|
|
28
|
+
? new IndexedDBStorageAdapter(directory.path, 'data')
|
|
29
|
+
: new AutomergeStorageAdapter(directory);
|
|
30
|
+
|
|
31
|
+
const chunks = await oldStorageAdapter.loadRange([]);
|
|
32
|
+
if (chunks.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const batch = db.batch();
|
|
36
|
+
log.info('found chunks on old storage adapter', { chunks: chunks.length });
|
|
37
|
+
for (const { key, data } of await oldStorageAdapter.loadRange([])) {
|
|
38
|
+
data && batch.put<StorageKey, Uint8Array>(key, data, { ...encodingOptions });
|
|
39
|
+
}
|
|
40
|
+
await batch.write();
|
|
41
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Reference } from '@dxos/echo-schema';
|
|
6
|
+
|
|
7
|
+
export const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Reference as it is stored in Automerge document.
|
|
11
|
+
*/
|
|
12
|
+
export type EncodedReferenceObject = {
|
|
13
|
+
'@type': typeof REFERENCE_TYPE_TAG;
|
|
14
|
+
itemId: string | null;
|
|
15
|
+
protocol: string | null;
|
|
16
|
+
host: string | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const encodeReference = (reference: Reference): EncodedReferenceObject => ({
|
|
20
|
+
'@type': REFERENCE_TYPE_TAG,
|
|
21
|
+
// NOTE: Automerge do not support undefined values, so we need to use null instead.
|
|
22
|
+
itemId: reference.itemId ?? null,
|
|
23
|
+
protocol: reference.protocol ?? null,
|
|
24
|
+
host: reference.host ?? null,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const decodeReference = (value: any) =>
|
|
28
|
+
new Reference(value.itemId, value.protocol ?? undefined, value.host ?? undefined);
|
|
29
|
+
|
|
30
|
+
export const isEncodedReferenceObject = (value: any): value is EncodedReferenceObject =>
|
|
31
|
+
typeof value === 'object' && value !== null && value['@type'] === REFERENCE_TYPE_TAG;
|