@dxos/client-services 0.4.10-main.fa5a270 → 0.4.10-main.fe71b4c

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 (98) hide show
  1. package/dist/lib/browser/{chunk-7PYX6UUA.mjs → chunk-7S34JE6M.mjs} +980 -656
  2. package/dist/lib/browser/chunk-7S34JE6M.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +60 -22
  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 +12 -5
  7. package/dist/lib/browser/packlets/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-4TX623I7.cjs → chunk-DQMGKBOV.cjs} +922 -685
  9. package/dist/lib/node/chunk-DQMGKBOV.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +96 -58
  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 +16 -9
  14. package/dist/lib/node/packlets/testing/index.cjs.map +3 -3
  15. package/dist/types/src/index.d.ts +1 -0
  16. package/dist/types/src/index.d.ts.map +1 -1
  17. package/dist/types/src/packlets/diagnostics/browser-diagnostics-broadcast.d.ts +5 -0
  18. package/dist/types/src/packlets/diagnostics/browser-diagnostics-broadcast.d.ts.map +1 -0
  19. package/dist/types/src/packlets/diagnostics/diagnostics-broadcast.d.ts +5 -0
  20. package/dist/types/src/packlets/diagnostics/diagnostics-broadcast.d.ts.map +1 -0
  21. package/dist/types/src/packlets/diagnostics/diagnostics-collector.d.ts +15 -0
  22. package/dist/types/src/packlets/diagnostics/diagnostics-collector.d.ts.map +1 -0
  23. package/dist/types/src/packlets/{services → diagnostics}/diagnostics.d.ts +1 -1
  24. package/dist/types/src/packlets/diagnostics/diagnostics.d.ts.map +1 -0
  25. package/dist/types/src/packlets/diagnostics/index.d.ts +4 -0
  26. package/dist/types/src/packlets/diagnostics/index.d.ts.map +1 -0
  27. package/dist/types/src/packlets/indexing/util.d.ts +3 -2
  28. package/dist/types/src/packlets/indexing/util.d.ts.map +1 -1
  29. package/dist/types/src/packlets/invitations/invitation-extension.d.ts +1 -0
  30. package/dist/types/src/packlets/invitations/invitation-extension.d.ts.map +1 -1
  31. package/dist/types/src/packlets/invitations/invitations-handler.d.ts +4 -2
  32. package/dist/types/src/packlets/invitations/invitations-handler.d.ts.map +1 -1
  33. package/dist/types/src/packlets/invitations/invitations-service.d.ts.map +1 -1
  34. package/dist/types/src/packlets/services/index.d.ts +1 -1
  35. package/dist/types/src/packlets/services/index.d.ts.map +1 -1
  36. package/dist/types/src/packlets/services/service-context.d.ts +7 -5
  37. package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
  38. package/dist/types/src/packlets/services/service-host.d.ts +5 -1
  39. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  40. package/dist/types/src/packlets/services/util.d.ts +1 -0
  41. package/dist/types/src/packlets/services/util.d.ts.map +1 -1
  42. package/dist/types/src/packlets/spaces/data-space.d.ts.map +1 -1
  43. package/dist/types/src/packlets/storage/index.d.ts +1 -0
  44. package/dist/types/src/packlets/storage/index.d.ts.map +1 -1
  45. package/dist/types/src/packlets/storage/level.d.ts +4 -0
  46. package/dist/types/src/packlets/storage/level.d.ts.map +1 -0
  47. package/dist/types/src/packlets/storage/storage.d.ts.map +1 -1
  48. package/dist/types/src/packlets/storage/util.d.ts +4 -0
  49. package/dist/types/src/packlets/storage/util.d.ts.map +1 -0
  50. package/dist/types/src/packlets/system/system-service.d.ts +1 -1
  51. package/dist/types/src/packlets/system/system-service.d.ts.map +1 -1
  52. package/dist/types/src/packlets/testing/test-builder.d.ts +4 -2
  53. package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
  54. package/dist/types/src/packlets/vault/shared-worker-connection.d.ts +5 -5
  55. package/dist/types/src/packlets/vault/shared-worker-connection.d.ts.map +1 -1
  56. package/dist/types/src/packlets/vault/worker-runtime.d.ts +2 -0
  57. package/dist/types/src/packlets/vault/worker-runtime.d.ts.map +1 -1
  58. package/dist/types/src/packlets/vault/worker-session.d.ts +2 -0
  59. package/dist/types/src/packlets/vault/worker-session.d.ts.map +1 -1
  60. package/dist/types/src/version.d.ts +1 -1
  61. package/package.json +36 -34
  62. package/src/index.ts +1 -0
  63. package/src/packlets/devices/devices-service.test.ts +1 -1
  64. package/src/packlets/diagnostics/browser-diagnostics-broadcast.ts +94 -0
  65. package/src/packlets/diagnostics/diagnostics-broadcast.ts +20 -0
  66. package/src/packlets/diagnostics/diagnostics-collector.ts +65 -0
  67. package/src/packlets/{services → diagnostics}/diagnostics.ts +2 -2
  68. package/src/packlets/diagnostics/index.ts +7 -0
  69. package/src/packlets/identity/identity-service.test.ts +1 -1
  70. package/src/packlets/indexing/util.ts +6 -6
  71. package/src/packlets/invitations/device-invitation-protocol.test.ts +1 -1
  72. package/src/packlets/invitations/invitation-extension.ts +28 -1
  73. package/src/packlets/invitations/invitations-handler.ts +73 -32
  74. package/src/packlets/invitations/invitations-service.ts +5 -5
  75. package/src/packlets/network/network-service.test.ts +1 -1
  76. package/src/packlets/services/automerge-host.test.ts +9 -3
  77. package/src/packlets/services/index.ts +1 -1
  78. package/src/packlets/services/service-context.test.ts +9 -6
  79. package/src/packlets/services/service-context.ts +18 -11
  80. package/src/packlets/services/service-host.ts +52 -9
  81. package/src/packlets/services/service-registry.test.ts +1 -1
  82. package/src/packlets/services/util.ts +2 -0
  83. package/src/packlets/spaces/data-space-manager.test.ts +4 -4
  84. package/src/packlets/spaces/data-space.ts +51 -2
  85. package/src/packlets/spaces/spaces-service.test.ts +1 -1
  86. package/src/packlets/storage/index.ts +1 -0
  87. package/src/packlets/storage/level.ts +19 -0
  88. package/src/packlets/storage/storage.ts +3 -9
  89. package/src/packlets/storage/util.ts +19 -0
  90. package/src/packlets/system/system-service.ts +1 -1
  91. package/src/packlets/testing/test-builder.ts +23 -5
  92. package/src/packlets/vault/shared-worker-connection.ts +3 -8
  93. package/src/packlets/vault/worker-runtime.ts +27 -2
  94. package/src/packlets/vault/worker-session.ts +6 -0
  95. package/src/version.ts +1 -1
  96. package/dist/lib/browser/chunk-7PYX6UUA.mjs.map +0 -7
  97. package/dist/lib/node/chunk-4TX623I7.cjs.map +0 -7
  98. package/dist/types/src/packlets/services/diagnostics.d.ts.map +0 -1
