@minca/core 0.1.15 → 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 (427) hide show
  1. package/README.md +2 -2
  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/identity/abilities.d.ts +2 -0
  10. package/dist/identity/abilities.d.ts.map +1 -1
  11. package/dist/identity/abilities.js +2 -0
  12. package/dist/identity/abilities.js.map +1 -1
  13. package/dist/identity/account-vault.d.ts +15 -71
  14. package/dist/identity/account-vault.d.ts.map +1 -1
  15. package/dist/identity/account-vault.js +45 -104
  16. package/dist/identity/account-vault.js.map +1 -1
  17. package/dist/identity/channel.d.ts +3 -9
  18. package/dist/identity/channel.d.ts.map +1 -1
  19. package/dist/identity/channel.js +42 -42
  20. package/dist/identity/channel.js.map +1 -1
  21. package/dist/identity/contact-key.d.ts +14 -29
  22. package/dist/identity/contact-key.d.ts.map +1 -1
  23. package/dist/identity/contact-key.js +31 -51
  24. package/dist/identity/contact-key.js.map +1 -1
  25. package/dist/identity/crypto-p256.d.ts.map +1 -1
  26. package/dist/identity/crypto-p256.js +39 -53
  27. package/dist/identity/crypto-p256.js.map +1 -1
  28. package/dist/identity/hpke.d.ts.map +1 -1
  29. package/dist/identity/hpke.js +19 -19
  30. package/dist/identity/hpke.js.map +1 -1
  31. package/dist/identity/identity-manager.d.ts.map +1 -1
  32. package/dist/identity/identity-manager.js +1 -2
  33. package/dist/identity/identity-manager.js.map +1 -1
  34. package/dist/identity/policy.d.ts.map +1 -1
  35. package/dist/identity/policy.js +35 -61
  36. package/dist/identity/policy.js.map +1 -1
  37. package/dist/identity/recovery-code.d.ts +2 -8
  38. package/dist/identity/recovery-code.d.ts.map +1 -1
  39. package/dist/identity/recovery-code.js +4 -11
  40. package/dist/identity/recovery-code.js.map +1 -1
  41. package/dist/identity/root-signer.d.ts +1 -6
  42. package/dist/identity/root-signer.d.ts.map +1 -1
  43. package/dist/identity/root-signer.js +12 -15
  44. package/dist/identity/root-signer.js.map +1 -1
  45. package/dist/identity/ucan.d.ts.map +1 -1
  46. package/dist/identity/ucan.js +48 -39
  47. package/dist/identity/ucan.js.map +1 -1
  48. package/dist/identity/webauthn.d.ts +6 -11
  49. package/dist/identity/webauthn.d.ts.map +1 -1
  50. package/dist/identity/webauthn.js +18 -30
  51. package/dist/identity/webauthn.js.map +1 -1
  52. package/dist/inbox/inbox.d.ts.map +1 -1
  53. package/dist/inbox/inbox.js +40 -30
  54. package/dist/inbox/inbox.js.map +1 -1
  55. package/dist/inbox/index.d.ts +2 -0
  56. package/dist/inbox/index.d.ts.map +1 -1
  57. package/dist/inbox/index.js +1 -0
  58. package/dist/inbox/index.js.map +1 -1
  59. package/dist/inbox/names.d.ts +54 -0
  60. package/dist/inbox/names.d.ts.map +1 -0
  61. package/dist/inbox/names.js +299 -0
  62. package/dist/inbox/names.js.map +1 -0
  63. package/dist/index.d.ts +3 -6
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +9 -12
  66. package/dist/index.js.map +1 -1
  67. package/dist/network/mesh.d.ts +7 -20
  68. package/dist/network/mesh.d.ts.map +1 -1
  69. package/dist/network/mesh.js +267 -245
  70. package/dist/network/mesh.js.map +1 -1
  71. package/dist/network/relay-link.d.ts +31 -0
  72. package/dist/network/relay-link.d.ts.map +1 -0
  73. package/dist/network/relay-link.js +62 -0
  74. package/dist/network/relay-link.js.map +1 -0
  75. package/dist/network/relay-minca.d.ts.map +1 -1
  76. package/dist/network/relay-minca.js +11 -49
  77. package/dist/network/relay-minca.js.map +1 -1
  78. package/dist/network/relay-nostr.d.ts.map +1 -1
  79. package/dist/network/relay-nostr.js +23 -63
  80. package/dist/network/relay-nostr.js.map +1 -1
  81. package/dist/network/relay-socket.d.ts +1 -1
  82. package/dist/network/relay-socket.d.ts.map +1 -1
  83. package/dist/network/relay-socket.js +3 -1
  84. package/dist/network/relay-socket.js.map +1 -1
  85. package/dist/node/actions.d.ts.map +1 -1
  86. package/dist/node/actions.js +50 -38
  87. package/dist/node/actions.js.map +1 -1
  88. package/dist/node/api.d.ts +6 -0
  89. package/dist/node/api.d.ts.map +1 -1
  90. package/dist/node/api.js +6 -0
  91. package/dist/node/api.js.map +1 -1
  92. package/dist/node/carrier.d.ts +5 -0
  93. package/dist/node/carrier.d.ts.map +1 -1
  94. package/dist/node/carrier.js +94 -73
  95. package/dist/node/carrier.js.map +1 -1
  96. package/dist/node/copy.d.ts +8 -7
  97. package/dist/node/copy.d.ts.map +1 -1
  98. package/dist/node/copy.js +54 -56
  99. package/dist/node/copy.js.map +1 -1
  100. package/dist/node/host.d.ts +39 -65
  101. package/dist/node/host.d.ts.map +1 -1
  102. package/dist/node/host.js +362 -379
  103. package/dist/node/host.js.map +1 -1
  104. package/dist/node/index.d.ts +2 -2
  105. package/dist/node/index.d.ts.map +1 -1
  106. package/dist/node/index.js +1 -1
  107. package/dist/node/index.js.map +1 -1
  108. package/dist/node/node-carriers.d.ts +35 -0
  109. package/dist/node/node-carriers.d.ts.map +1 -0
  110. package/dist/node/node-carriers.js +562 -0
  111. package/dist/node/node-carriers.js.map +1 -0
  112. package/dist/node/node-contacts.d.ts +11 -0
  113. package/dist/node/node-contacts.d.ts.map +1 -0
  114. package/dist/node/node-contacts.js +808 -0
  115. package/dist/node/node-contacts.js.map +1 -0
  116. package/dist/node/node-context.d.ts +76 -0
  117. package/dist/node/node-context.d.ts.map +1 -0
  118. package/dist/node/node-context.js +54 -0
  119. package/dist/node/node-context.js.map +1 -0
  120. package/dist/node/node-hosting.d.ts +20 -0
  121. package/dist/node/node-hosting.d.ts.map +1 -0
  122. package/dist/node/node-hosting.js +449 -0
  123. package/dist/node/node-hosting.js.map +1 -0
  124. package/dist/node/node-records.d.ts +18 -0
  125. package/dist/node/node-records.d.ts.map +1 -0
  126. package/dist/node/node-records.js +262 -0
  127. package/dist/node/node-records.js.map +1 -0
  128. package/dist/node/node.d.ts.map +1 -1
  129. package/dist/node/node.js +198 -1990
  130. package/dist/node/node.js.map +1 -1
  131. package/dist/node/remote.d.ts +5 -6
  132. package/dist/node/remote.d.ts.map +1 -1
  133. package/dist/node/remote.js +272 -260
  134. package/dist/node/remote.js.map +1 -1
  135. package/dist/node/space-catalog.d.ts +40 -0
  136. package/dist/node/space-catalog.d.ts.map +1 -0
  137. package/dist/node/space-catalog.js +158 -0
  138. package/dist/node/space-catalog.js.map +1 -0
  139. package/dist/node/space-checks.d.ts +25 -0
  140. package/dist/node/space-checks.d.ts.map +1 -0
  141. package/dist/node/space-checks.js +343 -0
  142. package/dist/node/space-checks.js.map +1 -0
  143. package/dist/node/space-history.d.ts +37 -0
  144. package/dist/node/space-history.d.ts.map +1 -0
  145. package/dist/node/space-history.js +350 -0
  146. package/dist/node/space-history.js.map +1 -0
  147. package/dist/node/space-holding.d.ts +49 -0
  148. package/dist/node/space-holding.d.ts.map +1 -0
  149. package/dist/node/space-holding.js +267 -0
  150. package/dist/node/space-holding.js.map +1 -0
  151. package/dist/node/space-index.d.ts +41 -0
  152. package/dist/node/space-index.d.ts.map +1 -0
  153. package/dist/node/space-index.js +217 -0
  154. package/dist/node/space-index.js.map +1 -0
  155. package/dist/node/space-keys.d.ts +34 -0
  156. package/dist/node/space-keys.d.ts.map +1 -0
  157. package/dist/node/space-keys.js +309 -0
  158. package/dist/node/space-keys.js.map +1 -0
  159. package/dist/node/space-lines.d.ts +33 -0
  160. package/dist/node/space-lines.d.ts.map +1 -0
  161. package/dist/node/space-lines.js +145 -0
  162. package/dist/node/space-lines.js.map +1 -0
  163. package/dist/node/space-membership.d.ts +69 -0
  164. package/dist/node/space-membership.d.ts.map +1 -0
  165. package/dist/node/space-membership.js +242 -0
  166. package/dist/node/space-membership.js.map +1 -0
  167. package/dist/node/space-mirrors.d.ts +43 -0
  168. package/dist/node/space-mirrors.d.ts.map +1 -0
  169. package/dist/node/space-mirrors.js +125 -0
  170. package/dist/node/space-mirrors.js.map +1 -0
  171. package/dist/node/space-peers.d.ts +104 -0
  172. package/dist/node/space-peers.d.ts.map +1 -0
  173. package/dist/node/space-peers.js +595 -0
  174. package/dist/node/space-peers.js.map +1 -0
  175. package/dist/node/space-profiles.d.ts +18 -0
  176. package/dist/node/space-profiles.d.ts.map +1 -0
  177. package/dist/node/space-profiles.js +105 -0
  178. package/dist/node/space-profiles.js.map +1 -0
  179. package/dist/node/space-rules.d.ts +29 -0
  180. package/dist/node/space-rules.d.ts.map +1 -0
  181. package/dist/node/space-rules.js +144 -0
  182. package/dist/node/space-rules.js.map +1 -0
  183. package/dist/node/space-runtime.d.ts +103 -142
  184. package/dist/node/space-runtime.d.ts.map +1 -1
  185. package/dist/node/space-runtime.js +290 -3963
  186. package/dist/node/space-runtime.js.map +1 -1
  187. package/dist/node/space-standing.d.ts +20 -0
  188. package/dist/node/space-standing.d.ts.map +1 -0
  189. package/dist/node/space-standing.js +317 -0
  190. package/dist/node/space-standing.js.map +1 -0
  191. package/dist/node/space-versions.d.ts +104 -0
  192. package/dist/node/space-versions.d.ts.map +1 -0
  193. package/dist/node/space-versions.js +153 -0
  194. package/dist/node/space-versions.js.map +1 -0
  195. package/dist/node/space-warrants.d.ts +25 -0
  196. package/dist/node/space-warrants.d.ts.map +1 -0
  197. package/dist/node/space-warrants.js +214 -0
  198. package/dist/node/space-warrants.js.map +1 -0
  199. package/dist/node/space-writing.d.ts +72 -0
  200. package/dist/node/space-writing.d.ts.map +1 -0
  201. package/dist/node/space-writing.js +440 -0
  202. package/dist/node/space-writing.js.map +1 -0
  203. package/dist/node/stores.d.ts +19 -50
  204. package/dist/node/stores.d.ts.map +1 -1
  205. package/dist/node/stores.js +14 -33
  206. package/dist/node/stores.js.map +1 -1
  207. package/dist/node/types.d.ts +225 -306
  208. package/dist/node/types.d.ts.map +1 -1
  209. package/dist/node/usage.d.ts +2 -24
  210. package/dist/node/usage.d.ts.map +1 -1
  211. package/dist/node/usage.js +118 -102
  212. package/dist/node/usage.js.map +1 -1
  213. package/dist/node/watch-notifications.d.ts.map +1 -1
  214. package/dist/node/watch-notifications.js +32 -29
  215. package/dist/node/watch-notifications.js.map +1 -1
  216. package/dist/node/worker.d.ts +5 -6
  217. package/dist/node/worker.d.ts.map +1 -1
  218. package/dist/node/worker.js +80 -72
  219. package/dist/node/worker.js.map +1 -1
  220. package/dist/node/writer-keys.d.ts +5 -6
  221. package/dist/node/writer-keys.d.ts.map +1 -1
  222. package/dist/node/writer-keys.js +66 -57
  223. package/dist/node/writer-keys.js.map +1 -1
  224. package/dist/node/writers.d.ts +12 -19
  225. package/dist/node/writers.d.ts.map +1 -1
  226. package/dist/node/writers.js +7 -15
  227. package/dist/node/writers.js.map +1 -1
  228. package/dist/privacy/direct.d.ts.map +1 -1
  229. package/dist/privacy/direct.js +31 -27
  230. package/dist/privacy/direct.js.map +1 -1
  231. package/dist/privacy/space-encryption.d.ts.map +1 -1
  232. package/dist/privacy/space-encryption.js +5 -9
  233. package/dist/privacy/space-encryption.js.map +1 -1
  234. package/dist/query/computed.d.ts.map +1 -1
  235. package/dist/query/computed.js +78 -77
  236. package/dist/query/computed.js.map +1 -1
  237. package/dist/query/engine.d.ts +3 -4
  238. package/dist/query/engine.d.ts.map +1 -1
  239. package/dist/query/engine.js +93 -86
  240. package/dist/query/engine.js.map +1 -1
  241. package/dist/query/filter.d.ts +8 -10
  242. package/dist/query/filter.d.ts.map +1 -1
  243. package/dist/query/filter.js +144 -161
  244. package/dist/query/filter.js.map +1 -1
  245. package/dist/records/cel-types.d.ts.map +1 -1
  246. package/dist/records/cel-types.js +13 -27
  247. package/dist/records/cel-types.js.map +1 -1
  248. package/dist/records/cel.d.ts.map +1 -1
  249. package/dist/records/cel.js +177 -242
  250. package/dist/records/cel.js.map +1 -1
  251. package/dist/records/rules.d.ts.map +1 -1
  252. package/dist/records/rules.js +46 -50
  253. package/dist/records/rules.js.map +1 -1
  254. package/dist/records/topics.d.ts.map +1 -1
  255. package/dist/records/topics.js +18 -22
  256. package/dist/records/topics.js.map +1 -1
  257. package/dist/schemas/automations.d.ts +8 -15
  258. package/dist/schemas/automations.d.ts.map +1 -1
  259. package/dist/schemas/automations.js +203 -165
  260. package/dist/schemas/automations.js.map +1 -1
  261. package/dist/schemas/blueprints.d.ts.map +1 -1
  262. package/dist/schemas/blueprints.js +14 -13
  263. package/dist/schemas/blueprints.js.map +1 -1
  264. package/dist/schemas/connectors.d.ts.map +1 -1
  265. package/dist/schemas/connectors.js +16 -11
  266. package/dist/schemas/connectors.js.map +1 -1
  267. package/dist/schemas/contacts.d.ts +57 -0
  268. package/dist/schemas/contacts.d.ts.map +1 -1
  269. package/dist/schemas/contacts.js +32 -0
  270. package/dist/schemas/contacts.js.map +1 -1
  271. package/dist/schemas/cron.d.ts.map +1 -1
  272. package/dist/schemas/cron.js +27 -28
  273. package/dist/schemas/cron.js.map +1 -1
  274. package/dist/schemas/defined.d.ts +6 -0
  275. package/dist/schemas/defined.d.ts.map +1 -0
  276. package/dist/schemas/defined.js +7 -0
  277. package/dist/schemas/defined.js.map +1 -0
  278. package/dist/schemas/guides.d.ts +3 -1
  279. package/dist/schemas/guides.d.ts.map +1 -1
  280. package/dist/schemas/guides.js +13 -12
  281. package/dist/schemas/guides.js.map +1 -1
  282. package/dist/schemas/index.d.ts +2 -2
  283. package/dist/schemas/index.d.ts.map +1 -1
  284. package/dist/schemas/index.js +18 -14
  285. package/dist/schemas/index.js.map +1 -1
  286. package/dist/session/account-hint.d.ts.map +1 -1
  287. package/dist/session/account-hint.js +8 -5
  288. package/dist/session/account-hint.js.map +1 -1
  289. package/dist/session/auth.d.ts +36 -2
  290. package/dist/session/auth.d.ts.map +1 -1
  291. package/dist/session/auth.js +95 -2
  292. package/dist/session/auth.js.map +1 -1
  293. package/dist/session/connect.d.ts +1 -0
  294. package/dist/session/connect.d.ts.map +1 -1
  295. package/dist/session/connect.js +3 -1
  296. package/dist/session/connect.js.map +1 -1
  297. package/dist/session/credentials.d.ts +8 -26
  298. package/dist/session/credentials.d.ts.map +1 -1
  299. package/dist/session/credentials.js +14 -32
  300. package/dist/session/credentials.js.map +1 -1
  301. package/dist/session/index.d.ts +1 -0
  302. package/dist/session/index.d.ts.map +1 -1
  303. package/dist/session/index.js +1 -0
  304. package/dist/session/index.js.map +1 -1
  305. package/dist/session/pairing.d.ts.map +1 -1
  306. package/dist/session/pairing.js +92 -88
  307. package/dist/session/pairing.js.map +1 -1
  308. package/dist/session/password-strength.d.ts.map +1 -1
  309. package/dist/session/password-strength.js +37 -37
  310. package/dist/session/password-strength.js.map +1 -1
  311. package/dist/session/places.d.ts.map +1 -1
  312. package/dist/session/places.js +18 -13
  313. package/dist/session/places.js.map +1 -1
  314. package/dist/session/popup.d.ts +13 -0
  315. package/dist/session/popup.d.ts.map +1 -0
  316. package/dist/session/popup.js +23 -0
  317. package/dist/session/popup.js.map +1 -0
  318. package/dist/space/constitution.d.ts.map +1 -1
  319. package/dist/space/constitution.js +22 -29
  320. package/dist/space/constitution.js.map +1 -1
  321. package/dist/space/grants.d.ts.map +1 -1
  322. package/dist/space/grants.js +37 -39
  323. package/dist/space/grants.js.map +1 -1
  324. package/dist/space/invite-link.d.ts.map +1 -1
  325. package/dist/space/invite-link.js +33 -33
  326. package/dist/space/invite-link.js.map +1 -1
  327. package/dist/space/own-automations.d.ts +13 -24
  328. package/dist/space/own-automations.d.ts.map +1 -1
  329. package/dist/space/own-automations.js +47 -37
  330. package/dist/space/own-automations.js.map +1 -1
  331. package/dist/space/roles.d.ts +56 -80
  332. package/dist/space/roles.d.ts.map +1 -1
  333. package/dist/space/roles.js +448 -441
  334. package/dist/space/roles.js.map +1 -1
  335. package/dist/space/space-access.d.ts +3 -4
  336. package/dist/space/space-access.d.ts.map +1 -1
  337. package/dist/space/space-access.js +3 -8
  338. package/dist/space/space-access.js.map +1 -1
  339. package/dist/storage/blob/directory.d.ts.map +1 -1
  340. package/dist/storage/blob/directory.js +39 -32
  341. package/dist/storage/blob/directory.js.map +1 -1
  342. package/dist/storage/blob/s3.d.ts.map +1 -1
  343. package/dist/storage/blob/s3.js +21 -19
  344. package/dist/storage/blob/s3.js.map +1 -1
  345. package/dist/storage/blob-store.d.ts +0 -9
  346. package/dist/storage/blob-store.d.ts.map +1 -1
  347. package/dist/storage/directory-access.d.ts +8 -32
  348. package/dist/storage/directory-access.d.ts.map +1 -1
  349. package/dist/storage/directory-access.js +11 -36
  350. package/dist/storage/directory-access.js.map +1 -1
  351. package/dist/storage/encrypted-adapter.d.ts +12 -32
  352. package/dist/storage/encrypted-adapter.d.ts.map +1 -1
  353. package/dist/storage/encrypted-adapter.js +31 -54
  354. package/dist/storage/encrypted-adapter.js.map +1 -1
  355. package/dist/storage/folder-adapter.d.ts +12 -26
  356. package/dist/storage/folder-adapter.d.ts.map +1 -1
  357. package/dist/storage/folder-adapter.js +37 -54
  358. package/dist/storage/folder-adapter.js.map +1 -1
  359. package/dist/storage/folder-reconcile.d.ts +12 -26
  360. package/dist/storage/folder-reconcile.d.ts.map +1 -1
  361. package/dist/storage/folder-reconcile.js +39 -48
  362. package/dist/storage/folder-reconcile.js.map +1 -1
  363. package/dist/storage/indexeddb-adapter.d.ts +2 -6
  364. package/dist/storage/indexeddb-adapter.d.ts.map +1 -1
  365. package/dist/storage/indexeddb-adapter.js +60 -114
  366. package/dist/storage/indexeddb-adapter.js.map +1 -1
  367. package/dist/storage/mirror.d.ts +0 -29
  368. package/dist/storage/mirror.d.ts.map +1 -1
  369. package/dist/storage/mirror.js +153 -110
  370. package/dist/storage/mirror.js.map +1 -1
  371. package/dist/storage/segment.d.ts.map +1 -1
  372. package/dist/storage/segment.js +3 -7
  373. package/dist/storage/segment.js.map +1 -1
  374. package/dist/storage/storage-provider.d.ts +6 -11
  375. package/dist/storage/storage-provider.d.ts.map +1 -1
  376. package/dist/storage/storage-provider.js +112 -116
  377. package/dist/storage/storage-provider.js.map +1 -1
  378. package/dist/sync/sync-engine.d.ts +10 -44
  379. package/dist/sync/sync-engine.d.ts.map +1 -1
  380. package/dist/sync/sync-engine.js +245 -247
  381. package/dist/sync/sync-engine.js.map +1 -1
  382. package/dist/sync/sync-messages.d.ts +1 -1
  383. package/dist/sync/sync-messages.d.ts.map +1 -1
  384. package/dist/sync/sync-messages.js +1 -1
  385. package/dist/sync/sync-messages.js.map +1 -1
  386. package/dist/utils/guards.d.ts +1 -0
  387. package/dist/utils/guards.d.ts.map +1 -1
  388. package/dist/utils/guards.js +1 -0
  389. package/dist/utils/guards.js.map +1 -1
  390. package/dist/utils/hmac.d.ts +5 -0
  391. package/dist/utils/hmac.d.ts.map +1 -0
  392. package/dist/utils/hmac.js +20 -0
  393. package/dist/utils/hmac.js.map +1 -0
  394. package/dist/utils/map.d.ts +10 -0
  395. package/dist/utils/map.d.ts.map +1 -0
  396. package/dist/utils/map.js +30 -0
  397. package/dist/utils/map.js.map +1 -0
  398. package/dist/utils/serial.d.ts +2 -0
  399. package/dist/utils/serial.d.ts.map +1 -1
  400. package/dist/utils/serial.js +9 -0
  401. package/dist/utils/serial.js.map +1 -1
  402. package/dist/validation/capability-gate.d.ts +7 -15
  403. package/dist/validation/capability-gate.d.ts.map +1 -1
  404. package/dist/validation/capability-gate.js +32 -36
  405. package/dist/validation/capability-gate.js.map +1 -1
  406. package/dist/validation/crypto-gate.d.ts.map +1 -1
  407. package/dist/validation/crypto-gate.js +12 -21
  408. package/dist/validation/crypto-gate.js.map +1 -1
  409. package/docs/README.md +1 -0
  410. package/docs/actions.md +1 -1
  411. package/docs/automations.md +1 -1
  412. package/docs/blueprints-and-guides.md +17 -1
  413. package/docs/building-an-app.md +7 -4
  414. package/docs/collections.md +3 -3
  415. package/docs/contacts.md +34 -1
  416. package/docs/contracts.md +1 -1
  417. package/docs/direct-messages.md +1 -1
  418. package/docs/inbox.md +5 -11
  419. package/docs/node.md +22 -14
  420. package/docs/records-and-queries.md +1 -1
  421. package/docs/sign-in.md +54 -26
  422. package/docs/spaces.md +6 -6
  423. package/docs/standard-library.md +7 -6
  424. package/docs/storage.md +4 -5
  425. package/docs/usernames.md +161 -0
  426. package/docs/warrants.md +1 -1
  427. package/package.json +1 -1
