@preventive/triage 1.0.0-alpha.8 → 1.0.0-alpha.81

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 (442) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/README.md +16 -0
  3. package/api/reap.ts +16 -78
  4. package/cli.js +6 -0
  5. package/client/dependency-paths.d.ts +3 -0
  6. package/client/dependency-paths.js +36 -0
  7. package/client/finding-link.js +305 -0
  8. package/client/linked-findings.d.ts +1 -0
  9. package/client/linked-findings.js +115 -0
  10. package/common/bundle-commit.d.ts +1 -0
  11. package/common/bundle-commit.js +4 -0
  12. package/common/bundle-compare-diff.d.ts +6 -0
  13. package/common/bundle-compare-diff.js +377 -0
  14. package/common/bundle-compare-inputs.d.ts +3 -0
  15. package/common/bundle-compare-inputs.js +141 -0
  16. package/common/bundle-compare-versions.js +172 -0
  17. package/common/bundle-comparison.d.ts +8 -0
  18. package/common/bundle-comparison.js +69 -0
  19. package/common/bundle-imports.js +20 -0
  20. package/common/bundle-metadata.d.ts +15 -0
  21. package/common/bundle-metadata.js +226 -0
  22. package/common/bundle-pkg-of.d.ts +1 -0
  23. package/common/bundle-pkg-of.js +90 -0
  24. package/common/bundle-reasons.d.ts +6 -0
  25. package/common/bundle-reasons.js +58 -0
  26. package/common/bundle-source-links.d.ts +6 -0
  27. package/common/bundle-source-links.js +216 -0
  28. package/common/bundle-sourcemap.d.ts +10 -0
  29. package/common/bundle-sourcemap.js +100 -0
  30. package/common/bundle-sources.d.ts +10 -0
  31. package/common/bundle-sources.js +374 -0
  32. package/common/bundle-stats.js +41 -0
  33. package/common/bundle-tabs.js +1 -0
  34. package/common/code-language.js +66 -0
  35. package/common/default-scan-models.ts +36 -0
  36. package/common/dependency-reviews.d.ts +18 -0
  37. package/common/dependency-reviews.js +64 -0
  38. package/common/finding-duplicates.js +63 -0
  39. package/common/finding-filters.js +313 -0
  40. package/common/finding-id.js +47 -0
  41. package/common/finding-repo.d.ts +5 -0
  42. package/common/finding-repo.js +67 -0
  43. package/common/github-issue-labels.d.ts +1 -0
  44. package/common/github-issue-labels.js +11 -0
  45. package/common/github-pr.ts +56 -0
  46. package/common/gzip.js +59 -0
  47. package/common/line-diff.d.ts +14 -0
  48. package/common/line-diff.js +181 -0
  49. package/common/linked-workspace-groups.js +63 -0
  50. package/common/managed/agent-tasks.ts +292 -0
  51. package/common/managed/comments.ts +43 -0
  52. package/common/managed/dependency-routes.js +25 -0
  53. package/common/managed/directory-packages.ts +28 -0
  54. package/common/managed/finding-detail.d.ts +5 -0
  55. package/common/managed/finding-detail.js +75 -0
  56. package/common/managed/finding-values.d.ts +2 -0
  57. package/common/managed/finding-values.js +138 -0
  58. package/common/managed/github-agents.ts +114 -0
  59. package/common/managed/github-logins.ts +49 -0
  60. package/common/managed/lite-name.d.ts +2 -0
  61. package/common/managed/lite-name.js +20 -0
  62. package/common/managed/npm-packages.d.ts +9 -0
  63. package/common/managed/npm-packages.js +63 -0
  64. package/common/managed/permissions.ts +31 -0
  65. package/common/managed/report-content.ts +72 -0
  66. package/common/managed/report-filter.ts +120 -0
  67. package/common/managed/repository-alias.ts +65 -0
  68. package/common/managed/review-view-routes.js +23 -0
  69. package/common/managed/risk-routes.js +30 -0
  70. package/common/managed/roles.ts +41 -0
  71. package/common/managed/routes.d.ts +2 -0
  72. package/common/managed/routes.js +385 -0
  73. package/common/managed/scan-models.ts +6 -0
  74. package/common/managed/stasis-config.ts +7 -0
  75. package/common/managed/team-app.d.ts +19 -0
  76. package/common/managed/team-app.js +129 -0
  77. package/common/managed/team-filters.js +26 -0
  78. package/common/managed/triage.ts +83 -0
  79. package/common/managed/upload-seal.ts +126 -0
  80. package/common/pretty-json.js +98 -0
  81. package/common/pretty-kinds.js +20 -0
  82. package/common/pretty-print.d.ts +6 -0
  83. package/common/pretty-print.js +36 -0
  84. package/common/pretty-sourcemap.js +70 -0
  85. package/common/repo-commit.d.ts +4 -0
  86. package/common/repo-commit.js +30 -0
  87. package/common/revalidation-conflicts.js +48 -0
  88. package/common/revalidation-input-groups.js +56 -0
  89. package/common/save-error-reason.ts +20 -7
  90. package/common/scan-server.ts +13 -0
  91. package/common/security-links.d.ts +5 -0
  92. package/common/security-links.js +33 -0
  93. package/common/server-info.ts +46 -0
  94. package/common/sourcemap-listing.js +78 -0
  95. package/common/sourcemap-mappings.js +136 -0
  96. package/common/sourcemap-unlist.js +85 -0
  97. package/common/stasis-files.d.ts +5 -0
  98. package/common/stasis-files.js +20 -0
  99. package/common/stasis-tgz.d.ts +6 -0
  100. package/common/stasis-tgz.js +27 -0
  101. package/common/utf8.d.ts +3 -0
  102. package/common/utf8.js +45 -0
  103. package/common/whitespace-insensitive.js +19 -0
  104. package/common/workspace-app.js +91 -0
  105. package/common/workspace-groups.js +176 -0
  106. package/out/activity.js +2 -0
  107. package/out/brotli-fallback.js +3 -3
  108. package/out/chunk-2XUEZHIP.js +28 -0
  109. package/out/chunk-55YIWY3H.js +5 -0
  110. package/out/chunk-563V5VVT.js +266 -0
  111. package/out/chunk-5AVGKFMR.js +1 -0
  112. package/out/chunk-5EAVJAD2.js +3 -0
  113. package/out/chunk-5M2X3QPK.js +6 -0
  114. package/out/chunk-5QWPKTCE.js +40 -0
  115. package/out/chunk-5UJHJWFU.js +4 -0
  116. package/out/chunk-6CCSTJWB.js +1 -0
  117. package/out/chunk-6VOB5WXX.js +2 -0
  118. package/out/chunk-6WMQVUT2.js +3 -0
  119. package/out/chunk-6Y6PZ2BU.js +9 -0
  120. package/out/chunk-74JZTGTO.js +11 -0
  121. package/out/chunk-7BKKYVWR.js +7 -0
  122. package/out/chunk-7C7DWL7I.js +1 -0
  123. package/out/chunk-7HP2VYIC.js +2 -0
  124. package/out/chunk-7YFJUT2P.js +1 -0
  125. package/out/chunk-7YO4QV5P.js +1 -0
  126. package/out/chunk-A4BBBHQU.js +1 -0
  127. package/out/chunk-B64CTMVU.js +4 -0
  128. package/out/chunk-BEKYZCPX.js +14 -0
  129. package/out/chunk-C4Z6ZBXV.js +10 -0
  130. package/out/chunk-CU3FPB3P.js +1 -0
  131. package/out/chunk-DEVAORP2.js +1 -0
  132. package/out/chunk-DOYTKRIR.js +53 -0
  133. package/out/chunk-DUASGS2O.js +8 -0
  134. package/out/chunk-EEJA7JBI.js +13 -0
  135. package/out/chunk-FSU2PLJL.js +1 -0
  136. package/out/chunk-GO7FT4OD.js +1 -0
  137. package/out/chunk-GX5UXWT4.js +1 -0
  138. package/out/chunk-H4FEC57K.js +1 -0
  139. package/out/chunk-ITUKKCCK.js +10 -0
  140. package/out/chunk-IYPY47MC.js +1 -0
  141. package/out/chunk-K4XJHM2T.js +1 -0
  142. package/out/chunk-LBD3M3J6.js +1 -0
  143. package/out/chunk-LVSLM7HB.js +1 -0
  144. package/out/chunk-LVW7WWV4.js +1 -0
  145. package/out/chunk-N57OWFKY.js +15 -0
  146. package/out/chunk-NRPC74AM.js +1 -0
  147. package/out/chunk-OAWQNDAO.js +3 -0
  148. package/out/chunk-OOJM4CTU.js +1 -0
  149. package/out/chunk-PEE47ANR.js +1 -0
  150. package/out/chunk-PKLVXUEI.js +11 -0
  151. package/out/chunk-PLGH63UC.js +2 -0
  152. package/out/chunk-PNHBZGDD.js +1 -0
  153. package/out/chunk-PTFYDJUP.js +1 -0
  154. package/out/chunk-PWFUOLZK.js +17 -0
  155. package/out/chunk-PXK7EU4V.js +1 -0
  156. package/out/chunk-QD3IBSF4.js +7 -0
  157. package/out/chunk-QI66YA4F.js +1 -0
  158. package/out/chunk-QRKFPLA4.js +80 -0
  159. package/out/chunk-R3NZFXVR.js +1 -0
  160. package/out/chunk-R5O6FAW4.js +2 -0
  161. package/out/chunk-RPNO56H2.js +1 -0
  162. package/out/chunk-SSPDMA6D.js +1 -0
  163. package/out/chunk-SZWPYLPC.js +1 -0
  164. package/out/chunk-TUII34K2.js +1 -0
  165. package/out/chunk-UJP4PQJA.js +1 -0
  166. package/out/chunk-UJSJMXEG.js +1 -0
  167. package/out/chunk-UR4WYMIP.js +1 -0
  168. package/out/chunk-VHEXSGGP.js +1 -0
  169. package/out/chunk-VYYUMYVW.js +9 -0
  170. package/out/chunk-WCDYSIKG.js +1 -0
  171. package/out/chunk-WFBVS4QI.js +1 -0
  172. package/out/chunk-XM4RLC4A.js +2 -0
  173. package/out/chunk-ZQHLECTI.js +1 -0
  174. package/out/client-managed-import.js +4 -0
  175. package/out/client-managed.js +37 -0
  176. package/out/client-sync.js +11 -17
  177. package/out/compare.js +26 -0
  178. package/out/dialogs.js +40 -0
  179. package/out/github-issue-authorized.html +3 -0
  180. package/out/github-issue-authorized.js +1 -0
  181. package/out/graph.js +19 -4
  182. package/out/index.html +104 -24
  183. package/out/prism.js +14 -10
  184. package/out/scan.js +2 -0
  185. package/out/stasis.svg +20 -0
  186. package/out/terminal.js +367 -39
  187. package/out/view.css +1 -1
  188. package/out/view.js +19 -147
  189. package/package.json +409 -64
  190. package/risk/actions/findings.js +161 -0
  191. package/risk/actions/resolve.js +287 -0
  192. package/risk/actions/scripts/commands.js +182 -0
  193. package/risk/actions/scripts/grade.js +113 -0
  194. package/risk/actions/scripts/packages.js +123 -0
  195. package/risk/actions/scripts/shell.js +169 -0
  196. package/risk/actions/uses.js +94 -0
  197. package/risk/actions/workflow.js +183 -0
  198. package/risk/files/characters.js +354 -0
  199. package/risk/files/hosts.js +206 -0
  200. package/risk/files/readability.js +185 -0
  201. package/risk/files/runs-code.js +568 -0
  202. package/risk/files/script.js +410 -0
  203. package/risk/files/source-map-link.js +43 -0
  204. package/risk/files/stamps.js +47 -0
  205. package/risk/index.d.ts +294 -0
  206. package/risk/index.js +12 -0
  207. package/risk/npm/assess.js +285 -0
  208. package/risk/npm/combined.js +68 -0
  209. package/risk/npm/compare.js +236 -0
  210. package/risk/npm/dependencies.js +172 -0
  211. package/risk/npm/diff.js +79 -0
  212. package/risk/npm/downloads.js +84 -0
  213. package/risk/npm/files.js +445 -0
  214. package/risk/npm/held.js +67 -0
  215. package/risk/npm/install-scripts.js +34 -0
  216. package/risk/npm/malware.js +38 -0
  217. package/risk/npm/manifest.js +65 -0
  218. package/risk/npm/new-dependency.js +60 -0
  219. package/risk/npm/old-version.js +108 -0
  220. package/risk/npm/registry.js +393 -0
  221. package/risk/npm/release.js +85 -0
  222. package/risk/npm/score.js +59 -0
  223. package/risk/npm/specs.js +124 -0
  224. package/risk/npm/tarball.js +169 -0
  225. package/server-common/database-config.ts +16 -0
  226. package/server-common/initialize.ts +18 -0
  227. package/server-common/npm-advisories.ts +101 -0
  228. package/{server → server-common}/origin.ts +5 -5
  229. package/server-common/reap.ts +48 -0
  230. package/server-common/scan-config.ts +19 -0
  231. package/server-common/standalone.ts +29 -0
  232. package/server-common/storage-crypto.ts +182 -0
  233. package/server-common/storage-log.ts +38 -0
  234. package/server-common/vercel-blob.ts +103 -0
  235. package/server-e2e/app.ts +487 -0
  236. package/{server → server-e2e}/bus-receiver.ts +2 -1
  237. package/{server → server-e2e}/cli.js +9 -4
  238. package/{server → server-e2e}/config.ts +50 -42
  239. package/{server → server-e2e}/db-neon.ts +2 -2
  240. package/{server → server-e2e}/db-revision-sql.ts +1 -1
  241. package/{server → server-e2e}/db-stmt.ts +2 -2
  242. package/{server → server-e2e}/db.ts +7 -7
  243. package/{server → server-e2e}/http.ts +44 -13
  244. package/{server → server-e2e}/hub.ts +40 -9
  245. package/server-e2e/index.ts +17 -0
  246. package/server-e2e/lifecycle.ts +95 -0
  247. package/{server → server-e2e}/neon-driver.ts +2 -2
  248. package/{server → server-e2e}/npm-proxy.ts +12 -145
  249. package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
  250. package/{server → server-e2e}/objstore/blob.ts +1 -1
  251. package/{server → server-e2e}/objstore/fetch-mint-guard.ts +2 -2
  252. package/{server → server-e2e}/objstore/handlers.ts +9 -8
  253. package/{server → server-e2e}/objstore/init.ts +19 -27
  254. package/{server → server-e2e}/objstore/reaper.ts +1 -1
  255. package/{server → server-e2e}/objstore/rest-deny.ts +1 -1
  256. package/{server → server-e2e}/objstore/rest-mint.ts +3 -3
  257. package/{server → server-e2e}/objstore/rest.ts +3 -3
  258. package/{server → server-e2e}/objstore/store-neon.ts +6 -10
  259. package/{server → server-e2e}/objstore/store.ts +7 -7
  260. package/{server → server-e2e}/objstore/tokens.ts +2 -2
  261. package/{server → server-e2e}/pubsub.ts +12 -9
  262. package/{server → server-e2e}/sign.ts +4 -4
  263. package/{server → server-e2e}/sse-server.ts +60 -33
  264. package/{server → server-e2e}/static.ts +18 -17
  265. package/{server → server-e2e}/sync-handlers.ts +64 -30
  266. package/{server → server-e2e}/ws-server.ts +20 -9
  267. package/server-managed/activity.ts +269 -0
  268. package/server-managed/agent-merges.ts +109 -0
  269. package/server-managed/app-agents.ts +23 -0
  270. package/server-managed/avatar-store.ts +13 -0
  271. package/server-managed/blob-store.ts +26 -0
  272. package/server-managed/brotli.ts +10 -0
  273. package/server-managed/bundle-advisories.ts +79 -0
  274. package/server-managed/bundle-advisory-inventory.ts +85 -0
  275. package/server-managed/bundle-build-diagnostics.d.ts +10 -0
  276. package/server-managed/bundle-build-diagnostics.js +23 -0
  277. package/server-managed/bundle-build-leases.ts +41 -0
  278. package/server-managed/bundle-build-worker.js +380 -0
  279. package/server-managed/bundle-build.ts +326 -0
  280. package/server-managed/bundle-cache.ts +261 -0
  281. package/server-managed/bundle-catalog.ts +29 -0
  282. package/server-managed/bundle-commits.ts +133 -0
  283. package/server-managed/bundle-dependency-reviews.ts +91 -0
  284. package/server-managed/bundle-store.ts +24 -0
  285. package/server-managed/bundle-summary-cache.ts +139 -0
  286. package/server-managed/bundle-validation.ts +33 -0
  287. package/server-managed/bundle.ts +151 -0
  288. package/server-managed/cache-storage.ts +20 -0
  289. package/server-managed/cli.js +21 -0
  290. package/server-managed/combined.ts +43 -0
  291. package/server-managed/comments.ts +138 -0
  292. package/server-managed/commit-coauthors.ts +105 -0
  293. package/server-managed/commit-lockfiles.ts +78 -0
  294. package/server-managed/comparison.ts +51 -0
  295. package/server-managed/config.ts +163 -0
  296. package/server-managed/content-access.ts +15 -0
  297. package/server-managed/crypto.ts +25 -0
  298. package/server-managed/db-methods.ts +1915 -0
  299. package/server-managed/db-neon.ts +398 -0
  300. package/server-managed/db-schema.ts +263 -0
  301. package/server-managed/db-table-names.ts +22 -0
  302. package/server-managed/db.ts +149 -0
  303. package/server-managed/dependabot-access.ts +61 -0
  304. package/server-managed/dependabot-alerts.ts +180 -0
  305. package/server-managed/dependency-review-comments.ts +158 -0
  306. package/server-managed/dependency-reviews.ts +369 -0
  307. package/server-managed/github-accounts.ts +59 -0
  308. package/server-managed/github-actions.ts +71 -0
  309. package/server-managed/github-app.ts +347 -0
  310. package/server-managed/github-errors.ts +92 -0
  311. package/server-managed/github-graphql.ts +42 -0
  312. package/server-managed/github-issue-links.ts +264 -0
  313. package/server-managed/github-issue-oauth.ts +30 -0
  314. package/server-managed/github-issues.ts +157 -0
  315. package/server-managed/github-link-oauth.ts +56 -0
  316. package/server-managed/github-links.ts +204 -0
  317. package/server-managed/github-metadata.ts +213 -0
  318. package/server-managed/github-oauth.ts +264 -0
  319. package/server-managed/github-pulls.ts +257 -0
  320. package/server-managed/github-requests.ts +181 -0
  321. package/server-managed/github-users.ts +30 -0
  322. package/server-managed/github-values.ts +26 -0
  323. package/server-managed/history-windows.ts +172 -0
  324. package/server-managed/http-response.ts +76 -0
  325. package/server-managed/http.ts +6327 -0
  326. package/server-managed/import-triage.ts +51 -0
  327. package/server-managed/index.ts +204 -0
  328. package/server-managed/kept-briefly.ts +37 -0
  329. package/server-managed/kept.ts +28 -0
  330. package/server-managed/link-reports.ts +149 -0
  331. package/server-managed/listed-activity.ts +39 -0
  332. package/server-managed/locked-packages.ts +234 -0
  333. package/server-managed/lockfile-scope.ts +58 -0
  334. package/server-managed/lru.ts +30 -0
  335. package/server-managed/managed-issues.ts +92 -0
  336. package/server-managed/management-catalog.ts +45 -0
  337. package/server-managed/management.ts +116 -0
  338. package/server-managed/npm-delta-worker.js +142 -0
  339. package/server-managed/npm-delta.ts +277 -0
  340. package/server-managed/npm-insights.ts +455 -0
  341. package/server-managed/npm-kept.ts +265 -0
  342. package/server-managed/npm-loads.ts +259 -0
  343. package/server-managed/npm-manifest.ts +117 -0
  344. package/server-managed/npm-packages.ts +469 -0
  345. package/server-managed/npm-patch-paths.ts +86 -0
  346. package/server-managed/npm-patch.ts +455 -0
  347. package/server-managed/npm-provenance.ts +127 -0
  348. package/server-managed/npm-risk.ts +287 -0
  349. package/server-managed/object-storage-disk.ts +147 -0
  350. package/server-managed/object-storage-vercel.ts +71 -0
  351. package/server-managed/object-storage.ts +82 -0
  352. package/server-managed/package-entry-points.ts +127 -0
  353. package/server-managed/pretty-copies.ts +22 -0
  354. package/server-managed/pretty-format.d.ts +3 -0
  355. package/server-managed/pretty-format.js +13 -0
  356. package/server-managed/pretty-print.ts +442 -0
  357. package/server-managed/public-workspace.ts +243 -0
  358. package/server-managed/pull-comment-reviews.ts +161 -0
  359. package/server-managed/pull-dependencies.ts +370 -0
  360. package/server-managed/pull-dependency-reviews.ts +407 -0
  361. package/server-managed/pull-reviews.ts +202 -0
  362. package/server-managed/queue.ts +54 -0
  363. package/server-managed/release-commits.ts +85 -0
  364. package/server-managed/rename-diff-worker.js +30 -0
  365. package/server-managed/rename-diff.ts +237 -0
  366. package/server-managed/repo-path.ts +21 -0
  367. package/server-managed/report-migration.ts +32 -0
  368. package/server-managed/report-opening.ts +77 -0
  369. package/server-managed/report-query.ts +10 -0
  370. package/server-managed/report-response.ts +16 -0
  371. package/server-managed/report-sources.ts +185 -0
  372. package/server-managed/report-store.ts +77 -0
  373. package/server-managed/repository-actions.ts +220 -0
  374. package/server-managed/repository-agents.ts +422 -0
  375. package/server-managed/repository-aliases.ts +119 -0
  376. package/server-managed/repository-browser.ts +361 -0
  377. package/server-managed/repository-changes.ts +280 -0
  378. package/server-managed/repository-code.ts +222 -0
  379. package/server-managed/repository-dependency-reviews.ts +182 -0
  380. package/server-managed/repository-discovery.ts +83 -0
  381. package/server-managed/repository-history.ts +428 -0
  382. package/server-managed/repository-issues.ts +293 -0
  383. package/server-managed/repository-policy.ts +27 -0
  384. package/server-managed/repository-pulls.ts +207 -0
  385. package/server-managed/repository-pulse.ts +477 -0
  386. package/server-managed/repository-risk-assess.ts +108 -0
  387. package/server-managed/repository-risk-audit.ts +78 -0
  388. package/server-managed/repository-risk-caches.ts +37 -0
  389. package/server-managed/repository-risk-read.ts +260 -0
  390. package/server-managed/repository-risk-stasis.ts +61 -0
  391. package/server-managed/repository-risk-turn.ts +97 -0
  392. package/server-managed/repository-risk.ts +516 -0
  393. package/server-managed/repository-squashes.ts +160 -0
  394. package/server-managed/repository-stamp.ts +41 -0
  395. package/server-managed/request-timing.ts +63 -0
  396. package/server-managed/reviewed-dependencies.ts +170 -0
  397. package/server-managed/revisions.ts +35 -0
  398. package/server-managed/scan-downloads.ts +121 -0
  399. package/server-managed/session.ts +132 -0
  400. package/server-managed/slugs.ts +38 -0
  401. package/server-managed/solidity-entry-points.ts +71 -0
  402. package/server-managed/sql-postgres.ts +34 -0
  403. package/server-managed/sql.ts +107 -0
  404. package/server-managed/stasis-config.ts +147 -0
  405. package/server-managed/stasis-dependencies.ts +221 -0
  406. package/server-managed/static.ts +28 -0
  407. package/server-managed/storage-command.ts +16 -0
  408. package/server-managed/storage-db.ts +183 -0
  409. package/server-managed/storage-encryption.ts +288 -0
  410. package/server-managed/storage-maintenance.ts +259 -0
  411. package/server-managed/storage-payload.ts +121 -0
  412. package/server-managed/storage-stores.ts +91 -0
  413. package/server-managed/storage.ts +82 -0
  414. package/server-managed/team-app-summaries.ts +56 -0
  415. package/server-managed/team-app.ts +156 -0
  416. package/server-managed/team-catalog.ts +7 -0
  417. package/server-managed/team-feed.ts +180 -0
  418. package/server-managed/team-npm-scopes.ts +103 -0
  419. package/server-managed/team-reports.ts +368 -0
  420. package/server-managed/triage-response.ts +16 -0
  421. package/server-managed/type-only-proof.js +161 -0
  422. package/server-managed/type-only-worker.js +31 -0
  423. package/server-managed/type-only.ts +135 -0
  424. package/server-managed/uploads.ts +106 -0
  425. package/server-managed/upstream-cache.ts +256 -0
  426. package/server-managed/view-as.ts +58 -0
  427. package/server-managed/worker-turns.ts +76 -0
  428. package/server-managed/workspace-shares.ts +170 -0
  429. package/server.ts +70 -0
  430. package/strip-types-loader.js +13 -10
  431. package/server/index.ts +0 -480
  432. package/server/lifecycle.ts +0 -208
  433. /package/{LICENSE → LICENSE-MIT} +0 -0
  434. /package/{server → server-e2e}/auth.ts +0 -0
  435. /package/{server → server-e2e}/config.example.json +0 -0
  436. /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
  437. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  438. /package/{server → server-e2e}/objstore/sign.ts +0 -0
  439. /package/{server → server-e2e}/peer.ts +0 -0
  440. /package/{server → server-e2e}/sse-session.ts +0 -0
  441. /package/{server → server-e2e}/util.ts +0 -0
  442. /package/{server → server-e2e}/validation.ts +0 -0
