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