@pkcprotocol/pkc-js 0.0.11

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 (843) hide show
  1. package/LICENSE +339 -0
  2. package/README.md +1663 -0
  3. package/dist/browser/challenges.d.ts +1 -0
  4. package/dist/browser/challenges.js +2 -0
  5. package/dist/browser/challenges.js.map +1 -0
  6. package/dist/browser/clients/base-client-manager.d.ts +126 -0
  7. package/dist/browser/clients/base-client-manager.js +673 -0
  8. package/dist/browser/clients/base-client-manager.js.map +1 -0
  9. package/dist/browser/clients/name-resolver-client.d.ts +8 -0
  10. package/dist/browser/clients/name-resolver-client.js +10 -0
  11. package/dist/browser/clients/name-resolver-client.js.map +1 -0
  12. package/dist/browser/clients/pkc-typed-emitter.d.ts +9 -0
  13. package/dist/browser/clients/pkc-typed-emitter.js +52 -0
  14. package/dist/browser/clients/pkc-typed-emitter.js.map +1 -0
  15. package/dist/browser/clients/rpc-client/decode-rpc-response-util.d.ts +8 -0
  16. package/dist/browser/clients/rpc-client/decode-rpc-response-util.js +53 -0
  17. package/dist/browser/clients/rpc-client/decode-rpc-response-util.js.map +1 -0
  18. package/dist/browser/clients/rpc-client/pkc-rpc-client.d.ts +68 -0
  19. package/dist/browser/clients/rpc-client/pkc-rpc-client.js +404 -0
  20. package/dist/browser/clients/rpc-client/pkc-rpc-client.js.map +1 -0
  21. package/dist/browser/clients/rpc-client/rpc-schema-util.d.ts +147 -0
  22. package/dist/browser/clients/rpc-client/rpc-schema-util.js +11 -0
  23. package/dist/browser/clients/rpc-client/rpc-schema-util.js.map +1 -0
  24. package/dist/browser/clients/rpc-client/schema.d.ts +433 -0
  25. package/dist/browser/clients/rpc-client/schema.js +49 -0
  26. package/dist/browser/clients/rpc-client/schema.js.map +1 -0
  27. package/dist/browser/clients/rpc-client/types.d.ts +8 -0
  28. package/dist/browser/clients/rpc-client/types.js +2 -0
  29. package/dist/browser/clients/rpc-client/types.js.map +1 -0
  30. package/dist/browser/community/community-client-manager.d.ts +60 -0
  31. package/dist/browser/community/community-client-manager.js +717 -0
  32. package/dist/browser/community/community-client-manager.js.map +1 -0
  33. package/dist/browser/community/community-clients.d.ts +18 -0
  34. package/dist/browser/community/community-clients.js +12 -0
  35. package/dist/browser/community/community-clients.js.map +1 -0
  36. package/dist/browser/community/community-wire.d.ts +20 -0
  37. package/dist/browser/community/community-wire.js +38 -0
  38. package/dist/browser/community/community-wire.js.map +1 -0
  39. package/dist/browser/community/remote-community.d.ts +110 -0
  40. package/dist/browser/community/remote-community.js +555 -0
  41. package/dist/browser/community/remote-community.js.map +1 -0
  42. package/dist/browser/community/rpc-local-community.d.ts +41 -0
  43. package/dist/browser/community/rpc-local-community.js +289 -0
  44. package/dist/browser/community/rpc-local-community.js.map +1 -0
  45. package/dist/browser/community/rpc-remote-community.d.ts +18 -0
  46. package/dist/browser/community/rpc-remote-community.js +286 -0
  47. package/dist/browser/community/rpc-remote-community.js.map +1 -0
  48. package/dist/browser/community/schema.d.ts +4217 -0
  49. package/dist/browser/community/schema.js +289 -0
  50. package/dist/browser/community/schema.js.map +1 -0
  51. package/dist/browser/community/types.d.ts +135 -0
  52. package/dist/browser/community/types.js +2 -0
  53. package/dist/browser/community/types.js.map +1 -0
  54. package/dist/browser/constants.d.ts +6 -0
  55. package/dist/browser/constants.js +9 -0
  56. package/dist/browser/constants.js.map +1 -0
  57. package/dist/browser/decorator-util.d.ts +1 -0
  58. package/dist/browser/decorator-util.js +35 -0
  59. package/dist/browser/decorator-util.js.map +1 -0
  60. package/dist/browser/errors.d.ts +343 -0
  61. package/dist/browser/errors.js +358 -0
  62. package/dist/browser/errors.js.map +1 -0
  63. package/dist/browser/general-util/limited-set.d.ts +15 -0
  64. package/dist/browser/general-util/limited-set.js +66 -0
  65. package/dist/browser/general-util/limited-set.js.map +1 -0
  66. package/dist/browser/generated-version.d.ts +1 -0
  67. package/dist/browser/generated-version.js +3 -0
  68. package/dist/browser/generated-version.js.map +1 -0
  69. package/dist/browser/generic-state-client.d.ts +6 -0
  70. package/dist/browser/generic-state-client.js +11 -0
  71. package/dist/browser/generic-state-client.js.map +1 -0
  72. package/dist/browser/helia/helia-for-pkc.d.ts +3 -0
  73. package/dist/browser/helia/helia-for-pkc.js +255 -0
  74. package/dist/browser/helia/helia-for-pkc.js.map +1 -0
  75. package/dist/browser/helia/ipns-over-pubsub-with-fetch.d.ts +36 -0
  76. package/dist/browser/helia/ipns-over-pubsub-with-fetch.js +229 -0
  77. package/dist/browser/helia/ipns-over-pubsub-with-fetch.js.map +1 -0
  78. package/dist/browser/helia/libp2pjsClient.d.ts +27 -0
  79. package/dist/browser/helia/libp2pjsClient.js +15 -0
  80. package/dist/browser/helia/libp2pjsClient.js.map +1 -0
  81. package/dist/browser/helia/types.d.ts +19 -0
  82. package/dist/browser/helia/types.js +2 -0
  83. package/dist/browser/helia/types.js.map +1 -0
  84. package/dist/browser/helia/util.d.ts +13 -0
  85. package/dist/browser/helia/util.js +98 -0
  86. package/dist/browser/helia/util.js.map +1 -0
  87. package/dist/browser/index.d.ts +244 -0
  88. package/dist/browser/index.js +36 -0
  89. package/dist/browser/index.js.map +1 -0
  90. package/dist/browser/logger.d.ts +12 -0
  91. package/dist/browser/logger.js +11 -0
  92. package/dist/browser/logger.js.map +1 -0
  93. package/dist/browser/pages/pages-client-manager.d.ts +159 -0
  94. package/dist/browser/pages/pages-client-manager.js +334 -0
  95. package/dist/browser/pages/pages-client-manager.js.map +1 -0
  96. package/dist/browser/pages/pages-clients.d.ts +11 -0
  97. package/dist/browser/pages/pages-clients.js +10 -0
  98. package/dist/browser/pages/pages-clients.js.map +1 -0
  99. package/dist/browser/pages/pages.d.ts +107 -0
  100. package/dist/browser/pages/pages.js +262 -0
  101. package/dist/browser/pages/pages.js.map +1 -0
  102. package/dist/browser/pages/schema-util.d.ts +3 -0
  103. package/dist/browser/pages/schema-util.js +3 -0
  104. package/dist/browser/pages/schema-util.js.map +1 -0
  105. package/dist/browser/pages/schema.d.ts +719 -0
  106. package/dist/browser/pages/schema.js +32 -0
  107. package/dist/browser/pages/schema.js.map +1 -0
  108. package/dist/browser/pages/types.d.ts +44 -0
  109. package/dist/browser/pages/types.js +2 -0
  110. package/dist/browser/pages/types.js.map +1 -0
  111. package/dist/browser/pages/util.d.ts +56 -0
  112. package/dist/browser/pages/util.js +446 -0
  113. package/dist/browser/pages/util.js.map +1 -0
  114. package/dist/browser/pkc/pkc-client-manager.d.ts +44 -0
  115. package/dist/browser/pkc/pkc-client-manager.js +156 -0
  116. package/dist/browser/pkc/pkc-client-manager.js.map +1 -0
  117. package/dist/browser/pkc/pkc-clients.d.ts +11 -0
  118. package/dist/browser/pkc/pkc-clients.js +8 -0
  119. package/dist/browser/pkc/pkc-clients.js.map +1 -0
  120. package/dist/browser/pkc/pkc-with-rpc-client.d.ts +19 -0
  121. package/dist/browser/pkc/pkc-with-rpc-client.js +128 -0
  122. package/dist/browser/pkc/pkc-with-rpc-client.js.map +1 -0
  123. package/dist/browser/pkc/pkc.d.ts +137 -0
  124. package/dist/browser/pkc/pkc.js +888 -0
  125. package/dist/browser/pkc/pkc.js.map +1 -0
  126. package/dist/browser/pkc/tracked-instance-registry-util.d.ts +44 -0
  127. package/dist/browser/pkc/tracked-instance-registry-util.js +106 -0
  128. package/dist/browser/pkc/tracked-instance-registry-util.js.map +1 -0
  129. package/dist/browser/pkc/tracked-instance-registry.d.ts +18 -0
  130. package/dist/browser/pkc/tracked-instance-registry.js +134 -0
  131. package/dist/browser/pkc/tracked-instance-registry.js.map +1 -0
  132. package/dist/browser/pkc-error.d.ts +65 -0
  133. package/dist/browser/pkc-error.js +137 -0
  134. package/dist/browser/pkc-error.js.map +1 -0
  135. package/dist/browser/publications/comment/comment-client-manager.d.ts +86 -0
  136. package/dist/browser/publications/comment/comment-client-manager.js +908 -0
  137. package/dist/browser/publications/comment/comment-client-manager.js.map +1 -0
  138. package/dist/browser/publications/comment/comment-clients.d.ts +19 -0
  139. package/dist/browser/publications/comment/comment-clients.js +12 -0
  140. package/dist/browser/publications/comment/comment-clients.js.map +1 -0
  141. package/dist/browser/publications/comment/comment-util.d.ts +10 -0
  142. package/dist/browser/publications/comment/comment-util.js +202 -0
  143. package/dist/browser/publications/comment/comment-util.js.map +1 -0
  144. package/dist/browser/publications/comment/comment.d.ts +147 -0
  145. package/dist/browser/publications/comment/comment.js +1044 -0
  146. package/dist/browser/publications/comment/comment.js.map +1 -0
  147. package/dist/browser/publications/comment/schema.d.ts +1237 -0
  148. package/dist/browser/publications/comment/schema.js +184 -0
  149. package/dist/browser/publications/comment/schema.js.map +1 -0
  150. package/dist/browser/publications/comment/types.d.ts +100 -0
  151. package/dist/browser/publications/comment/types.js +2 -0
  152. package/dist/browser/publications/comment/types.js.map +1 -0
  153. package/dist/browser/publications/comment-edit/comment-edit.d.ts +41 -0
  154. package/dist/browser/publications/comment-edit/comment-edit.js +63 -0
  155. package/dist/browser/publications/comment-edit/comment-edit.js.map +1 -0
  156. package/dist/browser/publications/comment-edit/schema.d.ts +295 -0
  157. package/dist/browser/publications/comment-edit/schema.js +55 -0
  158. package/dist/browser/publications/comment-edit/schema.js.map +1 -0
  159. package/dist/browser/publications/comment-edit/types.d.ts +25 -0
  160. package/dist/browser/publications/comment-edit/types.js +2 -0
  161. package/dist/browser/publications/comment-edit/types.js.map +1 -0
  162. package/dist/browser/publications/comment-moderation/comment-moderation.d.ts +36 -0
  163. package/dist/browser/publications/comment-moderation/comment-moderation.js +53 -0
  164. package/dist/browser/publications/comment-moderation/comment-moderation.js.map +1 -0
  165. package/dist/browser/publications/comment-moderation/schema.d.ts +315 -0
  166. package/dist/browser/publications/comment-moderation/schema.js +62 -0
  167. package/dist/browser/publications/comment-moderation/schema.js.map +1 -0
  168. package/dist/browser/publications/comment-moderation/types.d.ts +22 -0
  169. package/dist/browser/publications/comment-moderation/types.js +2 -0
  170. package/dist/browser/publications/comment-moderation/types.js.map +1 -0
  171. package/dist/browser/publications/community-edit/community-edit.d.ts +35 -0
  172. package/dist/browser/publications/community-edit/community-edit.js +50 -0
  173. package/dist/browser/publications/community-edit/community-edit.js.map +1 -0
  174. package/dist/browser/publications/community-edit/schema.d.ts +467 -0
  175. package/dist/browser/publications/community-edit/schema.js +36 -0
  176. package/dist/browser/publications/community-edit/schema.js.map +1 -0
  177. package/dist/browser/publications/community-edit/types.d.ts +19 -0
  178. package/dist/browser/publications/community-edit/types.js +2 -0
  179. package/dist/browser/publications/community-edit/types.js.map +1 -0
  180. package/dist/browser/publications/publication-author.d.ts +22 -0
  181. package/dist/browser/publications/publication-author.js +66 -0
  182. package/dist/browser/publications/publication-author.js.map +1 -0
  183. package/dist/browser/publications/publication-client-manager.d.ts +62 -0
  184. package/dist/browser/publications/publication-client-manager.js +257 -0
  185. package/dist/browser/publications/publication-client-manager.js.map +1 -0
  186. package/dist/browser/publications/publication-clients.d.ts +19 -0
  187. package/dist/browser/publications/publication-clients.js +12 -0
  188. package/dist/browser/publications/publication-clients.js.map +1 -0
  189. package/dist/browser/publications/publication-community.d.ts +55 -0
  190. package/dist/browser/publications/publication-community.js +87 -0
  191. package/dist/browser/publications/publication-community.js.map +1 -0
  192. package/dist/browser/publications/publication.d.ts +120 -0
  193. package/dist/browser/publications/publication.js +950 -0
  194. package/dist/browser/publications/publication.js.map +1 -0
  195. package/dist/browser/publications/types.d.ts +26 -0
  196. package/dist/browser/publications/types.js +2 -0
  197. package/dist/browser/publications/types.js.map +1 -0
  198. package/dist/browser/publications/vote/schema.d.ts +150 -0
  199. package/dist/browser/publications/vote/schema.js +44 -0
  200. package/dist/browser/publications/vote/schema.js.map +1 -0
  201. package/dist/browser/publications/vote/types.d.ts +21 -0
  202. package/dist/browser/publications/vote/types.js +2 -0
  203. package/dist/browser/publications/vote/types.js.map +1 -0
  204. package/dist/browser/publications/vote/vote.d.ts +36 -0
  205. package/dist/browser/publications/vote/vote.js +49 -0
  206. package/dist/browser/publications/vote/vote.js.map +1 -0
  207. package/dist/browser/pubsub-messages/schema.d.ts +964 -0
  208. package/dist/browser/pubsub-messages/schema.js +98 -0
  209. package/dist/browser/pubsub-messages/schema.js.map +1 -0
  210. package/dist/browser/pubsub-messages/types.d.ts +81 -0
  211. package/dist/browser/pubsub-messages/types.js +2 -0
  212. package/dist/browser/pubsub-messages/types.js.map +1 -0
  213. package/dist/browser/rpc/src/index.d.ts +483 -0
  214. package/dist/browser/rpc/src/index.js +1267 -0
  215. package/dist/browser/rpc/src/index.js.map +1 -0
  216. package/dist/browser/rpc/src/json-rpc-util.d.ts +1 -0
  217. package/dist/browser/rpc/src/json-rpc-util.js +19 -0
  218. package/dist/browser/rpc/src/json-rpc-util.js.map +1 -0
  219. package/dist/browser/rpc/src/lib/pkc-js/index.d.ts +132 -0
  220. package/dist/browser/rpc/src/lib/pkc-js/index.js +29 -0
  221. package/dist/browser/rpc/src/lib/pkc-js/index.js.map +1 -0
  222. package/dist/browser/rpc/src/lib/pkc-js/pkc-js-mock.d.ts +1 -0
  223. package/dist/browser/rpc/src/lib/pkc-js/pkc-js-mock.js +472 -0
  224. package/dist/browser/rpc/src/lib/pkc-js/pkc-js-mock.js.map +1 -0
  225. package/dist/browser/rpc/src/schema.d.ts +843 -0
  226. package/dist/browser/rpc/src/schema.js +28 -0
  227. package/dist/browser/rpc/src/schema.js.map +1 -0
  228. package/dist/browser/rpc/src/types.d.ts +24 -0
  229. package/dist/browser/rpc/src/types.js +2 -0
  230. package/dist/browser/rpc/src/types.js.map +1 -0
  231. package/dist/browser/rpc/src/utils.d.ts +7 -0
  232. package/dist/browser/rpc/src/utils.js +58 -0
  233. package/dist/browser/rpc/src/utils.js.map +1 -0
  234. package/dist/browser/runtime/browser/community/challenges/index.d.ts +6 -0
  235. package/dist/browser/runtime/browser/community/challenges/index.js +7 -0
  236. package/dist/browser/runtime/browser/community/challenges/index.js.map +1 -0
  237. package/dist/browser/runtime/browser/community/local-community.d.ts +3 -0
  238. package/dist/browser/runtime/browser/community/local-community.js +6 -0
  239. package/dist/browser/runtime/browser/community/local-community.js.map +1 -0
  240. package/dist/browser/runtime/browser/db-handler.d.ts +4 -0
  241. package/dist/browser/runtime/browser/db-handler.js +8 -0
  242. package/dist/browser/runtime/browser/db-handler.js.map +1 -0
  243. package/dist/browser/runtime/browser/localforage-lru.d.ts +15 -0
  244. package/dist/browser/runtime/browser/localforage-lru.js +140 -0
  245. package/dist/browser/runtime/browser/localforage-lru.js.map +1 -0
  246. package/dist/browser/runtime/browser/lru-storage.d.ts +14 -0
  247. package/dist/browser/runtime/browser/lru-storage.js +34 -0
  248. package/dist/browser/runtime/browser/lru-storage.js.map +1 -0
  249. package/dist/browser/runtime/browser/native-functions.d.ts +3 -0
  250. package/dist/browser/runtime/browser/native-functions.js +6 -0
  251. package/dist/browser/runtime/browser/native-functions.js.map +1 -0
  252. package/dist/browser/runtime/browser/polyfill.d.ts +3 -0
  253. package/dist/browser/runtime/browser/polyfill.js +37 -0
  254. package/dist/browser/runtime/browser/polyfill.js.map +1 -0
  255. package/dist/browser/runtime/browser/setup-kubo-address-rewriter-and-http-router.d.ts +1 -0
  256. package/dist/browser/runtime/browser/setup-kubo-address-rewriter-and-http-router.js +4 -0
  257. package/dist/browser/runtime/browser/setup-kubo-address-rewriter-and-http-router.js.map +1 -0
  258. package/dist/browser/runtime/browser/storage.d.ts +13 -0
  259. package/dist/browser/runtime/browser/storage.js +37 -0
  260. package/dist/browser/runtime/browser/storage.js.map +1 -0
  261. package/dist/browser/runtime/browser/util.d.ts +14 -0
  262. package/dist/browser/runtime/browser/util.js +61 -0
  263. package/dist/browser/runtime/browser/util.js.map +1 -0
  264. package/dist/browser/runtime/node/address-rewriter-db.d.ts +31 -0
  265. package/dist/browser/runtime/node/address-rewriter-db.js +156 -0
  266. package/dist/browser/runtime/node/address-rewriter-db.js.map +1 -0
  267. package/dist/browser/runtime/node/addresses-rewriter-proxy-server.d.ts +45 -0
  268. package/dist/browser/runtime/node/addresses-rewriter-proxy-server.js +493 -0
  269. package/dist/browser/runtime/node/addresses-rewriter-proxy-server.js.map +1 -0
  270. package/dist/browser/runtime/node/community/challenges/exclude/exclude.d.ts +8 -0
  271. package/dist/browser/runtime/node/community/challenges/exclude/exclude.js +280 -0
  272. package/dist/browser/runtime/node/community/challenges/exclude/exclude.js.map +1 -0
  273. package/dist/browser/runtime/node/community/challenges/exclude/index.d.ts +3 -0
  274. package/dist/browser/runtime/node/community/challenges/exclude/index.js +4 -0
  275. package/dist/browser/runtime/node/community/challenges/exclude/index.js.map +1 -0
  276. package/dist/browser/runtime/node/community/challenges/exclude/rate-limiter.d.ts +5 -0
  277. package/dist/browser/runtime/node/community/challenges/exclude/rate-limiter.js +127 -0
  278. package/dist/browser/runtime/node/community/challenges/exclude/rate-limiter.js.map +1 -0
  279. package/dist/browser/runtime/node/community/challenges/exclude/utils.d.ts +13 -0
  280. package/dist/browser/runtime/node/community/challenges/exclude/utils.js +52 -0
  281. package/dist/browser/runtime/node/community/challenges/exclude/utils.js.map +1 -0
  282. package/dist/browser/runtime/node/community/challenges/index.d.ts +32 -0
  283. package/dist/browser/runtime/node/community/challenges/index.js +307 -0
  284. package/dist/browser/runtime/node/community/challenges/index.js.map +1 -0
  285. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/blacklist.d.ts +5 -0
  286. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/blacklist.js +118 -0
  287. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/blacklist.js.map +1 -0
  288. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/fail.d.ts +5 -0
  289. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/fail.js +26 -0
  290. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/fail.js.map +1 -0
  291. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/publication-match.d.ts +5 -0
  292. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/publication-match.js +135 -0
  293. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/publication-match.js.map +1 -0
  294. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/question.d.ts +5 -0
  295. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/question.js +66 -0
  296. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/question.js.map +1 -0
  297. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/text-math.d.ts +5 -0
  298. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/text-math.js +61 -0
  299. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/text-math.js.map +1 -0
  300. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/whitelist.d.ts +5 -0
  301. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/whitelist.js +118 -0
  302. package/dist/browser/runtime/node/community/challenges/pkc-js-challenges/whitelist.js.map +1 -0
  303. package/dist/browser/runtime/node/community/db-handler-types.d.ts +19 -0
  304. package/dist/browser/runtime/node/community/db-handler-types.js +2 -0
  305. package/dist/browser/runtime/node/community/db-handler-types.js.map +1 -0
  306. package/dist/browser/runtime/node/community/db-handler.d.ts +226 -0
  307. package/dist/browser/runtime/node/community/db-handler.js +2462 -0
  308. package/dist/browser/runtime/node/community/db-handler.js.map +1 -0
  309. package/dist/browser/runtime/node/community/db-row-parser.d.ts +19 -0
  310. package/dist/browser/runtime/node/community/db-row-parser.js +40 -0
  311. package/dist/browser/runtime/node/community/db-row-parser.js.map +1 -0
  312. package/dist/browser/runtime/node/community/keyv-better-sqlite3.d.ts +68 -0
  313. package/dist/browser/runtime/node/community/keyv-better-sqlite3.js +251 -0
  314. package/dist/browser/runtime/node/community/keyv-better-sqlite3.js.map +1 -0
  315. package/dist/browser/runtime/node/community/local-community.d.ts +129 -0
  316. package/dist/browser/runtime/node/community/local-community.js +2978 -0
  317. package/dist/browser/runtime/node/community/local-community.js.map +1 -0
  318. package/dist/browser/runtime/node/community/page-generator.d.ts +433 -0
  319. package/dist/browser/runtime/node/community/page-generator.js +441 -0
  320. package/dist/browser/runtime/node/community/page-generator.js.map +1 -0
  321. package/dist/browser/runtime/node/lru-storage.d.ts +14 -0
  322. package/dist/browser/runtime/node/lru-storage.js +40 -0
  323. package/dist/browser/runtime/node/lru-storage.js.map +1 -0
  324. package/dist/browser/runtime/node/native-functions.d.ts +3 -0
  325. package/dist/browser/runtime/node/native-functions.js +7 -0
  326. package/dist/browser/runtime/node/native-functions.js.map +1 -0
  327. package/dist/browser/runtime/node/polyfill.d.ts +3 -0
  328. package/dist/browser/runtime/node/polyfill.js +20 -0
  329. package/dist/browser/runtime/node/polyfill.js.map +1 -0
  330. package/dist/browser/runtime/node/setup-kubo-address-rewriter-and-http-router.d.ts +4 -0
  331. package/dist/browser/runtime/node/setup-kubo-address-rewriter-and-http-router.js +240 -0
  332. package/dist/browser/runtime/node/setup-kubo-address-rewriter-and-http-router.js.map +1 -0
  333. package/dist/browser/runtime/node/sqlite-lru-cache.d.ts +52 -0
  334. package/dist/browser/runtime/node/sqlite-lru-cache.js +127 -0
  335. package/dist/browser/runtime/node/sqlite-lru-cache.js.map +1 -0
  336. package/dist/browser/runtime/node/storage.d.ts +14 -0
  337. package/dist/browser/runtime/node/storage.js +52 -0
  338. package/dist/browser/runtime/node/storage.js.map +1 -0
  339. package/dist/browser/runtime/node/test/helpers/hanging-runner.d.ts +1 -0
  340. package/dist/browser/runtime/node/test/helpers/hanging-runner.js +157 -0
  341. package/dist/browser/runtime/node/test/helpers/hanging-runner.js.map +1 -0
  342. package/dist/browser/runtime/node/test/helpers/run-hanging-node.d.ts +7 -0
  343. package/dist/browser/runtime/node/test/helpers/run-hanging-node.js +68 -0
  344. package/dist/browser/runtime/node/test/helpers/run-hanging-node.js.map +1 -0
  345. package/dist/browser/runtime/node/test/mock-http-router.d.ts +54 -0
  346. package/dist/browser/runtime/node/test/mock-http-router.js +397 -0
  347. package/dist/browser/runtime/node/test/mock-http-router.js.map +1 -0
  348. package/dist/browser/runtime/node/util.d.ts +43 -0
  349. package/dist/browser/runtime/node/util.js +384 -0
  350. package/dist/browser/runtime/node/util.js.map +1 -0
  351. package/dist/browser/schema/schema-util.d.ts +2751 -0
  352. package/dist/browser/schema/schema-util.js +562 -0
  353. package/dist/browser/schema/schema-util.js.map +1 -0
  354. package/dist/browser/schema/schema.d.ts +237 -0
  355. package/dist/browser/schema/schema.js +128 -0
  356. package/dist/browser/schema/schema.js.map +1 -0
  357. package/dist/browser/schema.d.ts +1142 -0
  358. package/dist/browser/schema.js +104 -0
  359. package/dist/browser/schema.js.map +1 -0
  360. package/dist/browser/signer/constants.d.ts +2 -0
  361. package/dist/browser/signer/constants.js +3 -0
  362. package/dist/browser/signer/constants.js.map +1 -0
  363. package/dist/browser/signer/encryption.d.ts +21 -0
  364. package/dist/browser/signer/encryption.js +122 -0
  365. package/dist/browser/signer/encryption.js.map +1 -0
  366. package/dist/browser/signer/index.d.ts +21 -0
  367. package/dist/browser/signer/index.js +49 -0
  368. package/dist/browser/signer/index.js.map +1 -0
  369. package/dist/browser/signer/signatures.d.ts +200 -0
  370. package/dist/browser/signer/signatures.js +594 -0
  371. package/dist/browser/signer/signatures.js.map +1 -0
  372. package/dist/browser/signer/types.d.ts +20 -0
  373. package/dist/browser/signer/types.js +2 -0
  374. package/dist/browser/signer/types.js.map +1 -0
  375. package/dist/browser/signer/util.d.ts +14 -0
  376. package/dist/browser/signer/util.js +156 -0
  377. package/dist/browser/signer/util.js.map +1 -0
  378. package/dist/browser/stats.d.ts +15 -0
  379. package/dist/browser/stats.js +64 -0
  380. package/dist/browser/stats.js.map +1 -0
  381. package/dist/browser/test/mock-ipfs-client.d.ts +34 -0
  382. package/dist/browser/test/mock-ipfs-client.js +208 -0
  383. package/dist/browser/test/mock-ipfs-client.js.map +1 -0
  384. package/dist/browser/test/node/hanging-test/scenarios/comment-publish-pending.scenario.d.ts +8 -0
  385. package/dist/browser/test/node/hanging-test/scenarios/comment-publish-pending.scenario.js +21 -0
  386. package/dist/browser/test/node/hanging-test/scenarios/comment-publish-pending.scenario.js.map +1 -0
  387. package/dist/browser/test/node/hanging-test/scenarios/comment-publish.scenario.d.ts +8 -0
  388. package/dist/browser/test/node/hanging-test/scenarios/comment-publish.scenario.js +19 -0
  389. package/dist/browser/test/node/hanging-test/scenarios/comment-publish.scenario.js.map +1 -0
  390. package/dist/browser/test/node/hanging-test/scenarios/comment-update.scenario.d.ts +8 -0
  391. package/dist/browser/test/node/hanging-test/scenarios/comment-update.scenario.js +22 -0
  392. package/dist/browser/test/node/hanging-test/scenarios/comment-update.scenario.js.map +1 -0
  393. package/dist/browser/test/node/hanging-test/scenarios/community-start.scenario.d.ts +8 -0
  394. package/dist/browser/test/node/hanging-test/scenarios/community-start.scenario.js +23 -0
  395. package/dist/browser/test/node/hanging-test/scenarios/community-start.scenario.js.map +1 -0
  396. package/dist/browser/test/node/hanging-test/scenarios/community-update.scenario.d.ts +8 -0
  397. package/dist/browser/test/node/hanging-test/scenarios/community-update.scenario.js +21 -0
  398. package/dist/browser/test/node/hanging-test/scenarios/community-update.scenario.js.map +1 -0
  399. package/dist/browser/test/node/hanging-test/scenarios/destroy-only.scenario.d.ts +7 -0
  400. package/dist/browser/test/node/hanging-test/scenarios/destroy-only.scenario.js +15 -0
  401. package/dist/browser/test/node/hanging-test/scenarios/destroy-only.scenario.js.map +1 -0
  402. package/dist/browser/test/node/hanging-test/scenarios/hanging-test-util.d.ts +30 -0
  403. package/dist/browser/test/node/hanging-test/scenarios/hanging-test-util.js +46 -0
  404. package/dist/browser/test/node/hanging-test/scenarios/hanging-test-util.js.map +1 -0
  405. package/dist/browser/test/test-util.d.ts +1019 -0
  406. package/dist/browser/test/test-util.js +1886 -0
  407. package/dist/browser/test/test-util.js.map +1 -0
  408. package/dist/browser/types.d.ts +165 -0
  409. package/dist/browser/types.js +2 -0
  410. package/dist/browser/types.js.map +1 -0
  411. package/dist/browser/util/inflight-fetch-manager.d.ts +11 -0
  412. package/dist/browser/util/inflight-fetch-manager.js +41 -0
  413. package/dist/browser/util/inflight-fetch-manager.js.map +1 -0
  414. package/dist/browser/util.d.ts +120 -0
  415. package/dist/browser/util.js +816 -0
  416. package/dist/browser/util.js.map +1 -0
  417. package/dist/browser/version.d.ts +7 -0
  418. package/dist/browser/version.js +12 -0
  419. package/dist/browser/version.js.map +1 -0
  420. package/dist/browser/zod-error-map.d.ts +1 -0
  421. package/dist/browser/zod-error-map.js +10 -0
  422. package/dist/browser/zod-error-map.js.map +1 -0
  423. package/dist/node/challenges.d.ts +1 -0
  424. package/dist/node/challenges.js +2 -0
  425. package/dist/node/challenges.js.map +1 -0
  426. package/dist/node/clients/base-client-manager.d.ts +126 -0
  427. package/dist/node/clients/base-client-manager.js +673 -0
  428. package/dist/node/clients/base-client-manager.js.map +1 -0
  429. package/dist/node/clients/name-resolver-client.d.ts +8 -0
  430. package/dist/node/clients/name-resolver-client.js +10 -0
  431. package/dist/node/clients/name-resolver-client.js.map +1 -0
  432. package/dist/node/clients/pkc-typed-emitter.d.ts +9 -0
  433. package/dist/node/clients/pkc-typed-emitter.js +52 -0
  434. package/dist/node/clients/pkc-typed-emitter.js.map +1 -0
  435. package/dist/node/clients/rpc-client/decode-rpc-response-util.d.ts +8 -0
  436. package/dist/node/clients/rpc-client/decode-rpc-response-util.js +53 -0
  437. package/dist/node/clients/rpc-client/decode-rpc-response-util.js.map +1 -0
  438. package/dist/node/clients/rpc-client/pkc-rpc-client.d.ts +68 -0
  439. package/dist/node/clients/rpc-client/pkc-rpc-client.js +404 -0
  440. package/dist/node/clients/rpc-client/pkc-rpc-client.js.map +1 -0
  441. package/dist/node/clients/rpc-client/rpc-schema-util.d.ts +147 -0
  442. package/dist/node/clients/rpc-client/rpc-schema-util.js +11 -0
  443. package/dist/node/clients/rpc-client/rpc-schema-util.js.map +1 -0
  444. package/dist/node/clients/rpc-client/schema.d.ts +433 -0
  445. package/dist/node/clients/rpc-client/schema.js +49 -0
  446. package/dist/node/clients/rpc-client/schema.js.map +1 -0
  447. package/dist/node/clients/rpc-client/types.d.ts +8 -0
  448. package/dist/node/clients/rpc-client/types.js +2 -0
  449. package/dist/node/clients/rpc-client/types.js.map +1 -0
  450. package/dist/node/community/community-client-manager.d.ts +60 -0
  451. package/dist/node/community/community-client-manager.js +717 -0
  452. package/dist/node/community/community-client-manager.js.map +1 -0
  453. package/dist/node/community/community-clients.d.ts +18 -0
  454. package/dist/node/community/community-clients.js +12 -0
  455. package/dist/node/community/community-clients.js.map +1 -0
  456. package/dist/node/community/community-wire.d.ts +20 -0
  457. package/dist/node/community/community-wire.js +38 -0
  458. package/dist/node/community/community-wire.js.map +1 -0
  459. package/dist/node/community/remote-community.d.ts +110 -0
  460. package/dist/node/community/remote-community.js +555 -0
  461. package/dist/node/community/remote-community.js.map +1 -0
  462. package/dist/node/community/rpc-local-community.d.ts +41 -0
  463. package/dist/node/community/rpc-local-community.js +289 -0
  464. package/dist/node/community/rpc-local-community.js.map +1 -0
  465. package/dist/node/community/rpc-remote-community.d.ts +18 -0
  466. package/dist/node/community/rpc-remote-community.js +286 -0
  467. package/dist/node/community/rpc-remote-community.js.map +1 -0
  468. package/dist/node/community/schema.d.ts +4217 -0
  469. package/dist/node/community/schema.js +289 -0
  470. package/dist/node/community/schema.js.map +1 -0
  471. package/dist/node/community/types.d.ts +135 -0
  472. package/dist/node/community/types.js +2 -0
  473. package/dist/node/community/types.js.map +1 -0
  474. package/dist/node/constants.d.ts +6 -0
  475. package/dist/node/constants.js +9 -0
  476. package/dist/node/constants.js.map +1 -0
  477. package/dist/node/decorator-util.d.ts +1 -0
  478. package/dist/node/decorator-util.js +35 -0
  479. package/dist/node/decorator-util.js.map +1 -0
  480. package/dist/node/errors.d.ts +343 -0
  481. package/dist/node/errors.js +358 -0
  482. package/dist/node/errors.js.map +1 -0
  483. package/dist/node/general-util/limited-set.d.ts +15 -0
  484. package/dist/node/general-util/limited-set.js +66 -0
  485. package/dist/node/general-util/limited-set.js.map +1 -0
  486. package/dist/node/generated-version.d.ts +1 -0
  487. package/dist/node/generated-version.js +3 -0
  488. package/dist/node/generated-version.js.map +1 -0
  489. package/dist/node/generic-state-client.d.ts +6 -0
  490. package/dist/node/generic-state-client.js +11 -0
  491. package/dist/node/generic-state-client.js.map +1 -0
  492. package/dist/node/helia/helia-for-pkc.d.ts +3 -0
  493. package/dist/node/helia/helia-for-pkc.js +255 -0
  494. package/dist/node/helia/helia-for-pkc.js.map +1 -0
  495. package/dist/node/helia/ipns-over-pubsub-with-fetch.d.ts +36 -0
  496. package/dist/node/helia/ipns-over-pubsub-with-fetch.js +229 -0
  497. package/dist/node/helia/ipns-over-pubsub-with-fetch.js.map +1 -0
  498. package/dist/node/helia/libp2pjsClient.d.ts +27 -0
  499. package/dist/node/helia/libp2pjsClient.js +15 -0
  500. package/dist/node/helia/libp2pjsClient.js.map +1 -0
  501. package/dist/node/helia/types.d.ts +19 -0
  502. package/dist/node/helia/types.js +2 -0
  503. package/dist/node/helia/types.js.map +1 -0
  504. package/dist/node/helia/util.d.ts +13 -0
  505. package/dist/node/helia/util.js +98 -0
  506. package/dist/node/helia/util.js.map +1 -0
  507. package/dist/node/index.d.ts +244 -0
  508. package/dist/node/index.js +36 -0
  509. package/dist/node/index.js.map +1 -0
  510. package/dist/node/logger.d.ts +12 -0
  511. package/dist/node/logger.js +11 -0
  512. package/dist/node/logger.js.map +1 -0
  513. package/dist/node/pages/pages-client-manager.d.ts +159 -0
  514. package/dist/node/pages/pages-client-manager.js +334 -0
  515. package/dist/node/pages/pages-client-manager.js.map +1 -0
  516. package/dist/node/pages/pages-clients.d.ts +11 -0
  517. package/dist/node/pages/pages-clients.js +10 -0
  518. package/dist/node/pages/pages-clients.js.map +1 -0
  519. package/dist/node/pages/pages.d.ts +107 -0
  520. package/dist/node/pages/pages.js +262 -0
  521. package/dist/node/pages/pages.js.map +1 -0
  522. package/dist/node/pages/schema-util.d.ts +3 -0
  523. package/dist/node/pages/schema-util.js +3 -0
  524. package/dist/node/pages/schema-util.js.map +1 -0
  525. package/dist/node/pages/schema.d.ts +719 -0
  526. package/dist/node/pages/schema.js +32 -0
  527. package/dist/node/pages/schema.js.map +1 -0
  528. package/dist/node/pages/types.d.ts +44 -0
  529. package/dist/node/pages/types.js +2 -0
  530. package/dist/node/pages/types.js.map +1 -0
  531. package/dist/node/pages/util.d.ts +56 -0
  532. package/dist/node/pages/util.js +446 -0
  533. package/dist/node/pages/util.js.map +1 -0
  534. package/dist/node/pkc/pkc-client-manager.d.ts +44 -0
  535. package/dist/node/pkc/pkc-client-manager.js +156 -0
  536. package/dist/node/pkc/pkc-client-manager.js.map +1 -0
  537. package/dist/node/pkc/pkc-clients.d.ts +11 -0
  538. package/dist/node/pkc/pkc-clients.js +8 -0
  539. package/dist/node/pkc/pkc-clients.js.map +1 -0
  540. package/dist/node/pkc/pkc-with-rpc-client.d.ts +19 -0
  541. package/dist/node/pkc/pkc-with-rpc-client.js +128 -0
  542. package/dist/node/pkc/pkc-with-rpc-client.js.map +1 -0
  543. package/dist/node/pkc/pkc.d.ts +137 -0
  544. package/dist/node/pkc/pkc.js +888 -0
  545. package/dist/node/pkc/pkc.js.map +1 -0
  546. package/dist/node/pkc/tracked-instance-registry-util.d.ts +44 -0
  547. package/dist/node/pkc/tracked-instance-registry-util.js +106 -0
  548. package/dist/node/pkc/tracked-instance-registry-util.js.map +1 -0
  549. package/dist/node/pkc/tracked-instance-registry.d.ts +18 -0
  550. package/dist/node/pkc/tracked-instance-registry.js +134 -0
  551. package/dist/node/pkc/tracked-instance-registry.js.map +1 -0
  552. package/dist/node/pkc-error.d.ts +65 -0
  553. package/dist/node/pkc-error.js +137 -0
  554. package/dist/node/pkc-error.js.map +1 -0
  555. package/dist/node/publications/comment/comment-client-manager.d.ts +86 -0
  556. package/dist/node/publications/comment/comment-client-manager.js +908 -0
  557. package/dist/node/publications/comment/comment-client-manager.js.map +1 -0
  558. package/dist/node/publications/comment/comment-clients.d.ts +19 -0
  559. package/dist/node/publications/comment/comment-clients.js +12 -0
  560. package/dist/node/publications/comment/comment-clients.js.map +1 -0
  561. package/dist/node/publications/comment/comment-util.d.ts +10 -0
  562. package/dist/node/publications/comment/comment-util.js +202 -0
  563. package/dist/node/publications/comment/comment-util.js.map +1 -0
  564. package/dist/node/publications/comment/comment.d.ts +147 -0
  565. package/dist/node/publications/comment/comment.js +1044 -0
  566. package/dist/node/publications/comment/comment.js.map +1 -0
  567. package/dist/node/publications/comment/schema.d.ts +1237 -0
  568. package/dist/node/publications/comment/schema.js +184 -0
  569. package/dist/node/publications/comment/schema.js.map +1 -0
  570. package/dist/node/publications/comment/types.d.ts +100 -0
  571. package/dist/node/publications/comment/types.js +2 -0
  572. package/dist/node/publications/comment/types.js.map +1 -0
  573. package/dist/node/publications/comment-edit/comment-edit.d.ts +41 -0
  574. package/dist/node/publications/comment-edit/comment-edit.js +63 -0
  575. package/dist/node/publications/comment-edit/comment-edit.js.map +1 -0
  576. package/dist/node/publications/comment-edit/schema.d.ts +295 -0
  577. package/dist/node/publications/comment-edit/schema.js +55 -0
  578. package/dist/node/publications/comment-edit/schema.js.map +1 -0
  579. package/dist/node/publications/comment-edit/types.d.ts +25 -0
  580. package/dist/node/publications/comment-edit/types.js +2 -0
  581. package/dist/node/publications/comment-edit/types.js.map +1 -0
  582. package/dist/node/publications/comment-moderation/comment-moderation.d.ts +36 -0
  583. package/dist/node/publications/comment-moderation/comment-moderation.js +53 -0
  584. package/dist/node/publications/comment-moderation/comment-moderation.js.map +1 -0
  585. package/dist/node/publications/comment-moderation/schema.d.ts +315 -0
  586. package/dist/node/publications/comment-moderation/schema.js +62 -0
  587. package/dist/node/publications/comment-moderation/schema.js.map +1 -0
  588. package/dist/node/publications/comment-moderation/types.d.ts +22 -0
  589. package/dist/node/publications/comment-moderation/types.js +2 -0
  590. package/dist/node/publications/comment-moderation/types.js.map +1 -0
  591. package/dist/node/publications/community-edit/community-edit.d.ts +35 -0
  592. package/dist/node/publications/community-edit/community-edit.js +50 -0
  593. package/dist/node/publications/community-edit/community-edit.js.map +1 -0
  594. package/dist/node/publications/community-edit/schema.d.ts +467 -0
  595. package/dist/node/publications/community-edit/schema.js +36 -0
  596. package/dist/node/publications/community-edit/schema.js.map +1 -0
  597. package/dist/node/publications/community-edit/types.d.ts +19 -0
  598. package/dist/node/publications/community-edit/types.js +2 -0
  599. package/dist/node/publications/community-edit/types.js.map +1 -0
  600. package/dist/node/publications/publication-author.d.ts +22 -0
  601. package/dist/node/publications/publication-author.js +66 -0
  602. package/dist/node/publications/publication-author.js.map +1 -0
  603. package/dist/node/publications/publication-client-manager.d.ts +62 -0
  604. package/dist/node/publications/publication-client-manager.js +257 -0
  605. package/dist/node/publications/publication-client-manager.js.map +1 -0
  606. package/dist/node/publications/publication-clients.d.ts +19 -0
  607. package/dist/node/publications/publication-clients.js +12 -0
  608. package/dist/node/publications/publication-clients.js.map +1 -0
  609. package/dist/node/publications/publication-community.d.ts +55 -0
  610. package/dist/node/publications/publication-community.js +87 -0
  611. package/dist/node/publications/publication-community.js.map +1 -0
  612. package/dist/node/publications/publication.d.ts +120 -0
  613. package/dist/node/publications/publication.js +950 -0
  614. package/dist/node/publications/publication.js.map +1 -0
  615. package/dist/node/publications/types.d.ts +26 -0
  616. package/dist/node/publications/types.js +2 -0
  617. package/dist/node/publications/types.js.map +1 -0
  618. package/dist/node/publications/vote/schema.d.ts +150 -0
  619. package/dist/node/publications/vote/schema.js +44 -0
  620. package/dist/node/publications/vote/schema.js.map +1 -0
  621. package/dist/node/publications/vote/types.d.ts +21 -0
  622. package/dist/node/publications/vote/types.js +2 -0
  623. package/dist/node/publications/vote/types.js.map +1 -0
  624. package/dist/node/publications/vote/vote.d.ts +36 -0
  625. package/dist/node/publications/vote/vote.js +49 -0
  626. package/dist/node/publications/vote/vote.js.map +1 -0
  627. package/dist/node/pubsub-messages/schema.d.ts +964 -0
  628. package/dist/node/pubsub-messages/schema.js +98 -0
  629. package/dist/node/pubsub-messages/schema.js.map +1 -0
  630. package/dist/node/pubsub-messages/types.d.ts +81 -0
  631. package/dist/node/pubsub-messages/types.js +2 -0
  632. package/dist/node/pubsub-messages/types.js.map +1 -0
  633. package/dist/node/rpc/src/index.d.ts +483 -0
  634. package/dist/node/rpc/src/index.js +1267 -0
  635. package/dist/node/rpc/src/index.js.map +1 -0
  636. package/dist/node/rpc/src/json-rpc-util.d.ts +1 -0
  637. package/dist/node/rpc/src/json-rpc-util.js +19 -0
  638. package/dist/node/rpc/src/json-rpc-util.js.map +1 -0
  639. package/dist/node/rpc/src/lib/pkc-js/index.d.ts +132 -0
  640. package/dist/node/rpc/src/lib/pkc-js/index.js +29 -0
  641. package/dist/node/rpc/src/lib/pkc-js/index.js.map +1 -0
  642. package/dist/node/rpc/src/lib/pkc-js/pkc-js-mock.d.ts +1 -0
  643. package/dist/node/rpc/src/lib/pkc-js/pkc-js-mock.js +472 -0
  644. package/dist/node/rpc/src/lib/pkc-js/pkc-js-mock.js.map +1 -0
  645. package/dist/node/rpc/src/schema.d.ts +843 -0
  646. package/dist/node/rpc/src/schema.js +28 -0
  647. package/dist/node/rpc/src/schema.js.map +1 -0
  648. package/dist/node/rpc/src/types.d.ts +24 -0
  649. package/dist/node/rpc/src/types.js +2 -0
  650. package/dist/node/rpc/src/types.js.map +1 -0
  651. package/dist/node/rpc/src/utils.d.ts +7 -0
  652. package/dist/node/rpc/src/utils.js +58 -0
  653. package/dist/node/rpc/src/utils.js.map +1 -0
  654. package/dist/node/runtime/browser/community/challenges/index.d.ts +6 -0
  655. package/dist/node/runtime/browser/community/challenges/index.js +7 -0
  656. package/dist/node/runtime/browser/community/challenges/index.js.map +1 -0
  657. package/dist/node/runtime/browser/community/local-community.d.ts +3 -0
  658. package/dist/node/runtime/browser/community/local-community.js +6 -0
  659. package/dist/node/runtime/browser/community/local-community.js.map +1 -0
  660. package/dist/node/runtime/browser/db-handler.d.ts +4 -0
  661. package/dist/node/runtime/browser/db-handler.js +8 -0
  662. package/dist/node/runtime/browser/db-handler.js.map +1 -0
  663. package/dist/node/runtime/browser/localforage-lru.d.ts +15 -0
  664. package/dist/node/runtime/browser/localforage-lru.js +140 -0
  665. package/dist/node/runtime/browser/localforage-lru.js.map +1 -0
  666. package/dist/node/runtime/browser/lru-storage.d.ts +14 -0
  667. package/dist/node/runtime/browser/lru-storage.js +34 -0
  668. package/dist/node/runtime/browser/lru-storage.js.map +1 -0
  669. package/dist/node/runtime/browser/native-functions.d.ts +3 -0
  670. package/dist/node/runtime/browser/native-functions.js +6 -0
  671. package/dist/node/runtime/browser/native-functions.js.map +1 -0
  672. package/dist/node/runtime/browser/polyfill.d.ts +3 -0
  673. package/dist/node/runtime/browser/polyfill.js +37 -0
  674. package/dist/node/runtime/browser/polyfill.js.map +1 -0
  675. package/dist/node/runtime/browser/setup-kubo-address-rewriter-and-http-router.d.ts +1 -0
  676. package/dist/node/runtime/browser/setup-kubo-address-rewriter-and-http-router.js +4 -0
  677. package/dist/node/runtime/browser/setup-kubo-address-rewriter-and-http-router.js.map +1 -0
  678. package/dist/node/runtime/browser/storage.d.ts +13 -0
  679. package/dist/node/runtime/browser/storage.js +37 -0
  680. package/dist/node/runtime/browser/storage.js.map +1 -0
  681. package/dist/node/runtime/browser/util.d.ts +14 -0
  682. package/dist/node/runtime/browser/util.js +61 -0
  683. package/dist/node/runtime/browser/util.js.map +1 -0
  684. package/dist/node/runtime/node/address-rewriter-db.d.ts +31 -0
  685. package/dist/node/runtime/node/address-rewriter-db.js +156 -0
  686. package/dist/node/runtime/node/address-rewriter-db.js.map +1 -0
  687. package/dist/node/runtime/node/addresses-rewriter-proxy-server.d.ts +45 -0
  688. package/dist/node/runtime/node/addresses-rewriter-proxy-server.js +493 -0
  689. package/dist/node/runtime/node/addresses-rewriter-proxy-server.js.map +1 -0
  690. package/dist/node/runtime/node/community/challenges/exclude/exclude.d.ts +8 -0
  691. package/dist/node/runtime/node/community/challenges/exclude/exclude.js +280 -0
  692. package/dist/node/runtime/node/community/challenges/exclude/exclude.js.map +1 -0
  693. package/dist/node/runtime/node/community/challenges/exclude/index.d.ts +3 -0
  694. package/dist/node/runtime/node/community/challenges/exclude/index.js +4 -0
  695. package/dist/node/runtime/node/community/challenges/exclude/index.js.map +1 -0
  696. package/dist/node/runtime/node/community/challenges/exclude/rate-limiter.d.ts +5 -0
  697. package/dist/node/runtime/node/community/challenges/exclude/rate-limiter.js +127 -0
  698. package/dist/node/runtime/node/community/challenges/exclude/rate-limiter.js.map +1 -0
  699. package/dist/node/runtime/node/community/challenges/exclude/utils.d.ts +13 -0
  700. package/dist/node/runtime/node/community/challenges/exclude/utils.js +52 -0
  701. package/dist/node/runtime/node/community/challenges/exclude/utils.js.map +1 -0
  702. package/dist/node/runtime/node/community/challenges/index.d.ts +32 -0
  703. package/dist/node/runtime/node/community/challenges/index.js +307 -0
  704. package/dist/node/runtime/node/community/challenges/index.js.map +1 -0
  705. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/blacklist.d.ts +5 -0
  706. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/blacklist.js +118 -0
  707. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/blacklist.js.map +1 -0
  708. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/fail.d.ts +5 -0
  709. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/fail.js +26 -0
  710. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/fail.js.map +1 -0
  711. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/publication-match.d.ts +5 -0
  712. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/publication-match.js +135 -0
  713. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/publication-match.js.map +1 -0
  714. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/question.d.ts +5 -0
  715. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/question.js +66 -0
  716. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/question.js.map +1 -0
  717. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/text-math.d.ts +5 -0
  718. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/text-math.js +61 -0
  719. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/text-math.js.map +1 -0
  720. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/whitelist.d.ts +5 -0
  721. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/whitelist.js +118 -0
  722. package/dist/node/runtime/node/community/challenges/pkc-js-challenges/whitelist.js.map +1 -0
  723. package/dist/node/runtime/node/community/db-handler-types.d.ts +19 -0
  724. package/dist/node/runtime/node/community/db-handler-types.js +2 -0
  725. package/dist/node/runtime/node/community/db-handler-types.js.map +1 -0
  726. package/dist/node/runtime/node/community/db-handler.d.ts +226 -0
  727. package/dist/node/runtime/node/community/db-handler.js +2462 -0
  728. package/dist/node/runtime/node/community/db-handler.js.map +1 -0
  729. package/dist/node/runtime/node/community/db-row-parser.d.ts +19 -0
  730. package/dist/node/runtime/node/community/db-row-parser.js +40 -0
  731. package/dist/node/runtime/node/community/db-row-parser.js.map +1 -0
  732. package/dist/node/runtime/node/community/keyv-better-sqlite3.d.ts +68 -0
  733. package/dist/node/runtime/node/community/keyv-better-sqlite3.js +251 -0
  734. package/dist/node/runtime/node/community/keyv-better-sqlite3.js.map +1 -0
  735. package/dist/node/runtime/node/community/local-community.d.ts +129 -0
  736. package/dist/node/runtime/node/community/local-community.js +2978 -0
  737. package/dist/node/runtime/node/community/local-community.js.map +1 -0
  738. package/dist/node/runtime/node/community/page-generator.d.ts +433 -0
  739. package/dist/node/runtime/node/community/page-generator.js +441 -0
  740. package/dist/node/runtime/node/community/page-generator.js.map +1 -0
  741. package/dist/node/runtime/node/lru-storage.d.ts +14 -0
  742. package/dist/node/runtime/node/lru-storage.js +40 -0
  743. package/dist/node/runtime/node/lru-storage.js.map +1 -0
  744. package/dist/node/runtime/node/native-functions.d.ts +3 -0
  745. package/dist/node/runtime/node/native-functions.js +7 -0
  746. package/dist/node/runtime/node/native-functions.js.map +1 -0
  747. package/dist/node/runtime/node/polyfill.d.ts +3 -0
  748. package/dist/node/runtime/node/polyfill.js +20 -0
  749. package/dist/node/runtime/node/polyfill.js.map +1 -0
  750. package/dist/node/runtime/node/setup-kubo-address-rewriter-and-http-router.d.ts +4 -0
  751. package/dist/node/runtime/node/setup-kubo-address-rewriter-and-http-router.js +240 -0
  752. package/dist/node/runtime/node/setup-kubo-address-rewriter-and-http-router.js.map +1 -0
  753. package/dist/node/runtime/node/sqlite-lru-cache.d.ts +52 -0
  754. package/dist/node/runtime/node/sqlite-lru-cache.js +127 -0
  755. package/dist/node/runtime/node/sqlite-lru-cache.js.map +1 -0
  756. package/dist/node/runtime/node/storage.d.ts +14 -0
  757. package/dist/node/runtime/node/storage.js +52 -0
  758. package/dist/node/runtime/node/storage.js.map +1 -0
  759. package/dist/node/runtime/node/test/helpers/hanging-runner.d.ts +1 -0
  760. package/dist/node/runtime/node/test/helpers/hanging-runner.js +157 -0
  761. package/dist/node/runtime/node/test/helpers/hanging-runner.js.map +1 -0
  762. package/dist/node/runtime/node/test/helpers/run-hanging-node.d.ts +7 -0
  763. package/dist/node/runtime/node/test/helpers/run-hanging-node.js +68 -0
  764. package/dist/node/runtime/node/test/helpers/run-hanging-node.js.map +1 -0
  765. package/dist/node/runtime/node/test/mock-http-router.d.ts +54 -0
  766. package/dist/node/runtime/node/test/mock-http-router.js +397 -0
  767. package/dist/node/runtime/node/test/mock-http-router.js.map +1 -0
  768. package/dist/node/runtime/node/util.d.ts +43 -0
  769. package/dist/node/runtime/node/util.js +384 -0
  770. package/dist/node/runtime/node/util.js.map +1 -0
  771. package/dist/node/schema/schema-util.d.ts +2751 -0
  772. package/dist/node/schema/schema-util.js +562 -0
  773. package/dist/node/schema/schema-util.js.map +1 -0
  774. package/dist/node/schema/schema.d.ts +237 -0
  775. package/dist/node/schema/schema.js +128 -0
  776. package/dist/node/schema/schema.js.map +1 -0
  777. package/dist/node/schema.d.ts +1142 -0
  778. package/dist/node/schema.js +104 -0
  779. package/dist/node/schema.js.map +1 -0
  780. package/dist/node/signer/constants.d.ts +2 -0
  781. package/dist/node/signer/constants.js +3 -0
  782. package/dist/node/signer/constants.js.map +1 -0
  783. package/dist/node/signer/encryption.d.ts +21 -0
  784. package/dist/node/signer/encryption.js +122 -0
  785. package/dist/node/signer/encryption.js.map +1 -0
  786. package/dist/node/signer/index.d.ts +21 -0
  787. package/dist/node/signer/index.js +49 -0
  788. package/dist/node/signer/index.js.map +1 -0
  789. package/dist/node/signer/signatures.d.ts +200 -0
  790. package/dist/node/signer/signatures.js +594 -0
  791. package/dist/node/signer/signatures.js.map +1 -0
  792. package/dist/node/signer/types.d.ts +20 -0
  793. package/dist/node/signer/types.js +2 -0
  794. package/dist/node/signer/types.js.map +1 -0
  795. package/dist/node/signer/util.d.ts +14 -0
  796. package/dist/node/signer/util.js +156 -0
  797. package/dist/node/signer/util.js.map +1 -0
  798. package/dist/node/stats.d.ts +15 -0
  799. package/dist/node/stats.js +64 -0
  800. package/dist/node/stats.js.map +1 -0
  801. package/dist/node/test/mock-ipfs-client.d.ts +34 -0
  802. package/dist/node/test/mock-ipfs-client.js +208 -0
  803. package/dist/node/test/mock-ipfs-client.js.map +1 -0
  804. package/dist/node/test/node/hanging-test/scenarios/comment-publish-pending.scenario.d.ts +8 -0
  805. package/dist/node/test/node/hanging-test/scenarios/comment-publish-pending.scenario.js +21 -0
  806. package/dist/node/test/node/hanging-test/scenarios/comment-publish-pending.scenario.js.map +1 -0
  807. package/dist/node/test/node/hanging-test/scenarios/comment-publish.scenario.d.ts +8 -0
  808. package/dist/node/test/node/hanging-test/scenarios/comment-publish.scenario.js +19 -0
  809. package/dist/node/test/node/hanging-test/scenarios/comment-publish.scenario.js.map +1 -0
  810. package/dist/node/test/node/hanging-test/scenarios/comment-update.scenario.d.ts +8 -0
  811. package/dist/node/test/node/hanging-test/scenarios/comment-update.scenario.js +22 -0
  812. package/dist/node/test/node/hanging-test/scenarios/comment-update.scenario.js.map +1 -0
  813. package/dist/node/test/node/hanging-test/scenarios/community-start.scenario.d.ts +8 -0
  814. package/dist/node/test/node/hanging-test/scenarios/community-start.scenario.js +23 -0
  815. package/dist/node/test/node/hanging-test/scenarios/community-start.scenario.js.map +1 -0
  816. package/dist/node/test/node/hanging-test/scenarios/community-update.scenario.d.ts +8 -0
  817. package/dist/node/test/node/hanging-test/scenarios/community-update.scenario.js +21 -0
  818. package/dist/node/test/node/hanging-test/scenarios/community-update.scenario.js.map +1 -0
  819. package/dist/node/test/node/hanging-test/scenarios/destroy-only.scenario.d.ts +7 -0
  820. package/dist/node/test/node/hanging-test/scenarios/destroy-only.scenario.js +15 -0
  821. package/dist/node/test/node/hanging-test/scenarios/destroy-only.scenario.js.map +1 -0
  822. package/dist/node/test/node/hanging-test/scenarios/hanging-test-util.d.ts +30 -0
  823. package/dist/node/test/node/hanging-test/scenarios/hanging-test-util.js +46 -0
  824. package/dist/node/test/node/hanging-test/scenarios/hanging-test-util.js.map +1 -0
  825. package/dist/node/test/test-util.d.ts +1019 -0
  826. package/dist/node/test/test-util.js +1886 -0
  827. package/dist/node/test/test-util.js.map +1 -0
  828. package/dist/node/types.d.ts +165 -0
  829. package/dist/node/types.js +2 -0
  830. package/dist/node/types.js.map +1 -0
  831. package/dist/node/util/inflight-fetch-manager.d.ts +11 -0
  832. package/dist/node/util/inflight-fetch-manager.js +41 -0
  833. package/dist/node/util/inflight-fetch-manager.js.map +1 -0
  834. package/dist/node/util.d.ts +120 -0
  835. package/dist/node/util.js +816 -0
  836. package/dist/node/util.js.map +1 -0
  837. package/dist/node/version.d.ts +7 -0
  838. package/dist/node/version.js +12 -0
  839. package/dist/node/version.js.map +1 -0
  840. package/dist/node/zod-error-map.d.ts +1 -0
  841. package/dist/node/zod-error-map.js +10 -0
  842. package/dist/node/zod-error-map.js.map +1 -0
  843. package/package.json +212 -0
