@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,127 @@
|
|
|
1
|
+
// npm's provenance for a public version (npm-packages.ts npmManifest marks
|
|
2
|
+
// one that has it): the SLSA statement npm's registry keeps beside it,
|
|
3
|
+
// saying where the version was built: from which repository and commit, by
|
|
4
|
+
// which workflow, on which builder, in which run, with its entry in
|
|
5
|
+
// Sigstore's transparency log. Only a statement naming this version's
|
|
6
|
+
// tarball (its name, version and sha512 digest) is read; its signature isn't
|
|
7
|
+
// checked here, as npm checks it when the version is published. npm is asked
|
|
8
|
+
// without the server's token, and only about a version it answers for
|
|
9
|
+
// without one (http.ts), each answer kept as npm-insights.ts keeps its own.
|
|
10
|
+
import { Buffer } from 'node:buffer'
|
|
11
|
+
import { SHA } from './github-values.ts'
|
|
12
|
+
import { kept } from './kept.ts'
|
|
13
|
+
import { plainObject } from './npm-manifest.ts'
|
|
14
|
+
import { NPM_REGISTRY, askUpstreamJson } from './npm-packages.ts'
|
|
15
|
+
|
|
16
|
+
const ATTESTATIONS_API = `${NPM_REGISTRY}/-/npm/v1/attestations`
|
|
17
|
+
const ATTESTATIONS_BYTES = 1024 * 1024
|
|
18
|
+
// SLSA's v1 statement, as npm writes it now, and v0.2, as it first did.
|
|
19
|
+
const SLSA = /^https:\/\/slsa\.dev\/provenance\/v(?:1|0\.2)$/u
|
|
20
|
+
const GITHUB_REPOSITORY = /^https:\/\/github\.com\/([\w.-]+\/[\w.-]+)$/u
|
|
21
|
+
|
|
22
|
+
// Where a version was built: its source `repository` (an https URL) and
|
|
23
|
+
// `commit`, the `workflow` file and the `ref` it ran on, the `builder` and
|
|
24
|
+
// the `run` (an https URL), its transparency log entry (`logIndex`), and
|
|
25
|
+
// whose runner built it (`runner`: `github-hosted`, `gitlab-hosted` or
|
|
26
|
+
// `self-hosted`, as its signing certificate says); each null where they
|
|
27
|
+
// don't say.
|
|
28
|
+
export interface NpmProvenance {
|
|
29
|
+
repository: string; commit: string | null; workflow: string | null; ref: string | null
|
|
30
|
+
builder: string | null; run: string | null; logIndex: number | null; runner: string | null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const text = (value: unknown) => typeof value === 'string' && value !== '' && value.length <= 2048 ? value : null
|
|
34
|
+
const https = (value: unknown) => {
|
|
35
|
+
const url = text(value)
|
|
36
|
+
try { return url && new URL(url).protocol === 'https:' ? url : null } catch { return null }
|
|
37
|
+
}
|
|
38
|
+
// `git+https://github.com/org/repo@refs/heads/main` → the repository and
|
|
39
|
+
// ref; GitLab's names no ref (`git+https://gitlab.com/group/project`).
|
|
40
|
+
function sourceOf(uri: unknown) {
|
|
41
|
+
const match = /^git\+(https:\/\/[^@\s]+?)(?:\.git)?(?:@(.+))?$/u.exec(text(uri) ?? '')
|
|
42
|
+
return match ? { repository: https(match[1]), ref: match[2] ?? null } : { repository: null, ref: null }
|
|
43
|
+
}
|
|
44
|
+
// What `keys` lead to in `value`, through objects and arrays.
|
|
45
|
+
const field = (value: unknown, ...keys: string[]): unknown =>
|
|
46
|
+
keys.reduce<unknown>((at, key) => plainObject(at) || Array.isArray(at) ? (at as Record<string, unknown>)[key] : undefined, value)
|
|
47
|
+
// The list they lead to, none where it is none.
|
|
48
|
+
const listAt = (value: unknown, ...keys: string[]): unknown[] => {
|
|
49
|
+
const list = field(value, ...keys)
|
|
50
|
+
return Array.isArray(list) ? list : []
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// What a statement says of the build, as v1 or v0.2 says it; null where it
|
|
54
|
+
// names no source repository.
|
|
55
|
+
function readStatement(predicateType: string, predicate: unknown): NpmProvenance | null {
|
|
56
|
+
if (predicateType.endsWith('v1')) {
|
|
57
|
+
const workflow = field(predicate, 'buildDefinition', 'externalParameters', 'workflow')
|
|
58
|
+
const source = field(predicate, 'buildDefinition', 'resolvedDependencies', '0')
|
|
59
|
+
const repository = https(field(workflow, 'repository')) ?? sourceOf(field(source, 'uri')).repository
|
|
60
|
+
if (!repository) return null
|
|
61
|
+
const commit = text(field(source, 'digest', 'gitCommit'))
|
|
62
|
+
return {
|
|
63
|
+
repository, commit: commit && SHA.test(commit) ? commit : null, workflow: text(field(workflow, 'path')),
|
|
64
|
+
ref: text(field(workflow, 'ref')) ?? sourceOf(field(source, 'uri')).ref, builder: https(field(predicate, 'runDetails', 'builder', 'id')),
|
|
65
|
+
run: https(field(predicate, 'runDetails', 'metadata', 'invocationId')), logIndex: null, runner: null,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const config = field(predicate, 'invocation', 'configSource')
|
|
69
|
+
const { repository, ref } = sourceOf(field(config, 'uri'))
|
|
70
|
+
if (!repository) return null
|
|
71
|
+
const commit = text(field(config, 'digest', 'sha1'))
|
|
72
|
+
// Its run: GitLab's job URL, or GitHub's as `<run id>-<attempt>`.
|
|
73
|
+
const invocation = field(predicate, 'metadata', 'buildInvocationId')
|
|
74
|
+
const run = /^(\d+)-(\d+)$/u.exec(text(invocation) ?? '')
|
|
75
|
+
const github = GITHUB_REPOSITORY.exec(repository)?.[1]
|
|
76
|
+
return {
|
|
77
|
+
repository, commit: commit && SHA.test(commit) ? commit : null, workflow: text(field(config, 'entryPoint')), ref,
|
|
78
|
+
builder: https(field(predicate, 'builder', 'id')),
|
|
79
|
+
run: https(invocation) ?? (run && github ? `https://github.com/${github}/actions/runs/${run[1]}/attempts/${run[2]}` : null), logIndex: null, runner: null,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Fulcio's Runner Environment extension (1.3.6.1.4.1.57264.1.11) in the
|
|
84
|
+
// certificate that signed it: its OID, then its value, a short UTF8String
|
|
85
|
+
// in an OCTET STRING, as the CI's token gave it. A builder's id doesn't
|
|
86
|
+
// tell GitLab's own runners from a project's.
|
|
87
|
+
const RUNNER_ENVIRONMENT = Buffer.from('060a2b0601040183bf30010b', 'hex')
|
|
88
|
+
function runnerOf(bundle: unknown): string | null {
|
|
89
|
+
const raw = field(bundle, 'verificationMaterial', 'certificate', 'rawBytes') ?? field(bundle, 'verificationMaterial', 'x509CertificateChain', 'certificates', '0', 'rawBytes')
|
|
90
|
+
if (typeof raw !== 'string') return null
|
|
91
|
+
const der = Buffer.from(raw, 'base64')
|
|
92
|
+
const at = der.indexOf(RUNNER_ENVIRONMENT) + RUNNER_ENVIRONMENT.length
|
|
93
|
+
if (at < RUNNER_ENVIRONMENT.length || der[at] !== 0x04 || der[at + 2] !== 0x0c || der[at + 1] !== der[at + 3]! + 2) return null
|
|
94
|
+
return text(der.subarray(at + 4, at + 4 + der[at + 3]!).toString('utf8'))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function askProvenance(name: string, version: string, integrity: string): Promise<NpmProvenance | null> {
|
|
98
|
+
const json = await askUpstreamJson(`${ATTESTATIONS_API}/${name.replace('/', '%2f')}@${encodeURIComponent(version)}`,
|
|
99
|
+
{ headers: { accept: 'application/json' }, limit: ATTESTATIONS_BYTES })
|
|
100
|
+
if (json === null) return null
|
|
101
|
+
const slsa = listAt(json, 'attestations').find(item => SLSA.test(text(field(item, 'predicateType')) ?? ''))
|
|
102
|
+
if (!slsa) return null
|
|
103
|
+
// The statement, whatever its length (the answer is bounded as a whole),
|
|
104
|
+
// unlike the fields read out of it.
|
|
105
|
+
const payload = field(slsa, 'bundle', 'dsseEnvelope', 'payload')
|
|
106
|
+
let statement: unknown
|
|
107
|
+
try { statement = JSON.parse(Buffer.from(typeof payload === 'string' ? payload : '', 'base64').toString('utf8')) } catch { return null }
|
|
108
|
+
// Only one naming this version's tarball: its purl, and its digest as the
|
|
109
|
+
// version's integrity has it.
|
|
110
|
+
const purl = `pkg:npm/${name.replace(/^@/u, '%40')}@${version}`
|
|
111
|
+
const digest = /^sha512-(.+)$/u.exec(integrity)?.[1]
|
|
112
|
+
if (!digest || !listAt(statement, 'subject').some(subject => field(subject, 'name') === purl && field(subject, 'digest', 'sha512') === Buffer.from(digest, 'base64').toString('hex'))) return null
|
|
113
|
+
const provenance = readStatement(text(field(slsa, 'predicateType'))!, field(statement, 'predicate'))
|
|
114
|
+
const logIndex = Number(field(slsa, 'bundle', 'verificationMaterial', 'tlogEntries', '0', 'logIndex'))
|
|
115
|
+
return provenance && { ...provenance, logIndex: Number.isSafeInteger(logIndex) && logIndex >= 0 ? logIndex : null, runner: runnerOf(field(slsa, 'bundle')) }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const provenances = kept<NpmProvenance | null>(1000)
|
|
119
|
+
|
|
120
|
+
// npm's provenance for a version of a public package, its tarball's
|
|
121
|
+
// `integrity` as the registry has it; null where npm keeps none for it.
|
|
122
|
+
export function npmProvenance(name: string, version: string, integrity: string): Promise<NpmProvenance | null> {
|
|
123
|
+
return provenances(`${name}@${version}`, () => askProvenance(name, version, integrity))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// One kept, asking for nothing; undefined where none is.
|
|
127
|
+
export const keptNpmProvenance = (name: string, version: string) => provenances.peek(`${name}@${version}`)
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// What calls for a look at an npm version, as risk tells it (risk/), for the
|
|
2
|
+
// managed viewer: risk is asked on the server alone, its records kept where
|
|
3
|
+
// each belongs by its type (risk/npm/registry.js, assess.js):
|
|
4
|
+
//
|
|
5
|
+
// - `risk/npm/assessments`, what of an assessment holds whatever the time:
|
|
6
|
+
// a public package's in the public npm cache, as its pretty-printed files
|
|
7
|
+
// are (storage-payload.ts PUBLIC_CACHE), by a hash of its key; a private
|
|
8
|
+
// one's in this instance's memory alone.
|
|
9
|
+
// - `risk/npm/versions`, what a version's document says, which npm never
|
|
10
|
+
// changes once published: a public package's in the database's upstream
|
|
11
|
+
// cache table, shared by every instance; a private one's in memory.
|
|
12
|
+
// - `risk/npm/downloads`, a package's downloads a day, which risk tells as
|
|
13
|
+
// kept for a week: a public package's in the database too, so that another
|
|
14
|
+
// instance, or this one restarted, asks npm's downloads API, which answers
|
|
15
|
+
// some 40 lookups a minute, none of them again that week, each removed a
|
|
16
|
+
// day past it (npm-kept.ts pruneKept); a private one's in memory.
|
|
17
|
+
// - `risk/npm/packages`, `risk/npm/old-versions`, `risk/npm/public` and
|
|
18
|
+
// `risk/npm/advisories`, which risk asks again after an hour: a public
|
|
19
|
+
// package's in memory and in the database too, read from memory first, so
|
|
20
|
+
// that a cold instance, as a serverless one starts, reads no whole document
|
|
21
|
+
// of npm's again that one before it read within the hour; by a hash of
|
|
22
|
+
// their type and key, as a dependency's spec runs to any length, each
|
|
23
|
+
// removed a day on, and one past 256 KiB as JSON (a package's deprecated
|
|
24
|
+
// versions, its messages and all, run long) in memory alone. A private
|
|
25
|
+
// one's in memory.
|
|
26
|
+
// - `npm/tarballs`: none asked, as tarballs are read as the viewer's loads
|
|
27
|
+
// read them, kept where they keep them (npm-loads.ts loadNpmTarball), and
|
|
28
|
+
// an assessment kept needs none.
|
|
29
|
+
// - Upstream's own, as risk asks a version's repository's advisories (its
|
|
30
|
+
// listing, `github/advisories`, and `npm/public`): `upstream`, the caller's
|
|
31
|
+
// store of them (known-listings.ts), else none.
|
|
32
|
+
//
|
|
33
|
+
// What memory keeps is held to 2,000 records and 64 MiB as JSON, the first
|
|
34
|
+
// kept going first.
|
|
35
|
+
//
|
|
36
|
+
// A cache failure is a miss, never a failed assessment. Packages' documents
|
|
37
|
+
// are read as every other npm document here is, within their bounds and
|
|
38
|
+
// shared budget (npm-packages.ts readNpmPackument), and tarballs as the
|
|
39
|
+
// viewer's loads read them, bounded as they arrive and among the loads made
|
|
40
|
+
// at once. Assessments made at once are bounded as those loads are, each
|
|
41
|
+
// unpacking up to two tarballs: one past them is refused as busy.
|
|
42
|
+
import { Buffer } from 'node:buffer'
|
|
43
|
+
import { createHash } from 'node:crypto'
|
|
44
|
+
import type { Client } from '@preventive/upstream/github.js'
|
|
45
|
+
import type { CacheStore } from '@preventive/upstream/npm.js'
|
|
46
|
+
import { DEFAULT_MINIMUM_RELEASE_AGE, type Finding, type NpmAssessment, assessNpm, npmMalware, npmReadyDownload, npmReadyDownloads } from '../risk/index.js'
|
|
47
|
+
import { readBytes } from './blob-store.ts'
|
|
48
|
+
import { CacheMissError, type CacheStorage } from './cache-storage.ts'
|
|
49
|
+
import { npmVersionStore, pruneKept } from './npm-kept.ts'
|
|
50
|
+
import { loadNpmTarball } from './npm-loads.ts'
|
|
51
|
+
import { NpmPackageError, type NpmVersionDocument, npmTokenReads, readNpmPackument, readNpmVersion } from './npm-packages.ts'
|
|
52
|
+
import { npmPackageScope } from '../common/managed/npm-packages.js'
|
|
53
|
+
import type { UpstreamCacheStore } from './upstream-cache.ts'
|
|
54
|
+
|
|
55
|
+
const MEMORY_ENTRIES = 2000
|
|
56
|
+
// As npm-loads.ts MAX_ACTIVE_LOADS.
|
|
57
|
+
const MAX_ACTIVE_ASSESSMENTS = 4
|
|
58
|
+
// Of them, those no reader waits on (`spare`), such as a scan's: the rest are
|
|
59
|
+
// left for readers.
|
|
60
|
+
export const MAX_SPARE_ASSESSMENTS = 2
|
|
61
|
+
let assessing = 0
|
|
62
|
+
// What memory holds at most, by its records' length as JSON: the first kept
|
|
63
|
+
// go first past it, and a record past an eighth of it isn't kept at all.
|
|
64
|
+
const MEMORY_LENGTH = 64 * 1024 * 1024
|
|
65
|
+
let memoryLength = 0
|
|
66
|
+
const memory = new Map<string, { value: unknown; length: number }>()
|
|
67
|
+
function forget(key: string) {
|
|
68
|
+
const kept = memory.get(key)
|
|
69
|
+
if (!kept) return
|
|
70
|
+
memory.delete(key)
|
|
71
|
+
memoryLength -= kept.length
|
|
72
|
+
}
|
|
73
|
+
function remember(key: string, value: unknown) {
|
|
74
|
+
forget(key)
|
|
75
|
+
const length = JSON.stringify(value)?.length ?? 0
|
|
76
|
+
if (length > MEMORY_LENGTH / 8) return
|
|
77
|
+
memory.set(key, { value, length })
|
|
78
|
+
memoryLength += length
|
|
79
|
+
while (memory.size > MEMORY_ENTRIES || memoryLength > MEMORY_LENGTH) forget(memory.keys().next().value!)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const digest = (type: string, key: string) => createHash('sha256').update(`${type}\0${key}`).digest('hex')
|
|
83
|
+
const hashed = (type: string, key: string) => `risk/${digest(type, key)}`
|
|
84
|
+
// What of a public package is kept in the database by its key, and for how
|
|
85
|
+
// long its downloads are: risk's week, and a day more.
|
|
86
|
+
const IN_DATABASE = new Set(['risk/npm/versions', 'risk/npm/downloads'])
|
|
87
|
+
const DOWNLOADS_KEPT_MS = 8 * 24 * 60 * 60_000
|
|
88
|
+
// What of a public package risk asks again after an hour, kept in memory and
|
|
89
|
+
// in the database (HOURLY_PREFIX), each a day, none past HOURLY_LENGTH as
|
|
90
|
+
// JSON there.
|
|
91
|
+
const HOURLY = new Set(['risk/npm/packages', 'risk/npm/old-versions', 'risk/npm/public', 'risk/npm/advisories'])
|
|
92
|
+
const HOURLY_PREFIX = 'risk/hourly/'
|
|
93
|
+
const HOURLY_KEPT_MS = 24 * 60 * 60_000
|
|
94
|
+
const HOURLY_LENGTH = 256 * 1024
|
|
95
|
+
const databaseKey = (type: string, key: string) => HOURLY.has(type) ? `${HOURLY_PREFIX}${digest(type, key)}` : `${type}/${key}`
|
|
96
|
+
|
|
97
|
+
export interface NpmRiskStores {
|
|
98
|
+
db: UpstreamCacheStore
|
|
99
|
+
npmCache?: CacheStorage | undefined
|
|
100
|
+
upstream?: CacheStore | undefined
|
|
101
|
+
debug?: boolean | undefined
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// A cache that reads and writes many of a type at once too, as risk's
|
|
105
|
+
// readying of downloads asks (npmReadyDownloads), and reads records of any
|
|
106
|
+
// types at once, as risk reads a package's record and its versions'
|
|
107
|
+
// (registry.js npmPackageFacts): one read and one write of the database each,
|
|
108
|
+
// not one a key, as a request's queries take turns on its connection
|
|
109
|
+
// (db-neon.ts).
|
|
110
|
+
export interface NpmRiskCache extends CacheStore {
|
|
111
|
+
readEach(entries: [type: string, key: string][]): Promise<unknown[]>
|
|
112
|
+
readMany(type: string, keys: string[]): Promise<Map<string, unknown>>
|
|
113
|
+
writeMany(type: string, entries: [key: string, value: unknown][]): Promise<void>
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Risk's cache for one package: `isPublic` whether anyone can read it, so
|
|
117
|
+
// that what is kept of it can be shared.
|
|
118
|
+
export function npmRiskCache({ db, npmCache, upstream, debug }: NpmRiskStores, isPublic: boolean): NpmRiskCache {
|
|
119
|
+
const warn = (what: string, error: unknown) => { if (debug) console.warn(`managed: risk cache ${what} failed:`, error) }
|
|
120
|
+
// The rows under `prefix` kept `keptMs` before `at` removed, as rows under
|
|
121
|
+
// it are written (pruneKept).
|
|
122
|
+
const prune = (prefix: string, keptMs: number, at: number) => pruneKept(db, prefix, keptMs, at, error => { warn('prune', error) })
|
|
123
|
+
const memoryKey = (type: string, key: string) => `${isPublic ? '' : 'private\0'}${type}\0${key}`
|
|
124
|
+
// Whether a record of `type` is read of the database: unless it is one
|
|
125
|
+
// kept in memory too (HOURLY) and there.
|
|
126
|
+
const ofDatabase = (type: string, key: string) => isPublic && (IN_DATABASE.has(type) || HOURLY.has(type) && !memory.has(memoryKey(type, key)))
|
|
127
|
+
// A record as the database keeps it, read: an hourly one kept in memory
|
|
128
|
+
// too, for the next to ask.
|
|
129
|
+
const readOf = (type: string, key: string, row: string | null | undefined) => {
|
|
130
|
+
const value: unknown = row == null ? null : JSON.parse(row)
|
|
131
|
+
if (value !== null && HOURLY.has(type)) remember(memoryKey(type, key), value)
|
|
132
|
+
return value
|
|
133
|
+
}
|
|
134
|
+
const cache: NpmRiskCache = {
|
|
135
|
+
async readEach(entries) {
|
|
136
|
+
const asked = new Set(entries.flatMap(([type, key], i) => ofDatabase(type, key) ? [i] : []))
|
|
137
|
+
let rows = new Map<string, string>()
|
|
138
|
+
if (asked.size > 0) {
|
|
139
|
+
try { rows = await db.getUpstreamCacheEntries([...asked].map(i => databaseKey(...entries[i]!))) } catch (error) { warn('read', error) }
|
|
140
|
+
}
|
|
141
|
+
return Promise.all(entries.map(([type, key], i) => {
|
|
142
|
+
if (!asked.has(i)) return cache.read(type, key)
|
|
143
|
+
try { return readOf(type, key, rows.get(databaseKey(type, key))) } catch (error) { warn('read', error); return null }
|
|
144
|
+
}))
|
|
145
|
+
},
|
|
146
|
+
async readMany(type, keys) {
|
|
147
|
+
const values = await cache.readEach(keys.map(key => [type, key]))
|
|
148
|
+
return new Map(keys.map((key, i) => [key, values[i]]))
|
|
149
|
+
},
|
|
150
|
+
async writeMany(type, entries) {
|
|
151
|
+
if (!isPublic || !IN_DATABASE.has(type)) { for (const [key, value] of entries) await cache.write(type, key, value); return }
|
|
152
|
+
const at = Date.now()
|
|
153
|
+
try { await db.setUpstreamCacheEntries(entries.map(([key, value]) => [`${type}/${key}`, JSON.stringify(value)]), at) } catch (error) { warn('write', error); return }
|
|
154
|
+
if (type === 'risk/npm/downloads') await prune(`${type}/`, DOWNLOADS_KEPT_MS, at)
|
|
155
|
+
},
|
|
156
|
+
async read(type, key) {
|
|
157
|
+
if (!type.startsWith('risk/')) return type === 'npm/tarballs' ? null : await upstream?.read(type, key) ?? null
|
|
158
|
+
try {
|
|
159
|
+
if (isPublic && type === 'risk/npm/assessments' && npmCache) return JSON.parse((await readBytes(await npmCache.open(hashed(type, key)))).toString('utf8'))
|
|
160
|
+
if (ofDatabase(type, key)) return readOf(type, key, await db.getUpstreamCacheEntry(databaseKey(type, key)))
|
|
161
|
+
} catch (error) { if (!(error instanceof CacheMissError)) warn('read', error); return null }
|
|
162
|
+
return memory.get(memoryKey(type, key))?.value ?? null
|
|
163
|
+
},
|
|
164
|
+
async write(type, key, value) {
|
|
165
|
+
if (!type.startsWith('risk/')) { if (type !== 'npm/tarballs') await upstream?.write(type, key, value); return }
|
|
166
|
+
if (isPublic && HOURLY.has(type)) remember(memoryKey(type, key), value)
|
|
167
|
+
try {
|
|
168
|
+
if (isPublic && type === 'risk/npm/assessments' && npmCache) { await npmCache.put(hashed(type, key), Buffer.from(JSON.stringify(value))); return }
|
|
169
|
+
if (isPublic && IN_DATABASE.has(type)) {
|
|
170
|
+
const at = Date.now()
|
|
171
|
+
await db.setUpstreamCacheEntry(databaseKey(type, key), JSON.stringify(value), at)
|
|
172
|
+
if (type === 'risk/npm/downloads') await prune(`${type}/`, DOWNLOADS_KEPT_MS, at)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
if (isPublic && HOURLY.has(type)) {
|
|
176
|
+
const json = JSON.stringify(value)
|
|
177
|
+
if (json.length > HOURLY_LENGTH) return
|
|
178
|
+
const at = Date.now()
|
|
179
|
+
await db.setUpstreamCacheEntry(databaseKey(type, key), json, at)
|
|
180
|
+
await prune(HOURLY_PREFIX, HOURLY_KEPT_MS, at)
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
} catch (error) { warn('write', error); return }
|
|
184
|
+
remember(memoryKey(type, key), value)
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
return cache
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// A version's tarball as the viewer's loads read it (npm-loads.ts), its bytes
|
|
191
|
+
// held to the integrity its reader's document names: given up on, and the
|
|
192
|
+
// load released, once `signal` aborts, the load going on for its other
|
|
193
|
+
// readers, so that a reader gone holds no assessment's place.
|
|
194
|
+
async function tarballOf(doc: NpmVersionDocument | undefined, signal: AbortSignal): Promise<Uint8Array> {
|
|
195
|
+
if (!doc) throw new NpmPackageError(502, 'upstream-invalid')
|
|
196
|
+
signal.throwIfAborted()
|
|
197
|
+
const load = loadNpmTarball(doc)
|
|
198
|
+
let onAbort = () => {}
|
|
199
|
+
const aborted = new Promise<never>((_, reject) => { onAbort = () => reject(signal.reason); signal.addEventListener('abort', onAbort, { once: true }) })
|
|
200
|
+
try { return await Promise.race([load.result, aborted]) } finally {
|
|
201
|
+
signal.removeEventListener('abort', onAbort)
|
|
202
|
+
load.release()
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The version `doc` names assessed against `old`'s, where given, each as the
|
|
207
|
+
// reader's readNpmVersion read it, or against the one risk picks of its
|
|
208
|
+
// versions (`'pick'`, assessNpm `oldVersion: true`), its document read as the
|
|
209
|
+
// open version's is where its tarball is wanted: trusted however new and however few
|
|
210
|
+
// download it where a team lists its scope (`teamScoped`), its own packages;
|
|
211
|
+
// else held to risk's minimum release age and flagged as unpopular. A new
|
|
212
|
+
// dependency on a package of a scope a team lists (`ownScope`, asked once a
|
|
213
|
+
// scope) is told no tier nor age, as an own package. Its
|
|
214
|
+
// documents are read with npm's token where the reader may have it
|
|
215
|
+
// (`privileged`, as readNpmVersion), its dependencies' without; what is kept
|
|
216
|
+
// of it is shared only where neither version is private, and OSV is asked of
|
|
217
|
+
// its advisories only where it isn't, a private package's name never sent to
|
|
218
|
+
// OSV, as it never is to Socket (http.ts); with `github`, its repository's
|
|
219
|
+
// too where it isn't private (assessNpm `github`). A `spare` one, no
|
|
220
|
+
// reader waiting on it, is made only within MAX_SPARE_ASSESSMENTS, and tells
|
|
221
|
+
// its dependencies without asking npm of their packages (assessNpm
|
|
222
|
+
// `dependencies`), nor its advisories, which a scan asks of a lockfile's
|
|
223
|
+
// packages at once (repository-risk.ts).
|
|
224
|
+
export function npmRisk(doc: NpmVersionDocument, old: NpmVersionDocument | 'pick' | null,
|
|
225
|
+
{ teamScoped, ownScope = () => Promise.resolve(false), fromEmpty = false, privileged, signal, spare = false, github = null, ...stores }: NpmRiskStores & { teamScoped: boolean;
|
|
226
|
+
ownScope?: (scope: string) => Promise<boolean>; fromEmpty?: boolean; privileged: boolean; signal: AbortSignal; spare?: boolean; github?: Client | null }): Promise<NpmAssessment> {
|
|
227
|
+
const scopes = new Map<string, Promise<boolean>>()
|
|
228
|
+
const owned = (name: string) => {
|
|
229
|
+
const scope = npmPackageScope(name)
|
|
230
|
+
if (scope === null) return false
|
|
231
|
+
if (!scopes.has(scope)) scopes.set(scope, ownScope(scope))
|
|
232
|
+
return scopes.get(scope)!
|
|
233
|
+
}
|
|
234
|
+
const given = old === 'pick' ? null : old
|
|
235
|
+
// The one picked, read as the viewer reads a version, kept a day where public.
|
|
236
|
+
const documentOf = async (version: string) => [doc, given].find(read => read?.version === version)
|
|
237
|
+
?? await readNpmVersion(doc.name, version, privileged, signal, npmVersionStore(stores.db, stores.debug)) ?? undefined
|
|
238
|
+
return bounded(spare, () => assessNpm(doc.name, doc.version, { oldVersion: old === 'pick' ? true : old?.version ?? null, fromEmpty,
|
|
239
|
+
minimumReleaseAge: teamScoped ? 0 : DEFAULT_MINIMUM_RELEASE_AGE,
|
|
240
|
+
popularity: !teamScoped, cache: npmRiskCache(stores, !doc.private && !given?.private), signal, dependencies: !spare, advisories: !spare, osv: !doc.private,
|
|
241
|
+
github: doc.private ? null : github, owned,
|
|
242
|
+
packument: (name, { authorized }) => readNpmPackument(name, privileged && authorized, signal),
|
|
243
|
+
tarball: async (_name, version) => tarballOf(await documentOf(version), signal) }))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// The malware finding npmRisk would tell of the version `doc` names, alone
|
|
247
|
+
// (risk npmMalware): its package taken down by npm, and npm's advisories of
|
|
248
|
+
// malware naming it, OSV's too where it isn't private, read and kept as
|
|
249
|
+
// npmRisk reads and keeps them, but its repository's, which a caller wanting
|
|
250
|
+
// them asks itself (as http.ts npmAdvisories does); none of its tarball nor
|
|
251
|
+
// its dependencies read, so none of the assessments' places taken. Its
|
|
252
|
+
// package's whole document as `packument` reads it, where given, as for its
|
|
253
|
+
// reader (`privileged`) it would be read: a caller reading it too reads it
|
|
254
|
+
// once. Null where none tells of any; an NpmRiskError where npm has no such
|
|
255
|
+
// version, nor malware explaining one removed, or can't be asked.
|
|
256
|
+
export function npmRiskMalware(doc: NpmVersionDocument, { privileged, signal, packument, ...stores }: NpmRiskStores & { privileged: boolean; signal: AbortSignal;
|
|
257
|
+
packument?: () => Promise<Record<string, unknown> | null> }): Promise<Finding | null> {
|
|
258
|
+
return npmMalware(doc.name, doc.version, { cache: npmRiskCache(stores, !doc.private), signal, osv: !doc.private,
|
|
259
|
+
packument: (name, { authorized }) => packument && name === doc.name && authorized ? packument() : readNpmPackument(name, privileged && authorized, signal) })
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// An assessment made within MAX_ACTIVE_ASSESSMENTS, a `spare` one within
|
|
263
|
+
// MAX_SPARE_ASSESSMENTS, all across the server; one past them refused as
|
|
264
|
+
// busy (`npm-busy`).
|
|
265
|
+
async function bounded(spare: boolean, assess: () => Promise<NpmAssessment>): Promise<NpmAssessment> {
|
|
266
|
+
if (assessing >= (spare ? MAX_SPARE_ASSESSMENTS : MAX_ACTIVE_ASSESSMENTS)) throw new NpmPackageError(429, 'npm-busy')
|
|
267
|
+
assessing++
|
|
268
|
+
try { return await assess() } finally { assessing-- }
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// A version npm has no document of, as risk tells one npm removed, as it
|
|
272
|
+
// removes every one it finds malware in (assessNpm): its package taken down
|
|
273
|
+
// by npm, as a scan's (`spare`), and its advisories of malware, npm's and,
|
|
274
|
+
// where anyone can read its package, OSV's, which alone has some of those
|
|
275
|
+
// npm removed (nx's), a private package's name never sent to OSV. Its
|
|
276
|
+
// package's document read anonymously, else with npm's token alone where
|
|
277
|
+
// that reads more for this reader (`privileged`, npmTokenReads), and kept as
|
|
278
|
+
// a private package's is; no
|
|
279
|
+
// tarball read, nothing of the version being left. Spare, within
|
|
280
|
+
// MAX_SPARE_ASSESSMENTS as a scan's others are. An NpmRiskError where
|
|
281
|
+
// nothing tells of it.
|
|
282
|
+
export function npmRiskRemoved(name: string, version: string,
|
|
283
|
+
{ teamScoped, privileged, signal, ...stores }: NpmRiskStores & { teamScoped: boolean; privileged: boolean; signal: AbortSignal }): Promise<NpmAssessment> {
|
|
284
|
+
return bounded(true, async () => {
|
|
285
|
+
const open = await readNpmPackument(name, false, signal)
|
|
286
|
+
return assessNpm(name, version, { minimumReleaseAge: teamScoped ? 0 : DEFAULT_MINIMUM_RELEASE_AGE, popularity: false, downloads: false, dependencies: false,
|
|
287
|
+
osv: open !== null, cache: npmRiskCache(stores, false), signal,
|
|
288
|
+
packument: (read, { authorized }) => read === name
|
|
289
|
+
? open || !npmTokenReads(name, privileged && authorized) ? Promise.resolve(open) : readNpmPackument(name, true, signal, { anonymous: false })
|
|
290
|
+
: readNpmPackument(read, privileged && authorized, signal),
|
|
291
|
+
tarball: () => Promise.reject(new NpmPackageError(502, 'upstream-invalid')) })
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// What a scan readies before assessing its packages (repository-risk.ts):
|
|
296
|
+
// their downloads, as risk asks them, kept where a public package's are
|
|
297
|
+
// (npmRiskCache), so that their assessments wait on npm's downloads API for
|
|
298
|
+
// none. At once, those kept and unscoped ones, in bulk, all public, as only a
|
|
299
|
+
// scoped package can be private; each other one on its own
|
|
300
|
+
// (npmReadyScanDownload), kept only where npm counts some, which it does of
|
|
301
|
+
// public packages alone: a private one's none, as of one it has no such
|
|
302
|
+
// package of, isn't kept where public ones are, for its assessment to ask
|
|
303
|
+
// its own where private ones are.
|
|
304
|
+
export const npmReadyScanDownloads = (names: string[], ready: (name: string) => void, stores: NpmRiskStores, signal: AbortSignal) =>
|
|
305
|
+
npmReadyDownloads(names, ready, { cache: npmRiskCache(stores, true), signal })
|
|
306
|
+
export function npmReadyScanDownload(name: string, stores: NpmRiskStores, signal: AbortSignal) {
|
|
307
|
+
const cache = npmRiskCache(stores, true)
|
|
308
|
+
const counted = (value: unknown) => !(value && typeof value === 'object' && 'none' in value)
|
|
309
|
+
return npmReadyDownload(name, { cache: { read: cache.read, write: (type, key, value) => counted(value) ? cache.write(type, key, value) : Promise.resolve() }, signal })
|
|
310
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { link, lstat, mkdir, open, readdir, rename, rm, rmdir, stat } from 'node:fs/promises'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import { type ObjectPage, type RawObjectStorage, objectPath, validateStoragePage } from './object-storage.ts'
|
|
5
|
+
|
|
6
|
+
function missing(err: unknown): boolean { return (err as NodeJS.ErrnoException).code === 'ENOENT' }
|
|
7
|
+
function version(info: { dev: number; ino: number; size: number; mtimeMs: number; ctimeMs: number }): string {
|
|
8
|
+
return JSON.stringify([info.dev, info.ino, info.size, info.mtimeMs, info.ctimeMs])
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function syncDirectory(dir: string): Promise<void> {
|
|
12
|
+
try {
|
|
13
|
+
const directory = await open(dir, 'r')
|
|
14
|
+
try { await directory.sync() } finally { await directory.close() }
|
|
15
|
+
} catch (err) {
|
|
16
|
+
// Windows and some mounted filesystems cannot open/fsync directories.
|
|
17
|
+
// Preserve real I/O failures; file fsync remains mandatory on every write.
|
|
18
|
+
if (!['EINVAL', 'ENOTSUP', 'EOPNOTSUPP', 'ENOSYS', 'EISDIR', 'EBADF', 'EPERM'].includes((err as NodeJS.ErrnoException).code ?? '')) throw err
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Keyset pagination survives concurrent file creation/deletion. A final sweep
|
|
23
|
+
// catches objects inserted before the cursor during migration.
|
|
24
|
+
async function listFiles(dir: string, prefix: string, cursor: string | null, limit: number, signal?: AbortSignal): Promise<ObjectPage> {
|
|
25
|
+
const objects: ObjectPage['objects'] = []
|
|
26
|
+
async function visit(key: string): Promise<void> {
|
|
27
|
+
signal?.throwIfAborted()
|
|
28
|
+
let entries
|
|
29
|
+
try {
|
|
30
|
+
if (!(await lstat(join(dir, key))).isDirectory()) {
|
|
31
|
+
console.warn('managed-storage: skipping unsupported directory', JSON.stringify(key))
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
entries = await readdir(join(dir, key), { withFileTypes: true })
|
|
35
|
+
}
|
|
36
|
+
catch (err) { if (missing(err)) return; throw err }
|
|
37
|
+
const order = (entry: typeof entries[number]) => `${entry.name}${entry.isDirectory() ? '/' : ''}`
|
|
38
|
+
entries.sort((a, b) => order(a) < order(b) ? -1 : order(a) > order(b) ? 1 : 0)
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
signal?.throwIfAborted()
|
|
41
|
+
const child = `${key}${entry.name}`
|
|
42
|
+
if (entry.isSymbolicLink() || (!entry.isDirectory() && !entry.isFile())) {
|
|
43
|
+
console.warn('managed-storage: skipping unsupported entry', JSON.stringify(child))
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
try { objectPath(child) }
|
|
47
|
+
catch { console.warn('managed-storage: skipping invalid entry', JSON.stringify(child)); continue }
|
|
48
|
+
if (entry.isDirectory()) {
|
|
49
|
+
const folder = `${child}/`
|
|
50
|
+
if (cursor && folder < cursor && !cursor.startsWith(folder)) continue
|
|
51
|
+
await visit(folder)
|
|
52
|
+
if (objects.length > limit) return
|
|
53
|
+
}
|
|
54
|
+
else if (entry.isFile() && (!cursor || child > cursor)) {
|
|
55
|
+
try { objects.push({ key: objectPath(child), modifiedAt: (await stat(join(dir, child))).mtimeMs }) }
|
|
56
|
+
catch (err) { if (!missing(err)) throw err }
|
|
57
|
+
if (objects.length > limit) return
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Another migration worker can observe an unlink before its writer has
|
|
61
|
+
// synced the directory. Persist those observed removals before an empty
|
|
62
|
+
// inventory can mark the shared migration complete.
|
|
63
|
+
try { await syncDirectory(join(dir, key)) } catch (err) { if (!missing(err)) throw err }
|
|
64
|
+
}
|
|
65
|
+
validateStoragePage(prefix, limit)
|
|
66
|
+
await visit(prefix)
|
|
67
|
+
return { objects: objects.slice(0, limit), cursor: objects.length > limit ? objects[limit - 1]!.key : null }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createDiskObjectStorage(dir: string): RawObjectStorage {
|
|
71
|
+
const path = (key: string) => join(dir, objectPath(key))
|
|
72
|
+
return {
|
|
73
|
+
async head(key, signal) {
|
|
74
|
+
signal?.throwIfAborted()
|
|
75
|
+
try {
|
|
76
|
+
const info = await stat(path(key))
|
|
77
|
+
return { size: info.size, version: version(info), modifiedAt: info.mtimeMs }
|
|
78
|
+
} catch (err) { if (missing(err)) return null; throw err }
|
|
79
|
+
},
|
|
80
|
+
async open(key, signal) {
|
|
81
|
+
signal?.throwIfAborted()
|
|
82
|
+
let file
|
|
83
|
+
try { file = await open(path(key), 'r') }
|
|
84
|
+
catch (err) { if (missing(err)) return null; throw err }
|
|
85
|
+
try {
|
|
86
|
+
const info = await file.stat()
|
|
87
|
+
return { size: info.size, version: version(info), modifiedAt: info.mtimeMs,
|
|
88
|
+
stream: file.createReadStream(signal ? { signal } : {}) }
|
|
89
|
+
} catch (err) { await file.close(); throw err }
|
|
90
|
+
},
|
|
91
|
+
async put(key, bytes, signal, expected) {
|
|
92
|
+
const target = path(key)
|
|
93
|
+
// Temp-file + rename makes replacement atomic to readers. Concurrent
|
|
94
|
+
// migrations use the same persisted data key and immutable plaintext.
|
|
95
|
+
const temp = `${target}.${randomUUID()}.tmp`
|
|
96
|
+
await mkdir(dirname(target), { recursive: true })
|
|
97
|
+
const file = await open(temp, 'wx', 0o600)
|
|
98
|
+
try {
|
|
99
|
+
await file.writeFile(bytes, signal ? { signal } : {})
|
|
100
|
+
await file.sync(); await file.close()
|
|
101
|
+
signal?.throwIfAborted()
|
|
102
|
+
if (expected === null) {
|
|
103
|
+
try { await link(temp, target) }
|
|
104
|
+
catch (err) { if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false; throw err }
|
|
105
|
+
} else if (expected === undefined) await rename(temp, target)
|
|
106
|
+
else {
|
|
107
|
+
try { if (version(await stat(target)) !== expected) return false }
|
|
108
|
+
catch (err) { if (missing(err)) return false; throw err }
|
|
109
|
+
await rename(temp, target)
|
|
110
|
+
}
|
|
111
|
+
await syncDirectory(dirname(target))
|
|
112
|
+
return true
|
|
113
|
+
} finally { await file.close(); await rm(temp, { force: true }) }
|
|
114
|
+
},
|
|
115
|
+
async delete(key, expected, signal) {
|
|
116
|
+
signal?.throwIfAborted()
|
|
117
|
+
try {
|
|
118
|
+
if (expected !== undefined && version(await stat(path(key))) !== expected) return false
|
|
119
|
+
await rm(path(key), { force: true })
|
|
120
|
+
// Persist plaintext removal before migration checkpoints completion.
|
|
121
|
+
// Otherwise a crash could restore the directory entry after SQL has
|
|
122
|
+
// durably disabled further plaintext inventory sweeps.
|
|
123
|
+
await syncDirectory(dirname(path(key)))
|
|
124
|
+
return true
|
|
125
|
+
} catch (err) { if (missing(err)) return true; throw err }
|
|
126
|
+
},
|
|
127
|
+
async sync(key, signal) { signal?.throwIfAborted(); await syncDirectory(dirname(path(key))) },
|
|
128
|
+
async prune(prefix, signal) {
|
|
129
|
+
if (!prefix.endsWith('/')) throw new Error('Invalid storage prefix')
|
|
130
|
+
objectPath(`${prefix}validate`)
|
|
131
|
+
async function visit(folder: string): Promise<void> {
|
|
132
|
+
signal?.throwIfAborted()
|
|
133
|
+
let entries
|
|
134
|
+
try {
|
|
135
|
+
if (!(await lstat(folder)).isDirectory()) return
|
|
136
|
+
entries = await readdir(folder, { withFileTypes: true })
|
|
137
|
+
}
|
|
138
|
+
catch (err) { if (missing(err)) return; throw err }
|
|
139
|
+
for (const entry of entries) if (entry.isDirectory()) await visit(join(folder, entry.name))
|
|
140
|
+
try { await rmdir(folder); await syncDirectory(dirname(folder)) }
|
|
141
|
+
catch (err) { if (!['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes((err as NodeJS.ErrnoException).code ?? '')) throw err }
|
|
142
|
+
}
|
|
143
|
+
await visit(join(dir, prefix))
|
|
144
|
+
},
|
|
145
|
+
list: (prefix, cursor, limit, signal) => listFiles(dir, prefix, cursor, limit, signal),
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Readable } from 'node:stream'
|
|
2
|
+
import { Buffer } from 'node:buffer'
|
|
3
|
+
import { type VercelBlobSdk, isNotFound, loadVercelBlobSdk } from '../server-common/vercel-blob.ts'
|
|
4
|
+
import { type RawObjectStorage, objectPath, validateStoragePage } from './object-storage.ts'
|
|
5
|
+
|
|
6
|
+
// The SDK's abort option, where there is a signal to give it; and when a blob
|
|
7
|
+
// was last written, now where the SDK does not say.
|
|
8
|
+
const abortable = (signal?: AbortSignal) => signal ? { abortSignal: signal } : {}
|
|
9
|
+
const modifiedAt = (uploadedAt?: Date | string | number) => new Date(uploadedAt ?? Date.now()).getTime()
|
|
10
|
+
|
|
11
|
+
export async function openVercelObjectStorage(token: string, sdk?: VercelBlobSdk): Promise<RawObjectStorage> {
|
|
12
|
+
const blobs = sdk ?? await loadVercelBlobSdk()
|
|
13
|
+
const path = (key: string) => `.managed/${objectPath(key)}`
|
|
14
|
+
const storage: RawObjectStorage = {
|
|
15
|
+
async head(key, signal) {
|
|
16
|
+
try {
|
|
17
|
+
const meta = await blobs.head(path(key), { token, ...abortable(signal) })
|
|
18
|
+
if (!meta.etag) throw new Error('Missing object version')
|
|
19
|
+
return { size: meta.size, version: meta.etag, modifiedAt: modifiedAt(meta.uploadedAt) }
|
|
20
|
+
} catch (err) { if (isNotFound(err, blobs)) return null; throw err }
|
|
21
|
+
},
|
|
22
|
+
async open(key, signal) {
|
|
23
|
+
try {
|
|
24
|
+
// Read the stored representation: transport compression can weaken its
|
|
25
|
+
// ETag, which cannot satisfy ifMatch, and changes the advertised size.
|
|
26
|
+
const result = await blobs.get(path(key), { token, access: 'private', useCache: false,
|
|
27
|
+
headers: { 'accept-encoding': 'identity' }, ...abortable(signal) })
|
|
28
|
+
if (!result) return null
|
|
29
|
+
if (result.statusCode !== 200 || !result.stream || !result.blob.etag) {
|
|
30
|
+
await result.stream?.cancel()
|
|
31
|
+
throw new Error('Unexpected blob response or missing object version')
|
|
32
|
+
}
|
|
33
|
+
return { size: result.blob.size === 0 ? null : result.blob.size, version: result.blob.etag,
|
|
34
|
+
modifiedAt: modifiedAt(result.blob.uploadedAt),
|
|
35
|
+
stream: Readable.fromWeb(result.stream as Parameters<typeof Readable.fromWeb>[0], signal ? { signal } : {}) }
|
|
36
|
+
} catch (err) { if (isNotFound(err, blobs)) return null; throw err }
|
|
37
|
+
},
|
|
38
|
+
async put(key, bytes, signal, expected, sizeHint) {
|
|
39
|
+
const multipart = (sizeHint ?? (Buffer.isBuffer(bytes) ? bytes.length : Infinity)) >= 5 * 1024 * 1024
|
|
40
|
+
try { await blobs.put(path(key), bytes, { token, access: 'private', addRandomSuffix: false, allowOverwrite: expected !== null,
|
|
41
|
+
...(expected ? { ifMatch: expected } : {}), multipart, contentType: 'application/octet-stream', cacheControlMaxAge: 60, ...abortable(signal) })
|
|
42
|
+
return true
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (blobs.BlobPreconditionFailedError && err instanceof blobs.BlobPreconditionFailedError) return false
|
|
45
|
+
// The SDK uses a generic error for an occupied pathname. Reconcile a
|
|
46
|
+
// create-only failure against storage; the caller verifies the winner.
|
|
47
|
+
if (expected === null && await storage.head(key, signal)) return false
|
|
48
|
+
throw err
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
async delete(key, version, signal) {
|
|
52
|
+
signal?.throwIfAborted()
|
|
53
|
+
try { await blobs.del(path(key), { token, ...(version ? { ifMatch: version } : {}), ...abortable(signal) }); return true }
|
|
54
|
+
catch (err) {
|
|
55
|
+
if (isNotFound(err, blobs)) return true
|
|
56
|
+
if (blobs.BlobPreconditionFailedError && err instanceof blobs.BlobPreconditionFailedError) return false
|
|
57
|
+
throw err
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
async list(prefix, cursor, limit, signal) {
|
|
61
|
+
signal?.throwIfAborted()
|
|
62
|
+
validateStoragePage(prefix, limit)
|
|
63
|
+
const page = await blobs.list({ prefix: `.managed/${prefix}`, token, limit, ...(cursor ? { cursor } : {}), ...abortable(signal) })
|
|
64
|
+
if (page.hasMore && (!page.cursor || page.cursor === cursor)) throw new Error('Invalid blob pagination')
|
|
65
|
+
return { objects: page.blobs.filter(blob => blob.pathname.startsWith(`.managed/${prefix}`)).map(blob => ({ key: blob.pathname.slice('.managed/'.length),
|
|
66
|
+
modifiedAt: modifiedAt(blob.uploadedAt) })),
|
|
67
|
+
cursor: page.hasMore ? page.cursor! : null }
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
return storage
|
|
71
|
+
}
|