@minca/core 0.1.8 → 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 (118) hide show
  1. package/dist/files/file-id.d.ts +51 -0
  2. package/dist/files/file-id.d.ts.map +1 -0
  3. package/dist/files/file-id.js +97 -0
  4. package/dist/files/file-id.js.map +1 -0
  5. package/dist/files/seal.d.ts +20 -0
  6. package/dist/files/seal.d.ts.map +1 -0
  7. package/dist/files/seal.js +77 -0
  8. package/dist/files/seal.js.map +1 -0
  9. package/dist/files/space-files.d.ts +51 -0
  10. package/dist/files/space-files.d.ts.map +1 -0
  11. package/dist/files/space-files.js +476 -0
  12. package/dist/files/space-files.js.map +1 -0
  13. package/dist/index.d.ts +5 -4
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -2
  16. package/dist/index.js.map +1 -1
  17. package/dist/node/api.d.ts +5 -1
  18. package/dist/node/api.d.ts.map +1 -1
  19. package/dist/node/api.js +10 -2
  20. package/dist/node/api.js.map +1 -1
  21. package/dist/node/carrier.d.ts +17 -2
  22. package/dist/node/carrier.d.ts.map +1 -1
  23. package/dist/node/carrier.js +26 -4
  24. package/dist/node/carrier.js.map +1 -1
  25. package/dist/node/copy.d.ts +0 -4
  26. package/dist/node/copy.d.ts.map +1 -1
  27. package/dist/node/copy.js +26 -0
  28. package/dist/node/copy.js.map +1 -1
  29. package/dist/node/host.d.ts +66 -1
  30. package/dist/node/host.d.ts.map +1 -1
  31. package/dist/node/host.js +571 -5
  32. package/dist/node/host.js.map +1 -1
  33. package/dist/node/index.d.ts +2 -2
  34. package/dist/node/index.d.ts.map +1 -1
  35. package/dist/node/index.js +1 -1
  36. package/dist/node/index.js.map +1 -1
  37. package/dist/node/node.d.ts.map +1 -1
  38. package/dist/node/node.js +100 -18
  39. package/dist/node/node.js.map +1 -1
  40. package/dist/node/space-runtime.d.ts +9 -0
  41. package/dist/node/space-runtime.d.ts.map +1 -1
  42. package/dist/node/space-runtime.js +125 -16
  43. package/dist/node/space-runtime.js.map +1 -1
  44. package/dist/node/stores.d.ts +44 -8
  45. package/dist/node/stores.d.ts.map +1 -1
  46. package/dist/node/stores.js +44 -7
  47. package/dist/node/stores.js.map +1 -1
  48. package/dist/node/types.d.ts +65 -6
  49. package/dist/node/types.d.ts.map +1 -1
  50. package/dist/node/usage.d.ts +54 -0
  51. package/dist/node/usage.d.ts.map +1 -0
  52. package/dist/node/usage.js +201 -0
  53. package/dist/node/usage.js.map +1 -0
  54. package/dist/schema/version.d.ts +5 -0
  55. package/dist/schema/version.d.ts.map +1 -1
  56. package/dist/schema/version.js +1 -0
  57. package/dist/schema/version.js.map +1 -1
  58. package/dist/schemas/fragments.d.ts +23 -2
  59. package/dist/schemas/fragments.d.ts.map +1 -1
  60. package/dist/schemas/fragments.js +5 -2
  61. package/dist/schemas/fragments.js.map +1 -1
  62. package/dist/schemas/library/annotations.d.ts +24 -0
  63. package/dist/schemas/library/annotations.d.ts.map +1 -1
  64. package/dist/schemas/library/community.d.ts +24 -0
  65. package/dist/schemas/library/community.d.ts.map +1 -1
  66. package/dist/schemas/library/life.d.ts +72 -0
  67. package/dist/schemas/library/life.d.ts.map +1 -1
  68. package/dist/schemas/library/media.d.ts +144 -0
  69. package/dist/schemas/library/media.d.ts.map +1 -1
  70. package/dist/schemas/library/money.d.ts +24 -0
  71. package/dist/schemas/library/money.d.ts.map +1 -1
  72. package/dist/schemas/library/publishing.d.ts +96 -0
  73. package/dist/schemas/library/publishing.d.ts.map +1 -1
  74. package/dist/schemas/library/social.d.ts +72 -0
  75. package/dist/schemas/library/social.d.ts.map +1 -1
  76. package/dist/session/auth.d.ts.map +1 -1
  77. package/dist/session/auth.js +8 -1
  78. package/dist/session/auth.js.map +1 -1
  79. package/dist/session/hosting.d.ts +73 -28
  80. package/dist/session/hosting.d.ts.map +1 -1
  81. package/dist/session/hosting.js +85 -5
  82. package/dist/session/hosting.js.map +1 -1
  83. package/dist/session/places.d.ts +2 -0
  84. package/dist/session/places.d.ts.map +1 -1
  85. package/dist/session/places.js +8 -2
  86. package/dist/session/places.js.map +1 -1
  87. package/dist/space/account-registry.d.ts +13 -0
  88. package/dist/space/account-registry.d.ts.map +1 -1
  89. package/dist/space/account-registry.js +13 -0
  90. package/dist/space/account-registry.js.map +1 -1
  91. package/dist/storage/blob/directory.d.ts +14 -0
  92. package/dist/storage/blob/directory.d.ts.map +1 -0
  93. package/dist/storage/blob/directory.js +80 -0
  94. package/dist/storage/blob/directory.js.map +1 -0
  95. package/dist/storage/folder-adapter.d.ts +22 -0
  96. package/dist/storage/folder-adapter.d.ts.map +1 -1
  97. package/dist/storage/folder-adapter.js +5 -5
  98. package/dist/storage/folder-adapter.js.map +1 -1
  99. package/dist/storage/mirror.d.ts +9 -2
  100. package/dist/storage/mirror.d.ts.map +1 -1
  101. package/dist/storage/mirror.js +60 -35
  102. package/dist/storage/mirror.js.map +1 -1
  103. package/dist/storage/segment.d.ts +7 -2
  104. package/dist/storage/segment.d.ts.map +1 -1
  105. package/dist/storage/segment.js +25 -3
  106. package/dist/storage/segment.js.map +1 -1
  107. package/dist/sync/sync-engine.d.ts +2 -0
  108. package/dist/sync/sync-engine.d.ts.map +1 -1
  109. package/dist/sync/sync-engine.js +5 -0
  110. package/dist/sync/sync-engine.js.map +1 -1
  111. package/dist/sync/sync-messages.d.ts +24 -0
  112. package/dist/sync/sync-messages.d.ts.map +1 -1
  113. package/dist/sync/sync-messages.js.map +1 -1
  114. package/docs/node.md +39 -22
  115. package/docs/spaces.md +18 -10
  116. package/docs/standard-library.md +16 -20
  117. package/docs/storage.md +120 -15
  118. package/package.json +1 -1