@@ -0,0 +1,142 @@
1
+ // The lines a package's files change (npm-delta.ts), off the event loop, paired as Compare pairs them (rename-diff.ts
2
+ // comparedFiles); and each file's fewest (`best`): the least of the diffs Compare draws of it (drawn), one per setting of
3
+ // its toggles the comparison has (`proves` types, `prints` code), those needing proofs or copies smallest first.
4
+ import { parentPort, workerData } from 'node:worker_threads'
5
+ import { treeLayout } from '@preventive/compare'
6
+ import { renamedDiff } from '@preventive/compare/names.js'
7
+ import { hidesMixedWhitespace } from '@preventive/compare/view.js'
8
+ import { computeBundleDiff } from '../common/bundle-compare-diff.js'
9
+ import { bundlePkgOf } from '../common/bundle-pkg-of.js'
10
+ import { blocksHold, changeBlocks, diffSize, isLargeDiff, markedCounts, textLines, typeFolds, withSharedBlankLines } from '../common/line-diff.js'
11
+ import { viewerKind, viewerPrintable } from '../common/pretty-kinds.js'
12
+ import { prettyExtension, printableSize } from '../common/pretty-print.js'
13
+ import { whitespaceInsensitive } from '../common/whitespace-insensitive.js'
14
+ import { npmFileReadability } from '../risk/files/readability.js'
15
+ import { prettyFormat } from './pretty-format.js'
16
+ import { typeOnlyRuns, typeOnlyStamped } from './type-only-proof.js'
17
+
18
+ const { base, copies, other, prettyUntil, prints, proves } = workerData
19
+ const kept = new Map(copies.map(([at, path, copy]) => [`${at}\0${path}`, copy])), made = []
20
+ const NONE = { additions: 0, deletions: 0 }
21
+ const least = (a, b) => (b.additions < a.additions || (b.additions === a.additions && b.deletions < a.deletions) ? b : a)
22
+ // Node worker messages have no browser target origin.
23
+ // oxlint-disable-next-line unicorn/require-post-message-target-origin
24
+ const tell = told => parentPort.postMessage(told)
25
+ const TYPESCRIPT = new Set(['ts', 'mts', 'cts'])
26
+ const RENAMABLE = /\.[cm]?js$/iu
27
+ const minified = (path, text) => text !== '' && npmFileReadability(path, text).category === 'minified'
28
+ const once = (file, key, make) => {
29
+ if (!file.memo.has(key)) file.memo.set(key, make())
30
+ return file.memo.get(key)
31
+ }
32
+
33
+ // As _fileState's diffed: indentation set aside where `ws`, but not where that hides blanks of more than one kind
34
+ // changing; the server's `blocks` where given, those of `marks` counted apart.
35
+ function diffed(file, key, before, after, ws, { blocks = null, marks = null } = {}) {
36
+ return once(file, `${key}\0${ws}`, () => {
37
+ let found = ws || !blocks ? changeBlocks(before, after, ws && 'leading') : blocks
38
+ if (ws && hidesMixedWhitespace(before, after, found)) found = blocks ?? changeBlocks(before, after)
39
+ return { blocks: found, ...markedCounts(found, marks) }
40
+ })
41
+ }
42
+
43
+ // A side's file as Compare's toggle prints it in `layout`: as the viewer prints its kind (pretty-kinds.js), else
44
+ // as the server copies code where the comparison has copies; null where it can't be.
45
+ async function copyOf(at, path, text, layout) {
46
+ if (text === '') return { text, blank: [] }
47
+ const kind = viewerKind(path)
48
+ try {
49
+ if (kind) return viewerPrintable(kind, text) ? { text: kind.copy(text, layout), blank: [] } : null
50
+ if (!prints || prettyExtension(path) === null || !printableSize(text)) return null
51
+ const known = kept.get(`${at}\0${path}`)
52
+ const copy = known ?? await prettyFormat(text, prettyExtension(path))
53
+ if ('error' in copy) return null
54
+ if (!known) made.push([at, path, copy])
55
+ return copy
56
+ } catch {
57
+ return null
58
+ }
59
+ }
60
+
61
+ // As _printing: a file's sides printed and drawn (_drawn), where Compare's toggle applies to it (a kind the viewer
62
+ // prints always, else either side minified, or its copies' diff smaller as drawn: _smaller); null where it doesn't.
63
+ async function printingOf(file, layout, ws) {
64
+ const sides = [[0, file.basePath, file.before], [1, file.path, file.after]]
65
+ const kinds = sides.filter(([, , text]) => text !== '').map(([, path]) => viewerKind(path))
66
+ const viewed = kinds.every(kind => kind && kind === kinds[0])
67
+ const unreadable = !viewed && sides.some(([, path, text]) => minified(path, text))
68
+ if (!viewed && !unreadable && (file.before === '' || file.after === '')) return null
69
+ const copy = ([at, path, text]) => once(file, `copy\0${layout}\0${at}`, () => copyOf(at, path, text, layout))
70
+ const a = await copy(sides[0]), b = a && await copy(sides[1])
71
+ if (!a || !b) return null
72
+ const shown = file.generated ? { before: a.text, after: b.text } : withSharedBlankLines(a.text, b.text, a.blank, b.blank)
73
+ if (viewed || unreadable) return { ...shown, kind: viewed && kinds[0], copies: [a.text, b.text] }
74
+ if (isLargeDiff(file.before, file.after) || isLargeDiff(shown.before, shown.after)) return null
75
+ const mode = ws && 'leading', now = diffSize(shown.before, shown.after, mode), was = diffSize(file.before, file.after, mode)
76
+ return now.lines < was.lines || (now.lines === was.lines && now.marks < was.marks) ? shown : null
77
+ }
78
+
79
+ // The diff Compare draws of a file with its toggles so (_fileState): whitespace hidden (`ws`), type-only changes
80
+ // (`types`), renamed names (`renames`), printed in `layout` or not (null); null where it draws none.
81
+ async function drawn(file, { ws, types, layout, renames }) {
82
+ const proof = types ? (file.stamped || once(file, 'runs', () => typeOnlyRuns(file.from, file.to))) : null
83
+ if (proof === true) return NONE
84
+ const folds = proof && once(file, 'folds', () => typeFolds(file.before, file.after, proof))
85
+ if (folds) return diffed(file, 'folds', folds.before, folds.after, ws)
86
+ const printing = layout && await once(file, `printing\0${layout}\0${ws}`, () => printingOf(file, layout, ws))
87
+ if (!printing) return diffed(file, 'texts', file.before, file.after, ws)
88
+ if (file.generated) {
89
+ const diff = await once(file, `renamed\0${layout}`, () => renamedDiff(printing.before, printing.after,
90
+ { extension: prettyExtension(file.basePath), otherExtension: prettyExtension(file.path) }).catch(() => null))
91
+ if (renames) return diff && blocksHold(diff.before, printing.after, diff.blocks) ? diffed(file, `hidden\0${layout}`, diff.before, printing.after, ws, { blocks: diff.blocks }) : null
92
+ if (diff) return diffed(file, `marked\0${layout}`, printing.before, printing.after, ws, { marks: diff.marks })
93
+ }
94
+ const found = diffed(file, `printed\0${layout}`, printing.before, printing.after, ws)
95
+ // Drawn as published where its kind refuses the diff of its copies (comparedListingRefusal).
96
+ const refused = printing.kind?.refusal?.({ a: textLines(printing.before), b: textLines(printing.after), blocks: found.blocks }, [file.before, file.after], printing.copies)
97
+ return refused == null ? found : diffed(file, 'texts', file.before, file.after, ws)
98
+ }
99
+
100
+ // Each setting of Compare's toggles that may draw a file otherwise.
101
+ const SETTINGS = [false, true].flatMap(ws => [false, true].flatMap(types => [null, 'indented', 'folded'].flatMap(layout => [false, true].map(renames => ({ ws, types, layout, renames })))))
102
+ const settingsOf = file => SETTINGS.filter(({ ws, types, layout, renames }) => (!ws || file.insensitive) && (!types || file.ts)
103
+ && (layout !== 'folded' || file.layouts) && (!renames || (layout && file.generated)))
104
+
105
+ const [olds, news] = [new Map(base), new Map(other)]
106
+ const { files } = computeBundleDiff(olds, news, path => bundlePkgOf(path), treeLayout(olds.keys(), news.keys()))
107
+ const best = { ...NONE }, later = [], total = { ...NONE }
108
+ for (const { path, basePath = path, side } of [...files.changed, ...files.onlyBase.map(row => ({ ...row, side: 0 })), ...files.onlyOther.map(row => ({ ...row, side: 1 }))]) {
109
+ const after = side === 0 ? '' : news.get(path), before = side === 1 ? '' : olds.get(basePath)
110
+ if (typeof before !== 'string' || typeof after !== 'string' || before === after) continue
111
+ const from = side === 1 ? null : { path: basePath, text: before }, to = side === 0 ? null : { path, text: after }
112
+ const ts = proves && [from, to].every(file => !file || TYPESCRIPT.has(prettyExtension(file.path)))
113
+ const file = { path, basePath, before, after, from, to, ts, stamped: ts && typeOnlyStamped(from, to), memo: new Map(),
114
+ insensitive: whitespaceInsensitive(path, after) && whitespaceInsensitive(basePath, before), layouts: [path, basePath].some(name => viewerKind(name)?.layouts),
115
+ generated: prints && RENAMABLE.test(path) && RENAMABLE.test(basePath) && minified(basePath, before) && minified(path, after) }
116
+ const published = diffed(file, 'texts', before, after, false)
117
+ // Additions less deletions is the same for any diff of two texts, so hiding whitespace can't lower one adding or removing none.
118
+ const fewest = file.stamped ? NONE : file.insensitive && published.additions && published.deletions ? least(published, diffed(file, 'texts', before, after, true)) : published
119
+ for (const [sum, count] of [[total, published], [best, fewest]]) {
120
+ sum.additions += count.additions
121
+ sum.deletions += count.deletions
122
+ }
123
+ const printable = [basePath, path].some(name => viewerKind(name) || (prints && prettyExtension(name) !== null))
124
+ if (fewest !== NONE && (ts || printable)) later.push({ ...file, fewest, size: before.length + after.length })
125
+ }
126
+ const count = complete => ({ ...total, best, complete })
127
+ tell({ sofar: count(false) })
128
+
129
+ let complete = true
130
+ for (const file of later.toSorted((a, b) => a.size - b.size)) {
131
+ if (Date.now() >= prettyUntil) { complete = false; break }
132
+ let now = file.fewest
133
+ for (const setting of settingsOf(file)) {
134
+ const counts = await drawn(file, setting)
135
+ if (counts) now = least(now, counts)
136
+ }
137
+ if (now === file.fewest) continue
138
+ best.additions += now.additions - file.fewest.additions
139
+ best.deletions += now.deletions - file.fewest.deletions
140
+ tell({ sofar: count(false) })
141
+ }
142
+ tell({ ...count(complete), made })
@@ -0,0 +1,277 @@
1
+ // The lines a version of a package changes against another, or against none,
2
+ // its whole package's (GET /api/npm/delta), as a list of versions wanting
3
+ // reviews tells them beside each: added and removed (`additions`,
4
+ // `deletions`), as git's --numstat counts them, of their text files, a
5
+ // renamed one diffed with what it was; each file's fewest as Compare can
6
+ // draw it (`best`); and whether a binary file, which they count none of,
7
+ // was added, removed or changed (`binary`), one moved as it was not. Worked
8
+ // out in a worker, off the event loop, as many at once per process as
9
+ // MAX_ACTIVE, both sides' files read together only once their turn has come,
10
+ // those waiting past MAX_WAITING refused, each waiting a minute at most and
11
+ // counting in a minute at most; one asked again meanwhile sharing the
12
+ // first's; and kept by both sides' integrities, in this instance and beyond
13
+ // it (`kept`), what a version holds never changing, but for one whose copies
14
+ // weren't all printed in time (`partial`). Its pretty-printed copies are
15
+ // Compare's, read and kept as Compare keeps them (`copies`). Access is the
16
+ // caller's to check.
17
+ import { createHash } from 'node:crypto'
18
+ import { isExactVersion } from '@preventive/upstream/semver.js'
19
+ import { layoutBlankList, prettyExtension } from '../common/pretty-print.js'
20
+ import { isPatchHash, reviewBase } from '../common/managed/npm-packages.js'
21
+ import { type KeptStore, VERSION_DELTA_KEPT_MS, npmDeltaStore, npmVersionDeltaStore, npmVersionStore, privateVersionDeltaStore } from './npm-kept.ts'
22
+ import { type Lru, lru } from './lru.ts'
23
+ import { plainObject } from './npm-manifest.ts'
24
+ import { NpmPackageError, type NpmPackageFile, VERSION_KEPT_MS, npmFileText } from './npm-packages.ts'
25
+ import { PRETTY_VERSION, type PrettyCopy } from './pretty-format.js'
26
+ import type { KeptCopies } from './pretty-copies.ts'
27
+ import { eachOf } from './queue.ts'
28
+ import type { UpstreamCacheStore } from './upstream-cache.ts'
29
+ import { workerTurns } from './worker-turns.ts'
30
+
31
+ export interface NpmDelta { additions: number; deletions: number; best: { additions: number; deletions: number }; binary: boolean; partial?: true }
32
+
33
+ const MAX_ACTIVE = 2
34
+ const MAX_WAITING = 64
35
+ const DELTA_TIMEOUT_MS = 60_000
36
+ // No file is begun pretty-printing in this last part of it, as one may take seconds.
37
+ const PRETTY_MARGIN_MS = 15_000
38
+ // The text the two sides may hold together, past which none is counted.
39
+ const MAX_TEXT_LENGTH = 64 * 1024 * 1024
40
+ // Bumped when what a delta counts changes; its keys carry it and PRETTY_VERSION, so none counted otherwise is read.
41
+ const DELTA_VERSION = 4
42
+ const COUNTING = `v${DELTA_VERSION}.${PRETTY_VERSION}`
43
+ const COPIES_AT_ONCE = 8
44
+
45
+ const deltas: Lru<NpmDelta> = lru(20_000)
46
+ const working = new Map<string, Promise<NpmDelta>>()
47
+ const workers = workerTurns({ what: 'package delta', active: MAX_ACTIVE, waiting: MAX_WAITING, error: NpmPackageError,
48
+ busy: 'delta-busy', timeout: 'delta-timeout', failed: 'delta-failed' })
49
+
50
+ const lines = (told: unknown): told is { additions: number; deletions: number } => typeof told === 'object' && told !== null
51
+ && Number.isSafeInteger((told as { additions?: unknown }).additions) && Number.isSafeInteger((told as { deletions?: unknown }).deletions)
52
+ // A delta as kept, none where it isn't one, or is partial.
53
+ function deltaOf(told: unknown): NpmDelta | null {
54
+ if (!lines(told)) return null
55
+ const { best, binary, partial } = told as { best?: unknown; binary?: unknown; partial?: unknown }
56
+ return lines(best) && typeof binary === 'boolean' && partial === undefined
57
+ ? { additions: told.additions, deletions: told.deletions, best: { additions: best.additions, deletions: best.deletions }, binary } : null
58
+ }
59
+
60
+ // A copy by side (0 the base, 1 the other) and path, as the worker reads and makes them.
61
+ type SideCopy = [number, string, PrettyCopy]
62
+ type Counted = Omit<NpmDelta, 'binary' | 'partial'> & { complete: boolean; made: SideCopy[] }
63
+ const isCounted = (told: unknown): told is Counted => lines(told) && lines((told as { best?: unknown }).best)
64
+ && typeof (told as { complete?: unknown }).complete === 'boolean' && Array.isArray((told as { made?: unknown }).made)
65
+ // The count a worker told so far, settled for should it be gone before its answer (a proof of what runs may take minutes).
66
+ const sofar = (told: unknown): Counted | null => {
67
+ const count = { ...(told as { sofar?: object } | null)?.sofar, made: [] }
68
+ return isCounted(count) ? count : null
69
+ }
70
+ const isCopy = (copy: unknown): copy is PrettyCopy => typeof (copy as { text?: unknown } | null)?.text === 'string' && layoutBlankList((copy as { blank?: unknown }).blank) !== null
71
+
72
+ // What the worker counts of `base` against `other`, each its files by path (a binary one's by its digest), given the copies
73
+ // kept, and whether Compare proves types and prints code (`drawable`).
74
+ function counted(base: [string, unknown][], other: [string, unknown][], copies: SideCopy[], drawable: { proves: boolean; prints: boolean }, deadline: number): Promise<Counted> {
75
+ return workers.run(new URL('./npm-delta-worker.js', import.meta.url), {
76
+ workerData: { base, other, copies, ...drawable, prettyUntil: deadline - PRETTY_MARGIN_MS }, execArgv: ['--disable-warning=ExperimentalWarning'], resourceLimits: { maxOldGenerationSizeMb: 1536 },
77
+ }, told => (isCounted(told) ? told : null), deadline, sofar)
78
+ }
79
+
80
+ // A side's text files, by path, how long their texts are, and its other,
81
+ // binary, files.
82
+ function texts(files: NpmPackageFile[] | null) {
83
+ let length = 0
84
+ const binaries: NpmPackageFile[] = []
85
+ const read = (files ?? []).flatMap(file => {
86
+ const text = file.link === undefined ? npmFileText(file.bytes) : null
87
+ if (text === null) { binaries.push(file); return [] }
88
+ length += text.length
89
+ return [[file.path, text] as [string, string]]
90
+ })
91
+ return { read, length, binaries }
92
+ }
93
+
94
+ const same = (a: Uint8Array, b: Uint8Array) => Buffer.from(a.buffer, a.byteOffset, a.byteLength).equals(b)
95
+ const digest = (bytes: Uint8Array) => createHash('sha256').update(bytes).digest('base64')
96
+
97
+ // Whether `news`, a side's binary files, differ from `olds`, the other's:
98
+ // one added, removed or changed, one moved where it is the same as one that
99
+ // was elsewhere not, matched by their digests, each file looked at once.
100
+ function binaryChanged(olds: NpmPackageFile[], news: NpmPackageFile[]): boolean {
101
+ if (olds.length !== news.length) return true
102
+ const left = new Map(olds.map(({ path, bytes }) => [path, bytes]))
103
+ const moved: Uint8Array[] = []
104
+ for (const { path, bytes } of news) {
105
+ const was = left.get(path)
106
+ if (was && same(was, bytes)) left.delete(path)
107
+ else moved.push(bytes)
108
+ }
109
+ // Those not where they were, by their digests, as many of each as there are.
110
+ const unmoved = new Map<string, number>()
111
+ for (const bytes of left.values()) {
112
+ const key = digest(bytes)
113
+ unmoved.set(key, (unmoved.get(key) ?? 0) + 1)
114
+ }
115
+ return moved.some(bytes => {
116
+ const key = digest(bytes)
117
+ const count = unmoved.get(key) ?? 0
118
+ if (count === 0) return true
119
+ unmoved.set(key, count - 1)
120
+ return false
121
+ })
122
+ }
123
+
124
+ type Side = { integrity: string; files: () => Promise<NpmPackageFile[]>; copies?: KeptCopies | null }
125
+ // A delta's key: the integrities of the version it is against, or none, and of the version; and, against one, whether
126
+ // they are `alike`, two unpatched versions of one package, as Compare proves types and prints code of those alone.
127
+ export const deltaKey = (base: string | null, other: string, alike = true) => `${COUNTING}\0${base !== null && !alike ? 'apart\0' : ''}${base ?? '(empty)'}\0${other}`
128
+ // The same of two public versions by their names and versions (`(empty)`
129
+ // for none), as npm never gives one other files, the first as the pnpm patch
130
+ // `patchHash` names changes it where one does, whose hash names its text:
131
+ // its key in npmVersionDeltaStore.
132
+ export const versionDeltaKey = (name: string, version: string, otherName: string, otherVersion: string, patchHash: string | null = null) =>
133
+ `${COUNTING}\0${name}@${version}\0${otherVersion === '(empty)' ? '(empty)' : `${otherName}@${otherVersion}`}${patchHash === null ? '' : `\0patch:${patchHash}`}`
134
+ // Those of `keys` (versionDeltaKey) kept, by key, as of `now`: one kept past
135
+ // VERSION_DELTA_KEPT_MS not, though no write has pruned it yet.
136
+ export async function keptVersionDeltas(keys: string[], kept: KeptStore<NpmDelta>, now = Date.now()): Promise<Map<string, NpmDelta>> {
137
+ const found = new Map<string, NpmDelta>()
138
+ for (const [key, row] of keys.length > 0 ? await kept.readMany([...new Set(keys)]) : []) {
139
+ const known = now - row.at < VERSION_DELTA_KEPT_MS ? deltaOf(row.value) : null
140
+ if (known) found.set(key, known)
141
+ }
142
+ return found
143
+ }
144
+
145
+ // The delta of `other` (its integrity, and how to read its files) against
146
+ // `base`, or none, as held, else shared with one asked before it, else
147
+ // looked for where it is kept, else worked out and kept: shared from before
148
+ // it is looked for, lest those asked together each look, find none, and work
149
+ // it out. `alike` as deltaKey has it.
150
+ export function npmDelta(base: Side | null, other: Side, kept: KeptStore<NpmDelta> | null = null, alike = true): Promise<NpmDelta> {
151
+ const key = deltaKey(base?.integrity ?? null, other.integrity, alike)
152
+ const known = deltas.get(key)
153
+ if (known) return Promise.resolve(known)
154
+ const shared = working.get(key)
155
+ if (shared) return shared
156
+ const work = worked(key, base, other, kept, { proves: !base || alike, prints: !!base && alike }).finally(() => working.delete(key))
157
+ working.set(key, work)
158
+ return work
159
+ }
160
+
161
+ // The deltas of those of `sides` (each the integrities of the version it is
162
+ // against, or none, and of the version) held or kept, by their keys
163
+ // (npmDelta's), in one read of `kept` for those not held: none worked out.
164
+ export async function keptNpmDeltas(sides: [string | null, string, boolean][], kept: KeptStore<NpmDelta>): Promise<Map<string, NpmDelta>> {
165
+ const keys = [...new Set(sides.map(([base, other, alike]) => deltaKey(base, other, alike)))]
166
+ const found = new Map(keys.flatMap(key => { const known = deltas.get(key); return known ? [[key, known] as const] : [] }))
167
+ const unheld = keys.filter(key => !found.has(key))
168
+ for (const [key, row] of unheld.length > 0 ? await kept.readMany(unheld) : []) {
169
+ const known = deltaOf(row.value)
170
+ if (!known) continue
171
+ deltas.set(key, known)
172
+ found.set(key, known)
173
+ }
174
+ return found
175
+ }
176
+
177
+ // Those kept of the printable files both sides have, changed, by side and path.
178
+ async function keptCopies(sides: (Side | null)[], files: Map<string, Uint8Array>[]): Promise<SideCopy[]> {
179
+ const changed = [...files[1]!].filter(([path, bytes]) => prettyExtension(path) !== null && files[0]!.has(path) && !same(files[0]!.get(path)!, bytes))
180
+ const asked = changed.flatMap(([path]) => [0, 1].map(at => [at, path] as const)).filter(([at]) => sides[at]?.copies)
181
+ const found = await eachOf(asked, COPIES_AT_ONCE, async ([at, path]) => [at, path, await sides[at]!.copies!.kept(path, files[at]!.get(path)!).catch(() => null)] as const)
182
+ return found.filter((copy): copy is SideCopy => copy[2] !== null)
183
+ }
184
+
185
+ // As kept, else its turn awaited, then both sides' files read, counted and
186
+ // kept, the copies it printed kept for Compare.
187
+ async function worked(key: string, base: Side | null, other: Side, kept: KeptStore<NpmDelta> | null, drawable: { proves: boolean; prints: boolean }): Promise<NpmDelta> {
188
+ const known = deltaOf((await kept?.read(key))?.value)
189
+ if (known) { deltas.set(key, known); return known }
190
+ const sides = [base, other]
191
+ const [delta, made, files] = await workers.inTurn(Date.now() + DELTA_TIMEOUT_MS, async () => {
192
+ // Its turn held till both sides are read, or have failed.
193
+ const [baseRead, otherRead] = await Promise.allSettled([base ? base.files() : Promise.resolve(null), other.files()])
194
+ if (baseRead.status === 'rejected') throw baseRead.reason
195
+ if (otherRead.status === 'rejected') throw otherRead.reason
196
+ const [olds, news] = [texts(baseRead.value), texts(otherRead.value)]
197
+ if (olds.length + news.length > MAX_TEXT_LENGTH) throw new NpmPackageError(413, 'package-too-large')
198
+ const binary = binaryChanged(olds.binaries, news.binaries)
199
+ const byPath = [baseRead.value ?? [], otherRead.value].map(side => new Map(side.filter(file => file.link === undefined).map(file => [file.path, file.bytes])))
200
+ const withBinaries = (side: typeof olds) => [...side.read, ...side.binaries.map(file => [file.path, { format: 'digest', digest: digest(file.bytes) }] as [string, unknown])]
201
+ const told = await counted(withBinaries(olds), withBinaries(news), await keptCopies(sides, byPath), drawable, Date.now() + DELTA_TIMEOUT_MS)
202
+ const counts: NpmDelta = { additions: told.additions, deletions: told.deletions, best: told.best, binary, ...told.complete ? {} : { partial: true as const } }
203
+ return [counts, told.made, byPath] as const
204
+ })
205
+ deltas.set(key, delta)
206
+ if (!delta.partial) await kept?.write(key, delta, Date.now())
207
+ await eachOf(made.filter(([at, path, copy]) => sides[at]?.copies && files[at]?.has(path) && isCopy(copy)), COPIES_AT_ONCE,
208
+ ([at, path, copy]) => sides[at]!.copies!.keep(path, files[at]!.get(path)!, copy))
209
+ return delta
210
+ }
211
+
212
+ // The lines the versions wanting reviews of `answers` (dependencies'
213
+ // answers: pull requests', default branches', bundles') change, as their rows
214
+ // show them (reviewBase), against a version of their package or another's,
215
+ // a patched one's as its patch changes it, where they are counted already:
216
+ // kept by their names (npmVersionDeltaStore, and privateVersionDeltaStore of
217
+ // private ones, as their readers read what the repository locks), else, of
218
+ // public versions npm answered for in a day (npmVersionStore), by their
219
+ // integrities, as npmDelta keys them, then kept by their names too. Only
220
+ // those `answers` list are told, of repositories their reader reads whole,
221
+ // whose lockfiles name them. `[name, version,
222
+ // base, delta]` each, or, of one patched or against another package's
223
+ // version, `[name, version, base, patchHash, baseName, delta]`
224
+ // (npm-delta.js seedNpmDeltas); none counted here, nor npm asked.
225
+ type DeltaAsked = { name: string; version: string; base: string; baseName: string; patch: string | null }
226
+ export async function countedDeltas(db: UpstreamCacheStore, answers: object[], debug = false): Promise<unknown[][]> {
227
+ const asked = new Map<string, DeltaAsked>()
228
+ const keyOf = ({ name, version, base, baseName, patch }: DeltaAsked) => versionDeltaKey(name, version, baseName, base, patch)
229
+ for (const answer of answers as { lockfiles?: { added?: unknown; unreviewed?: unknown }[]; review?: { required?: number; reviewedBases?: true } }[]) {
230
+ const required = answer.review?.required
231
+ if (!Number.isSafeInteger(required)) continue
232
+ for (const pkg of (answer.lockfiles ?? []).flatMap(dir => [...Array.isArray(dir.added) ? dir.added : [], ...Array.isArray(dir.unreviewed) ? dir.unreviewed : []]) as
233
+ { name?: unknown; version?: unknown; source?: unknown; was?: string[]; base?: { name: string; version: string }; reviews?: unknown; rejected?: unknown; baseline?: unknown
234
+ reviewed?: unknown; unsupported?: unknown; patch?: { hash?: unknown } }[]) {
235
+ if (pkg.source !== 'registry' || typeof pkg.name !== 'string' || typeof pkg.version !== 'string' || !isExactVersion(pkg.version)) continue
236
+ if (pkg.patch !== undefined && !isPatchHash(pkg.patch?.hash)) continue
237
+ if (pkg.unsupported || pkg.baseline || pkg.reviewed || typeof pkg.reviews !== 'number' || (pkg.reviews >= required! && !pkg.rejected)) continue
238
+ const base = reviewBase({ ...pkg, name: pkg.name, version: pkg.version, ...Array.isArray(pkg.was) ? { was: pkg.was.filter(prior => typeof prior === 'string') } : {} },
239
+ answer.review)
240
+ if (base && (typeof base.name !== 'string' || typeof base.version !== 'string')) continue
241
+ const one = { name: pkg.name, version: pkg.version, base: base?.version ?? '(empty)', baseName: base?.name ?? pkg.name, patch: (pkg.patch?.hash as string | undefined) ?? null }
242
+ asked.set(keyOf(one), one)
243
+ }
244
+ }
245
+ if (asked.size === 0) return []
246
+ // As the rows' key them: a version alike unpatched and against its own package's as ever.
247
+ const told = (one: DeltaAsked, delta: NpmDelta) => one.patch === null && one.baseName === one.name ? [one.name, one.version, one.base, delta]
248
+ : [one.name, one.version, one.base, one.patch, one.baseName, delta]
249
+ const byVersions = npmVersionDeltaStore<NpmDelta>(db, debug)
250
+ const [publicly, privately] = await Promise.all([byVersions, privateVersionDeltaStore<NpmDelta>(db, debug)].map(store => keptVersionDeltas([...asked.keys()], store)))
251
+ const named = new Map([...privately!, ...publicly!])
252
+ const byName = [...asked].flatMap(([key, one]) => named.has(key) ? [told(one, named.get(key)!)] : [])
253
+ for (const key of named.keys()) asked.delete(key)
254
+ if (asked.size === 0) return byName
255
+ const versions = [...asked.values()].flatMap(({ name, version, base, baseName }) => [{ name, version }, ...base === '(empty)' || !isExactVersion(base) ? [] : [{ name: baseName, version: base }]])
256
+ const kept = await npmVersionStore(db, debug).readMany!(versions)
257
+ const integrity = (name: string, version: string) => {
258
+ const hit = kept.get(`${name}@${version}`)
259
+ const dist = hit && Date.now() - hit.at < VERSION_KEPT_MS ? hit.json['dist'] : null
260
+ return plainObject(dist) && typeof dist['integrity'] === 'string' ? dist['integrity'] : null
261
+ }
262
+ // A patched one by its integrity as its patch changes it, as GET deltas keeps it.
263
+ const sides = [...asked].flatMap(([key, one]) => {
264
+ const against = one.base === '(empty)' ? null : integrity(one.baseName, one.base), other = integrity(one.name, one.version)
265
+ const alike = one.patch === null && one.baseName === one.name
266
+ return other && (one.base === '(empty)' || against) ? [{ key, one, sides: [against, one.patch === null ? other : `${other} patch:${one.patch}`, alike] as [string | null, string, boolean] }] : []
267
+ })
268
+ if (sides.length === 0) return byName
269
+ const found = await keptNpmDeltas(sides.map(({ sides: pair }) => pair), npmDeltaStore<NpmDelta>(db, debug))
270
+ const byIntegrity = sides.flatMap(({ key, one, sides: [against, other, alike] }) => {
271
+ const delta = found.get(deltaKey(against, other, alike))
272
+ return delta ? [{ key, one, delta }] : []
273
+ })
274
+ // Kept by their names too, as those npm told of are public, for a later read past the day.
275
+ await Promise.all(byIntegrity.map(({ key, delta }) => byVersions.write(key, delta, Date.now())))
276
+ return [...byName, ...byIntegrity.map(({ one, delta }) => told(one, delta))]
277
+ }