@@ -5,92 +5,42 @@ import { storedComputed, storedContract } from '../records/contract-builder.js';
5
5
  import { isRecord } from '../utils/guards.js';
6
6
  import { SESSION_COMMAND } from './node.js';
7
7
  import { API, buildNamespaces, callMethod, givenFor } from './api.js';
8
- /** A definition as plain data: its schema as JSON Schema, a contract or computed fields built in TypeScript as their CEL */
9
- function storedDefinition(definition) {
10
- const { contract: _built, computed: _derived, ...rest } = definition;
11
- const contract = storedContract(definition);
12
- const computed = storedComputed(definition);
13
- return {
14
- ...rest,
15
- schema: toJsonSchema(definition.schema),
16
- ...(contract !== undefined ? { contract } : {}),
17
- ...(computed !== undefined ? { computed } : {}),
18
- };
19
- }
20
8
  /** Tags every message, so a port shared with other traffic can tell ours apart */
21
9
  const TAG = 'minca-node';
10
+ const SIGNER_TAG = 'minca-signer';
22
11
  /** The node's own methods that cross; every namespace's do (`node/api.ts`) */
23
12
  const OWN_METHODS = Object.freeze({ iceServers: true, revokeKey: true, revokeSelf: true });
24
13
  /** How often the served side checks for a renewed delegation between calls */
