@minca/core 0.1.14 → 0.1.16

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 (528) hide show
  1. package/README.md +4 -4
  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 +21 -4
  5. package/dist/elements/auth-styles.js.map +1 -1
  6. package/dist/elements/minca-auth.d.ts.map +1 -1
  7. package/dist/elements/minca-auth.js +151 -57
  8. package/dist/elements/minca-auth.js.map +1 -1
  9. package/dist/files/space-files.d.ts +15 -3
  10. package/dist/files/space-files.d.ts.map +1 -1
  11. package/dist/files/space-files.js +225 -37
  12. package/dist/files/space-files.js.map +1 -1
  13. package/dist/identity/abilities.d.ts +2 -0
  14. package/dist/identity/abilities.d.ts.map +1 -1
  15. package/dist/identity/abilities.js +2 -0
  16. package/dist/identity/abilities.js.map +1 -1
  17. package/dist/identity/account-vault.d.ts +15 -71
  18. package/dist/identity/account-vault.d.ts.map +1 -1
  19. package/dist/identity/account-vault.js +45 -104
  20. package/dist/identity/account-vault.js.map +1 -1
  21. package/dist/identity/channel.d.ts +3 -9
  22. package/dist/identity/channel.d.ts.map +1 -1
  23. package/dist/identity/channel.js +42 -42
  24. package/dist/identity/channel.js.map +1 -1
  25. package/dist/identity/contact-key.d.ts +14 -29
  26. package/dist/identity/contact-key.d.ts.map +1 -1
  27. package/dist/identity/contact-key.js +31 -51
  28. package/dist/identity/contact-key.js.map +1 -1
  29. package/dist/identity/crypto-p256.d.ts.map +1 -1
  30. package/dist/identity/crypto-p256.js +39 -53
  31. package/dist/identity/crypto-p256.js.map +1 -1
  32. package/dist/identity/hpke.d.ts.map +1 -1
  33. package/dist/identity/hpke.js +19 -19
  34. package/dist/identity/hpke.js.map +1 -1
  35. package/dist/identity/identity-manager.d.ts.map +1 -1
  36. package/dist/identity/identity-manager.js +1 -2
  37. package/dist/identity/identity-manager.js.map +1 -1
  38. package/dist/identity/policy.d.ts.map +1 -1
  39. package/dist/identity/policy.js +35 -61
  40. package/dist/identity/policy.js.map +1 -1
  41. package/dist/identity/recovery-code.d.ts +2 -8
  42. package/dist/identity/recovery-code.d.ts.map +1 -1
  43. package/dist/identity/recovery-code.js +4 -11
  44. package/dist/identity/recovery-code.js.map +1 -1
  45. package/dist/identity/root-signer.d.ts +1 -6
  46. package/dist/identity/root-signer.d.ts.map +1 -1
  47. package/dist/identity/root-signer.js +12 -15
  48. package/dist/identity/root-signer.js.map +1 -1
  49. package/dist/identity/ucan.d.ts.map +1 -1
  50. package/dist/identity/ucan.js +48 -39
  51. package/dist/identity/ucan.js.map +1 -1
  52. package/dist/identity/webauthn.d.ts +6 -11
  53. package/dist/identity/webauthn.d.ts.map +1 -1
  54. package/dist/identity/webauthn.js +18 -30
  55. package/dist/identity/webauthn.js.map +1 -1
  56. package/dist/inbox/inbox.d.ts.map +1 -1
  57. package/dist/inbox/inbox.js +40 -30
  58. package/dist/inbox/inbox.js.map +1 -1
  59. package/dist/inbox/index.d.ts +2 -0
  60. package/dist/inbox/index.d.ts.map +1 -1
  61. package/dist/inbox/index.js +1 -0
  62. package/dist/inbox/index.js.map +1 -1
  63. package/dist/inbox/names.d.ts +54 -0
  64. package/dist/inbox/names.d.ts.map +1 -0
  65. package/dist/inbox/names.js +299 -0
  66. package/dist/inbox/names.js.map +1 -0
  67. package/dist/index.d.ts +3 -6
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +9 -12
  70. package/dist/index.js.map +1 -1
  71. package/dist/network/mesh.d.ts +7 -20
  72. package/dist/network/mesh.d.ts.map +1 -1
  73. package/dist/network/mesh.js +267 -245
  74. package/dist/network/mesh.js.map +1 -1
  75. package/dist/network/relay-link.d.ts +31 -0
  76. package/dist/network/relay-link.d.ts.map +1 -0
  77. package/dist/network/relay-link.js +62 -0
  78. package/dist/network/relay-link.js.map +1 -0
  79. package/dist/network/relay-minca.d.ts +12 -1
  80. package/dist/network/relay-minca.d.ts.map +1 -1
  81. package/dist/network/relay-minca.js +25 -55
  82. package/dist/network/relay-minca.js.map +1 -1
  83. package/dist/network/relay-nostr.d.ts +14 -3
  84. package/dist/network/relay-nostr.d.ts.map +1 -1
  85. package/dist/network/relay-nostr.js +34 -72
  86. package/dist/network/relay-nostr.js.map +1 -1
  87. package/dist/network/relay-socket.d.ts +1 -1
  88. package/dist/network/relay-socket.d.ts.map +1 -1
  89. package/dist/network/relay-socket.js +3 -1
  90. package/dist/network/relay-socket.js.map +1 -1
  91. package/dist/network/relays.d.ts +26 -17
  92. package/dist/network/relays.d.ts.map +1 -1
  93. package/dist/network/relays.js +21 -28
  94. package/dist/network/relays.js.map +1 -1
  95. package/dist/network/rendezvous.d.ts +1 -1
  96. package/dist/network/rendezvous.d.ts.map +1 -1
  97. package/dist/network/rendezvous.js +2 -2
  98. package/dist/network/rendezvous.js.map +1 -1
  99. package/dist/node/actions.d.ts.map +1 -1
  100. package/dist/node/actions.js +56 -39
  101. package/dist/node/actions.js.map +1 -1
  102. package/dist/node/api.d.ts +6 -0
  103. package/dist/node/api.d.ts.map +1 -1
  104. package/dist/node/api.js +6 -0
  105. package/dist/node/api.js.map +1 -1
  106. package/dist/node/carrier.d.ts +5 -0
  107. package/dist/node/carrier.d.ts.map +1 -1
  108. package/dist/node/carrier.js +94 -73
  109. package/dist/node/carrier.js.map +1 -1
  110. package/dist/node/copy.d.ts +8 -7
  111. package/dist/node/copy.d.ts.map +1 -1
  112. package/dist/node/copy.js +54 -56
  113. package/dist/node/copy.js.map +1 -1
  114. package/dist/node/host.d.ts +39 -65
  115. package/dist/node/host.d.ts.map +1 -1
  116. package/dist/node/host.js +362 -379
  117. package/dist/node/host.js.map +1 -1
  118. package/dist/node/index.d.ts +2 -2
  119. package/dist/node/index.d.ts.map +1 -1
  120. package/dist/node/index.js +1 -1
  121. package/dist/node/index.js.map +1 -1
  122. package/dist/node/node-carriers.d.ts +35 -0
  123. package/dist/node/node-carriers.d.ts.map +1 -0
  124. package/dist/node/node-carriers.js +562 -0
  125. package/dist/node/node-carriers.js.map +1 -0
  126. package/dist/node/node-contacts.d.ts +11 -0
  127. package/dist/node/node-contacts.d.ts.map +1 -0
  128. package/dist/node/node-contacts.js +808 -0
  129. package/dist/node/node-contacts.js.map +1 -0
  130. package/dist/node/node-context.d.ts +76 -0
  131. package/dist/node/node-context.d.ts.map +1 -0
  132. package/dist/node/node-context.js +54 -0
  133. package/dist/node/node-context.js.map +1 -0
  134. package/dist/node/node-hosting.d.ts +20 -0
  135. package/dist/node/node-hosting.d.ts.map +1 -0
  136. package/dist/node/node-hosting.js +449 -0
  137. package/dist/node/node-hosting.js.map +1 -0
  138. package/dist/node/node-records.d.ts +18 -0
  139. package/dist/node/node-records.d.ts.map +1 -0
  140. package/dist/node/node-records.js +262 -0
  141. package/dist/node/node-records.js.map +1 -0
  142. package/dist/node/node.d.ts.map +1 -1
  143. package/dist/node/node.js +197 -1989
  144. package/dist/node/node.js.map +1 -1
  145. package/dist/node/remote.d.ts +5 -6
  146. package/dist/node/remote.d.ts.map +1 -1
  147. package/dist/node/remote.js +272 -260
  148. package/dist/node/remote.js.map +1 -1
  149. package/dist/node/space-catalog.d.ts +40 -0
  150. package/dist/node/space-catalog.d.ts.map +1 -0
  151. package/dist/node/space-catalog.js +158 -0
  152. package/dist/node/space-catalog.js.map +1 -0
  153. package/dist/node/space-checks.d.ts +25 -0
  154. package/dist/node/space-checks.d.ts.map +1 -0
  155. package/dist/node/space-checks.js +343 -0
  156. package/dist/node/space-checks.js.map +1 -0
  157. package/dist/node/space-history.d.ts +37 -0
  158. package/dist/node/space-history.d.ts.map +1 -0
  159. package/dist/node/space-history.js +350 -0
  160. package/dist/node/space-history.js.map +1 -0
  161. package/dist/node/space-holding.d.ts +49 -0
  162. package/dist/node/space-holding.d.ts.map +1 -0
  163. package/dist/node/space-holding.js +267 -0
  164. package/dist/node/space-holding.js.map +1 -0
  165. package/dist/node/space-index.d.ts +41 -0
  166. package/dist/node/space-index.d.ts.map +1 -0
  167. package/dist/node/space-index.js +217 -0
  168. package/dist/node/space-index.js.map +1 -0
  169. package/dist/node/space-keys.d.ts +34 -0
  170. package/dist/node/space-keys.d.ts.map +1 -0
  171. package/dist/node/space-keys.js +309 -0
  172. package/dist/node/space-keys.js.map +1 -0
  173. package/dist/node/space-lines.d.ts +33 -0
  174. package/dist/node/space-lines.d.ts.map +1 -0
  175. package/dist/node/space-lines.js +145 -0
  176. package/dist/node/space-lines.js.map +1 -0
  177. package/dist/node/space-membership.d.ts +69 -0
  178. package/dist/node/space-membership.d.ts.map +1 -0
  179. package/dist/node/space-membership.js +242 -0
  180. package/dist/node/space-membership.js.map +1 -0
  181. package/dist/node/space-mirrors.d.ts +43 -0
  182. package/dist/node/space-mirrors.d.ts.map +1 -0
  183. package/dist/node/space-mirrors.js +125 -0
  184. package/dist/node/space-mirrors.js.map +1 -0
  185. package/dist/node/space-peers.d.ts +104 -0
  186. package/dist/node/space-peers.d.ts.map +1 -0
  187. package/dist/node/space-peers.js +595 -0
  188. package/dist/node/space-peers.js.map +1 -0
  189. package/dist/node/space-profiles.d.ts +18 -0
  190. package/dist/node/space-profiles.d.ts.map +1 -0
  191. package/dist/node/space-profiles.js +105 -0
  192. package/dist/node/space-profiles.js.map +1 -0
  193. package/dist/node/space-rules.d.ts +29 -0
  194. package/dist/node/space-rules.d.ts.map +1 -0
  195. package/dist/node/space-rules.js +144 -0
  196. package/dist/node/space-rules.js.map +1 -0
  197. package/dist/node/space-runtime.d.ts +104 -143
  198. package/dist/node/space-runtime.d.ts.map +1 -1
  199. package/dist/node/space-runtime.js +293 -3954
  200. package/dist/node/space-runtime.js.map +1 -1
  201. package/dist/node/space-standing.d.ts +20 -0
  202. package/dist/node/space-standing.d.ts.map +1 -0
  203. package/dist/node/space-standing.js +317 -0
  204. package/dist/node/space-standing.js.map +1 -0
  205. package/dist/node/space-versions.d.ts +104 -0
  206. package/dist/node/space-versions.d.ts.map +1 -0
  207. package/dist/node/space-versions.js +153 -0
  208. package/dist/node/space-versions.js.map +1 -0
  209. package/dist/node/space-warrants.d.ts +25 -0
  210. package/dist/node/space-warrants.d.ts.map +1 -0
  211. package/dist/node/space-warrants.js +214 -0
  212. package/dist/node/space-warrants.js.map +1 -0
  213. package/dist/node/space-writing.d.ts +72 -0
  214. package/dist/node/space-writing.d.ts.map +1 -0
  215. package/dist/node/space-writing.js +440 -0
  216. package/dist/node/space-writing.js.map +1 -0
  217. package/dist/node/stores.d.ts +19 -50
  218. package/dist/node/stores.d.ts.map +1 -1
  219. package/dist/node/stores.js +14 -33
  220. package/dist/node/stores.js.map +1 -1
  221. package/dist/node/types.d.ts +225 -305
  222. package/dist/node/types.d.ts.map +1 -1
  223. package/dist/node/usage.d.ts +2 -24
  224. package/dist/node/usage.d.ts.map +1 -1
  225. package/dist/node/usage.js +118 -102
  226. package/dist/node/usage.js.map +1 -1
  227. package/dist/node/watch-notifications.d.ts.map +1 -1
  228. package/dist/node/watch-notifications.js +32 -29
  229. package/dist/node/watch-notifications.js.map +1 -1
  230. package/dist/node/worker.d.ts +5 -6
  231. package/dist/node/worker.d.ts.map +1 -1
  232. package/dist/node/worker.js +80 -72
  233. package/dist/node/worker.js.map +1 -1
  234. package/dist/node/writer-keys.d.ts +5 -6
  235. package/dist/node/writer-keys.d.ts.map +1 -1
  236. package/dist/node/writer-keys.js +66 -57
  237. package/dist/node/writer-keys.js.map +1 -1
  238. package/dist/node/writers.d.ts +12 -19
  239. package/dist/node/writers.d.ts.map +1 -1
  240. package/dist/node/writers.js +7 -15
  241. package/dist/node/writers.js.map +1 -1
  242. package/dist/privacy/direct.d.ts.map +1 -1
  243. package/dist/privacy/direct.js +31 -27
  244. package/dist/privacy/direct.js.map +1 -1
  245. package/dist/privacy/space-encryption.d.ts.map +1 -1
  246. package/dist/privacy/space-encryption.js +5 -9
  247. package/dist/privacy/space-encryption.js.map +1 -1
  248. package/dist/query/computed.d.ts.map +1 -1
  249. package/dist/query/computed.js +78 -77
  250. package/dist/query/computed.js.map +1 -1
  251. package/dist/query/engine.d.ts +3 -4
  252. package/dist/query/engine.d.ts.map +1 -1
  253. package/dist/query/engine.js +93 -86
  254. package/dist/query/engine.js.map +1 -1
  255. package/dist/query/filter.d.ts +8 -10
  256. package/dist/query/filter.d.ts.map +1 -1
  257. package/dist/query/filter.js +144 -161
  258. package/dist/query/filter.js.map +1 -1
  259. package/dist/records/cel-types.d.ts.map +1 -1
  260. package/dist/records/cel-types.js +13 -27
  261. package/dist/records/cel-types.js.map +1 -1
  262. package/dist/records/cel.d.ts.map +1 -1
  263. package/dist/records/cel.js +177 -242
  264. package/dist/records/cel.js.map +1 -1
  265. package/dist/records/rules.d.ts.map +1 -1
  266. package/dist/records/rules.js +46 -50
  267. package/dist/records/rules.js.map +1 -1
  268. package/dist/records/topics.d.ts.map +1 -1
  269. package/dist/records/topics.js +18 -22
  270. package/dist/records/topics.js.map +1 -1
  271. package/dist/schema/collection-def.d.ts.map +1 -1
  272. package/dist/schema/collection-def.js +16 -5
  273. package/dist/schema/collection-def.js.map +1 -1
  274. package/dist/schemas/automations.d.ts +16 -15
  275. package/dist/schemas/automations.d.ts.map +1 -1
  276. package/dist/schemas/automations.js +203 -165
  277. package/dist/schemas/automations.js.map +1 -1
  278. package/dist/schemas/blueprints.d.ts.map +1 -1
  279. package/dist/schemas/blueprints.js +14 -13
  280. package/dist/schemas/blueprints.js.map +1 -1
  281. package/dist/schemas/connectors.d.ts +12 -0
  282. package/dist/schemas/connectors.d.ts.map +1 -1
  283. package/dist/schemas/connectors.js +16 -11
  284. package/dist/schemas/connectors.js.map +1 -1
  285. package/dist/schemas/contacts.d.ts +57 -0
  286. package/dist/schemas/contacts.d.ts.map +1 -1
  287. package/dist/schemas/contacts.js +32 -0
  288. package/dist/schemas/contacts.js.map +1 -1
  289. package/dist/schemas/cron.d.ts.map +1 -1
  290. package/dist/schemas/cron.js +27 -28
  291. package/dist/schemas/cron.js.map +1 -1
  292. package/dist/schemas/defined.d.ts +6 -0
  293. package/dist/schemas/defined.d.ts.map +1 -0
  294. package/dist/schemas/defined.js +7 -0
  295. package/dist/schemas/defined.js.map +1 -0
  296. package/dist/schemas/fragments.d.ts +22 -2
  297. package/dist/schemas/fragments.d.ts.map +1 -1
  298. package/dist/schemas/fragments.js +20 -12
  299. package/dist/schemas/fragments.js.map +1 -1
  300. package/dist/schemas/guides.d.ts +3 -1
  301. package/dist/schemas/guides.d.ts.map +1 -1
  302. package/dist/schemas/guides.js +13 -12
  303. package/dist/schemas/guides.js.map +1 -1
  304. package/dist/schemas/index.d.ts +2 -2
  305. package/dist/schemas/index.d.ts.map +1 -1
  306. package/dist/schemas/index.js +18 -14
  307. package/dist/schemas/index.js.map +1 -1
  308. package/dist/schemas/library/annotations.d.ts +30 -0
  309. package/dist/schemas/library/annotations.d.ts.map +1 -1
  310. package/dist/schemas/library/annotations.js +1 -0
  311. package/dist/schemas/library/annotations.js.map +1 -1
  312. package/dist/schemas/library/community.d.ts +28 -0
  313. package/dist/schemas/library/community.d.ts.map +1 -1
  314. package/dist/schemas/library/life.d.ts +62 -2
  315. package/dist/schemas/library/life.d.ts.map +1 -1
  316. package/dist/schemas/library/media.d.ts +36 -0
  317. package/dist/schemas/library/media.d.ts.map +1 -1
  318. package/dist/schemas/library/money.d.ts +74 -2
  319. package/dist/schemas/library/money.d.ts.map +1 -1
  320. package/dist/schemas/library/planning.d.ts +56 -0
  321. package/dist/schemas/library/planning.d.ts.map +1 -1
  322. package/dist/schemas/library/publishing.d.ts +80 -4
  323. package/dist/schemas/library/publishing.d.ts.map +1 -1
  324. package/dist/schemas/library/social.d.ts +44 -0
  325. package/dist/schemas/library/social.d.ts.map +1 -1
  326. package/dist/session/account-hint.d.ts.map +1 -1
  327. package/dist/session/account-hint.js +8 -5
  328. package/dist/session/account-hint.js.map +1 -1
  329. package/dist/session/auth.d.ts +36 -2
  330. package/dist/session/auth.d.ts.map +1 -1
  331. package/dist/session/auth.js +95 -2
  332. package/dist/session/auth.js.map +1 -1
  333. package/dist/session/connect.d.ts +1 -0
  334. package/dist/session/connect.d.ts.map +1 -1
  335. package/dist/session/connect.js +3 -1
  336. package/dist/session/connect.js.map +1 -1
  337. package/dist/session/credentials.d.ts +8 -26
  338. package/dist/session/credentials.d.ts.map +1 -1
  339. package/dist/session/credentials.js +14 -32
  340. package/dist/session/credentials.js.map +1 -1
  341. package/dist/session/index.d.ts +1 -0
  342. package/dist/session/index.d.ts.map +1 -1
  343. package/dist/session/index.js +1 -0
  344. package/dist/session/index.js.map +1 -1
  345. package/dist/session/pairing.d.ts.map +1 -1
  346. package/dist/session/pairing.js +92 -88
  347. package/dist/session/pairing.js.map +1 -1
  348. package/dist/session/password-strength.d.ts.map +1 -1
  349. package/dist/session/password-strength.js +37 -37
  350. package/dist/session/password-strength.js.map +1 -1
  351. package/dist/session/places.d.ts.map +1 -1
  352. package/dist/session/places.js +18 -13
  353. package/dist/session/places.js.map +1 -1
  354. package/dist/session/popup.d.ts +13 -0
  355. package/dist/session/popup.d.ts.map +1 -0
  356. package/dist/session/popup.js +23 -0
  357. package/dist/session/popup.js.map +1 -0
  358. package/dist/space/constitution.d.ts.map +1 -1
  359. package/dist/space/constitution.js +22 -29
  360. package/dist/space/constitution.js.map +1 -1
  361. package/dist/space/grants.d.ts.map +1 -1
  362. package/dist/space/grants.js +37 -39
  363. package/dist/space/grants.js.map +1 -1
  364. package/dist/space/invite-link.d.ts.map +1 -1
  365. package/dist/space/invite-link.js +33 -33
  366. package/dist/space/invite-link.js.map +1 -1
  367. package/dist/space/own-automations.d.ts +13 -24
  368. package/dist/space/own-automations.d.ts.map +1 -1
  369. package/dist/space/own-automations.js +47 -37
  370. package/dist/space/own-automations.js.map +1 -1
  371. package/dist/space/roles.d.ts +56 -80
  372. package/dist/space/roles.d.ts.map +1 -1
  373. package/dist/space/roles.js +448 -441
  374. package/dist/space/roles.js.map +1 -1
  375. package/dist/space/space-access.d.ts +3 -4
  376. package/dist/space/space-access.d.ts.map +1 -1
  377. package/dist/space/space-access.js +3 -8
  378. package/dist/space/space-access.js.map +1 -1
  379. package/dist/storage/blob/directory.d.ts.map +1 -1
  380. package/dist/storage/blob/directory.js +39 -32
  381. package/dist/storage/blob/directory.js.map +1 -1
  382. package/dist/storage/blob/s3.d.ts.map +1 -1
  383. package/dist/storage/blob/s3.js +21 -19
  384. package/dist/storage/blob/s3.js.map +1 -1
  385. package/dist/storage/blob-store.d.ts +0 -9
  386. package/dist/storage/blob-store.d.ts.map +1 -1
  387. package/dist/storage/directory-access.d.ts +8 -32
  388. package/dist/storage/directory-access.d.ts.map +1 -1
  389. package/dist/storage/directory-access.js +11 -36
  390. package/dist/storage/directory-access.js.map +1 -1
  391. package/dist/storage/encrypted-adapter.d.ts +12 -32
  392. package/dist/storage/encrypted-adapter.d.ts.map +1 -1
  393. package/dist/storage/encrypted-adapter.js +31 -54
  394. package/dist/storage/encrypted-adapter.js.map +1 -1
  395. package/dist/storage/folder-adapter.d.ts +12 -26
  396. package/dist/storage/folder-adapter.d.ts.map +1 -1
  397. package/dist/storage/folder-adapter.js +37 -54
  398. package/dist/storage/folder-adapter.js.map +1 -1
  399. package/dist/storage/folder-reconcile.d.ts +12 -26
  400. package/dist/storage/folder-reconcile.d.ts.map +1 -1
  401. package/dist/storage/folder-reconcile.js +39 -48
  402. package/dist/storage/folder-reconcile.js.map +1 -1
  403. package/dist/storage/indexeddb-adapter.d.ts +2 -6
  404. package/dist/storage/indexeddb-adapter.d.ts.map +1 -1
  405. package/dist/storage/indexeddb-adapter.js +60 -114
  406. package/dist/storage/indexeddb-adapter.js.map +1 -1
  407. package/dist/storage/mirror.d.ts +0 -29
  408. package/dist/storage/mirror.d.ts.map +1 -1
  409. package/dist/storage/mirror.js +153 -110
  410. package/dist/storage/mirror.js.map +1 -1
  411. package/dist/storage/segment.d.ts.map +1 -1
  412. package/dist/storage/segment.js +3 -7
  413. package/dist/storage/segment.js.map +1 -1
  414. package/dist/storage/storage-provider.d.ts +6 -11
  415. package/dist/storage/storage-provider.d.ts.map +1 -1
  416. package/dist/storage/storage-provider.js +112 -116
  417. package/dist/storage/storage-provider.js.map +1 -1
  418. package/dist/sync/sync-engine.d.ts +10 -44
  419. package/dist/sync/sync-engine.d.ts.map +1 -1
  420. package/dist/sync/sync-engine.js +245 -247
  421. package/dist/sync/sync-engine.js.map +1 -1
  422. package/dist/sync/sync-messages.d.ts +1 -1
  423. package/dist/sync/sync-messages.d.ts.map +1 -1
  424. package/dist/sync/sync-messages.js +1 -1
  425. package/dist/sync/sync-messages.js.map +1 -1
  426. package/dist/utils/guards.d.ts +1 -0
  427. package/dist/utils/guards.d.ts.map +1 -1
  428. package/dist/utils/guards.js +1 -0
  429. package/dist/utils/guards.js.map +1 -1
  430. package/dist/utils/hmac.d.ts +5 -0
  431. package/dist/utils/hmac.d.ts.map +1 -0
  432. package/dist/utils/hmac.js +20 -0
  433. package/dist/utils/hmac.js.map +1 -0
  434. package/dist/utils/map.d.ts +10 -0
  435. package/dist/utils/map.d.ts.map +1 -0
  436. package/dist/utils/map.js +30 -0
  437. package/dist/utils/map.js.map +1 -0
  438. package/dist/utils/serial.d.ts +2 -0
  439. package/dist/utils/serial.d.ts.map +1 -1
  440. package/dist/utils/serial.js +9 -0
  441. package/dist/utils/serial.js.map +1 -1
  442. package/dist/validation/capability-gate.d.ts +7 -15
  443. package/dist/validation/capability-gate.d.ts.map +1 -1
  444. package/dist/validation/capability-gate.js +32 -36
  445. package/dist/validation/capability-gate.js.map +1 -1
  446. package/dist/validation/crypto-gate.d.ts.map +1 -1
  447. package/dist/validation/crypto-gate.js +12 -21
  448. package/dist/validation/crypto-gate.js.map +1 -1
  449. package/docs/README.md +3 -2
  450. package/docs/actions.md +47 -20
  451. package/docs/automations.md +1 -1
  452. package/docs/blueprints-and-guides.md +57 -13
  453. package/docs/building-an-app.md +11 -8
  454. package/docs/collections.md +33 -5
  455. package/docs/contacts.md +34 -1
  456. package/docs/contracts.md +1 -1
  457. package/docs/direct-messages.md +1 -1
  458. package/docs/inbox.md +5 -11
  459. package/docs/node.md +36 -26
  460. package/docs/records-and-queries.md +1 -1
  461. package/docs/sign-in.md +54 -26
  462. package/docs/spaces.md +6 -6
  463. package/docs/standard-library.md +33 -31
  464. package/docs/storage.md +37 -15
  465. package/docs/usernames.md +161 -0
  466. package/docs/warrants.md +1 -1
  467. package/package.json +2 -21
  468. package/dist/calls/calls.d.ts +0 -140
  469. package/dist/calls/calls.d.ts.map +0 -1
  470. package/dist/calls/calls.js +0 -861
  471. package/dist/calls/calls.js.map +0 -1
  472. package/dist/calls/index.d.ts +0 -4
  473. package/dist/calls/index.d.ts.map +0 -1
  474. package/dist/calls/index.js +0 -3
  475. package/dist/calls/index.js.map +0 -1
  476. package/dist/react/context.d.ts +0 -81
  477. package/dist/react/context.d.ts.map +0 -1
  478. package/dist/react/context.js +0 -97
  479. package/dist/react/context.js.map +0 -1
  480. package/dist/react/follow.d.ts +0 -11
  481. package/dist/react/follow.d.ts.map +0 -1
  482. package/dist/react/follow.js +0 -7
  483. package/dist/react/follow.js.map +0 -1
  484. package/dist/react/index.d.ts +0 -44
  485. package/dist/react/index.d.ts.map +0 -1
  486. package/dist/react/index.js +0 -40
  487. package/dist/react/index.js.map +0 -1
  488. package/dist/react/minca-auth.d.ts +0 -19
  489. package/dist/react/minca-auth.d.ts.map +0 -1
  490. package/dist/react/minca-auth.js +0 -36
  491. package/dist/react/minca-auth.js.map +0 -1
  492. package/dist/react/use-calls.d.ts +0 -21
  493. package/dist/react/use-calls.d.ts.map +0 -1
  494. package/dist/react/use-calls.js +0 -39
  495. package/dist/react/use-calls.js.map +0 -1
  496. package/dist/react/use-invite-link.d.ts +0 -24
  497. package/dist/react/use-invite-link.d.ts.map +0 -1
  498. package/dist/react/use-invite-link.js +0 -62
  499. package/dist/react/use-invite-link.js.map +0 -1
  500. package/dist/react/use-live.d.ts +0 -16
  501. package/dist/react/use-live.d.ts.map +0 -1
  502. package/dist/react/use-live.js +0 -56
  503. package/dist/react/use-live.js.map +0 -1
  504. package/dist/react/use-my-name.d.ts +0 -12
  505. package/dist/react/use-my-name.d.ts.map +0 -1
  506. package/dist/react/use-my-name.js +0 -38
  507. package/dist/react/use-my-name.js.map +0 -1
  508. package/dist/react/use-names.d.ts +0 -7
  509. package/dist/react/use-names.d.ts.map +0 -1
  510. package/dist/react/use-names.js +0 -13
  511. package/dist/react/use-names.js.map +0 -1
  512. package/dist/react/use-network.d.ts +0 -9
  513. package/dist/react/use-network.d.ts.map +0 -1
  514. package/dist/react/use-network.js +0 -33
  515. package/dist/react/use-network.js.map +0 -1
  516. package/dist/react/use-query.d.ts +0 -14
  517. package/dist/react/use-query.d.ts.map +0 -1
  518. package/dist/react/use-query.js +0 -26
  519. package/dist/react/use-query.js.map +0 -1
  520. package/dist/react/use-space.d.ts +0 -29
  521. package/dist/react/use-space.d.ts.map +0 -1
  522. package/dist/react/use-space.js +0 -57
  523. package/dist/react/use-space.js.map +0 -1
  524. package/dist/react/use-spaces.d.ts +0 -29
  525. package/dist/react/use-spaces.d.ts.map +0 -1
  526. package/dist/react/use-spaces.js +0 -78
  527. package/dist/react/use-spaces.js.map +0 -1
  528. package/docs/calls.md +0 -189
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Keeps one store in step with its peers.
3
+ *
4
+ * 1. A peer says hello with a fingerprint of each collection it keeps. Equal
5
+ * fingerprints mean the same versions.
6
+ * 2. For each collection that differs, the initiator reconciles it with the
7
+ * other (`negentropy.ts`), and ends up knowing which versions each side lacks.
8
+ * 3. The initiator asks for what it lacks, and sends what the other lacks.
9
+ * Everything that arrives passes the gatekeeper before it is stored.
10
+ *
11
+ * Cost follows the difference, not the size. The initiator is the peer whose
12
+ * id sorts first; the other answers a hello with its own, which starts it.
13
+ *
14
+ * A node may hold every collection, or only some (plus `sys.*`, which every
15
+ * node holds), and says which in its hello. Two peers reconcile only what both
16
+ * hold. Whatever a node takes in from a peer, it tells that peer (`stored`), so
17
+ * a node holding part of a space can let go of its own writes.
18
+ */
19
+ import { getOrAdd } from '../utils/map.js';
1
20
  import { cidDigest, cidOfDigest } from '../utils/hash.js';
