@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
package/dist/node/host.js CHANGED
@@ -12,7 +12,6 @@ import { createListeners } from '../utils/events.js';
12
12
  const SPACE_SUBSCRIPTION_PREFIX = 'space:';
13
13
  /** A space's own subscription id: `space:<space id>` */
14
14
  export const spaceSubscription = (spaceId) => `${SPACE_SUBSCRIPTION_PREFIX}${spaceId}`;
15
- const SUBSCRIPTION_PREFIX = 'subscription:';
16
15
  /** The space a subscription is a space's own for, or null for an account's */
17
16
  const spaceIdOf = (id) => id.startsWith(SPACE_SUBSCRIPTION_PREFIX) && id.length > SPACE_SUBSCRIPTION_PREFIX.length && id.length <= 200
18
17
  ? id.slice(SPACE_SUBSCRIPTION_PREFIX.length)
@@ -23,21 +22,13 @@ export class NotAllowedError extends Error {
23
22
  super('This host only carries spaces for the accounts its owner named');
24
23
  }
25
24
  }
26
- /** Where the subscriptions are kept in the bucket */
27
- const BUCKET_PREFIX = 'host/subscriptions/';
28
- /** Where each space's standing against the free line is kept, there and in the bucket */
29
- const LINE_PREFIX = 'line:';
30
- const LINE_BUCKET_PREFIX = 'host/lines/';
31
25
  const DAY = 24 * 3600;
32
26
  /** How far under every limit a space must stay, for its window, to be free again */
33
27
  const UNDER = 0.8;
34
28
  /** How long after the host first sees a space what arrives may be its history, rather than people writing now */
35
29
  const CATCH_UP_SECONDS = 600;
36
- /** Where the host keeps which spaces it once stopped carrying for members, there and in the bucket */
37
- const STRUCK_PREFIX = 'struck:';
38
- /** Days of notice a space gets when it goes over again after its grace was spent, before it is stopped */
30
+ /** Days of notice a space gets when it goes over again after its grace was spent */
39
31
  const NOTICE_DAYS = 7;
40
- const STRUCK_BUCKET_PREFIX = 'host/struck/';
41
32
  /** Starts a host */
