@dxos/client-services 0.5.9-main.cb71230 → 0.5.9-main.d63ef8d
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-HSM5JUN6.mjs → chunk-C2VXW65X.mjs} +1101 -805
- package/dist/lib/browser/chunk-C2VXW65X.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1 -3
- package/dist/lib/browser/index.mjs.map +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/packlets/testing/index.mjs +11 -11
- package/dist/lib/browser/packlets/testing/index.mjs.map +3 -3
- package/dist/lib/node/{chunk-4OGPGXOU.cjs → chunk-OD7BTUYY.cjs} +1399 -1110
- package/dist/lib/node/chunk-OD7BTUYY.cjs.map +7 -0
- package/dist/lib/node/index.cjs +41 -43
- package/dist/lib/node/index.cjs.map +1 -1
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/packlets/testing/index.cjs +17 -17
- package/dist/lib/node/packlets/testing/index.cjs.map +3 -3
- package/dist/types/src/packlets/identity/default-space-state-machine.d.ts +19 -0
- package/dist/types/src/packlets/identity/default-space-state-machine.d.ts.map +1 -0
- package/dist/types/src/packlets/identity/identity-service.d.ts +14 -7
- package/dist/types/src/packlets/identity/identity-service.d.ts.map +1 -1
- package/dist/types/src/packlets/identity/identity.d.ts +4 -1
- package/dist/types/src/packlets/identity/identity.d.ts.map +1 -1
- package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
- package/dist/types/src/packlets/services/service-host.d.ts +1 -1
- package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
- package/dist/types/src/packlets/spaces/automerge-space-state.d.ts +4 -1
- package/dist/types/src/packlets/spaces/automerge-space-state.d.ts.map +1 -1
- package/dist/types/src/packlets/spaces/data-space-manager.d.ts +5 -3
- package/dist/types/src/packlets/spaces/data-space-manager.d.ts.map +1 -1
- package/dist/types/src/packlets/spaces/data-space.d.ts +9 -9
- package/dist/types/src/packlets/spaces/data-space.d.ts.map +1 -1
- package/dist/types/src/packlets/spaces/epoch-migrations.d.ts +23 -0
- package/dist/types/src/packlets/spaces/epoch-migrations.d.ts.map +1 -0
- package/dist/types/src/packlets/spaces/spaces-service.d.ts +2 -2
- package/dist/types/src/packlets/spaces/spaces-service.d.ts.map +1 -1
- package/dist/types/src/packlets/testing/test-builder.d.ts +8 -6
- package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/version.d.ts +1 -1
- package/package.json +36 -36
- package/src/packlets/identity/default-space-state-machine.ts +44 -0
- package/src/packlets/identity/identity-service.test.ts +35 -5
- package/src/packlets/identity/identity-service.ts +76 -8
- package/src/packlets/identity/identity.ts +25 -2
- package/src/packlets/services/service-context.ts +1 -4
- package/src/packlets/services/service-host.ts +13 -40
- package/src/packlets/spaces/automerge-space-state.ts +11 -2
- package/src/packlets/spaces/data-space-manager.test.ts +46 -1
- package/src/packlets/spaces/data-space-manager.ts +81 -31
- package/src/packlets/spaces/data-space.ts +82 -149
- package/src/packlets/spaces/epoch-migrations.ts +135 -0
- package/src/packlets/spaces/spaces-service.ts +4 -2
- package/src/packlets/testing/test-builder.ts +12 -10
- package/src/version.ts +1 -1
- package/dist/lib/browser/chunk-HSM5JUN6.mjs.map +0 -7
- package/dist/lib/node/chunk-4OGPGXOU.cjs.map +0 -7
|
@@ -12,7 +12,7 @@ import { log } from '@dxos/log';
|
|
|
12
12
|
import { SpaceState } from '@dxos/protocols/proto/dxos/client/services';
|
|
13
13
|
import { describe, openAndClose, test } from '@dxos/test';
|
|
14
14
|
|
|
15
|
-
import { TestBuilder } from '../testing';
|
|
15
|
+
import { TestBuilder, type TestPeer } from '../testing';
|
|
16
16
|
|
|
17
17
|
describe('DataSpaceManager', () => {
|
|
18
18
|
test('create space', async () => {
|
|
@@ -168,5 +168,50 @@ describe('DataSpaceManager', () => {
|
|
|
168
168
|
500,
|
|
169
169
|
);
|
|
170
170
|
});
|
|
171
|
+
|
|
172
|
+
test('activate opens a lazily loaded space', async () => {
|
|
173
|
+
const builder = new TestBuilder();
|
|
174
|
+
|
|
175
|
+
const peer = builder.createPeer();
|
|
176
|
+
await peer.createIdentity();
|
|
177
|
+
await openAndClose(peer.echoHost, peer.dataSpaceManager);
|
|
178
|
+
|
|
179
|
+
await peer.dataSpaceManager.createSpace();
|
|
180
|
+
await reloadDataSpaces(peer);
|
|
181
|
+
|
|
182
|
+
const space = getFirstSpace(peer);
|
|
183
|
+
expect(space.state).to.equal(SpaceState.CLOSED);
|
|
184
|
+
await space.activate();
|
|
185
|
+
await asyncTimeout(
|
|
186
|
+
space.stateUpdate.waitForCondition(() => space.state === SpaceState.READY),
|
|
187
|
+
500,
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('deactivate lazily loaded space ', async () => {
|
|
192
|
+
const builder = new TestBuilder();
|
|
193
|
+
|
|
194
|
+
const peer = builder.createPeer();
|
|
195
|
+
await peer.createIdentity();
|
|
196
|
+
await openAndClose(peer.echoHost, peer.dataSpaceManager);
|
|
197
|
+
|
|
198
|
+
await peer.dataSpaceManager.createSpace();
|
|
199
|
+
await reloadDataSpaces(peer);
|
|
200
|
+
|
|
201
|
+
await getFirstSpace(peer).deactivate();
|
|
202
|
+
|
|
203
|
+
await reloadDataSpaces(peer);
|
|
204
|
+
|
|
205
|
+
expect(getFirstSpace(peer).state).to.eq(SpaceState.INACTIVE);
|
|
206
|
+
});
|
|
171
207
|
});
|
|
208
|
+
|
|
209
|
+
const reloadDataSpaces = async (peer: TestPeer) => {
|
|
210
|
+
await peer.dataSpaceManager.close();
|
|
211
|
+
await peer.dataSpaceManager.open();
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const getFirstSpace = (peer: TestPeer) => {
|
|
215
|
+
return Array.from(peer.dataSpaceManager.spaces.values())[0];
|
|
216
|
+
};
|
|
172
217
|
});
|
|
@@ -4,15 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
import { Event, synchronized, trackLeaks } from '@dxos/async';
|
|
6
6
|
import { type Doc } from '@dxos/automerge/automerge';
|
|
7
|
-
import { type AutomergeUrl } from '@dxos/automerge/automerge-repo';
|
|
8
|
-
import {
|
|
7
|
+
import { type AutomergeUrl, type DocHandle } from '@dxos/automerge/automerge-repo';
|
|
8
|
+
import { PropertiesType } from '@dxos/client-protocol';
|
|
9
|
+
import { Context, cancelWithContext } from '@dxos/context';
|
|
9
10
|
import {
|
|
11
|
+
getCredentialAssertion,
|
|
10
12
|
type CredentialSigner,
|
|
11
13
|
type DelegateInvitationCredential,
|
|
12
|
-
getCredentialAssertion,
|
|
13
14
|
type MemberInfo,
|
|
14
15
|
} from '@dxos/credentials';
|
|
15
|
-
import { type EchoHost } from '@dxos/echo-db';
|
|
16
|
+
import { convertLegacyReferences, findInlineObjectOfType, type EchoHost } from '@dxos/echo-db';
|
|
16
17
|
import {
|
|
17
18
|
AuthStatus,
|
|
18
19
|
type MetadataStore,
|
|
@@ -21,7 +22,14 @@ import {
|
|
|
21
22
|
type SpaceProtocol,
|
|
22
23
|
type SpaceProtocolSession,
|
|
23
24
|
} from '@dxos/echo-pipeline';
|
|
24
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
LEGACY_TYPE_PROPERTIES,
|
|
27
|
+
SpaceDocVersion,
|
|
28
|
+
encodeReference,
|
|
29
|
+
type ObjectStructure,
|
|
30
|
+
type SpaceDoc,
|
|
31
|
+
} from '@dxos/echo-protocol';
|
|
32
|
+
import { TYPE_PROPERTIES, generateEchoId, getTypeReference } from '@dxos/echo-schema';
|
|
25
33
|
import { type FeedStore } from '@dxos/feed-store';
|
|
26
34
|
import { invariant } from '@dxos/invariant';
|
|
27
35
|
import { type Keyring } from '@dxos/keyring';
|
|
@@ -31,15 +39,15 @@ import { trace as Trace } from '@dxos/protocols';
|
|
|
31
39
|
import { Invitation, SpaceState } from '@dxos/protocols/proto/dxos/client/services';
|
|
32
40
|
import { type FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
33
41
|
import { type SpaceMetadata } from '@dxos/protocols/proto/dxos/echo/metadata';
|
|
34
|
-
import { type Credential, type ProfileDocument
|
|
42
|
+
import { SpaceMember, type Credential, type ProfileDocument } from '@dxos/protocols/proto/dxos/halo/credentials';
|
|
35
43
|
import { type DelegateSpaceInvitation } from '@dxos/protocols/proto/dxos/halo/invitations';
|
|
36
44
|
import { type PeerState } from '@dxos/protocols/proto/dxos/mesh/presence';
|
|
37
45
|
import { Gossip, Presence } from '@dxos/teleport-extension-gossip';
|
|
38
46
|
import { type Timeframe } from '@dxos/timeframe';
|
|
39
47
|
import { trace } from '@dxos/tracing';
|
|
40
|
-
import { ComplexMap, deferFunction, forEachAsync } from '@dxos/util';
|
|
48
|
+
import { ComplexMap, assignDeep, deferFunction, forEachAsync } from '@dxos/util';
|
|
41
49
|
|
|
42
|
-
import { DataSpace
|
|
50
|
+
import { DataSpace } from './data-space';
|
|
43
51
|
import { spaceGenesis } from './genesis';
|
|
44
52
|
import { createAuthProvider } from '../identity';
|
|
45
53
|
import { type InvitationsManager } from '../invitations';
|
|
@@ -47,6 +55,9 @@ import { type InvitationsManager } from '../invitations';
|
|
|
47
55
|
const PRESENCE_ANNOUNCE_INTERVAL = 10_000;
|
|
48
56
|
const PRESENCE_OFFLINE_TIMEOUT = 20_000;
|
|
49
57
|
|
|
58
|
+
// Space properties key for default metadata.
|
|
59
|
+
const DEFAULT_SPACE_KEY = '__DEFAULT__';
|
|
60
|
+
|
|
50
61
|
export interface SigningContext {
|
|
51
62
|
identityKey: PublicKey;
|
|
52
63
|
deviceKey: PublicKey;
|
|
@@ -88,8 +99,6 @@ export class DataSpaceManager {
|
|
|
88
99
|
|
|
89
100
|
private _isOpen = false;
|
|
90
101
|
private readonly _instanceId = PublicKey.random().toHex();
|
|
91
|
-
private readonly _spaceMemberPresenceAnnounceInterval: number;
|
|
92
|
-
private readonly _spaceMemberPresenceOfflineTimeout: number;
|
|
93
102
|
|
|
94
103
|
constructor(
|
|
95
104
|
private readonly _spaceManager: SpaceManager,
|
|
@@ -99,15 +108,8 @@ export class DataSpaceManager {
|
|
|
99
108
|
private readonly _feedStore: FeedStore<FeedMessage>,
|
|
100
109
|
private readonly _echoHost: EchoHost,
|
|
101
110
|
private readonly _invitationsManager: InvitationsManager,
|
|
102
|
-
|
|
111
|
+
private readonly _params?: DataSpaceManagerRuntimeParams,
|
|
103
112
|
) {
|
|
104
|
-
const {
|
|
105
|
-
spaceMemberPresenceAnnounceInterval = PRESENCE_ANNOUNCE_INTERVAL,
|
|
106
|
-
spaceMemberPresenceOfflineTimeout = PRESENCE_OFFLINE_TIMEOUT,
|
|
107
|
-
} = params ?? {};
|
|
108
|
-
this._spaceMemberPresenceAnnounceInterval = spaceMemberPresenceAnnounceInterval;
|
|
109
|
-
this._spaceMemberPresenceOfflineTimeout = spaceMemberPresenceOfflineTimeout;
|
|
110
|
-
|
|
111
113
|
trace.diagnostic({
|
|
112
114
|
id: 'spaces',
|
|
113
115
|
name: 'Spaces',
|
|
@@ -117,7 +119,7 @@ export class DataSpaceManager {
|
|
|
117
119
|
const rootHandle = rootUrl ? this._echoHost.automergeRepo.find(rootUrl as AutomergeUrl) : undefined;
|
|
118
120
|
const rootDoc = rootHandle?.docSync() as Doc<SpaceDoc> | undefined;
|
|
119
121
|
|
|
120
|
-
const properties = rootDoc &&
|
|
122
|
+
const properties = rootDoc && findInlineObjectOfType(rootDoc, TYPE_PROPERTIES);
|
|
121
123
|
|
|
122
124
|
return {
|
|
123
125
|
key: space.key.toHex(),
|
|
@@ -157,12 +159,6 @@ export class DataSpaceManager {
|
|
|
157
159
|
this._isOpen = true;
|
|
158
160
|
this.updated.emit();
|
|
159
161
|
|
|
160
|
-
for (const space of this._spaces.values()) {
|
|
161
|
-
if (space.state !== SpaceState.INACTIVE) {
|
|
162
|
-
space.initializeDataPipelineAsync();
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
162
|
log.trace('dxos.echo.data-space-manager.open', Trace.end({ id: this._instanceId }));
|
|
167
163
|
}
|
|
168
164
|
|
|
@@ -174,6 +170,7 @@ export class DataSpaceManager {
|
|
|
174
170
|
for (const space of this._spaces.values()) {
|
|
175
171
|
await space.close();
|
|
176
172
|
}
|
|
173
|
+
this._spaces.clear();
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
/**
|
|
@@ -197,6 +194,7 @@ export class DataSpaceManager {
|
|
|
197
194
|
|
|
198
195
|
const root = await this._echoHost.createSpaceRoot(spaceKey);
|
|
199
196
|
const space = await this._constructSpace(metadata);
|
|
197
|
+
await space.open();
|
|
200
198
|
|
|
201
199
|
const credentials = await spaceGenesis(this._keyring, this._signingContext, space.inner, root.url);
|
|
202
200
|
await this._metadataStore.addSpace(metadata);
|
|
@@ -211,6 +209,61 @@ export class DataSpaceManager {
|
|
|
211
209
|
return space;
|
|
212
210
|
}
|
|
213
211
|
|
|
212
|
+
async isDefaultSpace(space: DataSpace): Promise<boolean> {
|
|
213
|
+
if (!space.databaseRoot) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
switch (space.databaseRoot.getVersion()) {
|
|
217
|
+
case SpaceDocVersion.CURRENT: {
|
|
218
|
+
const [_, properties] = findInlineObjectOfType(space.databaseRoot.docSync()!, TYPE_PROPERTIES) ?? [];
|
|
219
|
+
return properties?.data?.[DEFAULT_SPACE_KEY] === this._signingContext.identityKey.toHex();
|
|
220
|
+
}
|
|
221
|
+
case SpaceDocVersion.LEGACY: {
|
|
222
|
+
const convertedDoc = await convertLegacyReferences(space.databaseRoot.docSync()!);
|
|
223
|
+
const [_, properties] = findInlineObjectOfType(convertedDoc, LEGACY_TYPE_PROPERTIES) ?? [];
|
|
224
|
+
return properties?.data?.[DEFAULT_SPACE_KEY] === this._signingContext.identityKey.toHex();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
default:
|
|
228
|
+
log.warn('unknown space version', { version: space.databaseRoot.getVersion(), spaceId: space.id });
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async createDefaultSpace() {
|
|
234
|
+
const space = await this.createSpace();
|
|
235
|
+
const document = await this._getSpaceRootDocument(space);
|
|
236
|
+
|
|
237
|
+
// TODO(dmaretskyi): Better API for low-level data access.
|
|
238
|
+
const properties: ObjectStructure = {
|
|
239
|
+
system: {
|
|
240
|
+
type: encodeReference(getTypeReference(PropertiesType)!),
|
|
241
|
+
},
|
|
242
|
+
data: {
|
|
243
|
+
[DEFAULT_SPACE_KEY]: this._signingContext.identityKey.toHex(),
|
|
244
|
+
},
|
|
245
|
+
meta: {
|
|
246
|
+
keys: [],
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const propertiesId = generateEchoId();
|
|
251
|
+
document.change((doc: SpaceDoc) => {
|
|
252
|
+
assignDeep(doc, ['objects', propertiesId], properties);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await this._echoHost.flush();
|
|
256
|
+
return space;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private async _getSpaceRootDocument(space: DataSpace): Promise<DocHandle<SpaceDoc>> {
|
|
260
|
+
const automergeIndex = space.automergeSpaceState.rootUrl;
|
|
261
|
+
invariant(automergeIndex);
|
|
262
|
+
const document = this._echoHost.automergeRepo.find<SpaceDoc>(automergeIndex as any);
|
|
263
|
+
await document.whenReady();
|
|
264
|
+
return document;
|
|
265
|
+
}
|
|
266
|
+
|
|
214
267
|
// TODO(burdon): Rename join space.
|
|
215
268
|
@synchronized
|
|
216
269
|
async acceptSpace(opts: AcceptSpaceOptions): Promise<DataSpace> {
|
|
@@ -226,6 +279,7 @@ export class DataSpaceManager {
|
|
|
226
279
|
};
|
|
227
280
|
|
|
228
281
|
const space = await this._constructSpace(metadata);
|
|
282
|
+
await space.open();
|
|
229
283
|
await this._metadataStore.addSpace(metadata);
|
|
230
284
|
space.initializeDataPipelineAsync();
|
|
231
285
|
|
|
@@ -254,8 +308,8 @@ export class DataSpaceManager {
|
|
|
254
308
|
localPeerId: this._signingContext.deviceKey,
|
|
255
309
|
});
|
|
256
310
|
const presence = new Presence({
|
|
257
|
-
announceInterval: this.
|
|
258
|
-
offlineTimeout: this.
|
|
311
|
+
announceInterval: this._params?.spaceMemberPresenceAnnounceInterval ?? PRESENCE_ANNOUNCE_INTERVAL,
|
|
312
|
+
offlineTimeout: this._params?.spaceMemberPresenceOfflineTimeout ?? PRESENCE_OFFLINE_TIMEOUT,
|
|
259
313
|
identityKey: this._signingContext.identityKey,
|
|
260
314
|
gossip,
|
|
261
315
|
});
|
|
@@ -336,10 +390,6 @@ export class DataSpaceManager {
|
|
|
336
390
|
}
|
|
337
391
|
});
|
|
338
392
|
|
|
339
|
-
if (metadata.state !== SpaceState.INACTIVE) {
|
|
340
|
-
await dataSpace.open();
|
|
341
|
-
}
|
|
342
|
-
|
|
343
393
|
if (metadata.controlTimeframe) {
|
|
344
394
|
dataSpace.inner.controlPipeline.state.setTargetTimeframe(metadata.controlTimeframe);
|
|
345
395
|
}
|
|
@@ -2,27 +2,25 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Event,
|
|
5
|
+
import { Event, Mutex, scheduleTask, sleep, synchronized, trackLeaks } from '@dxos/async';
|
|
6
6
|
import { AUTH_TIMEOUT } from '@dxos/client-protocol';
|
|
7
7
|
import { Context, ContextDisposedError, cancelWithContext } from '@dxos/context';
|
|
8
|
+
import type { SpecificCredential } from '@dxos/credentials';
|
|
8
9
|
import { timed, warnAfterTimeout } from '@dxos/debug';
|
|
9
|
-
import { type EchoHost } from '@dxos/echo-db';
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
createIdFromSpaceKey,
|
|
13
|
-
createMappedFeedWriter,
|
|
14
|
-
type MetadataStore,
|
|
15
|
-
type Space,
|
|
16
|
-
} from '@dxos/echo-pipeline';
|
|
17
|
-
import { type ObjectStructure, type SpaceDoc } from '@dxos/echo-protocol';
|
|
18
|
-
import { TYPE_PROPERTIES } from '@dxos/echo-schema';
|
|
10
|
+
import { type EchoHost, type DatabaseRoot } from '@dxos/echo-db';
|
|
11
|
+
import { createMappedFeedWriter, type MetadataStore, type Space } from '@dxos/echo-pipeline';
|
|
12
|
+
import { SpaceDocVersion } from '@dxos/echo-protocol';
|
|
19
13
|
import { type FeedStore } from '@dxos/feed-store';
|
|
20
|
-
import { failedInvariant
|
|
14
|
+
import { failedInvariant } from '@dxos/invariant';
|
|
21
15
|
import { type Keyring } from '@dxos/keyring';
|
|
22
16
|
import { PublicKey } from '@dxos/keys';
|
|
23
17
|
import { log } from '@dxos/log';
|
|
24
18
|
import { CancelledError, SystemError } from '@dxos/protocols';
|
|
25
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
type CreateEpochRequest,
|
|
21
|
+
SpaceState,
|
|
22
|
+
type Space as SpaceProto,
|
|
23
|
+
} from '@dxos/protocols/proto/dxos/client/services';
|
|
26
24
|
import { type FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
27
25
|
import { type SpaceCache } from '@dxos/protocols/proto/dxos/echo/metadata';
|
|
28
26
|
import {
|
|
@@ -36,10 +34,11 @@ import { type GossipMessage } from '@dxos/protocols/proto/dxos/mesh/teleport/gos
|
|
|
36
34
|
import { type Gossip, type Presence } from '@dxos/teleport-extension-gossip';
|
|
37
35
|
import { Timeframe } from '@dxos/timeframe';
|
|
38
36
|
import { trace } from '@dxos/tracing';
|
|
39
|
-
import { ComplexSet
|
|
37
|
+
import { ComplexSet } from '@dxos/util';
|
|
40
38
|
|
|
41
39
|
import { AutomergeSpaceState } from './automerge-space-state';
|
|
42
40
|
import { type SigningContext } from './data-space-manager';
|
|
41
|
+
import { runEpochMigration } from './epoch-migrations';
|
|
43
42
|
import { NotarizationPlugin } from './notarization-plugin';
|
|
44
43
|
import { TrustedKeySetAuthVerifier } from '../identity';
|
|
45
44
|
|
|
@@ -100,8 +99,12 @@ export class DataSpace {
|
|
|
100
99
|
// TODO(dmaretskyi): Move into Space?
|
|
101
100
|
private readonly _automergeSpaceState = new AutomergeSpaceState((rootUrl) => this._onNewAutomergeRoot(rootUrl));
|
|
102
101
|
|
|
102
|
+
private readonly _epochProcessingMutex = new Mutex();
|
|
103
|
+
|
|
103
104
|
private _state = SpaceState.CLOSED;
|
|
104
105
|
|
|
106
|
+
private _databaseRoot: DatabaseRoot | null = null;
|
|
107
|
+
|
|
105
108
|
/**
|
|
106
109
|
* Error for _state === SpaceState.ERROR.
|
|
107
110
|
*/
|
|
@@ -183,6 +186,10 @@ export class DataSpace {
|
|
|
183
186
|
return this._automergeSpaceState;
|
|
184
187
|
}
|
|
185
188
|
|
|
189
|
+
get databaseRoot(): DatabaseRoot | null {
|
|
190
|
+
return this._databaseRoot;
|
|
191
|
+
}
|
|
192
|
+
|
|
186
193
|
@trace.info({ depth: null })
|
|
187
194
|
private get _automergeInfo() {
|
|
188
195
|
return {
|
|
@@ -193,13 +200,17 @@ export class DataSpace {
|
|
|
193
200
|
|
|
194
201
|
@synchronized
|
|
195
202
|
async open() {
|
|
196
|
-
|
|
203
|
+
if (this._state === SpaceState.CLOSED) {
|
|
204
|
+
await this._open();
|
|
205
|
+
}
|
|
197
206
|
}
|
|
198
207
|
|
|
199
208
|
private async _open() {
|
|
209
|
+
await this._presence.open();
|
|
200
210
|
await this._gossip.open();
|
|
201
211
|
await this._notarizationPlugin.open();
|
|
202
212
|
await this._inner.spaceState.addCredentialProcessor(this._notarizationPlugin);
|
|
213
|
+
await this._automergeSpaceState.open();
|
|
203
214
|
await this._inner.spaceState.addCredentialProcessor(this._automergeSpaceState);
|
|
204
215
|
await this._inner.open(new Context());
|
|
205
216
|
this._state = SpaceState.CONTROL_ONLY;
|
|
@@ -225,10 +236,11 @@ export class DataSpace {
|
|
|
225
236
|
|
|
226
237
|
await this._inner.close();
|
|
227
238
|
await this._inner.spaceState.removeCredentialProcessor(this._automergeSpaceState);
|
|
239
|
+
await this._automergeSpaceState.close();
|
|
228
240
|
await this._inner.spaceState.removeCredentialProcessor(this._notarizationPlugin);
|
|
229
241
|
await this._notarizationPlugin.close();
|
|
230
242
|
|
|
231
|
-
await this._presence.
|
|
243
|
+
await this._presence.close();
|
|
232
244
|
await this._gossip.close();
|
|
233
245
|
}
|
|
234
246
|
|
|
@@ -279,12 +291,15 @@ export class DataSpace {
|
|
|
279
291
|
// Allow other tasks to run before loading the data pipeline.
|
|
280
292
|
await sleep(1);
|
|
281
293
|
|
|
294
|
+
const ready = this.stateUpdate.waitForCondition(() => this._state === SpaceState.READY);
|
|
295
|
+
|
|
282
296
|
this._automergeSpaceState.startProcessingRootDocs();
|
|
283
297
|
|
|
284
|
-
//
|
|
285
|
-
await
|
|
298
|
+
// TODO(dmaretskyi): Change so `initializeDataPipeline` doesn't wait for the space to be READY, but rather any state with a valid root.
|
|
299
|
+
await ready;
|
|
300
|
+
}
|
|
286
301
|
|
|
287
|
-
|
|
302
|
+
private async _enterReadyState() {
|
|
288
303
|
await this._callbacks.beforeReady?.();
|
|
289
304
|
|
|
290
305
|
this._state = SpaceState.READY;
|
|
@@ -376,6 +391,7 @@ export class DataSpace {
|
|
|
376
391
|
this._echoHost.replicateDocument(rootUrl);
|
|
377
392
|
const handle = this._echoHost.automergeRepo.find(rootUrl as any);
|
|
378
393
|
|
|
394
|
+
// TODO(dmaretskyi): Make this single-threaded (but doc loading should still be parallel to not block epoch processing).
|
|
379
395
|
queueMicrotask(async () => {
|
|
380
396
|
try {
|
|
381
397
|
await warnAfterTimeout(5_000, 'Automerge root doc load timeout (DataSpace)', async () => {
|
|
@@ -385,6 +401,10 @@ export class DataSpace {
|
|
|
385
401
|
return;
|
|
386
402
|
}
|
|
387
403
|
|
|
404
|
+
// Ensure only one root is processed at a time.
|
|
405
|
+
using _guard = await this._epochProcessingMutex.acquire();
|
|
406
|
+
|
|
407
|
+
// Attaching space keys to legacy documents.
|
|
388
408
|
const doc = handle.docSync() ?? failedInvariant();
|
|
389
409
|
if (!doc.access?.spaceKey) {
|
|
390
410
|
handle.change((doc: any) => {
|
|
@@ -394,10 +414,15 @@ export class DataSpace {
|
|
|
394
414
|
|
|
395
415
|
// TODO(dmaretskyi): Close roots.
|
|
396
416
|
// TODO(dmaretskyi): How do we handle changing to the next EPOCH?
|
|
397
|
-
|
|
398
|
-
|
|
417
|
+
const root = await this._echoHost.openSpaceRoot(handle.url);
|
|
418
|
+
this._databaseRoot = root;
|
|
419
|
+
if (root.getVersion() !== SpaceDocVersion.CURRENT) {
|
|
420
|
+
this._state = SpaceState.REQUIRES_MIGRATION;
|
|
421
|
+
this.stateUpdate.emit();
|
|
399
422
|
} else {
|
|
400
|
-
|
|
423
|
+
if (this._state !== SpaceState.READY) {
|
|
424
|
+
await this._enterReadyState();
|
|
425
|
+
}
|
|
401
426
|
}
|
|
402
427
|
} catch (err) {
|
|
403
428
|
if (err instanceof ContextDisposedError) {
|
|
@@ -420,131 +445,51 @@ export class DataSpace {
|
|
|
420
445
|
await this.inner.controlPipeline.writer.write({ credential: { credential } });
|
|
421
446
|
}
|
|
422
447
|
|
|
423
|
-
async createEpoch(options?: CreateEpochOptions) {
|
|
424
|
-
|
|
425
|
-
switch (options?.migration) {
|
|
426
|
-
case undefined:
|
|
427
|
-
case CreateEpochRequest.Migration.NONE:
|
|
428
|
-
{
|
|
429
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
430
|
-
epoch = {
|
|
431
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
432
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
433
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
434
|
-
automergeRoot: this._automergeSpaceState.lastEpoch?.subject.assertion?.automergeRoot,
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
break;
|
|
438
|
-
case CreateEpochRequest.Migration.INIT_AUTOMERGE:
|
|
439
|
-
{
|
|
440
|
-
const document = this._echoHost.automergeRepo.create();
|
|
441
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
442
|
-
epoch = {
|
|
443
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
444
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
445
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
446
|
-
automergeRoot: document.url,
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
break;
|
|
450
|
-
case CreateEpochRequest.Migration.PRUNE_AUTOMERGE_ROOT_HISTORY:
|
|
451
|
-
{
|
|
452
|
-
const currentRootUrl = this._automergeSpaceState.rootUrl;
|
|
453
|
-
const rootHandle = this._echoHost.automergeRepo.find(currentRootUrl as any);
|
|
454
|
-
await cancelWithContext(this._ctx, asyncTimeout(rootHandle.whenReady(), 10_000));
|
|
455
|
-
const newRoot = this._echoHost.automergeRepo.create(rootHandle.docSync());
|
|
456
|
-
invariant(typeof newRoot.url === 'string' && newRoot.url.length > 0);
|
|
457
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
458
|
-
epoch = {
|
|
459
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
460
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
461
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
462
|
-
automergeRoot: newRoot.url,
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
break;
|
|
466
|
-
case CreateEpochRequest.Migration.FRAGMENT_AUTOMERGE_ROOT:
|
|
467
|
-
{
|
|
468
|
-
log.info('Fragmenting');
|
|
469
|
-
|
|
470
|
-
const currentRootUrl = this._automergeSpaceState.rootUrl;
|
|
471
|
-
const rootHandle = this._echoHost.automergeRepo.find<SpaceDoc>(currentRootUrl as any);
|
|
472
|
-
await cancelWithContext(this._ctx, asyncTimeout(rootHandle.whenReady(), 10_000));
|
|
473
|
-
|
|
474
|
-
// Find properties object.
|
|
475
|
-
const objects = Object.entries((rootHandle.docSync() as SpaceDoc).objects!);
|
|
476
|
-
const properties = findPropertiesObject(rootHandle.docSync() as SpaceDoc);
|
|
477
|
-
const otherObjects = objects.filter(([key]) => key !== properties?.[0]);
|
|
478
|
-
invariant(properties, 'Properties not found');
|
|
479
|
-
|
|
480
|
-
// Create a new space doc with the properties object.
|
|
481
|
-
const newSpaceDoc: SpaceDoc = { ...rootHandle.docSync(), objects: Object.fromEntries([properties]) };
|
|
482
|
-
const newRoot = this._echoHost.automergeRepo.create(newSpaceDoc);
|
|
483
|
-
invariant(typeof newRoot.url === 'string' && newRoot.url.length > 0);
|
|
484
|
-
|
|
485
|
-
// Create new automerge documents for all objects.
|
|
486
|
-
const docLoader = new AutomergeDocumentLoaderImpl(
|
|
487
|
-
await createIdFromSpaceKey(this.key),
|
|
488
|
-
this._echoHost.automergeRepo,
|
|
489
|
-
this.key,
|
|
490
|
-
);
|
|
491
|
-
await docLoader.loadSpaceRootDocHandle(this._ctx, { rootUrl: newRoot.url });
|
|
492
|
-
|
|
493
|
-
otherObjects.forEach(([key, value]) => {
|
|
494
|
-
const handle = docLoader.createDocumentForObject(key);
|
|
495
|
-
handle.change((doc: any) => {
|
|
496
|
-
assignDeep(doc, ['objects', key], value);
|
|
497
|
-
});
|
|
498
|
-
});
|
|
448
|
+
async createEpoch(options?: CreateEpochOptions): Promise<SpecificCredential<Epoch> | null> {
|
|
449
|
+
const ctx = this._ctx.derive();
|
|
499
450
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
epoch = {
|
|
504
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
505
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
506
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
507
|
-
automergeRoot: newRoot.url,
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
break;
|
|
511
|
-
case CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT:
|
|
512
|
-
{
|
|
513
|
-
invariant(options.newAutomergeRoot);
|
|
514
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
515
|
-
epoch = {
|
|
516
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
517
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
518
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
519
|
-
automergeRoot: options.newAutomergeRoot,
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
break;
|
|
451
|
+
// Preserving existing behavior.
|
|
452
|
+
if (!options?.migration) {
|
|
453
|
+
return null;
|
|
523
454
|
}
|
|
524
455
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
456
|
+
const { newRoot } = await runEpochMigration(ctx, {
|
|
457
|
+
repo: this._echoHost.automergeRepo,
|
|
458
|
+
spaceId: this.id,
|
|
459
|
+
spaceKey: this.key,
|
|
460
|
+
migration: options.migration,
|
|
461
|
+
currentRoot: this._automergeSpaceState.rootUrl ?? null,
|
|
462
|
+
newAutomergeRoot: options.newAutomergeRoot,
|
|
463
|
+
});
|
|
528
464
|
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
465
|
+
const epoch: Epoch = {
|
|
466
|
+
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
467
|
+
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
468
|
+
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
469
|
+
automergeRoot: newRoot ?? this._automergeSpaceState.rootUrl,
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const credential = (await this._signingContext.credentialSigner.createCredential({
|
|
473
|
+
subject: this.key,
|
|
474
|
+
assertion: {
|
|
475
|
+
'@type': 'dxos.halo.credentials.Epoch',
|
|
476
|
+
...epoch,
|
|
538
477
|
},
|
|
478
|
+
})) as SpecificCredential<Epoch>;
|
|
479
|
+
|
|
480
|
+
const receipt = await this.inner.controlPipeline.writer.write({
|
|
481
|
+
credential: { credential },
|
|
539
482
|
});
|
|
540
483
|
|
|
541
484
|
await this.inner.controlPipeline.state.waitUntilTimeframe(new Timeframe([[receipt.feedKey, receipt.seq]]));
|
|
542
485
|
await this._echoHost.updateIndexes();
|
|
486
|
+
|
|
487
|
+
return credential;
|
|
543
488
|
}
|
|
544
489
|
|
|
545
490
|
@synchronized
|
|
546
491
|
async activate() {
|
|
547
|
-
if (
|
|
492
|
+
if (![SpaceState.CLOSED, SpaceState.INACTIVE].includes(this._state)) {
|
|
548
493
|
return;
|
|
549
494
|
}
|
|
550
495
|
|
|
@@ -558,25 +503,13 @@ export class DataSpace {
|
|
|
558
503
|
if (this._state === SpaceState.INACTIVE) {
|
|
559
504
|
return;
|
|
560
505
|
}
|
|
561
|
-
|
|
562
506
|
// Unregister from data service.
|
|
563
507
|
await this._metadataStore.setSpaceState(this.key, SpaceState.INACTIVE);
|
|
564
|
-
|
|
508
|
+
if (this._state !== SpaceState.CLOSED) {
|
|
509
|
+
await this._close();
|
|
510
|
+
}
|
|
565
511
|
this._state = SpaceState.INACTIVE;
|
|
566
512
|
log('new state', { state: SpaceState[this._state] });
|
|
567
513
|
this.stateUpdate.emit();
|
|
568
514
|
}
|
|
569
515
|
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Assumes properties are at root.
|
|
573
|
-
*/
|
|
574
|
-
export const findPropertiesObject = (spaceDoc: SpaceDoc): [string, ObjectStructure] | undefined => {
|
|
575
|
-
for (const id in spaceDoc.objects ?? {}) {
|
|
576
|
-
const obj = spaceDoc.objects![id];
|
|
577
|
-
if (obj.system.type?.itemId === TYPE_PROPERTIES) {
|
|
578
|
-
return [id, obj];
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
return undefined;
|
|
582
|
-
};
|