2
21
  import { base64UrlDecode, base64UrlEncode, bytesToHex } from '../utils/encoding.js';
3
22
  import { createEmitter } from '../utils/events.js';
@@ -7,32 +26,39 @@ import { parseSyncMessage, SYNC_PROTOCOL_VERSION, } from './sync-messages.js';
7
26
  const MAX_WAITING = 1000;
8
27
  /** Refusals remembered, so reconciling doesn't ask a peer for the same bad version every round */
9
28
  const MAX_REFUSED = 10_000;
10
- /** Whether a collection is held, given what a node holds */
11
- const holdsCollection = (holds, collection) => collection.startsWith('sys.') || holds === 'all' || holds.has(collection);
12
- /** Collection definitions first, then records by version — what others depend on comes first */
13
- const rank = (e) => (e?.collection === 'sys.collection' ? -1 : (e?.seq ?? 0));
14
- /** Records asked for, or sent, in one message. */
29
+ /** Records asked for, or sent, in one message */
15
30
  const MAX_IDS_PER_REQUEST = 200;
16
31
  /** `want`s in flight to one peer at a time; the rest queue */
17
32
  const MAX_WANTS_IN_FLIGHT = 4;
18
- /** Largest Negentropy message, in bytes before base64. Stays well inside a data channel's limit. */
33
+ /** Largest Negentropy message, in bytes before base64: well inside a data channel's limit */
19
34
  const FRAME_SIZE_LIMIT = 32_000;