25
14
  const DELEGATION_CHECK_MS = 60_000;
26
- function sendable(error) {
27
- if (!isRecord(error))
28
- return { name: 'Error', message: String(error) };
29
- return {
30
- name: typeof error.name === 'string' ? error.name : 'Error',
31
- message: typeof error.message === 'string' ? error.message : 'The node failed',
32
- ...(error.code !== undefined ? { code: error.code } : {}),
33
- };
34
- }
35
- function received(sent) {
36
- const fields = isRecord(sent) ? sent : {};
37
- const error = new Error(typeof fields.message === 'string' ? fields.message : 'The node failed');
38
- if (typeof fields.name === 'string')
39
- error.name = fields.name;
40
- if (fields.code !== undefined)
41
- Object.assign(error, { code: fields.code });
42
- return error;
43
- }
44
- const stateOf = (node) => ({
45
- did: node.did,
46
- sessionDid: node.sessionDid,
47
- delegation: node.delegation(),
48
- status: node.network.status(),
49
- });
50
- /** The method a call names, bound to its namespace, or null when it is not one that crosses */
51
- function methodAt(node, path) {
52
- const parts = path.split('.');
53
- const [first, second] = parts;
54
- if (first === undefined || parts.length > 2)
55
- return null;
56
- const listed = second === undefined ? OWN_METHODS : Object.hasOwn(API, first) ? Reflect.get(API, first) : null;
57
- const name = second ?? first;
58
- const owner = second === undefined ? node : Reflect.get(node, first);
59
- if (!isRecord(listed) || !Object.hasOwn(listed, name) || !isRecord(owner))
60
- return null;
61
- return (...args) => callMethod(owner, name, args);
62
- }
15
+ const CLOSED = 'That node has closed';
63
16
  /**
64
17
  * Answers for `node` over `port`, until the returned function is called.
65
18
  * Stopping leaves the node running: closing it is its owner's to do, or the
66
19
  * other side's, through `close()`.
67
20
  */
