@dxos/client-services 0.5.1-next.2ad6c47 → 0.5.1-next.5a770e3

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.
Files changed (94) hide show
  1. package/dist/lib/browser/{chunk-CMVTOS5E.mjs → chunk-MKFJAPSD.mjs} +1437 -1119
  2. package/dist/lib/browser/chunk-MKFJAPSD.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +36 -5
  4. package/dist/lib/browser/index.mjs.map +3 -3
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/packlets/testing/index.mjs +36 -14
  7. package/dist/lib/browser/packlets/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-EAWN3LCD.cjs → chunk-6MQMUI2L.cjs} +1552 -1238
  9. package/dist/lib/node/chunk-6MQMUI2L.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +78 -47
  11. package/dist/lib/node/index.cjs.map +3 -3
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/packlets/testing/index.cjs +40 -18
  14. package/dist/lib/node/packlets/testing/index.cjs.map +3 -3
  15. package/dist/types/src/packlets/diagnostics/diagnostics-collector.d.ts.map +1 -1
  16. package/dist/types/src/packlets/diagnostics/diagnostics.d.ts.map +1 -1
  17. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts +2 -1
  18. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts.map +1 -1
  19. package/dist/types/src/packlets/invitations/invitation-guest-extenstion.d.ts +39 -0
  20. package/dist/types/src/packlets/invitations/invitation-guest-extenstion.d.ts.map +1 -0
  21. package/dist/types/src/packlets/invitations/{invitation-extension.d.ts → invitation-host-extension.d.ts} +17 -31
  22. package/dist/types/src/packlets/invitations/invitation-host-extension.d.ts.map +1 -0
  23. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts +6 -1
  24. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts.map +1 -1
  25. package/dist/types/src/packlets/invitations/invitation-topology.d.ts +37 -0
  26. package/dist/types/src/packlets/invitations/invitation-topology.d.ts.map +1 -0
  27. package/dist/types/src/packlets/invitations/invitations-handler.d.ts +19 -10
  28. package/dist/types/src/packlets/invitations/invitations-handler.d.ts.map +1 -1
  29. package/dist/types/src/packlets/invitations/invitations-handler.test.d.ts +2 -0
  30. package/dist/types/src/packlets/invitations/invitations-handler.test.d.ts.map +1 -0
  31. package/dist/types/src/packlets/invitations/invitations-manager.d.ts +2 -1
  32. package/dist/types/src/packlets/invitations/invitations-manager.d.ts.map +1 -1
  33. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts +1 -0
  34. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts.map +1 -1
  35. package/dist/types/src/packlets/invitations/utils.d.ts +6 -0
  36. package/dist/types/src/packlets/invitations/utils.d.ts.map +1 -0
  37. package/dist/types/src/packlets/services/service-context.d.ts +12 -11
  38. package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
  39. package/dist/types/src/packlets/services/service-host.d.ts +1 -2
  40. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  41. package/dist/types/src/packlets/spaces/data-space-manager.d.ts +4 -3
  42. package/dist/types/src/packlets/spaces/data-space-manager.d.ts.map +1 -1
  43. package/dist/types/src/packlets/spaces/data-space.d.ts +4 -3
  44. package/dist/types/src/packlets/spaces/data-space.d.ts.map +1 -1
  45. package/dist/types/src/packlets/spaces/spaces-service.d.ts +2 -1
  46. package/dist/types/src/packlets/spaces/spaces-service.d.ts.map +1 -1
  47. package/dist/types/src/packlets/storage/level.d.ts +1 -2
  48. package/dist/types/src/packlets/storage/level.d.ts.map +1 -1
  49. package/dist/types/src/packlets/testing/invitation-utils.d.ts +2 -1
  50. package/dist/types/src/packlets/testing/invitation-utils.d.ts.map +1 -1
  51. package/dist/types/src/packlets/testing/test-builder.d.ts +5 -3
  52. package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
  53. package/dist/types/src/packlets/vault/shell-runtime.d.ts +10 -2
  54. package/dist/types/src/packlets/vault/shell-runtime.d.ts.map +1 -1
  55. package/dist/types/src/packlets/vault/worker-runtime.d.ts.map +1 -1
  56. package/dist/types/src/version.d.ts +1 -1
  57. package/package.json +36 -35
  58. package/src/packlets/diagnostics/diagnostics-collector.ts +14 -9
  59. package/src/packlets/diagnostics/diagnostics.ts +78 -68
  60. package/src/packlets/invitations/device-invitation-protocol.ts +5 -1
  61. package/src/packlets/invitations/invitation-guest-extenstion.ts +126 -0
  62. package/src/packlets/invitations/{invitation-extension.ts → invitation-host-extension.ts} +99 -105
  63. package/src/packlets/invitations/invitation-protocol.ts +7 -1
  64. package/src/packlets/invitations/invitation-topology.ts +87 -0
  65. package/src/packlets/invitations/invitations-handler.test.ts +361 -0
  66. package/src/packlets/invitations/invitations-handler.ts +246 -149
  67. package/src/packlets/invitations/invitations-manager.ts +45 -3
  68. package/src/packlets/invitations/space-invitation-protocol.ts +23 -3
  69. package/src/packlets/invitations/utils.ts +27 -0
  70. package/src/packlets/services/automerge-host.test.ts +6 -4
  71. package/src/packlets/services/service-context.test.ts +3 -3
  72. package/src/packlets/services/service-context.ts +18 -31
  73. package/src/packlets/services/service-host.test.ts +6 -0
  74. package/src/packlets/services/service-host.ts +11 -28
  75. package/src/packlets/spaces/data-space-manager.test.ts +4 -4
  76. package/src/packlets/spaces/data-space-manager.ts +8 -11
  77. package/src/packlets/spaces/data-space.ts +16 -19
  78. package/src/packlets/spaces/genesis.ts +1 -1
  79. package/src/packlets/spaces/spaces-service.ts +12 -6
  80. package/src/packlets/storage/level.ts +3 -2
  81. package/src/packlets/testing/invitation-utils.ts +23 -3
  82. package/src/packlets/testing/test-builder.ts +13 -15
  83. package/src/packlets/vault/shell-runtime.ts +40 -2
  84. package/src/packlets/vault/worker-runtime.ts +3 -1
  85. package/src/version.ts +1 -1
  86. package/dist/lib/browser/chunk-CMVTOS5E.mjs.map +0 -7
  87. package/dist/lib/node/chunk-EAWN3LCD.cjs.map +0 -7
  88. package/dist/types/src/packlets/indexing/index.d.ts +0 -2
  89. package/dist/types/src/packlets/indexing/index.d.ts.map +0 -1
  90. package/dist/types/src/packlets/indexing/util.d.ts +0 -11
  91. package/dist/types/src/packlets/indexing/util.d.ts.map +0 -1
  92. package/dist/types/src/packlets/invitations/invitation-extension.d.ts.map +0 -1
  93. package/src/packlets/indexing/index.ts +0 -5
  94. package/src/packlets/indexing/util.ts +0 -32
@@ -1196,13 +1196,16 @@ var DeviceInvitationProtocol = class {
1196
1196
  kind: Invitation.Kind.DEVICE
1197
1197
  };
1198
1198
  }
