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

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