@minca/core 0.1.7 → 0.1.9

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 (154) hide show
  1. package/dist/elements/auth-styles.d.ts +1 -1
  2. package/dist/elements/auth-styles.d.ts.map +1 -1
  3. package/dist/elements/auth-styles.js +16 -8
  4. package/dist/elements/auth-styles.js.map +1 -1
  5. package/dist/elements/minca-auth.d.ts +7 -0
  6. package/dist/elements/minca-auth.d.ts.map +1 -1
  7. package/dist/elements/minca-auth.js +107 -49
  8. package/dist/elements/minca-auth.js.map +1 -1
  9. package/dist/files/file-id.d.ts +51 -0
  10. package/dist/files/file-id.d.ts.map +1 -0
  11. package/dist/files/file-id.js +97 -0
  12. package/dist/files/file-id.js.map +1 -0
  13. package/dist/files/seal.d.ts +20 -0
  14. package/dist/files/seal.d.ts.map +1 -0
  15. package/dist/files/seal.js +77 -0
  16. package/dist/files/seal.js.map +1 -0
  17. package/dist/files/space-files.d.ts +51 -0
  18. package/dist/files/space-files.d.ts.map +1 -0
  19. package/dist/files/space-files.js +476 -0
  20. package/dist/files/space-files.js.map +1 -0
  21. package/dist/index.d.ts +5 -4
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -2
  24. package/dist/index.js.map +1 -1
  25. package/dist/node/api.d.ts +6 -1
  26. package/dist/node/api.d.ts.map +1 -1
  27. package/dist/node/api.js +17 -3
  28. package/dist/node/api.js.map +1 -1
  29. package/dist/node/carrier.d.ts +17 -2
  30. package/dist/node/carrier.d.ts.map +1 -1
  31. package/dist/node/carrier.js +26 -4
  32. package/dist/node/carrier.js.map +1 -1
  33. package/dist/node/copy.d.ts +0 -4
  34. package/dist/node/copy.d.ts.map +1 -1
  35. package/dist/node/copy.js +26 -0
  36. package/dist/node/copy.js.map +1 -1
  37. package/dist/node/host.d.ts +66 -1
  38. package/dist/node/host.d.ts.map +1 -1
  39. package/dist/node/host.js +571 -5
  40. package/dist/node/host.js.map +1 -1
  41. package/dist/node/index.d.ts +2 -2
  42. package/dist/node/index.d.ts.map +1 -1
  43. package/dist/node/index.js +1 -1
  44. package/dist/node/index.js.map +1 -1
  45. package/dist/node/node.d.ts.map +1 -1
  46. package/dist/node/node.js +235 -75
  47. package/dist/node/node.js.map +1 -1
  48. package/dist/node/space-runtime.d.ts +9 -0
  49. package/dist/node/space-runtime.d.ts.map +1 -1
  50. package/dist/node/space-runtime.js +125 -16
  51. package/dist/node/space-runtime.js.map +1 -1
  52. package/dist/node/stores.d.ts +44 -8
  53. package/dist/node/stores.d.ts.map +1 -1
  54. package/dist/node/stores.js +44 -7
  55. package/dist/node/stores.js.map +1 -1
  56. package/dist/node/types.d.ts +73 -6
  57. package/dist/node/types.d.ts.map +1 -1
  58. package/dist/node/usage.d.ts +54 -0
  59. package/dist/node/usage.d.ts.map +1 -0
  60. package/dist/node/usage.js +201 -0
  61. package/dist/node/usage.js.map +1 -0
  62. package/dist/node/writer-keys.d.ts +4 -1
  63. package/dist/node/writer-keys.d.ts.map +1 -1
  64. package/dist/node/writer-keys.js +25 -6
  65. package/dist/node/writer-keys.js.map +1 -1
  66. package/dist/node/writers.d.ts.map +1 -1
  67. package/dist/node/writers.js +10 -3
  68. package/dist/node/writers.js.map +1 -1
  69. package/dist/react/minca-auth.d.ts +3 -1
  70. package/dist/react/minca-auth.d.ts.map +1 -1
  71. package/dist/react/minca-auth.js +5 -3
  72. package/dist/react/minca-auth.js.map +1 -1
  73. package/dist/schema/version.d.ts +5 -0
  74. package/dist/schema/version.d.ts.map +1 -1
  75. package/dist/schema/version.js +1 -0
  76. package/dist/schema/version.js.map +1 -1
  77. package/dist/schemas/fragments.d.ts +23 -2
  78. package/dist/schemas/fragments.d.ts.map +1 -1
  79. package/dist/schemas/fragments.js +5 -2
  80. package/dist/schemas/fragments.js.map +1 -1
  81. package/dist/schemas/library/annotations.d.ts +24 -0
  82. package/dist/schemas/library/annotations.d.ts.map +1 -1
  83. package/dist/schemas/library/community.d.ts +24 -0
  84. package/dist/schemas/library/community.d.ts.map +1 -1
  85. package/dist/schemas/library/life.d.ts +72 -0
  86. package/dist/schemas/library/life.d.ts.map +1 -1
  87. package/dist/schemas/library/media.d.ts +144 -0
  88. package/dist/schemas/library/media.d.ts.map +1 -1
  89. package/dist/schemas/library/money.d.ts +24 -0
  90. package/dist/schemas/library/money.d.ts.map +1 -1
  91. package/dist/schemas/library/publishing.d.ts +96 -0
  92. package/dist/schemas/library/publishing.d.ts.map +1 -1
  93. package/dist/schemas/library/social.d.ts +72 -0
  94. package/dist/schemas/library/social.d.ts.map +1 -1
  95. package/dist/session/account-hint.d.ts +13 -0
  96. package/dist/session/account-hint.d.ts.map +1 -0
  97. package/dist/session/account-hint.js +51 -0
  98. package/dist/session/account-hint.js.map +1 -0
  99. package/dist/session/auth.d.ts +51 -3
  100. package/dist/session/auth.d.ts.map +1 -1
  101. package/dist/session/auth.js +262 -36
  102. package/dist/session/auth.js.map +1 -1
  103. package/dist/session/connect.d.ts.map +1 -1
  104. package/dist/session/connect.js +6 -2
  105. package/dist/session/connect.js.map +1 -1
  106. package/dist/session/hosting.d.ts +73 -28
  107. package/dist/session/hosting.d.ts.map +1 -1
  108. package/dist/session/hosting.js +85 -5
  109. package/dist/session/hosting.js.map +1 -1
  110. package/dist/session/index.d.ts +1 -0
  111. package/dist/session/index.d.ts.map +1 -1
  112. package/dist/session/index.js +1 -0
  113. package/dist/session/index.js.map +1 -1
  114. package/dist/session/pairing.d.ts +2 -0
  115. package/dist/session/pairing.d.ts.map +1 -1
  116. package/dist/session/pairing.js +8 -0
  117. package/dist/session/pairing.js.map +1 -1
  118. package/dist/session/places.d.ts +7 -0
  119. package/dist/session/places.d.ts.map +1 -1
  120. package/dist/session/places.js +20 -2
  121. package/dist/session/places.js.map +1 -1
  122. package/dist/space/account-registry.d.ts +13 -0
  123. package/dist/space/account-registry.d.ts.map +1 -1
  124. package/dist/space/account-registry.js +13 -0
  125. package/dist/space/account-registry.js.map +1 -1
  126. package/dist/storage/blob/directory.d.ts +14 -0
  127. package/dist/storage/blob/directory.d.ts.map +1 -0
  128. package/dist/storage/blob/directory.js +80 -0
  129. package/dist/storage/blob/directory.js.map +1 -0
  130. package/dist/storage/folder-adapter.d.ts +22 -0
  131. package/dist/storage/folder-adapter.d.ts.map +1 -1
  132. package/dist/storage/folder-adapter.js +5 -5
  133. package/dist/storage/folder-adapter.js.map +1 -1
  134. package/dist/storage/mirror.d.ts +9 -2
  135. package/dist/storage/mirror.d.ts.map +1 -1
  136. package/dist/storage/mirror.js +60 -35
  137. package/dist/storage/mirror.js.map +1 -1
  138. package/dist/storage/segment.d.ts +7 -2
  139. package/dist/storage/segment.d.ts.map +1 -1
  140. package/dist/storage/segment.js +25 -3
  141. package/dist/storage/segment.js.map +1 -1
  142. package/dist/sync/sync-engine.d.ts +2 -0
  143. package/dist/sync/sync-engine.d.ts.map +1 -1
  144. package/dist/sync/sync-engine.js +5 -0
  145. package/dist/sync/sync-engine.js.map +1 -1
  146. package/dist/sync/sync-messages.d.ts +24 -0
  147. package/dist/sync/sync-messages.d.ts.map +1 -1
  148. package/dist/sync/sync-messages.js.map +1 -1
  149. package/docs/node.md +63 -24
  150. package/docs/sign-in.md +123 -24
  151. package/docs/spaces.md +18 -10
  152. package/docs/standard-library.md +16 -20
  153. package/docs/storage.md +120 -15
  154. package/package.json +1 -1