1199
- async delegate(invitation) {
1199
+ async delegate() {
1200
+ throw new Error("delegation not supported");
1201
+ }
1202
+ async cancelDelegation() {
1200
1203
  throw new Error("delegation not supported");
1201
1204
  }
1202
1205
  async admit(_, request) {
1203
1206
  invariant4(request.device, void 0, {
1204
1207
  F: __dxlog_file5,
1205
- L: 42,
1208
+ L: 46,
1206
1209
  S: this,
1207
1210
  A: [
1208
1211
  "request.device",
@@ -1248,7 +1251,7 @@ var DeviceInvitationProtocol = class {
1248
1251
  async accept(response, request) {
1249
1252
  invariant4(response.device, void 0, {
1250
1253
  F: __dxlog_file5,
1251
- L: 87,
1254
+ L: 91,
1252
1255
  S: this,
1253
1256
  A: [
1254
1257
  "response.device",
@@ -1258,7 +1261,7 @@ var DeviceInvitationProtocol = class {
1258
1261
  const { identityKey, haloSpaceKey, genesisFeedKey, controlTimeframe } = response.device;
1259
1262
  invariant4(request.device, void 0, {
1260
1263
  F: __dxlog_file5,
1261
- L: 90,
1264
+ L: 94,
1262
1265
  S: this,
1263
1266
  A: [
1264
1267
  "request.device",
@@ -1283,34 +1286,53 @@ var DeviceInvitationProtocol = class {
1283
1286
  };
1284
1287
 
1285
1288
  // packages/sdk/client-services/src/packlets/invitations/invitations-handler.ts
1286
- import { PushStream, scheduleTask as scheduleTask2, TimeoutError, Trigger as Trigger3 } from "@dxos/async";
1287
- import { AuthenticatingInvitation, INVITATION_TIMEOUT } from "@dxos/client-protocol";
1288
- import { Context as Context5 } from "@dxos/context";
1289
+ import { Mutex, scheduleTask as scheduleTask3, TimeoutError } from "@dxos/async";
1290
+ import { INVITATION_TIMEOUT } from "@dxos/client-protocol";
1291
+ import { ContextDisposedError as ContextDisposedError2 } from "@dxos/context";
1289
1292
  import { createKeyPair, sign } from "@dxos/crypto";
1290
- import { invariant as invariant6 } from "@dxos/invariant";
1291
- import { PublicKey as PublicKey6 } from "@dxos/keys";
1292
- import { log as log5 } from "@dxos/log";
1293
- import { createTeleportProtocolFactory, StarTopology } from "@dxos/network-manager";
1294
- import { InvalidInvitationExtensionRoleError as InvalidInvitationExtensionRoleError2, trace as trace4 } from "@dxos/protocols";
1295
- import { Invitation as Invitation3 } from "@dxos/protocols/proto/dxos/client/services";
1293
+ import { invariant as invariant8 } from "@dxos/invariant";
1294
+ import { PublicKey as PublicKey7 } from "@dxos/keys";
1295
+ import { log as log7 } from "@dxos/log";
1296
+ import { createTeleportProtocolFactory } from "@dxos/network-manager";
1297
+ import { InvalidInvitationExtensionRoleError as InvalidInvitationExtensionRoleError3, trace as trace4 } from "@dxos/protocols";
1298
+ import { Invitation as Invitation4 } from "@dxos/protocols/proto/dxos/client/services";
1296
1299
  import { AuthenticationResponse as AuthenticationResponse2 } from "@dxos/protocols/proto/dxos/halo/invitations";
1300
+ import { Options as Options4 } from "@dxos/protocols/proto/dxos/halo/invitations";
1301
+ import { ComplexSet as ComplexSet3 } from "@dxos/util";
1297
1302
 
1298
- // packages/sdk/client-services/src/packlets/invitations/invitation-extension.ts
1303
+ // packages/sdk/client-services/src/packlets/invitations/invitation-guest-extenstion.ts
1299
1304
  import { Trigger as Trigger2 } from "@dxos/async";
1300
- import { cancelWithContext, Context as Context4 } from "@dxos/context";
1301
- import { randomBytes, verify } from "@dxos/crypto";
1305
+ import { cancelWithContext as cancelWithContext2, Context as Context4 } from "@dxos/context";
1302
1306
  import { invariant as invariant5 } from "@dxos/invariant";
1303
- import { PublicKey as PublicKey5 } from "@dxos/keys";
1304
1307
  import { log as log4 } from "@dxos/log";
1305
- import { InvalidInvitationExtensionRoleError, schema as schema2, trace as trace3 } from "@dxos/protocols";
1306
- import { Invitation as Invitation2 } from "@dxos/protocols/proto/dxos/client/services";
1307
- import { AuthenticationResponse, Options } from "@dxos/protocols/proto/dxos/halo/invitations";
1308
+ import { InvalidInvitationExtensionRoleError, schema as schema2 } from "@dxos/protocols";
1309
+ import { Options } from "@dxos/protocols/proto/dxos/halo/invitations";
1308
1310
  import { RpcExtension } from "@dxos/teleport";
1309
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitation-extension.ts";
1311
+
1312
+ // packages/sdk/client-services/src/packlets/invitations/utils.ts
1313
+ import { cancelWithContext, ContextDisposedError } from "@dxos/context";
1314
+ import { Invitation as Invitation2 } from "@dxos/protocols/proto/dxos/client/services";
1315
+ var stateToString = (state) => {
1316
+ return Object.entries(Invitation2.State).find(([key, val]) => val === state)?.[0] ?? "unknown";
1317
+ };
1318
+ var tryAcquireBeforeContextDisposed = async (ctx, mutex) => {
1319
+ let guard;
1320
+ return cancelWithContext(ctx, (async () => {
1321
+ guard = await mutex.acquire();
1322
+ if (ctx.disposed) {
1323
+ guard.release();
1324
+ guard = void 0;
1325
+ throw new ContextDisposedError();
1326
+ }
1327
+ return guard;
1328
+ })());
1329
+ };
1330
+
1331
+ // packages/sdk/client-services/src/packlets/invitations/invitation-guest-extenstion.ts
1332
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitation-guest-extenstion.ts";
1310
1333
  var OPTIONS_TIMEOUT = 1e4;
1311
- var MAX_OTP_ATTEMPTS = 3;
1312
- var InvitationHostExtension = class extends RpcExtension {
1313
- constructor(_callbacks) {
1334
+ var InvitationGuestExtension = class extends RpcExtension {
1335
+ constructor(_invitationFlowMutex, _callbacks) {
1314
1336
  super({
1315
1337
  requested: {
1316
1338
  InvitationHostService: schema2.getService("dxos.halo.invitations.InvitationHostService")
@@ -1319,15 +1341,152 @@ var InvitationHostExtension = class extends RpcExtension {
1319
1341
  InvitationHostService: schema2.getService("dxos.halo.invitations.InvitationHostService")
1320
1342
  }
1321
1343
  });
1344
+ this._invitationFlowMutex = _invitationFlowMutex;
1322
1345
  this._callbacks = _callbacks;
1323
1346
  this._ctx = new Context4();
1324
1347
  this._remoteOptionsTrigger = new Trigger2();
1348
+ this._invitationFlowLock = null;
1349
+ }
1350
+ hasFlowLock() {
1351
+ return this._invitationFlowLock != null;
1352
+ }
1353
+ async getHandlers() {
1354
+ return {
1355
+ InvitationHostService: {
1356
+ options: async (options) => {
1357
+ invariant5(!this._remoteOptions, "Remote options already set.", {
1358
+ F: __dxlog_file6,
1359
+ L: 63,
1360
+ S: this,
1361
+ A: [
1362
+ "!this._remoteOptions",
1363
+ "'Remote options already set.'"
1364
+ ]
1365
+ });
1366
+ this._remoteOptions = options;
1367
+ this._remoteOptionsTrigger.wake();
1368
+ },
1369
+ introduce: () => {
1370
+ throw new Error("Method not allowed.");
1371
+ },
1372
+ authenticate: () => {
1373
+ throw new Error("Method not allowed.");
1374
+ },
1375
+ admit: () => {
1376
+ throw new Error("Method not allowed.");
1377
+ }
1378
+ }
1379
+ };
1380
+ }
1381
+ async onOpen(context) {
1382
+ await super.onOpen(context);
1383
+ try {
1384
+ log4("guest acquire lock", void 0, {
1385
+ F: __dxlog_file6,
1386
+ L: 84,
1387
+ S: this,
1388
+ C: (f, a) => f(...a)
1389
+ });
1390
+ this._invitationFlowLock = await tryAcquireBeforeContextDisposed(this._ctx, this._invitationFlowMutex);
1391
+ log4("guest lock acquired", void 0, {
1392
+ F: __dxlog_file6,
1393
+ L: 86,
1394
+ S: this,
1395
+ C: (f, a) => f(...a)
1396
+ });
1397
+ await cancelWithContext2(this._ctx, this.rpc.InvitationHostService.options({
1398
+ role: Options.Role.GUEST
1399
+ }));
1400
+ log4("options sent", void 0, {
1401
+ F: __dxlog_file6,
1402
+ L: 88,
1403
+ S: this,
1404
+ C: (f, a) => f(...a)
1405
+ });
1406
+ await cancelWithContext2(this._ctx, this._remoteOptionsTrigger.wait({
1407
+ timeout: OPTIONS_TIMEOUT
1408
+ }));
1409
+ log4("options received", void 0, {
1410
+ F: __dxlog_file6,
1411
+ L: 90,
1412
+ S: this,
1413
+ C: (f, a) => f(...a)
1414
+ });
1415
+ if (this._remoteOptions?.role !== Options.Role.HOST) {
1416
+ throw new InvalidInvitationExtensionRoleError(void 0, {
1417
+ expected: Options.Role.HOST,
1418
+ remoteOptions: this._remoteOptions,
1419
+ remotePeerId: context.remotePeerId
1420
+ });
1421
+ }
1422
+ this._callbacks.onOpen(this._ctx, context);
1423
+ } catch (err) {
1424
+ if (this._invitationFlowLock != null) {
1425
+ this._callbacks.onError(err);
1426
+ }
1427
+ if (!this._ctx.disposed) {
1428
+ context.close(err);
1429
+ }
1430
+ }
1431
+ }
1432
+ async onClose() {
1433
+ await this._destroy();
1434
+ }
1435
+ async onAbort() {
1436
+ await this._destroy();
1437
+ }
1438
+ async _destroy() {
1439
+ await this._ctx.dispose();
1440
+ if (this._invitationFlowLock != null) {
1441
+ this._invitationFlowLock.release();
1442
+ this._invitationFlowLock = null;
1443
+ log4("invitation flow lock released", void 0, {
1444
+ F: __dxlog_file6,
1445
+ L: 123,
1446
+ S: this,
1447
+ C: (f, a) => f(...a)
1448
+ });
1449
+ }
1450
+ }
1451
+ };
1452
+
1453
+ // packages/sdk/client-services/src/packlets/invitations/invitation-host-extension.ts
1454
+ import { Trigger as Trigger3, scheduleTask as scheduleTask2 } from "@dxos/async";
1455
+ import { cancelWithContext as cancelWithContext3, Context as Context5 } from "@dxos/context";
1456
+ import { randomBytes, verify } from "@dxos/crypto";
1457
+ import { invariant as invariant6, InvariantViolation } from "@dxos/invariant";
1458
+ import { PublicKey as PublicKey5 } from "@dxos/keys";
1459
+ import { log as log5 } from "@dxos/log";
1460
+ import { InvalidInvitationExtensionRoleError as InvalidInvitationExtensionRoleError2, schema as schema3, trace as trace3 } from "@dxos/protocols";
1461
+ import { Invitation as Invitation3 } from "@dxos/protocols/proto/dxos/client/services";
1462
+ import { AuthenticationResponse, Options as Options2 } from "@dxos/protocols/proto/dxos/halo/invitations";
1463
+ import { RpcExtension as RpcExtension2 } from "@dxos/teleport";
1464
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitation-host-extension.ts";
1465
+ var OPTIONS_TIMEOUT2 = 1e4;
1466
+ var MAX_OTP_ATTEMPTS = 3;
1467
+ var InvitationHostExtension = class extends RpcExtension2 {
1468
+ constructor(_invitationFlowMutex, _callbacks) {
1469
+ super({
1470
+ requested: {
1471
+ InvitationHostService: schema3.getService("dxos.halo.invitations.InvitationHostService")
1472
+ },
1473
+ exposed: {
1474
+ InvitationHostService: schema3.getService("dxos.halo.invitations.InvitationHostService")
1475
+ }
1476
+ });
1477
+ this._invitationFlowMutex = _invitationFlowMutex;
1478
+ this._callbacks = _callbacks;
1479
+ this._ctx = new Context5();
1480
+ this._remoteOptionsTrigger = new Trigger3();
1325
1481
  this._challenge = void 0;
1326
- this.invitation = void 0;
1327
1482
  this.guestProfile = void 0;
1328
1483
  this.authenticationPassed = false;
1329
1484
  this.authenticationRetry = 0;
1330
- this.completedTrigger = new Trigger2();
1485
+ this.completedTrigger = new Trigger3();
1486
+ this._invitationFlowLock = null;
1487
+ }
1488
+ hasFlowLock() {
1489
+ return this._invitationFlowLock != null;
1331
1490
  }
1332
1491
  async getHandlers() {
1333
1492
  return {
@@ -1335,9 +1494,9 @@ var InvitationHostExtension = class extends RpcExtension {
1335
1494
  // Perhaps in the future we will have more complex logic here.
1336
1495
  InvitationHostService: {
1337
1496
  options: async (options) => {
1338
- invariant5(!this._remoteOptions, "Remote options already set.", {
1339
- F: __dxlog_file6,
1340
- L: 90,
1497
+ invariant6(!this._remoteOptions, "Remote options already set.", {
1498
+ F: __dxlog_file7,
1499
+ L: 101,
1341
1500
  S: this,
1342
1501
  A: [
1343
1502
  "!this._remoteOptions",
@@ -1350,90 +1509,86 @@ var InvitationHostExtension = class extends RpcExtension {
1350
1509
  introduce: async (request) => {
1351
1510
  const { profile, invitationId } = request;
1352
1511
  const traceId = PublicKey5.random().toHex();
1353
- log4.trace("dxos.sdk.invitation-handler.host.introduce", trace3.begin({
1512
+ log5.trace("dxos.sdk.invitation-handler.host.introduce", trace3.begin({
1354
1513
  id: traceId
1355
1514
  }), {
1356
- F: __dxlog_file6,
1357
- L: 99,
1515
+ F: __dxlog_file7,
1516
+ L: 110,
1358
1517
  S: this,
1359
1518
  C: (f, a) => f(...a)
1360
1519
  });
1361
- const invitation = await this._callbacks.resolveInvitation(request);
1362
- if (!invitation) {
1363
- log4.warn("invitation not found", {
1364
- invitationId
1520
+ const invitation = this._requireActiveInvitation();
1521
+ this._assertInvitationState(Invitation3.State.CONNECTED);
1522
+ if (invitationId !== invitation?.invitationId) {
1523
+ log5.warn("incorrect invitationId", {
1524
+ expected: invitation.invitationId,
1525
+ actual: invitationId
1365
1526
  }, {
1366
- F: __dxlog_file6,
1367
- L: 103,
1527
+ F: __dxlog_file7,
1528
+ L: 116,
1368
1529
  S: this,
1369
1530
  C: (f, a) => f(...a)
1370
1531
  });
1371
- this._callbacks.onError(new Error("Invitation not found."));
1532
+ this._callbacks.onError(new Error("Incorrect invitationId."));
1533
+ scheduleTask2(this._ctx, () => this.close());
1372
1534
  return {
1373
- authMethod: Invitation2.AuthMethod.NONE
1535
+ authMethod: Invitation3.AuthMethod.NONE
1374
1536
  };
1375
1537
  }
1376
- this.invitation = invitation;
1377
- log4("guest introduced itself", {
1538
+ log5("guest introduced themselves", {
1378
1539
  guestProfile: profile
1379
1540
  }, {
1380
- F: __dxlog_file6,
1381
- L: 112,
1541
+ F: __dxlog_file7,
1542
+ L: 125,
1382
1543
  S: this,
1383
1544
  C: (f, a) => f(...a)
1384
1545
  });
1385
1546
  this.guestProfile = profile;
1386
- this._callbacks.onStateUpdate({
1387
- ...this.invitation,
1388
- state: Invitation2.State.READY_FOR_AUTHENTICATION
1389
- });
1390
- this._challenge = this.invitation.authMethod === Invitation2.AuthMethod.KNOWN_PUBLIC_KEY ? randomBytes(32) : void 0;
1391
- log4.trace("dxos.sdk.invitation-handler.host.introduce", trace3.end({
1547
+ this._callbacks.onStateUpdate(Invitation3.State.READY_FOR_AUTHENTICATION);
1548
+ this._challenge = invitation.authMethod === Invitation3.AuthMethod.KNOWN_PUBLIC_KEY ? randomBytes(32) : void 0;
1549
+ log5.trace("dxos.sdk.invitation-handler.host.introduce", trace3.end({
1392
1550
  id: traceId
1393
1551
  }), {
1394
- F: __dxlog_file6,
1395
- L: 122,
1552
+ F: __dxlog_file7,
1553
+ L: 132,
1396
1554
  S: this,
1397
1555
  C: (f, a) => f(...a)
1398
1556
  });
1399
1557
  return {
1400
- authMethod: this.invitation.authMethod,
1558
+ authMethod: invitation.authMethod,
1401
1559
  challenge: this._challenge
1402
1560
  };
1403
1561
  },
1404
1562
  authenticate: async ({ authCode: code, signedChallenge }) => {
1405
1563
  const traceId = PublicKey5.random().toHex();
1406
- log4.trace("dxos.sdk.invitation-handler.host.authenticate", trace3.begin({
1564
+ log5.trace("dxos.sdk.invitation-handler.host.authenticate", trace3.begin({
1407
1565
  id: traceId
1408
1566
  }), {
1409
- F: __dxlog_file6,
1410
- L: 131,
1567
+ F: __dxlog_file7,
1568
+ L: 141,
1411
1569
  S: this,
1412
1570
  C: (f, a) => f(...a)
1413
1571
  });
1414
- log4("received authentication request", {
1572
+ const invitation = this._requireActiveInvitation();
1573
+ log5("received authentication request", {
1415
1574
  authCode: code
1416
1575
  }, {
1417
- F: __dxlog_file6,
1418
- L: 132,
1576
+ F: __dxlog_file7,
1577
+ L: 144,
1419
1578
  S: this,
1420
1579
  C: (f, a) => f(...a)
1421
1580
  });
1422
1581
  let status = AuthenticationResponse.Status.OK;
1423
- invariant5(this.invitation, "Invitation is not set.", {
1424
- F: __dxlog_file6,
1425
- L: 135,
1426
- S: this,
1427
- A: [
1428
- "this.invitation",
1429
- "'Invitation is not set.'"
1430
- ]
1431
- });
1432
- switch (this.invitation.authMethod) {
1433
- case Invitation2.AuthMethod.NONE: {
1434
- log4("authentication not required", void 0, {
1435
- F: __dxlog_file6,
1436
- L: 138,
1582
+ this._assertInvitationState([
1583
+ Invitation3.State.AUTHENTICATING,
1584
+ Invitation3.State.READY_FOR_AUTHENTICATION
1585
+ ]);
1586
+ this._callbacks.onStateUpdate(Invitation3.State.AUTHENTICATING);
1587
+ switch (invitation.authMethod) {
1588
+ case Invitation3.AuthMethod.NONE: {
1589
+ log5("authentication not required", void 0, {
1590
+ F: __dxlog_file7,
1591
+ L: 152,
1437
1592
  S: this,
1438
1593
  C: (f, a) => f(...a)
1439
1594
  });
@@ -1441,11 +1596,11 @@ var InvitationHostExtension = class extends RpcExtension {
1441
1596
  status: AuthenticationResponse.Status.OK
1442
1597
  };
1443
1598
  }
1444
- case Invitation2.AuthMethod.SHARED_SECRET: {
1445
- if (this.invitation.authCode) {
1599
+ case Invitation3.AuthMethod.SHARED_SECRET: {
1600
+ if (invitation.authCode) {
1446
1601
  if (this.authenticationRetry++ > MAX_OTP_ATTEMPTS) {
1447
1602
  status = AuthenticationResponse.Status.INVALID_OPT_ATTEMPTS;
1448
- } else if (code !== this.invitation.authCode) {
1603
+ } else if (code !== invitation.authCode) {
1449
1604
  status = AuthenticationResponse.Status.INVALID_OTP;
1450
1605
  } else {
1451
1606
  this.authenticationPassed = true;
@@ -1453,12 +1608,12 @@ var InvitationHostExtension = class extends RpcExtension {
1453
1608
  }
1454
1609
  break;
1455
1610
  }
1456
- case Invitation2.AuthMethod.KNOWN_PUBLIC_KEY: {
1457
- if (!this.invitation.guestKeypair) {
1611
+ case Invitation3.AuthMethod.KNOWN_PUBLIC_KEY: {
1612
+ if (!invitation.guestKeypair) {
1458
1613
  status = AuthenticationResponse.Status.INTERNAL_ERROR;
1459
1614
  break;
1460
1615
  }
1461
- const isSignatureValid = this._challenge && verify(this._challenge, Buffer.from(signedChallenge ?? []), this.invitation.guestKeypair.publicKey.asBuffer());
1616
+ const isSignatureValid = this._challenge && verify(this._challenge, Buffer.from(signedChallenge ?? []), invitation.guestKeypair.publicKey.asBuffer());
1462
1617
  if (isSignatureValid) {
1463
1618
  this.authenticationPassed = true;
1464
1619
  } else {
@@ -1467,11 +1622,11 @@ var InvitationHostExtension = class extends RpcExtension {
1467
1622
  break;
1468
1623
  }
1469
1624
  default: {
1470
- log4.error("invalid authentication method", {
1471
- authMethod: this.invitation.authMethod
1625
+ log5.error("invalid authentication method", {
1626
+ authMethod: invitation.authMethod
1472
1627
  }, {
1473
- F: __dxlog_file6,
1474
- L: 176,
1628
+ F: __dxlog_file7,
1629
+ L: 190,
1475
1630
  S: this,
1476
1631
  C: (f, a) => f(...a)
1477
1632
  });
@@ -1479,14 +1634,24 @@ var InvitationHostExtension = class extends RpcExtension {
1479
1634
  break;
1480
1635
  }
1481
1636
  }
1482
- log4.trace("dxos.sdk.invitation-handler.host.authenticate", trace3.end({
1637
+ if (![
1638
+ AuthenticationResponse.Status.OK,
1639
+ AuthenticationResponse.Status.INVALID_OTP
1640
+ ].includes(status)) {
1641
+ this._callbacks.onError(new Error(`Authentication failed, with status=${status}`));
1642
+ scheduleTask2(this._ctx, () => this.close());
1643
+ return {
1644
+ status
1645
+ };
1646
+ }
1647
+ log5.trace("dxos.sdk.invitation-handler.host.authenticate", trace3.end({
1483
1648
  id: traceId,
1484
1649
  data: {
1485
1650
  status
1486
1651
  }
1487
1652
  }), {
1488
- F: __dxlog_file6,
1489
- L: 182,
1653
+ F: __dxlog_file7,
1654
+ L: 202,
1490
1655
  S: this,
1491
1656
  C: (f, a) => f(...a)
1492
1657
  });
@@ -1496,33 +1661,28 @@ var InvitationHostExtension = class extends RpcExtension {
1496
1661
  },
1497
1662
  admit: async (request) => {
1498
1663
  const traceId = PublicKey5.random().toHex();
1499
- log4.trace("dxos.sdk.invitation-handler.host.admit", trace3.begin({
1664
+ log5.trace("dxos.sdk.invitation-handler.host.admit", trace3.begin({
1500
1665
  id: traceId
1501
1666
  }), {
1502
- F: __dxlog_file6,
1503
- L: 188,
1667
+ F: __dxlog_file7,
1668
+ L: 208,
1504
1669
  S: this,
1505
1670
  C: (f, a) => f(...a)
1506
1671
  });
1672
+ const invitation = this._requireActiveInvitation();
1507
1673
  try {
1508
- invariant5(this.invitation, "Invitation is not set.", {
1509
- F: __dxlog_file6,
1510
- L: 191,
1511
- S: this,
1512
- A: [
1513
- "this.invitation",
1514
- "'Invitation is not set.'"
1515
- ]
1516
- });
1517
- if (isAuthenticationRequired(this.invitation) && !this.authenticationPassed) {
1518
- throw new Error("Not authenticated");
1674
+ if (isAuthenticationRequired(invitation)) {
1675
+ this._assertInvitationState(Invitation3.State.AUTHENTICATING);
1676
+ if (!this.authenticationPassed) {
1677
+ throw new Error("Not authenticated");
1678
+ }
1519
1679
  }
1520
1680
  const response = await this._callbacks.admit(request);
1521
- log4.trace("dxos.sdk.invitation-handler.host.admit", trace3.end({
1681
+ log5.trace("dxos.sdk.invitation-handler.host.admit", trace3.end({
1522
1682
  id: traceId
1523
1683
  }), {
1524
- F: __dxlog_file6,
1525
- L: 199,
1684
+ F: __dxlog_file7,
1685
+ L: 222,
1526
1686
  S: this,
1527
1687
  C: (f, a) => f(...a)
1528
1688
  });
@@ -1538,149 +1698,204 @@ var InvitationHostExtension = class extends RpcExtension {
1538
1698
  async onOpen(context) {
1539
1699
  await super.onOpen(context);
1540
1700
  try {
1701
+ log5("host acquire lock", void 0, {
1702
+ F: __dxlog_file7,
1703
+ L: 237,
1704
+ S: this,
1705
+ C: (f, a) => f(...a)
1706
+ });
1707
+ this._invitationFlowLock = await tryAcquireBeforeContextDisposed(this._ctx, this._invitationFlowMutex);
1708
+ log5("host lock acquired", void 0, {
1709
+ F: __dxlog_file7,
1710
+ L: 239,
1711
+ S: this,
1712
+ C: (f, a) => f(...a)
1713
+ });
1714
+ const lastState = this._requireActiveInvitation().state;
1715
+ this._callbacks.onStateUpdate(Invitation3.State.CONNECTING);
1541
1716
  await this.rpc.InvitationHostService.options({
1542
- role: Options.Role.HOST
1717
+ role: Options2.Role.HOST
1543
1718
  });
1544
- await cancelWithContext(this._ctx, this._remoteOptionsTrigger.wait({
1545
- timeout: OPTIONS_TIMEOUT
1719
+ log5("options sent", void 0, {
1720
+ F: __dxlog_file7,
1721
+ L: 243,
1722
+ S: this,
1723
+ C: (f, a) => f(...a)
1724
+ });
1725
+ await cancelWithContext3(this._ctx, this._remoteOptionsTrigger.wait({
1726
+ timeout: OPTIONS_TIMEOUT2
1546
1727
  }));
1547
- if (this._remoteOptions?.role !== Options.Role.GUEST) {
1548
- throw new InvalidInvitationExtensionRoleError(void 0, {
1549
- expected: Options.Role.GUEST,
1550
- remoteOptions: this._remoteOptions
1728
+ log5("options received", void 0, {
1729
+ F: __dxlog_file7,
1730
+ L: 245,
1731
+ S: this,
1732
+ C: (f, a) => f(...a)
1733
+ });
1734
+ if (this._remoteOptions?.role !== Options2.Role.GUEST) {
1735
+ this._callbacks.onStateUpdate(lastState);
1736
+ throw new InvalidInvitationExtensionRoleError2(void 0, {
1737
+ expected: Options2.Role.GUEST,
1738
+ remoteOptions: this._remoteOptions,
1739
+ remotePeerId: context.remotePeerId
1551
1740
  });
1552
1741
  }
1553
- this._callbacks.onOpen();
1742
+ this._callbacks.onStateUpdate(Invitation3.State.CONNECTED);
1743
+ this._callbacks.onOpen(this._ctx, context);
1554
1744
  } catch (err) {
1555
- this._callbacks.onError(err);
1745
+ if (this._invitationFlowLock != null) {
1746
+ this._callbacks.onError(err);
1747
+ }
1748
+ if (!this._ctx.disposed) {
1749
+ context.close(err);
1750
+ }
1556
1751
  }
1557
1752
  }
1558
- async onClose() {
1559
- await this._ctx.dispose();
1753
+ _requireActiveInvitation() {
1754
+ const invitation = this._callbacks.activeInvitation;
1755
+ if (invitation == null) {
1756
+ scheduleTask2(this._ctx, () => this.close());
1757
+ throw new Error("Active invitation not found");
1758
+ }
1759
+ return invitation;
1560
1760
  }
1561
- };
1562
- var InvitationGuestExtension = class extends RpcExtension {
1563
- constructor(_callbacks) {
1564
- super({
1565
- requested: {
1566
- InvitationHostService: schema2.getService("dxos.halo.invitations.InvitationHostService")
1567
- },
1568
- exposed: {
1569
- InvitationHostService: schema2.getService("dxos.halo.invitations.InvitationHostService")
1570
- }
1571
- });
1572
- this._callbacks = _callbacks;
1573
- this._ctx = new Context4();
1574
- this._remoteOptionsTrigger = new Trigger2();
1761
+ _assertInvitationState(stateOrMany) {
1762
+ const invitation = this._requireActiveInvitation();
1763
+ const validStates = Array.isArray(stateOrMany) ? stateOrMany : [
1764
+ stateOrMany
1765
+ ];
1766
+ if (!validStates.includes(invitation.state)) {
1767
+ scheduleTask2(this._ctx, () => this.close());
1768
+ throw new InvariantViolation(`Expected ${stateToString(invitation.state)} to be one of [${validStates.map(stateToString).join(", ")}]`);
1769
+ }
1575
1770
  }
1576
- async getHandlers() {
1577
- return {
1578
- InvitationHostService: {
1579
- options: async (options) => {
1580
- invariant5(!this._remoteOptions, "Remote options already set.", {
1581
- F: __dxlog_file6,
1582
- L: 266,
1583
- S: this,
1584
- A: [
1585
- "!this._remoteOptions",
1586
- "'Remote options already set.'"
1587
- ]
1588
- });
1589
- this._remoteOptions = options;
1590
- this._remoteOptionsTrigger.wake();
1591
- },
1592
- introduce: () => {
1593
- throw new Error("Method not allowed.");
1594
- },
1595
- authenticate: () => {
1596
- throw new Error("Method not allowed.");
1597
- },
1598
- admit: () => {
1599
- throw new Error("Method not allowed.");
1600
- }
1601
- }
1602
- };
1771
+ async onClose() {
1772
+ await this._destroy();
1603
1773
  }
1604
- async onOpen(context) {
1605
- await super.onOpen(context);
1606
- try {
1607
- log4("begin options", void 0, {
1608
- F: __dxlog_file6,
1609
- L: 287,
1610
- S: this,
1611
- C: (f, a) => f(...a)
1612
- });
1613
- await cancelWithContext(this._ctx, this.rpc.InvitationHostService.options({
1614
- role: Options.Role.GUEST
1615
- }));
1616
- await cancelWithContext(this._ctx, this._remoteOptionsTrigger.wait({
1617
- timeout: OPTIONS_TIMEOUT
1618
- }));
1619
- log4("end options", void 0, {
1620
- F: __dxlog_file6,
1621
- L: 290,
1774
+ async onAbort() {
1775
+ await this._destroy();
1776
+ }
1777
+ async _destroy() {
1778
+ await this._ctx.dispose();
1779
+ if (this._invitationFlowLock != null) {
1780
+ this._invitationFlowLock?.release();
1781
+ this._invitationFlowLock = null;
1782
+ log5("invitation flow lock released", void 0, {
1783
+ F: __dxlog_file7,
1784
+ L: 300,
1622
1785
  S: this,
1623
1786
  C: (f, a) => f(...a)
1624
1787
  });
1625
- if (this._remoteOptions?.role !== Options.Role.HOST) {
1626
- throw new InvalidInvitationExtensionRoleError(void 0, {
1627
- expected: Options.Role.HOST,
1628
- remoteOptions: this._remoteOptions
1629
- });
1630
- }
1631
- this._callbacks.onOpen(this._ctx);
1632
- } catch (err) {
1633
- log4("openError", err, {
1634
- F: __dxlog_file6,
1635
- L: 300,
1788
+ }
1789
+ }
1790
+ };
1791
+ var isAuthenticationRequired = (invitation) => invitation.authMethod !== Invitation3.AuthMethod.NONE;
1792
+
1793
+ // packages/sdk/client-services/src/packlets/invitations/invitation-topology.ts
1794
+ import { invariant as invariant7 } from "@dxos/invariant";
1795
+ import { PublicKey as PublicKey6 } from "@dxos/keys";
1796
+ import { log as log6 } from "@dxos/log";
1797
+ import { Options as Options3 } from "@dxos/protocols/proto/dxos/halo/invitations";
1798
+ import { ComplexSet as ComplexSet2 } from "@dxos/util";
1799
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitation-topology.ts";
1800
+ var InvitationTopology = class {
1801
+ constructor(_role) {
1802
+ this._role = _role;
1803
+ this._seenPeers = new ComplexSet2(PublicKey6.hash);
1804
+ }
1805
+ init(controller) {
1806
+ invariant7(!this._controller, "Already initialized.", {
1807
+ F: __dxlog_file8,
1808
+ L: 42,
1809
+ S: this,
1810
+ A: [
1811
+ "!this._controller",
1812
+ "'Already initialized.'"
1813
+ ]
1814
+ });
1815
+ this._controller = controller;
1816
+ }
1817
+ update() {
1818
+ invariant7(this._controller, "Not initialized.", {
1819
+ F: __dxlog_file8,
1820
+ L: 47,
1821
+ S: this,
1822
+ A: [
1823
+ "this._controller",
1824
+ "'Not initialized.'"
1825
+ ]
1826
+ });
1827
+ const { ownPeerId, candidates, connected, allPeers } = this._controller.getState();
1828
+ if (this._role === Options3.Role.GUEST) {
1829
+ return;
1830
+ }
1831
+ if (connected.length > 0) {
1832
+ connected.forEach((c) => this._seenPeers.add(c));
1833
+ return;
1834
+ }
1835
+ const firstUnknownPeer = candidates.find((peerId) => !this._seenPeers.has(peerId));
1836
+ this._seenPeers = new ComplexSet2(PublicKey6.hash, allPeers.filter((peerId) => this._seenPeers.has(peerId)));
1837
+ if (firstUnknownPeer != null) {
1838
+ log6("invitation connect", {
1839
+ ownPeerId,
1840
+ remotePeerId: firstUnknownPeer
1841
+ }, {
1842
+ F: __dxlog_file8,
1843
+ L: 69,
1636
1844
  S: this,
1637
1845
  C: (f, a) => f(...a)
1638
1846
  });
1639
- this._callbacks.onError(err);
1847
+ this._controller.connect(firstUnknownPeer);
1848
+ this._seenPeers.add(firstUnknownPeer);
1640
1849
  }
1641
1850
  }
1642
- async onClose() {
1643
- log4("onClose", void 0, {
1644
- F: __dxlog_file6,
1645
- L: 306,
1851
+ async onOffer(peer) {
1852
+ invariant7(this._controller, "Not initialized.", {
1853
+ F: __dxlog_file8,
1854
+ L: 76,
1646
1855
  S: this,
1647
- C: (f, a) => f(...a)
1856
+ A: [
1857
+ "this._controller",
1858
+ "'Not initialized.'"
1859
+ ]
1648
1860
  });
1649
- await this._ctx.dispose();
1861
+ return !this._seenPeers.has(peer);
1862
+ }
1863
+ async destroy() {
1864
+ this._seenPeers.clear();
1865
+ }
1866
+ toString() {
1867
+ return `InvitationTopology(${this._role === Options3.Role.GUEST ? "guest" : "host"})`;
1650
1868
  }
1651
1869
  };
1652
- var isAuthenticationRequired = (invitation) => invitation.authMethod !== Invitation2.AuthMethod.NONE;
1653
1870
 
1654
1871
  // packages/sdk/client-services/src/packlets/invitations/invitations-handler.ts
1655
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-handler.ts";
1872
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-handler.ts";
1873
+ var MAX_DELEGATED_INVITATION_HOST_TRIES = 3;
1656
1874
  var InvitationsHandler = class {
1657
1875
  /**
1658
1876
  * @internal
1659
1877
  */
1660
- constructor(_networkManager) {
1878
+ constructor(_networkManager, _defaultTeleportParams) {
1661
1879
  this._networkManager = _networkManager;
1880
+ this._defaultTeleportParams = _defaultTeleportParams;
1662
1881
  }
1663
1882
  handleInvitationFlow(ctx, stream, protocol, invitation) {
1883
+ const guardedState = this._createGuardedState(ctx, invitation, stream);
1664
1884
  const createExtension = () => {
1665
- const extension = new InvitationHostExtension({
1666
- onStateUpdate: (invitation2) => {
1667
- stream.next({
1668
- ...invitation2,
1669
- state: Invitation3.State.READY_FOR_AUTHENTICATION
1670
- });
1885
+ const extension = new InvitationHostExtension(guardedState.mutex, {
1886
+ get activeInvitation() {
1887
+ return ctx.disposed ? null : guardedState.current;
1671
1888
  },
1672
- resolveInvitation: async ({ invitationId }) => {
1673
- if (invitationId && invitationId !== invitation.invitationId) {
1674
- return void 0;
1675
- }
1676
- return invitation;
1889
+ onStateUpdate: (newState) => {
1890
+ guardedState.set(extension, newState);
1891
+ return guardedState.current;
1677
1892
  },
1678
1893
  admit: async (admissionRequest) => {
1679
1894
  try {
1680
1895
  const deviceKey = admissionRequest.device?.deviceKey ?? admissionRequest.space?.deviceKey;
1681
- invariant6(deviceKey, void 0, {
1682
- F: __dxlog_file7,
1683
- L: 87,
1896
+ invariant8(deviceKey, void 0, {
1897
+ F: __dxlog_file9,
1898
+ L: 90,
1684
1899
  S: this,
1685
1900
  A: [
1686
1901
  "deviceKey",
@@ -1691,123 +1906,128 @@ var InvitationsHandler = class {
1691
1906
  extension.completedTrigger.wake(deviceKey);
1692
1907
  return admissionResponse;
1693
1908
  } catch (err) {
1694
- stream.error(err);
1909
+ guardedState.error(extension, err);
1695
1910
  throw err;
1696
1911
  }
1697
1912
  },
1698
- onOpen: () => {
1699
- scheduleTask2(ctx, async () => {
1700
- const traceId = PublicKey6.random().toHex();
1913
+ onOpen: (connectionCtx, extensionsCtx) => {
1914
+ let admitted = false;
1915
+ connectionCtx.onDispose(() => {
1916
+ if (!admitted) {
1917
+ guardedState.error(extension, new ContextDisposedError2());
1918
+ }
1919
+ });
1920
+ scheduleTask3(connectionCtx, async () => {
1921
+ const traceId = PublicKey7.random().toHex();
1701
1922
  try {
1702
- log5.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.begin({
1923
+ log7.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.begin({
1703
1924
  id: traceId
1704
1925
  }), {
1705
- F: __dxlog_file7,
1706
- L: 105,
1926
+ F: __dxlog_file9,
1927
+ L: 115,
1707
1928
  S: this,
1708
1929
  C: (f, a) => f(...a)
1709
1930
  });
1710
- log5("connected", {
1931
+ log7("connected", {
1711
1932
  ...protocol.toJSON()
1712
1933
  }, {
1713
- F: __dxlog_file7,
1714
- L: 106,
1934
+ F: __dxlog_file9,
1935
+ L: 116,
1715
1936
  S: this,
1716
1937
  C: (f, a) => f(...a)
1717
1938
  });
1718
- stream.next({
1719
- ...invitation,
1720
- state: Invitation3.State.CONNECTED
1721
- });
1722
1939
  const deviceKey = await extension.completedTrigger.wait({
1723
1940
  timeout: invitation.timeout
1724
1941
  });
1725
- log5("admitted guest", {
1942
+ log7("admitted guest", {
1726
1943
  guest: deviceKey,
1727
1944
  ...protocol.toJSON()
1728
1945
  }, {
1729
- F: __dxlog_file7,
1730
- L: 109,
1946
+ F: __dxlog_file9,
1947
+ L: 118,
1731
1948
  S: this,
1732
1949
  C: (f, a) => f(...a)
1733
1950
  });
1734
- stream.next({
1735
- ...invitation,
1736
- state: Invitation3.State.SUCCESS
1737
- });
1738
- log5.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.end({
1951
+ guardedState.set(extension, Invitation4.State.SUCCESS);
1952
+ log7.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.end({
1739
1953
  id: traceId
1740
1954
  }), {
1741
- F: __dxlog_file7,
1742
- L: 111,
1743
- S: this,
1744
- C: (f, a) => f(...a)
1745
- });
1746
- } catch (err) {
1747
- if (err instanceof TimeoutError) {
1748
- log5("timeout", {
1749
- ...protocol.toJSON()
1750
- }, {
1751
- F: __dxlog_file7,
1752
- L: 114,
1753
- S: this,
1754
- C: (f, a) => f(...a)
1755
- });
1756
- stream.next({
1757
- ...invitation,
1758
- state: Invitation3.State.TIMEOUT
1759
- });
1760
- } else {
1761
- log5.error("failed", err, {
1762
- F: __dxlog_file7,
1763
- L: 117,
1764
- S: this,
1765
- C: (f, a) => f(...a)
1766
- });
1767
- stream.error(err);
1768
- }
1769
- log5.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.error({
1770
- id: traceId,
1771
- error: err
1772
- }), {
1773
- F: __dxlog_file7,
1955
+ F: __dxlog_file9,
1774
1956
  L: 120,
1775
1957
  S: this,
1776
1958
  C: (f, a) => f(...a)
1777
1959
  });
1778
- } finally {
1960
+ admitted = true;
1779
1961
  if (!invitation.multiUse) {
1780
- await swarmConnection.close();
1781
1962
  await ctx.dispose();
1782
1963
  }
1964
+ } catch (err) {
1965
+ if (err instanceof TimeoutError) {
1966
+ if (guardedState.set(extension, Invitation4.State.TIMEOUT)) {
1967
+ log7("timeout", {
1968
+ ...protocol.toJSON()
1969
+ }, {
1970
+ F: __dxlog_file9,
1971
+ L: 129,
1972
+ S: this,
1973
+ C: (f, a) => f(...a)
1974
+ });
1975
+ }
1976
+ } else {
1977
+ if (guardedState.error(extension, err)) {
1978
+ log7.error("failed", err, {
1979
+ F: __dxlog_file9,
1980
+ L: 133,
1981
+ S: this,
1982
+ C: (f, a) => f(...a)
1983
+ });
1984
+ }
1985
+ }
1986
+ log7.trace("dxos.sdk.invitations-handler.host.onOpen", trace4.error({
1987
+ id: traceId,
1988
+ error: err
1989
+ }), {
1990
+ F: __dxlog_file9,
1991
+ L: 136,
1992
+ S: this,
1993
+ C: (f, a) => f(...a)
1994
+ });
1995
+ extensionsCtx.close(err);
1783
1996
  }
1784
1997
  });
1785
1998
  },
1786
1999
  onError: (err) => {
1787
- if (err instanceof InvalidInvitationExtensionRoleError2) {
1788
- return;
1789
- }
1790
- if (err instanceof TimeoutError) {
1791
- log5("timeout", {
1792
- ...protocol.toJSON()
2000
+ if (err instanceof InvalidInvitationExtensionRoleError3) {
2001
+ log7("invalid role", {
2002
+ ...err.context
1793
2003
  }, {
1794
- F: __dxlog_file7,
1795
- L: 135,
2004
+ F: __dxlog_file9,
2005
+ L: 144,
1796
2006
  S: this,
1797
2007
  C: (f, a) => f(...a)
1798
2008
  });
1799
- stream.next({
1800
- ...invitation,
1801
- state: Invitation3.State.TIMEOUT
1802
- });
2009
+ return;
2010
+ }
2011
+ if (err instanceof TimeoutError) {
2012
+ if (guardedState.set(extension, Invitation4.State.TIMEOUT)) {
2013
+ log7("timeout", {
2014
+ err
2015
+ }, {
2016
+ F: __dxlog_file9,
2017
+ L: 149,
2018
+ S: this,
2019
+ C: (f, a) => f(...a)
2020
+ });
2021
+ }
1803
2022
  } else {
1804
- log5.error("failed", err, {
1805
- F: __dxlog_file7,
1806
- L: 138,
1807
- S: this,
1808
- C: (f, a) => f(...a)
1809
- });
1810
- stream.error(err);
2023
+ if (guardedState.error(extension, err)) {
2024
+ log7.error("failed", err, {
2025
+ F: __dxlog_file9,
2026
+ L: 153,
2027
+ S: this,
2028
+ C: (f, a) => f(...a)
2029
+ });
2030
+ }
1811
2031
  }
1812
2032
  }
1813
2033
  });
@@ -1815,58 +2035,32 @@ var InvitationsHandler = class {
1815
2035
  };
1816
2036
  if (invitation.lifetime && invitation.created) {
1817
2037
  if (invitation.created.getTime() + invitation.lifetime * 1e3 < Date.now()) {
1818
- log5.warn("invitation has already expired", void 0, {
1819
- F: __dxlog_file7,
1820
- L: 149,
2038
+ log7.warn("invitation has already expired", void 0, {
2039
+ F: __dxlog_file9,
2040
+ L: 164,
1821
2041
  S: this,
1822
2042
  C: (f, a) => f(...a)
1823
2043
  });
1824
2044
  } else {
1825
- scheduleTask2(ctx, async () => {
2045
+ scheduleTask3(ctx, async () => {
1826
2046
  await swarmConnection.close();
1827
- stream.next({
1828
- ...invitation,
1829
- state: Invitation3.State.EXPIRED
1830
- });
2047
+ guardedState.set(null, Invitation4.State.EXPIRED);
1831
2048
  await ctx.dispose();
1832
2049
  }, invitation.created.getTime() + invitation.lifetime * 1e3 - Date.now());
1833
2050
  }
1834
2051
  }
1835
2052
  let swarmConnection;
1836
- const invitationLabel = "invitation host for " + (invitation.kind === Invitation3.Kind.DEVICE ? "device" : `space ${invitation.spaceKey?.truncate()}`);
1837
- scheduleTask2(ctx, async () => {
1838
- const topic = invitation.swarmKey;
1839
- swarmConnection = await this._networkManager.joinSwarm({
1840
- topic,
1841
- peerId: topic,
1842
- protocolProvider: createTeleportProtocolFactory(async (teleport) => {
1843
- teleport.addExtension("dxos.halo.invitations", createExtension());
1844
- }),
1845
- topology: new StarTopology(topic),
1846
- label: invitationLabel
1847
- });
1848
- ctx.onDispose(() => swarmConnection.close());
1849
- stream.next({
1850
- ...invitation,
1851
- state: Invitation3.State.CONNECTING
1852
- });
2053
+ scheduleTask3(ctx, async () => {
2054
+ swarmConnection = await this._joinSwarm(ctx, invitation, Options4.Role.HOST, createExtension);
2055
+ guardedState.set(null, Invitation4.State.CONNECTING);
1853
2056
  });
1854
2057
  }
1855
- acceptInvitation(protocol, invitation, deviceProfile) {
2058
+ acceptInvitation(ctx, stream, protocol, invitation, otpEnteredTrigger, deviceProfile) {
1856
2059
  const { timeout = INVITATION_TIMEOUT } = invitation;
1857
- invariant6(protocol, void 0, {
1858
- F: __dxlog_file7,
1859
- L: 191,
1860
- S: this,
1861
- A: [
1862
- "protocol",
1863
- ""
1864
- ]
1865
- });
1866
2060
  if (deviceProfile) {
1867
- invariant6(invitation.kind === Invitation3.Kind.DEVICE, "deviceProfile provided for non-device invitation", {
1868
- F: __dxlog_file7,
1869
- L: 194,
2061
+ invariant8(invitation.kind === Invitation4.Kind.DEVICE, "deviceProfile provided for non-device invitation", {
2062
+ F: __dxlog_file9,
2063
+ L: 197,
1870
2064
  S: this,
1871
2065
  A: [
1872
2066
  "invitation.kind === Invitation.Kind.DEVICE",
@@ -1874,142 +2068,117 @@ var InvitationsHandler = class {
1874
2068
  ]
1875
2069
  });
1876
2070
  }
1877
- const authenticated = new Trigger3();
1878
- let admitted = false;
1879
- let currentState;
1880
- const stream = new PushStream();
1881
- const setState = (newData) => {
1882
- invariant6(newData.state !== void 0, void 0, {
1883
- F: __dxlog_file7,
1884
- L: 205,
1885
- S: this,
1886
- A: [
1887
- "newData.state !== undefined",
1888
- ""
1889
- ]
1890
- });
1891
- currentState = newData.state;
1892
- stream.next({
1893
- ...invitation,
1894
- ...newData
1895
- });
1896
- };
1897
- const ctx = new Context5({
1898
- onError: (err) => {
1899
- if (err instanceof TimeoutError) {
1900
- log5("timeout", {
1901
- ...protocol.toJSON()
1902
- }, {
1903
- F: __dxlog_file7,
1904
- L: 213,
1905
- S: this,
1906
- C: (f, a) => f(...a)
1907
- });
1908
- setState({
1909
- state: Invitation3.State.TIMEOUT
1910
- });
1911
- } else {
1912
- log5.warn("auth failed", err, {
1913
- F: __dxlog_file7,
1914
- L: 216,
1915
- S: this,
1916
- C: (f, a) => f(...a)
1917
- });
1918
- stream.error(err);
1919
- }
1920
- void ctx.dispose();
1921
- }
1922
- });
1923
- ctx.onDispose(() => {
1924
- log5("complete", {
1925
- ...protocol.toJSON()
2071
+ const triedPeersIds = new ComplexSet3(PublicKey7.hash);
2072
+ const guardedState = this._createGuardedState(ctx, invitation, stream);
2073
+ const shouldCancelInvitationFlow = (extension) => {
2074
+ const isLockedByAnotherConnection = guardedState.mutex.isLocked() && !extension.hasFlowLock();
2075
+ log7("should cancel invitation flow", {
2076
+ isLockedByAnotherConnection,
2077
+ invitationType: Invitation4.Type.DELEGATED,
2078
+ triedPeers: triedPeersIds.size
1926
2079
  }, {
1927
- F: __dxlog_file7,
1928
- L: 224,
2080
+ F: __dxlog_file9,
2081
+ L: 205,
1929
2082
  S: this,
1930
2083
  C: (f, a) => f(...a)
1931
2084
  });
1932
- stream.complete();
1933
- });
2085
+ if (isLockedByAnotherConnection) {
2086
+ return false;
2087
+ }
2088
+ return invitation.type !== Invitation4.Type.DELEGATED || triedPeersIds.size >= MAX_DELEGATED_INVITATION_HOST_TRIES;
2089
+ };
2090
+ let admitted = false;
1934
2091
  const createExtension = () => {
1935
- let connectionCount = 0;
1936
- const extension = new InvitationGuestExtension({
1937
- onOpen: (extensionCtx) => {
1938
- extensionCtx.onDispose(async () => {
1939
- log5("extension disposed", {
1940
- currentState
2092
+ const extension = new InvitationGuestExtension(guardedState.mutex, {
2093
+ onStateUpdate: (newState) => {
2094
+ guardedState.set(extension, newState);
2095
+ },
2096
+ onOpen: (connectionCtx, extensionCtx) => {
2097
+ triedPeersIds.add(extensionCtx.remotePeerId);
2098
+ if (admitted) {
2099
+ extensionCtx.close();
2100
+ return;
2101
+ }
2102
+ connectionCtx.onDispose(async () => {
2103
+ log7("extension disposed", {
2104
+ admitted,
2105
+ currentState: guardedState.current.state
1941
2106
  }, {
1942
- F: __dxlog_file7,
1943
- L: 234,
2107
+ F: __dxlog_file9,
2108
+ L: 233,
1944
2109
  S: this,
1945
2110
  C: (f, a) => f(...a)
1946
2111
  });
1947
2112
  if (!admitted) {
1948
- stream.error(new Error("Remote peer disconnected."));
2113
+ guardedState.error(extension, new ContextDisposedError2());
2114
+ if (shouldCancelInvitationFlow(extension)) {
2115
+ await ctx.dispose();
2116
+ }
1949
2117
  }
1950
2118
  });
1951
- scheduleTask2(ctx, async () => {
1952
- const traceId = PublicKey6.random().toHex();
2119
+ scheduleTask3(connectionCtx, async () => {
2120
+ const traceId = PublicKey7.random().toHex();
1953
2121
  try {
1954
- log5.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.begin({
2122
+ log7.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.begin({
1955
2123
  id: traceId
1956
2124
  }), {
1957
- F: __dxlog_file7,
1958
- L: 243,
2125
+ F: __dxlog_file9,
2126
+ L: 245,
1959
2127
  S: this,
1960
2128
  C: (f, a) => f(...a)
1961
2129
  });
1962
- if (++connectionCount > 1) {
1963
- throw new Error(`multiple connections detected: ${connectionCount}`);
1964
- }
1965
- scheduleTask2(ctx, () => ctx.raise(new TimeoutError(timeout)), timeout);
1966
- log5("connected", {
2130
+ scheduleTask3(connectionCtx, () => {
2131
+ guardedState.set(extension, Invitation4.State.TIMEOUT);
2132
+ extensionCtx.close();
2133
+ }, timeout);
2134
+ log7("connected", {
1967
2135
  ...protocol.toJSON()
1968
2136
  }, {
1969
- F: __dxlog_file7,
1970
- L: 251,
2137
+ F: __dxlog_file9,
2138
+ L: 256,
1971
2139
  S: this,
1972
2140
  C: (f, a) => f(...a)
1973
2141
  });
1974
- setState({
1975
- state: Invitation3.State.CONNECTED
1976
- });
1977
- log5("introduce", {
2142
+ guardedState.set(extension, Invitation4.State.CONNECTED);
2143
+ log7("introduce", {
1978
2144
  ...protocol.toJSON()
1979
2145
  }, {
1980
- F: __dxlog_file7,
1981
- L: 255,
2146
+ F: __dxlog_file9,
2147
+ L: 260,
1982
2148
  S: this,
1983
2149
  C: (f, a) => f(...a)
1984
2150
  });
1985
- const introductionResponse = await extension.rpc.InvitationHostService.introduce(protocol.createIntroduction());
1986
- log5("introduce response", {
2151
+ const introductionResponse = await extension.rpc.InvitationHostService.introduce({
2152
+ invitationId: invitation.invitationId,
2153
+ ...protocol.createIntroduction()
2154
+ });
2155
+ log7("introduce response", {
1987
2156
  ...protocol.toJSON(),
1988
2157
  response: introductionResponse
1989
2158
  }, {
1990
- F: __dxlog_file7,
1991
- L: 259,
2159
+ F: __dxlog_file9,
2160
+ L: 265,
1992
2161
  S: this,
1993
2162
  C: (f, a) => f(...a)
1994
2163
  });
1995
2164
  invitation.authMethod = introductionResponse.authMethod;
1996
2165
  if (isAuthenticationRequired(invitation)) {
1997
2166
  switch (invitation.authMethod) {
1998
- case Invitation3.AuthMethod.SHARED_SECRET:
1999
- await this._handleGuestOtpAuth(extension, setState, authenticated, {
2167
+ case Invitation4.AuthMethod.SHARED_SECRET:
2168
+ await this._handleGuestOtpAuth(extension, (state) => guardedState.set(extension, state), otpEnteredTrigger, {
2000
2169
  timeout
2001
2170
  });
2002
2171
  break;
2003
- case Invitation3.AuthMethod.KNOWN_PUBLIC_KEY:
2004
- await this._handleGuestKpkAuth(extension, setState, invitation, introductionResponse);
2172
+ case Invitation4.AuthMethod.KNOWN_PUBLIC_KEY:
2173
+ await this._handleGuestKpkAuth(extension, (state) => guardedState.set(extension, state), invitation, introductionResponse);
2005
2174
  break;
2006
2175
  }
2007
2176
  }
2008
- log5("request admission", {
2177
+ log7("request admission", {
2009
2178
  ...protocol.toJSON()
2010
2179
  }, {
2011
- F: __dxlog_file7,
2012
- L: 275,
2180
+ F: __dxlog_file9,
2181
+ L: 291,
2013
2182
  S: this,
2014
2183
  C: (f, a) => f(...a)
2015
2184
  });
@@ -2017,158 +2186,225 @@ var InvitationsHandler = class {
2017
2186
  const admissionResponse = await extension.rpc.InvitationHostService.admit(admissionRequest);
2018
2187
  admitted = true;
2019
2188
  const result = await protocol.accept(admissionResponse, admissionRequest);
2020
- log5("admitted by host", {
2189
+ log7("admitted by host", {
2021
2190
  ...protocol.toJSON()
2022
2191
  }, {
2023
- F: __dxlog_file7,
2024
- L: 286,
2192
+ F: __dxlog_file9,
2193
+ L: 302,
2025
2194
  S: this,
2026
2195
  C: (f, a) => f(...a)
2027
2196
  });
2028
- setState({
2197
+ await guardedState.complete({
2198
+ ...guardedState.current,
2029
2199
  ...result,
2030
- target: invitation.target,
2031
- state: Invitation3.State.SUCCESS
2200
+ state: Invitation4.State.SUCCESS
2032
2201
  });
2033
- log5.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.end({
2202
+ log7.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.end({
2034
2203
  id: traceId
2035
2204
  }), {
2036
- F: __dxlog_file7,
2037
- L: 288,
2205
+ F: __dxlog_file9,
2206
+ L: 308,
2038
2207
  S: this,
2039
2208
  C: (f, a) => f(...a)
2040
2209
  });
2041
2210
  } catch (err) {
2042
2211
  if (err instanceof TimeoutError) {
2043
- log5("timeout", {
2212
+ log7("timeout", {
2044
2213
  ...protocol.toJSON()
2045
2214
  }, {
2046
- F: __dxlog_file7,
2047
- L: 291,
2215
+ F: __dxlog_file9,
2216
+ L: 311,
2048
2217
  S: this,
2049
2218
  C: (f, a) => f(...a)
2050
2219
  });
2051
- setState({
2052
- state: Invitation3.State.TIMEOUT
2053
- });
2220
+ guardedState.set(extension, Invitation4.State.TIMEOUT);
2054
2221
  } else {
2055
- log5("auth failed", err, {
2056
- F: __dxlog_file7,
2057
- L: 294,
2222
+ log7("auth failed", err, {
2223
+ F: __dxlog_file9,
2224
+ L: 314,
2058
2225
  S: this,
2059
2226
  C: (f, a) => f(...a)
2060
2227
  });
2061
- stream.error(err);
2228
+ guardedState.error(extension, err);
2062
2229
  }
2063
- log5.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.error({
2230
+ extensionCtx.close(err);
2231
+ log7.trace("dxos.sdk.invitations-handler.guest.onOpen", trace4.error({
2064
2232
  id: traceId,
2065
2233
  error: err
2066
2234
  }), {
2067
- F: __dxlog_file7,
2068
- L: 297,
2235
+ F: __dxlog_file9,
2236
+ L: 318,
2069
2237
  S: this,
2070
2238
  C: (f, a) => f(...a)
2071
2239
  });
2072
- } finally {
2073
- await ctx.dispose();
2074
2240
  }
2075
2241
  });
2076
2242
  },
2077
2243
  onError: (err) => {
2078
- if (err instanceof InvalidInvitationExtensionRoleError2) {
2244
+ if (err instanceof InvalidInvitationExtensionRoleError3) {
2079
2245
  return;
2080
2246
  }
2081
2247
  if (err instanceof TimeoutError) {
2082
- log5("timeout", {
2248
+ log7("timeout", {
2083
2249
  ...protocol.toJSON()
2084
2250
  }, {
2085
- F: __dxlog_file7,
2086
- L: 308,
2251
+ F: __dxlog_file9,
2252
+ L: 327,
2087
2253
  S: this,
2088
2254
  C: (f, a) => f(...a)
2089
2255
  });
2090
- setState({
2091
- state: Invitation3.State.TIMEOUT
2092
- });
2256
+ guardedState.set(extension, Invitation4.State.TIMEOUT);
2093
2257
  } else {
2094
- log5("auth failed", err, {
2095
- F: __dxlog_file7,
2096
- L: 311,
2258
+ log7("auth failed", err, {
2259
+ F: __dxlog_file9,
2260
+ L: 330,
2097
2261
  S: this,
2098
2262
  C: (f, a) => f(...a)
2099
2263
  });
2100
- stream.error(err);
2264
+ guardedState.error(extension, err);
2101
2265
  }
2102
2266
  }
2103
2267
  });
2104
2268
  return extension;
2105
2269
  };
2106
- scheduleTask2(ctx, async () => {
2270
+ scheduleTask3(ctx, async () => {
2107
2271
  const error = protocol.checkInvitation(invitation);
2108
2272
  if (error) {
2109
2273
  stream.error(error);
2274
+ await ctx.dispose();
2110
2275
  } else {
2111
- invariant6(invitation.swarmKey, void 0, {
2112
- F: __dxlog_file7,
2113
- L: 325,
2276
+ invariant8(invitation.swarmKey, void 0, {
2277
+ F: __dxlog_file9,
2278
+ L: 345,
2114
2279
  S: this,
2115
2280
  A: [
2116
2281
  "invitation.swarmKey",
2117
2282
  ""
2118
2283
  ]
2119
2284
  });
2120
- const topic = invitation.swarmKey;
2121
- const swarmConnection = await this._networkManager.joinSwarm({
2122
- topic,
2123
- peerId: PublicKey6.random(),
2124
- protocolProvider: createTeleportProtocolFactory(async (teleport) => {
2125
- teleport.addExtension("dxos.halo.invitations", createExtension());
2126
- }),
2127
- topology: new StarTopology(topic),
2128
- label: "invitation guest"
2129
- });
2130
- ctx.onDispose(() => swarmConnection.close());
2131
- setState({
2132
- state: Invitation3.State.CONNECTING
2133
- });
2285
+ await this._joinSwarm(ctx, invitation, Options4.Role.GUEST, createExtension);
2286
+ guardedState.set(null, Invitation4.State.CONNECTING);
2134
2287
  }
2135
2288
  });
2136
- const observable = new AuthenticatingInvitation({
2137
- initialInvitation: invitation,
2138
- subscriber: stream.observable,
2139
- onCancel: async () => {
2140
- setState({
2141
- state: Invitation3.State.CANCELLED
2142
- });
2143
- await ctx.dispose();
2289
+ }
2290
+ async _joinSwarm(ctx, invitation, role, extensionFactory) {
2291
+ let label;
2292
+ if (role === Options4.Role.GUEST) {
2293
+ label = "invitation guest";
2294
+ } else if (invitation.kind === Invitation4.Kind.DEVICE) {
2295
+ label = "invitation host for device";
2296
+ } else {
2297
+ label = `invitation host for space ${invitation.spaceKey?.truncate()}`;
2298
+ }
2299
+ const swarmConnection = await this._networkManager.joinSwarm({
2300
+ topic: invitation.swarmKey,
2301
+ peerId: PublicKey7.random(),
2302
+ protocolProvider: createTeleportProtocolFactory(async (teleport) => {
2303
+ teleport.addExtension("dxos.halo.invitations", extensionFactory());
2304
+ }, this._defaultTeleportParams),
2305
+ topology: new InvitationTopology(role),
2306
+ label
2307
+ });
2308
+ ctx.onDispose(() => swarmConnection.close());
2309
+ return swarmConnection;
2310
+ }
2311
+ /**
2312
+ * A utility object for serializing invitation state changes by multiple concurrent
2313
+ * invitation flow connections.
2314
+ */
2315
+ _createGuardedState(ctx, invitation, stream) {
2316
+ const mutex = new Mutex();
2317
+ let lastActiveExtension = null;
2318
+ let currentInvitation = {
2319
+ ...invitation
2320
+ };
2321
+ const isStateChangeAllowed = (extension) => {
2322
+ if (ctx.disposed || extension !== null && mutex.isLocked() && !extension.hasFlowLock()) {
2323
+ return false;
2324
+ }
2325
+ return extension == null || lastActiveExtension !== extension || this._isNotTerminal(currentInvitation.state);
2326
+ };
2327
+ return {
2328
+ mutex,
2329
+ get current() {
2330
+ return currentInvitation;
2144
2331
  },
2145
- onAuthenticate: async (code) => {
2146
- authenticated.wake(code);
2332
+ // disposing context prevents any further state updates
2333
+ complete: (newState) => {
2334
+ currentInvitation = {
2335
+ ...currentInvitation,
2336
+ ...newState
2337
+ };
2338
+ stream.next(currentInvitation);
2339
+ return ctx.dispose();
2340
+ },
2341
+ set: (extension, newState) => {
2342
+ if (isStateChangeAllowed(extension)) {
2343
+ this._logStateUpdate(currentInvitation, extension, newState);
2344
+ currentInvitation = {
2345
+ ...currentInvitation,
2346
+ state: newState
2347
+ };
2348
+ stream.next(currentInvitation);
2349
+ lastActiveExtension = extension;
2350
+ return true;
2351
+ }
2352
+ return false;
2353
+ },
2354
+ error: (extension, error) => {
2355
+ if (isStateChangeAllowed(extension)) {
2356
+ this._logStateUpdate(currentInvitation, extension, Invitation4.State.ERROR);
2357
+ currentInvitation = {
2358
+ ...currentInvitation,
2359
+ state: Invitation4.State.ERROR
2360
+ };
2361
+ stream.next(currentInvitation);
2362
+ stream.error(error);
2363
+ lastActiveExtension = extension;
2364
+ return true;
2365
+ }
2366
+ return false;
2147
2367
  }
2368
+ };
2369
+ }
2370
+ _logStateUpdate(invitation, actor, newState) {
2371
+ log7("invitation state update", {
2372
+ actor: actor?.constructor.name,
2373
+ newState: stateToString(newState),
2374
+ oldState: stateToString(invitation.state)
2375
+ }, {
2376
+ F: __dxlog_file9,
2377
+ L: 438,
2378
+ S: this,
2379
+ C: (f, a) => f(...a)
2148
2380
  });
2149
- return observable;
2381
+ }
2382
+ _isNotTerminal(currentState) {
2383
+ return ![
2384
+ Invitation4.State.SUCCESS,
2385
+ Invitation4.State.ERROR,
2386
+ Invitation4.State.CANCELLED,
2387
+ Invitation4.State.TIMEOUT,
2388
+ Invitation4.State.EXPIRED
2389
+ ].includes(currentState);
2150
2390
  }
2151
2391
  async _handleGuestOtpAuth(extension, setState, authenticated, options) {
2152
2392
  for (let attempt = 1; attempt <= MAX_OTP_ATTEMPTS; attempt++) {
2153
- log5("guest waiting for authentication code...", void 0, {
2154
- F: __dxlog_file7,
2155
- L: 365,
2393
+ log7("guest waiting for authentication code...", void 0, {
2394
+ F: __dxlog_file9,
2395
+ L: 462,
2156
2396
  S: this,
2157
2397
  C: (f, a) => f(...a)
2158
2398
  });
2159
- setState({
2160
- state: Invitation3.State.READY_FOR_AUTHENTICATION
2161
- });
2399
+ setState(Invitation4.State.READY_FOR_AUTHENTICATION);
2162
2400
  const authCode = await authenticated.wait(options);
2163
- log5("sending authentication request", void 0, {
2164
- F: __dxlog_file7,
2165
- L: 369,
2401
+ log7("sending authentication request", void 0, {
2402
+ F: __dxlog_file9,
2403
+ L: 466,
2166
2404
  S: this,
2167
2405
  C: (f, a) => f(...a)
2168
2406
  });
2169
- setState({
2170
- state: Invitation3.State.AUTHENTICATING
2171
- });
2407
+ setState(Invitation4.State.AUTHENTICATING);
2172
2408
  const response = await extension.rpc.InvitationHostService.authenticate({
2173
2409
  authCode
2174
2410
  });
@@ -2179,11 +2415,11 @@ var InvitationsHandler = class {
2179
2415
  if (attempt === MAX_OTP_ATTEMPTS) {
2180
2416
  throw new Error(`Maximum retry attempts: ${MAX_OTP_ATTEMPTS}`);
2181
2417
  } else {
2182
- log5("retrying invalid code", {
2418
+ log7("retrying invalid code", {
2183
2419
  attempt
2184
2420
  }, {
2185
- F: __dxlog_file7,
2186
- L: 380,
2421
+ F: __dxlog_file9,
2422
+ L: 477,
2187
2423
  S: this,
2188
2424
  C: (f, a) => f(...a)
2189
2425
  });
@@ -2199,15 +2435,13 @@ var InvitationsHandler = class {
2199
2435
  if (introductionResponse.challenge == null) {
2200
2436
  throw new Error("challenge missing in the introduction");
2201
2437
  }
2202
- log5("sending authentication request", void 0, {
2203
- F: __dxlog_file7,
2204
- L: 399,
2438
+ log7("sending authentication request", void 0, {
2439
+ F: __dxlog_file9,
2440
+ L: 496,
2205
2441
  S: this,
2206
2442
  C: (f, a) => f(...a)
2207
2443
  });
2208
- setState({
2209
- state: Invitation3.State.AUTHENTICATING
2210
- });
2444
+ setState(Invitation4.State.AUTHENTICATING);
2211
2445
  const signature = sign(Buffer.from(introductionResponse.challenge), invitation.guestKeypair.privateKey);
2212
2446
  const response = await extension.rpc.InvitationHostService.authenticate({
2213
2447
  signedChallenge: signature
@@ -2220,7 +2454,7 @@ var InvitationsHandler = class {
2220
2454
  var createAdmissionKeypair = () => {
2221
2455
  const keypair = createKeyPair();
2222
2456
  return {
2223
- publicKey: PublicKey6.from(keypair.publicKey),
2457
+ publicKey: PublicKey7.from(keypair.publicKey),
2224
2458
  privateKey: keypair.secretKey
2225
2459
  };
2226
2460
  };
@@ -2323,14 +2557,14 @@ var InvitationsServiceImpl = class {
2323
2557
  };
2324
2558
 
2325
2559
  // packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts
2326
- import { createAdmissionCredentials, createDelegatedSpaceInvitationCredential, getCredentialAssertion } from "@dxos/credentials";
2560
+ import { createAdmissionCredentials, createCancelDelegatedSpaceInvitationCredential, createDelegatedSpaceInvitationCredential, getCredentialAssertion } from "@dxos/credentials";
2327
2561
  import { writeMessages as writeMessages2 } from "@dxos/feed-store";
2328
- import { invariant as invariant7 } from "@dxos/invariant";
2329
- import { log as log6 } from "@dxos/log";
2562
+ import { invariant as invariant9 } from "@dxos/invariant";
2563
+ import { log as log8 } from "@dxos/log";
2330
2564
  import { AlreadyJoinedError as AlreadyJoinedError2 } from "@dxos/protocols";
2331
- import { Invitation as Invitation4 } from "@dxos/protocols/proto/dxos/client/services";
2565
+ import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
2332
2566
  import { SpaceMember } from "@dxos/protocols/proto/dxos/halo/credentials";
2333
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts";
2567
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts";
2334
2568
  var SpaceInvitationProtocol = class {
2335
2569
  constructor(_spaceManager, _signingContext, _keyring, _spaceKey) {
2336
2570
  this._spaceManager = _spaceManager;
@@ -2346,33 +2580,33 @@ var SpaceInvitationProtocol = class {
2346
2580
  }
2347
2581
  getInvitationContext() {
2348
2582
  return {
2349
- kind: Invitation4.Kind.SPACE,
2583
+ kind: Invitation5.Kind.SPACE,
2350
2584
  spaceKey: this._spaceKey
2351
2585
  };
2352
2586
  }
2353
2587
  async admit(invitation, request, guestProfile) {
2354
- invariant7(this._spaceKey, void 0, {
2355
- F: __dxlog_file8,
2356
- L: 55,
2588
+ invariant9(this._spaceKey, void 0, {
2589
+ F: __dxlog_file10,
2590
+ L: 56,
2357
2591
  S: this,
2358
2592
  A: [
2359
2593
  "this._spaceKey",
2360
2594
  ""
2361
2595
  ]
2362
2596
  });
2363
- const space = await this._spaceManager.spaces.get(this._spaceKey);
2364
- invariant7(space, void 0, {
2365
- F: __dxlog_file8,
2366
- L: 57,
2597
+ const space = this._spaceManager.spaces.get(this._spaceKey);
2598
+ invariant9(space, void 0, {
2599
+ F: __dxlog_file10,
2600
+ L: 58,
2367
2601
  S: this,
2368
2602
  A: [
2369
2603
  "space",
2370
2604
  ""
2371
2605
  ]
2372
2606
  });
2373
- invariant7(request.space, void 0, {
2374
- F: __dxlog_file8,
2375
- L: 59,
2607
+ invariant9(request.space, void 0, {
2608
+ F: __dxlog_file10,
2609
+ L: 60,
2376
2610
  S: this,
2377
2611
  A: [
2378
2612
  "request.space",
@@ -2380,19 +2614,19 @@ var SpaceInvitationProtocol = class {
2380
2614
  ]
2381
2615
  });
2382
2616
  const { identityKey, deviceKey } = request.space;
2383
- log6("writing guest credentials", {
2617
+ log8("writing guest credentials", {
2384
2618
  host: this._signingContext.deviceKey,
2385
2619
  guest: deviceKey
2386
2620
  }, {
2387
- F: __dxlog_file8,
2388
- L: 62,
2621
+ F: __dxlog_file10,
2622
+ L: 63,
2389
2623
  S: this,
2390
2624
  C: (f, a) => f(...a)
2391
2625
  });
2392
- const credentials = await createAdmissionCredentials(this._signingContext.credentialSigner, identityKey, space.key, space.inner.genesisFeedKey, guestProfile, invitation.delegationCredentialId);
2393
- invariant7(credentials[0].credential, void 0, {
2394
- F: __dxlog_file8,
2395
- L: 74,
2626
+ const credentials = await createAdmissionCredentials(this._signingContext.credentialSigner, identityKey, space.key, space.inner.genesisFeedKey, invitation.role ?? SpaceMember.Role.ADMIN, space.inner.spaceState.membershipChainHeads, guestProfile, invitation.delegationCredentialId);
2627
+ invariant9(credentials[0].credential, void 0, {
2628
+ F: __dxlog_file10,
2629
+ L: 77,
2396
2630
  S: this,
2397
2631
  A: [
2398
2632
  "credentials[0].credential",
@@ -2400,9 +2634,9 @@ var SpaceInvitationProtocol = class {
2400
2634
  ]
2401
2635
  });
2402
2636
  const spaceMemberCredential = credentials[0].credential.credential;
2403
- invariant7(getCredentialAssertion(spaceMemberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
2404
- F: __dxlog_file8,
2405
- L: 76,
2637
+ invariant9(getCredentialAssertion(spaceMemberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
2638
+ F: __dxlog_file10,
2639
+ L: 79,
2406
2640
  S: this,
2407
2641
  A: [
2408
2642
  "getCredentialAssertion(spaceMemberCredential)['@type'] === 'dxos.halo.credentials.SpaceMember'",
@@ -2418,29 +2652,29 @@ var SpaceInvitationProtocol = class {
2418
2652
  };
2419
2653
  }
2420
2654
  async delegate(invitation) {
2421
- invariant7(this._spaceKey, void 0, {
2422
- F: __dxlog_file8,
2423
- L: 89,
2655
+ invariant9(this._spaceKey, void 0, {
2656
+ F: __dxlog_file10,
2657
+ L: 92,
2424
2658
  S: this,
2425
2659
  A: [
2426
2660
  "this._spaceKey",
2427
2661
  ""
2428
2662
  ]
2429
2663
  });
2430
- const space = await this._spaceManager.spaces.get(this._spaceKey);
2431
- invariant7(space, void 0, {
2432
- F: __dxlog_file8,
2433
- L: 91,
2664
+ const space = this._spaceManager.spaces.get(this._spaceKey);
2665
+ invariant9(space, void 0, {
2666
+ F: __dxlog_file10,
2667
+ L: 94,
2434
2668
  S: this,
2435
2669
  A: [
2436
2670
  "space",
2437
2671
  ""
2438
2672
  ]
2439
2673
  });
2440
- if (invitation.authMethod === Invitation4.AuthMethod.KNOWN_PUBLIC_KEY) {
2441
- invariant7(invitation.guestKeypair?.publicKey, void 0, {
2442
- F: __dxlog_file8,
2443
- L: 93,
2674
+ if (invitation.authMethod === Invitation5.AuthMethod.KNOWN_PUBLIC_KEY) {
2675
+ invariant9(invitation.guestKeypair?.publicKey, void 0, {
2676
+ F: __dxlog_file10,
2677
+ L: 96,
2444
2678
  S: this,
2445
2679
  A: [
2446
2680
  "invitation.guestKeypair?.publicKey",
@@ -2448,12 +2682,12 @@ var SpaceInvitationProtocol = class {
2448
2682
  ]
2449
2683
  });
2450
2684
  }
2451
- log6("writing delegate space invitation", {
2685
+ log8("writing delegate space invitation", {
2452
2686
  host: this._signingContext.deviceKey,
2453
2687
  id: invitation.invitationId
2454
2688
  }, {
2455
- F: __dxlog_file8,
2456
- L: 96,
2689
+ F: __dxlog_file10,
2690
+ L: 99,
2457
2691
  S: this,
2458
2692
  C: (f, a) => f(...a)
2459
2693
  });
@@ -2461,14 +2695,14 @@ var SpaceInvitationProtocol = class {
2461
2695
  invitationId: invitation.invitationId,
2462
2696
  authMethod: invitation.authMethod,
2463
2697
  swarmKey: invitation.swarmKey,
2464
- role: SpaceMember.Role.ADMIN,
2698
+ role: invitation.role ?? SpaceMember.Role.ADMIN,
2465
2699
  expiresOn: invitation.lifetime ? new Date((invitation.created?.getTime() ?? Date.now()) + invitation.lifetime) : void 0,
2466
2700
  multiUse: invitation.multiUse ?? false,
2467
- guestKey: invitation.authMethod === Invitation4.AuthMethod.KNOWN_PUBLIC_KEY ? invitation.guestKeypair.publicKey : void 0
2701
+ guestKey: invitation.authMethod === Invitation5.AuthMethod.KNOWN_PUBLIC_KEY ? invitation.guestKeypair.publicKey : void 0
2468
2702
  });
2469
- invariant7(credential.credential, void 0, {
2470
- F: __dxlog_file8,
2471
- L: 116,
2703
+ invariant9(credential.credential, void 0, {
2704
+ F: __dxlog_file10,
2705
+ L: 119,
2472
2706
  S: this,
2473
2707
  A: [
2474
2708
  "credential.credential",
@@ -2480,6 +2714,58 @@ var SpaceInvitationProtocol = class {
2480
2714
  ]);
2481
2715
  return credential.credential.credential.id;
2482
2716
  }
2717
+ async cancelDelegation(invitation) {
2718
+ invariant9(this._spaceKey, void 0, {
2719
+ F: __dxlog_file10,
2720
+ L: 125,
2721
+ S: this,
2722
+ A: [
2723
+ "this._spaceKey",
2724
+ ""
2725
+ ]
2726
+ });
2727
+ invariant9(invitation.type === Invitation5.Type.DELEGATED && invitation.delegationCredentialId, void 0, {
2728
+ F: __dxlog_file10,
2729
+ L: 126,
2730
+ S: this,
2731
+ A: [
2732
+ "invitation.type === Invitation.Type.DELEGATED && invitation.delegationCredentialId",
2733
+ ""
2734
+ ]
2735
+ });
2736
+ const space = this._spaceManager.spaces.get(this._spaceKey);
2737
+ invariant9(space, void 0, {
2738
+ F: __dxlog_file10,
2739
+ L: 128,
2740
+ S: this,
2741
+ A: [
2742
+ "space",
2743
+ ""
2744
+ ]
2745
+ });
2746
+ log8("cancelling delegated space invitation", {
2747
+ host: this._signingContext.deviceKey,
2748
+ id: invitation.invitationId
2749
+ }, {
2750
+ F: __dxlog_file10,
2751
+ L: 130,
2752
+ S: this,
2753
+ C: (f, a) => f(...a)
2754
+ });
2755
+ const credential = await createCancelDelegatedSpaceInvitationCredential(this._signingContext.credentialSigner, space.key, invitation.delegationCredentialId);
2756
+ invariant9(credential.credential, void 0, {
2757
+ F: __dxlog_file10,
2758
+ L: 137,
2759
+ S: this,
2760
+ A: [
2761
+ "credential.credential",
2762
+ ""
2763
+ ]
2764
+ });
2765
+ await writeMessages2(space.inner.controlPipeline.writer, [
2766
+ credential
2767
+ ]);
2768
+ }
2483
2769
  checkInvitation(invitation) {
2484
2770
  if (invitation.spaceKey && this._spaceManager.spaces.has(invitation.spaceKey)) {
2485
2771
  return new AlreadyJoinedError2("Already joined space.");
@@ -2503,9 +2789,9 @@ var SpaceInvitationProtocol = class {
2503
2789
  };
2504
2790
  }
2505
2791
  async accept(response) {
2506
- invariant7(response.space, void 0, {
2507
- F: __dxlog_file8,
2508
- L: 149,
2792
+ invariant9(response.space, void 0, {
2793
+ F: __dxlog_file10,
2794
+ L: 169,
2509
2795
  S: this,
2510
2796
  A: [
2511
2797
  "response.space",
@@ -2514,18 +2800,18 @@ var SpaceInvitationProtocol = class {
2514
2800
  });
2515
2801
  const { credential, controlTimeframe, dataTimeframe } = response.space;
2516
2802
  const assertion = getCredentialAssertion(credential);
2517
- invariant7(assertion["@type"] === "dxos.halo.credentials.SpaceMember", "Invalid credential", {
2518
- F: __dxlog_file8,
2519
- L: 152,
2803
+ invariant9(assertion["@type"] === "dxos.halo.credentials.SpaceMember", "Invalid credential", {
2804
+ F: __dxlog_file10,
2805
+ L: 172,
2520
2806
  S: this,
2521
2807
  A: [
2522
2808
  "assertion['@type'] === 'dxos.halo.credentials.SpaceMember'",
2523
2809
  "'Invalid credential'"
2524
2810
  ]
2525
2811
  });
2526
- invariant7(credential.subject.id.equals(this._signingContext.identityKey), void 0, {
2527
- F: __dxlog_file8,
2528
- L: 153,
2812
+ invariant9(credential.subject.id.equals(this._signingContext.identityKey), void 0, {
2813
+ F: __dxlog_file10,
2814
+ L: 173,
2529
2815
  S: this,
2530
2816
  A: [
2531
2817
  "credential.subject.id.equals(this._signingContext.identityKey)",
@@ -2549,16 +2835,17 @@ var SpaceInvitationProtocol = class {
2549
2835
  };
2550
2836
 
2551
2837
  // packages/sdk/client-services/src/packlets/invitations/invitations-manager.ts
2552
- import { Event as Event3, PushStream as PushStream2 } from "@dxos/async";
2553
- import { AUTHENTICATION_CODE_LENGTH, CancellableInvitation, INVITATION_TIMEOUT as INVITATION_TIMEOUT2 } from "@dxos/client-protocol";
2838
+ import { Event as Event3, PushStream, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
2839
+ import { AuthenticatingInvitation, AUTHENTICATION_CODE_LENGTH, CancellableInvitation, INVITATION_TIMEOUT as INVITATION_TIMEOUT2 } from "@dxos/client-protocol";
2554
2840
  import { Context as Context6 } from "@dxos/context";
2555
2841
  import { generatePasscode } from "@dxos/credentials";
2556
2842
  import { hasInvitationExpired } from "@dxos/echo-pipeline";
2557
- import { invariant as invariant8 } from "@dxos/invariant";
2558
- import { PublicKey as PublicKey7 } from "@dxos/keys";
2559
- import { log as log7 } from "@dxos/log";
2560
- import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
2561
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-manager.ts";
2843
+ import { invariant as invariant10 } from "@dxos/invariant";
2844
+ import { PublicKey as PublicKey8 } from "@dxos/keys";
2845
+ import { log as log9 } from "@dxos/log";
2846
+ import { Invitation as Invitation6 } from "@dxos/protocols/proto/dxos/client/services";
2847
+ import { SpaceMember as SpaceMember2 } from "@dxos/protocols/proto/dxos/halo/credentials";
2848
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-manager.ts";
2562
2849
  var InvitationsManager = class {
2563
2850
  constructor(_invitationsHandler, _getHandler, _metadataStore) {
2564
2851
  this._invitationsHandler = _invitationsHandler;
@@ -2596,9 +2883,9 @@ var InvitationsManager = class {
2596
2883
  try {
2597
2884
  await this._persistIfRequired(handler, stream, invitation);
2598
2885
  } catch (err) {
2599
- log7.catch(err, void 0, {
2600
- F: __dxlog_file9,
2601
- L: 76,
2886
+ log9.catch(err, void 0, {
2887
+ F: __dxlog_file11,
2888
+ L: 77,
2602
2889
  S: this,
2603
2890
  C: (f, a) => f(...a)
2604
2891
  });
@@ -2619,9 +2906,9 @@ var InvitationsManager = class {
2619
2906
  const persistentInvitations = this._metadataStore.getInvitations();
2620
2907
  const freshInvitations = persistentInvitations.filter((invitation) => !hasInvitationExpired(invitation));
2621
2908
  const loadTasks = freshInvitations.map((persistentInvitation) => {
2622
- invariant8(!this._createInvitations.get(persistentInvitation.invitationId), "invitation already exists", {
2623
- F: __dxlog_file9,
2624
- L: 97,
2909
+ invariant10(!this._createInvitations.get(persistentInvitation.invitationId), "invitation already exists", {
2910
+ F: __dxlog_file11,
2911
+ L: 98,
2625
2912
  S: this,
2626
2913
  A: [
2627
2914
  "!this._createInvitations.get(persistentInvitation.invitationId)",
@@ -2638,9 +2925,9 @@ var InvitationsManager = class {
2638
2925
  invitations: cInvitations.map((invitation) => invitation.get())
2639
2926
  };
2640
2927
  } catch (err) {
2641
- log7.catch(err, void 0, {
2642
- F: __dxlog_file9,
2643
- L: 104,
2928
+ log9.catch(err, void 0, {
2929
+ F: __dxlog_file11,
2930
+ L: 105,
2644
2931
  S: this,
2645
2932
  C: (f, a) => f(...a)
2646
2933
  });
@@ -2659,7 +2946,8 @@ var InvitationsManager = class {
2659
2946
  return existingInvitation;
2660
2947
  }
2661
2948
  const handler = this._getHandler(options);
2662
- const invitation = this._invitationsHandler.acceptInvitation(handler, options, request.deviceProfile);
2949
+ const { ctx, invitation, stream, otpEnteredTrigger } = this._createObservableAcceptingInvitation(handler, options);
2950
+ this._invitationsHandler.acceptInvitation(ctx, stream, handler, options, otpEnteredTrigger, request.deviceProfile);
2663
2951
  this._acceptInvitations.set(invitation.get().invitationId, invitation);
2664
2952
  this.invitationAccepted.emit(invitation.get());
2665
2953
  this._onInvitationComplete(invitation, () => {
@@ -2669,15 +2957,15 @@ var InvitationsManager = class {
2669
2957
  return invitation;
2670
2958
  }
2671
2959
  async authenticate({ invitationId, authCode }) {
2672
- log7("authenticating...", void 0, {
2673
- F: __dxlog_file9,
2674
- L: 133,
2960
+ log9("authenticating...", void 0, {
2961
+ F: __dxlog_file11,
2962
+ L: 135,
2675
2963
  S: this,
2676
2964
  C: (f, a) => f(...a)
2677
2965
  });
2678
- invariant8(invitationId, void 0, {
2679
- F: __dxlog_file9,
2680
- L: 134,
2966
+ invariant10(invitationId, void 0, {
2967
+ F: __dxlog_file11,
2968
+ L: 136,
2681
2969
  S: this,
2682
2970
  A: [
2683
2971
  "invitationId",
@@ -2686,11 +2974,11 @@ var InvitationsManager = class {
2686
2974
  });
2687
2975
  const observable = this._acceptInvitations.get(invitationId);
2688
2976
  if (!observable) {
2689
- log7.warn("invalid invitation", {
2977
+ log9.warn("invalid invitation", {
2690
2978
  invitationId
2691
2979
  }, {
2692
- F: __dxlog_file9,
2693
- L: 137,
2980
+ F: __dxlog_file11,
2981
+ L: 139,
2694
2982
  S: this,
2695
2983
  C: (f, a) => f(...a)
2696
2984
  });
@@ -2699,17 +2987,17 @@ var InvitationsManager = class {
2699
2987
  }
2700
2988
  }
2701
2989
  async cancelInvitation({ invitationId }) {
2702
- log7("cancelInvitation...", {
2990
+ log9("cancelInvitation...", {
2703
2991
  invitationId
2704
2992
  }, {
2705
- F: __dxlog_file9,
2706
- L: 144,
2993
+ F: __dxlog_file11,
2994
+ L: 146,
2707
2995
  S: this,
2708
2996
  C: (f, a) => f(...a)
2709
- });
2710
- invariant8(invitationId, void 0, {
2711
- F: __dxlog_file9,
2712
- L: 145,
2997
+ });
2998
+ invariant10(invitationId, void 0, {
2999
+ F: __dxlog_file11,
3000
+ L: 147,
2713
3001
  S: this,
2714
3002
  A: [
2715
3003
  "invitationId",
@@ -2721,6 +3009,10 @@ var InvitationsManager = class {
2721
3009
  if (created.get().persistent) {
2722
3010
  await this._metadataStore.removeInvitation(invitationId);
2723
3011
  }
3012
+ if (created.get().type === Invitation6.Type.DELEGATED) {
3013
+ const handler = this._getHandler(created.get());
3014
+ await handler.cancelDelegation(created.get());
3015
+ }
2724
3016
  await created.cancel();
2725
3017
  this._createInvitations.delete(invitationId);
2726
3018
  this.removedCreated.emit(created.get());
@@ -2751,8 +3043,8 @@ var InvitationsManager = class {
2751
3043
  }
2752
3044
  }
2753
3045
  _createInvitation(protocol, options) {
2754
- const { invitationId = PublicKey7.random().toHex(), type = Invitation5.Type.INTERACTIVE, authMethod = Invitation5.AuthMethod.SHARED_SECRET, state = Invitation5.State.INIT, timeout = INVITATION_TIMEOUT2, swarmKey = PublicKey7.random(), persistent = options?.authMethod !== Invitation5.AuthMethod.KNOWN_PUBLIC_KEY, created = /* @__PURE__ */ new Date(), guestKeypair = void 0, lifetime = 86400, multiUse = false } = options ?? {};
2755
- const authCode = options?.authCode ?? (authMethod === Invitation5.AuthMethod.SHARED_SECRET ? generatePasscode(AUTHENTICATION_CODE_LENGTH) : void 0);
3046
+ const { invitationId = PublicKey8.random().toHex(), type = Invitation6.Type.INTERACTIVE, authMethod = Invitation6.AuthMethod.SHARED_SECRET, state = Invitation6.State.INIT, timeout = INVITATION_TIMEOUT2, swarmKey = PublicKey8.random(), persistent = options?.authMethod !== Invitation6.AuthMethod.KNOWN_PUBLIC_KEY, created = /* @__PURE__ */ new Date(), guestKeypair = void 0, role = SpaceMember2.Role.ADMIN, lifetime = 86400, multiUse = false } = options ?? {};
3047
+ const authCode = options?.authCode ?? (authMethod === Invitation6.AuthMethod.SHARED_SECRET ? generatePasscode(AUTHENTICATION_CODE_LENGTH) : void 0);
2756
3048
  return {
2757
3049
  invitationId,
2758
3050
  type,
@@ -2761,17 +3053,18 @@ var InvitationsManager = class {
2761
3053
  swarmKey,
2762
3054
  authCode,
2763
3055
  timeout,
2764
- persistent: persistent && type !== Invitation5.Type.DELEGATED,
2765
- guestKeypair: guestKeypair ?? (authMethod === Invitation5.AuthMethod.KNOWN_PUBLIC_KEY ? createAdmissionKeypair() : void 0),
3056
+ persistent: persistent && type !== Invitation6.Type.DELEGATED,
3057
+ guestKeypair: guestKeypair ?? (authMethod === Invitation6.AuthMethod.KNOWN_PUBLIC_KEY ? createAdmissionKeypair() : void 0),
2766
3058
  created,
2767
3059
  lifetime,
3060
+ role,
2768
3061
  multiUse,
2769
3062
  delegationCredentialId: options?.delegationCredentialId,
2770
3063
  ...protocol.getInvitationContext()
2771
3064
  };
2772
3065
  }
2773
3066
  _createObservableInvitation(handler, invitation) {
2774
- const stream = new PushStream2();
3067
+ const stream = new PushStream();
2775
3068
  const ctx = new Context6({
2776
3069
  onError: (err) => {
2777
3070
  stream.error(err);
@@ -2779,11 +3072,11 @@ var InvitationsManager = class {
2779
3072
  }
2780
3073
  });
2781
3074
  ctx.onDispose(() => {
2782
- log7("complete", {
3075
+ log9("complete", {
2783
3076
  ...handler.toJSON()
2784
3077
  }, {
2785
- F: __dxlog_file9,
2786
- L: 228,
3078
+ F: __dxlog_file11,
3079
+ L: 236,
2787
3080
  S: this,
2788
3081
  C: (f, a) => f(...a)
2789
3082
  });
@@ -2795,7 +3088,7 @@ var InvitationsManager = class {
2795
3088
  onCancel: async () => {
2796
3089
  stream.next({
2797
3090
  ...invitation,
2798
- state: Invitation5.State.CANCELLED
3091
+ state: Invitation6.State.CANCELLED
2799
3092
  });
2800
3093
  await ctx.dispose();
2801
3094
  }
@@ -2806,8 +3099,73 @@ var InvitationsManager = class {
2806
3099
  observableInvitation
2807
3100
  };
2808
3101
  }
3102
+ _createObservableAcceptingInvitation(handler, initialState) {
3103
+ const otpEnteredTrigger = new Trigger4();
3104
+ const stream = new PushStream();
3105
+ const ctx = new Context6({
3106
+ onError: (err) => {
3107
+ if (err instanceof TimeoutError2) {
3108
+ log9("timeout", {
3109
+ ...handler.toJSON()
3110
+ }, {
3111
+ F: __dxlog_file11,
3112
+ L: 256,
3113
+ S: this,
3114
+ C: (f, a) => f(...a)
3115
+ });
3116
+ stream.next({
3117
+ ...initialState,
3118
+ state: Invitation6.State.TIMEOUT
3119
+ });
3120
+ } else {
3121
+ log9.warn("auth failed", err, {
3122
+ F: __dxlog_file11,
3123
+ L: 259,
3124
+ S: this,
3125
+ C: (f, a) => f(...a)
3126
+ });
3127
+ stream.next({
3128
+ ...initialState,
3129
+ state: Invitation6.State.ERROR
3130
+ });
3131
+ }
3132
+ void ctx.dispose();
3133
+ }
3134
+ });
3135
+ ctx.onDispose(() => {
3136
+ log9("complete", {
3137
+ ...handler.toJSON()
3138
+ }, {
3139
+ F: __dxlog_file11,
3140
+ L: 266,
3141
+ S: this,
3142
+ C: (f, a) => f(...a)
3143
+ });
3144
+ stream.complete();
3145
+ });
3146
+ const invitation = new AuthenticatingInvitation({
3147
+ initialInvitation: initialState,
3148
+ subscriber: stream.observable,
3149
+ onCancel: async () => {
3150
+ stream.next({
3151
+ ...initialState,
3152
+ state: Invitation6.State.CANCELLED
3153
+ });
3154
+ await ctx.dispose();
3155
+ },
3156
+ onAuthenticate: async (code) => {
3157
+ otpEnteredTrigger.wake(code);
3158
+ }
3159
+ });
3160
+ return {
3161
+ ctx,
3162
+ invitation,
3163
+ stream,
3164
+ otpEnteredTrigger
3165
+ };
3166
+ }
2809
3167
  async _persistIfRequired(handler, changeStream, invitation) {
2810
- if (invitation.type === Invitation5.Type.DELEGATED && invitation.delegationCredentialId == null) {
3168
+ if (invitation.type === Invitation6.Type.DELEGATED && invitation.delegationCredentialId == null) {
2811
3169
  const delegationCredentialId = await handler.delegate(invitation);
2812
3170
  changeStream.next({
2813
3171
  ...invitation,
@@ -2822,9 +3180,9 @@ var InvitationsManager = class {
2822
3180
  try {
2823
3181
  await this._metadataStore.removeInvitation(invitation.invitationId);
2824
3182
  } catch (err) {
2825
- log7.catch(err, void 0, {
2826
- F: __dxlog_file9,
2827
- L: 260,
3183
+ log9.catch(err, void 0, {
3184
+ F: __dxlog_file11,
3185
+ L: 302,
2828
3186
  S: this,
2829
3187
  C: (f, a) => f(...a)
2830
3188
  });
@@ -2919,22 +3277,23 @@ ClientRpcServer = _ts_decorate3([
2919
3277
  ], ClientRpcServer);
2920
3278
 
2921
3279
  // packages/sdk/client-services/src/packlets/spaces/data-space.ts
2922
- import { Event as Event6, asyncTimeout, scheduleTask as scheduleTask4, sleep as sleep2, synchronized, trackLeaks } from "@dxos/async";
3280
+ import { Event as Event6, asyncTimeout, scheduleTask as scheduleTask5, sleep as sleep2, synchronized, trackLeaks } from "@dxos/async";
2923
3281
  import { AUTH_TIMEOUT as AUTH_TIMEOUT2 } from "@dxos/client-protocol";
2924
- import { cancelWithContext as cancelWithContext2, Context as Context8, ContextDisposedError } from "@dxos/context";
3282
+ import { cancelWithContext as cancelWithContext4, Context as Context8, ContextDisposedError as ContextDisposedError3 } from "@dxos/context";
2925
3283
  import { timed, warnAfterTimeout } from "@dxos/debug";
2926
3284
  import { createMappedFeedWriter } from "@dxos/echo-pipeline";
2927
3285
  import { AutomergeDocumentLoaderImpl } from "@dxos/echo-pipeline";
2928
3286
  import { TYPE_PROPERTIES } from "@dxos/echo-schema";
2929
- import { failedInvariant, invariant as invariant10 } from "@dxos/invariant";
2930
- import { PublicKey as PublicKey9 } from "@dxos/keys";
2931
- import { log as log9 } from "@dxos/log";
3287
+ import { failedInvariant, invariant as invariant12 } from "@dxos/invariant";
3288
+ import { PublicKey as PublicKey10 } from "@dxos/keys";
3289
+ import { log as log11 } from "@dxos/log";
2932
3290
  import { CancelledError, SystemError } from "@dxos/protocols";
2933
3291
  import { SpaceState, CreateEpochRequest } from "@dxos/protocols/proto/dxos/client/services";
3292
+ import { SpaceMember as SpaceMember3 } from "@dxos/protocols/proto/dxos/halo/credentials";
2934
3293
  import { AdmittedFeed as AdmittedFeed3 } from "@dxos/protocols/proto/dxos/halo/credentials";
2935
3294
  import { Timeframe as Timeframe2 } from "@dxos/timeframe";
2936
3295
  import { trace as trace6 } from "@dxos/tracing";
2937
- import { ComplexSet as ComplexSet3, assignDeep } from "@dxos/util";
3296
+ import { ComplexSet as ComplexSet5, assignDeep } from "@dxos/util";
2938
3297
 
2939
3298
  // packages/sdk/client-services/src/packlets/spaces/automerge-space-state.ts
2940
3299
  import { Event as Event4 } from "@dxos/async";
@@ -2975,15 +3334,15 @@ var AutomergeSpaceState = class {
2975
3334
  };
2976
3335
 
2977
3336
  // packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts
2978
- import { DeferredTask, Event as Event5, scheduleTask as scheduleTask3, sleep, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
3337
+ import { DeferredTask, Event as Event5, scheduleTask as scheduleTask4, sleep, TimeoutError as TimeoutError3, Trigger as Trigger5 } from "@dxos/async";
2979
3338
  import { Context as Context7, rejectOnDispose } from "@dxos/context";
2980
- import { invariant as invariant9 } from "@dxos/invariant";
2981
- import { PublicKey as PublicKey8 } from "@dxos/keys";
2982
- import { log as log8 } from "@dxos/log";
2983
- import { schema as schema3 } from "@dxos/protocols";
2984
- import { RpcExtension as RpcExtension2 } from "@dxos/teleport";
2985
- import { ComplexMap as ComplexMap2, ComplexSet as ComplexSet2, entry } from "@dxos/util";
2986
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts";
3339
+ import { invariant as invariant11 } from "@dxos/invariant";
3340
+ import { PublicKey as PublicKey9 } from "@dxos/keys";
3341
+ import { log as log10 } from "@dxos/log";
3342
+ import { schema as schema4 } from "@dxos/protocols";
3343
+ import { RpcExtension as RpcExtension3 } from "@dxos/teleport";
3344
+ import { ComplexMap as ComplexMap2, ComplexSet as ComplexSet4, entry } from "@dxos/util";
3345
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts";
2987
3346
  var DEFAULT_RETRY_TIMEOUT = 1e3;
2988
3347
  var DEFAULT_SUCCESS_DELAY = 1e3;
2989
3348
  var DEFAULT_NOTARIZE_TIMEOUT = 1e4;
@@ -2993,8 +3352,8 @@ var NotarizationPlugin = class {
2993
3352
  this._ctx = new Context7();
2994
3353
  this._extensionOpened = new Event5();
2995
3354
  this._extensions = /* @__PURE__ */ new Set();
2996
- this._processedCredentials = new ComplexSet2(PublicKey8.hash);
2997
- this._processCredentialsTriggers = new ComplexMap2(PublicKey8.hash);
3355
+ this._processedCredentials = new ComplexSet4(PublicKey9.hash);
3356
+ this._processCredentialsTriggers = new ComplexMap2(PublicKey9.hash);
2998
3357
  }
2999
3358
  get hasWriter() {
3000
3359
  return !!this._writer;
@@ -3008,16 +3367,16 @@ var NotarizationPlugin = class {
3008
3367
  * Request credentials to be notarized.
3009
3368
  */
3010
3369
  async notarize({ ctx: opCtx, credentials, timeout = DEFAULT_NOTARIZE_TIMEOUT, retryTimeout = DEFAULT_RETRY_TIMEOUT, successDelay = DEFAULT_SUCCESS_DELAY }) {
3011
- log8("notarize", {
3370
+ log10("notarize", {
3012
3371
  credentials
3013
3372
  }, {
3014
- F: __dxlog_file10,
3373
+ F: __dxlog_file12,
3015
3374
  L: 90,
3016
3375
  S: this,
3017
3376
  C: (f, a) => f(...a)
3018
3377
  });
3019
- invariant9(credentials.every((credential) => credential.id), "Credentials must have an id", {
3020
- F: __dxlog_file10,
3378
+ invariant11(credentials.every((credential) => credential.id), "Credentials must have an id", {
3379
+ F: __dxlog_file12,
3021
3380
  L: 91,
3022
3381
  S: this,
3023
3382
  A: [
@@ -3025,13 +3384,13 @@ var NotarizationPlugin = class {
3025
3384
  "'Credentials must have an id'"
3026
3385
  ]
3027
3386
  });
3028
- const errors = new Trigger4();
3387
+ const errors = new Trigger5();
3029
3388
  const ctx = this._ctx.derive({
3030
3389
  onError: (err) => {
3031
- log8.warn("Notarization error", {
3390
+ log10.warn("Notarization error", {
3032
3391
  err
3033
3392
  }, {
3034
- F: __dxlog_file10,
3393
+ F: __dxlog_file12,
3035
3394
  L: 99,
3036
3395
  S: this,
3037
3396
  C: (f, a) => f(...a)
@@ -3042,18 +3401,18 @@ var NotarizationPlugin = class {
3042
3401
  });
3043
3402
  opCtx?.onDispose(() => ctx.dispose());
3044
3403
  if (timeout !== 0) {
3045
- scheduleTask3(ctx, () => {
3046
- log8.warn("Notarization timeout", {
3404
+ scheduleTask4(ctx, () => {
3405
+ log10.warn("Notarization timeout", {
3047
3406
  timeout,
3048
3407
  peers: Array.from(this._extensions).map((extension) => extension.remotePeerId)
3049
3408
  }, {
3050
- F: __dxlog_file10,
3409
+ F: __dxlog_file12,
3051
3410
  L: 111,
3052
3411
  S: this,
3053
3412
  C: (f, a) => f(...a)
3054
3413
  });
3055
3414
  void ctx.dispose();
3056
- errors.throw(new TimeoutError2(timeout, "Notarization timed out"));
3415
+ errors.throw(new TimeoutError3(timeout, "Notarization timed out"));
3057
3416
  }, timeout);
3058
3417
  }
3059
3418
  const allNotarized = Promise.all(credentials.map((credential) => this._waitUntilProcessed(credential.id)));
@@ -3067,24 +3426,24 @@ var NotarizationPlugin = class {
3067
3426
  ...this._extensions
3068
3427
  ].find((peer2) => !peersTried.has(peer2));
3069
3428
  if (!peer) {
3070
- log8.info("Exhausted all peers to notarize with", {
3429
+ log10.info("Exhausted all peers to notarize with", {
3071
3430
  retryIn: retryTimeout
3072
3431
  }, {
3073
- F: __dxlog_file10,
3432
+ F: __dxlog_file12,
3074
3433
  L: 136,
3075
3434
  S: this,
3076
3435
  C: (f, a) => f(...a)
3077
3436
  });
3078
3437
  peersTried.clear();
3079
- scheduleTask3(ctx, () => notarizeTask.schedule(), retryTimeout);
3438
+ scheduleTask4(ctx, () => notarizeTask.schedule(), retryTimeout);
3080
3439
  return;
3081
3440
  }
3082
3441
  peersTried.add(peer);
3083
- log8("try notarizing", {
3442
+ log10("try notarizing", {
3084
3443
  peer: peer.localPeerId,
3085
3444
  credentialId: credentials.map((credential) => credential.id)
3086
3445
  }, {
3087
- F: __dxlog_file10,
3446
+ F: __dxlog_file12,
3088
3447
  L: 143,
3089
3448
  S: this,
3090
3449
  C: (f, a) => f(...a)
@@ -3092,8 +3451,8 @@ var NotarizationPlugin = class {
3092
3451
  await peer.rpc.NotarizationService.notarize({
3093
3452
  credentials: credentials.filter((credential) => !this._processedCredentials.has(credential.id))
3094
3453
  });
3095
- log8("success", void 0, {
3096
- F: __dxlog_file10,
3454
+ log10("success", void 0, {
3455
+ F: __dxlog_file12,
3097
3456
  L: 147,
3098
3457
  S: this,
3099
3458
  C: (f, a) => f(...a)
@@ -3101,8 +3460,8 @@ var NotarizationPlugin = class {
3101
3460
  await sleep(successDelay);
3102
3461
  } catch (err) {
3103
3462
  if (!ctx.disposed && !err.message.includes(WRITER_NOT_SET_ERROR_CODE)) {
3104
- log8.info("error notarizing (recoverable)", err, {
3105
- F: __dxlog_file10,
3463
+ log10.info("error notarizing (recoverable)", err, {
3464
+ F: __dxlog_file12,
3106
3465
  L: 151,
3107
3466
  S: this,
3108
3467
  C: (f, a) => f(...a)
@@ -3119,8 +3478,8 @@ var NotarizationPlugin = class {
3119
3478
  allNotarized,
3120
3479
  errors.wait()
3121
3480
  ]);
3122
- log8("done", void 0, {
3123
- F: __dxlog_file10,
3481
+ log10("done", void 0, {
3482
+ F: __dxlog_file12,
3124
3483
  L: 162,
3125
3484
  S: this,
3126
3485
  C: (f, a) => f(...a)
@@ -3141,8 +3500,8 @@ var NotarizationPlugin = class {
3141
3500
  this._processCredentialsTriggers.delete(credential.id);
3142
3501
  }
3143
3502
  setWriter(writer) {
3144
- invariant9(!this._writer, "Writer already set.", {
3145
- F: __dxlog_file10,
3503
+ invariant11(!this._writer, "Writer already set.", {
3504
+ F: __dxlog_file12,
3146
3505
  L: 181,
3147
3506
  S: this,
3148
3507
  A: [
@@ -3156,7 +3515,7 @@ var NotarizationPlugin = class {
3156
3515
  if (this._processedCredentials.has(id)) {
3157
3516
  return;
3158
3517
  }
3159
- await entry(this._processCredentialsTriggers, id).orInsert(new Trigger4()).value.wait();
3518
+ await entry(this._processCredentialsTriggers, id).orInsert(new Trigger5()).value.wait();
3160
3519
  }
3161
3520
  /**
3162
3521
  * Requests from other peers to notarize credentials.
@@ -3166,8 +3525,8 @@ var NotarizationPlugin = class {
3166
3525
  throw new Error(WRITER_NOT_SET_ERROR_CODE);
3167
3526
  }
3168
3527
  for (const credential of request.credentials ?? []) {
3169
- invariant9(credential.id, "Credential must have an id", {
3170
- F: __dxlog_file10,
3528
+ invariant11(credential.id, "Credential must have an id", {
3529
+ F: __dxlog_file12,
3171
3530
  L: 200,
3172
3531
  S: this,
3173
3532
  A: [
@@ -3184,10 +3543,10 @@ var NotarizationPlugin = class {
3184
3543
  createExtension() {
3185
3544
  const extension = new NotarizationTeleportExtension({
3186
3545
  onOpen: async () => {
3187
- log8("extension opened", {
3546
+ log10("extension opened", {
3188
3547
  peer: extension.localPeerId
3189
3548
  }, {
3190
- F: __dxlog_file10,
3549
+ F: __dxlog_file12,
3191
3550
  L: 211,
3192
3551
  S: this,
3193
3552
  C: (f, a) => f(...a)
@@ -3196,10 +3555,10 @@ var NotarizationPlugin = class {
3196
3555
  this._extensionOpened.emit();
3197
3556
  },
3198
3557
  onClose: async () => {
3199
- log8("extension closed", {
3558
+ log10("extension closed", {
3200
3559
  peer: extension.localPeerId
3201
3560
  }, {
3202
- F: __dxlog_file10,
3561
+ F: __dxlog_file12,
3203
3562
  L: 216,
3204
3563
  S: this,
3205
3564
  C: (f, a) => f(...a)
@@ -3211,14 +3570,14 @@ var NotarizationPlugin = class {
3211
3570
  return extension;
3212
3571
  }
3213
3572
  };
3214
- var NotarizationTeleportExtension = class extends RpcExtension2 {
3573
+ var NotarizationTeleportExtension = class extends RpcExtension3 {
3215
3574
  constructor(_params) {
3216
3575
  super({
3217
3576
  requested: {
3218
- NotarizationService: schema3.getService("dxos.mesh.teleport.notarization.NotarizationService")
3577
+ NotarizationService: schema4.getService("dxos.mesh.teleport.notarization.NotarizationService")
3219
3578
  },
3220
3579
  exposed: {
3221
- NotarizationService: schema3.getService("dxos.mesh.teleport.notarization.NotarizationService")
3580
+ NotarizationService: schema4.getService("dxos.mesh.teleport.notarization.NotarizationService")
3222
3581
  }
3223
3582
  });
3224
3583
  this._params = _params;
@@ -3253,7 +3612,7 @@ function _ts_decorate4(decorators, target, key, desc) {
3253
3612
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3254
3613
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3255
3614
  }
3256
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space.ts";
3615
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space.ts";
3257
3616
  var DataSpace = class {
3258
3617
  constructor(params) {
3259
3618
  this._ctx = new Context8();
@@ -3277,19 +3636,19 @@ var DataSpace = class {
3277
3636
  this._metadataStore = params.metadataStore;
3278
3637
  this._signingContext = params.signingContext;
3279
3638
  this._callbacks = params.callbacks ?? {};
3280
- this._automergeHost = params.automergeHost;
3639
+ this._echoHost = params.echoHost;
3281
3640
  this.authVerifier = new TrustedKeySetAuthVerifier({
3282
- trustedKeysProvider: () => new ComplexSet3(PublicKey9.hash, Array.from(this._inner.spaceState.members.values()).filter((member) => !member.removed).map((member) => member.key)),
3641
+ trustedKeysProvider: () => new ComplexSet5(PublicKey10.hash, Array.from(this._inner.spaceState.members.values()).filter((member) => member.role !== SpaceMember3.Role.REMOVED).map((member) => member.key)),
3283
3642
  update: this._inner.stateUpdate,
3284
3643
  authTimeout: AUTH_TIMEOUT2
3285
3644
  });
3286
3645
  this._cache = params.cache;
3287
3646
  this._state = params.initialState;
3288
- log9("new state", {
3647
+ log11("new state", {
3289
3648
  state: SpaceState[this._state]
3290
3649
  }, {
3291
- F: __dxlog_file11,
3292
- L: 140,
3650
+ F: __dxlog_file13,
3651
+ L: 137,
3293
3652
  S: this,
3294
3653
  C: (f, a) => f(...a)
3295
3654
  });
@@ -3335,11 +3694,11 @@ var DataSpace = class {
3335
3694
  await this._inner.spaceState.addCredentialProcessor(this._automergeSpaceState);
3336
3695
  await this._inner.open(new Context8());
3337
3696
  this._state = SpaceState.CONTROL_ONLY;
3338
- log9("new state", {
3697
+ log11("new state", {
3339
3698
  state: SpaceState[this._state]
3340
3699
  }, {
3341
- F: __dxlog_file11,
3342
- L: 198,
3700
+ F: __dxlog_file13,
3701
+ L: 195,
3343
3702
  S: this,
3344
3703
  C: (f, a) => f(...a)
3345
3704
  });
@@ -3353,11 +3712,11 @@ var DataSpace = class {
3353
3712
  async _close() {
3354
3713
  await this._callbacks.beforeClose?.();
3355
3714
  this._state = SpaceState.CLOSED;
3356
- log9("new state", {
3715
+ log11("new state", {
3357
3716
  state: SpaceState[this._state]
3358
3717
  }, {
3359
- F: __dxlog_file11,
3360
- L: 212,
3718
+ F: __dxlog_file13,
3719
+ L: 209,
3361
3720
  S: this,
3362
3721
  C: (f, a) => f(...a)
3363
3722
  });
@@ -3381,32 +3740,32 @@ var DataSpace = class {
3381
3740
  * Initialize the data pipeline in a separate task.
3382
3741
  */
3383
3742
  initializeDataPipelineAsync() {
3384
- scheduleTask4(this._ctx, async () => {
3743
+ scheduleTask5(this._ctx, async () => {
3385
3744
  try {
3386
3745
  this.metrics.pipelineInitBegin = /* @__PURE__ */ new Date();
3387
3746
  await this.initializeDataPipeline();
3388
3747
  } catch (err) {
3389
- if (err instanceof CancelledError || err instanceof ContextDisposedError) {
3390
- log9("data pipeline initialization cancelled", err, {
3391
- F: __dxlog_file11,
3392
- L: 245,
3748
+ if (err instanceof CancelledError || err instanceof ContextDisposedError3) {
3749
+ log11("data pipeline initialization cancelled", err, {
3750
+ F: __dxlog_file13,
3751
+ L: 242,
3393
3752
  S: this,
3394
3753
  C: (f, a) => f(...a)
3395
3754
  });
3396
3755
  return;
3397
3756
  }
3398
- log9.error("Error initializing data pipeline", err, {
3399
- F: __dxlog_file11,
3400
- L: 249,
3757
+ log11.error("Error initializing data pipeline", err, {
3758
+ F: __dxlog_file13,
3759
+ L: 246,
3401
3760
  S: this,
3402
3761
  C: (f, a) => f(...a)
3403
3762
  });
3404
3763
  this._state = SpaceState.ERROR;
3405
- log9("new state", {
3764
+ log11("new state", {
3406
3765
  state: SpaceState[this._state]
3407
3766
  }, {
3408
- F: __dxlog_file11,
3409
- L: 251,
3767
+ F: __dxlog_file13,
3768
+ L: 248,
3410
3769
  S: this,
3411
3770
  C: (f, a) => f(...a)
3412
3771
  });
@@ -3422,31 +3781,31 @@ var DataSpace = class {
3422
3781
  throw new SystemError("Invalid operation");
3423
3782
  }
3424
3783
  this._state = SpaceState.INITIALIZING;
3425
- log9("new state", {
3784
+ log11("new state", {
3426
3785
  state: SpaceState[this._state]
3427
3786
  }, {
3428
- F: __dxlog_file11,
3429
- L: 267,
3787
+ F: __dxlog_file13,
3788
+ L: 264,
3430
3789
  S: this,
3431
3790
  C: (f, a) => f(...a)
3432
3791
  });
3433
3792
  await this._initializeAndReadControlPipeline();
3434
3793
  await sleep2(1);
3435
3794
  this._automergeSpaceState.startProcessingRootDocs();
3436
- await cancelWithContext2(this._ctx, this.automergeSpaceState.ensureEpochInitialized());
3437
- log9("data pipeline ready", void 0, {
3438
- F: __dxlog_file11,
3439
- L: 279,
3795
+ await cancelWithContext4(this._ctx, this.automergeSpaceState.ensureEpochInitialized());
3796
+ log11("data pipeline ready", void 0, {
3797
+ F: __dxlog_file13,
3798
+ L: 276,
3440
3799
  S: this,
3441
3800
  C: (f, a) => f(...a)
3442
3801
  });
3443
3802
  await this._callbacks.beforeReady?.();
3444
3803
  this._state = SpaceState.READY;
3445
- log9("new state", {
3804
+ log11("new state", {
3446
3805
  state: SpaceState[this._state]
3447
3806
  }, {
3448
- F: __dxlog_file11,
3449
- L: 283,
3807
+ F: __dxlog_file13,
3808
+ L: 280,
3450
3809
  S: this,
3451
3810
  C: (f, a) => f(...a)
3452
3811
  });
@@ -3460,9 +3819,9 @@ var DataSpace = class {
3460
3819
  });
3461
3820
  this.metrics.controlPipelineReady = /* @__PURE__ */ new Date();
3462
3821
  await this._createWritableFeeds();
3463
- log9("writable feeds created", void 0, {
3464
- F: __dxlog_file11,
3465
- L: 299,
3822
+ log11("writable feeds created", void 0, {
3823
+ F: __dxlog_file13,
3824
+ L: 296,
3466
3825
  S: this,
3467
3826
  C: (f, a) => f(...a)
3468
3827
  });
@@ -3520,21 +3879,21 @@ var DataSpace = class {
3520
3879
  }
3521
3880
  }
3522
3881
  _onNewAutomergeRoot(rootUrl) {
3523
- log9("loading automerge root doc for space", {
3882
+ log11("loading automerge root doc for space", {
3524
3883
  space: this.key,
3525
3884
  rootUrl
3526
3885
  }, {
3527
- F: __dxlog_file11,
3528
- L: 365,
3886
+ F: __dxlog_file13,
3887
+ L: 362,
3529
3888
  S: this,
3530
3889
  C: (f, a) => f(...a)
3531
3890
  });
3532
- this._automergeHost._requestedDocs.add(rootUrl);
3533
- const handle = this._automergeHost.repo.find(rootUrl);
3891
+ this._echoHost.replicateDocument(rootUrl);
3892
+ const handle = this._echoHost.automergeRepo.find(rootUrl);
3534
3893
  queueMicrotask(async () => {
3535
3894
  try {
3536
3895
  await warnAfterTimeout(5e3, "Automerge root doc load timeout (DataSpace)", async () => {
3537
- await cancelWithContext2(this._ctx, handle.whenReady());
3896
+ await cancelWithContext4(this._ctx, handle.whenReady());
3538
3897
  });
3539
3898
  if (this._ctx.disposed) {
3540
3899
  return;
@@ -3548,16 +3907,16 @@ var DataSpace = class {
3548
3907
  });
3549
3908
  }
3550
3909
  } catch (err) {
3551
- if (err instanceof ContextDisposedError) {
3910
+ if (err instanceof ContextDisposedError3) {
3552
3911
  return;
3553
3912
  }
3554
- log9.warn("error loading automerge root doc", {
3913
+ log11.warn("error loading automerge root doc", {
3555
3914
  space: this.key,
3556
3915
  rootUrl,
3557
3916
  err
3558
3917
  }, {
3559
- F: __dxlog_file11,
3560
- L: 388,
3918
+ F: __dxlog_file13,
3919
+ L: 385,
3561
3920
  S: this,
3562
3921
  C: (f, a) => f(...a)
3563
3922
  });
@@ -3595,7 +3954,7 @@ var DataSpace = class {
3595
3954
  break;
3596
3955
  case CreateEpochRequest.Migration.INIT_AUTOMERGE:
3597
3956
  {
3598
- const document = this._automergeHost.repo.create();
3957
+ const document = this._echoHost.automergeRepo.create();
3599
3958
  epoch = {
3600
3959
  previousId: this._automergeSpaceState.lastEpoch?.id,
3601
3960
  number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
@@ -3607,12 +3966,12 @@ var DataSpace = class {
3607
3966
  case CreateEpochRequest.Migration.PRUNE_AUTOMERGE_ROOT_HISTORY:
3608
3967
  {
3609
3968
  const currentRootUrl = this._automergeSpaceState.rootUrl;
3610
- const rootHandle = this._automergeHost.repo.find(currentRootUrl);
3611
- await cancelWithContext2(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3612
- const newRoot = this._automergeHost.repo.create(rootHandle.docSync());
3613
- invariant10(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3614
- F: __dxlog_file11,
3615
- L: 438,
3969
+ const rootHandle = this._echoHost.automergeRepo.find(currentRootUrl);
3970
+ await cancelWithContext4(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3971
+ const newRoot = this._echoHost.automergeRepo.create(rootHandle.docSync());
3972
+ invariant12(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3973
+ F: __dxlog_file13,
3974
+ L: 435,
3616
3975
  S: this,
3617
3976
  A: [
3618
3977
  "typeof newRoot.url === 'string' && newRoot.url.length > 0",
@@ -3629,21 +3988,21 @@ var DataSpace = class {
3629
3988
  break;
3630
3989
  case CreateEpochRequest.Migration.FRAGMENT_AUTOMERGE_ROOT:
3631
3990
  {
3632
- log9.info("Fragmenting", void 0, {
3633
- F: __dxlog_file11,
3634
- L: 450,
3991
+ log11.info("Fragmenting", void 0, {
3992
+ F: __dxlog_file13,
3993
+ L: 447,
3635
3994
  S: this,
3636
3995
  C: (f, a) => f(...a)
3637
3996
  });
3638
3997
  const currentRootUrl = this._automergeSpaceState.rootUrl;
3639
- const rootHandle = this._automergeHost.repo.find(currentRootUrl);
3640
- await cancelWithContext2(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3998
+ const rootHandle = this._echoHost.automergeRepo.find(currentRootUrl);
3999
+ await cancelWithContext4(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3641
4000
  const objects = Object.entries(rootHandle.docSync().objects);
3642
4001
  const properties = objects.find(([_, value]) => value.system.type?.itemId === TYPE_PROPERTIES);
3643
4002
  const otherObjects = objects.filter(([key]) => key !== properties?.[0]);
3644
- invariant10(properties, "Properties not found", {
3645
- F: __dxlog_file11,
3646
- L: 460,
4003
+ invariant12(properties, "Properties not found", {
4004
+ F: __dxlog_file13,
4005
+ L: 457,
3647
4006
  S: this,
3648
4007
  A: [
3649
4008
  "properties",
@@ -3656,17 +4015,17 @@ var DataSpace = class {
3656
4015
  properties
3657
4016
  ])
3658
4017
  };
3659
- const newRoot = this._automergeHost.repo.create(newSpaceDoc);
3660
- invariant10(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3661
- F: __dxlog_file11,
3662
- L: 465,
4018
+ const newRoot = this._echoHost.automergeRepo.create(newSpaceDoc);
4019
+ invariant12(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
4020
+ F: __dxlog_file13,
4021
+ L: 462,
3663
4022
  S: this,
3664
4023
  A: [
3665
4024
  "typeof newRoot.url === 'string' && newRoot.url.length > 0",
3666
4025
  ""
3667
4026
  ]
3668
4027
  });
3669
- const docLoader = new AutomergeDocumentLoaderImpl(this.key, this._automergeHost.repo);
4028
+ const docLoader = new AutomergeDocumentLoaderImpl(this.key, this._echoHost.automergeRepo);
3670
4029
  await docLoader.loadSpaceRootDocHandle(this._ctx, {
3671
4030
  rootUrl: newRoot.url
3672
4031
  });
@@ -3724,11 +4083,11 @@ var DataSpace = class {
3724
4083
  await this._metadataStore.setSpaceState(this.key, SpaceState.INACTIVE);
3725
4084
  await this._close();
3726
4085
  this._state = SpaceState.INACTIVE;
3727
- log9("new state", {
4086
+ log11("new state", {
3728
4087
  state: SpaceState[this._state]
3729
4088
  }, {
3730
- F: __dxlog_file11,
3731
- L: 531,
4089
+ F: __dxlog_file13,
4090
+ L: 528,
3732
4091
  S: this,
3733
4092
  C: (f, a) => f(...a)
3734
4093
  });
@@ -3783,20 +4142,20 @@ DataSpace = _ts_decorate4([
3783
4142
 
3784
4143
  // packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts
3785
4144
  import { Event as Event7, synchronized as synchronized2, trackLeaks as trackLeaks2 } from "@dxos/async";
3786
- import { Context as Context9, cancelWithContext as cancelWithContext3 } from "@dxos/context";
4145
+ import { Context as Context9, cancelWithContext as cancelWithContext5 } from "@dxos/context";
3787
4146
  import { getCredentialAssertion as getCredentialAssertion2 } from "@dxos/credentials";
3788
- import { invariant as invariant11 } from "@dxos/invariant";
3789
- import { PublicKey as PublicKey10 } from "@dxos/keys";
3790
- import { log as log10 } from "@dxos/log";
4147
+ import { invariant as invariant13 } from "@dxos/invariant";
4148
+ import { PublicKey as PublicKey11 } from "@dxos/keys";
4149
+ import { log as log12 } from "@dxos/log";
3791
4150
  import { trace as trace7 } from "@dxos/protocols";
3792
- import { Invitation as Invitation6, SpaceState as SpaceState2 } from "@dxos/protocols/proto/dxos/client/services";
4151
+ import { Invitation as Invitation7, SpaceState as SpaceState2 } from "@dxos/protocols/proto/dxos/client/services";
3793
4152
  import { Gossip as Gossip2, Presence as Presence2 } from "@dxos/teleport-extension-gossip";
3794
4153
  import { ComplexMap as ComplexMap3, deferFunction as deferFunction2, forEachAsync } from "@dxos/util";
3795
4154
 
3796
4155
  // packages/sdk/client-services/src/packlets/spaces/genesis.ts
3797
4156
  import { createCredential } from "@dxos/credentials";
3798
4157
  import { failUndefined } from "@dxos/debug";
3799
- import { AdmittedFeed as AdmittedFeed4, SpaceMember as SpaceMember2 } from "@dxos/protocols/proto/dxos/halo/credentials";
4158
+ import { AdmittedFeed as AdmittedFeed4, SpaceMember as SpaceMember4 } from "@dxos/protocols/proto/dxos/halo/credentials";
3800
4159
  import { Timeframe as Timeframe3 } from "@dxos/timeframe";
3801
4160
  var spaceGenesis = async (keyring, signingContext, space, automergeRoot) => {
3802
4161
  const credentials = [
@@ -3816,7 +4175,7 @@ var spaceGenesis = async (keyring, signingContext, space, automergeRoot) => {
3816
4175
  assertion: {
3817
4176
  "@type": "dxos.halo.credentials.SpaceMember",
3818
4177
  spaceKey: space.key,
3819
- role: SpaceMember2.Role.ADMIN,
4178
+ role: SpaceMember4.Role.OWNER,
3820
4179
  profile: signingContext.getProfile(),
3821
4180
  genesisFeedKey: space.controlFeedKey ?? failUndefined()
3822
4181
  }
@@ -3874,23 +4233,23 @@ function _ts_decorate5(decorators, target, key, desc) {
3874
4233
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3875
4234
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3876
4235
  }
3877
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts";
4236
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts";
3878
4237
  var PRESENCE_ANNOUNCE_INTERVAL = 1e4;
3879
4238
  var PRESENCE_OFFLINE_TIMEOUT = 2e4;
3880
4239
  var DataSpaceManager = class {
3881
- constructor(_spaceManager, _metadataStore, _keyring, _signingContext, _feedStore, _automergeHost, _invitationsManager, params) {
4240
+ constructor(_spaceManager, _metadataStore, _keyring, _signingContext, _feedStore, _echoHost, _invitationsManager, params) {
3882
4241
  this._spaceManager = _spaceManager;
3883
4242
  this._metadataStore = _metadataStore;
3884
4243
  this._keyring = _keyring;
3885
4244
  this._signingContext = _signingContext;
3886
4245
  this._feedStore = _feedStore;
3887
- this._automergeHost = _automergeHost;
4246
+ this._echoHost = _echoHost;
3888
4247
  this._invitationsManager = _invitationsManager;
3889
4248
  this._ctx = new Context9();
3890
4249
  this.updated = new Event7();
3891
- this._spaces = new ComplexMap3(PublicKey10.hash);
4250
+ this._spaces = new ComplexMap3(PublicKey11.hash);
3892
4251
  this._isOpen = false;
3893
- this._instanceId = PublicKey10.random().toHex();
4252
+ this._instanceId = PublicKey11.random().toHex();
3894
4253
  const { spaceMemberPresenceAnnounceInterval = PRESENCE_ANNOUNCE_INTERVAL, spaceMemberPresenceOfflineTimeout = PRESENCE_OFFLINE_TIMEOUT } = params ?? {};
3895
4254
  this._spaceMemberPresenceAnnounceInterval = spaceMemberPresenceAnnounceInterval;
3896
4255
  this._spaceMemberPresenceOfflineTimeout = spaceMemberPresenceOfflineTimeout;
@@ -3900,46 +4259,46 @@ var DataSpaceManager = class {
3900
4259
  return this._spaces;
3901
4260
  }
3902
4261
  async open() {
3903
- log10("open", void 0, {
3904
- F: __dxlog_file12,
3905
- L: 101,
4262
+ log12("open", void 0, {
4263
+ F: __dxlog_file14,
4264
+ L: 102,
3906
4265
  S: this,
3907
4266
  C: (f, a) => f(...a)
3908
4267
  });
3909
- log10.trace("dxos.echo.data-space-manager.open", trace7.begin({
4268
+ log12.trace("dxos.echo.data-space-manager.open", trace7.begin({
3910
4269
  id: this._instanceId
3911
4270
  }), {
3912
- F: __dxlog_file12,
3913
- L: 102,
4271
+ F: __dxlog_file14,
4272
+ L: 103,
3914
4273
  S: this,
3915
4274
  C: (f, a) => f(...a)
3916
4275
  });
3917
- log10("metadata loaded", {
4276
+ log12("metadata loaded", {
3918
4277
  spaces: this._metadataStore.spaces.length
3919
4278
  }, {
3920
- F: __dxlog_file12,
3921
- L: 103,
4279
+ F: __dxlog_file14,
4280
+ L: 104,
3922
4281
  S: this,
3923
4282
  C: (f, a) => f(...a)
3924
4283
  });
3925
4284
  await forEachAsync(this._metadataStore.spaces, async (spaceMetadata) => {
3926
4285
  try {
3927
- log10("load space", {
4286
+ log12("load space", {
3928
4287
  spaceMetadata
3929
4288
  }, {
3930
- F: __dxlog_file12,
3931
- L: 107,
4289
+ F: __dxlog_file14,
4290
+ L: 108,
3932
4291
  S: this,
3933
4292
  C: (f, a) => f(...a)
3934
4293
  });
3935
4294
  await this._constructSpace(spaceMetadata);
3936
4295
  } catch (err) {
3937
- log10.error("Error loading space", {
4296
+ log12.error("Error loading space", {
3938
4297
  spaceMetadata,
3939
4298
  err
3940
4299
  }, {
3941
- F: __dxlog_file12,
3942
- L: 110,
4300
+ F: __dxlog_file14,
4301
+ L: 111,
3943
4302
  S: this,
3944
4303
  C: (f, a) => f(...a)
3945
4304
  });
@@ -3952,19 +4311,19 @@ var DataSpaceManager = class {
3952
4311
  space.initializeDataPipelineAsync();
3953
4312
  }
3954
4313
  }
3955
- log10.trace("dxos.echo.data-space-manager.open", trace7.end({
4314
+ log12.trace("dxos.echo.data-space-manager.open", trace7.end({
3956
4315
  id: this._instanceId
3957
4316
  }), {
3958
- F: __dxlog_file12,
3959
- L: 123,
4317
+ F: __dxlog_file14,
4318
+ L: 124,
3960
4319
  S: this,
3961
4320
  C: (f, a) => f(...a)
3962
4321
  });
3963
4322
  }
3964
4323
  async close() {
3965
- log10("close", void 0, {
3966
- F: __dxlog_file12,
3967
- L: 128,
4324
+ log12("close", void 0, {
4325
+ F: __dxlog_file14,
4326
+ L: 129,
3968
4327
  S: this,
3969
4328
  C: (f, a) => f(...a)
3970
4329
  });
@@ -3978,9 +4337,9 @@ var DataSpaceManager = class {
3978
4337
  * Creates a new space writing the genesis credentials to the control feed.
3979
4338
  */
3980
4339
  async createSpace() {
3981
- invariant11(this._isOpen, "Not open.", {
3982
- F: __dxlog_file12,
3983
- L: 141,
4340
+ invariant13(this._isOpen, "Not open.", {
4341
+ F: __dxlog_file14,
4342
+ L: 142,
3984
4343
  S: this,
3985
4344
  A: [
3986
4345
  "this._isOpen",
@@ -3997,27 +4356,22 @@ var DataSpaceManager = class {
3997
4356
  dataFeedKey,
3998
4357
  state: SpaceState2.ACTIVE
3999
4358
  };
4000
- log10("creating space...", {
4359
+ log12("creating space...", {
4001
4360
  spaceKey
4002
4361
  }, {
4003
- F: __dxlog_file12,
4004
- L: 153,
4362
+ F: __dxlog_file14,
4363
+ L: 154,
4005
4364
  S: this,
4006
4365
  C: (f, a) => f(...a)
4007
4366
  });
4008
- const automergeRoot = this._automergeHost.repo.create();
4009
- automergeRoot.change((doc) => {
4010
- doc.access = {
4011
- spaceKey: spaceKey.toHex()
4012
- };
4013
- });
4367
+ const automergeRootUrl = await this._echoHost.createSpaceRoot(spaceKey);
4014
4368
  const space = await this._constructSpace(metadata);
4015
- const credentials = await spaceGenesis(this._keyring, this._signingContext, space.inner, automergeRoot.url);
4369
+ const credentials = await spaceGenesis(this._keyring, this._signingContext, space.inner, automergeRootUrl);
4016
4370
  await this._metadataStore.addSpace(metadata);
4017
4371
  const memberCredential = credentials[1];
4018
- invariant11(getCredentialAssertion2(memberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
4019
- F: __dxlog_file12,
4020
- L: 166,
4372
+ invariant13(getCredentialAssertion2(memberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
4373
+ F: __dxlog_file14,
4374
+ L: 163,
4021
4375
  S: this,
4022
4376
  A: [
4023
4377
  "getCredentialAssertion(memberCredential)['@type'] === 'dxos.halo.credentials.SpaceMember'",
@@ -4031,26 +4385,26 @@ var DataSpaceManager = class {
4031
4385
  }
4032
4386
  // TODO(burdon): Rename join space.
4033
4387
  async acceptSpace(opts) {
4034
- log10("accept space", {
4388
+ log12("accept space", {
4035
4389
  opts
4036
4390
  }, {
4037
- F: __dxlog_file12,
4038
- L: 178,
4391
+ F: __dxlog_file14,
4392
+ L: 175,
4039
4393
  S: this,
4040
4394
  C: (f, a) => f(...a)
4041
4395
  });
4042
- invariant11(this._isOpen, "Not open.", {
4043
- F: __dxlog_file12,
4044
- L: 179,
4396
+ invariant13(this._isOpen, "Not open.", {
4397
+ F: __dxlog_file14,
4398
+ L: 176,
4045
4399
  S: this,
4046
4400
  A: [
4047
4401
  "this._isOpen",
4048
4402
  "'Not open.'"
4049
4403
  ]
4050
4404
  });
4051
- invariant11(!this._spaces.has(opts.spaceKey), "Space already exists.", {
4052
- F: __dxlog_file12,
4053
- L: 180,
4405
+ invariant13(!this._spaces.has(opts.spaceKey), "Space already exists.", {
4406
+ F: __dxlog_file14,
4407
+ L: 177,
4054
4408
  S: this,
4055
4409
  A: [
4056
4410
  "!this._spaces.has(opts.spaceKey)",
@@ -4075,17 +4429,17 @@ var DataSpaceManager = class {
4075
4429
  * TODO(dmaretskyi): Consider removing.
4076
4430
  */
4077
4431
  async waitUntilSpaceReady(spaceKey) {
4078
- await cancelWithContext3(this._ctx, this.updated.waitForCondition(() => {
4432
+ await cancelWithContext5(this._ctx, this.updated.waitForCondition(() => {
4079
4433
  const space = this._spaces.get(spaceKey);
4080
4434
  return !!space && space.state === SpaceState2.READY;
4081
4435
  }));
4082
4436
  }
4083
4437
  async _constructSpace(metadata) {
4084
- log10("construct space", {
4438
+ log12("construct space", {
4085
4439
  metadata
4086
4440
  }, {
4087
- F: __dxlog_file12,
4088
- L: 213,
4441
+ F: __dxlog_file14,
4442
+ L: 210,
4089
4443
  S: this,
4090
4444
  C: (f, a) => f(...a)
4091
4445
  });
@@ -4117,13 +4471,13 @@ var DataSpaceManager = class {
4117
4471
  remotePeerId: session.remotePeerId
4118
4472
  }));
4119
4473
  session.addExtension("dxos.mesh.teleport.notarization", dataSpace.notarizationPlugin.createExtension());
4120
- this._automergeHost.authorizeDevice(space.key, session.remotePeerId);
4121
- session.addExtension("dxos.mesh.teleport.automerge", this._automergeHost.createExtension());
4474
+ this._echoHost.authorizeDevice(space.key, session.remotePeerId);
4475
+ session.addExtension("dxos.mesh.teleport.automerge", this._echoHost.createReplicationExtension());
4122
4476
  },
4123
4477
  onAuthFailure: () => {
4124
- log10.warn("auth failure", void 0, {
4125
- F: __dxlog_file12,
4126
- L: 250,
4478
+ log12.warn("auth failure", void 0, {
4479
+ F: __dxlog_file14,
4480
+ L: 247,
4127
4481
  S: this,
4128
4482
  C: (f, a) => f(...a)
4129
4483
  });
@@ -4143,25 +4497,26 @@ var DataSpaceManager = class {
4143
4497
  presence,
4144
4498
  keyring: this._keyring,
4145
4499
  feedStore: this._feedStore,
4500
+ echoHost: this._echoHost,
4146
4501
  signingContext: this._signingContext,
4147
4502
  callbacks: {
4148
4503
  beforeReady: async () => {
4149
- log10("before space ready", {
4504
+ log12("before space ready", {
4150
4505
  space: space.key
4151
4506
  }, {
4152
- F: __dxlog_file12,
4153
- L: 271,
4507
+ F: __dxlog_file14,
4508
+ L: 269,
4154
4509
  S: this,
4155
4510
  C: (f, a) => f(...a)
4156
4511
  });
4157
4512
  },
4158
4513
  afterReady: async () => {
4159
- log10("after space ready", {
4514
+ log12("after space ready", {
4160
4515
  space: space.key,
4161
4516
  open: this._isOpen
4162
4517
  }, {
4163
- F: __dxlog_file12,
4164
- L: 274,
4518
+ F: __dxlog_file14,
4519
+ L: 272,
4165
4520
  S: this,
4166
4521
  C: (f, a) => f(...a)
4167
4522
  });
@@ -4173,18 +4528,17 @@ var DataSpaceManager = class {
4173
4528
  }
4174
4529
  },
4175
4530
  beforeClose: async () => {
4176
- log10("before space close", {
4531
+ log12("before space close", {
4177
4532
  space: space.key
4178
4533
  }, {
4179
- F: __dxlog_file12,
4180
- L: 281,
4534
+ F: __dxlog_file14,
4535
+ L: 279,
4181
4536
  S: this,
4182
4537
  C: (f, a) => f(...a)
4183
4538
  });
4184
4539
  }
4185
4540
  },
4186
- cache: metadata.cache,
4187
- automergeHost: this._automergeHost
4541
+ cache: metadata.cache
4188
4542
  });
4189
4543
  if (metadata.state !== SpaceState2.INACTIVE) {
4190
4544
  await dataSpace.open();
@@ -4213,8 +4567,8 @@ var DataSpaceManager = class {
4213
4567
  async _createDelegatedInvitations(space, invitations) {
4214
4568
  const tasks = invitations.map(([credentialId, invitation]) => {
4215
4569
  return this._invitationsManager.createInvitation({
4216
- type: Invitation6.Type.DELEGATED,
4217
- kind: Invitation6.Kind.SPACE,
4570
+ type: Invitation7.Type.DELEGATED,
4571
+ kind: Invitation7.Kind.SPACE,
4218
4572
  spaceKey: space.key,
4219
4573
  authMethod: invitation.authMethod,
4220
4574
  invitationId: invitation.invitationId,
@@ -4248,14 +4602,15 @@ DataSpaceManager = _ts_decorate5([
4248
4602
  ], DataSpaceManager);
4249
4603
 
4250
4604
  // packages/sdk/client-services/src/packlets/spaces/spaces-service.ts
4251
- import { EventSubscriptions as EventSubscriptions2, UpdateScheduler, scheduleTask as scheduleTask5 } from "@dxos/async";
4605
+ import { EventSubscriptions as EventSubscriptions2, UpdateScheduler, scheduleTask as scheduleTask6 } from "@dxos/async";
4252
4606
  import { Stream as Stream10 } from "@dxos/codec-protobuf";
4253
4607
  import { raise as raise2 } from "@dxos/debug";
4254
- import { invariant as invariant12 } from "@dxos/invariant";
4255
- import { log as log11 } from "@dxos/log";
4608
+ import { invariant as invariant14 } from "@dxos/invariant";
4609
+ import { log as log13 } from "@dxos/log";
4256
4610
  import { ApiError, SpaceNotFoundError, encodeError } from "@dxos/protocols";
4257
- import { SpaceMember as SpaceMember3, SpaceState as SpaceState3 } from "@dxos/protocols/proto/dxos/client/services";
4258
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/spaces-service.ts";
4611
+ import { SpaceMember as SpaceMember5, SpaceState as SpaceState3 } from "@dxos/protocols/proto/dxos/client/services";
4612
+ import { SpaceMember as HaloSpaceMember } from "@dxos/protocols/proto/dxos/halo/credentials";
4613
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/spaces-service.ts";
4259
4614
  var SpacesServiceImpl = class {
4260
4615
  constructor(_identityManager, _spaceManager, _getDataSpaceManager) {
4261
4616
  this._identityManager = _identityManager;
@@ -4286,16 +4641,19 @@ var SpacesServiceImpl = class {
4286
4641
  }
4287
4642
  }
4288
4643
  }
4644
+ async updateMemberRole(_) {
4645
+ throw new Error("not implemented");
4646
+ }
4289
4647
  querySpaces() {
4290
4648
  return new Stream10(({ next, ctx }) => {
4291
4649
  const scheduler = new UpdateScheduler(ctx, async () => {
4292
4650
  const dataSpaceManager = await this._getDataSpaceManager();
4293
4651
  const spaces = Array.from(dataSpaceManager.spaces.values()).map((space) => this._serializeSpace(space));
4294
- log11("update", {
4652
+ log13("update", {
4295
4653
  spaces
4296
4654
  }, {
4297
- F: __dxlog_file13,
4298
- L: 77,
4655
+ F: __dxlog_file15,
4656
+ L: 82,
4299
4657
  S: this,
4300
4658
  C: (f, a) => f(...a)
4301
4659
  });
@@ -4305,7 +4663,7 @@ var SpacesServiceImpl = class {
4305
4663
  }, {
4306
4664
  maxFrequency: false ? void 0 : 2
4307
4665
  });
4308
- scheduleTask5(ctx, async () => {
4666
+ scheduleTask6(ctx, async () => {
4309
4667
  const dataSpaceManager = await this._getDataSpaceManager();
4310
4668
  const subscriptions = new EventSubscriptions2();
4311
4669
  ctx.onDispose(() => subscriptions.clear());
@@ -4339,7 +4697,7 @@ var SpacesServiceImpl = class {
4339
4697
  }
4340
4698
  subscribeMessages({ spaceKey, channel }) {
4341
4699
  return new Stream10(({ ctx, next }) => {
4342
- scheduleTask5(ctx, async () => {
4700
+ scheduleTask6(ctx, async () => {
4343
4701
  const dataSpaceManager = await this._getDataSpaceManager();
4344
4702
  const space = dataSpaceManager.spaces.get(spaceKey) ?? raise2(new SpaceNotFoundError(spaceKey));
4345
4703
  const handle = space.listen(getChannelId(channel), (message) => {
@@ -4358,7 +4716,7 @@ var SpacesServiceImpl = class {
4358
4716
  }
4359
4717
  };
4360
4718
  ctx.onDispose(() => space.spaceState.removeCredentialProcessor(processor));
4361
- scheduleTask5(ctx, async () => {
4719
+ scheduleTask6(ctx, async () => {
4362
4720
  await space.spaceState.addCredentialProcessor(processor);
4363
4721
  if (noTail) {
4364
4722
  close();
@@ -4376,18 +4734,18 @@ var SpacesServiceImpl = class {
4376
4734
  }
4377
4735
  });
4378
4736
  } else {
4379
- invariant12(!credential.id, "Id on unsigned credentials is not allowed", {
4380
- F: __dxlog_file13,
4381
- L: 164,
4737
+ invariant14(!credential.id, "Id on unsigned credentials is not allowed", {
4738
+ F: __dxlog_file15,
4739
+ L: 169,
4382
4740
  S: this,
4383
4741
  A: [
4384
4742
  "!credential.id",
4385
4743
  "'Id on unsigned credentials is not allowed'"
4386
4744
  ]
4387
4745
  });
4388
- invariant12(this._identityManager.identity, "Identity is not available", {
4389
- F: __dxlog_file13,
4390
- L: 165,
4746
+ invariant14(this._identityManager.identity, "Identity is not available", {
4747
+ F: __dxlog_file15,
4748
+ L: 170,
4391
4749
  S: this,
4392
4750
  A: [
4393
4751
  "this._identityManager.identity",
@@ -4395,9 +4753,9 @@ var SpacesServiceImpl = class {
4395
4753
  ]
4396
4754
  });
4397
4755
  const signer = this._identityManager.identity.getIdentityCredentialSigner();
4398
- invariant12(credential.issuer.equals(signer.getIssuer()), void 0, {
4399
- F: __dxlog_file13,
4400
- L: 167,
4756
+ invariant14(credential.issuer.equals(signer.getIssuer()), void 0, {
4757
+ F: __dxlog_file15,
4758
+ L: 172,
4401
4759
  S: this,
4402
4760
  A: [
4403
4761
  "credential.issuer.equals(signer.getIssuer())",
@@ -4452,7 +4810,7 @@ var SpacesServiceImpl = class {
4452
4810
  identityKey: member.key,
4453
4811
  profile: member.profile ?? {}
4454
4812
  },
4455
- presence: member.removed ? SpaceMember3.PresenceState.REMOVED : isMe || peers.length > 0 ? SpaceMember3.PresenceState.ONLINE : SpaceMember3.PresenceState.OFFLINE,
4813
+ presence: member.role === HaloSpaceMember.Role.REMOVED ? SpaceMember5.PresenceState.REMOVED : isMe || peers.length > 0 ? SpaceMember5.PresenceState.ONLINE : SpaceMember5.PresenceState.OFFLINE,
4456
4814
  peerStates: peers
4457
4815
  };
4458
4816
  }),
@@ -4465,53 +4823,22 @@ var SpacesServiceImpl = class {
4465
4823
  var getChannelId = (channel) => `user-channel/${channel}`;
4466
4824
 
4467
4825
  // packages/sdk/client-services/src/packlets/services/service-context.ts
4468
- import { Trigger as Trigger5 } from "@dxos/async";
4826
+ import { Trigger as Trigger6 } from "@dxos/async";
4469
4827
  import { Context as Context10, Resource } from "@dxos/context";
4470
4828
  import { getCredentialAssertion as getCredentialAssertion3 } from "@dxos/credentials";
4471
4829
  import { failUndefined as failUndefined2 } from "@dxos/debug";
4472
- import { AutomergeHost, MetadataStore, SnapshotStore, SpaceManager, valueEncoding } from "@dxos/echo-pipeline";
4830
+ import { EchoHost } from "@dxos/echo-db";
4831
+ import { MetadataStore, SnapshotStore, SpaceManager, valueEncoding } from "@dxos/echo-pipeline";
4473
4832
  import { FeedFactory, FeedStore } from "@dxos/feed-store";
4474
- import { IndexMetadataStore, IndexStore, Indexer, createStorageCallbacks } from "@dxos/indexing";
4475
- import { invariant as invariant13 } from "@dxos/invariant";
4833
+ import { invariant as invariant15 } from "@dxos/invariant";
4476
4834
  import { Keyring } from "@dxos/keyring";
4477
- import { PublicKey as PublicKey11 } from "@dxos/keys";
4478
- import { log as log12 } from "@dxos/log";
4835
+ import { PublicKey as PublicKey12 } from "@dxos/keys";
4836
+ import { log as log14 } from "@dxos/log";
4479
4837
  import { InvalidStorageVersionError, STORAGE_VERSION, trace as trace8 } from "@dxos/protocols";
4480
- import { Invitation as Invitation7 } from "@dxos/protocols/proto/dxos/client/services";
4838
+ import { Invitation as Invitation8 } from "@dxos/protocols/proto/dxos/client/services";
4481
4839
  import { BlobStore } from "@dxos/teleport-extension-object-sync";
4482
4840
  import { trace as Trace2 } from "@dxos/tracing";
4483
4841
  import { safeInstanceof } from "@dxos/util";
4484
-
4485
- // packages/sdk/client-services/src/packlets/indexing/util.ts
4486
- import { getHeads } from "@dxos/automerge/automerge";
4487
- import { idCodec } from "@dxos/protocols";
4488
- var createSelectedDocumentsIterator = (automergeHost) => (
4489
- /**
4490
- * Get object data blobs from Automerge Repo by ids.
4491
- * @param ids
4492
- */
4493
- // TODO(mykola): Unload automerge handles after usage.
4494
- async function* loadDocuments(ids) {
4495
- for (const id of ids) {
4496
- const { documentId, objectId } = idCodec.decode(id);
4497
- const handle = automergeHost.repo.handles[documentId] ?? automergeHost.repo.find(documentId);
4498
- if (!handle.isReady()) {
4499
- await handle.whenReady();
4500
- }
4501
- const doc = handle.docSync();
4502
- const hash = getHeads(doc).join("");
4503
- yield doc.objects?.[objectId] ? [
4504
- {
4505
- id,
4506
- object: doc.objects[objectId],
4507
- currentHash: hash
4508
- }
4509
- ] : [];
4510
- }
4511
- }
4512
- );
4513
-
4514
- // packages/sdk/client-services/src/packlets/services/service-context.ts
4515
4842
  function _ts_decorate6(decorators, target, key, desc) {
4516
4843
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4517
4844
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -4522,7 +4849,7 @@ function _ts_decorate6(decorators, target, key, desc) {
4522
4849
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
4523
4850
  return c > 3 && r && Object.defineProperty(target, key, r), r;
4524
4851
  }
4525
- var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-context.ts";
4852
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-context.ts";
4526
4853
  var ServiceContext = class extends Resource {
4527
4854
  constructor(storage, level, networkManager, signalManager, _runtimeParams) {
4528
4855
  super();
@@ -4531,9 +4858,9 @@ var ServiceContext = class extends Resource {
4531
4858
  this.networkManager = networkManager;
4532
4859
  this.signalManager = signalManager;
4533
4860
  this._runtimeParams = _runtimeParams;
4534
- this.initialized = new Trigger5();
4861
+ this.initialized = new Trigger6();
4535
4862
  this._handlerFactories = /* @__PURE__ */ new Map();
4536
- this._instanceId = PublicKey11.random().toHex();
4863
+ this._instanceId = PublicKey12.random().toHex();
4537
4864
  this.metadataStore = new MetadataStore(storage.createDirectory("metadata"));
4538
4865
  this.snapshotStore = new SnapshotStore(storage.createDirectory("snapshots"));
4539
4866
  this.blobStore = new BlobStore(storage.createDirectory("blobs"));
@@ -4556,48 +4883,33 @@ var ServiceContext = class extends Resource {
4556
4883
  snapshotStore: this.snapshotStore
4557
4884
  });
4558
4885
  this.identityManager = new IdentityManager(this.metadataStore, this.keyring, this.feedStore, this.spaceManager, this._runtimeParams);
4559
- this.indexMetadata = new IndexMetadataStore({
4560
- db: level.sublevel("index-metadata")
4561
- });
4562
- this.automergeHost = new AutomergeHost({
4563
- directory: storage.createDirectory("automerge"),
4564
- db: level.sublevel("automerge"),
4565
- storageCallbacks: createStorageCallbacks({
4566
- host: () => this.automergeHost,
4567
- metadata: this.indexMetadata
4568
- })
4569
- });
4570
- this.indexer = new Indexer({
4571
- db: this.level,
4572
- indexStore: new IndexStore({
4573
- db: level.sublevel("index-storage")
4574
- }),
4575
- metadataStore: this.indexMetadata,
4576
- loadDocuments: createSelectedDocumentsIterator(this.automergeHost)
4886
+ this.echoHost = new EchoHost({
4887
+ kv: this.level,
4888
+ storage: this.storage
4577
4889
  });
4578
- this.invitations = new InvitationsHandler(this.networkManager);
4890
+ this.invitations = new InvitationsHandler(this.networkManager, _runtimeParams?.invitationConnectionDefaultParams);
4579
4891
  this.invitationsManager = new InvitationsManager(this.invitations, (invitation) => this.getInvitationHandler(invitation), this.metadataStore);
4580
- this._handlerFactories.set(Invitation7.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => this.identityManager.identity ?? failUndefined2(), this._acceptIdentity.bind(this)));
4892
+ this._handlerFactories.set(Invitation8.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => this.identityManager.identity ?? failUndefined2(), this._acceptIdentity.bind(this)));
4581
4893
  }
4582
4894
  async _open(ctx) {
4583
4895
  await this._checkStorageVersion();
4584
- log12("opening...", void 0, {
4585
- F: __dxlog_file14,
4586
- L: 164,
4896
+ log14("opening...", void 0, {
4897
+ F: __dxlog_file16,
4898
+ L: 152,
4587
4899
  S: this,
4588
4900
  C: (f, a) => f(...a)
4589
4901
  });
4590
- log12.trace("dxos.sdk.service-context.open", trace8.begin({
4902
+ log14.trace("dxos.sdk.service-context.open", trace8.begin({
4591
4903
  id: this._instanceId
4592
4904
  }), {
4593
- F: __dxlog_file14,
4594
- L: 165,
4905
+ F: __dxlog_file16,
4906
+ L: 153,
4595
4907
  S: this,
4596
4908
  C: (f, a) => f(...a)
4597
4909
  });
4598
4910
  await this.signalManager.open();
4599
4911
  await this.networkManager.open();
4600
- await this.automergeHost.open();
4912
+ await this.echoHost.open(ctx);
4601
4913
  await this.metadataStore.load();
4602
4914
  await this.spaceManager.open();
4603
4915
  await this.identityManager.open(ctx);
@@ -4605,51 +4917,50 @@ var ServiceContext = class extends Resource {
4605
4917
  await this._initialize(ctx);
4606
4918
  }
4607
4919
  const loadedInvitations = await this.invitationsManager.loadPersistentInvitations();
4608
- log12("loaded persistent invitations", {
4920
+ log14("loaded persistent invitations", {
4609
4921
  count: loadedInvitations.invitations?.length
4610
4922
  }, {
4611
- F: __dxlog_file14,
4612
- L: 178,
4923
+ F: __dxlog_file16,
4924
+ L: 166,
4613
4925
  S: this,
4614
4926
  C: (f, a) => f(...a)
4615
4927
  });
4616
- log12.trace("dxos.sdk.service-context.open", trace8.end({
4928
+ log14.trace("dxos.sdk.service-context.open", trace8.end({
4617
4929
  id: this._instanceId
4618
4930
  }), {
4619
- F: __dxlog_file14,
4620
- L: 180,
4931
+ F: __dxlog_file16,
4932
+ L: 168,
4621
4933
  S: this,
4622
4934
  C: (f, a) => f(...a)
4623
4935
  });
4624
- log12("opened", void 0, {
4625
- F: __dxlog_file14,
4626
- L: 181,
4936
+ log14("opened", void 0, {
4937
+ F: __dxlog_file16,
4938
+ L: 169,
4627
4939
  S: this,
4628
4940
  C: (f, a) => f(...a)
4629
4941
  });
4630
4942
  }
4631
- async _close() {
4632
- log12("closing...", void 0, {
4633
- F: __dxlog_file14,
4634
- L: 185,
4943
+ async _close(ctx) {
4944
+ log14("closing...", void 0, {
4945
+ F: __dxlog_file16,
4946
+ L: 173,
4635
4947
  S: this,
4636
4948
  C: (f, a) => f(...a)
4637
4949
  });
4638
4950
  if (this._deviceSpaceSync && this.identityManager.identity) {
4639
4951
  await this.identityManager.identity.space.spaceState.removeCredentialProcessor(this._deviceSpaceSync);
4640
4952
  }
4641
- await this.automergeHost.close();
4642
4953
  await this.dataSpaceManager?.close();
4643
4954
  await this.identityManager.close();
4644
4955
  await this.spaceManager.close();
4645
4956
  await this.feedStore.close();
4957
+ await this.metadataStore.close();
4958
+ await this.echoHost.close(ctx);
4646
4959
  await this.networkManager.close();
4647
4960
  await this.signalManager.close();
4648
- await this.metadataStore.close();
4649
- await this.indexer.destroy();
4650
- log12("closed", void 0, {
4651
- F: __dxlog_file14,
4652
- L: 198,
4961
+ log14("closed", void 0, {
4962
+ F: __dxlog_file16,
4963
+ L: 185,
4653
4964
  S: this,
4654
4965
  C: (f, a) => f(...a)
4655
4966
  });
@@ -4661,9 +4972,9 @@ var ServiceContext = class extends Resource {
4661
4972
  }
4662
4973
  getInvitationHandler(invitation) {
4663
4974
  const factory = this._handlerFactories.get(invitation.kind);
4664
- invariant13(factory, `Unknown invitation kind: ${invitation.kind}`, {
4665
- F: __dxlog_file14,
4666
- L: 209,
4975
+ invariant15(factory, `Unknown invitation kind: ${invitation.kind}`, {
4976
+ F: __dxlog_file16,
4977
+ L: 196,
4667
4978
  S: this,
4668
4979
  A: [
4669
4980
  "factory",
@@ -4693,9 +5004,9 @@ var ServiceContext = class extends Resource {
4693
5004
  }
4694
5005
  // Called when identity is created.
4695
5006
  async _initialize(ctx) {
4696
- log12("initializing spaces...", void 0, {
4697
- F: __dxlog_file14,
4698
- L: 240,
5007
+ log14("initializing spaces...", void 0, {
5008
+ F: __dxlog_file16,
5009
+ L: 227,
4699
5010
  S: this,
4700
5011
  C: (f, a) => f(...a)
4701
5012
  });
@@ -4713,12 +5024,12 @@ var ServiceContext = class extends Resource {
4713
5024
  });
4714
5025
  }
4715
5026
  };
4716
- this.dataSpaceManager = new DataSpaceManager(this.spaceManager, this.metadataStore, this.keyring, signingContext, this.feedStore, this.automergeHost, this.invitationsManager, this._runtimeParams);
5027
+ this.dataSpaceManager = new DataSpaceManager(this.spaceManager, this.metadataStore, this.keyring, signingContext, this.feedStore, this.echoHost, this.invitationsManager, this._runtimeParams);
4717
5028
  await this.dataSpaceManager.open();
4718
- this._handlerFactories.set(Invitation7.Kind.SPACE, (invitation) => {
4719
- invariant13(this.dataSpaceManager, "dataSpaceManager not initialized yet", {
4720
- F: __dxlog_file14,
4721
- L: 265,
5029
+ this._handlerFactories.set(Invitation8.Kind.SPACE, (invitation) => {
5030
+ invariant15(this.dataSpaceManager, "dataSpaceManager not initialized yet", {
5031
+ F: __dxlog_file16,
5032
+ L: 252,
4722
5033
  S: this,
4723
5034
  A: [
4724
5035
  "this.dataSpaceManager",
@@ -4738,33 +5049,33 @@ var ServiceContext = class extends Resource {
4738
5049
  return;
4739
5050
  }
4740
5051
  if (!this.dataSpaceManager) {
4741
- log12("dataSpaceManager not initialized yet, ignoring space admission", {
5052
+ log14("dataSpaceManager not initialized yet, ignoring space admission", {
4742
5053
  details: assertion
4743
5054
  }, {
4744
- F: __dxlog_file14,
4745
- L: 281,
5055
+ F: __dxlog_file16,
5056
+ L: 268,
4746
5057
  S: this,
4747
5058
  C: (f, a) => f(...a)
4748
5059
  });
4749
5060
  return;
4750
5061
  }
4751
5062
  if (this.dataSpaceManager.spaces.has(assertion.spaceKey)) {
4752
- log12("space already exists, ignoring space admission", {
5063
+ log14("space already exists, ignoring space admission", {
4753
5064
  details: assertion
4754
5065
  }, {
4755
- F: __dxlog_file14,
4756
- L: 285,
5066
+ F: __dxlog_file16,
5067
+ L: 272,
4757
5068
  S: this,
4758
5069
  C: (f, a) => f(...a)
4759
5070
  });
4760
5071
  return;
4761
5072
  }
4762
5073
  try {
4763
- log12("accepting space recorded in halo", {
5074
+ log14("accepting space recorded in halo", {
4764
5075
  details: assertion
4765
5076
  }, {
4766
- F: __dxlog_file14,
4767
- L: 290,
5077
+ F: __dxlog_file16,
5078
+ L: 277,
4768
5079
  S: this,
4769
5080
  C: (f, a) => f(...a)
4770
5081
  });
@@ -4773,9 +5084,9 @@ var ServiceContext = class extends Resource {
4773
5084
  genesisFeedKey: assertion.genesisFeedKey
4774
5085
  });
4775
5086
  } catch (err) {
4776
- log12.catch(err, void 0, {
4777
- F: __dxlog_file14,
4778
- L: 296,
5087
+ log14.catch(err, void 0, {
5088
+ F: __dxlog_file16,
5089
+ L: 283,
4779
5090
  S: this,
4780
5091
  C: (f, a) => f(...a)
4781
5092
  });
@@ -4821,15 +5132,16 @@ var ServiceRegistry = class {
4821
5132
  };
4822
5133
 
4823
5134
  // packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts
5135
+ import { asyncTimeout as asyncTimeout2 } from "@dxos/async";
4824
5136
  import { getFirstStreamValue } from "@dxos/codec-protobuf";
4825
5137
  import { credentialTypeFilter } from "@dxos/credentials";
4826
- import { invariant as invariant14 } from "@dxos/invariant";
5138
+ import { invariant as invariant16 } from "@dxos/invariant";
4827
5139
  import { STORAGE_VERSION as STORAGE_VERSION2 } from "@dxos/protocols";
4828
- import { SpaceMember as SpaceMember4 } from "@dxos/protocols/proto/dxos/client/services";
5140
+ import { SpaceMember as SpaceMember6 } from "@dxos/protocols/proto/dxos/client/services";
4829
5141
  import { TRACE_PROCESSOR } from "@dxos/tracing";
4830
5142
 
4831
5143
  // packages/sdk/client-services/src/version.ts
4832
- var DXOS_VERSION = "0.5.1-next.2ad6c47";
5144
+ var DXOS_VERSION = "0.5.1-next.5a770e3";
4833
5145
 
4834
5146
  // packages/sdk/client-services/src/packlets/services/platform.ts
4835
5147
  import { Platform } from "@dxos/protocols/proto/dxos/client/services";
@@ -4862,7 +5174,7 @@ var getPlatform = () => {
4862
5174
  };
4863
5175
 
4864
5176
  // packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts
4865
- var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts";
5177
+ var __dxlog_file17 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts";
4866
5178
  var DEFAULT_TIMEOUT = 1e3;
4867
5179
  var createDiagnostics = async (clientServices, serviceContext, config) => {
4868
5180
  const diagnostics = {
@@ -4876,63 +5188,55 @@ var createDiagnostics = async (clientServices, serviceContext, config) => {
4876
5188
  },
4877
5189
  trace: TRACE_PROCESSOR.getDiagnostics()
4878
5190
  };
4879
- {
4880
- invariant14(clientServices.LoggingService, "SystemService is not available.", {
4881
- F: __dxlog_file15,
4882
- L: 108,
4883
- S: void 0,
4884
- A: [
4885
- "clientServices.LoggingService",
4886
- "'SystemService is not available.'"
4887
- ]
4888
- });
4889
- diagnostics.metrics = await getFirstStreamValue(clientServices.LoggingService.queryMetrics({}), {
4890
- timeout: DEFAULT_TIMEOUT
4891
- }).catch(() => void 0);
4892
- }
4893
- if (typeof navigator !== "undefined" && navigator.storage) {
4894
- const map = /* @__PURE__ */ new Map();
4895
- const dir = await navigator.storage.getDirectory();
4896
- for await (const filename of dir?.keys()) {
4897
- const idx = filename.indexOf("-", filename.indexOf("-") + 1);
4898
- if (idx === -1) {
4899
- continue;
5191
+ await Promise.all([
5192
+ (async () => {
5193
+ invariant16(clientServices.LoggingService, "SystemService is not available.", {
5194
+ F: __dxlog_file17,
5195
+ L: 110,
5196
+ S: void 0,
5197
+ A: [
5198
+ "clientServices.LoggingService",
5199
+ "'SystemService is not available.'"
5200
+ ]
5201
+ });
5202
+ diagnostics.metrics = await getFirstStreamValue(clientServices.LoggingService.queryMetrics({}), {
5203
+ timeout: DEFAULT_TIMEOUT
5204
+ }).catch(() => void 0);
5205
+ })(),
5206
+ (async () => {
5207
+ diagnostics.storage = await asyncTimeout2(getStorageDiagnostics(), DEFAULT_TIMEOUT).catch(() => void 0);
5208
+ })(),
5209
+ async () => {
5210
+ const identity = serviceContext.identityManager.identity;
5211
+ if (identity) {
5212
+ diagnostics.identity = {
5213
+ identityKey: identity.identityKey,
5214
+ spaceKey: identity.space.key,
5215
+ profile: identity.profileDocument
5216
+ };
5217
+ const { devices } = await getFirstStreamValue(clientServices.DevicesService.queryDevices(), {
5218
+ timeout: DEFAULT_TIMEOUT
5219
+ }).catch(() => void 0) ?? {};
5220
+ diagnostics.devices = devices;
5221
+ if (serviceContext.dataSpaceManager) {
5222
+ diagnostics.spaces = await Promise.all(Array.from(serviceContext.dataSpaceManager.spaces.values()).map((space) => getSpaceStats(space)) ?? []);
5223
+ }
5224
+ const { feeds = [] } = await getFirstStreamValue(clientServices.DevtoolsHost.subscribeToFeeds({}), {
5225
+ timeout: DEFAULT_TIMEOUT
5226
+ }).catch(() => void 0) ?? {};
5227
+ diagnostics.feeds = feeds.map(({ feedKey, bytes, length }) => ({
5228
+ feedKey,
5229
+ bytes,
5230
+ length
5231
+ }));
5232
+ const status = await getFirstStreamValue(clientServices.NetworkService.queryStatus(), {
5233
+ timeout: DEFAULT_TIMEOUT
5234
+ }).catch(() => void 0);
5235
+ diagnostics.networkStatus = status;
5236
+ diagnostics.swarms = serviceContext.networkManager.connectionLog?.swarms;
4900
5237
  }
4901
- map.set(filename.slice(0, idx), (map.get(filename.slice(0, idx)) ?? 0) + 1);
4902
- }
4903
- diagnostics.storage = Array.from(map.entries()).sort((a, b) => b[1] - a[1]).map(([file, count]) => ({
4904
- file,
4905
- count
4906
- }));
4907
- }
4908
- const identity = serviceContext.identityManager.identity;
4909
- if (identity) {
4910
- diagnostics.identity = {
4911
- identityKey: identity.identityKey,
4912
- spaceKey: identity.space.key,
4913
- profile: identity.profileDocument
4914
- };
4915
- const { devices } = await getFirstStreamValue(clientServices.DevicesService.queryDevices(), {
4916
- timeout: DEFAULT_TIMEOUT
4917
- }).catch(() => void 0) ?? {};
4918
- diagnostics.devices = devices;
4919
- if (serviceContext.dataSpaceManager) {
4920
- diagnostics.spaces = await Promise.all(Array.from(serviceContext.dataSpaceManager.spaces.values()).map((space) => getSpaceStats(space)) ?? []);
4921
5238
  }
4922
- const { feeds = [] } = await getFirstStreamValue(clientServices.DevtoolsHost.subscribeToFeeds({}), {
4923
- timeout: DEFAULT_TIMEOUT
4924
- }).catch(() => void 0) ?? {};
4925
- diagnostics.feeds = feeds.map(({ feedKey, bytes, length }) => ({
4926
- feedKey,
4927
- bytes,
4928
- length
4929
- }));
4930
- const status = await getFirstStreamValue(clientServices.NetworkService.queryStatus(), {
4931
- timeout: DEFAULT_TIMEOUT
4932
- }).catch(() => void 0);
4933
- diagnostics.networkStatus = status;
4934
- diagnostics.swarms = serviceContext.networkManager.connectionLog?.swarms;
4935
- }
5239
+ ]);
4936
5240
  diagnostics.config = config.values;
4937
5241
  return diagnostics;
4938
5242
  };
@@ -4951,7 +5255,7 @@ var getSpaceStats = async (space) => {
4951
5255
  displayName: member.assertion.profile?.displayName
4952
5256
  }
4953
5257
  },
4954
- presence: space.presence.getPeersOnline().filter(({ identityKey }) => identityKey.equals(member.key)).length > 0 ? SpaceMember4.PresenceState.ONLINE : SpaceMember4.PresenceState.OFFLINE
5258
+ presence: space.presence.getPeersOnline().filter(({ identityKey }) => identityKey.equals(member.key)).length > 0 ? SpaceMember6.PresenceState.ONLINE : SpaceMember6.PresenceState.OFFLINE
4955
5259
  })),
4956
5260
  pipeline: {
4957
5261
  // TODO(burdon): Pick properties from credentials if needed.
@@ -4969,11 +5273,29 @@ var getSpaceStats = async (space) => {
4969
5273
  }
4970
5274
  return stats;
4971
5275
  };
5276
+ var getStorageDiagnostics = async () => {
5277
+ if (typeof navigator === "undefined" || !navigator.storage) {
5278
+ return void 0;
5279
+ }
5280
+ const map = /* @__PURE__ */ new Map();
5281
+ const dir = await navigator.storage.getDirectory();
5282
+ for await (const filename of dir?.keys()) {
5283
+ const idx = filename.indexOf("-", filename.indexOf("-") + 1);
5284
+ if (idx === -1) {
5285
+ continue;
5286
+ }
5287
+ map.set(filename.slice(0, idx), (map.get(filename.slice(0, idx)) ?? 0) + 1);
5288
+ }
5289
+ return Array.from(map.entries()).sort((a, b) => b[1] - a[1]).map(([file, count]) => ({
5290
+ file,
5291
+ count
5292
+ }));
5293
+ };
4972
5294
 
4973
5295
  // packages/sdk/client-services/src/packlets/diagnostics/browser-diagnostics-broadcast.ts
4974
- import { Trigger as Trigger6 } from "@dxos/async";
4975
- import { log as log13 } from "@dxos/log";
4976
- var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/browser-diagnostics-broadcast.ts";
5296
+ import { Trigger as Trigger7 } from "@dxos/async";
5297
+ import { log as log15 } from "@dxos/log";
5298
+ var __dxlog_file18 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/browser-diagnostics-broadcast.ts";
4977
5299
  var CHANNEL_NAME = "dxos.diagnostics.broadcast";
4978
5300
  var MessageType;
4979
5301
  (function(MessageType2) {
@@ -4988,7 +5310,7 @@ var createCollectDiagnosticsBroadcastSender = () => {
4988
5310
  let expectedResponse = "probe-ack";
4989
5311
  let channel;
4990
5312
  try {
4991
- const trigger = new Trigger6();
5313
+ const trigger = new Trigger7();
4992
5314
  channel = new BroadcastChannel(CHANNEL_NAME);
4993
5315
  channel.onmessage = (msg) => {
4994
5316
  if (expectedResponse === msg.data.type) {
@@ -5041,8 +5363,8 @@ var createCollectDiagnosticsBroadcastHandler = (systemService) => {
5041
5363
  });
5042
5364
  }
5043
5365
  } catch (error) {
5044
- log13.catch(error, void 0, {
5045
- F: __dxlog_file16,
5366
+ log15.catch(error, void 0, {
5367
+ F: __dxlog_file18,
5046
5368
  L: 77,
5047
5369
  S: void 0,
5048
5370
  C: (f, a) => f(...a)
@@ -5073,12 +5395,11 @@ import { jsonKeyReplacer as jsonKeyReplacer2, nonNullable } from "@dxos/util";
5073
5395
  import { Event as Event9, synchronized as synchronized3 } from "@dxos/async";
5074
5396
  import { clientServiceBundle, defaultKey, Properties } from "@dxos/client-protocol";
5075
5397
  import { Context as Context11 } from "@dxos/context";
5076
- import { DataServiceImpl, encodeReference } from "@dxos/echo-pipeline";
5398
+ import { encodeReference } from "@dxos/echo-protocol";
5077
5399
  import { getTypeReference } from "@dxos/echo-schema";
5078
- import { QueryServiceImpl } from "@dxos/indexing";
5079
- import { invariant as invariant16 } from "@dxos/invariant";
5080
- import { PublicKey as PublicKey14 } from "@dxos/keys";
5081
- import { log as log16 } from "@dxos/log";
5400
+ import { invariant as invariant18 } from "@dxos/invariant";
5401
+ import { PublicKey as PublicKey15 } from "@dxos/keys";
5402
+ import { log as log18 } from "@dxos/log";
5082
5403
  import { WebsocketSignalManager } from "@dxos/messaging";
5083
5404
  import { NetworkManager, createSimplePeerTransportFactory } from "@dxos/network-manager";
5084
5405
  import { trace as trace9 } from "@dxos/protocols";
@@ -5090,9 +5411,9 @@ import { WebsocketRpcClient } from "@dxos/websocket-rpc";
5090
5411
  // packages/sdk/client-services/src/packlets/devices/devices-service.ts
5091
5412
  import { EventSubscriptions as EventSubscriptions3 } from "@dxos/async";
5092
5413
  import { Stream as Stream11 } from "@dxos/codec-protobuf";
5093
- import { invariant as invariant15 } from "@dxos/invariant";
5414
+ import { invariant as invariant17 } from "@dxos/invariant";
5094
5415
  import { Device as Device2, DeviceKind as DeviceKind2 } from "@dxos/protocols/proto/dxos/client/services";
5095
- var __dxlog_file17 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/devices/devices-service.ts";
5416
+ var __dxlog_file19 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/devices/devices-service.ts";
5096
5417
  var DevicesServiceImpl = class {
5097
5418
  constructor(_identityManager) {
5098
5419
  this._identityManager = _identityManager;
@@ -5109,8 +5430,8 @@ var DevicesServiceImpl = class {
5109
5430
  devices: []
5110
5431
  });
5111
5432
  } else {
5112
- invariant15(this._identityManager.identity?.presence, "presence not present", {
5113
- F: __dxlog_file17,
5433
+ invariant17(this._identityManager.identity?.presence, "presence not present", {
5434
+ F: __dxlog_file19,
5114
5435
  L: 32,
5115
5436
  S: this,
5116
5437
  A: [
@@ -5170,9 +5491,9 @@ var DevicesServiceImpl = class {
5170
5491
  };
5171
5492
 
5172
5493
  // packages/sdk/client-services/src/packlets/locks/browser.ts
5173
- import { asyncTimeout as asyncTimeout2, Trigger as Trigger7 } from "@dxos/async";
5494
+ import { asyncTimeout as asyncTimeout3, Trigger as Trigger8 } from "@dxos/async";
5174
5495
  import { RESOURCE_LOCK_TIMEOUT } from "@dxos/client-protocol";
5175
- import { log as log14, logInfo } from "@dxos/log";
5496
+ import { log as log16, logInfo } from "@dxos/log";
5176
5497
  function _ts_decorate7(decorators, target, key, desc) {
5177
5498
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5178
5499
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -5183,7 +5504,7 @@ function _ts_decorate7(decorators, target, key, desc) {
5183
5504
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5184
5505
  return c > 3 && r && Object.defineProperty(target, key, r), r;
5185
5506
  }
5186
- var __dxlog_file18 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/locks/browser.ts";
5507
+ var __dxlog_file20 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/locks/browser.ts";
5187
5508
  var Message;
5188
5509
  (function(Message2) {
5189
5510
  Message2["ACQUIRING"] = "acquiring";
@@ -5191,7 +5512,7 @@ var Message;
5191
5512
  var Lock = class {
5192
5513
  constructor({ lockKey, onAcquire, onRelease }) {
5193
5514
  this._broadcastChannel = new BroadcastChannel("vault-resource-lock");
5194
- this._releaseTrigger = new Trigger7();
5515
+ this._releaseTrigger = new Trigger8();
5195
5516
  this._lockKey = lockKey;
5196
5517
  this._onAcquire = onAcquire;
5197
5518
  this._onRelease = onRelease;
@@ -5205,29 +5526,29 @@ var Lock = class {
5205
5526
  message: "acquiring"
5206
5527
  });
5207
5528
  try {
5208
- log14("aquiring lock...", void 0, {
5209
- F: __dxlog_file18,
5529
+ log16("aquiring lock...", void 0, {
5530
+ F: __dxlog_file20,
5210
5531
  L: 42,
5211
5532
  S: this,
5212
5533
  C: (f, a) => f(...a)
5213
5534
  });
5214
- await asyncTimeout2(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
5215
- log14("acquired lock", void 0, {
5216
- F: __dxlog_file18,
5535
+ await asyncTimeout3(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
5536
+ log16("acquired lock", void 0, {
5537
+ F: __dxlog_file20,
5217
5538
  L: 44,
5218
5539
  S: this,
5219
5540
  C: (f, a) => f(...a)
5220
5541
  });
5221
5542
  } catch {
5222
- log14("stealing lock...", void 0, {
5223
- F: __dxlog_file18,
5543
+ log16("stealing lock...", void 0, {
5544
+ F: __dxlog_file20,
5224
5545
  L: 46,
5225
5546
  S: this,
5226
5547
  C: (f, a) => f(...a)
5227
5548
  });
5228
5549
  await this._requestLock(true);
5229
- log14("stolen lock", void 0, {
5230
- F: __dxlog_file18,
5550
+ log16("stolen lock", void 0, {
5551
+ F: __dxlog_file20,
5231
5552
  L: 48,
5232
5553
  S: this,
5233
5554
  C: (f, a) => f(...a)
@@ -5243,31 +5564,31 @@ var Lock = class {
5243
5564
  }
5244
5565
  }
5245
5566
  async _requestLock(steal = false) {
5246
- log14("requesting lock...", {
5567
+ log16("requesting lock...", {
5247
5568
  steal
5248
5569
  }, {
5249
- F: __dxlog_file18,
5570
+ F: __dxlog_file20,
5250
5571
  L: 63,
5251
5572
  S: this,
5252
5573
  C: (f, a) => f(...a)
5253
5574
  });
5254
- const acquired = new Trigger7();
5575
+ const acquired = new Trigger8();
5255
5576
  void navigator.locks.request(this._lockKey, {
5256
5577
  steal
5257
5578
  }, async () => {
5258
5579
  await this._onAcquire?.();
5259
5580
  acquired.wake();
5260
- this._releaseTrigger = new Trigger7();
5581
+ this._releaseTrigger = new Trigger8();
5261
5582
  await this._releaseTrigger.wait();
5262
- log14("releasing lock...", void 0, {
5263
- F: __dxlog_file18,
5583
+ log16("releasing lock...", void 0, {
5584
+ F: __dxlog_file20,
5264
5585
  L: 72,
5265
5586
  S: this,
5266
5587
  C: (f, a) => f(...a)
5267
5588
  });
5268
5589
  await this._onRelease?.();
5269
- log14("released lock", void 0, {
5270
- F: __dxlog_file18,
5590
+ log16("released lock", void 0, {
5591
+ F: __dxlog_file20,
5271
5592
  L: 74,
5272
5593
  S: this,
5273
5594
  C: (f, a) => f(...a)
@@ -5276,10 +5597,10 @@ var Lock = class {
5276
5597
  await this._onRelease?.();
5277
5598
  });
5278
5599
  await acquired.wait();
5279
- log14("recieved lock", {
5600
+ log16("recieved lock", {
5280
5601
  steal
5281
5602
  }, {
5282
- F: __dxlog_file18,
5603
+ F: __dxlog_file20,
5283
5604
  L: 81,
5284
5605
  S: this,
5285
5606
  C: (f, a) => f(...a)
@@ -5296,25 +5617,25 @@ var isLocked = (lockPath) => {
5296
5617
  // packages/sdk/client-services/src/packlets/logging/logging-service.ts
5297
5618
  import { Event as Event8 } from "@dxos/async";
5298
5619
  import { Stream as Stream12 } from "@dxos/codec-protobuf";
5299
- import { PublicKey as PublicKey12 } from "@dxos/keys";
5300
- import { getContextFromEntry, log as log15 } from "@dxos/log";
5620
+ import { PublicKey as PublicKey13 } from "@dxos/keys";
5621
+ import { getContextFromEntry, log as log17 } from "@dxos/log";
5301
5622
  import { QueryLogsRequest } from "@dxos/protocols/proto/dxos/client/services";
5302
5623
  import { getDebugName, jsonify, numericalValues, tracer } from "@dxos/util";
5303
5624
  var LoggingServiceImpl = class {
5304
5625
  constructor() {
5305
5626
  this._logs = new Event8();
5306
5627
  this._started = Date.now();
5307
- this._sessionId = PublicKey12.random().toHex();
5628
+ this._sessionId = PublicKey13.random().toHex();
5308
5629
  this._logProcessor = (_config, entry2) => {
5309
5630
  this._logs.emit(entry2);
5310
5631
  };
5311
5632
  }
5312
5633
  async open() {
5313
- log15.runtimeConfig.processors.push(this._logProcessor);
5634
+ log17.runtimeConfig.processors.push(this._logProcessor);
5314
5635
  }
5315
5636
  async close() {
5316
- const index = log15.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
5317
- log15.runtimeConfig.processors.splice(index, 1);
5637
+ const index = log17.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
5638
+ log17.runtimeConfig.processors.splice(index, 1);
5318
5639
  }
5319
5640
  async controlMetrics({ reset, record }) {
5320
5641
  if (reset) {
@@ -5513,13 +5834,13 @@ var toStorageType = (type) => {
5513
5834
  };
5514
5835
 
5515
5836
  // packages/sdk/client-services/src/packlets/storage/level.ts
5516
- import { Level } from "level";
5517
5837
  import path from "@dxos/node-std/path";
5518
- import { PublicKey as PublicKey13 } from "@dxos/keys";
5838
+ import { PublicKey as PublicKey14 } from "@dxos/keys";
5839
+ import { createLevel as createKV } from "@dxos/kv-store";
5519
5840
  var createLevel = async (config) => {
5520
5841
  const persistent = isPersistent(config);
5521
- const storagePath = persistent ? path.join(getRootPath(config), "level") : `/tmp/dxos-${PublicKey13.random().toHex()}`;
5522
- const level = new Level(storagePath);
5842
+ const storagePath = persistent ? path.join(getRootPath(config), "level") : `/tmp/dxos-${PublicKey14.random().toHex()}`;
5843
+ const level = createKV(storagePath);
5523
5844
  await level.open();
5524
5845
  return level;
5525
5846
  };
@@ -5592,7 +5913,7 @@ function _ts_decorate8(decorators, target, key, desc) {
5592
5913
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5593
5914
  return c > 3 && r && Object.defineProperty(target, key, r), r;
5594
5915
  }
5595
- var __dxlog_file19 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-host.ts";
5916
+ var __dxlog_file21 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-host.ts";
5596
5917
  var ClientServicesHost = class {
5597
5918
  constructor({
5598
5919
  config,
@@ -5680,25 +6001,25 @@ var ClientServicesHost = class {
5680
6001
  * Can only be called once.
5681
6002
  */
5682
6003
  initialize({ config, ...options }) {
5683
- invariant16(!this._open, "service host is open", {
5684
- F: __dxlog_file19,
5685
- L: 198,
6004
+ invariant18(!this._open, "service host is open", {
6005
+ F: __dxlog_file21,
6006
+ L: 189,
5686
6007
  S: this,
5687
6008
  A: [
5688
6009
  "!this._open",
5689
6010
  "'service host is open'"
5690
6011
  ]
5691
6012
  });
5692
- log16("initializing...", void 0, {
5693
- F: __dxlog_file19,
5694
- L: 199,
6013
+ log18("initializing...", void 0, {
6014
+ F: __dxlog_file21,
6015
+ L: 190,
5695
6016
  S: this,
5696
6017
  C: (f, a) => f(...a)
5697
6018
  });
5698
6019
  if (config) {
5699
- invariant16(!this._config, "config already set", {
5700
- F: __dxlog_file19,
5701
- L: 202,
6020
+ invariant18(!this._config, "config already set", {
6021
+ F: __dxlog_file21,
6022
+ L: 193,
5702
6023
  S: this,
5703
6024
  A: [
5704
6025
  "!this._config",
@@ -5711,9 +6032,9 @@ var ClientServicesHost = class {
5711
6032
  }
5712
6033
  }
5713
6034
  if (!options.signalManager) {
5714
- log16.warn("running signaling without telemetry metadata.", void 0, {
5715
- F: __dxlog_file19,
5716
- L: 210,
6035
+ log18.warn("running signaling without telemetry metadata.", void 0, {
6036
+ F: __dxlog_file21,
6037
+ L: 201,
5717
6038
  S: this,
5718
6039
  C: (f, a) => f(...a)
5719
6040
  });
@@ -5722,9 +6043,9 @@ var ClientServicesHost = class {
5722
6043
  iceServers: this._config?.get("runtime.services.ice")
5723
6044
  }), signalManager = new WebsocketSignalManager(this._config?.get("runtime.services.signaling") ?? []) } = options;
5724
6045
  this._signalManager = signalManager;
5725
- invariant16(!this._networkManager, "network manager already set", {
5726
- F: __dxlog_file19,
5727
- L: 221,
6046
+ invariant18(!this._networkManager, "network manager already set", {
6047
+ F: __dxlog_file21,
6048
+ L: 212,
5728
6049
  S: this,
5729
6050
  A: [
5730
6051
  "!this._networkManager",
@@ -5736,9 +6057,9 @@ var ClientServicesHost = class {
5736
6057
  transportFactory,
5737
6058
  signalManager
5738
6059
  });
5739
- log16("initialized", void 0, {
5740
- F: __dxlog_file19,
5741
- L: 228,
6060
+ log18("initialized", void 0, {
6061
+ F: __dxlog_file21,
6062
+ L: 219,
5742
6063
  S: this,
5743
6064
  C: (f, a) => f(...a)
5744
6065
  });
@@ -5747,45 +6068,45 @@ var ClientServicesHost = class {
5747
6068
  if (this._open) {
5748
6069
  return;
5749
6070
  }
5750
- const traceId = PublicKey14.random().toHex();
5751
- log16.trace("dxos.client-services.host.open", trace9.begin({
6071
+ const traceId = PublicKey15.random().toHex();
6072
+ log18.trace("dxos.client-services.host.open", trace9.begin({
5752
6073
  id: traceId
5753
6074
  }), {
5754
- F: __dxlog_file19,
5755
- L: 239,
6075
+ F: __dxlog_file21,
6076
+ L: 230,
5756
6077
  S: this,
5757
6078
  C: (f, a) => f(...a)
5758
6079
  });
5759
- invariant16(this._config, "config not set", {
5760
- F: __dxlog_file19,
5761
- L: 241,
6080
+ invariant18(this._config, "config not set", {
6081
+ F: __dxlog_file21,
6082
+ L: 232,
5762
6083
  S: this,
5763
6084
  A: [
5764
6085
  "this._config",
5765
6086
  "'config not set'"
5766
6087
  ]
5767
6088
  });
5768
- invariant16(this._storage, "storage not set", {
5769
- F: __dxlog_file19,
5770
- L: 242,
6089
+ invariant18(this._storage, "storage not set", {
6090
+ F: __dxlog_file21,
6091
+ L: 233,
5771
6092
  S: this,
5772
6093
  A: [
5773
6094
  "this._storage",
5774
6095
  "'storage not set'"
5775
6096
  ]
5776
6097
  });
5777
- invariant16(this._signalManager, "signal manager not set", {
5778
- F: __dxlog_file19,
5779
- L: 243,
6098
+ invariant18(this._signalManager, "signal manager not set", {
6099
+ F: __dxlog_file21,
6100
+ L: 234,
5780
6101
  S: this,
5781
6102
  A: [
5782
6103
  "this._signalManager",
5783
6104
  "'signal manager not set'"
5784
6105
  ]
5785
6106
  });
5786
- invariant16(this._networkManager, "network manager not set", {
5787
- F: __dxlog_file19,
5788
- L: 244,
6107
+ invariant18(this._networkManager, "network manager not set", {
6108
+ F: __dxlog_file21,
6109
+ L: 235,
5789
6110
  S: this,
5790
6111
  A: [
5791
6112
  "this._networkManager",
@@ -5793,26 +6114,21 @@ var ClientServicesHost = class {
5793
6114
  ]
5794
6115
  });
5795
6116
  this._opening = true;
5796
- log16("opening...", {
6117
+ log18("opening...", {
5797
6118
  lockKey: this._resourceLock?.lockKey
5798
6119
  }, {
5799
- F: __dxlog_file19,
5800
- L: 247,
6120
+ F: __dxlog_file21,
6121
+ L: 238,
5801
6122
  S: this,
5802
6123
  C: (f, a) => f(...a)
5803
6124
  });
6125
+ await this._resourceLock?.acquire();
5804
6126
  if (!this._level) {
5805
6127
  this._level = await createLevel(this._config.get("runtime.client.storage", {}));
5806
6128
  }
5807
6129
  await this._level.open();
5808
- await this._resourceLock?.acquire();
5809
6130
  await this._loggingService.open();
5810
6131
  this._serviceContext = new ServiceContext(this._storage, this._level, this._networkManager, this._signalManager, this._runtimeParams);
5811
- this._queryService = new QueryServiceImpl({
5812
- indexer: this._serviceContext.indexer,
5813
- automergeHost: this._serviceContext.automergeHost
5814
- });
5815
- await this._queryService.open(ctx);
5816
6132
  this._serviceRegistry.setServices({
5817
6133
  SystemService: this._systemService,
5818
6134
  IdentityService: new IdentityServiceImpl((params) => this._createIdentity(params), this._serviceContext.identityManager, this._serviceContext.keyring, (profile) => this._serviceContext.broadcastProfileUpdate(profile)),
@@ -5822,8 +6138,8 @@ var ClientServicesHost = class {
5822
6138
  await this._serviceContext.initialized.wait();
5823
6139
  return this._serviceContext.dataSpaceManager;
5824
6140
  }),
5825
- DataService: new DataServiceImpl(this._serviceContext.automergeHost),
5826
- QueryService: this._queryService,
6141
+ DataService: this._serviceContext.echoHost.dataService,
6142
+ QueryService: this._serviceContext.echoHost.queryService,
5827
6143
  NetworkService: new NetworkServiceImpl(this._serviceContext.networkManager, this._serviceContext.signalManager),
5828
6144
  LoggingService: this._loggingService,
5829
6145
  TracingService: this._tracingService,
@@ -5850,19 +6166,19 @@ var ClientServicesHost = class {
5850
6166
  this._open = true;
5851
6167
  this._statusUpdate.emit();
5852
6168
  const deviceKey = this._serviceContext.identityManager.identity?.deviceKey;
5853
- log16("opened", {
6169
+ log18("opened", {
5854
6170
  deviceKey
5855
6171
  }, {
5856
- F: __dxlog_file19,
5857
- L: 330,
6172
+ F: __dxlog_file21,
6173
+ L: 314,
5858
6174
  S: this,
5859
6175
  C: (f, a) => f(...a)
5860
6176
  });
5861
- log16.trace("dxos.client-services.host.open", trace9.end({
6177
+ log18.trace("dxos.client-services.host.open", trace9.end({
5862
6178
  id: traceId
5863
6179
  }), {
5864
- F: __dxlog_file19,
5865
- L: 331,
6180
+ F: __dxlog_file21,
6181
+ L: 315,
5866
6182
  S: this,
5867
6183
  C: (f, a) => f(...a)
5868
6184
  });
@@ -5872,11 +6188,11 @@ var ClientServicesHost = class {
5872
6188
  return;
5873
6189
  }
5874
6190
  const deviceKey = this._serviceContext.identityManager.identity?.deviceKey;
5875
- log16("closing...", {
6191
+ log18("closing...", {
5876
6192
  deviceKey
5877
6193
  }, {
5878
- F: __dxlog_file19,
5879
- L: 342,
6194
+ F: __dxlog_file21,
6195
+ L: 326,
5880
6196
  S: this,
5881
6197
  C: (f, a) => f(...a)
5882
6198
  });
@@ -5886,49 +6202,48 @@ var ClientServicesHost = class {
5886
6202
  SystemService: this._systemService
5887
6203
  });
5888
6204
  await this._loggingService.close();
5889
- await this._queryService.close();
5890
6205
  await this._serviceContext.close();
5891
6206
  await this._level?.close();
5892
6207
  this._open = false;
5893
6208
  this._statusUpdate.emit();
5894
- log16("closed", {
6209
+ log18("closed", {
5895
6210
  deviceKey
5896
6211
  }, {
5897
- F: __dxlog_file19,
5898
- L: 352,
6212
+ F: __dxlog_file21,
6213
+ L: 335,
5899
6214
  S: this,
5900
6215
  C: (f, a) => f(...a)
5901
6216
  });
5902
6217
  }
5903
6218
  async reset() {
5904
- const traceId = PublicKey14.random().toHex();
5905
- log16.trace("dxos.sdk.client-services-host.reset", trace9.begin({
6219
+ const traceId = PublicKey15.random().toHex();
6220
+ log18.trace("dxos.sdk.client-services-host.reset", trace9.begin({
5906
6221
  id: traceId
5907
6222
  }), {
5908
- F: __dxlog_file19,
5909
- L: 357,
6223
+ F: __dxlog_file21,
6224
+ L: 340,
5910
6225
  S: this,
5911
6226
  C: (f, a) => f(...a)
5912
6227
  });
5913
- log16("resetting...", void 0, {
5914
- F: __dxlog_file19,
5915
- L: 359,
6228
+ log18.info("resetting...", void 0, {
6229
+ F: __dxlog_file21,
6230
+ L: 342,
5916
6231
  S: this,
5917
6232
  C: (f, a) => f(...a)
5918
6233
  });
5919
6234
  await this._serviceContext?.close();
5920
6235
  await this._storage.reset();
5921
- log16("reset", void 0, {
5922
- F: __dxlog_file19,
5923
- L: 362,
6236
+ log18.info("reset", void 0, {
6237
+ F: __dxlog_file21,
6238
+ L: 345,
5924
6239
  S: this,
5925
6240
  C: (f, a) => f(...a)
5926
6241
  });
5927
- log16.trace("dxos.sdk.client-services-host.reset", trace9.end({
6242
+ log18.trace("dxos.sdk.client-services-host.reset", trace9.end({
5928
6243
  id: traceId
5929
6244
  }), {
5930
- F: __dxlog_file19,
5931
- L: 363,
6245
+ F: __dxlog_file21,
6246
+ L: 346,
5932
6247
  S: this,
5933
6248
  C: (f, a) => f(...a)
5934
6249
  });
@@ -5939,16 +6254,16 @@ var ClientServicesHost = class {
5939
6254
  await this._serviceContext.initialized.wait();
5940
6255
  const space = await this._serviceContext.dataSpaceManager.createSpace();
5941
6256
  const automergeIndex = space.automergeSpaceState.rootUrl;
5942
- invariant16(automergeIndex, void 0, {
5943
- F: __dxlog_file19,
5944
- L: 375,
6257
+ invariant18(automergeIndex, void 0, {
6258
+ F: __dxlog_file21,
6259
+ L: 358,
5945
6260
  S: this,
5946
6261
  A: [
5947
6262
  "automergeIndex",
5948
6263
  ""
5949
6264
  ]
5950
6265
  });
5951
- const document = await this._serviceContext.automergeHost.repo.find(automergeIndex);
6266
+ const document = await this._serviceContext.echoHost.automergeRepo.find(automergeIndex);
5952
6267
  await document.whenReady();
5953
6268
  const properties = {
5954
6269
  system: {
@@ -5961,14 +6276,14 @@ var ClientServicesHost = class {
5961
6276
  keys: []
5962
6277
  }
5963
6278
  };
5964
- const propertiesId = PublicKey14.random().toHex();
6279
+ const propertiesId = PublicKey15.random().toHex();
5965
6280
  document.change((doc) => {
5966
6281
  assignDeep2(doc, [
5967
6282
  "objects",
5968
6283
  propertiesId
5969
6284
  ], properties);
5970
6285
  });
5971
- await this._serviceContext.automergeHost.repo.flush();
6286
+ await this._serviceContext.echoHost.flush();
5972
6287
  return identity;
5973
6288
  }
5974
6289
  };
@@ -5991,11 +6306,12 @@ ClientServicesHost = _ts_decorate8([
5991
6306
  ], ClientServicesHost);
5992
6307
 
5993
6308
  // packages/sdk/client-services/src/packlets/services/util.ts
5994
- import { PublicKey as PublicKey15 } from "@dxos/keys";
6309
+ import { PublicKey as PublicKey16 } from "@dxos/keys";
5995
6310
  import { humanize } from "@dxos/util";
5996
6311
  var ClientServicesProviderResource = Symbol.for("dxos.resource.ClientServices");
5997
6312
 
5998
6313
  // packages/sdk/client-services/src/packlets/diagnostics/diagnostics-collector.ts
6314
+ var GET_DIAGNOSTICS_RPC_TIMEOUT = 1e4;
5999
6315
  var DiagnosticsCollector = class {
6000
6316
  static {
6001
6317
  this.broadcastSender = createCollectDiagnosticsBroadcastSender();
@@ -6003,6 +6319,8 @@ var DiagnosticsCollector = class {
6003
6319
  static async collect(config = findConfigs(), services = findSystemServiceProvider(), options = {}) {
6004
6320
  const serviceDiagnostics = await services?.services?.SystemService?.getDiagnostics({
6005
6321
  keys: options.humanize ? GetDiagnosticsRequest2.KEY_OPTION.HUMANIZE : options.truncate ? GetDiagnosticsRequest2.KEY_OPTION.TRUNCATE : void 0
6322
+ }, {
6323
+ timeout: GET_DIAGNOSTICS_RPC_TIMEOUT
6006
6324
  });
6007
6325
  const clientDiagnostics = {
6008
6326
  config,
@@ -6019,12 +6337,12 @@ var DiagnosticsCollector = class {
6019
6337
  }
6020
6338
  };
6021
6339
  var findSystemServiceProvider = () => {
6022
- const serviceProviders = TRACE_PROCESSOR3.findByAnnotation(ClientServicesProviderResource);
6340
+ const serviceProviders = TRACE_PROCESSOR3.findResourcesByAnnotation(ClientServicesProviderResource);
6023
6341
  const providerResource = serviceProviders.find((r) => r.instance.deref()?.services?.SystemService != null);
6024
6342
  return providerResource?.instance?.deref() ?? null;
6025
6343
  };
6026
6344
  var findConfigs = () => {
6027
- const configs = TRACE_PROCESSOR3.findByAnnotation(ConfigResource);
6345
+ const configs = TRACE_PROCESSOR3.findResourcesByAnnotation(ConfigResource);
6028
6346
  return configs.map((r) => r.instance.deref()).filter(nonNullable);
6029
6347
  };
6030
6348
 
@@ -6068,4 +6386,4 @@ export {
6068
6386
  ClientServicesHost,
6069
6387
  ClientServicesProviderResource
6070
6388
  };
6071
- //# sourceMappingURL=chunk-CMVTOS5E.mjs.map
6389
+ //# sourceMappingURL=chunk-MKFJAPSD.mjs.map