@dxos/client-services 0.5.9-main.bdf733d → 0.5.9-main.bf3bb8f
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-KNGR7BYM.mjs → chunk-IUSAD4RP.mjs} +1678 -935
- package/dist/lib/browser/chunk-IUSAD4RP.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +13 -4
- 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 +20 -13
- package/dist/lib/browser/packlets/testing/index.mjs.map +3 -3
- package/dist/lib/node/{chunk-WWHTBQNR.cjs → chunk-5PALJZPW.cjs} +1936 -1200
- package/dist/lib/node/chunk-5PALJZPW.cjs.map +7 -0
- package/dist/lib/node/index.cjs +53 -44
- 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 +26 -19
- package/dist/lib/node/packlets/testing/index.cjs.map +3 -3
- package/dist/types/src/packlets/identity/contacts-service.d.ts +14 -0
- package/dist/types/src/packlets/identity/contacts-service.d.ts.map +1 -0
- 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/invitations/invitations-handler.d.ts.map +1 -1
- package/dist/types/src/packlets/invitations/space-invitation-protocol.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 +15 -4
- package/dist/types/src/packlets/spaces/data-space-manager.d.ts.map +1 -1
- package/dist/types/src/packlets/spaces/data-space.d.ts +10 -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 +5 -2
- package/dist/types/src/packlets/spaces/spaces-service.d.ts.map +1 -1
- package/dist/types/src/packlets/storage/index.d.ts +1 -0
- package/dist/types/src/packlets/storage/index.d.ts.map +1 -1
- package/dist/types/src/packlets/storage/profile-archive.d.ts +14 -0
- package/dist/types/src/packlets/storage/profile-archive.d.ts.map +1 -0
- 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/contacts-service.ts +85 -0
- 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 +82 -8
- package/src/packlets/identity/identity.ts +25 -2
- package/src/packlets/invitations/invitations-handler.ts +13 -5
- package/src/packlets/invitations/space-invitation-protocol.ts +11 -32
- package/src/packlets/services/service-context.ts +1 -4
- package/src/packlets/services/service-host.ts +23 -42
- 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 +136 -33
- package/src/packlets/spaces/data-space.ts +89 -140
- package/src/packlets/spaces/epoch-migrations.ts +154 -0
- package/src/packlets/spaces/spaces-service.ts +56 -4
- package/src/packlets/storage/index.ts +1 -0
- package/src/packlets/storage/profile-archive.ts +111 -0
- package/src/packlets/testing/test-builder.ts +12 -10
- package/src/version.ts +1 -1
- package/dist/lib/browser/chunk-KNGR7BYM.mjs.map +0 -7
- package/dist/lib/node/chunk-WWHTBQNR.cjs.map +0 -7
|
@@ -4,15 +4,17 @@
|
|
|
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
|
-
|
|
14
|
+
createAdmissionCredentials,
|
|
13
15
|
type MemberInfo,
|
|
14
16
|
} from '@dxos/credentials';
|
|
15
|
-
import { type EchoHost } from '@dxos/echo-db';
|
|
17
|
+
import { convertLegacyReferences, findInlineObjectOfType, type EchoHost } from '@dxos/echo-db';
|
|
16
18
|
import {
|
|
17
19
|
AuthStatus,
|
|
18
20
|
type MetadataStore,
|
|
@@ -21,25 +23,33 @@ import {
|
|
|
21
23
|
type SpaceProtocol,
|
|
22
24
|
type SpaceProtocolSession,
|
|
23
25
|
} from '@dxos/echo-pipeline';
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
+
import { CredentialServerExtension } from '@dxos/echo-pipeline';
|
|
27
|
+
import {
|
|
28
|
+
LEGACY_TYPE_PROPERTIES,
|
|
29
|
+
SpaceDocVersion,
|
|
30
|
+
encodeReference,
|
|
31
|
+
type ObjectStructure,
|
|
32
|
+
type SpaceDoc,
|
|
33
|
+
} from '@dxos/echo-protocol';
|
|
34
|
+
import { TYPE_PROPERTIES, generateEchoId, getTypeReference } from '@dxos/echo-schema';
|
|
35
|
+
import { type FeedStore, writeMessages } from '@dxos/feed-store';
|
|
26
36
|
import { invariant } from '@dxos/invariant';
|
|
27
37
|
import { type Keyring } from '@dxos/keyring';
|
|
28
38
|
import { PublicKey } from '@dxos/keys';
|
|
29
39
|
import { log } from '@dxos/log';
|
|
30
|
-
import { trace as Trace } from '@dxos/protocols';
|
|
40
|
+
import { trace as Trace, AlreadyJoinedError } from '@dxos/protocols';
|
|
31
41
|
import { Invitation, SpaceState } from '@dxos/protocols/proto/dxos/client/services';
|
|
32
42
|
import { type FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
33
43
|
import { type SpaceMetadata } from '@dxos/protocols/proto/dxos/echo/metadata';
|
|
34
|
-
import { type Credential, type ProfileDocument
|
|
44
|
+
import { SpaceMember, type Credential, type ProfileDocument } from '@dxos/protocols/proto/dxos/halo/credentials';
|
|
35
45
|
import { type DelegateSpaceInvitation } from '@dxos/protocols/proto/dxos/halo/invitations';
|
|
36
46
|
import { type PeerState } from '@dxos/protocols/proto/dxos/mesh/presence';
|
|
37
47
|
import { Gossip, Presence } from '@dxos/teleport-extension-gossip';
|
|
38
48
|
import { type Timeframe } from '@dxos/timeframe';
|
|
39
49
|
import { trace } from '@dxos/tracing';
|
|
40
|
-
import { ComplexMap, deferFunction, forEachAsync } from '@dxos/util';
|
|
50
|
+
import { ComplexMap, assignDeep, deferFunction, forEachAsync } from '@dxos/util';
|
|
41
51
|
|
|
42
|
-
import { DataSpace
|
|
52
|
+
import { DataSpace } from './data-space';
|
|
43
53
|
import { spaceGenesis } from './genesis';
|
|
44
54
|
import { createAuthProvider } from '../identity';
|
|
45
55
|
import { type InvitationsManager } from '../invitations';
|
|
@@ -47,6 +57,9 @@ import { type InvitationsManager } from '../invitations';
|
|
|
47
57
|
const PRESENCE_ANNOUNCE_INTERVAL = 10_000;
|
|
48
58
|
const PRESENCE_OFFLINE_TIMEOUT = 20_000;
|
|
49
59
|
|
|
60
|
+
// Space properties key for default metadata.
|
|
61
|
+
const DEFAULT_SPACE_KEY = '__DEFAULT__';
|
|
62
|
+
|
|
50
63
|
export interface SigningContext {
|
|
51
64
|
identityKey: PublicKey;
|
|
52
65
|
deviceKey: PublicKey;
|
|
@@ -73,6 +86,14 @@ export type AcceptSpaceOptions = {
|
|
|
73
86
|
dataTimeframe?: Timeframe;
|
|
74
87
|
};
|
|
75
88
|
|
|
89
|
+
export type AdmitMemberOptions = {
|
|
90
|
+
spaceKey: PublicKey;
|
|
91
|
+
identityKey: PublicKey;
|
|
92
|
+
role: SpaceMember.Role;
|
|
93
|
+
profile?: ProfileDocument;
|
|
94
|
+
delegationCredentialId?: PublicKey;
|
|
95
|
+
};
|
|
96
|
+
|
|
76
97
|
export type DataSpaceManagerRuntimeParams = {
|
|
77
98
|
spaceMemberPresenceAnnounceInterval?: number;
|
|
78
99
|
spaceMemberPresenceOfflineTimeout?: number;
|
|
@@ -88,8 +109,6 @@ export class DataSpaceManager {
|
|
|
88
109
|
|
|
89
110
|
private _isOpen = false;
|
|
90
111
|
private readonly _instanceId = PublicKey.random().toHex();
|
|
91
|
-
private readonly _spaceMemberPresenceAnnounceInterval: number;
|
|
92
|
-
private readonly _spaceMemberPresenceOfflineTimeout: number;
|
|
93
112
|
|
|
94
113
|
constructor(
|
|
95
114
|
private readonly _spaceManager: SpaceManager,
|
|
@@ -99,15 +118,8 @@ export class DataSpaceManager {
|
|
|
99
118
|
private readonly _feedStore: FeedStore<FeedMessage>,
|
|
100
119
|
private readonly _echoHost: EchoHost,
|
|
101
120
|
private readonly _invitationsManager: InvitationsManager,
|
|
102
|
-
|
|
121
|
+
private readonly _params?: DataSpaceManagerRuntimeParams,
|
|
103
122
|
) {
|
|
104
|
-
const {
|
|
105
|
-
spaceMemberPresenceAnnounceInterval = PRESENCE_ANNOUNCE_INTERVAL,
|
|
106
|
-
spaceMemberPresenceOfflineTimeout = PRESENCE_OFFLINE_TIMEOUT,
|
|
107
|
-
} = params ?? {};
|
|
108
|
-
this._spaceMemberPresenceAnnounceInterval = spaceMemberPresenceAnnounceInterval;
|
|
109
|
-
this._spaceMemberPresenceOfflineTimeout = spaceMemberPresenceOfflineTimeout;
|
|
110
|
-
|
|
111
123
|
trace.diagnostic({
|
|
112
124
|
id: 'spaces',
|
|
113
125
|
name: 'Spaces',
|
|
@@ -117,7 +129,7 @@ export class DataSpaceManager {
|
|
|
117
129
|
const rootHandle = rootUrl ? this._echoHost.automergeRepo.find(rootUrl as AutomergeUrl) : undefined;
|
|
118
130
|
const rootDoc = rootHandle?.docSync() as Doc<SpaceDoc> | undefined;
|
|
119
131
|
|
|
120
|
-
const properties = rootDoc &&
|
|
132
|
+
const properties = rootDoc && findInlineObjectOfType(rootDoc, TYPE_PROPERTIES);
|
|
121
133
|
|
|
122
134
|
return {
|
|
123
135
|
key: space.key.toHex(),
|
|
@@ -157,12 +169,6 @@ export class DataSpaceManager {
|
|
|
157
169
|
this._isOpen = true;
|
|
158
170
|
this.updated.emit();
|
|
159
171
|
|
|
160
|
-
for (const space of this._spaces.values()) {
|
|
161
|
-
if (space.state !== SpaceState.INACTIVE) {
|
|
162
|
-
space.initializeDataPipelineAsync();
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
172
|
log.trace('dxos.echo.data-space-manager.open', Trace.end({ id: this._instanceId }));
|
|
167
173
|
}
|
|
168
174
|
|
|
@@ -174,6 +180,7 @@ export class DataSpaceManager {
|
|
|
174
180
|
for (const space of this._spaces.values()) {
|
|
175
181
|
await space.close();
|
|
176
182
|
}
|
|
183
|
+
this._spaces.clear();
|
|
177
184
|
}
|
|
178
185
|
|
|
179
186
|
/**
|
|
@@ -197,6 +204,7 @@ export class DataSpaceManager {
|
|
|
197
204
|
|
|
198
205
|
const root = await this._echoHost.createSpaceRoot(spaceKey);
|
|
199
206
|
const space = await this._constructSpace(metadata);
|
|
207
|
+
await space.open();
|
|
200
208
|
|
|
201
209
|
const credentials = await spaceGenesis(this._keyring, this._signingContext, space.inner, root.url);
|
|
202
210
|
await this._metadataStore.addSpace(metadata);
|
|
@@ -211,6 +219,61 @@ export class DataSpaceManager {
|
|
|
211
219
|
return space;
|
|
212
220
|
}
|
|
213
221
|
|
|
222
|
+
async isDefaultSpace(space: DataSpace): Promise<boolean> {
|
|
223
|
+
if (!space.databaseRoot) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
switch (space.databaseRoot.getVersion()) {
|
|
227
|
+
case SpaceDocVersion.CURRENT: {
|
|
228
|
+
const [_, properties] = findInlineObjectOfType(space.databaseRoot.docSync()!, TYPE_PROPERTIES) ?? [];
|
|
229
|
+
return properties?.data?.[DEFAULT_SPACE_KEY] === this._signingContext.identityKey.toHex();
|
|
230
|
+
}
|
|
231
|
+
case SpaceDocVersion.LEGACY: {
|
|
232
|
+
const convertedDoc = await convertLegacyReferences(space.databaseRoot.docSync()!);
|
|
233
|
+
const [_, properties] = findInlineObjectOfType(convertedDoc, LEGACY_TYPE_PROPERTIES) ?? [];
|
|
234
|
+
return properties?.data?.[DEFAULT_SPACE_KEY] === this._signingContext.identityKey.toHex();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
default:
|
|
238
|
+
log.warn('unknown space version', { version: space.databaseRoot.getVersion(), spaceId: space.id });
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async createDefaultSpace() {
|
|
244
|
+
const space = await this.createSpace();
|
|
245
|
+
const document = await this._getSpaceRootDocument(space);
|
|
246
|
+
|
|
247
|
+
// TODO(dmaretskyi): Better API for low-level data access.
|
|
248
|
+
const properties: ObjectStructure = {
|
|
249
|
+
system: {
|
|
250
|
+
type: encodeReference(getTypeReference(PropertiesType)!),
|
|
251
|
+
},
|
|
252
|
+
data: {
|
|
253
|
+
[DEFAULT_SPACE_KEY]: this._signingContext.identityKey.toHex(),
|
|
254
|
+
},
|
|
255
|
+
meta: {
|
|
256
|
+
keys: [],
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const propertiesId = generateEchoId();
|
|
261
|
+
document.change((doc: SpaceDoc) => {
|
|
262
|
+
assignDeep(doc, ['objects', propertiesId], properties);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
await this._echoHost.flush();
|
|
266
|
+
return space;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private async _getSpaceRootDocument(space: DataSpace): Promise<DocHandle<SpaceDoc>> {
|
|
270
|
+
const automergeIndex = space.automergeSpaceState.rootUrl;
|
|
271
|
+
invariant(automergeIndex);
|
|
272
|
+
const document = this._echoHost.automergeRepo.find<SpaceDoc>(automergeIndex as any);
|
|
273
|
+
await document.whenReady();
|
|
274
|
+
return document;
|
|
275
|
+
}
|
|
276
|
+
|
|
214
277
|
// TODO(burdon): Rename join space.
|
|
215
278
|
@synchronized
|
|
216
279
|
async acceptSpace(opts: AcceptSpaceOptions): Promise<DataSpace> {
|
|
@@ -226,6 +289,7 @@ export class DataSpaceManager {
|
|
|
226
289
|
};
|
|
227
290
|
|
|
228
291
|
const space = await this._constructSpace(metadata);
|
|
292
|
+
await space.open();
|
|
229
293
|
await this._metadataStore.addSpace(metadata);
|
|
230
294
|
space.initializeDataPipelineAsync();
|
|
231
295
|
|
|
@@ -233,6 +297,35 @@ export class DataSpaceManager {
|
|
|
233
297
|
return space;
|
|
234
298
|
}
|
|
235
299
|
|
|
300
|
+
async admitMember(options: AdmitMemberOptions): Promise<Credential> {
|
|
301
|
+
const space = this._spaceManager.spaces.get(options.spaceKey);
|
|
302
|
+
invariant(space);
|
|
303
|
+
|
|
304
|
+
if (space.spaceState.getMemberRole(options.identityKey) !== SpaceMember.Role.REMOVED) {
|
|
305
|
+
throw new AlreadyJoinedError();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// TODO(burdon): Check if already admitted.
|
|
309
|
+
const credentials: FeedMessage.Payload[] = await createAdmissionCredentials(
|
|
310
|
+
this._signingContext.credentialSigner,
|
|
311
|
+
options.identityKey,
|
|
312
|
+
space.key,
|
|
313
|
+
space.genesisFeedKey,
|
|
314
|
+
options.role,
|
|
315
|
+
space.spaceState.membershipChainHeads,
|
|
316
|
+
options.profile,
|
|
317
|
+
options.delegationCredentialId,
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
// TODO(dmaretskyi): Refactor.
|
|
321
|
+
invariant(credentials[0].credential);
|
|
322
|
+
const spaceMemberCredential = credentials[0].credential.credential;
|
|
323
|
+
invariant(getCredentialAssertion(spaceMemberCredential)['@type'] === 'dxos.halo.credentials.SpaceMember');
|
|
324
|
+
await writeMessages(space.controlPipeline.writer, credentials);
|
|
325
|
+
|
|
326
|
+
return spaceMemberCredential;
|
|
327
|
+
}
|
|
328
|
+
|
|
236
329
|
/**
|
|
237
330
|
* Wait until the space data pipeline is fully initialized.
|
|
238
331
|
* Used by invitation handler.
|
|
@@ -248,14 +341,27 @@ export class DataSpaceManager {
|
|
|
248
341
|
);
|
|
249
342
|
}
|
|
250
343
|
|
|
344
|
+
public async requestSpaceAdmissionCredential(spaceKey: PublicKey): Promise<Credential> {
|
|
345
|
+
return this._spaceManager.requestSpaceAdmissionCredential({
|
|
346
|
+
spaceKey,
|
|
347
|
+
identityKey: this._signingContext.identityKey,
|
|
348
|
+
timeout: 15_000,
|
|
349
|
+
swarmIdentity: {
|
|
350
|
+
peerKey: this._signingContext.deviceKey,
|
|
351
|
+
credentialProvider: createAuthProvider(this._signingContext.credentialSigner),
|
|
352
|
+
credentialAuthenticator: async () => true,
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
251
357
|
private async _constructSpace(metadata: SpaceMetadata) {
|
|
252
358
|
log('construct space', { metadata });
|
|
253
359
|
const gossip = new Gossip({
|
|
254
360
|
localPeerId: this._signingContext.deviceKey,
|
|
255
361
|
});
|
|
256
362
|
const presence = new Presence({
|
|
257
|
-
announceInterval: this.
|
|
258
|
-
offlineTimeout: this.
|
|
363
|
+
announceInterval: this._params?.spaceMemberPresenceAnnounceInterval ?? PRESENCE_ANNOUNCE_INTERVAL,
|
|
364
|
+
offlineTimeout: this._params?.spaceMemberPresenceOfflineTimeout ?? PRESENCE_OFFLINE_TIMEOUT,
|
|
259
365
|
identityKey: this._signingContext.identityKey,
|
|
260
366
|
gossip,
|
|
261
367
|
});
|
|
@@ -277,6 +383,7 @@ export class DataSpaceManager {
|
|
|
277
383
|
credentialAuthenticator: deferFunction(() => dataSpace.authVerifier.verifier),
|
|
278
384
|
},
|
|
279
385
|
onAuthorizedConnection: (session) => {
|
|
386
|
+
session.addExtension('dxos.mesh.teleport.admission-discovery', new CredentialServerExtension(space));
|
|
280
387
|
session.addExtension(
|
|
281
388
|
'dxos.mesh.teleport.gossip',
|
|
282
389
|
gossip.createExtension({ remotePeerId: session.remotePeerId }),
|
|
@@ -336,10 +443,6 @@ export class DataSpaceManager {
|
|
|
336
443
|
}
|
|
337
444
|
});
|
|
338
445
|
|
|
339
|
-
if (metadata.state !== SpaceState.INACTIVE) {
|
|
340
|
-
await dataSpace.open();
|
|
341
|
-
}
|
|
342
|
-
|
|
343
446
|
if (metadata.controlTimeframe) {
|
|
344
447
|
dataSpace.inner.controlPipeline.state.setTargetTimeframe(metadata.controlTimeframe);
|
|
345
448
|
}
|
|
@@ -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
|
|
|
@@ -76,6 +75,7 @@ export type DataSpaceParams = {
|
|
|
76
75
|
|
|
77
76
|
export type CreateEpochOptions = {
|
|
78
77
|
migration?: CreateEpochRequest.Migration;
|
|
78
|
+
newAutomergeRoot?: string;
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
@trackLeaks('open', 'close')
|
|
@@ -99,8 +99,12 @@ export class DataSpace {
|
|
|
99
99
|
// TODO(dmaretskyi): Move into Space?
|
|
100
100
|
private readonly _automergeSpaceState = new AutomergeSpaceState((rootUrl) => this._onNewAutomergeRoot(rootUrl));
|
|
101
101
|
|
|
102
|
+
private readonly _epochProcessingMutex = new Mutex();
|
|
103
|
+
|
|
102
104
|
private _state = SpaceState.CLOSED;
|
|
103
105
|
|
|
106
|
+
private _databaseRoot: DatabaseRoot | null = null;
|
|
107
|
+
|
|
104
108
|
/**
|
|
105
109
|
* Error for _state === SpaceState.ERROR.
|
|
106
110
|
*/
|
|
@@ -182,6 +186,10 @@ export class DataSpace {
|
|
|
182
186
|
return this._automergeSpaceState;
|
|
183
187
|
}
|
|
184
188
|
|
|
189
|
+
get databaseRoot(): DatabaseRoot | null {
|
|
190
|
+
return this._databaseRoot;
|
|
191
|
+
}
|
|
192
|
+
|
|
185
193
|
@trace.info({ depth: null })
|
|
186
194
|
private get _automergeInfo() {
|
|
187
195
|
return {
|
|
@@ -192,13 +200,17 @@ export class DataSpace {
|
|
|
192
200
|
|
|
193
201
|
@synchronized
|
|
194
202
|
async open() {
|
|
195
|
-
|
|
203
|
+
if (this._state === SpaceState.CLOSED) {
|
|
204
|
+
await this._open();
|
|
205
|
+
}
|
|
196
206
|
}
|
|
197
207
|
|
|
198
208
|
private async _open() {
|
|
209
|
+
await this._presence.open();
|
|
199
210
|
await this._gossip.open();
|
|
200
211
|
await this._notarizationPlugin.open();
|
|
201
212
|
await this._inner.spaceState.addCredentialProcessor(this._notarizationPlugin);
|
|
213
|
+
await this._automergeSpaceState.open();
|
|
202
214
|
await this._inner.spaceState.addCredentialProcessor(this._automergeSpaceState);
|
|
203
215
|
await this._inner.open(new Context());
|
|
204
216
|
this._state = SpaceState.CONTROL_ONLY;
|
|
@@ -224,10 +236,11 @@ export class DataSpace {
|
|
|
224
236
|
|
|
225
237
|
await this._inner.close();
|
|
226
238
|
await this._inner.spaceState.removeCredentialProcessor(this._automergeSpaceState);
|
|
239
|
+
await this._automergeSpaceState.close();
|
|
227
240
|
await this._inner.spaceState.removeCredentialProcessor(this._notarizationPlugin);
|
|
228
241
|
await this._notarizationPlugin.close();
|
|
229
242
|
|
|
230
|
-
await this._presence.
|
|
243
|
+
await this._presence.close();
|
|
231
244
|
await this._gossip.close();
|
|
232
245
|
}
|
|
233
246
|
|
|
@@ -278,12 +291,15 @@ export class DataSpace {
|
|
|
278
291
|
// Allow other tasks to run before loading the data pipeline.
|
|
279
292
|
await sleep(1);
|
|
280
293
|
|
|
294
|
+
const ready = this.stateUpdate.waitForCondition(() => this._state === SpaceState.READY);
|
|
295
|
+
|
|
281
296
|
this._automergeSpaceState.startProcessingRootDocs();
|
|
282
297
|
|
|
283
|
-
//
|
|
284
|
-
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
|
+
}
|
|
285
301
|
|
|
286
|
-
|
|
302
|
+
private async _enterReadyState() {
|
|
287
303
|
await this._callbacks.beforeReady?.();
|
|
288
304
|
|
|
289
305
|
this._state = SpaceState.READY;
|
|
@@ -370,11 +386,10 @@ export class DataSpace {
|
|
|
370
386
|
|
|
371
387
|
private _onNewAutomergeRoot(rootUrl: string) {
|
|
372
388
|
log('loading automerge root doc for space', { space: this.key, rootUrl });
|
|
373
|
-
|
|
374
|
-
// Workaround for https://github.com/automerge/automerge-repo/pull/292
|
|
375
|
-
this._echoHost.replicateDocument(rootUrl);
|
|
389
|
+
|
|
376
390
|
const handle = this._echoHost.automergeRepo.find(rootUrl as any);
|
|
377
391
|
|
|
392
|
+
// TODO(dmaretskyi): Make this single-threaded (but doc loading should still be parallel to not block epoch processing).
|
|
378
393
|
queueMicrotask(async () => {
|
|
379
394
|
try {
|
|
380
395
|
await warnAfterTimeout(5_000, 'Automerge root doc load timeout (DataSpace)', async () => {
|
|
@@ -384,6 +399,10 @@ export class DataSpace {
|
|
|
384
399
|
return;
|
|
385
400
|
}
|
|
386
401
|
|
|
402
|
+
// Ensure only one root is processed at a time.
|
|
403
|
+
using _guard = await this._epochProcessingMutex.acquire();
|
|
404
|
+
|
|
405
|
+
// Attaching space keys to legacy documents.
|
|
387
406
|
const doc = handle.docSync() ?? failedInvariant();
|
|
388
407
|
if (!doc.access?.spaceKey) {
|
|
389
408
|
handle.change((doc: any) => {
|
|
@@ -393,10 +412,19 @@ export class DataSpace {
|
|
|
393
412
|
|
|
394
413
|
// TODO(dmaretskyi): Close roots.
|
|
395
414
|
// TODO(dmaretskyi): How do we handle changing to the next EPOCH?
|
|
396
|
-
|
|
397
|
-
|
|
415
|
+
const root = await this._echoHost.openSpaceRoot(handle.url);
|
|
416
|
+
|
|
417
|
+
// NOTE: Make sure this assignment happens synchronously together with the state change.
|
|
418
|
+
this._databaseRoot = root;
|
|
419
|
+
if (root.getVersion() !== SpaceDocVersion.CURRENT) {
|
|
420
|
+
if (this._state !== SpaceState.REQUIRES_MIGRATION) {
|
|
421
|
+
this._state = SpaceState.REQUIRES_MIGRATION;
|
|
422
|
+
this.stateUpdate.emit();
|
|
423
|
+
}
|
|
398
424
|
} else {
|
|
399
|
-
|
|
425
|
+
if (this._state !== SpaceState.READY) {
|
|
426
|
+
await this._enterReadyState();
|
|
427
|
+
}
|
|
400
428
|
}
|
|
401
429
|
} catch (err) {
|
|
402
430
|
if (err instanceof ContextDisposedError) {
|
|
@@ -419,118 +447,51 @@ export class DataSpace {
|
|
|
419
447
|
await this.inner.controlPipeline.writer.write({ credential: { credential } });
|
|
420
448
|
}
|
|
421
449
|
|
|
422
|
-
async createEpoch(options?: CreateEpochOptions) {
|
|
423
|
-
|
|
424
|
-
switch (options?.migration) {
|
|
425
|
-
case undefined:
|
|
426
|
-
case CreateEpochRequest.Migration.NONE:
|
|
427
|
-
{
|
|
428
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
429
|
-
epoch = {
|
|
430
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
431
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
432
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
433
|
-
automergeRoot: this._automergeSpaceState.lastEpoch?.subject.assertion?.automergeRoot,
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
break;
|
|
437
|
-
case CreateEpochRequest.Migration.INIT_AUTOMERGE:
|
|
438
|
-
{
|
|
439
|
-
const document = this._echoHost.automergeRepo.create();
|
|
440
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
441
|
-
epoch = {
|
|
442
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
443
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
444
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
445
|
-
automergeRoot: document.url,
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
break;
|
|
449
|
-
case CreateEpochRequest.Migration.PRUNE_AUTOMERGE_ROOT_HISTORY:
|
|
450
|
-
{
|
|
451
|
-
const currentRootUrl = this._automergeSpaceState.rootUrl;
|
|
452
|
-
const rootHandle = this._echoHost.automergeRepo.find(currentRootUrl as any);
|
|
453
|
-
await cancelWithContext(this._ctx, asyncTimeout(rootHandle.whenReady(), 10_000));
|
|
454
|
-
const newRoot = this._echoHost.automergeRepo.create(rootHandle.docSync());
|
|
455
|
-
invariant(typeof newRoot.url === 'string' && newRoot.url.length > 0);
|
|
456
|
-
// TODO(dmaretskyi): Unify epoch construction.
|
|
457
|
-
epoch = {
|
|
458
|
-
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
459
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
460
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
461
|
-
automergeRoot: newRoot.url,
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
break;
|
|
465
|
-
case CreateEpochRequest.Migration.FRAGMENT_AUTOMERGE_ROOT:
|
|
466
|
-
{
|
|
467
|
-
log.info('Fragmenting');
|
|
468
|
-
|
|
469
|
-
const currentRootUrl = this._automergeSpaceState.rootUrl;
|
|
470
|
-
const rootHandle = this._echoHost.automergeRepo.find<SpaceDoc>(currentRootUrl as any);
|
|
471
|
-
await cancelWithContext(this._ctx, asyncTimeout(rootHandle.whenReady(), 10_000));
|
|
472
|
-
|
|
473
|
-
// Find properties object.
|
|
474
|
-
const objects = Object.entries((rootHandle.docSync() as SpaceDoc).objects!);
|
|
475
|
-
const properties = findPropertiesObject(rootHandle.docSync() as SpaceDoc);
|
|
476
|
-
const otherObjects = objects.filter(([key]) => key !== properties?.[0]);
|
|
477
|
-
invariant(properties, 'Properties not found');
|
|
478
|
-
|
|
479
|
-
// Create a new space doc with the properties object.
|
|
480
|
-
const newSpaceDoc: SpaceDoc = { ...rootHandle.docSync(), objects: Object.fromEntries([properties]) };
|
|
481
|
-
const newRoot = this._echoHost.automergeRepo.create(newSpaceDoc);
|
|
482
|
-
invariant(typeof newRoot.url === 'string' && newRoot.url.length > 0);
|
|
483
|
-
|
|
484
|
-
// Create new automerge documents for all objects.
|
|
485
|
-
const docLoader = new AutomergeDocumentLoaderImpl(
|
|
486
|
-
await createIdFromSpaceKey(this.key),
|
|
487
|
-
this._echoHost.automergeRepo,
|
|
488
|
-
this.key,
|
|
489
|
-
);
|
|
490
|
-
await docLoader.loadSpaceRootDocHandle(this._ctx, { rootUrl: newRoot.url });
|
|
491
|
-
|
|
492
|
-
otherObjects.forEach(([key, value]) => {
|
|
493
|
-
const handle = docLoader.createDocumentForObject(key);
|
|
494
|
-
handle.change((doc: any) => {
|
|
495
|
-
assignDeep(doc, ['objects', key], value);
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
// TODO(mykola): Delete old root.
|
|
450
|
+
async createEpoch(options?: CreateEpochOptions): Promise<SpecificCredential<Epoch> | null> {
|
|
451
|
+
const ctx = this._ctx.derive();
|
|
500
452
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
505
|
-
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
506
|
-
automergeRoot: newRoot.url,
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
break;
|
|
453
|
+
// Preserving existing behavior.
|
|
454
|
+
if (!options?.migration) {
|
|
455
|
+
return null;
|
|
510
456
|
}
|
|
511
457
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
458
|
+
const { newRoot } = await runEpochMigration(ctx, {
|
|
459
|
+
echoHost: this._echoHost,
|
|
460
|
+
spaceId: this.id,
|
|
461
|
+
spaceKey: this.key,
|
|
462
|
+
migration: options.migration,
|
|
463
|
+
currentRoot: this._automergeSpaceState.rootUrl ?? null,
|
|
464
|
+
newAutomergeRoot: options.newAutomergeRoot,
|
|
465
|
+
});
|
|
515
466
|
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
467
|
+
const epoch: Epoch = {
|
|
468
|
+
previousId: this._automergeSpaceState.lastEpoch?.id,
|
|
469
|
+
number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
|
|
470
|
+
timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe(),
|
|
471
|
+
automergeRoot: newRoot ?? this._automergeSpaceState.rootUrl,
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
const credential = (await this._signingContext.credentialSigner.createCredential({
|
|
475
|
+
subject: this.key,
|
|
476
|
+
assertion: {
|
|
477
|
+
'@type': 'dxos.halo.credentials.Epoch',
|
|
478
|
+
...epoch,
|
|
525
479
|
},
|
|
480
|
+
})) as SpecificCredential<Epoch>;
|
|
481
|
+
|
|
482
|
+
const receipt = await this.inner.controlPipeline.writer.write({
|
|
483
|
+
credential: { credential },
|
|
526
484
|
});
|
|
527
485
|
|
|
528
486
|
await this.inner.controlPipeline.state.waitUntilTimeframe(new Timeframe([[receipt.feedKey, receipt.seq]]));
|
|
487
|
+
await this._echoHost.updateIndexes();
|
|
488
|
+
|
|
489
|
+
return credential;
|
|
529
490
|
}
|
|
530
491
|
|
|
531
492
|
@synchronized
|
|
532
493
|
async activate() {
|
|
533
|
-
if (
|
|
494
|
+
if (![SpaceState.CLOSED, SpaceState.INACTIVE].includes(this._state)) {
|
|
534
495
|
return;
|
|
535
496
|
}
|
|
536
497
|
|
|
@@ -544,25 +505,13 @@ export class DataSpace {
|
|
|
544
505
|
if (this._state === SpaceState.INACTIVE) {
|
|
545
506
|
return;
|
|
546
507
|
}
|
|
547
|
-
|
|
548
508
|
// Unregister from data service.
|
|
549
509
|
await this._metadataStore.setSpaceState(this.key, SpaceState.INACTIVE);
|
|
550
|
-
|
|
510
|
+
if (this._state !== SpaceState.CLOSED) {
|
|
511
|
+
await this._close();
|
|
512
|
+
}
|
|
551
513
|
this._state = SpaceState.INACTIVE;
|
|
552
514
|
log('new state', { state: SpaceState[this._state] });
|
|
553
515
|
this.stateUpdate.emit();
|
|
554
516
|
}
|
|
555
517
|
}
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
* Assumes properties are at root.
|
|
559
|
-
*/
|
|
560
|
-
export const findPropertiesObject = (spaceDoc: SpaceDoc): [string, ObjectStructure] | undefined => {
|
|
561
|
-
for (const id in spaceDoc.objects ?? {}) {
|
|
562
|
-
const obj = spaceDoc.objects![id];
|
|
563
|
-
if (obj.system.type?.itemId === TYPE_PROPERTIES) {
|
|
564
|
-
return [id, obj];
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
return undefined;
|
|
568
|
-
};
|