@minca/core 0.2.0 → 0.4.0

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 (420) hide show
  1. package/README.md +2 -2
  2. package/dist/{session → hosting}/hosting.d.ts +2 -8
  3. package/dist/hosting/hosting.d.ts.map +1 -0
  4. package/dist/{session → hosting}/hosting.js +3 -3
  5. package/dist/hosting/hosting.js.map +1 -0
  6. package/dist/identity/abilities.d.ts +0 -2
  7. package/dist/identity/abilities.d.ts.map +1 -1
  8. package/dist/identity/abilities.js +2 -2
  9. package/dist/identity/abilities.js.map +1 -1
  10. package/dist/identity/account-vault.d.ts +0 -2
  11. package/dist/identity/account-vault.d.ts.map +1 -1
  12. package/dist/identity/account-vault.js +1 -1
  13. package/dist/identity/account-vault.js.map +1 -1
  14. package/dist/identity/crypto-p256.d.ts +1 -1
  15. package/dist/identity/crypto-p256.d.ts.map +1 -1
  16. package/dist/identity/ucan.d.ts +2 -1
  17. package/dist/identity/ucan.d.ts.map +1 -1
  18. package/dist/identity/ucan.js +1 -1
  19. package/dist/identity/ucan.js.map +1 -1
  20. package/dist/inbox/inbox.d.ts +0 -21
  21. package/dist/inbox/inbox.d.ts.map +1 -1
  22. package/dist/inbox/inbox.js +4 -4
  23. package/dist/inbox/inbox.js.map +1 -1
  24. package/dist/inbox/index.d.ts +4 -5
  25. package/dist/inbox/index.d.ts.map +1 -1
  26. package/dist/inbox/index.js +2 -3
  27. package/dist/inbox/index.js.map +1 -1
  28. package/dist/inbox/names.d.ts +0 -4
  29. package/dist/inbox/names.d.ts.map +1 -1
  30. package/dist/inbox/names.js +2 -2
  31. package/dist/inbox/names.js.map +1 -1
  32. package/dist/index.d.ts +50 -84
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +38 -60
  35. package/dist/index.js.map +1 -1
  36. package/dist/network/mesh.d.ts.map +1 -1
  37. package/dist/network/mesh.js +23 -3
  38. package/dist/network/mesh.js.map +1 -1
  39. package/dist/network/network-manager.d.ts +5 -0
  40. package/dist/network/network-manager.d.ts.map +1 -1
  41. package/dist/network/network-manager.js +3 -0
  42. package/dist/network/network-manager.js.map +1 -1
  43. package/dist/network/peer-auth.d.ts +17 -5
  44. package/dist/network/peer-auth.d.ts.map +1 -1
  45. package/dist/network/peer-auth.js +19 -9
  46. package/dist/network/peer-auth.js.map +1 -1
  47. package/dist/network/rendezvous.d.ts +2 -0
  48. package/dist/network/rendezvous.d.ts.map +1 -1
  49. package/dist/network/rendezvous.js +5 -0
  50. package/dist/network/rendezvous.js.map +1 -1
  51. package/dist/network/rtc-transport.d.ts.map +1 -1
  52. package/dist/network/rtc-transport.js +9 -3
  53. package/dist/network/rtc-transport.js.map +1 -1
  54. package/dist/network/transport.d.ts +12 -0
  55. package/dist/network/transport.d.ts.map +1 -1
  56. package/dist/network/ws-transport.d.ts +1 -1
  57. package/dist/network/ws-transport.d.ts.map +1 -1
  58. package/dist/network/ws-transport.js +14 -4
  59. package/dist/network/ws-transport.js.map +1 -1
  60. package/dist/node/carrier.d.ts +1 -33
  61. package/dist/node/carrier.d.ts.map +1 -1
  62. package/dist/node/carrier.js +38 -68
  63. package/dist/node/carrier.js.map +1 -1
  64. package/dist/node/index.d.ts +5 -16
  65. package/dist/node/index.d.ts.map +1 -1
  66. package/dist/node/index.js +2 -9
  67. package/dist/node/index.js.map +1 -1
  68. package/dist/node/node-carriers.d.ts.map +1 -1
  69. package/dist/node/node-carriers.js +21 -52
  70. package/dist/node/node-carriers.js.map +1 -1
  71. package/dist/node/node-context.d.ts +0 -2
  72. package/dist/node/node-context.d.ts.map +1 -1
  73. package/dist/node/node-context.js.map +1 -1
  74. package/dist/node/node-hosting.d.ts.map +1 -1
  75. package/dist/node/node-hosting.js +14 -5
  76. package/dist/node/node-hosting.js.map +1 -1
  77. package/dist/node/node-records.js +2 -2
  78. package/dist/node/node-records.js.map +1 -1
  79. package/dist/node/node.d.ts.map +1 -1
  80. package/dist/node/node.js +152 -36
  81. package/dist/node/node.js.map +1 -1
  82. package/dist/node/space-checks.d.ts +3 -1
  83. package/dist/node/space-checks.d.ts.map +1 -1
  84. package/dist/node/space-checks.js +7 -3
  85. package/dist/node/space-checks.js.map +1 -1
  86. package/dist/node/space-holding.d.ts +5 -0
  87. package/dist/node/space-holding.d.ts.map +1 -1
  88. package/dist/node/space-holding.js +90 -5
  89. package/dist/node/space-holding.js.map +1 -1
  90. package/dist/node/space-lines.d.ts +3 -0
  91. package/dist/node/space-lines.d.ts.map +1 -1
  92. package/dist/node/space-lines.js +13 -1
  93. package/dist/node/space-lines.js.map +1 -1
  94. package/dist/node/space-membership.js +4 -4
  95. package/dist/node/space-membership.js.map +1 -1
  96. package/dist/node/space-mirrors.d.ts +1 -1
  97. package/dist/node/space-mirrors.d.ts.map +1 -1
  98. package/dist/node/space-mirrors.js +17 -5
  99. package/dist/node/space-mirrors.js.map +1 -1
  100. package/dist/node/space-peers.d.ts +7 -2
  101. package/dist/node/space-peers.d.ts.map +1 -1
  102. package/dist/node/space-peers.js +44 -10
  103. package/dist/node/space-peers.js.map +1 -1
  104. package/dist/node/space-runtime.d.ts +7 -3
  105. package/dist/node/space-runtime.d.ts.map +1 -1
  106. package/dist/node/space-runtime.js +32 -10
  107. package/dist/node/space-runtime.js.map +1 -1
  108. package/dist/node/space-standing.d.ts +1 -1
  109. package/dist/node/space-standing.d.ts.map +1 -1
  110. package/dist/node/space-standing.js +23 -42
  111. package/dist/node/space-standing.js.map +1 -1
  112. package/dist/node/space-warrants.js +1 -1
  113. package/dist/node/space-warrants.js.map +1 -1
  114. package/dist/node/space-writing.d.ts.map +1 -1
  115. package/dist/node/space-writing.js +21 -32
  116. package/dist/node/space-writing.js.map +1 -1
  117. package/dist/node/stores.d.ts +9 -57
  118. package/dist/node/stores.d.ts.map +1 -1
  119. package/dist/node/stores.js +1 -72
  120. package/dist/node/stores.js.map +1 -1
  121. package/dist/node/types.d.ts +19 -8
  122. package/dist/node/types.d.ts.map +1 -1
  123. package/dist/node/writer-keys.d.ts +15 -14
  124. package/dist/node/writer-keys.d.ts.map +1 -1
  125. package/dist/node/writer-keys.js +3 -115
  126. package/dist/node/writer-keys.js.map +1 -1
  127. package/dist/privacy/direct.d.ts +0 -7
  128. package/dist/privacy/direct.d.ts.map +1 -1
  129. package/dist/privacy/direct.js +2 -2
  130. package/dist/privacy/direct.js.map +1 -1
  131. package/dist/query/filter.d.ts +0 -2
  132. package/dist/query/filter.d.ts.map +1 -1
  133. package/dist/query/filter.js +1 -1
  134. package/dist/query/filter.js.map +1 -1
  135. package/dist/query/types.d.ts +2 -6
  136. package/dist/query/types.d.ts.map +1 -1
  137. package/dist/query/types.js.map +1 -1
  138. package/dist/records/key.d.ts +18 -19
  139. package/dist/records/key.d.ts.map +1 -1
  140. package/dist/records/key.js +55 -43
  141. package/dist/records/key.js.map +1 -1
  142. package/dist/records/links.js +1 -1
  143. package/dist/records/rules.d.ts +1 -1
  144. package/dist/records/rules.d.ts.map +1 -1
  145. package/dist/records/rules.js +6 -4
  146. package/dist/records/rules.js.map +1 -1
  147. package/dist/schemas/direct.d.ts +70 -0
  148. package/dist/schemas/direct.d.ts.map +1 -0
  149. package/dist/schemas/direct.js +39 -0
  150. package/dist/schemas/direct.js.map +1 -0
  151. package/dist/schemas/index.d.ts +10 -73
  152. package/dist/schemas/index.d.ts.map +1 -1
  153. package/dist/schemas/index.js +8 -66
  154. package/dist/schemas/index.js.map +1 -1
  155. package/dist/space/account-registry.d.ts +0 -3
  156. package/dist/space/account-registry.d.ts.map +1 -1
  157. package/dist/space/account-registry.js.map +1 -1
  158. package/dist/space/own-automations.d.ts +51 -95
  159. package/dist/space/own-automations.d.ts.map +1 -1
  160. package/dist/space/own-automations.js +100 -295
  161. package/dist/space/own-automations.js.map +1 -1
  162. package/dist/space/pass.d.ts +8 -3
  163. package/dist/space/pass.d.ts.map +1 -1
  164. package/dist/space/pass.js +4 -3
  165. package/dist/space/pass.js.map +1 -1
  166. package/dist/space/space-manager.d.ts +1 -3
  167. package/dist/space/space-manager.d.ts.map +1 -1
  168. package/dist/space/space-manager.js +5 -7
  169. package/dist/space/space-manager.js.map +1 -1
  170. package/dist/storage/body-cache.d.ts +29 -0
  171. package/dist/storage/body-cache.d.ts.map +1 -0
  172. package/dist/storage/body-cache.js +64 -0
  173. package/dist/storage/body-cache.js.map +1 -0
  174. package/dist/storage/encrypted-adapter.d.ts +0 -5
  175. package/dist/storage/encrypted-adapter.d.ts.map +1 -1
  176. package/dist/storage/encrypted-adapter.js +1 -1
  177. package/dist/storage/encrypted-adapter.js.map +1 -1
  178. package/dist/storage/mirror.d.ts.map +1 -1
  179. package/dist/storage/mirror.js +35 -16
  180. package/dist/storage/mirror.js.map +1 -1
  181. package/dist/storage/segment.d.ts +6 -2
  182. package/dist/storage/segment.d.ts.map +1 -1
  183. package/dist/storage/segment.js +8 -4
  184. package/dist/storage/segment.js.map +1 -1
  185. package/dist/storage/shared-store.d.ts +32 -0
  186. package/dist/storage/shared-store.d.ts.map +1 -0
  187. package/dist/storage/{folder-reconcile.js → shared-store.js} +15 -15
  188. package/dist/storage/shared-store.js.map +1 -0
  189. package/dist/storage/storage-provider.d.ts +19 -9
  190. package/dist/storage/storage-provider.d.ts.map +1 -1
  191. package/dist/storage/storage-provider.js +155 -62
  192. package/dist/storage/storage-provider.js.map +1 -1
  193. package/dist/sync/sync-engine.d.ts +17 -4
  194. package/dist/sync/sync-engine.d.ts.map +1 -1
  195. package/dist/sync/sync-engine.js +298 -84
  196. package/dist/sync/sync-engine.js.map +1 -1
  197. package/dist/sync/sync-messages.d.ts +5 -1
  198. package/dist/sync/sync-messages.d.ts.map +1 -1
  199. package/dist/sync/sync-messages.js +1 -1
  200. package/dist/sync/sync-messages.js.map +1 -1
  201. package/dist/types.d.ts +19 -7
  202. package/dist/types.d.ts.map +1 -1
  203. package/dist/types.js +4 -1
  204. package/dist/types.js.map +1 -1
  205. package/dist/utils/index.d.ts +5 -5
  206. package/dist/utils/index.d.ts.map +1 -1
  207. package/dist/utils/index.js +5 -5
  208. package/dist/utils/index.js.map +1 -1
  209. package/dist/utils/step.d.ts +8 -0
  210. package/dist/utils/step.d.ts.map +1 -0
  211. package/dist/utils/step.js +15 -0
  212. package/dist/utils/step.js.map +1 -0
  213. package/docs/README.md +4 -3
  214. package/docs/actions.md +41 -2
  215. package/docs/agents.md +13 -10
  216. package/docs/automations.md +16 -48
  217. package/docs/blueprints-and-guides.md +2 -2
  218. package/docs/bots.md +3 -3
  219. package/docs/building-an-app.md +2 -2
  220. package/docs/collections.md +2 -4
  221. package/docs/computed.md +1 -2
  222. package/docs/node.md +108 -40
  223. package/docs/proposing-collections.md +3 -3
  224. package/docs/query-format.md +6 -5
  225. package/docs/records-and-queries.md +12 -13
  226. package/docs/sign-in.md +16 -16
  227. package/docs/spaces.md +0 -5
  228. package/docs/standard-library.md +6 -8
  229. package/docs/storage.md +114 -22
  230. package/docs/usernames.md +1 -1
  231. package/package.json +15 -25
  232. package/dist/identity/account-store.d.ts +0 -83
  233. package/dist/identity/account-store.d.ts.map +0 -1
  234. package/dist/identity/account-store.js +0 -234
  235. package/dist/identity/account-store.js.map +0 -1
  236. package/dist/identity/device-key.d.ts +0 -26
  237. package/dist/identity/device-key.d.ts.map +0 -1
  238. package/dist/identity/device-key.js +0 -48
  239. package/dist/identity/device-key.js.map +0 -1
  240. package/dist/identity/idb.d.ts +0 -19
  241. package/dist/identity/idb.d.ts.map +0 -1
  242. package/dist/identity/idb.js +0 -56
  243. package/dist/identity/idb.js.map +0 -1
  244. package/dist/node/actions.d.ts +0 -85
  245. package/dist/node/actions.d.ts.map +0 -1
  246. package/dist/node/actions.js +0 -921
  247. package/dist/node/actions.js.map +0 -1
  248. package/dist/node/host.d.ts +0 -187
  249. package/dist/node/host.d.ts.map +0 -1
  250. package/dist/node/host.js +0 -818
  251. package/dist/node/host.js.map +0 -1
  252. package/dist/node/usage.d.ts +0 -32
  253. package/dist/node/usage.d.ts.map +0 -1
  254. package/dist/node/usage.js +0 -217
  255. package/dist/node/usage.js.map +0 -1
  256. package/dist/node/watch-notifications.d.ts +0 -25
  257. package/dist/node/watch-notifications.d.ts.map +0 -1
  258. package/dist/node/watch-notifications.js +0 -166
  259. package/dist/node/watch-notifications.js.map +0 -1
  260. package/dist/node/worker.d.ts +0 -83
  261. package/dist/node/worker.d.ts.map +0 -1
  262. package/dist/node/worker.js +0 -305
  263. package/dist/node/worker.js.map +0 -1
  264. package/dist/node-worker.d.ts +0 -2
  265. package/dist/node-worker.d.ts.map +0 -1
  266. package/dist/node-worker.js +0 -16
  267. package/dist/node-worker.js.map +0 -1
  268. package/dist/schemas/automations.d.ts +0 -390
  269. package/dist/schemas/automations.d.ts.map +0 -1
  270. package/dist/schemas/automations.js +0 -619
  271. package/dist/schemas/automations.js.map +0 -1
  272. package/dist/schemas/blueprints.d.ts +0 -122
  273. package/dist/schemas/blueprints.d.ts.map +0 -1
  274. package/dist/schemas/blueprints.js +0 -337
  275. package/dist/schemas/blueprints.js.map +0 -1
  276. package/dist/schemas/choosing.d.ts +0 -19
  277. package/dist/schemas/choosing.d.ts.map +0 -1
  278. package/dist/schemas/choosing.js +0 -69
  279. package/dist/schemas/choosing.js.map +0 -1
  280. package/dist/schemas/connectors.d.ts +0 -202
  281. package/dist/schemas/connectors.d.ts.map +0 -1
  282. package/dist/schemas/connectors.js +0 -140
  283. package/dist/schemas/connectors.js.map +0 -1
  284. package/dist/schemas/cron.d.ts +0 -14
  285. package/dist/schemas/cron.d.ts.map +0 -1
  286. package/dist/schemas/cron.js +0 -75
  287. package/dist/schemas/cron.js.map +0 -1
  288. package/dist/schemas/geo.d.ts +0 -63
  289. package/dist/schemas/geo.d.ts.map +0 -1
  290. package/dist/schemas/geo.js +0 -131
  291. package/dist/schemas/geo.js.map +0 -1
  292. package/dist/schemas/guides.d.ts +0 -26
  293. package/dist/schemas/guides.d.ts.map +0 -1
  294. package/dist/schemas/guides.js +0 -33
  295. package/dist/schemas/guides.js.map +0 -1
  296. package/dist/schemas/library/annotations.d.ts +0 -616
  297. package/dist/schemas/library/annotations.d.ts.map +0 -1
  298. package/dist/schemas/library/annotations.js +0 -156
  299. package/dist/schemas/library/annotations.js.map +0 -1
  300. package/dist/schemas/library/collections.d.ts +0 -211
  301. package/dist/schemas/library/collections.d.ts.map +0 -1
  302. package/dist/schemas/library/collections.js +0 -57
  303. package/dist/schemas/library/collections.js.map +0 -1
  304. package/dist/schemas/library/community.d.ts +0 -1136
  305. package/dist/schemas/library/community.d.ts.map +0 -1
  306. package/dist/schemas/library/community.js +0 -315
  307. package/dist/schemas/library/community.js.map +0 -1
  308. package/dist/schemas/library/life.d.ts +0 -1440
  309. package/dist/schemas/library/life.d.ts.map +0 -1
  310. package/dist/schemas/library/life.js +0 -202
  311. package/dist/schemas/library/life.js.map +0 -1
  312. package/dist/schemas/library/media.d.ts +0 -1002
  313. package/dist/schemas/library/media.d.ts.map +0 -1
  314. package/dist/schemas/library/media.js +0 -110
  315. package/dist/schemas/library/media.js.map +0 -1
  316. package/dist/schemas/library/money.d.ts +0 -1221
  317. package/dist/schemas/library/money.d.ts.map +0 -1
  318. package/dist/schemas/library/money.js +0 -168
  319. package/dist/schemas/library/money.js.map +0 -1
  320. package/dist/schemas/library/planning.d.ts +0 -1107
  321. package/dist/schemas/library/planning.d.ts.map +0 -1
  322. package/dist/schemas/library/planning.js +0 -214
  323. package/dist/schemas/library/planning.js.map +0 -1
  324. package/dist/schemas/library/publishing.d.ts +0 -1499
  325. package/dist/schemas/library/publishing.d.ts.map +0 -1
  326. package/dist/schemas/library/publishing.js +0 -285
  327. package/dist/schemas/library/publishing.js.map +0 -1
  328. package/dist/schemas/library/social.d.ts +0 -1257
  329. package/dist/schemas/library/social.d.ts.map +0 -1
  330. package/dist/schemas/library/social.js +0 -147
  331. package/dist/schemas/library/social.js.map +0 -1
  332. package/dist/schemas/standard.d.ts +0 -21
  333. package/dist/schemas/standard.d.ts.map +0 -1
  334. package/dist/schemas/standard.js +0 -39
  335. package/dist/schemas/standard.js.map +0 -1
  336. package/dist/session/account-hint.d.ts +0 -13
  337. package/dist/session/account-hint.d.ts.map +0 -1
  338. package/dist/session/account-hint.js +0 -54
  339. package/dist/session/account-hint.js.map +0 -1
  340. package/dist/session/auth.d.ts +0 -411
  341. package/dist/session/auth.d.ts.map +0 -1
  342. package/dist/session/auth.js +0 -1529
  343. package/dist/session/auth.js.map +0 -1
  344. package/dist/session/channel-room.d.ts +0 -26
  345. package/dist/session/channel-room.d.ts.map +0 -1
  346. package/dist/session/channel-room.js +0 -56
  347. package/dist/session/channel-room.js.map +0 -1
  348. package/dist/session/connect-code.d.ts +0 -127
  349. package/dist/session/connect-code.d.ts.map +0 -1
  350. package/dist/session/connect-code.js +0 -408
  351. package/dist/session/connect-code.js.map +0 -1
  352. package/dist/session/connect.d.ts +0 -386
  353. package/dist/session/connect.d.ts.map +0 -1
  354. package/dist/session/connect.js +0 -583
  355. package/dist/session/connect.js.map +0 -1
  356. package/dist/session/connection.d.ts +0 -135
  357. package/dist/session/connection.d.ts.map +0 -1
  358. package/dist/session/connection.js +0 -215
  359. package/dist/session/connection.js.map +0 -1
  360. package/dist/session/credentials.d.ts +0 -30
  361. package/dist/session/credentials.d.ts.map +0 -1
  362. package/dist/session/credentials.js +0 -63
  363. package/dist/session/credentials.js.map +0 -1
  364. package/dist/session/hosting.d.ts.map +0 -1
  365. package/dist/session/hosting.js.map +0 -1
  366. package/dist/session/index.d.ts +0 -29
  367. package/dist/session/index.d.ts.map +0 -1
  368. package/dist/session/index.js +0 -21
  369. package/dist/session/index.js.map +0 -1
  370. package/dist/session/pairing.d.ts +0 -97
  371. package/dist/session/pairing.d.ts.map +0 -1
  372. package/dist/session/pairing.js +0 -251
  373. package/dist/session/pairing.js.map +0 -1
  374. package/dist/session/password-strength.d.ts +0 -22
  375. package/dist/session/password-strength.d.ts.map +0 -1
  376. package/dist/session/password-strength.js +0 -130
  377. package/dist/session/password-strength.js.map +0 -1
  378. package/dist/session/places.d.ts +0 -71
  379. package/dist/session/places.d.ts.map +0 -1
  380. package/dist/session/places.js +0 -130
  381. package/dist/session/places.js.map +0 -1
  382. package/dist/session/popup.d.ts +0 -13
  383. package/dist/session/popup.d.ts.map +0 -1
  384. package/dist/session/popup.js +0 -23
  385. package/dist/session/popup.js.map +0 -1
  386. package/dist/session/stay-signed-in.d.ts +0 -49
  387. package/dist/session/stay-signed-in.d.ts.map +0 -1
  388. package/dist/session/stay-signed-in.js +0 -141
  389. package/dist/session/stay-signed-in.js.map +0 -1
  390. package/dist/storage/blob/directory.d.ts +0 -14
  391. package/dist/storage/blob/directory.d.ts.map +0 -1
  392. package/dist/storage/blob/directory.js +0 -87
  393. package/dist/storage/blob/directory.js.map +0 -1
  394. package/dist/storage/blob/memory.d.ts +0 -6
  395. package/dist/storage/blob/memory.d.ts.map +0 -1
  396. package/dist/storage/blob/memory.js +0 -20
  397. package/dist/storage/blob/memory.js.map +0 -1
  398. package/dist/storage/blob/s3.d.ts +0 -16
  399. package/dist/storage/blob/s3.d.ts.map +0 -1
  400. package/dist/storage/blob/s3.js +0 -91
  401. package/dist/storage/blob/s3.js.map +0 -1
  402. package/dist/storage/directory-access.d.ts +0 -36
  403. package/dist/storage/directory-access.d.ts.map +0 -1
  404. package/dist/storage/directory-access.js +0 -89
  405. package/dist/storage/directory-access.js.map +0 -1
  406. package/dist/storage/folder-adapter.d.ts +0 -94
  407. package/dist/storage/folder-adapter.d.ts.map +0 -1
  408. package/dist/storage/folder-adapter.js +0 -215
  409. package/dist/storage/folder-adapter.js.map +0 -1
  410. package/dist/storage/folder-reconcile.d.ts +0 -33
  411. package/dist/storage/folder-reconcile.d.ts.map +0 -1
  412. package/dist/storage/folder-reconcile.js.map +0 -1
  413. package/dist/storage/index.d.ts +0 -6
  414. package/dist/storage/index.d.ts.map +0 -1
  415. package/dist/storage/index.js +0 -5
  416. package/dist/storage/index.js.map +0 -1
  417. package/dist/storage/indexeddb-adapter.d.ts +0 -8
  418. package/dist/storage/indexeddb-adapter.d.ts.map +0 -1
  419. package/dist/storage/indexeddb-adapter.js +0 -103
  420. package/dist/storage/indexeddb-adapter.js.map +0 -1