35
+ /** Collections one hello may name. Past this it is hostile, not big. */
36
+ const MAX_COLLECTIONS = 1000;
37
+ /** Rounds one reconciliation may take before it is given up as runaway */
38
+ const MAX_ROUNDS = 64;
39
+ /** A session or want that has heard nothing for this long is given up on */
40
+ const STALE_MS = 30_000;
41
+ /** Whether a collection is held, given what a node holds */
42
+ const holdsCollection = (holds, collection) => collection.startsWith('sys.') || holds === 'all' || holds.has(collection);
43
+ /** What a hello says the peer holds; anything malformed counts as holding nothing past `sys.*` */
44
+ function readHolds(value) {
45
+ if (value === undefined || value === 'all')
46
+ return 'all';
47
+ if (!Array.isArray(value) || value.length > MAX_COLLECTIONS)
48
+ return new Set();
49
+ return new Set(value.filter((c) => typeof c === 'string'));
50
+ }
51
+ /** Collection definitions first, then records by version: what others depend on comes first */
52
+ const rank = (e) => (e?.collection === 'sys.collection' ? -1 : (e?.seq ?? 0));
20
53
  /**
21
- * Versions in messages of at most `size`, newest first, with every version of
22
- * one record in the same message where they fit. A message is taken in whole
23
- * (§8 of the sync spec), so a record arrives with its first version and the
24
- * versions that changed or deleted it, and is never shown as it once was —
25
- * nor waits for the rest while the peer sends everything else.
54
+ * Versions in messages of at most `size`, with every version of one record in
55
+ * the same message where they fit. A message is taken in whole, so a record
56
+ * arrives with the versions that changed or deleted it, never as it once was.
26
57
  */
