@preventive/triage 1.0.0-alpha.9 → 1.0.0-alpha.90
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 +378 -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 +80 -0
- package/common/default-scan-models.ts +37 -0
- package/common/dependency-reviews.d.ts +19 -0
- package/common/dependency-reviews.js +67 -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 +3 -0
- package/common/github-issue-labels.js +42 -0
- package/common/github-pr.ts +56 -0
- package/common/gzip.js +59 -0
- package/common/line-diff.d.ts +15 -0
- package/common/line-diff.js +188 -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 +48 -0
- package/common/managed/routes.d.ts +2 -0
- package/common/managed/routes.js +386 -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/npm-token.d.ts +2 -0
- package/common/npm-token.js +14 -0
- package/common/pretty-json.js +123 -0
- package/common/pretty-kinds.js +20 -0
- package/common/pretty-print.d.ts +7 -0
- package/common/pretty-print.js +41 -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/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-3DOSBILK.js +4 -0
- package/out/chunk-55YIWY3H.js +5 -0
- package/out/chunk-5AVGKFMR.js +1 -0
- package/out/chunk-5PWEAEA5.js +1 -0
- package/out/chunk-6VOB5WXX.js +2 -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-A4BBBHQU.js +1 -0
- package/out/chunk-B64CTMVU.js +4 -0
- package/out/chunk-BEKYZCPX.js +14 -0
- package/out/chunk-BZJU5EEV.js +1 -0
- package/out/chunk-C4Z6ZBXV.js +10 -0
- package/out/chunk-CJVMNJDZ.js +3 -0
- package/out/chunk-CU3FPB3P.js +1 -0
- package/out/chunk-DEVAORP2.js +1 -0
- package/out/chunk-DUASGS2O.js +8 -0
- package/out/chunk-EEJA7JBI.js +13 -0
- package/out/chunk-ESXA7KID.js +1 -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-HGWFRZNC.js +267 -0
- package/out/chunk-IYPY47MC.js +1 -0
- package/out/chunk-JZAROLQT.js +40 -0
- package/out/chunk-K4XJHM2T.js +1 -0
- package/out/chunk-KP2XM324.js +6 -0
- package/out/chunk-LRWB6QVO.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-OOJM4CTU.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-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-RAM3GDAD.js +1 -0
- package/out/chunk-RPNO56H2.js +1 -0
- package/out/chunk-SMNFBOLO.js +10 -0
- package/out/chunk-SSPDMA6D.js +1 -0
- package/out/chunk-SZTCJVZJ.js +55 -0
- package/out/chunk-SZWPYLPC.js +1 -0
- package/out/chunk-TKPNRQNN.js +3 -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-UTYWTUTC.js +1 -0
- package/out/chunk-VHEXSGGP.js +1 -0
- package/out/chunk-VYYUMYVW.js +9 -0
- package/out/chunk-WBK5ZN4K.js +3 -0
- package/out/chunk-WCDYSIKG.js +1 -0
- package/out/chunk-XM4RLC4A.js +2 -0
- package/out/chunk-XRAAYXPR.js +1 -0
- package/out/chunk-ZQHLECTI.js +1 -0
- package/out/client-managed-import.js +4 -0
- package/out/client-managed.js +35 -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 -5
- package/out/index.html +105 -25
- 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 -148
- package/package.json +416 -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 +420 -0
- package/risk/files/source-map-link.js +43 -0
- package/risk/files/stamps.js +47 -0
- package/risk/index.d.ts +309 -0
- package/risk/index.js +12 -0
- package/risk/npm/assess.js +303 -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 +401 -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 +52 -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 +3 -3
- package/{server → server-e2e}/static.ts +18 -17
- package/{server → server-e2e}/sync-handlers.ts +56 -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 +21 -0
- package/server-managed/blob-store.ts +26 -0
- package/server-managed/brotli.ts +10 -0
- package/server-managed/bundle-advisories.ts +176 -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 +387 -0
- package/server-managed/bundle-build.ts +328 -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 +1950 -0
- package/server-managed/db-neon.ts +401 -0
- package/server-managed/db-schema.ts +271 -0
- package/server-managed/db-table-names.ts +22 -0
- package/server-managed/db.ts +150 -0
- package/server-managed/dependabot-access.ts +85 -0
- package/server-managed/dependabot-alerts.ts +197 -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 +169 -0
- package/server-managed/github-link-oauth.ts +56 -0
- package/server-managed/github-links.ts +204 -0
- package/server-managed/github-metadata.ts +218 -0
- package/server-managed/github-oauth.ts +264 -0
- package/server-managed/github-pulls.ts +290 -0
- package/server-managed/github-requests.ts +181 -0
- package/server-managed/github-users.ts +53 -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 +6722 -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/known-listings.ts +112 -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 +203 -0
- package/server-managed/npm-delta.ts +277 -0
- package/server-managed/npm-insights.ts +455 -0
- package/server-managed/npm-kept.ts +274 -0
- package/server-managed/npm-loads.ts +259 -0
- package/server-managed/npm-manifest.ts +117 -0
- package/server-managed/npm-packages.ts +477 -0
- package/server-managed/npm-patch-paths.ts +86 -0
- package/server-managed/npm-patch.ts +457 -0
- package/server-managed/npm-provenance.ts +127 -0
- package/server-managed/npm-risk.ts +310 -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 +4 -0
- package/server-managed/pretty-format.js +27 -0
- package/server-managed/pretty-print.ts +444 -0
- package/server-managed/public-workspace.ts +242 -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 +458 -0
- package/server-managed/pull-reviews.ts +202 -0
- package/server-managed/pulse-walk.ts +164 -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 +362 -0
- package/server-managed/repository-changes.ts +280 -0
- package/server-managed/repository-code.ts +222 -0
- package/server-managed/repository-dependency-reviews.ts +220 -0
- package/server-managed/repository-discovery.ts +83 -0
- package/server-managed/repository-history.ts +444 -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 +409 -0
- package/server-managed/repository-risk-assess.ts +179 -0
- package/server-managed/repository-risk-audit.ts +145 -0
- package/server-managed/repository-risk-caches.ts +56 -0
- package/server-managed/repository-risk-kept.ts +86 -0
- package/server-managed/repository-risk-read.ts +296 -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 +528 -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 +280 -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 +57 -0
- package/server-managed/team-app.ts +162 -0
- package/server-managed/team-catalog.ts +7 -0
- package/server-managed/team-feed.ts +180 -0
- package/server-managed/team-icons.ts +50 -0
- package/server-managed/team-npm-scopes.ts +103 -0
- package/server-managed/team-progress.ts +77 -0
- package/server-managed/team-reports.ts +369 -0
- package/server-managed/triage-response.ts +16 -0
- package/server-managed/type-only-proof.js +181 -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 +275 -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,86 @@
|
|
|
1
|
+
// Paths as git writes them in a diff's headers (npm-patch.ts parsePatch):
|
|
2
|
+
// quoted as C writes a string where they need it, prefixed `a/` and `b/`.
|
|
3
|
+
import { Buffer } from 'node:buffer'
|
|
4
|
+
import { NPM_PATH_FOLDS } from '../risk/index.js'
|
|
5
|
+
import { NpmPackageError } from './npm-packages.ts'
|
|
6
|
+
|
|
7
|
+
const unreadable = () => new NpmPackageError(422, 'patch-unreadable')
|
|
8
|
+
|
|
9
|
+
// A path as git writes it, unquoted where quoted (as C writes a string, its
|
|
10
|
+
// bytes past ASCII in octal); null where it is quoted otherwise.
|
|
11
|
+
const ESCAPES: Record<string, number> = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
|
|
12
|
+
export function unquoted(path: string): string | null {
|
|
13
|
+
if (!path.startsWith('"')) return path
|
|
14
|
+
const bytes: number[] = []
|
|
15
|
+
for (let at = 1; at < path.length; at++) {
|
|
16
|
+
const char = path[at]!
|
|
17
|
+
if (char === '"') return at === path.length - 1 ? new TextDecoder().decode(Uint8Array.from(bytes)) : null
|
|
18
|
+
if (char !== '\\') { bytes.push(...Buffer.from(char)); continue }
|
|
19
|
+
const next = path[++at] ?? ''
|
|
20
|
+
const octal = /^[0-3][0-7]{2}/u.exec(path.slice(at))?.[0]
|
|
21
|
+
if (octal) { bytes.push(Number.parseInt(octal, 8)); at += 2 }
|
|
22
|
+
else if (next in ESCAPES) bytes.push(ESCAPES[next]!)
|
|
23
|
+
else return null
|
|
24
|
+
}
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A side's path in the package, as a `---` or `+++` line names it after its
|
|
29
|
+
// prefix (`a/` or `b/`), git's tab after one with a space in it left out;
|
|
30
|
+
// null for none (`/dev/null`).
|
|
31
|
+
export function sidePath(named: string, prefix: string): string | null {
|
|
32
|
+
const path = unquoted(named.endsWith('\t') ? named.slice(0, -1) : named)
|
|
33
|
+
if (path === '/dev/null') return null
|
|
34
|
+
if (path === null || !path.startsWith(prefix)) throw unreadable()
|
|
35
|
+
return path.slice(prefix.length)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The paths a `diff --git` line names, where they are the same, as git
|
|
39
|
+
// writes them for a file neither renamed nor copied: `a/<path> b/<path>`.
|
|
40
|
+
export function gitPath(named: string): string | null {
|
|
41
|
+
if (named.startsWith('"')) {
|
|
42
|
+
const quoted = /^("(?:[^"\\]|\\.)*") ("(?:[^"\\]|\\.)*")$/u.exec(named)
|
|
43
|
+
const [a, b] = quoted ? [unquoted(quoted[1]!), unquoted(quoted[2]!)] : []
|
|
44
|
+
return a?.startsWith('a/') && b?.startsWith('b/') && a.slice(2) === b.slice(2) ? a.slice(2) : null
|
|
45
|
+
}
|
|
46
|
+
const length = (named.length - 5) / 2
|
|
47
|
+
const path = named.slice(2, 2 + length)
|
|
48
|
+
return Number.isInteger(length) && named.startsWith('a/') && named.slice(2 + length) === ` b/${path}` ? path : null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Whether a `diff --git` line's paths (`rest`, past `diff --git `) are
|
|
52
|
+
// `before` and `after`, as git writes them, either quoted or not.
|
|
53
|
+
export function namesBoth(rest: string, before: string, after: string): boolean {
|
|
54
|
+
const first = rest.startsWith('"') ? /^"(?:[^"\\]|\\.)*"/u.exec(rest)?.[0] : `a/${before}`
|
|
55
|
+
if (first === undefined || !rest.startsWith(`${first} `)) return false
|
|
56
|
+
const second = rest.slice(first.length + 1)
|
|
57
|
+
return unquoted(first) === `a/${before}` && unquoted(second) === `b/${after}`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A path every pnpm takes as written: none of its names starts or ends in
|
|
61
|
+
// whitespace, which pnpm 10 and 11 trim off a path, or is `node_modules`, in
|
|
62
|
+
// which pnpm 12 may leave out a file a patch creates.
|
|
63
|
+
export const isPlainPath = (path: string) => path.split('/').every(name => name === name.trim() && name.toLowerCase() !== 'node_modules')
|
|
64
|
+
|
|
65
|
+
// Takes each path a patch creates or renames a file to, refusing one macOS or
|
|
66
|
+
// Windows may take for another spelled otherwise (NPM_PATH_FOLDS): one of the
|
|
67
|
+
// package's `paths`, a directory of one, or one taken before. So `License`
|
|
68
|
+
// beside `LICENSE`, or `Lib/a.js` beside `lib/`, which pnpm versions may
|
|
69
|
+
// write over the other, or into it.
|
|
70
|
+
export function pathClaims(paths: Iterable<string>): (path: string) => void {
|
|
71
|
+
// By each fold, the spelling of each path and directory; '' for several.
|
|
72
|
+
const spelled = NPM_PATH_FOLDS.map(() => new Map<string, string>())
|
|
73
|
+
const take = (path: string, claim: boolean) => {
|
|
74
|
+
for (let at = path.indexOf('/'); ; at = path.indexOf('/', at + 1)) {
|
|
75
|
+
const name = at === -1 ? path : path.slice(0, at)
|
|
76
|
+
for (const [i, fold] of NPM_PATH_FOLDS.entries()) {
|
|
77
|
+
const key = fold(name), was = spelled[i]!.get(key)
|
|
78
|
+
if (claim && was !== undefined && was !== name) throw unreadable()
|
|
79
|
+
spelled[i]!.set(key, was === undefined || was === name ? name : '')
|
|
80
|
+
}
|
|
81
|
+
if (at === -1) return
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const path of paths) take(path, false)
|
|
85
|
+
return path => take(path, true)
|
|
86
|
+
}
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
// A pnpm patch of an npm package version (patchedDependencies), as its
|
|
2
|
+
// dependency review reads it (pull-dependency-reviews.ts): its file, which a
|
|
3
|
+
// lockfile names (locked-packages.ts `patchPath`), or which is found where it
|
|
4
|
+
// names it by its hash alone (withPatchFiles), or by its hash among a
|
|
5
|
+
// commit's patches (foundPatch), read of a repository at a commit, and held
|
|
6
|
+
// to the hash the lockfile names it by, as pnpm 10 and later hash it
|
|
7
|
+
// (hashOfPatch; one pnpm 9 names by an md5 isn't read), by which it is kept
|
|
8
|
+
// for good, read of GitHub once; and applied to the version's files as pnpm applies
|
|
9
|
+
// it as it installs it (patch-package's applyPatch, which pnpm 10 bundles):
|
|
10
|
+
// each file it deletes, creates, renames or changes, a change's hunks each
|
|
11
|
+
// found in the file as it was, where its header says, else as many as FUZZ
|
|
12
|
+
// lines either side, its lines the same but for the space they end in, and
|
|
13
|
+
// those it keeps kept as the file has them. pnpm 10, 11 and 12 each read a
|
|
14
|
+
// patch their own way, so one is read only where none installs other than
|
|
15
|
+
// it shows (parsePatch): a line outside what git writes, paths that
|
|
16
|
+
// disagree, a rename that also creates or deletes, or a hunk whose missing
|
|
17
|
+
// final newline the file doesn't bear out, isn't read. A file it makes
|
|
18
|
+
// executable, or no longer, is so, as every pnpm makes it. Nor is a patch of
|
|
19
|
+
// a file's bytes (git's binary patch) or of a mode but 644 and 755, or of a
|
|
20
|
+
// file outside the package, read; and one that doesn't apply fails as a whole.
|
|
21
|
+
import { Buffer } from 'node:buffer'
|
|
22
|
+
import { createHash } from 'node:crypto'
|
|
23
|
+
import { YamlError, parseYaml } from '@preventive/lockfile/yaml.js'
|
|
24
|
+
import { isGithubRepoName } from '../common/github-pr.ts'
|
|
25
|
+
import { isPatchHash, isPatchPath } from '../common/managed/npm-packages.js'
|
|
26
|
+
import { GithubApiError } from './github-errors.ts'
|
|
27
|
+
import { isStasisConfig } from '../common/managed/stasis-config.ts'
|
|
28
|
+
import { SHA, isId } from './github-values.ts'
|
|
29
|
+
import { type LockedPackage, pathFrom } from './locked-packages.ts'
|
|
30
|
+
import { type Lru, lru } from './lru.ts'
|
|
31
|
+
import type { KeptStore } from './npm-kept.ts'
|
|
32
|
+
import { plainObject } from './npm-manifest.ts'
|
|
33
|
+
import { NpmPackageError, type NpmPackageFile, npmFileText, pathOrder } from './npm-packages.ts'
|
|
34
|
+
import { gitPath, isPlainPath, namesBoth, pathClaims, sidePath, unquoted } from './npm-patch-paths.ts'
|
|
35
|
+
import { type GithubAsked, type PullDependencies, lockfileFormat, repositoryFile } from './pull-dependencies.ts'
|
|
36
|
+
import { eachOf } from './queue.ts'
|
|
37
|
+
|
|
38
|
+
// Where a patch is read: its file in a repository (by its id) at a commit,
|
|
39
|
+
// from the repository's root; and the patch, by its hash besides. A request
|
|
40
|
+
// names its repository by its id, or by its name (`owner/name`) in its place
|
|
41
|
+
// (http.ts wholeRepositoryAccess), and may leave its path out, the patch
|
|
42
|
+
// looked for there (foundPatch); or the patch by its hash alone, which only
|
|
43
|
+
// one kept answers (keptPatch).
|
|
44
|
+
export interface PatchSource { repoId: number; commit: string; path: string }
|
|
45
|
+
export interface NpmPatch extends PatchSource { hash: string }
|
|
46
|
+
export type RequestedPatch = (Omit<NpmPatch, 'repoId' | 'path'> & { repoId: number | string; path?: string }) | { hash: string }
|
|
47
|
+
|
|
48
|
+
// The most a patch's file may hold, and how many lines either side of where a
|
|
49
|
+
// hunk says its lines are they are looked for; and how many patches an
|
|
50
|
+
// answer looks for, those past them left for the next, and how many at once.
|
|
51
|
+
const MAX_PATCH_BYTES = 8 * 1024 * 1024
|
|
52
|
+
const FUZZ = 20
|
|
53
|
+
const MAX_FOUND = 100
|
|
54
|
+
const FIND_AT_ONCE = 4
|
|
55
|
+
|
|
56
|
+
// The patch a request names, by its hash (`hash`), its repository's id
|
|
57
|
+
// (`repoId`, a number or its digits) or name, the commit and its file's
|
|
58
|
+
// path or none, or by its hash alone; null where it names none, and
|
|
59
|
+
// 'invalid' where it names one otherwise.
|
|
60
|
+
export function requestedPatch({ hash, repoId, commit, path }: { hash: unknown; repoId?: unknown; commit?: unknown; path?: unknown }): RequestedPatch | null | 'invalid' {
|
|
61
|
+
if ([repoId, commit, path].every(field => field == null)) return hash == null ? null : isPatchHash(hash) ? { hash } : 'invalid'
|
|
62
|
+
const id = typeof repoId === 'string' && /^[1-9]\d{0,15}$/u.test(repoId) ? Number(repoId) : repoId
|
|
63
|
+
if (!isPatchHash(hash) || !(isId(id) || typeof id === 'string' && isGithubRepoName(id)) || typeof commit !== 'string' || !SHA.test(commit)
|
|
64
|
+
|| path != null && !isPatchPath(path)) return 'invalid'
|
|
65
|
+
return { hash, repoId: id, commit, ...typeof path === 'string' ? { path } : {} }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The hash pnpm names a patch's text by, a sha256 in hex of it with its
|
|
69
|
+
// CRLFs as LFs, as pnpm 10 and later read a patch's file (the CRs of lines
|
|
70
|
+
// it ends otherwise kept); and whether `text` is the patch `hash` names.
|
|
71
|
+
export const hashOfPatch = (text: string) => createHash('sha256').update(text.replaceAll('\r\n', '\n')).digest('hex')
|
|
72
|
+
export const isPatchOf = (text: string, hash: string) => hashOfPatch(text) === hash
|
|
73
|
+
|
|
74
|
+
// The text of a patch's file in repository `fullName` at `commit`, read with
|
|
75
|
+
// `token`; null where GitHub has no text of it, or it is past MAX_PATCH_BYTES.
|
|
76
|
+
const patchFileAt = (fullName: string, token: string, path: string, commit: string) => repositoryFile(fullName, token, path, commit).then(text => text !== null
|
|
77
|
+
&& Buffer.byteLength(text) > MAX_PATCH_BYTES ? null : text, (err: unknown) => { if (err instanceof GithubApiError && err.message === 'github-malformed') return null; throw err })
|
|
78
|
+
|
|
79
|
+
// A change a patch makes to a file at `path` of the package: deleting it,
|
|
80
|
+
// creating it, renaming it from another (`from`), and its hunks, as
|
|
81
|
+
// patch-package reads them: where its header says its lines are (`at`, the
|
|
82
|
+
// first's index, as it reads `-0`, the first line), how many it finds
|
|
83
|
+
// (`length`), and its lines, each without its LF, in runs of a kind
|
|
84
|
+
// (`parts`): those it keeps, removes or adds, the last of a side told where
|
|
85
|
+
// the file has no LF after it (`unended`); and whether it makes the file
|
|
86
|
+
// executable, or no longer (`executable`), where it says.
|
|
87
|
+
export interface HunkPart { kind: 'context' | 'deletion' | 'insertion'; lines: string[]; unended?: true }
|
|
88
|
+
export interface PatchHunk { at: number; length: number; parts: HunkPart[] }
|
|
89
|
+
export interface PatchedFile { path: string; from?: string; deleted?: true; created?: true; executable?: boolean; hunks: PatchHunk[] }
|
|
90
|
+
|
|
91
|
+
const unreadable = () => new NpmPackageError(422, 'patch-unreadable')
|
|
92
|
+
|
|
93
|
+
// The hunk whose `@@` line is at `at` of `lines`, those before `end` read,
|
|
94
|
+
// and the line after it, as patch-package reads it: a line of its own
|
|
95
|
+
// kind, else of those it keeps where it is empty, or a CR alone.
|
|
96
|
+
function readHunk(lines: string[], at: number, end: number): { hunk: PatchHunk; next: number } {
|
|
97
|
+
const range = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u.exec(lines[at++]!)
|
|
98
|
+
if (!range) throw unreadable()
|
|
99
|
+
let news = Number(range[4] ?? 1), olds = Number(range[2] ?? 1)
|
|
100
|
+
const hunk: PatchHunk = { at: Math.max(Number(range[1]), 1) - 1, length: olds, parts: [] }
|
|
101
|
+
// Lines added with none to find them by, past the file's start (git's -U0): pnpm 10 and 11 add them
|
|
102
|
+
// before the line named, pnpm 12 after it.
|
|
103
|
+
if (olds === 0 && Number(range[1]) > 0) throw unreadable()
|
|
104
|
+
while (olds > 0 || news > 0) {
|
|
105
|
+
// A line its count reads past those read isn't one.
|
|
106
|
+
if (at >= end) throw unreadable()
|
|
107
|
+
const line = lines[at++]!
|
|
108
|
+
const kind = line === '' || line === '\r' || line[0] === ' ' ? 'context' : line[0] === '-' ? 'deletion' : line[0] === '+' ? 'insertion' : null
|
|
109
|
+
if (!kind) throw unreadable()
|
|
110
|
+
const last = hunk.parts.at(-1)
|
|
111
|
+
const part = last?.kind === kind ? last : hunk.parts[hunk.parts.push({ kind, lines: [] }) - 1]!
|
|
112
|
+
part.lines.push(line.slice(1))
|
|
113
|
+
if (kind !== 'insertion') olds--
|
|
114
|
+
if (kind !== 'deletion') news--
|
|
115
|
+
if (olds < 0 || news < 0) throw unreadable()
|
|
116
|
+
// The line before it the last of its side, with no LF after it.
|
|
117
|
+
if (lines[at]?.startsWith('\\')) {
|
|
118
|
+
if (!lines[at++]!.startsWith('\')) throw unreadable()
|
|
119
|
+
part.unended = true
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { hunk, next: at }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A patch's files as git's diff writes them, as pnpm writes a patch: a file's
|
|
126
|
+
// `diff --git` line, its headers, the paths its sides have, and its hunks.
|
|
127
|
+
// One whose own lines end in CRLFs, as an editor or git's autocrlf leaves
|
|
128
|
+
// one, is read as patch-package reads it: its headers but their CRs, its
|
|
129
|
+
// hunks' lines with them. pnpm 10 and 11 also read hunks where git puts none
|
|
130
|
+
// (with no `diff --git` line, past a blank or `index` line, after another
|
|
131
|
+
// file's hunks) and pnpm 12 doesn't, so a line git doesn't write is refused,
|
|
132
|
+
// but an empty one within a hunk's count, git's ` ` as an editor trims it,
|
|
133
|
+
// which every pnpm reads as that, and blank lines at the end. So are a
|
|
134
|
+
// `diff --git` line naming other paths
|
|
135
|
+
// than its sides (pnpm 10 and 11 take one, pnpm 12 the other), a rename that
|
|
136
|
+
// also creates or deletes, and a `new mode` line without its `old mode`
|
|
137
|
+
// (pnpm 12 alone reads one). Throws for one it can't read, or of bytes.
|
|
138
|
+
export function parsePatch(patch: string): PatchedFile[] {
|
|
139
|
+
// One whose last line has no LF: pnpm 12 alone adds that line without one.
|
|
140
|
+
if (patch !== '' && !patch.endsWith('\n')) throw unreadable()
|
|
141
|
+
const lines = patch.split('\n')
|
|
142
|
+
// The lines read: those but the empty one past the patch's last ending.
|
|
143
|
+
const end = lines.length - (patch.endsWith('\n') ? 1 : 0)
|
|
144
|
+
// A header's line but its CR.
|
|
145
|
+
const headerAt = (at: number) => lines[at]?.replace(/\r$/u, '') ?? ''
|
|
146
|
+
const files: PatchedFile[] = []
|
|
147
|
+
for (let at = 0; at < lines.length;) {
|
|
148
|
+
const head = headerAt(at++)
|
|
149
|
+
if (head === '' && lines.slice(at, end).every((_, i) => headerAt(at + i) === '')) break
|
|
150
|
+
if (!head.startsWith('diff --git ')) throw unreadable()
|
|
151
|
+
let created = false, deleted = false, executable: boolean | undefined, from: string | null = null, to: string | null = null
|
|
152
|
+
let sides: [string | null, string | null] | null = null
|
|
153
|
+
const seen = new Set<string>()
|
|
154
|
+
for (; at < end && !/^(?:diff --git |@@ |--- )/u.test(lines[at]!); at++) {
|
|
155
|
+
const header = headerAt(at)
|
|
156
|
+
if (header === 'GIT binary patch' || header.startsWith('Binary files ')) throw new NpmPackageError(422, 'patch-unsupported')
|
|
157
|
+
const kind = /^(?:index|similarity index|dissimilarity index|deleted file mode|new file mode|rename from|rename to|old mode|new mode|copy from|copy to) /u.exec(header)?.[0]
|
|
158
|
+
if (kind === undefined || seen.has(kind)) throw unreadable()
|
|
159
|
+
seen.add(kind)
|
|
160
|
+
// A mode it names: `100644`, or `100755` (executable).
|
|
161
|
+
const mode = kind.endsWith('mode ') ? header.slice(kind.length) : undefined
|
|
162
|
+
if (kind.startsWith('copy ') || (mode !== undefined && mode !== '100644' && mode !== '100755')) throw new NpmPackageError(422, 'patch-unsupported')
|
|
163
|
+
if (kind === 'deleted file mode ') deleted = true
|
|
164
|
+
else if (kind === 'new file mode ') { created = true; executable = mode === '100755' }
|
|
165
|
+
else if (kind === 'new mode ') executable = mode === '100755'
|
|
166
|
+
else if (kind === 'rename from ') from = unquoted(header.slice(kind.length))
|
|
167
|
+
else if (kind === 'rename to ') to = unquoted(header.slice(kind.length))
|
|
168
|
+
}
|
|
169
|
+
if (lines[at]?.startsWith('--- ')) {
|
|
170
|
+
if (!lines[at + 1]?.startsWith('+++ ')) throw unreadable()
|
|
171
|
+
sides = [sidePath(headerAt(at).slice(4), 'a/'), sidePath(headerAt(at + 1).slice(4), 'b/')]
|
|
172
|
+
at += 2
|
|
173
|
+
}
|
|
174
|
+
if ((from === null) !== (to === null) || (deleted && created) || (from !== null && (deleted || created)) || seen.has('old mode ') !== seen.has('new mode ')) throw unreadable()
|
|
175
|
+
const rest = head.slice('diff --git '.length)
|
|
176
|
+
const path = sides ? (created ? sides[1] : sides[0]) : gitPath(rest)
|
|
177
|
+
const after = to ?? path, before = from ?? path
|
|
178
|
+
if (before == null || after == null || ![before, after].every(name => isPatchPath(name) && isPlainPath(name)) || !namesBoth(rest, before, after)
|
|
179
|
+
|| (sides && (sides[0] !== (created ? null : before) || sides[1] !== (deleted ? null : after)))) throw unreadable()
|
|
180
|
+
const hunks: PatchHunk[] = []
|
|
181
|
+
while (lines[at]?.startsWith('@@ ')) {
|
|
182
|
+
const { hunk, next } = readHunk(lines, at, end)
|
|
183
|
+
hunks.push(hunk)
|
|
184
|
+
at = next
|
|
185
|
+
}
|
|
186
|
+
// A file created is its one hunk's lines, as git writes it, which pnpm 10 alone writes of a hunk otherwise.
|
|
187
|
+
if (created && (hunks.length > 1 || hunks.some(hunk => hunk.parts.some(part => part.kind !== 'insertion')))) throw unreadable()
|
|
188
|
+
files.push({ path: deleted ? before : after, ...from === null ? {} : { from: before }, ...deleted ? { deleted: true as const } : {},
|
|
189
|
+
...created ? { created: true as const } : {}, ...executable === undefined || deleted ? {} : { executable }, hunks: deleted ? [] : hunks })
|
|
190
|
+
}
|
|
191
|
+
return files
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const doesNotApply = () => new NpmPackageError(422, 'patch-does-not-apply')
|
|
195
|
+
|
|
196
|
+
// A change a hunk makes to a file's lines (`lines`, split at its LFs): those
|
|
197
|
+
// at `splice` that it removes (`remove`) and adds in their place (`insert`),
|
|
198
|
+
// or an empty last line it adds or removes, its LF after the last.
|
|
199
|
+
type LineChange = { splice: number; remove: number; insert: string[] } | 'push' | 'pop'
|
|
200
|
+
|
|
201
|
+
// The changes `hunk` makes where its lines are at `at` of `lines`, as
|
|
202
|
+
// patch-package works them out (evaluateHunk): those it finds the same but
|
|
203
|
+
// for the space they end in, as it compares them; else null.
|
|
204
|
+
function changesAt(hunk: PatchHunk, lines: string[], at: number): LineChange[] | null {
|
|
205
|
+
if (at < 0 || lines.length - at < hunk.length) return null
|
|
206
|
+
const changes: LineChange[] = []
|
|
207
|
+
for (const part of hunk.parts) {
|
|
208
|
+
if (part.kind === 'insertion') {
|
|
209
|
+
changes.push({ splice: at, remove: 0, insert: part.lines })
|
|
210
|
+
if (part.unended) changes.push('pop')
|
|
211
|
+
continue
|
|
212
|
+
}
|
|
213
|
+
for (const line of part.lines) if (lines[at++]!.trimEnd() !== line.trimEnd()) return null
|
|
214
|
+
if (part.kind === 'deletion') {
|
|
215
|
+
changes.push({ splice: at - part.lines.length, remove: part.lines.length, insert: [] })
|
|
216
|
+
if (part.unended) changes.push('push')
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return changes
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Whether `hunk`, found at `at` of `lines`, may say a side ends with no LF
|
|
223
|
+
// (`unended`): pnpm versions apply that mark alike only where the hunk ends
|
|
224
|
+
// where the file does, and the file agrees: no LF at its end for the old
|
|
225
|
+
// side's mark; one, or the old side's mark, for the new side's.
|
|
226
|
+
function endsAgree(hunk: PatchHunk, lines: string[], at: number): boolean {
|
|
227
|
+
const unended = lines.at(-1) !== ''
|
|
228
|
+
const ending = at + hunk.length === lines.length - (unended ? 0 : 1)
|
|
229
|
+
const pushes = hunk.parts.some(part => part.kind === 'deletion' && part.unended)
|
|
230
|
+
const pops = hunk.parts.some(part => part.kind === 'insertion' && part.unended)
|
|
231
|
+
return (!pushes || (ending && unended)) && (!pops || (ending && (pushes || !unended)))
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// `text` with `hunks` applied as patch-package applies them (applyPatch):
|
|
235
|
+
// each one's lines found in the text as it was, where its header says, else
|
|
236
|
+
// as near as FUZZ lines either side, the nearer first, and the earlier of
|
|
237
|
+
// two as near; then its changes made in turn, past the lines those before
|
|
238
|
+
// added or removed.
|
|
239
|
+
function applied(text: string, hunks: PatchHunk[]): string {
|
|
240
|
+
const lines = text.split('\n')
|
|
241
|
+
const changes = hunks.flatMap(hunk => {
|
|
242
|
+
for (let offset = 0; Math.abs(offset) <= FUZZ; offset = offset < 0 ? -offset : -offset - 1) {
|
|
243
|
+
const found = changesAt(hunk, lines, hunk.at + offset)
|
|
244
|
+
// Where pnpm finds it, whatever its marks say, lest another place be shown changed than it changes.
|
|
245
|
+
if (found && !endsAgree(hunk, lines, hunk.at + offset)) throw doesNotApply()
|
|
246
|
+
if (found) return found
|
|
247
|
+
}
|
|
248
|
+
throw doesNotApply()
|
|
249
|
+
})
|
|
250
|
+
let shift = 0
|
|
251
|
+
for (const change of changes) {
|
|
252
|
+
if (change === 'push') lines.push('')
|
|
253
|
+
else if (change === 'pop') lines.pop()
|
|
254
|
+
else {
|
|
255
|
+
lines.splice(change.splice + shift, change.remove, ...change.insert)
|
|
256
|
+
shift += change.insert.length - change.remove
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return lines.join('\n')
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// A version's `files` with `patch` applied, in the order of their paths, as
|
|
263
|
+
// its tarball's are read; a file it changes the text of must be text. One it
|
|
264
|
+
// creates holds its first hunk's first lines, in place of any there, as
|
|
265
|
+
// patch-package writes it. A file keeps its mode but where the patch says
|
|
266
|
+
// otherwise. A symlink, which pnpm never installs, is none to patch. A path
|
|
267
|
+
// it creates or renames to is one macOS and Windows take as written (pathClaims).
|
|
268
|
+
export function patchedFiles(files: NpmPackageFile[], patch: PatchedFile[]): NpmPackageFile[] {
|
|
269
|
+
const byPath = new Map(files.map(file => [file.path, file]))
|
|
270
|
+
const encoder = new TextEncoder()
|
|
271
|
+
let claim: ((path: string) => void) | undefined
|
|
272
|
+
for (const file of patch) {
|
|
273
|
+
if (file.created || file.from !== undefined) (claim ??= pathClaims(files.map(each => each.path)))(file.path)
|
|
274
|
+
if (file.created) {
|
|
275
|
+
const first = file.hunks[0]?.parts[0]
|
|
276
|
+
byPath.set(file.path, { path: file.path, bytes: encoder.encode(first ? `${first.lines.join('\n')}${first.unended ? '' : '\n'}` : ''),
|
|
277
|
+
...file.executable ? { executable: true as const } : {} })
|
|
278
|
+
continue
|
|
279
|
+
}
|
|
280
|
+
const source = file.from ?? file.path
|
|
281
|
+
const was = byPath.get(source)
|
|
282
|
+
if (!was || was.link !== undefined) throw doesNotApply()
|
|
283
|
+
if (file.deleted) { byPath.delete(source); continue }
|
|
284
|
+
let next = was.bytes
|
|
285
|
+
if (file.hunks.length > 0) {
|
|
286
|
+
const text = npmFileText(was.bytes)
|
|
287
|
+
if (text === null) throw doesNotApply()
|
|
288
|
+
next = encoder.encode(applied(text, file.hunks))
|
|
289
|
+
}
|
|
290
|
+
if (file.from !== undefined) {
|
|
291
|
+
if (byPath.has(file.path)) throw doesNotApply()
|
|
292
|
+
byPath.delete(source)
|
|
293
|
+
}
|
|
294
|
+
byPath.set(file.path, { path: file.path, bytes: next, ...(file.executable ?? was.executable) ? { executable: true as const } : {} })
|
|
295
|
+
}
|
|
296
|
+
return [...byPath.values()].toSorted((a, b) => pathOrder(a.path, b.path))
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// About the bytes a patch read takes: the characters of its paths and of the
|
|
300
|
+
// lines its hunks hold, and the room past them each line takes (LINE_COST)
|
|
301
|
+
// and each file, hunk and part (HUNK_COST), as V8 takes it; so a patch of a
|
|
302
|
+
// few long lines (a minified file's) weighs about its size, and one of many
|
|
303
|
+
// short lines or tiny hunks many times it, as it takes.
|
|
304
|
+
const HUNK_COST = 256, LINE_COST = 40
|
|
305
|
+
const held = (lines: string[]) => lines.reduce((sum, line) => sum + LINE_COST + line.length, HUNK_COST)
|
|
306
|
+
export const patchWeight = (files: PatchedFile[]) => files.reduce((sum, file) => sum + HUNK_COST + file.path.length + (file.from?.length ?? 0)
|
|
307
|
+
+ file.hunks.reduce((bytes, hunk) => bytes + HUNK_COST + hunk.parts.reduce((parts, part) => parts + held(part.lines), 0), 0), 1)
|
|
308
|
+
|
|
309
|
+
// What is kept of a patch, for good, by its repository and hash (patchKey):
|
|
310
|
+
// where it was read, and its text, which its hash names wherever it is.
|
|
311
|
+
// Kept by its repository too, as only one who reads all of that is given it.
|
|
312
|
+
export interface KeptPatch { commit: string; path: string; text: string }
|
|
313
|
+
export const patchKey = (repoId: number, hash: string) => `${repoId}\0${hash}`
|
|
314
|
+
|
|
315
|
+
// Patches read lately, with where each was read, by their keys, as many as
|
|
316
|
+
// MAX_KEPT_PATCHES of the bytes they take (patchWeight); and the path each
|
|
317
|
+
// found or kept is at (`located`): '' by its key and a commit for one not
|
|
318
|
+
// found there, lest each telling look again, none for one not looked for;
|
|
319
|
+
// and '' by its repository's key and a commit whose patches were all kept.
|
|
320
|
+
const MAX_KEPT_PATCHES = 64 * 1024 * 1024
|
|
321
|
+
export interface ReadPatch { source: PatchSource; files: PatchedFile[] }
|
|
322
|
+
const patches: Lru<ReadPatch> = lru(MAX_KEPT_PATCHES, read => patchWeight(read.files))
|
|
323
|
+
const located: Lru<string> = lru(100_000)
|
|
324
|
+
|
|
325
|
+
// The patch `patch` names, and where it was read (`source`): as kept by its
|
|
326
|
+
// repository and hash, in this instance, else beyond it (`kept`, held to its
|
|
327
|
+
// hash again), wherever the request says it is; else read of repository
|
|
328
|
+
// `fullName` with `token` at the commit and path it names, held to its
|
|
329
|
+
// hash, and kept for good; `asked` told where GitHub was. Its file missing
|
|
330
|
+
// there, or other than its hash says, or past MAX_PATCH_BYTES, fails, as
|
|
331
|
+
// does one that isn't read as git writes a diff (parsePatch).
|
|
332
|
+
export async function readPatch(fullName: string, token: string, patch: NpmPatch, kept: KeptStore<KeptPatch>, asked?: GithubAsked): Promise<ReadPatch> {
|
|
333
|
+
const key = patchKey(patch.repoId, patch.hash)
|
|
334
|
+
const known = patches.get(key)
|
|
335
|
+
if (known) return known
|
|
336
|
+
let stored = (await kept.read(key))?.value
|
|
337
|
+
if (typeof stored?.text !== 'string' || !isPatchOf(stored.text, patch.hash)) {
|
|
338
|
+
if (asked) asked.github = true
|
|
339
|
+
const text = await repositoryFile(fullName, token, patch.path, patch.commit)
|
|
340
|
+
if (text === null) throw new NpmPackageError(404, 'patch-not-found')
|
|
341
|
+
if (Buffer.byteLength(text) > MAX_PATCH_BYTES) throw new NpmPackageError(413, 'patch-too-large')
|
|
342
|
+
if (!isPatchOf(text, patch.hash)) throw new NpmPackageError(409, 'patch-mismatch')
|
|
343
|
+
stored = { commit: patch.commit, path: patch.path, text }
|
|
344
|
+
await kept.write(key, stored, Date.now())
|
|
345
|
+
}
|
|
346
|
+
const read = { source: { repoId: patch.repoId, commit: stored.commit, path: stored.path }, files: parsePatch(stored.text) }
|
|
347
|
+
patches.set(key, read)
|
|
348
|
+
return read
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// The patch `hash` names as kept, of the first of repositories `repoIds`
|
|
352
|
+
// that keeps it, in this instance, else beyond it (`kept`, held to its hash
|
|
353
|
+
// again), GitHub asked for none of it; null where none does.
|
|
354
|
+
export async function keptPatch(repoIds: number[], hash: string, kept: KeptStore<KeptPatch>): Promise<ReadPatch | null> {
|
|
355
|
+
const keys = repoIds.map(repoId => patchKey(repoId, hash))
|
|
356
|
+
const known = keys.map(key => patches.get(key)).find(Boolean)
|
|
357
|
+
if (known) return known
|
|
358
|
+
const stored = keys.length > 0 ? await kept.readMany(keys) : new Map()
|
|
359
|
+
for (const [at, key] of keys.entries()) {
|
|
360
|
+
const patch = stored.get(key)?.value
|
|
361
|
+
if (typeof patch?.text !== 'string' || !isPatchOf(patch.text, hash)) continue
|
|
362
|
+
const read = { source: { repoId: repoIds[at]!, commit: patch.commit, path: patch.path }, files: parsePatch(patch.text) }
|
|
363
|
+
patches.set(key, read)
|
|
364
|
+
return read
|
|
365
|
+
}
|
|
366
|
+
return null
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// The patch `patch` names by its hash at a commit of its repository, its
|
|
370
|
+
// file unnamed: as kept by its repository and hash (keptPatch); else among
|
|
371
|
+
// the patches of the commit's files (`listed` lists them, with their bytes;
|
|
372
|
+
// null where its repository isn't looked in), each `.patch` file in a
|
|
373
|
+
// `patches` directory beside a pnpm lockfile or a stasis.config.json, where
|
|
374
|
+
// pnpm patch-commit writes them, as many as MAX_FOUND, read of repository
|
|
375
|
+
// `fullName` with `token` and each kept for good by its own hash, so a
|
|
376
|
+
// commit is looked in once; `asked` told where GitHub was. One none of them
|
|
377
|
+
// is fails as not found.
|
|
378
|
+
export async function foundPatch(fullName: string, token: string, patch: Omit<NpmPatch, 'path'>, kept: KeptStore<KeptPatch>,
|
|
379
|
+
listed: () => Promise<{ files: string[]; sizes: number[] } | null>, asked?: GithubAsked): Promise<ReadPatch> {
|
|
380
|
+
const ofIt = () => keptPatch([patch.repoId], patch.hash, kept), searched = `${patchKey(patch.repoId, '')}\0${patch.commit}`
|
|
381
|
+
let found = await ofIt()
|
|
382
|
+
const tree = found || located.get(searched) !== undefined ? null : await listed()
|
|
383
|
+
if (tree) {
|
|
384
|
+
if (asked) asked.github = true
|
|
385
|
+
const dirOf = (path: string) => path.slice(0, path.lastIndexOf('/') + 1)
|
|
386
|
+
const beside = new Set(tree.files.filter(path => lockfileFormat(path) === 'pnpm' || isStasisConfig(path)).map(path => `${dirOf(path)}patches/`))
|
|
387
|
+
const paths = tree.files.filter((path, at) => path.endsWith('.patch') && beside.has(dirOf(path)) && (tree.sizes[at] ?? 0) <= MAX_PATCH_BYTES)
|
|
388
|
+
await eachOf(paths.slice(0, MAX_FOUND), FIND_AT_ONCE, path => patchFileAt(fullName, token, path, patch.commit)
|
|
389
|
+
.then(text => text === null ? undefined : kept.write(patchKey(patch.repoId, hashOfPatch(text)), { commit: patch.commit, path, text }, Date.now())))
|
|
390
|
+
located.set(searched, '')
|
|
391
|
+
found = await ofIt()
|
|
392
|
+
}
|
|
393
|
+
if (!found) throw new NpmPackageError(404, 'patch-not-found')
|
|
394
|
+
return found
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// The files a pnpm-workspace.yaml (`workspace`, its text, null for none)
|
|
398
|
+
// names its patches by, as pnpm 11 and later read their patchedDependencies
|
|
399
|
+
// there alone, by a version or a package; none where its reader refuses it.
|
|
400
|
+
function patchedBy(workspace: string | null): Record<string, unknown> {
|
|
401
|
+
let settings: unknown
|
|
402
|
+
try { settings = workspace === null ? null : parseYaml(workspace, { workflow: true }) } catch (err) {
|
|
403
|
+
if (!(err instanceof YamlError)) throw err
|
|
404
|
+
}
|
|
405
|
+
const patched = plainObject(settings) ? settings['patchedDependencies'] : null
|
|
406
|
+
return plainObject(patched) ? patched : {}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// A version pnpm patches by a patch named by a sha256, with its lockfile's
|
|
410
|
+
// directory: one its lockfile names by its hash alone (pnpm 11's and later),
|
|
411
|
+
// which wants its file found; and, `keepNamed`, one whose file it names.
|
|
412
|
+
type Patched = LockedPackage & { patchHash: string; patchDir: string; version: string }
|
|
413
|
+
const patchedBy256 = (pkg: LockedPackage, keepNamed = false): pkg is Patched => (pkg.patchPath === undefined || keepNamed) && pkg.patchDir !== undefined
|
|
414
|
+
&& pkg.version !== null && isPatchHash(pkg.patchHash)
|
|
415
|
+
|
|
416
|
+
// `told`, each version of its that pnpm patches by a patch its lockfile names
|
|
417
|
+
// by its hash alone with its patch's file (`patchPath`), as a lockfile naming
|
|
418
|
+
// one tells it: as kept by its repository and hash, else the file the
|
|
419
|
+
// pnpm-workspace.yaml beside its lockfile names of it (patchedBy: its
|
|
420
|
+
// version's, else its package's) at `told.head`, held to its hash and kept
|
|
421
|
+
// for good, so each is found once, as many an answer as MAX_FOUND; one not
|
|
422
|
+
// found left with none. With `keepNamed`, as a default branch's are told,
|
|
423
|
+
// each patch whose file a lockfile names is read there and kept as well, so
|
|
424
|
+
// that a link naming it by its hash alone finds it later (keptPatch).
|
|
425
|
+
// `asked` told where GitHub was. A file GitHub has no text of, or past
|
|
426
|
+
// MAX_PATCH_BYTES, is none; any other failure of GitHub's is the whole
|
|
427
|
+
// answer's, as a lockfile's is.
|
|
428
|
+
export async function withPatchFiles<T extends PullDependencies>(told: T, repoId: number, fullName: string, token: string, kept: KeptStore<KeptPatch>,
|
|
429
|
+
asked?: GithubAsked, { keepNamed = false } = {}): Promise<T> {
|
|
430
|
+
const wanted = new Map<string, Patched>()
|
|
431
|
+
for (const dir of told.lockfiles) {
|
|
432
|
+
for (const pkg of 'error' in dir ? [] : [...dir.added, ...dir.locked]) if (patchedBy256(pkg, keepNamed)) wanted.set(patchKey(repoId, pkg.patchHash), pkg)
|
|
433
|
+
}
|
|
434
|
+
if (wanted.size === 0) return told
|
|
435
|
+
const at = (key: string) => located.get(key) ?? located.get(`${key}\0${told.head}`)
|
|
436
|
+
const unknown = [...wanted.keys()].filter(key => at(key) === undefined)
|
|
437
|
+
for (const [key, { value }] of unknown.length > 0 ? await kept.readMany(unknown) : []) if (typeof value?.path === 'string') located.set(key, value.path)
|
|
438
|
+
const files = new Map<string, Promise<string | null>>(), read = (path: string) => files.get(path) ?? files.set(path, patchFileAt(fullName, token, path, told.head)).get(path)!
|
|
439
|
+
const workspaces = new Map<string, Promise<Record<string, unknown>>>()
|
|
440
|
+
const namedIn = (dir: string) => workspaces.get(dir) ?? workspaces.set(dir, read(pathFrom(dir, 'pnpm-workspace.yaml')!).then(patchedBy)).get(dir)!
|
|
441
|
+
await eachOf(unknown.filter(key => at(key) === undefined).slice(0, MAX_FOUND), FIND_AT_ONCE, async key => {
|
|
442
|
+
const pkg = wanted.get(key)!
|
|
443
|
+
if (asked) asked.github = true
|
|
444
|
+
const patched = pkg.patchPath === undefined ? await namedIn(pkg.patchDir) : {}
|
|
445
|
+
const file = pkg.patchPath ?? patched[`${pkg.name}@${pkg.version}`] ?? patched[pkg.name]
|
|
446
|
+
const path = typeof file === 'string' ? pkg.patchPath ?? pathFrom(pkg.patchDir, file) : null
|
|
447
|
+
const text = path === null ? null : await read(path)
|
|
448
|
+
if (path === null || text === null || !isPatchOf(text, pkg.patchHash)) { located.set(`${key}\0${told.head}`, ''); return }
|
|
449
|
+
await kept.write(key, { commit: told.head, path, text }, Date.now())
|
|
450
|
+
located.set(key, path)
|
|
451
|
+
})
|
|
452
|
+
const named = <P extends LockedPackage>(pkg: P): P => {
|
|
453
|
+
const patchPath = patchedBy256(pkg) && at(patchKey(repoId, pkg.patchHash))
|
|
454
|
+
return patchPath ? { ...pkg, patchPath } : pkg
|
|
455
|
+
}
|
|
456
|
+
return { ...told, lockfiles: told.lockfiles.map(dir => 'error' in dir ? dir : { ...dir, added: dir.added.map(named), locked: dir.locked.map(named) }) }
|
|
457
|
+
}
|