@@ -0,0 +1,1267 @@
1
+ import { Server as RpcWebsocketsServer } from "rpc-websockets";
2
+ import { mkdirSync } from "fs";
3
+ import path from "path";
4
+ import Database from "better-sqlite3";
5
+ import PKCJs, { setPKCJs } from "./lib/pkc-js/index.js";
6
+ import { encodeChallengeAnswerMessage, encodeChallengeMessage, encodeChallengeRequest, encodeChallengeVerificationMessage, generateSubscriptionId } from "./utils.js";
7
+ import Logger from "../../logger.js";
8
+ import { PKCError } from "../../pkc-error.js";
9
+ import { LocalCommunity } from "../../runtime/browser/community/local-community.js";
10
+ import { hideClassPrivateProps, replaceXWithY } from "../../util.js";
11
+ import * as remeda from "remeda";
12
+ import { parseCommentChallengeRequestToEncryptSchemaWithPKCErrorIfItFails, parseCommentEditChallengeRequestToEncryptSchemaWithPKCErrorIfItFails, parseCommentModerationChallengeRequestToEncryptSchemaWithPKCErrorIfItFails, parseCreateNewLocalCommunityUserOptionsSchemaWithPKCErrorIfItFails, parseCreatePKCWsServerOptionsSchemaWithPKCErrorIfItFails, parseDecryptedChallengeAnswerWithPKCErrorIfItFails, parseSetNewSettingsPKCWsServerSchemaWithPKCErrorIfItFails, parseCommunityEditChallengeRequestToEncryptSchemaWithPKCErrorIfItFails, parseCommunityEditOptionsSchemaWithPKCErrorIfItFails, parseVoteChallengeRequestToEncryptSchemaWithPKCErrorIfItFails } from "../../schema/schema-util.js";
13
+ import { stringify as deterministicStringify } from "safe-stable-stringify";
14
+ import { TypedEmitter } from "tiny-typed-emitter";
15
+ import { sanitizeRpcNotificationResult } from "./json-rpc-util.js";
16
+ import { buildPageRuntimeFields, buildPagesRuntimeFields } from "../../pages/util.js";
17
+ import { parseRpcCommunityAddressParam, parseRpcCommunityLookupParam, parseRpcAuthorNameParam, parseRpcCidParam, parseRpcCommentRepliesPageParam, parseRpcCommunityPageParam, parseRpcEditCommunityParam, parseRpcPublishChallengeAnswersParam, parseRpcUnsubscribeParam } from "../../clients/rpc-client/rpc-schema-util.js";
18
+ import { findStartedCommunity } from "../../pkc/tracked-instance-registry-util.js";
19
+ // store started communities to be able to stop them
20
+ // store as a singleton because not possible to start the same community twice at the same time
21
+ const log = Logger("pkc-js-rpc:pkc-ws-server");
22
+ // TODO need to think how to update PKC instance of publication after setSettings?
23
+ class PKCWsServer extends TypedEmitter {
24
+ constructor({ port, server, pkc, authKey, startStartedCommunitiesOnStartup }) {
25
+ super();
26
+ this.connections = {};
27
+ this.subscriptionCleanups = {};
28
+ // store publishing publications so they can be used by publishChallengeAnswers
29
+ this.publishing = {};
30
+ this._setSettingsQueue = Promise.resolve();
31
+ this._trackedCommunityListeners = new WeakMap();
32
+ this._getIpFromConnectionRequest = (req) => req.socket.remoteAddress; // we set it up here so we can mock it in tests
33
+ this._onSettingsChange = {}; // TODO rename this to _afterSettingsChange
34
+ this._startedCommunities = {}; // TODO replace this with pkc._startedCommunities
35
+ this._autoStartOnBoot = false;
36
+ const log = Logger("pkc-js:PKCWsServer");
37
+ this.authKey = authKey;
38
+ this._autoStartOnBoot = startStartedCommunitiesOnStartup ?? true;
39
+ // don't instantiate pkc in constructor because it's an async function
40
+ this._initPKC(pkc);
41
+ this.rpcWebsockets = new RpcWebsocketsServer({
42
+ port,
43
+ server,
44
+ verifyClient: ({ req }, callback) => {
45
+ // block non-localhost requests without auth key for security
46
+ const requestOriginatorIp = this._getIpFromConnectionRequest(req);
47
+ log.trace("Received new connection request from", requestOriginatorIp, "with url", req.url);
48
+ const xForwardedFor = Boolean(req.rawHeaders.find((item, i) => item.toLowerCase() === "x-forwarded-for" && i % 2 === 0));
49
+ // client is on localhost and server is not forwarded by a proxy
50
+ // req.socket.localAddress is the local address of the rpc server
51
+ const isLocalhost = req.socket.localAddress && req.socket.localAddress === requestOriginatorIp && !xForwardedFor;
52
+ // the request path is the auth key, e.g. localhost:9138/some-random-auth-key (not secure on http)
53
+ const hasAuth = this.authKey && `/${this.authKey}` === req.url;
54
+ // if isn't localhost and doesn't have auth, block access
55
+ if (!isLocalhost && !hasAuth) {
56
+ log(`Rejecting RPC connection request from`, requestOriginatorIp, `rejected because there is no auth key, url:`, req.url);
57
+ callback(false, 403, "You need to set the auth key to connect remotely");
58
+ }
59
+ else
60
+ callback(true);
61
+ }
62
+ });
63
+ // rpc-sockets uses this library https://www.npmjs.com/package/ws
64
+ this.ws = this.rpcWebsockets.wss;
65
+ // forward errors to PKCWsServer
66
+ this.rpcWebsockets.on("error", (error) => {
67
+ log.error("RPC server", "Received an error on rpc-websockets", error);
68
+ this._emitError(error);
69
+ });
70
+ // save connections to send messages to them later
71
+ this.ws.on("connection", (ws) => {
72
+ //@ts-ignore-error
73
+ this.connections[ws._id] = ws;
74
+ //@ts-ignore-error
75
+ this.subscriptionCleanups[ws._id] = {};
76
+ //@ts-expect-error
77
+ this._onSettingsChange[ws._id] = {};
78
+ //@ts-expect-error
79
+ log("Established connection with new RPC client", ws._id);
80
+ });
81
+ // cleanup on disconnect
82
+ this.rpcWebsockets.on("disconnection", async (ws) => {
83
+ log("RPC client disconnected", ws._id, "number of rpc clients connected", this.rpcWebsockets.wss.clients.size);
84
+ const subscriptionCleanups = this.subscriptionCleanups[ws._id];
85
+ if (!subscriptionCleanups) {
86
+ delete this.subscriptionCleanups[ws._id];
87
+ delete this.connections[ws._id];
88
+ delete this._onSettingsChange[ws._id];
89
+ log("Disconnected from RPC client (no subscriptions to clean)", ws._id);
90
+ return;
91
+ }
92
+ for (const subscriptionId in subscriptionCleanups) {
93
+ await subscriptionCleanups[subscriptionId]();
94
+ delete subscriptionCleanups[subscriptionId];
95
+ }
96
+ delete this.subscriptionCleanups[ws._id];
97
+ delete this.connections[ws._id];
98
+ delete this._onSettingsChange[ws._id];
99
+ log("Disconnected from RPC client", ws._id);
100
+ });
101
+ // register all JSON RPC methods
102
+ this.rpcWebsocketsRegister("getComment", this.getComment.bind(this));
103
+ this.rpcWebsocketsRegister("getCommunityPage", this.getCommunityPage.bind(this));
104
+ this.rpcWebsocketsRegister("getCommentPage", this.getCommentPage.bind(this));
105
+ this.rpcWebsocketsRegister("createCommunity", this.createCommunity.bind(this));
106
+ this.rpcWebsocketsRegister("startCommunity", this.startCommunity.bind(this));
107
+ this.rpcWebsocketsRegister("stopCommunity", this.stopCommunity.bind(this));
108
+ this.rpcWebsocketsRegister("editCommunity", this.editCommunity.bind(this));
109
+ this.rpcWebsocketsRegister("deleteCommunity", this.deleteCommunity.bind(this));
110
+ this.rpcWebsocketsRegister("communitiesSubscribe", this.communitiesSubscribe.bind(this));
111
+ this.rpcWebsocketsRegister("settingsSubscribe", this.settingsSubscribe.bind(this));
112
+ this.rpcWebsocketsRegister("fetchCid", this.fetchCid.bind(this));
113
+ this.rpcWebsocketsRegister("resolveAuthorName", this.resolveAuthorName.bind(this));
114
+ this.rpcWebsocketsRegister("setSettings", this.setSettings.bind(this));
115
+ // JSON RPC pubsub methods
116
+ this.rpcWebsocketsRegister("commentUpdateSubscribe", this.commentUpdateSubscribe.bind(this));
117
+ this.rpcWebsocketsRegister("communityUpdateSubscribe", this.communityUpdateSubscribe.bind(this));
118
+ this.rpcWebsocketsRegister("publishComment", this.publishComment.bind(this));
119
+ this.rpcWebsocketsRegister("publishCommunityEdit", this.publishCommunityEdit.bind(this));
120
+ this.rpcWebsocketsRegister("publishVote", this.publishVote.bind(this));
121
+ this.rpcWebsocketsRegister("publishCommentEdit", this.publishCommentEdit.bind(this));
122
+ this.rpcWebsocketsRegister("publishCommentModeration", this.publishCommentModeration.bind(this));
123
+ this.rpcWebsocketsRegister("publishChallengeAnswers", this.publishChallengeAnswers.bind(this));
124
+ this.rpcWebsocketsRegister("unsubscribe", this.unsubscribe.bind(this));
125
+ hideClassPrivateProps(this);
126
+ }
127
+ async getStartedCommunity(address) {
128
+ if (!(address in this._startedCommunities))
129
+ throw Error("Can't call getStartedCommunity when the community hasn't been started");
130
+ // if pending, wait until no longer pendng
131
+ while (this._startedCommunities[address] === "pending") {
132
+ await new Promise((r) => setTimeout(r, 20));
133
+ }
134
+ return this._startedCommunities[address];
135
+ }
136
+ _emitError(error) {
137
+ if (this.listeners("error").length === 0)
138
+ log.error("Unhandled error. This may crash your process, you need to listen for error event on PKCRpcWsServer", error);
139
+ this.emit("error", error);
140
+ }
141
+ // SQLite-based state management for auto-start functionality
142
+ _getRpcStateDb() {
143
+ if (this._rpcStateDb)
144
+ return this._rpcStateDb;
145
+ const dataPath = this.pkc.dataPath;
146
+ if (!dataPath)
147
+ return undefined;
148
+ const rpcServerDir = path.join(dataPath, "rpc-server");
149
+ mkdirSync(rpcServerDir, { recursive: true });
150
+ const dbPath = path.join(rpcServerDir, "rpc-state.db");
151
+ this._rpcStateDb = new Database(dbPath);
152
+ this._rpcStateDb.pragma("journal_mode = WAL");
153
+ // Auto-migrate old table name
154
+ try {
155
+ this._rpcStateDb.exec("ALTER TABLE community_states RENAME TO community_states");
156
+ }
157
+ catch (_) { }
158
+ this._rpcStateDb.exec(`
159
+ CREATE TABLE IF NOT EXISTS community_states (
160
+ address TEXT PRIMARY KEY,
161
+ wasStarted INTEGER NOT NULL DEFAULT 0,
162
+ wasExplicitlyStopped INTEGER NOT NULL DEFAULT 0
163
+ )
164
+ `);
165
+ return this._rpcStateDb;
166
+ }
167
+ _updateCommunityState(address, update) {
168
+ const db = this._getRpcStateDb();
169
+ if (!db)
170
+ return;
171
+ // Ensure row exists with defaults (INSERT OR IGNORE won't fail if row already exists)
172
+ db.prepare("INSERT OR IGNORE INTO community_states (address) VALUES (?)").run(address);
173
+ // Update only the specified fields
174
+ if (update.wasStarted !== undefined) {
175
+ db.prepare("UPDATE community_states SET wasStarted = ? WHERE address = ?").run(update.wasStarted ? 1 : 0, address);
176
+ }
177
+ if (update.wasExplicitlyStopped !== undefined) {
178
+ db.prepare("UPDATE community_states SET wasExplicitlyStopped = ? WHERE address = ?").run(update.wasExplicitlyStopped ? 1 : 0, address);
179
+ }
180
+ }
181
+ _removeCommunityState(address) {
182
+ const db = this._getRpcStateDb();
183
+ if (!db)
184
+ return;
185
+ db.prepare("DELETE FROM community_states WHERE address = ?").run(address);
186
+ }
187
+ async _autoStartPreviousCommunities() {
188
+ if (!this._autoStartOnBoot)
189
+ return;
190
+ const autoStartLog = Logger("pkc-js-rpc:pkc-ws-server:auto-start");
191
+ autoStartLog("Checking for previously started communities to auto-start");
192
+ const db = this._getRpcStateDb();
193
+ if (!db)
194
+ return;
195
+ const rows = db.prepare("SELECT address FROM community_states WHERE wasStarted = 1 AND wasExplicitlyStopped = 0").all();
196
+ const pkc = await this._getPKCInstance();
197
+ const localCommunities = pkc.communities;
198
+ for (const row of rows) {
199
+ if (!localCommunities.includes(row.address)) {
200
+ autoStartLog(`Skipping auto-start for ${row.address} - community no longer exists`);
201
+ this._removeCommunityState(row.address);
202
+ continue;
203
+ }
204
+ if (row.address in this._startedCommunities) {
205
+ autoStartLog(`Skipping auto-start for ${row.address} - already started`);
206
+ continue;
207
+ }
208
+ autoStartLog(`Auto-starting community: ${row.address}`);
209
+ try {
210
+ await this._internalStartCommunity(row.address);
211
+ autoStartLog(`Successfully auto-started: ${row.address}`);
212
+ }
213
+ catch (e) {
214
+ autoStartLog.error(`Failed to auto-start community ${row.address}`, e);
215
+ this._emitError(e instanceof Error ? e : new Error(`Failed to auto-start ${row.address}: ${String(e)}`));
216
+ }
217
+ }
218
+ }
219
+ async _internalStartCommunity(address) {
220
+ const pkc = await this._getPKCInstance();
221
+ this._startedCommunities[address] = "pending";
222
+ try {
223
+ const community = await pkc.createCommunity({ address });
224
+ community.started = true;
225
+ await community.start();
226
+ this._startedCommunities[address] = community;
227
+ this._updateCommunityState(address, { wasStarted: true, wasExplicitlyStopped: false });
228
+ return community;
229
+ }
230
+ catch (e) {
231
+ delete this._startedCommunities[address];
232
+ throw e;
233
+ }
234
+ }
235
+ // util function to log errors of registered methods
236
+ rpcWebsocketsRegister(method, callback) {
237
+ const callbackWithErrorHandled = async (params, connectionId) => {
238
+ try {
239
+ const res = await callback(params, connectionId);
240
+ return res;
241
+ }
242
+ catch (e) {
243
+ const typedError = e;
244
+ log.error(`${callback.name} error`, { params, error: typedError });
245
+ // We need to stringify the error here because rpc-websocket will remove props from PKCError
246
+ if (typedError instanceof PKCError) {
247
+ const errorJson = JSON.parse(JSON.stringify(typedError));
248
+ delete errorJson["stack"];
249
+ throw errorJson;
250
+ }
251
+ else
252
+ throw typedError;
253
+ }
254
+ };
255
+ this.rpcWebsockets.register(method, callbackWithErrorHandled);
256
+ // register localhost:9138/<auth-key> to bypass block on non-localhost requests, using /<auth-key> as namespace
257
+ if (this.authKey) {
258
+ this.rpcWebsockets.register(method, callbackWithErrorHandled, `/${this.authKey}`);
259
+ }
260
+ }
261
+ // send json rpc notification message (no id field, but must have subscription id)
262
+ jsonRpcSendNotification({ method, result, subscription, event, connectionId }) {
263
+ const message = {
264
+ jsonrpc: "2.0",
265
+ method,
266
+ params: {
267
+ result: sanitizeRpcNotificationResult(event, result),
268
+ subscription,
269
+ event
270
+ }
271
+ };
272
+ this.connections[connectionId]?.send?.(JSON.stringify(message));
273
+ }
274
+ _registerPublishing(subscriptionId, publication, pkc, connectionId) {
275
+ this.publishing[subscriptionId] = { publication, pkc, connectionId };
276
+ }
277
+ _clearPublishing(subscriptionId) {
278
+ const record = this.publishing[subscriptionId];
279
+ if (record?.timeout)
280
+ clearTimeout(record.timeout);
281
+ delete this.publishing[subscriptionId];
282
+ }
283
+ async _forceCleanupPublication(subscriptionId, reason) {
284
+ const record = this.publishing[subscriptionId];
285
+ if (!record)
286
+ return;
287
+ const cleanup = await this.subscriptionCleanups?.[record.connectionId]?.[subscriptionId];
288
+ log(`Force-cleaning publication ${subscriptionId} after ${reason}`);
289
+ if (cleanup) {
290
+ await cleanup();
291
+ if (this.subscriptionCleanups?.[record.connectionId])
292
+ delete this.subscriptionCleanups[record.connectionId][subscriptionId];
293
+ }
294
+ this._clearPublishing(subscriptionId);
295
+ await this._retirePKCIfNeeded(record.pkc);
296
+ }
297
+ async _retirePKCIfNeeded(pkc) {
298
+ const activePublishes = Object.values(this.publishing).filter(({ pkc: p }) => p === pkc).length;
299
+ if (activePublishes === 0 && !pkc.destroyed) {
300
+ // nothing relies on this instance anymore
301
+ await pkc.destroy().catch((error) => log.error("Failed destroying old pkc immediately after setSettings", { error }));
302
+ return;
303
+ }
304
+ }
305
+ async _getPKCInstance() {
306
+ await this._setSettingsQueue;
307
+ return this.pkc;
308
+ }
309
+ async getComment(params) {
310
+ const getCommentArgs = parseRpcCidParam(params[0]);
311
+ const comment = await (await this._getPKCInstance()).getComment(getCommentArgs);
312
+ // TODO may need to be changed later
313
+ return comment.raw.comment;
314
+ }
315
+ async getCommunityPage(params) {
316
+ const { cid: pageCid, communityAddress, type, pageMaxSize } = parseRpcCommunityPageParam(params[0]);
317
+ const pkc = await this._getPKCInstance();
318
+ // Use started community to fetch the page if possible, to expedite the process
319
+ const community = communityAddress in this._startedCommunities
320
+ ? await this.getStartedCommunity(communityAddress)
321
+ : await pkc.createCommunity({ address: communityAddress });
322
+ const { page } = type === "posts"
323
+ ? await community.posts._fetchAndVerifyPage({ pageCid, pageMaxSize })
324
+ : await community.modQueue._fetchAndVerifyPage({ pageCid, pageMaxSize });
325
+ const runtimeFields = buildPageRuntimeFields(page, pkc._memCaches.nameResolvedCache);
326
+ return { page, runtimeFields };
327
+ }
328
+ async getCommentPage(params) {
329
+ const { cid: pageCid, commentCid, communityAddress, pageMaxSize } = parseRpcCommentRepliesPageParam(params[0]);
330
+ const pkc = await this._getPKCInstance();
331
+ const comment = await pkc.createComment({ cid: commentCid, communityAddress });
332
+ const { page } = await comment.replies._fetchAndVerifyPage({ pageCid, pageMaxSize });
333
+ const runtimeFields = buildPageRuntimeFields(page, pkc._memCaches.nameResolvedCache);
334
+ return { page, runtimeFields };
335
+ }
336
+ async createCommunity(params) {
337
+ const createCommunityOptions = parseCreateNewLocalCommunityUserOptionsSchemaWithPKCErrorIfItFails(params[0]);
338
+ const pkc = await this._getPKCInstance();
339
+ const community = await pkc.createCommunity(createCommunityOptions);
340
+ if (!(community instanceof LocalCommunity))
341
+ throw Error("Failed to create a local community. This is a critical error");
342
+ return community.toJSONInternalRpcBeforeFirstUpdate();
343
+ }
344
+ _trackCommunityListener(community, event, listener) {
345
+ let listenersByEvent = this._trackedCommunityListeners.get(community);
346
+ if (!listenersByEvent) {
347
+ listenersByEvent = new Map();
348
+ this._trackedCommunityListeners.set(community, listenersByEvent);
349
+ }
350
+ let listeners = listenersByEvent.get(event);
351
+ if (!listeners) {
352
+ listeners = new Set();
353
+ listenersByEvent.set(event, listeners);
354
+ }
355
+ listeners.add(listener);
356
+ }
357
+ _untrackCommunityListener(community, event, listener) {
358
+ const listenersByEvent = this._trackedCommunityListeners.get(community);
359
+ if (!listenersByEvent)
360
+ return;
361
+ const listeners = listenersByEvent.get(event);
362
+ if (!listeners)
363
+ return;
364
+ listeners.delete(listener);
365
+ if (listeners.size === 0)
366
+ listenersByEvent.delete(event);
367
+ if (listenersByEvent.size === 0)
368
+ this._trackedCommunityListeners.delete(community);
369
+ }
370
+ _setupStartedEvents(community, connectionId, subscriptionId) {
371
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({ method: "startCommunity", subscription: subscriptionId, event, result, connectionId });
372
+ const getUpdateJson = () => typeof community.updatedAt === "number"
373
+ ? community.toJSONInternalRpcAfterFirstUpdate()
374
+ : community.toJSONInternalRpcBeforeFirstUpdate();
375
+ const updateListener = () => {
376
+ const json = getUpdateJson();
377
+ // Merge preloaded page runtimeFields into existing runtimeFields
378
+ const communityIpfsRecord = community.raw.communityIpfs;
379
+ if (communityIpfsRecord?.posts?.pages && "runtimeFields" in json && json.runtimeFields) {
380
+ Object.assign(json.runtimeFields, {
381
+ posts: { pages: buildPagesRuntimeFields(communityIpfsRecord.posts.pages, community._pkc._memCaches.nameResolvedCache) }
382
+ });
383
+ }
384
+ sendEvent("update", json);
385
+ };
386
+ community.on("update", updateListener);
387
+ this._trackCommunityListener(community, "update", updateListener);
388
+ const startedStateListener = () => sendEvent("startedstatechange", { state: community.startedState });
389
+ community.on("startedstatechange", startedStateListener);
390
+ this._trackCommunityListener(community, "startedstatechange", startedStateListener);
391
+ const requestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
392
+ community.on("challengerequest", requestListener);
393
+ this._trackCommunityListener(community, "challengerequest", requestListener);
394
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
395
+ community.on("challenge", challengeListener);
396
+ this._trackCommunityListener(community, "challenge", challengeListener);
397
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
398
+ community.on("challengeanswer", challengeAnswerListener);
399
+ this._trackCommunityListener(community, "challengeanswer", challengeAnswerListener);
400
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
401
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification)
402
+ });
403
+ community.on("challengeverification", challengeVerificationListener);
404
+ this._trackCommunityListener(community, "challengeverification", challengeVerificationListener);
405
+ const errorListener = (error) => {
406
+ const rpcError = error;
407
+ if (community.state === "started")
408
+ rpcError.details = { ...rpcError.details, newStartedState: community.startedState };
409
+ else if (community.state === "updating")
410
+ rpcError.details = { ...rpcError.details, newUpdatingState: community.updatingState };
411
+ log("community rpc error", rpcError);
412
+ sendEvent("error", rpcError);
413
+ };
414
+ community.on("error", errorListener);
415
+ this._trackCommunityListener(community, "error", errorListener);
416
+ // cleanup function
417
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
418
+ community.removeListener("update", updateListener);
419
+ this._untrackCommunityListener(community, "update", updateListener);
420
+ community.removeListener("startedstatechange", startedStateListener);
421
+ this._untrackCommunityListener(community, "startedstatechange", startedStateListener);
422
+ community.removeListener("challengerequest", requestListener);
423
+ this._untrackCommunityListener(community, "challengerequest", requestListener);
424
+ community.removeListener("challenge", challengeListener);
425
+ this._untrackCommunityListener(community, "challenge", challengeListener);
426
+ community.removeListener("challengeanswer", challengeAnswerListener);
427
+ this._untrackCommunityListener(community, "challengeanswer", challengeAnswerListener);
428
+ community.removeListener("challengeverification", challengeVerificationListener);
429
+ this._untrackCommunityListener(community, "challengeverification", challengeVerificationListener);
430
+ community.removeListener("error", errorListener);
431
+ this._untrackCommunityListener(community, "error", errorListener);
432
+ if (this._onSettingsChange[connectionId])
433
+ delete this._onSettingsChange[connectionId][subscriptionId];
434
+ };
435
+ }
436
+ async startCommunity(params, connectionId) {
437
+ const { address } = parseRpcCommunityAddressParam(params[0]);
438
+ const pkc = await this._getPKCInstance();
439
+ const localCommunities = pkc.communities;
440
+ if (!localCommunities.includes(address))
441
+ throw new PKCError("ERR_RPC_CLIENT_ATTEMPTING_TO_START_A_REMOTE_COMMUNITY", { communityAddress: address });
442
+ const subscriptionId = generateSubscriptionId();
443
+ const startCommunityImpl = async () => {
444
+ const pkc = await this._getPKCInstance();
445
+ const isCommunityStarted = address in this._startedCommunities;
446
+ if (isCommunityStarted) {
447
+ const community = await this.getStartedCommunity(address);
448
+ this._setupStartedEvents(community, connectionId, subscriptionId);
449
+ }
450
+ else {
451
+ try {
452
+ this._startedCommunities[address] = "pending";
453
+ const community = await pkc.createCommunity({ address });
454
+ this._setupStartedEvents(community, connectionId, subscriptionId);
455
+ community.started = true; // a small hack to make sure first update has started=true
456
+ community.emit("update", community); // Need to emit an update so rpc user can receive community props prior to running
457
+ await community.start();
458
+ this._startedCommunities[address] = community;
459
+ this._updateCommunityState(address, { wasStarted: true, wasExplicitlyStopped: false });
460
+ }
461
+ catch (e) {
462
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
463
+ if (cleanup)
464
+ await cleanup();
465
+ delete this._startedCommunities[address];
466
+ throw e;
467
+ }
468
+ }
469
+ };
470
+ this._onSettingsChange[connectionId][subscriptionId] = async ({ newPKC }) => {
471
+ const current = this._startedCommunities[address];
472
+ if (!current || current === "pending")
473
+ return;
474
+ const community = await this.getStartedCommunity(address);
475
+ // mark as pending so other consumers wait while we restart with the new pkc instance
476
+ this._startedCommunities[address] = "pending";
477
+ try {
478
+ await community.stop();
479
+ community._pkc = newPKC;
480
+ await community.start();
481
+ this._startedCommunities[address] = community;
482
+ }
483
+ catch (error) {
484
+ delete this._startedCommunities[address];
485
+ throw error;
486
+ }
487
+ };
488
+ await startCommunityImpl();
489
+ return subscriptionId;
490
+ }
491
+ async stopCommunity(params) {
492
+ const { address } = parseRpcCommunityAddressParam(params[0]);
493
+ const pkc = await this._getPKCInstance();
494
+ const localCommunities = pkc.communities;
495
+ if (!localCommunities.includes(address))
496
+ throw new PKCError("ERR_RPC_CLIENT_TRYING_TO_STOP_REMOTE_COMMUNITY", { communityAddress: address });
497
+ const isCommunityStarted = address in this._startedCommunities;
498
+ if (!isCommunityStarted)
499
+ throw new PKCError("ERR_RPC_CLIENT_TRYING_TO_STOP_COMMUNITY_THAT_IS_NOT_RUNNING", { communityAddress: address });
500
+ const startedCommunity = await this.getStartedCommunity(address);
501
+ await startedCommunity.stop();
502
+ // emit last updates so subscribed instances can set their state to stopped
503
+ await this._postStoppingOrDeleting(startedCommunity);
504
+ delete this._startedCommunities[address];
505
+ this._updateCommunityState(address, { wasExplicitlyStopped: true });
506
+ return true;
507
+ }
508
+ async _postStoppingOrDeleting(community) {
509
+ // emit the last updates
510
+ // remove all listeners
511
+ community.emit("update", community);
512
+ community.emit("startedstatechange", community.startedState);
513
+ const trackedListeners = this._trackedCommunityListeners.get(community);
514
+ if (trackedListeners) {
515
+ for (const [event, listeners] of trackedListeners) {
516
+ for (const listener of listeners) {
517
+ community.removeListener(event, listener);
518
+ }
519
+ }
520
+ this._trackedCommunityListeners.delete(community);
521
+ }
522
+ }
523
+ async editCommunity(params) {
524
+ const rawParam = params[0];
525
+ rawParam.editOptions = replaceXWithY(rawParam.editOptions, null, undefined);
526
+ const { address, editOptions } = parseRpcEditCommunityParam(rawParam);
527
+ const editCommunityOptions = parseCommunityEditOptionsSchemaWithPKCErrorIfItFails(editOptions);
528
+ const pkc = await this._getPKCInstance();
529
+ const localCommunities = pkc.communities;
530
+ if (!localCommunities.includes(address))
531
+ throw new PKCError("ERR_RPC_CLIENT_TRYING_TO_EDIT_REMOTE_COMMUNITY", { communityAddress: address });
532
+ let community;
533
+ if (this._startedCommunities[address] instanceof LocalCommunity)
534
+ community = this._startedCommunities[address];
535
+ else {
536
+ community = await pkc.createCommunity({ address });
537
+ community.once("error", (error) => {
538
+ log.error("RPC server Received an error on community", community.address, "edit", error);
539
+ });
540
+ }
541
+ await community.edit(editCommunityOptions);
542
+ if (editCommunityOptions.address && this._startedCommunities[address]) {
543
+ // if (editCommunityOptions.address && this._startedCommunities[address] && editCommunityOptions.address !== address) {
544
+ this._startedCommunities[editCommunityOptions.address] = this._startedCommunities[address];
545
+ delete this._startedCommunities[address];
546
+ // Update RPC state with new address
547
+ const db = this._getRpcStateDb();
548
+ if (db) {
549
+ db.prepare("UPDATE community_states SET address = @newAddress WHERE address = @oldAddress").run({
550
+ newAddress: editCommunityOptions.address,
551
+ oldAddress: address
552
+ });
553
+ }
554
+ }
555
+ if (typeof community.updatedAt === "number")
556
+ return community.toJSONInternalRpcAfterFirstUpdate();
557
+ else
558
+ return community.toJSONInternalRpcBeforeFirstUpdate();
559
+ }
560
+ async deleteCommunity(params) {
561
+ const { address } = parseRpcCommunityAddressParam(params[0]);
562
+ const pkc = await this._getPKCInstance();
563
+ const addresses = pkc.communities;
564
+ if (!addresses.includes(address))
565
+ throw new PKCError("ERR_RPC_CLIENT_TRYING_TO_DELETE_REMOTE_COMMUNITY", { communityAddress: address });
566
+ const isCommunityStarted = address in this._startedCommunities;
567
+ const community = isCommunityStarted
568
+ ? await this.getStartedCommunity(address)
569
+ : await pkc.createCommunity({ address });
570
+ await community.delete();
571
+ await this._postStoppingOrDeleting(community);
572
+ delete this._startedCommunities[address];
573
+ this._removeCommunityState(address);
574
+ return true;
575
+ }
576
+ async communitiesSubscribe(params, connectionId) {
577
+ // TODO need to implement _onSettingsChange here
578
+ const subscriptionId = generateSubscriptionId();
579
+ const sendEvent = (event, result) => {
580
+ this.jsonRpcSendNotification({
581
+ method: "communitiesNotification",
582
+ subscription: Number(subscriptionId),
583
+ event,
584
+ result,
585
+ connectionId
586
+ });
587
+ };
588
+ const pkcSubscribeEvent = (newCommunities) => sendEvent("communitieschange", { communities: newCommunities });
589
+ const pkc = await this._getPKCInstance();
590
+ pkc.on("communitieschange", pkcSubscribeEvent);
591
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
592
+ pkc.removeListener("communitieschange", pkcSubscribeEvent);
593
+ };
594
+ sendEvent("communitieschange", { communities: pkc.communities });
595
+ return subscriptionId;
596
+ }
597
+ async fetchCid(params) {
598
+ const parsedArgs = parseRpcCidParam(params[0]);
599
+ const pkc = await this._getPKCInstance();
600
+ const res = await pkc.fetchCid(parsedArgs);
601
+ if (typeof res !== "string")
602
+ throw Error("Result of fetchCid should be a string");
603
+ return { content: res };
604
+ }
605
+ _serializeSettingsFromPKC(pkc) {
606
+ const pkcOptions = pkc.parsedPKCOptions;
607
+ const builtInChallenges = PKCJs.PKC.challenges || {};
608
+ const allChallengeFactories = {
609
+ ...builtInChallenges, // built-ins first
610
+ ...(pkc.settings?.challenges || {}) // user-defined override
611
+ };
612
+ const challenges = remeda.mapValues(allChallengeFactories, (challengeFactory) => remeda.omit(challengeFactory({ challengeSettings: {} }), ["getChallenge"]));
613
+ return { pkcOptions, challenges };
614
+ }
615
+ async settingsSubscribe(params, connectionId) {
616
+ const subscriptionId = generateSubscriptionId();
617
+ const sendEvent = (event, result) => {
618
+ this.jsonRpcSendNotification({
619
+ method: "settingsNotification",
620
+ subscription: Number(subscriptionId),
621
+ event,
622
+ result,
623
+ connectionId
624
+ });
625
+ };
626
+ const sendRpcSettings = async ({ newPKC }) => {
627
+ sendEvent("settingschange", this._serializeSettingsFromPKC(newPKC));
628
+ };
629
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
630
+ if (this._onSettingsChange[connectionId])
631
+ delete this._onSettingsChange[connectionId][subscriptionId];
632
+ };
633
+ this._onSettingsChange[connectionId][subscriptionId] = sendRpcSettings;
634
+ await sendRpcSettings({ newPKC: this.pkc });
635
+ return subscriptionId;
636
+ }
637
+ _initPKC(pkc) {
638
+ this.pkc = pkc;
639
+ pkc.on("error", (error) => log.error("RPC server", "Received an error on pkc instance", error));
640
+ }
641
+ async _createPKCInstanceFromSetSettings(newOptions) {
642
+ return PKCJs.PKC(newOptions);
643
+ }
644
+ async setSettings(params) {
645
+ const runSetSettings = async () => {
646
+ const settings = parseSetNewSettingsPKCWsServerSchemaWithPKCErrorIfItFails(params[0]);
647
+ const currentSettings = this._serializeSettingsFromPKC(this.pkc);
648
+ if (deterministicStringify(settings.pkcOptions) === deterministicStringify(currentSettings.pkcOptions)) {
649
+ log("RPC client called setSettings with the same settings as the current one, aborting");
650
+ return;
651
+ }
652
+ log(`RPC client called setSettings, the clients need to call all subscription methods again`);
653
+ const oldPKC = this.pkc;
654
+ // Strip nameResolvers from client settings — RPC server ignores them for now
655
+ const { nameResolvers: _stripNr, ...pkcOptionsWithoutNameResolvers } = settings.pkcOptions;
656
+ const newPKC = await this._createPKCInstanceFromSetSettings({
657
+ ...pkcOptionsWithoutNameResolvers,
658
+ nameResolvers: this.pkc.parsedPKCOptions.nameResolvers
659
+ });
660
+ this._initPKC(newPKC); // swap to new instance first so new RPC calls don't hit a destroyed pkc
661
+ // send a settingsNotification to all subscribers
662
+ for (const connectionId of remeda.keys.strict(this._onSettingsChange)) {
663
+ const connectionHandlers = this._onSettingsChange[connectionId];
664
+ if (!connectionHandlers)
665
+ continue;
666
+ for (const subscriptionId of remeda.keys.strict(connectionHandlers)) {
667
+ const handler = connectionHandlers[subscriptionId];
668
+ if (handler)
669
+ await handler({ newPKC });
670
+ }
671
+ }
672
+ // ensure any existing publications get a timeout if they were created before the first setSettings
673
+ for (const [subscriptionId, pub] of Object.entries(this.publishing).filter((pub) => pub[1].pkc === oldPKC)) {
674
+ pub.timeout = setTimeout(async () => {
675
+ await this._forceCleanupPublication(Number(subscriptionId), "timeout");
676
+ }, 60000);
677
+ }
678
+ setTimeout(async () => {
679
+ await this._retirePKCIfNeeded(oldPKC);
680
+ }, 60000); // set this in a timeout because createCommunity may be using it
681
+ };
682
+ const setSettingsRun = this._setSettingsQueue.then(() => runSetSettings());
683
+ // keep queue usable even if a run fails; error still propagates to the caller via setSettingsRun
684
+ this._setSettingsQueue = setSettingsRun.catch(() => { });
685
+ await setSettingsRun;
686
+ return true;
687
+ }
688
+ async commentUpdateSubscribe(params, connectionId) {
689
+ const logUpdate = Logger("pkc-js-rpc:pkc-ws-server:commentUpdateSubscribe");
690
+ const parsedCommentUpdateArgs = parseRpcCidParam(params[0]);
691
+ const subscriptionId = generateSubscriptionId();
692
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
693
+ method: "commentUpdateNotification",
694
+ subscription: subscriptionId,
695
+ event,
696
+ result,
697
+ connectionId
698
+ });
699
+ let sentCommentIpfsUpdateEvent = false;
700
+ let lastSentNameResolved = undefined;
701
+ const pkc = await this._getPKCInstance();
702
+ const comment = await pkc.createComment(parsedCommentUpdateArgs);
703
+ const sendUpdate = () => {
704
+ if (!sentCommentIpfsUpdateEvent && comment.raw.comment) {
705
+ sendEvent("comment", {
706
+ comment: comment.raw.comment,
707
+ runtimeFields: { author: { nameResolved: comment.author.nameResolved } }
708
+ });
709
+ sentCommentIpfsUpdateEvent = true;
710
+ lastSentNameResolved = comment.author.nameResolved;
711
+ }
712
+ if (comment.raw.commentUpdate) {
713
+ const updateEvent = { commentUpdate: comment.raw.commentUpdate };
714
+ const runtimeFields = {};
715
+ if (comment.raw.commentUpdate.replies?.pages) {
716
+ runtimeFields.replies = {
717
+ pages: buildPagesRuntimeFields(comment.raw.commentUpdate.replies.pages, pkc._memCaches.nameResolvedCache)
718
+ };
719
+ }
720
+ if (typeof comment.author.nameResolved === "boolean") {
721
+ runtimeFields.author = { nameResolved: comment.author.nameResolved };
722
+ }
723
+ if (Object.keys(runtimeFields).length > 0) {
724
+ updateEvent.runtimeFields = runtimeFields;
725
+ }
726
+ sendEvent("update", updateEvent);
727
+ lastSentNameResolved = comment.author.nameResolved;
728
+ }
729
+ else if (sentCommentIpfsUpdateEvent &&
730
+ typeof comment.author.nameResolved === "boolean" &&
731
+ comment.author.nameResolved !== lastSentNameResolved) {
732
+ // nameResolved changed but no commentUpdate to piggyback on — send dedicated runtimeupdate event
733
+ sendEvent("runtimeupdate", { author: { nameResolved: comment.author.nameResolved } });
734
+ lastSentNameResolved = comment.author.nameResolved;
735
+ }
736
+ };
737
+ const updateListener = () => sendUpdate();
738
+ comment.on("update", updateListener);
739
+ const updatingStateListener = () => sendEvent("updatingstatechange", { state: comment.updatingState });
740
+ comment.on("updatingstatechange", updatingStateListener);
741
+ const stateListener = () => sendEvent("statechange", { state: comment.state });
742
+ comment.on("statechange", stateListener);
743
+ const errorListener = (error) => {
744
+ const errorWithNewUpdatingState = error;
745
+ if (comment.state === "publishing")
746
+ errorWithNewUpdatingState.details = { ...errorWithNewUpdatingState.details, newPublishingState: comment.publishingState };
747
+ else if (comment.state === "updating")
748
+ errorWithNewUpdatingState.details = { ...errorWithNewUpdatingState.details, newUpdatingState: comment.updatingState };
749
+ sendEvent("error", errorWithNewUpdatingState);
750
+ };
751
+ comment.on("error", errorListener);
752
+ // cleanup function
753
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
754
+ logUpdate("Cleaning up commentUpdate subscription", { subscriptionId, connectionId, cid: comment.cid });
755
+ comment.removeListener("update", updateListener);
756
+ comment.removeListener("updatingstatechange", updatingStateListener);
757
+ comment.removeListener("statechange", stateListener);
758
+ comment.removeListener("error", errorListener);
759
+ await comment.stop();
760
+ if (this._onSettingsChange[connectionId])
761
+ delete this._onSettingsChange[connectionId][subscriptionId];
762
+ };
763
+ this._onSettingsChange[connectionId][subscriptionId] = async ({ newPKC }) => {
764
+ // TODO need to clean up and remove old comment here, and create a new comment
765
+ comment._pkc = newPKC;
766
+ await comment.update();
767
+ };
768
+ // if fail, cleanup
769
+ try {
770
+ sendUpdate();
771
+ await comment.update();
772
+ }
773
+ catch (e) {
774
+ logUpdate.error("Cleaning up subscription to comment", comment.cid, "because comment.update threw an error", e);
775
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
776
+ if (cleanup)
777
+ await cleanup();
778
+ throw e;
779
+ }
780
+ return subscriptionId;
781
+ }
782
+ async communityUpdateSubscribe(params, connectionId) {
783
+ const parsedCommunityUpdateArgs = parseRpcCommunityLookupParam(params[0]);
784
+ const subscriptionId = generateSubscriptionId();
785
+ await this._bindCommunityUpdateSubscription(parsedCommunityUpdateArgs, connectionId, subscriptionId);
786
+ return subscriptionId;
787
+ }
788
+ async _bindCommunityUpdateSubscription(parsedArgs, connectionId, subscriptionId) {
789
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
790
+ method: "communityUpdateNotification",
791
+ subscription: subscriptionId,
792
+ event,
793
+ result,
794
+ connectionId
795
+ });
796
+ const pkc = await this._getPKCInstance();
797
+ const startedCommunity = findStartedCommunity(pkc, parsedArgs);
798
+ const isStartedCommunity = Boolean(startedCommunity);
799
+ const community = startedCommunity || await pkc.createCommunity(parsedArgs);
800
+ const sendCommunityJson = () => {
801
+ let jsonToSend;
802
+ if (community instanceof LocalCommunity)
803
+ jsonToSend =
804
+ typeof community.updatedAt === "number"
805
+ ? community.toJSONInternalRpcAfterFirstUpdate()
806
+ : community.toJSONInternalRpcBeforeFirstUpdate();
807
+ else
808
+ jsonToSend = community.toJSONRpcRemote();
809
+ // Merge preloaded page runtimeFields into existing runtimeFields
810
+ const communityIpfsRecord = community.raw.communityIpfs;
811
+ if (communityIpfsRecord?.posts?.pages && "runtimeFields" in jsonToSend && jsonToSend.runtimeFields) {
812
+ Object.assign(jsonToSend.runtimeFields, {
813
+ posts: { pages: buildPagesRuntimeFields(communityIpfsRecord.posts.pages, pkc._memCaches.nameResolvedCache) }
814
+ });
815
+ }
816
+ sendEvent("update", jsonToSend);
817
+ };
818
+ const updateListener = () => sendCommunityJson();
819
+ community.on("update", updateListener);
820
+ const updatingStateListener = () => sendEvent("updatingstatechange", { state: community.updatingState });
821
+ community.on("updatingstatechange", updatingStateListener);
822
+ // listener for startestatechange
823
+ const startedStateListener = () => sendEvent("updatingstatechange", { state: community.startedState });
824
+ if (isStartedCommunity) {
825
+ community.on("startedstatechange", startedStateListener);
826
+ }
827
+ const errorListener = (error) => {
828
+ const rpcError = error;
829
+ if (community.state === "started")
830
+ rpcError.details = { ...rpcError.details, newStartedState: community.startedState };
831
+ else if (community.state === "updating")
832
+ rpcError.details = { ...rpcError.details, newUpdatingState: community.updatingState };
833
+ log("community rpc error", rpcError);
834
+ sendEvent("error", rpcError);
835
+ };
836
+ community.on("error", errorListener);
837
+ // cleanup function
838
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
839
+ log("Cleaning up community", community.address, "client subscription");
840
+ community.removeListener("update", updateListener);
841
+ community.removeListener("updatingstatechange", updatingStateListener);
842
+ community.removeListener("error", errorListener);
843
+ community.removeListener("startedstatechange", startedStateListener);
844
+ if (this._onSettingsChange[connectionId])
845
+ delete this._onSettingsChange[connectionId][subscriptionId];
846
+ // We don't wanna stop the local community if it's running already, this function is just for fetching updates
847
+ // if we comment this out remove test passes
848
+ if (!isStartedCommunity && community.state !== "stopped")
849
+ await community.stop();
850
+ };
851
+ this._onSettingsChange[connectionId][subscriptionId] = async ({ newPKC }) => {
852
+ // TODO this may need changing
853
+ if (!isStartedCommunity) {
854
+ community._pkc = newPKC;
855
+ await community.stop();
856
+ await community.update();
857
+ }
858
+ };
859
+ // if fail, cleanup
860
+ try {
861
+ // need to send an update with first communityUpdate if it's a local community
862
+ if ("signer" in community || community.raw.communityIpfs)
863
+ sendCommunityJson();
864
+ // No need to call .update() if it's already running locally because we're listening to update event
865
+ if (!isStartedCommunity)
866
+ await community.update();
867
+ }
868
+ catch (e) {
869
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
870
+ if (cleanup)
871
+ await cleanup();
872
+ throw e;
873
+ }
874
+ }
875
+ async _createCommentInstanceFromPublishCommentParams(params) {
876
+ const pkc = await this._getPKCInstance();
877
+ const comment = await pkc.createComment(params.comment);
878
+ comment.challengeRequest = remeda.omit(params, ["comment"]);
879
+ return comment;
880
+ }
881
+ async publishComment(params, connectionId) {
882
+ // TODO need to implement _onSettingsChange here
883
+ const publishOptions = parseCommentChallengeRequestToEncryptSchemaWithPKCErrorIfItFails(params[0]);
884
+ const subscriptionId = generateSubscriptionId();
885
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
886
+ method: "publishCommentNotification",
887
+ subscription: subscriptionId,
888
+ event,
889
+ result,
890
+ connectionId
891
+ });
892
+ const comment = await this._createCommentInstanceFromPublishCommentParams(publishOptions);
893
+ this._registerPublishing(subscriptionId, comment, comment._pkc, connectionId);
894
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
895
+ comment.on("challenge", challengeListener);
896
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
897
+ comment.on("challengeanswer", challengeAnswerListener);
898
+ const challengeRequestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
899
+ comment.on("challengerequest", challengeRequestListener);
900
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
901
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification),
902
+ runtimeFields: { author: { nameResolved: comment.author.nameResolved } }
903
+ });
904
+ comment.on("challengeverification", challengeVerificationListener);
905
+ const publishingStateListener = () => {
906
+ sendEvent("publishingstatechange", { state: comment.publishingState });
907
+ };
908
+ comment.on("publishingstatechange", publishingStateListener);
909
+ const stateListener = () => sendEvent("statechange", { state: comment.state });
910
+ comment.on("statechange", stateListener);
911
+ const errorListener = (error) => {
912
+ const commentRpcError = error;
913
+ commentRpcError.details = {
914
+ ...commentRpcError.details,
915
+ newPublishingState: comment.publishingState
916
+ };
917
+ sendEvent("error", commentRpcError);
918
+ };
919
+ comment.on("error", errorListener);
920
+ // cleanup function
921
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
922
+ comment.removeListener("challenge", challengeListener);
923
+ comment.removeListener("challengeanswer", challengeAnswerListener);
924
+ comment.removeListener("challengerequest", challengeRequestListener);
925
+ comment.removeListener("challengeverification", challengeVerificationListener);
926
+ comment.removeListener("publishingstatechange", publishingStateListener);
927
+ comment.removeListener("statechange", stateListener);
928
+ comment.removeListener("error", errorListener);
929
+ await comment.stop();
930
+ this._clearPublishing(subscriptionId);
931
+ if (this._onSettingsChange[connectionId])
932
+ delete this._onSettingsChange[connectionId][subscriptionId];
933
+ };
934
+ // if fail, cleanup
935
+ try {
936
+ await comment.publish();
937
+ }
938
+ catch (e) {
939
+ const error = e;
940
+ error.details = { ...error.details, publishThrowError: true };
941
+ errorListener(error);
942
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
943
+ if (cleanup)
944
+ await cleanup();
945
+ return subscriptionId;
946
+ }
947
+ return subscriptionId;
948
+ }
949
+ async _createVoteInstanceFromPublishVoteParams(params) {
950
+ const pkc = await this._getPKCInstance();
951
+ const vote = await pkc.createVote(params.vote);
952
+ vote.challengeRequest = remeda.omit(params, ["vote"]);
953
+ return vote;
954
+ }
955
+ async publishVote(params, connectionId) {
956
+ // TODO need to implement _onSettingsChange here
957
+ const publishOptions = parseVoteChallengeRequestToEncryptSchemaWithPKCErrorIfItFails(params[0]);
958
+ // TODO need to think, what happens if user never sends a unsubsribe call?
959
+ // publication will never get removed from this.publishing
960
+ const subscriptionId = generateSubscriptionId();
961
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({ method: "publishVoteNotification", subscription: subscriptionId, event, result, connectionId });
962
+ const vote = await this._createVoteInstanceFromPublishVoteParams(publishOptions);
963
+ this._registerPublishing(subscriptionId, vote, vote._pkc, connectionId);
964
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
965
+ vote.on("challenge", challengeListener);
966
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
967
+ vote.on("challengeanswer", challengeAnswerListener);
968
+ const challengeRequestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
969
+ vote.on("challengerequest", challengeRequestListener);
970
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
971
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification)
972
+ });
973
+ vote.on("challengeverification", challengeVerificationListener);
974
+ const publishingStateListener = () => sendEvent("publishingstatechange", { state: vote.publishingState });
975
+ vote.on("publishingstatechange", publishingStateListener);
976
+ const errorListener = (error) => {
977
+ const voteRpcError = error;
978
+ voteRpcError.details = { ...voteRpcError.details, newPublishingState: vote.publishingState };
979
+ sendEvent("error", voteRpcError);
980
+ };
981
+ vote.on("error", errorListener);
982
+ // cleanup function
983
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
984
+ this._clearPublishing(subscriptionId);
985
+ await vote.stop();
986
+ vote.removeListener("challenge", challengeListener);
987
+ vote.removeListener("challengeanswer", challengeAnswerListener);
988
+ vote.removeListener("challengerequest", challengeRequestListener);
989
+ vote.removeListener("challengeverification", challengeVerificationListener);
990
+ vote.removeListener("publishingstatechange", publishingStateListener);
991
+ vote.removeListener("error", errorListener);
992
+ };
993
+ // if fail, cleanup
994
+ try {
995
+ await vote.publish();
996
+ }
997
+ catch (e) {
998
+ const error = e;
999
+ error.details = { ...error.details, publishThrowError: true };
1000
+ errorListener(error);
1001
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
1002
+ if (cleanup)
1003
+ await cleanup();
1004
+ }
1005
+ return subscriptionId;
1006
+ }
1007
+ async _createCommunityEditInstanceFromPublishCommunityEditParams(params) {
1008
+ const pkc = await this._getPKCInstance();
1009
+ const communityEdit = await pkc.createCommunityEdit(params.communityEdit);
1010
+ communityEdit.challengeRequest = remeda.omit(params, ["communityEdit"]);
1011
+ return communityEdit;
1012
+ }
1013
+ async publishCommunityEdit(params, connectionId) {
1014
+ // TODO need to implement _onSettingsChange here
1015
+ const publishOptions = parseCommunityEditChallengeRequestToEncryptSchemaWithPKCErrorIfItFails(params[0]);
1016
+ const subscriptionId = generateSubscriptionId();
1017
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
1018
+ method: "publishCommunityEditNotification",
1019
+ subscription: subscriptionId,
1020
+ event,
1021
+ result,
1022
+ connectionId
1023
+ });
1024
+ const communityEdit = await this._createCommunityEditInstanceFromPublishCommunityEditParams(publishOptions);
1025
+ this._registerPublishing(subscriptionId, communityEdit, communityEdit._pkc, connectionId);
1026
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
1027
+ communityEdit.on("challenge", challengeListener);
1028
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
1029
+ communityEdit.on("challengeanswer", challengeAnswerListener);
1030
+ const challengeRequestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
1031
+ communityEdit.on("challengerequest", challengeRequestListener);
1032
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
1033
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification)
1034
+ });
1035
+ communityEdit.on("challengeverification", challengeVerificationListener);
1036
+ const publishingStateListener = () => sendEvent("publishingstatechange", { state: communityEdit.publishingState });
1037
+ communityEdit.on("publishingstatechange", publishingStateListener);
1038
+ const errorListener = (error) => {
1039
+ const editRpcError = error;
1040
+ editRpcError.details = { ...editRpcError.details, newPublishingState: communityEdit.publishingState };
1041
+ sendEvent("error", editRpcError);
1042
+ };
1043
+ communityEdit.on("error", errorListener);
1044
+ // cleanup function
1045
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
1046
+ this._clearPublishing(subscriptionId);
1047
+ await communityEdit.stop();
1048
+ communityEdit.removeListener("challenge", challengeListener);
1049
+ communityEdit.removeListener("challengeanswer", challengeAnswerListener);
1050
+ communityEdit.removeListener("challengerequest", challengeRequestListener);
1051
+ communityEdit.removeListener("challengeverification", challengeVerificationListener);
1052
+ communityEdit.removeListener("publishingstatechange", publishingStateListener);
1053
+ communityEdit.removeListener("error", errorListener);
1054
+ };
1055
+ // if fail, cleanup
1056
+ try {
1057
+ await communityEdit.publish();
1058
+ }
1059
+ catch (e) {
1060
+ const error = e;
1061
+ error.details = { ...error.details, publishThrowError: true };
1062
+ errorListener(error);
1063
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
1064
+ if (cleanup)
1065
+ await cleanup();
1066
+ }
1067
+ return subscriptionId;
1068
+ }
1069
+ async _createCommentEditInstanceFromPublishCommentEditParams(params) {
1070
+ const pkc = await this._getPKCInstance();
1071
+ const commentEdit = await pkc.createCommentEdit(params.commentEdit);
1072
+ commentEdit.challengeRequest = remeda.omit(params, ["commentEdit"]);
1073
+ return commentEdit;
1074
+ }
1075
+ async publishCommentEdit(params, connectionId) {
1076
+ // TODO need to implement _onSettingsChange here
1077
+ const publishOptions = parseCommentEditChallengeRequestToEncryptSchemaWithPKCErrorIfItFails(params[0]);
1078
+ const subscriptionId = generateSubscriptionId();
1079
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
1080
+ method: "publishCommentEditNotification",
1081
+ subscription: subscriptionId,
1082
+ event,
1083
+ result,
1084
+ connectionId
1085
+ });
1086
+ const commentEdit = await this._createCommentEditInstanceFromPublishCommentEditParams(publishOptions);
1087
+ this._registerPublishing(subscriptionId, commentEdit, commentEdit._pkc, connectionId);
1088
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
1089
+ commentEdit.on("challenge", challengeListener);
1090
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
1091
+ commentEdit.on("challengeanswer", challengeAnswerListener);
1092
+ const challengeRequestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
1093
+ commentEdit.on("challengerequest", challengeRequestListener);
1094
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
1095
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification)
1096
+ });
1097
+ commentEdit.on("challengeverification", challengeVerificationListener);
1098
+ const publishingStateListener = () => sendEvent("publishingstatechange", { state: commentEdit.publishingState });
1099
+ commentEdit.on("publishingstatechange", publishingStateListener);
1100
+ const errorListener = (error) => {
1101
+ const commentEditRpcError = error;
1102
+ commentEditRpcError.details = {
1103
+ ...commentEditRpcError.details,
1104
+ newPublishingState: commentEdit.publishingState
1105
+ };
1106
+ sendEvent("error", commentEditRpcError);
1107
+ };
1108
+ commentEdit.on("error", errorListener);
1109
+ // cleanup function
1110
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
1111
+ this._clearPublishing(subscriptionId);
1112
+ await commentEdit.stop();
1113
+ commentEdit.removeListener("challenge", challengeListener);
1114
+ commentEdit.removeListener("challengeanswer", challengeAnswerListener);
1115
+ commentEdit.removeListener("challengerequest", challengeRequestListener);
1116
+ commentEdit.removeListener("challengeverification", challengeVerificationListener);
1117
+ commentEdit.removeListener("publishingstatechange", publishingStateListener);
1118
+ commentEdit.removeListener("error", errorListener);
1119
+ };
1120
+ // if fail, cleanup
1121
+ try {
1122
+ await commentEdit.publish();
1123
+ }
1124
+ catch (e) {
1125
+ const error = e;
1126
+ error.details = { ...error.details, publishThrowError: true };
1127
+ errorListener(error);
1128
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
1129
+ if (cleanup)
1130
+ await cleanup();
1131
+ }
1132
+ return subscriptionId;
1133
+ }
1134
+ async _createCommentModerationInstanceFromPublishCommentModerationParams(params) {
1135
+ const pkc = await this._getPKCInstance();
1136
+ const commentModeration = await pkc.createCommentModeration(params.commentModeration);
1137
+ commentModeration.challengeRequest = remeda.omit(params, ["commentModeration"]);
1138
+ return commentModeration;
1139
+ }
1140
+ async publishCommentModeration(params, connectionId) {
1141
+ // TODO need to implement _onSettingsChange here
1142
+ const publishOptions = parseCommentModerationChallengeRequestToEncryptSchemaWithPKCErrorIfItFails(params[0]);
1143
+ const subscriptionId = generateSubscriptionId();
1144
+ const sendEvent = (event, result) => this.jsonRpcSendNotification({
1145
+ method: "publishCommentModerationNotification",
1146
+ subscription: subscriptionId,
1147
+ event,
1148
+ result,
1149
+ connectionId
1150
+ });
1151
+ const commentMod = await this._createCommentModerationInstanceFromPublishCommentModerationParams(publishOptions);
1152
+ this._registerPublishing(subscriptionId, commentMod, commentMod._pkc, connectionId);
1153
+ const challengeListener = (challenge) => sendEvent("challenge", encodeChallengeMessage(challenge));
1154
+ commentMod.on("challenge", challengeListener);
1155
+ const challengeAnswerListener = (answer) => sendEvent("challengeanswer", encodeChallengeAnswerMessage(answer));
1156
+ commentMod.on("challengeanswer", challengeAnswerListener);
1157
+ const challengeRequestListener = (request) => sendEvent("challengerequest", encodeChallengeRequest(request));
1158
+ commentMod.on("challengerequest", challengeRequestListener);
1159
+ const challengeVerificationListener = (challengeVerification) => sendEvent("challengeverification", {
1160
+ challengeVerification: encodeChallengeVerificationMessage(challengeVerification)
1161
+ });
1162
+ commentMod.on("challengeverification", challengeVerificationListener);
1163
+ const publishingStateListener = () => sendEvent("publishingstatechange", { state: commentMod.publishingState });
1164
+ commentMod.on("publishingstatechange", publishingStateListener);
1165
+ const errorListener = (error) => {
1166
+ const commentModRpcError = error;
1167
+ commentModRpcError.details = {
1168
+ ...commentModRpcError.details,
1169
+ newPublishingState: commentMod.publishingState
1170
+ };
1171
+ sendEvent("error", commentModRpcError);
1172
+ };
1173
+ commentMod.on("error", errorListener);
1174
+ // cleanup function
1175
+ this.subscriptionCleanups[connectionId][subscriptionId] = async () => {
1176
+ commentMod.removeListener("challenge", challengeListener);
1177
+ commentMod.removeListener("challengeanswer", challengeAnswerListener);
1178
+ commentMod.removeListener("challengerequest", challengeRequestListener);
1179
+ commentMod.removeListener("challengeverification", challengeVerificationListener);
1180
+ commentMod.removeListener("publishingstatechange", publishingStateListener);
1181
+ commentMod.removeListener("error", errorListener);
1182
+ await commentMod.stop();
1183
+ this._clearPublishing(subscriptionId);
1184
+ };
1185
+ // if fail, cleanup
1186
+ try {
1187
+ await commentMod.publish();
1188
+ }
1189
+ catch (e) {
1190
+ const error = e;
1191
+ error.details = { ...error.details, publishThrowError: true };
1192
+ errorListener(error);
1193
+ const cleanup = this.subscriptionCleanups?.[connectionId]?.[subscriptionId];
1194
+ if (cleanup)
1195
+ await cleanup();
1196
+ }
1197
+ return subscriptionId;
1198
+ }
1199
+ async publishChallengeAnswers(params) {
1200
+ const parsed = parseRpcPublishChallengeAnswersParam(params[0]);
1201
+ const subscriptionId = parsed.subscriptionId;
1202
+ const decryptedChallengeAnswers = parseDecryptedChallengeAnswerWithPKCErrorIfItFails({
1203
+ challengeAnswers: parsed.challengeAnswers
1204
+ });
1205
+ const record = this.publishing[subscriptionId];
1206
+ if (!record?.publication) {
1207
+ throw Error(`no subscription with id '${subscriptionId}'`);
1208
+ }
1209
+ const publication = record.publication;
1210
+ await this._getPKCInstance(); // to await for settings change
1211
+ await publication.publishChallengeAnswers(decryptedChallengeAnswers.challengeAnswers);
1212
+ return true;
1213
+ }
1214
+ async resolveAuthorName(params) {
1215
+ const parsedArgs = parseRpcAuthorNameParam(params[0]);
1216
+ const pkc = await this._getPKCInstance();
1217
+ const resolvedAuthorAddress = await pkc.resolveAuthorName(parsedArgs);
1218
+ return { resolvedAddress: resolvedAuthorAddress };
1219
+ }
1220
+ async unsubscribe(params, connectionId) {
1221
+ const { subscriptionId } = parseRpcUnsubscribeParam(params[0]);
1222
+ log("Received unsubscribe", { connectionId, subscriptionId });
1223
+ const connectionCleanups = this.subscriptionCleanups[connectionId];
1224
+ if (!connectionCleanups || !connectionCleanups[subscriptionId])
1225
+ return true;
1226
+ await connectionCleanups[subscriptionId](); // commenting this out fixes the timeout with remove.test.js
1227
+ delete connectionCleanups[subscriptionId];
1228
+ return true;
1229
+ }
1230
+ async destroy() {
1231
+ for (const connectionId of remeda.keys.strict(this.subscriptionCleanups))
1232
+ for (const subscriptionId of remeda.keys.strict(this.subscriptionCleanups[connectionId]))
1233
+ await this.unsubscribe([{ subscriptionId: Number(subscriptionId) }], connectionId);
1234
+ this.ws.close();
1235
+ const pkc = await this._getPKCInstance();
1236
+ await pkc.destroy(); // this will stop all started communities
1237
+ for (const communityAddress of remeda.keys.strict(this._startedCommunities)) {
1238
+ delete this._startedCommunities[communityAddress];
1239
+ }
1240
+ this._rpcStateDb?.close();
1241
+ this._rpcStateDb = undefined;
1242
+ this._onSettingsChange = {};
1243
+ }
1244
+ }
1245
+ const createPKCWsServer = async (options) => {
1246
+ const parsedOptions = parseCreatePKCWsServerOptionsSchemaWithPKCErrorIfItFails(options);
1247
+ const pkc = await PKCJs.PKC(parsedOptions.pkcOptions);
1248
+ const pkcWss = new PKCWsServer({
1249
+ pkc,
1250
+ port: parsedOptions.port,
1251
+ server: parsedOptions.server,
1252
+ authKey: parsedOptions.authKey,
1253
+ startStartedCommunitiesOnStartup: parsedOptions.startStartedCommunitiesOnStartup
1254
+ });
1255
+ // Auto-start previously started communities (fire-and-forget, non-blocking)
1256
+ pkcWss._autoStartPreviousCommunities().catch((e) => {
1257
+ log.error("Failed to auto-start previous communities", e);
1258
+ });
1259
+ return pkcWss;
1260
+ };
1261
+ const PKCRpc = {
1262
+ PKCWsServer: createPKCWsServer,
1263
+ // for mocking pkc-js during tests
1264
+ setPKCJs
1265
+ };
1266
+ export default PKCRpc;
1267
+ //# sourceMappingURL=index.js.map