27
58
  function byRecord(versions, size) {
28
59
  const records = new Map();
29
- for (const version of versions) {
30
- const record = records.get(version.key);
31
- if (record)
32
- record.push(version);
33
- else
34
- records.set(version.key, [version]);
35
- }
60
+ for (const version of versions)
61
+ getOrAdd(records, version.key, () => []).push(version);
36
62
  const messages = [];
37
63
  let message = [];
38
64
  for (const record of records.values()) {
@@ -40,7 +66,7 @@ function byRecord(versions, size) {
40
66
  messages.push(message);
41
67
  message = [];
42
68
  }
43
- // A record with more versions than fit in one message goes over several, newest first.
69
+ // A record with more versions than fit in one message goes over several.
44
70
  for (let i = 0; i < record.length; i += size) {
45
71
  const part = record.slice(i, i + size);
46
72
  if (message.length + part.length > size) {
@@ -54,73 +80,45 @@ function byRecord(versions, size) {
54
80
  messages.push(message);
55
81
  return messages;
56
82
  }
57
- /** Collections one hello may name. Past this it is hostile, not big. */
58
- const MAX_COLLECTIONS = 1000;
59
- /** Rounds one reconciliation may take before it is given up as runaway. */
60
- const MAX_ROUNDS = 64;
61
- /** A session that has heard nothing for this long is given up on. */
62
- const STALE_MS = 30_000;
63
83
  /** Creates a sync engine orchestrator. */
64
84
  export function createSyncEngine(config) {
65
85
  const { storageProvider: storage, sendToPeer, heartbeatInterval = 30000, validate, self } = config;
66
86
  const ourHolds = config.holds ?? (() => 'all');
87
+ const { on, off, emit } = createEmitter();
88
+ const report = (err) => emit('error', err);
67
89
  /** Peers to say hello to and push to */
68
90
  const peers = new Set();
69
- /** What's in flight with each peer */
70
91
  const states = new Map();
71
- let intervalId = null;
72
- let nextId = 1;
73
- const { on, off, emit } = createEmitter();
74
- const stamp = (msg) => ({ v: SYNC_PROTOCOL_VERSION, ...msg });
75
- const send = (peerId, msg) => sendToPeer(peerId, stamp(msg));
76
- const stateOf = (peerId) => states.get(peerId) ??
77
- states
78
- .set(peerId, { sessions: new Map(), wants: new Map(), queued: [], queuedIds: new Set(), holds: 'all' })
79
- .get(peerId);
80
- // ─── Taking in versions ────────────────────────────────────────────
81
- /** Records that could not be judged yet — tried again whenever something new is admitted */
92
+ /** Peers found level with this node, until it takes in or writes anything more */
93
+ const level = new Set();
94
+ /** Records that could not be judged yet, tried again whenever something new is admitted */
82
95
  const waiting = new Map();
83
96
  /**
84
- * Versions the gatekeeper refused, as `peer + id`, oldest first. By peer,
85
- * not by id alone: an id doesn't cover the signature, so a stranger's
86
- * mangled copy shares the real record's id — refusing it must not stop us
87
- * fetching the real one from someone else.
97
+ * Refused versions, by peer and id, oldest first. An id doesn't cover the
98
+ * signature, so a stranger's mangled copy must not stop us fetching the real one.
88
99
  */
89
100
  const refused = new Set();
90
- /**
91
- * Which connection to each peer is open now, numbered across all of them:
92
- * what a message arrived on, so one from a connection since closed is not
93
- * taken for the new one's.
94
- */
101
+ /** Which connection to each peer is open now, so a message from a closed one is dropped */
95
102
  const connections = new Map();
103
+ /** Where each peer's messages are up to */
104
+ const inOrder = new Map();
96
105
  let lastConnection = 0;
97
- const refusal = (peerId, id) => `${peerId}\n${id}`;
98
- /**
99
- * Commits a version from a peer, but only if the gatekeeper allows it.
100
- * A version waiting for its first version, or for the version before it,
101
- * names them, so they are asked for at once rather than on the next round.
102
- */
106
+ let intervalId = null;
107
+ let nextId = 1;
108
+ const send = (peerId, msg) => sendToPeer(peerId, { v: SYNC_PROTOCOL_VERSION, ...msg });
109
+ const stateOf = (peerId) => getOrAdd(states, peerId, newPeerState);
110
+ const peerHolds = (peerId, collection) => holdsCollection(states.get(peerId)?.holds ?? 'all', collection);
111
+ const isRefused = (peerId, id) => refused.has(refusal(peerId, id));
112
+ // ─── Taking in versions ────────────────────────────────────────────
113
+ /** Stores a version from a peer if the gatekeeper allows it; one that must wait names what it waits for */
103
114
  const admit = async (peerId, version, intake) => {
104
- if (validate) {
105
- const verdict = await validate(version);
106
- if (!verdict.valid) {
107
- if (verdict.later) {
108
- waiting.set(version.id, { peerId, version });
109
- if (waiting.size > MAX_WAITING)
110
- waiting.delete(waiting.keys().next().value);
111
- for (const needed of version.seq > 0 ? [version.genesis, version.prev] : []) {
112
- if (typeof needed === 'string' && !(await storage.getVersion(needed)))
113
- intake.firsts.set(needed, version.collection);
114
- }
115
- }
116
- else {
117
- refused.add(refusal(peerId, version.id));
118
- if (refused.size > MAX_REFUSED)
119
- refused.delete(refused.values().next().value);
120
- emit('rejected', peerId, version, verdict.reason);
121
- }
122
- return false;
123
- }
115
+ const verdict = validate ? await validate(version) : { valid: true };
116
+ if (!verdict.valid) {
117
+ if (verdict.later)
118
+ await holdBack(peerId, version, intake);
119
+ else
120
+ refuse(peerId, version, verdict.reason);
121
+ return false;
124
122
  }
125
123
  waiting.delete(version.id);
126
124
  await storage.addVersion(version);
@@ -128,6 +126,21 @@ export function createSyncEngine(config) {
128
126
  emit('version-received', version);
129
127
  return true;
130
128
  };
129
+ const holdBack = async (peerId, version, intake) => {
130
+ waiting.set(version.id, { peerId, version });
131
+ dropOldest(waiting, MAX_WAITING);
132
+ const dependsOn = version.seq > 0 ? [version.genesis, version.prev] : [];
133
+ for (const needed of dependsOn) {
134
+ if (typeof needed === 'string' && !(await storage.getVersion(needed)))
135
+ intake.firsts.set(needed, version.collection);
136
+ }
137
+ };
138
+ const refuse = (peerId, version, reason) => {
139
+ refused.add(refusal(peerId, version.id));
140
+ dropOldest(refused, MAX_REFUSED);
141
+ emit('rejected', peerId, version, reason);
142
+ };
143
+ /** Tries every waiting version again until nothing more goes in; each sender hears what was stored */
131
144
  const retryWaiting = async (intake) => {
132
145
  const stored = new Map();
133
146
  let progressed = true;
@@ -138,20 +151,16 @@ export function createSyncEngine(config) {
138
151
  if (!(await admit(held.peerId, held.version, intake)))
139
152
  continue;
140
153
  progressed = true;
141
- (stored.get(held.peerId) ?? stored.set(held.peerId, []).get(held.peerId)).push(id);
154
+ getOrAdd(stored, held.peerId, () => []).push(id);
142
155
  }
143
156
  }
144
- // Stored at last: the peer that sent it hears so, as it would have had it gone in at once.
145
157
  for (const [peerId, ids] of stored)
146
158
  if (peers.has(peerId))
147
159
  send(peerId, { type: 'stored', ids });
148
160
  };
149
161
  /**
150
- * Queues these versions to ask a peer for, in this order, or ahead of
151
- * everything queued (`first`), as the space's own records always are: who
152
- * may do what decides whether the rest counts. A few wants are in flight at
153
- * a time: what arrives can then pull what it waits for ahead of the rest,
154
- * and fewer versions wait at once.
162
+ * Queues ids to ask a peer for, behind the rest or ahead of them (`ahead`,
163
+ * and always for `sys.*`: who may do what decides whether the rest counts).
155
164
  */
156
165
  const want = (peerId, collection, ids, ahead = false) => {
157
166
  const first = ahead || collection.startsWith('sys.');
@@ -165,41 +174,21 @@ export function createSyncEngine(config) {
165
174
  state.queued.push(...entries);
166
175
  pump(peerId);
167
176
  };
168
- /** Sends wants from the front of the queue while there is room in flight, at most 200 ids each, one collection each */
177
+ /** Sends wants from the front of the queue while there is room in flight */
169
178
  const pump = (peerId) => {
170
179
  const state = states.get(peerId);
171
180
  if (!state)
172
181
  return;
173
182
  while (state.wants.size < MAX_WANTS_IN_FLIGHT && state.queuedIds.size > 0) {
174
- const ids = [];
175
- let collection = null;
176
- while (state.queued.length > 0 && ids.length < MAX_IDS_PER_REQUEST) {
177
- const next = state.queued[0];
178
- if (!state.queuedIds.has(next.id)) {
179
- state.queued.shift();
180
- continue;
181
- }
182
- if (collection !== null && next.collection !== collection)
183
- break;
184
- collection = next.collection;
185
- state.queued.shift();
186
- state.queuedIds.delete(next.id);
187
- ids.push(next.id);
188
- }
189
- if (collection === null)
183
+ const batch = takeBatch(state);
184
+ if (!batch)
190
185
  break;
191
- const wantId = nextId++;
192
- state.wants.set(wantId, { ids: new Set(ids), collection, at: Date.now() });
193
- send(peerId, { type: 'want', id: wantId, ids });
186
+ const id = nextId++;
187
+ state.wants.set(id, { ids: new Set(batch.ids), collection: batch.collection, at: Date.now() });
188
+ send(peerId, { type: 'want', id, ids: batch.ids });
194
189
  }
195
190
  };
196
- /** Ids this node has asked a peer for and not yet been answered */
197
- const asked = (peerId) => new Set([...(states.get(peerId)?.wants.values() ?? [])].flatMap((w) => [...w.ids]));
198
- /**
199
- * Admits a batch: definitions and first versions before what depends on
200
- * them, so little has to wait. Versions of collections this node doesn't
201
- * hold are passed over. The peer is told which it now has.
202
- */
191
+ /** Admits a batch, what others depend on first, passing over collections not held here */
203
192
  const admitAll = async (peerId, versions) => {
204
193
  const holds = ourHolds();
205
194
  const stored = [];
@@ -216,23 +205,27 @@ export function createSyncEngine(config) {
216
205
  send(peerId, { type: 'stored', ids: stored });
217
206
  await retryWaiting(intake);
218
207
  }
219
- // First versions still missing once everything that could go in has.
220
- if (intake.firsts.size > 0 && peers.has(peerId)) {
221
- const inFlight = asked(peerId);
222
- const byCollection = new Map();
223
- for (const [id, collection] of intake.firsts) {
224
- if (inFlight.has(id) || refused.has(refusal(peerId, id)) || (await storage.getVersion(id)))
225
- continue;
226
- (byCollection.get(collection) ?? byCollection.set(collection, []).get(collection)).push(id);
227
- }
228
- for (const [collection, ids] of byCollection)
229
- want(peerId, collection, ids, true);
230
- }
208
+ if (intake.firsts.size > 0 && peers.has(peerId))
209
+ await askFirsts(peerId, intake.firsts);
231
210
  if (intake.placed.length > 0) {
232
211
  level.clear();
233
212
  emit('received', intake.placed);
234
213
  }
235
214
  };
215
+ /** Asks at once for the versions still waited on, unless asked, refused or already here */
216
+ const askFirsts = async (peerId, firsts) => {
217
+ const inFlight = askedOf(peerId);
218
+ const byCollection = new Map();
219
+ for (const [id, collection] of firsts) {
220
+ if (inFlight.has(id) || isRefused(peerId, id) || (await storage.getVersion(id)))
221
+ continue;
222
+ getOrAdd(byCollection, collection, () => []).push(id);
223
+ }
224
+ for (const [collection, ids] of byCollection)
225
+ want(peerId, collection, ids, true);
226
+ };
227
+ /** Ids asked of a peer and not yet answered */
228
+ const askedOf = (peerId) => new Set([...(states.get(peerId)?.wants.values() ?? [])].flatMap((w) => [...w.ids]));
236
229
  // ─── Hello ─────────────────────────────────────────────────────────
237
230
  /** A fingerprint of each collection held and kept, hex */
238
231
  const ourSums = async (holds) => {
@@ -252,14 +245,7 @@ export function createSyncEngine(config) {
252
245
  ...(reply ? { reply } : {}),
253
246
  });
254
247
  };
255
- /** What a hello says the peer holds; anything malformed counts as holding nothing past `sys.*` */
256
- const readHolds = (value) => {
257
- if (value === undefined || value === 'all')
258
- return 'all';
259
- if (!Array.isArray(value) || value.length > MAX_COLLECTIONS)
260
- return new Set();
261
- return new Set(value.filter((c) => typeof c === 'string'));
262
- };
248
+ const sayHello = (peerId, reply = false) => void hello(peerId, reply).catch(report);
263
249
  const onHello = async (peerId, theirs, theirHolds, reply) => {
264
250
  if (typeof theirs !== 'object' || theirs === null)
265
251
  return;
@@ -270,20 +256,17 @@ export function createSyncEngine(config) {
270
256
  state.holds = readHolds(theirHolds);
271
257
  const holds = ourHolds();
272
258
  const ours = await ourSums(holds);
273
- // Everything either side names, and every collection this node holds even if it has none of it yet.
259
+ // Every collection this node holds counts, even one it has nothing of yet.
274
260
  const named = new Set([...names, ...Object.keys(ours), ...(holds === 'all' ? [] : holds)]);
275
261
  const shared = [...named].filter((c) => holdsCollection(holds, c) && holdsCollection(state.holds, c));
276
262
  const differing = shared.filter((c) => theirs[c] !== ours[c]);
277
263
  for (const c of shared)
278
264
  if (theirs[c] === ours[c])
279
265
  emit('level', peerId, c, state.holds === 'all');
280
- if (differing.length === 0) {
281
- if (!busy(peerId))
282
- synced(peerId);
283
- return;
284
- }
266
+ if (differing.length === 0)
267
+ return settleIfDone(peerId);
285
268
  if (self !== undefined && self > peerId) {
286
- // The other side starts. It may not have heard from us yet: tell it.
269
+ // The other side starts. It may not have heard from us yet.
287
270
  if (!reply)
288
271
  await hello(peerId, true);
289
272
  return;
@@ -297,15 +280,17 @@ export function createSyncEngine(config) {
297
280
  return !!state && (state.sessions.size > 0 || state.wants.size > 0 || state.queuedIds.size > 0);
298
281
  };
299
282
  const settleIfDone = (peerId) => {
300
- if (!busy(peerId))
301
- synced(peerId);
302
- };
303
- /** Peers found level with this node, until it takes in or writes anything more */
304
- const level = new Set();
305
- const synced = (peerId) => {
283
+ if (busy(peerId))
284
+ return;
306
285
  level.add(peerId);
307
286
  emit('synced', peerId, states.get(peerId)?.holds === 'all');
308
287
  };
288
+ const sendRound = (peerId, session, message) => send(peerId, {
289
+ type: 'reconcile',
290
+ id: session.id,
291
+ collection: session.collection,
292
+ message: base64UrlEncode(message),
293
+ });
309
294
  const begin = async (peerId, collection) => {
310
295
  if (typeof collection !== 'string')
311
296
  return;
@@ -331,30 +316,38 @@ export function createSyncEngine(config) {
331
316
  need: [],
332
317
  };
333
318
  state.sessions.set(collection, session);
334
- send(peerId, {
335
- type: 'reconcile',
336
- id: session.id,
337
- collection,
338
- message: base64UrlEncode(await reconciler.initiate()),
339
- });
319
+ sendRound(peerId, session, await reconciler.initiate());
340
320
  };
341
321
  /**
342
- * A collection's session and every request it made are over: level with the
343
- * peer, as far as could be. The peer is told where things stand now, so it
344
- * knows it is level too — marked a reply, so it never starts another round.
322
+ * When a collection's session and its wants are over, this node is level
323
+ * with the peer, and tells it so in a reply hello that starts no new round.
345
324
  */
346
325
  const levelIfDone = (peerId, collection) => {
347
326
  const state = states.get(peerId);
348
327
  if (!state || state.sessions.has(collection))
349
328
  return;
350
- for (const want of state.wants.values())
351
- if (want.collection === collection)
352
- return;
353
- for (const queued of state.queued)
354
- if (queued.collection === collection && state.queuedIds.has(queued.id))
355
- return;
329
+ const wanting = [...state.wants.values()].some((w) => w.collection === collection);
330
+ const queued = state.queued.some((q) => q.collection === collection && state.queuedIds.has(q.id));
331
+ if (wanting || queued)
332
+ return;
356
333
  emit('level', peerId, collection, state.holds === 'all');
357
- void hello(peerId, true).catch((err) => emit('error', err));
334
+ sayHello(peerId, true);
335
+ };
336
+ /**
337
+ * Ends a session: asks for what we lack and sends what they lack, newest
338
+ * first. Only now, since rounds find versions oldest first, and sending as
339
+ * they go would show a record as it once was.
340
+ */
341
+ const endSession = async (peerId, state, session) => {
342
+ // Asked for before the session goes, so the peer never looks idle in between.
343
+ want(peerId, session.collection, session.need.reverse());
344
+ state.sessions.delete(session.collection);
345
+ const found = await Promise.all(session.have.reverse().map((id) => storage.getVersion(id)));
346
+ const have = found.filter((v) => v !== null);
347
+ for (const versions of byRecord(have, MAX_IDS_PER_REQUEST))
348
+ send(peerId, { type: 'versions', versions });
349
+ if (session.again)
350
+ void begin(peerId, session.collection).catch(report);
358
351
  };
359
352
  const onReconciled = async (peerId, id, message, held) => {
360
353
  const state = states.get(peerId);
@@ -362,23 +355,9 @@ export function createSyncEngine(config) {
362
355
  if (!state || !session)
363
356
  return; // not ours, or already over
364
357
  session.touchedAt = Date.now();
365
- // What they lack goes, and what we lack is asked for, once the session
366
- // has found it all — newest first. Rounds find items oldest first, so
367
- // sending as they go would show a record as it once was (its first
368
- // version) until the version that deleted or changed it came along.
369
- const end = async () => {
370
- // Asked for before the session goes, so the peer never looks idle in between.
371
- want(peerId, session.collection, session.need.reverse());
372
- state.sessions.delete(session.collection);
373
- const have = (await Promise.all(session.have.reverse().map((v) => storage.getVersion(v)))).filter((v) => v !== null);
374
- for (const versions of byRecord(have, MAX_IDS_PER_REQUEST))
375
- send(peerId, { type: 'versions', versions });
376
- if (session.again)
377
- void begin(peerId, session.collection).catch((err) => emit('error', err));
378
- };
379
358
  if (!held) {
380
- // They don't hold it after all — they changed what they hold since their hello.
381
- await end();
359
+ // They changed what they hold since their hello.
360
+ await endSession(peerId, state, session);
382
361
  return settleIfDone(peerId);
383
362
  }
384
363
  let round;
@@ -386,25 +365,23 @@ export function createSyncEngine(config) {
386
365
  round = await session.reconciler.reconcile(base64UrlDecode(message));
387
366
  }
388
367
  catch (err) {
389
- await end();
390
- emit('error', new Error(`Sync with ${peerId} abandoned: ${err instanceof Error ? err.message : String(err)}`));
368
+ await endSession(peerId, state, session);
369
+ emit('error', abandoned(peerId, err instanceof Error ? err.message : String(err)));
391
370
  return settleIfDone(peerId);
392
371
  }
393
- const fresh = (id) => !session.handled.has(id) && !!session.handled.add(id);
394
- session.have.push(...round.have.map(cidOfDigest).filter(fresh));
395
- session.need.push(...round.need.map(cidOfDigest).filter((v) => fresh(v) && !refused.has(refusal(peerId, v))));
396
- if (round.message && ++session.rounds < MAX_ROUNDS) {
397
- send(peerId, {
398
- type: 'reconcile',
399
- id: session.id,
400
- collection: session.collection,
401
- message: base64UrlEncode(round.message),
402
- });
403
- return;
404
- }
372
+ const isNew = (versionId) => {
373
+ if (session.handled.has(versionId))
374
+ return false;
375
+ session.handled.add(versionId);
376
+ return true;
377
+ };
378
+ session.have.push(...round.have.map(cidOfDigest).filter(isNew));
379
+ session.need.push(...round.need.map(cidOfDigest).filter((v) => isNew(v) && !isRefused(peerId, v)));
380
+ if (round.message && ++session.rounds < MAX_ROUNDS)
381
+ return sendRound(peerId, session, round.message);
405
382
  if (round.message)
406
- emit('error', new Error(`Sync with ${peerId} abandoned: more than ${MAX_ROUNDS} rounds`));
407
- await end();
383
+ emit('error', abandoned(peerId, `more than ${MAX_ROUNDS} rounds`));
384
+ await endSession(peerId, state, session);
408
385
  levelIfDone(peerId, session.collection);
409
386
  settleIfDone(peerId);
410
387
  };
@@ -434,91 +411,82 @@ export function createSyncEngine(config) {
434
411
  send(peerId, { type: 'versions', id, versions });
435
412
  };
436
413
  const onVersions = async (peerId, id, versions) => {
414
+ // Sent unasked, because we lacked them: each still passes the gatekeeper.
415
+ if (id === undefined)
416
+ return admitAll(peerId, versions.slice(0, MAX_IDS_PER_REQUEST));
437
417
  const state = states.get(peerId);
438
- if (id === undefined) {
439
- // Sent because we lacked them. Nothing is taken on trust: each passes the gatekeeper.
440
- await admitAll(peerId, versions.slice(0, MAX_IDS_PER_REQUEST));
441
- return;
442
- }
443
418
  const asked = state?.wants.get(id);
444
419
  if (!state || !asked)
445
420
  return;
446
- // Only versions asked for are considered.
447
421
  await admitAll(peerId, versions.filter((v) => asked.ids.has(v?.id)));
448
422
  state.wants.delete(id);
449
423
  pump(peerId);
450
424
  levelIfDone(peerId, asked.collection);
451
425
  settleIfDone(peerId);
452
426
  };
427
+ const onStored = (peerId, ids) => {
428
+ if (!Array.isArray(ids))
429
+ return;
430
+ const named = ids.filter((v) => typeof v === 'string');
431
+ emit('stored', peerId, named.slice(0, 10 * MAX_IDS_PER_REQUEST));
432
+ };
453
433
  // ─── Housekeeping ──────────────────────────────────────────────────
454
- /** Drops sessions and requests a peer stopped answering, so a lost message can't wedge sync */
434
+ /** Drops sessions and wants a peer stopped answering, so a lost message can't wedge sync */
455
435
  const sweep = () => {
456
436
  const now = Date.now();
437
+ const stale = (at) => now - at > STALE_MS;
457
438
  for (const [peerId, state] of states) {
458
439
  for (const [collection, session] of state.sessions)
459
- if (now - session.touchedAt > STALE_MS)
440
+ if (stale(session.touchedAt))
460
441
  state.sessions.delete(collection);
461
- // A want whose answer was lost: what it asked for is found again on the next round.
462
- let dropped = false;
463
- for (const [id, want] of state.wants) {
464
- if (now - want.at <= STALE_MS)
465
- continue;
466
- state.wants.delete(id);
467
- dropped = true;
468
- }
469
- if (!dropped)
442
+ // What a lost want asked for is found again on the next round.
443
+ const lost = [...state.wants].filter(([, want]) => stale(want.at));
444
+ if (lost.length === 0)
470
445
  continue;
446
+ for (const [id] of lost)
447
+ state.wants.delete(id);
471
448
  pump(peerId);
472
449
  settleIfDone(peerId);
473
450
  }
474
451
  };
475
- /** Handles one message from a peer; anything that is not a sync message this peer speaks is ignored */
476
452
  const handle = async (peerId, message) => {
477
453
  try {
478
454
  const msg = parseSyncMessage(message);
479
- // Malformed, a protocol version this peer does not speak, or a peer not
480
- // added yet: an answer to it would have no way back and be lost, leaving
481
- // a session waiting. Each side says hello once it adds the other, so
482
- // nothing said before then is needed.
455
+ // A peer not added yet has no way back for an answer. Each side says
456
+ // hello once it adds the other, so nothing said before then is needed.
483
457
  if (!msg || !peers.has(peerId))
484
458
  return;
485
459
  switch (msg.type) {
486
460
  case 'hello':
487
- await onHello(peerId, msg.sums, msg.holds, msg.reply === true);
488
- break;
461
+ return await onHello(peerId, msg.sums, msg.holds, msg.reply === true);
489
462
  case 'reconcile':
490
- await onReconcile(peerId, msg.id, msg.collection, msg.message);
491
- break;
463
+ return await onReconcile(peerId, msg.id, msg.collection, msg.message);
492
464
  case 'reconciled':
493
- await onReconciled(peerId, msg.id, msg.message, msg.held !== false);
494
- break;
465
+ return await onReconciled(peerId, msg.id, msg.message, msg.held !== false);
495
466
  case 'want':
496
- await serveWant(peerId, msg.id, Array.isArray(msg.ids) ? msg.ids : []);
497
- break;
467
+ return await serveWant(peerId, msg.id, Array.isArray(msg.ids) ? msg.ids : []);
498
468
  case 'versions':
499
- await onVersions(peerId, msg.id, Array.isArray(msg.versions) ? msg.versions : []);
500
- break;
469
+ return await onVersions(peerId, msg.id, Array.isArray(msg.versions) ? msg.versions : []);
501
470
  case 'push-update':
502
471
  if (msg.version && typeof msg.version === 'object')
503
472
  await admitAll(peerId, [msg.version]);
504
- break;
473
+ return;
505
474
  case 'want-file':
506
475
  case 'file':
507
476
  case 'file-done':
508
- await config.files?.(peerId, msg);
509
- break;
477
+ return await config.files?.(peerId, msg);
510
478
  case 'stored':
511
- if (Array.isArray(msg.ids))
512
- emit('stored', peerId, msg.ids.filter((v) => typeof v === 'string').slice(0, 10 * MAX_IDS_PER_REQUEST));
513
- break;
479
+ return onStored(peerId, msg.ids);
514
480
  }
515
481
  }
516
482
  catch (err) {
517
- emit('error', err);
483
+ report(err);
518
484
  }
519
485
  };
520
- /** Where each peer's messages are up to */
521
- const inOrder = new Map();
486
+ const forget = (peerId) => {
487
+ states.delete(peerId);
488
+ level.delete(peerId);
489
+ };
522
490
  return {
523
491
  start() {
524
492
  if (intervalId !== null)
@@ -526,21 +494,19 @@ export function createSyncEngine(config) {
526
494
  intervalId = setInterval(() => {
527
495
  sweep();
528
496
  for (const peer of peers)
529
- void hello(peer).catch((err) => emit('error', err));
497
+ sayHello(peer);
530
498
  }, heartbeatInterval);
531
499
  },
532
500
  stop() {
533
- if (intervalId !== null) {
501
+ if (intervalId !== null)
534
502
  clearInterval(intervalId);
535
- intervalId = null;
536
- }
503
+ intervalId = null;
537
504
  states.clear();
538
505
  },
539
506
  handleMessage(peerId, message) {
540
- // One peer's messages are handled in the order they came: a hello saying
541
- // where things stand must not overtake the versions sent before it.
542
- // One that came on a connection since closed is dropped when its turn
543
- // comes: the peer may be back on a new one, which knows nothing of it.
507
+ // One peer's messages run in the order they came: a hello must not
508
+ // overtake the versions sent before it. One from a connection closed
509
+ // since is dropped when its turn comes.
544
510
  const connection = connections.get(peerId);
545
511
  const run = (inOrder.get(peerId) ?? Promise.resolve()).then(() => connections.get(peerId) === connection ? handle(peerId, message) : undefined);
546
512
  inOrder.set(peerId, run);
@@ -553,40 +519,72 @@ export function createSyncEngine(config) {
553
519
  notifyPeers(peerIds) {
554
520
  for (const peer of peerIds)
555
521
  if (peers.has(peer))
556
- void hello(peer).catch((err) => emit('error', err));
522
+ sayHello(peer);
557
523
  },
558
524
  ask(collection, ids) {
559
525
  if (ids.length === 0)
560
526
  return;
561
527
  for (const peer of peers)
562
- if (holdsCollection(states.get(peer)?.holds ?? 'all', collection))
528
+ if (peerHolds(peer, collection))
563
529
  want(peer, collection, ids, true);
564
530
  },
565
531
  levelWith: (peerId) => level.has(peerId),
566
532
  onLocalChange(version) {
567
533
  level.clear();
568
- // Not to a peer whose hello said it doesn't hold the collection: it would pass it by.
569
- for (const peer of peers) {
570
- if (holdsCollection(states.get(peer)?.holds ?? 'all', version.collection))
534
+ // Not to a peer that doesn't hold the collection: it would pass it by.
535
+ for (const peer of peers)
536
+ if (peerHolds(peer, version.collection))
571
537
  send(peer, { type: 'push-update', version });
572
- }
573
538
  },
574
539
  addPeer(peerId) {
575
540
  // A new connection: nothing half-finished from an earlier one carries over.
576
541
  connections.set(peerId, ++lastConnection);
577
- states.delete(peerId);
578
- level.delete(peerId);
542
+ forget(peerId);
579
543
  peers.add(peerId);
580
544
  },
581
545
  removePeer(peerId) {
582
- // A dropped peer must not leave half-finished sessions holding memory.
583
546
  connections.delete(peerId);
584
547
  peers.delete(peerId);
585
- states.delete(peerId);
586
- level.delete(peerId);
548
+ forget(peerId);
587
549
  },
588
550
  on,
589
551
  off,
590
552
  };
591
553
  }
554
+ /** Takes up to one want's worth of queued ids, all of one collection, off the front of the queue */
555
+ function takeBatch(state) {
556
+ const ids = [];
557
+ let collection = null;
558
+ while (state.queued.length > 0 && ids.length < MAX_IDS_PER_REQUEST) {
559
+ const next = state.queued[0];
560
+ if (!state.queuedIds.has(next.id)) {
561
+ state.queued.shift();
562
+ continue;
563
+ }
564
+ if (collection !== null && next.collection !== collection)
565
+ break;
566
+ collection = next.collection;
567
+ state.queued.shift();
568
+ state.queuedIds.delete(next.id);
569
+ ids.push(next.id);
570
+ }
571
+ return collection === null ? null : { collection, ids };
572
+ }
573
+ const newPeerState = () => ({
574
+ sessions: new Map(),
575
+ wants: new Map(),
576
+ queued: [],
577
+ queuedIds: new Set(),
578
+ holds: 'all',
579
+ });
580
+ const refusal = (peerId, id) => `${peerId}\n${id}`;
581
+ const abandoned = (peerId, why) => new Error(`Sync with ${peerId} abandoned: ${why}`);
582
+ /** Drops the oldest entry once a map or set is over `max` */
583
+ function dropOldest(items, max) {
584
+ if (items.size <= max)
585
+ return;
586
+ const oldest = items.keys().next();
587
+ if (!oldest.done)
588
+ items.delete(oldest.value);
589
+ }
592
590
  //# sourceMappingURL=sync-engine.js.map