@@ -1282,6 +1282,7 @@ import { PushStream, scheduleTask as scheduleTask2, TimeoutError, Trigger as Tri
1282
1282
  import { AuthenticatingInvitation, AUTHENTICATION_CODE_LENGTH, CancellableInvitation, INVITATION_TIMEOUT } from "@dxos/client-protocol";
1283
1283
  import { Context as Context5 } from "@dxos/context";
1284
1284
  import { generatePasscode } from "@dxos/credentials";
1285
+ import { createKeyPair, sign } from "@dxos/crypto";
1285
1286
  import { invariant as invariant6 } from "@dxos/invariant";
1286
1287
  import { PublicKey as PublicKey6 } from "@dxos/keys";
1287
1288
  import { log as log5 } from "@dxos/log";
@@ -1293,6 +1294,7 @@ import { AuthenticationResponse as AuthenticationResponse2 } from "@dxos/protoco
1293
1294
  // packages/sdk/client-services/src/packlets/invitations/invitation-extension.ts
1294
1295
  import { Trigger as Trigger2 } from "@dxos/async";
1295
1296
  import { cancelWithContext, Context as Context4 } from "@dxos/context";
1297
+ import { randomBytes, verify } from "@dxos/crypto";
1296
1298
  import { invariant as invariant5 } from "@dxos/invariant";
1297
1299
  import { PublicKey as PublicKey5 } from "@dxos/keys";
1298
1300
  import { log as log4 } from "@dxos/log";
@@ -1316,6 +1318,7 @@ var InvitationHostExtension = class extends RpcExtension {
1316
1318
  this._callbacks = _callbacks;
1317
1319
  this._ctx = new Context4();
1318
1320
  this._remoteOptionsTrigger = new Trigger2();
1321
+ this._challenge = void 0;
1319
1322
  this.invitation = void 0;
1320
1323
  this.guestProfile = void 0;
1321
1324
  this.authenticationPassed = false;
@@ -1330,7 +1333,7 @@ var InvitationHostExtension = class extends RpcExtension {
1330
1333
  options: async (options) => {
1331
1334
  invariant5(!this._remoteOptions, "Remote options already set.", {
1332
1335
  F: __dxlog_file6,
1333
- L: 87,
1336
+ L: 90,
1334
1337
  S: this,
1335
1338
  A: [
1336
1339
  "!this._remoteOptions",
@@ -1347,7 +1350,7 @@ var InvitationHostExtension = class extends RpcExtension {
1347
1350
  id: traceId
1348
1351
  }), {
1349
1352
  F: __dxlog_file6,
1350
- L: 96,
1353
+ L: 99,
1351
1354
  S: this,
1352
1355
  C: (f, a) => f(...a)
1353
1356
  });
@@ -1357,7 +1360,7 @@ var InvitationHostExtension = class extends RpcExtension {
1357
1360
  invitationId
1358
1361
  }, {
1359
1362
  F: __dxlog_file6,
1360
- L: 100,
1363
+ L: 103,
1361
1364
  S: this,
1362
1365
  C: (f, a) => f(...a)
1363
1366
  });
@@ -1371,7 +1374,7 @@ var InvitationHostExtension = class extends RpcExtension {
1371
1374
  guestProfile: profile
1372
1375
  }, {
1373
1376
  F: __dxlog_file6,
1374
- L: 109,
1377
+ L: 112,
1375
1378
  S: this,
1376
1379
  C: (f, a) => f(...a)
1377
1380
  });
@@ -1380,25 +1383,27 @@ var InvitationHostExtension = class extends RpcExtension {
1380
1383
  ...this.invitation,
1381
1384
  state: Invitation2.State.READY_FOR_AUTHENTICATION
1382
1385
  });
1386
+ this._challenge = this.invitation.authMethod === Invitation2.AuthMethod.KNOWN_PUBLIC_KEY ? randomBytes(32) : void 0;
1383
1387
  log4.trace("dxos.sdk.invitation-handler.host.introduce", trace3.end({
1384
1388
  id: traceId
1385
1389
  }), {
1386
1390
  F: __dxlog_file6,
1387
- L: 116,
1391
+ L: 122,
1388
1392
  S: this,
1389
1393
  C: (f, a) => f(...a)
1390
1394
  });
1391
1395
  return {
1392
- authMethod: this.invitation.authMethod
1396
+ authMethod: this.invitation.authMethod,
1397
+ challenge: this._challenge
1393
1398
  };
1394
1399
  },
1395
- authenticate: async ({ authCode: code }) => {
1400
+ authenticate: async ({ authCode: code, signedChallenge }) => {
1396
1401
  const traceId = PublicKey5.random().toHex();
1397
1402
  log4.trace("dxos.sdk.invitation-handler.host.authenticate", trace3.begin({
1398
1403
  id: traceId
1399
1404
  }), {
1400
1405
  F: __dxlog_file6,
1401
- L: 124,
1406
+ L: 131,
1402
1407
  S: this,
1403
1408
  C: (f, a) => f(...a)
1404
1409
  });
@@ -1406,14 +1411,14 @@ var InvitationHostExtension = class extends RpcExtension {
1406
1411
  authCode: code
1407
1412
  }, {
1408
1413
  F: __dxlog_file6,
1409
- L: 125,
1414
+ L: 132,
1410
1415
  S: this,
1411
1416
  C: (f, a) => f(...a)
1412
1417
  });
1413
1418
  let status = AuthenticationResponse.Status.OK;
1414
1419
  invariant5(this.invitation, "Invitation is not set.", {
1415
1420
  F: __dxlog_file6,
1416
- L: 128,
1421
+ L: 135,
1417
1422
  S: this,
1418
1423
  A: [
1419
1424
  "this.invitation",
@@ -1424,7 +1429,7 @@ var InvitationHostExtension = class extends RpcExtension {
1424
1429
  case Invitation2.AuthMethod.NONE: {
1425
1430
  log4("authentication not required", void 0, {
1426
1431
  F: __dxlog_file6,
1427
- L: 131,
1432
+ L: 138,
1428
1433
  S: this,
1429
1434
  C: (f, a) => f(...a)
1430
1435
  });
@@ -1444,12 +1449,25 @@ var InvitationHostExtension = class extends RpcExtension {
1444
1449
  }
1445
1450
  break;
1446
1451
  }
1452
+ case Invitation2.AuthMethod.KNOWN_PUBLIC_KEY: {
1453
+ if (!this.invitation.guestKeypair) {
1454
+ status = AuthenticationResponse.Status.INTERNAL_ERROR;
1455
+ break;
1456
+ }
1457
+ const isSignatureValid = this._challenge && verify(this._challenge, Buffer.from(signedChallenge ?? []), this.invitation.guestKeypair.publicKey.asBuffer());
1458
+ if (isSignatureValid) {
1459
+ this.authenticationPassed = true;
1460
+ } else {
1461
+ status = AuthenticationResponse.Status.INVALID_SIGNATURE;
1462
+ }
1463
+ break;
1464
+ }
1447
1465
  default: {
1448
1466
  log4.error("invalid authentication method", {
1449
1467
  authMethod: this.invitation.authMethod
1450
1468
  }, {
1451
1469
  F: __dxlog_file6,
1452
- L: 149,
1470
+ L: 176,
1453
1471
  S: this,
1454
1472
  C: (f, a) => f(...a)
1455
1473
  });
@@ -1464,7 +1482,7 @@ var InvitationHostExtension = class extends RpcExtension {
1464
1482
  }
1465
1483
  }), {
1466
1484
  F: __dxlog_file6,
1467
- L: 155,
1485
+ L: 182,
1468
1486
  S: this,
1469
1487
  C: (f, a) => f(...a)
1470
1488
  });
@@ -1478,14 +1496,14 @@ var InvitationHostExtension = class extends RpcExtension {
1478
1496
  id: traceId
1479
1497
  }), {
1480
1498
  F: __dxlog_file6,
1481
- L: 161,
1499
+ L: 188,
1482
1500
  S: this,
1483
1501
  C: (f, a) => f(...a)
1484
1502
  });
1485
1503
  try {
1486
1504
  invariant5(this.invitation, "Invitation is not set.", {
1487
1505
  F: __dxlog_file6,
1488
- L: 164,
1506
+ L: 191,
1489
1507
  S: this,
1490
1508
  A: [
1491
1509
  "this.invitation",
@@ -1500,7 +1518,7 @@ var InvitationHostExtension = class extends RpcExtension {
1500
1518
  id: traceId
1501
1519
  }), {
1502
1520
  F: __dxlog_file6,
1503
- L: 172,
1521
+ L: 199,
1504
1522
  S: this,
1505
1523
  C: (f, a) => f(...a)
1506
1524
  });
@@ -1557,7 +1575,7 @@ var InvitationGuestExtension = class extends RpcExtension {
1557
1575
  options: async (options) => {
1558
1576
  invariant5(!this._remoteOptions, "Remote options already set.", {
1559
1577
  F: __dxlog_file6,
1560
- L: 239,
1578
+ L: 266,
1561
1579
  S: this,
1562
1580
  A: [
1563
1581
  "!this._remoteOptions",
@@ -1584,7 +1602,7 @@ var InvitationGuestExtension = class extends RpcExtension {
1584
1602
  try {
1585
1603
  log4("begin options", void 0, {
1586
1604
  F: __dxlog_file6,
1587
- L: 260,
1605
+ L: 287,
1588
1606
  S: this,
1589
1607
  C: (f, a) => f(...a)
1590
1608
  });
@@ -1596,7 +1614,7 @@ var InvitationGuestExtension = class extends RpcExtension {
1596
1614
  }));
1597
1615
  log4("end options", void 0, {
1598
1616
  F: __dxlog_file6,
1599
- L: 263,
1617
+ L: 290,
1600
1618
  S: this,
1601
1619
  C: (f, a) => f(...a)
1602
1620
  });
@@ -1610,7 +1628,7 @@ var InvitationGuestExtension = class extends RpcExtension {
1610
1628
  } catch (err) {
1611
1629
  log4("openError", err, {
1612
1630
  F: __dxlog_file6,
1613
- L: 273,
1631
+ L: 300,
1614
1632
  S: this,
1615
1633
  C: (f, a) => f(...a)
1616
1634
  });
@@ -1620,7 +1638,7 @@ var InvitationGuestExtension = class extends RpcExtension {
1620
1638
  async onClose() {
1621
1639
  log4("onClose", void 0, {
1622
1640
  F: __dxlog_file6,
1623
- L: 279,
1641
+ L: 306,
1624
1642
  S: this,
1625
1643
  C: (f, a) => f(...a)
1626
1644
  });
@@ -1639,11 +1657,11 @@ var InvitationsHandler = class {
1639
1657
  this._networkManager = _networkManager;
1640
1658
  }
1641
1659
  createInvitation(protocol, options) {
1642
- const { invitationId = PublicKey6.random().toHex(), type = Invitation3.Type.INTERACTIVE, authMethod = Invitation3.AuthMethod.SHARED_SECRET, state = Invitation3.State.INIT, timeout = INVITATION_TIMEOUT, swarmKey = PublicKey6.random(), persistent = true, created = /* @__PURE__ */ new Date(), lifetime = 86400 } = options ?? {};
1660
+ const { invitationId = PublicKey6.random().toHex(), type = Invitation3.Type.INTERACTIVE, authMethod = Invitation3.AuthMethod.SHARED_SECRET, state = Invitation3.State.INIT, timeout = INVITATION_TIMEOUT, swarmKey = PublicKey6.random(), persistent = options?.authMethod !== Invitation3.AuthMethod.KNOWN_PUBLIC_KEY, created = /* @__PURE__ */ new Date(), guestKeypair = void 0, lifetime = 86400, multiUse = false } = options ?? {};
1643
1661
  const authCode = options?.authCode ?? (authMethod === Invitation3.AuthMethod.SHARED_SECRET ? generatePasscode(AUTHENTICATION_CODE_LENGTH) : void 0);
1644
1662
  invariant6(protocol, void 0, {
1645
1663
  F: __dxlog_file7,
1646
- L: 84,
1664
+ L: 87,
1647
1665
  S: this,
1648
1666
  A: [
1649
1667
  "protocol",
@@ -1658,9 +1676,11 @@ var InvitationsHandler = class {
1658
1676
  swarmKey,
1659
1677
  authCode,
1660
1678
  timeout,
1661
- persistent,
1679
+ persistent: persistent && type !== Invitation3.Type.DELEGATED,
1680
+ guestKeypair: guestKeypair ?? (authMethod === Invitation3.AuthMethod.KNOWN_PUBLIC_KEY ? createAdmissionKeypair() : void 0),
1662
1681
  created,
1663
1682
  lifetime,
1683
+ multiUse,
1664
1684
  ...protocol.getInvitationContext()
1665
1685
  };
1666
1686
  const stream = new PushStream();
@@ -1675,7 +1695,7 @@ var InvitationsHandler = class {
1675
1695
  ...protocol.toJSON()
1676
1696
  }, {
1677
1697
  F: __dxlog_file7,
1678
- L: 109,
1698
+ L: 115,
1679
1699
  S: this,
1680
1700
  C: (f, a) => f(...a)
1681
1701
  });
@@ -1700,7 +1720,7 @@ var InvitationsHandler = class {
1700
1720
  const deviceKey = admissionRequest.device?.deviceKey ?? admissionRequest.space?.deviceKey;
1701
1721
  invariant6(deviceKey, void 0, {
1702
1722
  F: __dxlog_file7,
1703
- L: 130,
1723
+ L: 136,
1704
1724
  S: this,
1705
1725
  A: [
1706
1726
  "deviceKey",
@@ -1723,7 +1743,7 @@ var InvitationsHandler = class {
1723
1743
  id: traceId
1724
1744
  }), {
1725
1745
  F: __dxlog_file7,
1726
- L: 148,
1746
+ L: 154,
1727
1747
  S: this,
1728
1748
  C: (f, a) => f(...a)
1729
1749
  });
@@ -1731,7 +1751,7 @@ var InvitationsHandler = class {
1731
1751
  ...protocol.toJSON()
1732
1752
  }, {
1733
1753
  F: __dxlog_file7,
1734
- L: 149,
1754
+ L: 155,
1735
1755
  S: this,
1736
1756
  C: (f, a) => f(...a)
1737
1757
  });
@@ -1747,7 +1767,7 @@ var InvitationsHandler = class {
1747
1767
  ...protocol.toJSON()
1748
1768
  }, {
1749
1769
  F: __dxlog_file7,
1750
- L: 152,
1770
+ L: 158,
1751
1771
  S: this,
1752
1772
  C: (f, a) => f(...a)
1753
1773
  });
@@ -1759,7 +1779,7 @@ var InvitationsHandler = class {
1759
1779
  id: traceId
1760
1780
  }), {
1761
1781
  F: __dxlog_file7,
1762
- L: 154,
1782
+ L: 160,
1763
1783
  S: this,
1764
1784
  C: (f, a) => f(...a)
1765
1785
  });
@@ -1769,7 +1789,7 @@ var InvitationsHandler = class {
1769
1789
  ...protocol.toJSON()
1770
1790
  }, {
1771
1791
  F: __dxlog_file7,
1772
- L: 157,
1792
+ L: 163,
1773
1793
  S: this,
1774
1794
  C: (f, a) => f(...a)
1775
1795
  });
@@ -1780,7 +1800,7 @@ var InvitationsHandler = class {
1780
1800
  } else {
1781
1801
  log5.error("failed", err, {
1782
1802
  F: __dxlog_file7,
1783
- L: 160,
1803
+ L: 166,
1784
1804
  S: this,
1785
1805
  C: (f, a) => f(...a)
1786
1806
  });
@@ -1791,12 +1811,12 @@ var InvitationsHandler = class {
1791
1811
  error: err
1792
1812
  }), {
1793
1813
  F: __dxlog_file7,
1794
- L: 163,
1814
+ L: 169,
1795
1815
  S: this,
1796
1816
  C: (f, a) => f(...a)
1797
1817
  });
1798
1818
  } finally {
1799
- if (type !== Invitation3.Type.MULTIUSE) {
1819
+ if (!multiUse) {
1800
1820
  await swarmConnection.close();
1801
1821
  await ctx.dispose();
1802
1822
  }
@@ -1812,7 +1832,7 @@ var InvitationsHandler = class {
1812
1832
  ...protocol.toJSON()
1813
1833
  }, {
1814
1834
  F: __dxlog_file7,
1815
- L: 178,
1835
+ L: 184,
1816
1836
  S: this,
1817
1837
  C: (f, a) => f(...a)
1818
1838
  });
@@ -1823,7 +1843,7 @@ var InvitationsHandler = class {
1823
1843
  } else {
1824
1844
  log5.error("failed", err, {
1825
1845
  F: __dxlog_file7,
1826
- L: 181,
1846
+ L: 187,
1827
1847
  S: this,
1828
1848
  C: (f, a) => f(...a)
1829
1849
  });
@@ -1837,7 +1857,7 @@ var InvitationsHandler = class {
1837
1857
  if (invitation.created.getTime() + invitation.lifetime * 1e3 < Date.now()) {
1838
1858
  log5.warn("invitation has already expired", void 0, {
1839
1859
  F: __dxlog_file7,
1840
- L: 192,
1860
+ L: 198,
1841
1861
  S: this,
1842
1862
  C: (f, a) => f(...a)
1843
1863
  });
@@ -1888,7 +1908,7 @@ var InvitationsHandler = class {
1888
1908
  const { timeout = INVITATION_TIMEOUT } = invitation;
1889
1909
  invariant6(protocol, void 0, {
1890
1910
  F: __dxlog_file7,
1891
- L: 246,
1911
+ L: 252,
1892
1912
  S: this,
1893
1913
  A: [
1894
1914
  "protocol",
@@ -1898,7 +1918,7 @@ var InvitationsHandler = class {
1898
1918
  if (deviceProfile) {
1899
1919
  invariant6(invitation.kind === Invitation3.Kind.DEVICE, "deviceProfile provided for non-device invitation", {
1900
1920
  F: __dxlog_file7,
1901
- L: 250,
1921
+ L: 256,
1902
1922
  S: this,
1903
1923
  A: [
1904
1924
  "invitation.kind === Invitation.Kind.DEVICE",
@@ -1913,7 +1933,7 @@ var InvitationsHandler = class {
1913
1933
  const setState = (newData) => {
1914
1934
  invariant6(newData.state !== void 0, void 0, {
1915
1935
  F: __dxlog_file7,
1916
- L: 261,
1936
+ L: 267,
1917
1937
  S: this,
1918
1938
  A: [
1919
1939
  "newData.state !== undefined",
@@ -1933,7 +1953,7 @@ var InvitationsHandler = class {
1933
1953
  ...protocol.toJSON()
1934
1954
  }, {
1935
1955
  F: __dxlog_file7,
1936
- L: 269,
1956
+ L: 275,
1937
1957
  S: this,
1938
1958
  C: (f, a) => f(...a)
1939
1959
  });
@@ -1943,7 +1963,7 @@ var InvitationsHandler = class {
1943
1963
  } else {
1944
1964
  log5.warn("auth failed", err, {
1945
1965
  F: __dxlog_file7,
1946
- L: 272,
1966
+ L: 278,
1947
1967
  S: this,
1948
1968
  C: (f, a) => f(...a)
1949
1969
  });
@@ -1957,7 +1977,7 @@ var InvitationsHandler = class {
1957
1977
  ...protocol.toJSON()
1958
1978
  }, {
1959
1979
  F: __dxlog_file7,
1960
- L: 280,
1980
+ L: 286,
1961
1981
  S: this,
1962
1982
  C: (f, a) => f(...a)
1963
1983
  });
@@ -1972,7 +1992,7 @@ var InvitationsHandler = class {
1972
1992
  currentState
1973
1993
  }, {
1974
1994
  F: __dxlog_file7,
1975
- L: 290,
1995
+ L: 296,
1976
1996
  S: this,
1977
1997
  C: (f, a) => f(...a)
1978
1998
  });
@@ -1987,7 +2007,7 @@ var InvitationsHandler = class {
1987
2007
  id: traceId
1988
2008
  }), {
1989
2009
  F: __dxlog_file7,
1990
- L: 299,
2010
+ L: 305,
1991
2011
  S: this,
1992
2012
  C: (f, a) => f(...a)
1993
2013
  });
@@ -1999,7 +2019,7 @@ var InvitationsHandler = class {
1999
2019
  ...protocol.toJSON()
2000
2020
  }, {
2001
2021
  F: __dxlog_file7,
2002
- L: 307,
2022
+ L: 313,
2003
2023
  S: this,
2004
2024
  C: (f, a) => f(...a)
2005
2025
  });
@@ -2010,7 +2030,7 @@ var InvitationsHandler = class {
2010
2030
  ...protocol.toJSON()
2011
2031
  }, {
2012
2032
  F: __dxlog_file7,
2013
- L: 311,
2033
+ L: 317,
2014
2034
  S: this,
2015
2035
  C: (f, a) => f(...a)
2016
2036
  });
@@ -2020,62 +2040,28 @@ var InvitationsHandler = class {
2020
2040
  response: introductionResponse
2021
2041
  }, {
2022
2042
  F: __dxlog_file7,
2023
- L: 315,
2043
+ L: 321,
2024
2044
  S: this,
2025
2045
  C: (f, a) => f(...a)
2026
2046
  });
2027
2047
  invitation.authMethod = introductionResponse.authMethod;
2028
2048
  if (isAuthenticationRequired(invitation)) {
2029
- for (let attempt = 1; attempt <= MAX_OTP_ATTEMPTS; attempt++) {
2030
- log5("guest waiting for authentication code...", void 0, {
2031
- F: __dxlog_file7,
2032
- L: 321,
2033
- S: this,
2034
- C: (f, a) => f(...a)
2035
- });
2036
- setState({
2037
- state: Invitation3.State.READY_FOR_AUTHENTICATION
2038
- });
2039
- const authCode = await authenticated.wait({
2040
- timeout
2041
- });
2042
- log5("sending authentication request", void 0, {
2043
- F: __dxlog_file7,
2044
- L: 325,
2045
- S: this,
2046
- C: (f, a) => f(...a)
2047
- });
2048
- setState({
2049
- state: Invitation3.State.AUTHENTICATING
2050
- });
2051
- const response = await extension.rpc.InvitationHostService.authenticate({
2052
- authCode
2053
- });
2054
- if (response.status === void 0 || response.status === AuthenticationResponse2.Status.OK) {
2049
+ switch (invitation.authMethod) {
2050
+ case Invitation3.AuthMethod.SHARED_SECRET:
2051
+ await this._handleGuestOtpAuth(extension, setState, authenticated, {
2052
+ timeout
2053
+ });
2054
+ break;
2055
+ case Invitation3.AuthMethod.KNOWN_PUBLIC_KEY:
2056
+ await this._handleGuestKpkAuth(extension, setState, invitation, introductionResponse);
2055
2057
  break;
2056
- }
2057
- if (response.status === AuthenticationResponse2.Status.INVALID_OTP) {
2058
- if (attempt === MAX_OTP_ATTEMPTS) {
2059
- throw new Error(`Maximum retry attempts: ${MAX_OTP_ATTEMPTS}`);
2060
- } else {
2061
- log5("retrying invalid code", {
2062
- attempt
2063
- }, {
2064
- F: __dxlog_file7,
2065
- L: 336,
2066
- S: this,
2067
- C: (f, a) => f(...a)
2068
- });
2069
- authenticated.reset();
2070
- }
2071
- }
2072
2058
  }
2073
2059
  }
2074
2060
  log5("request admission", {
2075
2061
  ...protocol.toJSON()
2076
2062
  }, {
2077
2063
  F: __dxlog_file7,
2078
- L: 344,
2064
+ L: 337,
2079
2065
  S: this,
2080
2066
  C: (f, a) => f(...a)
2081
2067
  });
@@ -2087,7 +2073,7 @@ var InvitationsHandler = class {
2087
2073
  ...protocol.toJSON()
2088
2074
  }, {
2089
2075
  F: __dxlog_file7,
2090
- L: 355,
2076
+ L: 348,
2091
2077
  S: this,
2092
2078
  C: (f, a) => f(...a)
2093
2079
  });
@@ -2100,7 +2086,7 @@ var InvitationsHandler = class {
2100
2086
  id: traceId
2101
2087
  }), {
2102
2088
  F: __dxlog_file7,
2103
- L: 357,
2089
+ L: 350,
2104
2090
  S: this,
2105
2091
  C: (f, a) => f(...a)
2106
2092
  });
@@ -2110,7 +2096,7 @@ var InvitationsHandler = class {
2110
2096
  ...protocol.toJSON()
2111
2097
  }, {
2112
2098
  F: __dxlog_file7,
2113
- L: 360,
2099
+ L: 353,
2114
2100
  S: this,
2115
2101
  C: (f, a) => f(...a)
2116
2102
  });
@@ -2120,7 +2106,7 @@ var InvitationsHandler = class {
2120
2106
  } else {
2121
2107
  log5("auth failed", err, {
2122
2108
  F: __dxlog_file7,
2123
- L: 363,
2109
+ L: 356,
2124
2110
  S: this,
2125
2111
  C: (f, a) => f(...a)
2126
2112
  });
@@ -2131,7 +2117,7 @@ var InvitationsHandler = class {
2131
2117
  error: err
2132
2118
  }), {
2133
2119
  F: __dxlog_file7,
2134
- L: 366,
2120
+ L: 359,
2135
2121
  S: this,
2136
2122
  C: (f, a) => f(...a)
2137
2123
  });
@@ -2149,7 +2135,7 @@ var InvitationsHandler = class {
2149
2135
  ...protocol.toJSON()
2150
2136
  }, {
2151
2137
  F: __dxlog_file7,
2152
- L: 377,
2138
+ L: 370,
2153
2139
  S: this,
2154
2140
  C: (f, a) => f(...a)
2155
2141
  });
@@ -2159,7 +2145,7 @@ var InvitationsHandler = class {
2159
2145
  } else {
2160
2146
  log5("auth failed", err, {
2161
2147
  F: __dxlog_file7,
2162
- L: 380,
2148
+ L: 373,
2163
2149
  S: this,
2164
2150
  C: (f, a) => f(...a)
2165
2151
  });
@@ -2176,7 +2162,7 @@ var InvitationsHandler = class {
2176
2162
  } else {
2177
2163
  invariant6(invitation.swarmKey, void 0, {
2178
2164
  F: __dxlog_file7,
2179
- L: 394,
2165
+ L: 387,
2180
2166
  S: this,
2181
2167
  A: [
2182
2168
  "invitation.swarmKey",
@@ -2214,15 +2200,88 @@ var InvitationsHandler = class {
2214
2200
  });
2215
2201
  return observable;
2216
2202
  }
2203
+ async _handleGuestOtpAuth(extension, setState, authenticated, options) {
2204
+ for (let attempt = 1; attempt <= MAX_OTP_ATTEMPTS; attempt++) {
2205
+ log5("guest waiting for authentication code...", void 0, {
2206
+ F: __dxlog_file7,
2207
+ L: 427,
2208
+ S: this,
2209
+ C: (f, a) => f(...a)
2210
+ });
2211
+ setState({
2212
+ state: Invitation3.State.READY_FOR_AUTHENTICATION
2213
+ });
2214
+ const authCode = await authenticated.wait(options);
2215
+ log5("sending authentication request", void 0, {
2216
+ F: __dxlog_file7,
2217
+ L: 431,
2218
+ S: this,
2219
+ C: (f, a) => f(...a)
2220
+ });
2221
+ setState({
2222
+ state: Invitation3.State.AUTHENTICATING
2223
+ });
2224
+ const response = await extension.rpc.InvitationHostService.authenticate({
2225
+ authCode
2226
+ });
2227
+ if (response.status === void 0 || response.status === AuthenticationResponse2.Status.OK) {
2228
+ break;
2229
+ }
2230
+ if (response.status === AuthenticationResponse2.Status.INVALID_OTP) {
2231
+ if (attempt === MAX_OTP_ATTEMPTS) {
2232
+ throw new Error(`Maximum retry attempts: ${MAX_OTP_ATTEMPTS}`);
2233
+ } else {
2234
+ log5("retrying invalid code", {
2235
+ attempt
2236
+ }, {
2237
+ F: __dxlog_file7,
2238
+ L: 442,
2239
+ S: this,
2240
+ C: (f, a) => f(...a)
2241
+ });
2242
+ authenticated.reset();
2243
+ }
2244
+ }
2245
+ }
2246
+ }
2247
+ async _handleGuestKpkAuth(extension, setState, invitation, introductionResponse) {
2248
+ if (invitation.guestKeypair?.privateKey == null) {
2249
+ throw new Error("keypair missing in the invitation");
2250
+ }
2251
+ if (introductionResponse.challenge == null) {
2252
+ throw new Error("challenge missing in the introduction");
2253
+ }
2254
+ log5("sending authentication request", void 0, {
2255
+ F: __dxlog_file7,
2256
+ L: 461,
2257
+ S: this,
2258
+ C: (f, a) => f(...a)
2259
+ });
2260
+ setState({
2261
+ state: Invitation3.State.AUTHENTICATING
2262
+ });
2263
+ const signature = sign(Buffer.from(introductionResponse.challenge), invitation.guestKeypair.privateKey);
2264
+ const response = await extension.rpc.InvitationHostService.authenticate({
2265
+ signedChallenge: signature
2266
+ });
2267
+ if (response.status !== AuthenticationResponse2.Status.OK) {
2268
+ throw new Error(`Authentication failed with code: ${response.status}`);
2269
+ }
2270
+ }
2217
2271
  };
2218
- var invitationExpired = (invitation) => {
2219
- return invitation.created && invitation.lifetime && invitation.lifetime !== 0 && invitation.created.getTime() + invitation.lifetime * 1e3 < Date.now();
2272
+ var createAdmissionKeypair = () => {
2273
+ const keypair = createKeyPair();
2274
+ return {
2275
+ publicKey: PublicKey6.from(keypair.publicKey),
2276
+ privateKey: keypair.secretKey
2277
+ };
2220
2278
  };
2221
2279
 
2222
2280
  // packages/sdk/client-services/src/packlets/invitations/invitations-service.ts
2223
2281
  import { Event as Event3, scheduleTask as scheduleTask3 } from "@dxos/async";
2224
2282
  import { Stream as Stream8 } from "@dxos/codec-protobuf";
2225
2283
  import { Context as Context6 } from "@dxos/context";
2284
+ import { hasInvitationExpired } from "@dxos/echo-pipeline";
2226
2285
  import { invariant as invariant7 } from "@dxos/invariant";
2227
2286
  import { log as log6 } from "@dxos/log";
2228
2287
  import { Invitation as Invitation4, QueryInvitationsResponse } from "@dxos/protocols/proto/dxos/client/services";
@@ -2281,7 +2340,7 @@ var InvitationsServiceImpl = class {
2281
2340
  await this._metadataStore.removeInvitation(invitation.get().invitationId);
2282
2341
  }
2283
2342
  this._createInvitations.delete(invitation.get().invitationId);
2284
- if (invitation.get().type !== Invitation4.Type.MULTIUSE) {
2343
+ if (!invitation.get().multiUse) {
2285
2344
  this._removedCreated.emit(invitation.get());
2286
2345
  }
2287
2346
  });
@@ -2289,7 +2348,7 @@ var InvitationsServiceImpl = class {
2289
2348
  }
2290
2349
  async loadPersistentInvitations() {
2291
2350
  const persistentInvitations = this._metadataStore.getInvitations();
2292
- const freshInvitations = persistentInvitations.filter(async (invitation) => !invitationExpired(invitation));
2351
+ const freshInvitations = persistentInvitations.filter(async (invitation) => !hasInvitationExpired(invitation));
2293
2352
  const cInvitations = freshInvitations.map((persistentInvitation) => {
2294
2353
  invariant7(!this._createInvitations.get(persistentInvitation.invitationId), "invitation already exists", {
2295
2354
  F: __dxlog_file8,
@@ -2342,7 +2401,7 @@ var InvitationsServiceImpl = class {
2342
2401
  }, () => {
2343
2402
  close();
2344
2403
  this._acceptInvitations.delete(invitation.get().invitationId);
2345
- if (invitation.get().type !== Invitation4.Type.MULTIUSE) {
2404
+ if (!invitation.get().multiUse) {
2346
2405
  this._removedAccepted.emit(invitation.get());
2347
2406
  }
2348
2407
  });
@@ -2730,163 +2789,15 @@ ClientRpcServer = _ts_decorate3([
2730
2789
  trace5.resource()
2731
2790
  ], ClientRpcServer);
2732
2791
 
2733
- // packages/sdk/client-services/src/packlets/services/diagnostics.ts
2734
- import { getFirstStreamValue } from "@dxos/codec-protobuf";
2735
- import { credentialTypeFilter } from "@dxos/credentials";
2736
- import { invariant as invariant9 } from "@dxos/invariant";
2737
- import { STORAGE_VERSION } from "@dxos/protocols";
2738
- import { SpaceMember } from "@dxos/protocols/proto/dxos/client/services";
2739
- import { TRACE_PROCESSOR } from "@dxos/tracing";
2740
-
2741
- // packages/sdk/client-services/src/packlets/services/platform.ts
2742
- import { Platform } from "@dxos/protocols/proto/dxos/client/services";
2743
- var getPlatform = () => {
2744
- if (process.browser) {
2745
- if (typeof window !== "undefined") {
2746
- const { userAgent } = window.navigator;
2747
- return {
2748
- type: Platform.PLATFORM_TYPE.BROWSER,
2749
- userAgent,
2750
- uptime: Math.floor((Date.now() - window.performance.timeOrigin) / 1e3)
2751
- };
2752
- } else {
2753
- return {
2754
- type: Platform.PLATFORM_TYPE.SHARED_WORKER,
2755
- uptime: Math.floor((Date.now() - performance.timeOrigin) / 1e3)
2756
- };
2757
- }
2758
- } else {
2759
- const { platform: platform2, version, arch } = process;
2760
- return {
2761
- type: Platform.PLATFORM_TYPE.NODE,
2762
- platform: platform2,
2763
- arch,
2764
- runtime: version,
2765
- uptime: Math.floor(process.uptime()),
2766
- memory: process.memoryUsage()
2767
- };
2768
- }
2769
- };
2770
-
2771
- // packages/sdk/client-services/src/version.ts
2772
- var DXOS_VERSION = "0.4.10-main.fa5a270";
2773
-
2774
- // packages/sdk/client-services/src/packlets/services/diagnostics.ts
2775
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/diagnostics.ts";
2776
- var DEFAULT_TIMEOUT = 1e3;
2777
- var createDiagnostics = async (clientServices, serviceContext, config) => {
2778
- const diagnostics = {
2779
- created: (/* @__PURE__ */ new Date()).toISOString(),
2780
- platform: getPlatform(),
2781
- client: {
2782
- version: DXOS_VERSION,
2783
- storage: {
2784
- version: STORAGE_VERSION
2785
- }
2786
- },
2787
- trace: TRACE_PROCESSOR.getDiagnostics()
2788
- };
2789
- {
2790
- invariant9(clientServices.LoggingService, "SystemService is not available.", {
2791
- F: __dxlog_file10,
2792
- L: 108,
2793
- S: void 0,
2794
- A: [
2795
- "clientServices.LoggingService",
2796
- "'SystemService is not available.'"
2797
- ]
2798
- });
2799
- diagnostics.metrics = await getFirstStreamValue(clientServices.LoggingService.queryMetrics({}), {
2800
- timeout: DEFAULT_TIMEOUT
2801
- }).catch(() => void 0);
2802
- }
2803
- if (typeof navigator !== "undefined" && navigator.storage) {
2804
- const map = /* @__PURE__ */ new Map();
2805
- const dir = await navigator.storage.getDirectory();
2806
- for await (const filename of dir?.keys()) {
2807
- const idx = filename.indexOf("-", filename.indexOf("-") + 1);
2808
- if (idx === -1) {
2809
- continue;
2810
- }
2811
- map.set(filename.slice(0, idx), (map.get(filename.slice(0, idx)) ?? 0) + 1);
2812
- }
2813
- diagnostics.storage = Array.from(map.entries()).sort((a, b) => b[1] - a[1]).map(([file, count]) => ({
2814
- file,
2815
- count
2816
- }));
2817
- }
2818
- const identity = serviceContext.identityManager.identity;
2819
- if (identity) {
2820
- diagnostics.identity = {
2821
- identityKey: identity.identityKey,
2822
- spaceKey: identity.space.key,
2823
- profile: identity.profileDocument
2824
- };
2825
- const { devices } = await getFirstStreamValue(clientServices.DevicesService.queryDevices(), {
2826
- timeout: DEFAULT_TIMEOUT
2827
- }).catch(() => void 0) ?? {};
2828
- diagnostics.devices = devices;
2829
- if (serviceContext.dataSpaceManager) {
2830
- diagnostics.spaces = await Promise.all(Array.from(serviceContext.dataSpaceManager.spaces.values()).map((space) => getSpaceStats(space)) ?? []);
2831
- }
2832
- const { feeds = [] } = await getFirstStreamValue(clientServices.DevtoolsHost.subscribeToFeeds({}), {
2833
- timeout: DEFAULT_TIMEOUT
2834
- }).catch(() => void 0) ?? {};
2835
- diagnostics.feeds = feeds.map(({ feedKey, bytes, length }) => ({
2836
- feedKey,
2837
- bytes,
2838
- length
2839
- }));
2840
- const status = await getFirstStreamValue(clientServices.NetworkService.queryStatus(), {
2841
- timeout: DEFAULT_TIMEOUT
2842
- }).catch(() => void 0);
2843
- diagnostics.networkStatus = status;
2844
- diagnostics.swarms = serviceContext.networkManager.connectionLog?.swarms;
2845
- }
2846
- diagnostics.config = config.values;
2847
- return diagnostics;
2848
- };
2849
- var getSpaceStats = async (space) => {
2850
- const stats = {
2851
- key: space.key,
2852
- metrics: space.metrics,
2853
- epochs: space.inner.spaceState.credentials.filter(credentialTypeFilter("dxos.halo.credentials.Epoch")).map((credential) => ({
2854
- ...credential.subject.assertion,
2855
- id: credential.id
2856
- })),
2857
- members: Array.from(space.inner.spaceState.members.values()).map((member) => ({
2858
- identity: {
2859
- identityKey: member.key,
2860
- profile: {
2861
- displayName: member.assertion.profile?.displayName
2862
- }
2863
- },
2864
- presence: space.presence.getPeersOnline().filter(({ identityKey }) => identityKey.equals(member.key)).length > 0 ? SpaceMember.PresenceState.ONLINE : SpaceMember.PresenceState.OFFLINE
2865
- })),
2866
- pipeline: {
2867
- // TODO(burdon): Pick properties from credentials if needed.
2868
- currentEpoch: space.automergeSpaceState.lastEpoch,
2869
- appliedEpoch: space.automergeSpaceState.lastEpoch,
2870
- controlFeeds: space.inner.controlPipeline.state.feeds.map((feed) => feed.key),
2871
- currentControlTimeframe: space.inner.controlPipeline.state.timeframe,
2872
- targetControlTimeframe: space.inner.controlPipeline.state.targetTimeframe,
2873
- totalControlTimeframe: space.inner.controlPipeline.state.endTimeframe
2874
- }
2875
- };
2876
- if (stats.metrics) {
2877
- const { open, ready } = stats.metrics;
2878
- stats.metrics.startupTime = open && ready && ready.getTime() - open.getTime();
2879
- }
2880
- return stats;
2881
- };
2882
-
2883
2792
  // packages/sdk/client-services/src/packlets/spaces/data-space.ts
2884
2793
  import { Event as Event6, asyncTimeout, scheduleTask as scheduleTask5, sleep as sleep2, synchronized, trackLeaks } from "@dxos/async";
2885
2794
  import { AUTH_TIMEOUT as AUTH_TIMEOUT2 } from "@dxos/client-protocol";
2886
2795
  import { cancelWithContext as cancelWithContext2, Context as Context8, ContextDisposedError } from "@dxos/context";
2887
2796
  import { timed, warnAfterTimeout } from "@dxos/debug";
2797
+ import { TYPE_PROPERTIES } from "@dxos/echo-db";
2888
2798
  import { createMappedFeedWriter } from "@dxos/echo-pipeline";
2889
- import { failedInvariant, invariant as invariant11 } from "@dxos/invariant";
2799
+ import { AutomergeDocumentLoaderImpl } from "@dxos/echo-pipeline";
2800
+ import { failedInvariant, invariant as invariant10 } from "@dxos/invariant";
2890
2801
  import { PublicKey as PublicKey8 } from "@dxos/keys";
2891
2802
  import { log as log9 } from "@dxos/log";
2892
2803
  import { CancelledError, SystemError } from "@dxos/protocols";
@@ -2894,7 +2805,7 @@ import { SpaceState, CreateEpochRequest } from "@dxos/protocols/proto/dxos/clien
2894
2805
  import { AdmittedFeed as AdmittedFeed3 } from "@dxos/protocols/proto/dxos/halo/credentials";
2895
2806
  import { Timeframe as Timeframe2 } from "@dxos/timeframe";
2896
2807
  import { trace as trace6 } from "@dxos/tracing";
2897
- import { ComplexSet as ComplexSet3 } from "@dxos/util";
2808
+ import { ComplexSet as ComplexSet3, assignDeep } from "@dxos/util";
2898
2809
 
2899
2810
  // packages/sdk/client-services/src/packlets/spaces/automerge-space-state.ts
2900
2811
  import { Event as Event4 } from "@dxos/async";
@@ -2937,13 +2848,13 @@ var AutomergeSpaceState = class {
2937
2848
  // packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts
2938
2849
  import { DeferredTask, Event as Event5, scheduleTask as scheduleTask4, sleep, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
2939
2850
  import { Context as Context7, rejectOnDispose } from "@dxos/context";
2940
- import { invariant as invariant10 } from "@dxos/invariant";
2851
+ import { invariant as invariant9 } from "@dxos/invariant";
2941
2852
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2942
2853
  import { log as log8 } from "@dxos/log";
2943
2854
  import { schema as schema3 } from "@dxos/protocols";
2944
2855
  import { RpcExtension as RpcExtension2 } from "@dxos/teleport";
2945
2856
  import { ComplexMap as ComplexMap2, ComplexSet as ComplexSet2, entry } from "@dxos/util";
2946
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts";
2857
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts";
2947
2858
  var DEFAULT_RETRY_TIMEOUT = 1e3;
2948
2859
  var DEFAULT_SUCCESS_DELAY = 1e3;
2949
2860
  var DEFAULT_NOTARIZE_TIMEOUT = 1e4;
@@ -2971,13 +2882,13 @@ var NotarizationPlugin = class {
2971
2882
  log8("notarize", {
2972
2883
  credentials
2973
2884
  }, {
2974
- F: __dxlog_file11,
2885
+ F: __dxlog_file10,
2975
2886
  L: 90,
2976
2887
  S: this,
2977
2888
  C: (f, a) => f(...a)
2978
2889
  });
2979
- invariant10(credentials.every((credential) => credential.id), "Credentials must have an id", {
2980
- F: __dxlog_file11,
2890
+ invariant9(credentials.every((credential) => credential.id), "Credentials must have an id", {
2891
+ F: __dxlog_file10,
2981
2892
  L: 91,
2982
2893
  S: this,
2983
2894
  A: [
@@ -2991,7 +2902,7 @@ var NotarizationPlugin = class {
2991
2902
  log8.warn("Notarization error", {
2992
2903
  err
2993
2904
  }, {
2994
- F: __dxlog_file11,
2905
+ F: __dxlog_file10,
2995
2906
  L: 99,
2996
2907
  S: this,
2997
2908
  C: (f, a) => f(...a)
@@ -3007,7 +2918,7 @@ var NotarizationPlugin = class {
3007
2918
  timeout,
3008
2919
  peers: Array.from(this._extensions).map((extension) => extension.remotePeerId)
3009
2920
  }, {
3010
- F: __dxlog_file11,
2921
+ F: __dxlog_file10,
3011
2922
  L: 111,
3012
2923
  S: this,
3013
2924
  C: (f, a) => f(...a)
@@ -3030,7 +2941,7 @@ var NotarizationPlugin = class {
3030
2941
  log8.info("Exhausted all peers to notarize with", {
3031
2942
  retryIn: retryTimeout
3032
2943
  }, {
3033
- F: __dxlog_file11,
2944
+ F: __dxlog_file10,
3034
2945
  L: 136,
3035
2946
  S: this,
3036
2947
  C: (f, a) => f(...a)
@@ -3044,7 +2955,7 @@ var NotarizationPlugin = class {
3044
2955
  peer: peer.localPeerId,
3045
2956
  credentialId: credentials.map((credential) => credential.id)
3046
2957
  }, {
3047
- F: __dxlog_file11,
2958
+ F: __dxlog_file10,
3048
2959
  L: 143,
3049
2960
  S: this,
3050
2961
  C: (f, a) => f(...a)
@@ -3053,7 +2964,7 @@ var NotarizationPlugin = class {
3053
2964
  credentials: credentials.filter((credential) => !this._processedCredentials.has(credential.id))
3054
2965
  });
3055
2966
  log8("success", void 0, {
3056
- F: __dxlog_file11,
2967
+ F: __dxlog_file10,
3057
2968
  L: 147,
3058
2969
  S: this,
3059
2970
  C: (f, a) => f(...a)
@@ -3062,7 +2973,7 @@ var NotarizationPlugin = class {
3062
2973
  } catch (err) {
3063
2974
  if (!ctx.disposed && !err.message.includes(WRITER_NOT_SET_ERROR_CODE)) {
3064
2975
  log8.info("error notarizing (recoverable)", err, {
3065
- F: __dxlog_file11,
2976
+ F: __dxlog_file10,
3066
2977
  L: 151,
3067
2978
  S: this,
3068
2979
  C: (f, a) => f(...a)
@@ -3080,7 +2991,7 @@ var NotarizationPlugin = class {
3080
2991
  errors.wait()
3081
2992
  ]);
3082
2993
  log8("done", void 0, {
3083
- F: __dxlog_file11,
2994
+ F: __dxlog_file10,
3084
2995
  L: 162,
3085
2996
  S: this,
3086
2997
  C: (f, a) => f(...a)
@@ -3101,8 +3012,8 @@ var NotarizationPlugin = class {
3101
3012
  this._processCredentialsTriggers.delete(credential.id);
3102
3013
  }
3103
3014
  setWriter(writer) {
3104
- invariant10(!this._writer, "Writer already set.", {
3105
- F: __dxlog_file11,
3015
+ invariant9(!this._writer, "Writer already set.", {
3016
+ F: __dxlog_file10,
3106
3017
  L: 181,
3107
3018
  S: this,
3108
3019
  A: [
@@ -3126,8 +3037,8 @@ var NotarizationPlugin = class {
3126
3037
  throw new Error(WRITER_NOT_SET_ERROR_CODE);
3127
3038
  }
3128
3039
  for (const credential of request.credentials ?? []) {
3129
- invariant10(credential.id, "Credential must have an id", {
3130
- F: __dxlog_file11,
3040
+ invariant9(credential.id, "Credential must have an id", {
3041
+ F: __dxlog_file10,
3131
3042
  L: 200,
3132
3043
  S: this,
3133
3044
  A: [
@@ -3147,7 +3058,7 @@ var NotarizationPlugin = class {
3147
3058
  log8("extension opened", {
3148
3059
  peer: extension.localPeerId
3149
3060
  }, {
3150
- F: __dxlog_file11,
3061
+ F: __dxlog_file10,
3151
3062
  L: 211,
3152
3063
  S: this,
3153
3064
  C: (f, a) => f(...a)
@@ -3159,7 +3070,7 @@ var NotarizationPlugin = class {
3159
3070
  log8("extension closed", {
3160
3071
  peer: extension.localPeerId
3161
3072
  }, {
3162
- F: __dxlog_file11,
3073
+ F: __dxlog_file10,
3163
3074
  L: 216,
3164
3075
  S: this,
3165
3076
  C: (f, a) => f(...a)
@@ -3213,7 +3124,7 @@ function _ts_decorate4(decorators, target, key, desc) {
3213
3124
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3214
3125
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3215
3126
  }
3216
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space.ts";
3127
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space.ts";
3217
3128
  var DataSpace = class {
3218
3129
  constructor(params) {
3219
3130
  this._ctx = new Context8();
@@ -3248,8 +3159,8 @@ var DataSpace = class {
3248
3159
  log9("new state", {
3249
3160
  state: SpaceState[this._state]
3250
3161
  }, {
3251
- F: __dxlog_file12,
3252
- L: 132,
3162
+ F: __dxlog_file11,
3163
+ L: 140,
3253
3164
  S: this,
3254
3165
  C: (f, a) => f(...a)
3255
3166
  });
@@ -3298,8 +3209,8 @@ var DataSpace = class {
3298
3209
  log9("new state", {
3299
3210
  state: SpaceState[this._state]
3300
3211
  }, {
3301
- F: __dxlog_file12,
3302
- L: 190,
3212
+ F: __dxlog_file11,
3213
+ L: 198,
3303
3214
  S: this,
3304
3215
  C: (f, a) => f(...a)
3305
3216
  });
@@ -3316,8 +3227,8 @@ var DataSpace = class {
3316
3227
  log9("new state", {
3317
3228
  state: SpaceState[this._state]
3318
3229
  }, {
3319
- F: __dxlog_file12,
3320
- L: 204,
3230
+ F: __dxlog_file11,
3231
+ L: 212,
3321
3232
  S: this,
3322
3233
  C: (f, a) => f(...a)
3323
3234
  });
@@ -3348,16 +3259,16 @@ var DataSpace = class {
3348
3259
  } catch (err) {
3349
3260
  if (err instanceof CancelledError || err instanceof ContextDisposedError) {
3350
3261
  log9("data pipeline initialization cancelled", err, {
3351
- F: __dxlog_file12,
3352
- L: 237,
3262
+ F: __dxlog_file11,
3263
+ L: 245,
3353
3264
  S: this,
3354
3265
  C: (f, a) => f(...a)
3355
3266
  });
3356
3267
  return;
3357
3268
  }
3358
3269
  log9.error("Error initializing data pipeline", err, {
3359
- F: __dxlog_file12,
3360
- L: 241,
3270
+ F: __dxlog_file11,
3271
+ L: 249,
3361
3272
  S: this,
3362
3273
  C: (f, a) => f(...a)
3363
3274
  });
@@ -3365,8 +3276,8 @@ var DataSpace = class {
3365
3276
  log9("new state", {
3366
3277
  state: SpaceState[this._state]
3367
3278
  }, {
3368
- F: __dxlog_file12,
3369
- L: 243,
3279
+ F: __dxlog_file11,
3280
+ L: 251,
3370
3281
  S: this,
3371
3282
  C: (f, a) => f(...a)
3372
3283
  });
@@ -3385,8 +3296,8 @@ var DataSpace = class {
3385
3296
  log9("new state", {
3386
3297
  state: SpaceState[this._state]
3387
3298
  }, {
3388
- F: __dxlog_file12,
3389
- L: 259,
3299
+ F: __dxlog_file11,
3300
+ L: 267,
3390
3301
  S: this,
3391
3302
  C: (f, a) => f(...a)
3392
3303
  });
@@ -3395,8 +3306,8 @@ var DataSpace = class {
3395
3306
  this._automergeSpaceState.startProcessingRootDocs();
3396
3307
  await cancelWithContext2(this._ctx, this.automergeSpaceState.ensureEpochInitialized());
3397
3308
  log9("data pipeline ready", void 0, {
3398
- F: __dxlog_file12,
3399
- L: 271,
3309
+ F: __dxlog_file11,
3310
+ L: 279,
3400
3311
  S: this,
3401
3312
  C: (f, a) => f(...a)
3402
3313
  });
@@ -3405,8 +3316,8 @@ var DataSpace = class {
3405
3316
  log9("new state", {
3406
3317
  state: SpaceState[this._state]
3407
3318
  }, {
3408
- F: __dxlog_file12,
3409
- L: 275,
3319
+ F: __dxlog_file11,
3320
+ L: 283,
3410
3321
  S: this,
3411
3322
  C: (f, a) => f(...a)
3412
3323
  });
@@ -3421,8 +3332,8 @@ var DataSpace = class {
3421
3332
  this.metrics.controlPipelineReady = /* @__PURE__ */ new Date();
3422
3333
  await this._createWritableFeeds();
3423
3334
  log9("writable feeds created", void 0, {
3424
- F: __dxlog_file12,
3425
- L: 291,
3335
+ F: __dxlog_file11,
3336
+ L: 299,
3426
3337
  S: this,
3427
3338
  C: (f, a) => f(...a)
3428
3339
  });
@@ -3484,8 +3395,8 @@ var DataSpace = class {
3484
3395
  space: this.key,
3485
3396
  rootUrl
3486
3397
  }, {
3487
- F: __dxlog_file12,
3488
- L: 357,
3398
+ F: __dxlog_file11,
3399
+ L: 365,
3489
3400
  S: this,
3490
3401
  C: (f, a) => f(...a)
3491
3402
  });
@@ -3516,8 +3427,8 @@ var DataSpace = class {
3516
3427
  rootUrl,
3517
3428
  err
3518
3429
  }, {
3519
- F: __dxlog_file12,
3520
- L: 380,
3430
+ F: __dxlog_file11,
3431
+ L: 388,
3521
3432
  S: this,
3522
3433
  C: (f, a) => f(...a)
3523
3434
  });
@@ -3570,9 +3481,9 @@ var DataSpace = class {
3570
3481
  const rootHandle = this._automergeHost.repo.find(currentRootUrl);
3571
3482
  await cancelWithContext2(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3572
3483
  const newRoot = this._automergeHost.repo.create(rootHandle.docSync());
3573
- invariant11(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3574
- F: __dxlog_file12,
3575
- L: 430,
3484
+ invariant10(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3485
+ F: __dxlog_file11,
3486
+ L: 438,
3576
3487
  S: this,
3577
3488
  A: [
3578
3489
  "typeof newRoot.url === 'string' && newRoot.url.length > 0",
@@ -3587,30 +3498,90 @@ var DataSpace = class {
3587
3498
  };
3588
3499
  }
3589
3500
  break;
3590
- }
3591
- if (!epoch) {
3592
- return;
3593
- }
3594
- const receipt = await this.inner.controlPipeline.writer.write({
3595
- credential: {
3596
- credential: await this._signingContext.credentialSigner.createCredential({
3597
- subject: this.key,
3598
- assertion: {
3599
- "@type": "dxos.halo.credentials.Epoch",
3600
- ...epoch
3601
- }
3602
- })
3603
- }
3604
- });
3605
- await this.inner.controlPipeline.state.waitUntilTimeframe(new Timeframe2([
3606
- [
3607
- receipt.feedKey,
3608
- receipt.seq
3609
- ]
3610
- ]));
3611
- }
3612
- async activate() {
3613
- if (this._state !== SpaceState.INACTIVE) {
3501
+ case CreateEpochRequest.Migration.FRAGMENT_AUTOMERGE_ROOT:
3502
+ {
3503
+ log9.info("Fragmenting", void 0, {
3504
+ F: __dxlog_file11,
3505
+ L: 450,
3506
+ S: this,
3507
+ C: (f, a) => f(...a)
3508
+ });
3509
+ const currentRootUrl = this._automergeSpaceState.rootUrl;
3510
+ const rootHandle = this._automergeHost.repo.find(currentRootUrl);
3511
+ await cancelWithContext2(this._ctx, asyncTimeout(rootHandle.whenReady(), 1e4));
3512
+ const objects = Object.entries(rootHandle.docSync().objects);
3513
+ const properties = objects.find(([_, value]) => value.system.type?.itemId === TYPE_PROPERTIES);
3514
+ const otherObjects = objects.filter(([key]) => key !== properties?.[0]);
3515
+ invariant10(properties, "Properties not found", {
3516
+ F: __dxlog_file11,
3517
+ L: 460,
3518
+ S: this,
3519
+ A: [
3520
+ "properties",
3521
+ "'Properties not found'"
3522
+ ]
3523
+ });
3524
+ const newSpaceDoc = {
3525
+ ...rootHandle.docSync(),
3526
+ objects: Object.fromEntries([
3527
+ properties
3528
+ ])
3529
+ };
3530
+ const newRoot = this._automergeHost.repo.create(newSpaceDoc);
3531
+ invariant10(typeof newRoot.url === "string" && newRoot.url.length > 0, void 0, {
3532
+ F: __dxlog_file11,
3533
+ L: 465,
3534
+ S: this,
3535
+ A: [
3536
+ "typeof newRoot.url === 'string' && newRoot.url.length > 0",
3537
+ ""
3538
+ ]
3539
+ });
3540
+ const docLoader = new AutomergeDocumentLoaderImpl(this.key, this._automergeHost.repo);
3541
+ await docLoader.loadSpaceRootDocHandle(this._ctx, {
3542
+ rootUrl: newRoot.url
3543
+ });
3544
+ otherObjects.forEach(([key, value]) => {
3545
+ const handle = docLoader.createDocumentForObject(key);
3546
+ handle.change((doc) => {
3547
+ assignDeep(doc, [
3548
+ "objects",
3549
+ key
3550
+ ], value);
3551
+ });
3552
+ });
3553
+ epoch = {
3554
+ previousId: this._automergeSpaceState.lastEpoch?.id,
3555
+ number: (this._automergeSpaceState.lastEpoch?.subject.assertion.number ?? -1) + 1,
3556
+ timeframe: this._automergeSpaceState.lastEpoch?.subject.assertion.timeframe ?? new Timeframe2(),
3557
+ automergeRoot: newRoot.url
3558
+ };
3559
+ }
3560
+ break;
3561
+ }
3562
+ if (!epoch) {
3563
+ return;
3564
+ }
3565
+ const receipt = await this.inner.controlPipeline.writer.write({
3566
+ credential: {
3567
+ credential: await this._signingContext.credentialSigner.createCredential({
3568
+ subject: this.key,
3569
+ assertion: {
3570
+ "@type": "dxos.halo.credentials.Epoch",
3571
+ ...epoch
3572
+ }
3573
+ })
3574
+ }
3575
+ });
3576
+ await this.inner.controlPipeline.state.waitUntilTimeframe(new Timeframe2([
3577
+ [
3578
+ receipt.feedKey,
3579
+ receipt.seq
3580
+ ]
3581
+ ]));
3582
+ }
3583
+ async activate() {
3584
+ if (this._state !== SpaceState.INACTIVE) {
3614
3585
  return;
3615
3586
  }
3616
3587
  await this._metadataStore.setSpaceState(this.key, SpaceState.ACTIVE);
@@ -3627,8 +3598,8 @@ var DataSpace = class {
3627
3598
  log9("new state", {
3628
3599
  state: SpaceState[this._state]
3629
3600
  }, {
3630
- F: __dxlog_file12,
3631
- L: 482,
3601
+ F: __dxlog_file11,
3602
+ L: 531,
3632
3603
  S: this,
3633
3604
  C: (f, a) => f(...a)
3634
3605
  });
@@ -3685,7 +3656,7 @@ DataSpace = _ts_decorate4([
3685
3656
  import { Event as Event7, synchronized as synchronized2, trackLeaks as trackLeaks2 } from "@dxos/async";
3686
3657
  import { Context as Context9, cancelWithContext as cancelWithContext3 } from "@dxos/context";
3687
3658
  import { getCredentialAssertion as getCredentialAssertion2 } from "@dxos/credentials";
3688
- import { invariant as invariant12 } from "@dxos/invariant";
3659
+ import { invariant as invariant11 } from "@dxos/invariant";
3689
3660
  import { PublicKey as PublicKey9 } from "@dxos/keys";
3690
3661
  import { log as log10 } from "@dxos/log";
3691
3662
  import { trace as trace7 } from "@dxos/protocols";
@@ -3696,7 +3667,7 @@ import { ComplexMap as ComplexMap3, deferFunction as deferFunction2, forEachAsyn
3696
3667
  // packages/sdk/client-services/src/packlets/spaces/genesis.ts
3697
3668
  import { createCredential } from "@dxos/credentials";
3698
3669
  import { failUndefined } from "@dxos/debug";
3699
- import { AdmittedFeed as AdmittedFeed4, SpaceMember as SpaceMember2 } from "@dxos/protocols/proto/dxos/halo/credentials";
3670
+ import { AdmittedFeed as AdmittedFeed4, SpaceMember } from "@dxos/protocols/proto/dxos/halo/credentials";
3700
3671
  import { Timeframe as Timeframe3 } from "@dxos/timeframe";
3701
3672
  var spaceGenesis = async (keyring, signingContext, space, automergeRoot) => {
3702
3673
  const credentials = [
@@ -3716,7 +3687,7 @@ var spaceGenesis = async (keyring, signingContext, space, automergeRoot) => {
3716
3687
  assertion: {
3717
3688
  "@type": "dxos.halo.credentials.SpaceMember",
3718
3689
  spaceKey: space.key,
3719
- role: SpaceMember2.Role.ADMIN,
3690
+ role: SpaceMember.Role.ADMIN,
3720
3691
  profile: signingContext.getProfile(),
3721
3692
  genesisFeedKey: space.controlFeedKey ?? failUndefined()
3722
3693
  }
@@ -3774,7 +3745,7 @@ function _ts_decorate5(decorators, target, key, desc) {
3774
3745
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3775
3746
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3776
3747
  }
3777
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts";
3748
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts";
3778
3749
  var PRESENCE_ANNOUNCE_INTERVAL = 1e4;
3779
3750
  var PRESENCE_OFFLINE_TIMEOUT = 2e4;
3780
3751
  var DataSpaceManager = class {
@@ -3800,7 +3771,7 @@ var DataSpaceManager = class {
3800
3771
  }
3801
3772
  async open() {
3802
3773
  log10("open", void 0, {
3803
- F: __dxlog_file13,
3774
+ F: __dxlog_file12,
3804
3775
  L: 98,
3805
3776
  S: this,
3806
3777
  C: (f, a) => f(...a)
@@ -3808,7 +3779,7 @@ var DataSpaceManager = class {
3808
3779
  log10.trace("dxos.echo.data-space-manager.open", trace7.begin({
3809
3780
  id: this._instanceId
3810
3781
  }), {
3811
- F: __dxlog_file13,
3782
+ F: __dxlog_file12,
3812
3783
  L: 99,
3813
3784
  S: this,
3814
3785
  C: (f, a) => f(...a)
@@ -3816,7 +3787,7 @@ var DataSpaceManager = class {
3816
3787
  log10("metadata loaded", {
3817
3788
  spaces: this._metadataStore.spaces.length
3818
3789
  }, {
3819
- F: __dxlog_file13,
3790
+ F: __dxlog_file12,
3820
3791
  L: 100,
3821
3792
  S: this,
3822
3793
  C: (f, a) => f(...a)
@@ -3826,7 +3797,7 @@ var DataSpaceManager = class {
3826
3797
  log10("load space", {
3827
3798
  spaceMetadata
3828
3799
  }, {
3829
- F: __dxlog_file13,
3800
+ F: __dxlog_file12,
3830
3801
  L: 104,
3831
3802
  S: this,
3832
3803
  C: (f, a) => f(...a)
@@ -3837,7 +3808,7 @@ var DataSpaceManager = class {
3837
3808
  spaceMetadata,
3838
3809
  err
3839
3810
  }, {
3840
- F: __dxlog_file13,
3811
+ F: __dxlog_file12,
3841
3812
  L: 107,
3842
3813
  S: this,
3843
3814
  C: (f, a) => f(...a)
@@ -3854,7 +3825,7 @@ var DataSpaceManager = class {
3854
3825
  log10.trace("dxos.echo.data-space-manager.open", trace7.end({
3855
3826
  id: this._instanceId
3856
3827
  }), {
3857
- F: __dxlog_file13,
3828
+ F: __dxlog_file12,
3858
3829
  L: 120,
3859
3830
  S: this,
3860
3831
  C: (f, a) => f(...a)
@@ -3862,7 +3833,7 @@ var DataSpaceManager = class {
3862
3833
  }
3863
3834
  async close() {
3864
3835
  log10("close", void 0, {
3865
- F: __dxlog_file13,
3836
+ F: __dxlog_file12,
3866
3837
  L: 125,
3867
3838
  S: this,
3868
3839
  C: (f, a) => f(...a)
@@ -3877,8 +3848,8 @@ var DataSpaceManager = class {
3877
3848
  * Creates a new space writing the genesis credentials to the control feed.
3878
3849
  */
3879
3850
  async createSpace() {
3880
- invariant12(this._isOpen, "Not open.", {
3881
- F: __dxlog_file13,
3851
+ invariant11(this._isOpen, "Not open.", {
3852
+ F: __dxlog_file12,
3882
3853
  L: 138,
3883
3854
  S: this,
3884
3855
  A: [
@@ -3899,7 +3870,7 @@ var DataSpaceManager = class {
3899
3870
  log10("creating space...", {
3900
3871
  spaceKey
3901
3872
  }, {
3902
- F: __dxlog_file13,
3873
+ F: __dxlog_file12,
3903
3874
  L: 150,
3904
3875
  S: this,
3905
3876
  C: (f, a) => f(...a)
@@ -3914,8 +3885,8 @@ var DataSpaceManager = class {
3914
3885
  const credentials = await spaceGenesis(this._keyring, this._signingContext, space.inner, automergeRoot.url);
3915
3886
  await this._metadataStore.addSpace(metadata);
3916
3887
  const memberCredential = credentials[1];
3917
- invariant12(getCredentialAssertion2(memberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
3918
- F: __dxlog_file13,
3888
+ invariant11(getCredentialAssertion2(memberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
3889
+ F: __dxlog_file12,
3919
3890
  L: 163,
3920
3891
  S: this,
3921
3892
  A: [
@@ -3933,13 +3904,13 @@ var DataSpaceManager = class {
3933
3904
  log10("accept space", {
3934
3905
  opts
3935
3906
  }, {
3936
- F: __dxlog_file13,
3907
+ F: __dxlog_file12,
3937
3908
  L: 175,
3938
3909
  S: this,
3939
3910
  C: (f, a) => f(...a)
3940
3911
  });
3941
- invariant12(this._isOpen, "Not open.", {
3942
- F: __dxlog_file13,
3912
+ invariant11(this._isOpen, "Not open.", {
3913
+ F: __dxlog_file12,
3943
3914
  L: 176,
3944
3915
  S: this,
3945
3916
  A: [
@@ -3947,8 +3918,8 @@ var DataSpaceManager = class {
3947
3918
  "'Not open.'"
3948
3919
  ]
3949
3920
  });
3950
- invariant12(!this._spaces.has(opts.spaceKey), "Space already exists.", {
3951
- F: __dxlog_file13,
3921
+ invariant11(!this._spaces.has(opts.spaceKey), "Space already exists.", {
3922
+ F: __dxlog_file12,
3952
3923
  L: 177,
3953
3924
  S: this,
3954
3925
  A: [
@@ -3983,7 +3954,7 @@ var DataSpaceManager = class {
3983
3954
  log10("construct space", {
3984
3955
  metadata
3985
3956
  }, {
3986
- F: __dxlog_file13,
3957
+ F: __dxlog_file12,
3987
3958
  L: 210,
3988
3959
  S: this,
3989
3960
  C: (f, a) => f(...a)
@@ -4021,7 +3992,7 @@ var DataSpaceManager = class {
4021
3992
  },
4022
3993
  onAuthFailure: () => {
4023
3994
  log10.warn("auth failure", void 0, {
4024
- F: __dxlog_file13,
3995
+ F: __dxlog_file12,
4025
3996
  L: 247,
4026
3997
  S: this,
4027
3998
  C: (f, a) => f(...a)
@@ -4045,7 +4016,7 @@ var DataSpaceManager = class {
4045
4016
  log10("before space ready", {
4046
4017
  space: space.key
4047
4018
  }, {
4048
- F: __dxlog_file13,
4019
+ F: __dxlog_file12,
4049
4020
  L: 265,
4050
4021
  S: this,
4051
4022
  C: (f, a) => f(...a)
@@ -4056,7 +4027,7 @@ var DataSpaceManager = class {
4056
4027
  space: space.key,
4057
4028
  open: this._isOpen
4058
4029
  }, {
4059
- F: __dxlog_file13,
4030
+ F: __dxlog_file12,
4060
4031
  L: 268,
4061
4032
  S: this,
4062
4033
  C: (f, a) => f(...a)
@@ -4069,7 +4040,7 @@ var DataSpaceManager = class {
4069
4040
  log10("before space close", {
4070
4041
  space: space.key
4071
4042
  }, {
4072
- F: __dxlog_file13,
4043
+ F: __dxlog_file12,
4073
4044
  L: 274,
4074
4045
  S: this,
4075
4046
  C: (f, a) => f(...a)
@@ -4109,11 +4080,11 @@ DataSpaceManager = _ts_decorate5([
4109
4080
  import { EventSubscriptions as EventSubscriptions2, UpdateScheduler, scheduleTask as scheduleTask6 } from "@dxos/async";
4110
4081
  import { Stream as Stream10 } from "@dxos/codec-protobuf";
4111
4082
  import { raise as raise2 } from "@dxos/debug";
4112
- import { invariant as invariant13 } from "@dxos/invariant";
4083
+ import { invariant as invariant12 } from "@dxos/invariant";
4113
4084
  import { log as log11 } from "@dxos/log";
4114
4085
  import { ApiError, SpaceNotFoundError, encodeError } from "@dxos/protocols";
4115
- import { SpaceMember as SpaceMember3, SpaceState as SpaceState3 } from "@dxos/protocols/proto/dxos/client/services";
4116
- var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/spaces-service.ts";
4086
+ import { SpaceMember as SpaceMember2, SpaceState as SpaceState3 } from "@dxos/protocols/proto/dxos/client/services";
4087
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/spaces-service.ts";
4117
4088
  var SpacesServiceImpl = class {
4118
4089
  constructor(_identityManager, _spaceManager, _getDataSpaceManager) {
4119
4090
  this._identityManager = _identityManager;
@@ -4152,7 +4123,7 @@ var SpacesServiceImpl = class {
4152
4123
  log11("update", {
4153
4124
  spaces
4154
4125
  }, {
4155
- F: __dxlog_file14,
4126
+ F: __dxlog_file13,
4156
4127
  L: 77,
4157
4128
  S: this,
4158
4129
  C: (f, a) => f(...a)
@@ -4234,8 +4205,8 @@ var SpacesServiceImpl = class {
4234
4205
  }
4235
4206
  });
4236
4207
  } else {
4237
- invariant13(!credential.id, "Id on unsigned credentials is not allowed", {
4238
- F: __dxlog_file14,
4208
+ invariant12(!credential.id, "Id on unsigned credentials is not allowed", {
4209
+ F: __dxlog_file13,
4239
4210
  L: 164,
4240
4211
  S: this,
4241
4212
  A: [
@@ -4243,8 +4214,8 @@ var SpacesServiceImpl = class {
4243
4214
  "'Id on unsigned credentials is not allowed'"
4244
4215
  ]
4245
4216
  });
4246
- invariant13(this._identityManager.identity, "Identity is not available", {
4247
- F: __dxlog_file14,
4217
+ invariant12(this._identityManager.identity, "Identity is not available", {
4218
+ F: __dxlog_file13,
4248
4219
  L: 165,
4249
4220
  S: this,
4250
4221
  A: [
@@ -4253,8 +4224,8 @@ var SpacesServiceImpl = class {
4253
4224
  ]
4254
4225
  });
4255
4226
  const signer = this._identityManager.identity.getIdentityCredentialSigner();
4256
- invariant13(credential.issuer.equals(signer.getIssuer()), void 0, {
4257
- F: __dxlog_file14,
4227
+ invariant12(credential.issuer.equals(signer.getIssuer()), void 0, {
4228
+ F: __dxlog_file13,
4258
4229
  L: 167,
4259
4230
  S: this,
4260
4231
  A: [
@@ -4310,7 +4281,7 @@ var SpacesServiceImpl = class {
4310
4281
  identityKey: member.key,
4311
4282
  profile: member.profile ?? {}
4312
4283
  },
4313
- presence: member.removed ? SpaceMember3.PresenceState.REMOVED : isMe || peers.length > 0 ? SpaceMember3.PresenceState.ONLINE : SpaceMember3.PresenceState.OFFLINE,
4284
+ presence: member.removed ? SpaceMember2.PresenceState.REMOVED : isMe || peers.length > 0 ? SpaceMember2.PresenceState.ONLINE : SpaceMember2.PresenceState.OFFLINE,
4314
4285
  peerStates: peers
4315
4286
  };
4316
4287
  }),
@@ -4324,17 +4295,17 @@ var getChannelId = (channel) => `user-channel/${channel}`;
4324
4295
 
4325
4296
  // packages/sdk/client-services/src/packlets/services/service-context.ts
4326
4297
  import { Trigger as Trigger5 } from "@dxos/async";
4327
- import { Context as Context10 } from "@dxos/context";
4298
+ import { Context as Context10, Resource } from "@dxos/context";
4328
4299
  import { getCredentialAssertion as getCredentialAssertion3 } from "@dxos/credentials";
4329
4300
  import { failUndefined as failUndefined2 } from "@dxos/debug";
4330
4301
  import { AutomergeHost, MetadataStore, SnapshotStore, SpaceManager, valueEncoding } from "@dxos/echo-pipeline";
4331
4302
  import { FeedFactory, FeedStore } from "@dxos/feed-store";
4332
- import { IndexMetadataStore, IndexStore, Indexer } from "@dxos/indexing";
4333
- import { invariant as invariant14 } from "@dxos/invariant";
4303
+ import { IndexMetadataStore, IndexStore, Indexer, createStorageCallbacks } from "@dxos/indexing";
4304
+ import { invariant as invariant13 } from "@dxos/invariant";
4334
4305
  import { Keyring } from "@dxos/keyring";
4335
4306
  import { PublicKey as PublicKey10 } from "@dxos/keys";
4336
4307
  import { log as log12 } from "@dxos/log";
4337
- import { InvalidStorageVersionError, STORAGE_VERSION as STORAGE_VERSION2, trace as trace8 } from "@dxos/protocols";
4308
+ import { InvalidStorageVersionError, STORAGE_VERSION, trace as trace8 } from "@dxos/protocols";
4338
4309
  import { Invitation as Invitation6 } from "@dxos/protocols/proto/dxos/client/services";
4339
4310
  import { BlobStore } from "@dxos/teleport-extension-object-sync";
4340
4311
  import { trace as Trace2 } from "@dxos/tracing";
@@ -4344,7 +4315,7 @@ import { safeInstanceof } from "@dxos/util";
4344
4315
  import { getHeads } from "@dxos/automerge/automerge";
4345
4316
  import { warnAfterTimeout as warnAfterTimeout2 } from "@dxos/debug";
4346
4317
  import { idCodec } from "@dxos/protocols";
4347
- var createLoadDocuments = (automergeHost) => (
4318
+ var createSelectedDocumentsIterator = (automergeHost) => (
4348
4319
  /**
4349
4320
  * Get object data blobs from Automerge Repo by ids.
4350
4321
  * @param ids
@@ -4357,17 +4328,17 @@ var createLoadDocuments = (automergeHost) => (
4357
4328
  await warnAfterTimeout2(5e3, "to long to load doc", () => handle.whenReady());
4358
4329
  const doc = handle.docSync();
4359
4330
  const hash = getHeads(doc).join("");
4360
- yield [
4331
+ yield doc.objects?.[objectId] ? [
4361
4332
  {
4362
4333
  id,
4363
4334
  object: doc.objects[objectId],
4364
4335
  currentHash: hash
4365
4336
  }
4366
- ];
4337
+ ] : [];
4367
4338
  }
4368
4339
  }
4369
4340
  );
4370
- var createGetAllDocuments = (automergeHost) => (
4341
+ var createDocumentsIterator = (automergeHost) => (
4371
4342
  /**
4372
4343
  * Recursively get all object data blobs from Automerge Repo.
4373
4344
  * @param ids
@@ -4390,7 +4361,7 @@ var createGetAllDocuments = (automergeHost) => (
4390
4361
  objectId
4391
4362
  }),
4392
4363
  object,
4393
- currentHash: heads.at(-1)
4364
+ currentHash: heads.join("")
4394
4365
  };
4395
4366
  });
4396
4367
  }
@@ -4430,10 +4401,12 @@ function _ts_decorate6(decorators, target, key, desc) {
4430
4401
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
4431
4402
  return c > 3 && r && Object.defineProperty(target, key, r), r;
4432
4403
  }
4433
- var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-context.ts";
4434
- var ServiceContext = class {
4435
- constructor(storage, networkManager, signalManager, _runtimeParams) {
4404
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-context.ts";
4405
+ var ServiceContext = class extends Resource {
4406
+ constructor(storage, level, networkManager, signalManager, _runtimeParams) {
4407
+ super();
4436
4408
  this.storage = storage;
4409
+ this.level = level;
4437
4410
  this.networkManager = networkManager;
4438
4411
  this.signalManager = signalManager;
4439
4412
  this._runtimeParams = _runtimeParams;
@@ -4463,41 +4436,46 @@ var ServiceContext = class {
4463
4436
  });
4464
4437
  this.identityManager = new IdentityManager(this.metadataStore, this.keyring, this.feedStore, this.spaceManager, this._runtimeParams);
4465
4438
  this.indexMetadata = new IndexMetadataStore({
4466
- directory: storage.createDirectory("index-metadata")
4439
+ db: level.sublevel("index-metadata")
4467
4440
  });
4468
4441
  this.automergeHost = new AutomergeHost({
4469
4442
  directory: storage.createDirectory("automerge"),
4470
- metadata: this.indexMetadata
4443
+ db: level.sublevel("automerge"),
4444
+ storageCallbacks: createStorageCallbacks({
4445
+ host: () => this.automergeHost,
4446
+ metadata: this.indexMetadata
4447
+ })
4471
4448
  });
4472
4449
  this.indexer = new Indexer({
4473
4450
  indexStore: new IndexStore({
4474
- directory: storage.createDirectory("index-store")
4451
+ db: level.sublevel("index-storage")
4475
4452
  }),
4476
4453
  metadataStore: this.indexMetadata,
4477
- loadDocuments: createLoadDocuments(this.automergeHost),
4478
- getAllDocuments: createGetAllDocuments(this.automergeHost)
4454
+ loadDocuments: createSelectedDocumentsIterator(this.automergeHost),
4455
+ getAllDocuments: createDocumentsIterator(this.automergeHost)
4479
4456
  });
4480
4457
  this.invitations = new InvitationsHandler(this.networkManager);
4481
4458
  this._handlerFactories.set(Invitation6.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => this.identityManager.identity ?? failUndefined2(), this._acceptIdentity.bind(this)));
4482
4459
  }
4483
- async open(ctx) {
4460
+ async _open(ctx) {
4484
4461
  await this._checkStorageVersion();
4485
4462
  log12("opening...", void 0, {
4486
- F: __dxlog_file15,
4487
- L: 151,
4463
+ F: __dxlog_file14,
4464
+ L: 157,
4488
4465
  S: this,
4489
4466
  C: (f, a) => f(...a)
4490
4467
  });
4491
4468
  log12.trace("dxos.sdk.service-context.open", trace8.begin({
4492
4469
  id: this._instanceId
4493
4470
  }), {
4494
- F: __dxlog_file15,
4495
- L: 152,
4471
+ F: __dxlog_file14,
4472
+ L: 158,
4496
4473
  S: this,
4497
4474
  C: (f, a) => f(...a)
4498
4475
  });
4499
4476
  await this.signalManager.open();
4500
4477
  await this.networkManager.open();
4478
+ await this.automergeHost.open();
4501
4479
  await this.metadataStore.load();
4502
4480
  await this.spaceManager.open();
4503
4481
  await this.identityManager.open(ctx);
@@ -4507,22 +4485,22 @@ var ServiceContext = class {
4507
4485
  log12.trace("dxos.sdk.service-context.open", trace8.end({
4508
4486
  id: this._instanceId
4509
4487
  }), {
4510
- F: __dxlog_file15,
4511
- L: 162,
4488
+ F: __dxlog_file14,
4489
+ L: 169,
4512
4490
  S: this,
4513
4491
  C: (f, a) => f(...a)
4514
4492
  });
4515
4493
  log12("opened", void 0, {
4516
- F: __dxlog_file15,
4517
- L: 163,
4494
+ F: __dxlog_file14,
4495
+ L: 170,
4518
4496
  S: this,
4519
4497
  C: (f, a) => f(...a)
4520
4498
  });
4521
4499
  }
4522
- async close() {
4500
+ async _close() {
4523
4501
  log12("closing...", void 0, {
4524
- F: __dxlog_file15,
4525
- L: 167,
4502
+ F: __dxlog_file14,
4503
+ L: 174,
4526
4504
  S: this,
4527
4505
  C: (f, a) => f(...a)
4528
4506
  });
@@ -4539,8 +4517,8 @@ var ServiceContext = class {
4539
4517
  await this.metadataStore.close();
4540
4518
  await this.indexer.destroy();
4541
4519
  log12("closed", void 0, {
4542
- F: __dxlog_file15,
4543
- L: 180,
4520
+ F: __dxlog_file14,
4521
+ L: 187,
4544
4522
  S: this,
4545
4523
  C: (f, a) => f(...a)
4546
4524
  });
@@ -4552,9 +4530,9 @@ var ServiceContext = class {
4552
4530
  }
4553
4531
  getInvitationHandler(invitation) {
4554
4532
  const factory = this._handlerFactories.get(invitation.kind);
4555
- invariant14(factory, `Unknown invitation kind: ${invitation.kind}`, {
4556
- F: __dxlog_file15,
4557
- L: 191,
4533
+ invariant13(factory, `Unknown invitation kind: ${invitation.kind}`, {
4534
+ F: __dxlog_file14,
4535
+ L: 198,
4558
4536
  S: this,
4559
4537
  A: [
4560
4538
  "factory",
@@ -4578,15 +4556,15 @@ var ServiceContext = class {
4578
4556
  }
4579
4557
  async _checkStorageVersion() {
4580
4558
  await this.metadataStore.load();
4581
- if (this.metadataStore.version !== STORAGE_VERSION2) {
4582
- throw new InvalidStorageVersionError(STORAGE_VERSION2, this.metadataStore.version);
4559
+ if (this.metadataStore.version !== STORAGE_VERSION) {
4560
+ throw new InvalidStorageVersionError(STORAGE_VERSION, this.metadataStore.version);
4583
4561
  }
4584
4562
  }
4585
4563
  // Called when identity is created.
4586
4564
  async _initialize(ctx) {
4587
4565
  log12("initializing spaces...", void 0, {
4588
- F: __dxlog_file15,
4589
- L: 222,
4566
+ F: __dxlog_file14,
4567
+ L: 229,
4590
4568
  S: this,
4591
4569
  C: (f, a) => f(...a)
4592
4570
  });
@@ -4607,9 +4585,9 @@ var ServiceContext = class {
4607
4585
  this.dataSpaceManager = new DataSpaceManager(this.spaceManager, this.metadataStore, this.keyring, signingContext, this.feedStore, this.automergeHost, this._runtimeParams);
4608
4586
  await this.dataSpaceManager.open();
4609
4587
  this._handlerFactories.set(Invitation6.Kind.SPACE, (invitation) => {
4610
- invariant14(this.dataSpaceManager, "dataSpaceManager not initialized yet", {
4611
- F: __dxlog_file15,
4612
- L: 246,
4588
+ invariant13(this.dataSpaceManager, "dataSpaceManager not initialized yet", {
4589
+ F: __dxlog_file14,
4590
+ L: 253,
4613
4591
  S: this,
4614
4592
  A: [
4615
4593
  "this.dataSpaceManager",
@@ -4632,8 +4610,8 @@ var ServiceContext = class {
4632
4610
  log12("dataSpaceManager not initialized yet, ignoring space admission", {
4633
4611
  details: assertion
4634
4612
  }, {
4635
- F: __dxlog_file15,
4636
- L: 262,
4613
+ F: __dxlog_file14,
4614
+ L: 269,
4637
4615
  S: this,
4638
4616
  C: (f, a) => f(...a)
4639
4617
  });
@@ -4643,8 +4621,8 @@ var ServiceContext = class {
4643
4621
  log12("space already exists, ignoring space admission", {
4644
4622
  details: assertion
4645
4623
  }, {
4646
- F: __dxlog_file15,
4647
- L: 266,
4624
+ F: __dxlog_file14,
4625
+ L: 273,
4648
4626
  S: this,
4649
4627
  C: (f, a) => f(...a)
4650
4628
  });
@@ -4654,8 +4632,8 @@ var ServiceContext = class {
4654
4632
  log12("accepting space recorded in halo", {
4655
4633
  details: assertion
4656
4634
  }, {
4657
- F: __dxlog_file15,
4658
- L: 271,
4635
+ F: __dxlog_file14,
4636
+ L: 278,
4659
4637
  S: this,
4660
4638
  C: (f, a) => f(...a)
4661
4639
  });
@@ -4665,8 +4643,8 @@ var ServiceContext = class {
4665
4643
  });
4666
4644
  } catch (err) {
4667
4645
  log12.catch(err, void 0, {
4668
- F: __dxlog_file15,
4669
- L: 277,
4646
+ F: __dxlog_file14,
4647
+ L: 284,
4670
4648
  S: this,
4671
4649
  C: (f, a) => f(...a)
4672
4650
  });
@@ -4678,7 +4656,7 @@ var ServiceContext = class {
4678
4656
  };
4679
4657
  _ts_decorate6([
4680
4658
  Trace2.span()
4681
- ], ServiceContext.prototype, "open", null);
4659
+ ], ServiceContext.prototype, "_open", null);
4682
4660
  _ts_decorate6([
4683
4661
  Trace2.span()
4684
4662
  ], ServiceContext.prototype, "_initialize", null);
@@ -4711,195 +4689,271 @@ var ServiceRegistry = class {
4711
4689
  }
4712
4690
  };
4713
4691
 
4714
- // packages/sdk/client-services/src/packlets/locks/browser.ts
4715
- import { asyncTimeout as asyncTimeout2, Trigger as Trigger6 } from "@dxos/async";
4716
- import { RESOURCE_LOCK_TIMEOUT } from "@dxos/client-protocol";
4717
- import { log as log13, logInfo } from "@dxos/log";
4718
- function _ts_decorate7(decorators, target, key, desc) {
4719
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4720
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
4721
- r = Reflect.decorate(decorators, target, key, desc);
4722
- else
4723
- for (var i = decorators.length - 1; i >= 0; i--)
4724
- if (d = decorators[i])
4725
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
4726
- return c > 3 && r && Object.defineProperty(target, key, r), r;
4727
- }
4728
- var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/locks/browser.ts";
4729
- var Message;
4730
- (function(Message2) {
4731
- Message2["ACQUIRING"] = "acquiring";
4732
- })(Message || (Message = {}));
4733
- var Lock = class {
4734
- constructor({ lockKey, onAcquire, onRelease }) {
4735
- this._broadcastChannel = new BroadcastChannel("vault-resource-lock");
4736
- this._releaseTrigger = new Trigger6();
4737
- this._lockKey = lockKey;
4738
- this._onAcquire = onAcquire;
4739
- this._onRelease = onRelease;
4740
- this._broadcastChannel.onmessage = this._onMessage.bind(this);
4741
- }
4742
- get lockKey() {
4743
- return this._lockKey;
4744
- }
4745
- async acquire() {
4746
- this._broadcastChannel.postMessage({
4747
- message: "acquiring"
4748
- });
4749
- try {
4750
- log13("aquiring lock...", void 0, {
4751
- F: __dxlog_file16,
4752
- L: 42,
4753
- S: this,
4754
- C: (f, a) => f(...a)
4755
- });
4756
- await asyncTimeout2(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
4757
- log13("acquired lock", void 0, {
4758
- F: __dxlog_file16,
4759
- L: 44,
4760
- S: this,
4761
- C: (f, a) => f(...a)
4762
- });
4763
- } catch {
4764
- log13("stealing lock...", void 0, {
4765
- F: __dxlog_file16,
4766
- L: 46,
4767
- S: this,
4768
- C: (f, a) => f(...a)
4769
- });
4770
- await this._requestLock(true);
4771
- log13("stolen lock", void 0, {
4772
- F: __dxlog_file16,
4773
- L: 48,
4774
- S: this,
4775
- C: (f, a) => f(...a)
4776
- });
4692
+ // packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts
4693
+ import { getFirstStreamValue } from "@dxos/codec-protobuf";
4694
+ import { credentialTypeFilter } from "@dxos/credentials";
4695
+ import { invariant as invariant14 } from "@dxos/invariant";
4696
+ import { STORAGE_VERSION as STORAGE_VERSION2 } from "@dxos/protocols";
4697
+ import { SpaceMember as SpaceMember3 } from "@dxos/protocols/proto/dxos/client/services";
4698
+ import { TRACE_PROCESSOR } from "@dxos/tracing";
4699
+
4700
+ // packages/sdk/client-services/src/version.ts
4701
+ var DXOS_VERSION = "0.4.10-main.fe71b4c";
4702
+
4703
+ // packages/sdk/client-services/src/packlets/services/platform.ts
4704
+ import { Platform } from "@dxos/protocols/proto/dxos/client/services";
4705
+ var getPlatform = () => {
4706
+ if (process.browser) {
4707
+ if (typeof window !== "undefined") {
4708
+ const { userAgent } = window.navigator;
4709
+ return {
4710
+ type: Platform.PLATFORM_TYPE.BROWSER,
4711
+ userAgent,
4712
+ uptime: Math.floor((Date.now() - window.performance.timeOrigin) / 1e3)
4713
+ };
4714
+ } else {
4715
+ return {
4716
+ type: Platform.PLATFORM_TYPE.SHARED_WORKER,
4717
+ uptime: Math.floor((Date.now() - performance.timeOrigin) / 1e3)
4718
+ };
4777
4719
  }
4720
+ } else {
4721
+ const { platform: platform2, version, arch } = process;
4722
+ return {
4723
+ type: Platform.PLATFORM_TYPE.NODE,
4724
+ platform: platform2,
4725
+ arch,
4726
+ runtime: version,
4727
+ uptime: Math.floor(process.uptime()),
4728
+ memory: process.memoryUsage()
4729
+ };
4778
4730
  }
4779
- async release() {
4780
- this._releaseTrigger.wake();
4731
+ };
4732
+
4733
+ // packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts
4734
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/diagnostics.ts";
4735
+ var DEFAULT_TIMEOUT = 1e3;
4736
+ var createDiagnostics = async (clientServices, serviceContext, config) => {
4737
+ const diagnostics = {
4738
+ created: (/* @__PURE__ */ new Date()).toISOString(),
4739
+ platform: getPlatform(),
4740
+ client: {
4741
+ version: DXOS_VERSION,
4742
+ storage: {
4743
+ version: STORAGE_VERSION2
4744
+ }
4745
+ },
4746
+ trace: TRACE_PROCESSOR.getDiagnostics()
4747
+ };
4748
+ {
4749
+ invariant14(clientServices.LoggingService, "SystemService is not available.", {
4750
+ F: __dxlog_file15,
4751
+ L: 108,
4752
+ S: void 0,
4753
+ A: [
4754
+ "clientServices.LoggingService",
4755
+ "'SystemService is not available.'"
4756
+ ]
4757
+ });
4758
+ diagnostics.metrics = await getFirstStreamValue(clientServices.LoggingService.queryMetrics({}), {
4759
+ timeout: DEFAULT_TIMEOUT
4760
+ }).catch(() => void 0);
4781
4761
  }
4782
- _onMessage(event) {
4783
- if (event.data.message === "acquiring") {
4784
- this._releaseTrigger.wake();
4762
+ if (typeof navigator !== "undefined" && navigator.storage) {
4763
+ const map = /* @__PURE__ */ new Map();
4764
+ const dir = await navigator.storage.getDirectory();
4765
+ for await (const filename of dir?.keys()) {
4766
+ const idx = filename.indexOf("-", filename.indexOf("-") + 1);
4767
+ if (idx === -1) {
4768
+ continue;
4769
+ }
4770
+ map.set(filename.slice(0, idx), (map.get(filename.slice(0, idx)) ?? 0) + 1);
4785
4771
  }
4772
+ diagnostics.storage = Array.from(map.entries()).sort((a, b) => b[1] - a[1]).map(([file, count]) => ({
4773
+ file,
4774
+ count
4775
+ }));
4786
4776
  }
4787
- async _requestLock(steal = false) {
4788
- log13("requesting lock...", {
4789
- steal
4790
- }, {
4791
- F: __dxlog_file16,
4792
- L: 63,
4793
- S: this,
4794
- C: (f, a) => f(...a)
4795
- });
4796
- const acquired = new Trigger6();
4797
- void navigator.locks.request(this._lockKey, {
4798
- steal
4799
- }, async () => {
4800
- await this._onAcquire?.();
4801
- acquired.wake();
4802
- this._releaseTrigger = new Trigger6();
4803
- await this._releaseTrigger.wait();
4804
- log13("releasing lock...", void 0, {
4805
- F: __dxlog_file16,
4806
- L: 72,
4807
- S: this,
4808
- C: (f, a) => f(...a)
4809
- });
4810
- await this._onRelease?.();
4811
- log13("released lock", void 0, {
4812
- F: __dxlog_file16,
4813
- L: 74,
4814
- S: this,
4815
- C: (f, a) => f(...a)
4816
- });
4817
- }).catch(async () => {
4818
- await this._onRelease?.();
4819
- });
4820
- await acquired.wait();
4821
- log13("recieved lock", {
4822
- steal
4823
- }, {
4824
- F: __dxlog_file16,
4825
- L: 81,
4826
- S: this,
4827
- C: (f, a) => f(...a)
4828
- });
4777
+ const identity = serviceContext.identityManager.identity;
4778
+ if (identity) {
4779
+ diagnostics.identity = {
4780
+ identityKey: identity.identityKey,
4781
+ spaceKey: identity.space.key,
4782
+ profile: identity.profileDocument
4783
+ };
4784
+ const { devices } = await getFirstStreamValue(clientServices.DevicesService.queryDevices(), {
4785
+ timeout: DEFAULT_TIMEOUT
4786
+ }).catch(() => void 0) ?? {};
4787
+ diagnostics.devices = devices;
4788
+ if (serviceContext.dataSpaceManager) {
4789
+ diagnostics.spaces = await Promise.all(Array.from(serviceContext.dataSpaceManager.spaces.values()).map((space) => getSpaceStats(space)) ?? []);
4790
+ }
4791
+ const { feeds = [] } = await getFirstStreamValue(clientServices.DevtoolsHost.subscribeToFeeds({}), {
4792
+ timeout: DEFAULT_TIMEOUT
4793
+ }).catch(() => void 0) ?? {};
4794
+ diagnostics.feeds = feeds.map(({ feedKey, bytes, length }) => ({
4795
+ feedKey,
4796
+ bytes,
4797
+ length
4798
+ }));
4799
+ const status = await getFirstStreamValue(clientServices.NetworkService.queryStatus(), {
4800
+ timeout: DEFAULT_TIMEOUT
4801
+ }).catch(() => void 0);
4802
+ diagnostics.networkStatus = status;
4803
+ diagnostics.swarms = serviceContext.networkManager.connectionLog?.swarms;
4829
4804
  }
4805
+ diagnostics.config = config.values;
4806
+ return diagnostics;
4830
4807
  };
4831
- _ts_decorate7([
4832
- logInfo
4833
- ], Lock.prototype, "lockKey", null);
4834
- var isLocked = (lockPath) => {
4835
- throw new Error("Not implemented");
4808
+ var getSpaceStats = async (space) => {
4809
+ const stats = {
4810
+ key: space.key,
4811
+ metrics: space.metrics,
4812
+ epochs: space.inner.spaceState.credentials.filter(credentialTypeFilter("dxos.halo.credentials.Epoch")).map((credential) => ({
4813
+ ...credential.subject.assertion,
4814
+ id: credential.id
4815
+ })),
4816
+ members: Array.from(space.inner.spaceState.members.values()).map((member) => ({
4817
+ identity: {
4818
+ identityKey: member.key,
4819
+ profile: {
4820
+ displayName: member.assertion.profile?.displayName
4821
+ }
4822
+ },
4823
+ presence: space.presence.getPeersOnline().filter(({ identityKey }) => identityKey.equals(member.key)).length > 0 ? SpaceMember3.PresenceState.ONLINE : SpaceMember3.PresenceState.OFFLINE
4824
+ })),
4825
+ pipeline: {
4826
+ // TODO(burdon): Pick properties from credentials if needed.
4827
+ currentEpoch: space.automergeSpaceState.lastEpoch,
4828
+ appliedEpoch: space.automergeSpaceState.lastEpoch,
4829
+ controlFeeds: space.inner.controlPipeline.state.feeds.map((feed) => feed.key),
4830
+ currentControlTimeframe: space.inner.controlPipeline.state.timeframe,
4831
+ targetControlTimeframe: space.inner.controlPipeline.state.targetTimeframe,
4832
+ totalControlTimeframe: space.inner.controlPipeline.state.endTimeframe
4833
+ }
4834
+ };
4835
+ if (stats.metrics) {
4836
+ const { open, ready } = stats.metrics;
4837
+ stats.metrics.startupTime = open && ready && ready.getTime() - open.getTime();
4838
+ }
4839
+ return stats;
4836
4840
  };
4837
4841
 
4838
- // packages/sdk/client-services/src/packlets/storage/storage.ts
4839
- import { DX_DATA } from "@dxos/client-protocol";
4840
- import { InvalidConfigError } from "@dxos/protocols";
4841
- import { Runtime } from "@dxos/protocols/proto/dxos/config";
4842
- import { createStorage, StorageType } from "@dxos/random-access-storage";
4843
- import { isNode as isNode2 } from "@dxos/util";
4844
- var StorageDriver = Runtime.Client.Storage.StorageDriver;
4845
- var createStorageObjects = (config) => {
4846
- const { persistent = false, keyStore, dataStore, dataRoot = isNode2() ? DX_DATA : "dxos/storage" } = config ?? {};
4847
- if (persistent && dataStore === StorageDriver.RAM) {
4848
- throw new InvalidConfigError("RAM storage cannot be used in persistent mode.");
4849
- }
4850
- if (!persistent && dataStore !== void 0 && dataStore !== StorageDriver.RAM) {
4851
- throw new InvalidConfigError("Cannot use a persistent storage in not persistent mode.");
4852
- }
4853
- if (persistent && keyStore === StorageDriver.RAM) {
4854
- throw new InvalidConfigError("RAM key storage cannot be used in persistent mode.");
4855
- }
4856
- if (!persistent && keyStore !== StorageDriver.RAM && keyStore !== void 0) {
4857
- throw new InvalidConfigError("Cannot use a persistent key storage in not persistent mode.");
4858
- }
4842
+ // packages/sdk/client-services/src/packlets/diagnostics/browser-diagnostics-broadcast.ts
4843
+ import { Trigger as Trigger6 } from "@dxos/async";
4844
+ import { log as log13 } from "@dxos/log";
4845
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/diagnostics/browser-diagnostics-broadcast.ts";
4846
+ var CHANNEL_NAME = "dxos.diagnostics.broadcast";
4847
+ var MessageType;
4848
+ (function(MessageType2) {
4849
+ MessageType2["PROBE"] = "probe";
4850
+ MessageType2["PROBE_ACK"] = "probe-ack";
4851
+ MessageType2["REQUEST_DIAGNOSTICS"] = "request-diagnostics";
4852
+ MessageType2["RECEIVE_DIAGNOSTICS"] = "receive-diagnostics";
4853
+ })(MessageType || (MessageType = {}));
4854
+ var createCollectDiagnosticsBroadcastSender = () => {
4859
4855
  return {
4860
- storage: createStorage({
4861
- type: persistent ? toStorageType(dataStore) : StorageType.RAM,
4862
- root: `${dataRoot}/`
4863
- })
4856
+ broadcastDiagnosticsRequest: async () => {
4857
+ let expectedResponse = "probe-ack";
4858
+ let channel;
4859
+ try {
4860
+ const trigger = new Trigger6();
4861
+ channel = new BroadcastChannel(CHANNEL_NAME);
4862
+ channel.onmessage = (msg) => {
4863
+ if (expectedResponse === msg.data.type) {
4864
+ trigger.wake(msg.data);
4865
+ }
4866
+ };
4867
+ channel.postMessage({
4868
+ type: "probe"
4869
+ });
4870
+ await trigger.wait({
4871
+ timeout: 200
4872
+ });
4873
+ expectedResponse = "receive-diagnostics";
4874
+ trigger.reset();
4875
+ channel.postMessage({
4876
+ type: "request-diagnostics"
4877
+ });
4878
+ const diagnostics = await trigger.wait({
4879
+ timeout: 5e3
4880
+ });
4881
+ return diagnostics.payload;
4882
+ } catch (e) {
4883
+ const errorDescription = e instanceof Error ? e.message : JSON.stringify(e);
4884
+ return {
4885
+ expectedResponse,
4886
+ errorDescription
4887
+ };
4888
+ } finally {
4889
+ safeClose(channel);
4890
+ }
4891
+ }
4864
4892
  };
4865
4893
  };
4866
- var toStorageType = (type) => {
4867
- switch (type) {
4868
- case void 0:
4869
- return void 0;
4870
- case StorageDriver.RAM:
4871
- return StorageType.RAM;
4872
- case StorageDriver.CHROME:
4873
- return StorageType.CHROME;
4874
- case StorageDriver.FIREFOX:
4875
- return StorageType.FIREFOX;
4876
- case StorageDriver.IDB:
4877
- return StorageType.IDB;
4878
- case StorageDriver.NODE:
4879
- return StorageType.NODE;
4880
- case StorageDriver.WEBFS:
4881
- return StorageType.WEBFS;
4882
- default:
4883
- throw new Error(`Invalid storage type: ${StorageDriver[type]}`);
4894
+ var createCollectDiagnosticsBroadcastHandler = (systemService) => {
4895
+ let channel;
4896
+ return {
4897
+ start: () => {
4898
+ channel = new BroadcastChannel(CHANNEL_NAME);
4899
+ channel.onmessage = async (message) => {
4900
+ try {
4901
+ if (message.data.type === "probe") {
4902
+ channel?.postMessage({
4903
+ type: "probe-ack"
4904
+ });
4905
+ } else if (message.data.type === "request-diagnostics") {
4906
+ const diagnostics = await systemService.getDiagnostics({});
4907
+ channel?.postMessage({
4908
+ type: "receive-diagnostics",
4909
+ payload: diagnostics
4910
+ });
4911
+ }
4912
+ } catch (error) {
4913
+ log13.catch(error, void 0, {
4914
+ F: __dxlog_file16,
4915
+ L: 77,
4916
+ S: void 0,
4917
+ C: (f, a) => f(...a)
4918
+ });
4919
+ }
4920
+ };
4921
+ },
4922
+ stop: () => {
4923
+ safeClose(channel);
4924
+ channel = void 0;
4925
+ }
4926
+ };
4927
+ };
4928
+ var safeClose = (channel) => {
4929
+ try {
4930
+ channel?.close();
4931
+ } catch (e) {
4884
4932
  }
4885
4933
  };
4886
4934
 
4935
+ // packages/sdk/client-services/src/packlets/diagnostics/diagnostics-collector.ts
4936
+ import { ConfigResource } from "@dxos/config";
4937
+ import { GetDiagnosticsRequest as GetDiagnosticsRequest2 } from "@dxos/protocols/proto/dxos/client/services";
4938
+ import { TRACE_PROCESSOR as TRACE_PROCESSOR3 } from "@dxos/tracing";
4939
+ import { jsonKeyReplacer as jsonKeyReplacer2, nonNullable } from "@dxos/util";
4940
+
4887
4941
  // packages/sdk/client-services/src/packlets/services/service-host.ts
4888
4942
  import { Event as Event9, synchronized as synchronized3 } from "@dxos/async";
4889
- import { Properties, clientServiceBundle, defaultKey } from "@dxos/client-protocol";
4943
+ import { clientServiceBundle, defaultKey, Properties } from "@dxos/client-protocol";
4890
4944
  import { Context as Context11 } from "@dxos/context";
4891
- import { DataServiceImpl } from "@dxos/echo-pipeline";
4892
- import { getAutomergeObjectCore, getRawDoc } from "@dxos/echo-schema";
4945
+ import { DataServiceImpl, encodeReference } from "@dxos/echo-pipeline";
4946
+ import { getTypeReference } from "@dxos/echo-schema";
4893
4947
  import { IndexServiceImpl } from "@dxos/indexing";
4894
4948
  import { invariant as invariant16 } from "@dxos/invariant";
4895
- import { PublicKey as PublicKey12 } from "@dxos/keys";
4896
- import { log as log15 } from "@dxos/log";
4949
+ import { PublicKey as PublicKey13 } from "@dxos/keys";
4950
+ import { log as log16 } from "@dxos/log";
4897
4951
  import { WebsocketSignalManager } from "@dxos/messaging";
4898
4952
  import { NetworkManager, createSimplePeerTransportFactory } from "@dxos/network-manager";
4899
4953
  import { trace as trace9 } from "@dxos/protocols";
4900
4954
  import { SystemStatus } from "@dxos/protocols/proto/dxos/client/services";
4901
4955
  import { TRACE_PROCESSOR as TRACE_PROCESSOR2, trace as Trace3 } from "@dxos/tracing";
4902
- import { assignDeep } from "@dxos/util";
4956
+ import { assignDeep as assignDeep2 } from "@dxos/util";
4903
4957
  import { WebsocketRpcClient } from "@dxos/websocket-rpc";
4904
4958
 
4905
4959
  // packages/sdk/client-services/src/packlets/devices/devices-service.ts
@@ -4984,11 +5038,135 @@ var DevicesServiceImpl = class {
4984
5038
  }
4985
5039
  };
4986
5040
 
5041
+ // packages/sdk/client-services/src/packlets/locks/browser.ts
5042
+ import { asyncTimeout as asyncTimeout2, Trigger as Trigger7 } from "@dxos/async";
5043
+ import { RESOURCE_LOCK_TIMEOUT } from "@dxos/client-protocol";
5044
+ import { log as log14, logInfo } from "@dxos/log";
5045
+ function _ts_decorate7(decorators, target, key, desc) {
5046
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5047
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
5048
+ r = Reflect.decorate(decorators, target, key, desc);
5049
+ else
5050
+ for (var i = decorators.length - 1; i >= 0; i--)
5051
+ if (d = decorators[i])
5052
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5053
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
5054
+ }
5055
+ var __dxlog_file18 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/locks/browser.ts";
5056
+ var Message;
5057
+ (function(Message2) {
5058
+ Message2["ACQUIRING"] = "acquiring";
5059
+ })(Message || (Message = {}));
5060
+ var Lock = class {
5061
+ constructor({ lockKey, onAcquire, onRelease }) {
5062
+ this._broadcastChannel = new BroadcastChannel("vault-resource-lock");
5063
+ this._releaseTrigger = new Trigger7();
5064
+ this._lockKey = lockKey;
5065
+ this._onAcquire = onAcquire;
5066
+ this._onRelease = onRelease;
5067
+ this._broadcastChannel.onmessage = this._onMessage.bind(this);
5068
+ }
5069
+ get lockKey() {
5070
+ return this._lockKey;
5071
+ }
5072
+ async acquire() {
5073
+ this._broadcastChannel.postMessage({
5074
+ message: "acquiring"
5075
+ });
5076
+ try {
5077
+ log14("aquiring lock...", void 0, {
5078
+ F: __dxlog_file18,
5079
+ L: 42,
5080
+ S: this,
5081
+ C: (f, a) => f(...a)
5082
+ });
5083
+ await asyncTimeout2(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
5084
+ log14("acquired lock", void 0, {
5085
+ F: __dxlog_file18,
5086
+ L: 44,
5087
+ S: this,
5088
+ C: (f, a) => f(...a)
5089
+ });
5090
+ } catch {
5091
+ log14("stealing lock...", void 0, {
5092
+ F: __dxlog_file18,
5093
+ L: 46,
5094
+ S: this,
5095
+ C: (f, a) => f(...a)
5096
+ });
5097
+ await this._requestLock(true);
5098
+ log14("stolen lock", void 0, {
5099
+ F: __dxlog_file18,
5100
+ L: 48,
5101
+ S: this,
5102
+ C: (f, a) => f(...a)
5103
+ });
5104
+ }
5105
+ }
5106
+ async release() {
5107
+ this._releaseTrigger.wake();
5108
+ }
5109
+ _onMessage(event) {
5110
+ if (event.data.message === "acquiring") {
5111
+ this._releaseTrigger.wake();
5112
+ }
5113
+ }
5114
+ async _requestLock(steal = false) {
5115
+ log14("requesting lock...", {
5116
+ steal
5117
+ }, {
5118
+ F: __dxlog_file18,
5119
+ L: 63,
5120
+ S: this,
5121
+ C: (f, a) => f(...a)
5122
+ });
5123
+ const acquired = new Trigger7();
5124
+ void navigator.locks.request(this._lockKey, {
5125
+ steal
5126
+ }, async () => {
5127
+ await this._onAcquire?.();
5128
+ acquired.wake();
5129
+ this._releaseTrigger = new Trigger7();
5130
+ await this._releaseTrigger.wait();
5131
+ log14("releasing lock...", void 0, {
5132
+ F: __dxlog_file18,
5133
+ L: 72,
5134
+ S: this,
5135
+ C: (f, a) => f(...a)
5136
+ });
5137
+ await this._onRelease?.();
5138
+ log14("released lock", void 0, {
5139
+ F: __dxlog_file18,
5140
+ L: 74,
5141
+ S: this,
5142
+ C: (f, a) => f(...a)
5143
+ });
5144
+ }).catch(async () => {
5145
+ await this._onRelease?.();
5146
+ });
5147
+ await acquired.wait();
5148
+ log14("recieved lock", {
5149
+ steal
5150
+ }, {
5151
+ F: __dxlog_file18,
5152
+ L: 81,
5153
+ S: this,
5154
+ C: (f, a) => f(...a)
5155
+ });
5156
+ }
5157
+ };
5158
+ _ts_decorate7([
5159
+ logInfo
5160
+ ], Lock.prototype, "lockKey", null);
5161
+ var isLocked = (lockPath) => {
5162
+ throw new Error("Not implemented");
5163
+ };
5164
+
4987
5165
  // packages/sdk/client-services/src/packlets/logging/logging-service.ts
4988
5166
  import { Event as Event8 } from "@dxos/async";
4989
5167
  import { Stream as Stream12 } from "@dxos/codec-protobuf";
4990
5168
  import { PublicKey as PublicKey11 } from "@dxos/keys";
4991
- import { getContextFromEntry, log as log14 } from "@dxos/log";
5169
+ import { getContextFromEntry, log as log15 } from "@dxos/log";
4992
5170
  import { QueryLogsRequest } from "@dxos/protocols/proto/dxos/client/services";
4993
5171
  import { getDebugName, jsonify, numericalValues, tracer } from "@dxos/util";
4994
5172
  var LoggingServiceImpl = class {
@@ -5001,11 +5179,11 @@ var LoggingServiceImpl = class {
5001
5179
  };
5002
5180
  }
5003
5181
  async open() {
5004
- log14.runtimeConfig.processors.push(this._logProcessor);
5182
+ log15.runtimeConfig.processors.push(this._logProcessor);
5005
5183
  }
5006
5184
  async close() {
5007
- const index = log14.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
5008
- log14.runtimeConfig.processors.splice(index, 1);
5185
+ const index = log15.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
5186
+ log15.runtimeConfig.processors.splice(index, 1);
5009
5187
  }
5010
5188
  async controlMetrics({ reset, record }) {
5011
5189
  if (reset) {
@@ -5090,12 +5268,12 @@ var LoggingServiceImpl = class {
5090
5268
  });
5091
5269
  }
5092
5270
  };
5093
- var matchFilter = (filter, level, path, options) => {
5271
+ var matchFilter = (filter, level, path2, options) => {
5094
5272
  switch (options) {
5095
5273
  case QueryLogsRequest.MatchingOptions.INCLUSIVE:
5096
- return level >= filter.level && (!filter.pattern || path.includes(filter.pattern));
5274
+ return level >= filter.level && (!filter.pattern || path2.includes(filter.pattern));
5097
5275
  case QueryLogsRequest.MatchingOptions.EXPLICIT:
5098
- return level === filter.level && (!filter.pattern || path.includes(filter.pattern));
5276
+ return level === filter.level && (!filter.pattern || path2.includes(filter.pattern));
5099
5277
  }
5100
5278
  };
5101
5279
  var shouldLog = (entry2, request) => {
@@ -5141,6 +5319,80 @@ var NetworkServiceImpl = class {
5141
5319
  }
5142
5320
  };
5143
5321
 
5322
+ // packages/sdk/client-services/src/packlets/storage/storage.ts
5323
+ import { InvalidConfigError } from "@dxos/protocols";
5324
+ import { Runtime as Runtime2 } from "@dxos/protocols/proto/dxos/config";
5325
+ import { createStorage, StorageType } from "@dxos/random-access-storage";
5326
+
5327
+ // packages/sdk/client-services/src/packlets/storage/util.ts
5328
+ import { DX_DATA } from "@dxos/client-protocol";
5329
+ import { Runtime } from "@dxos/protocols/proto/dxos/config";
5330
+ import { isNode as isNode2 } from "@dxos/util";
5331
+ var getRootPath = (config) => {
5332
+ const { dataRoot = isNode2() ? DX_DATA : "dxos/storage" } = config ?? {};
5333
+ return `${dataRoot}/`;
5334
+ };
5335
+ var isPersistent = (config) => {
5336
+ const { persistent = false } = config ?? {};
5337
+ return config.dataStore !== void 0 && config.dataStore !== Runtime.Client.Storage.StorageDriver.RAM || persistent;
5338
+ };
5339
+
5340
+ // packages/sdk/client-services/src/packlets/storage/storage.ts
5341
+ var StorageDriver = Runtime2.Client.Storage.StorageDriver;
5342
+ var createStorageObjects = (config) => {
5343
+ const { persistent = false, keyStore, dataStore } = config ?? {};
5344
+ if (persistent && dataStore === StorageDriver.RAM) {
5345
+ throw new InvalidConfigError("RAM storage cannot be used in persistent mode.");
5346
+ }
5347
+ if (!persistent && dataStore !== void 0 && dataStore !== StorageDriver.RAM) {
5348
+ throw new InvalidConfigError("Cannot use a persistent storage in not persistent mode.");
5349
+ }
5350
+ if (persistent && keyStore === StorageDriver.RAM) {
5351
+ throw new InvalidConfigError("RAM key storage cannot be used in persistent mode.");
5352
+ }
5353
+ if (!persistent && keyStore !== StorageDriver.RAM && keyStore !== void 0) {
5354
+ throw new InvalidConfigError("Cannot use a persistent key storage in not persistent mode.");
5355
+ }
5356
+ return {
5357
+ storage: createStorage({
5358
+ type: persistent ? toStorageType(dataStore) : StorageType.RAM,
5359
+ root: getRootPath(config)
5360
+ })
5361
+ };
5362
+ };
5363
+ var toStorageType = (type) => {
5364
+ switch (type) {
5365
+ case void 0:
5366
+ return void 0;
5367
+ case StorageDriver.RAM:
5368
+ return StorageType.RAM;
5369
+ case StorageDriver.CHROME:
5370
+ return StorageType.CHROME;
5371
+ case StorageDriver.FIREFOX:
5372
+ return StorageType.FIREFOX;
5373
+ case StorageDriver.IDB:
5374
+ return StorageType.IDB;
5375
+ case StorageDriver.NODE:
5376
+ return StorageType.NODE;
5377
+ case StorageDriver.WEBFS:
5378
+ return StorageType.WEBFS;
5379
+ default:
5380
+ throw new Error(`Invalid storage type: ${StorageDriver[type]}`);
5381
+ }
5382
+ };
5383
+
5384
+ // packages/sdk/client-services/src/packlets/storage/level.ts
5385
+ import { Level } from "level";
5386
+ import path from "@dxos/node-std/path";
5387
+ import { PublicKey as PublicKey12 } from "@dxos/keys";
5388
+ var createLevel = async (config) => {
5389
+ const persistent = isPersistent(config);
5390
+ const storagePath = persistent ? path.join(getRootPath(config), "level") : `/tmp/dxos-${PublicKey12.random().toHex()}`;
5391
+ const level = new Level(storagePath);
5392
+ await level.open();
5393
+ return level;
5394
+ };
5395
+
5144
5396
  // packages/sdk/client-services/src/packlets/system/system-service.ts
5145
5397
  import { Stream as Stream14 } from "@dxos/codec-protobuf";
5146
5398
  import { GetDiagnosticsRequest } from "@dxos/protocols/proto/dxos/client/services";
@@ -5209,13 +5461,14 @@ function _ts_decorate8(decorators, target, key, desc) {
5209
5461
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5210
5462
  return c > 3 && r && Object.defineProperty(target, key, r), r;
5211
5463
  }
5212
- var __dxlog_file18 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-host.ts";
5464
+ var __dxlog_file19 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/services/service-host.ts";
5213
5465
  var ClientServicesHost = class {
5214
5466
  constructor({
5215
5467
  config,
5216
5468
  transportFactory,
5217
5469
  signalManager,
5218
5470
  storage,
5471
+ level,
5219
5472
  // TODO(wittjosiah): Turn this on by default.
5220
5473
  lockKey,
5221
5474
  callbacks,
@@ -5226,6 +5479,7 @@ var ClientServicesHost = class {
5226
5479
  this._opening = false;
5227
5480
  this._open = false;
5228
5481
  this._storage = storage;
5482
+ this._level = level;
5229
5483
  this._callbacks = callbacks;
5230
5484
  this._runtimeParams = runtimeParams;
5231
5485
  if (config) {
@@ -5264,6 +5518,7 @@ var ClientServicesHost = class {
5264
5518
  await this.reset();
5265
5519
  }
5266
5520
  });
5521
+ this.diagnosticsBroadcastHandler = createCollectDiagnosticsBroadcastHandler(this._systemService);
5267
5522
  this._loggingService = new LoggingServiceImpl();
5268
5523
  this._serviceRegistry = new ServiceRegistry(clientServiceBundle, {
5269
5524
  SystemService: this._systemService,
@@ -5295,24 +5550,24 @@ var ClientServicesHost = class {
5295
5550
  */
5296
5551
  initialize({ config, ...options }) {
5297
5552
  invariant16(!this._open, "service host is open", {
5298
- F: __dxlog_file18,
5299
- L: 179,
5553
+ F: __dxlog_file19,
5554
+ L: 197,
5300
5555
  S: this,
5301
5556
  A: [
5302
5557
  "!this._open",
5303
5558
  "'service host is open'"
5304
5559
  ]
5305
5560
  });
5306
- log15("initializing...", void 0, {
5307
- F: __dxlog_file18,
5308
- L: 180,
5561
+ log16("initializing...", void 0, {
5562
+ F: __dxlog_file19,
5563
+ L: 198,
5309
5564
  S: this,
5310
5565
  C: (f, a) => f(...a)
5311
5566
  });
5312
5567
  if (config) {
5313
5568
  invariant16(!this._config, "config already set", {
5314
- F: __dxlog_file18,
5315
- L: 183,
5569
+ F: __dxlog_file19,
5570
+ L: 201,
5316
5571
  S: this,
5317
5572
  A: [
5318
5573
  "!this._config",
@@ -5324,13 +5579,21 @@ var ClientServicesHost = class {
5324
5579
  this._storage = createStorageObjects(config.get("runtime.client.storage", {})).storage;
5325
5580
  }
5326
5581
  }
5582
+ if (!options.signalManager) {
5583
+ log16.warn("running signaling without telemetry metadata.", void 0, {
5584
+ F: __dxlog_file19,
5585
+ L: 209,
5586
+ S: this,
5587
+ C: (f, a) => f(...a)
5588
+ });
5589
+ }
5327
5590
  const { connectionLog = true, transportFactory = createSimplePeerTransportFactory({
5328
5591
  iceServers: this._config?.get("runtime.services.ice")
5329
5592
  }), signalManager = new WebsocketSignalManager(this._config?.get("runtime.services.signaling") ?? []) } = options;
5330
5593
  this._signalManager = signalManager;
5331
5594
  invariant16(!this._networkManager, "network manager already set", {
5332
- F: __dxlog_file18,
5333
- L: 199,
5595
+ F: __dxlog_file19,
5596
+ L: 220,
5334
5597
  S: this,
5335
5598
  A: [
5336
5599
  "!this._networkManager",
@@ -5342,9 +5605,9 @@ var ClientServicesHost = class {
5342
5605
  transportFactory,
5343
5606
  signalManager
5344
5607
  });
5345
- log15("initialized", void 0, {
5346
- F: __dxlog_file18,
5347
- L: 206,
5608
+ log16("initialized", void 0, {
5609
+ F: __dxlog_file19,
5610
+ L: 227,
5348
5611
  S: this,
5349
5612
  C: (f, a) => f(...a)
5350
5613
  });
@@ -5353,18 +5616,18 @@ var ClientServicesHost = class {
5353
5616
  if (this._open) {
5354
5617
  return;
5355
5618
  }
5356
- const traceId = PublicKey12.random().toHex();
5357
- log15.trace("dxos.client-services.host.open", trace9.begin({
5619
+ const traceId = PublicKey13.random().toHex();
5620
+ log16.trace("dxos.client-services.host.open", trace9.begin({
5358
5621
  id: traceId
5359
5622
  }), {
5360
- F: __dxlog_file18,
5361
- L: 217,
5623
+ F: __dxlog_file19,
5624
+ L: 238,
5362
5625
  S: this,
5363
5626
  C: (f, a) => f(...a)
5364
5627
  });
5365
5628
  invariant16(this._config, "config not set", {
5366
- F: __dxlog_file18,
5367
- L: 219,
5629
+ F: __dxlog_file19,
5630
+ L: 240,
5368
5631
  S: this,
5369
5632
  A: [
5370
5633
  "this._config",
@@ -5372,8 +5635,8 @@ var ClientServicesHost = class {
5372
5635
  ]
5373
5636
  });
5374
5637
  invariant16(this._storage, "storage not set", {
5375
- F: __dxlog_file18,
5376
- L: 220,
5638
+ F: __dxlog_file19,
5639
+ L: 241,
5377
5640
  S: this,
5378
5641
  A: [
5379
5642
  "this._storage",
@@ -5381,8 +5644,8 @@ var ClientServicesHost = class {
5381
5644
  ]
5382
5645
  });
5383
5646
  invariant16(this._signalManager, "signal manager not set", {
5384
- F: __dxlog_file18,
5385
- L: 221,
5647
+ F: __dxlog_file19,
5648
+ L: 242,
5386
5649
  S: this,
5387
5650
  A: [
5388
5651
  "this._signalManager",
@@ -5390,8 +5653,8 @@ var ClientServicesHost = class {
5390
5653
  ]
5391
5654
  });
5392
5655
  invariant16(this._networkManager, "network manager not set", {
5393
- F: __dxlog_file18,
5394
- L: 222,
5656
+ F: __dxlog_file19,
5657
+ L: 243,
5395
5658
  S: this,
5396
5659
  A: [
5397
5660
  "this._networkManager",
@@ -5399,17 +5662,21 @@ var ClientServicesHost = class {
5399
5662
  ]
5400
5663
  });
5401
5664
  this._opening = true;
5402
- log15("opening...", {
5665
+ log16("opening...", {
5403
5666
  lockKey: this._resourceLock?.lockKey
5404
5667
  }, {
5405
- F: __dxlog_file18,
5406
- L: 225,
5668
+ F: __dxlog_file19,
5669
+ L: 246,
5407
5670
  S: this,
5408
5671
  C: (f, a) => f(...a)
5409
5672
  });
5673
+ if (!this._level) {
5674
+ this._level = await createLevel(this._config.get("runtime.client.storage", {}));
5675
+ }
5676
+ await this._level.open();
5410
5677
  await this._resourceLock?.acquire();
5411
5678
  await this._loggingService.open();
5412
- this._serviceContext = new ServiceContext(this._storage, this._networkManager, this._signalManager, this._runtimeParams);
5679
+ this._serviceContext = new ServiceContext(this._storage, this._level, this._networkManager, this._signalManager, this._runtimeParams);
5413
5680
  this._serviceRegistry.setServices({
5414
5681
  SystemService: this._systemService,
5415
5682
  IdentityService: new IdentityServiceImpl((params) => this._createIdentity(params), this._serviceContext.identityManager, this._serviceContext.keyring, (profile) => this._serviceContext.broadcastProfileUpdate(profile)),
@@ -5436,8 +5703,8 @@ var ClientServicesHost = class {
5436
5703
  });
5437
5704
  await this._serviceContext.open(ctx);
5438
5705
  invariant16(this.serviceRegistry.services.InvitationsService, void 0, {
5439
- F: __dxlog_file18,
5440
- L: 286,
5706
+ F: __dxlog_file19,
5707
+ L: 314,
5441
5708
  S: this,
5442
5709
  A: [
5443
5710
  "this.serviceRegistry.services.InvitationsService",
@@ -5445,11 +5712,11 @@ var ClientServicesHost = class {
5445
5712
  ]
5446
5713
  });
5447
5714
  const loadedInvitations = await this.serviceRegistry.services.InvitationsService.loadPersistentInvitations();
5448
- log15("loaded persistent invitations", {
5715
+ log16("loaded persistent invitations", {
5449
5716
  count: loadedInvitations.invitations?.length
5450
5717
  }, {
5451
- F: __dxlog_file18,
5452
- L: 289,
5718
+ F: __dxlog_file19,
5719
+ L: 317,
5453
5720
  S: this,
5454
5721
  C: (f, a) => f(...a)
5455
5722
  });
@@ -5463,23 +5730,24 @@ var ClientServicesHost = class {
5463
5730
  });
5464
5731
  void this._devtoolsProxy.open();
5465
5732
  }
5733
+ this.diagnosticsBroadcastHandler.start();
5466
5734
  this._opening = false;
5467
5735
  this._open = true;
5468
5736
  this._statusUpdate.emit();
5469
5737
  const deviceKey = this._serviceContext.identityManager.identity?.deviceKey;
5470
- log15("opened", {
5738
+ log16("opened", {
5471
5739
  deviceKey
5472
5740
  }, {
5473
- F: __dxlog_file18,
5474
- L: 306,
5741
+ F: __dxlog_file19,
5742
+ L: 335,
5475
5743
  S: this,
5476
5744
  C: (f, a) => f(...a)
5477
5745
  });
5478
- log15.trace("dxos.client-services.host.open", trace9.end({
5746
+ log16.trace("dxos.client-services.host.open", trace9.end({
5479
5747
  id: traceId
5480
5748
  }), {
5481
- F: __dxlog_file18,
5482
- L: 307,
5749
+ F: __dxlog_file19,
5750
+ L: 336,
5483
5751
  S: this,
5484
5752
  C: (f, a) => f(...a)
5485
5753
  });
@@ -5489,60 +5757,62 @@ var ClientServicesHost = class {
5489
5757
  return;
5490
5758
  }
5491
5759
  const deviceKey = this._serviceContext.identityManager.identity?.deviceKey;
5492
- log15("closing...", {
5760
+ log16("closing...", {
5493
5761
  deviceKey
5494
5762
  }, {
5495
- F: __dxlog_file18,
5496
- L: 318,
5763
+ F: __dxlog_file19,
5764
+ L: 347,
5497
5765
  S: this,
5498
5766
  C: (f, a) => f(...a)
5499
5767
  });
5768
+ this.diagnosticsBroadcastHandler.stop();
5500
5769
  await this._devtoolsProxy?.close();
5501
5770
  this._serviceRegistry.setServices({
5502
5771
  SystemService: this._systemService
5503
5772
  });
5504
5773
  await this._loggingService.close();
5505
5774
  await this._serviceContext.close();
5775
+ await this._level?.close();
5506
5776
  this._open = false;
5507
5777
  this._statusUpdate.emit();
5508
- log15("closed", {
5778
+ log16("closed", {
5509
5779
  deviceKey
5510
5780
  }, {
5511
- F: __dxlog_file18,
5512
- L: 325,
5781
+ F: __dxlog_file19,
5782
+ L: 356,
5513
5783
  S: this,
5514
5784
  C: (f, a) => f(...a)
5515
5785
  });
5516
5786
  }
5517
5787
  async reset() {
5518
- const traceId = PublicKey12.random().toHex();
5519
- log15.trace("dxos.sdk.client-services-host.reset", trace9.begin({
5788
+ const traceId = PublicKey13.random().toHex();
5789
+ log16.trace("dxos.sdk.client-services-host.reset", trace9.begin({
5520
5790
  id: traceId
5521
5791
  }), {
5522
- F: __dxlog_file18,
5523
- L: 330,
5792
+ F: __dxlog_file19,
5793
+ L: 361,
5524
5794
  S: this,
5525
5795
  C: (f, a) => f(...a)
5526
5796
  });
5527
- log15("resetting...", void 0, {
5528
- F: __dxlog_file18,
5529
- L: 332,
5797
+ log16("resetting...", void 0, {
5798
+ F: __dxlog_file19,
5799
+ L: 363,
5530
5800
  S: this,
5531
5801
  C: (f, a) => f(...a)
5532
5802
  });
5533
5803
  await this._serviceContext?.close();
5534
5804
  await this._storage.reset();
5535
- log15("reset", void 0, {
5536
- F: __dxlog_file18,
5537
- L: 335,
5805
+ log16("reset", void 0, {
5806
+ F: __dxlog_file19,
5807
+ L: 366,
5538
5808
  S: this,
5539
5809
  C: (f, a) => f(...a)
5540
5810
  });
5541
- log15.trace("dxos.sdk.client-services-host.reset", trace9.end({
5811
+ log16.trace("dxos.sdk.client-services-host.reset", trace9.end({
5542
5812
  id: traceId
5543
5813
  }), {
5544
- F: __dxlog_file18,
5545
- L: 336,
5814
+ F: __dxlog_file19,
5815
+ L: 367,
5546
5816
  S: this,
5547
5817
  C: (f, a) => f(...a)
5548
5818
  });
@@ -5552,12 +5822,10 @@ var ClientServicesHost = class {
5552
5822
  const identity = await this._serviceContext.createIdentity(params);
5553
5823
  await this._serviceContext.initialized.wait();
5554
5824
  const space = await this._serviceContext.dataSpaceManager.createSpace();
5555
- const obj = new Properties(void 0);
5556
- obj[defaultKey] = identity.identityKey.toHex();
5557
5825
  const automergeIndex = space.automergeSpaceState.rootUrl;
5558
5826
  invariant16(automergeIndex, void 0, {
5559
- F: __dxlog_file18,
5560
- L: 351,
5827
+ F: __dxlog_file19,
5828
+ L: 379,
5561
5829
  S: this,
5562
5830
  A: [
5563
5831
  "automergeIndex",
@@ -5566,12 +5834,25 @@ var ClientServicesHost = class {
5566
5834
  });
5567
5835
  const document = await this._serviceContext.automergeHost.repo.find(automergeIndex);
5568
5836
  await document.whenReady();
5837
+ const properties = {
5838
+ system: {
5839
+ type: encodeReference(getTypeReference(Properties))
5840
+ },
5841
+ data: {
5842
+ [defaultKey]: identity.identityKey.toHex()
5843
+ },
5844
+ meta: {
5845
+ keys: []
5846
+ }
5847
+ };
5848
+ const propertiesId = PublicKey13.random().toHex();
5569
5849
  document.change((doc) => {
5570
- assignDeep(doc, [
5850
+ assignDeep2(doc, [
5571
5851
  "objects",
5572
- getAutomergeObjectCore(obj).id
5573
- ], getRawDoc(obj).handle.docSync());
5852
+ propertiesId
5853
+ ], properties);
5574
5854
  });
5855
+ await this._serviceContext.automergeHost.repo.flush();
5575
5856
  return identity;
5576
5857
  }
5577
5858
  };
@@ -5593,6 +5874,44 @@ ClientServicesHost = _ts_decorate8([
5593
5874
  Trace3.resource()
5594
5875
  ], ClientServicesHost);
5595
5876
 
5877
+ // packages/sdk/client-services/src/packlets/services/util.ts
5878
+ import { PublicKey as PublicKey14 } from "@dxos/keys";
5879
+ import { humanize } from "@dxos/util";
5880
+ var ClientServicesProviderResource = Symbol.for("dxos.resource.ClientServices");
5881
+
5882
+ // packages/sdk/client-services/src/packlets/diagnostics/diagnostics-collector.ts
5883
+ var DiagnosticsCollector = class {
5884
+ static {
5885
+ this.broadcastSender = createCollectDiagnosticsBroadcastSender();
5886
+ }
5887
+ static async collect(config = findConfigs(), services = findSystemServiceProvider(), options = {}) {
5888
+ const serviceDiagnostics = await services?.services?.SystemService?.getDiagnostics({
5889
+ keys: options.humanize ? GetDiagnosticsRequest2.KEY_OPTION.HUMANIZE : options.truncate ? GetDiagnosticsRequest2.KEY_OPTION.TRUNCATE : void 0
5890
+ });
5891
+ const clientDiagnostics = {
5892
+ config,
5893
+ trace: TRACE_PROCESSOR3.getDiagnostics()
5894
+ };
5895
+ const diagnostics = serviceDiagnostics != null ? {
5896
+ client: clientDiagnostics,
5897
+ services: serviceDiagnostics
5898
+ } : {
5899
+ client: clientDiagnostics,
5900
+ broadcast: await this.broadcastSender.broadcastDiagnosticsRequest()
5901
+ };
5902
+ return JSON.parse(JSON.stringify(diagnostics, jsonKeyReplacer2(options)));
5903
+ }
5904
+ };
5905
+ var findSystemServiceProvider = () => {
5906
+ const serviceProviders = TRACE_PROCESSOR3.findByAnnotation(ClientServicesProviderResource);
5907
+ const providerResource = serviceProviders.find((r) => r.instance.deref()?.services?.SystemService != null);
5908
+ return providerResource?.instance?.deref() ?? null;
5909
+ };
5910
+ var findConfigs = () => {
5911
+ const configs = TRACE_PROCESSOR3.findByAnnotation(ConfigResource);
5912
+ return configs.map((r) => r.instance.deref()).filter(nonNullable);
5913
+ };
5914
+
5596
5915
  export {
5597
5916
  Buffer,
5598
5917
  subscribeToFeeds,
@@ -5612,19 +5931,24 @@ export {
5612
5931
  IdentityServiceImpl,
5613
5932
  DeviceInvitationProtocol,
5614
5933
  InvitationsHandler,
5615
- invitationExpired,
5934
+ createAdmissionKeypair,
5616
5935
  InvitationsServiceImpl,
5617
5936
  SpaceInvitationProtocol,
5618
5937
  ClientRpcServer,
5619
- createDiagnostics,
5620
5938
  DataSpace,
5621
5939
  DataSpaceManager,
5622
5940
  SpacesServiceImpl,
5623
5941
  ServiceContext,
5624
5942
  ServiceRegistry,
5943
+ createDiagnostics,
5944
+ createCollectDiagnosticsBroadcastSender,
5945
+ createCollectDiagnosticsBroadcastHandler,
5946
+ DiagnosticsCollector,
5625
5947
  Lock,
5626
5948
  isLocked,
5627
5949
  createStorageObjects,
5628
- ClientServicesHost
5950
+ createLevel,
5951
+ ClientServicesHost,
5952
+ ClientServicesProviderResource
5629
5953
  };
5630
- //# sourceMappingURL=chunk-7PYX6UUA.mjs.map
5954
+ //# sourceMappingURL=chunk-7S34JE6M.mjs.map