package/dist/node/node.js CHANGED
@@ -37,7 +37,7 @@ import { contact as contactSchema, contactRequest as contactRequestSchema, door
37
37
  import { checkAnswer, clip, doorTopic, encodeDoorCode, KNOCK_TTL_SECONDS, knockId, MAX_DOOR_RELAYS, openKnock, parseDoorCode, sealKnock, signAnswer, signPurge, } from '../doors/doors.js';
38
38
  import { createMailboxClient } from '../network/mailbox.js';
39
39
  import { team } from '../space/presets.js';
40
- import { CARRIER_COLLECTION, deriveAccountRegistry, deriveContactsSpace, MEMBERSHIP_COLLECTION, PROFILE_COLLECTION, PROFILE_KEY, } from '../space/account-registry.js';
40
+ import { CARRIER_COLLECTION, deriveAccountRegistry, deriveContactsSpace, MEMBERSHIP_COLLECTION, PROFILE_COLLECTION, PROFILE_KEY, isAvatarRef, } from '../space/account-registry.js';
41
41
  import { CARRY_CLOSED_KEY, makePass, PASS_COLLECTION, passKey } from '../space/pass.js';
42
42
  import { checkOwnAutomation, OWN_AUTOMATIONS, WAKE_COLLECTION, linkedWakes, wakeFor, } from '../space/own-automations.js';
43
43
  import { createHostClient, connectorOffers, createSpaceHostClient, describeHost, hostPeerAddress, HOSTING_COLLECTION, HostError, newSubscriptionSeed, readStatus, subscriptionKey, } from '../session/hosting.js';
@@ -252,9 +252,14 @@ export async function createNode(config) {
252
252
  if (!accountSpaceId)
253
253
  return null;
254
254
  const profile = await (await runtime(accountSpaceId)).get(PROFILE_KEY);
255
- return profile?.verified && profile.root === config.signer.did && typeof profile.body?.name === 'string'
256
- ? { name: profile.body.name, updatedAt: profile.updatedAt }
257
- : null;
255
+ if (!profile?.verified || profile.root !== config.signer.did || typeof profile.body?.name !== 'string')
256
+ return null;
257
+ const avatar = profile.body.avatar;
258
+ return {
259
+ name: profile.body.name,
260
+ updatedAt: profile.updatedAt,
261
+ ...(isAvatarRef(avatar) ? { avatar } : {}),
262
+ };
258
263
  }
259
264
  const ownName = async () => (await ownProfile())?.name ?? null;
260
265
  /** Whether a space is the account's own machinery: its registry, its contacts, a carry space */
@@ -267,10 +272,47 @@ export async function createNode(config) {
267
272
  async function publishProfile(spaceId, open) {
268
273
  if (spaceId === accountSpaceId || spaceId === contactsSpaceId || narrowNote)
269
274
  return;
270
- const name = await ownName();
271
- if (name)
272
- await open.publishProfile({ name, ...(contactKeys ? { contactKey: contactKeys.publicKey } : {}) });
275
+ const own = await ownProfile();
276
+ if (!own)
277
+ return;
278
+ const base = { name: own.name, ...(contactKeys ? { contactKey: contactKeys.publicKey } : {}) };
279
+ const held = (await open.profiles()).find((profile) => profile.did === config.signer.did)?.avatar;
280
+ const current = isAvatarRef(held) && held.source === own.avatar?.hash ? held : undefined;
281
+ // The name and contact key go out now, with the picture the space already has; a new picture
282
+ // follows once it is copied, which may wait on its bytes reaching this device.
283
+ const shown = own.avatar ? (current ?? (isAvatarRef(held) ? held : undefined)) : undefined;
284
+ await open.publishProfile({ ...base, ...(shown ? { avatar: shown } : {}) });
285
+ if (own.avatar && !current)
286
+ void avatarIn(spaceId, open, own.avatar)
287
+ .then((avatar) => open.publishProfile({ ...base, avatar }))
288
+ .catch(() => {
289
+ // Its bytes are on a device that is away: copied next time the profile is published.
290
+ });
291
+ }
292
+ /**
293
+ * The account's picture as a file of this space: a space's members can't
294
+ * read the registry, so each space holds its own copy, made once and named
295
+ * by the registry file it copies.
296
+ */
297
+ function avatarIn(spaceId, open, avatar) {
298
+ // One copy per space and picture, however many publishes ask at once (a space opening, a change).
299
+ const at = `${spaceId}:${avatar.hash}`;
300
+ let copying = avatarCopies.get(at);
301
+ if (!copying) {
302
+ copying = (async () => {
303
+ const held = (await open.profiles()).find((profile) => profile.did === config.signer.did)?.avatar;
304
+ if (isAvatarRef(held) && held.source === avatar.hash)
305
+ return held;
306
+ const registryRt = await runtime(accountSpaceId);
307
+ const copy = await open.addFile(await registryRt.readFile(avatar));
308
+ return { ...copy, mime: avatar.mime, source: avatar.hash };
309
+ })();
310
+ copying.catch(() => avatarCopies.delete(at));
311
+ avatarCopies.set(at, copying);
312
+ }
313
+ return copying;
273
314
  }
315
+ const avatarCopies = new Map();
274
316
  async function publishProfileToOpenSpaces() {
275
317
  for (const [spaceId, open] of runtimes) {
276
318
  await open.then((rt) => publishProfile(spaceId, rt)).catch(() => { });
@@ -296,6 +338,7 @@ export async function createNode(config) {
296
338
  // The account's own spaces are always held whole: every device needs all of them.
297
339
  ...(config.cache && !ownSpace(spaceId) ? { cache: config.cache } : {}),
298
340
  watchIntervalMs: config.watchIntervalMs ?? 2000,
341
+ ...(config.fetchFiles ? { fetchFiles: config.fetchFiles } : {}),
299
342
  emit: fromRuntime,
300
343
  onRole: (role) => {
301
344
  if (spaceId === accountSpaceId)
@@ -494,6 +537,7 @@ export async function createNode(config) {
494
537
  return;
495
538
  const passes = new Map();
496
539
  const spaces = [];
540
+ const owned = await ownAutomations();
497
541
  // The registry and the contacts too, so a restore can come through the carrier.
498
542
  passes.set(await passKey(account.space.id), await makePass(account));
499
543
  if (contactsRecord)
@@ -504,6 +548,7 @@ export async function createNode(config) {
504
548
  const record = await registry.get(membership.body.space);
505
549
  if (!record)
506
550
  continue;
551
+ // Opened even with no wakes to make: opening is also how this device learns the space's key changed.
507
552
  const declared = new Map((await (await runtime(record.space.id)).collections()).map((c) => [c.name, c]));
508
553
  spaces.push({
509
554
  id: record.space.id,
@@ -519,10 +564,13 @@ export async function createNode(config) {
519
564
  // Held without its key — nothing to pass on until it arrives.
520
565
  }
521
566
  }
567
+ // Removed meanwhile, on this device or another: its passes stay deleted.
568
+ if (!(await liveCarriers()).some((live) => live.space === carrier.space))
569
+ return;
522
570
  const carry = await runtime(carrier.space);
523
571
  await writeExactly(carry, PASS_COLLECTION, passes, (key) => key.startsWith('pass:'));
524
572
  const wakes = new Map();
525
- for (const own of await ownAutomations()) {
573
+ for (const own of owned) {
526
574
  const wake = await wakeFor(own, spaces, config.signer.did);
527
575
  wakes.set(`wake:${own.id}`, wake);
528
576
  // A 👍 can make a todo done: what a computed field the automation names reads wakes too.
@@ -555,11 +603,6 @@ export async function createNode(config) {
555
603
  }
556
604
  /** The carriers the account uses now */
557
605
  const liveCarriers = async () => (await carrierRecords()).flatMap(({ record }) => (!record.deleted && record.body ? [record.body] : []));
558
- /** Every carrier gets the wakes as they are now */
559
- async function passWakesOn() {
560
- for (const carrier of await liveCarriers())
561
- await syncPasses(carrier).catch(() => { });
562
- }
563
606
  /**
564
607
  * Names the account's carriers — its extensions, its hosts — as keepers of
565
608
  * a space it manages, and stops naming ones it removed, so apps there hold
@@ -654,16 +697,15 @@ export async function createNode(config) {
654
697
  }
655
698
  // Held here but never recorded — joined before the registry existed, or on
656
699
  // a node without the account key. Recorded now, so other devices follow.
700
+ // Held still: a carrier that failed while this ran has taken its carry space back out.
701
+ const stillHeld = new Set((await registry.list()).map((record) => record.space.id));
657
702
  for (const spaceId of held) {
658
- if (!known.has(spaceId))
703
+ if (!known.has(spaceId) && stillHeld.has(spaceId) && !carrySpaces.has(spaceId))
659
704
  await remember(spaceId);
660
705
  }
661
- for (const { record, space: spaceId } of carriers) {
662
- if (record.deleted || !record.body || !held.has(spaceId))
663
- continue;
664
- // Only a device that may write in the carry space can do this; others leave it to one that can.
665
- await syncPasses(record.body).catch(() => { });
666
- }
706
+ // Not awaited: it opens every space the account holds, which from a data
707
+ // folder takes long, and nothing here needs it done. Signing in waits on this.
708
+ void passAll().catch(() => { });
667
709
  // Carriers came or went: the open spaces this account manages say so.
668
710
  for (const [spaceId, open] of runtimes)
669
711
  void open.then((rt) => nameKeepers(spaceId, rt)).catch(() => { });
@@ -675,6 +717,35 @@ export async function createNode(config) {
675
717
  if (changed)
676
718
  emit({ type: 'spaces' });
677
719
  }
720
+ /** Whether `work` settled within `waitMs` */
721
+ const within = (work, waitMs) => new Promise((resolve) => {
722
+ const timer = setTimeout(() => resolve(false), waitMs);
723
+ void work.then(() => {
724
+ clearTimeout(timer);
725
+ resolve(true);
726
+ }, () => {
727
+ clearTimeout(timer);
728
+ resolve(false);
729
+ });
730
+ });
731
+ /** Every carrier the account uses gets its passes and wakes as they are now, one run at a time */
732
+ /** Writing in carry spaces, one thing at a time: a removal never lands in the middle of a refill */
733
+ let carryTurn = Promise.resolve();
734
+ const inCarryTurn = (work) => {
735
+ const run = carryTurn.catch(() => { }).then(work);
736
+ carryTurn = run;
737
+ return run;
738
+ };
739
+ const passAll = serial(() => inCarryTurn(async () => {
740
+ for (const { record, space: spaceId } of await carrierRecords()) {
741
+ if (closed)
742
+ return;
743
+ if (record.deleted || !record.body || !(await registry.get(spaceId)))
744
+ continue;
745
+ // Only a device that may write in the carry space can do this; others leave it to one that can.
746
+ await syncPasses(record.body).catch(() => { });
747
+ }
748
+ }));
678
749
  const reconcile = serial(() => reconcileOnce().catch((error) => {
679
750
  if (!closed)
680
751
  console.error('Could not reconcile the account registry:', error);
@@ -844,7 +915,8 @@ export async function createNode(config) {
844
915
  throw new Error(problem);
845
916
  // A system collection: nothing defines it, so no other key of the account can make it final.
846
917
  await registryRt.upsertSystem(OWN_AUTOMATIONS, id, body);
847
- await passWakesOn();
918
+ // Every carrier gets the wakes as they are now, in the background (`carriers.settled`).
919
+ void passAll().catch(() => { });
848
920
  return { ...body, id };
849
921
  }
850
922
  const automations = Object.freeze({
@@ -871,7 +943,7 @@ export async function createNode(config) {
871
943
  if (!(await ownAutomations()).some((view) => view.id === id))
872
944
  return;
873
945
  await registryRt.removeSystem(id);
874
- await passWakesOn();
946
+ void passAll().catch(() => { });
875
947
  },
876
948
  async versions(ids) {
877
949
  if (!accountSpaceId)
@@ -891,61 +963,90 @@ export async function createNode(config) {
891
963
  return 0;
892
964
  const taken = await (await runtime(accountSpaceId)).take(mine);
893
965
  if (taken > 0)
894
- await passWakesOn();
966
+ void passAll().catch(() => { });
895
967
  return taken;
896
968
  },
897
969
  });
898
- const carriers = Object.freeze({
899
- list: async () => (await liveCarriers()).map(({ space, did, name, since }) => ({ space, did, name, since })),
900
- async add(carrier) {
901
- const registryRt = await requireAccount('Using a carrier');
902
- // The same carrier again keeps its carry space: what it holds stays good, and nothing is closed under it.
903
- const known = (await liveCarriers()).find((c) => c.did === carrier.did);
904
- if (known) {
905
- await syncPasses(known);
906
- return { space: known.space, invite: known.invite };
907
- }
908
- const name = carrier.name.trim().slice(0, 80) || 'Carrier';
909
- // Private, so the passes in it are sealed; the account alone may write there.
910
- const record = await registry.create({
911
- name: `Carried by ${name}`,
912
- visibility: 'private',
913
- creator: config.signer.did,
914
- });
915
- carrySpaces.add(record.space.id);
970
+ /** Carriers being added now, by key */
971
+ const adding = new Map();
972
+ async function addCarrier(carrier) {
973
+ const registryRt = await requireAccount('Using a carrier');
974
+ // The same carrier again keeps its carry space: what it holds stays good, and nothing is closed under it.
975
+ const known = (await liveCarriers()).find((c) => c.did === carrier.did);
976
+ if (known) {
977
+ void passAll().catch(() => { });
978
+ return { space: known.space, invite: known.invite };
979
+ }
980
+ const name = carrier.name.trim().slice(0, 80) || 'Carrier';
981
+ // Private, so the passes in it are sealed; the account alone may write there.
982
+ const record = await registry.create({
983
+ name: `Carried by ${name}`,
984
+ visibility: 'private',
985
+ creator: config.signer.did,
986
+ });
987
+ carrySpaces.add(record.space.id);
988
+ let body;
989
+ try {
916
990
  const invite = await registry.createInvite(record.space.id, config.signer.did);
917
- const body = {
918
- space: record.space.id,
919
- invite,
920
- did: carrier.did,
921
- name,
922
- since: new Date().toISOString(),
923
- };
991
+ body = { space: record.space.id, invite, did: carrier.did, name, since: new Date().toISOString() };
924
992
  await registryRt.upsertSystem(CARRIER_COLLECTION, await carrierKey(record.space.id), body);
925
- await syncPasses(body);
926
- return { space: record.space.id, invite };
993
+ }
994
+ catch (error) {
995
+ // Without its record nothing says it is a carry space: held, a restart would list it as one the account uses.
996
+ // Left in `carrySpaces`: a reconcile already under way must not record it either.
997
+ await closeRuntime(record.space.id);
998
+ await registry.remove(record.space.id);
999
+ throw error;
1000
+ }
1001
+ // Its passes before its invite, as one batch: a carrier joining an empty carry space takes spaces as they trickle in.
1002
+ // Not in turn behind a refill: nothing removes a carrier that is only now being added.
1003
+ await syncPasses(body);
1004
+ return { space: record.space.id, invite: body.invite };
1005
+ }
1006
+ const carriers = Object.freeze({
1007
+ list: async () => (await liveCarriers()).map(({ space, did, name, since }) => ({ space, did, name, since })),
1008
+ add(carrier) {
1009
+ // Asked again while the first is under way (a click and a reconcile, say): the same answer, not a second carry space.
1010
+ const pending = adding.get(carrier.did);
1011
+ if (pending)
1012
+ return pending;
1013
+ const made = addCarrier(carrier).finally(() => adding.delete(carrier.did));
1014
+ adding.set(carrier.did, made);
1015
+ return made;
927
1016
  },
1017
+ settled: (waitMs) => within(passAll.done(), waitMs),
928
1018
  async reached(spaceId, waitMs) {
929
1019
  const carrier = (await liveCarriers()).find((c) => c.space === spaceId);
930
1020
  if (!carrier || !accountSpaceId)
931
1021
  return false;
932
1022
  const deadline = Date.now() + waitMs;
1023
+ // Level with the passes and wakes as they are now: a refill under way first, within the same time.
1024
+ await within(passAll.done(), waitMs);
933
1025
  // Its passes first: only through them does it come to the registry, and hold its own record there.
934
- if (!(await (await runtime(spaceId)).syncedWith(carrier.did, waitMs)))
1026
+ if (!(await (await runtime(spaceId)).syncedWith(carrier.did, Math.max(0, deadline - Date.now()))))
935
1027
  return false;
936
1028
  return (await runtime(accountSpaceId)).syncedWith(carrier.did, Math.max(0, deadline - Date.now()));
937
1029
  },
938
1030
  async remove(spaceId) {
939
1031
  const registryRt = await requireAccount('Removing a carrier');
940
- const found = (await carrierRecords()).find(({ record, space }) => !record.deleted && space === spaceId)?.record;
941
- if (!found)
942
- return;
943
- const carry = await runtime(spaceId);
944
- // Passes gone first, then the word to forget everything, then the carrier's record.
945
- for (const pass of await ownRecords(carry, { collection: PASS_COLLECTION }, (r) => r.key.startsWith('pass:')))
946
- await carry.removeSystem(pass.key);
947
- await carry.upsertSystem(PASS_COLLECTION, CARRY_CLOSED_KEY, { v: 1, closed: true });
948
- await registryRt.removeSystem(found.key);
1032
+ // In turn with any refill, which would otherwise put the passes back.
1033
+ await inCarryTurn(async () => {
1034
+ const live = (await carrierRecords()).filter(({ record }) => !record.deleted && record.body);
1035
+ const did = live.find(({ space }) => space === spaceId)?.record.body?.did;
1036
+ if (!did)
1037
+ return;
1038
+ // Every carry space it has: added on two devices at once, it may hold the other one (spec 06 §4.2).
1039
+ for (const { record, space } of live) {
1040
+ if (record.body?.did !== did)
1041
+ continue;
1042
+ const carry = await runtime(space);
1043
+ // Passes gone first, then the word to forget everything, then the carrier's record.
1044
+ for (const pass of await ownRecords(carry, { collection: PASS_COLLECTION }, (r) => r.key.startsWith('pass:')))
1045
+ await carry.removeSystem(pass.key);
1046
+ await carry.upsertSystem(PASS_COLLECTION, CARRY_CLOSED_KEY, { v: 1, closed: true });
1047
+ await registryRt.removeSystem(record.key);
1048
+ }
1049
+ });
949
1050
  },
950
1051
  });
951
1052
  // ─── Hosting ───────────────────────────────────────────────────────
@@ -998,6 +1099,8 @@ export async function createNode(config) {
998
1099
  kept.state === status.state &&
999
1100
  kept.paidUntil === status.paidUntil &&
1000
1101
  kept.renews === status.renews &&
1102
+ kept.plan === status.plan &&
1103
+ JSON.stringify(kept.over ?? []) === JSON.stringify(status.over ?? []) &&
1001
1104
  hosting.name === name &&
1002
1105
  hosting.peer === peer;
1003
1106
  if (same)
@@ -1015,9 +1118,10 @@ export async function createNode(config) {
1015
1118
  const description = await describeKnown(hosting);
1016
1119
  let { status, receipt } = await client.status();
1017
1120
  const due = force || Date.now() - (handedAt.get(hosting.url) ?? 0) > HAND_AGAIN_MS;
1121
+ // A host with limits keeps an account's spaces with nothing paid: its free plan.
1018
1122
  const paid = status.state === 'active' ||
1019
1123
  status.state === 'grace' ||
1020
- (description.free && status.state !== 'lapsed');
1124
+ ((description.free || description.limits !== undefined) && status.state !== 'lapsed');
1021
1125
  const inFlight = handing.get(hosting.url);
1022
1126
  if (!status.carrying && inFlight) {
1023
1127
  // Another look is handing the spaces over right now: wait for it,
@@ -1048,7 +1152,7 @@ export async function createNode(config) {
1048
1152
  name: description.name,
1049
1153
  status,
1050
1154
  live: true,
1051
- plans: plansFor(description, 'account'),
1155
+ fund: description.free ? null : (description.fund ?? null),
1052
1156
  reminds: description.remind === true,
1053
1157
  };
1054
1158
  }
@@ -1058,14 +1162,12 @@ export async function createNode(config) {
1058
1162
  name: hosting.name ?? new URL(hosting.url).host,
1059
1163
  status: kept,
1060
1164
  live: false,
1061
- plans: [],
1165
+ fund: null,
1062
1166
  reminds: false,
1063
1167
  error: error instanceof Error ? error.message : String(error),
1064
1168
  };
1065
1169
  }
1066
1170
  }
1067
- /** The plans a host describes for an account's subscription, or a space's own */
1068
- const plansFor = (description, who) => (description.plans ?? []).filter((plan) => Array.isArray(plan.for) && plan.for.includes(who));
1069
1171
  /** Every device keeps its hosts carrying: after the registry changes, ask each once more */
1070
1172
  async function keepHosted() {
1071
1173
  if (narrowNote)
@@ -1265,6 +1367,16 @@ export async function createNode(config) {
1265
1367
  async list() {
1266
1368
  return Promise.all((await hostingRecords()).map((known) => viewHosting(known)));
1267
1369
  },
1370
+ async over(spaceId) {
1371
+ const found = [];
1372
+ for (const hosting of await hostingRecords()) {
1373
+ const status = hosting.receipt ? await readStatus(hosting.receipt, hosting.host, provider) : null;
1374
+ const over = status?.over?.find((one) => one.space === spaceId);
1375
+ if (over)
1376
+ found.push({ url: hosting.url, name: hosting.name ?? new URL(hosting.url).host, over });
1377
+ }
1378
+ return found;
1379
+ },
1268
1380
  async use(address) {
1269
1381
  const registryRt = await requireAccount('Using a host');
1270
1382
  const base = checkAddress(address, 'A host').origin;
@@ -1284,11 +1396,8 @@ export async function createNode(config) {
1284
1396
  await registryRt.upsertSystem(HOSTING_COLLECTION, await hostingKey(base), record);
1285
1397
  return viewHosting(record, true);
1286
1398
  },
1287
- async pay(url, plan) {
1288
- return (await hostClient(await requireHosting(url))).client.pay(plan);
1289
- },
1290
- async manage(url) {
1291
- return (await hostClient(await requireHosting(url))).client.manage();
1399
+ async pay(url, payment) {
1400
+ return (await hostClient(await requireHosting(url))).client.pay(payment);
1292
1401
  },
1293
1402
  async remind(url, email) {
1294
1403
  await (await hostClient(await requireHosting(url))).client.remind(email);
@@ -2012,12 +2121,36 @@ export async function createNode(config) {
2012
2121
  const trimmed = name.trim();
2013
2122
  if (!trimmed)
2014
2123
  throw new Error('A name cannot be empty');
2124
+ const avatar = (await ownProfile())?.avatar;
2015
2125
  const written = await registryRt.upsertSystem(PROFILE_COLLECTION, PROFILE_KEY, {
2016
2126
  name: trimmed,
2127
+ ...(avatar ? { avatar } : {}),
2128
+ });
2129
+ emit({ type: 'account' });
2130
+ await publishProfileToOpenSpaces();
2131
+ return { name: trimmed, updatedAt: written.updatedAt, ...(avatar ? { avatar } : {}) };
2132
+ },
2133
+ async setAvatar(image, options = {}) {
2134
+ const registryRt = await requireAccount('Changing the picture across devices');
2135
+ const name = (await ownName()) ?? options.name?.trim();
2136
+ if (!name)
2137
+ throw new Error('Give the account a name first');
2138
+ if (image && !image.mime.startsWith('image/'))
2139
+ throw new Error('A picture is an image');
2140
+ const avatar = image ? { ...(await registryRt.addFile(image.bytes)), mime: image.mime } : null;
2141
+ const written = await registryRt.upsertSystem(PROFILE_COLLECTION, PROFILE_KEY, {
2142
+ name,
2143
+ ...(avatar ? { avatar } : {}),
2017
2144
  });
2018
2145
  emit({ type: 'account' });
2019
2146
  await publishProfileToOpenSpaces();
2020
- return { name: trimmed, updatedAt: written.updatedAt };
2147
+ return { name, updatedAt: written.updatedAt, ...(avatar ? { avatar } : {}) };
2148
+ },
2149
+ async picture() {
2150
+ const avatar = (await ownProfile())?.avatar;
2151
+ if (!avatar || !accountSpaceId)
2152
+ return null;
2153
+ return { bytes: await (await runtime(accountSpaceId)).readFile(avatar), mime: avatar.mime };
2021
2154
  },
2022
2155
  revoke: async (key, options) => (await requireAccount('Revoking in the account registry')).revoke(key, options),
2023
2156
  });
@@ -2034,6 +2167,12 @@ export async function createNode(config) {
2034
2167
  linked: async (spaceId, key, options) => (await runtime(spaceId)).linked(key, options),
2035
2168
  delete: async (spaceId, key) => (await runtime(spaceId)).remove(key, as ? { as } : undefined),
2036
2169
  history: async (spaceId, key) => (await runtime(spaceId)).history(key),
2170
+ addFile: async (spaceId, bytes, about) => ({
2171
+ ...(await (await runtime(spaceId)).addFile(bytes)),
2172
+ mime: about.mime,
2173
+ ...(about.name !== undefined ? { name: about.name } : {}),
2174
+ }),
2175
+ readFile: async (spaceId, ref) => (await runtime(spaceId)).readFile(ref),
2037
2176
  can: perSpace((rt, action, target) => rt.can(action, target)),
2038
2177
  async query(spaceId, query) {
2039
2178
  const space = await runtime(spaceId);
@@ -2128,6 +2267,7 @@ export async function createNode(config) {
2128
2267
  throw new Error('That space was not given to this agent');
2129
2268
  return hosting.space(spaceId);
2130
2269
  },
2270
+ over: async (spaceId) => (allowed(spaceId) ? hosting.over(spaceId) : []),
2131
2271
  },
2132
2272
  contacts: {
2133
2273
  space: async () => (contactsGiven() ? contactsSpaceId : null),
@@ -2194,19 +2334,30 @@ export async function createNode(config) {
2194
2334
  subscribe: events.subscribe,
2195
2335
  async revokeSelf(options = {}) {
2196
2336
  const key = sessionDid;
2337
+ // Forgotten first: a tab closed while the revokes wait must not leave a
2338
+ // revoked key behind, to be claimed and refused on every write after.
2339
+ await claimed?.forget().catch(() => { });
2197
2340
  // Every space it may write in: the listed ones, its carry spaces and the contacts space.
2198
2341
  const ids = new Set([
2199
2342
  ...(await spaces.list()).filter((space) => space.writable).map((space) => space.id),
2200
2343
  ...carrySpaces,
2201
2344
  ...(contactsSpaceId ? [contactsSpaceId] : []),
2202
2345
  ]);
2346
+ // With anyone online, every revoke waits for a peer to take it in, closed
2347
+ // spaces too, which open for it. With nobody, waiting would only keep a
2348
+ // person signing out looking at a spinner: they go out with the next sync.
2349
+ let anyone = false;
2350
+ for (const open of runtimes.values()) {
2351
+ const rt = await open.catch(() => null);
2352
+ if (rt && (await rt.status()).peers.length > 0)
2353
+ anyone = true;
2354
+ }
2355
+ const wait = anyone ? options : {};
2203
2356
  // Each its own store: one that can't be done must not keep the rest from happening.
2204
2357
  await Promise.all([
2205
- ...[...ids].map((spaceId) => spaces.revoke(spaceId, key, options).catch(() => false)),
2206
- node.account.revoke(key, options).catch(() => false),
2358
+ ...[...ids].map((spaceId) => spaces.revoke(spaceId, key, wait).catch(() => false)),
2359
+ node.account.revoke(key, wait).catch(() => false),
2207
2360
  ]);
2208
- // Forgotten now, not at close: a crash before closing must not leave a revoked key to be claimed again.
2209
- await claimed?.forget().catch(() => { });
2210
2361
  },
2211
2362
  async close() {
2212
2363
  if (closed)
@@ -2225,6 +2376,15 @@ export async function createNode(config) {
2225
2376
  events.clear();
2226
2377
  },
2227
2378
  });
2379
+ // A lasting key the account revoked on an earlier visit, which was never
2380
+ // forgotten here: every write it signs would be refused. Forgotten now, and
2381
+ // this run starts again on a key of its own; the next claims a new lasting one.
2382
+ if (claimed && accountSpaceId && (await (await runtime(accountSpaceId)).isRevoked(sessionDid))) {
2383
+ await claimed.forget().catch(() => { });
2384
+ await node.close();
2385
+ const { writerKey: _revoked, ...rest } = config;
2386
+ return createNode(rest);
2387
+ }
2228
2388
  return node;
2229
2389
  }
2230
2390
  //# sourceMappingURL=node.js.map