@minca/core 0.1.6 โ†’ 0.1.8

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 (55) hide show
  1. package/README.md +247 -215
  2. package/dist/elements/auth-styles.d.ts +1 -1
  3. package/dist/elements/auth-styles.d.ts.map +1 -1
  4. package/dist/elements/auth-styles.js +16 -8
  5. package/dist/elements/auth-styles.js.map +1 -1
  6. package/dist/elements/minca-auth.d.ts +7 -0
  7. package/dist/elements/minca-auth.d.ts.map +1 -1
  8. package/dist/elements/minca-auth.js +107 -49
  9. package/dist/elements/minca-auth.js.map +1 -1
  10. package/dist/node/api.d.ts +1 -0
  11. package/dist/node/api.d.ts.map +1 -1
  12. package/dist/node/api.js +7 -1
  13. package/dist/node/api.js.map +1 -1
  14. package/dist/node/node.d.ts.map +1 -1
  15. package/dist/node/node.js +135 -57
  16. package/dist/node/node.js.map +1 -1
  17. package/dist/node/types.d.ts +8 -0
  18. package/dist/node/types.d.ts.map +1 -1
  19. package/dist/node/writer-keys.d.ts +4 -1
  20. package/dist/node/writer-keys.d.ts.map +1 -1
  21. package/dist/node/writer-keys.js +25 -6
  22. package/dist/node/writer-keys.js.map +1 -1
  23. package/dist/node/writers.d.ts.map +1 -1
  24. package/dist/node/writers.js +10 -3
  25. package/dist/node/writers.js.map +1 -1
  26. package/dist/react/minca-auth.d.ts +3 -1
  27. package/dist/react/minca-auth.d.ts.map +1 -1
  28. package/dist/react/minca-auth.js +5 -3
  29. package/dist/react/minca-auth.js.map +1 -1
  30. package/dist/session/account-hint.d.ts +13 -0
  31. package/dist/session/account-hint.d.ts.map +1 -0
  32. package/dist/session/account-hint.js +51 -0
  33. package/dist/session/account-hint.js.map +1 -0
  34. package/dist/session/auth.d.ts +51 -3
  35. package/dist/session/auth.d.ts.map +1 -1
  36. package/dist/session/auth.js +254 -35
  37. package/dist/session/auth.js.map +1 -1
  38. package/dist/session/connect.d.ts.map +1 -1
  39. package/dist/session/connect.js +6 -2
  40. package/dist/session/connect.js.map +1 -1
  41. package/dist/session/index.d.ts +1 -0
  42. package/dist/session/index.d.ts.map +1 -1
  43. package/dist/session/index.js +1 -0
  44. package/dist/session/index.js.map +1 -1
  45. package/dist/session/pairing.d.ts +2 -0
  46. package/dist/session/pairing.d.ts.map +1 -1
  47. package/dist/session/pairing.js +8 -0
  48. package/dist/session/pairing.js.map +1 -1
  49. package/dist/session/places.d.ts +5 -0
  50. package/dist/session/places.d.ts.map +1 -1
  51. package/dist/session/places.js +12 -0
  52. package/dist/session/places.js.map +1 -1
  53. package/docs/node.md +24 -2
  54. package/docs/sign-in.md +123 -24
  55. package/package.json +2 -2
