@dxos/echo-pipeline 0.4.10-main.c42bfdb → 0.4.10-main.ccba876
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-RA6MLCZM.mjs → chunk-SYE4EK33.mjs} +17 -9
- package/dist/lib/browser/chunk-SYE4EK33.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +237 -141
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +8 -2
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-KGIYLJBT.cjs → chunk-WCTX6RNS.cjs} +19 -11
- package/dist/lib/node/chunk-WCTX6RNS.cjs.map +7 -0
- package/dist/lib/node/index.cjs +241 -150
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +18 -13
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/types/src/automerge/automerge-host.d.ts +18 -9
- package/dist/types/src/automerge/automerge-host.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 +27 -0
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -0
- 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/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 +5 -0
- package/dist/types/src/automerge/types.d.ts.map +1 -1
- 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/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/package.json +33 -30
- package/src/automerge/automerge-host.test.ts +19 -5
- package/src/automerge/automerge-host.ts +37 -27
- package/src/automerge/level.test.ts +43 -0
- package/src/automerge/leveldb-storage-adapter.ts +105 -0
- package/src/automerge/migrations.ts +41 -0
- package/src/automerge/storage-adapter.test.ts +90 -0
- package/src/automerge/types.ts +5 -3
- package/src/metadata/metadata-store.ts +17 -8
- package/src/testing/index.ts +1 -0
- package/src/testing/level.ts +11 -0
- package/dist/lib/browser/chunk-RA6MLCZM.mjs.map +0 -7
- package/dist/lib/node/chunk-KGIYLJBT.cjs.map +0 -7
|
@@ -10,8 +10,8 @@ import {
|
|
|
10
10
|
type StorageKey,
|
|
11
11
|
type DocHandle,
|
|
12
12
|
type DocHandleChangePayload,
|
|
13
|
+
type StorageAdapterInterface,
|
|
13
14
|
} from '@dxos/automerge/automerge-repo';
|
|
14
|
-
import { IndexedDBStorageAdapter } from '@dxos/automerge/automerge-repo-storage-indexeddb';
|
|
15
15
|
import { type Stream } from '@dxos/codec-protobuf';
|
|
16
16
|
import { Context } from '@dxos/context';
|
|
17
17
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -23,15 +23,16 @@ import {
|
|
|
23
23
|
type SyncRepoRequest,
|
|
24
24
|
type SyncRepoResponse,
|
|
25
25
|
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
26
|
-
import {
|
|
26
|
+
import { type Directory } from '@dxos/random-access-storage';
|
|
27
27
|
import { type AutomergeReplicator } from '@dxos/teleport-extension-automerge-replicator';
|
|
28
28
|
import { trace } from '@dxos/tracing';
|
|
29
|
-
import { ComplexMap, ComplexSet, defaultMap, mapValues } from '@dxos/util';
|
|
29
|
+
import { ComplexMap, ComplexSet, type MaybePromise, defaultMap, mapValues } from '@dxos/util';
|
|
30
30
|
|
|
31
|
-
import {
|
|
32
|
-
import { AutomergeStorageWrapper } from './automerge-storage–wrapper';
|
|
31
|
+
import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
|
|
33
32
|
import { LocalHostNetworkAdapter } from './local-host-network-adapter';
|
|
34
33
|
import { MeshNetworkAdapter } from './mesh-network-adapter';
|
|
34
|
+
import { levelMigration } from './migrations';
|
|
35
|
+
import { type MySublevel } from './types';
|
|
35
36
|
|
|
36
37
|
export type { DocumentId };
|
|
37
38
|
|
|
@@ -40,20 +41,28 @@ export interface MetadataMethods {
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
export type AutomergeHostParams = {
|
|
43
|
-
|
|
44
|
+
db: MaybePromise<MySublevel>;
|
|
45
|
+
/**
|
|
46
|
+
* For migration purposes.
|
|
47
|
+
*/
|
|
48
|
+
directory?: Directory;
|
|
44
49
|
metadata?: MetadataMethods;
|
|
45
50
|
};
|
|
46
51
|
|
|
47
52
|
@trace.resource()
|
|
48
53
|
export class AutomergeHost {
|
|
49
54
|
private readonly _ctx = new Context();
|
|
50
|
-
private readonly
|
|
51
|
-
private readonly
|
|
52
|
-
private readonly
|
|
53
|
-
|
|
55
|
+
private readonly _directory?: Directory;
|
|
56
|
+
private readonly _db: MaybePromise<MySublevel>;
|
|
57
|
+
private readonly _metadata?: MetadataMethods;
|
|
58
|
+
|
|
59
|
+
private _repo!: Repo;
|
|
60
|
+
private _meshNetwork!: MeshNetworkAdapter;
|
|
61
|
+
private _clientNetwork!: LocalHostNetworkAdapter;
|
|
62
|
+
private _storage!: StorageAdapterInterface & { close?: () => void };
|
|
54
63
|
|
|
55
64
|
@trace.info()
|
|
56
|
-
private
|
|
65
|
+
private _peerId!: string;
|
|
57
66
|
|
|
58
67
|
/**
|
|
59
68
|
* spaceKey -> deviceKey[]
|
|
@@ -61,24 +70,25 @@ export class AutomergeHost {
|
|
|
61
70
|
private readonly _authorizedDevices = new ComplexMap<PublicKey, ComplexSet<PublicKey>>(PublicKey.hash);
|
|
62
71
|
|
|
63
72
|
private readonly _updatingMetadata = new Map<string, Promise<void>>();
|
|
64
|
-
private readonly _metadata?: MetadataMethods;
|
|
65
73
|
|
|
66
74
|
public _requestedDocs = new Set<string>();
|
|
67
75
|
|
|
68
|
-
constructor({ directory, metadata }: AutomergeHostParams) {
|
|
76
|
+
constructor({ directory, db, metadata }: AutomergeHostParams) {
|
|
77
|
+
this._directory = directory;
|
|
78
|
+
this._db = db;
|
|
69
79
|
this._metadata = metadata;
|
|
70
|
-
|
|
71
|
-
this._clientNetwork = new LocalHostNetworkAdapter();
|
|
80
|
+
}
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
? new IndexedDBStorageAdapter(directory.path, 'data')
|
|
78
|
-
: new AutomergeStorageAdapter(directory),
|
|
82
|
+
async open() {
|
|
83
|
+
this._directory && (await levelMigration({ db: await this._db, directory: this._directory }));
|
|
84
|
+
this._storage = new LevelDBStorageAdapter({
|
|
85
|
+
db: await this._db,
|
|
79
86
|
callbacks: { beforeSave: (params) => this._beforeSave(params) },
|
|
80
87
|
});
|
|
81
88
|
this._peerId = `host-${PublicKey.random().toHex()}` as PeerId;
|
|
89
|
+
|
|
90
|
+
this._meshNetwork = new MeshNetworkAdapter();
|
|
91
|
+
this._clientNetwork = new LocalHostNetworkAdapter();
|
|
82
92
|
this._repo = new Repo({
|
|
83
93
|
peerId: this._peerId as PeerId,
|
|
84
94
|
network: [this._clientNetwork, this._meshNetwork],
|
|
@@ -148,6 +158,12 @@ export class AutomergeHost {
|
|
|
148
158
|
}
|
|
149
159
|
}
|
|
150
160
|
|
|
161
|
+
async close() {
|
|
162
|
+
this._storage.close?.();
|
|
163
|
+
await this._clientNetwork.close();
|
|
164
|
+
await this._ctx.dispose();
|
|
165
|
+
}
|
|
166
|
+
|
|
151
167
|
get repo(): Repo {
|
|
152
168
|
return this._repo;
|
|
153
169
|
}
|
|
@@ -224,12 +240,6 @@ export class AutomergeHost {
|
|
|
224
240
|
return this._repo.peers;
|
|
225
241
|
}
|
|
226
242
|
|
|
227
|
-
async close() {
|
|
228
|
-
await this._storage.close();
|
|
229
|
-
await this._clientNetwork.close();
|
|
230
|
-
await this._ctx.dispose();
|
|
231
|
-
}
|
|
232
|
-
|
|
233
243
|
//
|
|
234
244
|
// Methods for client-services.
|
|
235
245
|
//
|
|
@@ -0,0 +1,43 @@
|
|
|
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, test } from '@dxos/test';
|
|
10
|
+
|
|
11
|
+
import { createTestLevel } from '../testing';
|
|
12
|
+
|
|
13
|
+
describe('Level', () => {
|
|
14
|
+
test('missing keys', async () => {
|
|
15
|
+
const level = createTestLevel();
|
|
16
|
+
await level.open();
|
|
17
|
+
|
|
18
|
+
expect(() => level.get('missing')).to.throw;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('data persistance after reload', async () => {
|
|
22
|
+
const path = `/tmp/dxos-${PublicKey.random().toHex()}`;
|
|
23
|
+
const level = new Level<string, string>(path);
|
|
24
|
+
await level.open();
|
|
25
|
+
|
|
26
|
+
const key = 'name';
|
|
27
|
+
const value = 'Rich';
|
|
28
|
+
{
|
|
29
|
+
await level.put(key, value);
|
|
30
|
+
expect(await level.get(key)).to.equal(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await level.close();
|
|
34
|
+
|
|
35
|
+
{
|
|
36
|
+
const level = new Level<string, string>(path);
|
|
37
|
+
await level.open();
|
|
38
|
+
expect(await level.get(key)).to.equal(value);
|
|
39
|
+
await level.clear();
|
|
40
|
+
expect(() => level.get(key)).to.throw;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
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 { type MaybePromise } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { type MySublevel } from './types';
|
|
11
|
+
|
|
12
|
+
export type LevelDBStorageAdapterParams = {
|
|
13
|
+
db: MySublevel;
|
|
14
|
+
callbacks?: StorageCallbacks;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type StorageCallbacks = {
|
|
18
|
+
beforeSave?: (path: StorageKey) => MaybePromise<void>;
|
|
19
|
+
afterSave?: (path: StorageKey) => MaybePromise<void>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class LevelDBStorageAdapter implements StorageAdapterInterface {
|
|
23
|
+
private _state: 'opened' | 'closed' = 'opened';
|
|
24
|
+
constructor(private readonly _params: LevelDBStorageAdapterParams) {}
|
|
25
|
+
|
|
26
|
+
async load(keyArray: StorageKey): Promise<Uint8Array | undefined> {
|
|
27
|
+
if (this._state !== 'opened') {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return this._params.db
|
|
31
|
+
.get<StorageKey, Uint8Array>(keyArray, { ...encodingOptions })
|
|
32
|
+
.catch((err) => (err.code === 'LEVEL_NOT_FOUND' ? undefined : Promise.reject(err)));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async save(keyArray: StorageKey, binary: Uint8Array): Promise<void> {
|
|
36
|
+
if (this._state !== 'opened') {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
await this._params.callbacks?.beforeSave?.(keyArray);
|
|
40
|
+
await this._params.db.put<StorageKey, Uint8Array>(keyArray, Buffer.from(binary), {
|
|
41
|
+
...encodingOptions,
|
|
42
|
+
});
|
|
43
|
+
await this._params.callbacks?.afterSave?.(keyArray);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async remove(keyArray: StorageKey): Promise<void> {
|
|
47
|
+
if (this._state !== 'opened') {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
await this._params.db.del<StorageKey>(keyArray, { ...encodingOptions });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async loadRange(keyPrefix: StorageKey): Promise<Chunk[]> {
|
|
54
|
+
if (this._state !== 'opened') {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
const result: Chunk[] = [];
|
|
58
|
+
for await (const [key, value] of this._params.db.iterator<StorageKey, Uint8Array>({
|
|
59
|
+
gte: keyPrefix,
|
|
60
|
+
lte: [...keyPrefix, '\uffff'],
|
|
61
|
+
...encodingOptions,
|
|
62
|
+
})) {
|
|
63
|
+
result.push({
|
|
64
|
+
key,
|
|
65
|
+
data: value,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async removeRange(keyPrefix: StorageKey): Promise<void> {
|
|
72
|
+
if (this._state !== 'opened') {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
const batch = this._params.db.batch();
|
|
76
|
+
|
|
77
|
+
for await (const [key] of this._params.db.iterator<StorageKey, Uint8Array>({
|
|
78
|
+
gte: keyPrefix,
|
|
79
|
+
lte: [...keyPrefix, '\uffff'],
|
|
80
|
+
...encodingOptions,
|
|
81
|
+
})) {
|
|
82
|
+
batch.del<StorageKey>(key, { ...encodingOptions });
|
|
83
|
+
}
|
|
84
|
+
await batch.write();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
close() {
|
|
88
|
+
this._state = 'closed';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const keyEncoder: MixedEncoding<StorageKey, Uint8Array, StorageKey> = {
|
|
93
|
+
encode: (key: StorageKey): Uint8Array =>
|
|
94
|
+
Buffer.from(key.map((k) => k.replaceAll('%', '%25').replaceAll('-', '%2D')).join('-')),
|
|
95
|
+
decode: (key: Uint8Array): StorageKey =>
|
|
96
|
+
Buffer.from(key)
|
|
97
|
+
.toString()
|
|
98
|
+
.split('-')
|
|
99
|
+
.map((k) => k.replaceAll('%2D', '-').replaceAll('%25', '%')),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const encodingOptions = {
|
|
103
|
+
keyEncoding: keyEncoder,
|
|
104
|
+
valueEncoding: 'buffer',
|
|
105
|
+
};
|
|
@@ -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 MySublevel } from './types';
|
|
13
|
+
|
|
14
|
+
export const levelMigration = async ({ db, directory }: { db: MySublevel; 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,90 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { type StorageAdapterInterface } from '@dxos/automerge/automerge-repo';
|
|
8
|
+
import { PublicKey } from '@dxos/keys';
|
|
9
|
+
import { StorageType, createStorage } from '@dxos/random-access-storage';
|
|
10
|
+
import { afterTest, describe, test } from '@dxos/test';
|
|
11
|
+
import { type MaybePromise } from '@dxos/util';
|
|
12
|
+
|
|
13
|
+
import { AutomergeStorageAdapter } from './automerge-storage-adapter';
|
|
14
|
+
import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
|
|
15
|
+
import { createTestLevel } from '../testing';
|
|
16
|
+
|
|
17
|
+
const runTests = (
|
|
18
|
+
testNamespace: string,
|
|
19
|
+
/** Run per test. Expects automatic clean-up with `afterTest`. */ createAdapter: () => MaybePromise<StorageAdapterInterface>,
|
|
20
|
+
) => {
|
|
21
|
+
describe(testNamespace, () => {
|
|
22
|
+
const chunks = [
|
|
23
|
+
{ key: ['a', 'b', 'c', '1'], data: PublicKey.random().asUint8Array() },
|
|
24
|
+
{ key: ['a', 'b', 'c', '2'], data: PublicKey.random().asUint8Array() },
|
|
25
|
+
{ key: ['a', 'b', 'd', '3'], data: PublicKey.random().asUint8Array() },
|
|
26
|
+
{ key: ['a', 'b', 'd', '4'], data: PublicKey.random().asUint8Array() },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
test('should store and retrieve data', async () => {
|
|
30
|
+
const adapter = await createAdapter();
|
|
31
|
+
|
|
32
|
+
await adapter.save(chunks[0].key, chunks[0].data);
|
|
33
|
+
expect(await adapter.load(chunks[0].key)).to.deep.equal(chunks[0].data);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('loadRange return inputs with correct prefixes', async () => {
|
|
37
|
+
const adapter = await createAdapter();
|
|
38
|
+
|
|
39
|
+
for (const chunk of chunks) {
|
|
40
|
+
await adapter.save(chunk.key, chunk.data);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
expect((await adapter.loadRange(['a', 'b'])).length).to.equal(4);
|
|
44
|
+
expect((await adapter.loadRange(['a', 'b', 'c']))[0]).to.deep.equal(chunks[0]);
|
|
45
|
+
expect((await adapter.loadRange(['a', 'b', 'c'])).length).to.equal(2);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('deletion works', async () => {
|
|
49
|
+
const adapter = await createAdapter();
|
|
50
|
+
|
|
51
|
+
for (const chunk of chunks) {
|
|
52
|
+
await adapter.save(chunk.key, chunk.data);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
await adapter.remove(['a', 'b', 'c', '1']);
|
|
56
|
+
|
|
57
|
+
expect((await adapter.loadRange(['a', 'b'])).length).to.equal(3);
|
|
58
|
+
expect((await adapter.loadRange(['a', 'b', 'c'])).length).to.equal(1);
|
|
59
|
+
|
|
60
|
+
await adapter.removeRange(['a', 'b', 'd']);
|
|
61
|
+
|
|
62
|
+
expect((await adapter.loadRange(['a', 'b'])).length).to.equal(1);
|
|
63
|
+
expect((await adapter.loadRange(['a', 'b']))[0]).to.deep.equal(chunks[1]);
|
|
64
|
+
expect(await adapter.load(['a', 'b', 'c', '2'])).to.deep.equal(chunks[1].data);
|
|
65
|
+
expect(await adapter.load(['a', 'b', 'd', '3'])).to.be.undefined;
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Run tests for AutomergeStorageAdapter.
|
|
72
|
+
*/
|
|
73
|
+
runTests('AutomergeStorageAdapter', () => {
|
|
74
|
+
const storage = createStorage({ type: StorageType.RAM });
|
|
75
|
+
afterTest(() => storage.close());
|
|
76
|
+
const dir = storage.createDirectory('automerge');
|
|
77
|
+
const adapter = new AutomergeStorageAdapter(dir);
|
|
78
|
+
afterTest(() => adapter.close());
|
|
79
|
+
return adapter;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Run tests for LevelDBStorageAdapter.
|
|
84
|
+
*/
|
|
85
|
+
runTests('LevelDBStorageAdapter', async () => {
|
|
86
|
+
const level = createTestLevel();
|
|
87
|
+
await level.open();
|
|
88
|
+
afterTest(() => level.close());
|
|
89
|
+
return new LevelDBStorageAdapter({ db: level.sublevel('automerge') });
|
|
90
|
+
});
|
package/src/automerge/types.ts
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
//
|
|
5
|
+
import { type AbstractSublevel } from 'abstract-level';
|
|
6
|
+
import { type Level } from 'level';
|
|
8
7
|
|
|
9
8
|
import { type EncodedReferenceObject } from './reference';
|
|
10
9
|
|
|
@@ -81,3 +80,6 @@ export type ObjectSystem = {
|
|
|
81
80
|
*/
|
|
82
81
|
type?: EncodedReferenceObject;
|
|
83
82
|
};
|
|
83
|
+
|
|
84
|
+
export type MyLevel = Level<string, string>;
|
|
85
|
+
export type MySublevel = AbstractSublevel<any, string | Buffer | Uint8Array, string, string>;
|
|
@@ -11,7 +11,7 @@ import { invariant } from '@dxos/invariant';
|
|
|
11
11
|
import { PublicKey } from '@dxos/keys';
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
import { DataCorruptionError, STORAGE_VERSION, schema } from '@dxos/protocols';
|
|
14
|
-
import {
|
|
14
|
+
import { Invitation, SpaceState } from '@dxos/protocols/proto/dxos/client/services';
|
|
15
15
|
import {
|
|
16
16
|
type ControlPipelineSnapshot,
|
|
17
17
|
EchoMetadata,
|
|
@@ -174,13 +174,8 @@ export class MetadataStore {
|
|
|
174
174
|
scheduleTaskInterval(
|
|
175
175
|
this._invitationCleanupCtx,
|
|
176
176
|
async () => {
|
|
177
|
-
for (const invitation of this.
|
|
178
|
-
if (
|
|
179
|
-
invitation.created &&
|
|
180
|
-
invitation.lifetime &&
|
|
181
|
-
invitation.lifetime !== 0 &&
|
|
182
|
-
invitation.created.getTime() + invitation.lifetime * 1000 < Date.now()
|
|
183
|
-
) {
|
|
177
|
+
for (const invitation of this._metadata.invitations ?? []) {
|
|
178
|
+
if (hasInvitationExpired(invitation) || isLegacyInvitationFormat(invitation)) {
|
|
184
179
|
await this.removeInvitation(invitation.invitationId);
|
|
185
180
|
}
|
|
186
181
|
}
|
|
@@ -343,3 +338,17 @@ export class MetadataStore {
|
|
|
343
338
|
}
|
|
344
339
|
|
|
345
340
|
const fromBytesInt32 = (buf: Buffer) => buf.readInt32LE(0);
|
|
341
|
+
|
|
342
|
+
export const hasInvitationExpired = (invitation: Invitation): boolean => {
|
|
343
|
+
return Boolean(
|
|
344
|
+
invitation.created &&
|
|
345
|
+
invitation.lifetime &&
|
|
346
|
+
invitation.lifetime !== 0 &&
|
|
347
|
+
invitation.created.getTime() + invitation.lifetime * 1000 < Date.now(),
|
|
348
|
+
);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// TODO: remove once "multiuse" type invitations get removed from local metadata of existing profiles
|
|
352
|
+
const isLegacyInvitationFormat = (invitation: Invitation): boolean => {
|
|
353
|
+
return invitation.type === Invitation.Type.MULTIUSE;
|
|
354
|
+
};
|
package/src/testing/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Level } from 'level';
|
|
6
|
+
|
|
7
|
+
import { PublicKey } from '@dxos/keys';
|
|
8
|
+
|
|
9
|
+
import { type MyLevel } from '../automerge/types';
|
|
10
|
+
|
|
11
|
+
export const createTestLevel = (): MyLevel => new Level<string, string>(`/tmp/dxos-${PublicKey.random().toHex()}`);
|