@dxos/client-services 0.4.10-main.c42bfdb → 0.4.10-main.c75170d

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 (50) hide show
  1. package/dist/lib/browser/{chunk-W7UANCHR.mjs → chunk-JP7F2IH3.mjs} +513 -403
  2. package/dist/lib/browser/chunk-JP7F2IH3.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +5 -3
  4. package/dist/lib/browser/index.mjs.map +1 -1
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/packlets/testing/index.mjs +9 -4
  7. package/dist/lib/browser/packlets/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-JSVLZGJM.cjs → chunk-34EZSH65.cjs} +520 -411
  9. package/dist/lib/node/chunk-34EZSH65.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +45 -43
  11. package/dist/lib/node/index.cjs.map +1 -1
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/packlets/testing/index.cjs +14 -9
  14. package/dist/lib/node/packlets/testing/index.cjs.map +3 -3
  15. package/dist/types/src/packlets/indexing/util.d.ts +2 -1
  16. package/dist/types/src/packlets/indexing/util.d.ts.map +1 -1
  17. package/dist/types/src/packlets/invitations/index.d.ts +1 -0
  18. package/dist/types/src/packlets/invitations/index.d.ts.map +1 -1
  19. package/dist/types/src/packlets/invitations/invitation-extension.d.ts +1 -0
  20. package/dist/types/src/packlets/invitations/invitation-extension.d.ts.map +1 -1
  21. package/dist/types/src/packlets/invitations/invitations-handler.d.ts +4 -2
  22. package/dist/types/src/packlets/invitations/invitations-handler.d.ts.map +1 -1
  23. package/dist/types/src/packlets/invitations/invitations-manager.d.ts +42 -0
  24. package/dist/types/src/packlets/invitations/invitations-manager.d.ts.map +1 -0
  25. package/dist/types/src/packlets/invitations/invitations-service.d.ts +7 -23
  26. package/dist/types/src/packlets/invitations/invitations-service.d.ts.map +1 -1
  27. package/dist/types/src/packlets/services/service-context.d.ts +2 -0
  28. package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
  29. package/dist/types/src/packlets/services/service-host.d.ts +3 -1
  30. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  31. package/dist/types/src/packlets/testing/test-builder.d.ts +3 -1
  32. package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
  33. package/dist/types/src/version.d.ts +1 -1
  34. package/package.json +34 -34
  35. package/src/packlets/indexing/util.ts +2 -2
  36. package/src/packlets/invitations/index.ts +1 -0
  37. package/src/packlets/invitations/invitation-extension.ts +28 -1
  38. package/src/packlets/invitations/invitations-handler.ts +74 -34
  39. package/src/packlets/invitations/invitations-manager.ts +197 -0
  40. package/src/packlets/invitations/invitations-service.ts +21 -168
  41. package/src/packlets/services/automerge-host.test.ts +9 -3
  42. package/src/packlets/services/service-context.test.ts +4 -1
  43. package/src/packlets/services/service-context.ts +16 -3
  44. package/src/packlets/services/service-host.ts +15 -13
  45. package/src/packlets/spaces/data-space-manager.test.ts +4 -4
  46. package/src/packlets/storage/level.ts +1 -1
  47. package/src/packlets/testing/test-builder.ts +20 -4
  48. package/src/version.ts +1 -1
  49. package/dist/lib/browser/chunk-W7UANCHR.mjs.map +0 -7
  50. package/dist/lib/node/chunk-JSVLZGJM.cjs.map +0 -7
@@ -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
  });
@@ -1833,11 +1853,11 @@ var InvitationsHandler = class {
1833
1853
  });
1834
1854
  return extension;
1835
1855
  };
