@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.
- package/LICENSE-APACHE +202 -0
- package/README.md +16 -0
- package/api/reap.ts +16 -78
- package/cli.js +6 -0
- package/client/dependency-paths.d.ts +3 -0
- package/client/dependency-paths.js +36 -0
- package/client/finding-link.js +305 -0
- package/client/linked-findings.d.ts +1 -0
- package/client/linked-findings.js +115 -0
- package/common/bundle-commit.d.ts +1 -0
- package/common/bundle-commit.js +4 -0
- package/common/bundle-compare-diff.d.ts +6 -0
- package/common/bundle-compare-diff.js +377 -0
- package/common/bundle-compare-inputs.d.ts +3 -0
- package/common/bundle-compare-inputs.js +141 -0
- package/common/bundle-compare-versions.js +172 -0
- package/common/bundle-comparison.d.ts +8 -0
- package/common/bundle-comparison.js +69 -0
- package/common/bundle-imports.js +20 -0
- package/common/bundle-metadata.d.ts +15 -0
- package/common/bundle-metadata.js +226 -0
- package/common/bundle-pkg-of.d.ts +1 -0
- package/common/bundle-pkg-of.js +90 -0
- package/common/bundle-reasons.d.ts +6 -0
- package/common/bundle-reasons.js +58 -0
- package/common/bundle-source-links.d.ts +6 -0
- package/common/bundle-source-links.js +216 -0
- package/common/bundle-sourcemap.d.ts +10 -0
- package/common/bundle-sourcemap.js +100 -0
- package/common/bundle-sources.d.ts +10 -0
- package/common/bundle-sources.js +374 -0
- package/common/bundle-stats.js +41 -0
- package/common/bundle-tabs.js +1 -0
- package/common/code-language.js +66 -0
- package/common/default-scan-models.ts +36 -0
- package/common/dependency-reviews.d.ts +18 -0
- package/common/dependency-reviews.js +64 -0
- package/common/finding-duplicates.js +63 -0
- package/common/finding-filters.js +313 -0
- package/common/finding-id.js +47 -0
- package/common/finding-repo.d.ts +5 -0
- package/common/finding-repo.js +67 -0
- package/common/github-issue-labels.d.ts +1 -0
- package/common/github-issue-labels.js +11 -0
- package/common/github-pr.ts +56 -0
- package/common/gzip.js +59 -0
- package/common/line-diff.d.ts +14 -0
- package/common/line-diff.js +181 -0
- package/common/linked-workspace-groups.js +63 -0
- package/common/managed/agent-tasks.ts +292 -0
- package/common/managed/comments.ts +43 -0
- package/common/managed/dependency-routes.js +25 -0
- package/common/managed/directory-packages.ts +28 -0
- package/common/managed/finding-detail.d.ts +5 -0
- package/common/managed/finding-detail.js +75 -0
- package/common/managed/finding-values.d.ts +2 -0
- package/common/managed/finding-values.js +138 -0
- package/common/managed/github-agents.ts +114 -0
- package/common/managed/github-logins.ts +49 -0
- package/common/managed/lite-name.d.ts +2 -0
- package/common/managed/lite-name.js +20 -0
- package/common/managed/npm-packages.d.ts +9 -0
- package/common/managed/npm-packages.js +63 -0
- package/common/managed/permissions.ts +31 -0
- package/common/managed/report-content.ts +72 -0
- package/common/managed/report-filter.ts +120 -0
- package/common/managed/repository-alias.ts +65 -0
- package/common/managed/review-view-routes.js +23 -0
- package/common/managed/risk-routes.js +30 -0
- package/common/managed/roles.ts +41 -0
- package/common/managed/routes.d.ts +2 -0
- package/common/managed/routes.js +385 -0
- package/common/managed/scan-models.ts +6 -0
- package/common/managed/stasis-config.ts +7 -0
- package/common/managed/team-app.d.ts +19 -0
- package/common/managed/team-app.js +129 -0
- package/common/managed/team-filters.js +26 -0
- package/common/managed/triage.ts +83 -0
- package/common/managed/upload-seal.ts +126 -0
- package/common/pretty-json.js +98 -0
- package/common/pretty-kinds.js +20 -0
- package/common/pretty-print.d.ts +6 -0
- package/common/pretty-print.js +36 -0
- package/common/pretty-sourcemap.js +70 -0
- package/common/repo-commit.d.ts +4 -0
- package/common/repo-commit.js +30 -0
- package/common/revalidation-conflicts.js +48 -0
- package/common/revalidation-input-groups.js +56 -0
- package/common/save-error-reason.ts +20 -7
- package/common/scan-server.ts +13 -0
- package/common/security-links.d.ts +5 -0
- package/common/security-links.js +33 -0
- package/common/server-info.ts +46 -0
- package/common/sourcemap-listing.js +78 -0
- package/common/sourcemap-mappings.js +136 -0
- package/common/sourcemap-unlist.js +85 -0
- package/common/stasis-files.d.ts +5 -0
- package/common/stasis-files.js +20 -0
- package/common/stasis-tgz.d.ts +6 -0
- package/common/stasis-tgz.js +27 -0
- package/common/utf8.d.ts +3 -0
- package/common/utf8.js +45 -0
- package/common/whitespace-insensitive.js +19 -0
- package/common/workspace-app.js +91 -0
- package/common/workspace-groups.js +176 -0
- package/out/activity.js +2 -0
- package/out/brotli-fallback.js +3 -3
- package/out/chunk-2XUEZHIP.js +28 -0
- package/out/chunk-55YIWY3H.js +5 -0
- package/out/chunk-563V5VVT.js +266 -0
- package/out/chunk-5AVGKFMR.js +1 -0
- package/out/chunk-5EAVJAD2.js +3 -0
- package/out/chunk-5M2X3QPK.js +6 -0
- package/out/chunk-5QWPKTCE.js +40 -0
- package/out/chunk-5UJHJWFU.js +4 -0
- package/out/chunk-6CCSTJWB.js +1 -0
- package/out/chunk-6VOB5WXX.js +2 -0
- package/out/chunk-6WMQVUT2.js +3 -0
- package/out/chunk-6Y6PZ2BU.js +9 -0
- package/out/chunk-74JZTGTO.js +11 -0
- package/out/chunk-7BKKYVWR.js +7 -0
- package/out/chunk-7C7DWL7I.js +1 -0
- package/out/chunk-7HP2VYIC.js +2 -0
- package/out/chunk-7YFJUT2P.js +1 -0
- package/out/chunk-7YO4QV5P.js +1 -0
- package/out/chunk-A4BBBHQU.js +1 -0
- package/out/chunk-B64CTMVU.js +4 -0
- package/out/chunk-BEKYZCPX.js +14 -0
- package/out/chunk-C4Z6ZBXV.js +10 -0
- package/out/chunk-CU3FPB3P.js +1 -0
- package/out/chunk-DEVAORP2.js +1 -0
- package/out/chunk-DOYTKRIR.js +53 -0
- package/out/chunk-DUASGS2O.js +8 -0
- package/out/chunk-EEJA7JBI.js +13 -0
- package/out/chunk-FSU2PLJL.js +1 -0
- package/out/chunk-GO7FT4OD.js +1 -0
- package/out/chunk-GX5UXWT4.js +1 -0
- package/out/chunk-H4FEC57K.js +1 -0
- package/out/chunk-ITUKKCCK.js +10 -0
- package/out/chunk-IYPY47MC.js +1 -0
- package/out/chunk-K4XJHM2T.js +1 -0
- package/out/chunk-LBD3M3J6.js +1 -0
- package/out/chunk-LVSLM7HB.js +1 -0
- package/out/chunk-LVW7WWV4.js +1 -0
- package/out/chunk-N57OWFKY.js +15 -0
- package/out/chunk-NRPC74AM.js +1 -0
- package/out/chunk-OAWQNDAO.js +3 -0
- package/out/chunk-OOJM4CTU.js +1 -0
- package/out/chunk-PEE47ANR.js +1 -0
- package/out/chunk-PKLVXUEI.js +11 -0
- package/out/chunk-PLGH63UC.js +2 -0
- package/out/chunk-PNHBZGDD.js +1 -0
- package/out/chunk-PTFYDJUP.js +1 -0
- package/out/chunk-PWFUOLZK.js +17 -0
- package/out/chunk-PXK7EU4V.js +1 -0
- package/out/chunk-QD3IBSF4.js +7 -0
- package/out/chunk-QI66YA4F.js +1 -0
- package/out/chunk-QRKFPLA4.js +80 -0
- package/out/chunk-R3NZFXVR.js +1 -0
- package/out/chunk-R5O6FAW4.js +2 -0
- package/out/chunk-RPNO56H2.js +1 -0
- package/out/chunk-SSPDMA6D.js +1 -0
- package/out/chunk-SZWPYLPC.js +1 -0
- package/out/chunk-TUII34K2.js +1 -0
- package/out/chunk-UJP4PQJA.js +1 -0
- package/out/chunk-UJSJMXEG.js +1 -0
- package/out/chunk-UR4WYMIP.js +1 -0
- package/out/chunk-VHEXSGGP.js +1 -0
- package/out/chunk-VYYUMYVW.js +9 -0
- package/out/chunk-WCDYSIKG.js +1 -0
- package/out/chunk-WFBVS4QI.js +1 -0
- package/out/chunk-XM4RLC4A.js +2 -0
- package/out/chunk-ZQHLECTI.js +1 -0
- package/out/client-managed-import.js +4 -0
- package/out/client-managed.js +37 -0
- package/out/client-sync.js +11 -17
- package/out/compare.js +26 -0
- package/out/dialogs.js +40 -0
- package/out/github-issue-authorized.html +3 -0
- package/out/github-issue-authorized.js +1 -0
- package/out/graph.js +19 -4
- package/out/index.html +104 -24
- package/out/prism.js +14 -10
- package/out/scan.js +2 -0
- package/out/stasis.svg +20 -0
- package/out/terminal.js +367 -39
- package/out/view.css +1 -1
- package/out/view.js +19 -147
- package/package.json +409 -64
- package/risk/actions/findings.js +161 -0
- package/risk/actions/resolve.js +287 -0
- package/risk/actions/scripts/commands.js +182 -0
- package/risk/actions/scripts/grade.js +113 -0
- package/risk/actions/scripts/packages.js +123 -0
- package/risk/actions/scripts/shell.js +169 -0
- package/risk/actions/uses.js +94 -0
- package/risk/actions/workflow.js +183 -0
- package/risk/files/characters.js +354 -0
- package/risk/files/hosts.js +206 -0
- package/risk/files/readability.js +185 -0
- package/risk/files/runs-code.js +568 -0
- package/risk/files/script.js +410 -0
- package/risk/files/source-map-link.js +43 -0
- package/risk/files/stamps.js +47 -0
- package/risk/index.d.ts +294 -0
- package/risk/index.js +12 -0
- package/risk/npm/assess.js +285 -0
- package/risk/npm/combined.js +68 -0
- package/risk/npm/compare.js +236 -0
- package/risk/npm/dependencies.js +172 -0
- package/risk/npm/diff.js +79 -0
- package/risk/npm/downloads.js +84 -0
- package/risk/npm/files.js +445 -0
- package/risk/npm/held.js +67 -0
- package/risk/npm/install-scripts.js +34 -0
- package/risk/npm/malware.js +38 -0
- package/risk/npm/manifest.js +65 -0
- package/risk/npm/new-dependency.js +60 -0
- package/risk/npm/old-version.js +108 -0
- package/risk/npm/registry.js +393 -0
- package/risk/npm/release.js +85 -0
- package/risk/npm/score.js +59 -0
- package/risk/npm/specs.js +124 -0
- package/risk/npm/tarball.js +169 -0
- package/server-common/database-config.ts +16 -0
- package/server-common/initialize.ts +18 -0
- package/server-common/npm-advisories.ts +101 -0
- package/{server → server-common}/origin.ts +5 -5
- package/server-common/reap.ts +48 -0
- package/server-common/scan-config.ts +19 -0
- package/server-common/standalone.ts +29 -0
- package/server-common/storage-crypto.ts +182 -0
- package/server-common/storage-log.ts +38 -0
- package/server-common/vercel-blob.ts +103 -0
- package/server-e2e/app.ts +487 -0
- package/{server → server-e2e}/bus-receiver.ts +2 -1
- package/{server → server-e2e}/cli.js +9 -4
- package/{server → server-e2e}/config.ts +50 -42
- package/{server → server-e2e}/db-neon.ts +2 -2
- package/{server → server-e2e}/db-revision-sql.ts +1 -1
- package/{server → server-e2e}/db-stmt.ts +2 -2
- package/{server → server-e2e}/db.ts +7 -7
- package/{server → server-e2e}/http.ts +44 -13
- package/{server → server-e2e}/hub.ts +40 -9
- package/server-e2e/index.ts +17 -0
- package/server-e2e/lifecycle.ts +95 -0
- package/{server → server-e2e}/neon-driver.ts +2 -2
- package/{server → server-e2e}/npm-proxy.ts +12 -145
- package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
- package/{server → server-e2e}/objstore/blob.ts +1 -1
- package/{server → server-e2e}/objstore/fetch-mint-guard.ts +2 -2
- package/{server → server-e2e}/objstore/handlers.ts +9 -8
- package/{server → server-e2e}/objstore/init.ts +19 -27
- package/{server → server-e2e}/objstore/reaper.ts +1 -1
- package/{server → server-e2e}/objstore/rest-deny.ts +1 -1
- package/{server → server-e2e}/objstore/rest-mint.ts +3 -3
- package/{server → server-e2e}/objstore/rest.ts +3 -3
- package/{server → server-e2e}/objstore/store-neon.ts +6 -10
- package/{server → server-e2e}/objstore/store.ts +7 -7
- package/{server → server-e2e}/objstore/tokens.ts +2 -2
- package/{server → server-e2e}/pubsub.ts +12 -9
- package/{server → server-e2e}/sign.ts +4 -4
- package/{server → server-e2e}/sse-server.ts +60 -33
- package/{server → server-e2e}/static.ts +18 -17
- package/{server → server-e2e}/sync-handlers.ts +64 -30
- package/{server → server-e2e}/ws-server.ts +20 -9
- package/server-managed/activity.ts +269 -0
- package/server-managed/agent-merges.ts +109 -0
- package/server-managed/app-agents.ts +23 -0
- package/server-managed/avatar-store.ts +13 -0
- package/server-managed/blob-store.ts +26 -0
- package/server-managed/brotli.ts +10 -0
- package/server-managed/bundle-advisories.ts +79 -0
- package/server-managed/bundle-advisory-inventory.ts +85 -0
- package/server-managed/bundle-build-diagnostics.d.ts +10 -0
- package/server-managed/bundle-build-diagnostics.js +23 -0
- package/server-managed/bundle-build-leases.ts +41 -0
- package/server-managed/bundle-build-worker.js +380 -0
- package/server-managed/bundle-build.ts +326 -0
- package/server-managed/bundle-cache.ts +261 -0
- package/server-managed/bundle-catalog.ts +29 -0
- package/server-managed/bundle-commits.ts +133 -0
- package/server-managed/bundle-dependency-reviews.ts +91 -0
- package/server-managed/bundle-store.ts +24 -0
- package/server-managed/bundle-summary-cache.ts +139 -0
- package/server-managed/bundle-validation.ts +33 -0
- package/server-managed/bundle.ts +151 -0
- package/server-managed/cache-storage.ts +20 -0
- package/server-managed/cli.js +21 -0
- package/server-managed/combined.ts +43 -0
- package/server-managed/comments.ts +138 -0
- package/server-managed/commit-coauthors.ts +105 -0
- package/server-managed/commit-lockfiles.ts +78 -0
- package/server-managed/comparison.ts +51 -0
- package/server-managed/config.ts +163 -0
- package/server-managed/content-access.ts +15 -0
- package/server-managed/crypto.ts +25 -0
- package/server-managed/db-methods.ts +1915 -0
- package/server-managed/db-neon.ts +398 -0
- package/server-managed/db-schema.ts +263 -0
- package/server-managed/db-table-names.ts +22 -0
- package/server-managed/db.ts +149 -0
- package/server-managed/dependabot-access.ts +61 -0
- package/server-managed/dependabot-alerts.ts +180 -0
- package/server-managed/dependency-review-comments.ts +158 -0
- package/server-managed/dependency-reviews.ts +369 -0
- package/server-managed/github-accounts.ts +59 -0
- package/server-managed/github-actions.ts +71 -0
- package/server-managed/github-app.ts +347 -0
- package/server-managed/github-errors.ts +92 -0
- package/server-managed/github-graphql.ts +42 -0
- package/server-managed/github-issue-links.ts +264 -0
- package/server-managed/github-issue-oauth.ts +30 -0
- package/server-managed/github-issues.ts +157 -0
- package/server-managed/github-link-oauth.ts +56 -0
- package/server-managed/github-links.ts +204 -0
- package/server-managed/github-metadata.ts +213 -0
- package/server-managed/github-oauth.ts +264 -0
- package/server-managed/github-pulls.ts +257 -0
- package/server-managed/github-requests.ts +181 -0
- package/server-managed/github-users.ts +30 -0
- package/server-managed/github-values.ts +26 -0
- package/server-managed/history-windows.ts +172 -0
- package/server-managed/http-response.ts +76 -0
- package/server-managed/http.ts +6327 -0
- package/server-managed/import-triage.ts +51 -0
- package/server-managed/index.ts +204 -0
- package/server-managed/kept-briefly.ts +37 -0
- package/server-managed/kept.ts +28 -0
- package/server-managed/link-reports.ts +149 -0
- package/server-managed/listed-activity.ts +39 -0
- package/server-managed/locked-packages.ts +234 -0
- package/server-managed/lockfile-scope.ts +58 -0
- package/server-managed/lru.ts +30 -0
- package/server-managed/managed-issues.ts +92 -0
- package/server-managed/management-catalog.ts +45 -0
- package/server-managed/management.ts +116 -0
- package/server-managed/npm-delta-worker.js +142 -0
- package/server-managed/npm-delta.ts +277 -0
- package/server-managed/npm-insights.ts +455 -0
- package/server-managed/npm-kept.ts +265 -0
- package/server-managed/npm-loads.ts +259 -0
- package/server-managed/npm-manifest.ts +117 -0
- package/server-managed/npm-packages.ts +469 -0
- package/server-managed/npm-patch-paths.ts +86 -0
- package/server-managed/npm-patch.ts +455 -0
- package/server-managed/npm-provenance.ts +127 -0
- package/server-managed/npm-risk.ts +287 -0
- package/server-managed/object-storage-disk.ts +147 -0
- package/server-managed/object-storage-vercel.ts +71 -0
- package/server-managed/object-storage.ts +82 -0
- package/server-managed/package-entry-points.ts +127 -0
- package/server-managed/pretty-copies.ts +22 -0
- package/server-managed/pretty-format.d.ts +3 -0
- package/server-managed/pretty-format.js +13 -0
- package/server-managed/pretty-print.ts +442 -0
- package/server-managed/public-workspace.ts +243 -0
- package/server-managed/pull-comment-reviews.ts +161 -0
- package/server-managed/pull-dependencies.ts +370 -0
- package/server-managed/pull-dependency-reviews.ts +407 -0
- package/server-managed/pull-reviews.ts +202 -0
- package/server-managed/queue.ts +54 -0
- package/server-managed/release-commits.ts +85 -0
- package/server-managed/rename-diff-worker.js +30 -0
- package/server-managed/rename-diff.ts +237 -0
- package/server-managed/repo-path.ts +21 -0
- package/server-managed/report-migration.ts +32 -0
- package/server-managed/report-opening.ts +77 -0
- package/server-managed/report-query.ts +10 -0
- package/server-managed/report-response.ts +16 -0
- package/server-managed/report-sources.ts +185 -0
- package/server-managed/report-store.ts +77 -0
- package/server-managed/repository-actions.ts +220 -0
- package/server-managed/repository-agents.ts +422 -0
- package/server-managed/repository-aliases.ts +119 -0
- package/server-managed/repository-browser.ts +361 -0
- package/server-managed/repository-changes.ts +280 -0
- package/server-managed/repository-code.ts +222 -0
- package/server-managed/repository-dependency-reviews.ts +182 -0
- package/server-managed/repository-discovery.ts +83 -0
- package/server-managed/repository-history.ts +428 -0
- package/server-managed/repository-issues.ts +293 -0
- package/server-managed/repository-policy.ts +27 -0
- package/server-managed/repository-pulls.ts +207 -0
- package/server-managed/repository-pulse.ts +477 -0
- package/server-managed/repository-risk-assess.ts +108 -0
- package/server-managed/repository-risk-audit.ts +78 -0
- package/server-managed/repository-risk-caches.ts +37 -0
- package/server-managed/repository-risk-read.ts +260 -0
- package/server-managed/repository-risk-stasis.ts +61 -0
- package/server-managed/repository-risk-turn.ts +97 -0
- package/server-managed/repository-risk.ts +516 -0
- package/server-managed/repository-squashes.ts +160 -0
- package/server-managed/repository-stamp.ts +41 -0
- package/server-managed/request-timing.ts +63 -0
- package/server-managed/reviewed-dependencies.ts +170 -0
- package/server-managed/revisions.ts +35 -0
- package/server-managed/scan-downloads.ts +121 -0
- package/server-managed/session.ts +132 -0
- package/server-managed/slugs.ts +38 -0
- package/server-managed/solidity-entry-points.ts +71 -0
- package/server-managed/sql-postgres.ts +34 -0
- package/server-managed/sql.ts +107 -0
- package/server-managed/stasis-config.ts +147 -0
- package/server-managed/stasis-dependencies.ts +221 -0
- package/server-managed/static.ts +28 -0
- package/server-managed/storage-command.ts +16 -0
- package/server-managed/storage-db.ts +183 -0
- package/server-managed/storage-encryption.ts +288 -0
- package/server-managed/storage-maintenance.ts +259 -0
- package/server-managed/storage-payload.ts +121 -0
- package/server-managed/storage-stores.ts +91 -0
- package/server-managed/storage.ts +82 -0
- package/server-managed/team-app-summaries.ts +56 -0
- package/server-managed/team-app.ts +156 -0
- package/server-managed/team-catalog.ts +7 -0
- package/server-managed/team-feed.ts +180 -0
- package/server-managed/team-npm-scopes.ts +103 -0
- package/server-managed/team-reports.ts +368 -0
- package/server-managed/triage-response.ts +16 -0
- package/server-managed/type-only-proof.js +161 -0
- package/server-managed/type-only-worker.js +31 -0
- package/server-managed/type-only.ts +135 -0
- package/server-managed/uploads.ts +106 -0
- package/server-managed/upstream-cache.ts +256 -0
- package/server-managed/view-as.ts +58 -0
- package/server-managed/worker-turns.ts +76 -0
- package/server-managed/workspace-shares.ts +170 -0
- package/server.ts +70 -0
- package/strip-types-loader.js +13 -10
- package/server/index.ts +0 -480
- package/server/lifecycle.ts +0 -208
- /package/{LICENSE → LICENSE-MIT} +0 -0
- /package/{server → server-e2e}/auth.ts +0 -0
- /package/{server → server-e2e}/config.example.json +0 -0
- /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
- /package/{server → server-e2e}/objstore/fs.ts +0 -0
- /package/{server → server-e2e}/objstore/sign.ts +0 -0
- /package/{server → server-e2e}/peer.ts +0 -0
- /package/{server → server-e2e}/sse-session.ts +0 -0
- /package/{server → server-e2e}/util.ts +0 -0
- /package/{server → server-e2e}/validation.ts +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { setImmediate } from 'node:timers/promises'
|
|
2
|
+
import { bundleCommitHash } from '../common/bundle-commit.js'
|
|
3
|
+
import type { BundleSummary } from './bundle-cache.ts'
|
|
4
|
+
import type { ManagedDb } from './db.ts'
|
|
5
|
+
import { type GithubCommit, githubCommitKey } from './github-metadata.ts'
|
|
6
|
+
import { LOGIN, MAX_NAME, timeOf } from './github-values.ts'
|
|
7
|
+
|
|
8
|
+
const MAX_MESSAGE_LENGTH = 65_536
|
|
9
|
+
const BACKFILL_LIMIT = 4
|
|
10
|
+
const RETRY_MS = 5 * 60_000
|
|
11
|
+
|
|
12
|
+
type CommitDetails = Omit<GithubCommit, 'key' | 'fetchedAt'>
|
|
13
|
+
// What viewers see of a cached commit: its subject in place of the message.
|
|
14
|
+
type CatalogCommitDetails = Omit<CommitDetails, 'message'> & { subject: string }
|
|
15
|
+
// What a catalog sends with a bundle whose summary records a commit, when
|
|
16
|
+
// the cache holds its details, tags pointing to it, or both. `github` is the
|
|
17
|
+
// repository they were cached for: the one the bundle is stored at.
|
|
18
|
+
interface BundleCommitInfo { sha: string; github: string; tags: string[]; details: CatalogCommitDetails | null }
|
|
19
|
+
interface CatalogBundle { integrity: string; repoId: number | null; repoFullName: string | null }
|
|
20
|
+
type Summaries = ReadonlyMap<string, { summary: BundleSummary | null }>
|
|
21
|
+
export interface CatalogCommit { repoId: number; sha: string; key: string }
|
|
22
|
+
interface CommitReader { commitDetails(sha: string): Promise<CommitDetails | null> }
|
|
23
|
+
|
|
24
|
+
function truncate(value: string, max: number): string {
|
|
25
|
+
if (value.length <= max) return value
|
|
26
|
+
// Never leave half of a surrogate pair at the end.
|
|
27
|
+
return value.slice(0, /[\uD800-\uDBFF]/u.test(value[max - 1]!) ? max - 1 : max)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function name(value: unknown): string | null {
|
|
31
|
+
return typeof value === 'string' && value.trim() ? truncate(value.trim(), MAX_NAME) : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// `body` is a GitHub list-commits page that starts at `sha`, so its first
|
|
35
|
+
// commit is that one, without the files GET /commits/:sha would add.
|
|
36
|
+
export function parseGithubCommit(sha: string, body: unknown): CommitDetails | null {
|
|
37
|
+
const item = Array.isArray(body) ? body[0] as {
|
|
38
|
+
sha?: unknown; author?: { login?: unknown } | null
|
|
39
|
+
commit?: { message?: unknown; author?: { name?: unknown; date?: unknown } | null; committer?: { date?: unknown } | null }
|
|
40
|
+
} | null : null
|
|
41
|
+
if (item?.sha !== sha || typeof item.commit?.message !== 'string') return null
|
|
42
|
+
const login = item.author?.login
|
|
43
|
+
return {
|
|
44
|
+
message: truncate(item.commit.message, MAX_MESSAGE_LENGTH),
|
|
45
|
+
authorName: name(item.commit.author?.name),
|
|
46
|
+
authorLogin: typeof login === 'string' && LOGIN.test(login) ? login : null,
|
|
47
|
+
authoredAt: timeOf(item.commit.author?.date),
|
|
48
|
+
committedAt: timeOf(item.commit.committer?.date),
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// A commit's subject, as `git log --format=%s` gives it: the message's first
|
|
53
|
+
// paragraph on one line, here without the Claude-Session lines Claude Code
|
|
54
|
+
// adds. Viewers see no more of the message, so catalogs send no more.
|
|
55
|
+
export function commitSubject(message: string): string {
|
|
56
|
+
const paragraph = message.replaceAll('\r\n', '\n').trim().split(/\n[ \t]*\n/u, 1)[0] ?? ''
|
|
57
|
+
return paragraph.split('\n').map(line => line.trim()).filter(line => line && !/^Claude-Session:/iu.test(line)).join(' ')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The commit a bundle's own summary records, in the repository it is stored at.
|
|
61
|
+
function catalogCommit(bundle: CatalogBundle, summaries: Summaries): CatalogCommit | null {
|
|
62
|
+
const sha = bundleCommitHash(summaries.get(bundle.integrity)?.summary?.commit)
|
|
63
|
+
return sha && bundle.repoId != null ? { repoId: bundle.repoId, sha, key: githubCommitKey(bundle.repoId, sha) } : null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Reads only the cache: viewing a bundle never requests its commit's tags,
|
|
67
|
+
// and a commit missing from the cache is left for backfillBundleCommits.
|
|
68
|
+
// Call it before the catalog's final access check: `commitInfo` answers for
|
|
69
|
+
// the bundles of that last snapshot without another await, and has nothing
|
|
70
|
+
// for a commit or repository this read did not cover.
|
|
71
|
+
export async function bundleCommits(db: ManagedDb, bundles: readonly CatalogBundle[], summaries: Summaries) {
|
|
72
|
+
const wanted = new Map<string, CatalogCommit>()
|
|
73
|
+
for (const bundle of bundles) {
|
|
74
|
+
const commit = catalogCommit(bundle, summaries)
|
|
75
|
+
if (commit) wanted.set(commit.key, commit)
|
|
76
|
+
}
|
|
77
|
+
const keys = [...wanted.keys()]
|
|
78
|
+
const details = new Map<string, CatalogCommitDetails>()
|
|
79
|
+
const tags = new Map<string, string[]>()
|
|
80
|
+
if (keys.length > 0) {
|
|
81
|
+
for (const { key, fetchedAt: _fetchedAt, message, ...commit } of await db.listGithubCommits(keys)) {
|
|
82
|
+
details.set(key, { subject: commitSubject(message), ...commit })
|
|
83
|
+
}
|
|
84
|
+
for (const tag of await db.listGithubCommitTags(keys)) tags.set(tag.key, [...tags.get(tag.key) ?? [], tag.name])
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
commitInfo(bundle: CatalogBundle): BundleCommitInfo | null {
|
|
88
|
+
const commit = catalogCommit(bundle, summaries)
|
|
89
|
+
if (!commit || !bundle.repoFullName || (!details.has(commit.key) && !tags.has(commit.key))) return null
|
|
90
|
+
return { sha: commit.sha, github: bundle.repoFullName, tags: tags.get(commit.key) ?? [], details: details.get(commit.key) ?? null }
|
|
91
|
+
},
|
|
92
|
+
missing: [...wanted.values()].filter(commit => !details.has(commit.key)),
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Store the details of a commit, read with the reader's verified repository
|
|
97
|
+
// access, unless the cache already has them. Best effort: true once cached.
|
|
98
|
+
export async function cacheCommitDetails(db: ManagedDb, reader: CommitReader, repoId: number, sha: string): Promise<boolean> {
|
|
99
|
+
const key = githubCommitKey(repoId, sha)
|
|
100
|
+
try {
|
|
101
|
+
if ((await db.listGithubCommits([key])).length > 0) return true
|
|
102
|
+
const details = await reader.commitDetails(sha)
|
|
103
|
+
if (details) await db.setGithubCommits([{ key, ...details, fetchedAt: Date.now() }])
|
|
104
|
+
return details !== null
|
|
105
|
+
} catch { return false }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const active = new Set<string>()
|
|
109
|
+
const retries = new Map<string, number>()
|
|
110
|
+
|
|
111
|
+
// Called after the catalog response is sent: the next one has the details.
|
|
112
|
+
// Reads each commit with the viewer's own repository access, so a viewer who
|
|
113
|
+
// cannot read it on GitHub leaves it for another (retried after a delay for
|
|
114
|
+
// them). Commits never change, so each is stored once and kept.
|
|
115
|
+
export async function backfillBundleCommits(db: ManagedDb, userId: string, missing: readonly CatalogCommit[],
|
|
116
|
+
readerFor: (repoId: number) => Promise<CommitReader | null>): Promise<void> {
|
|
117
|
+
if (missing.length === 0) return
|
|
118
|
+
await setImmediate()
|
|
119
|
+
const now = Date.now()
|
|
120
|
+
for (const [key, retryAt] of retries) if (retryAt <= now) retries.delete(key)
|
|
121
|
+
const batch = missing.filter(commit => !active.has(commit.key) && !retries.has(`${userId}\n${commit.key}`)).slice(0, BACKFILL_LIMIT)
|
|
122
|
+
for (const commit of batch) active.add(commit.key)
|
|
123
|
+
try {
|
|
124
|
+
for (const repoId of new Set(batch.map(commit => commit.repoId))) {
|
|
125
|
+
const reader = await readerFor(repoId).catch(() => null)
|
|
126
|
+
for (const commit of batch.filter(entry => entry.repoId === repoId)) {
|
|
127
|
+
if (!reader || !await cacheCommitDetails(db, reader, repoId, commit.sha)) retries.set(`${userId}\n${commit.key}`, Date.now() + RETRY_MS)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} finally {
|
|
131
|
+
for (const commit of batch) active.delete(commit.key)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// What a bundle's npm dependencies want of review, in a repository that
|
|
2
|
+
// reviews dependencies (repository-dependency-reviews.ts), as its Overview
|
|
3
|
+
// tells it: the package versions it bundles (bundle-advisory-inventory.ts),
|
|
4
|
+
// each as the lockfiles that install it lock it at the commit it was built
|
|
5
|
+
// from, whence their integrities, sources and patches, which reviews are held
|
|
6
|
+
// to (pull-dependency-reviews.ts), judged as the other versions a pull
|
|
7
|
+
// request's lockfiles lock are: by reviews of them whole or compared with any
|
|
8
|
+
// version of their package, else passed by the commit it is reviewed since
|
|
9
|
+
// or the host's reviewed dependencies.
|
|
10
|
+
//
|
|
11
|
+
// Its lockfiles are those its review holds to (lockfile-scope.ts) in its
|
|
12
|
+
// directory or a directory above it, as an install of it reads one there, a
|
|
13
|
+
// workspace's at its root; each version as the nearest of their directories
|
|
14
|
+
// that locks it locks it, every integrity and patch it locks it with. One no
|
|
15
|
+
// lockfile there locks (`unlocked`) can't be reviewed, as there is nothing to
|
|
16
|
+
// hold its reviews to: it fails the review, as one that can't be reviewed
|
|
17
|
+
// does. Where its review holds to none of the lockfiles there, but there are
|
|
18
|
+
// some, its dependencies aren't reviewed (`reviewedIn`).
|
|
19
|
+
import { type CommitLockfiles, lockedIn } from './commit-lockfiles.ts'
|
|
20
|
+
import type { LockedPackage } from './locked-packages.ts'
|
|
21
|
+
import { MAX_LOCKED, type PullDependencies, lockedWith, lockfileFormat } from './pull-dependencies.ts'
|
|
22
|
+
import { MAX_UNREVIEWED, hashesOf } from './pull-dependency-reviews.ts'
|
|
23
|
+
|
|
24
|
+
// A version a bundle holds: its package's name and its version; and, as a
|
|
25
|
+
// Stasis lockfile tells one (stasis-dependencies.ts), how it was installed:
|
|
26
|
+
// its tarball's integrity, and pnpm's hash of the patch applied to it.
|
|
27
|
+
export interface BundledVersion { name: string; version: string; integrity?: string; patch?: string }
|
|
28
|
+
|
|
29
|
+
// The directory a file at `path` is in, '' for the repository's root.
|
|
30
|
+
export const directoryOf = (path: string) => path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''
|
|
31
|
+
// A bundle's directory as one is compared: without the slashes either end.
|
|
32
|
+
const bundleDirectory = (directory: string) => directory.replaceAll(/^\/+|\/+$/gu, '')
|
|
33
|
+
const depth = (dir: string) => dir === '' ? 0 : dir.split('/').length
|
|
34
|
+
|
|
35
|
+
// Whether the lockfile at `path` may install the bundle in `directory`: one
|
|
36
|
+
// in it, or in a directory above it.
|
|
37
|
+
export function installsBundle(path: string, directory: string): boolean {
|
|
38
|
+
const at = bundleDirectory(directory), dir = directoryOf(path)
|
|
39
|
+
return dir === '' || at === dir || at.startsWith(`${dir}/`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Whether `pkg` is locked as `one` was installed, where it tells how: with
|
|
43
|
+
// its tarball's integrity among its own, and its patch, or none.
|
|
44
|
+
const lockedAsInstalled = (one: BundledVersion, pkg: LockedPackage) => one.integrity !== undefined
|
|
45
|
+
&& hashesOf(pkg).includes(one.integrity) && (pkg.patchHash ?? '') === (one.patch ?? '')
|
|
46
|
+
|
|
47
|
+
// The versions `bundled` as `read`'s lockfiles lock them (`locked`), each as
|
|
48
|
+
// the nearest of their directories locking it locks it; one told with how it
|
|
49
|
+
// was installed, as the nearest locking it so does, where one does. One a
|
|
50
|
+
// lockfile installs from a directory of the project's own by no version, as
|
|
51
|
+
// pnpm's does, is locked by its name alone, where none there locks it by its
|
|
52
|
+
// version: a bundle holds it by its package.json's, with no integrity, as
|
|
53
|
+
// Stasis before it tagged one `npm:directory`
|
|
54
|
+
// (common/managed/directory-packages.ts) records it; one installed from an
|
|
55
|
+
// archive, told with its integrity, never so. With those none lock
|
|
56
|
+
// (`unlocked`), how many versions there are (`versions`), and the
|
|
57
|
+
// lockfiles' paths, the nearest first.
|
|
58
|
+
export function lockedVersions(bundled: BundledVersion[], read: CommitLockfiles) {
|
|
59
|
+
const byDirectory = Map.groupBy(read.lockfiles.map(({ path }) => path), directoryOf)
|
|
60
|
+
// Each directory's versions by their name and version, the nearest first.
|
|
61
|
+
const nearest = [...byDirectory].toSorted(([a], [b]) => depth(b) - depth(a)).map(([dir, paths]) =>
|
|
62
|
+
({ paths, locked: Map.groupBy(lockedIn(read, path => directoryOf(path) === dir).packages, pkg => `${pkg.name}@${pkg.version ?? ''}`) }))
|
|
63
|
+
const found = new Map<string, LockedPackage>()
|
|
64
|
+
const unlocked = new Map<string, BundledVersion>()
|
|
65
|
+
for (const one of bundled) {
|
|
66
|
+
const key = `${one.name}@${one.version}`
|
|
67
|
+
const directory = (locked: Map<string, LockedPackage[]>) => one.integrity === undefined ? locked.get(`${one.name}@`)?.filter(pkg => pkg.source === 'file') : undefined
|
|
68
|
+
const locking = nearest.flatMap(({ locked }) => { const locks = locked.get(key) ?? directory(locked); return locks?.length ? [locks] : [] })
|
|
69
|
+
const variants = locking.map(locks => locks.filter(pkg => lockedAsInstalled(one, pkg))).find(locks => locks.length > 0) ?? locking[0]
|
|
70
|
+
if (!variants) { unlocked.set(key, { name: one.name, version: one.version }); continue }
|
|
71
|
+
for (const pkg of variants) if (!found.has(lockedWith(pkg))) found.set(lockedWith(pkg), pkg)
|
|
72
|
+
}
|
|
73
|
+
return { locked: [...found.values()], unlocked: [...unlocked.values()], versions: new Set(bundled.map(one => `${one.name}@${one.version}`)).size,
|
|
74
|
+
paths: nearest.flatMap(({ paths }) => paths) }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The versions `bundled` as `read`'s lockfiles (those installing the bundle,
|
|
78
|
+
// read at `commit`) lock them (lockedVersions), as one directory of a pull
|
|
79
|
+
// request's lockfiles (pull-dependencies.ts) whose versions it adds none of,
|
|
80
|
+
// those past MAX_LOCKED counted (`moreLocked`), and each lockfile left
|
|
81
|
+
// unread a directory of its own failing the review; with those none lock
|
|
82
|
+
// (`unlocked`, as many as MAX_UNREVIEWED, how many more `moreUnlocked`) and
|
|
83
|
+
// how many versions it bundles (`versions`), each once.
|
|
84
|
+
export function bundleDependencies(bundled: BundledVersion[], read: CommitLockfiles, commit: string) {
|
|
85
|
+
const { locked, unlocked, versions, paths } = lockedVersions(bundled, read)
|
|
86
|
+
const told: PullDependencies = { base: commit, head: commit, cut: false, lockfiles: [
|
|
87
|
+
{ paths, added: [], more: 0, locked: locked.slice(0, MAX_LOCKED), moreLocked: Math.max(0, locked.length - MAX_LOCKED) },
|
|
88
|
+
...read.unread.map(path => ({ paths: [path], error: lockfileFormat(path) === 'bun' ? 'lockfile-unsupported' : 'lockfile-unreadable' })),
|
|
89
|
+
] }
|
|
90
|
+
return { told, versions, unlocked: unlocked.slice(0, MAX_UNREVIEWED), moreUnlocked: Math.max(0, unlocked.length - MAX_UNREVIEWED) }
|
|
91
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Sourcemaps are stored once as Brotli. The DB retains the uploaded filename,
|
|
2
|
+
// byte size and integrity so compression does not change report links/dedup.
|
|
3
|
+
import type { Buffer } from 'node:buffer'
|
|
4
|
+
import type { BlobStore, OpenedBlob } from './blob-store.ts'
|
|
5
|
+
import { encodeBrotli } from './brotli.ts'
|
|
6
|
+
|
|
7
|
+
export interface BundleStore {
|
|
8
|
+
put(id: string, bytes: Buffer, kind: string | null): Promise<string | null>
|
|
9
|
+
get(id: string, kind: string | null): Promise<Buffer | null>
|
|
10
|
+
open(id: string, kind: string | null): Promise<OpenedBlob | null>
|
|
11
|
+
delete(id: string): Promise<void>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createBundleStore(archives: BlobStore, sourcemaps: BlobStore): BundleStore {
|
|
15
|
+
const storage = (kind: string | null) => kind === 'sourcemap' ? sourcemaps : archives
|
|
16
|
+
return {
|
|
17
|
+
async put(id, bytes, kind) {
|
|
18
|
+
return storage(kind).put(id, kind === 'sourcemap' ? await encodeBrotli(bytes) : bytes)
|
|
19
|
+
},
|
|
20
|
+
get: (id, kind) => storage(kind).get(id),
|
|
21
|
+
open: (id, kind) => storage(kind).open(id),
|
|
22
|
+
async delete(id) { await Promise.all([archives.delete(id), sourcemaps.delete(id)]) },
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
import { setImmediate } from 'node:timers/promises'
|
|
3
|
+
import { CacheMissError } from './cache-storage.ts'
|
|
4
|
+
import { decodeUtf8 } from '../common/utf8.js'
|
|
5
|
+
import { bundleCommitHash } from '../common/bundle-commit.js'
|
|
6
|
+
import type { OpenedBlob } from './blob-store.ts'
|
|
7
|
+
import type { BundleCacheRecord, BundleCacheStorage, BundleSummary } from './bundle-cache.ts'
|
|
8
|
+
|
|
9
|
+
// Version 3 adds the optional source commit; version 4 counts lines of code
|
|
10
|
+
// without blank lines; version 5 adds a Stasis bundle's format version and
|
|
11
|
+
// versioned package count. Older bundles use bounded backfill; until it
|
|
12
|
+
// reaches one, catalogs serve its version 4 summary, which lacks only those.
|
|
13
|
+
export const SUMMARY_FILENAME = 'v5-summary.json'
|
|
14
|
+
const PREVIOUS_SUMMARY_FILENAME = 'v4-summary.json'
|
|
15
|
+
const RETRY_MS = 5 * 60_000
|
|
16
|
+
const BACKFILL_LIMIT = 4
|
|
17
|
+
type CachedSummary = BundleSummary | { retryAt: number } | { previous: BundleSummary }
|
|
18
|
+
// Whether a summary is kept of a bundle: a Stasis bundle's or a sourcemap's.
|
|
19
|
+
const summarized = (record: BundleCacheRecord) => record.kind === 'stasis' || record.kind === 'sourcemap'
|
|
20
|
+
|
|
21
|
+
async function readCurrentSummary(storage: BundleCacheStorage, id: string): Promise<CachedSummary> {
|
|
22
|
+
try { return await readSummary(storage, id, SUMMARY_FILENAME) }
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (!(error instanceof CacheMissError)) throw error
|
|
25
|
+
const previous = await readSummary(storage, id, PREVIOUS_SUMMARY_FILENAME)
|
|
26
|
+
return 'retryAt' in previous ? previous : { previous }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// A cached file's bytes, read whole and its stream destroyed; null past `max`.
|
|
31
|
+
export async function readCachedBytes(cached: OpenedBlob, max: number): Promise<Buffer | null> {
|
|
32
|
+
try {
|
|
33
|
+
const chunks: Buffer[] = []
|
|
34
|
+
let size = 0
|
|
35
|
+
for await (const chunk of cached.stream) {
|
|
36
|
+
size += chunk.length
|
|
37
|
+
if (size > max) return null
|
|
38
|
+
chunks.push(Buffer.from(chunk))
|
|
39
|
+
}
|
|
40
|
+
return Buffer.concat(chunks)
|
|
41
|
+
} finally { cached.stream.destroy() }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function readSummary(storage: BundleCacheStorage, id: string, file: string): Promise<BundleSummary | { retryAt: number }> {
|
|
45
|
+
const bytes = await readCachedBytes(await storage.open(id, file), 1024)
|
|
46
|
+
if (!bytes) throw new Error('Invalid bundle summary')
|
|
47
|
+
const summary = JSON.parse(decodeUtf8(bytes)) as BundleSummary | { retryAt: number }
|
|
48
|
+
if (summary && 'retryAt' in summary && Number.isSafeInteger(summary.retryAt) && summary.retryAt > 0) return summary
|
|
49
|
+
const counts = summary as BundleSummary
|
|
50
|
+
if (!counts || ![counts.files, counts.codeFiles, counts.lines].every(value => Number.isSafeInteger(value) && value >= 0)
|
|
51
|
+
|| counts.codeFiles > counts.files || (counts.commit !== undefined && !bundleCommitHash(counts.commit))
|
|
52
|
+
|| [counts.stasisVersion, counts.versionedPackages].some(value => value !== undefined && !(Number.isSafeInteger(value) && value >= 0))) throw new Error('Invalid bundle summary')
|
|
53
|
+
return counts
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createBundleSummaryCache(storage: BundleCacheStorage, build: (record: BundleCacheRecord) => Promise<BundleSummary>, exists: (id: string) => Promise<unknown>) {
|
|
57
|
+
const entries = new Map<string, { id: string; value: CachedSummary }>()
|
|
58
|
+
const reads = new Map<string, { id: string; job: Promise<CachedSummary | null> }>()
|
|
59
|
+
const active = new Map<string, Promise<void>>()
|
|
60
|
+
let pending: Promise<void> | null = null
|
|
61
|
+
function remember(record: BundleCacheRecord, value: CachedSummary) {
|
|
62
|
+
// A completed build supersedes any older storage read still in flight.
|
|
63
|
+
reads.delete(record.integrity)
|
|
64
|
+
// Summaries are tiny and immutable by integrity. Refuse overflow admission
|
|
65
|
+
// instead of evicting the next hit during a scan larger than the cache.
|
|
66
|
+
if (entries.size >= 4096 && !entries.has(record.integrity)) return
|
|
67
|
+
entries.set(record.integrity, { id: record.id, value })
|
|
68
|
+
}
|
|
69
|
+
function read(record: BundleCacheRecord): Promise<CachedSummary | null> {
|
|
70
|
+
const known = entries.get(record.integrity)
|
|
71
|
+
if (known) return Promise.resolve(known.value)
|
|
72
|
+
const existing = reads.get(record.integrity)
|
|
73
|
+
if (existing) return existing.job
|
|
74
|
+
// Concurrent catalogs share the decoded summary, never the response stream.
|
|
75
|
+
// Only in-flight work is shared: misses and failures remain retryable.
|
|
76
|
+
const job: Promise<CachedSummary | null> = readCurrentSummary(storage, record.id).then(value => {
|
|
77
|
+
if (reads.get(record.integrity)?.job === job) remember(record, value)
|
|
78
|
+
return entries.get(record.integrity)?.value ?? value
|
|
79
|
+
}).catch(error => {
|
|
80
|
+
if (error instanceof CacheMissError) return null
|
|
81
|
+
throw error
|
|
82
|
+
}).finally(() => { if (reads.get(record.integrity)?.job === job) reads.delete(record.integrity) })
|
|
83
|
+
reads.set(record.integrity, { id: record.id, job })
|
|
84
|
+
return job
|
|
85
|
+
}
|
|
86
|
+
async function publish(record: BundleCacheRecord, value: CachedSummary) {
|
|
87
|
+
if (!await exists(record.id)) return
|
|
88
|
+
await storage.put(record.id, SUMMARY_FILENAME, Buffer.from(JSON.stringify(value)))
|
|
89
|
+
if (!await exists(record.id)) { await storage.delete(record.id); return }
|
|
90
|
+
remember(record, value)
|
|
91
|
+
}
|
|
92
|
+
async function buildOne(record: BundleCacheRecord) {
|
|
93
|
+
try { await publish(record, await build(record)) }
|
|
94
|
+
catch { await publish(record, { retryAt: Date.now() + RETRY_MS }).catch(() => {}) }
|
|
95
|
+
}
|
|
96
|
+
async function summaryStatus(record: BundleCacheRecord) {
|
|
97
|
+
const value = summarized(record) ? await read(record) : null
|
|
98
|
+
if (!value || 'retryAt' in value) return { summary: null, summaryRetryAt: value?.retryAt ?? null }
|
|
99
|
+
return { summary: 'previous' in value ? value.previous : value, summaryRetryAt: null }
|
|
100
|
+
}
|
|
101
|
+
async function backfill(records: readonly BundleCacheRecord[]) {
|
|
102
|
+
// Start after the catalog has been sent. Keep this work awaited by the
|
|
103
|
+
// request owner (including serverless), with no detached tasks or backlog.
|
|
104
|
+
await setImmediate()
|
|
105
|
+
let attempts = 0
|
|
106
|
+
for (const record of records) {
|
|
107
|
+
if (!summarized(record)) continue
|
|
108
|
+
const cached = await read(record).catch(() => null)
|
|
109
|
+
if (cached && !('previous' in cached) && (!('retryAt' in cached) || cached.retryAt > Date.now())) continue
|
|
110
|
+
if (!await exists(record.id)) continue
|
|
111
|
+
if (attempts++ >= BACKFILL_LIMIT) break
|
|
112
|
+
const job = buildOne(record)
|
|
113
|
+
active.set(record.id, job)
|
|
114
|
+
try { await job } finally { active.delete(record.id) }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
remember,
|
|
119
|
+
summaryStatus,
|
|
120
|
+
async summary(record: BundleCacheRecord): Promise<BundleSummary | null> {
|
|
121
|
+
return (await summaryStatus(record)).summary
|
|
122
|
+
},
|
|
123
|
+
backfill(records: readonly BundleCacheRecord[]) {
|
|
124
|
+
if (pending) return Promise.resolve() // Another catalog already owns the bounded batch.
|
|
125
|
+
const job = backfill(records).finally(() => { if (pending === job) pending = null })
|
|
126
|
+
pending = job
|
|
127
|
+
return job
|
|
128
|
+
},
|
|
129
|
+
async forget(id: string) {
|
|
130
|
+
// Only this bundle can republish its cache during deletion. A stalled
|
|
131
|
+
// backfill elsewhere must not delay removing these bytes or responding.
|
|
132
|
+
await active.get(id)
|
|
133
|
+
for (const [hash, entry] of entries) if (entry.id === id) entries.delete(hash)
|
|
134
|
+
// Do not wait for remote reads during deletion. Their callers still own
|
|
135
|
+
// and drain them, but a late response must not repopulate this cache.
|
|
136
|
+
for (const [hash, entry] of reads) if (entry.id === id) reads.delete(hash)
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// A Stasis bundle validated (GET and POST /api/bundles/:id/validation) by
|
|
2
|
+
// Stasis's own validation (`@exodus/stasis/validate`; its rules are in
|
|
3
|
+
// Stasis's doc/validate.md), the repository read the app's way: with its
|
|
4
|
+
// reader's GitHub access, own code by git's blob ids of the commit's listing.
|
|
5
|
+
import { type BundleValidation, type ValidationPlan, followingMoves, validateBundle, validateRepository } from '@exodus/stasis/validate'
|
|
6
|
+
import { createClient } from '@preventive/upstream/github.js'
|
|
7
|
+
import { upstreamGithub } from './github-errors.ts'
|
|
8
|
+
import { USER_AGENT } from './github-requests.ts'
|
|
9
|
+
|
|
10
|
+
export { type BundleValidation, type PlannedBundle, validationPlan } from '@exodus/stasis/validate'
|
|
11
|
+
|
|
12
|
+
// The rules a validation was made by, kept with it: one made by others is made again.
|
|
13
|
+
export const BUNDLE_VALIDATION_READ = 3
|
|
14
|
+
|
|
15
|
+
type Options = { deadline?: number }
|
|
16
|
+
|
|
17
|
+
// The bundle `plan`'s dependencies alone, held to their registry, own code unverified for `reason`.
|
|
18
|
+
export const validateDependencies = (plan: ValidationPlan, reason: string, options: Options = {}): Promise<BundleValidation> =>
|
|
19
|
+
validateBundle(plan, { reason }, options)
|
|
20
|
+
|
|
21
|
+
// The bundle `plan` validated at `commit` of `fullName`, read with `token`,
|
|
22
|
+
// built in `directory`: own code held to git's blob ids of the commit's tree
|
|
23
|
+
// as upstream lists it (each listing held to its tree's id), the commit to the
|
|
24
|
+
// repository's own history, the dependencies to the lockfiles installing it;
|
|
25
|
+
// what can't be held to told in the report (own code's `reason`,
|
|
26
|
+
// `lockfileError`), not thrown.
|
|
27
|
+
export function validateAtCommit(plan: ValidationPlan, { fullName, token, commit, directory }: { fullName: string; token: string; commit: string; directory: string },
|
|
28
|
+
options: Options = {}): Promise<BundleValidation> {
|
|
29
|
+
const client = createClient({ token, userAgent: USER_AGENT })
|
|
30
|
+
const repository = followingMoves(client, fullName)
|
|
31
|
+
return upstreamGithub(() => validateRepository(plan, { repository, commit, directory },
|
|
32
|
+
{ ...options, strict: false, listTree: paths => client.listRepoTree({ repo: repository.name(), sha: commit, paths }) }))
|
|
33
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Bundle helpers for the managed server: the content-addressed identity used to
|
|
2
|
+
// dedupe stored bundles and to auto-link reports to them.
|
|
3
|
+
//
|
|
4
|
+
// A report's top-level `bundleHashes` lists the `sha512-<base64>` integrities
|
|
5
|
+
// the analyzer ran against (see ui/view/ingest.js); a stored bundle's identity
|
|
6
|
+
// is that same integrity computed from its bytes. Matching the two is how a
|
|
7
|
+
// report auto-links to its bundle.
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
import type { Buffer } from 'node:buffer'
|
|
10
|
+
import { StringDecoder } from 'node:string_decoder'
|
|
11
|
+
import { brotliDecompress, createBrotliDecompress, createGunzip } from 'node:zlib'
|
|
12
|
+
import { promisify } from 'node:util'
|
|
13
|
+
import { unpackStream } from '@preventive/archive/tar.js'
|
|
14
|
+
import { parseBundleContents } from '../common/bundle-metadata.js'
|
|
15
|
+
import { isStasisBundleName, isStasisTgzName } from '../common/stasis-files.js'
|
|
16
|
+
import { parseStasisTgz } from '../common/stasis-tgz.js'
|
|
17
|
+
import { bundleFilesAsMap } from '../common/bundle-sources.js'
|
|
18
|
+
import { commonFileDirectory } from '../common/managed/repository-alias.ts'
|
|
19
|
+
import { decodeUtf8 } from '../common/utf8.js'
|
|
20
|
+
|
|
21
|
+
// `sha512-<base64>` identity for a bundle's bytes. MUST stay byte-identical to
|
|
22
|
+
// the client's common/integrity.js (SHA-512 → standard base64 WITH padding) so
|
|
23
|
+
// a report's `bundleHashes` entries match a stored bundle's computed integrity.
|
|
24
|
+
// (The client uses `Uint8Array.toBase64()`, whose default is standard base64 +
|
|
25
|
+
// padding — the same bytes node:crypto's base64 digest emits.)
|
|
26
|
+
export function bundleIntegrity(bytes: Buffer): string {
|
|
27
|
+
return `sha512-${createHash('sha512').update(bytes).digest('base64')}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Classify an uploaded bundle by filename — mirrors ui/view/ingest.js
|
|
31
|
+
// `bundleKind`: sourcemap (.map) or stasis (stasis.code.br / .stasis.code.br,
|
|
32
|
+
// stasis.tgz / .stasis.tgz). null for anything else (stored anyway; the kind
|
|
33
|
+
// is informational). A stasis bundle's format is told by its name, as Stasis
|
|
34
|
+
// tells it (isStasisTgzName).
|
|
35
|
+
export function bundleKind(filename: string): 'sourcemap' | 'stasis' | null {
|
|
36
|
+
// Browsers insert a duplicate-download counter before the final extension.
|
|
37
|
+
const lower = filename.toLowerCase().replace(/ \(\d+\)(?=\.[^.]*$)/u, '')
|
|
38
|
+
if (lower.endsWith('.map')) return 'sourcemap'
|
|
39
|
+
if (isStasisBundleName(lower)) return 'stasis'
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface BundleRepo { github?: unknown; directory?: unknown }
|
|
44
|
+
const HEADER_KEYS = new Set(['version', 'config', 'repo', 'package'])
|
|
45
|
+
const MAX_HEADER_BYTES = 64 * 1024
|
|
46
|
+
|
|
47
|
+
// Stasis writes origin metadata before entries, formats and source bodies.
|
|
48
|
+
// undefined means another chunk is needed; null means the header has no repo.
|
|
49
|
+
function headerRepo(text: string): BundleRepo | null | undefined {
|
|
50
|
+
let depth = 0, escaped = false, expectKey = false, quoted = false
|
|
51
|
+
let key = '', start = 0, valueStart = 0
|
|
52
|
+
for (let i = 0; i < text.length; i++) {
|
|
53
|
+
const c = text[i]
|
|
54
|
+
if (quoted) {
|
|
55
|
+
if (escaped) { escaped = false; continue }
|
|
56
|
+
if (c === '\\') { escaped = true; continue }
|
|
57
|
+
if (c !== '"') continue
|
|
58
|
+
quoted = false
|
|
59
|
+
if (depth === 1 && expectKey) {
|
|
60
|
+
key = JSON.parse(text.slice(start, i + 1)) as string
|
|
61
|
+
if (!HEADER_KEYS.has(key)) return null
|
|
62
|
+
expectKey = false
|
|
63
|
+
}
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
if (c === '"') { quoted = true; start = i; continue }
|
|
67
|
+
if (c === '{' || c === '[') {
|
|
68
|
+
if (depth === 0 && c !== '{') return null
|
|
69
|
+
depth++
|
|
70
|
+
if (depth === 1) expectKey = true
|
|
71
|
+
} else if (depth === 1 && (c === ',' || c === '}')) {
|
|
72
|
+
if (key === 'repo') return plainRepo(JSON.parse(text.slice(valueStart, i)))
|
|
73
|
+
if (c === '}') return null
|
|
74
|
+
expectKey = true
|
|
75
|
+
} else if (c === '}' || c === ']') depth--
|
|
76
|
+
else if (depth === 1 && c === ':') valueStart = i + 1
|
|
77
|
+
else if (depth === 0 && c?.trim()) return null
|
|
78
|
+
}
|
|
79
|
+
return undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const plainRepo = (repo: unknown) => repo != null && typeof repo === 'object' && !Array.isArray(repo) ? repo as BundleRepo : null
|
|
83
|
+
|
|
84
|
+
// A .stasis.tgz's repo, as its stasis.lock.json records it: the first entry
|
|
85
|
+
// of its tar, as Stasis requires one (Bundle.fromTar refuses a tar it isn't
|
|
86
|
+
// first in), read alone, its gunzipped bytes bounded as the lockfile's own are.
|
|
87
|
+
const MAX_TGZ_LOCKFILE_BYTES = 64 * 1024 * 1024
|
|
88
|
+
async function tgzRepo(bytes: Buffer): Promise<BundleRepo | null> {
|
|
89
|
+
const stream = createGunzip({ chunkSize: 64 * 1024 })
|
|
90
|
+
stream.end(bytes)
|
|
91
|
+
async function* bounded() {
|
|
92
|
+
let size = 0
|
|
93
|
+
for await (const chunk of stream) {
|
|
94
|
+
size += (chunk as Buffer).length
|
|
95
|
+
if (size > MAX_TGZ_LOCKFILE_BYTES) throw new Error('Lockfile too large')
|
|
96
|
+
yield chunk as Buffer
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
for await (const entry of unpackStream(bounded())) {
|
|
101
|
+
if (entry.name !== 'stasis.lock.json') return null
|
|
102
|
+
return plainRepo((JSON.parse(decodeUtf8(entry.data)) as { repo?: unknown } | null)?.repo)
|
|
103
|
+
}
|
|
104
|
+
return null
|
|
105
|
+
} catch { return null }
|
|
106
|
+
finally { stream.destroy() }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Decode only a bounded prefix, stopping as soon as repo is read or the
|
|
110
|
+
// header ends: a stasis.code.br's, but where `filename` names a .stasis.tgz,
|
|
111
|
+
// its stasis.lock.json (tgzRepo). Unknown or malformed archives remain uploadable as opaque bytes.
|
|
112
|
+
export async function bundleRepo(bytes: Buffer, filename = ''): Promise<BundleRepo | null> {
|
|
113
|
+
if (isStasisTgzName(filename)) return tgzRepo(bytes)
|
|
114
|
+
const stream = createBrotliDecompress({ chunkSize: 4096 })
|
|
115
|
+
const decoder = new StringDecoder('utf8')
|
|
116
|
+
let size = 0, text = ''
|
|
117
|
+
stream.end(bytes)
|
|
118
|
+
try {
|
|
119
|
+
for await (const chunk of stream) {
|
|
120
|
+
size += chunk.length
|
|
121
|
+
if (size > MAX_HEADER_BYTES) return null
|
|
122
|
+
text += decoder.write(chunk)
|
|
123
|
+
const repo = headerRepo(text)
|
|
124
|
+
if (repo !== undefined) return repo
|
|
125
|
+
}
|
|
126
|
+
return null
|
|
127
|
+
} catch { return null }
|
|
128
|
+
finally { stream.destroy() }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const decompress = promisify(brotliDecompress)
|
|
132
|
+
// Same decoded size bound as the bundle metadata cache; only reached when an
|
|
133
|
+
// alias can use a shared file suffix. Invalid/opaque bundles keep header defaults.
|
|
134
|
+
export async function bundleFilePrefix(bytes: Buffer, filename = ''): Promise<string> {
|
|
135
|
+
try {
|
|
136
|
+
const identity = { integrity: '', kind: 'stasis', size: bytes.length }
|
|
137
|
+
const details = isStasisTgzName(filename) ? { ...identity, bundle: await parseStasisTgz(bytes) }
|
|
138
|
+
: parseBundleContents(decodeUtf8(await decompress(bytes, { maxOutputLength: 512 * 1024 * 1024 })), identity)
|
|
139
|
+
return commonFileDirectory(bundleFilesAsMap(details).keys())
|
|
140
|
+
} catch { return '' }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Extract the bundle integrities a report declares (its top-level
|
|
144
|
+
// `bundleHashes`), from the JSON it parsed to (readManagedReport's, as is).
|
|
145
|
+
// Non-JSON reports (markdown / CSV, given as null) or a missing field → [].
|
|
146
|
+
export function reportBundleHashes(data: unknown): string[] {
|
|
147
|
+
if (data == null || typeof data !== 'object') return []
|
|
148
|
+
const hashes = (data as { bundleHashes?: unknown }).bundleHashes
|
|
149
|
+
if (!Array.isArray(hashes)) return []
|
|
150
|
+
return hashes.filter((x): x is string => typeof x === 'string' && x !== '')
|
|
151
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Buffer } from 'node:buffer'
|
|
2
|
+
import type { OpenedBlob } from './blob-store.ts'
|
|
3
|
+
|
|
4
|
+
export class CacheMissError extends Error {
|
|
5
|
+
constructor() { super('Managed cache unavailable') }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Keys are internal relative paths; delete removes a whole directory of
|
|
9
|
+
// variants, but for one directory within it where `keep` names one.
|
|
10
|
+
export interface CacheStorage {
|
|
11
|
+
exists(key: string): Promise<boolean>
|
|
12
|
+
put(key: string, bytes: Buffer): Promise<void>
|
|
13
|
+
open(key: string): Promise<OpenedBlob>
|
|
14
|
+
delete(prefix: string, keep?: string): Promise<void>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function validateCacheKey(key: string): string {
|
|
18
|
+
if (!key.split('/').every(part => /^[a-z0-9][a-z0-9.-]*$/iu.test(part))) throw new Error('Invalid cache key')
|
|
19
|
+
return key
|
|
20
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Executable entry for the managed-server bin. See server-e2e/cli.js for the
|
|
3
|
+
// full rationale: Node refuses to strip TypeScript types from files under
|
|
4
|
+
// node_modules, so once this package is installed as a dependency
|
|
5
|
+
// server-managed/index.ts can't be executed directly. Register the in-thread
|
|
6
|
+
// type-stripping hook FIRST, then load the server via a dynamic import (a
|
|
7
|
+
// static import would be fetched before the hook is active) and start it.
|
|
8
|
+
//
|
|
9
|
+
// Imported here rather than run as the process entry, so index.ts's
|
|
10
|
+
// `import.meta.main` auto-start gate stays off and we call `start()` ourselves.
|
|
11
|
+
import '../strip-types-loader.js'
|
|
12
|
+
const command = process.argv[2]
|
|
13
|
+
if (command === '--storage-encryption-status') {
|
|
14
|
+
const { storageCommand } = await import('./storage-command.ts')
|
|
15
|
+
await storageCommand(process.argv[3])
|
|
16
|
+
} else if (command) {
|
|
17
|
+
throw new Error(`Unknown command: ${command}`)
|
|
18
|
+
} else {
|
|
19
|
+
const { start } = await import('./index.ts')
|
|
20
|
+
await start()
|
|
21
|
+
}
|