package/dist/node/node.js CHANGED
@@ -494,6 +494,7 @@ export async function createNode(config) {
494
494
  return;
495
495
  const passes = new Map();
496
496
  const spaces = [];
497
+ const owned = await ownAutomations();
497
498
  // The registry and the contacts too, so a restore can come through the carrier.
498
499
  passes.set(await passKey(account.space.id), await makePass(account));
499
500
  if (contactsRecord)
@@ -504,6 +505,7 @@ export async function createNode(config) {
504
505
  const record = await registry.get(membership.body.space);
505
506
  if (!record)
506
507
  continue;
508
+ // Opened even with no wakes to make: opening is also how this device learns the space's key changed.
507
509
  const declared = new Map((await (await runtime(record.space.id)).collections()).map((c) => [c.name, c]));
508
510
  spaces.push({
509
511
  id: record.space.id,
@@ -519,10 +521,13 @@ export async function createNode(config) {
519
521
  // Held without its key โ€” nothing to pass on until it arrives.
520
522
  }
521
523
  }
524
+ // Removed meanwhile, on this device or another: its passes stay deleted.
525
+ if (!(await liveCarriers()).some((live) => live.space === carrier.space))
526
+ return;
522
527
  const carry = await runtime(carrier.space);
523
528
  await writeExactly(carry, PASS_COLLECTION, passes, (key) => key.startsWith('pass:'));
524
529
  const wakes = new Map();
525
- for (const own of await ownAutomations()) {
530
+ for (const own of owned) {
526
531
  const wake = await wakeFor(own, spaces, config.signer.did);
527
532
  wakes.set(`wake:${own.id}`, wake);
528
533
  // A ๐Ÿ‘ can make a todo done: what a computed field the automation names reads wakes too.
@@ -555,11 +560,6 @@ export async function createNode(config) {
555
560
  }
556
561
  /** The carriers the account uses now */
557
562
  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
563
  /**
564
564
  * Names the account's carriers โ€” its extensions, its hosts โ€” as keepers of
565
565
  * a space it manages, and stops naming ones it removed, so apps there hold
@@ -654,16 +654,15 @@ export async function createNode(config) {
654
654
  }
655
655
  // Held here but never recorded โ€” joined before the registry existed, or on
656
656
  // a node without the account key. Recorded now, so other devices follow.
657
+ // Held still: a carrier that failed while this ran has taken its carry space back out.
658
+ const stillHeld = new Set((await registry.list()).map((record) => record.space.id));
657
659
  for (const spaceId of held) {
658
- if (!known.has(spaceId))
660
+ if (!known.has(spaceId) && stillHeld.has(spaceId) && !carrySpaces.has(spaceId))
659
661
  await remember(spaceId);
660
662
  }
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
- }
663
+ // Not awaited: it opens every space the account holds, which from a data
664
+ // folder takes long, and nothing here needs it done. Signing in waits on this.
665
+ void passAll().catch(() => { });
667
666
  // Carriers came or went: the open spaces this account manages say so.
668
667
  for (const [spaceId, open] of runtimes)
669
668
  void open.then((rt) => nameKeepers(spaceId, rt)).catch(() => { });
@@ -675,6 +674,35 @@ export async function createNode(config) {
675
674
  if (changed)
676
675
  emit({ type: 'spaces' });
677
676
  }
677
+ /** Whether `work` settled within `waitMs` */
678
+ const within = (work, waitMs) => new Promise((resolve) => {
679
+ const timer = setTimeout(() => resolve(false), waitMs);
680
+ void work.then(() => {
681
+ clearTimeout(timer);
682
+ resolve(true);
683
+ }, () => {
684
+ clearTimeout(timer);
685
+ resolve(false);
686
+ });
687
+ });
688
+ /** Every carrier the account uses gets its passes and wakes as they are now, one run at a time */
689
+ /** Writing in carry spaces, one thing at a time: a removal never lands in the middle of a refill */
690
+ let carryTurn = Promise.resolve();
691
+ const inCarryTurn = (work) => {
692
+ const run = carryTurn.catch(() => { }).then(work);
693
+ carryTurn = run;
694
+ return run;
695
+ };
696
+ const passAll = serial(() => inCarryTurn(async () => {
697
+ for (const { record, space: spaceId } of await carrierRecords()) {
698
+ if (closed)
699
+ return;
700
+ if (record.deleted || !record.body || !(await registry.get(spaceId)))
701
+ continue;
702
+ // Only a device that may write in the carry space can do this; others leave it to one that can.
703
+ await syncPasses(record.body).catch(() => { });
704
+ }
705
+ }));
678
706
  const reconcile = serial(() => reconcileOnce().catch((error) => {
679
707
  if (!closed)
680
708
  console.error('Could not reconcile the account registry:', error);
@@ -844,7 +872,8 @@ export async function createNode(config) {
844
872
  throw new Error(problem);
845
873
  // A system collection: nothing defines it, so no other key of the account can make it final.
846
874
  await registryRt.upsertSystem(OWN_AUTOMATIONS, id, body);
847
- await passWakesOn();
875
+ // Every carrier gets the wakes as they are now, in the background (`carriers.settled`).
876
+ void passAll().catch(() => { });
848
877
  return { ...body, id };
849
878
  }
850
879
  const automations = Object.freeze({
@@ -871,7 +900,7 @@ export async function createNode(config) {
871
900
  if (!(await ownAutomations()).some((view) => view.id === id))
872
901
  return;
873
902
  await registryRt.removeSystem(id);
874
- await passWakesOn();
903
+ void passAll().catch(() => { });
875
904
  },
876
905
  async versions(ids) {
877
906
  if (!accountSpaceId)
@@ -891,61 +920,90 @@ export async function createNode(config) {
891
920
  return 0;
892
921
  const taken = await (await runtime(accountSpaceId)).take(mine);
893
922
  if (taken > 0)
894
- await passWakesOn();
923
+ void passAll().catch(() => { });
895
924
  return taken;
896
925
  },
897
926
  });
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);
927
+ /** Carriers being added now, by key */
928
+ const adding = new Map();
929
+ async function addCarrier(carrier) {
930
+ const registryRt = await requireAccount('Using a carrier');
931
+ // The same carrier again keeps its carry space: what it holds stays good, and nothing is closed under it.
932
+ const known = (await liveCarriers()).find((c) => c.did === carrier.did);
933
+ if (known) {
934
+ void passAll().catch(() => { });
935
+ return { space: known.space, invite: known.invite };
936
+ }
937
+ const name = carrier.name.trim().slice(0, 80) || 'Carrier';
938
+ // Private, so the passes in it are sealed; the account alone may write there.
939
+ const record = await registry.create({
940
+ name: `Carried by ${name}`,
941
+ visibility: 'private',
942
+ creator: config.signer.did,
943
+ });
944
+ carrySpaces.add(record.space.id);
945
+ let body;
946
+ try {
916
947
  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
- };
948
+ body = { space: record.space.id, invite, did: carrier.did, name, since: new Date().toISOString() };
924
949
  await registryRt.upsertSystem(CARRIER_COLLECTION, await carrierKey(record.space.id), body);
925
- await syncPasses(body);
926
- return { space: record.space.id, invite };
950
+ }
951
+ catch (error) {
952
+ // Without its record nothing says it is a carry space: held, a restart would list it as one the account uses.
953
+ // Left in `carrySpaces`: a reconcile already under way must not record it either.
954
+ await closeRuntime(record.space.id);
955
+ await registry.remove(record.space.id);
956
+ throw error;
957
+ }
958
+ // Its passes before its invite, as one batch: a carrier joining an empty carry space takes spaces as they trickle in.
959
+ // Not in turn behind a refill: nothing removes a carrier that is only now being added.
960
+ await syncPasses(body);
961
+ return { space: record.space.id, invite: body.invite };
962
+ }
963
+ const carriers = Object.freeze({
964
+ list: async () => (await liveCarriers()).map(({ space, did, name, since }) => ({ space, did, name, since })),
965
+ add(carrier) {
966
+ // Asked again while the first is under way (a click and a reconcile, say): the same answer, not a second carry space.
967
+ const pending = adding.get(carrier.did);
968
+ if (pending)
969
+ return pending;
970
+ const made = addCarrier(carrier).finally(() => adding.delete(carrier.did));
971
+ adding.set(carrier.did, made);
972
+ return made;
927
973
  },
974
+ settled: (waitMs) => within(passAll.done(), waitMs),
928
975
  async reached(spaceId, waitMs) {
929
976
  const carrier = (await liveCarriers()).find((c) => c.space === spaceId);
930
977
  if (!carrier || !accountSpaceId)
931
978
  return false;
932
979
  const deadline = Date.now() + waitMs;
980
+ // Level with the passes and wakes as they are now: a refill under way first, within the same time.
981
+ await within(passAll.done(), waitMs);
933
982
  // 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)))
983
+ if (!(await (await runtime(spaceId)).syncedWith(carrier.did, Math.max(0, deadline - Date.now()))))
935
984
  return false;
936
985
  return (await runtime(accountSpaceId)).syncedWith(carrier.did, Math.max(0, deadline - Date.now()));
937
986
  },
938
987
  async remove(spaceId) {
939
988
  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);
989
+ // In turn with any refill, which would otherwise put the passes back.
990
+ await inCarryTurn(async () => {
991
+ const live = (await carrierRecords()).filter(({ record }) => !record.deleted && record.body);
992
+ const did = live.find(({ space }) => space === spaceId)?.record.body?.did;
993
+ if (!did)
994
+ return;
995
+ // Every carry space it has: added on two devices at once, it may hold the other one (spec 06 ยง4.2).
996
+ for (const { record, space } of live) {
997
+ if (record.body?.did !== did)
998
+ continue;
999
+ const carry = await runtime(space);
1000
+ // Passes gone first, then the word to forget everything, then the carrier's record.
1001
+ for (const pass of await ownRecords(carry, { collection: PASS_COLLECTION }, (r) => r.key.startsWith('pass:')))
1002
+ await carry.removeSystem(pass.key);
1003
+ await carry.upsertSystem(PASS_COLLECTION, CARRY_CLOSED_KEY, { v: 1, closed: true });
1004
+ await registryRt.removeSystem(record.key);
1005
+ }
1006
+ });
949
1007
  },
950
1008
  });
951
1009
  // โ”€โ”€โ”€ Hosting โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@@ -2194,19 +2252,30 @@ export async function createNode(config) {
2194
2252
  subscribe: events.subscribe,
2195
2253
  async revokeSelf(options = {}) {
2196
2254
  const key = sessionDid;
2255
+ // Forgotten first: a tab closed while the revokes wait must not leave a
2256
+ // revoked key behind, to be claimed and refused on every write after.
2257
+ await claimed?.forget().catch(() => { });
2197
2258
  // Every space it may write in: the listed ones, its carry spaces and the contacts space.
2198
2259
  const ids = new Set([
2199
2260
  ...(await spaces.list()).filter((space) => space.writable).map((space) => space.id),
2200
2261
  ...carrySpaces,
2201
2262
  ...(contactsSpaceId ? [contactsSpaceId] : []),
2202
2263
  ]);
2264
+ // With anyone online, every revoke waits for a peer to take it in, closed
2265
+ // spaces too, which open for it. With nobody, waiting would only keep a
2266
+ // person signing out looking at a spinner: they go out with the next sync.
2267
+ let anyone = false;
2268
+ for (const open of runtimes.values()) {
2269
+ const rt = await open.catch(() => null);
2270
+ if (rt && (await rt.status()).peers.length > 0)
2271
+ anyone = true;
2272
+ }
2273
+ const wait = anyone ? options : {};
2203
2274
  // Each its own store: one that can't be done must not keep the rest from happening.
2204
2275
  await Promise.all([
2205
- ...[...ids].map((spaceId) => spaces.revoke(spaceId, key, options).catch(() => false)),
2206
- node.account.revoke(key, options).catch(() => false),
2276
+ ...[...ids].map((spaceId) => spaces.revoke(spaceId, key, wait).catch(() => false)),
2277
+ node.account.revoke(key, wait).catch(() => false),
2207
2278
  ]);
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
2279
  },
2211
2280
  async close() {
2212
2281
  if (closed)
@@ -2225,6 +2294,15 @@ export async function createNode(config) {
2225
2294
  events.clear();
2226
2295
  },
2227
2296
  });
2297
+ // A lasting key the account revoked on an earlier visit, which was never
2298
+ // forgotten here: every write it signs would be refused. Forgotten now, and
2299
+ // this run starts again on a key of its own; the next claims a new lasting one.
2300
+ if (claimed && accountSpaceId && (await (await runtime(accountSpaceId)).isRevoked(sessionDid))) {
2301
+ await claimed.forget().catch(() => { });
2302
+ await node.close();
2303
+ const { writerKey: _revoked, ...rest } = config;
2304
+ return createNode(rest);
2305
+ }
2228
2306
  return node;
2229
2307
  }
2230
2308
  //# sourceMappingURL=node.js.map