1836
- if (invitation.lifetime && invitation.created && invitation.lifetime !== 0) {
1856
+ if (invitation.lifetime && invitation.created) {
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: 255,
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: 266,
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: 274,
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: 277,
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: 285,
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: 295,
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: 304,
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: 312,
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: 316,
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: 320,
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: 336,
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: 347,
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: 349,
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: 352,
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: 355,
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: 358,
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: 369,
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: 372,
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: 386,
2180
2166
  S: this,
2181
2167
  A: [
2182
2168
  "invitation.swarmKey",
@@ -2214,206 +2200,115 @@ 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: 426,
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: 430,
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: 441,
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: 460,
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
- import { Event as Event3, scheduleTask as scheduleTask3 } from "@dxos/async";
2224
2281
  import { Stream as Stream8 } from "@dxos/codec-protobuf";
2225
- import { Context as Context6 } from "@dxos/context";
2226
- import { invariant as invariant7 } from "@dxos/invariant";
2227
- import { log as log6 } from "@dxos/log";
2228
- import { Invitation as Invitation4, QueryInvitationsResponse } from "@dxos/protocols/proto/dxos/client/services";
2229
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-service.ts";
2282
+ import { QueryInvitationsResponse } from "@dxos/protocols/proto/dxos/client/services";
2230
2283
  var InvitationsServiceImpl = class {
2231
- constructor(_invitationsHandler, _getHandler, _metadataStore) {
2232
- this._invitationsHandler = _invitationsHandler;
2233
- this._getHandler = _getHandler;
2234
- this._metadataStore = _metadataStore;
2235
- this._createInvitations = /* @__PURE__ */ new Map();
2236
- this._acceptInvitations = /* @__PURE__ */ new Map();
2237
- this._invitationCreated = new Event3();
2238
- this._invitationAccepted = new Event3();
2239
- this._removedCreated = new Event3();
2240
- this._removedAccepted = new Event3();
2241
- this._saved = new Event3();
2242
- this._persistentInvitationsLoadedEvent = new Event3();
2243
- this._persistentInvitationsLoaded = false;
2284
+ constructor(_invitationsManager) {
2285
+ this._invitationsManager = _invitationsManager;
2244
2286
  }
2245
2287
  // TODO(burdon): Guest/host label.
2246
2288
  getLoggingContext() {
2247
2289
  return {};
2248
2290
  }
2249
2291
  createInvitation(options) {
2250
- let invitation;
2251
- const savePersistentInvitationCtx = new Context6();
2252
- const existingInvitation = this._createInvitations.get(options.invitationId);
2253
- if (existingInvitation) {
2254
- invitation = existingInvitation;
2255
- } else {
2256
- const handler = this._getHandler(options);
2257
- invitation = this._invitationsHandler.createInvitation(handler, options);
2258
- this._createInvitations.set(invitation.get().invitationId, invitation);
2259
- this._invitationCreated.emit(invitation.get());
2260
- }
2292
+ const invitation = this._invitationsManager.createInvitation(options);
2261
2293
  return new Stream8(({ next, close }) => {
2262
- if (invitation.get().persistent) {
2263
- scheduleTask3(savePersistentInvitationCtx, async () => {
2264
- try {
2265
- await this._metadataStore.addInvitation(invitation.get());
2266
- this._saved.emit(invitation.get());
2267
- } catch (err) {
2268
- close(err);
2269
- }
2270
- });
2271
- }
2272
- invitation.subscribe((invitation2) => {
2273
- next(invitation2);
2274
- }, async (err) => {
2275
- await savePersistentInvitationCtx.dispose();
2276
- close(err);
2277
- }, async () => {
2278
- close();
2279
- if (invitation.get().persistent) {
2280
- await savePersistentInvitationCtx.dispose();
2281
- await this._metadataStore.removeInvitation(invitation.get().invitationId);
2282
- }
2283
- this._createInvitations.delete(invitation.get().invitationId);
2284
- if (invitation.get().type !== Invitation4.Type.MULTIUSE) {
2285
- this._removedCreated.emit(invitation.get());
2286
- }
2287
- });
2294
+ invitation.subscribe(next, close, close);
2288
2295
  });
2289
2296
  }
2290
- async loadPersistentInvitations() {
2291
- const persistentInvitations = this._metadataStore.getInvitations();
2292
- const freshInvitations = persistentInvitations.filter(async (invitation) => !invitationExpired(invitation));
2293
- const cInvitations = freshInvitations.map((persistentInvitation) => {
2294
- invariant7(!this._createInvitations.get(persistentInvitation.invitationId), "invitation already exists", {
2295
- F: __dxlog_file8,
2296
- L: 109,
2297
- S: this,
2298
- A: [
2299
- "!this._createInvitations.get(persistentInvitation.invitationId)",
2300
- "'invitation already exists'"
2301
- ]
2302
- });
2303
- const handler = this._getHandler(persistentInvitation);
2304
- const invitation = this._invitationsHandler.createInvitation(handler, persistentInvitation);
2305
- this._createInvitations.set(invitation.get().invitationId, invitation);
2306
- this._invitationCreated.emit(invitation.get());
2307
- return persistentInvitation;
2308
- });
2309
- this._persistentInvitationsLoadedEvent.emit();
2310
- this._persistentInvitationsLoaded = true;
2311
- return {
2312
- invitations: cInvitations
2313
- };
2314
- }
2315
- acceptInvitation({ invitation: options, deviceProfile }) {
2316
- let invitation;
2317
- if (deviceProfile) {
2318
- invariant7(options.kind === Invitation4.Kind.DEVICE, "deviceProfile provided for non-device invitation", {
2319
- F: __dxlog_file8,
2320
- L: 127,
2321
- S: this,
2322
- A: [
2323
- "options.kind === Invitation.Kind.DEVICE",
2324
- "'deviceProfile provided for non-device invitation'"
2325
- ]
2326
- });
2327
- }
2328
- const existingInvitation = this._acceptInvitations.get(options.invitationId);
2329
- if (existingInvitation) {
2330
- invitation = existingInvitation;
2331
- } else {
2332
- const handler = this._getHandler(options);
2333
- invitation = this._invitationsHandler.acceptInvitation(handler, options, deviceProfile);
2334
- this._acceptInvitations.set(invitation.get().invitationId, invitation);
2335
- this._invitationAccepted.emit(invitation.get());
2336
- }
2297
+ acceptInvitation(request) {
2298
+ const invitation = this._invitationsManager.acceptInvitation(request);
2337
2299
  return new Stream8(({ next, close }) => {
2338
- invitation.subscribe((invitation2) => {
2339
- next(invitation2);
2340
- }, (err) => {
2341
- close(err);
2342
- }, () => {
2343
- close();
2344
- this._acceptInvitations.delete(invitation.get().invitationId);
2345
- if (invitation.get().type !== Invitation4.Type.MULTIUSE) {
2346
- this._removedAccepted.emit(invitation.get());
2347
- }
2348
- });
2300
+ invitation.subscribe(next, close, close);
2349
2301
  });
2350
2302
  }
2351
- async authenticate({ invitationId, authCode }) {
2352
- log6("authenticating...", void 0, {
2353
- F: __dxlog_file8,
2354
- L: 160,
2355
- S: this,
2356
- C: (f, a) => f(...a)
2357
- });
2358
- invariant7(invitationId, void 0, {
2359
- F: __dxlog_file8,
2360
- L: 161,
2361
- S: this,
2362
- A: [
2363
- "invitationId",
2364
- ""
2365
- ]
2366
- });
2367
- const observable = this._acceptInvitations.get(invitationId);
2368
- if (!observable) {
2369
- log6.warn("invalid invitation", {
2370
- invitationId
2371
- }, {
2372
- F: __dxlog_file8,
2373
- L: 164,
2374
- S: this,
2375
- C: (f, a) => f(...a)
2376
- });
2377
- } else {
2378
- await observable.authenticate(authCode);
2379
- }
2303
+ async authenticate(request) {
2304
+ return this._invitationsManager.authenticate(request);
2380
2305
  }
2381
- async cancelInvitation({ invitationId }) {
2382
- log6("cancelInvitation...", {
2383
- invitationId
2384
- }, {
2385
- F: __dxlog_file8,
2386
- L: 171,
2387
- S: this,
2388
- C: (f, a) => f(...a)
2389
- });
2390
- invariant7(invitationId, void 0, {
2391
- F: __dxlog_file8,
2392
- L: 172,
2393
- S: this,
2394
- A: [
2395
- "invitationId",
2396
- ""
2397
- ]
2398
- });
2399
- const created = this._createInvitations.get(invitationId);
2400
- const accepted = this._acceptInvitations.get(invitationId);
2401
- if (created) {
2402
- await created.cancel();
2403
- this._createInvitations.delete(invitationId);
2404
- this._removedCreated.emit(created.get());
2405
- if (created.get().persistent) {
2406
- await this._metadataStore.removeInvitation(created.get().invitationId);
2407
- }
2408
- } else if (accepted) {
2409
- await accepted.cancel();
2410
- this._acceptInvitations.delete(invitationId);
2411
- this._removedAccepted.emit(accepted.get());
2412
- }
2306
+ async cancelInvitation(request) {
2307
+ return this._invitationsManager.cancelInvitation(request);
2413
2308
  }
2414
2309
  queryInvitations() {
2415
2310
  return new Stream8(({ next, ctx }) => {
2416
- this._invitationCreated.on(ctx, (invitation) => {
2311
+ this._invitationsManager.invitationCreated.on(ctx, (invitation) => {
2417
2312
  next({
2418
2313
  action: QueryInvitationsResponse.Action.ADDED,
2419
2314
  type: QueryInvitationsResponse.Type.CREATED,
@@ -2422,7 +2317,7 @@ var InvitationsServiceImpl = class {
2422
2317
  ]
2423
2318
  });
2424
2319
  });
2425
- this._invitationAccepted.on(ctx, (invitation) => {
2320
+ this._invitationsManager.invitationAccepted.on(ctx, (invitation) => {
2426
2321
  next({
2427
2322
  action: QueryInvitationsResponse.Action.ADDED,
2428
2323
  type: QueryInvitationsResponse.Type.ACCEPTED,
@@ -2431,7 +2326,7 @@ var InvitationsServiceImpl = class {
2431
2326
  ]
2432
2327
  });
2433
2328
  });
2434
- this._removedCreated.on(ctx, (invitation) => {
2329
+ this._invitationsManager.removedCreated.on(ctx, (invitation) => {
2435
2330
  next({
2436
2331
  action: QueryInvitationsResponse.Action.REMOVED,
2437
2332
  type: QueryInvitationsResponse.Type.CREATED,
@@ -2440,7 +2335,7 @@ var InvitationsServiceImpl = class {
2440
2335
  ]
2441
2336
  });
2442
2337
  });
2443
- this._removedAccepted.on(ctx, (invitation) => {
2338
+ this._invitationsManager.removedAccepted.on(ctx, (invitation) => {
2444
2339
  next({
2445
2340
  action: QueryInvitationsResponse.Action.REMOVED,
2446
2341
  type: QueryInvitationsResponse.Type.ACCEPTED,
@@ -2449,7 +2344,7 @@ var InvitationsServiceImpl = class {
2449
2344
  ]
2450
2345
  });
2451
2346
  });
2452
- this._saved.on(ctx, (invitation) => {
2347
+ this._invitationsManager.saved.on(ctx, (invitation) => {
2453
2348
  next({
2454
2349
  action: QueryInvitationsResponse.Action.SAVED,
2455
2350
  type: QueryInvitationsResponse.Type.CREATED,
@@ -2461,28 +2356,21 @@ var InvitationsServiceImpl = class {
2461
2356
  next({
2462
2357
  action: QueryInvitationsResponse.Action.ADDED,
2463
2358
  type: QueryInvitationsResponse.Type.CREATED,
2464
- invitations: Array.from(this._createInvitations.values()).map((invitation) => invitation.get()),
2359
+ invitations: this._invitationsManager.getCreatedInvitations(),
2465
2360
  existing: true
2466
2361
  });
2467
2362
  next({
2468
2363
  action: QueryInvitationsResponse.Action.ADDED,
2469
2364
  type: QueryInvitationsResponse.Type.ACCEPTED,
2470
- invitations: Array.from(this._acceptInvitations.values()).map((invitation) => invitation.get()),
2365
+ invitations: this._invitationsManager.getAcceptedInvitations(),
2471
2366
  existing: true
2472
2367
  });
2473
- if (this._persistentInvitationsLoaded) {
2368
+ this._invitationsManager.onPersistentInvitationsLoaded(ctx, () => {
2474
2369
  next({
2475
2370
  action: QueryInvitationsResponse.Action.LOAD_COMPLETE,
2476
2371
  type: QueryInvitationsResponse.Type.CREATED
2477
2372
  });
2478
- } else {
2479
- this._persistentInvitationsLoadedEvent.on(ctx, () => {
2480
- next({
2481
- action: QueryInvitationsResponse.Action.LOAD_COMPLETE,
2482
- type: QueryInvitationsResponse.Type.CREATED
2483
- });
2484
- });
2485
- }
2373
+ });
2486
2374
  });
2487
2375
  }
2488
2376
  };
@@ -2490,11 +2378,11 @@ var InvitationsServiceImpl = class {
2490
2378
  // packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts
2491
2379
  import { createAdmissionCredentials, getCredentialAssertion } from "@dxos/credentials";
2492
2380
  import { writeMessages as writeMessages2 } from "@dxos/feed-store";
2493
- import { invariant as invariant8 } from "@dxos/invariant";
2494
- import { log as log7 } from "@dxos/log";
2381
+ import { invariant as invariant7 } from "@dxos/invariant";
2382
+ import { log as log6 } from "@dxos/log";
2495
2383
  import { AlreadyJoinedError as AlreadyJoinedError2 } from "@dxos/protocols";
2496
- import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
2497
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts";
2384
+ import { Invitation as Invitation4 } from "@dxos/protocols/proto/dxos/client/services";
2385
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/space-invitation-protocol.ts";
2498
2386
  var SpaceInvitationProtocol = class {
2499
2387
  constructor(_spaceManager, _signingContext, _keyring, _spaceKey) {
2500
2388
  this._spaceManager = _spaceManager;
@@ -2510,13 +2398,13 @@ var SpaceInvitationProtocol = class {
2510
2398
  }
2511
2399
  getInvitationContext() {
2512
2400
  return {
2513
- kind: Invitation5.Kind.SPACE,
2401
+ kind: Invitation4.Kind.SPACE,
2514
2402
  spaceKey: this._spaceKey
2515
2403
  };
2516
2404
  }
2517
2405
  async admit(request, guestProfile) {
2518
- invariant8(this._spaceKey, void 0, {
2519
- F: __dxlog_file9,
2406
+ invariant7(this._spaceKey, void 0, {
2407
+ F: __dxlog_file8,
2520
2408
  L: 47,
2521
2409
  S: this,
2522
2410
  A: [
@@ -2525,8 +2413,8 @@ var SpaceInvitationProtocol = class {
2525
2413
  ]
2526
2414
  });
2527
2415
  const space = await this._spaceManager.spaces.get(this._spaceKey);
2528
- invariant8(space, void 0, {
2529
- F: __dxlog_file9,
2416
+ invariant7(space, void 0, {
2417
+ F: __dxlog_file8,
2530
2418
  L: 49,
2531
2419
  S: this,
2532
2420
  A: [
@@ -2534,8 +2422,8 @@ var SpaceInvitationProtocol = class {
2534
2422
  ""
2535
2423
  ]
2536
2424
  });
2537
- invariant8(request.space, void 0, {
2538
- F: __dxlog_file9,
2425
+ invariant7(request.space, void 0, {
2426
+ F: __dxlog_file8,
2539
2427
  L: 51,
2540
2428
  S: this,
2541
2429
  A: [
@@ -2544,18 +2432,18 @@ var SpaceInvitationProtocol = class {
2544
2432
  ]
2545
2433
  });
2546
2434
  const { identityKey, deviceKey } = request.space;
2547
- log7("writing guest credentials", {
2435
+ log6("writing guest credentials", {
2548
2436
  host: this._signingContext.deviceKey,
2549
2437
  guest: deviceKey
2550
2438
  }, {
2551
- F: __dxlog_file9,
2439
+ F: __dxlog_file8,
2552
2440
  L: 54,
2553
2441
  S: this,
2554
2442
  C: (f, a) => f(...a)
2555
2443
  });
2556
2444
  const credentials = await createAdmissionCredentials(this._signingContext.credentialSigner, identityKey, space.key, space.inner.genesisFeedKey, guestProfile);
2557
- invariant8(credentials[0].credential, void 0, {
2558
- F: __dxlog_file9,
2445
+ invariant7(credentials[0].credential, void 0, {
2446
+ F: __dxlog_file8,
2559
2447
  L: 65,
2560
2448
  S: this,
2561
2449
  A: [
@@ -2564,8 +2452,8 @@ var SpaceInvitationProtocol = class {
2564
2452
  ]
2565
2453
  });
2566
2454
  const spaceMemberCredential = credentials[0].credential.credential;
2567
- invariant8(getCredentialAssertion(spaceMemberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
2568
- F: __dxlog_file9,
2455
+ invariant7(getCredentialAssertion(spaceMemberCredential)["@type"] === "dxos.halo.credentials.SpaceMember", void 0, {
2456
+ F: __dxlog_file8,
2569
2457
  L: 67,
2570
2458
  S: this,
2571
2459
  A: [
@@ -2604,8 +2492,8 @@ var SpaceInvitationProtocol = class {
2604
2492
  };
2605
2493
  }
2606
2494
  async accept(response) {
2607
- invariant8(response.space, void 0, {
2608
- F: __dxlog_file9,
2495
+ invariant7(response.space, void 0, {
2496
+ F: __dxlog_file8,
2609
2497
  L: 107,
2610
2498
  S: this,
2611
2499
  A: [
@@ -2615,8 +2503,8 @@ var SpaceInvitationProtocol = class {
2615
2503
  });
2616
2504
  const { credential, controlTimeframe, dataTimeframe } = response.space;
2617
2505
  const assertion = getCredentialAssertion(credential);
2618
- invariant8(assertion["@type"] === "dxos.halo.credentials.SpaceMember", "Invalid credential", {
2619
- F: __dxlog_file9,
2506
+ invariant7(assertion["@type"] === "dxos.halo.credentials.SpaceMember", "Invalid credential", {
2507
+ F: __dxlog_file8,
2620
2508
  L: 110,
2621
2509
  S: this,
2622
2510
  A: [
@@ -2624,8 +2512,8 @@ var SpaceInvitationProtocol = class {
2624
2512
  "'Invalid credential'"
2625
2513
  ]
2626
2514
  });
2627
- invariant8(credential.subject.id.equals(this._signingContext.identityKey), void 0, {
2628
- F: __dxlog_file9,
2515
+ invariant7(credential.subject.id.equals(this._signingContext.identityKey), void 0, {
2516
+ F: __dxlog_file8,
2629
2517
  L: 111,
2630
2518
  S: this,
2631
2519
  A: [
@@ -2649,6 +2537,227 @@ var SpaceInvitationProtocol = class {
2649
2537
  }
2650
2538
  };
2651
2539
 
2540
+ // packages/sdk/client-services/src/packlets/invitations/invitations-manager.ts
2541
+ import { Event as Event3 } from "@dxos/async";
2542
+ import { hasInvitationExpired } from "@dxos/echo-pipeline";
2543
+ import { invariant as invariant8 } from "@dxos/invariant";
2544
+ import { log as log7 } from "@dxos/log";
2545
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/invitations/invitations-manager.ts";
2546
+ var InvitationsManager = class {
2547
+ constructor(_invitationsHandler, _getHandler, _metadataStore) {
2548
+ this._invitationsHandler = _invitationsHandler;
2549
+ this._getHandler = _getHandler;
2550
+ this._metadataStore = _metadataStore;
2551
+ this._createInvitations = /* @__PURE__ */ new Map();
2552
+ this._acceptInvitations = /* @__PURE__ */ new Map();
2553
+ this.invitationCreated = new Event3();
2554
+ this.invitationAccepted = new Event3();
2555
+ this.removedCreated = new Event3();
2556
+ this.removedAccepted = new Event3();
2557
+ this.saved = new Event3();
2558
+ this._persistentInvitationsLoadedEvent = new Event3();
2559
+ this._persistentInvitationsLoaded = false;
2560
+ }
2561
+ createInvitation(options) {
2562
+ const existingInvitation = this._createInvitations.get(options.invitationId);
2563
+ if (existingInvitation) {
2564
+ return existingInvitation;
2565
+ }
2566
+ const handler = this._getHandler(options);
2567
+ const invitation = this._invitationsHandler.createInvitation(handler, options);
2568
+ this._createInvitations.set(invitation.get().invitationId, invitation);
2569
+ this.invitationCreated.emit(invitation.get());
2570
+ const saveInvitationTask = invitation.get().persistent ? this._safePersistInBackground(invitation) : Promise.resolve();
2571
+ this._onInvitationComplete(invitation, async () => {
2572
+ this._createInvitations.delete(invitation.get().invitationId);
2573
+ this.removedCreated.emit(invitation.get());
2574
+ if (invitation.get().persistent) {
2575
+ await saveInvitationTask;
2576
+ await this._safeDeleteInvitation(invitation.get());
2577
+ }
2578
+ });
2579
+ return invitation;
2580
+ }
2581
+ async loadPersistentInvitations() {
2582
+ if (this._persistentInvitationsLoaded) {
2583
+ const invitations = this.getCreatedInvitations().filter((i) => i.persistent);
2584
+ return {
2585
+ invitations
2586
+ };
2587
+ }
2588
+ try {
2589
+ const persistentInvitations = this._metadataStore.getInvitations();
2590
+ const freshInvitations = persistentInvitations.filter((invitation) => !hasInvitationExpired(invitation));
2591
+ const cInvitations = freshInvitations.map((persistentInvitation) => {
2592
+ invariant8(!this._createInvitations.get(persistentInvitation.invitationId), "invitation already exists", {
2593
+ F: __dxlog_file9,
2594
+ L: 82,
2595
+ S: this,
2596
+ A: [
2597
+ "!this._createInvitations.get(persistentInvitation.invitationId)",
2598
+ "'invitation already exists'"
2599
+ ]
2600
+ });
2601
+ return this.createInvitation({
2602
+ ...persistentInvitation,
2603
+ persistent: false
2604
+ }).get();
2605
+ });
2606
+ return {
2607
+ invitations: cInvitations
2608
+ };
2609
+ } catch (err) {
2610
+ log7.catch(err, void 0, {
2611
+ F: __dxlog_file9,
2612
+ L: 88,
2613
+ S: this,
2614
+ C: (f, a) => f(...a)
2615
+ });
2616
+ return {
2617
+ invitations: []
2618
+ };
2619
+ } finally {
2620
+ this._persistentInvitationsLoadedEvent.emit();
2621
+ this._persistentInvitationsLoaded = true;
2622
+ }
2623
+ }
2624
+ acceptInvitation(request) {
2625
+ const options = request.invitation;
2626
+ const existingInvitation = this._acceptInvitations.get(options.invitationId);
2627
+ if (existingInvitation) {
2628
+ return existingInvitation;
2629
+ }
2630
+ const handler = this._getHandler(options);
2631
+ const invitation = this._invitationsHandler.acceptInvitation(handler, options, request.deviceProfile);
2632
+ this._acceptInvitations.set(invitation.get().invitationId, invitation);
2633
+ this.invitationAccepted.emit(invitation.get());
2634
+ this._onInvitationComplete(invitation, () => {
2635
+ this._acceptInvitations.delete(invitation.get().invitationId);
2636
+ this.removedAccepted.emit(invitation.get());
2637
+ });
2638
+ return invitation;
2639
+ }
2640
+ async authenticate({ invitationId, authCode }) {
2641
+ log7("authenticating...", void 0, {
2642
+ F: __dxlog_file9,
2643
+ L: 117,
2644
+ S: this,
2645
+ C: (f, a) => f(...a)
2646
+ });
2647
+ invariant8(invitationId, void 0, {
2648
+ F: __dxlog_file9,
2649
+ L: 118,
2650
+ S: this,
2651
+ A: [
2652
+ "invitationId",
2653
+ ""
2654
+ ]
2655
+ });
2656
+ const observable = this._acceptInvitations.get(invitationId);
2657
+ if (!observable) {
2658
+ log7.warn("invalid invitation", {
2659
+ invitationId
2660
+ }, {
2661
+ F: __dxlog_file9,
2662
+ L: 121,
2663
+ S: this,
2664
+ C: (f, a) => f(...a)
2665
+ });
2666
+ } else {
2667
+ await observable.authenticate(authCode);
2668
+ }
2669
+ }
2670
+ async cancelInvitation({ invitationId }) {
2671
+ log7("cancelInvitation...", {
2672
+ invitationId
2673
+ }, {
2674
+ F: __dxlog_file9,
2675
+ L: 128,
2676
+ S: this,
2677
+ C: (f, a) => f(...a)
2678
+ });
2679
+ invariant8(invitationId, void 0, {
2680
+ F: __dxlog_file9,
2681
+ L: 129,
2682
+ S: this,
2683
+ A: [
2684
+ "invitationId",
2685
+ ""
2686
+ ]
2687
+ });
2688
+ const created = this._createInvitations.get(invitationId);
2689
+ if (created) {
2690
+ if (created.get().persistent) {
2691
+ await this._metadataStore.removeInvitation(invitationId);
2692
+ }
2693
+ await created.cancel();
2694
+ this._createInvitations.delete(invitationId);
2695
+ this.removedCreated.emit(created.get());
2696
+ return;
2697
+ }
2698
+ const accepted = this._acceptInvitations.get(invitationId);
2699
+ if (accepted) {
2700
+ await accepted.cancel();
2701
+ this._acceptInvitations.delete(invitationId);
2702
+ this.removedAccepted.emit(accepted.get());
2703
+ }
2704
+ }
2705
+ getCreatedInvitations() {
2706
+ return [
2707
+ ...this._createInvitations.values()
2708
+ ].map((i) => i.get());
2709
+ }
2710
+ getAcceptedInvitations() {
2711
+ return [
2712
+ ...this._acceptInvitations.values()
2713
+ ].map((i) => i.get());
2714
+ }
2715
+ onPersistentInvitationsLoaded(ctx, callback) {
2716
+ if (this._persistentInvitationsLoaded) {
2717
+ callback();
2718
+ } else {
2719
+ this._persistentInvitationsLoadedEvent.once(ctx, () => callback());
2720
+ }
2721
+ }
2722
+ _safePersistInBackground(invitation) {
2723
+ return new Promise((resolve) => {
2724
+ setTimeout(async () => {
2725
+ try {
2726
+ await this._metadataStore.addInvitation(invitation.get());
2727
+ this.saved.emit(invitation.get());
2728
+ } catch (err) {
2729
+ log7.catch(err, void 0, {
2730
+ F: __dxlog_file9,
2731
+ L: 173,
2732
+ S: this,
2733
+ C: (f, a) => f(...a)
2734
+ });
2735
+ await invitation.cancel();
2736
+ } finally {
2737
+ resolve();
2738
+ }
2739
+ });
2740
+ });
2741
+ }
2742
+ async _safeDeleteInvitation(invitation) {
2743
+ try {
2744
+ await this._metadataStore.removeInvitation(invitation.invitationId);
2745
+ } catch (err) {
2746
+ log7.catch(err, void 0, {
2747
+ F: __dxlog_file9,
2748
+ L: 186,
2749
+ S: this,
2750
+ C: (f, a) => f(...a)
2751
+ });
2752
+ }
2753
+ }
2754
+ _onInvitationComplete(invitation, callback) {
2755
+ invitation.subscribe(() => {
2756
+ }, () => {
2757
+ }, callback);
2758
+ }
2759
+ };
2760
+
2652
2761
  // packages/sdk/client-services/src/packlets/services/client-rpc-server.ts
2653
2762
  import { Stream as Stream9 } from "@dxos/codec-protobuf";
2654
2763
  import { raise } from "@dxos/debug";
@@ -2731,9 +2840,9 @@ ClientRpcServer = _ts_decorate3([
2731
2840
  ], ClientRpcServer);
2732
2841
 
2733
2842
  // packages/sdk/client-services/src/packlets/spaces/data-space.ts
2734
- import { Event as Event6, asyncTimeout, scheduleTask as scheduleTask5, sleep as sleep2, synchronized, trackLeaks } from "@dxos/async";
2843
+ import { Event as Event6, asyncTimeout, scheduleTask as scheduleTask4, sleep as sleep2, synchronized, trackLeaks } from "@dxos/async";
2735
2844
  import { AUTH_TIMEOUT as AUTH_TIMEOUT2 } from "@dxos/client-protocol";
2736
- import { cancelWithContext as cancelWithContext2, Context as Context8, ContextDisposedError } from "@dxos/context";
2845
+ import { cancelWithContext as cancelWithContext2, Context as Context7, ContextDisposedError } from "@dxos/context";
2737
2846
  import { timed, warnAfterTimeout } from "@dxos/debug";
2738
2847
  import { TYPE_PROPERTIES } from "@dxos/echo-db";
2739
2848
  import { createMappedFeedWriter } from "@dxos/echo-pipeline";
@@ -2787,8 +2896,8 @@ var AutomergeSpaceState = class {
2787
2896
  };
2788
2897
 
2789
2898
  // packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts
2790
- import { DeferredTask, Event as Event5, scheduleTask as scheduleTask4, sleep, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
2791
- import { Context as Context7, rejectOnDispose } from "@dxos/context";
2899
+ import { DeferredTask, Event as Event5, scheduleTask as scheduleTask3, sleep, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
2900
+ import { Context as Context6, rejectOnDispose } from "@dxos/context";
2792
2901
  import { invariant as invariant9 } from "@dxos/invariant";
2793
2902
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2794
2903
  import { log as log8 } from "@dxos/log";
@@ -2802,7 +2911,7 @@ var DEFAULT_NOTARIZE_TIMEOUT = 1e4;
2802
2911
  var WRITER_NOT_SET_ERROR_CODE = "WRITER_NOT_SET";
2803
2912
  var NotarizationPlugin = class {
2804
2913
  constructor() {
2805
- this._ctx = new Context7();
2914
+ this._ctx = new Context6();
2806
2915
  this._extensionOpened = new Event5();
2807
2916
  this._extensions = /* @__PURE__ */ new Set();
2808
2917
  this._processedCredentials = new ComplexSet2(PublicKey7.hash);
@@ -2854,7 +2963,7 @@ var NotarizationPlugin = class {
2854
2963
  });
2855
2964
  opCtx?.onDispose(() => ctx.dispose());
2856
2965
  if (timeout !== 0) {
2857
- scheduleTask4(ctx, () => {
2966
+ scheduleTask3(ctx, () => {
2858
2967
  log8.warn("Notarization timeout", {
2859
2968
  timeout,
2860
2969
  peers: Array.from(this._extensions).map((extension) => extension.remotePeerId)
@@ -2888,7 +2997,7 @@ var NotarizationPlugin = class {
2888
2997
  C: (f, a) => f(...a)
2889
2998
  });
2890
2999
  peersTried.clear();
2891
- scheduleTask4(ctx, () => notarizeTask.schedule(), retryTimeout);
3000
+ scheduleTask3(ctx, () => notarizeTask.schedule(), retryTimeout);
2892
3001
  return;
2893
3002
  }
2894
3003
  peersTried.add(peer);
@@ -3068,7 +3177,7 @@ function _ts_decorate4(decorators, target, key, desc) {
3068
3177
  var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/sdk/client-services/src/packlets/spaces/data-space.ts";
3069
3178
  var DataSpace = class {
3070
3179
  constructor(params) {
3071
- this._ctx = new Context8();
3180
+ this._ctx = new Context7();
3072
3181
  this._notarizationPlugin = new NotarizationPlugin();
3073
3182
  this._cache = void 0;
3074
3183
  // TODO(dmaretskyi): Move into Space?
@@ -3145,7 +3254,7 @@ var DataSpace = class {
3145
3254
  await this._notarizationPlugin.open();
3146
3255
  await this._inner.spaceState.addCredentialProcessor(this._notarizationPlugin);
3147
3256
  await this._inner.spaceState.addCredentialProcessor(this._automergeSpaceState);
3148
- await this._inner.open(new Context8());
3257
+ await this._inner.open(new Context7());
3149
3258
  this._state = SpaceState.CONTROL_ONLY;
3150
3259
  log9("new state", {
3151
3260
  state: SpaceState[this._state]
@@ -3174,7 +3283,7 @@ var DataSpace = class {
3174
3283
  C: (f, a) => f(...a)
3175
3284
  });
3176
3285
  await this._ctx.dispose();
3177
- this._ctx = new Context8();
3286
+ this._ctx = new Context7();
3178
3287
  await this.authVerifier.close();
3179
3288
  await this._inner.close();
3180
3289
  await this._inner.spaceState.removeCredentialProcessor(this._automergeSpaceState);
@@ -3193,7 +3302,7 @@ var DataSpace = class {
3193
3302
  * Initialize the data pipeline in a separate task.
3194
3303
  */
3195
3304
  initializeDataPipelineAsync() {
3196
- scheduleTask5(this._ctx, async () => {
3305
+ scheduleTask4(this._ctx, async () => {
3197
3306
  try {
3198
3307
  this.metrics.pipelineInitBegin = /* @__PURE__ */ new Date();
3199
3308
  await this.initializeDataPipeline();
@@ -3595,7 +3704,7 @@ DataSpace = _ts_decorate4([
3595
3704
 
3596
3705
  // packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts
3597
3706
  import { Event as Event7, synchronized as synchronized2, trackLeaks as trackLeaks2 } from "@dxos/async";
3598
- import { Context as Context9, cancelWithContext as cancelWithContext3 } from "@dxos/context";
3707
+ import { Context as Context8, cancelWithContext as cancelWithContext3 } from "@dxos/context";
3599
3708
  import { getCredentialAssertion as getCredentialAssertion2 } from "@dxos/credentials";
3600
3709
  import { invariant as invariant11 } from "@dxos/invariant";
3601
3710
  import { PublicKey as PublicKey9 } from "@dxos/keys";
@@ -3697,7 +3806,7 @@ var DataSpaceManager = class {
3697
3806
  this._signingContext = _signingContext;
3698
3807
  this._feedStore = _feedStore;
3699
3808
  this._automergeHost = _automergeHost;
3700
- this._ctx = new Context9();
3809
+ this._ctx = new Context8();
3701
3810
  this.updated = new Event7();
3702
3811
  this._spaces = new ComplexMap3(PublicKey9.hash);
3703
3812
  this._isOpen = false;
@@ -4018,7 +4127,7 @@ DataSpaceManager = _ts_decorate5([
4018
4127
  ], DataSpaceManager);
4019
4128
 
4020
4129
  // packages/sdk/client-services/src/packlets/spaces/spaces-service.ts
4021
- import { EventSubscriptions as EventSubscriptions2, UpdateScheduler, scheduleTask as scheduleTask6 } from "@dxos/async";
4130
+ import { EventSubscriptions as EventSubscriptions2, UpdateScheduler, scheduleTask as scheduleTask5 } from "@dxos/async";
4022
4131
  import { Stream as Stream10 } from "@dxos/codec-protobuf";
4023
4132
  import { raise as raise2 } from "@dxos/debug";
4024
4133
  import { invariant as invariant12 } from "@dxos/invariant";
@@ -4075,7 +4184,7 @@ var SpacesServiceImpl = class {
4075
4184
  }, {
4076
4185
  maxFrequency: false ? void 0 : 2
4077
4186
  });
4078
- scheduleTask6(ctx, async () => {
4187
+ scheduleTask5(ctx, async () => {
4079
4188
  const dataSpaceManager = await this._getDataSpaceManager();
4080
4189
  const subscriptions = new EventSubscriptions2();
4081
4190
  ctx.onDispose(() => subscriptions.clear());
@@ -4109,7 +4218,7 @@ var SpacesServiceImpl = class {
4109
4218
  }
4110
4219
  subscribeMessages({ spaceKey, channel }) {
4111
4220
  return new Stream10(({ ctx, next }) => {
4112
- scheduleTask6(ctx, async () => {
4221
+ scheduleTask5(ctx, async () => {
4113
4222
  const dataSpaceManager = await this._getDataSpaceManager();
4114
4223
  const space = dataSpaceManager.spaces.get(spaceKey) ?? raise2(new SpaceNotFoundError(spaceKey));
4115
4224
  const handle = space.listen(getChannelId(channel), (message) => {
@@ -4128,7 +4237,7 @@ var SpacesServiceImpl = class {
4128
4237
  }
4129
4238
  };
4130
4239
  ctx.onDispose(() => space.spaceState.removeCredentialProcessor(processor));
4131
- scheduleTask6(ctx, async () => {
4240
+ scheduleTask5(ctx, async () => {
4132
4241
  await space.spaceState.addCredentialProcessor(processor);
4133
4242
  if (noTail) {
4134
4243
  close();
@@ -4236,18 +4345,18 @@ var getChannelId = (channel) => `user-channel/${channel}`;
4236
4345
 
4237
4346
  // packages/sdk/client-services/src/packlets/services/service-context.ts
4238
4347
  import { Trigger as Trigger5 } from "@dxos/async";
4239
- import { Context as Context10, Resource } from "@dxos/context";
4348
+ import { Context as Context9, Resource } from "@dxos/context";
4240
4349
  import { getCredentialAssertion as getCredentialAssertion3 } from "@dxos/credentials";
4241
4350
  import { failUndefined as failUndefined2 } from "@dxos/debug";
4242
4351
  import { AutomergeHost, MetadataStore, SnapshotStore, SpaceManager, valueEncoding } from "@dxos/echo-pipeline";
4243
4352
  import { FeedFactory, FeedStore } from "@dxos/feed-store";
4244
- import { IndexMetadataStore, IndexStore, Indexer } from "@dxos/indexing";
4353
+ import { IndexMetadataStore, IndexStore, Indexer, createStorageCallbacks } from "@dxos/indexing";
4245
4354
  import { invariant as invariant13 } from "@dxos/invariant";
4246
4355
  import { Keyring } from "@dxos/keyring";
4247
4356
  import { PublicKey as PublicKey10 } from "@dxos/keys";
4248
4357
  import { log as log12 } from "@dxos/log";
4249
4358
  import { InvalidStorageVersionError, STORAGE_VERSION, trace as trace8 } from "@dxos/protocols";
4250
- import { Invitation as Invitation6 } from "@dxos/protocols/proto/dxos/client/services";
4359
+ import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
4251
4360
  import { BlobStore } from "@dxos/teleport-extension-object-sync";
4252
4361
  import { trace as Trace2 } from "@dxos/tracing";
4253
4362
  import { safeInstanceof } from "@dxos/util";
@@ -4381,24 +4490,29 @@ var ServiceContext = class extends Resource {
4381
4490
  });
4382
4491
  this.automergeHost = new AutomergeHost({
4383
4492
  directory: storage.createDirectory("automerge"),
4384
- metadata: this.indexMetadata
4493
+ db: level.sublevel("automerge"),
4494
+ storageCallbacks: createStorageCallbacks({
4495
+ host: () => this.automergeHost,
4496
+ metadata: this.indexMetadata
4497
+ })
4385
4498
  });
4386
4499
  this.indexer = new Indexer({
4387
4500
  indexStore: new IndexStore({
4388
- directory: storage.createDirectory("index-store")
4501
+ db: level.sublevel("index-storage")
4389
4502
  }),
4390
4503
  metadataStore: this.indexMetadata,
4391
4504
  loadDocuments: createSelectedDocumentsIterator(this.automergeHost),
4392
4505
  getAllDocuments: createDocumentsIterator(this.automergeHost)
4393
4506
  });
4394
4507
  this.invitations = new InvitationsHandler(this.networkManager);
4395
- this._handlerFactories.set(Invitation6.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => this.identityManager.identity ?? failUndefined2(), this._acceptIdentity.bind(this)));
4508
+ this.invitationsManager = new InvitationsManager(this.invitations, (invitation) => this.getInvitationHandler(invitation), this.metadataStore);
4509
+ this._handlerFactories.set(Invitation5.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => this.identityManager.identity ?? failUndefined2(), this._acceptIdentity.bind(this)));
4396
4510
  }
4397
4511
  async _open(ctx) {
4398
4512
  await this._checkStorageVersion();
4399
4513
  log12("opening...", void 0, {
4400
4514
  F: __dxlog_file14,
4401
- L: 156,
4515
+ L: 164,
4402
4516
  S: this,
4403
4517
  C: (f, a) => f(...a)
4404
4518
  });
@@ -4406,29 +4520,39 @@ var ServiceContext = class extends Resource {
4406
4520
  id: this._instanceId
4407
4521
  }), {
4408
4522
  F: __dxlog_file14,
4409
- L: 157,
4523
+ L: 165,
4410
4524
  S: this,
4411
4525
  C: (f, a) => f(...a)
4412
4526
  });
4413
4527
  await this.signalManager.open();
4414
4528
  await this.networkManager.open();
4529
+ await this.automergeHost.open();
4415
4530
  await this.metadataStore.load();
4416
4531
  await this.spaceManager.open();
4417
4532
  await this.identityManager.open(ctx);
4418
4533
  if (this.identityManager.identity) {
4419
4534
  await this._initialize(ctx);
4420
4535
  }
4536
+ const loadedInvitations = await this.invitationsManager.loadPersistentInvitations();
4537
+ log12("loaded persistent invitations", {
4538
+ count: loadedInvitations.invitations?.length
4539
+ }, {
4540
+ F: __dxlog_file14,
4541
+ L: 178,
4542
+ S: this,
4543
+ C: (f, a) => f(...a)
4544
+ });
4421
4545
  log12.trace("dxos.sdk.service-context.open", trace8.end({
4422
4546
  id: this._instanceId
4423
4547
  }), {
4424
4548
  F: __dxlog_file14,
4425
- L: 167,
4549
+ L: 180,
4426
4550
  S: this,
4427
4551
  C: (f, a) => f(...a)
4428
4552
  });
4429
4553
  log12("opened", void 0, {
4430
4554
  F: __dxlog_file14,
4431
- L: 168,
4555
+ L: 181,
4432
4556
  S: this,
4433
4557
  C: (f, a) => f(...a)
4434
4558
  });
@@ -4436,7 +4560,7 @@ var ServiceContext = class extends Resource {
4436
4560
  async _close() {
4437
4561
  log12("closing...", void 0, {
4438
4562
  F: __dxlog_file14,
4439
- L: 172,
4563
+ L: 185,
4440
4564
  S: this,
4441
4565
  C: (f, a) => f(...a)
4442
4566
  });
@@ -4454,21 +4578,21 @@ var ServiceContext = class extends Resource {
4454
4578
  await this.indexer.destroy();
4455
4579
  log12("closed", void 0, {
4456
4580
  F: __dxlog_file14,
4457
- L: 185,
4581
+ L: 198,
4458
4582
  S: this,
4459
4583
  C: (f, a) => f(...a)
4460
4584
  });
4461
4585
  }
4462
4586
  async createIdentity(params = {}) {
4463
4587
  const identity = await this.identityManager.createIdentity(params);
4464
- await this._initialize(new Context10());
4588
+ await this._initialize(new Context9());
4465
4589
  return identity;
4466
4590
  }
4467
4591
  getInvitationHandler(invitation) {
4468
4592
  const factory = this._handlerFactories.get(invitation.kind);
4469
4593
  invariant13(factory, `Unknown invitation kind: ${invitation.kind}`, {
4470
4594
  F: __dxlog_file14,
4471
- L: 196,
4595
+ L: 209,
4472
4596
  S: this,
4473
4597
  A: [
4474
4598
  "factory",
@@ -4487,7 +4611,7 @@ var ServiceContext = class extends Resource {
4487
4611
  }
4488
4612
  async _acceptIdentity(params) {
4489
4613
  const identity = await this.identityManager.acceptIdentity(params);
4490
- await this._initialize(new Context10());
4614
+ await this._initialize(new Context9());
4491
4615
  return identity;
4492
4616
  }
4493
4617
  async _checkStorageVersion() {
@@ -4500,7 +4624,7 @@ var ServiceContext = class extends Resource {
4500
4624
  async _initialize(ctx) {
4501
4625
  log12("initializing spaces...", void 0, {
4502
4626
  F: __dxlog_file14,
4503
- L: 227,
4627
+ L: 240,
4504
4628
  S: this,
4505
4629
  C: (f, a) => f(...a)
4506
4630
  });
@@ -4520,10 +4644,10 @@ var ServiceContext = class extends Resource {
4520
4644
  };
4521
4645
  this.dataSpaceManager = new DataSpaceManager(this.spaceManager, this.metadataStore, this.keyring, signingContext, this.feedStore, this.automergeHost, this._runtimeParams);
4522
4646
  await this.dataSpaceManager.open();
4523
- this._handlerFactories.set(Invitation6.Kind.SPACE, (invitation) => {
4647
+ this._handlerFactories.set(Invitation5.Kind.SPACE, (invitation) => {
4524
4648
  invariant13(this.dataSpaceManager, "dataSpaceManager not initialized yet", {
4525
4649
  F: __dxlog_file14,
4526
- L: 251,
4650
+ L: 264,
4527
4651
  S: this,
4528
4652
  A: [
4529
4653
  "this.dataSpaceManager",
@@ -4547,7 +4671,7 @@ var ServiceContext = class extends Resource {
4547
4671
  details: assertion
4548
4672
  }, {
4549
4673
  F: __dxlog_file14,
4550
- L: 267,
4674
+ L: 280,
4551
4675
  S: this,
4552
4676
  C: (f, a) => f(...a)
4553
4677
  });
@@ -4558,7 +4682,7 @@ var ServiceContext = class extends Resource {
4558
4682
  details: assertion
4559
4683
  }, {
4560
4684
  F: __dxlog_file14,
4561
- L: 271,
4685
+ L: 284,
4562
4686
  S: this,
4563
4687
  C: (f, a) => f(...a)
4564
4688
  });
@@ -4569,7 +4693,7 @@ var ServiceContext = class extends Resource {
4569
4693
  details: assertion
4570
4694
  }, {
4571
4695
  F: __dxlog_file14,
4572
- L: 276,
4696
+ L: 289,
4573
4697
  S: this,
4574
4698
  C: (f, a) => f(...a)
4575
4699
  });
@@ -4580,7 +4704,7 @@ var ServiceContext = class extends Resource {
4580
4704
  } catch (err) {
4581
4705
  log12.catch(err, void 0, {
4582
4706
  F: __dxlog_file14,
4583
- L: 282,
4707
+ L: 295,
4584
4708
  S: this,
4585
4709
  C: (f, a) => f(...a)
4586
4710
  });
@@ -4634,7 +4758,7 @@ import { SpaceMember as SpaceMember3 } from "@dxos/protocols/proto/dxos/client/s
4634
4758
  import { TRACE_PROCESSOR } from "@dxos/tracing";
4635
4759
 
4636
4760
  // packages/sdk/client-services/src/version.ts
4637
- var DXOS_VERSION = "0.4.10-main.c42bfdb";
4761
+ var DXOS_VERSION = "0.4.10-main.c75170d";
4638
4762
 
4639
4763
  // packages/sdk/client-services/src/packlets/services/platform.ts
4640
4764
  import { Platform } from "@dxos/protocols/proto/dxos/client/services";
@@ -4877,9 +5001,9 @@ import { jsonKeyReplacer as jsonKeyReplacer2, nonNullable } from "@dxos/util";
4877
5001
  // packages/sdk/client-services/src/packlets/services/service-host.ts
4878
5002
  import { Event as Event9, synchronized as synchronized3 } from "@dxos/async";
4879
5003
  import { clientServiceBundle, defaultKey, Properties } from "@dxos/client-protocol";
4880
- import { Context as Context11 } from "@dxos/context";
5004
+ import { Context as Context10 } from "@dxos/context";
4881
5005
  import { DataServiceImpl, encodeReference } from "@dxos/echo-pipeline";
4882
- import * as E from "@dxos/echo-schema";
5006
+ import { getTypeReference } from "@dxos/echo-schema";
4883
5007
  import { IndexServiceImpl } from "@dxos/indexing";
4884
5008
  import { invariant as invariant16 } from "@dxos/invariant";
4885
5009
  import { PublicKey as PublicKey13 } from "@dxos/keys";
@@ -5323,7 +5447,7 @@ import path from "@dxos/node-std/path";
5323
5447
  import { PublicKey as PublicKey12 } from "@dxos/keys";
5324
5448
  var createLevel = async (config) => {
5325
5449
  const persistent = isPersistent(config);
5326
- const storagePath = persistent ? getRootPath(config) : path.join("tmp", "level", PublicKey12.random().toHex());
5450
+ const storagePath = persistent ? path.join(getRootPath(config), "level") : `/tmp/dxos-${PublicKey12.random().toHex()}`;
5327
5451
  const level = new Level(storagePath);
5328
5452
  await level.open();
5329
5453
  return level;
@@ -5404,6 +5528,7 @@ var ClientServicesHost = class {
5404
5528
  transportFactory,
5405
5529
  signalManager,
5406
5530
  storage,
5531
+ level,
5407
5532
  // TODO(wittjosiah): Turn this on by default.
5408
5533
  lockKey,
5409
5534
  callbacks,
@@ -5414,6 +5539,7 @@ var ClientServicesHost = class {
5414
5539
  this._opening = false;
5415
5540
  this._open = false;
5416
5541
  this._storage = storage;
5542
+ this._level = level;
5417
5543
  this._callbacks = callbacks;
5418
5544
  this._runtimeParams = runtimeParams;
5419
5545
  if (config) {
@@ -5428,7 +5554,7 @@ var ClientServicesHost = class {
5428
5554
  lockKey,
5429
5555
  onAcquire: () => {
5430
5556
  if (!this._opening) {
5431
- void this.open(new Context11());
5557
+ void this.open(new Context10());
5432
5558
  }
5433
5559
  },
5434
5560
  onRelease: () => this.close()
@@ -5485,7 +5611,7 @@ var ClientServicesHost = class {
5485
5611
  initialize({ config, ...options }) {
5486
5612
  invariant16(!this._open, "service host is open", {
5487
5613
  F: __dxlog_file19,
5488
- L: 188,
5614
+ L: 197,
5489
5615
  S: this,
5490
5616
  A: [
5491
5617
  "!this._open",
@@ -5494,14 +5620,14 @@ var ClientServicesHost = class {
5494
5620
  });
5495
5621
  log16("initializing...", void 0, {
5496
5622
  F: __dxlog_file19,
5497
- L: 189,
5623
+ L: 198,
5498
5624
  S: this,
5499
5625
  C: (f, a) => f(...a)
5500
5626
  });
5501
5627
  if (config) {
5502
5628
  invariant16(!this._config, "config already set", {
5503
5629
  F: __dxlog_file19,
5504
- L: 192,
5630
+ L: 201,
5505
5631
  S: this,
5506
5632
  A: [
5507
5633
  "!this._config",
@@ -5516,7 +5642,7 @@ var ClientServicesHost = class {
5516
5642
  if (!options.signalManager) {
5517
5643
  log16.warn("running signaling without telemetry metadata.", void 0, {
5518
5644
  F: __dxlog_file19,
5519
- L: 200,
5645
+ L: 209,
5520
5646
  S: this,
5521
5647
  C: (f, a) => f(...a)
5522
5648
  });
@@ -5527,7 +5653,7 @@ var ClientServicesHost = class {
5527
5653
  this._signalManager = signalManager;
5528
5654
  invariant16(!this._networkManager, "network manager already set", {
5529
5655
  F: __dxlog_file19,
5530
- L: 211,
5656
+ L: 220,
5531
5657
  S: this,
5532
5658
  A: [
5533
5659
  "!this._networkManager",
@@ -5541,7 +5667,7 @@ var ClientServicesHost = class {
5541
5667
  });
5542
5668
  log16("initialized", void 0, {
5543
5669
  F: __dxlog_file19,
5544
- L: 218,
5670
+ L: 227,
5545
5671
  S: this,
5546
5672
  C: (f, a) => f(...a)
5547
5673
  });
@@ -5555,13 +5681,13 @@ var ClientServicesHost = class {
5555
5681
  id: traceId
5556
5682
  }), {
5557
5683
  F: __dxlog_file19,
5558
- L: 229,
5684
+ L: 238,
5559
5685
  S: this,
5560
5686
  C: (f, a) => f(...a)
5561
5687
  });
5562
5688
  invariant16(this._config, "config not set", {
5563
5689
  F: __dxlog_file19,
5564
- L: 231,
5690
+ L: 240,
5565
5691
  S: this,
5566
5692
  A: [
5567
5693
  "this._config",
@@ -5570,7 +5696,7 @@ var ClientServicesHost = class {
5570
5696
  });
5571
5697
  invariant16(this._storage, "storage not set", {
5572
5698
  F: __dxlog_file19,
5573
- L: 232,
5699
+ L: 241,
5574
5700
  S: this,
5575
5701
  A: [
5576
5702
  "this._storage",
@@ -5579,7 +5705,7 @@ var ClientServicesHost = class {
5579
5705
  });
5580
5706
  invariant16(this._signalManager, "signal manager not set", {
5581
5707
  F: __dxlog_file19,
5582
- L: 233,
5708
+ L: 242,
5583
5709
  S: this,
5584
5710
  A: [
5585
5711
  "this._signalManager",
@@ -5588,7 +5714,7 @@ var ClientServicesHost = class {
5588
5714
  });
5589
5715
  invariant16(this._networkManager, "network manager not set", {
5590
5716
  F: __dxlog_file19,
5591
- L: 234,
5717
+ L: 243,
5592
5718
  S: this,
5593
5719
  A: [
5594
5720
  "this._networkManager",
@@ -5600,20 +5726,21 @@ var ClientServicesHost = class {
5600
5726
  lockKey: this._resourceLock?.lockKey
5601
5727
  }, {
5602
5728
  F: __dxlog_file19,
5603
- L: 237,
5729
+ L: 246,
5604
5730
  S: this,
5605
5731
  C: (f, a) => f(...a)
5606
5732
  });
5607
5733
  if (!this._level) {
5608
5734
  this._level = await createLevel(this._config.get("runtime.client.storage", {}));
5609
5735
  }
5736
+ await this._level.open();
5610
5737
  await this._resourceLock?.acquire();
5611
5738
  await this._loggingService.open();
5612
5739
  this._serviceContext = new ServiceContext(this._storage, this._level, this._networkManager, this._signalManager, this._runtimeParams);
5613
5740
  this._serviceRegistry.setServices({
5614
5741
  SystemService: this._systemService,
5615
5742
  IdentityService: new IdentityServiceImpl((params) => this._createIdentity(params), this._serviceContext.identityManager, this._serviceContext.keyring, (profile) => this._serviceContext.broadcastProfileUpdate(profile)),
5616
- InvitationsService: new InvitationsServiceImpl(this._serviceContext.invitations, (invitation) => this._serviceContext.getInvitationHandler(invitation), this._serviceContext.metadataStore),
5743
+ InvitationsService: new InvitationsServiceImpl(this._serviceContext.invitationsManager),
5617
5744
  DevicesService: new DevicesServiceImpl(this._serviceContext.identityManager),
5618
5745
  SpacesService: new SpacesServiceImpl(this._serviceContext.identityManager, this._serviceContext.spaceManager, async () => {
5619
5746
  await this._serviceContext.initialized.wait();
@@ -5635,24 +5762,6 @@ var ClientServicesHost = class {
5635
5762
  })
5636
5763
  });
5637
5764
  await this._serviceContext.open(ctx);
5638
- invariant16(this.serviceRegistry.services.InvitationsService, void 0, {
5639
- F: __dxlog_file19,
5640
- L: 303,
5641
- S: this,
5642
- A: [
5643
- "this.serviceRegistry.services.InvitationsService",
5644
- ""
5645
- ]
5646
- });
5647
- const loadedInvitations = await this.serviceRegistry.services.InvitationsService.loadPersistentInvitations();
5648
- log16("loaded persistent invitations", {
5649
- count: loadedInvitations.invitations?.length
5650
- }, {
5651
- F: __dxlog_file19,
5652
- L: 306,
5653
- S: this,
5654
- C: (f, a) => f(...a)
5655
- });
5656
5765
  const devtoolsProxy = this._config?.get("runtime.client.devtoolsProxy");
5657
5766
  if (devtoolsProxy) {
5658
5767
  this._devtoolsProxy = new WebsocketRpcClient({
@@ -5672,7 +5781,7 @@ var ClientServicesHost = class {
5672
5781
  deviceKey
5673
5782
  }, {
5674
5783
  F: __dxlog_file19,
5675
- L: 324,
5784
+ L: 326,
5676
5785
  S: this,
5677
5786
  C: (f, a) => f(...a)
5678
5787
  });
@@ -5680,7 +5789,7 @@ var ClientServicesHost = class {
5680
5789
  id: traceId
5681
5790
  }), {
5682
5791
  F: __dxlog_file19,
5683
- L: 325,
5792
+ L: 327,
5684
5793
  S: this,
5685
5794
  C: (f, a) => f(...a)
5686
5795
  });
@@ -5694,7 +5803,7 @@ var ClientServicesHost = class {
5694
5803
  deviceKey
5695
5804
  }, {
5696
5805
  F: __dxlog_file19,
5697
- L: 336,
5806
+ L: 338,
5698
5807
  S: this,
5699
5808
  C: (f, a) => f(...a)
5700
5809
  });
@@ -5712,7 +5821,7 @@ var ClientServicesHost = class {
5712
5821
  deviceKey
5713
5822
  }, {
5714
5823
  F: __dxlog_file19,
5715
- L: 345,
5824
+ L: 347,
5716
5825
  S: this,
5717
5826
  C: (f, a) => f(...a)
5718
5827
  });
@@ -5723,13 +5832,13 @@ var ClientServicesHost = class {
5723
5832
  id: traceId
5724
5833
  }), {
5725
5834
  F: __dxlog_file19,
5726
- L: 350,
5835
+ L: 352,
5727
5836
  S: this,
5728
5837
  C: (f, a) => f(...a)
5729
5838
  });
5730
5839
  log16("resetting...", void 0, {
5731
5840
  F: __dxlog_file19,
5732
- L: 352,
5841
+ L: 354,
5733
5842
  S: this,
5734
5843
  C: (f, a) => f(...a)
5735
5844
  });
@@ -5737,7 +5846,7 @@ var ClientServicesHost = class {
5737
5846
  await this._storage.reset();
5738
5847
  log16("reset", void 0, {
5739
5848
  F: __dxlog_file19,
5740
- L: 355,
5849
+ L: 357,
5741
5850
  S: this,
5742
5851
  C: (f, a) => f(...a)
5743
5852
  });
@@ -5745,7 +5854,7 @@ var ClientServicesHost = class {
5745
5854
  id: traceId
5746
5855
  }), {
5747
5856
  F: __dxlog_file19,
5748
- L: 356,
5857
+ L: 358,
5749
5858
  S: this,
5750
5859
  C: (f, a) => f(...a)
5751
5860
  });
@@ -5758,7 +5867,7 @@ var ClientServicesHost = class {
5758
5867
  const automergeIndex = space.automergeSpaceState.rootUrl;
5759
5868
  invariant16(automergeIndex, void 0, {
5760
5869
  F: __dxlog_file19,
5761
- L: 368,
5870
+ L: 370,
5762
5871
  S: this,
5763
5872
  A: [
5764
5873
  "automergeIndex",
@@ -5769,7 +5878,7 @@ var ClientServicesHost = class {
5769
5878
  await document.whenReady();
5770
5879
  const properties = {
5771
5880
  system: {
5772
- type: encodeReference(E.getTypeReference(Properties))
5881
+ type: encodeReference(getTypeReference(Properties))
5773
5882
  },
5774
5883
  data: {
5775
5884
  [defaultKey]: identity.identityKey.toHex()
@@ -5864,9 +5973,10 @@ export {
5864
5973
  IdentityServiceImpl,
5865
5974
  DeviceInvitationProtocol,
5866
5975
  InvitationsHandler,
5867
- invitationExpired,
5976
+ createAdmissionKeypair,
5868
5977
  InvitationsServiceImpl,
5869
5978
  SpaceInvitationProtocol,
5979
+ InvitationsManager,
5870
5980
  ClientRpcServer,
5871
5981
  DataSpace,
5872
5982
  DataSpaceManager,
@@ -5884,4 +5994,4 @@ export {
5884
5994
  ClientServicesHost,
5885
5995
  ClientServicesProviderResource
5886
5996
  };
5887
- //# sourceMappingURL=chunk-W7UANCHR.mjs.map
5997
+ //# sourceMappingURL=chunk-JP7F2IH3.mjs.map