@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,410 @@
1
+ // How a script's text is read, for risk's readers of a file
2
+ // (readability.js, runs-code.js, hosts.js): JavaScript's and CSS's strings,
3
+ // regular expressions and comments, set apart from their code, what of their
4
+ // blanks a minifier drops, which files are scripts node runs, and each
5
+ // offset's line.
6
+ import { stretched } from './characters.js'
7
+
8
+ // Code minified into lines shorter than NPM_LONG_LINE: outside its strings and
9
+ // comments (`/* @__PURE__ */`), its lines average more than anyone writes and
10
+ // next to none of its spaces are ones a minifier drops. Told only in what
11
+ // minifiers write, as GitHub Linguist tells minified files only in JavaScript
12
+ // and CSS, each by its strings and comments (matched in one pass, so that
13
+ // neither starts inside the other, a comment with the spaces around it) and
14
+ // the spaces it can do without.
15
+ export const MINIFIED_AVERAGE = 110
16
+ const MINIFIED_SPACES = .01
17
+ // What a regular expression set aside holds.
18
+ const REGEX = '\uE000'
19
+ // A template's text, after its backtick or a substitution's `}`, to its end
20
+ // or the next substitution's `${`.
21
+ const TEMPLATE_TEXT = String.raw`${stretched(String.raw`[^\x60\\$]|\\[\s\S]|\$(?!\{)`)}(?:\x60|\$\{|(?![\s\S]))`
22
+ // A string, to its quote or its line's end; and a regular expression's part:
23
+ // a character, an escape or a class, to its `]` or as far as it goes (its
24
+ // line's end), so it ends one way.
25
+ const STRING = quote => String.raw`${quote}${stretched(String.raw`[^${quote}\\\n]|\\(?:\r\n|[\s\S])`)}(?:${quote}|$)`
26
+ const REGEX_PART = () => String.raw`[^/\\[\n]|\\.|\[${stretched(String.raw`[^\]\\\n]|\\.`)}(?:\]|(?![^\\\n]|\\.))`
27
+ // JavaScript's strings, templates (to a substitution's `${`, as jsMatchAll
28
+ // reads one) and regular expressions (the first group) and its comments, a
29
+ // regular expression read where `context` (a lookbehind) says one starts.
30
+ // A hashbang, the file's first line (`#!/usr/bin/env node`), a comment, after
31
+ // a byte order mark too, which node's ES module loader drops first. A
32
+ // string's or template's escape may be a line continuation (`\` before a line
33
+ // break, CRLF's whole in a string, whose LF would end it).
34
+ // Linear on any text: a regular expression's context looked back on only
35
+ // before a `/`, a comment's leading blanks taken only from where they start,
36
+ // and what is left open read to where it would end, a string or a regular
37
+ // expression its line's end, a template or a comment the text's, rather than
38
+ // tried again from each later quote or slash; each run of their text read a
39
+ // stretch at a time (stretched), as one loop over megabytes runs out of stack.
40
+ const literalsSource = context => String.raw`((?=\/)${context}\/(?![/*])(?:${REGEX_PART()})${stretched(REGEX_PART)}(?:\/[a-z]*|(?=\\?$))|${STRING('"')}|${STRING("'")}|\x60${TEMPLATE_TEXT})`
41
+ + String.raw`|(?<![ \t])[ \t]*(?:\/\*${stretched(String.raw`[^*]|\*(?!\/)`)}(?:\*\/|(?![\s\S]))|\/\/${stretched('.')}|(?<![\s\S])\uFEFF?#!${stretched('.')})[ \t]*`
42
+ // A regular expression first where a value starts (after `=>`, `...`, an
43
+ // operator or a keyword, not a property: `x.default/2`, `this.#in/2`), so a
44
+ // quote in it (`/["']/`) starts no string; after a `)` or `}` too where it
45
+ // closes a control-flow statement's condition or a block, as jsMatchAll reads
46
+ // one. A `++` or `--` is a postfix one, as JavaScript reads them in pairs,
47
+ // after which `/` divides (`i++ / 2`); an odd run of them ends in an operator
48
+ // (`a + /x/`, `a +++ /x/`).
49
+ const VALUE_STARTS = String.raw`(?<=(?:^|\.\.\.|[(,=:[!&|?{;>*/%^<~]|(?<!\+)(?:\+\+)*\+|(?<!-)(?:--)*-|(?<![\p{ID_Continue}$.#])(?:await|case|default|delete|do|else|extends|in|instanceof|new|of|return|throw|typeof|void|yield))[ \t]*)`
50
+ // JavaScript's strings and regular expressions (the first group) and its
51
+ // comments, for a reader that sets them aside (runs-code.js, hosts.js), read
52
+ // through jsMatchAll, which tells a regular expression after a `)` or `}`
53
+ // from a division.
54
+ export const JS_STRING_OR_COMMENT = new RegExp(literalsSource(VALUE_STARTS), 'gmu')
55
+ const JS = {
56
+ // Its literals, what one holds aside: a template's substitutions', and
57
+ // what is read both ways past a `}` (jsMatchAll).
58
+ * matchAll(text) {
59
+ let end = 0
60
+ for (const found of jsMatchAll(text)) {
61
+ if (found.index < end) continue
62
+ end = found.index + found[0].length
63
+ yield found
64
+ }
65
+ },
66
+ // Beside punctuation (`a, b`, `x = 1`), not between two words, as
67
+ // JavaScript tells a word's characters (`return a`, `var π`, `var \u03c0`),
68
+ droppable: /(?<![\p{ID_Continue}$\\\u200C\u200D])[ \t]+|[ \t]+(?![\p{ID_Continue}$\\\u200C\u200D])/gu,
69
+ // nor between two `+`, two `-` or two `/` (`a+ +b` is no `a++b`, `a/ /b/`
70
+ // no comment), nor after a regular expression before a word (`/a/ in b`).
71
+ needed: (code, index, run) => ('+-/'.includes(code[index - 1]) && code[index + run.length] === code[index - 1])
72
+ || (code[index - 2] === REGEX && /[\p{ID_Continue}$\\\u200C\u200D]/u.test(code[index + run.length])),
73
+ }
74
+ // A stylesheet's strings (the first group) and comments: no `//` comments, so
75
+ // `url(https://…)` is code. A string's escape may be a line continuation (`\`
76
+ // before a line break, CRLF's whole).
77
+ const CSS_STRING_OR_COMMENT = /("(?:[^"\\\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\n]|\\(?:\r\n|[\s\S]))*')|[ \t]*\/\*[\s\S]*?\*\/[ \t]*/gu
78
+ const CSS = {
79
+ matchAll: text => text.matchAll(CSS_STRING_OR_COMMENT),
80
+ // Beside braces, `;`, `,`, `>`, parentheses, after `:` (`a { color: red }`)
81
+ // and beside a selector's `+` or `~` (`.a + .b`), not those a selector or a
82
+ // value needs (`.a .b`, `1px solid #fff`, `a :hover`),
83
+ droppable: /(?<=[{};,:>(])[ \t]+|[ \t]+(?=[{};,>)!])|(?<=[+~])[ \t]+|[ \t]+(?=[+~])/gu,
84
+ // nor beside `+` or `~` in parentheses (`calc(1px + var(--x))`) or in a
85
+ // custom property's value, kept for a `calc()` to come (`--gap:1px + 2px`).
86
+ needed: (code, index, run, depth) => /[+~]/u.test(code[index - 1] + code[index + run.length])
87
+ && (depth > 0 || /^\s*--[^:]*:/u.test(code.slice(Math.max(...['{', ';', '}'].map(end => code.lastIndexOf(end, index))) + 1, index))),
88
+ }
89
+
90
+ // A `/` after a `)` or `}`, blanks between or none, that starts no comment: a
91
+ // regular expression's where the `)` closes a control-flow statement's
92
+ // condition (`if (x) /["]/.test(s)`, `while (i--) /'/.test(s)`) or the `}` a
93
+ // block, else a division's (`(a) / b`, `x = {} / 1`), told apart by the
94
+ // parentheses and braces before it, which no lookbehind can match.
95
+ const AFTER_CLOSE = String.raw`(?=\/)(?<=[)}][ \t]*)(\/)(?![/*])`
96
+ // A template's text after a substitution's `}` (the first group).
97
+ const TEMPLATE_REST = new RegExp(String.raw`(\}${TEMPLATE_TEXT})`, 'uy')
98
+ // What a `{` opens, by the code before it, blanks aside: a BLOCK (after `;`,
99
+ // `}`, `=>`, a condition's `)`, `else`, `do`, `try` or `finally`, in a block
100
+ // or first in the text), after which `/` starts a regular expression; a
101
+ // VALUE, an object literal (after an operator, `...`, a keyword on its line
102
+ // taking a value, or first in a substitution), after which it divides; or
103
+ // EITHER, as a function's, class's or label's body may be, as only a parse
104
+ // tells, TypeScript's after `>` or `void` among them (`(): Promise<void> {`,
105
+ // `extends A<B> {`, `(): void {`).
106
+ const BLOCK = 'block', EITHER = 'either', VALUE = 'value'
107
+ const BLOCK_AFTER = /(?:[;}]|=>|(?<![\p{ID_Continue}$#])(?:else|do|try|finally))$/u
108
+ const VALUE_AFTER = /(?:[=([,?!~%^&|*/<]|\.\.\.|(?<!\+)(?:\+\+)*\+|(?<!-)(?:--)*-)$/u
109
+ const KEYWORD_AFTER = /(?<![\p{ID_Continue}$#.])(?:typeof|delete|in|instanceof|new|case|return|throw)$/u
110
+ function braceKind(text, at, read, closed, enclosing) {
111
+ let code = at
112
+ while (code > read && /\s/u.test(text[code - 1])) code--
113
+ if (code === read) return read === 0 ? BLOCK : read === enclosing?.code ? VALUE : EITHER
114
+ const before = text.slice(Math.max(read, code - 11), code)
115
+ if (BLOCK_AFTER.test(before) || (closed && before.endsWith(')')) || (enclosing === BLOCK && before.endsWith('{'))) return BLOCK
116
+ return VALUE_AFTER.test(before) || (KEYWORD_AFTER.test(before) && !text.slice(code, at).includes('\n')) ? VALUE : EITHER
117
+ }
118
+ // Whether only blanks are before `at` on its line, where a regular
119
+ // expression may follow a `}` on a line before.
120
+ const LINE_START = /(?<=^[ \t]*)/muy
121
+ function startsLine(text, at) {
122
+ LINE_START.lastIndex = at
123
+ return LINE_START.test(text)
124
+ }
125
+ // Past a `/` after a `}` that is EITHER's, where a string, template or
126
+ // regular expression one reading holds the other may read as code: the rest
127
+ // read as any reading may, each of a reader's own matches in it (`own`), each
128
+ // string and template from each quote or backtick no backslash escapes to the
129
+ // next of its kind (ANY_LITERAL, a string to its line's end at most), and
130
+ // last the rest whole as a string's, template's or regular expression's
131
+ // text, so nothing node runs is hidden whichever way it reads it. Linear, as
132
+ // the literals of a kind never overlap.
133
+ const ANY_LITERAL = new RegExp(String.raw`(?<!\\)(${STRING('"')}|${STRING("'")}|\u0060${stretched(String.raw`[^\u0060\\]|\\[\s\S]`)}(?:\u0060|(?![\s\S])))`, 'gmu')
134
+ const REST = new RegExp(`(${stretched(String.raw`[\s\S]`)})`, 'uy')
135
+ function* readAny(text, own, from) {
136
+ const next = (pattern, at) => {
137
+ if (pattern === null) return null
138
+ pattern.lastIndex = at
139
+ return pattern.exec(text)
140
+ }
141
+ for (let literal = next(ANY_LITERAL, from), mine = next(own, from); literal !== null || mine !== null;) {
142
+ if (mine === null || (literal !== null && literal.index <= mine.index)) {
143
+ yield literal
144
+ literal = next(ANY_LITERAL, literal.index + 1)
145
+ } else {
146
+ yield mine
147
+ mine = next(own, mine.index + Math.max(mine[0].length, 1))
148
+ }
149
+ }
150
+ REST.lastIndex = from
151
+ yield REST.exec(text)
152
+ }
153
+ // A `(` opening a condition: after `if`, `while`, `for` (`for await` too) or
154
+ // `with` (its first group, the blanks after it its second), a word of its own
155
+ // (not `elif(`), not a property's name, blanks after its dot or none (`x.if(`,
156
+ // `x. if(`), nor one after a comment there (`x./* c */if(`), as jsMatchAll
157
+ // tells by the comment before it.
158
+ const CONDITION = /(?<=(?<![\p{ID_Continue}$#])(?<!\.\s*)(if|while|with|for(?:\s+await)?)(\s*))\(/uy
159
+ // What a match that is neither a string nor a reader's own starts with: a
160
+ // comment's `//` or `/*`, or a hashbang, blanks before it.
161
+ const COMMENT = /^[ \t]*(?:\/[/*]|\uFEFF?#!)/u
162
+ // A property's dot before where it is looked back from, blanks between.
163
+ const DOT_BEFORE = /(?<=\.\s*)/uy
164
+ // The first character that is no blank.
165
+ const CODE = /\S/gu
166
+ // A template literal read (piece's `template`) as a match: from its start to
167
+ // `end`, its first group its pieces' text, each substitution's code left out
168
+ // (`/proc/${pid}/mem` read as `/proc/${}/mem`), and where each piece starts,
169
+ // there and in the text (`pieces`, as jsOffset reads them).
170
+ const templateFound = (text, { index, pieces, text: pieced }, end) => Object.assign([text.slice(index, end), pieced], { index, pieces })
171
+ // A template's piece (`found`, to a `${` or from a substitution's `}`, of
172
+ // `template` or the first of one): its text added to the template's, and
173
+ // where that ends, the template put in its place before what its
174
+ // substitutions hold (`templates.held`), all given back once none is open.
175
+ function* piece(text, found, template, { open, held }, braces) {
176
+ const end = found.index + found[0].length
177
+ if (template === null) {
178
+ template = { index: found.index, text: '', pieces: [], slot: held.length }
179
+ open.push(template)
180
+ held.push(null)
181
+ }
182
+ template.pieces.push([template.text.length, found.index])
183
+ template.text += found[1]
184
+ if (found[1].endsWith('${')) braces.push({ code: end, template })
185
+ else {
186
+ held[template.slot] = templateFound(text, open.pop(), end)
187
+ if (open.length === 0) yield* held.splice(0)
188
+ }
189
+ }
190
+ // What is held for the templates open where the text, or the reading of it,
191
+ // ends, each read to there.
192
+ function* ended(text, { open, held }) {
193
+ for (const template of open) held[template.slot] = templateFound(text, template, text.length)
194
+ yield* held
195
+ }
196
+ // Where `within` a literal's text (jsMatchAll's first group) is in the text:
197
+ // a template's, its substitutions' code left out, by where its pieces are.
198
+ export function jsOffset({ index, pieces }, within) {
199
+ if (pieces === undefined) return index + within
200
+ let high = pieces.length, low = 0
201
+ while (low < high) {
202
+ const middle = (low + high) >> 1
203
+ if (pieces[middle][0] <= within) low = middle + 1
204
+ else high = middle
205
+ }
206
+ return pieces[low - 1][1] + within - pieces[low - 1][0]
207
+ }
208
+ // How jsMatchAll reads a text for a reader's alternatives (a pattern's
209
+ // source): by JS_STRING_OR_COMMENT's, theirs, AFTER_CLOSE and a brace last
210
+ // (`scan`, their groups `slash` and `brace`); at that `/`, by the same with a
211
+ // regular expression read wherever one starts (`regex`); and past it, by
212
+ // theirs alone (`own`, no first group). Each made once.
213
+ const READERS = new Map()
214
+ function readerOf(alternatives) {
215
+ let reader = READERS.get(alternatives)
216
+ if (!reader) {
217
+ const theirs = alternatives === '' ? '' : `|${alternatives}`
218
+ const scan = new RegExp(`${JS_STRING_OR_COMMENT.source}${theirs}|${AFTER_CLOSE}|([{}])`, 'gmu')
219
+ const groups = new RegExp(`${scan.source}|`, 'u').exec('').length
220
+ reader = { scan, slash: groups - 2, brace: groups - 1, regex: new RegExp(`${literalsSource('')}${theirs}`, 'muy'),
221
+ own: alternatives === '' ? null : new RegExp(`(?!)()${theirs}`, 'gmu') }
222
+ READERS.set(alternatives, reader)
223
+ }
224
+ return reader
225
+ }
226
+ // Each of JavaScript's strings, regular expressions and comments in a text, as
227
+ // JS_STRING_OR_COMMENT matches them (the first group a string's, a
228
+ // template's, its substitutions' code left out (templateFound), or a
229
+ // regular expression's), and each match of a reader's `alternatives` (a
230
+ // pattern's source, read with JS_STRING_OR_COMMENT's flags) in its code, a
231
+ // substitution's too, in order, as matchAll finds them, a template before
232
+ // what its substitutions hold; but a regular expression after the `)`
233
+ // closing a control-flow statement's condition or the `}` closing a block
234
+ // read as one, by the parentheses and braces in the code before it
235
+ // (AFTER_CLOSE, a regular expression at a line's start too after a `}`), a
236
+ // reader's own matches' among them, a string's, a regular expression's or a
237
+ // comment's not; and past a `}` that may close either (braceKind's EITHER),
238
+ // the rest as readAny reads it, out of order.
239
+ // Linear on any text as JS_STRING_OR_COMMENT is: what is matched is never
240
+ // matched again, nor the code between read twice, but by readAny, once.
241
+ export function* jsMatchAll(text, alternatives = '') {
242
+ const { scan, slash, brace, regex, own } = readerOf(alternatives)
243
+ // Whether each parenthesis open opens a condition and whether the last one
244
+ // closed did; what each brace open opens (braceKind), a substitution's where
245
+ // its code starts; what the last `}` closed and where, while only blanks
246
+ // and comments follow it; and how far the code is read.
247
+ const braces = [], open = []
248
+ let after = null, closed = false, read = 0
249
+ // Each template open, and what is read while one is, held for it to come
250
+ // first.
251
+ const templates = { held: [], open: [] }
252
+ // The last comment read: whether a property's dot is before it, blanks and
253
+ // comments between, and where what follows it starts, blanks aside.
254
+ let comment = { dot: false, next: -1 }
255
+ for (let at = 0; ;) {
256
+ scan.lastIndex = at
257
+ const match = scan.exec(text)
258
+ if (match === null) {
259
+ yield* ended(text, templates)
260
+ return
261
+ }
262
+ // The parentheses in the code before it, a reader's own match's among it.
263
+ const code = text.slice(read, match.index)
264
+ for (let closes = code.indexOf(')'), opens = code.indexOf('('); opens !== -1 || closes !== -1;) {
265
+ if (closes === -1 || (opens !== -1 && opens < closes)) {
266
+ const next = read + opens
267
+ CONDITION.lastIndex = next
268
+ const keyword = CONDITION.exec(text)
269
+ // Not where its keyword starts right after a comment following a
270
+ // property's dot.
271
+ open.push(keyword !== null && !(comment.dot && comment.next === next - keyword[2].length - keyword[1].length))
272
+ opens = code.indexOf('(', opens + 1)
273
+ } else {
274
+ closed = open.pop() === true
275
+ closes = code.indexOf(')', closes + 1)
276
+ }
277
+ }
278
+ if (after !== null && /\S/u.test(code.slice(read < after.end ? 1 : 0))) after = null
279
+ const opened = match[brace] === '{' ? braceKind(text, match.index, read, closed, braces.at(-1)) : null
280
+ read = match.index
281
+ let found = match, template = null
282
+ if (opened !== null) {
283
+ braces.push(opened)
284
+ at = match.index + 1
285
+ continue
286
+ }
287
+ if (match[brace] !== undefined) {
288
+ // What it closes; a substitution's end, its template's text next.
289
+ const kind = braces.pop() ?? EITHER
290
+ at = match.index + 1
291
+ if (typeof kind === 'string') {
292
+ after = { kind, end: at }
293
+ continue
294
+ }
295
+ TEMPLATE_REST.lastIndex = match.index
296
+ found = TEMPLATE_REST.exec(text)
297
+ template = kind.template
298
+ } else if (match[slash] !== undefined || (after !== null && match[1]?.[0] === '/' && startsLine(text, match.index))) {
299
+ // A value's `}` divides on its line alone: past it, it may end a
300
+ // statement, as a type's does in TypeScript (`type T = {}`).
301
+ const kind = match[slash] === undefined && after.kind !== BLOCK ? EITHER : after?.kind ?? (closed ? BLOCK : VALUE)
302
+ if (kind === EITHER) {
303
+ yield* ended(text, templates)
304
+ yield* readAny(text, own, match.index + 1)
305
+ return
306
+ }
307
+ regex.lastIndex = match.index
308
+ found = kind === VALUE ? null : match[slash] === undefined ? match : regex.exec(text)
309
+ }
310
+ // A division, its `/` code.
311
+ if (found === null) {
312
+ at = match.index + 1
313
+ continue
314
+ }
315
+ // A string's, a regular expression's or a comment's text no code; a
316
+ // template's to a substitution's `${`, its code next, the template
317
+ // whole where it ends, before what its substitutions hold.
318
+ const end = found.index + found[0].length
319
+ if (found[1] !== undefined) {
320
+ read = at = end
321
+ after = null
322
+ if (template !== null || (found[1][0] === '\u0060' && found[1].endsWith('${'))) {
323
+ yield* piece(text, found, template, templates, braces)
324
+ continue
325
+ }
326
+ } else if (COMMENT.test(found[0])) {
327
+ // As the comment before it says where only blanks are between them.
328
+ DOT_BEFORE.lastIndex = found.index
329
+ const dot = comment.next === found.index + found[0].search(/\S/u) ? comment.dot : DOT_BEFORE.test(text)
330
+ CODE.lastIndex = end
331
+ comment = { dot, next: CODE.exec(text)?.index ?? text.length }
332
+ read = end
333
+ } else after = null
334
+ if (templates.open.length > 0) templates.held.push(found)
335
+ else yield found
336
+ at = Math.max(end, found.index + 1)
337
+ }
338
+ }
339
+ // A stylesheet's by its name, else JavaScript's for a script by its name, or
340
+ // for any other where node runs it as one (`script`).
341
+ export const minifiable = (path, script = false) => /\.css$/iu.test(path) ? CSS : script || /\.[cm]?js$/iu.test(path) ? JS : null
342
+ // How much of a text is read for how its lines are minified: their average
343
+ // is told by as much of them, and a text of millions of lines is read no
344
+ // further.
345
+ const MINIFIED_READ = 4 * 1024 * 1024
346
+ // Its code's lines' average length where they are minified into short ones,
347
+ // else 0, told by its first MINIFIED_READ characters.
348
+ export function minifiedAverage(whole, { matchAll, droppable, needed }) {
349
+ const text = whole.length > MINIFIED_READ ? whole.slice(0, whole.lastIndexOf('\n', MINIFIED_READ) + 1 || MINIFIED_READ) : whole
350
+ // Its lines of code alone, as long as what is on them: a comment set aside
351
+ // leaves nothing but its line breaks, a string or template `""` and its, a
352
+ // regular expression `/REGEX/`, so the code either side stays apart.
353
+ const parts = []
354
+ let from = 0
355
+ for (const { 0: match, 1: string, index } of matchAll(text)) {
356
+ parts.push(text.slice(from, index), string === undefined ? '' : string[0] === '/' ? `/${REGEX}/` : '""', match.replaceAll(/[^\n]/gu, ''))
357
+ from = index + match.length
358
+ }
359
+ parts.push(text.slice(from))
360
+ const lines = parts.join('').replaceAll(/^[ \t]+/gmu, '')
361
+ .split('\n').filter(line => line.trim() !== '')
362
+ const code = lines.join('\n'), length = code.length - (lines.length - 1)
363
+ // Counted by character: a run aligning `=` is as many spaces as it is wide.
364
+ let depth = 0, dropped = 0, scanned = 0
365
+ for (const { 0: run, index } of code.matchAll(droppable)) {
366
+ for (; scanned < index; scanned++) depth = Math.max(0, depth + (code[scanned] === '(' ? 1 : code[scanned] === ')' ? -1 : 0))
367
+ if (!needed(code, index, run, depth)) dropped += run.length
368
+ }
369
+ return lines.length > 0 && length > MINIFIED_AVERAGE * lines.length && dropped < MINIFIED_SPACES * length ? Math.round(length / lines.length) : 0
370
+ }
371
+
372
+ // JavaScript or TypeScript (a declaration file too, which node runs as any
373
+ // other), or a script run by node by its hashbang (`bin/cli`, with no
374
+ // extension), a byte order mark before it or not, as node runs one after it
375
+ // as an ES module (in a `"type": "module"` package).
376
+ const SCRIPT = /\.[cm]?[jt]sx?$/iu
377
+ const NODE_HASHBANG = new RegExp(String.raw`^\uFEFF?#!${stretched('.')}\bnode\b`, 'u')
378
+ // Whether a file (its path and text) is a script, as SCRIPT and NODE_HASHBANG
379
+ // tell, or as node runs it whatever its name (`script`, npm/files.js
380
+ // npmLoadedScripts).
381
+ export const npmIsScript = (path, text, script = false) => typeof text === 'string' && (script || SCRIPT.test(path) || NODE_HASHBANG.test(text))
382
+ // How many places a file is told doing a thing, the first ones, at most
383
+ // (runs-code.js, hosts.js): a file doing it on millions of lines is told by
384
+ // its first, and what it holds of them kept to a bound however many there
385
+ // are.
386
+ export const NPM_MAX_FOUND = 100
387
+ // Every how many line breaks one is kept where lineOf counts from.
388
+ const LINE_MARK = 1024
389
+ // Each offset in a text as its line: one more than the line breaks before it,
390
+ // counted on from the nearest of every LINE_MARKth, which alone are kept.
391
+ export function lineOf(text) {
392
+ let marks = null
393
+ return offset => {
394
+ if (!marks) {
395
+ marks = []
396
+ for (let at = text.indexOf('\n'), count = 0; at !== -1; at = text.indexOf('\n', at + 1), count++) if (count % LINE_MARK === 0) marks.push(at)
397
+ }
398
+ let high = marks.length, low = 0
399
+ while (low < high) {
400
+ const middle = (low + high) >> 1
401
+ if (marks[middle] < offset) low = middle + 1
402
+ else high = middle
403
+ }
404
+ if (low === 0) return 1
405
+ let breaks = (low - 1) * LINE_MARK + 1
406
+ for (let at = text.indexOf('\n', marks[low - 1] + 1); at !== -1 && at < offset; at = text.indexOf('\n', at + 1)) breaks++
407
+ return breaks + 1
408
+ }
409
+ }
410
+
@@ -0,0 +1,43 @@
1
+ // How a built file names its source map, and what a source map is: read
2
+ // alike where a version's map is told rebuilt with its file (npm/compare.js
3
+ // rebuiltWithMap) and where a map's listing is of the file beside it that
4
+ // names it (common/pretty-sourcemap.js).
5
+
6
+ // A `sourceMappingURL` comment and no more, on a file's last line.
7
+ const SOURCE_MAP_FILE = /^\s*(?:\/\/[#@] sourceMappingURL=(\S+)|\/\*[#@] sourceMappingURL=(\S+?)\s*\*\/)\s*$/u
8
+ // A map name a build writes (`a.js.map`, `../maps/a.map`): segments of
9
+ // letters, digits and `._~@+-`, as a browser reads them alike. One at a URL,
10
+ // from a site's root, or with an escape or a backslash names none.
11
+ const MAP_NAME = /^(?:[\w.~@+-]+\/)*[\w.~@+-]+\.map$/u
12
+
13
+ // The map a file's text names on its last line by a plain path from the
14
+ // file's own (MAP_NAME), or null: none named, or one named otherwise (a data:
15
+ // URL, a site's).
16
+ export function mapNamed(text) {
17
+ if (typeof text !== 'string') return null
18
+ const trimmed = text.trimEnd()
19
+ const directive = SOURCE_MAP_FILE.exec(trimmed.slice(trimmed.lastIndexOf('\n') + 1))
20
+ const name = directive?.[1] ?? directive?.[2]
21
+ return name && MAP_NAME.test(name) ? name : null
22
+ }
23
+
24
+ // The path `name` names from beside `file`, or null for one above the root.
25
+ export function besideFile(file, name) {
26
+ const parts = file.split('/').slice(0, -1)
27
+ for (const part of name.split('/')) {
28
+ if (part === '..' && parts.length === 0) return null
29
+ if (part === '..') parts.pop()
30
+ else if (part !== '.') parts.push(part)
31
+ }
32
+ return parts.join('/')
33
+ }
34
+
35
+ // Whether a text is a source map: JSON with `mappings`, or each of an
36
+ // indexed map's `sections` with its own.
37
+ export function isSourceMap(text) {
38
+ try {
39
+ const json = JSON.parse(text)
40
+ return typeof json?.mappings === 'string'
41
+ || (Array.isArray(json?.sections) && json.sections.length > 0 && json.sections.every(section => typeof section?.map?.mappings === 'string'))
42
+ } catch { return false }
43
+ }
@@ -0,0 +1,47 @@
1
+ // A file's stamps: what risk reads of one file (the readers beside this), and
2
+ // what of it calls for a look, as a list of files tags it and orders it by:
3
+ // each `{ name, severity }`, its tag's text and how severe it is (SEVERITIES).
4
+ import { READABILITY, npmFileReadability, npmLabelMark, npmLargeCode, npmReadabilityLabels, npmTaggedCategory } from './readability.js'
5
+ import { npmExfiltration, npmHostSeverity, npmHostTags, npmHosts } from './hosts.js'
6
+ import { npmRunsCode, npmRunsCodeFlags } from './runs-code.js'
7
+
8
+ // How severe a stamp or a finding is, the most first: `critical` red,
9
+ // `high` a warning's orange, `moderate` yellow, `low`, and a `notice`, under
10
+ // a low one.
11
+ export const SEVERITIES = ['critical', 'high', 'moderate', 'low', 'notice']
12
+
13
+ // A label's mark (readability.js READABILITY) as a severity: a warning's
14
+ // orange, or a notice.
15
+ const severityOf = mark => mark === 'warn' ? 'high' : 'notice'
16
+
17
+ // A file's stamps, from what was read of it: what its code does beyond
18
+ // itself (runs-code.js npmRunsCodeFlags), a warning's or, for the whole
19
+ // environment read out and publishing, critical; the services it names
20
+ // (hosts.js), a stamp a tag, at the tag's severity (Exfiltration critical,
21
+ // Remote high, IP lookup moderate); a large code file, a notice; an
22
+ // obfuscator's fingerprint, a warning's; and its control characters, long
23
+ // lines and category, by their marks, but for what reads as most files do
24
+ // (UTF-8, ASCII) and a source map.
25
+ function npmStampsOf({ path, bytes, readability, runsCode, exfiltration, script }) {
26
+ const category = npmTaggedCategory(readability)
27
+ const listed = category !== 'ascii' && category !== 'map' && READABILITY.get(category).mark
28
+ return [...npmRunsCodeFlags(runsCode),
29
+ ...npmHostTags(exfiltration).map(([tag, services]) => ({ name: tag, severity: npmHostSeverity(tag, services) })),
30
+ ...npmLargeCode(path, bytes, script) ? [{ name: 'Large', severity: 'notice' }] : [],
31
+ ...readability.obfuscation ? [{ name: 'Obfuscated', severity: 'high' }] : [],
32
+ ...npmReadabilityLabels(readability).filter(label => label === 'controls' || label === 'long')
33
+ .map(label => ({ name: READABILITY.get(label).tag, severity: severityOf(npmLabelMark(readability, label)) })),
34
+ ...listed ? [{ name: READABILITY.get(category).tag, severity: severityOf(listed) }] : []]
35
+ }
36
+
37
+ // Everything risk reads of a file (`path`, its `text`, null for one that
38
+ // isn't, and its size in `bytes`): how it reads (npmFileReadability), what
39
+ // its code does beyond itself (npmRunsCode), the services it names under the
40
+ // hosts' tags (npmExfiltration) and the hosts its code names (npmHosts), and its
41
+ // `stamps` (npmStampsOf); read as a script where node runs it as one whatever
42
+ // its name (`script`, npm/files.js npmLoadedScripts).
43
+ export function npmFileRisk(path, text, bytes, script = false) {
44
+ const readability = npmFileReadability(path, text, script)
45
+ const exfiltration = npmExfiltration(path, text, script), runsCode = npmRunsCode(path, text, script)
46
+ return { readability, runsCode, exfiltration, hosts: npmHosts(path, text, script), stamps: npmStampsOf({ path, bytes, readability, runsCode, exfiltration, script }) }
47
+ }