@@ -48,8 +48,11 @@ import { ACCESS_COLLECTIONS, ASSIGN_COLLECTION, DEFINITION_COLLECTION, INVITE_CO
48
48
  import { checkFoundingRole, definitionBody, KEY_CHANGE, readDefinition, RECORD_DELETE, RECORD_WRITE, SPACE_RELAYS, } from '../space/grants.js';
49
49
  import { FOUNDER } from '../space/space-manager.js';
50
50
  import { createSyncEngine } from '../sync/sync-engine.js';
51
+ import { SYNC_PROTOCOL_VERSION } from '../sync/sync-messages.js';
52
+ import { createSpaceFiles } from '../files/space-files.js';
53
+ import { fileRefsIn, MAX_FILES } from '../files/file-id.js';
51
54
  import { CATALOG_COLLECTION, checkStoredCollection, isStoredCollection, toJsonSchema, validateJsonSchema, } from '../schema/collection-def.js';
52
- import { CARRIER_COLLECTION, MEMBERSHIP_COLLECTION, PROFILE_COLLECTION } from '../space/account-registry.js';
55
+ import { CARRIER_COLLECTION, MEMBERSHIP_COLLECTION, PROFILE_COLLECTION, isAvatarRef, } from '../space/account-registry.js';
53
56
  import { PASS_COLLECTION } from '../space/pass.js';
54
57
  import { base32Encode, cidOfDigest, hashedKey, sha256 } from '../utils/hash.js';
55
58
  import { sameTags, tagsFor, topicKey, topicTag } from '../records/topics.js';
@@ -1728,6 +1731,9 @@ export async function openSpaceRuntime(deps) {
1728
1731
  break;
1729
1732
  found = { did: verdict.root, name: name.trim().slice(0, 64), updatedAt: version.createdAt };
1730
1733
  }
1734
+ // The picture, from the newest version: one without it shows none (the older stay kept, as retained).
1735
+ if (!found.avatar && found.updatedAt === version.createdAt && isAvatarRef(body?.avatar))
1736
+ found = { ...found, avatar: body.avatar };
1731
1737
  // The contact key from the newest version that has one: it never changes, and an app
1732
1738
  // without it that wrote a newer version first must not hide it.
1733
1739
  if (isContactPublicKey(body?.contactKey)) {
@@ -1776,7 +1782,45 @@ export async function openSpaceRuntime(deps) {
1776
1782
  * verdict still holds. Given nothing — another tab, a folder, new keys —
1777
1783
  * everything is.
1778
1784
  */
1785
+ let spaceFiles = null;
1786
+ const missingFiles = () => {
1787
+ throw new Error('The space is still opening');
1788
+ };
1789
+ let filesTimer = null;
1790
+ /** One pass at a time; a change during one runs another after it */
1791
+ let filesPass = null;
1792
+ let filesAgain = false;
1793
+ /** After a change: which files are named is worked out again, and soon kept, dropped and fetched to match */
1794
+ function filesChanged() {
1795
+ spaceFiles?.changed();
1796
+ if (filesPass) {
1797
+ filesAgain = true;
1798
+ return;
1799
+ }
1800
+ if (filesTimer || closed)
1801
+ return;
1802
+ filesTimer = setTimeout(() => {
1803
+ filesTimer = null;
1804
+ filesPass = (async () => {
1805
+ await spaceFiles?.tidy();
1806
+ if (deps.fetchFiles === 'all')
1807
+ await spaceFiles?.fetchAll();
1808
+ })()
1809
+ .catch(() => {
1810
+ // A peer or mirror out of reach; the next change tries again.
1811
+ })
1812
+ .finally(() => {
1813
+ filesPass = null;
1814
+ if (filesAgain && !closed) {
1815
+ filesAgain = false;
1816
+ filesChanged();
1817
+ }
1818
+ });
1819
+ }, 2000);
1820
+ unref(filesTimer);
1821
+ }
1779
1822
  const recordsChanged = (placed) => {
1823
+ filesChanged();
1780
1824
  if (placed)
1781
1825
  noteArrived(placed);
1782
1826
  linkIndexCache = null;
@@ -2127,6 +2171,14 @@ export async function openSpaceRuntime(deps) {
2127
2171
  //
2128
2172
  // Every mirror is one more peer: what it holds goes through the same gates
2129
2173
  // as a peer's records, and what this node holds goes out to it.
2174
+ /** What a mirror holds goes through the same gates as a peer's records */
2175
+ const fromMirror = async (version) => {
2176
+ const verdict = await admit(version);
2177
+ if (!verdict.ok && !verdict.withdrawn)
2178
+ return verdict.later ? 'later' : 'refused';
2179
+ await storage.addVersion(version);
2180
+ return 'stored';
2181
+ };
2130
2182
  const mirrors = [];
2131
2183
  for (const [index, store] of (deps.mirrors ?? []).entries()) {
2132
2184
  mirrors.push(await createMirror({
@@ -2134,15 +2186,24 @@ export async function openSpaceRuntime(deps) {
2134
2186
  space: space.id,
2135
2187
  storage,
2136
2188
  state: await deps.stores(`mirrors/${space.id}/${index}`),
2137
- accept: async (version) => {
2138
- const verdict = await admit(version);
2139
- if (!verdict.ok && !verdict.withdrawn)
2140
- return verdict.later ? 'later' : 'refused';
2141
- await storage.addVersion(version);
2142
- return 'stored';
2143
- },
2189
+ accept: fromMirror,
2144
2190
  }));
2145
2191
  }
2192
+ // A data folder is one more: its spaces are segments, like a bucket's, and
2193
+ // what this device holds of them is its own (`node/stores.ts`).
2194
+ const folderMirror = deps.stores.folder
2195
+ ? await createMirror({
2196
+ store: deps.stores.folder,
2197
+ space: space.id,
2198
+ storage,
2199
+ state: await deps.stores(`folder/${space.id}`),
2200
+ accept: fromMirror,
2201
+ // Apps and the CLI on one computer meet through the folder: soon, not in a bucket's time.
2202
+ flushMs: 1000,
2203
+ })
2204
+ : null;
2205
+ if (folderMirror)
2206
+ mirrors.push(folderMirror);
2146
2207
  /** Reads every mirror in the background: a space that opens from nothing — a host that lost its disk — fills in as it goes */
2147
2208
  const pullMirrors = () => Promise.all(mirrors.map((mirror) => mirror
2148
2209
  .pull()
@@ -2187,10 +2248,32 @@ export async function openSpaceRuntime(deps) {
2187
2248
  });
2188
2249
  /** Which network a peer was met on, so replies go back the same way */
2189
2250
  const routes = new Map();
2251
+ // ─── Files ─────────────────────────────────────────────────────────
2252
+ //
2253
+ // Named on the outside of the versions that refer to them, fetched chunk by
2254
+ // chunk and checked as they come, kept while a body that names them is
2255
+ // (spec 05 §16.6).
2256
+ spaceFiles = createSpaceFiles({
2257
+ space: space.id,
2258
+ adapter,
2259
+ storage,
2260
+ send: (peer, message) => routes.get(peer)?.send(peer, {
2261
+ type: 'sync',
2262
+ from: session.did,
2263
+ payload: { ...message, v: SYNC_PROTOCOL_VERSION },
2264
+ }),
2265
+ peers: () => connectedPeers(),
2266
+ mirrors: () => mirrors.map((mirror) => ({ store: mirror.store, writer: mirror.writer })),
2267
+ stands: async (version) => {
2268
+ const standing = await standingOf(version);
2269
+ return standing.ok || !!standing.withdrawn;
2270
+ },
2271
+ });
2190
2272
  const sync = createSyncEngine({
2191
2273
  storageProvider: storage,
2192
2274
  self: session.did,
2193
2275
  holds,
2276
+ files: (peer, message) => spaceFiles?.onMessage(peer, message) ?? Promise.resolve(),
2194
2277
  sendToPeer: (peerId, message) => {
2195
2278
  routes.get(peerId)?.send(peerId, { type: 'sync', from: session.did, payload: message });
2196
2279
  },
@@ -2442,19 +2525,28 @@ export async function openSpaceRuntime(deps) {
2442
2525
  }
2443
2526
  // ─── Writers outside this process ──────────────────────────────────
2444
2527
  //
2445
- // A folder can be written by another origin, or by another device behind
2446
- // whatever syncs the folder. Nothing announces it, so it is looked for.
2528
+ // A data folder is written by other apps, and by other devices behind
2529
+ // whatever syncs it; a directory store by another process (a command beside
2530
+ // a daemon). Nothing announces either, so both are looked for.
2531
+ /** Whether another writer changed anything this node holds */
2532
+ const look = async () => {
2533
+ // The same verdict as for a version from a peer.
2534
+ const reconciled = isFolderAdapter(adapter)
2535
+ ? (await reconcileFolder(storage, adapter, async (version) => storable(await admit(version)))).changed
2536
+ : false;
2537
+ const pulled = folderMirror ? (await folderMirror.pull()).added > 0 : false;
2538
+ return reconciled || pulled;
2539
+ };
2447
2540
  let watchTimer = null;
2448
- if (deps.watchIntervalMs > 0 && isFolderAdapter(adapter)) {
2541
+ if (deps.watchIntervalMs > 0 && (folderMirror || isFolderAdapter(adapter))) {
2449
2542
  let running = false;
2450
2543
  watchTimer = setInterval(() => {
2451
2544
  if (running)
2452
2545
  return;
2453
2546
  running = true;
2454
- // The same verdict as for a version from a peer.
2455
- reconcileFolder(storage, adapter, async (version) => storable(await admit(version)))
2456
- .then((result) => {
2457
- if (!result.changed)
2547
+ look()
2548
+ .then((changed) => {
2549
+ if (!changed)
2458
2550
  return;
2459
2551
  recordsChanged();
2460
2552
  announceSoon();
@@ -2739,6 +2831,10 @@ export async function openSpaceRuntime(deps) {
2739
2831
  }
2740
2832
  let payload = null;
2741
2833
  let tags = [];
2834
+ // The files the body refers to, named on the outside so keepers fetch and keep them.
2835
+ const named = deleted ? [] : fileRefsIn(body);
2836
+ if (named.length > MAX_FILES)
2837
+ throw new Error(`A record names at most ${MAX_FILES} files`);
2742
2838
  if (!deleted) {
2743
2839
  // Refused here, where the writer can fix it. On arrival a misfit is kept
2744
2840
  // and flagged instead — see `conforms`.
@@ -2814,6 +2910,7 @@ export async function openSpaceRuntime(deps) {
2814
2910
  // In the clear only where the body is: a private space sealed them above.
2815
2911
  ...(space.visibility === 'public' && !deleted && links.length ? { links } : {}),
2816
2912
  ...(tags.length ? { tags } : {}),
2913
+ ...(named.length ? { files: named } : {}),
2817
2914
  log,
2818
2915
  }), writer.key);
2819
2916
  // The same verdict every other peer will reach: refused here, with the reason, rather than there.
@@ -3466,6 +3563,8 @@ export async function openSpaceRuntime(deps) {
3466
3563
  // Once at open, then after every change.
3467
3564
  keepUp();
3468
3565
  const pulling = pullMirrors();
3566
+ // What this device holds and the folder lacks (copied in, or written before the folder was) goes out soon.
3567
+ folderMirror?.changed();
3469
3568
  return Object.freeze({
3470
3569
  async list(options = {}) {
3471
3570
  if (options.collection)
@@ -3522,6 +3621,10 @@ export async function openSpaceRuntime(deps) {
3522
3621
  return false;
3523
3622
  return mayNow(current.collection, action, await genesisOf(current));
3524
3623
  },
3624
+ /** Seals a file, keeps it, and gives back the reference a body names it by (spec 05 §16.6) */
3625
+ addFile: (bytes) => (spaceFiles ?? missingFiles()).add(bytes),
3626
+ /** The bytes of a file a body refers to: held here, or fetched from peers or mirrors */
3627
+ readFile: (ref) => (spaceFiles ?? missingFiles()).open(ref),
3525
3628
  /** For the node itself: writes the next version of a record in a collection `put` refuses, like the profile */
3526
3629
  upsertSystem: (collection, recordKey, body) => upsert(collection, recordKey, body),
3527
3630
  async profiles() {
@@ -3536,11 +3639,13 @@ export async function openSpaceRuntime(deps) {
3536
3639
  const mine = (await profileMap()).get(deps.rootDid);
3537
3640
  // An app without the contact key must not take away the one another device published.
3538
3641
  const contactKey = profile.contactKey ?? mine?.contactKey;
3539
- if (!name || (mine?.name === name && mine.contactKey === contactKey))
3642
+ if (!name ||
3643
+ (mine?.name === name && mine.contactKey === contactKey && mine.avatar?.hash === profile.avatar?.hash))
3540
3644
  return;
3541
3645
  await upsert(PROFILE_COLLECTION, await profileKey(deps.rootDid), {
3542
3646
  name,
3543
3647
  ...(contactKey ? { contactKey } : {}),
3648
+ ...(profile.avatar ? { avatar: profile.avatar } : {}),
3544
3649
  });
3545
3650
  },
3546
3651
  rotateKey,
@@ -4033,6 +4138,10 @@ export async function openSpaceRuntime(deps) {
4033
4138
  await keepingUp.done();
4034
4139
  await pulling;
4035
4140
  await Promise.all(mirrors.map((mirror) => mirror.close()));
4141
+ if (filesTimer)
4142
+ clearTimeout(filesTimer);
4143
+ spaceFiles?.close();
4144
+ await filesPass;
4036
4145
  if (watchTimer)
4037
4146
  clearInterval(watchTimer);
4038
4147
  if (announceTimer)