spec-3nweb-server 1.8.5 → 1.8.7

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 (340) hide show
  1. package/.gitlab-ci-linux.yml +50 -0
  2. package/.gitlab-ci-mac.yml +29 -0
  3. package/.gitlab-ci-windows.yml +29 -0
  4. package/.gitlab-ci.yml +63 -0
  5. package/build/3nstorage/resources/store.d.ts +2 -2
  6. package/build/3nstorage/resources/store.js +1 -1
  7. package/build/3nstorage/resources/users.d.ts +3 -3
  8. package/build/3nstorage/resources/users.js +1 -1
  9. package/build/3nstorage/resources/utils.d.ts +6 -0
  10. package/build/3nstorage/resources/utils.js +41 -0
  11. package/build/3nstorage/routes/owner/archive-obj-version.js +8 -10
  12. package/build/3nstorage/routes/owner/cancel-trans.js +5 -4
  13. package/build/3nstorage/routes/owner/delete-obj.js +7 -8
  14. package/build/3nstorage/routes/owner/get-archived-obj-ver.js +11 -13
  15. package/build/3nstorage/routes/owner/get-current-obj.js +7 -6
  16. package/build/3nstorage/routes/owner/get-obj-status.js +8 -8
  17. package/build/3nstorage/routes/owner/put-current-obj.js +25 -55
  18. package/build/3nstorage/routes/owner/session-params.js +2 -0
  19. package/build/asmail/resources/utils.d.ts +6 -0
  20. package/build/asmail/resources/utils.js +36 -0
  21. package/build/asmail/routes/delivery/finalize-delivery.js +3 -2
  22. package/build/asmail/routes/delivery/pre-flight.js +9 -26
  23. package/build/asmail/routes/delivery/provide-recipient-pubkey.js +4 -3
  24. package/build/asmail/routes/delivery/put-metadata.js +13 -21
  25. package/build/asmail/routes/delivery/put-obj.js +19 -25
  26. package/build/asmail/routes/delivery/restart-session.d.ts +4 -8
  27. package/build/asmail/routes/delivery/restart-session.js +10 -23
  28. package/build/asmail/routes/delivery/sender-authorization.js +5 -5
  29. package/build/asmail/routes/delivery/start-session.js +9 -24
  30. package/build/asmail/routes/retrieval/get-message-meta.js +4 -7
  31. package/build/asmail/routes/retrieval/get-message-obj.js +10 -7
  32. package/build/asmail/routes/retrieval/list-messages.js +4 -4
  33. package/build/asmail/routes/retrieval/remove-message.js +3 -2
  34. package/build/mock/tls-options.js +46 -44
  35. package/build/tests/admin-proto/signup.d.ts +1 -0
  36. package/build/tests/admin-proto/signup.js +235 -0
  37. package/build/tests/jasmine.d.ts +1 -0
  38. package/build/tests/jasmine.js +32 -0
  39. package/build/tests/libs-for-tests/3nstorage.d.ts +22 -0
  40. package/build/tests/libs-for-tests/3nstorage.js +123 -0
  41. package/build/tests/libs-for-tests/asmail.d.ts +22 -0
  42. package/build/tests/libs-for-tests/asmail.js +179 -0
  43. package/build/tests/libs-for-tests/assert.d.ts +1 -0
  44. package/build/tests/libs-for-tests/assert.js +26 -0
  45. package/build/tests/libs-for-tests/async-jasmine.d.ts +7 -0
  46. package/build/tests/libs-for-tests/async-jasmine.js +77 -0
  47. package/build/tests/libs-for-tests/bytes-equal.d.ts +1 -0
  48. package/build/tests/libs-for-tests/bytes-equal.js +34 -0
  49. package/build/tests/libs-for-tests/json-copy.d.ts +1 -0
  50. package/build/tests/libs-for-tests/json-copy.js +45 -0
  51. package/build/tests/libs-for-tests/json-equal.d.ts +4 -0
  52. package/build/tests/libs-for-tests/json-equal.js +67 -0
  53. package/build/tests/libs-for-tests/mailerid.d.ts +13 -0
  54. package/build/tests/libs-for-tests/mailerid.js +114 -0
  55. package/build/tests/libs-for-tests/pkl.d.ts +28 -0
  56. package/build/tests/libs-for-tests/pkl.js +89 -0
  57. package/build/tests/libs-for-tests/server-components/3nstorage.d.ts +20 -0
  58. package/build/tests/libs-for-tests/server-components/3nstorage.js +200 -0
  59. package/build/tests/libs-for-tests/server-components/admin.d.ts +6 -0
  60. package/build/tests/libs-for-tests/server-components/admin.js +43 -0
  61. package/build/tests/libs-for-tests/server-components/asmail.d.ts +16 -0
  62. package/build/tests/libs-for-tests/server-components/asmail.js +143 -0
  63. package/build/tests/libs-for-tests/server-components/component.d.ts +13 -0
  64. package/build/tests/libs-for-tests/server-components/component.js +77 -0
  65. package/build/tests/libs-for-tests/server-components/mailerid.d.ts +10 -0
  66. package/build/tests/libs-for-tests/server-components/mailerid.js +103 -0
  67. package/build/tests/libs-for-tests/server-runner.d.ts +15 -0
  68. package/build/tests/libs-for-tests/server-runner.js +69 -0
  69. package/build/tests/libs-for-tests/spec-assembly.d.ts +6 -0
  70. package/build/tests/libs-for-tests/spec-assembly.js +55 -0
  71. package/build/tests/libs-for-tests/ws-utils.d.ts +3 -0
  72. package/build/tests/libs-for-tests/ws-utils.js +59 -0
  73. package/build/tests/libs-for-tests/xhr-utils.d.ts +61 -0
  74. package/build/tests/libs-for-tests/xhr-utils.js +154 -0
  75. package/build/tests/protocols/3nstorage/owner/specs/archive-obj-ver.d.ts +2 -0
  76. package/build/tests/protocols/3nstorage/owner/specs/archive-obj-ver.js +84 -0
  77. package/build/tests/protocols/3nstorage/owner/specs/archive-root-ver.d.ts +2 -0
  78. package/build/tests/protocols/3nstorage/owner/specs/archive-root-ver.js +78 -0
  79. package/build/tests/protocols/3nstorage/owner/specs/delete-archive-obj-ver.d.ts +2 -0
  80. package/build/tests/protocols/3nstorage/owner/specs/delete-archive-obj-ver.js +83 -0
  81. package/build/tests/protocols/3nstorage/owner/specs/delete-archive-root-ver.d.ts +2 -0
  82. package/build/tests/protocols/3nstorage/owner/specs/delete-archive-root-ver.js +77 -0
  83. package/build/tests/protocols/3nstorage/owner/specs/delete-obj.d.ts +2 -0
  84. package/build/tests/protocols/3nstorage/owner/specs/delete-obj.js +75 -0
  85. package/build/tests/protocols/3nstorage/owner/specs/events.d.ts +2 -0
  86. package/build/tests/protocols/3nstorage/owner/specs/events.js +91 -0
  87. package/build/tests/protocols/3nstorage/owner/specs/get-current-obj.d.ts +2 -0
  88. package/build/tests/protocols/3nstorage/owner/specs/get-current-obj.js +269 -0
  89. package/build/tests/protocols/3nstorage/owner/specs/get-current-root.d.ts +2 -0
  90. package/build/tests/protocols/3nstorage/owner/specs/get-current-root.js +262 -0
  91. package/build/tests/protocols/3nstorage/owner/specs/get-obj-status.d.ts +2 -0
  92. package/build/tests/protocols/3nstorage/owner/specs/get-obj-status.js +79 -0
  93. package/build/tests/protocols/3nstorage/owner/specs/get-root-status.d.ts +2 -0
  94. package/build/tests/protocols/3nstorage/owner/specs/get-root-status.js +72 -0
  95. package/build/tests/protocols/3nstorage/owner/specs/put-current-obj.d.ts +2 -0
  96. package/build/tests/protocols/3nstorage/owner/specs/put-current-obj.js +287 -0
  97. package/build/tests/protocols/3nstorage/owner/specs/put-current-root.d.ts +2 -0
  98. package/build/tests/protocols/3nstorage/owner/specs/put-current-root.js +274 -0
  99. package/build/tests/protocols/3nstorage/owner/specs/trans-cancel-root.d.ts +2 -0
  100. package/build/tests/protocols/3nstorage/owner/specs/trans-cancel-root.js +73 -0
  101. package/build/tests/protocols/3nstorage/owner/specs/trans-cancel.d.ts +2 -0
  102. package/build/tests/protocols/3nstorage/owner/specs/trans-cancel.js +72 -0
  103. package/build/tests/protocols/3nstorage/owner/test-utils.d.ts +14 -0
  104. package/build/tests/protocols/3nstorage/owner/test-utils.js +54 -0
  105. package/build/tests/protocols/3nstorage/owner.d.ts +1 -0
  106. package/build/tests/protocols/3nstorage/owner.js +138 -0
  107. package/build/tests/protocols/asmail/config.d.ts +1 -0
  108. package/build/tests/protocols/asmail/config.js +117 -0
  109. package/build/tests/protocols/asmail/delivery/specs/finalize-delivery.d.ts +2 -0
  110. package/build/tests/protocols/asmail/delivery/specs/finalize-delivery.js +60 -0
  111. package/build/tests/protocols/asmail/delivery/specs/provide-recipient-pubkey.d.ts +2 -0
  112. package/build/tests/protocols/asmail/delivery/specs/provide-recipient-pubkey.js +68 -0
  113. package/build/tests/protocols/asmail/delivery/specs/put-metadata.d.ts +2 -0
  114. package/build/tests/protocols/asmail/delivery/specs/put-metadata.js +83 -0
  115. package/build/tests/protocols/asmail/delivery/specs/put-obj.d.ts +2 -0
  116. package/build/tests/protocols/asmail/delivery/specs/put-obj.js +123 -0
  117. package/build/tests/protocols/asmail/delivery/specs/restart-session.d.ts +2 -0
  118. package/build/tests/protocols/asmail/delivery/specs/restart-session.js +79 -0
  119. package/build/tests/protocols/asmail/delivery/specs/start-session.d.ts +2 -0
  120. package/build/tests/protocols/asmail/delivery/specs/start-session.js +64 -0
  121. package/build/tests/protocols/asmail/delivery/test-utils.d.ts +16 -0
  122. package/build/tests/protocols/asmail/delivery/test-utils.js +46 -0
  123. package/build/tests/protocols/asmail/delivery.d.ts +1 -0
  124. package/build/tests/protocols/asmail/delivery.js +46 -0
  125. package/build/tests/protocols/asmail/retrieval/specs/events.d.ts +2 -0
  126. package/build/tests/protocols/asmail/retrieval/specs/events.js +62 -0
  127. package/build/tests/protocols/asmail/retrieval/specs/get-message-meta.d.ts +2 -0
  128. package/build/tests/protocols/asmail/retrieval/specs/get-message-meta.js +70 -0
  129. package/build/tests/protocols/asmail/retrieval/specs/get-message-obj.d.ts +2 -0
  130. package/build/tests/protocols/asmail/retrieval/specs/get-message-obj.js +166 -0
  131. package/build/tests/protocols/asmail/retrieval/specs/list-messages.d.ts +2 -0
  132. package/build/tests/protocols/asmail/retrieval/specs/list-messages.js +96 -0
  133. package/build/tests/protocols/asmail/retrieval/specs/remove-message.d.ts +2 -0
  134. package/build/tests/protocols/asmail/retrieval/specs/remove-message.js +66 -0
  135. package/build/tests/protocols/asmail/retrieval/test-utils.d.ts +17 -0
  136. package/build/tests/protocols/asmail/retrieval/test-utils.js +46 -0
  137. package/build/tests/protocols/asmail/retrieval.d.ts +1 -0
  138. package/build/tests/protocols/asmail/retrieval.js +92 -0
  139. package/build/tests/protocols/mailerid.d.ts +1 -0
  140. package/build/tests/protocols/mailerid.js +210 -0
  141. package/build/tests/shared-checks/check-mid-login.d.ts +2 -0
  142. package/build/tests/shared-checks/check-mid-login.js +131 -0
  143. package/build/tests/shared-checks/requests.d.ts +20 -0
  144. package/build/tests/shared-checks/requests.js +123 -0
  145. package/package.json +1 -1
  146. package/packing/create-deb.sh +57 -0
  147. package/packing/deb/lib/systemd/system/3nweb.service +11 -0
  148. package/packing/deb/postinst +44 -0
  149. package/packing/deb/usr/share/doc/3nweb-server/conf-for-postinstall.yaml +19 -0
  150. package/packing/deb/usr/share/doc/3nweb-server/copyright +22 -0
  151. package/publish-task.sh +10 -0
  152. package/push-to-github.sh +105 -0
  153. package/ts-code/3nstorage/3nstorage-app.ts +99 -0
  154. package/ts-code/3nstorage/owner.ts +178 -0
  155. package/ts-code/3nstorage/resources/sessions.ts +37 -0
  156. package/ts-code/3nstorage/resources/store.ts +896 -0
  157. package/ts-code/3nstorage/resources/users.ts +258 -0
  158. package/ts-code/3nstorage/resources/utils.ts +45 -0
  159. package/ts-code/3nstorage/routes/owner/archive-obj-version.ts +71 -0
  160. package/ts-code/3nstorage/routes/owner/cancel-trans.ts +60 -0
  161. package/ts-code/3nstorage/routes/owner/delete-obj.ts +99 -0
  162. package/ts-code/3nstorage/routes/owner/get-archived-obj-ver.ts +109 -0
  163. package/ts-code/3nstorage/routes/owner/get-current-obj.ts +124 -0
  164. package/ts-code/3nstorage/routes/owner/get-obj-status.ts +55 -0
  165. package/ts-code/3nstorage/routes/owner/param-getter.ts +51 -0
  166. package/ts-code/3nstorage/routes/owner/param-setter.ts +50 -0
  167. package/ts-code/3nstorage/routes/owner/put-current-obj.ts +257 -0
  168. package/ts-code/3nstorage/routes/owner/session-params.ts +50 -0
  169. package/ts-code/3nstorage/shared.ts +43 -0
  170. package/ts-code/asmail/asmail-app.ts +91 -0
  171. package/ts-code/asmail/config.ts +96 -0
  172. package/ts-code/asmail/delivery.ts +94 -0
  173. package/ts-code/asmail/resources/delivery-sessions.ts +118 -0
  174. package/ts-code/asmail/resources/inbox.ts +696 -0
  175. package/ts-code/asmail/resources/recipients.ts +495 -0
  176. package/ts-code/asmail/resources/sessions.ts +37 -0
  177. package/ts-code/asmail/resources/utils.ts +41 -0
  178. package/ts-code/asmail/retrieval.ts +96 -0
  179. package/ts-code/asmail/routes/config/param-getter.ts +49 -0
  180. package/ts-code/asmail/routes/config/param-setter.ts +49 -0
  181. package/ts-code/asmail/routes/delivery/finalize-delivery.ts +51 -0
  182. package/ts-code/asmail/routes/delivery/pre-flight.ts +118 -0
  183. package/ts-code/asmail/routes/delivery/provide-recipient-pubkey.ts +58 -0
  184. package/ts-code/asmail/routes/delivery/put-metadata.ts +85 -0
  185. package/ts-code/asmail/routes/delivery/put-obj.ts +152 -0
  186. package/ts-code/asmail/routes/delivery/restart-session.ts +117 -0
  187. package/ts-code/asmail/routes/delivery/sender-authorization.ts +81 -0
  188. package/ts-code/asmail/routes/delivery/start-session.ts +136 -0
  189. package/ts-code/asmail/routes/retrieval/get-message-meta.ts +50 -0
  190. package/ts-code/asmail/routes/retrieval/get-message-obj.ts +106 -0
  191. package/ts-code/asmail/routes/retrieval/list-messages.ts +68 -0
  192. package/ts-code/asmail/routes/retrieval/remove-message.ts +48 -0
  193. package/ts-code/cli/index.ts +82 -0
  194. package/ts-code/cli/run-cmd.ts +81 -0
  195. package/ts-code/cli/show-sample-config.ts +78 -0
  196. package/ts-code/cli/signup-cmd.ts +229 -0
  197. package/ts-code/cli/user-cmd.ts +68 -0
  198. package/ts-code/cli/utils.ts +105 -0
  199. package/ts-code/config/default-confs.ts +22 -0
  200. package/ts-code/config/from-yaml.ts +35 -0
  201. package/ts-code/config/letsencrypt.ts +77 -0
  202. package/ts-code/config/signup.ts +137 -0
  203. package/ts-code/lib-common/admin-api/signup.ts +120 -0
  204. package/ts-code/lib-common/assert.ts +22 -0
  205. package/ts-code/lib-common/async-fs-node.ts +656 -0
  206. package/ts-code/lib-common/buffer-utils.ts +124 -0
  207. package/ts-code/lib-common/byte-streaming/common.ts +166 -0
  208. package/ts-code/lib-common/bytes-equal.ts +25 -0
  209. package/ts-code/lib-common/canonical-address.ts +92 -0
  210. package/ts-code/lib-common/exceptions/error.ts +44 -0
  211. package/ts-code/lib-common/exceptions/file.ts +84 -0
  212. package/ts-code/lib-common/exceptions/http.ts +69 -0
  213. package/ts-code/lib-common/ipc/generic-ipc.ts +750 -0
  214. package/ts-code/lib-common/ipc/ws-ipc.ts +117 -0
  215. package/ts-code/lib-common/json-equal.ts +55 -0
  216. package/ts-code/lib-common/json-utils.ts +130 -0
  217. package/ts-code/lib-common/jwkeys.ts +246 -0
  218. package/ts-code/lib-common/map-of-sets.ts +60 -0
  219. package/ts-code/lib-common/mid-sigs-NaCl-Ed.ts +677 -0
  220. package/ts-code/lib-common/objs-on-disk/file-layout.ts +480 -0
  221. package/ts-code/lib-common/objs-on-disk/obj-file.ts +325 -0
  222. package/ts-code/lib-common/objs-on-disk/utils.ts +201 -0
  223. package/ts-code/lib-common/objs-on-disk/v1-obj-file-format.ts +329 -0
  224. package/ts-code/lib-common/processes.ts +501 -0
  225. package/ts-code/lib-common/random-node.ts +79 -0
  226. package/ts-code/lib-common/service-api/3nstorage/owner.ts +559 -0
  227. package/ts-code/lib-common/service-api/3nstorage/root-route.ts +22 -0
  228. package/ts-code/lib-common/service-api/asmail/config.ts +144 -0
  229. package/ts-code/lib-common/service-api/asmail/delivery.ts +250 -0
  230. package/ts-code/lib-common/service-api/asmail/retrieval.ts +238 -0
  231. package/ts-code/lib-common/service-api/asmail/root-route.ts +22 -0
  232. package/ts-code/lib-common/service-api/ipc-api.ts +44 -0
  233. package/ts-code/lib-common/service-api/locker.ts +44 -0
  234. package/ts-code/lib-common/service-api/mailer-id/login.ts +80 -0
  235. package/ts-code/lib-common/service-api/mailer-id/provisioning.ts +64 -0
  236. package/ts-code/lib-common/service-api/mailer-id/root-route.ts +25 -0
  237. package/ts-code/lib-common/service-api/pub-key-login.ts +75 -0
  238. package/ts-code/lib-common/service-api/web-socket-base.ts +87 -0
  239. package/ts-code/lib-common/session-encryptor.ts +54 -0
  240. package/ts-code/lib-common/time-window-cache.ts +74 -0
  241. package/ts-code/lib-common/transferable.d.ts +30 -0
  242. package/ts-code/lib-server/conf-util.ts +35 -0
  243. package/ts-code/lib-server/mid-access.ts +45 -0
  244. package/ts-code/lib-server/middleware/body-parsers.ts +248 -0
  245. package/ts-code/lib-server/middleware/error-handler.ts +79 -0
  246. package/ts-code/lib-server/pkl-access.ts +46 -0
  247. package/ts-code/lib-server/resources/dns.ts +151 -0
  248. package/ts-code/lib-server/resources/mailerid-authorizer.ts +115 -0
  249. package/ts-code/lib-server/resources/mem-backed-sessions-factory.ts +132 -0
  250. package/ts-code/lib-server/resources/server-data-folders.ts +160 -0
  251. package/ts-code/lib-server/resources/server-key-for-pkl-challenge.ts +41 -0
  252. package/ts-code/lib-server/resources/sessions.ts +232 -0
  253. package/ts-code/lib-server/resources/user-files.ts +147 -0
  254. package/ts-code/lib-server/routes/pub-key-login/complete-exchange.ts +63 -0
  255. package/ts-code/lib-server/routes/pub-key-login/start-exchange.ts +189 -0
  256. package/ts-code/lib-server/routes/sessions/close.ts +27 -0
  257. package/ts-code/lib-server/routes/sessions/mid-auth.ts +77 -0
  258. package/ts-code/lib-server/routes/sessions/start.ts +116 -0
  259. package/ts-code/lib-server/web-sockets/app.ts +339 -0
  260. package/ts-code/lib-server/web-sockets/server-events.ts +63 -0
  261. package/ts-code/lib.ts +29 -0
  262. package/ts-code/locker/locker-app.ts +60 -0
  263. package/ts-code/locker/resources/lockers.ts +17 -0
  264. package/ts-code/locker/resources/sessions.ts +37 -0
  265. package/ts-code/locker/routes/create-locker.ts +47 -0
  266. package/ts-code/mailerId/mailerId-app.ts +107 -0
  267. package/ts-code/mailerId/resources/certifier.ts +181 -0
  268. package/ts-code/mailerId/resources/sessions.ts +44 -0
  269. package/ts-code/mailerId/resources/users.ts +88 -0
  270. package/ts-code/mailerId/routes/certify.ts +68 -0
  271. package/ts-code/main.ts +304 -0
  272. package/ts-code/mock/dns.ts +106 -0
  273. package/ts-code/mock/mock-as-child-proc.ts +130 -0
  274. package/ts-code/mock/node-child-ipc.ts +69 -0
  275. package/ts-code/mock/run-in-proc.ts +111 -0
  276. package/ts-code/mock/tls-options.ts +73 -0
  277. package/ts-code/services.ts +138 -0
  278. package/ts-code/signup/resources/users.ts +429 -0
  279. package/ts-code/signup/routes/add.ts +65 -0
  280. package/ts-code/signup/routes/get-available-addresses.ts +49 -0
  281. package/ts-code/signup/routes/get-available-domains.ts +45 -0
  282. package/ts-code/signup/signup-app.ts +54 -0
  283. package/ts-code/signup/signup-tokens.ts +148 -0
  284. package/ts-code/tests/admin-proto/signup.ts +277 -0
  285. package/ts-code/tests/jasmine.ts +37 -0
  286. package/ts-code/tests/libs-for-tests/3nstorage.ts +144 -0
  287. package/ts-code/tests/libs-for-tests/asmail.ts +220 -0
  288. package/ts-code/tests/libs-for-tests/assert.ts +22 -0
  289. package/ts-code/tests/libs-for-tests/async-jasmine.ts +89 -0
  290. package/ts-code/tests/libs-for-tests/bytes-equal.ts +25 -0
  291. package/ts-code/tests/libs-for-tests/json-copy.ts +38 -0
  292. package/ts-code/tests/libs-for-tests/json-equal.ts +55 -0
  293. package/ts-code/tests/libs-for-tests/mailerid.ts +123 -0
  294. package/ts-code/tests/libs-for-tests/pkl.ts +127 -0
  295. package/ts-code/tests/libs-for-tests/server-components/3nstorage.ts +206 -0
  296. package/ts-code/tests/libs-for-tests/server-components/admin.ts +49 -0
  297. package/ts-code/tests/libs-for-tests/server-components/asmail.ts +152 -0
  298. package/ts-code/tests/libs-for-tests/server-components/component.ts +90 -0
  299. package/ts-code/tests/libs-for-tests/server-components/mailerid.ts +114 -0
  300. package/ts-code/tests/libs-for-tests/server-runner.ts +82 -0
  301. package/ts-code/tests/libs-for-tests/spec-assembly.ts +60 -0
  302. package/ts-code/tests/libs-for-tests/ws-utils.ts +61 -0
  303. package/ts-code/tests/libs-for-tests/xhr-utils.ts +179 -0
  304. package/ts-code/tests/protocols/3nstorage/owner/specs/archive-obj-ver.ts +100 -0
  305. package/ts-code/tests/protocols/3nstorage/owner/specs/archive-root-ver.ts +91 -0
  306. package/ts-code/tests/protocols/3nstorage/owner/specs/delete-archive-obj-ver.ts +99 -0
  307. package/ts-code/tests/protocols/3nstorage/owner/specs/delete-archive-root-ver.ts +90 -0
  308. package/ts-code/tests/protocols/3nstorage/owner/specs/delete-obj.ts +84 -0
  309. package/ts-code/tests/protocols/3nstorage/owner/specs/events.ts +125 -0
  310. package/ts-code/tests/protocols/3nstorage/owner/specs/get-current-obj.ts +298 -0
  311. package/ts-code/tests/protocols/3nstorage/owner/specs/get-current-root.ts +290 -0
  312. package/ts-code/tests/protocols/3nstorage/owner/specs/get-obj-status.ts +88 -0
  313. package/ts-code/tests/protocols/3nstorage/owner/specs/get-root-status.ts +80 -0
  314. package/ts-code/tests/protocols/3nstorage/owner/specs/put-current-obj.ts +366 -0
  315. package/ts-code/tests/protocols/3nstorage/owner/specs/put-current-root.ts +329 -0
  316. package/ts-code/tests/protocols/3nstorage/owner/specs/trans-cancel-root.ts +86 -0
  317. package/ts-code/tests/protocols/3nstorage/owner/specs/trans-cancel.ts +86 -0
  318. package/ts-code/tests/protocols/3nstorage/owner/test-utils.ts +72 -0
  319. package/ts-code/tests/protocols/3nstorage/owner.ts +198 -0
  320. package/ts-code/tests/protocols/asmail/config.ts +172 -0
  321. package/ts-code/tests/protocols/asmail/delivery/specs/finalize-delivery.ts +69 -0
  322. package/ts-code/tests/protocols/asmail/delivery/specs/provide-recipient-pubkey.ts +88 -0
  323. package/ts-code/tests/protocols/asmail/delivery/specs/put-metadata.ts +102 -0
  324. package/ts-code/tests/protocols/asmail/delivery/specs/put-obj.ts +156 -0
  325. package/ts-code/tests/protocols/asmail/delivery/specs/restart-session.ts +97 -0
  326. package/ts-code/tests/protocols/asmail/delivery/specs/start-session.ts +79 -0
  327. package/ts-code/tests/protocols/asmail/delivery/test-utils.ts +64 -0
  328. package/ts-code/tests/protocols/asmail/delivery.ts +56 -0
  329. package/ts-code/tests/protocols/asmail/retrieval/specs/events.ts +78 -0
  330. package/ts-code/tests/protocols/asmail/retrieval/specs/get-message-meta.ts +80 -0
  331. package/ts-code/tests/protocols/asmail/retrieval/specs/get-message-obj.ts +197 -0
  332. package/ts-code/tests/protocols/asmail/retrieval/specs/list-messages.ts +110 -0
  333. package/ts-code/tests/protocols/asmail/retrieval/specs/remove-message.ts +74 -0
  334. package/ts-code/tests/protocols/asmail/retrieval/test-utils.ts +60 -0
  335. package/ts-code/tests/protocols/asmail/retrieval.ts +115 -0
  336. package/ts-code/tests/protocols/mailerid.ts +271 -0
  337. package/ts-code/tests/shared-checks/check-mid-login.ts +164 -0
  338. package/ts-code/tests/shared-checks/requests.ts +135 -0
  339. package/ts-code/tsconfig.json +11 -0
  340. package/ts-code/web3n.d.ts +1068 -0