42
33
  export async function createHostNode(config) {
43
34
  const provider = config.provider ?? createP256Provider();
@@ -46,29 +37,33 @@ export async function createHostNode(config) {
46
37
  const allowed = (account) => !config.allow || config.allow.includes(account);
47
38
  const store = await config.stores('host');
48
39
  const events = createListeners('host');
40
+ const limits = config.free ? null : (config.limits ?? null);
41
+ const lineGrace = (limits?.graceDays ?? 0) * DAY;
42
+ const keepSeconds = (limits?.keepDays ?? 0) * DAY;
43
+ // Each kind of record is kept in the host's store and in the bucket, which a new disk starts from.
44
+ const kept = {
45
+ subscriptions: shelf(store, config.mirror, 'subscription:', {
46
+ ...bucketNames('host/subscriptions/', { encoded: true, suffix: '.json' }),
47
+ nameOf: (_key, bytes) => subscriptionIdIn(bytes),
48
+ }),
49
+ lines: shelf(store, config.mirror, 'line:', bucketNames('host/lines/', { suffix: '.json' })),
50
+ rooms: shelf(store, config.mirror, 'room:', bucketNames('host/rooms/', { encoded: true })),
51
+ struck: shelf(store, config.mirror, 'struck:', bucketNames('host/struck/')),
52
+ };
49
53
  const read = async (id) => {
50
- const bytes = await store.get(`${SUBSCRIPTION_PREFIX}${id}`);
51
- // Only `write` below puts anything here; a check would turn a damaged record into a missing one.
54
+ const text = await kept.subscriptions.get(id);
55
+ // Only `write` puts anything here; a check would turn a damaged record into a missing one.
52
56
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes
53
- return bytes ? JSON.parse(utf8Decode(bytes)) : null;
57
+ return text ? JSON.parse(text) : null;
54
58
  };
55
- const bucketKey = (id) => `${BUCKET_PREFIX}${encodeURIComponent(id)}.json`;
56
59
  const write = async (subscription) => {
57
- const bytes = utf8Encode(JSON.stringify(subscription));
58
- await store.put(`${SUBSCRIPTION_PREFIX}${subscription.id}`, bytes);
59
- // The bucket's copy is what a host with a new disk starts from.
60
- await config.mirror?.put(bucketKey(subscription.id), bytes);
60
+ await kept.subscriptions.put(subscription.id, JSON.stringify(subscription));
61
61
  return subscription;
62
62
  };
63
- const forget = async (id) => {
64
- await store.delete(`${SUBSCRIPTION_PREFIX}${id}`);
65
- await config.mirror?.delete(bucketKey(id));
66
- };
67
63
  const list = async () => {
68
- const keys = await store.list(SUBSCRIPTION_PREFIX);
69
- return (await Promise.all(keys.map((key) => read(key.slice(SUBSCRIPTION_PREFIX.length))))).filter((s) => s !== null);
64
+ const all = await Promise.all((await kept.subscriptions.names()).map(read));
65
+ return all.filter((subscription) => subscription !== null);
70
66
  };
71
- const limits = config.free ? null : (config.limits ?? null);
72
67
  const state = (subscription) => {
73
68
  if (config.free || subscription.paidUntil >= now())
74
69
  return 'active';
@@ -78,7 +73,11 @@ export async function createHostNode(config) {
78
73
  return subscription.paidUntil + graceSeconds >= now() ? 'grace' : 'lapsed';
79
74
  };
80
75
  /** Which room an account's subscription has, at a host with limits; null otherwise */
81
- const plan = (subscription) => limits && !spaceIdOf(subscription.id) ? (subscription.paidUntil >= now() ? 'paid' : 'free') : null;
76
+ const plan = (subscription) => {
77
+ if (!limits || spaceIdOf(subscription.id))
78
+ return null;
79
+ return subscription.paidUntil >= now() ? 'paid' : 'free';
80
+ };
82
81
  // ─── The free line ─────────────────────────────────────────────────
83
82
  const meter = limits
84
83
  ? await createUsageMeter({
@@ -88,140 +87,77 @@ export async function createHostNode(config) {
88
87
  })
89
88
  : null;
90
89
  const lines = new Map();
91
- const lineBucketKey = (spaceId) => `${LINE_BUCKET_PREFIX}${spaceId}.json`;
92
- const writeLine = async (spaceId, line) => {
93
- if (line)
94
- lines.set(spaceId, line);
95
- else
96
- lines.delete(spaceId);
97
- if (line) {
98
- const bytes = utf8Encode(JSON.stringify(line));
99
- await store.put(`${LINE_PREFIX}${spaceId}`, bytes);
100
- // A restart, or a new disk, keeps the grace clock where it was.
101
- await config.mirror?.put(lineBucketKey(spaceId), bytes);
90
+ /** Each account over its room */
91
+ const rooms = new Map();
92
+ /** Spaces members' subscriptions once stopped carrying, and when: one back over the line gets no new grace */
93
+ const struck = new Map();
94
+ if (limits) {
95
+ for (const [spaceId, text] of await kept.lines.load()) {
96
+ const line = parseLine(JSON.parse(text));
97
+ if (line)
98
+ lines.set(spaceId, line);
102
99
  }
103
- else {
104
- await store.delete(`${LINE_PREFIX}${spaceId}`);
105
- await config.mirror?.delete(lineBucketKey(spaceId));
100
+ for (const [account, text] of await kept.rooms.load()) {
101
+ const room = parseRoom(JSON.parse(text));
102
+ if (room)
103
+ rooms.set(account, room);
106
104
  }
107
- };
108
- if (limits) {
109
- if (config.mirror && (await store.list(LINE_PREFIX)).length === 0)
110
- for (const key of await config.mirror.list(LINE_BUCKET_PREFIX)) {
111
- const bytes = await config.mirror.get(key);
112
- const spaceId = key.slice(LINE_BUCKET_PREFIX.length, -'.json'.length);
113
- if (bytes && spaceId)
114
- await store.put(`${LINE_PREFIX}${spaceId}`, bytes);
115
- }
116
- for (const key of await store.list(LINE_PREFIX)) {
117
- const bytes = await store.get(key);
118
- const line = bytes ? JSON.parse(utf8Decode(bytes)) : null;
119
- if (isRecord(line) &&
120
- typeof line.since === 'number' &&
121
- typeof line.from === 'number' &&
122
- Array.isArray(line.reasons))
123
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes, shape checked
124
- lines.set(key.slice(LINE_PREFIX.length), line);
105
+ for (const [spaceId, text] of await kept.struck.load()) {
106
+ const at = Number(text);
107
+ if (Number.isFinite(at))
108
+ struck.set(spaceId, at);
125
109
  }
126
110
  }
127
- const lineGrace = (limits?.graceDays ?? 0) * DAY;
128
- /** Each account over its room, there and in the bucket */
129
- const ROOM_PREFIX = 'room:';
130
- const ROOM_BUCKET_PREFIX = 'host/rooms/';
131
- const rooms = new Map();
132
- if (limits) {
133
- if (config.mirror && (await store.list(ROOM_PREFIX)).length === 0)
134
- for (const key of await config.mirror.list(ROOM_BUCKET_PREFIX)) {
135
- const bytes = await config.mirror.get(key);
136
- // Named in the bucket by the account DID, encoded: the store by the DID itself.
137
- if (bytes)
138
- await store.put(`${ROOM_PREFIX}${decodeURIComponent(key.slice(ROOM_BUCKET_PREFIX.length))}`, bytes);
139
- }
140
- for (const key of await store.list(ROOM_PREFIX)) {
141
- const bytes = await store.get(key);
142
- const room = bytes ? JSON.parse(utf8Decode(bytes)) : null;
143
- if (isRecord(room) && typeof room.full === 'number') {
144
- const held = {};
145
- if (isRecord(room.held))
146
- for (const [spaceId, since] of Object.entries(room.held))
147
- if (typeof since === 'number')
148
- held[spaceId] = since;
149
- rooms.set(key.slice(ROOM_PREFIX.length), {
150
- full: room.full,
151
- ...(typeof room.fits === 'number' ? { fits: room.fits } : {}),
152
- ...(room.refused === true ? { refused: true } : {}),
153
- ...(Object.keys(held).length ? { held } : {}),
154
- });
155
- }
111
+ async function writeLine(spaceId, line) {
112
+ if (line) {
113
+ lines.set(spaceId, line);
114
+ await kept.lines.put(spaceId, JSON.stringify(line));
115
+ }
116
+ else {
117
+ lines.delete(spaceId);
118
+ await kept.lines.delete(spaceId);
156
119
  }
157
120
  }
158
- const writeRoom = async (account, room) => {
159
- if (JSON.stringify(room) === JSON.stringify(rooms.get(account) ?? null))
121
+ async function writeRoom(account, room) {
122
+ if (sameJson(room, rooms.get(account) ?? null))
160
123
  return;
161
- const key = encodeURIComponent(account);
162
124
  if (room) {
163
125
  rooms.set(account, room);
164
- const bytes = utf8Encode(JSON.stringify(room));
165
- await store.put(`${ROOM_PREFIX}${account}`, bytes);
166
- await config.mirror?.put(`${ROOM_BUCKET_PREFIX}${key}`, bytes);
126
+ await kept.rooms.put(account, JSON.stringify(room));
167
127
  }
168
128
  else {
169
129
  rooms.delete(account);
170
- await store.delete(`${ROOM_PREFIX}${account}`);
171
- await config.mirror?.delete(`${ROOM_BUCKET_PREFIX}${key}`);
130
+ await kept.rooms.delete(account);
172
131
  }
173
- };
174
- /** The account a subscription carries for */
132
+ }
133
+ async function strike(spaceId, at) {
134
+ struck.set(spaceId, at);
135
+ await kept.struck.put(spaceId, String(at));
136
+ }
137
+ async function unstrike(spaceId) {
138
+ if (!struck.delete(spaceId))
139
+ return;
140
+ await kept.struck.delete(spaceId);
141
+ }
175
142
  const accountOf = (subscription) => subscription.carry?.account;
143
+ const roomOf = (subscription) => rooms.get(accountOf(subscription) ?? '');
176
144
  /** Whether a subscription's account is past its notice of being over its room */
177
145
  const outOfRoom = (subscription) => {
178
- const room = rooms.get(accountOf(subscription) ?? '');
146
+ const room = roomOf(subscription);
179
147
  return room !== undefined && room.full + lineGrace <= now();
180
148
  };
181
- /** Each carry space's spaces held back for want of its room, and all of them together */
149
+ /** Each carry space's spaces held back for want of its room */
182
150
  const holds = new Map();
183
151
  /** Those whose copy the host still keeps: held back less than `keepDays` for some account */
184
152
  const roomHeld = new Set();
185
153
  /** Held back longer, their copy already deleted: not deleted again until the host restarts */
186
154
  const dropped = new Set();
187
155
  /** Spaces members' subscriptions no longer carry, being over the line with nobody paying */
188
- const skipped = new Set([...lines].flatMap(([spaceId, line]) => (line.stopped !== undefined ? [spaceId] : [])));
189
- /**
190
- * Spaces members' subscriptions once stopped carrying, and when: one that
191
- * comes back over the line after the host forgot it gets no new grace.
192
- */
193
- const struck = new Map();
194
- const strike = async (spaceId, at) => {
195
- struck.set(spaceId, at);
196
- const bytes = utf8Encode(String(at));
197
- await store.put(`${STRUCK_PREFIX}${spaceId}`, bytes);
198
- await config.mirror?.put(`${STRUCK_BUCKET_PREFIX}${spaceId}`, bytes);
199
- };
200
- const unstrike = async (spaceId) => {
201
- if (!struck.delete(spaceId))
202
- return;
203
- await store.delete(`${STRUCK_PREFIX}${spaceId}`);
204
- await config.mirror?.delete(`${STRUCK_BUCKET_PREFIX}${spaceId}`);
205
- };
206
- if (limits) {
207
- if (config.mirror && (await store.list(STRUCK_PREFIX)).length === 0)
208
- for (const key of await config.mirror.list(STRUCK_BUCKET_PREFIX)) {
209
- const bytes = await config.mirror.get(key);
210
- if (bytes)
211
- await store.put(`${STRUCK_PREFIX}${key.slice(STRUCK_BUCKET_PREFIX.length)}`, bytes);
212
- }
213
- for (const key of await store.list(STRUCK_PREFIX)) {
214
- const bytes = await store.get(key);
215
- const at = bytes ? Number(utf8Decode(bytes)) : NaN;
216
- if (Number.isFinite(at))
217
- struck.set(key.slice(STRUCK_PREFIX.length), at);
218
- }
219
- }
156
+ const skipped = new Set([...lines].filter(([, line]) => line.stopped !== undefined).map(([id]) => id));
220
157
  /**
221
158
  * A version reached a carried space: its account wrote there, unless it is
222
- * the host's own. Counted by when it arrived, whatever its writer says of
223
- * when it was written, except while the host first catches up on a space:
224
- * then what says it is older than the window is its history.
159
+ * the host's own. Counted by when it arrived, except while the host first
160
+ * catches up on a space: then what is older than the window is history.
225
161
  */
226
162
  const counted = (spaceId, version) => {
227
163
  if (!meter || !limits)
@@ -229,20 +165,22 @@ export async function createHostNode(config) {
229
165
  const account = version.proof?.payload.iss ?? version.author;
230
166
  const written = Date.parse(version.createdAt) / 1000;
231
167
  const old = Number.isFinite(written) && written < now() - limits.space.windowDays * DAY;
232
- void (async () => {
233
- const first = await meter.firstSeen(spaceId);
234
- if (old && now() - first < CATCH_UP_SECONDS)
168
+ const count = async () => {
169
+ const catchingUp = now() - (await meter.firstSeen(spaceId)) < CATCH_UP_SECONDS;
170
+ if (old && catchingUp)
235
171
  return;
236
172
  if (!(await config.ignore?.(account)))
237
173
  await meter.wrote(spaceId, account);
238
- })().catch(() => { });
174
+ };
175
+ void count().catch(() => { });
239
176
  };
240
177
  /** Whether anyone but the account holds a role in its carry space: then it is a shared space */
241
178
  const sharedCarry = async (carrySpace, account) => {
242
179
  const entry = core.carried.get(carrySpace);
243
180
  if (!entry)
244
181
  return false;
245
- return (await entry.runtime.access()).members.some((member) => member.did !== account && (member.founder || member.roles.length > 0));
182
+ const { members } = await entry.runtime.access();
183
+ return members.some((member) => member.did !== account && (member.founder || member.roles.length > 0));
246
184
  };
247
185
  /** Stops a carry space being any subscription's */
248
186
  const letCarryGo = async (carrySpace) => {
@@ -261,18 +199,8 @@ export async function createHostNode(config) {
261
199
  return undefined;
262
200
  return (await entry.runtime.access()).members.map((member) => member.did);
263
201
  };
264
- // A fresh disk: the subscriptions come back from the bucket, and the spaces with them.
265
- if (config.mirror && (await store.list(SUBSCRIPTION_PREFIX)).length === 0) {
266
- for (const key of await config.mirror.list(BUCKET_PREFIX)) {
267
- const bytes = await config.mirror.get(key);
268
- if (!bytes)
269
- continue;
270
- const subscription = JSON.parse(utf8Decode(bytes));
271
- if (isRecord(subscription) && typeof subscription.id === 'string')
272
- await store.put(`${SUBSCRIPTION_PREFIX}${subscription.id}`, bytes);
273
- }
274
- }
275
- /** The subscription an account's carry space belongs to, when it wrote `carry:closed` */
202
+ await kept.subscriptions.restore();
203
+ /** The account's carry space being let go, after it wrote `carry:closed` */
276
204
  let closing = Promise.resolve();
277
205
  /** Whether the carry core below is made, so its events may look at it */
278
206
  let opened = false;
@@ -298,79 +226,67 @@ export async function createHostNode(config) {
298
226
  }
299
227
  : {}),
300
228
  ...(config.spacesPerCarry !== undefined ? { most: config.spacesPerCarry } : {}),
301
- ...(config.mirror
302
- ? {
303
- mirror: config.mirror,
304
- // One stopped for being over the line is kept a while, so a fund brings it back as it was.
305
- onRelease: async (spaceId) => {
306
- if (!skipped.has(spaceId) && !roomHeld.has(spaceId))
307
- await deleteMirrored(config.mirror, spaceId);
308
- },
309
- }
310
- : {}),
229
+ ...(config.mirror ? { mirror: config.mirror, onRelease: releaseMirrored(config.mirror) } : {}),
230
+ // The account stopped using this host: forget its carry space; the subscription stays paid.
311
231
  onClosed: (carrySpace) => {
312
- // The account stopped using this host: forget its carry space; the subscription stays paid.
313
- closing = closing.then(async () => {
314
- for (const subscription of await list()) {
315
- if (subscription.carry?.space !== carrySpace)
316
- continue;
317
- const { carry: _gone, ...rest } = subscription;
318
- await write(rest);
319
- }
320
- await core.removeCarry(carrySpace);
321
- });
232
+ closing = closing.then(() => letCarryGo(carrySpace));
322
233
  },
323
234
  });
324
235
  opened = true;
325
- // Carry again what was carried before a restart — the store is only a cache of the spaces, but the list is ours.
326
- for (const subscription of await list()) {
327
- if (subscription.pass !== undefined && state(subscription) !== 'lapsed') {
328
- await core.addPass(subscription.pass).catch((error) => {
329
- console.error(`Could not carry for subscription ${subscription.id}:`, error);
330
- });
331
- continue;
236
+ /** One stopped for being over the line is kept a while, so a fund brings it back as it was */
237
+ function releaseMirrored(mirror) {
238
+ return async (spaceId) => {
239
+ if (!skipped.has(spaceId) && !roomHeld.has(spaceId))
240
+ await deleteMirrored(mirror, spaceId);
241
+ };
242
+ }
243
+ await carryAgain();
244
+ /** Carries again what was carried before a restart: the store caches the spaces, but the list is ours */
245
+ async function carryAgain() {
246
+ for (const subscription of await list()) {
247
+ if (state(subscription) === 'lapsed')
248
+ continue;
249
+ const fail = (error) => console.error(`Could not carry for subscription ${subscription.id}:`, error);
250
+ if (subscription.pass !== undefined)
251
+ await core.addPass(subscription.pass).catch(fail);
252
+ // An account taken off the list since is not carried again.
253
+ else if (subscription.carry && allowed(subscription.carry.account))
254
+ await core.addCarry(subscription.carry.account, subscription.carry.invite).catch(fail);
332
255
  }
333
- // An account taken off the list since is not carried again.
334
- if (!subscription.carry || state(subscription) === 'lapsed' || !allowed(subscription.carry.account))
335
- continue;
336
- await core.addCarry(subscription.carry.account, subscription.carry.invite).catch((error) => {
337
- console.error(`Could not carry for subscription ${subscription.id}:`, error);
338
- });
339
256
  }
340
- /** Whether another subscription still carries this carry space */
341
- const carriedByOther = async (carrySpace, except) => (await list()).some((other) => other.id !== except && other.carry?.space === carrySpace);
342
- /** Whether a space's own subscription pays for it now, within the room its fund buys: the free line does not apply then */
257
+ /** Stops carrying a carry space, unless another subscription still carries it */
258
+ const releaseCarry = async (carrySpace, except) => {
259
+ const others = await list();
260
+ if (others.some((other) => other.id !== except && other.carry?.space === carrySpace))
261
+ return;
262
+ await core.removeCarry(carrySpace);
263
+ };
264
+ /** Whether a space's own subscription pays for it now: the free line does not apply then */
343
265
  const paysItself = async (spaceId) => {
344
- const own = await read(`${SPACE_SUBSCRIPTION_PREFIX}${spaceId}`);
266
+ const own = await read(spaceSubscription(spaceId));
345
267
  return !!own && state(own) === 'active';
346
268
  };
347
- /** The spaces the free line is about: those accounts' passes name, and those carried for themselves; never a carry space */
269
+ /** The spaces the free line is about: those accounts' passes name, and the carry spaces too */
348
270
  const metered = () => {
349
- const found = new Set();
350
- for (const entry of core.carries.values())
351
- for (const spaceId of entry.named)
352
- found.add(spaceId);
353
- // Carry spaces too: an account's own has one writer, and a shared space handed over as one is held to the line.
354
- for (const spaceId of core.carried.keys())
355
- found.add(spaceId);
356
- return found;
271
+ const named = [...core.carries.values()].flatMap((entry) => [...entry.named]);
272
+ // An account's own carry space has one writer; a shared space handed over as one is held to the line.
273
+ return new Set([...named, ...core.carried.keys()]);
357
274
  };
275
+ const measure = async (spaceId) => (await config.measure?.(spaceId).catch(() => 0)) ?? 0;
358
276
  /** Where one space stands now: its counts, its size, and the line it is held to */
359
277
  async function standing(spaceId, before, own) {
360
278
  if (!limits || !meter)
361
279
  return null;
362
280
  const t = now();
363
- const window = limits.space.windowDays * DAY;
364
- const keep = (limits.keepDays ?? 0) * DAY;
365
- // Members stopped carrying it, so nothing it does reaches here: it stays as it was, and is forgotten after a while.
281
+ // Members stopped carrying it, so nothing it does reaches here: it stays, and is forgotten after a while.
366
282
  if (before?.stopped !== undefined && !own)
367
- return before.stopped + keep <= t ? null : before;
368
- // Not carried now, so who is a member can't be read: judged again once it is, as it was till then.
283
+ return before.stopped + keepSeconds <= t ? null : before;
284
+ // Not carried now, so its members can't be read: it stays as it was until it is.
369
285
  const members = await membersOf(spaceId);
370
286
  if (!members)
371
287
  return before ?? null;
372
288
  const counts = await meter.usage(spaceId, members);
373
- const bytes = (await config.measure?.(spaceId).catch(() => 0)) ?? 0;
289
+ const bytes = await measure(spaceId);
374
290
  const shared = counts.writers >= 2;
375
291
  // One writer is never a community, whatever the limit says.
376
292
  const most = Math.max(limits.space.writers, 1);
@@ -381,18 +297,17 @@ export async function createHostNode(config) {
381
297
  reasons.push('bytes');
382
298
  const wellUnder = counts.writers <= most * UNDER && (!shared || bytes <= limits.space.bytes * UNDER);
383
299
  let line = before ?? null;
384
- // Its grace was spent once, by a stop or by running out while it was under: over again,
385
- // it gets only a short notice, never a whole grace again.
386
- const notice = t - lineGrace + NOTICE_DAYS * DAY;
387
- const spent = before ? before.calm === true && before.from + lineGrace <= t : struck.has(spaceId);
388
300
  if (reasons.length > 0) {
301
+ // A grace spent once, by a stop or by running out while under, buys only a short notice next time.
302
+ const notice = t - lineGrace + NOTICE_DAYS * DAY;
303
+ const spent = before ? before.calm === true && before.from + lineGrace <= t : struck.has(spaceId);
389
304
  const from = spent ? Math.max(before?.from ?? notice, notice) : (before?.from ?? t);
390
305
  line = { since: before?.since ?? t, from, reasons, lastOver: t };
391
306
  }
392
307
  else if (line && wellUnder) {
393
308
  const under = line.under ?? t;
394
309
  // Free again only after a whole window well under, so a busy week does not flip it.
395
- if (t - under >= window)
310
+ if (t - under >= limits.space.windowDays * DAY)
396
311
  return null;
397
312
  line = { ...line, calm: true, under };
398
313
  }
@@ -402,100 +317,86 @@ export async function createHostNode(config) {
402
317
  }
403
318
  if (!line)
404
319
  return null;
405
- // While it pays for itself the line does not apply. Its grace is given once: paying for
406
- // a while does not buy it again, so once the fund stops, what is left of it is all it has.
320
+ // While it pays for itself the line does not apply, but its grace is not given again.
407
321
  if (own) {
408
322
  const { stopped: _paid, ...rest } = line;
409
323
  return rest;
410
324
  }
411
- // Stopped once its grace runs out if it is over then, or was at any look in the last days
412
- // before: one that went back under in time keeps its notice and is carried, but going
413
- // under for an hour as the date passes buys nothing.
325
+ // Stopped once its grace runs out if it is over then, or was in the last days before:
326
+ // going under for an hour as the date passes buys nothing.
414
327
  const until = line.from + lineGrace;
415
328
  const lately = (line.lastOver ?? 0) >= until - NOTICE_DAYS * DAY;
416
- return (reasons.length > 0 || lately) && until <= t && line.stopped === undefined
417
- ? { ...line, stopped: t }
418
- : line;
329
+ const stops = (reasons.length > 0 || lately) && until <= t && line.stopped === undefined;
330
+ return stops ? { ...line, stopped: t } : line;
419
331
  }
420
332
  /**
421
- * Each space against the free line, and members' subscriptions let go of
422
- * those past it. Run now and then. Says which spaces went over, stopped
423
- * being carried for members, or came back under.
333
+ * Holds each space to the free line, and lets members' subscriptions go of
334
+ * those past it. Run now and then. Says which spaces went over, stopped, or
335
+ * came back under.
424
336
  */
425
337
  async function evaluate() {
426
- const changed = {
427
- over: [],
428
- stopped: [],
429
- under: [],
430
- };
338
+ const changed = { over: [], stopped: [], under: [] };
431
339
  if (!limits || !meter)
432
340
  return changed;
433
341
  await meter.prune();
434
342
  const spaces = metered();
435
343
  for (const spaceId of new Set([...spaces, ...lines.keys()]))
436
344
  await evaluateOne(spaceId, spaces, changed).catch((error) => {
437
- // One space that can't be judged now is judged next time; the others go on.
345
+ // Judged again next time; the others go on.
438
346
  console.error(`Could not hold space ${spaceId} to the free line:`, error);
439
347
  });
440
348
  await core.refresh();
441
349
  return changed;
442
350
  }
443
351
  async function evaluateOne(spaceId, spaces, changed) {
444
- if (!meter)
352
+ if (!meter || !limits)
445
353
  return;
446
- {
447
- const before = lines.get(spaceId);
448
- const own = await paysItself(spaceId);
449
- // A space nobody names for a while keeps its line: the line goes only by a whole window
450
- // under, or after a stop, by `keepDays`; never because it was let go for an hour.
451
- const after = spaces.has(spaceId) || before ? await standing(spaceId, before, own) : null;
452
- // A stop long forgotten, and a window more under: its grace is its own again.
453
- const struckAt = struck.get(spaceId);
454
- if (!before && !after && struckAt !== undefined && limits) {
455
- const forgotten = ((limits.keepDays ?? 0) + limits.space.windowDays) * DAY;
456
- if (struckAt + forgotten <= now())
457
- await unstrike(spaceId);
458
- }
459
- if (JSON.stringify(after) !== JSON.stringify(before ?? null))
460
- await writeLine(spaceId, after);
461
- if (after && !before)
462
- changed.over.push(spaceId);
463
- if (after?.stopped !== undefined && before?.stopped === undefined) {
464
- changed.stopped.push(spaceId);
465
- await strike(spaceId, after.stopped);
466
- // A shared space handed over as someone's carry space: it stops being theirs to carry.
467
- // Skipped first, so letting it go keeps the bucket's copy.
468
- skipped.add(spaceId);
469
- if (core.carries.has(spaceId))
470
- await letCarryGo(spaceId);
471
- }
472
- // A carry space someone else came to hold a role in is let go: it is a shared space.
473
- const account = core.carries.get(spaceId)?.account;
474
- if (account && (await sharedCarry(spaceId, account)))
354
+ const before = lines.get(spaceId);
355
+ const own = await paysItself(spaceId);
356
+ // A space nobody names for a while keeps its line, never dropped because it was let go for an hour.
357
+ const after = spaces.has(spaceId) || before ? await standing(spaceId, before, own) : null;
358
+ // A stop long forgotten, and a window more under: its grace is its own again.
359
+ const struckAt = struck.get(spaceId);
360
+ const forgotten = ((limits.keepDays ?? 0) + limits.space.windowDays) * DAY;
361
+ if (!before && !after && struckAt !== undefined && struckAt + forgotten <= now())
362
+ await unstrike(spaceId);
363
+ if (!sameJson(after, before ?? null))
364
+ await writeLine(spaceId, after);
365
+ if (after && !before)
366
+ changed.over.push(spaceId);
367
+ if (after?.stopped !== undefined && before?.stopped === undefined) {
368
+ changed.stopped.push(spaceId);
369
+ await strike(spaceId, after.stopped);
370
+ // Skipped first, so letting a carry space go keeps the bucket's copy.
371
+ skipped.add(spaceId);
372
+ if (core.carries.has(spaceId))
475
373
  await letCarryGo(spaceId);
476
- if (before && !after)
477
- changed.under.push(spaceId);
478
- // Free again after a whole window well under: its grace is its own again.
479
- if (before && !after && before.stopped === undefined)
480
- await unstrike(spaceId);
481
- const skip = after?.stopped !== undefined && !own;
482
- if (skip)
483
- skipped.add(spaceId);
484
- else
485
- skipped.delete(spaceId);
486
- // Forgotten after it was kept a while: what the host held of it goes too, unless something carries it.
487
- if (before?.stopped !== undefined && !after) {
488
- await meter.forget(spaceId);
489
- if (config.mirror && !core.carried.has(spaceId))
490
- await deleteMirrored(config.mirror, spaceId).catch(() => { });
491
- }
374
+ }
375
+ // A carry space someone else came to hold a role in is a shared space.
376
+ const account = core.carries.get(spaceId)?.account;
377
+ if (account && (await sharedCarry(spaceId, account)))
378
+ await letCarryGo(spaceId);
379
+ if (before && !after)
380
+ changed.under.push(spaceId);
381
+ if (before && !after && before.stopped === undefined)
382
+ await unstrike(spaceId);
383
+ if (after?.stopped !== undefined && !own)
384
+ skipped.add(spaceId);
385
+ else
386
+ skipped.delete(spaceId);
387
+ // Forgotten after it was kept a while: the host's copy goes too, unless something carries it.
388
+ if (before?.stopped !== undefined && !after) {
389
+ await meter.forget(spaceId);
390
+ if (config.mirror && !core.carried.has(spaceId))
391
+ await deleteMirrored(config.mirror, spaceId).catch(() => { });
492
392
  }
493
393
  }
494
394
  /** The spaces over the line among these, as a status says them */
495
395
  const overOf = (spaceIds) => [...spaceIds].flatMap((spaceId) => {
496
396
  const line = lines.get(spaceId);
497
- // One back under and carried is not over: it keeps its line, quietly, until free again.
498
- return line && (!line.calm || line.stopped !== undefined)
397
+ // One back under and carried keeps its line quietly, until free again.
398
+ const over = line && (!line.calm || line.stopped !== undefined);
399
+ return over
499
400
  ? [{ space: spaceId, since: line.since, until: line.from + lineGrace, reasons: line.reasons }]
500
401
  : [];
501
402
  });
@@ -505,6 +406,37 @@ export async function createHostNode(config) {
505
406
  throw new Error('A subscription is named by its key, or by the space it is for');
506
407
  return (await read(id)) ?? write({ id, paidUntil: 0, since: now() });
507
408
  };
409
+ /** Which spaces are held back now, in a form to compare */
410
+ const holdsSignature = () => [...holds]
411
+ .map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
412
+ .sort()
413
+ .join(' ');
414
+ /** The spaces some account holds back for less than `keepDays`, whose copy the host keeps */
415
+ function recountRoomHeld(t) {
416
+ roomHeld.clear();
417
+ for (const room of rooms.values())
418
+ for (const [spaceId, since] of Object.entries(room.held ?? {}))
419
+ if (since + keepSeconds > t)
420
+ roomHeld.add(spaceId);
421
+ }
422
+ /** Held back longer than `keepDays` for every account that holds it: the host's copy goes, as for a stop */
423
+ async function dropLongHeld() {
424
+ for (const held of holds.values())
425
+ for (const spaceId of held) {
426
+ if (roomHeld.has(spaceId) ||
427
+ dropped.has(spaceId) ||
428
+ skipped.has(spaceId) ||
429
+ core.carried.has(spaceId))
430
+ continue;
431
+ dropped.add(spaceId);
432
+ if (config.mirror)
433
+ await deleteMirrored(config.mirror, spaceId).catch(() => { });
434
+ }
435
+ // Carried again: should it be held that long again, its copy goes again.
436
+ for (const spaceId of dropped)
437
+ if (core.carried.has(spaceId))
438
+ dropped.delete(spaceId);
439
+ }
508
440
  return Object.freeze({
509
441
  did: core.did,
510
442
  /** The short links its accounts keep, under a mailbox topic: what its relay answers besides drops */
@@ -519,9 +451,8 @@ export async function createHostNode(config) {
519
451
  /** The limits it keeps to, or null when it has none */
520
452
  limits,
521
453
  /**
522
- * Whether a subscription's spaces take more than its room, as its host
523
- * measured. The notice runs from when they first did, and goes only after
524
- * a whole window of fitting, so a dip under for an hour starts nothing anew.
454
+ * Whether a subscription's spaces take more than its room. The notice runs
455
+ * from when they first did, and goes only after a whole window of fitting.
525
456
  */
526
457
  async setFull(id, over) {
527
458
  const subscription = await read(id);
@@ -530,27 +461,24 @@ export async function createHostNode(config) {
530
461
  return;
531
462
  const room = rooms.get(account);
532
463
  const t = now();
533
- if (over)
464
+ if (over) {
534
465
  await writeRoom(account, {
535
466
  full: room?.full ?? t,
536
467
  ...(room?.refused ? { refused: true } : {}),
537
468
  ...(room?.held ? { held: room.held } : {}),
538
469
  });
539
- else if (room) {
540
- const fits = room.fits ?? t;
541
- // Fitting a whole window: the notice goes, though a refusal stays until the host's owner says.
542
- if (t - fits >= limits.space.windowDays * DAY && !room.refused)
543
- await writeRoom(account, null);
544
- else
545
- await writeRoom(account, { ...room, fits });
470
+ return;
546
471
  }
472
+ if (!room)
473
+ return;
474
+ const fits = room.fits ?? t;
475
+ // A refusal stays until the host's owner says.
476
+ const fitWholeWindow = t - fits >= limits.space.windowDays * DAY && !room.refused;
477
+ await writeRoom(account, fitWholeWindow ? null : { ...room, fits });
547
478
  },
548
479
  /** Whether a subscription is past its notice of being over its room */
549
480
  outOfRoom,
550
- /**
551
- * Holds back these solo spaces of a subscription's for want of room, and
552
- * no others of its: the host keeps them, unserved, until they fit.
553
- */
481
+ /** Holds back these solo spaces of a subscription's for want of room, and no others: kept, unserved, until they fit */
554
482
  async holdForRoom(id, spaceIds) {
555
483
  const subscription = await read(id);
556
484
  const carrySpace = subscription?.carry?.space;
@@ -564,47 +492,17 @@ export async function createHostNode(config) {
564
492
  const held = Object.fromEntries(spaceIds.map((spaceId) => [spaceId, before[spaceId] ?? t]));
565
493
  await writeRoom(account, Object.keys(held).length ? { ...rest, held } : rest);
566
494
  }
567
- const before = [...holds]
568
- .map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
569
- .sort()
570
- .join(' ');
495
+ const before = holdsSignature();
571
496
  if (spaceIds.length)
572
497
  holds.set(carrySpace, new Set(spaceIds));
573
498
  else
574
499
  holds.delete(carrySpace);
575
- roomHeld.clear();
576
- const keep = (limits?.keepDays ?? 0) * DAY;
577
- for (const one of rooms.values())
578
- for (const [spaceId, since] of Object.entries(one.held ?? {}))
579
- if (since + keep > t)
580
- roomHeld.add(spaceId);
581
- // Held back longer than that for every account that holds it: the host's copy goes, as for a stop.
582
- for (const held of holds.values())
583
- for (const spaceId of held) {
584
- if (roomHeld.has(spaceId) ||
585
- dropped.has(spaceId) ||
586
- skipped.has(spaceId) ||
587
- core.carried.has(spaceId))
588
- continue;
589
- dropped.add(spaceId);
590
- if (config.mirror)
591
- await deleteMirrored(config.mirror, spaceId).catch(() => { });
592
- }
593
- // Carried again, by room made or someone else: should it be held that long again, its copy goes again.
594
- for (const spaceId of dropped)
595
- if (core.carried.has(spaceId))
596
- dropped.delete(spaceId);
597
- const after = [...holds]
598
- .map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
599
- .sort()
600
- .join(' ');
601
- if (after !== before)
500
+ recountRoomHeld(t);
501
+ await dropLongHeld();
502
+ if (holdsSignature() !== before)
602
503
  await core.refresh();
603
504
  },
604
- /**
605
- * Stops carrying a subscription's carry space, which is over its room by
606
- * itself past its notice, and turns it away until the host's owner says.
607
- */
505
+ /** Stops carrying a carry space over its room by itself past its notice, until the host's owner says */
608
506
  async refuse(id) {
609
507
  const subscription = await read(id);
610
508
  if (!subscription?.carry)
@@ -612,8 +510,7 @@ export async function createHostNode(config) {
612
510
  const { carry, ...rest } = subscription;
613
511
  await writeRoom(carry.account, { ...(rooms.get(carry.account) ?? { full: now() }), refused: true });
614
512
  await write(rest);
615
- if (!(await carriedByOther(carry.space, id)))
616
- await core.removeCarry(carry.space);
513
+ await releaseCarry(carry.space, id);
617
514
  },
618
515
  async seen(id) {
619
516
  const subscription = await read(id);
@@ -645,32 +542,30 @@ export async function createHostNode(config) {
645
542
  async usage(spaceId) {
646
543
  if (!meter)
647
544
  return null;
648
- const counts = await meter.usage(spaceId, await membersOf(spaceId));
649
- const bytes = core.carried.has(spaceId) ? ((await config.measure?.(spaceId).catch(() => 0)) ?? 0) : 0;
650
- return { writers: counts.writers, readers: counts.readers, bytes, windowDays: counts.windowDays };
545
+ const { writers, readers, windowDays } = await meter.usage(spaceId, await membersOf(spaceId));
546
+ const bytes = core.carried.has(spaceId) ? await measure(spaceId) : 0;
547
+ return { writers, readers, bytes, windowDays };
651
548
  },
652
549
  /** When a subscription's notice of being over its room runs out, or null when it is not over */
653
550
  fullUntil: (subscription) => {
654
- const room = rooms.get(accountOf(subscription) ?? '');
551
+ const room = roomOf(subscription);
655
552
  return room ? { since: room.full, until: room.full + lineGrace } : null;
656
553
  },
657
- /** Whether members' subscriptions hold a space back, being past the free line: no member's room counts it */
554
+ /** Whether members' subscriptions hold a space back, being past the free line */
658
555
  heldBack: (spaceId) => skipped.has(spaceId),
659
556
  /** Whether a space pays for itself now, so no account's room counts it */
660
557
  paysItself,
661
- /** Moves a subscription's paid-until date — the one thing payments do */
558
+ /** Moves a subscription's paid-until date: the one thing payments do */
662
559
  async extend(id, until, customer) {
663
560
  const subscription = await subscribe(id);
664
- const extended = {
561
+ const extended = await write({
665
562
  ...subscription,
666
563
  paidUntil: Math.max(subscription.paidUntil, until),
667
564
  ...(customer ? { customer } : {}),
668
- };
669
- await write(extended);
565
+ });
670
566
  // Paid again in time: carry again what the grace period had kept.
671
- if (extended.carry && !core.carries.has(extended.carry.space)) {
567
+ if (extended.carry && !core.carries.has(extended.carry.space))
672
568
  await core.addCarry(extended.carry.account, extended.carry.invite);
673
- }
674
569
  if (extended.pass !== undefined)
675
570
  await core.addPass(extended.pass).catch(() => { });
676
571
  return extended;
@@ -680,9 +575,7 @@ export async function createHostNode(config) {
680
575
  const subscription = await subscribe(id);
681
576
  if (subscription.paidUntil === until)
682
577
  return subscription;
683
- const set = { ...subscription, paidUntil: until };
684
- await write(set);
685
- // Money came back in time: carry again what the grace period had kept.
578
+ const set = await write({ ...subscription, paidUntil: until });
686
579
  if (set.pass !== undefined && state(set) !== 'lapsed')
687
580
  await core.addPass(set.pass).catch(() => { });
688
581
  return set;
@@ -705,37 +598,19 @@ export async function createHostNode(config) {
705
598
  throw new Error('An account is named by its DID');
706
599
  if (!allowed(account))
707
600
  throw new NotAllowedError();
708
- // Kept by the account, so a new subscription key brings nothing back; paying does, as a balance has no room.
709
- const room = rooms.get(account);
710
- if (room?.refused && plan(subscription) !== 'paid')
711
- throw new Error('This host stopped carrying for this account: its carry space was over its room');
712
- if (room?.refused) {
713
- const { refused: _lifted, ...rest } = room;
714
- await writeRoom(account, rest);
715
- }
716
- // A shared space stopped for being over the line comes back only through its own fund.
601
+ await liftRefusal(account, plan(subscription));
717
602
  const asked = parseSpaceInvite(invite).space.id;
718
- if (lines.get(asked)?.stopped !== undefined && !(await paysItself(asked)))
719
- throw new Error('That space is over this host’s free line: only its own fund carries it');
720
- // One carried, or named, for others already is theirs to carry: taking it as a carry space
721
- // would take it from them, and letting it go again would drop the host's copy.
722
- if (limits && !core.carries.has(asked)) {
723
- const others = core.carried.has(asked) || [...core.carries.values()].some((entry) => entry.named.has(asked));
724
- if (others)
725
- throw new Error('That is a space others are members of, not a carry space');
726
- }
603
+ await checkCarrySpace(asked);
727
604
  const space = await core.addCarry(account, invite);
728
- // A carry space is the account's alone: anyone else holding a role there makes it a shared
729
- // space, which members' own passes carry, each told of the line.
605
+ // Anyone else holding a role there makes it a shared space, which members' own passes carry.
730
606
  if (limits && (await sharedCarry(space, account))) {
731
- if (!(await carriedByOther(space, id)))
732
- await core.removeCarry(space);
607
+ await releaseCarry(space, id);
733
608
  throw new Error('That is a space others are members of, not a carry space');
734
609
  }
735
610
  const before = subscription.carry;
736
611
  const attached = await write({ ...subscription, carry: { account, space, invite } });
737
- if (before && before.space !== space && !(await carriedByOther(before.space, id)))
738
- await core.removeCarry(before.space);
612
+ if (before && before.space !== space)
613
+ await releaseCarry(before.space, id);
739
614
  return attached;
740
615
  },
741
616
  /** Stops carrying for a subscription; the subscription stays */
@@ -745,8 +620,7 @@ export async function createHostNode(config) {
745
620
  return;
746
621
  const { carry, ...rest } = subscription;
747
622
  await write(rest);
748
- if (!(await carriedByOther(carry.space, id)))
749
- await core.removeCarry(carry.space);
623
+ await releaseCarry(carry.space, id);
750
624
  },
751
625
  /**
752
626
  * Carries a space for its own subscription, from a pass. A later pass (the
@@ -773,25 +647,19 @@ export async function createHostNode(config) {
773
647
  },
774
648
  /** Drops what lapsed past its grace period, and free subscriptions idle too long. Run now and then. */
775
649
  async sweep() {
776
- const dropped = [];
777
- const idle = limits?.idleDays !== undefined ? limits.idleDays * DAY : null;
778
- for (let subscription of await list()) {
779
- // One from before idle subscriptions were dropped counts from now, not from when it was made.
780
- if (idle !== null && plan(subscription) === 'free' && subscription.seen === undefined)
781
- subscription = await write({ ...subscription, seen: now() });
782
- const idled = idle !== null && plan(subscription) === 'free' && (subscription.seen ?? now()) + idle < now();
783
- if (state(subscription) !== 'lapsed' && !idled)
650
+ const gone = [];
651
+ for (const subscription of await list()) {
652
+ if (!(await expired(subscription)))
784
653
  continue;
785
- await forget(subscription.id);
654
+ await kept.subscriptions.delete(subscription.id);
786
655
  const spaceId = spaceIdOf(subscription.id);
787
656
  if (spaceId)
788
657
  await core.removePass(spaceId);
789
- if (subscription.carry && !(await carriedByOther(subscription.carry.space, subscription.id))) {
790
- await core.removeCarry(subscription.carry.space);
791
- }
792
- dropped.push(subscription.id);
658
+ if (subscription.carry)
659
+ await releaseCarry(subscription.carry.space, subscription.id);
660
+ gone.push(subscription.id);
793
661
  }
794
- return dropped;
662
+ return gone;
795
663
  },
796
664
  spaces: core.spaces,
797
665
  /** Reads every account's passes again, taking a space that waited for room: after what `full` says changed */
@@ -831,5 +699,120 @@ export async function createHostNode(config) {
831
699
  events.clear();
832
700
  },
833
701
  });
702
+ /** A refused account comes back only by paying, since a balance has no room; a new key brings nothing back */
703
+ async function liftRefusal(account, room) {
704
+ const refused = rooms.get(account);
705
+ if (!refused?.refused)
706
+ return;
707
+ if (room !== 'paid')
708
+ throw new Error('This host stopped carrying for this account: its carry space was over its room');
709
+ const { refused: _lifted, ...rest } = refused;
710
+ await writeRoom(account, rest);
711
+ }
712
+ /** Refuses as a carry space one that is others' to carry */
713
+ async function checkCarrySpace(spaceId) {
714
+ // Stopped for being over the line, it comes back only through its own fund.
715
+ if (lines.get(spaceId)?.stopped !== undefined && !(await paysItself(spaceId)))
716
+ throw new Error('That space is over this host’s free line: only its own fund carries it');
717
+ if (!limits || core.carries.has(spaceId))
718
+ return;
719
+ // Taking it would take it from them, and letting it go again would drop the host's copy.
720
+ const others = core.carried.has(spaceId) || [...core.carries.values()].some((entry) => entry.named.has(spaceId));
721
+ if (others)
722
+ throw new Error('That is a space others are members of, not a carry space');
723
+ }
724
+ /** Lapsed past its grace, or a free subscription idle too long */
725
+ async function expired(subscription) {
726
+ const idle = limits?.idleDays !== undefined ? limits.idleDays * DAY : null;
727
+ if (idle === null || plan(subscription) !== 'free')
728
+ return state(subscription) === 'lapsed';
729
+ let seen = subscription.seen;
730
+ // One from before idle subscriptions were dropped counts from now, not from when it was made.
731
+ if (seen === undefined) {
732
+ seen = now();
733
+ await write({ ...subscription, seen });
734
+ }
735
+ return state(subscription) === 'lapsed' || seen + idle < now();
736
+ }
737
+ }
738
+ function bucketNames(prefix, { encoded = false, suffix = '' } = {}) {
739
+ return {
740
+ prefix,
741
+ key: (name) => `${prefix}${encoded ? encodeURIComponent(name) : name}${suffix}`,
742
+ nameOf: (key) => {
743
+ const name = key.slice(prefix.length, key.length - suffix.length);
744
+ return (encoded ? decodeURIComponent(name) : name) || null;
745
+ },
746
+ };
747
+ }
748
+ /** Records kept as text in the host's store under `prefix`, and in the bucket too */
749
+ function shelf(store, mirror, prefix, bucket) {
750
+ const at = (name) => `${prefix}${name}`;
751
+ const names = async () => (await store.list(prefix)).map((key) => key.slice(prefix.length));
752
+ async function get(name) {
753
+ const bytes = await store.get(at(name));
754
+ return bytes ? utf8Decode(bytes) : null;
755
+ }
756
+ /** A fresh disk starts from the bucket's copy */
757
+ async function restore() {
758
+ if (!mirror || (await names()).length > 0)
759
+ return;
760
+ for (const key of await mirror.list(bucket.prefix)) {
761
+ const bytes = await mirror.get(key);
762
+ const name = bytes && bucket.nameOf(key, bytes);
763
+ if (bytes && name)
764
+ await store.put(at(name), bytes);
765
+ }
766
+ }
767
+ return {
768
+ get,
769
+ names,
770
+ restore,
771
+ async put(name, text) {
772
+ const bytes = utf8Encode(text);
773
+ await store.put(at(name), bytes);
774
+ await mirror?.put(bucket.key(name), bytes);
775
+ },
776
+ async delete(name) {
777
+ await store.delete(at(name));
778
+ await mirror?.delete(bucket.key(name));
779
+ },
780
+ /** Every record, after restoring from the bucket */
781
+ async load() {
782
+ await restore();
783
+ const entries = [];
784
+ for (const name of await names()) {
785
+ const text = await get(name);
786
+ if (text !== null)
787
+ entries.push([name, text]);
788
+ }
789
+ return entries;
790
+ },
791
+ };
792
+ }
793
+ function subscriptionIdIn(bytes) {
794
+ const subscription = JSON.parse(utf8Decode(bytes));
795
+ return isRecord(subscription) && typeof subscription.id === 'string' ? subscription.id : null;
796
+ }
797
+ function parseLine(value) {
798
+ const fits = isRecord(value) &&
799
+ typeof value.since === 'number' &&
800
+ typeof value.from === 'number' &&
801
+ Array.isArray(value.reasons);
802
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes, shape checked
803
+ return fits ? value : null;
804
+ }
805
+ function parseRoom(value) {
806
+ if (!isRecord(value) || typeof value.full !== 'number')
807
+ return null;
808
+ const given = isRecord(value.held) ? Object.entries(value.held) : [];
809
+ const held = Object.fromEntries(given.filter((entry) => typeof entry[1] === 'number'));
810
+ return {
811
+ full: value.full,
812
+ ...(typeof value.fits === 'number' ? { fits: value.fits } : {}),
813
+ ...(value.refused === true ? { refused: true } : {}),
814
+ ...(Object.keys(held).length ? { held } : {}),
815
+ };
834
816
  }
817
+ const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
835
818
  //# sourceMappingURL=host.js.map