68
21
  export function serveNode(node, port) {
22
+ const post = (message) => send(port, TAG, message);
69
23
  const watches = new Map();
70
24
  const holds = new Map();
71
- let nextHold = 0;
72
- let sent = node.delegation().sig;
25
+ let lastHold = 0;
26
+ let sentSig = node.delegation().sig;
73
27
  let closed = false;
74
- const post = (message) => {
75
- port.postMessage({ [TAG]: true, ...message });
76
- };
77
- const sendState = () => {
28
+ function sendState() {
78
29
  const state = stateOf(node);
79
- sent = state.delegation.sig;
30
+ sentSig = state.delegation.sig;
80
31
  post({ kind: 'state', state });
81
- };
82
- /** Sends the delegation again when the node has renewed it */
83
- const checkDelegation = () => {
84
- if (node.delegation().sig !== sent)
32
+ }
33
+ function checkDelegation() {
34
+ if (node.delegation().sig !== sentSig)
85
35
  sendState();
86
- };
36
+ }
87
37
  const unsubscribe = node.subscribe((event) => {
88
38
  post({ kind: 'event', event });
89
39
  if (event.type === 'network')
90
40
  sendState();
91
41
  });
92
42
  const timer = setInterval(checkDelegation, DELEGATION_CHECK_MS);
93
- const stop = () => {
43
+ function stop() {
94
44
  clearInterval(timer);
95
45
  unsubscribe();
96
46
  for (const unwatch of watches.values())
@@ -99,77 +49,91 @@ export function serveNode(node, port) {
99
49
  for (const release of holds.values())
100
50
  void release().catch(() => { });
101
51
  holds.clear();
102
- };
103
- const run = async (path, args) => {
52
+ }
53
+ /** Calls that are not the node's own methods */
54
+ const special = new Map([
55
+ ['hello', async () => stateOf(node)],
56
+ [
57
+ 'hold',
58
+ async ([spaceId]) => {
59
+ const id = ++lastHold;
60
+ holds.set(id, await node.spaces.hold(String(spaceId)));
61
+ return id;
62
+ },
63
+ ],
64
+ [
65
+ 'release',
66
+ async ([held]) => {
67
+ const release = holds.get(Number(held));
68
+ holds.delete(Number(held));
69
+ await release?.();
70
+ },
71
+ ],
72
+ [
73
+ 'close',
74
+ async () => {
75
+ // Later calls are told it has closed.
76
+ closed = true;
77
+ stop();
78
+ await node.close();
79
+ },
80
+ ],
81
+ ]);
82
+ async function run(path, args) {
104
83
  if (closed)
105
- throw new Error('That node has closed');
106
- if (path === 'hello')
107
- return stateOf(node);
108
- if (path === 'hold') {
109
- const release = await node.spaces.hold(String(args[0]));
110
- const id = ++nextHold;
111
- holds.set(id, release);
112
- return id;
113
- }
114
- if (path === 'release') {
115
- const release = holds.get(Number(args[0]));
116
- holds.delete(Number(args[0]));
117
- await release?.();
118
- return undefined;
119
- }
120
- if (path === 'close') {
121
- // With the node closed there is nothing left to renew; later calls are told it has closed.
122
- closed = true;
123
- stop();
124
- await node.close();
125
- return undefined;
126
- }
127
- const method = methodAt(node, path);
84
+ throw new Error(CLOSED);
85
+ const method = special.get(path) ?? methodAt(node, path);
128
86
  if (!method)
129
87
  throw new Error(`A node has no method ${path}`);
130
- return await method(...args);
131
- };
132
- const onMessage = (event) => {
133
- const message = event.data;
134
- if (!isRecord(message) || message[TAG] !== true)
135
- return;
136
- if (message.kind === 'call' && typeof message.id === 'number' && typeof message.path === 'string') {
137
- const { id } = message;
138
- const args = Array.isArray(message.args) ? message.args : [];
139
- void run(message.path, args).then((value) => {
140
- try {
141
- post({ kind: 'reply', id, ok: true, value });
142
- }
143
- catch (error) {
144
- // A value that won't clone: say so, rather than leave the call waiting.
145
- post({ kind: 'reply', id, ok: false, error: sendable(error) });
146
- }
147
- checkDelegation();
148
- }, (error) => post({ kind: 'reply', id, ok: false, error: sendable(error) }));
88
+ return await method(args);
89
+ }
90
+ function reply(id, value) {
91
+ try {
92
+ post({ kind: 'reply', id, ok: true, value });
149
93
  }
150
- else if (message.kind === 'watch' &&
151
- typeof message.watch === 'number' &&
152
- typeof message.space === 'string') {
153
- const { watch } = message;
154
- if (!isRecord(message.query)) {
155
- post({ kind: 'result', watch, error: sendable(new Error('Nothing to watch')) });
156
- return;
157
- }
158
- const unwatch = node.records.watch(message.space,
159
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the query runner checks it and reports what's wrong through onError
160
- message.query, (result) => post({ kind: 'result', watch, result }), (error) => post({ kind: 'result', watch, error: sendable(error) }));
161
- watches.set(watch, unwatch);
94
+ catch (error) {
95
+ // A value that won't clone: say so, rather than leave the call waiting.
96
+ post({ kind: 'reply', id, ok: false, error: sendable(error) });
162
97
  }
163
- else if (message.kind === 'unwatch' && typeof message.watch === 'number') {
164
- watches.get(message.watch)?.();
165
- watches.delete(message.watch);
98
+ }
99
+ function onCall(message) {
100
+ const { id, path } = message;
101
+ if (typeof id !== 'number' || typeof path !== 'string')
102
+ return;
103
+ const args = Array.isArray(message.args) ? message.args : [];
104
+ void run(path, args).then((value) => {
105
+ reply(id, value);
106
+ checkDelegation();
107
+ }, (error) => post({ kind: 'reply', id, ok: false, error: sendable(error) }));
108
+ }
109
+ function onWatch(message) {
110
+ const { watch, space, query } = message;
111
+ if (typeof watch !== 'number' || typeof space !== 'string')
112
+ return;
113
+ if (!isRecord(query)) {
114
+ post({ kind: 'result', watch, error: sendable(new Error('Nothing to watch')) });
115
+ return;
166
116
  }
117
+ const unwatch = node.records.watch(space,
118
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the query runner checks it and reports what's wrong through onError
119
+ query, (result) => post({ kind: 'result', watch, result }), (error) => post({ kind: 'result', watch, error: sendable(error) }));
120
+ watches.set(watch, unwatch);
121
+ }
122
+ function onUnwatch({ watch }) {
123
+ if (typeof watch !== 'number')
124
+ return;
125
+ watches.get(watch)?.();
126
+ watches.delete(watch);
127
+ }
128
+ const handlers = {
129
+ call: onCall,
130
+ watch: onWatch,
131
+ unwatch: onUnwatch,
167
132
  };
168
- port.addEventListener('message', onMessage);
169
- port.start?.();
133
+ const stopListening = listen(port, TAG, (message) => handlerFor(handlers, message)?.(message));
170
134
  return () => {
171
135
  stop();
172
- port.removeEventListener('message', onMessage);
136
+ stopListening();
173
137
  };
174
138
  }
175
139
  /**
@@ -177,31 +141,21 @@ export function serveNode(node, port) {
177
141
  * each call a message. Resolves once the node has said who it is.
178
142
  */
179
143
  export async function remoteNode(port) {
180
- const pending = new Map();
144
+ const post = (message) => send(port, TAG, message);
145
+ const calls = createCalls();
181
146
  const listeners = new Set();
182
- let state = null;
183
147
  const watchers = new Map();
148
+ let lastWatch = 0;
149
+ let state = null;
184
150
  /** Closed from this side: calls fail at once */
185
151
  let closed = false;
186
- let nextId = 0;
187
- const post = (message) => {
188
- port.postMessage({ [TAG]: true, ...message });
189
- };
190
- const onMessage = (event) => {
191
- const message = event.data;
192
- if (!isRecord(message) || message[TAG] !== true)
193
- return;
194
- if (message.kind === 'reply' && typeof message.id === 'number') {
195
- const waiting = pending.get(message.id);
196
- pending.delete(message.id);
197
- if (message.ok === true)
198
- waiting?.resolve(message.value);
199
- else
200
- waiting?.reject(received(message.error));
201
- }
202
- else if (message.kind === 'event' && isRecord(message.event)) {
152
+ const handlers = {
153
+ reply: calls.settle,
154
+ event({ event }) {
155
+ if (!isRecord(event))
156
+ return;
203
157
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the served node sent it from its own subscribe
204
- const nodeEvent = message.event;
158
+ const nodeEvent = event;
205
159
  for (const listener of listeners) {
206
160
  try {
207
161
  listener(nodeEvent);
@@ -210,46 +164,46 @@ export async function remoteNode(port) {
210
164
  console.error(error);
211
165
  }
212
166
  }
213
- }
214
- else if (message.kind === 'state' && isRecord(message.state)) {
167
+ },
168
+ state(message) {
215
169
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the served node sent it from its own state
216
- state = message.state;
217
- }
218
- else if (message.kind === 'result' && typeof message.watch === 'number') {
219
- const watcher = watchers.get(message.watch);
170
+ if (isRecord(message.state))
171
+ state = message.state;
172
+ },
173
+ result(message) {
174
+ const watcher = typeof message.watch === 'number' ? watchers.get(message.watch) : undefined;
220
175
  if (!watcher)
221
176
  return;
222
177
  if (message.error !== undefined)
223
178
  watcher.onError(received(message.error));
224
179
  else
225
180
  watcher.onResult(message.result);
226
- }
181
+ },
227
182
  };
228
- port.addEventListener('message', onMessage);
229
- port.start?.();
183
+ const stopListening = listen(port, TAG, (message) => handlerFor(handlers, message)?.(message));
230
184
  /** Calls a method of the node on the other end; it answers with that method's type */
231
- const ask = (path, args) => new Promise((resolve, reject) => {
232
- if (closed) {
233
- reject(new Error('That node has closed'));
234
- return;
235
- }
236
- const id = ++nextId;
237
- pending.set(id, {
238
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the served node ran the method this one stands for
239
- resolve: (value) => resolve(value),
240
- reject,
185
+ const ask = (path, args) => closed
186
+ ? Promise.reject(new Error(CLOSED))
187
+ : calls.call((id) => post({ kind: 'call', id, path, args }));
188
+ function watch(spaceId, query, onResult, onError) {
189
+ const id = ++lastWatch;
190
+ watchers.set(id, {
191
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the served node ran this query
192
+ onResult: (result) => onResult(result),
193
+ onError: onError ?? ((error) => console.error(error)),
241
194
  });
242
195
  try {
243
- post({ kind: 'call', id, path, args });
196
+ post({ kind: 'watch', watch: id, space: spaceId, query: plainQuery(query) });
244
197
  }
245
198
  catch (error) {
246
- // An argument that won't clone, a function say.
247
- pending.delete(id);
248
- reject(error instanceof Error ? error : new Error(String(error)));
199
+ watchers.delete(id);
200
+ throw error;
249
201
  }
250
- });
251
- /** The name a typed collection reference stands for; a validator can't be cloned */
252
- const collectionName = (ref) => isRecord(ref) && typeof ref.name === 'string' ? ref.name : ref;
202
+ return () => {
203
+ if (watchers.delete(id))
204
+ post({ kind: 'unwatch', watch: id });
205
+ };
206
+ }
253
207
  const initial = await ask('hello', []);
254
208
  const current = () => state ?? initial;
255
209
  // Every method is asked of the other end, except these.
@@ -265,33 +219,10 @@ export async function remoteNode(port) {
265
219
  // Either overload: the record comes back typed as the call asked.
266
220
  put: (...args) => ask('records.put', [args[0], collectionName(args[1]), ...args.slice(2)]),
267
221
  query: (spaceId, query) => ask('records.query', [spaceId, plainQuery(query)]),
268
- watch: (spaceId, query, onResult, onError) => {
269
- const watch = ++nextId;
270
- watchers.set(watch, {
271
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the served node ran this query
272
- onResult: (result) => onResult(result),
273
- onError: onError ?? ((error) => console.error(error)),
274
- });
275
- try {
276
- post({ kind: 'watch', watch, space: spaceId, query: plainQuery(query) });
277
- }
278
- catch (error) {
279
- watchers.delete(watch);
280
- throw error;
281
- }
282
- return () => {
283
- if (!watchers.delete(watch))
284
- return;
285
- post({ kind: 'unwatch', watch });
286
- };
287
- },
222
+ watch,
288
223
  },
289
224
  collections: {
290
- define: async (spaceId, definition) => ask('collections.define', [
291
- spaceId,
292
- // Plain data: a contract or computed fields built in TypeScript are functions, which can't be posted.
293
- storedDefinition(definition),
294
- ]),
225
+ define: async (spaceId, definition) => ask('collections.define', [spaceId, storedDefinition(definition)]),
295
226
  },
296
227
  network: {
297
228
  status: () => current().status,
@@ -322,90 +253,171 @@ export async function remoteNode(port) {
322
253
  await ask('close', []);
323
254
  closed = true;
324
255
  listeners.clear();
325
- port.removeEventListener('message', onMessage);
326
- for (const waiting of pending.values())
327
- waiting.reject(new Error('That node has closed'));
328
- pending.clear();
256
+ stopListening();
257
+ calls.rejectAll(new Error(CLOSED));
329
258
  watchers.clear();
330
259
  },
331
260
  });
332
261
  }
333
- const SIGNER_TAG = 'minca-signer';
334
262
  /**
335
- * Signs session notes for a `remoteSigner` on the other side of `port`: the
336
- * one note a node asks for, everything about the account (`SESSION_COMMAND`)
337
- * to its key until a time. No other shape: the far end still picks the key and
338
- * the time, as the node it runs does. The root key stays here: a
339
- * passkey needs a document, and a key in a tab should stay in the tab that
340
- * holds it. Returns a function that stops.
263
+ * Signs session notes for a `remoteSigner` on the other side of `port`: only
264
+ * the one note a node asks for, everything about the account
265
+ * (`SESSION_COMMAND`) to its key until a time. The root key stays here: a
266
+ * passkey needs a document, and a key in a tab should stay in that tab.
267
+ * Returns a function that stops.
341
268
  */
342
269
  export function serveSigner(signer, port) {
343
- const post = (message) => {
344
- port.postMessage({ [SIGNER_TAG]: true, ...message });
345
- };
346
- const onMessage = (event) => {
347
- const message = event.data;
348
- if (!isRecord(message) || message[SIGNER_TAG] !== true || typeof message.id !== 'number')
349
- return;
270
+ async function answer(message) {
271
+ if (message.kind === 'hello')
272
+ return { did: signer.did };
273
+ const { params } = message;
274
+ if (message.kind !== 'delegate' || !isSessionRequest(params))
275
+ throw new Error('A signer across a port signs only session notes');
276
+ return signer.delegate({
277
+ audience: params.audience,
278
+ command: SESSION_COMMAND,
279
+ expiration: params.expiration,
280
+ });
281
+ }
282
+ return listen(port, SIGNER_TAG, (message) => {
350
283
  const { id } = message;
351
- const run = async () => {
352
- if (message.kind === 'hello')
353
- return { did: signer.did };
354
- const params = message.params;
355
- if (message.kind !== 'delegate' ||
356
- !isRecord(params) ||
357
- typeof params.audience !== 'string' ||
358
- typeof params.expiration !== 'number' ||
359
- params.command !== SESSION_COMMAND ||
360
- params.subject !== undefined ||
361
- params.policy !== undefined)
362
- throw new Error('A signer across a port signs only session notes');
363
- return signer.delegate({
364
- audience: params.audience,
365
- command: SESSION_COMMAND,
366
- expiration: params.expiration,
367
- });
368
- };
369
- void run().then((value) => post({ kind: 'reply', id, ok: true, value }), (error) => post({ kind: 'reply', id, ok: false, error: sendable(error) }));
370
- };
371
- port.addEventListener('message', onMessage);
372
- port.start?.();
373
- return () => port.removeEventListener('message', onMessage);
284
+ if (typeof id !== 'number')
285
+ return;
286
+ void answer(message).then((value) => send(port, SIGNER_TAG, { kind: 'reply', id, ok: true, value }), (error) => send(port, SIGNER_TAG, { kind: 'reply', id, ok: false, error: sendable(error) }));
287
+ });
374
288
  }
375
289
  /** The signer a `serveSigner` keeps on the other side of `port`, for `createNode` to sign sessions with */
376
290
  export async function remoteSigner(port) {
377
- const pending = new Map();
378
- let nextId = 0;
379
- port.addEventListener('message', (event) => {
380
- const message = event.data;
381
- if (!isRecord(message) || message[SIGNER_TAG] !== true || message.kind !== 'reply')
382
- return;
383
- const waiting = pending.get(Number(message.id));
384
- pending.delete(Number(message.id));
385
- if (message.ok === true)
386
- waiting?.resolve(message.value);
387
- else
388
- waiting?.reject(received(message.error));
389
- });
390
- port.start?.();
391
- const ask = (message) => new Promise((resolve, reject) => {
392
- const id = ++nextId;
393
- pending.set(id, {
394
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the signer on the other side answered this call
395
- resolve: (value) => resolve(value),
396
- reject,
397
- });
398
- port.postMessage({ [SIGNER_TAG]: true, kind: message.kind, params: message.params, id });
291
+ const calls = createCalls();
292
+ listen(port, SIGNER_TAG, (message) => {
293
+ if (message.kind === 'reply')
294
+ calls.settle(message);
399
295
  });
400
- const { did } = await ask({ kind: 'hello' });
296
+ const ask = (kind, params) => calls.call((id) => send(port, SIGNER_TAG, { kind, params, id }));
297
+ const { did } = await ask('hello');
401
298
  return Object.freeze({
402
299
  did,
403
300
  delegate: async (params) => {
404
- const token = parseDelegation(await ask({ kind: 'delegate', params }));
301
+ const token = parseDelegation(await ask('delegate', params));
405
302
  if (!token)
406
303
  throw new Error('The signer answered with something that is not a delegation');
407
304
  return token;
408
305
  },
409
306
  });
410
307
  }
308
+ /** A session note's request, and nothing else: the far end still picks the key and the time */
309
+ function isSessionRequest(params) {
310
+ return (isRecord(params) &&
311
+ typeof params.audience === 'string' &&
312
+ typeof params.expiration === 'number' &&
313
+ params.command === SESSION_COMMAND &&
314
+ params.subject === undefined &&
315
+ params.policy === undefined);
316
+ }
317
+ /** A definition as plain data: its schema as JSON Schema, a contract or computed fields built in TypeScript as their CEL */
318
+ function storedDefinition(definition) {
319
+ const { contract: _built, computed: _derived, ...rest } = definition;
320
+ const contract = storedContract(definition);
321
+ const computed = storedComputed(definition);
322
+ return {
323
+ ...rest,
324
+ schema: toJsonSchema(definition.schema),
325
+ ...(contract !== undefined ? { contract } : {}),
326
+ ...(computed !== undefined ? { computed } : {}),
327
+ };
328
+ }
329
+ /** The name a typed collection reference stands for; a validator can't be cloned */
330
+ const collectionName = (ref) => isRecord(ref) && typeof ref.name === 'string' ? ref.name : ref;
331
+ const stateOf = (node) => ({
332
+ did: node.did,
333
+ sessionDid: node.sessionDid,
334
+ delegation: node.delegation(),
335
+ status: node.network.status(),
336
+ });
337
+ /** The method a call names, bound to its namespace, or null when it is not one that crosses */
338
+ function methodAt(node, path) {
339
+ const parts = path.split('.');
340
+ const [first, second] = parts;
341
+ if (first === undefined || parts.length > 2)
342
+ return null;
343
+ const listed = second === undefined ? OWN_METHODS : Object.hasOwn(API, first) ? Reflect.get(API, first) : null;
344
+ const name = second ?? first;
345
+ const owner = second === undefined ? node : Reflect.get(node, first);
346
+ if (!isRecord(listed) || !Object.hasOwn(listed, name) || !isRecord(owner))
347
+ return null;
348
+ return (args) => callMethod(owner, name, args);
349
+ }
350
+ function handlerFor(handlers, message) {
351
+ const { kind } = message;
352
+ return typeof kind === 'string' && Object.hasOwn(handlers, kind) ? handlers[kind] : undefined;
353
+ }
354
+ // Messages: tagged, so a port shared with other traffic can tell ours apart.
355
+ function send(port, tag, message) {
356
+ port.postMessage({ [tag]: true, ...message });
357
+ }
358
+ /** Hears the messages tagged `tag` on `port`; returns a function that stops */
359
+ function listen(port, tag, handle) {
360
+ const onMessage = (event) => {
361
+ const message = event.data;
362
+ if (isRecord(message) && message[tag] === true)
363
+ handle(message);
364
+ };
365
+ port.addEventListener('message', onMessage);
366
+ port.start?.();
367
+ return () => port.removeEventListener('message', onMessage);
368
+ }
369
+ /** Calls sent across a port, waiting for their replies by id */
370
+ function createCalls() {
371
+ const waiting = new Map();
372
+ let lastId = 0;
373
+ return {
374
+ /** Sends a call with `post`; resolves with its reply's value */
375
+ call: (post) => new Promise((resolve, reject) => {
376
+ const id = ++lastId;
377
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the other side ran the call this one stands for
378
+ waiting.set(id, { resolve: (value) => resolve(value), reject });
379
+ try {
380
+ post(id);
381
+ }
382
+ catch (error) {
383
+ // An argument that won't clone, a function say.
384
+ waiting.delete(id);
385
+ reject(error instanceof Error ? error : new Error(String(error)));
386
+ }
387
+ }),
388
+ settle: (message) => {
389
+ const id = Number(message.id);
390
+ const call = waiting.get(id);
391
+ waiting.delete(id);
392
+ if (message.ok === true)
393
+ call?.resolve(message.value);
394
+ else
395
+ call?.reject(received(message.error));
396
+ },
397
+ rejectAll: (error) => {
398
+ for (const call of waiting.values())
399
+ call.reject(error);
400
+ waiting.clear();
401
+ },
402
+ };
403
+ }
404
+ // Errors cross as plain fields.
405
+ function sendable(error) {
406
+ if (!isRecord(error))
407
+ return { name: 'Error', message: String(error) };
408
+ return {
409
+ name: typeof error.name === 'string' ? error.name : 'Error',
410
+ message: typeof error.message === 'string' ? error.message : 'The node failed',
411
+ ...(error.code !== undefined ? { code: error.code } : {}),
412
+ };
413
+ }
414
+ function received(sent) {
415
+ const fields = isRecord(sent) ? sent : {};
416
+ const error = new Error(typeof fields.message === 'string' ? fields.message : 'The node failed');
417
+ if (typeof fields.name === 'string')
418
+ error.name = fields.name;
419
+ if (fields.code !== undefined)
420
+ Object.assign(error, { code: fields.code });
421
+ return error;
422
+ }
411
423
  //# sourceMappingURL=remote.js.map