@@ -0,0 +1,750 @@
1
+ /*
2
+ Copyright (C) 2016 - 2017 3NSoft Inc.
3
+
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU General Public License as published by the Free Software
6
+ Foundation, either version 3 of the License, or (at your option) any later
7
+ version.
8
+
9
+ This program is distributed in the hope that it will be useful, but
10
+ WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
+ See the GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License along with
15
+ this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { Envelope, EventEnvelope, EventEndEnvelope, EventException, events,
19
+ RequestEnvelope, ReplyEnvelope }
20
+ from '../service-api/ipc-api';
21
+ import { ErrorWithCause } from '../exceptions/error';
22
+ import { MapOfSets } from '../map-of-sets';
23
+ import { NamedProcs } from '../processes';
24
+
25
+ export { Envelope, EventEnvelope, EventEndEnvelope, EventException, RequestEnvelope, ReplyEnvelope } from '../service-api/ipc-api';
26
+
27
+ export type RequestHandler<TReq, TRes> = web3n.ipc.RequestHandler<TReq, TRes>;
28
+ export type RequestingClient = web3n.ipc.RequestingClient;
29
+ export type RequestServer = web3n.ipc.RequestServer;
30
+ export type EventfulServer = web3n.ipc.EventfulServer;
31
+ export type SubscribingClient = web3n.ipc.SubscribingClient;
32
+ export type Observer<T> = web3n.Observer<T>;
33
+
34
+ interface TransferrableError {
35
+ name?: string;
36
+ message: string;
37
+ stack?: string;
38
+ cause?: any;
39
+ }
40
+
41
+ type RuntimeException = web3n.RuntimeException;
42
+
43
+ function toTransferrableError(e: ErrorWithCause): TransferrableError {
44
+ const err: TransferrableError = {
45
+ message: e.message,
46
+ name: e.name,
47
+ stack: e.stack,
48
+ };
49
+ if (e.cause) {
50
+ if ((typeof e.cause !== 'object') ||
51
+ (e.cause as any as RuntimeException).runtimeException) {
52
+ err.cause = e.cause;
53
+ } else {
54
+ err.cause = toTransferrableError(e.cause);
55
+ }
56
+ }
57
+ return err;
58
+ }
59
+
60
+ export type RawDuplex<T> = web3n.ipc.RawDuplex<T>;
61
+
62
+ interface Deferred {
63
+ resolve(result?: any): void;
64
+ reject(cause: any): void;
65
+ notify(progress: any): void;
66
+ }
67
+
68
+ abstract class MessageHandler {
69
+
70
+ private detachFromComm: (() => void)|undefined;
71
+ private onEndCBs: ((err?: any) => void)[]|undefined = undefined;
72
+
73
+ protected constructor(
74
+ public channel: string|undefined,
75
+ public rawDuplex: RawDuplex<Envelope>
76
+ ) {
77
+ this.detachFromComm = this.rawDuplex.subscribe({
78
+ next: env => this.handleMsg(env),
79
+ complete: () => this.handleCompletion(),
80
+ error: err => this.handleCompletion(err)
81
+ });
82
+ }
83
+
84
+ protected abstract handleMsg(env: Envelope): void;
85
+
86
+ protected handleCompletion(err?: any): void {
87
+ if (!this.detachFromComm) { return; }
88
+ this.rawDuplex = (undefined as any);
89
+ this.detachFromComm = undefined;
90
+ if (this.onEndCBs) {
91
+ for (const cb of this.onEndCBs) {
92
+ try {
93
+ cb(err);
94
+ } catch (err2) {
95
+ console.error(err2);
96
+ }
97
+ }
98
+ this.onEndCBs = undefined;
99
+ }
100
+ }
101
+
102
+ on(event: 'end', cb: (err?: any) => void): void {
103
+ if (!this.onEndCBs) {
104
+ this.onEndCBs = [];
105
+ }
106
+ this.onEndCBs.push(cb);
107
+ }
108
+
109
+ close() {
110
+ if (!this.detachFromComm) { return; }
111
+ this.detachFromComm();
112
+ this.handleCompletion();
113
+ }
114
+
115
+ }
116
+ Object.freeze(MessageHandler.prototype);
117
+ Object.freeze(MessageHandler);
118
+
119
+ class RequestingSide extends MessageHandler implements RequestingClient {
120
+
121
+ private counter = 0;
122
+ private replyDeferreds = new Map<number, Deferred>();
123
+
124
+ constructor(channel: string|undefined, comm: RawDuplex<Envelope>) {
125
+ super(channel, comm);
126
+ }
127
+
128
+ protected handleMsg(env: Envelope): void {
129
+ if (!env || (env.type !== 'reply')) { return; }
130
+ if (this.channel && !env.reqName.startsWith(this.channel)) { return; }
131
+ const deferredReply = this.replyDeferreds.get(env.reqCount);
132
+ if (!deferredReply) { return; }
133
+ if (env.isInProgress) {
134
+ deferredReply.notify(env.rep)
135
+ } else {
136
+ if (env.err) {
137
+ deferredReply.reject(env.err);
138
+ } else {
139
+ deferredReply.resolve(env.rep);
140
+ }
141
+ this.replyDeferreds.delete(env.reqCount);
142
+ }
143
+ }
144
+
145
+ protected handleCompletion(err?: any): void {
146
+ super.handleCompletion(err);
147
+ this.rejectAndClearPendingRequests();
148
+ }
149
+
150
+ /**
151
+ * This rejects all pending request, clearing internal containers.
152
+ */
153
+ private rejectAndClearPendingRequests(): void {
154
+ for (const reqNum of this.replyDeferreds.keys()) {
155
+ if (isNaN(reqNum)) { continue; }
156
+ const deferred = this.replyDeferreds.get(reqNum);
157
+ if (!deferred) { continue; }
158
+ deferred.reject(new Error('All pending requests are flushed.'));
159
+ }
160
+ this.replyDeferreds.clear();
161
+ }
162
+
163
+ makeRequest<T>(name: string, req: any, notifyCallback?: (progress: any) => void): Promise<T> {
164
+ this.counter += 1;
165
+ if (this.counter === Number.MAX_SAFE_INTEGER) {
166
+ this.counter = Number.MIN_SAFE_INTEGER;
167
+ }
168
+ if (this.channel) {
169
+ name = `${this.channel}/${name}`;
170
+ }
171
+ const envelope: RequestEnvelope<any> = {
172
+ type: 'request',
173
+ name,
174
+ count: this.counter,
175
+ req: req
176
+ };
177
+ const promise = new Promise<T>((resolve, reject) => {
178
+ const deferredReply: Deferred = {
179
+ resolve, reject,
180
+ notify: (notifyCallback ? notifyCallback : () => {})
181
+ }
182
+ this.replyDeferreds.set(this.counter, deferredReply)
183
+ });
184
+ this.rawDuplex.postMessage(envelope);
185
+ return promise;
186
+ }
187
+
188
+ wrap(): RequestingClient {
189
+ const w: RequestingClient = {
190
+ makeRequest: this.makeRequest.bind(this),
191
+ close: this.close.bind(this),
192
+ on: this.on.bind(this)
193
+ };
194
+ return Object.freeze(w);
195
+ }
196
+
197
+ }
198
+ Object.freeze(RequestingSide.prototype);
199
+ Object.freeze(RequestingSide);
200
+
201
+ export function makeRequestingClient(channel: string|undefined, comm: RawDuplex<Envelope>): RequestingClient {
202
+ return (new RequestingSide(channel, comm)).wrap();
203
+ }
204
+
205
+ class ReplyingSide extends MessageHandler implements RequestServer {
206
+
207
+ private requestHandlers = new Map<string, RequestHandler<any, any>>();
208
+
209
+ constructor(channel: string|undefined, comm: RawDuplex<Envelope>) {
210
+ super(channel, comm);
211
+ }
212
+
213
+ protected async handleMsg(req: Envelope): Promise<void> {
214
+ if (!req || (req.type !== 'request')) { return; }
215
+ if (this.channel && !req.name.startsWith(this.channel)) { return; }
216
+ const handler = this.requestHandlers.get(req.name);
217
+ let reply: ReplyEnvelope<any>;
218
+ if (handler) {
219
+ try {
220
+ const result = await handler(req);
221
+ reply = this.normalReply(req, result);
222
+ } catch (err) {
223
+ reply = this.errorReply(req, err);
224
+ }
225
+ } else {
226
+ reply = this.errorReply(req, new Error(
227
+ `Have no handler for request named ${req.name}`));
228
+ }
229
+ this.sendReply(req, reply);
230
+ }
231
+
232
+ protected handleCompletion(err?: any): void {
233
+ super.handleCompletion(err);
234
+ this.requestHandlers.clear();
235
+ }
236
+
237
+ notifyOfProgressOnRequest<T>(env: RequestEnvelope<any>, rep: T): void {
238
+ const reply = this.normalReply(env, rep, true);
239
+ this.sendReply(env, reply);
240
+ }
241
+
242
+ private sendReply(req: RequestEnvelope<any>, reply: ReplyEnvelope<any>):
243
+ void {
244
+ if (this.rawDuplex) {
245
+ this.rawDuplex.postMessage(reply);
246
+ } else {
247
+ console.warn(`Fail to send reply to request ${req.name} #${req.count}, due to ipc point being already disconnected.`);
248
+ }
249
+ }
250
+
251
+ private errorReply(env: RequestEnvelope<any>,
252
+ err: Error|RuntimeException): ReplyEnvelope<any> {
253
+ const reply: ReplyEnvelope<any> = {
254
+ type: 'reply',
255
+ reqName: env.name,
256
+ reqCount: env.count,
257
+ rep: null
258
+ };
259
+ if ((err as RuntimeException).runtimeException) {
260
+ reply.err = err;
261
+ } else {
262
+ reply.err = toTransferrableError(err as ErrorWithCause);
263
+ }
264
+ return reply;
265
+ }
266
+
267
+ private normalReply<T>(env: RequestEnvelope<any>, rep: T,
268
+ isInProgress = false): ReplyEnvelope<any> {
269
+ const reply: ReplyEnvelope<T> = {
270
+ type: 'reply',
271
+ reqName: env.name,
272
+ reqCount: env.count,
273
+ rep: rep,
274
+ };
275
+ if (isInProgress) {
276
+ reply.isInProgress = true;
277
+ }
278
+ return reply;
279
+ }
280
+
281
+ /**
282
+ * @param reqName is a name of request, to be handled by a given handler
283
+ * @param handler handles requests that come from the other side
284
+ */
285
+ addHandler(reqName: string, handler: RequestHandler<any, any>): void {
286
+ if (typeof handler !== 'function') { throw new Error(
287
+ `Given handler for request ${reqName} is not a function`); }
288
+ if (this.channel) {
289
+ reqName = `${this.channel}/${reqName}`;
290
+ }
291
+ const existingHandler = this.requestHandlers.get(reqName);
292
+ if (existingHandler) { throw new Error(
293
+ `Handler is already set for request ${reqName}`); }
294
+ this.requestHandlers.set(reqName, handler);
295
+ }
296
+
297
+ wrap(): RequestServer {
298
+ const w: RequestServer = {
299
+ addHandler: this.addHandler.bind(this),
300
+ notifyOfProgressOnRequest: this.notifyOfProgressOnRequest.bind(this),
301
+ close: this.close.bind(this),
302
+ on: this.on.bind(this)
303
+ };
304
+ return Object.freeze(w);
305
+ }
306
+
307
+ }
308
+ Object.freeze(ReplyingSide.prototype);
309
+ Object.freeze(ReplyingSide);
310
+
311
+ export function makeRequestServer(channel: string|undefined,
312
+ comm: RawDuplex<Envelope>): RequestServer {
313
+ return (new ReplyingSide(channel, comm)).wrap();
314
+ }
315
+
316
+ function makeUnknownEventException(): EventException {
317
+ return {
318
+ runtimeException: true,
319
+ type: 'events',
320
+ unknownEvent: true
321
+ };
322
+ }
323
+
324
+ type SubscriptionHandler = (eventChannel: string) => Promise<void>;
325
+ type UnsubscriptionHandler = (eventChannel: string) => void;
326
+ type GroupInfo = {
327
+ name: string;
328
+ subscribe: SubscriptionHandler;
329
+ unsubscribe?: UnsubscriptionHandler;
330
+ }
331
+
332
+ class EventsSendingSide extends ReplyingSide {
333
+
334
+ private subscriptionGroups: GroupInfo[] = [];
335
+ private subscribedEvents = new Set<string>();
336
+
337
+ constructor(channel: string|undefined, comm: RawDuplex<Envelope>) {
338
+ super(channel, comm);
339
+ this.addHandler(
340
+ events.subscribe.REQ_NAME, this.handleSubscribe.bind(this)
341
+ );
342
+ this.addHandler(
343
+ events.unsubscribe.REQ_NAME, this.handleUnsubscribe.bind(this)
344
+ );
345
+ }
346
+
347
+ private findGroup(eventChannel: string): GroupInfo|undefined {
348
+ for (const gr of this.subscriptionGroups) {
349
+ if (eventChannel.startsWith(gr.name)) { return gr; }
350
+ }
351
+ return;
352
+ }
353
+
354
+ private async handleSubscribe(env: RequestEnvelope<string>): Promise<void> {
355
+ const event = env.req;
356
+ const gr = this.findGroup(event);
357
+ if (!gr) { throw makeUnknownEventException(); }
358
+ this.subscribedEvents.add(event);
359
+ await gr.subscribe(event);
360
+ }
361
+
362
+ private handleUnsubscribe(env: RequestEnvelope<string>): void {
363
+ const event = env.req;
364
+ const gr = this.findGroup(event);
365
+ if (!gr) { throw makeUnknownEventException(); }
366
+ this.subscribedEvents.delete(event);
367
+ if (gr.unsubscribe) {
368
+ gr.unsubscribe(event);
369
+ }
370
+ }
371
+
372
+ addEventGroup(
373
+ group: string, subscriptionHandler: SubscriptionHandler, unsubscriptionHandler?: UnsubscriptionHandler
374
+ ): void {
375
+ if (this.findGroup(group)) { throw new Error(
376
+ `Event subscription group ${group} is already present`); }
377
+ const gr: GroupInfo = {
378
+ name: group,
379
+ subscribe: subscriptionHandler,
380
+ unsubscribe: unsubscriptionHandler
381
+ };
382
+ this.subscriptionGroups.push(gr);
383
+ }
384
+
385
+ sendEvent<T>(channel: string, event: T): void {
386
+ if (!this.rawDuplex) { return; }
387
+ if (!this.subscribedEvents.has(channel)) { return; }
388
+ if (this.channel) {
389
+ channel = `${this.channel}/${channel}`;
390
+ }
391
+ const eventEnv: EventEnvelope<T> = {
392
+ type: 'event',
393
+ channel,
394
+ event
395
+ };
396
+ this.rawDuplex.postMessage(eventEnv);
397
+ }
398
+
399
+ endEvent(channel: string, err?: any): void {
400
+ if (!this.subscribedEvents.has(channel)) { return; }
401
+ this.subscribedEvents.delete(channel);
402
+ if (this.channel) {
403
+ channel = `${this.channel}/${channel}`;
404
+ }
405
+ const eventEnv: EventEndEnvelope = {
406
+ type: 'event-end',
407
+ channel,
408
+ err: (err ? toTransferrableError(err) : undefined)
409
+ };
410
+ this.rawDuplex.postMessage(eventEnv);
411
+ }
412
+
413
+ protected handleCompletion(err?: any): void {
414
+ super.handleCompletion(err);
415
+ this.subscribedEvents.clear();
416
+ this.subscriptionGroups = [];
417
+ }
418
+
419
+ wrap(): EventfulServer {
420
+ const reqServer = super.wrap();
421
+ const w: EventfulServer = {
422
+ addHandler: reqServer.addHandler,
423
+ notifyOfProgressOnRequest: reqServer.notifyOfProgressOnRequest,
424
+ close: reqServer.close,
425
+ on: reqServer.on,
426
+ addEventGroup: this.addEventGroup.bind(this),
427
+ sendEvent: this.sendEvent.bind(this),
428
+ endEvent: this.endEvent.bind(this)
429
+ };
430
+ return Object.freeze(w);
431
+ }
432
+
433
+ }
434
+ Object.freeze(EventsSendingSide.prototype);
435
+ Object.freeze(EventsSendingSide);
436
+
437
+ export function makeEventfulServer(channel: string|undefined, comm: RawDuplex<Envelope>): EventfulServer {
438
+ return (new EventsSendingSide(channel, comm)).wrap();
439
+ }
440
+
441
+
442
+ class EventListener<T> {
443
+ constructor(
444
+ private channel: string,
445
+ public observer: Observer<T>,
446
+ public detach: () => void) {
447
+ Object.freeze(this);
448
+ }
449
+ processNext(o: T): boolean {
450
+ try {
451
+ if (this.observer.next) {
452
+ this.observer.next(o);
453
+ }
454
+ return true;
455
+ } catch (err) {
456
+ if (this.observer.error) {
457
+ try {
458
+ this.observer.error(err);
459
+ } catch (err2) {
460
+ console.error(`Error occured when handling event ${this.channel}:`);
461
+ console.error(err.stack ? err.stack : JSON.stringify(err, null, ' '));
462
+ console.error(`Followed by error in error handling callback:`);
463
+ console.error(err2.stack ?
464
+ err2.stack : JSON.stringify(err2, null, ' '));
465
+ }
466
+ } else {
467
+ console.error(`Error occured when handling event ${this.channel}:`);
468
+ console.error(err.stack ? err.stack : JSON.stringify(err, null, ' '));
469
+ }
470
+ return false;
471
+ }
472
+ }
473
+ }
474
+ Object.freeze(EventListener.prototype);
475
+ Object.freeze(EventListener);
476
+
477
+ class EventsReceivingSide extends RequestingSide implements SubscribingClient {
478
+
479
+ /**
480
+ * This is a map from ipc channel name to all listeners on it.
481
+ * Note is that ipc channel is /-concatenation of this.channel and a channel
482
+ * name, to which listener is attached.
483
+ */
484
+ private listeners = new MapOfSets<string, EventListener<any>>();
485
+
486
+ private channels: IpcEventChannels;
487
+
488
+ constructor(channel: string|undefined, comm: RawDuplex<Envelope>) {
489
+ super(channel, comm);
490
+ this.channels = new IpcEventChannels(
491
+ ipcChannel => this.makeRequest<void>(events.subscribe.REQ_NAME, ipcChannel)
492
+ .catch(err => this.completeEvent(ipcChannel, err)),
493
+ ipcChannel => this.makeRequest<void>(events.unsubscribe.REQ_NAME, ipcChannel)
494
+ .catch(noop)
495
+ );
496
+ }
497
+
498
+ protected handleMsg(env: Envelope): void {
499
+ if (env.type === 'event') {
500
+ this.handleEvent(env);
501
+ } else if (env.type === 'event-end') {
502
+ this.handleEventEnd(env);
503
+ } else {
504
+ super.handleMsg(env);
505
+ }
506
+ }
507
+
508
+ private handleEvent(env: EventEnvelope<any>): void {
509
+ if (this.channel && !env.channel.startsWith(this.channel)) { return; }
510
+ const listeners = this.listeners.get(env.channel);
511
+ if (!listeners) { return; }
512
+ let detachCalls: (() => void)[]|undefined = undefined;
513
+ for (const listener of listeners) {
514
+ if (listener.processNext(env.event)) { continue; }
515
+ if (!detachCalls) {
516
+ detachCalls = [];
517
+ }
518
+ detachCalls.push(listener.detach);
519
+ }
520
+ if (detachCalls) {
521
+ for(const detach of detachCalls) {
522
+ detach();
523
+ }
524
+ if (!this.listeners.get(env.channel)) {
525
+ this.channels.unsubscribeFrom(env.channel);
526
+ }
527
+ }
528
+ }
529
+
530
+ private handleEventEnd(env: EventEndEnvelope): void {
531
+ if (this.channel && !env.channel.startsWith(this.channel)) { return; }
532
+ this.completeEvent(env.channel);
533
+ }
534
+
535
+ /**
536
+ * This removes given event from subscription, and removes listeners,
537
+ * triggering onend's.
538
+ * @param channel event that has been unsubscribed
539
+ * @param err
540
+ */
541
+ private completeEvent(ipcChannel: string, err?: any): void {
542
+ const listeners = this.listeners.get(ipcChannel);
543
+ if (!listeners) { return; }
544
+ this.listeners.removeAll(ipcChannel);
545
+ for (const listener of listeners) {
546
+ try {
547
+ if (err === undefined) {
548
+ if (listener.observer.complete) {
549
+ listener.observer.complete();
550
+ }
551
+ } else {
552
+ if (listener.observer.error) {
553
+ listener.observer.error(err);
554
+ }
555
+ }
556
+ } catch (err2) {
557
+ console.error(err2);
558
+ }
559
+ }
560
+ }
561
+
562
+ protected handleCompletion(err?: any): void {
563
+ super.handleCompletion(err);
564
+ this.listeners.clear();
565
+ }
566
+
567
+ subscribe<T>(channel: string, observer: Observer<T>): () => void {
568
+ if (!observer.next && !observer.complete && !observer.error) {
569
+ throw new Error(`Given observer has no methods for events/notifications`); }
570
+ const ipcChannel = this.channel ? `${this.channel}/${channel}` : channel;
571
+ const detach = (): void => {
572
+ this.listeners.remove(ipcChannel, listener);
573
+ if (!this.listeners.get(ipcChannel)) {
574
+ this.channels.unsubscribeFrom(ipcChannel);
575
+ }
576
+ }
577
+ const listener = new EventListener<T>(channel, observer, detach);
578
+ this.listeners.add(ipcChannel, listener);
579
+ this.channels.subscribeTo(ipcChannel);
580
+ return detach;
581
+ }
582
+
583
+ wrap(): SubscribingClient {
584
+ const reqClient = super.wrap();
585
+ const w: SubscribingClient = {
586
+ makeRequest: reqClient.makeRequest,
587
+ close: reqClient.close,
588
+ on: reqClient.on,
589
+ subscribe: this.subscribe.bind(this)
590
+ };
591
+ return Object.freeze(w);
592
+ }
593
+
594
+ }
595
+ Object.freeze(EventsReceivingSide.prototype);
596
+ Object.freeze(EventsReceivingSide);
597
+
598
+ class IpcEventChannels {
599
+
600
+ /**
601
+ * These are all subscribed ipc channels.
602
+ */
603
+ private subscribedIpcChannels = new Set<string>();
604
+
605
+ private subscriptionProcs = new NamedProcs();
606
+
607
+ constructor(
608
+ private subscribe: (ipcChannel: string) => Promise<void>,
609
+ private unsubscribe: (ipcChannel: string) => void) {
610
+ Object.freeze(this);
611
+ }
612
+
613
+ subscribeTo(ipcChannel: string): void {
614
+ if (this.subscribedIpcChannels.has(ipcChannel)) { return; }
615
+ this.subscriptionProcs.startOrChain(ipcChannel, async () => {
616
+ if (this.subscribedIpcChannels.has(ipcChannel)) { return; }
617
+ this.subscribedIpcChannels.add(ipcChannel);
618
+ await this.subscribe(ipcChannel);
619
+ });
620
+ }
621
+
622
+ async unsubscribeFrom(ipcChannel: string): Promise<void> {
623
+ if (!this.subscribedIpcChannels.has(ipcChannel)) { return; }
624
+ this.subscriptionProcs.startOrChain(ipcChannel, async () => {
625
+ if (!this.subscribedIpcChannels.has(ipcChannel)) { return; }
626
+ this.subscribedIpcChannels.delete(ipcChannel);
627
+ this.unsubscribe(ipcChannel);
628
+ });
629
+ }
630
+
631
+ }
632
+ Object.freeze(IpcEventChannels.prototype);
633
+ Object.freeze(IpcEventChannels);
634
+
635
+ function noop() {}
636
+
637
+ export function makeSubscribingClient(channel: string|undefined,
638
+ comm: RawDuplex<Envelope>): SubscribingClient {
639
+ return (new EventsReceivingSide(channel, comm)).wrap();
640
+ }
641
+
642
+ export class MultiObserverWrap<T> {
643
+
644
+ private isDone = false;
645
+ private obs = new Set<Observer<T>>();
646
+
647
+ get done(): boolean {
648
+ return this.isDone;
649
+ }
650
+
651
+ constructor() {
652
+ Object.seal(this);
653
+ }
654
+
655
+ add(obs: Observer<T>): () => void {
656
+ if (this.isDone) { throw new Error(
657
+ `Cannot add observer, as this source is already done`); }
658
+ this.obs.add(obs);
659
+ return () => { this.obs.delete(obs); };
660
+ }
661
+
662
+ next(o: T): void {
663
+ if (this.isDone) { return; }
664
+ for (const obs of this.obs) {
665
+ if (!obs.next) { continue; }
666
+ obs.next(o);
667
+ }
668
+ }
669
+
670
+ error(err: any): void {
671
+ if (this.isDone) { return; }
672
+ for (const obs of this.obs) {
673
+ if (!obs.error) { continue; }
674
+ obs.error(err);
675
+ }
676
+ this.setDone();
677
+ }
678
+
679
+ complete(): void {
680
+ if (this.isDone) { return; }
681
+ for (const obs of this.obs) {
682
+ if (!obs.complete) { continue; }
683
+ obs.complete();
684
+ }
685
+ this.setDone();
686
+ }
687
+
688
+ private setDone(): void {
689
+ this.obs.clear();
690
+ this.isDone = true;
691
+ }
692
+ }
693
+ Object.freeze(MultiObserverWrap.prototype);
694
+ Object.freeze(MultiObserverWrap);
695
+
696
+ export class SingleObserverWrap<T> {
697
+
698
+ private isDone = false;
699
+ private obs?: Observer<T> = undefined;
700
+
701
+ get done(): boolean {
702
+ return this.isDone;
703
+ }
704
+
705
+ constructor() {
706
+ Object.seal(this);
707
+ }
708
+
709
+ set(obs: Observer<T>): void {
710
+ if (this.isDone) { throw new Error(
711
+ `Cannot add observer, as this source is already done`); }
712
+ if (this.obs) { throw new Error(
713
+ `Single observer is already set at this source`); }
714
+ this.obs = obs;
715
+ }
716
+
717
+ detach = () => this.setDone();
718
+
719
+ next(o: T): void {
720
+ if (this.isDone || !this.obs) { return; }
721
+ if (this.obs.next) {
722
+ this.obs.next(o);
723
+ }
724
+ }
725
+
726
+ error(err: any): void {
727
+ if (this.isDone) { return; }
728
+ if (this.obs && this.obs.error) {
729
+ this.obs.error(err);
730
+ }
731
+ this.setDone();
732
+ }
733
+
734
+ complete(): void {
735
+ if (this.isDone) { return; }
736
+ if (this.obs && this.obs.complete) {
737
+ this.obs.complete();
738
+ }
739
+ this.setDone();
740
+ }
741
+
742
+ private setDone(): void {
743
+ this.obs = undefined;
744
+ this.isDone = true;
745
+ }
746
+ }
747
+ Object.freeze(SingleObserverWrap.prototype);
748
+ Object.freeze(SingleObserverWrap);
749
+
750
+ Object.freeze(exports);