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,132 @@
1
+ /*
2
+ Copyright (C) 2015 - 2017, 2021 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
+ /**
19
+ * This module constructs memory-backed sessions factories.
20
+ */
21
+
22
+ import { Factory, Session, BaseSessionFactory, wrapFactory } from './sessions';
23
+ import { bytes as randomBytes } from '../../lib-common/random-node';
24
+ import { base64urlSafe } from '../../lib-common/buffer-utils';
25
+ import { compareVectors } from 'ecma-nacl';
26
+
27
+ const ID_BYTES_LEN = 30;
28
+ const ID_STR_CHUNK_LEN = 15;
29
+
30
+
31
+ export class InMemorySessions<T> extends BaseSessionFactory<T> {
32
+
33
+ private sessions = new Map<string, [Uint8Array, Session<T>]>();
34
+ private timeoutCodeIntervalId: NodeJS.Timer|undefined = undefined;
35
+ private timeoutMillis: number;
36
+
37
+ private checkSessionsForTimeout = () => {
38
+ const now = Date.now();
39
+ for (const [, s] of this.sessions.values()) {
40
+ if ((now - s.lastAccessedAt) >= this.timeoutMillis) { s.close(); }
41
+ }
42
+ };
43
+
44
+ private makeDefaultSessionParams: (sessionId: string) => T;
45
+
46
+ protected constructor(
47
+ timeoutSecs: number, makeSessionParams: (sessionId: string) => T
48
+ ) {
49
+ super();
50
+ if ((typeof timeoutSecs !== 'number') || (timeoutSecs <= 0)) {
51
+ throw new Error("Given timeout must be a number greater than zero."); }
52
+ this.timeoutMillis = timeoutSecs*1000;
53
+ if (typeof makeSessionParams !== 'function') { throw new TypeError(
54
+ `Given argument 'makeSessionParams' is not a function.`) }
55
+ this.makeDefaultSessionParams = makeSessionParams;
56
+ }
57
+
58
+ async generate(): Promise<Session<T>> {
59
+ let newSessionId: string;
60
+ do {
61
+ newSessionId = base64urlSafe.pack(await randomBytes(ID_BYTES_LEN));
62
+ } while (this.sessions.has(strChunk(newSessionId)));
63
+ const params = this.makeDefaultSessionParams(newSessionId);
64
+ const session = this.makeSession(newSessionId, params);
65
+ return session;
66
+ }
67
+
68
+ protected async add(s: Session<T>): Promise<void> {
69
+ const { key, idBytes } = idToChunks(s.id);
70
+ this.sessions.set(key, [idBytes, s]);
71
+ if (this.sessions.size === 1) {
72
+ this.timeoutCodeIntervalId = setInterval(
73
+ this.checkSessionsForTimeout, this.timeoutMillis/2);
74
+ }
75
+ }
76
+
77
+ protected async remove(s: Session<T>): Promise<void> {
78
+ if (this.sessions.delete(strChunk(s.id))) {
79
+ if (this.sessions.size === 0) {
80
+ clearInterval(this.timeoutCodeIntervalId!);
81
+ this.timeoutCodeIntervalId = undefined;
82
+ }
83
+ }
84
+ }
85
+
86
+ protected async get(sId: string): Promise<Session<T>|undefined> {
87
+ const idAndSession = this.sessions.get(strChunk(sId));
88
+ return ((idAndSession && sameIdBytes(sId, idAndSession[0])) ?
89
+ idAndSession[1] : undefined);
90
+ }
91
+
92
+ /**
93
+ * This returns factory, that works properly in a single process application,
94
+ * clearing up sessions that past given timeout.
95
+ * @param timeout is a session timeout in seconds
96
+ * @param makeDefaultSessionParams is a function that creates session
97
+ * parameters object in some initial/default state
98
+ */
99
+ static factory<T>(
100
+ timeout: number, makeDefaultSessionParams: () => T
101
+ ): Factory<T> {
102
+ const factory = new InMemorySessions(timeout, makeDefaultSessionParams);
103
+ Object.seal(factory);
104
+ return Object.freeze(wrapFactory(factory));
105
+ }
106
+
107
+ }
108
+ Object.freeze(InMemorySessions.prototype);
109
+ Object.freeze(InMemorySessions);
110
+
111
+
112
+ function sameIdBytes(idStr: string, idBytes: Uint8Array): boolean {
113
+ try {
114
+ const bytes = base64urlSafe.open(idStr);
115
+ return compareVectors(bytes, idBytes);
116
+ } catch (err) {
117
+ return false;
118
+ }
119
+ }
120
+
121
+ function idToChunks(sessionId: string): { key: string; idBytes: Uint8Array; } {
122
+ const idBytes = base64urlSafe.open(sessionId);
123
+ const key = sessionId.substring(0, ID_STR_CHUNK_LEN);
124
+ return { key, idBytes };
125
+ }
126
+
127
+ function strChunk(sessionId: string): string {
128
+ return sessionId.substring(0, ID_STR_CHUNK_LEN);
129
+ }
130
+
131
+
132
+ Object.freeze(exports);
@@ -0,0 +1,160 @@
1
+ /*
2
+ Copyright (C) 2020 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 { base64urlSafe, utf8 } from '../../lib-common/buffer-utils';
19
+ import { toCanonicalAddress } from '../../lib-common/canonical-address';
20
+ import { join } from 'path';
21
+ import { promises as fsPromises, mkdirSync } from 'fs';
22
+
23
+ export function addressToFName(address: string): string {
24
+ return idStringToFName(toCanonicalAddress(address));
25
+ }
26
+
27
+ function idStringToFName(id: string): string {
28
+ return base64urlSafe.pack(utf8.pack(id));
29
+ }
30
+
31
+ export interface FolderDef {
32
+ path: string;
33
+ }
34
+
35
+ export interface RootDataTree {
36
+ tokens: FolderDef;
37
+ users: FolderDef;
38
+ defaults: FolderDef;
39
+ }
40
+
41
+ export interface DefaultsDataTree {
42
+ info: FolderDef;
43
+ store: FolderDef & {
44
+ params: FolderDef;
45
+ };
46
+ mail: FolderDef & {
47
+ params: FolderDef & {
48
+ anonymous: FolderDef;
49
+ authenticated: FolderDef;
50
+ };
51
+ };
52
+ }
53
+
54
+ export interface UserDataTree {
55
+ mail: FolderDef & {
56
+ messages: FolderDef;
57
+ delivery: FolderDef;
58
+ params: FolderDef;
59
+ };
60
+ store: FolderDef & {
61
+ objects: FolderDef;
62
+ transactions: FolderDef;
63
+ root: FolderDef;
64
+ params: FolderDef;
65
+ };
66
+ info: FolderDef;
67
+ }
68
+
69
+ export function treeInRootFolder(rootFolder: string): RootDataTree {
70
+ const defaults = join(rootFolder, 'defaults');
71
+ return {
72
+ users: { path: join(rootFolder, 'users') },
73
+ defaults: {
74
+ path: defaults,
75
+ },
76
+ tokens: { path: tokensInRootFolder(rootFolder) }
77
+ };
78
+ }
79
+
80
+ export function tokensInRootFolder(rootFolder: string): string {
81
+ return join(rootFolder, 'tokens');
82
+ }
83
+
84
+ export function userDataInRootFolder(root: string, userId: string): string {
85
+ return join(root, 'users', addressToFName(userId));
86
+ }
87
+
88
+ // XXX use this instead of userDataInRootFolder for locker storage
89
+ export function lockerDataInRootFolder(root: string, lockerId: string): string {
90
+ return join(root, 'lockers', idStringToFName(lockerId));
91
+ }
92
+
93
+ export function treeInDefaultsFolder(defaults: string): DefaultsDataTree {
94
+ const mail = join(defaults, 'mail');
95
+ const mailParams = join(mail, 'params');
96
+ const store = join(defaults, 'store');
97
+ return {
98
+ info: { path: join(defaults, 'info') },
99
+ mail: {
100
+ path: mail,
101
+ params: {
102
+ path: mailParams,
103
+ anonymous: { path: join(mailParams, 'anonymous') },
104
+ authenticated: { path: join(mailParams, 'authenticated') }
105
+ }
106
+ },
107
+ store: {
108
+ path: store,
109
+ params: { path: join(store, 'params') }
110
+ }
111
+ };
112
+ }
113
+
114
+ export function treeInUserFolder(userFoder: string): UserDataTree {
115
+ const mail = join(userFoder, 'mail');
116
+ const store = join(userFoder, 'store');
117
+ return {
118
+ info: { path: join(userFoder, 'info') },
119
+ mail: {
120
+ path: mail,
121
+ delivery: { path: join(mail, 'delivery') },
122
+ messages: { path: join(mail, 'messages') },
123
+ params: { path: join(mail, 'params') }
124
+ },
125
+ store: {
126
+ path: store,
127
+ params: { path: join(store, 'params') },
128
+ root: { path: join(store, 'root') },
129
+ objects: { path: join(store, 'objects') },
130
+ transactions: { path: join(store, 'transactions') }
131
+ }
132
+ };
133
+ }
134
+
135
+ export async function createFolderTree(
136
+ structure: RootDataTree|UserDataTree|DefaultsDataTree|FolderDef
137
+ ): Promise<void> {
138
+ for (const [ key, def ] of Object.entries(structure)) {
139
+ if (key === 'path') {
140
+ await fsPromises.mkdir(def, { recursive: true });
141
+ } else {
142
+ await createFolderTree(def);
143
+ }
144
+ }
145
+ }
146
+
147
+ export function createFolderTreeSync(
148
+ structure: RootDataTree|UserDataTree|DefaultsDataTree|string
149
+ ): void {
150
+ for (const [ key, def ] of Object.entries(structure)) {
151
+ if (key === 'path') {
152
+ mkdirSync(def, { recursive: true });
153
+ } else {
154
+ createFolderTreeSync(def);
155
+ }
156
+ }
157
+ }
158
+
159
+
160
+ Object.freeze(exports);
@@ -0,0 +1,41 @@
1
+ /*
2
+ Copyright (C) 2015 - 2016 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
+ /**
19
+ * This module is a test-grade function to calculate DH-shared keys for login.
20
+ */
21
+
22
+ import { box } from 'ecma-nacl';
23
+ import * as random from '../../lib-common/random-node';
24
+
25
+ // XXX update the key from time-to-time, and this must be single instance
26
+
27
+ const skeyForPKLChallenge = random.bytesSync(32);
28
+
29
+ const pkeyForPKLChallenge = box.generate_pubkey(skeyForPKLChallenge);
30
+
31
+ export function calcNaClBoxSharedKey(userPubKey: Uint8Array): {
32
+ dhsharedKey: Uint8Array; serverPubKey: Uint8Array;
33
+ } {
34
+ const dhsharedKey = box.calc_dhshared_key(userPubKey, skeyForPKLChallenge);
35
+ return {
36
+ dhsharedKey: dhsharedKey,
37
+ serverPubKey: pkeyForPKLChallenge
38
+ };
39
+ }
40
+
41
+ Object.freeze(exports);
@@ -0,0 +1,232 @@
1
+ /*
2
+ Copyright (C) 2015 - 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 * as express from 'express';
19
+ import * as http from 'http';
20
+
21
+ // Constant custom header
22
+ export const SESSION_ID_HEADER = "X-Session-Id";
23
+ const LOW_CASED_SESSION_ID_HEADER = SESSION_ID_HEADER.toLowerCase();
24
+
25
+ export interface SessionParams {
26
+ userId: string;
27
+ }
28
+
29
+ export interface Session<T> {
30
+ params: T;
31
+ isAuthorized: boolean;
32
+ id: string;
33
+ lastAccessedAt: number;
34
+ addCleanUp(func: { (): void; }): void;
35
+ putIdIntoResponse(res: express.Response): void;
36
+ close(): void;
37
+ }
38
+
39
+ export interface Request<T> extends express.Request {
40
+ session: Session<T>;
41
+ }
42
+
43
+ export interface BaseRequest<T> extends http.IncomingMessage {
44
+ session: Session<T>;
45
+ }
46
+
47
+ export type CheckSession<T> = (req: BaseRequest<T>) => Promise<boolean>;
48
+
49
+ export type GenerateSession<T> = () => Promise<Session<T>>;
50
+
51
+ export interface Factory<T> {
52
+ generate: GenerateSession<T>;
53
+ ensureAuthorizedSession(): express.RequestHandler;
54
+ ensureOpenedSession(): express.RequestHandler;
55
+ checkSession(): express.RequestHandler;
56
+ ensureAuthorizedSessionForSocketStart(): CheckSession<T>;
57
+ }
58
+
59
+ export abstract class BaseSessionFactory<T> implements Factory<T> {
60
+
61
+ abstract generate(): Promise<Session<T>>;
62
+
63
+ protected abstract get(sId: string): Promise<Session<T>|undefined>;
64
+
65
+ protected abstract add(s: Session<any>): Promise<void>;
66
+
67
+ protected abstract remove(s: Session<T>): Promise<void>;
68
+
69
+ protected async makeSession(id: string, params: T): Promise<Session<T>> {
70
+ const cleanUpFuncs: ({ (): void; }|undefined)[] = [];
71
+ const session: Session<T> = {
72
+ params,
73
+ isAuthorized: false,
74
+ id: id,
75
+ lastAccessedAt: Date.now(),
76
+ addCleanUp: (func: { (): void; }): void => {
77
+ if ('function' !== typeof func) { throw new Error(
78
+ "Given argument func must be function."); }
79
+ cleanUpFuncs.push(func);
80
+ },
81
+ close: (): void => {
82
+ this.remove(session);
83
+ for (let i=0; i<cleanUpFuncs.length; i+=1) {
84
+ const func = cleanUpFuncs[i];
85
+ cleanUpFuncs[i] = undefined;
86
+ try {
87
+ if (typeof func === 'function') { func(); }
88
+ } catch (err) {
89
+ // where to log error(s)?
90
+ }
91
+ }
92
+ },
93
+ putIdIntoResponse: (res: express.Response): void => {
94
+ const header = {};
95
+ header[SESSION_ID_HEADER] = session.id;
96
+ res.set(header);
97
+ }
98
+ };
99
+ Object.seal(session);
100
+ await this.add(session);
101
+ return session;
102
+ }
103
+
104
+ /**
105
+ * This returns middleware function, that adds to request object a 'session'
106
+ * field with existing valid session object, or, if no session found, and
107
+ * it is configured so, responds with 401.
108
+ * @param factory to which resulting middleware is bound.
109
+ * @param send401WhenMissingSession is a flag, which, when true, makes
110
+ * middleware function to send 401 reply, when valid session object cannot
111
+ * be found.
112
+ * @param sessionMustBeAuthorized is a flag, which, when true,, makes
113
+ * middleware function to send 401 reply, when session needs to go through
114
+ * sender authorization step.
115
+ */
116
+ private makeSessionMiddleware(
117
+ send401WhenMissingSession: boolean,
118
+ sessionMustBeAuthorized: boolean
119
+ ): express.RequestHandler {
120
+ return async (
121
+ req: Request<T>, res: express.Response, next: express.NextFunction
122
+ ) => {
123
+ if ('OPTIONS' == req.method) {
124
+ next();
125
+ return;
126
+ }
127
+
128
+ // get session id header
129
+ const sessionId = req.get(SESSION_ID_HEADER);
130
+ if (typeof sessionId !== 'string') {
131
+ if (send401WhenMissingSession) {
132
+ res.status(401).send("Required to start new session.");
133
+ } else {
134
+ next();
135
+ }
136
+ return;
137
+ }
138
+
139
+ // get session, and attach it to request
140
+ const session = await this.get(sessionId);
141
+ if (session) {
142
+ req.session = session;
143
+ session.lastAccessedAt = Date.now();
144
+ }
145
+
146
+ // send error status or continue, depending on session and flags
147
+ if (send401WhenMissingSession) {
148
+ if (!req.session) {
149
+ res.status(401).send("Required to start new session.");
150
+ } else if (sessionMustBeAuthorized &&
151
+ !req.session.isAuthorized) {
152
+ res.status(401).send(
153
+ "Required to complete authorization step.");
154
+ } else {
155
+ next();
156
+ }
157
+ } else {
158
+ next();
159
+ }
160
+ };
161
+ }
162
+
163
+ /**
164
+ * This returns middleware function, that adds to request object a 'session'
165
+ * field with existing valid session object, or, if no session found, or if
166
+ * it is not yet set as authorized, responds with 401.
167
+ */
168
+ ensureAuthorizedSession(): express.RequestHandler {
169
+ return this.makeSessionMiddleware(true, true);
170
+ }
171
+
172
+ /**
173
+ * This returns middleware function, that adds to request object a 'session'
174
+ * field with existing valid session object, even if session is not yet
175
+ * authorized. If there is no session found, middleware responds with 401.
176
+ */
177
+ ensureOpenedSession(): express.RequestHandler {
178
+ return this.makeSessionMiddleware(true, false);
179
+ }
180
+
181
+ /**
182
+ * This returns middleware function, that adds to request object a 'session'
183
+ * field with existing valid session object, or creates a new not authorized,
184
+ * yet, session.
185
+ */
186
+ checkSession(): express.RequestHandler {
187
+ return this.makeSessionMiddleware(false, false);
188
+ }
189
+
190
+ /**
191
+ * This returns a function that checks for an authorized session, attaching
192
+ * it to request object, and resolving returned promise to true. Else, if
193
+ * there is no session found, or if a session is not yet set as authorized,
194
+ * returned promise resolves to false.
195
+ * This function should be used for verification of incoming web-socket
196
+ * connection requests.
197
+ */
198
+ ensureAuthorizedSessionForSocketStart(): CheckSession<T> {
199
+ return async (req: BaseRequest<T>): Promise<boolean> => {
200
+ // get session id header
201
+ const sessionId = req.headers[LOW_CASED_SESSION_ID_HEADER];
202
+ if (typeof sessionId !== 'string') { return false; }
203
+
204
+ // get session, and check it
205
+ const session = await this.get(sessionId);
206
+ if (!session) { return false; }
207
+ if (!session.isAuthorized) { return false; }
208
+
209
+ // attach session to request
210
+ req.session = session;
211
+ session.lastAccessedAt = Date.now();
212
+ return true;
213
+ };
214
+ }
215
+
216
+ }
217
+ Object.freeze(BaseSessionFactory.prototype);
218
+ Object.freeze(BaseSessionFactory);
219
+
220
+ export function wrapFactory<T>(impl: Factory<T>): Factory<T> {
221
+ const wrap: Factory<T> = {
222
+ checkSession: impl.checkSession.bind(impl),
223
+ ensureAuthorizedSession: impl.ensureAuthorizedSession.bind(impl),
224
+ ensureOpenedSession: impl.ensureOpenedSession.bind(impl),
225
+ ensureAuthorizedSessionForSocketStart:
226
+ impl.ensureAuthorizedSessionForSocketStart.bind(impl),
227
+ generate: impl.generate.bind(impl)
228
+ };
229
+ return wrap;
230
+ }
231
+
232
+ Object.freeze(exports);
@@ -0,0 +1,147 @@
1
+ /*
2
+ Copyright (C) 2015 - 2017, 2020 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 * as fs from '../../lib-common/async-fs-node';
19
+ import { FileException, Code as excCode } from '../../lib-common/exceptions/file';
20
+ import { stringToNumOfBytes } from '../conf-util';
21
+ import { toCanonicalAddress } from '../../lib-common/canonical-address';
22
+ import { base64urlSafe, utf8 } from '../../lib-common/buffer-utils';
23
+ import { defer } from '../../lib-common/processes';
24
+ import { join } from 'path';
25
+
26
+ const DEFAULT_FILE_WRITE_BUFFER_SIZE = 4*1024;
27
+ const DEFAULT_FILE_READ_BUFFER_SIZE = 64*1024;
28
+
29
+ export const SC = {
30
+ USER_UNKNOWN: 'user-unknown',
31
+ };
32
+ Object.freeze(SC);
33
+
34
+ async function readJsonFile<T>(path): Promise<T> {
35
+ const buf = await fs.readFile(path);
36
+ return <T> JSON.parse(buf.toString('utf8'));
37
+ }
38
+
39
+ export function addressToFName(address: string): string {
40
+ return base64urlSafe.pack(utf8.pack(toCanonicalAddress(address)));
41
+ }
42
+
43
+
44
+ export abstract class UserFiles<ParamTypes extends object> {
45
+
46
+ protected readonly fileWritingBufferSize: number;
47
+ protected readonly fileReadingBufferSize: number;
48
+
49
+ constructor(
50
+ public readonly userId: string,
51
+ public readonly path: string,
52
+ writeBufferSize?: string|number, readBufferSize?: string|number
53
+ ) {
54
+ this.fileWritingBufferSize = (writeBufferSize ?
55
+ stringToNumOfBytes(writeBufferSize) :
56
+ DEFAULT_FILE_WRITE_BUFFER_SIZE
57
+ );
58
+ this.fileReadingBufferSize = (readBufferSize ?
59
+ stringToNumOfBytes(readBufferSize) :
60
+ DEFAULT_FILE_READ_BUFFER_SIZE
61
+ );
62
+ }
63
+
64
+ async ensureUserExistsOnDisk(): Promise<void> {
65
+ try {
66
+ const stats = await fs.stat(this.path);
67
+ if (!stats.isDirectory()) {
68
+ throw new Error(
69
+ `Path for users' folder is not a folder: ${this.path}`
70
+ );
71
+ }
72
+ } catch (err) {
73
+ if ((err as FileException).code === excCode.notFound) {
74
+ throw SC.USER_UNKNOWN;
75
+ }
76
+ throw err;
77
+ }
78
+ }
79
+
80
+ private commonInfoFolder(): string {
81
+ return join(this.path, '..', 'info');
82
+ }
83
+
84
+ getSpaceQuota(): Promise<number> {
85
+ return readJsonFile<number>(join(this.commonInfoFolder(), 'quota'));
86
+ }
87
+
88
+ getParam<P extends keyof ParamTypes>(
89
+ paramFileName: P
90
+ ): Promise<ParamTypes[P]> {
91
+ const filePath = join(this.path, 'params', paramFileName as string);
92
+ return readJsonFile<ParamTypes[P]>(filePath);
93
+ }
94
+
95
+ protected setParam<P extends keyof ParamTypes>(
96
+ paramFileName: P, param: ParamTypes[P]
97
+ ): Promise<void> {
98
+ const filePath = join(this.path, 'params', paramFileName as string);
99
+ return fs.writeFile(
100
+ filePath, JSON.stringify(param), { encoding: 'utf8', flag: 'w' }
101
+ );
102
+ }
103
+
104
+ }
105
+ Object.freeze(UserFiles.prototype);
106
+ Object.freeze(UserFiles);
107
+
108
+
109
+ export type ObjPipe = (outStream: NodeJS.WritableStream) => Promise<void>;
110
+
111
+ export interface ObjReader {
112
+ len: number;
113
+
114
+ /**
115
+ * This is a pipe function that reads bytes directly from file.
116
+ */
117
+ pipe?: ObjPipe;
118
+
119
+ /**
120
+ * This is header length. It is present only if header is included in piped
121
+ * bytes.
122
+ */
123
+ headerLen?: number;
124
+
125
+ /**
126
+ * This is a total segments' length of this object version.
127
+ */
128
+ segsLen: number;
129
+
130
+ }
131
+
132
+ export async function pipeBytes(src: NodeJS.ReadableStream,
133
+ sink: NodeJS.WritableStream): Promise<void> {
134
+ const deferred = defer<void>();
135
+ src.pipe(sink, { end: false });
136
+ src.on('error', (err) => {
137
+ deferred.reject(err);
138
+ src.unpipe(sink);
139
+ });
140
+ src.on('end', () => {
141
+ src.unpipe(sink);
142
+ deferred.resolve();
143
+ });
144
+ return deferred.promise;
145
+ }
146
+
147
+ Object.freeze(exports);