package/dist/node/host.js DELETED
@@ -1,818 +0,0 @@
1
- import { createUsageMeter } from './usage.js';
2
- import { createP256Provider } from '../identity/crypto-p256.js';
3
- import { createServerAuth } from '../network/peer-auth.js';
4
- import { utf8Decode, utf8Encode } from '../utils/encoding.js';
5
- import { isRecord } from '../utils/guards.js';
6
- import { createCarryCore } from './carrier.js';
7
- import { deleteMirrored } from '../storage/mirror.js';
8
- import { openPass } from '../space/pass.js';
9
- import { parseSpaceInvite } from '../space/space-manager.js';
10
- import { createListeners } from '../utils/events.js';
11
- /** What names a space's own subscription at a host */
12
- const SPACE_SUBSCRIPTION_PREFIX = 'space:';
13
- /** A space's own subscription id: `space:<space id>` */
14
- export const spaceSubscription = (spaceId) => `${SPACE_SUBSCRIPTION_PREFIX}${spaceId}`;
15
- /** The space a subscription is a space's own for, or null for an account's */
16
- const spaceIdOf = (id) => id.startsWith(SPACE_SUBSCRIPTION_PREFIX) && id.length > SPACE_SUBSCRIPTION_PREFIX.length && id.length <= 200
17
- ? id.slice(SPACE_SUBSCRIPTION_PREFIX.length)
18
- : null;
19
- /** An account this host was not told to carry for */
20
- export class NotAllowedError extends Error {
21
- constructor() {
22
- super('This host only carries spaces for the accounts its owner named');
23
- }
24
- }
25
- const DAY = 24 * 3600;
26
- /** How far under every limit a space must stay, for its window, to be free again */
27
- const UNDER = 0.8;
28
- /** How long after the host first sees a space what arrives may be its history, rather than people writing now */
29
- const CATCH_UP_SECONDS = 600;
30
- /** Days of notice a space gets when it goes over again after its grace was spent */
31
- const NOTICE_DAYS = 7;
32
- /** Starts a host */
33
- export async function createHostNode(config) {
34
- const provider = config.provider ?? createP256Provider();
35
- const now = config.now ?? (() => Math.floor(Date.now() / 1000));
36
- const graceSeconds = (config.graceDays ?? 30) * DAY;
37
- const allowed = (account) => !config.allow || config.allow.includes(account);
38
- const store = await config.stores('host');
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
- };
53
- const read = async (id) => {
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.
56
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes
57
- return text ? JSON.parse(text) : null;
58
- };
59
- const write = async (subscription) => {
60
- await kept.subscriptions.put(subscription.id, JSON.stringify(subscription));
61
- return subscription;
62
- };
63
- const list = async () => {
64
- const all = await Promise.all((await kept.subscriptions.names()).map(read));
65
- return all.filter((subscription) => subscription !== null);
66
- };
67
- const state = (subscription) => {
68
- if (config.free || subscription.paidUntil >= now())
69
- return 'active';
70
- // With limits an account needs no payment: not paying is the free plan.
71
- if (limits && !spaceIdOf(subscription.id))
72
- return 'active';
73
- return subscription.paidUntil + graceSeconds >= now() ? 'grace' : 'lapsed';
74
- };
75
- /** Which room an account's subscription has, at a host with limits; null otherwise */
76
- const plan = (subscription) => {
77
- if (!limits || spaceIdOf(subscription.id))
78
- return null;
79
- return subscription.paidUntil >= now() ? 'paid' : 'free';
80
- };
81
- // ─── The free line ─────────────────────────────────────────────────
82
- const meter = limits
83
- ? await createUsageMeter({
84
- store: await config.stores('host-usage'),
85
- windowDays: limits.space.windowDays,
86
- now,
87
- })
88
- : null;
89
- const lines = new Map();
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);
99
- }
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);
104
- }
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);
109
- }
110
- }
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);
119
- }
120
- }
121
- async function writeRoom(account, room) {
122
- if (sameJson(room, rooms.get(account) ?? null))
123
- return;
124
- if (room) {
125
- rooms.set(account, room);
126
- await kept.rooms.put(account, JSON.stringify(room));
127
- }
128
- else {
129
- rooms.delete(account);
130
- await kept.rooms.delete(account);
131
- }
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
- }
142
- const accountOf = (subscription) => subscription.carry?.account;
143
- const roomOf = (subscription) => rooms.get(accountOf(subscription) ?? '');
144
- /** Whether a subscription's account is past its notice of being over its room */
145
- const outOfRoom = (subscription) => {
146
- const room = roomOf(subscription);
147
- return room !== undefined && room.full + lineGrace <= now();
148
- };
149
- /** Each carry space's spaces held back for want of its room */
150
- const holds = new Map();
151
- /** Those whose copy the host still keeps: held back less than `keepDays` for some account */
152
- const roomHeld = new Set();
153
- /** Held back longer, their copy already deleted: not deleted again until the host restarts */
154
- const dropped = new Set();
155
- /** Spaces members' subscriptions no longer carry, being over the line with nobody paying */
156
- const skipped = new Set([...lines].filter(([, line]) => line.stopped !== undefined).map(([id]) => id));
157
- /**
158
- * A version reached a carried space: its account wrote there, unless it is
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.
161
- */
162
- const counted = (spaceId, version) => {
163
- if (!meter || !limits)
164
- return;
165
- const account = version.proof?.payload.iss ?? version.author;
166
- const written = Date.parse(version.createdAt) / 1000;
167
- const old = Number.isFinite(written) && written < now() - limits.space.windowDays * DAY;
168
- const count = async () => {
169
- const catchingUp = now() - (await meter.firstSeen(spaceId)) < CATCH_UP_SECONDS;
170
- if (old && catchingUp)
171
- return;
172
- if (!(await config.ignore?.(account)))
173
- await meter.wrote(spaceId, account);
174
- };
175
- void count().catch(() => { });
176
- };
177
- /** Whether anyone but the account holds a role in its carry space: then it is a shared space */
178
- const sharedCarry = async (carrySpace, account) => {
179
- const entry = core.carried.get(carrySpace);
180
- if (!entry)
181
- return false;
182
- const { members } = await entry.runtime.access();
183
- return members.some((member) => member.did !== account && (member.founder || member.roles.length > 0));
184
- };
185
- /** Stops a carry space being any subscription's */
186
- const letCarryGo = async (carrySpace) => {
187
- for (const subscription of await list()) {
188
- if (subscription.carry?.space !== carrySpace)
189
- continue;
190
- const { carry: _gone, ...rest } = subscription;
191
- await write(rest);
192
- }
193
- await core.removeCarry(carrySpace);
194
- };
195
- /** The accounts that are members of a carried space now: only their writes count */
196
- const membersOf = async (spaceId) => {
197
- const entry = core.carried.get(spaceId);
198
- if (!entry)
199
- return undefined;
200
- return (await entry.runtime.access()).members.map((member) => member.did);
201
- };
202
- await kept.subscriptions.restore();
203
- /** The account's carry space being let go, after it wrote `carry:closed` */
204
- let closing = Promise.resolve();
205
- /** Whether the carry core below is made, so its events may look at it */
206
- let opened = false;
207
- const core = await createCarryCore({
208
- key: config.key,
209
- stores: config.stores,
210
- ...(config.network ? { network: config.network } : {}),
211
- provider,
212
- ...(config.watchIntervalMs !== undefined ? { watchIntervalMs: config.watchIntervalMs } : {}),
213
- emit: (event) => {
214
- // A space newly carried: what arrives over the next minutes is it catching up.
215
- if (event.type === 'spaces' && meter && opened)
216
- for (const spaceId of core.carried.keys())
217
- void meter.firstSeen(spaceId).catch(() => { });
218
- events.emit(event);
219
- },
220
- ...(config.full ? { full: config.full } : {}),
221
- ...(limits
222
- ? {
223
- skip: (spaceId) => skipped.has(spaceId),
224
- held: (carrySpace) => holds.get(carrySpace),
225
- onArrived: counted,
226
- }
227
- : {}),
228
- ...(config.spacesPerCarry !== undefined ? { most: config.spacesPerCarry } : {}),
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.
231
- onClosed: (carrySpace) => {
232
- closing = closing.then(() => letCarryGo(carrySpace));
233
- },
234
- });
235
- opened = true;
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);
255
- }
256
- }
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 */
265
- const paysItself = async (spaceId) => {
266
- const own = await read(spaceSubscription(spaceId));
267
- return !!own && state(own) === 'active';
268
- };
269
- /** The spaces the free line is about: those accounts' passes name, and the carry spaces too */
270
- const metered = () => {
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()]);
274
- };
275
- const measure = async (spaceId) => (await config.measure?.(spaceId).catch(() => 0)) ?? 0;
276
- /** Where one space stands now: its counts, its size, and the line it is held to */
277
- async function standing(spaceId, before, own) {
278
- if (!limits || !meter)
279
- return null;
280
- const t = now();
281
- // Members stopped carrying it, so nothing it does reaches here: it stays, and is forgotten after a while.
282
- if (before?.stopped !== undefined && !own)
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.
285
- const members = await membersOf(spaceId);
286
- if (!members)
287
- return before ?? null;
288
- const counts = await meter.usage(spaceId, members);
289
- const bytes = await measure(spaceId);
290
- const shared = counts.writers >= 2;
291
- // One writer is never a community, whatever the limit says.
292
- const most = Math.max(limits.space.writers, 1);
293
- const reasons = [];
294
- if (counts.writers > most)
295
- reasons.push('writers');
296
- if (shared && bytes > limits.space.bytes)
297
- reasons.push('bytes');
298
- const wellUnder = counts.writers <= most * UNDER && (!shared || bytes <= limits.space.bytes * UNDER);
299
- let line = before ?? null;
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);
304
- const from = spent ? Math.max(before?.from ?? notice, notice) : (before?.from ?? t);
305
- line = { since: before?.since ?? t, from, reasons, lastOver: t };
306
- }
307
- else if (line && wellUnder) {
308
- const under = line.under ?? t;
309
- // Free again only after a whole window well under, so a busy week does not flip it.
310
- if (t - under >= limits.space.windowDays * DAY)
311
- return null;
312
- line = { ...line, calm: true, under };
313
- }
314
- else if (line) {
315
- const { under: _gone, ...rest } = line;
316
- line = { ...rest, calm: true };
317
- }
318
- if (!line)
319
- return null;
320
- // While it pays for itself the line does not apply, but its grace is not given again.
321
- if (own) {
322
- const { stopped: _paid, ...rest } = line;
323
- return rest;
324
- }
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.
327
- const until = line.from + lineGrace;
328
- const lately = (line.lastOver ?? 0) >= until - NOTICE_DAYS * DAY;
329
- const stops = (reasons.length > 0 || lately) && until <= t && line.stopped === undefined;
330
- return stops ? { ...line, stopped: t } : line;
331
- }
332
- /**
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.
336
- */
337
- async function evaluate() {
338
- const changed = { over: [], stopped: [], under: [] };
339
- if (!limits || !meter)
340
- return changed;
341
- await meter.prune();
342
- const spaces = metered();
343
- for (const spaceId of new Set([...spaces, ...lines.keys()]))
344
- await evaluateOne(spaceId, spaces, changed).catch((error) => {
345
- // Judged again next time; the others go on.
346
- console.error(`Could not hold space ${spaceId} to the free line:`, error);
347
- });
348
- await core.refresh();
349
- return changed;
350
- }
351
- async function evaluateOne(spaceId, spaces, changed) {
352
- if (!meter || !limits)
353
- return;
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))
373
- await letCarryGo(spaceId);
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(() => { });
392
- }
393
- }
394
- /** The spaces over the line among these, as a status says them */
395
- const overOf = (spaceIds) => [...spaceIds].flatMap((spaceId) => {
396
- const line = lines.get(spaceId);
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
400
- ? [{ space: spaceId, since: line.since, until: line.from + lineGrace, reasons: line.reasons }]
401
- : [];
402
- });
403
- /** Makes a subscription if there is none by this id, and returns it */
404
- const subscribe = async (id) => {
405
- if (!id.startsWith('did:key:') && !spaceIdOf(id))
406
- throw new Error('A subscription is named by its key, or by the space it is for');
407
- return (await read(id)) ?? write({ id, paidUntil: 0, since: now() });
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
- }
440
- return Object.freeze({
441
- did: core.did,
442
- /** The short links its accounts keep, under a mailbox topic: what its relay answers besides drops */
443
- linksAt: core.linksAt,
444
- subscribe,
445
- get: read,
446
- list,
447
- /** Whether a subscription is paid, in its grace period, or lapsed */
448
- state,
449
- /** At a host with limits, which room an account's subscription has: null at one without */
450
- plan,
451
- /** The limits it keeps to, or null when it has none */
452
- limits,
453
- /**
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.
456
- */
457
- async setFull(id, over) {
458
- const subscription = await read(id);
459
- const account = subscription && accountOf(subscription);
460
- if (!account || !limits)
461
- return;
462
- const room = rooms.get(account);
463
- const t = now();
464
- if (over) {
465
- await writeRoom(account, {
466
- full: room?.full ?? t,
467
- ...(room?.refused ? { refused: true } : {}),
468
- ...(room?.held ? { held: room.held } : {}),
469
- });
470
- return;
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 });
478
- },
479
- /** Whether a subscription is past its notice of being over its room */
480
- outOfRoom,
481
- /** Holds back these solo spaces of a subscription's for want of room, and no others: kept, unserved, until they fit */
482
- async holdForRoom(id, spaceIds) {
483
- const subscription = await read(id);
484
- const carrySpace = subscription?.carry?.space;
485
- const account = subscription && accountOf(subscription);
486
- if (!carrySpace || !account)
487
- return;
488
- const t = now();
489
- const room = rooms.get(account);
490
- if (room) {
491
- const { held: before = {}, ...rest } = room;
492
- const held = Object.fromEntries(spaceIds.map((spaceId) => [spaceId, before[spaceId] ?? t]));
493
- await writeRoom(account, Object.keys(held).length ? { ...rest, held } : rest);
494
- }
495
- const before = holdsSignature();
496
- if (spaceIds.length)
497
- holds.set(carrySpace, new Set(spaceIds));
498
- else
499
- holds.delete(carrySpace);
500
- recountRoomHeld(t);
501
- await dropLongHeld();
502
- if (holdsSignature() !== before)
503
- await core.refresh();
504
- },
505
- /** Stops carrying a carry space over its room by itself past its notice, until the host's owner says */
506
- async refuse(id) {
507
- const subscription = await read(id);
508
- if (!subscription?.carry)
509
- return;
510
- const { carry, ...rest } = subscription;
511
- await writeRoom(carry.account, { ...(rooms.get(carry.account) ?? { full: now() }), refused: true });
512
- await write(rest);
513
- await releaseCarry(carry.space, id);
514
- },
515
- async seen(id) {
516
- const subscription = await read(id);
517
- if (!subscription || (subscription.seen !== undefined && now() - subscription.seen < DAY))
518
- return;
519
- await write({ ...subscription, seen: now() });
520
- },
521
- /** A session key proved it reads a carried space: counted, though readers only ever hint */
522
- read(spaceId, session) {
523
- if (core.carried.has(spaceId))
524
- meter?.read(spaceId, session);
525
- },
526
- evaluate,
527
- /** The spaces over the free line that a subscription names, or its own space when it is one */
528
- async overFor(id) {
529
- const own = spaceIdOf(id);
530
- if (own)
531
- return overOf([own]);
532
- const space = (await read(id))?.carry?.space;
533
- const named = space ? [space, ...(core.carries.get(space)?.named ?? [])] : [];
534
- // One its own fund pays for is nothing its members need to hear about.
535
- const unpaid = [];
536
- for (const spaceId of named)
537
- if (lines.has(spaceId) && !(await paysItself(spaceId)))
538
- unpaid.push(spaceId);
539
- return overOf(unpaid);
540
- },
541
- /** How a space is used, as a status says it; null at a host without limits */
542
- async usage(spaceId) {
543
- if (!meter)
544
- return null;
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 };
548
- },
549
- /** When a subscription's notice of being over its room runs out, or null when it is not over */
550
- fullUntil: (subscription) => {
551
- const room = roomOf(subscription);
552
- return room ? { since: room.full, until: room.full + lineGrace } : null;
553
- },
554
- /** Whether members' subscriptions hold a space back, being past the free line */
555
- heldBack: (spaceId) => skipped.has(spaceId),
556
- /** Whether a space pays for itself now, so no account's room counts it */
557
- paysItself,
558
- /** Moves a subscription's paid-until date: the one thing payments do */
559
- async extend(id, until, customer) {
560
- const subscription = await subscribe(id);
561
- const extended = await write({
562
- ...subscription,
563
- paidUntil: Math.max(subscription.paidUntil, until),
564
- ...(customer ? { customer } : {}),
565
- });
566
- // Paid again in time: carry again what the grace period had kept.
567
- if (extended.carry && !core.carries.has(extended.carry.space))
568
- await core.addCarry(extended.carry.account, extended.carry.invite);
569
- if (extended.pass !== undefined)
570
- await core.addPass(extended.pass).catch(() => { });
571
- return extended;
572
- },
573
- /** Sets a paid-until date outright, earlier too: what a fund's estimate says as it is spent */
574
- async setPaidUntil(id, until) {
575
- const subscription = await subscribe(id);
576
- if (subscription.paidUntil === until)
577
- return subscription;
578
- const set = await write({ ...subscription, paidUntil: until });
579
- if (set.pass !== undefined && state(set) !== 'lapsed')
580
- await core.addPass(set.pass).catch(() => { });
581
- return set;
582
- },
583
- /** Keeps a wallet payment asked for, or drops it (null) once it arrived */
584
- async setInvoice(id, invoice) {
585
- const { invoice: _before, ...subscription } = await subscribe(id);
586
- return write(invoice ? { ...subscription, invoice } : subscription);
587
- },
588
- /**
589
- * Starts carrying an account's spaces for a subscription: its carry space,
590
- * and every space its passes name. Replaces what the subscription carried.
591
- * @throws When the subscription is lapsed, or the invite is not a carry space's
592
- */
593
- async attach(id, account, invite) {
594
- const subscription = await read(id);
595
- if (!subscription || state(subscription) === 'lapsed')
596
- throw new Error('That subscription is not paid for');
597
- if (!account.startsWith('did:key:'))
598
- throw new Error('An account is named by its DID');
599
- if (!allowed(account))
600
- throw new NotAllowedError();
601
- await liftRefusal(account, plan(subscription));
602
- const asked = parseSpaceInvite(invite).space.id;
603
- await checkCarrySpace(asked);
604
- const space = await core.addCarry(account, invite);
605
- // Anyone else holding a role there makes it a shared space, which members' own passes carry.
606
- if (limits && (await sharedCarry(space, account))) {
607
- await releaseCarry(space, id);
608
- throw new Error('That is a space others are members of, not a carry space');
609
- }
610
- const before = subscription.carry;
611
- const attached = await write({ ...subscription, carry: { account, space, invite } });
612
- if (before && before.space !== space)
613
- await releaseCarry(before.space, id);
614
- return attached;
615
- },
616
- /** Stops carrying for a subscription; the subscription stays */
617
- async detach(id) {
618
- const subscription = await read(id);
619
- if (!subscription?.carry)
620
- return;
621
- const { carry, ...rest } = subscription;
622
- await write(rest);
623
- await releaseCarry(carry.space, id);
624
- },
625
- /**
626
- * Carries a space for its own subscription, from a pass. A later pass (the
627
- * space's key changed) replaces the one before.
628
- * @throws When the subscription is lapsed, or the pass is not for its space
629
- */
630
- async carrySpace(id, pass) {
631
- const spaceId = spaceIdOf(id);
632
- if (!spaceId)
633
- throw new Error('Only a space’s own subscription carries a space from a pass');
634
- const subscription = await read(id);
635
- if (!subscription || state(subscription) === 'lapsed')
636
- throw new Error('That space is not paid for');
637
- const opened = await openPass(pass, provider);
638
- if (!opened || opened.space.id !== spaceId)
639
- throw new Error('That is not a pass for this space');
640
- await core.addPass(pass);
641
- return write({ ...subscription, pass });
642
- },
643
- /** The read key a space's own subscription carries it with, as a DID: null when public or not carried */
644
- readKeyOf(id) {
645
- const spaceId = spaceIdOf(id);
646
- return spaceId ? core.readKeyOf(spaceId) : null;
647
- },
648
- /** Drops what lapsed past its grace period, and free subscriptions idle too long. Run now and then. */
649
- async sweep() {
650
- const gone = [];
651
- for (const subscription of await list()) {
652
- if (!(await expired(subscription)))
653
- continue;
654
- await kept.subscriptions.delete(subscription.id);
655
- const spaceId = spaceIdOf(subscription.id);
656
- if (spaceId)
657
- await core.removePass(spaceId);
658
- if (subscription.carry)
659
- await releaseCarry(subscription.carry.space, subscription.id);
660
- gone.push(subscription.id);
661
- }
662
- return gone;
663
- },
664
- spaces: core.spaces,
665
- /** Reads every account's passes again, taking a space that waited for room: after what `full` says changed */
666
- recheck: () => core.refresh(),
667
- /** Every space a subscription's carry space names, carried or not */
668
- async namedFor(id) {
669
- const space = (await read(id))?.carry?.space;
670
- return [...(space ? (core.carries.get(space)?.named ?? []) : [])];
671
- },
672
- /** How many spaces a subscription's account asks the host to carry */
673
- async carriedFor(id) {
674
- const space = (await read(id))?.carry?.space;
675
- return space ? (core.carries.get(space)?.wants.size ?? 0) : 0;
676
- },
677
- /** The spaces a subscription is carried for now: its carry space and those its passes name, or a space's own */
678
- async spacesOf(id) {
679
- const subscription = await read(id);
680
- const own = spaceIdOf(id);
681
- if (own)
682
- return subscription?.pass !== undefined ? [own] : [];
683
- const space = subscription?.carry?.space;
684
- return space ? [space, ...(core.carries.get(space)?.wants ?? [])] : [];
685
- },
686
- /** For a server taking sockets: checks a connecting peer against the space's history. Null for a space not carried. */
687
- async authenticator(spaceId) {
688
- const entry = core.carried.get(spaceId);
689
- if (!entry)
690
- return null;
691
- const read = entry.record.space.visibility === 'private' ? entry.runtime.readAccess() : null;
692
- return createServerAuth(spaceId, read, config.key.privateKey, provider);
693
- },
694
- subscribeEvents: events.subscribe,
695
- async close() {
696
- await closing;
697
- await core.close();
698
- await store.close();
699
- events.clear();
700
- },
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
- };
816
- }
817
- const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
818
- //# sourceMappingURL=host.js.map