@milaboratories/pl-drivers 1.9.0 → 1.9.1

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 (331) hide show
  1. package/dist/clients/constructors.cjs +45 -0
  2. package/dist/clients/constructors.cjs.map +1 -0
  3. package/dist/clients/constructors.d.ts +0 -1
  4. package/dist/clients/constructors.js +39 -0
  5. package/dist/clients/constructors.js.map +1 -0
  6. package/dist/clients/download.cjs +149 -0
  7. package/dist/clients/download.cjs.map +1 -0
  8. package/dist/clients/download.d.ts +0 -1
  9. package/dist/clients/download.js +121 -0
  10. package/dist/clients/download.js.map +1 -0
  11. package/dist/clients/logs.cjs +44 -0
  12. package/dist/clients/logs.cjs.map +1 -0
  13. package/dist/clients/logs.d.ts +0 -1
  14. package/dist/clients/logs.js +42 -0
  15. package/dist/clients/logs.js.map +1 -0
  16. package/dist/clients/ls_api.cjs +23 -0
  17. package/dist/clients/ls_api.cjs.map +1 -0
  18. package/dist/clients/ls_api.d.ts +0 -1
  19. package/dist/clients/ls_api.js +21 -0
  20. package/dist/clients/ls_api.js.map +1 -0
  21. package/dist/clients/progress.cjs +58 -0
  22. package/dist/clients/progress.cjs.map +1 -0
  23. package/dist/clients/progress.d.ts +1 -3
  24. package/dist/clients/progress.js +56 -0
  25. package/dist/clients/progress.js.map +1 -0
  26. package/dist/clients/upload.cjs +188 -0
  27. package/dist/clients/upload.cjs.map +1 -0
  28. package/dist/clients/upload.d.ts +1 -3
  29. package/dist/clients/upload.js +163 -0
  30. package/dist/clients/upload.js.map +1 -0
  31. package/dist/drivers/download_blob/blob_key.cjs +34 -0
  32. package/dist/drivers/download_blob/blob_key.cjs.map +1 -0
  33. package/dist/drivers/download_blob/blob_key.d.ts +0 -1
  34. package/dist/drivers/download_blob/blob_key.js +12 -0
  35. package/dist/drivers/download_blob/blob_key.js.map +1 -0
  36. package/dist/drivers/download_blob/download_blob.cjs +442 -0
  37. package/dist/drivers/download_blob/download_blob.cjs.map +1 -0
  38. package/dist/drivers/download_blob/download_blob.d.ts +0 -1
  39. package/dist/drivers/download_blob/download_blob.js +417 -0
  40. package/dist/drivers/download_blob/download_blob.js.map +1 -0
  41. package/dist/drivers/download_blob/download_blob_task.cjs +170 -0
  42. package/dist/drivers/download_blob/download_blob_task.cjs.map +1 -0
  43. package/dist/drivers/download_blob/download_blob_task.d.ts +0 -1
  44. package/dist/drivers/download_blob/download_blob_task.js +146 -0
  45. package/dist/drivers/download_blob/download_blob_task.js.map +1 -0
  46. package/dist/drivers/download_blob/sparse_cache/cache.cjs +202 -0
  47. package/dist/drivers/download_blob/sparse_cache/cache.cjs.map +1 -0
  48. package/dist/drivers/download_blob/sparse_cache/cache.d.ts +0 -1
  49. package/dist/drivers/download_blob/sparse_cache/cache.js +197 -0
  50. package/dist/drivers/download_blob/sparse_cache/cache.js.map +1 -0
  51. package/dist/drivers/download_blob/sparse_cache/file.cjs +61 -0
  52. package/dist/drivers/download_blob/sparse_cache/file.cjs.map +1 -0
  53. package/dist/drivers/download_blob/sparse_cache/file.d.ts +0 -1
  54. package/dist/drivers/download_blob/sparse_cache/file.js +39 -0
  55. package/dist/drivers/download_blob/sparse_cache/file.js.map +1 -0
  56. package/dist/drivers/download_blob/sparse_cache/ranges.cjs +104 -0
  57. package/dist/drivers/download_blob/sparse_cache/ranges.cjs.map +1 -0
  58. package/dist/drivers/download_blob/sparse_cache/ranges.d.ts +0 -1
  59. package/dist/drivers/download_blob/sparse_cache/ranges.js +76 -0
  60. package/dist/drivers/download_blob/sparse_cache/ranges.js.map +1 -0
  61. package/dist/drivers/download_blob_url/driver.cjs +169 -0
  62. package/dist/drivers/download_blob_url/driver.cjs.map +1 -0
  63. package/dist/drivers/download_blob_url/driver.d.ts +0 -1
  64. package/dist/drivers/download_blob_url/driver.js +148 -0
  65. package/dist/drivers/download_blob_url/driver.js.map +1 -0
  66. package/dist/drivers/download_blob_url/driver_id.cjs +9 -0
  67. package/dist/drivers/download_blob_url/driver_id.cjs.map +1 -0
  68. package/dist/drivers/download_blob_url/driver_id.d.ts +0 -1
  69. package/dist/drivers/download_blob_url/driver_id.js +7 -0
  70. package/dist/drivers/download_blob_url/driver_id.js.map +1 -0
  71. package/dist/drivers/download_blob_url/snapshot.cjs +18 -0
  72. package/dist/drivers/download_blob_url/snapshot.cjs.map +1 -0
  73. package/dist/drivers/download_blob_url/snapshot.d.ts +2 -3
  74. package/dist/drivers/download_blob_url/snapshot.js +15 -0
  75. package/dist/drivers/download_blob_url/snapshot.js.map +1 -0
  76. package/dist/drivers/download_blob_url/task.cjs +209 -0
  77. package/dist/drivers/download_blob_url/task.cjs.map +1 -0
  78. package/dist/drivers/download_blob_url/task.d.ts +0 -1
  79. package/dist/drivers/download_blob_url/task.js +184 -0
  80. package/dist/drivers/download_blob_url/task.js.map +1 -0
  81. package/dist/drivers/download_blob_url/url.d.ts +1 -1
  82. package/dist/drivers/download_url/driver.cjs +149 -0
  83. package/dist/drivers/download_url/driver.cjs.map +1 -0
  84. package/dist/drivers/download_url/driver.d.ts +0 -1
  85. package/dist/drivers/download_url/driver.js +128 -0
  86. package/dist/drivers/download_url/driver.js.map +1 -0
  87. package/dist/drivers/download_url/task.cjs +150 -0
  88. package/dist/drivers/download_url/task.cjs.map +1 -0
  89. package/dist/drivers/download_url/task.d.ts +0 -1
  90. package/dist/drivers/download_url/task.js +124 -0
  91. package/dist/drivers/download_url/task.js.map +1 -0
  92. package/dist/drivers/helpers/download_local_handle.cjs +26 -0
  93. package/dist/drivers/helpers/download_local_handle.cjs.map +1 -0
  94. package/dist/drivers/helpers/download_local_handle.d.ts +0 -1
  95. package/dist/drivers/helpers/download_local_handle.js +22 -0
  96. package/dist/drivers/helpers/download_local_handle.js.map +1 -0
  97. package/dist/drivers/helpers/download_remote_handle.cjs +36 -0
  98. package/dist/drivers/helpers/download_remote_handle.cjs.map +1 -0
  99. package/dist/drivers/helpers/download_remote_handle.d.ts +0 -1
  100. package/dist/drivers/helpers/download_remote_handle.js +32 -0
  101. package/dist/drivers/helpers/download_remote_handle.js.map +1 -0
  102. package/dist/drivers/helpers/files_cache.cjs +68 -0
  103. package/dist/drivers/helpers/files_cache.cjs.map +1 -0
  104. package/dist/drivers/helpers/files_cache.d.ts +0 -1
  105. package/dist/drivers/helpers/files_cache.js +66 -0
  106. package/dist/drivers/helpers/files_cache.js.map +1 -0
  107. package/dist/drivers/helpers/helpers.cjs +34 -0
  108. package/dist/drivers/helpers/helpers.cjs.map +1 -0
  109. package/dist/drivers/helpers/helpers.d.ts +0 -1
  110. package/dist/drivers/helpers/helpers.js +31 -0
  111. package/dist/drivers/helpers/helpers.js.map +1 -0
  112. package/dist/drivers/helpers/logs_handle.cjs +50 -0
  113. package/dist/drivers/helpers/logs_handle.cjs.map +1 -0
  114. package/dist/drivers/helpers/logs_handle.d.ts +0 -1
  115. package/dist/drivers/helpers/logs_handle.js +43 -0
  116. package/dist/drivers/helpers/logs_handle.js.map +1 -0
  117. package/dist/drivers/helpers/ls_remote_import_handle.cjs +34 -0
  118. package/dist/drivers/helpers/ls_remote_import_handle.cjs.map +1 -0
  119. package/dist/drivers/helpers/ls_remote_import_handle.d.ts +0 -1
  120. package/dist/drivers/helpers/ls_remote_import_handle.js +29 -0
  121. package/dist/drivers/helpers/ls_remote_import_handle.js.map +1 -0
  122. package/dist/drivers/helpers/ls_storage_entry.cjs +64 -0
  123. package/dist/drivers/helpers/ls_storage_entry.cjs.map +1 -0
  124. package/dist/drivers/helpers/ls_storage_entry.d.ts +0 -1
  125. package/dist/drivers/helpers/ls_storage_entry.js +58 -0
  126. package/dist/drivers/helpers/ls_storage_entry.js.map +1 -0
  127. package/dist/drivers/helpers/polling_ops.d.ts +0 -1
  128. package/dist/drivers/helpers/read_file.cjs +54 -0
  129. package/dist/drivers/helpers/read_file.cjs.map +1 -0
  130. package/dist/drivers/helpers/read_file.d.ts +0 -1
  131. package/dist/drivers/helpers/read_file.js +33 -0
  132. package/dist/drivers/helpers/read_file.js.map +1 -0
  133. package/dist/drivers/helpers/test_helpers.d.ts +0 -1
  134. package/dist/drivers/logs.cjs +118 -0
  135. package/dist/drivers/logs.cjs.map +1 -0
  136. package/dist/drivers/logs.d.ts +0 -1
  137. package/dist/drivers/logs.js +116 -0
  138. package/dist/drivers/logs.js.map +1 -0
  139. package/dist/drivers/logs_stream.cjs +238 -0
  140. package/dist/drivers/logs_stream.cjs.map +1 -0
  141. package/dist/drivers/logs_stream.d.ts +0 -1
  142. package/dist/drivers/logs_stream.js +236 -0
  143. package/dist/drivers/logs_stream.js.map +1 -0
  144. package/dist/drivers/ls.cjs +236 -0
  145. package/dist/drivers/ls.cjs.map +1 -0
  146. package/dist/drivers/ls.d.ts +0 -1
  147. package/dist/drivers/ls.js +214 -0
  148. package/dist/drivers/ls.js.map +1 -0
  149. package/dist/drivers/types.cjs +72 -0
  150. package/dist/drivers/types.cjs.map +1 -0
  151. package/dist/drivers/types.d.ts +4 -5
  152. package/dist/drivers/types.js +64 -0
  153. package/dist/drivers/types.js.map +1 -0
  154. package/dist/drivers/upload.cjs +154 -0
  155. package/dist/drivers/upload.cjs.map +1 -0
  156. package/dist/drivers/upload.d.ts +0 -1
  157. package/dist/drivers/upload.js +151 -0
  158. package/dist/drivers/upload.js.map +1 -0
  159. package/dist/drivers/upload_task.cjs +293 -0
  160. package/dist/drivers/upload_task.cjs.map +1 -0
  161. package/dist/drivers/upload_task.d.ts +0 -1
  162. package/dist/drivers/upload_task.js +285 -0
  163. package/dist/drivers/upload_task.js.map +1 -0
  164. package/dist/drivers/urls/url.cjs +59 -0
  165. package/dist/drivers/urls/url.cjs.map +1 -0
  166. package/dist/drivers/urls/url.d.ts +0 -1
  167. package/dist/drivers/urls/url.js +54 -0
  168. package/dist/drivers/urls/url.js.map +1 -0
  169. package/dist/drivers/virtual_storages.cjs +53 -0
  170. package/dist/drivers/virtual_storages.cjs.map +1 -0
  171. package/dist/drivers/virtual_storages.d.ts +0 -1
  172. package/dist/drivers/virtual_storages.js +51 -0
  173. package/dist/drivers/virtual_storages.js.map +1 -0
  174. package/dist/helpers/download.cjs +63 -0
  175. package/dist/helpers/download.cjs.map +1 -0
  176. package/dist/helpers/download.d.ts +0 -1
  177. package/dist/helpers/download.js +60 -0
  178. package/dist/helpers/download.js.map +1 -0
  179. package/dist/helpers/validate.cjs +12 -0
  180. package/dist/helpers/validate.cjs.map +1 -0
  181. package/dist/helpers/validate.d.ts +0 -1
  182. package/dist/helpers/validate.js +10 -0
  183. package/dist/helpers/validate.js.map +1 -0
  184. package/dist/index.cjs +72 -0
  185. package/dist/index.cjs.map +1 -0
  186. package/dist/index.d.ts +0 -1
  187. package/dist/index.js +19 -2
  188. package/dist/index.js.map +1 -1
  189. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts +0 -1
  190. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.cjs +261 -0
  191. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.cjs.map +1 -0
  192. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.cjs +31 -0
  193. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.cjs.map +1 -0
  194. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts +3 -5
  195. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.js +29 -0
  196. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.js.map +1 -0
  197. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts +0 -1
  198. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.js +256 -0
  199. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.js.map +1 -0
  200. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.cjs +301 -0
  201. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.cjs.map +1 -0
  202. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.cjs +34 -0
  203. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.cjs.map +1 -0
  204. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts +3 -5
  205. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.js +32 -0
  206. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.js.map +1 -0
  207. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts +0 -1
  208. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.js +296 -0
  209. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.js.map +1 -0
  210. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.cjs +410 -0
  211. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.cjs.map +1 -0
  212. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.cjs +38 -0
  213. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.cjs.map +1 -0
  214. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts +3 -5
  215. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.js +36 -0
  216. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.js.map +1 -0
  217. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts +0 -1
  218. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.js +403 -0
  219. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.js.map +1 -0
  220. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.cjs +486 -0
  221. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.cjs.map +1 -0
  222. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.cjs +86 -0
  223. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.cjs.map +1 -0
  224. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts +3 -5
  225. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.js +84 -0
  226. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.js.map +1 -0
  227. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts +0 -1
  228. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.js +478 -0
  229. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.js.map +1 -0
  230. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.cjs +712 -0
  231. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.cjs.map +1 -0
  232. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.cjs +71 -0
  233. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.cjs.map +1 -0
  234. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.d.ts +3 -5
  235. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.js +69 -0
  236. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.js.map +1 -0
  237. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.d.ts +0 -1
  238. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.js +701 -0
  239. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.js.map +1 -0
  240. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts +3 -5
  241. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +0 -1
  242. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts +0 -1
  243. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts +0 -1
  244. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts +0 -1
  245. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts +0 -1
  246. package/dist/proto/google/api/http.d.ts +0 -1
  247. package/dist/proto/google/protobuf/any.d.ts +0 -1
  248. package/dist/proto/google/protobuf/descriptor.d.ts +0 -1
  249. package/dist/proto/google/protobuf/duration.cjs +105 -0
  250. package/dist/proto/google/protobuf/duration.cjs.map +1 -0
  251. package/dist/proto/google/protobuf/duration.d.ts +0 -1
  252. package/dist/proto/google/protobuf/duration.js +103 -0
  253. package/dist/proto/google/protobuf/duration.js.map +1 -0
  254. package/dist/proto/google/protobuf/empty.d.ts +0 -1
  255. package/dist/proto/google/protobuf/struct.d.ts +0 -1
  256. package/dist/proto/google/protobuf/timestamp.cjs +133 -0
  257. package/dist/proto/google/protobuf/timestamp.cjs.map +1 -0
  258. package/dist/proto/google/protobuf/timestamp.d.ts +0 -1
  259. package/dist/proto/google/protobuf/timestamp.js +131 -0
  260. package/dist/proto/google/protobuf/timestamp.js.map +1 -0
  261. package/dist/proto/google/protobuf/wrappers.d.ts +0 -1
  262. package/dist/test_env.d.ts +0 -1
  263. package/package.json +16 -15
  264. package/dist/clients/constructors.d.ts.map +0 -1
  265. package/dist/clients/download.d.ts.map +0 -1
  266. package/dist/clients/logs.d.ts.map +0 -1
  267. package/dist/clients/ls_api.d.ts.map +0 -1
  268. package/dist/clients/progress.d.ts.map +0 -1
  269. package/dist/clients/upload.d.ts.map +0 -1
  270. package/dist/drivers/download_blob/blob_key.d.ts.map +0 -1
  271. package/dist/drivers/download_blob/download_blob.d.ts.map +0 -1
  272. package/dist/drivers/download_blob/download_blob_task.d.ts.map +0 -1
  273. package/dist/drivers/download_blob/sparse_cache/cache.d.ts.map +0 -1
  274. package/dist/drivers/download_blob/sparse_cache/file.d.ts.map +0 -1
  275. package/dist/drivers/download_blob/sparse_cache/ranges.d.ts.map +0 -1
  276. package/dist/drivers/download_blob_url/driver.d.ts.map +0 -1
  277. package/dist/drivers/download_blob_url/driver_id.d.ts.map +0 -1
  278. package/dist/drivers/download_blob_url/snapshot.d.ts.map +0 -1
  279. package/dist/drivers/download_blob_url/task.d.ts.map +0 -1
  280. package/dist/drivers/download_blob_url/url.d.ts.map +0 -1
  281. package/dist/drivers/download_url/driver.d.ts.map +0 -1
  282. package/dist/drivers/download_url/task.d.ts.map +0 -1
  283. package/dist/drivers/helpers/download_local_handle.d.ts.map +0 -1
  284. package/dist/drivers/helpers/download_remote_handle.d.ts.map +0 -1
  285. package/dist/drivers/helpers/files_cache.d.ts.map +0 -1
  286. package/dist/drivers/helpers/helpers.d.ts.map +0 -1
  287. package/dist/drivers/helpers/logs_handle.d.ts.map +0 -1
  288. package/dist/drivers/helpers/ls_remote_import_handle.d.ts.map +0 -1
  289. package/dist/drivers/helpers/ls_storage_entry.d.ts.map +0 -1
  290. package/dist/drivers/helpers/polling_ops.d.ts.map +0 -1
  291. package/dist/drivers/helpers/read_file.d.ts.map +0 -1
  292. package/dist/drivers/helpers/test_helpers.d.ts.map +0 -1
  293. package/dist/drivers/logs.d.ts.map +0 -1
  294. package/dist/drivers/logs_stream.d.ts.map +0 -1
  295. package/dist/drivers/ls.d.ts.map +0 -1
  296. package/dist/drivers/types.d.ts.map +0 -1
  297. package/dist/drivers/upload.d.ts.map +0 -1
  298. package/dist/drivers/upload_task.d.ts.map +0 -1
  299. package/dist/drivers/urls/url.d.ts.map +0 -1
  300. package/dist/drivers/virtual_storages.d.ts.map +0 -1
  301. package/dist/helpers/download.d.ts.map +0 -1
  302. package/dist/helpers/validate.d.ts.map +0 -1
  303. package/dist/index.d.ts.map +0 -1
  304. package/dist/index.mjs +0 -4892
  305. package/dist/index.mjs.map +0 -1
  306. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts.map +0 -1
  307. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts.map +0 -1
  308. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts.map +0 -1
  309. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts.map +0 -1
  310. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts.map +0 -1
  311. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts.map +0 -1
  312. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts.map +0 -1
  313. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts.map +0 -1
  314. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts.map +0 -1
  315. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.client.d.ts.map +0 -1
  316. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/uploadapi/protocol.d.ts.map +0 -1
  317. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts.map +0 -1
  318. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +0 -1
  319. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts.map +0 -1
  320. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts.map +0 -1
  321. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts.map +0 -1
  322. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts.map +0 -1
  323. package/dist/proto/google/api/http.d.ts.map +0 -1
  324. package/dist/proto/google/protobuf/any.d.ts.map +0 -1
  325. package/dist/proto/google/protobuf/descriptor.d.ts.map +0 -1
  326. package/dist/proto/google/protobuf/duration.d.ts.map +0 -1
  327. package/dist/proto/google/protobuf/empty.d.ts.map +0 -1
  328. package/dist/proto/google/protobuf/struct.d.ts.map +0 -1
  329. package/dist/proto/google/protobuf/timestamp.d.ts.map +0 -1
  330. package/dist/proto/google/protobuf/wrappers.d.ts.map +0 -1
  331. package/dist/test_env.d.ts.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.js","sources":["../../../../src/drivers/download_blob/sparse_cache/cache.ts"],"sourcesContent":["import { RangeBytes } from '@milaboratories/pl-model-common';\nimport { ensureDirExists, fileExists, mapEntries, MiLogger } from '@milaboratories/ts-helpers';\nimport { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport { addRange, doesRangeExist, Ranges, rangesFileName, rangesFilePostfix, rangesSize, readRangesFile, writeRangesFile } from './ranges';\nimport { writeToSparseFile } from './file';\nimport { functions } from '@milaboratories/helpers';\n\n/** The implementer of SparseCacheRanges could throw it if ranges were corrupted. */\nexport class CorruptedRangesError extends Error {\n name = 'CorruptedRangesError';\n}\n\n/** Extracted ranges methods to be able to store ranges somewhere else (e.g. in memory for tests). */\nexport interface SparseCacheRanges {\n get(key: string): Promise<Ranges>;\n set(key: string, ranges: Ranges): Promise<void>;\n delete(key: string): Promise<void>;\n}\n\n/** Stores ranges in a directory as JSON files (the default implementation). */\nexport class SparseCacheFsRanges implements SparseCacheRanges {\n constructor(\n private readonly logger: MiLogger,\n private readonly cacheDir: string,\n ) {}\n\n private fPath(key: string): string {\n return path.join(this.cacheDir, rangesFileName(key));\n }\n\n async get(key: string): Promise<Ranges> {\n return await readRangesFile(this.logger, this.fPath(key));\n }\n\n async set(key: string, ranges: Ranges) {\n return await writeRangesFile(this.logger, this.fPath(key), ranges);\n }\n\n async delete(key: string) {\n await fs.rm(this.fPath(key));\n }\n}\n\n/** Extracted interface for storing sparse files. */\nexport interface SparseFileStorage {\n all(): Promise<string[]>;\n exists(key: string): Promise<boolean>;\n path(key: string): string;\n write(key: string, data: Uint8Array, from: number): Promise<void>;\n delete(key: string): Promise<void>;\n}\n\n/** Stores sparse files in a directory (the default implementation). */\nexport class SparseCacheFsFile implements SparseFileStorage {\n private readonly suffix = '.sparse.bin';\n\n constructor(\n private readonly logger: MiLogger,\n private readonly cacheDir: string,\n ) {}\n\n async all(): Promise<string[]> {\n await ensureDirExists(this.cacheDir);\n const files = await fs.readdir(this.cacheDir);\n return files.filter((f) => f.endsWith(this.suffix));\n }\n\n async exists(key: string): Promise<boolean> {\n return await fileExists(this.path(key));\n }\n\n path(key: string): string {\n return path.join(this.cacheDir, key + this.suffix);\n }\n\n async write(key: string, data: Uint8Array, from: number): Promise<void> {\n await ensureDirExists(this.cacheDir);\n await writeToSparseFile(this.logger, process.platform, this.path(key), data, from);\n }\n\n async delete(key: string): Promise<void> {\n await fs.rm(this.path(key));\n }\n}\n\n/** LRU cache for ranges of sparse files. */\nexport class SparseCache {\n /** Fields are public for tests. */\n\n /** The lock to make sure cache requests are done one by one. */\n private lock = new functions.AwaitLock()\n\n public keyToLastAccessTime = new Map<string, Date>();\n public size = 0;\n\n constructor(\n public readonly logger: MiLogger,\n /** The hard limit in bytes. */\n public readonly maxSize: number,\n public readonly ranges: SparseCacheRanges,\n public readonly storage: SparseFileStorage,\n ) {}\n\n /** Resets a cache's size by rereading everything we already store.\n * Safe for concurrent use. */\n async reset() {\n await withLock(this.lock, async () => {\n await this.resetUnsafe();\n })\n }\n\n /** Returns a path to the key if the range exists in a cache, otherwise returns undefined.\n * Safe for concurrent use. */\n async get(key: string, range: RangeBytes): Promise<string | undefined> {\n return await withLock(this.lock, async () => {\n return await this.getUnsafe(key, range);\n });\n }\n\n /** Sets data to the cache's file and clear the cache if it's needed.\n * Safe for concurrent use. */\n async set(key: string, range: RangeBytes, data: Uint8Array): Promise<void> {\n await withLock(this.lock, async () => {\n await this.setUnsafe(key, range, data);\n })\n }\n\n private async resetUnsafe() {\n this.size = 0;\n this.keyToLastAccessTime = new Map<string, Date>();\n\n const now = new Date();\n // In rmKey method we first deletes the key from a storage and only then from ranges,\n // so if something goes wrong between 2 operations,\n // on reset the logic will be correct.\n for (const key of await this.storage.all()) {\n const ranges = await this.ranges.get(key);\n this.size += rangesSize(ranges);\n this.keyToLastAccessTime.set(key, now);\n }\n }\n\n private async getUnsafe(key: string, range: RangeBytes): Promise<string | undefined> {\n // It first checks the storage, and then the ranges.\n // In another method, when we remove a key, it first deletes a key from a storage and then from ranges,\n // so if we don't have a key in storage but have it in ranges, the logic here is correct.\n // We probably could reverse the operations here and there, and everywhere we work with both storage and ranges.\n if (await this.storage.exists(key)) {\n this.keyToLastAccessTime.set(key, new Date());\n\n const ranges = await this.getRanges(key);\n if (doesRangeExist(ranges, range)) {\n return this.storage.path(key);\n }\n\n return undefined;\n }\n\n return undefined;\n }\n\n private async setUnsafe(key: string, range: { from: number; to: number; }, data: Uint8Array) {\n await this.setWithoutEviction(key, range, data);\n await this.ensureEvicted();\n }\n\n /** Sets a key and recalculates a size, but doesn't ensures that the size is less than the hard limit. */\n async setWithoutEviction(key: string, range: RangeBytes, data: Uint8Array): Promise<void> {\n if (range.to - range.from !== data.length) {\n throw new Error(\n `SparseCache.set: trying to set ${key} with wrong range length: `\n + `range: ${JSON.stringify(range)}, data: ${data.length}`\n );\n }\n\n this.keyToLastAccessTime.set(key, new Date());\n\n const ranges = await this.getRanges(key);\n this.size -= rangesSize(ranges);\n\n await this.storage.write(key, data, range.from);\n\n const newRanges = addRange(ranges, range);\n this.size += rangesSize(newRanges);\n\n await this.ranges.set(key, newRanges);\n }\n\n /** Ensures the size is less than hard limit by deleting the oldest keys. */\n async ensureEvicted(): Promise<void> {\n const byTime = mapEntries(this.keyToLastAccessTime);\n byTime.sort(([_, aDate], [__, bDate]) => bDate.getTime() - aDate.getTime());\n\n while (this.size > this.maxSize) {\n const keyAndDate = byTime.pop(); // removes the oldest\n if (!keyAndDate) {\n break;\n }\n const [key, _] = keyAndDate;\n\n const ranges = await this.getRanges(key);\n this.size -= rangesSize(ranges);\n this.rmKey(key);\n }\n }\n\n /** Gets ranges and if they were corrupted, then remove the file from the cache and reset the cache's size. */\n private async getRanges(key: string) {\n try {\n return await this.ranges.get(key);\n } catch (e: unknown) {\n if (e instanceof CorruptedRangesError) {\n // We need to reset a state of the cache and update current size,\n // it's the only way to calculate the real size when one of the ranges were corrupted.\n await this.rmKey(key);\n await this.resetUnsafe();\n\n return await this.ranges.get(key);\n }\n\n throw e;\n }\n }\n\n /** Removes a key the state of the cache. The size should be updated. */\n private async rmKey(key: string) {\n await this.storage.delete(key);\n await this.ranges.delete(key);\n this.keyToLastAccessTime.delete(key);\n }\n}\n\n/** Acquires the lock and executes a callback. */\nasync function withLock<T>(lock: functions.AwaitLock, cb: () => Promise<T>): Promise<T> {\n try {\n await lock.acquireAsync();\n return await cb();\n } finally {\n lock.release();\n }\n}\n"],"names":["path","fs"],"mappings":";;;;;;;AAQA;AACM,MAAO,oBAAqB,SAAQ,KAAK,CAAA;IAC7C,IAAI,GAAG,sBAAsB;AAC9B;AASD;MACa,mBAAmB,CAAA;AAEX,IAAA,MAAA;AACA,IAAA,QAAA;IAFnB,WAAA,CACmB,MAAgB,EAChB,QAAgB,EAAA;QADhB,IAAA,CAAA,MAAM,GAAN,MAAM;QACN,IAAA,CAAA,QAAQ,GAAR,QAAQ;IACxB;AAEK,IAAA,KAAK,CAAC,GAAW,EAAA;AACvB,QAAA,OAAOA,aAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,CAAC;IACtD;IAEA,MAAM,GAAG,CAAC,GAAW,EAAA;AACnB,QAAA,OAAO,MAAM,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAA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sHelpers = require('@milaboratories/ts-helpers');
4
+ var fsp = require('node:fs/promises');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var fsp__namespace = /*#__PURE__*/_interopNamespaceDefault(fsp);
24
+
25
+ /** Creates a sparse file for all systems
26
+ * Table of what supports sparse files:
27
+ * https://en.wikipedia.org/wiki/Comparison_of_file_systems#Allocation_and_layout_policies */
28
+ async function createSparseFile(logger, path, platform) {
29
+ try {
30
+ const ensureCreated = await fsp__namespace.open(path, 'w');
31
+ await ensureCreated.close();
32
+ await ensureSparseOnWindows(path, platform);
33
+ }
34
+ catch (error) {
35
+ logger.error(`Error creating file ${path} on platform ${platform}: ${error}`);
36
+ }
37
+ }
38
+ /** Sets a sparse flag on Windows.
39
+ * We could check the file is sparse by running:
40
+ * `fsutil sparse queryflag <path>`
41
+ * and
42
+ * `fsutil sparse queryrange <path>`
43
+ */
44
+ async function ensureSparseOnWindows(path, platform) {
45
+ if (platform === 'win32') {
46
+ await tsHelpers.spawnAsync('fsutil', ['sparse', 'setflag', `"${path}"`], { stdio: 'pipe' });
47
+ }
48
+ }
49
+ /** Ensures the file is created and writes to it. */
50
+ async function writeToSparseFile(logger, platform, path, data, from) {
51
+ if (!(await tsHelpers.fileExists(path))) {
52
+ await createSparseFile(logger, path, platform);
53
+ }
54
+ const fileHandle = await fsp__namespace.open(path, 'r+');
55
+ await fileHandle.write(data, 0, data.length, from);
56
+ await fileHandle.close();
57
+ }
58
+
59
+ exports.createSparseFile = createSparseFile;
60
+ exports.writeToSparseFile = writeToSparseFile;
61
+ //# sourceMappingURL=file.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.cjs","sources":["../../../../src/drivers/download_blob/sparse_cache/file.ts"],"sourcesContent":["import { fileExists, MiLogger, spawnAsync } from \"@milaboratories/ts-helpers\";\nimport * as fs from 'node:fs/promises';\n\n/** Creates a sparse file for all systems\n * Table of what supports sparse files:\n * https://en.wikipedia.org/wiki/Comparison_of_file_systems#Allocation_and_layout_policies */\nexport async function createSparseFile(\n logger: MiLogger,\n path: string,\n platform: NodeJS.Platform,\n) {\n try {\n const ensureCreated = await fs.open(path, 'w');\n await ensureCreated.close();\n\n await ensureSparseOnWindows(path, platform);\n } catch (error: unknown) {\n logger.error(`Error creating file ${path} on platform ${platform}: ${error}`);\n }\n}\n\n/** Sets a sparse flag on Windows.\n * We could check the file is sparse by running:\n * `fsutil sparse queryflag <path>`\n * and\n * `fsutil sparse queryrange <path>`\n */\nasync function ensureSparseOnWindows(path: string, platform: NodeJS.Platform) {\n if (platform === 'win32') {\n await spawnAsync('fsutil', ['sparse', 'setflag', `\"${path}\"`], { stdio: 'pipe' });\n }\n}\n\n/** Ensures the file is created and writes to it. */\nexport async function writeToSparseFile(\n logger: MiLogger,\n platform: NodeJS.Platform,\n path: string,\n data: Uint8Array,\n from: number\n) {\n if (!(await fileExists(path))) {\n await createSparseFile(logger, path, platform);\n }\n\n const fileHandle = await fs.open(path, 'r+');\n await fileHandle.write(data, 0, data.length, from);\n await fileHandle.close();\n}\n"],"names":["fs","spawnAsync","fileExists"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA;;AAE6F;AACtF,eAAe,gBAAgB,CACpC,MAAgB,EAChB,IAAY,EACZ,QAAyB,EAAA;AAEzB,IAAA,IAAI;QACF,MAAM,aAAa,GAAG,MAAMA,cAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC;AAC9C,QAAA,MAAM,aAAa,CAAC,KAAK,EAAE;AAE3B,QAAA,MAAM,qBAAqB,CAAC,IAAI,EAAE,QAAQ,CAAC;IAC7C;IAAE,OAAO,KAAc,EAAE;QACvB,MAAM,CAAC,KAAK,CAAC,CAAA,oBAAA,EAAuB,IAAI,CAAA,aAAA,EAAgB,QAAQ,CAAA,EAAA,EAAK,KAAK,CAAA,CAAE,CAAC;IAC/E;AACF;AAEA;;;;;AAKG;AACH,eAAe,qBAAqB,CAAC,IAAY,EAAE,QAAyB,EAAA;AAC1E,IAAA,IAAI,QAAQ,KAAK,OAAO,EAAE;QACxB,MAAMC,oBAAU,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,CAAG,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnF;AACF;AAEA;AACO,eAAe,iBAAiB,CACrC,MAAgB,EAChB,QAAyB,EACzB,IAAY,EACZ,IAAgB,EAChB,IAAY,EAAA;IAEZ,IAAI,EAAE,MAAMC,oBAAU,CAAC,IAAI,CAAC,CAAC,EAAE;QAC7B,MAAM,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC;IAChD;IAEA,MAAM,UAAU,GAAG,MAAMF,cAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC;AAC5C,IAAA,MAAM,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC;AAClD,IAAA,MAAM,UAAU,CAAC,KAAK,EAAE;AAC1B;;;;;"}
@@ -5,4 +5,3 @@ import { MiLogger } from '@milaboratories/ts-helpers';
5
5
  export declare function createSparseFile(logger: MiLogger, path: string, platform: NodeJS.Platform): Promise<void>;
6
6
  /** Ensures the file is created and writes to it. */
7
7
  export declare function writeToSparseFile(logger: MiLogger, platform: NodeJS.Platform, path: string, data: Uint8Array, from: number): Promise<void>;
8
- //# sourceMappingURL=file.d.ts.map
@@ -0,0 +1,39 @@
1
+ import { fileExists, spawnAsync } from '@milaboratories/ts-helpers';
2
+ import * as fsp from 'node:fs/promises';
3
+
4
+ /** Creates a sparse file for all systems
5
+ * Table of what supports sparse files:
6
+ * https://en.wikipedia.org/wiki/Comparison_of_file_systems#Allocation_and_layout_policies */
7
+ async function createSparseFile(logger, path, platform) {
8
+ try {
9
+ const ensureCreated = await fsp.open(path, 'w');
10
+ await ensureCreated.close();
11
+ await ensureSparseOnWindows(path, platform);
12
+ }
13
+ catch (error) {
14
+ logger.error(`Error creating file ${path} on platform ${platform}: ${error}`);
15
+ }
16
+ }
17
+ /** Sets a sparse flag on Windows.
18
+ * We could check the file is sparse by running:
19
+ * `fsutil sparse queryflag <path>`
20
+ * and
21
+ * `fsutil sparse queryrange <path>`
22
+ */
23
+ async function ensureSparseOnWindows(path, platform) {
24
+ if (platform === 'win32') {
25
+ await spawnAsync('fsutil', ['sparse', 'setflag', `"${path}"`], { stdio: 'pipe' });
26
+ }
27
+ }
28
+ /** Ensures the file is created and writes to it. */
29
+ async function writeToSparseFile(logger, platform, path, data, from) {
30
+ if (!(await fileExists(path))) {
31
+ await createSparseFile(logger, path, platform);
32
+ }
33
+ const fileHandle = await fsp.open(path, 'r+');
34
+ await fileHandle.write(data, 0, data.length, from);
35
+ await fileHandle.close();
36
+ }
37
+
38
+ export { createSparseFile, writeToSparseFile };
39
+ //# sourceMappingURL=file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.js","sources":["../../../../src/drivers/download_blob/sparse_cache/file.ts"],"sourcesContent":["import { fileExists, MiLogger, spawnAsync } from \"@milaboratories/ts-helpers\";\nimport * as fs from 'node:fs/promises';\n\n/** Creates a sparse file for all systems\n * Table of what supports sparse files:\n * https://en.wikipedia.org/wiki/Comparison_of_file_systems#Allocation_and_layout_policies */\nexport async function createSparseFile(\n logger: MiLogger,\n path: string,\n platform: NodeJS.Platform,\n) {\n try {\n const ensureCreated = await fs.open(path, 'w');\n await ensureCreated.close();\n\n await ensureSparseOnWindows(path, platform);\n } catch (error: unknown) {\n logger.error(`Error creating file ${path} on platform ${platform}: ${error}`);\n }\n}\n\n/** Sets a sparse flag on Windows.\n * We could check the file is sparse by running:\n * `fsutil sparse queryflag <path>`\n * and\n * `fsutil sparse queryrange <path>`\n */\nasync function ensureSparseOnWindows(path: string, platform: NodeJS.Platform) {\n if (platform === 'win32') {\n await spawnAsync('fsutil', ['sparse', 'setflag', `\"${path}\"`], { stdio: 'pipe' });\n }\n}\n\n/** Ensures the file is created and writes to it. */\nexport async function writeToSparseFile(\n logger: MiLogger,\n platform: NodeJS.Platform,\n path: string,\n data: Uint8Array,\n from: number\n) {\n if (!(await fileExists(path))) {\n await createSparseFile(logger, path, platform);\n }\n\n const fileHandle = await fs.open(path, 'r+');\n await fileHandle.write(data, 0, data.length, from);\n await fileHandle.close();\n}\n"],"names":["fs"],"mappings":";;;AAGA;;AAE6F;AACtF,eAAe,gBAAgB,CACpC,MAAgB,EAChB,IAAY,EACZ,QAAyB,EAAA;AAEzB,IAAA,IAAI;QACF,MAAM,aAAa,GAAG,MAAMA,GAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC;AAC9C,QAAA,MAAM,aAAa,CAAC,KAAK,EAAE;AAE3B,QAAA,MAAM,qBAAqB,CAAC,IAAI,EAAE,QAAQ,CAAC;IAC7C;IAAE,OAAO,KAAc,EAAE;QACvB,MAAM,CAAC,KAAK,CAAC,CAAA,oBAAA,EAAuB,IAAI,CAAA,aAAA,EAAgB,QAAQ,CAAA,EAAA,EAAK,KAAK,CAAA,CAAE,CAAC;IAC/E;AACF;AAEA;;;;;AAKG;AACH,eAAe,qBAAqB,CAAC,IAAY,EAAE,QAAyB,EAAA;AAC1E,IAAA,IAAI,QAAQ,KAAK,OAAO,EAAE;QACxB,MAAM,UAAU,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,CAAG,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnF;AACF;AAEA;AACO,eAAe,iBAAiB,CACrC,MAAgB,EAChB,QAAyB,EACzB,IAAY,EACZ,IAAgB,EAChB,IAAY,EAAA;IAEZ,IAAI,EAAE,MAAM,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE;QAC7B,MAAM,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC;IAChD;IAEA,MAAM,UAAU,GAAG,MAAMA,GAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC;AAC5C,IAAA,MAAM,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC;AAClD,IAAA,MAAM,UAAU,CAAC,KAAK,EAAE;AAC1B;;;;"}
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+ var fsp = require('node:fs/promises');
5
+ var plModelCommon = require('@milaboratories/pl-model-common');
6
+ var tsHelpers = require('@milaboratories/ts-helpers');
7
+ var cache = require('./cache.cjs');
8
+
9
+ function _interopNamespaceDefault(e) {
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var fsp__namespace = /*#__PURE__*/_interopNamespaceDefault(fsp);
27
+
28
+ /** The content of the ranges file: ranges of bytes.
29
+ * The ranges should be normalized: sorted and no overlaps.
30
+ * For that, use `normalizeRanges` function. */
31
+ const Ranges = zod.z.object({
32
+ ranges: zod.z.array(plModelCommon.RangeBytes),
33
+ });
34
+ const rangesFilePostfix = '.ranges.json';
35
+ function rangesFileName(fPath) {
36
+ return fPath + rangesFilePostfix;
37
+ }
38
+ async function readRangesFile(logger, path) {
39
+ let ranges = { ranges: [] };
40
+ try {
41
+ const file = await fsp__namespace.readFile(path, 'utf8');
42
+ ranges = Ranges.parse(JSON.parse(file));
43
+ }
44
+ catch (e) {
45
+ if (e instanceof SyntaxError || e instanceof zod.z.ZodError) {
46
+ const msg = `readRangesFile: the file ${path} was corrupted: ${e}`;
47
+ logger.error(msg);
48
+ throw new cache.CorruptedRangesError(msg);
49
+ }
50
+ if (!(e instanceof Error && 'code' in e && e.code === 'ENOENT')) {
51
+ throw e;
52
+ }
53
+ // If the file does not exist, assume the ranges are empty.
54
+ }
55
+ normalizeRanges(ranges);
56
+ return ranges;
57
+ }
58
+ /** Writes to a temporal file and then renames it atomically. */
59
+ async function writeRangesFile(logger, path, ranges) {
60
+ await tsHelpers.createPathAtomically(logger, path, async (tempPath) => {
61
+ await fsp__namespace.writeFile(tempPath, JSON.stringify(ranges, null, 2), { flag: 'wx' });
62
+ });
63
+ }
64
+ /** Sorts and merges overlapping ranges. */
65
+ function normalizeRanges(s) {
66
+ s.ranges.sort((a, b) => a.from - b.from);
67
+ for (let i = 0; i < s.ranges.length - 1; i++) {
68
+ if (s.ranges[i].to >= s.ranges[i + 1].from) {
69
+ mergeRanges(s, i);
70
+ i--;
71
+ }
72
+ }
73
+ }
74
+ function mergeRanges(s, i) {
75
+ const from = Math.min(s.ranges[i].from, s.ranges[i + 1].from);
76
+ const to = Math.max(s.ranges[i].to, s.ranges[i + 1].to);
77
+ s.ranges.splice(i, 2, { from, to });
78
+ }
79
+ function rangesSize(s) {
80
+ return s.ranges.reduce((acc, range) => acc + range.to - range.from, 0);
81
+ }
82
+ function doesRangeExist(allRanges, range) {
83
+ for (const r of allRanges.ranges) {
84
+ if (r.from <= range.from && range.to <= r.to) {
85
+ return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+ function addRange(s, range) {
91
+ s.ranges.push(range);
92
+ normalizeRanges(s);
93
+ return s;
94
+ }
95
+
96
+ exports.addRange = addRange;
97
+ exports.doesRangeExist = doesRangeExist;
98
+ exports.normalizeRanges = normalizeRanges;
99
+ exports.rangesFileName = rangesFileName;
100
+ exports.rangesFilePostfix = rangesFilePostfix;
101
+ exports.rangesSize = rangesSize;
102
+ exports.readRangesFile = readRangesFile;
103
+ exports.writeRangesFile = writeRangesFile;
104
+ //# sourceMappingURL=ranges.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ranges.cjs","sources":["../../../../src/drivers/download_blob/sparse_cache/ranges.ts"],"sourcesContent":["import { z } from \"zod\";\nimport * as fs from 'node:fs/promises';\nimport { RangeBytes } from \"@milaboratories/pl-model-common\";\nimport { createPathAtomically, MiLogger } from \"@milaboratories/ts-helpers\";\nimport { CorruptedRangesError } from \"./cache\";\n\n/** The content of the ranges file: ranges of bytes.\n * The ranges should be normalized: sorted and no overlaps.\n * For that, use `normalizeRanges` function. */\nconst Ranges = z.object({\n ranges: z.array(RangeBytes),\n});\n\nexport type Ranges = z.infer<typeof Ranges>;\n\nexport const rangesFilePostfix = '.ranges.json';\n\nexport function rangesFileName(fPath: string): string {\n return fPath + rangesFilePostfix;\n}\n\nexport async function readRangesFile(logger: MiLogger, path: string): Promise<Ranges> {\n let ranges: Ranges = { ranges: [] };\n try {\n const file = await fs.readFile(path, 'utf8');\n ranges = Ranges.parse(JSON.parse(file));\n } catch (e: unknown) {\n\n if (e instanceof SyntaxError || e instanceof z.ZodError) {\n const msg = `readRangesFile: the file ${path} was corrupted: ${e}`;\n logger.error(msg);\n throw new CorruptedRangesError(msg);\n }\n\n if (!(e instanceof Error && 'code' in e && e.code === 'ENOENT')) {\n throw e;\n }\n\n // If the file does not exist, assume the ranges are empty.\n }\n\n normalizeRanges(ranges);\n\n return ranges;\n}\n\n/** Writes to a temporal file and then renames it atomically. */\nexport async function writeRangesFile(logger: MiLogger, path: string, ranges: Ranges) {\n await createPathAtomically(logger, path, async (tempPath: string) => {\n await fs.writeFile(tempPath, JSON.stringify(ranges, null, 2), { flag: 'wx' });\n });\n}\n\n/** Sorts and merges overlapping ranges. */\nexport function normalizeRanges(s: Ranges) {\n s.ranges.sort((a, b) => a.from - b.from);\n\n for (let i = 0; i < s.ranges.length - 1; i++) {\n if (s.ranges[i].to >= s.ranges[i + 1].from) {\n mergeRanges(s, i);\n i--;\n }\n }\n}\n\nfunction mergeRanges(s: Ranges, i: number) {\n const from = Math.min(s.ranges[i].from, s.ranges[i + 1].from);\n const to = Math.max(s.ranges[i].to, s.ranges[i + 1].to);\n\n s.ranges.splice(i, 2, { from, to });\n}\n\nexport function rangesSize(s: Ranges) {\n return s.ranges.reduce((acc, range) => acc + range.to - range.from, 0);\n}\n\nexport function doesRangeExist(allRanges: Ranges, range: RangeBytes): boolean {\n for (const r of allRanges.ranges) {\n if (r.from <= range.from && range.to <= r.to) {\n return true;\n }\n }\n\n return false;\n}\n\nexport function addRange(s: Ranges, range: RangeBytes) {\n s.ranges.push(range);\n normalizeRanges(s);\n\n return s;\n}\n\n"],"names":["z","RangeBytes","fs","CorruptedRangesError","createPathAtomically"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;;AAE+C;AAC/C,MAAM,MAAM,GAAGA,KAAC,CAAC,MAAM,CAAC;AACtB,IAAA,MAAM,EAAEA,KAAC,CAAC,KAAK,CAACC,wBAAU,CAAC;AAC5B,CAAA,CAAC;AAIK,MAAM,iBAAiB,GAAG;AAE3B,SAAU,cAAc,CAAC,KAAa,EAAA;IAC1C,OAAO,KAAK,GAAG,iBAAiB;AAClC;AAEO,eAAe,cAAc,CAAC,MAAgB,EAAE,IAAY,EAAA;AACjE,IAAA,IAAI,MAAM,GAAW,EAAE,MAAM,EAAE,EAAE,EAAE;AACnC,IAAA,IAAI;QACF,MAAM,IAAI,GAAG,MAAMC,cAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;AAC5C,QAAA,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzC;IAAE,OAAO,CAAU,EAAE;QAEnB,IAAI,CAAC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@@ -43,4 +43,3 @@ export declare function addRange(s: Ranges, range: RangeBytes): {
43
43
  }[];
44
44
  };
45
45
  export {};
46
- //# sourceMappingURL=ranges.d.ts.map
@@ -0,0 +1,76 @@
1
+ import { z } from 'zod';
2
+ import * as fsp from 'node:fs/promises';
3
+ import { RangeBytes } from '@milaboratories/pl-model-common';
4
+ import { createPathAtomically } from '@milaboratories/ts-helpers';
5
+ import { CorruptedRangesError } from './cache.js';
6
+
7
+ /** The content of the ranges file: ranges of bytes.
8
+ * The ranges should be normalized: sorted and no overlaps.
9
+ * For that, use `normalizeRanges` function. */
10
+ const Ranges = z.object({
11
+ ranges: z.array(RangeBytes),
12
+ });
13
+ const rangesFilePostfix = '.ranges.json';
14
+ function rangesFileName(fPath) {
15
+ return fPath + rangesFilePostfix;
16
+ }
17
+ async function readRangesFile(logger, path) {
18
+ let ranges = { ranges: [] };
19
+ try {
20
+ const file = await fsp.readFile(path, 'utf8');
21
+ ranges = Ranges.parse(JSON.parse(file));
22
+ }
23
+ catch (e) {
24
+ if (e instanceof SyntaxError || e instanceof z.ZodError) {
25
+ const msg = `readRangesFile: the file ${path} was corrupted: ${e}`;
26
+ logger.error(msg);
27
+ throw new CorruptedRangesError(msg);
28
+ }
29
+ if (!(e instanceof Error && 'code' in e && e.code === 'ENOENT')) {
30
+ throw e;
31
+ }
32
+ // If the file does not exist, assume the ranges are empty.
33
+ }
34
+ normalizeRanges(ranges);
35
+ return ranges;
36
+ }
37
+ /** Writes to a temporal file and then renames it atomically. */
38
+ async function writeRangesFile(logger, path, ranges) {
39
+ await createPathAtomically(logger, path, async (tempPath) => {
40
+ await fsp.writeFile(tempPath, JSON.stringify(ranges, null, 2), { flag: 'wx' });
41
+ });
42
+ }
43
+ /** Sorts and merges overlapping ranges. */
44
+ function normalizeRanges(s) {
45
+ s.ranges.sort((a, b) => a.from - b.from);
46
+ for (let i = 0; i < s.ranges.length - 1; i++) {
47
+ if (s.ranges[i].to >= s.ranges[i + 1].from) {
48
+ mergeRanges(s, i);
49
+ i--;
50
+ }
51
+ }
52
+ }
53
+ function mergeRanges(s, i) {
54
+ const from = Math.min(s.ranges[i].from, s.ranges[i + 1].from);
55
+ const to = Math.max(s.ranges[i].to, s.ranges[i + 1].to);
56
+ s.ranges.splice(i, 2, { from, to });
57
+ }
58
+ function rangesSize(s) {
59
+ return s.ranges.reduce((acc, range) => acc + range.to - range.from, 0);
60
+ }
61
+ function doesRangeExist(allRanges, range) {
62
+ for (const r of allRanges.ranges) {
63
+ if (r.from <= range.from && range.to <= r.to) {
64
+ return true;
65
+ }
66
+ }
67
+ return false;
68
+ }
69
+ function addRange(s, range) {
70
+ s.ranges.push(range);
71
+ normalizeRanges(s);
72
+ return s;
73
+ }
74
+
75
+ export { addRange, doesRangeExist, normalizeRanges, rangesFileName, rangesFilePostfix, rangesSize, readRangesFile, writeRangesFile };
76
+ //# sourceMappingURL=ranges.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ranges.js","sources":["../../../../src/drivers/download_blob/sparse_cache/ranges.ts"],"sourcesContent":["import { z } from \"zod\";\nimport * as fs from 'node:fs/promises';\nimport { RangeBytes } from \"@milaboratories/pl-model-common\";\nimport { createPathAtomically, MiLogger } from \"@milaboratories/ts-helpers\";\nimport { CorruptedRangesError } from \"./cache\";\n\n/** The content of the ranges file: ranges of bytes.\n * The ranges should be normalized: sorted and no overlaps.\n * For that, use `normalizeRanges` function. */\nconst Ranges = z.object({\n ranges: z.array(RangeBytes),\n});\n\nexport type Ranges = z.infer<typeof Ranges>;\n\nexport const rangesFilePostfix = '.ranges.json';\n\nexport function rangesFileName(fPath: string): string {\n return fPath + rangesFilePostfix;\n}\n\nexport async function readRangesFile(logger: MiLogger, path: string): Promise<Ranges> {\n let ranges: Ranges = { ranges: [] };\n try {\n const file = await fs.readFile(path, 'utf8');\n ranges = Ranges.parse(JSON.parse(file));\n } catch (e: unknown) {\n\n if (e instanceof SyntaxError || e instanceof z.ZodError) {\n const msg = `readRangesFile: the file ${path} was corrupted: ${e}`;\n logger.error(msg);\n throw new CorruptedRangesError(msg);\n }\n\n if (!(e instanceof Error && 'code' in e && e.code === 'ENOENT')) {\n throw e;\n }\n\n // If the file does not exist, assume the ranges are empty.\n }\n\n normalizeRanges(ranges);\n\n return ranges;\n}\n\n/** Writes to a temporal file and then renames it atomically. */\nexport async function writeRangesFile(logger: MiLogger, path: string, ranges: Ranges) {\n await createPathAtomically(logger, path, async (tempPath: string) => {\n await fs.writeFile(tempPath, JSON.stringify(ranges, null, 2), { flag: 'wx' });\n });\n}\n\n/** Sorts and merges overlapping ranges. */\nexport function normalizeRanges(s: Ranges) {\n s.ranges.sort((a, b) => a.from - b.from);\n\n for (let i = 0; i < s.ranges.length - 1; i++) {\n if (s.ranges[i].to >= s.ranges[i + 1].from) {\n mergeRanges(s, i);\n i--;\n }\n }\n}\n\nfunction mergeRanges(s: Ranges, i: number) {\n const from = Math.min(s.ranges[i].from, s.ranges[i + 1].from);\n const to = Math.max(s.ranges[i].to, s.ranges[i + 1].to);\n\n s.ranges.splice(i, 2, { from, to });\n}\n\nexport function rangesSize(s: Ranges) {\n return s.ranges.reduce((acc, range) => acc + range.to - range.from, 0);\n}\n\nexport function doesRangeExist(allRanges: Ranges, range: RangeBytes): boolean {\n for (const r of allRanges.ranges) {\n if (r.from <= range.from && range.to <= r.to) {\n return true;\n }\n }\n\n return false;\n}\n\nexport function addRange(s: Ranges, range: RangeBytes) {\n s.ranges.push(range);\n normalizeRanges(s);\n\n return s;\n}\n\n"],"names":["fs"],"mappings":";;;;;;AAMA;;AAE+C;AAC/C,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;AACtB,IAAA,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC;AAC5B,CAAA,CAAC;AAIK,MAAM,iBAAiB,GAAG;AAE3B,SAAU,cAAc,CAAC,KAAa,EAAA;IAC1C,OAAO,KAAK,GAAG,iBAAiB;AAClC;AAEO,eAAe,cAAc,CAAC,MAAgB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AC,IAAI,IAAI,KAAK,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,EAAE;AAC5C,YAAA,OAAO,IAAI;QACb;IACF;AAEA,IAAA,OAAO,KAAK;AACd;AAEM,SAAU,QAAQ,CAAC,CAAS,EAAE,KAAiB,EAAA;AACnD,IAAA,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;IACpB,eAAe,CAAC,CAAC,CAAC;AAElB,IAAA,OAAO,CAAC;AACV;;;;"}
@@ -0,0 +1,169 @@
1
+ 'use strict';
2
+
3
+ var computable = require('@milaboratories/computable');
4
+ var tsHelpers = require('@milaboratories/ts-helpers');
5
+ var node_crypto = require('node:crypto');
6
+ var path = require('node:path');
7
+ var files_cache = require('../helpers/files_cache.cjs');
8
+ var plClient = require('@milaboratories/pl-client');
9
+ var plModelCommon = require('@milaboratories/pl-model-common');
10
+ var snapshot = require('./snapshot.cjs');
11
+ var plTree = require('@milaboratories/pl-tree');
12
+ var task = require('./task.cjs');
13
+ var url = require('../urls/url.cjs');
14
+ var driver_id = require('./driver_id.cjs');
15
+ var download_blob_task = require('../download_blob/download_blob_task.cjs');
16
+
17
+ function _interopNamespaceDefault(e) {
18
+ var n = Object.create(null);
19
+ if (e) {
20
+ Object.keys(e).forEach(function (k) {
21
+ if (k !== 'default') {
22
+ var d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: function () { return e[k]; }
26
+ });
27
+ }
28
+ });
29
+ }
30
+ n.default = e;
31
+ return Object.freeze(n);
32
+ }
33
+
34
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
35
+
36
+ /** Downloads .tar, .tar.gz or zip archives,
37
+ * extracts them into saveDir and gets a url for it. */
38
+ class DownloadBlobToURLDriver {
39
+ logger;
40
+ signer;
41
+ clientDownload;
42
+ saveDir;
43
+ opts;
44
+ idToDownload = new Map();
45
+ downloadQueue;
46
+ /** Writes and removes files to a hard drive and holds a counter for every
47
+ * file that should be kept. */
48
+ cache;
49
+ constructor(logger, signer, clientDownload, saveDir, opts = {
50
+ cacheSoftSizeBytes: 50 * 1024 * 1024,
51
+ nConcurrentDownloads: 50,
52
+ }) {
53
+ this.logger = logger;
54
+ this.signer = signer;
55
+ this.clientDownload = clientDownload;
56
+ this.saveDir = saveDir;
57
+ this.opts = opts;
58
+ this.downloadQueue = new tsHelpers.TaskProcessor(this.logger, this.opts.nConcurrentDownloads, {
59
+ type: 'exponentialWithMaxDelayBackoff',
60
+ initialDelay: 10000,
61
+ maxDelay: 30000,
62
+ backoffMultiplier: 1.5,
63
+ jitter: 0.5,
64
+ });
65
+ this.cache = new files_cache.FilesCache(this.opts.cacheSoftSizeBytes);
66
+ }
67
+ info() {
68
+ return {
69
+ saveDir: this.saveDir,
70
+ opts: this.opts,
71
+ idToDownloadSize: this.idToDownload.size,
72
+ idToDownloadKeys: this.idToDownload.keys(),
73
+ idToDownload: Array.from(this.idToDownload.entries()).map(([id, task]) => [id, task.info()]),
74
+ };
75
+ }
76
+ /**
77
+ * @returns full path to the referenced file
78
+ */
79
+ getPathForCustomProtocol(url$1) {
80
+ if (plModelCommon.isFolderURL(url$1)) {
81
+ return url.getPathForFolderURL(this.signer, url$1, this.saveDir);
82
+ }
83
+ throw new Error(`getPathForCustomProtocol: ${url$1} is invalid`);
84
+ }
85
+ extractArchiveAndGetURL(res, format, ctx) {
86
+ // wrap result as computable, if we were not given an existing computable context
87
+ if (ctx === undefined)
88
+ return computable.Computable.make((c) => this.extractArchiveAndGetURL(res, format, c));
89
+ const rInfo = plTree.isPlTreeEntry(res)
90
+ ? snapshot.makeDownloadableBlobSnapshot(res, ctx)
91
+ : res;
92
+ const callerId = node_crypto.randomUUID();
93
+ ctx.addOnDestroy(() => this.releasePath(rInfo.id, format, callerId));
94
+ const result = this.extractArchiveAndGetURLNoCtx(rInfo, format, ctx.watcher, callerId);
95
+ if (result?.url === undefined)
96
+ ctx.markUnstable(`a path to the downloaded archive might be undefined. The current result: ${result}`);
97
+ if (result?.error !== undefined)
98
+ throw result?.error;
99
+ return result?.url;
100
+ }
101
+ extractArchiveAndGetURLNoCtx(rInfo, format, w, callerId) {
102
+ const task = this.idToDownload.get(driver_id.newId(rInfo.id, format));
103
+ if (task != undefined) {
104
+ task.attach(w, callerId);
105
+ return task.getURL();
106
+ }
107
+ const newTask = this.setNewTask(w, rInfo, format, callerId);
108
+ this.downloadQueue.push({
109
+ fn: async () => this.downloadUrl(newTask, callerId),
110
+ recoverableErrorPredicate: (e) => !download_blob_task.nonRecoverableError(e),
111
+ });
112
+ return newTask.getURL();
113
+ }
114
+ /** Downloads and extracts a tar archive if it wasn't downloaded yet. */
115
+ async downloadUrl(task, callerId) {
116
+ await task.download();
117
+ // Might be undefined if a error happened
118
+ if (task.getURL()?.url != undefined)
119
+ this.cache.addCache(task, callerId);
120
+ }
121
+ /** Removes a directory and aborts a downloading task when all callers
122
+ * are not interested in it. */
123
+ async releasePath(id, format, callerId) {
124
+ const task$1 = this.idToDownload.get(driver_id.newId(id, format));
125
+ if (task$1 == undefined)
126
+ return;
127
+ if (this.cache.existsFile(task$1.path)) {
128
+ const toDelete = this.cache.removeFile(task$1.path, callerId);
129
+ await Promise.all(toDelete.map(async (task$1) => {
130
+ await task.rmRFDir(task$1.path);
131
+ this.cache.removeCache(task$1);
132
+ this.removeTask(task$1, `the task ${plClient.stringifyWithResourceId(task$1.info())} was removed`
133
+ + `from cache along with ${plClient.stringifyWithResourceId(toDelete.map((t) => t.info()))}`);
134
+ }));
135
+ }
136
+ else {
137
+ // The task is still in a downloading queue.
138
+ const deleted = task$1.counter.dec(callerId);
139
+ if (deleted)
140
+ this.removeTask(task$1, `the task ${plClient.stringifyWithResourceId(task$1.info())} was removed from cache`);
141
+ }
142
+ }
143
+ /** Removes all files from a hard drive. */
144
+ async releaseAll() {
145
+ this.downloadQueue.stop();
146
+ await Promise.all(Array.from(this.idToDownload.entries()).map(async ([_, task$1]) => {
147
+ await task.rmRFDir(task$1.path);
148
+ this.cache.removeCache(task$1);
149
+ this.removeTask(task$1, `the task ${plClient.stringifyWithResourceId(task$1.info())} was released when the driver was closed`);
150
+ }));
151
+ }
152
+ setNewTask(w, rInfo, format, callerId) {
153
+ const result = new task.DownloadAndUnarchiveTask(this.logger, this.signer, this.saveDir, this.getFilePath(rInfo.id, format), rInfo, format, this.clientDownload);
154
+ result.attach(w, callerId);
155
+ this.idToDownload.set(driver_id.newId(rInfo.id, format), result);
156
+ return result;
157
+ }
158
+ removeTask(task, reason) {
159
+ task.abort(reason);
160
+ task.change.markChanged(`task for ${plClient.resourceIdToString(task.rInfo.id)} removed: ${reason}`);
161
+ this.idToDownload.delete(driver_id.newId(task.rInfo.id, task.format));
162
+ }
163
+ getFilePath(id, format) {
164
+ return path__namespace.join(this.saveDir, `${String(BigInt(id))}_${format}`);
165
+ }
166
+ }
167
+
168
+ exports.DownloadBlobToURLDriver = DownloadBlobToURLDriver;
169
+ //# sourceMappingURL=driver.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"driver.cjs","sources":["../../../src/drivers/download_blob_url/driver.ts"],"sourcesContent":["import type { ComputableCtx, Watcher } from '@milaboratories/computable';\nimport { Computable } from '@milaboratories/computable';\nimport type {\n MiLogger,\n Signer } from '@milaboratories/ts-helpers';\nimport {\n TaskProcessor,\n} from '@milaboratories/ts-helpers';\nimport { randomUUID } from 'node:crypto';\nimport * as path from 'node:path';\nimport { FilesCache } from '../helpers/files_cache';\nimport type { ResourceId } from '@milaboratories/pl-client';\nimport { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';\nimport { type ArchiveFormat, type BlobToURLDriver, type FolderURL, isFolderURL } from '@milaboratories/pl-model-common';\nimport type { DownloadableBlobSnapshot } from './snapshot';\nimport { makeDownloadableBlobSnapshot } from './snapshot';\nimport type { PlTreeEntry } from '@milaboratories/pl-tree';\nimport { isPlTreeEntry } from '@milaboratories/pl-tree';\nimport { DownloadAndUnarchiveTask, rmRFDir } from './task';\nimport type { ClientDownload } from '../../clients/download';\nimport { getPathForFolderURL } from '../urls/url';\nimport type { Id } from './driver_id';\nimport { newId } from './driver_id';\nimport { nonRecoverableError } from '../download_blob/download_blob_task';\n\nexport type DownloadBlobToURLDriverOps = {\n cacheSoftSizeBytes: number;\n nConcurrentDownloads: number;\n};\n\n/** Downloads .tar, .tar.gz or zip archives,\n * extracts them into saveDir and gets a url for it. */\nexport class DownloadBlobToURLDriver implements BlobToURLDriver {\n private idToDownload: Map<Id, DownloadAndUnarchiveTask> = new Map();\n private downloadQueue: TaskProcessor;\n\n /** Writes and removes files to a hard drive and holds a counter for every\n * file that should be kept. */\n private cache: FilesCache<DownloadAndUnarchiveTask>;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly signer: Signer,\n private readonly clientDownload: ClientDownload,\n private readonly saveDir: string,\n private readonly opts: DownloadBlobToURLDriverOps = {\n cacheSoftSizeBytes: 50 * 1024 * 1024,\n nConcurrentDownloads: 50,\n },\n ) {\n this.downloadQueue = new TaskProcessor(this.logger, this.opts.nConcurrentDownloads, {\n type: 'exponentialWithMaxDelayBackoff',\n initialDelay: 10000,\n maxDelay: 30000,\n backoffMultiplier: 1.5,\n jitter: 0.5,\n });\n this.cache = new FilesCache(this.opts.cacheSoftSizeBytes);\n }\n\n public info(): any {\n return {\n saveDir: this.saveDir,\n opts: this.opts,\n idToDownloadSize: this.idToDownload.size,\n idToDownloadKeys: this.idToDownload.keys(),\n idToDownload: Array.from(this.idToDownload.entries()).map(([id, task]) => [id, task.info()]),\n };\n }\n\n /**\n * @returns full path to the referenced file\n */\n getPathForCustomProtocol(url: FolderURL): string {\n if (isFolderURL(url)) {\n return getPathForFolderURL(this.signer, url, this.saveDir);\n }\n\n throw new Error(`getPathForCustomProtocol: ${url} is invalid`);\n }\n\n extractArchiveAndGetURL(\n res: DownloadableBlobSnapshot | PlTreeEntry,\n format: ArchiveFormat,\n ctx: ComputableCtx,\n ): FolderURL | undefined;\n\n extractArchiveAndGetURL(\n res: DownloadableBlobSnapshot | PlTreeEntry,\n format: ArchiveFormat,\n ): Computable<FolderURL | undefined>;\n\n extractArchiveAndGetURL(\n res: DownloadableBlobSnapshot | PlTreeEntry,\n format: ArchiveFormat,\n ctx?: ComputableCtx,\n ): Computable<FolderURL | undefined> | FolderURL | undefined {\n // wrap result as computable, if we were not given an existing computable context\n if (ctx === undefined)\n return Computable.make((c) => this.extractArchiveAndGetURL(res, format, c));\n\n const rInfo: DownloadableBlobSnapshot = isPlTreeEntry(res)\n ? makeDownloadableBlobSnapshot(res, ctx)\n : res;\n\n const callerId = randomUUID();\n\n ctx.addOnDestroy(() => this.releasePath(rInfo.id, format, callerId));\n\n const result = this.extractArchiveAndGetURLNoCtx(rInfo, format, ctx.watcher, callerId);\n if (result?.url === undefined)\n ctx.markUnstable(\n `a path to the downloaded archive might be undefined. The current result: ${result}`,\n );\n\n if (result?.error !== undefined)\n throw result?.error;\n\n return result?.url;\n }\n\n private extractArchiveAndGetURLNoCtx(\n rInfo: DownloadableBlobSnapshot,\n format: ArchiveFormat,\n w: Watcher,\n callerId: string,\n ) {\n const task = this.idToDownload.get(newId(rInfo.id, format));\n\n if (task != undefined) {\n task.attach(w, callerId);\n return task.getURL();\n }\n\n const newTask = this.setNewTask(w, rInfo, format, callerId);\n this.downloadQueue.push({\n fn: async () => this.downloadUrl(newTask, callerId),\n recoverableErrorPredicate: (e) => !nonRecoverableError(e),\n });\n\n return newTask.getURL();\n }\n\n /** Downloads and extracts a tar archive if it wasn't downloaded yet. */\n async downloadUrl(task: DownloadAndUnarchiveTask, callerId: string) {\n await task.download();\n // Might be undefined if a error happened\n if (task.getURL()?.url != undefined) this.cache.addCache(task, callerId);\n }\n\n /** Removes a directory and aborts a downloading task when all callers\n * are not interested in it. */\n async releasePath(id: ResourceId, format: ArchiveFormat, callerId: string): Promise<void> {\n const task = this.idToDownload.get(newId(id, format));\n if (task == undefined) return;\n\n if (this.cache.existsFile(task.path)) {\n const toDelete = this.cache.removeFile(task.path, callerId);\n\n await Promise.all(\n toDelete.map(async (task: DownloadAndUnarchiveTask) => {\n await rmRFDir(task.path);\n this.cache.removeCache(task);\n\n this.removeTask(\n task,\n `the task ${stringifyWithResourceId(task.info())} was removed`\n + `from cache along with ${stringifyWithResourceId(toDelete.map((t) => t.info()))}`,\n );\n }),\n );\n } else {\n // The task is still in a downloading queue.\n const deleted = task.counter.dec(callerId);\n if (deleted)\n this.removeTask(\n task,\n `the task ${stringifyWithResourceId(task.info())} was removed from cache`,\n );\n }\n }\n\n /** Removes all files from a hard drive. */\n async releaseAll() {\n this.downloadQueue.stop();\n\n await Promise.all(\n Array.from(this.idToDownload.entries()).map(async ([_, task]) => {\n await rmRFDir(task.path);\n this.cache.removeCache(task);\n\n this.removeTask(\n task,\n `the task ${stringifyWithResourceId(task.info())} was released when the driver was closed`,\n );\n }),\n );\n }\n\n private setNewTask(w: Watcher, rInfo: DownloadableBlobSnapshot, format: ArchiveFormat, callerId: string) {\n const result = new DownloadAndUnarchiveTask(\n this.logger,\n this.signer,\n this.saveDir,\n this.getFilePath(rInfo.id, format),\n rInfo,\n format,\n this.clientDownload,\n );\n result.attach(w, callerId);\n this.idToDownload.set(newId(rInfo.id, format), result);\n\n return result;\n }\n\n private removeTask(task: DownloadAndUnarchiveTask, reason: string) {\n task.abort(reason);\n task.change.markChanged(`task for ${resourceIdToString(task.rInfo.id)} removed: ${reason}`);\n this.idToDownload.delete(newId(task.rInfo.id, task.format));\n }\n\n private getFilePath(id: ResourceId, format: ArchiveFormat): string {\n return path.join(this.saveDir, `${String(BigInt(id))}_${format}`);\n }\n}\n"],"names":["TaskProcessor","FilesCache","url","isFolderURL","getPathForFolderURL","Computable","isPlTreeEntry","makeDownloadableBlobSnapshot","randomUUID","newId","nonRecoverableError","task","rmRFDir","stringifyWithResourceId","DownloadAndUnarchiveTask","resourceIdToString","path"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA;AACuD;MAC1C,uBAAuB,C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AAC,IAAI,CAAC,OAAO,EAAE,CAAA,EAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAA,CAAE,CAAC;IACnE;AACD;;;;"}
@@ -43,4 +43,3 @@ export declare class DownloadBlobToURLDriver implements BlobToURLDriver {
43
43
  private removeTask;
44
44
  private getFilePath;
45
45
  }
46
- //# sourceMappingURL=driver.d.ts.map