loadwise 0.8.0 → 0.10.0
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/PERFORMANCE.md +17 -0
- package/README.md +294 -700
- package/agent/hooks/check-edited-file.mjs +800 -0
- package/dist/bin/github-action.js +72 -18
- package/dist/bin/loadwise.js +21 -34
- package/dist/lib/accessibility/angular-literal.js +1 -1
- package/dist/lib/accessibility/aria-tables.js +10 -6
- package/dist/lib/accessibility/astro-template.js +111 -0
- package/dist/lib/accessibility/autofocus.js +94 -0
- package/dist/lib/accessibility/document-reference-rules.js +143 -0
- package/dist/lib/accessibility/form-context.js +7 -2
- package/dist/lib/accessibility/frontend-detect.js +18 -8
- package/dist/lib/accessibility/heading-order.js +59 -0
- package/dist/lib/accessibility/interaction-semantics.js +90 -0
- package/dist/lib/accessibility/jinja-template.js +150 -0
- package/dist/lib/accessibility/jsx-elements.js +164 -21
- package/dist/lib/accessibility/language-tags.js +18 -0
- package/dist/lib/accessibility/markup-files.js +44 -17
- package/dist/lib/accessibility/markup.js +4 -1
- package/dist/lib/accessibility/media-rules.js +81 -0
- package/dist/lib/accessibility/native-interactive.js +145 -0
- package/dist/lib/accessibility/rule-ids.js +30 -0
- package/dist/lib/accessibility/rule-utilities.js +92 -0
- package/dist/lib/accessibility/rules.js +77 -140
- package/dist/lib/accessibility/site-fixes/aria-typo.js +65 -0
- package/dist/lib/accessibility/site-fixes/buttons.js +4 -11
- package/dist/lib/accessibility/site-fixes/email-autocomplete.js +103 -0
- package/dist/lib/accessibility/site-fixes/html-language.js +84 -0
- package/dist/lib/accessibility/site-fixes/media-autoplay.js +87 -0
- package/dist/lib/accessibility/site-fixes/positive-tabindex.js +102 -0
- package/dist/lib/accessibility/site-fixes/source-position.js +11 -0
- package/dist/lib/accessibility/site-fixes/viewport.js +107 -0
- package/dist/lib/accessibility/table-headers.js +70 -0
- package/dist/lib/accessibility/template-attributes.js +13 -2
- package/dist/lib/accessibility/template-binding-scope.js +122 -0
- package/dist/lib/accessibility/template-elements.js +259 -109
- package/dist/lib/accessibility/template-file.js +111 -22
- package/dist/lib/accessibility/template-tag-tables.js +86 -0
- package/dist/lib/accessibility/usage.js +1 -1
- package/dist/lib/editor/accessibility-examples.js +5 -0
- package/dist/lib/editor/actions.js +26 -14
- package/dist/lib/editor/apply-fix.js +30 -2
- package/dist/lib/editor/configured-setup.js +9 -14
- package/dist/lib/editor/diagnostics.js +121 -135
- package/dist/lib/editor/dismissals.js +21 -10
- package/dist/lib/editor/doctor.js +167 -0
- package/dist/lib/editor/editor-document.js +17 -1
- package/dist/lib/editor/editor-launcher.js +60 -26
- package/dist/lib/editor/finding-comparison.js +4 -25
- package/dist/lib/editor/fix-edits.js +87 -8
- package/dist/lib/editor/hover-markdown.js +104 -0
- package/dist/lib/editor/inline-fix.js +50 -0
- package/dist/lib/editor/inline-recommendation.js +27 -0
- package/dist/lib/editor/jetbrains-setup.js +2 -1
- package/dist/lib/editor/language-server.js +305 -56
- package/dist/lib/editor/native-setup.js +7 -2
- package/dist/lib/editor/perf-watch.js +7 -5
- package/dist/lib/editor/performance-examples.js +31 -0
- package/dist/lib/editor/polyglot-selection.js +193 -0
- package/dist/lib/editor/project-policy.js +146 -0
- package/dist/lib/editor/project-watches.js +86 -0
- package/dist/lib/editor/release-vsix.js +23 -1
- package/dist/lib/editor/reliability-examples.js +36 -0
- package/dist/lib/editor/scan-session.js +212 -62
- package/dist/lib/editor/security-examples.js +24 -14
- package/dist/lib/editor/server-commands.js +24 -11
- package/dist/lib/editor/setup.js +7 -12
- package/dist/lib/editor/standalone-performance-examples.js +51 -0
- package/dist/lib/editor/workspace-files.js +32 -8
- package/dist/lib/editor/workspace-paths.js +29 -7
- package/dist/lib/editor/zed-settings.js +118 -0
- package/dist/lib/editor/zed-setup.js +88 -0
- package/dist/lib/maintainability/analysis/analyze-source.js +8 -3
- package/dist/lib/maintainability/analysis/source-exclusions.js +2 -1
- package/dist/lib/maintainability/analysis/source-facts.js +2 -2
- package/dist/lib/maintainability/architecture/architecture.js +50 -2
- package/dist/lib/maintainability/architecture/graph.js +2 -1
- package/dist/lib/maintainability/architecture/layout.js +2 -1
- package/dist/lib/maintainability/cli/apply-fix.js +32 -7
- package/dist/lib/maintainability/cli/changed-lines.js +7 -113
- package/dist/lib/maintainability/cli/finding-ids.js +3 -6
- package/dist/lib/maintainability/cli/fix-cli.js +1 -9
- package/dist/lib/maintainability/cli/git-inventory.js +5 -6
- package/dist/lib/maintainability/cli/inputs.js +24 -16
- package/dist/lib/maintainability/cli/scan-arguments.js +33 -0
- package/dist/lib/maintainability/cli/site-fix.js +31 -13
- package/dist/lib/maintainability/cli.js +31 -38
- package/dist/lib/maintainability/entry-points/entry-points.js +27 -68
- package/dist/lib/maintainability/examples.js +60 -0
- package/dist/lib/maintainability/polyglot-duplicates.js +1 -0
- package/dist/lib/maintainability/polyglot.js +337 -12
- package/dist/lib/maintainability/project.js +4 -4
- package/dist/lib/maintainability/quality/console-tooling.js +114 -0
- package/dist/lib/maintainability/quality/declarative-imports.js +18 -0
- package/dist/lib/maintainability/quality/deprecated-apis.js +227 -40
- package/dist/lib/maintainability/quality/duplicates.js +5 -11
- package/dist/lib/maintainability/quality/generic-assertion-target.js +75 -0
- package/dist/lib/maintainability/quality/limits.js +5 -1
- package/dist/lib/maintainability/quality/modern-node-callbacks.js +188 -0
- package/dist/lib/maintainability/quality/modern-syntax.js +301 -0
- package/dist/lib/maintainability/quality/size-limits.js +2 -1
- package/dist/lib/maintainability/quality/stray-console.js +31 -7
- package/dist/lib/maintainability/quality/tsconfig-strict.js +432 -21
- package/dist/lib/maintainability/quality/type-escape-hatches.js +411 -0
- package/dist/lib/maintainability/quality/type-safety-hygiene.js +132 -42
- package/dist/lib/maintainability/quality/unowned-source.js +15 -0
- package/dist/lib/maintainability/rule-ids.js +27 -0
- package/dist/lib/maintainability/scan-changed.js +39 -3
- package/dist/lib/maintainability/scan-worker.js +3 -5
- package/dist/lib/maintainability/scan.js +26 -4
- package/dist/lib/maintainability/site-fixes/duplicate-edits.js +3 -2
- package/dist/lib/maintainability/site-fixes/duplicate-methods.js +7 -2
- package/dist/lib/maintainability/site-fixes/duplicates.js +3 -2
- package/dist/lib/maintainability/site-fixes/focused-test.js +230 -0
- package/dist/lib/maintainability/site-fixes/helpers.js +2 -6
- package/dist/lib/maintainability/site-fixes/if-rewrites.js +1 -7
- package/dist/lib/maintainability/site-fixes/large-module.js +2 -1
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +15 -6
- package/dist/lib/maintainability/unused/ancestor-aliases.js +2 -2
- package/dist/lib/maintainability/unused/module-usage-dynamic.js +2 -1
- package/dist/lib/maintainability/unused/module-usage-named-exports.js +125 -0
- package/dist/lib/maintainability/unused/module-usage-signatures.js +116 -0
- package/dist/lib/maintainability/unused/module-usage.js +75 -109
- package/dist/lib/maintainability/unused/unused-bindings.js +143 -8
- package/dist/lib/maintainability/unused/unused-dependencies.js +218 -0
- package/dist/lib/maintainability/unused/unused-modules.js +39 -1
- package/dist/lib/performance/analysis/call-syntax.js +2 -1
- package/dist/lib/performance/analysis/function-kinds.js +1 -5
- package/dist/lib/performance/analysis/missing-packages.js +13 -7
- package/dist/lib/performance/analysis/package-types.js +10 -2
- package/dist/lib/performance/analysis/schema-tooling.js +157 -8
- package/dist/lib/performance/analysis/source-inputs.js +17 -12
- package/dist/lib/performance/analysis/source-text.js +2 -2
- package/dist/lib/performance/analysis/unscanned-javascript.js +6 -5
- package/dist/lib/performance/analyze.js +25 -6
- package/dist/lib/performance/async/async-errors.js +162 -14
- package/dist/lib/performance/async/await-before-early-exit.js +1 -6
- package/dist/lib/performance/async/floating-promises.js +27 -6
- package/dist/lib/performance/async/promise-return-escapes-catch.js +35 -2
- package/dist/lib/performance/async/stream-errors.js +1 -3
- package/dist/lib/performance/cli/changed-lines.js +14 -111
- package/dist/lib/performance/cli/cli-arguments.js +11 -9
- package/dist/lib/performance/cli/cli-markup.js +42 -0
- package/dist/lib/performance/cli/cli-output.js +9 -3
- package/dist/lib/performance/cli/cli-policy.js +174 -17
- package/dist/lib/performance/cli/cli-scan.js +170 -139
- package/dist/lib/performance/cli/finding-ids.js +7 -7
- package/dist/lib/performance/cli/fix-cli.js +83 -34
- package/dist/lib/performance/cli/prisma-schema-findings.js +23 -0
- package/dist/lib/performance/cli/scan-outcome.js +19 -1
- package/dist/lib/performance/cli/shared-security-inputs.js +27 -0
- package/dist/lib/performance/cli/watch-security.js +33 -0
- package/dist/lib/performance/cli/watch.js +42 -15
- package/dist/lib/performance/cli.js +8 -7
- package/dist/lib/performance/collections/array-drains.js +354 -0
- package/dist/lib/performance/collections/collection-bindings.js +1 -2
- package/dist/lib/performance/collections/collection-bounds.js +3 -2
- package/dist/lib/performance/collections/collection-copy.js +51 -1
- package/dist/lib/performance/collections/collection-lookups.js +74 -13
- package/dist/lib/performance/collections/collection-reduce.js +32 -6
- package/dist/lib/performance/collections/collections.js +34 -2
- package/dist/lib/performance/collections/json-clone.js +137 -17
- package/dist/lib/performance/collections/regex-allocation.js +156 -0
- package/dist/lib/performance/collections/repeated.js +26 -8
- package/dist/lib/performance/collections/sort-extreme.js +18 -1
- package/dist/lib/performance/collections/workbook-decodes.js +3 -2
- package/dist/lib/performance/contracts/cli.js +340 -21
- package/dist/lib/performance/contracts/git-files.js +102 -5
- package/dist/lib/performance/contracts/openapi-schema.js +187 -0
- package/dist/lib/performance/contracts/openapi-sources.js +260 -0
- package/dist/lib/performance/contracts/openapi.js +172 -233
- package/dist/lib/performance/contracts/protobuf.js +301 -0
- package/dist/lib/performance/contracts/trpc-moves.js +46 -0
- package/dist/lib/performance/contracts/trpc.js +477 -0
- package/dist/lib/performance/datastores/pg-integrity.js +2 -1
- package/dist/lib/performance/datastores/pooling.js +3 -18
- package/dist/lib/performance/datastores/postgres.js +41 -33
- package/dist/lib/performance/datastores/redis-keys.js +4 -3
- package/dist/lib/performance/datastores/redis.js +8 -1
- package/dist/lib/performance/datastores/repeated-reads.js +104 -0
- package/dist/lib/performance/datastores/search-clients.js +9 -0
- package/dist/lib/performance/datastores/session-store.js +166 -7
- package/dist/lib/performance/datastores/supabase.js +73 -0
- package/dist/lib/performance/db/checks.js +1 -1
- package/dist/lib/performance/db/cli-mysql.js +12 -2
- package/dist/lib/performance/db/cli.js +38 -5
- package/dist/lib/performance/db/drivers.js +20 -4
- package/dist/lib/performance/db/measure.js +1 -1
- package/dist/lib/performance/db/mysql-catalog.js +1 -1
- package/dist/lib/performance/db/plan-estimates.js +46 -7
- package/dist/lib/performance/db/redact.js +1 -3
- package/dist/lib/performance/db/snapshot.js +1 -1
- package/dist/lib/performance/db/sqlite-plan.js +95 -0
- package/dist/lib/performance/engine/compiler.js +3 -2
- package/dist/lib/performance/engine/native-checker.js +85 -10
- package/dist/lib/performance/engine/native-syntax.js +17 -0
- package/dist/lib/performance/engine/native-workspaces.js +36 -8
- package/dist/lib/performance/engine/native.js +42 -16
- package/dist/lib/performance/engine/review-config-inputs.js +57 -0
- package/dist/lib/performance/evidence/plans.js +14 -16
- package/dist/lib/performance/evidence/trace-formats.js +182 -0
- package/dist/lib/performance/evidence/traces.js +6 -6
- package/dist/lib/performance/frontend/angular-bootstrap.js +309 -0
- package/dist/lib/performance/frontend/angular-template-calls.js +187 -0
- package/dist/lib/performance/frontend/barrel-subpaths.js +204 -0
- package/dist/lib/performance/frontend/effect-cleanup.js +30 -4
- package/dist/lib/performance/frontend/frontend-dom-perf.js +79 -15
- package/dist/lib/performance/frontend/frontend-perf.js +119 -176
- package/dist/lib/performance/frontend/frontend-render-perf.js +17 -13
- package/dist/lib/performance/frontend/jsx-element-perf.js +171 -0
- package/dist/lib/performance/frontend/layout-binary-probe.js +186 -0
- package/dist/lib/performance/frontend/next-fetch-waterfall.js +405 -0
- package/dist/lib/performance/frontend/rxjs-subscriptions.js +155 -0
- package/dist/lib/performance/frontend/seo/client-fetched-content.js +196 -0
- package/dist/lib/performance/frontend/seo/navigation-without-link.js +116 -0
- package/dist/lib/performance/frontend/seo/next-routes.js +114 -0
- package/dist/lib/performance/frontend/seo/route-metadata.js +158 -0
- package/dist/lib/performance/frontend/seo/seo-rules.js +9 -0
- package/dist/lib/performance/frontend/seo/sitewide-noindex.js +103 -0
- package/dist/lib/performance/frontend/template-tracking.js +215 -0
- package/dist/lib/performance/frontend/vue-options-hooks.js +108 -0
- package/dist/lib/performance/index-rules/index-rules-table.js +3 -3
- package/dist/lib/performance/index-rules/redundant-indexes.js +6 -13
- package/dist/lib/performance/migrations/cli.js +65 -14
- package/dist/lib/performance/migrations/discover.js +63 -32
- package/dist/lib/performance/migrations/enforce.js +5 -12
- package/dist/lib/performance/migrations/extract-code-sql.js +10 -7
- package/dist/lib/performance/migrations/extract-code.js +1 -0
- package/dist/lib/performance/migrations/extract-sql.js +45 -4
- package/dist/lib/performance/migrations/extract-sqlite.js +302 -0
- package/dist/lib/performance/migrations/operations.js +122 -8
- package/dist/lib/performance/migrations/postgres-registry.js +6 -6
- package/dist/lib/performance/migrations/postgres-table-history.js +147 -0
- package/dist/lib/performance/migrations/rules-postgres-shared.js +4 -6
- package/dist/lib/performance/migrations/rules-postgres.js +67 -13
- package/dist/lib/performance/migrations/rules-sqlite.js +118 -0
- package/dist/lib/performance/migrations/sqlserver-creation.js +131 -0
- package/dist/lib/performance/migrations/sqlserver.js +81 -0
- package/dist/lib/performance/models/client-imports.js +286 -0
- package/dist/lib/performance/models/index.js +46 -8
- package/dist/lib/performance/orm/cartesian-includes.js +49 -22
- package/dist/lib/performance/orm/drizzle.js +86 -27
- package/dist/lib/performance/orm/knex-loop-operations.js +96 -0
- package/dist/lib/performance/orm/orm-owners.js +3 -5
- package/dist/lib/performance/orm/orm-queries.js +50 -152
- package/dist/lib/performance/orm/orm-result-uses.js +116 -0
- package/dist/lib/performance/orm/prisma-integrity.js +24 -3
- package/dist/lib/performance/orm/prisma.js +32 -33
- package/dist/lib/performance/orm/typeorm-batch-filter.js +43 -0
- package/dist/lib/performance/orm/typeorm-projection.js +2 -2
- package/dist/lib/performance/orm/typeorm-transaction-loops.js +3 -4
- package/dist/lib/performance/orm/typeorm-updates.js +1 -3
- package/dist/lib/performance/orm/unbounded-list-page-size.js +179 -0
- package/dist/lib/performance/orm/unbounded-list-queries.js +63 -146
- package/dist/lib/performance/polyglot.js +173 -61
- package/dist/lib/performance/probe-caches.js +42 -0
- package/dist/lib/performance/program-batches.js +138 -0
- package/dist/lib/performance/projects.js +86 -89
- package/dist/lib/performance/query-loops/guard-queries.js +2 -1
- package/dist/lib/performance/query-loops/parallel-reads-policy.js +1 -3
- package/dist/lib/performance/query-loops/query-loop-batch.js +81 -7
- package/dist/lib/performance/query-loops/query-loop-bounds.js +1 -13
- package/dist/lib/performance/query-loops/query-policy.js +19 -20
- package/dist/lib/performance/query-loops/query-results.js +1 -6
- package/dist/lib/performance/query-loops/query-write-context.js +89 -0
- package/dist/lib/performance/query-loops/sequential-loops.js +6 -24
- package/dist/lib/performance/query-loops/service-call-resolution.js +17 -2
- package/dist/lib/performance/query-loops/service-loop-source.js +124 -0
- package/dist/lib/performance/query-loops/service-loops.js +86 -25
- package/dist/lib/performance/query-loops/throw-on-miss-loops.js +141 -0
- package/dist/lib/performance/request/backend-mastery.js +18 -30
- package/dist/lib/performance/request/fixed-emitter-loop.js +98 -0
- package/dist/lib/performance/request/fresh-mapped-elements.js +137 -0
- package/dist/lib/performance/request/graphql-list-lookups.js +258 -0
- package/dist/lib/performance/request/http-options-writes.js +2 -8
- package/dist/lib/performance/request/http-timeouts.js +3 -2
- package/dist/lib/performance/request/json-stringify-output.js +217 -0
- package/dist/lib/performance/request/llm-max-tokens.js +27 -8
- package/dist/lib/performance/request/mongoose-lean.js +15 -10
- package/dist/lib/performance/request/node-event-listener-leak.js +190 -35
- package/dist/lib/performance/request/node-fs-context.js +6 -8
- package/dist/lib/performance/request/node-fs.js +4 -6
- package/dist/lib/performance/request/password-hashing.js +4 -3
- package/dist/lib/performance/request/phase5-ast.js +3 -9
- package/dist/lib/performance/request/phase5-rules.js +50 -102
- package/dist/lib/performance/request/promise-schedulers.js +180 -0
- package/dist/lib/performance/request/pure-js-hashing.js +2 -1
- package/dist/lib/performance/request/request-handlers.js +44 -7
- package/dist/lib/performance/request/request-inputs.js +121 -26
- package/dist/lib/performance/request/unbounded-promise-all.js +142 -52
- package/dist/lib/performance/rule-ids.js +72 -5
- package/dist/lib/performance/schema/context.js +7 -16
- package/dist/lib/performance/schema/model.js +1 -9
- package/dist/lib/performance/schema/prisma-owners.js +15 -1
- package/dist/lib/performance/schema/prisma-schema.js +5 -6
- package/dist/lib/performance/schema/project-indexes-events.js +8 -2
- package/dist/lib/performance/schema/project-indexes.js +4 -2
- package/dist/lib/performance/schema/query-site-support.js +1 -1
- package/dist/lib/performance/schema/sequelize-schema.js +1 -3
- package/dist/lib/performance/schema/ts-helpers.js +3 -7
- package/dist/lib/performance/shared-libs.js +48 -0
- package/dist/lib/performance/site-fix.js +33 -7
- package/dist/lib/performance/site-fixes/collection-copy.js +2 -1
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collection-find.js +2 -1
- package/dist/lib/performance/site-fixes/collection-helpers.js +0 -6
- package/dist/lib/performance/site-fixes/collection-lookups.js +2 -1
- package/dist/lib/performance/site-fixes/database.js +7 -16
- package/dist/lib/performance/site-fixes/drizzle-index.js +2 -9
- package/dist/lib/performance/site-fixes/frontend-fixes.js +47 -15
- package/dist/lib/performance/site-fixes/helpers.js +19 -18
- package/dist/lib/performance/site-fixes/last-insert-id.js +2 -1
- package/dist/lib/performance/site-fixes/llm-max-tokens.js +56 -0
- package/dist/lib/performance/site-fixes/migration-column-rules.js +11 -7
- package/dist/lib/performance/site-fixes/migration-lock-rules.js +6 -3
- package/dist/lib/performance/site-fixes/migrations.js +4 -0
- package/dist/lib/performance/site-fixes/pure-js-hashing.js +2 -2
- package/dist/lib/performance/site-fixes/query-shapes.js +1 -4
- package/dist/lib/performance/site-fixes/service-loop-ast.js +1 -9
- package/dist/lib/performance/site-fixes/small-rewrites.js +28 -2
- package/dist/lib/performance/site-fixes/template-tracking-fixes.js +82 -0
- package/dist/lib/performance/site-fixes/writes.js +1 -6
- package/dist/lib/performance/sql/local-statements.js +108 -0
- package/dist/lib/performance/sql/mysql-tokens.js +1 -4
- package/dist/lib/performance/sql/pg-parser.js +3 -5
- package/dist/lib/performance/sql/sqlserver-control.js +43 -0
- package/dist/lib/reliability/analyze.js +48 -5
- package/dist/lib/reliability/boundary-access.js +34 -27
- package/dist/lib/reliability/callback-completion.js +10 -2
- package/dist/lib/reliability/child-process.js +2 -2
- package/dist/lib/reliability/crlf-line-split.js +124 -73
- package/dist/lib/reliability/crlf-references.js +78 -0
- package/dist/lib/reliability/default-numeric-sort.js +25 -0
- package/dist/lib/reliability/error-cause-plan.js +29 -0
- package/dist/lib/reliability/error-values.js +68 -27
- package/dist/lib/reliability/json-cast.js +368 -0
- package/dist/lib/reliability/key-test-callee.js +50 -0
- package/dist/lib/reliability/lossy-text-rewrite.js +94 -0
- package/dist/lib/reliability/manifest-boundary.js +466 -0
- package/dist/lib/reliability/manifest-snapshot.js +40 -0
- package/dist/lib/reliability/parse-failure-flow.js +5 -2
- package/dist/lib/reliability/parse-failure-paths.js +39 -8
- package/dist/lib/reliability/parse-failure-reset.js +5 -17
- package/dist/lib/reliability/pool-timeout.js +31 -7
- package/dist/lib/reliability/promise-usage.js +281 -0
- package/dist/lib/reliability/react-bugs.js +16 -6
- package/dist/lib/reliability/react-state-aliases.js +94 -4
- package/dist/lib/reliability/react-state-order.js +2 -2
- package/dist/lib/reliability/react-state-paths.js +2 -2
- package/dist/lib/reliability/react-state-setter.js +5 -11
- package/dist/lib/reliability/react-state-walker.js +3 -3
- package/dist/lib/reliability/react-state.js +24 -5
- package/dist/lib/reliability/required-member.js +19 -0
- package/dist/lib/reliability/resource-leak-intervals.js +4 -7
- package/dist/lib/reliability/resource-leaks.js +4 -7
- package/dist/lib/reliability/rule-ids.js +70 -1
- package/dist/lib/reliability/same-file-io.js +135 -0
- package/dist/lib/reliability/short-write.js +220 -0
- package/dist/lib/reliability/site-fixes/crlf-line-split.js +71 -0
- package/dist/lib/reliability/site-fixes/error-values.js +40 -0
- package/dist/lib/reliability/site-fixes/pool-timeout.js +51 -0
- package/dist/lib/reliability/source-helpers.js +18 -8
- package/dist/lib/reliability/standalone-sources.js +8 -6
- package/dist/lib/reliability/suspicious-logic.js +6 -8
- package/dist/lib/reliability/swallowed-errors.js +3 -7
- package/dist/lib/reliability/switch-exhaustive.js +195 -5
- package/dist/lib/reliability/test-cleanup-after-await.js +2 -2
- package/dist/lib/reliability/throw-string-plan.js +121 -0
- package/dist/lib/reliability/uncaught-exception.js +2 -7
- package/dist/lib/reliability/usage.js +1 -1
- package/dist/lib/reliability/working-directory.js +101 -0
- package/dist/lib/security/analyze.js +2 -0
- package/dist/lib/security/auth-crypto/hashing.js +21 -4
- package/dist/lib/security/auth-crypto/index.js +6 -1
- package/dist/lib/security/auth-crypto/jwt-decode-helpers.js +163 -0
- package/dist/lib/security/auth-crypto/jwt.js +52 -36
- package/dist/lib/security/auth-crypto/plaintext-password.js +2 -1
- package/dist/lib/security/auth-crypto/random.js +2 -1
- package/dist/lib/security/auth-crypto/rate-limits.js +79 -8
- package/dist/lib/security/auth-crypto/rule-ids.js +2 -0
- package/dist/lib/security/auth-crypto/secrets.js +132 -19
- package/dist/lib/security/auth-crypto/shared.js +1 -4
- package/dist/lib/security/auth-crypto/signature-verification.js +29 -0
- package/dist/lib/security/auth-crypto/transport-and-ciphers.js +43 -3
- package/dist/lib/security/auth-crypto/weak-hash.js +183 -0
- package/dist/lib/security/bindings.js +41 -9
- package/dist/lib/security/cli.js +84 -27
- package/dist/lib/security/cookie-csrf.js +316 -7
- package/dist/lib/security/credential-entropy.js +12 -0
- package/dist/lib/security/deps/cargo-lock.js +78 -0
- package/dist/lib/security/deps/cli.js +4 -4
- package/dist/lib/security/deps/fixed-version.js +2 -2
- package/dist/lib/security/deps/lockfiles.js +207 -17
- package/dist/lib/security/deps/osv.js +190 -59
- package/dist/lib/security/deps/scan.js +87 -19
- package/dist/lib/security/examples/{auth-crypto.cjs → auth-crypto.js} +11 -5
- package/dist/lib/security/examples/{deps.cjs → deps.js} +1 -3
- package/dist/lib/security/examples/{exposure.cjs → exposure.js} +17 -3
- package/dist/lib/security/examples/files.js +146 -0
- package/dist/lib/security/examples/{injection.cjs → injection.js} +13 -3
- package/dist/lib/security/examples/{moved.cjs → moved.js} +1 -3
- package/dist/lib/security/examples/{request-forgery.cjs → request-forgery.js} +2 -4
- package/dist/lib/security/exposure/backend-security-mastery.js +2 -1
- package/dist/lib/security/exposure/client-env.js +2 -1
- package/dist/lib/security/exposure/content-disposition.js +71 -0
- package/dist/lib/security/exposure/cookies.js +200 -1
- package/dist/lib/security/exposure/cors.js +63 -4
- package/dist/lib/security/exposure/frontend-security.js +18 -62
- package/dist/lib/security/exposure/index.js +9 -0
- package/dist/lib/security/exposure/js-yaml.js +203 -0
- package/dist/lib/security/exposure/message-effects.js +266 -0
- package/dist/lib/security/exposure/package-manifest.js +2 -2
- package/dist/lib/security/exposure/prototype-pollution.js +99 -5
- package/dist/lib/security/exposure/reflected-xss.js +47 -12
- package/dist/lib/security/exposure/response-content-type.js +181 -0
- package/dist/lib/security/exposure/rule-ids.js +4 -0
- package/dist/lib/security/exposure/trust-boundary.js +142 -0
- package/dist/lib/security/exposure/url-secrets.js +38 -2
- package/dist/lib/security/exposure/webhook-verification.js +231 -0
- package/dist/lib/security/express-registration.js +45 -0
- package/dist/lib/security/express-router-factories.js +118 -0
- package/dist/lib/security/files/azure-pipelines.js +69 -0
- package/dist/lib/security/files/bitbucket-pipelines.js +32 -0
- package/dist/lib/security/files/ci-platforms.js +16 -0
- package/dist/lib/security/files/ci-shared.js +260 -0
- package/dist/lib/security/files/circleci.js +81 -0
- package/dist/lib/security/files/component-markup.js +66 -0
- package/dist/lib/security/files/compose.js +2 -1
- package/dist/lib/security/files/dockerfile.js +188 -3
- package/dist/lib/security/files/github-actions.js +490 -0
- package/dist/lib/security/files/gitlab-ci.js +90 -0
- package/dist/lib/security/files/hcl.js +264 -0
- package/dist/lib/security/files/hosting-config.js +159 -0
- package/dist/lib/security/files/http-headers.js +65 -0
- package/dist/lib/security/files/index.js +182 -33
- package/dist/lib/security/files/jenkinsfile.js +482 -0
- package/dist/lib/security/files/json-nodes.js +67 -0
- package/dist/lib/security/files/kubernetes.js +136 -0
- package/dist/lib/security/files/nginx.js +501 -0
- package/dist/lib/security/files/package-scripts.js +175 -0
- package/dist/lib/security/files/project-files.js +98 -31
- package/dist/lib/security/files/rule-ids.js +31 -0
- package/dist/lib/security/files/secret-values.js +31 -5
- package/dist/lib/security/files/secrets.js +53 -18
- package/dist/lib/security/files/terraform.js +345 -0
- package/dist/lib/security/injection/code.js +33 -5
- package/dist/lib/security/injection/command.js +51 -4
- package/dist/lib/security/injection/escape-preserving.js +219 -0
- package/dist/lib/security/injection/escape-replace.js +174 -2
- package/dist/lib/security/injection/index.js +5 -1
- package/dist/lib/security/injection/nosql.js +3 -2
- package/dist/lib/security/injection/redos.js +25 -4
- package/dist/lib/security/injection/rule-ids.js +3 -0
- package/dist/lib/security/injection/shell-output-guard.js +97 -0
- package/dist/lib/security/injection/sql.js +53 -2
- package/dist/lib/security/injection/xpath.js +35 -0
- package/dist/lib/security/lexical-declaration.js +28 -0
- package/dist/lib/security/pg-identifier-guards.js +146 -5
- package/dist/lib/security/phase5-rules.js +1 -0
- package/dist/lib/security/polyglot.js +35 -3
- package/dist/lib/security/provider-credentials.js +29 -0
- package/dist/lib/security/redirect-settings.js +131 -6
- package/dist/lib/security/request-forgery/contained-helper.js +75 -0
- package/dist/lib/security/request-forgery/member-chain.js +41 -0
- package/dist/lib/security/request-forgery/open-redirect-query.js +2 -1
- package/dist/lib/security/request-forgery/open-redirect.js +69 -9
- package/dist/lib/security/request-forgery/parsed-host-guard.js +213 -0
- package/dist/lib/security/request-forgery/path-traversal.js +36 -1
- package/dist/lib/security/request-forgery/shared.js +205 -30
- package/dist/lib/security/request-forgery/ssrf.js +145 -1
- package/dist/lib/security/rule-descriptions.js +46 -1
- package/dist/lib/security/rule-ids.js +50 -0
- package/dist/lib/security/scope/drizzle-policy.js +2 -1
- package/dist/lib/security/scope/kysely-policy.js +173 -0
- package/dist/lib/security/site-fixes/auth-crypto.js +3 -1
- package/dist/lib/security/taint/accessors.js +364 -0
- package/dist/lib/security/taint/array-writes.js +139 -0
- package/dist/lib/security/taint/binding-inputs.js +4 -3
- package/dist/lib/security/taint/field-access.js +80 -0
- package/dist/lib/security/taint/filename-guard.js +81 -6
- package/dist/lib/security/taint/guards.js +150 -17
- package/dist/lib/security/taint/index.js +41 -116
- package/dist/lib/security/taint/original-string-validation.js +229 -0
- package/dist/lib/security/taint/propagate.js +5 -6
- package/dist/lib/security/taint/resolution-cache.js +10 -1
- package/dist/lib/security/taint/sanitizers.js +5 -5
- package/dist/lib/security/taint/shape-flows.js +128 -0
- package/dist/lib/security/taint/sink-alphabets.js +237 -0
- package/dist/lib/security/taint/sources.js +151 -8
- package/dist/lib/security/taint/trace-nodes.js +35 -2
- package/dist/lib/security/taint/validators.js +59 -20
- package/dist/lib/security/trusted-bindings.js +44 -0
- package/dist/lib/shared/agent/agent-init.js +84 -9
- package/dist/lib/shared/agent/agent-integrations.js +626 -0
- package/dist/lib/shared/agent/agent-prompt.js +2 -1
- package/dist/lib/shared/agent/agent-session.js +1 -1
- package/dist/lib/shared/agent/agent-toml.js +98 -0
- package/dist/lib/shared/agent/agent-verify.js +21 -3
- package/dist/lib/shared/agent/git-hook.js +31 -4
- package/dist/lib/shared/agent/notify-editor.js +2 -1
- package/dist/lib/shared/agent/review-delivery.js +3 -1
- package/dist/lib/shared/agent/review-mcp.js +67 -13
- package/dist/lib/shared/agent/review-request.js +4 -0
- package/dist/lib/shared/analysis/ast-helpers.js +50 -0
- package/dist/lib/shared/analysis/ast-semantics.js +23 -1
- package/dist/lib/shared/analysis/binding-names.js +13 -0
- package/dist/lib/shared/analysis/execution-context.js +49 -9
- package/dist/lib/shared/analysis/hot-path-graph.js +55 -19
- package/dist/lib/shared/analysis/next-env-config.js +232 -0
- package/dist/lib/shared/analysis/polyglot-compatibility.js +102 -23
- package/dist/lib/shared/analysis/scan-cache-git.js +114 -0
- package/dist/lib/shared/analysis/scan-cache-loadwise-packages.js +54 -0
- package/dist/lib/shared/analysis/scan-cache-project-inputs.js +221 -0
- package/dist/lib/shared/analysis/scan-cache-sources.js +235 -0
- package/dist/lib/shared/analysis/scan-cache-storage.js +204 -0
- package/dist/lib/shared/analysis/scan-cache.js +373 -166
- package/dist/lib/shared/analysis/valid-javascript.js +118 -0
- package/dist/lib/shared/changed-lines.js +129 -0
- package/dist/lib/shared/check-options.js +63 -0
- package/dist/lib/shared/check-watch-inputs.js +152 -0
- package/dist/lib/shared/check-watch-process.js +57 -0
- package/dist/lib/shared/check-watch.js +136 -0
- package/dist/lib/shared/check.js +97 -40
- package/dist/lib/shared/completion-shells.js +127 -0
- package/dist/lib/shared/completion.js +124 -0
- package/dist/lib/shared/config-rule-validation.js +135 -0
- package/dist/lib/shared/config.js +330 -163
- package/dist/lib/shared/conventional-projects.js +40 -0
- package/dist/lib/shared/declarative-rule-packs.js +173 -0
- package/dist/lib/shared/explain.js +47 -9
- package/dist/lib/shared/fix/fix-command.js +2 -0
- package/dist/lib/shared/fix/fix-convergence.js +45 -128
- package/dist/lib/shared/fix/fix-history.js +130 -26
- package/dist/lib/shared/fix/fix-interactive.js +78 -0
- package/dist/lib/shared/fix/fix-patch.js +89 -0
- package/dist/lib/shared/fix/fix-polyglot-preview.js +166 -0
- package/dist/lib/shared/fix/fix-presentation-biome.js +120 -0
- package/dist/lib/shared/fix/fix-presentation-prettier.js +27 -10
- package/dist/lib/shared/fix/fix-presentation-rows.js +5 -1
- package/dist/lib/shared/fix/fix-presentation.js +22 -15
- package/dist/lib/shared/fix/fix-project-membership.js +79 -0
- package/dist/lib/shared/fix/fix-reversal-proof.js +107 -0
- package/dist/lib/shared/fix/fix-reverse.js +37 -0
- package/dist/lib/shared/fix/fix-safety.js +4 -4
- package/dist/lib/shared/fix/fix-scope.js +13 -9
- package/dist/lib/shared/fix/fix-source-snapshot.js +92 -0
- package/dist/lib/shared/fix/fix-staging.js +129 -0
- package/dist/lib/shared/fix/fix-terminal.js +34 -9
- package/dist/lib/shared/fix/fix-typecheck.js +21 -8
- package/dist/lib/shared/fix/fix-usage.js +47 -0
- package/dist/lib/shared/fix/project-file-access.js +85 -29
- package/dist/lib/shared/fix/read-fix-text.js +23 -0
- package/dist/lib/shared/fix/reversal-lock.js +37 -0
- package/dist/lib/shared/fix/review-fix-mode.js +9 -0
- package/dist/lib/shared/fix.js +157 -52
- package/dist/lib/shared/formats/ci-consumers.js +84 -0
- package/dist/lib/shared/formats/finding-location.js +52 -3
- package/dist/lib/shared/formats/github.js +22 -7
- package/dist/lib/shared/formats/index.js +21 -5
- package/dist/lib/shared/formats/readable.js +63 -0
- package/dist/lib/shared/formats/report-rows.js +38 -0
- package/dist/lib/shared/formats/sarif-fixes.js +261 -0
- package/dist/lib/shared/formats/sarif.js +175 -33
- package/dist/lib/shared/formats/security-cwe.js +142 -0
- package/dist/lib/shared/node-scan.js +64 -0
- package/dist/lib/shared/package-root.js +2 -0
- package/dist/lib/shared/project-root.js +27 -1
- package/dist/lib/shared/reporting/action-comparison.js +78 -0
- package/dist/lib/shared/reporting/action-summary.js +31 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +7 -2
- package/dist/lib/shared/reporting/baseline.js +12 -12
- package/dist/lib/shared/reporting/check-text.js +7 -5
- package/dist/lib/shared/reporting/finding-id-hash.js +7 -0
- package/dist/lib/shared/reporting/impact-levels.js +1 -1
- package/dist/lib/shared/reporting/pillar-rules.js +13 -1
- package/dist/lib/shared/reporting/pillars.js +61 -23
- package/dist/lib/shared/reporting/rule-docs.js +2 -2
- package/dist/lib/shared/reporting/rule-pack-fingerprint.js +21 -0
- package/dist/lib/shared/reporting/source-report.js +20 -6
- package/dist/lib/shared/reporting/stray-console-methods.js +1 -0
- package/dist/lib/shared/rule-registry.js +6 -1
- package/dist/lib/shared/system/atomic-write.js +12 -6
- package/dist/lib/shared/system/component-source.js +28 -0
- package/dist/lib/shared/system/environment.js +12 -0
- package/dist/lib/shared/system/file-key.js +8 -0
- package/dist/lib/shared/system/git.js +62 -0
- package/dist/lib/shared/system/glob.js +233 -28
- package/dist/lib/shared/system/jsonc-config.js +63 -0
- package/dist/lib/shared/system/path-inside.js +19 -0
- package/dist/lib/shared/system/posix-path.js +22 -0
- package/dist/lib/shared/system/read-json.js +10 -0
- package/dist/lib/shared/system/sentry.js +6 -3
- package/dist/lib/shared/system/terminal-style.js +2 -2
- package/dist/lib/shared/system/text-helpers.js +40 -0
- package/dist/lib/shared/system/windows-orphans-script.js +75 -0
- package/dist/lib/shared/system/windows-orphans.js +28 -0
- package/dist/lib/shared/types/changed-lines.js +1 -0
- package/dist/lib/shared/types/completion.js +1 -0
- package/dist/lib/shared/workspace-selection.js +67 -0
- package/dist/lib/shared/workspaces.js +215 -68
- package/dist/polyglot/maintainability/rules.js +119 -3
- package/examples/README.md +45 -0
- package/examples/clean/avatar.html +1 -0
- package/examples/clean/source.ts +10 -0
- package/examples/clean/tsconfig.json +4 -0
- package/examples/findings/avatar.html +1 -0
- package/examples/findings/source.ts +11 -0
- package/examples/findings/tsconfig.json +4 -0
- package/examples/labels-clean.py +5 -0
- package/integrations/zed/Cargo.lock +1 -1
- package/integrations/zed/Cargo.toml +1 -1
- package/integrations/zed/LICENSE +201 -0
- package/integrations/zed/extension.toml +3 -3
- package/integrations/zed/extension.wasm +0 -0
- package/integrations/zed/src/lib.rs +108 -8
- package/package.json +28 -14
- package/polyglot/performance/rules/csharp.yml +173 -10
- package/polyglot/performance/rules/go.yml +15 -7
- package/polyglot/performance/rules/input.yml +12 -0
- package/polyglot/performance/rules/java.yml +52 -1
- package/polyglot/performance/rules/php.yml +146 -0
- package/polyglot/performance/rules/python.yml +7 -3
- package/polyglot/performance/rules/ruby.yml +81 -0
- package/polyglot/performance/sgconfig.yml +2 -0
- package/polyglot/reliability/rules/csharp.yml +105 -0
- package/polyglot/reliability/rules/go.yml +339 -0
- package/polyglot/reliability/rules/input.yml +6 -0
- package/polyglot/reliability/rules/java.yml +218 -0
- package/polyglot/reliability/rules/kotlin.yml +49 -0
- package/polyglot/reliability/rules/python.yml +288 -0
- package/polyglot/reliability/rules/rust.yml +83 -0
- package/polyglot/reliability/sgconfig.yml +1 -0
- package/polyglot/security/rules/csharp.yml +143 -3
- package/polyglot/security/rules/go.yml +59 -4
- package/polyglot/security/rules/java.yml +103 -0
- package/polyglot/security/rules/python.yml +413 -21
- package/polyglot/security/rules/rust.yml +176 -3
- package/polyglot/security/sgconfig.yml +2 -0
- package/polyglot/security/utils/java-imported-jca-cipher.yml +32 -0
- package/polyglot/security/utils/java-unshadowed-javax.yml +29 -0
- package/schema/loadwise.config.schema.json +1137 -600
- package/schema/loadwise.outcomes.schema.json +43 -0
- package/types/scan.d.ts +53 -0
- package/dist/lib/maintainability/site-fixes/exports.js +0 -25
- package/dist/lib/security/examples/files.cjs +0 -30
- package/docs/assets/cli-workflow.gif +0 -0
- package/plugin/.claude-plugin/plugin.json +0 -9
- package/plugin/commands/loadwise.md +0 -29
- package/plugin/hooks/check-edited-file.mjs +0 -173
- package/plugin/hooks/hooks.json +0 -17
- /package/dist/lib/{maintainability/types/changed-lines.js → editor/types/workspace-fix.js} +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { nextConfigNames } from './next-env-config.js';
|
|
6
|
+
import { isPathInsideOrEqual } from '../system/path-inside.js';
|
|
7
|
+
import { parseMaintainabilityArguments } from '../../maintainability/cli/scan-arguments.js';
|
|
8
|
+
import { lockfileNames } from '../../security/deps/lockfiles.js';
|
|
9
|
+
// Checkers read these from disk to select sources, so their contents count even when Git ignores them.
|
|
10
|
+
// The dependency checks read a lockfile and its registry settings even when Git ignores them.
|
|
11
|
+
const registryConfigNames = ['.npmrc', 'bunfig.toml'];
|
|
12
|
+
const projectConfigNames = [
|
|
13
|
+
'tsconfig.json',
|
|
14
|
+
'jsconfig.json',
|
|
15
|
+
'pnpm-workspace.yaml',
|
|
16
|
+
'lerna.json',
|
|
17
|
+
...lockfileNames,
|
|
18
|
+
...registryConfigNames,
|
|
19
|
+
];
|
|
20
|
+
const projectInputs = /(?:^|\/)(?:package|[jt]sconfig)\.json$|\.[cm]?[jt]sx?$/i;
|
|
21
|
+
export function addAncestorDirectories(directories, directory) {
|
|
22
|
+
for (let folder = directory; !directories.has(folder); folder = path.posix.dirname(folder)) {
|
|
23
|
+
directories.add(folder);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function projectConfigHash(absolute, fileHash) {
|
|
27
|
+
try {
|
|
28
|
+
const stat = fs.statSync(absolute, { throwIfNoEntry: false });
|
|
29
|
+
if (stat?.isFile())
|
|
30
|
+
return fileHash(absolute, stat);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// An indexed path can have a missing or replaced parent in the working tree.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function hashIgnoredPackageInputs(hash, root, directory, names, fileHash) {
|
|
37
|
+
const manifestName = path.posix.join(directory, 'package.json');
|
|
38
|
+
const filename = path.join(root, manifestName);
|
|
39
|
+
const manifest = fs.lstatSync(filename, { throwIfNoEntry: false });
|
|
40
|
+
if (!manifest)
|
|
41
|
+
return;
|
|
42
|
+
if (!names.has(manifestName)) {
|
|
43
|
+
// Other checkers read a linked manifest's contents, but only a regular file can own a Next config.
|
|
44
|
+
const target = manifest.isSymbolicLink() ? fs.statSync(filename, { throwIfNoEntry: false }) : manifest;
|
|
45
|
+
hash.update(`\0manifest\0${manifestName}\0${manifest.mode}\0${target?.isFile() ? fileHash(filename, target) : ''}`);
|
|
46
|
+
}
|
|
47
|
+
if (!manifest.isFile())
|
|
48
|
+
return;
|
|
49
|
+
// Only package directories can own Next configs. A second path of any type makes publication ambiguous.
|
|
50
|
+
for (const config of nextConfigNames) {
|
|
51
|
+
const name = path.posix.join(directory, config);
|
|
52
|
+
if (names.has(name))
|
|
53
|
+
continue;
|
|
54
|
+
const candidate = path.join(root, name);
|
|
55
|
+
const stat = fs.lstatSync(candidate, { throwIfNoEntry: false });
|
|
56
|
+
if (stat)
|
|
57
|
+
hash.update(`\0next-config\0${name}\0${stat.isFile() ? fileHash(candidate, stat) : stat.mode}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Configless and workspace discovery check source ancestors on disk, including markers Git ignores.
|
|
61
|
+
export function hashIgnoredProjectConfigs(hash, topLevel, relativeCwd, names, fileHash) {
|
|
62
|
+
const directories = new Set(['.']);
|
|
63
|
+
addAncestorDirectories(directories, relativeCwd || '.');
|
|
64
|
+
for (const name of names) {
|
|
65
|
+
if (projectInputs.test(name))
|
|
66
|
+
addAncestorDirectories(directories, path.posix.dirname(name));
|
|
67
|
+
}
|
|
68
|
+
for (const directory of directories) {
|
|
69
|
+
hashIgnoredPackageInputs(hash, topLevel, directory, names, fileHash);
|
|
70
|
+
for (const configName of projectConfigNames) {
|
|
71
|
+
const name = path.posix.join(directory, configName);
|
|
72
|
+
if (names.has(name))
|
|
73
|
+
continue;
|
|
74
|
+
const configHash = projectConfigHash(path.join(topLevel, name), fileHash);
|
|
75
|
+
if (configHash !== undefined)
|
|
76
|
+
hash.update(`\0project-config\0${name}\0${configHash}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Git lists a linked folder as one entry, so the files it holds outside the repository are missing from the
|
|
81
|
+
// fingerprint while checkers still read them.
|
|
82
|
+
export function linksOutside(absolute, realTopLevel) {
|
|
83
|
+
try {
|
|
84
|
+
if (!fs.statSync(absolute, { throwIfNoEntry: false })?.isDirectory())
|
|
85
|
+
return false;
|
|
86
|
+
return !isPathInsideOrEqual(realTopLevel(), fs.realpathSync.native(absolute));
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// The dependency checks also read the user's npm and Bun registry settings, from the environment and the home folder.
|
|
93
|
+
const registryVariables = [
|
|
94
|
+
'BUN_CONFIG_REGISTRY',
|
|
95
|
+
'NPM_CONFIG_REGISTRY',
|
|
96
|
+
'npm_config_registry',
|
|
97
|
+
'NPM_CONFIG_USERCONFIG',
|
|
98
|
+
'XDG_CONFIG_HOME',
|
|
99
|
+
];
|
|
100
|
+
/** Undefined when a check could read a registry setting the key cannot name. The files are keyed by their contents. */
|
|
101
|
+
export function registryConfigFingerprint() {
|
|
102
|
+
const home = os.homedir();
|
|
103
|
+
const { NPM_CONFIG_USERCONFIG, XDG_CONFIG_HOME } = process.env;
|
|
104
|
+
// The checks resolve a relative user config from each lockfile's folder, which only the scan finds.
|
|
105
|
+
if (NPM_CONFIG_USERCONFIG && !path.isAbsolute(NPM_CONFIG_USERCONFIG))
|
|
106
|
+
return undefined;
|
|
107
|
+
const configHome = XDG_CONFIG_HOME && path.isAbsolute(XDG_CONFIG_HOME) ? XDG_CONFIG_HOME : home;
|
|
108
|
+
const files = [path.join(home, '.npmrc'), path.join(configHome, '.npmrc'), path.join(configHome, '.bunfig.toml')];
|
|
109
|
+
if (NPM_CONFIG_USERCONFIG)
|
|
110
|
+
files.push(NPM_CONFIG_USERCONFIG);
|
|
111
|
+
const hash = crypto.createHash('sha1');
|
|
112
|
+
for (const name of registryVariables)
|
|
113
|
+
hash.update(`${name}=${process.env[name] ?? ''}\0`);
|
|
114
|
+
for (const file of files) {
|
|
115
|
+
let contents = '\0absent';
|
|
116
|
+
try {
|
|
117
|
+
contents = fs.readFileSync(file, 'utf8');
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// A missing or unreadable file leaves the registry unchanged, as the checks read it.
|
|
121
|
+
}
|
|
122
|
+
hash.update(`${file}\0${contents}\0`);
|
|
123
|
+
}
|
|
124
|
+
return { fingerprint: hash.digest('hex'), files };
|
|
125
|
+
}
|
|
126
|
+
// Only a link whose target leaves the repository makes a checker read outside it. A dangling link resolves to itself.
|
|
127
|
+
export function targetsOutside(absolute, realTopLevel) {
|
|
128
|
+
try {
|
|
129
|
+
return !isPathInsideOrEqual(realTopLevel(), fs.realpathSync.native(absolute));
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// The keyed files travel to the checker in one environment variable, which Windows limits to 32,767 characters.
|
|
136
|
+
const keyedFilesLimit = 30_000;
|
|
137
|
+
export function fitsKeyedFiles(keyedFiles) {
|
|
138
|
+
return JSON.stringify(keyedFiles).length <= keyedFilesLimit;
|
|
139
|
+
}
|
|
140
|
+
export function isLink(file) {
|
|
141
|
+
try {
|
|
142
|
+
return fs.lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink() ?? false;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// An unreadable entry is not followed by the checkers either.
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
export function insideGitRepository(directory) {
|
|
150
|
+
if (process.env.GIT_DIR)
|
|
151
|
+
return true;
|
|
152
|
+
for (let ancestor = directory;; ancestor = path.dirname(ancestor)) {
|
|
153
|
+
try {
|
|
154
|
+
if (fs.lstatSync(path.join(ancestor, '.git'), { throwIfNoEntry: false }))
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// Membership cannot be ruled out, so the scan is not keyed from another folder; the caller bypasses the cache.
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if (path.dirname(ancestor) === ancestor)
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// The folder a checker child scans: its `-p`/`--project` value, or maintainability's positional folder. `check` runs
|
|
166
|
+
// each pillar as such a child with `-p <path>`, so a `check path/to/app` from outside a repository is cached there.
|
|
167
|
+
export function scannedProject(args, cwd) {
|
|
168
|
+
const flagIndex = args.findIndex((arg) => arg === '-p' || arg === '--project');
|
|
169
|
+
let named = flagIndex >= 0 ? args[flagIndex + 1] : args.find((arg) => arg.startsWith('--project='))?.slice('--project='.length);
|
|
170
|
+
if (named === undefined && args[0] === 'maint') {
|
|
171
|
+
try {
|
|
172
|
+
named = parseMaintainabilityArguments(args.slice(1)).positionals[0];
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (!named)
|
|
179
|
+
return undefined;
|
|
180
|
+
const absolute = path.resolve(cwd, named);
|
|
181
|
+
let stat;
|
|
182
|
+
try {
|
|
183
|
+
stat = fs.statSync(absolute, { throwIfNoEntry: false });
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
// A looping or unreadable path is the checker's to report; the scan runs uncached.
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
if (stat?.isDirectory())
|
|
190
|
+
return absolute;
|
|
191
|
+
return stat?.isFile() ? path.dirname(absolute) : undefined;
|
|
192
|
+
}
|
|
193
|
+
// node_modules is ignored by git, but installing or removing a package changes the types the
|
|
194
|
+
// checkers see. Package managers rewrite these files on every install, so their size and time stand
|
|
195
|
+
// in for the whole tree.
|
|
196
|
+
const installMarkers = [
|
|
197
|
+
'node_modules',
|
|
198
|
+
'node_modules/.package-lock.json',
|
|
199
|
+
'node_modules/.yarn-state.yml',
|
|
200
|
+
'node_modules/.modules.yaml',
|
|
201
|
+
'node_modules/.pnpm/lock.yaml',
|
|
202
|
+
];
|
|
203
|
+
export function hashMarkersIn(hash, base, directory) {
|
|
204
|
+
for (const marker of installMarkers) {
|
|
205
|
+
const stat = fs.statSync(path.join(base, directory, marker), { throwIfNoEntry: false });
|
|
206
|
+
// The other markers live inside node_modules.
|
|
207
|
+
if (!stat && marker === 'node_modules')
|
|
208
|
+
return;
|
|
209
|
+
if (stat)
|
|
210
|
+
hash.update(`\0${directory}\0${marker}\0${stat.size}\0${stat.mtimeMs}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// A listed file resolves packages from a node_modules in any folder above it, a nested install that is no workspace
|
|
214
|
+
// included, so every such folder's install markers count, and so does the absence of an install there.
|
|
215
|
+
export function hashListedFolderInstalls(hash, topLevel, names) {
|
|
216
|
+
const directories = new Set();
|
|
217
|
+
for (const name of names)
|
|
218
|
+
addAncestorDirectories(directories, path.posix.dirname(name));
|
|
219
|
+
for (const directory of directories)
|
|
220
|
+
hashMarkersIn(hash, topLevel, directory);
|
|
221
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// The scan cache keys a report by the Git repository's files, but TypeScript follows imports wherever they lead.
|
|
2
|
+
// A checker child whose report may be stored loads this module with --import; it watches file reads, and the
|
|
3
|
+
// first source file read from outside the repository tells the parent not to store the report.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { syncBuiltinESMExports } from 'node:module';
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
8
|
+
import { packageRoot } from '../package-root.js';
|
|
9
|
+
import { realExistingPrefix } from '../project-root.js';
|
|
10
|
+
import { loadwisePackages } from './scan-cache-loadwise-packages.js';
|
|
11
|
+
import { isPathInsideOrEqual } from '../system/path-inside.js';
|
|
12
|
+
const topLevelVariable = 'LOADWISE_INTERNAL_SCAN_CACHE_TOP_LEVEL';
|
|
13
|
+
const keyedFilesVariable = 'LOADWISE_INTERNAL_SCAN_CACHE_KEYED_FILES';
|
|
14
|
+
const outsideSourceSignal = 'loadwise:outside-repository-source';
|
|
15
|
+
// Program sources, the JSON modules they import (manifests and configs too), templates, Prisma schemas and migrations,
|
|
16
|
+
// OpenAPI references and Protobuf imports can change findings. A read outside the repository counts unless the parent
|
|
17
|
+
// keyed that exact file.
|
|
18
|
+
const sourceExtension = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|html?|jsonc?|ya?ml|toml|sql|prisma|proto)$/i;
|
|
19
|
+
function keyedFilesFromEnvironment() {
|
|
20
|
+
let listed;
|
|
21
|
+
try {
|
|
22
|
+
listed = JSON.parse(process.env[keyedFilesVariable] || '[]');
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// Keying nothing only means more reads count as outside, which is the safe direction.
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
return Array.isArray(listed) ? listed.filter((file) => typeof file === 'string') : [];
|
|
29
|
+
}
|
|
30
|
+
function realRoots(topLevel) {
|
|
31
|
+
// The parent names keyed files as Git and TypeScript spell them, which can differ from the child's spelling.
|
|
32
|
+
const keyedFiles = new Set();
|
|
33
|
+
// A missing file is keyed by its absence, and a lookup for it resolves through its nearest existing folder.
|
|
34
|
+
for (const file of keyedFilesFromEnvironment())
|
|
35
|
+
keyedFiles.add(realExistingPrefix(file));
|
|
36
|
+
const loadwiseRoot = fs.realpathSync.native(packageRoot);
|
|
37
|
+
return {
|
|
38
|
+
topLevel: fs.realpathSync.native(topLevel),
|
|
39
|
+
loadwiseRoot,
|
|
40
|
+
loadwisePackages: loadwisePackages(loadwiseRoot),
|
|
41
|
+
keyedFiles,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// The install markers key node_modules in the repository and in every folder above it. Any other node_modules tree is
|
|
45
|
+
// not keyed, apart from Loadwise's own packages, whose versions the key holds.
|
|
46
|
+
function keyedInstall(resolved, { topLevel }) {
|
|
47
|
+
const segments = resolved.split(/[\\/]/);
|
|
48
|
+
const index = segments.indexOf('node_modules');
|
|
49
|
+
if (index < 1)
|
|
50
|
+
return false;
|
|
51
|
+
const owner = segments.slice(0, index).join(path.sep) + path.sep;
|
|
52
|
+
return isPathInsideOrEqual(owner, topLevel);
|
|
53
|
+
}
|
|
54
|
+
// Read APIs also take file URLs and Buffers; a descriptor was already checked when it was opened.
|
|
55
|
+
function pathText(file) {
|
|
56
|
+
if (typeof file === 'string')
|
|
57
|
+
return file;
|
|
58
|
+
if (file instanceof URL)
|
|
59
|
+
return file.protocol === 'file:' ? fileURLToPath(file) : undefined;
|
|
60
|
+
return Buffer.isBuffer(file) ? file.toString() : undefined;
|
|
61
|
+
}
|
|
62
|
+
// Captured before the wrappers replace it, so resolving a watched path does not watch itself.
|
|
63
|
+
const lstatUnwatched = fs.lstatSync;
|
|
64
|
+
// A link anywhere on the path counts, the file itself included, also inside ignored folders and under node_modules.
|
|
65
|
+
// A missing file resolves through its nearest existing folder, since a later read of it would land there, except
|
|
66
|
+
// under a dangling link, whose target can appear anywhere: that path has no known place.
|
|
67
|
+
function resolvedPath(absolute) {
|
|
68
|
+
const missing = [];
|
|
69
|
+
for (let current = absolute;; current = path.dirname(current)) {
|
|
70
|
+
try {
|
|
71
|
+
return path.join(fs.realpathSync.native(current), ...missing.reverse());
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
if (lstatUnwatched(current, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
75
|
+
return undefined;
|
|
76
|
+
if (current === path.dirname(current))
|
|
77
|
+
return absolute;
|
|
78
|
+
missing.push(path.basename(current));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function isOutsideSource(argument, roots) {
|
|
83
|
+
const file = pathText(argument);
|
|
84
|
+
return file !== undefined && sourceExtension.test(file) && isOutside(file, roots);
|
|
85
|
+
}
|
|
86
|
+
// A folder listing, such as a tsconfig `include` walk, finds sources that may not exist yet, so listing any folder
|
|
87
|
+
// outside the repository counts, whatever it holds.
|
|
88
|
+
function isOutsideFolder(argument, roots) {
|
|
89
|
+
const folder = pathText(argument);
|
|
90
|
+
return folder !== undefined && isOutside(folder, roots);
|
|
91
|
+
}
|
|
92
|
+
function isOutside(file, roots) {
|
|
93
|
+
// `path.resolve` would drop a `..` by its text, while POSIX follows a link before the `..` that comes after it.
|
|
94
|
+
const absolute = path.isAbsolute(file) ? file : `${process.cwd()}${path.sep}${file}`;
|
|
95
|
+
const resolved = resolvedPath(absolute);
|
|
96
|
+
if (resolved === undefined)
|
|
97
|
+
return true;
|
|
98
|
+
const watched = roots();
|
|
99
|
+
if (isPathInsideOrEqual(watched.topLevel, resolved) || isLoadwiseInstall(resolved, watched))
|
|
100
|
+
return false;
|
|
101
|
+
return !keyedInstall(resolved, watched) && !watched.keyedFiles.has(resolved);
|
|
102
|
+
}
|
|
103
|
+
const caseInsensitivePaths = process.platform === 'win32' || process.platform === 'darwin';
|
|
104
|
+
// Windows and macOS match a path in any letter case. TypeScript also probes its own path with the case swapped to
|
|
105
|
+
// learn whether the file system is case-sensitive; elsewhere that path does not exist, so a missing path matches in
|
|
106
|
+
// any case too, while an existing file must match exactly. The deepest of Loadwise's packages that holds a path owns
|
|
107
|
+
// it, and a package nested in its node_modules that is not one of Loadwise's own is no part of the install.
|
|
108
|
+
function isLoadwiseInstall(resolved, { loadwiseRoot, loadwisePackages }) {
|
|
109
|
+
const anyCase = caseInsensitivePaths || !lstatUnwatched(resolved, { throwIfNoEntry: false });
|
|
110
|
+
const fold = (text) => (anyCase ? text.toLowerCase() : text);
|
|
111
|
+
const target = fold(resolved);
|
|
112
|
+
let owner;
|
|
113
|
+
for (const installed of [loadwiseRoot, ...loadwisePackages]) {
|
|
114
|
+
const candidate = fold(installed);
|
|
115
|
+
if (isPathInsideOrEqual(candidate, target) && candidate.length > (owner?.length ?? -1))
|
|
116
|
+
owner = candidate;
|
|
117
|
+
}
|
|
118
|
+
return owner !== undefined && !path.relative(owner, target).split(path.sep).includes('node_modules');
|
|
119
|
+
}
|
|
120
|
+
function reportOutsideSource() {
|
|
121
|
+
if (!process.send || !process.connected || !process.env[topLevelVariable])
|
|
122
|
+
return;
|
|
123
|
+
process.send(outsideSourceSignal, undefined, undefined, () => undefined);
|
|
124
|
+
}
|
|
125
|
+
// `fs.open` takes its callback where the flags would be, and opens for reading when they are left out.
|
|
126
|
+
function opensForReading(flags) {
|
|
127
|
+
if (flags === undefined || typeof flags === 'function')
|
|
128
|
+
return true;
|
|
129
|
+
if (typeof flags === 'number')
|
|
130
|
+
return (flags & (fs.constants.O_WRONLY | fs.constants.O_RDWR)) !== fs.constants.O_WRONLY;
|
|
131
|
+
return typeof flags === 'string' && (flags.includes('r') || flags.includes('+'));
|
|
132
|
+
}
|
|
133
|
+
// Set once this process watches its reads.
|
|
134
|
+
let noteRead;
|
|
135
|
+
function watchSourceReads(topLevel) {
|
|
136
|
+
let reported = false;
|
|
137
|
+
// Resolved once, on the first watched read.
|
|
138
|
+
let roots;
|
|
139
|
+
const watchedRoots = () => (roots ??= realRoots(topLevel));
|
|
140
|
+
const noter = (isOutsideRead) => (file) => {
|
|
141
|
+
if (reported)
|
|
142
|
+
return;
|
|
143
|
+
let outside;
|
|
144
|
+
try {
|
|
145
|
+
outside = isOutsideRead(file, watchedRoots);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// The repository or Loadwise's install vanished mid-scan, as a concurrent install can do: the read goes ahead,
|
|
149
|
+
// and the report is not stored.
|
|
150
|
+
outside = true;
|
|
151
|
+
}
|
|
152
|
+
if (!outside)
|
|
153
|
+
return;
|
|
154
|
+
reported = true;
|
|
155
|
+
reportOutsideSource();
|
|
156
|
+
};
|
|
157
|
+
noteRead = noter(isOutsideSource);
|
|
158
|
+
const note = noteRead;
|
|
159
|
+
// Each wrapper forwards every overload unchanged, which its own signature cannot spell out.
|
|
160
|
+
const watched = (read, reads = () => true, noteFile = note) => function watchedRead(...call) {
|
|
161
|
+
if (reads(call[1]))
|
|
162
|
+
noteFile(call[0]);
|
|
163
|
+
return Reflect.apply(read, this, call);
|
|
164
|
+
};
|
|
165
|
+
const noteListing = noter(isOutsideFolder);
|
|
166
|
+
const always = () => true;
|
|
167
|
+
Object.assign(fs, {
|
|
168
|
+
readdirSync: watched(fs.readdirSync, always, noteListing),
|
|
169
|
+
readdir: watched(fs.readdir, always, noteListing),
|
|
170
|
+
opendirSync: watched(fs.opendirSync, always, noteListing),
|
|
171
|
+
opendir: watched(fs.opendir, always, noteListing),
|
|
172
|
+
});
|
|
173
|
+
Object.assign(fs.promises, {
|
|
174
|
+
readdir: watched(fs.promises.readdir, always, noteListing),
|
|
175
|
+
opendir: watched(fs.promises.opendir, always, noteListing),
|
|
176
|
+
});
|
|
177
|
+
// Checkers read sources synchronously (TypeScript) and asynchronously (maintainability) alike.
|
|
178
|
+
Object.assign(fs, { readFileSync: watched(fs.readFileSync), readFile: watched(fs.readFile) });
|
|
179
|
+
Object.assign(fs.promises, { readFile: watched(fs.promises.readFile) });
|
|
180
|
+
// A descriptor or stream read starts with an open, so opening a source to read it counts as reading it.
|
|
181
|
+
Object.assign(fs, {
|
|
182
|
+
openSync: watched(fs.openSync, opensForReading),
|
|
183
|
+
open: watched(fs.open, opensForReading),
|
|
184
|
+
createReadStream: watched(fs.createReadStream),
|
|
185
|
+
});
|
|
186
|
+
Object.assign(fs.promises, { open: watched(fs.promises.open, opensForReading) });
|
|
187
|
+
// Module resolution probes candidates before it reads one, so an outside import that is still missing, and would
|
|
188
|
+
// resolve once created, shows up only as a probe.
|
|
189
|
+
Object.assign(fs, {
|
|
190
|
+
statSync: watched(fs.statSync),
|
|
191
|
+
lstatSync: watched(fs.lstatSync),
|
|
192
|
+
existsSync: watched(fs.existsSync),
|
|
193
|
+
accessSync: watched(fs.accessSync),
|
|
194
|
+
stat: watched(fs.stat),
|
|
195
|
+
lstat: watched(fs.lstat),
|
|
196
|
+
access: watched(fs.access),
|
|
197
|
+
exists: watched(fs.exists),
|
|
198
|
+
});
|
|
199
|
+
Object.assign(fs.promises, {
|
|
200
|
+
stat: watched(fs.promises.stat),
|
|
201
|
+
lstat: watched(fs.promises.lstat),
|
|
202
|
+
access: watched(fs.promises.access),
|
|
203
|
+
});
|
|
204
|
+
// Modules that imported these by name see the wrappers too.
|
|
205
|
+
syncBuiltinESMExports();
|
|
206
|
+
}
|
|
207
|
+
/** Checks sources a reader outside this process loaded, such as the TypeScript 7 native compiler, as if read here. */
|
|
208
|
+
function noteSourcesRead(files) {
|
|
209
|
+
if (noteRead)
|
|
210
|
+
for (const file of files)
|
|
211
|
+
noteRead(file);
|
|
212
|
+
}
|
|
213
|
+
function isOutsideSourceSignal(message) {
|
|
214
|
+
return message === outsideSourceSignal;
|
|
215
|
+
}
|
|
216
|
+
/** The node arguments and environment that make a checker child watch its reads for `watch`. */
|
|
217
|
+
function sourceWatchSpawn(watch) {
|
|
218
|
+
return {
|
|
219
|
+
execArgv: ['--import', pathToFileURL(import.meta.filename).href],
|
|
220
|
+
env: { [topLevelVariable]: watch.topLevel, [keyedFilesVariable]: JSON.stringify(watch.keyedFiles) },
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/** The watch an uncached nested run passes on, so a grandchild's outside read still reaches the caching parent. */
|
|
224
|
+
function inheritedSourceWatch() {
|
|
225
|
+
const topLevel = process.env[topLevelVariable];
|
|
226
|
+
return topLevel
|
|
227
|
+
? { topLevel, keyedFiles: keyedFilesFromEnvironment(), onOutsideSource: reportOutsideSource }
|
|
228
|
+
: undefined;
|
|
229
|
+
}
|
|
230
|
+
// Only a process with a parent channel installs the wrapper, for the rest of its life. Worker threads have no
|
|
231
|
+
// process.send and stay unwatched; maintainability's scan workers read only the project files listed for them.
|
|
232
|
+
const watchedTopLevel = process.env[topLevelVariable];
|
|
233
|
+
if (watchedTopLevel && process.send)
|
|
234
|
+
watchSourceReads(watchedTopLevel);
|
|
235
|
+
export { inheritedSourceWatch, isOutsideSourceSignal, noteSourcesRead, reportOutsideSource, sourceWatchSpawn };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { writeResolvedFileAtomic } from '../system/atomic-write.js';
|
|
5
|
+
import { errorCode } from '../system/error-code.js';
|
|
6
|
+
const cacheFilename = /^[0-9a-f]{64}\.json$/;
|
|
7
|
+
const temporaryCacheFilename = /^\.[0-9a-f]{64}\.json\.[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\.tmp$/;
|
|
8
|
+
function recordPrefix(name) {
|
|
9
|
+
return `{"kind":"loadwise-scan-cache/v1","key":"${name.slice(0, -'.json'.length)}","report":`;
|
|
10
|
+
}
|
|
11
|
+
function isGeneratedCacheFile(filename) {
|
|
12
|
+
const name = path.basename(filename);
|
|
13
|
+
return temporaryCacheFilename.test(name) || (cacheFilename.test(name) && ownedCacheRecord(filename) !== undefined);
|
|
14
|
+
}
|
|
15
|
+
function trustedAncestor(entry, uid, cacheOwned, isCacheDirectory) {
|
|
16
|
+
// System-owned path aliases such as macOS /var are allowed above the selected root.
|
|
17
|
+
if (!entry.isDirectory())
|
|
18
|
+
return entry.isSymbolicLink() && uid !== undefined && entry.uid === 0 && !cacheOwned;
|
|
19
|
+
if (uid === undefined)
|
|
20
|
+
return true;
|
|
21
|
+
if (entry.uid !== uid && (cacheOwned || entry.uid !== 0))
|
|
22
|
+
return false;
|
|
23
|
+
// Sticky shared ancestors such as /tmp protect user-owned children against replacement.
|
|
24
|
+
if (entry.mode & 0o022 && (cacheOwned || !(entry.mode & 0o1000)))
|
|
25
|
+
return false;
|
|
26
|
+
return !isCacheDirectory || (entry.mode & 0o077) === 0;
|
|
27
|
+
}
|
|
28
|
+
// The nearest folder, from the cache directory up, that another user could replace.
|
|
29
|
+
function untrustedCacheAncestor(directory) {
|
|
30
|
+
const uid = process.getuid?.();
|
|
31
|
+
const customRoot = path.dirname(directory);
|
|
32
|
+
for (let current = directory;; current = path.dirname(current)) {
|
|
33
|
+
const entry = fs.lstatSync(current, { throwIfNoEntry: false });
|
|
34
|
+
const isCacheDirectory = current === directory;
|
|
35
|
+
const cacheOwned = isCacheDirectory || current === customRoot;
|
|
36
|
+
if (entry && !trustedAncestor(entry, uid, cacheOwned, isCacheDirectory))
|
|
37
|
+
return current;
|
|
38
|
+
if (path.dirname(current) === current)
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function trustedCacheAncestors(directory) {
|
|
43
|
+
return untrustedCacheAncestor(directory) === undefined;
|
|
44
|
+
}
|
|
45
|
+
function realProfileRoot(root) {
|
|
46
|
+
if (!root || !path.isAbsolute(root))
|
|
47
|
+
return [];
|
|
48
|
+
try {
|
|
49
|
+
return [fs.realpathSync.native(root)];
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Windows exposes no owner or permission bits through fs.Stats, so a cache there is trusted only inside the
|
|
56
|
+
// user's own profile, whose default ACL keeps other standard users out. Returns the folder to report otherwise.
|
|
57
|
+
function outsideWindowsProfile(canonical) {
|
|
58
|
+
if (process.platform !== 'win32')
|
|
59
|
+
return;
|
|
60
|
+
const profileRoots = [os.homedir(), process.env.LOCALAPPDATA].flatMap(realProfileRoot);
|
|
61
|
+
// Both sides come from realpath, which spells existing folders as stored, so an exact prefix match also
|
|
62
|
+
// rejects a differently cased sibling in a case-sensitive folder.
|
|
63
|
+
const inside = (root) => canonical.startsWith(root.endsWith(path.sep) ? root : root + path.sep);
|
|
64
|
+
return profileRoots.some(inside) ? undefined : path.dirname(canonical);
|
|
65
|
+
}
|
|
66
|
+
function createCacheDirectories(parent, missing, canonical) {
|
|
67
|
+
for (const name of missing) {
|
|
68
|
+
parent = path.join(parent, name);
|
|
69
|
+
try {
|
|
70
|
+
fs.mkdirSync(parent, { mode: 0o700 });
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (errorCode(error) !== 'EEXIST')
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (!fs.lstatSync(parent).isDirectory() || !trustedCacheAncestors(canonical))
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
// onUntrusted receives the folder that made the cache directory unsafe to use.
|
|
82
|
+
function trustedCacheDirectory(requested, create = false, onUntrusted) {
|
|
83
|
+
const directory = path.resolve(requested);
|
|
84
|
+
const rejectedBy = (folder) => {
|
|
85
|
+
if (folder !== undefined)
|
|
86
|
+
onUntrusted?.(folder);
|
|
87
|
+
return folder !== undefined;
|
|
88
|
+
};
|
|
89
|
+
try {
|
|
90
|
+
if (rejectedBy(untrustedCacheAncestor(directory)))
|
|
91
|
+
return;
|
|
92
|
+
// Resolve existing parents even before the first report has created its cache directory.
|
|
93
|
+
const missing = [];
|
|
94
|
+
let current = directory;
|
|
95
|
+
while (!fs.lstatSync(current, { throwIfNoEntry: false })) {
|
|
96
|
+
missing.unshift(path.basename(current));
|
|
97
|
+
current = path.dirname(current);
|
|
98
|
+
}
|
|
99
|
+
const parent = fs.realpathSync.native(current);
|
|
100
|
+
const canonical = path.join(parent, ...missing);
|
|
101
|
+
if (rejectedBy(outsideWindowsProfile(canonical)))
|
|
102
|
+
return;
|
|
103
|
+
if (canonical !== directory && rejectedBy(untrustedCacheAncestor(canonical)))
|
|
104
|
+
return;
|
|
105
|
+
if (create && !createCacheDirectories(parent, missing, canonical))
|
|
106
|
+
return;
|
|
107
|
+
return canonical;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function trustedCacheEntry(entry) {
|
|
114
|
+
const uid = process.getuid?.();
|
|
115
|
+
return (entry.isFile() && entry.nlink === 1 && (uid === undefined || (entry.uid === uid && (entry.mode & 0o077) === 0)));
|
|
116
|
+
}
|
|
117
|
+
function withTrustedCacheFile(filename, updateTimestamp, visit) {
|
|
118
|
+
try {
|
|
119
|
+
const entry = fs.lstatSync(filename, { throwIfNoEntry: false });
|
|
120
|
+
if (!entry || !trustedCacheEntry(entry))
|
|
121
|
+
return;
|
|
122
|
+
const { O_RDONLY, O_RDWR, O_NOFOLLOW, O_NONBLOCK } = fs.constants;
|
|
123
|
+
// Windows needs a writable handle to update the entry's timestamp.
|
|
124
|
+
const access = updateTimestamp && process.platform === 'win32' ? O_RDWR : O_RDONLY;
|
|
125
|
+
const descriptor = fs.openSync(filename, access | (O_NOFOLLOW ?? 0) | (O_NONBLOCK ?? 0));
|
|
126
|
+
try {
|
|
127
|
+
const opened = fs.fstatSync(descriptor);
|
|
128
|
+
if (!trustedCacheEntry(opened) || opened.dev !== entry.dev || opened.ino !== entry.ino)
|
|
129
|
+
return;
|
|
130
|
+
const prefix = Buffer.from(recordPrefix(path.basename(filename)));
|
|
131
|
+
const header = Buffer.alloc(prefix.length);
|
|
132
|
+
if (fs.readSync(descriptor, header, 0, header.length, 0) !== prefix.length || !header.equals(prefix))
|
|
133
|
+
return;
|
|
134
|
+
return visit(descriptor, opened);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
fs.closeSync(descriptor);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function ownedCacheRecord(filename) {
|
|
145
|
+
// Retention only needs the bounded ownership header. The full report can be large.
|
|
146
|
+
return withTrustedCacheFile(filename, false, (_descriptor, opened) => opened);
|
|
147
|
+
}
|
|
148
|
+
function readCacheEntry(filename, isValid) {
|
|
149
|
+
return withTrustedCacheFile(filename, true, (descriptor) => {
|
|
150
|
+
let record;
|
|
151
|
+
try {
|
|
152
|
+
record = JSON.parse(fs.readFileSync(descriptor, 'utf8'));
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!record || typeof record !== 'object' || !('report' in record) || !isValid(record.report))
|
|
158
|
+
return;
|
|
159
|
+
const now = new Date();
|
|
160
|
+
try {
|
|
161
|
+
fs.futimesSync(descriptor, now, now);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// The timestamp only orders retention; a read-only cache mount still serves a valid hit.
|
|
165
|
+
}
|
|
166
|
+
return record.report;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function writeCacheEntry(directory, name, report, maxEntries, protectedEntries) {
|
|
170
|
+
try {
|
|
171
|
+
// The cache directory may have changed while the checker ran.
|
|
172
|
+
if (trustedCacheDirectory(directory, true) !== directory)
|
|
173
|
+
return;
|
|
174
|
+
const tracked = protectedEntries(directory);
|
|
175
|
+
if (!tracked || tracked.has(name))
|
|
176
|
+
return;
|
|
177
|
+
const filename = path.join(directory, name);
|
|
178
|
+
const entry = fs.lstatSync(filename, { throwIfNoEntry: false });
|
|
179
|
+
// A trusted entry with a missing or foreign header, such as one truncated by a full disk, is replaced
|
|
180
|
+
// so the key does not stay uncached.
|
|
181
|
+
if (entry && !trustedCacheEntry(entry))
|
|
182
|
+
return;
|
|
183
|
+
writeResolvedFileAtomic(filename, recordPrefix(name) + JSON.stringify(report) + '}', 0o600);
|
|
184
|
+
const candidates = fs.readdirSync(directory).filter((file) => cacheFilename.test(file) && !tracked.has(file));
|
|
185
|
+
if (candidates.length <= maxEntries)
|
|
186
|
+
return;
|
|
187
|
+
const entries = candidates
|
|
188
|
+
.flatMap((file) => {
|
|
189
|
+
const target = path.join(directory, file);
|
|
190
|
+
const stat = ownedCacheRecord(target);
|
|
191
|
+
return stat ? [{ target, stat }] : [];
|
|
192
|
+
})
|
|
193
|
+
.sort((first, second) => second.stat.mtimeMs - first.stat.mtimeMs);
|
|
194
|
+
for (const { target, stat } of entries.slice(maxEntries)) {
|
|
195
|
+
const current = fs.lstatSync(target, { throwIfNoEntry: false });
|
|
196
|
+
if (current && trustedCacheEntry(current) && current.dev === stat.dev && current.ino === stat.ino)
|
|
197
|
+
fs.rmSync(target, { force: true });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
// A cache that cannot be written only costs speed.
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export { isGeneratedCacheFile, readCacheEntry, trustedCacheDirectory, writeCacheEntry };
|