@smoothbricks/nx-plugin 0.4.26 → 0.4.27
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/README.md +994 -117
- package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-bound-failures.js +31 -0
- package/dist/bin/smoo-nx-cargo-hash.js +28 -3
- package/dist/bin/smoo-nx-exec.js +15 -73
- package/dist/bin/smoo-nx-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-failures.js +3 -0
- package/dist/bin/smoo-nx-nextest-extract.js +3 -2
- package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
- package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
- package/dist/bin/smoo-nx-test-walks.js +34 -0
- package/dist/bounded-test-policy.d.ts +7 -1
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +101 -40
- package/dist/build-state-output-policy.d.ts +25 -0
- package/dist/build-state-output-policy.d.ts.map +1 -0
- package/dist/build-state-output-policy.js +64 -0
- package/dist/cargo-closure-input.d.ts +3 -2
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +16 -15
- package/dist/cargo-dev-profile.d.ts +37 -0
- package/dist/cargo-dev-profile.d.ts.map +1 -0
- package/dist/cargo-dev-profile.js +108 -0
- package/dist/cargo-source-hash.d.ts +43 -6
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +309 -73
- package/dist/cargo-toolchain-policy.d.ts +4 -0
- package/dist/cargo-toolchain-policy.d.ts.map +1 -1
- package/dist/cargo-toolchain-policy.js +5 -0
- package/dist/cargo-workspace.d.ts +101 -5
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +291 -10
- package/dist/cross-check-policy.d.ts +21 -3
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +24 -6
- package/dist/disk-command-policy.d.ts +7 -0
- package/dist/disk-command-policy.d.ts.map +1 -0
- package/dist/disk-command-policy.js +98 -0
- package/dist/ensure-built-receipt.d.ts +25 -0
- package/dist/ensure-built-receipt.d.ts.map +1 -0
- package/dist/ensure-built-receipt.js +261 -0
- package/dist/ensure-built-witness.d.ts +57 -0
- package/dist/ensure-built-witness.d.ts.map +1 -0
- package/dist/ensure-built-witness.js +412 -0
- package/dist/ensure-built.d.ts +207 -12
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +576 -202
- package/dist/eslint-file-set.d.ts +7 -0
- package/dist/eslint-file-set.d.ts.map +1 -0
- package/dist/eslint-file-set.js +12 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-pool.js +379 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/disk-lease.js +91 -0
- package/dist/executors/bounded-exec/executor.d.ts +45 -2
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +447 -52
- package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
- package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/gateway-lease.js +143 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
- package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
- package/dist/executors/bounded-exec/progress.d.ts +35 -0
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
- package/dist/executors/bounded-exec/progress.js +79 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp.js +1528 -0
- package/dist/executors/bounded-exec/schema.d.ts +29 -11
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.d.ts +162 -0
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
- package/dist/executors/bounded-exec/verdict.js +318 -0
- package/dist/executors/shard-timings/executor.d.ts +14 -0
- package/dist/executors/shard-timings/executor.d.ts.map +1 -0
- package/dist/executors/shard-timings/executor.js +25 -0
- package/dist/executors/shard-timings/schema.d.ts +4 -0
- package/dist/executors/shard-timings/schema.d.ts.map +1 -0
- package/dist/executors/shard-timings/schema.js +1 -0
- package/dist/failure-summary.d.ts +5 -0
- package/dist/failure-summary.d.ts.map +1 -0
- package/dist/failure-summary.js +118 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -1
- package/dist/generators/create-package/generator.js +6 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +370 -138
- package/dist/managed-files/ci-workflow.d.ts +13 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +44 -13
- package/dist/managed-files/files.d.ts +7 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +62 -8
- package/dist/managed-files/json-edit.d.ts +21 -0
- package/dist/managed-files/json-edit.d.ts.map +1 -0
- package/dist/managed-files/json-edit.js +295 -0
- package/dist/managed-files/managed-format.d.ts +33 -1
- package/dist/managed-files/managed-format.d.ts.map +1 -1
- package/dist/managed-files/managed-format.js +82 -2
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +231 -48
- package/dist/managed-files/tree.d.ts +5 -0
- package/dist/managed-files/tree.d.ts.map +1 -1
- package/dist/managed-files/tree.js +5 -3
- package/dist/manifest-hash.d.ts.map +1 -1
- package/dist/manifest-hash.js +2 -5
- package/dist/nextest-code-bound.d.ts +62 -0
- package/dist/nextest-code-bound.d.ts.map +1 -0
- package/dist/nextest-code-bound.js +154 -0
- package/dist/nextest-extraction.d.ts +24 -8
- package/dist/nextest-extraction.d.ts.map +1 -1
- package/dist/nextest-extraction.js +42 -7
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +3 -5
- package/dist/release-config-policy.d.ts.map +1 -1
- package/dist/release-config-policy.js +3 -5
- package/dist/runtime-input.d.ts +14 -0
- package/dist/runtime-input.d.ts.map +1 -0
- package/dist/runtime-input.js +15 -0
- package/dist/rust-tokens.d.ts +11 -0
- package/dist/rust-tokens.d.ts.map +1 -0
- package/dist/rust-tokens.js +81 -0
- package/dist/task-results.d.ts +6 -0
- package/dist/task-results.d.ts.map +1 -0
- package/dist/task-results.js +61 -0
- package/dist/test-program.d.ts +54 -0
- package/dist/test-program.d.ts.map +1 -0
- package/dist/test-program.js +292 -0
- package/dist/test-timings.d.ts +71 -0
- package/dist/test-timings.d.ts.map +1 -0
- package/dist/test-timings.js +393 -0
- package/dist/test-walk-policy.d.ts +103 -0
- package/dist/test-walk-policy.d.ts.map +1 -0
- package/dist/test-walk-policy.js +657 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +824 -0
- package/dist/type-guards.d.ts +3 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +4 -0
- package/dist/typecheck-test-policy.d.ts +8 -5
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +113 -100
- package/dist/workspace-config-policy.d.ts +8 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +175 -63
- package/dist/workspace-index.d.ts +12 -0
- package/dist/workspace-index.d.ts.map +1 -0
- package/dist/workspace-index.js +17 -0
- package/dist/workspace-manifest.d.ts +37 -0
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/executors.json +5 -0
- package/loader.js +13 -11
- package/managed/raw/envrc +33 -67
- package/managed/raw/git-format-staged.yml +30 -35
- package/managed/raw/patches/nx@23.2.1.patch +3484 -0
- package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
- package/managed/raw/tooling/direnv/developer-links.ts +569 -11
- package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
- package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
- package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
- package/managed/raw/tooling/direnv/repo-path +14 -2
- package/managed/raw/tooling/direnv/setup-environment.ts +962 -148
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
- package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
- package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
- package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
- package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
- package/managed/templates/github/actions/cache-nx/action.yml +1 -1
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
- package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
- package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
- package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
- package/managed/templates/github/workflows/managed-files.yml +1 -2
- package/nextest.toml +8 -41
- package/package.json +74 -12
- package/runtime-input.sh +81 -0
- package/src/__tests__/counted-cargo.ts +33 -14
- package/src/__tests__/fixture-nx-env.ts +111 -46
- package/src/__tests__/progress-clock.ts +43 -0
- package/src/__tests__/runtime-input.ts +23 -0
- package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
- package/src/bin/smoo-nx-bound-failures.ts +33 -0
- package/src/bin/smoo-nx-cargo-hash.ts +28 -3
- package/src/bin/smoo-nx-exec.ts +16 -74
- package/src/bin/smoo-nx-failures.ts +4 -0
- package/src/bin/smoo-nx-nextest-extract.ts +3 -2
- package/src/bin/smoo-nx-test-walks.ts +42 -0
- package/src/bounded-test-policy.test.ts +268 -21
- package/src/bounded-test-policy.ts +114 -42
- package/src/build-state-output-policy.test.ts +115 -0
- package/src/build-state-output-policy.ts +90 -0
- package/src/bun-bootstrap.test.ts +57 -3
- package/src/bun-file-packages.test.ts +47 -0
- package/src/cargo-closure-input.test.ts +138 -66
- package/src/cargo-closure-input.ts +17 -15
- package/src/cargo-dev-profile.ts +137 -0
- package/src/cargo-resolve-cache.test.ts +70 -1
- package/src/cargo-source-hash.test.ts +193 -97
- package/src/cargo-source-hash.ts +345 -68
- package/src/cargo-toolchain-policy.ts +6 -0
- package/src/cargo-workspace.test.ts +320 -0
- package/src/cargo-workspace.ts +336 -10
- package/src/cross-check-policy.test.ts +59 -1
- package/src/cross-check-policy.ts +25 -6
- package/src/disk-command-policy.test.ts +61 -0
- package/src/disk-command-policy.ts +107 -0
- package/src/ensure-built-receipt.test.ts +184 -0
- package/src/ensure-built-receipt.ts +263 -0
- package/src/ensure-built-witness.test.ts +344 -0
- package/src/ensure-built-witness.ts +252 -0
- package/src/ensure-built.test.ts +1036 -578
- package/src/ensure-built.ts +820 -224
- package/src/eslint-file-set.ts +9 -0
- package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
- package/src/executors/bounded-exec/cpu-pool.ts +417 -0
- package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
- package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
- package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
- package/src/executors/bounded-exec/disk-lease.ts +139 -0
- package/src/executors/bounded-exec/executor-host.test.ts +778 -0
- package/src/executors/bounded-exec/executor.test.ts +1116 -63
- package/src/executors/bounded-exec/executor.ts +565 -67
- package/src/executors/bounded-exec/gateway-lease.ts +176 -0
- package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
- package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
- package/src/executors/bounded-exec/progress.test.ts +79 -0
- package/src/executors/bounded-exec/progress.ts +118 -0
- package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
- package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
- package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
- package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
- package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
- package/src/executors/bounded-exec/ram-temp.ts +1918 -0
- package/src/executors/bounded-exec/schema.json +11 -5
- package/src/executors/bounded-exec/schema.ts +27 -11
- package/src/executors/bounded-exec/verdict.test.ts +440 -0
- package/src/executors/bounded-exec/verdict.ts +433 -0
- package/src/executors/shard-timings/executor.ts +36 -0
- package/src/executors/shard-timings/schema.json +21 -0
- package/src/executors/shard-timings/schema.ts +4 -0
- package/src/failure-summary.ts +119 -0
- package/src/generators/bounded-test-targets/generator.test.ts +4 -4
- package/src/generators/bun-test-tracing/generator.test.ts +2 -2
- package/src/generators/create-package/generator.test.ts +6 -0
- package/src/generators/create-package/generator.ts +6 -2
- package/src/host-controller-wiring.test.ts +31 -0
- package/src/index.test.ts +909 -295
- package/src/index.ts +441 -143
- package/src/linked-snapshots.nx.test.ts +172 -0
- package/src/managed-files/ci-workflow.ts +54 -13
- package/src/managed-files/files.ts +65 -9
- package/src/managed-files/forgejo-artifact.test.ts +395 -0
- package/src/managed-files/generator.test.ts +60 -1
- package/src/managed-files/json-edit.test.ts +216 -0
- package/src/managed-files/json-edit.ts +290 -0
- package/src/managed-files/managed-format-json.test.ts +308 -0
- package/src/managed-files/managed-format.ts +90 -2
- package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
- package/src/managed-files/publish-workflow.ts +285 -67
- package/src/managed-files/repo-path.test.ts +129 -0
- package/src/managed-files/trace-db-artifact.test.ts +7 -0
- package/src/managed-files/tree.ts +11 -3
- package/src/manifest-hash.ts +2 -6
- package/src/nextest-code-bound.test.ts +155 -0
- package/src/nextest-code-bound.ts +195 -0
- package/src/nextest-extraction.test.ts +37 -5
- package/src/nextest-extraction.ts +51 -9
- package/src/nextest-partitions.test.ts +427 -82
- package/src/nx-cache-bound.test.ts +5 -10
- package/src/nx-cache-restore-mtime.test.ts +170 -0
- package/src/nx-daemon-claim.test.ts +277 -0
- package/src/nx-daemon-idle-cpu.test.ts +81 -0
- package/src/nx-daemon-idle-policy.test.ts +51 -0
- package/src/nx-daemon-ignore-files.test.ts +210 -0
- package/src/nx-daemon-root-normalization.test.ts +105 -0
- package/src/nx-daemon-validity.test.ts +925 -0
- package/src/nx-dot-input-policy.test.ts +353 -0
- package/src/nx-failure-life-cycles.test.ts +207 -0
- package/src/nx-files-archive-racy.test.ts +97 -0
- package/src/nx-foreign-workspace-env.test.ts +329 -0
- package/src/nx-incomplete-run-summary.test.ts +70 -0
- package/src/nx-plugin-load-errors.test.ts +120 -0
- package/src/nx-project-resolution-cache.test.ts +176 -0
- package/src/nx-raw-inputs.test.ts +166 -0
- package/src/nx-resident-graph-workers.test.ts +7 -16
- package/src/nx-run-information.test.ts +118 -0
- package/src/nx-skipped-task-exit.test.ts +180 -0
- package/src/nx-state-location.test.ts +63 -0
- package/src/nx-store-resolution.test.ts +26 -25
- package/src/nx-task-graph-cycles.test.ts +148 -0
- package/src/nx-task-output-lease.test.ts +414 -0
- package/src/nx-use-local.test.ts +103 -0
- package/src/package-target-policy.ts +3 -6
- package/src/release-config-policy.ts +3 -6
- package/src/runtime-input.test.ts +215 -0
- package/src/runtime-input.ts +17 -0
- package/src/rust-tokens.ts +78 -0
- package/src/serial-group-wiring.test.ts +326 -0
- package/src/task-history-namespace.test.ts +87 -89
- package/src/task-results.test.ts +226 -0
- package/src/task-results.ts +75 -0
- package/src/test-program.test.ts +303 -0
- package/src/test-program.ts +341 -0
- package/src/test-timings.test.ts +240 -0
- package/src/test-timings.ts +311 -0
- package/src/test-walk-policy.test.ts +585 -0
- package/src/test-walk-policy.ts +731 -0
- package/src/testing.test.ts +509 -0
- package/src/testing.ts +922 -0
- package/src/type-guards.ts +4 -0
- package/src/typecheck-test-policy.test.ts +496 -11
- package/src/typecheck-test-policy.ts +137 -103
- package/src/workspace-config-policy.test.ts +212 -13
- package/src/workspace-config-policy.ts +101 -73
- package/src/workspace-index.ts +18 -0
- package/src/workspace-manifest.ts +41 -0
- package/test-timings.json +85 -30
- package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
- package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
|
@@ -0,0 +1,845 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable built snapshots of a source-linked package.
|
|
3
|
+
*
|
|
4
|
+
* A `smoo.developerLinks` declaration with a `snapshot` binds the package's
|
|
5
|
+
* aliases to a sealed copy of its checkout instead of the live directory. The
|
|
6
|
+
* provider's land, rebuild or install rewrites its `dist`, its native library
|
|
7
|
+
* and its own `node_modules/.bun` store whenever it likes; a consumer task that
|
|
8
|
+
* resolved the live directory would then read bytes no key of it describes. A
|
|
9
|
+
* snapshot is copied from one provider state and never changes: a provider
|
|
10
|
+
* change reaches the consumer only as another snapshot under another name.
|
|
11
|
+
*
|
|
12
|
+
* What a snapshot holds, laid out as in the provider checkout so relative
|
|
13
|
+
* resolution means what it meant there (exports, tsconfig `paths`, and the
|
|
14
|
+
* native resolver's checkout-relative library):
|
|
15
|
+
*
|
|
16
|
+
* - every provider workspace package in the declared package's link closure:
|
|
17
|
+
* its manifest, the files its manifest's `files` publishes, and its
|
|
18
|
+
* `tsconfig.json` with every relative `extends` (Bun applies a source
|
|
19
|
+
* module's tsconfig `paths`);
|
|
20
|
+
* - the provider root manifest, which owns the files outside every package;
|
|
21
|
+
* - the native library each declared artifact module's `nativeArtifactPath({})`
|
|
22
|
+
* names: the checkout's own build, never a consumer override;
|
|
23
|
+
* - each such package's `node_modules` links. A link into the provider's own
|
|
24
|
+
* `node_modules/.bun` store copies that store entry whole; a link into Bun's
|
|
25
|
+
* global cache stays an absolute link to it. Any other target refuses.
|
|
26
|
+
*
|
|
27
|
+
* Provenance is the provider's own Nx producer receipt. Every declared target
|
|
28
|
+
* must be a probe hit (the read-only `probeBuilt`, which runs and
|
|
29
|
+
* writes nothing), all at one HEAD, and every copied provider file must be
|
|
30
|
+
* either the blob HEAD tracks at its path or an output the receipt inventories
|
|
31
|
+
* with the same bytes. A stale output, a modified or untracked source file, or
|
|
32
|
+
* a target that is not current refuses with its cause; nothing in the provider
|
|
33
|
+
* is built or written. The capture is bracketed by a stat fingerprint of the
|
|
34
|
+
* provider files it copies, so a provider written to while it is copied is
|
|
35
|
+
* refused rather than half taken.
|
|
36
|
+
*
|
|
37
|
+
* A snapshot is named by the SHA-256 of its census (every entry's path, type,
|
|
38
|
+
* executable bit, and its bytes or link text) together with its producer
|
|
39
|
+
* receipt identity: the provider root, HEAD and its tree, the probe's file-input
|
|
40
|
+
* provenance, and every task id and hash. Other bytes, another commit or another
|
|
41
|
+
* task hash is another snapshot, and nothing binds without a fresh probe, so no
|
|
42
|
+
* name or provenance is ever reused for a provider state it does not describe.
|
|
43
|
+
* Files are sealed read-only, and a reader that trusts a snapshot verifies its
|
|
44
|
+
* census and name (`verifySnapshot`): modes and paths prove nothing.
|
|
45
|
+
*
|
|
46
|
+
* The store is `.cache/linked-snapshots` in the consumer checkout. Under cowshed
|
|
47
|
+
* it is declared build state (`.cowshed.toml` `[build] state`), so it lives on
|
|
48
|
+
* the checkout's build volume and travels with it (smoothbricks
|
|
49
|
+
* specs/cowshed/16_build_volumes.md, "Declared build state"). A store cowshed
|
|
50
|
+
* has not linked yet is refused, never filled: a refresh discards a real
|
|
51
|
+
* directory at a declared path.
|
|
52
|
+
*
|
|
53
|
+
* Dependency-free except for the probe, which a capture loads from the
|
|
54
|
+
* consumer's installed @smoothbricks/nx-plugin only when it needs one: shell
|
|
55
|
+
* entry imports this before anything is installed.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
import { spawnSync } from 'node:child_process';
|
|
59
|
+
import { createHash } from 'node:crypto';
|
|
60
|
+
import {
|
|
61
|
+
chmodSync,
|
|
62
|
+
constants,
|
|
63
|
+
copyFileSync,
|
|
64
|
+
type Dirent,
|
|
65
|
+
lstatSync,
|
|
66
|
+
mkdirSync,
|
|
67
|
+
mkdtempSync,
|
|
68
|
+
readdirSync,
|
|
69
|
+
readFileSync,
|
|
70
|
+
readlinkSync,
|
|
71
|
+
realpathSync,
|
|
72
|
+
renameSync,
|
|
73
|
+
rmSync,
|
|
74
|
+
statSync,
|
|
75
|
+
symlinkSync,
|
|
76
|
+
writeFileSync,
|
|
77
|
+
} from 'node:fs';
|
|
78
|
+
import path from 'node:path';
|
|
79
|
+
import type * as EnsureBuiltModule from '@smoothbricks/nx-plugin/ensure-built';
|
|
80
|
+
import type { EnsureBuiltProbeResult } from '@smoothbricks/nx-plugin/ensure-built';
|
|
81
|
+
|
|
82
|
+
/** Presence only: what a loaded probe does is the plugin's contract, as the CLI checks keepDeveloperLinks. */
|
|
83
|
+
function isProbeBuilt(value: unknown): value is typeof EnsureBuiltModule.probeBuilt {
|
|
84
|
+
return typeof value === 'function';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Where a consumer checkout keeps its snapshots, relative to its root. */
|
|
88
|
+
export const SNAPSHOT_STORE = path.join('.cache', 'linked-snapshots');
|
|
89
|
+
|
|
90
|
+
/** The file at the root of each sealed snapshot naming its identity and every entry. */
|
|
91
|
+
export const SNAPSHOT_RECEIPT = '.linked-snapshot.json';
|
|
92
|
+
|
|
93
|
+
/** What a `snapshot` declaration asks of the provider: the targets that build it and its native resolvers. */
|
|
94
|
+
export interface SnapshotDeclaration {
|
|
95
|
+
/** Provider Nx targets (`project:target`) whose recorded outputs the snapshot publishes. */
|
|
96
|
+
readonly targets: readonly string[];
|
|
97
|
+
/** Modules of the package exporting `nativeArtifactPath()`, its own resolution of a native library. */
|
|
98
|
+
readonly artifactModules: readonly string[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type CensusEntry =
|
|
102
|
+
| { readonly path: string; readonly type: 'file'; readonly executable: boolean; readonly sha256: string }
|
|
103
|
+
| { readonly path: string; readonly type: 'symlink'; readonly linkText: string };
|
|
104
|
+
|
|
105
|
+
/** The provider state a snapshot was copied from: its HEAD and the Nx tasks whose outputs it holds. */
|
|
106
|
+
export interface SnapshotProvenance {
|
|
107
|
+
readonly root: string;
|
|
108
|
+
readonly head: string;
|
|
109
|
+
readonly headTree: string;
|
|
110
|
+
/** How each probe vouched for its file inputs: exactly, or conservatively over the whole provider source. */
|
|
111
|
+
readonly fileInputs: readonly string[];
|
|
112
|
+
readonly tasks: readonly { readonly id: string; readonly hash: string }[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface SnapshotReceipt {
|
|
116
|
+
readonly version: 1;
|
|
117
|
+
readonly id: string;
|
|
118
|
+
readonly name: string;
|
|
119
|
+
/** The declared package's directory inside the snapshot. */
|
|
120
|
+
readonly packagePath: string;
|
|
121
|
+
readonly provenance: SnapshotProvenance;
|
|
122
|
+
readonly census: readonly CensusEntry[];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type SnapshotBinding =
|
|
126
|
+
| { readonly kind: 'snapshot'; readonly id: string; readonly directory: string }
|
|
127
|
+
| { readonly kind: 'refused'; readonly reason: string };
|
|
128
|
+
|
|
129
|
+
/** Asks the provider whether `target` is current, without running anything. */
|
|
130
|
+
export type ProducerProbe = (target: string, providerRoot: string) => Promise<EnsureBuiltProbeResult>;
|
|
131
|
+
|
|
132
|
+
/** A known reason a snapshot cannot be made or trusted. It is said, never keyed. */
|
|
133
|
+
export class SnapshotRefusal extends Error {}
|
|
134
|
+
|
|
135
|
+
type PlanNode =
|
|
136
|
+
| { readonly type: 'file'; readonly source: string; readonly provenance: 'provider' | 'store' }
|
|
137
|
+
| { readonly type: 'symlink'; readonly linkText: string };
|
|
138
|
+
|
|
139
|
+
interface SnapshotPlan {
|
|
140
|
+
readonly name: string;
|
|
141
|
+
readonly root: string;
|
|
142
|
+
readonly packagePath: string;
|
|
143
|
+
readonly nodes: ReadonlyMap<string, PlanNode>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
type ProducerOutput = Extract<
|
|
147
|
+
EnsureBuiltProbeResult,
|
|
148
|
+
{ readonly disposition: 'hit' }
|
|
149
|
+
>['receipt']['tasks'][number]['outputs'][number];
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The snapshot `source` binds to now: its provider is probed, copied and attested every time, and the result is
|
|
153
|
+
* the sealed snapshot of exactly that census and receipt, published now or verified when it already exists. A
|
|
154
|
+
* refusal names its cause and leaves every binding as it was.
|
|
155
|
+
*/
|
|
156
|
+
export async function snapshotBinding(
|
|
157
|
+
root: string,
|
|
158
|
+
name: string,
|
|
159
|
+
source: string,
|
|
160
|
+
declaration: SnapshotDeclaration,
|
|
161
|
+
probe: ProducerProbe = installedProbe(root),
|
|
162
|
+
): Promise<SnapshotBinding> {
|
|
163
|
+
try {
|
|
164
|
+
const store = snapshotStore(root);
|
|
165
|
+
const plan = planSnapshot(source, name, declaration);
|
|
166
|
+
const id = await capture(store, plan, declaration, probe, fingerprintOf(plan));
|
|
167
|
+
return { kind: 'snapshot', id, directory: path.join(store, id, plan.packagePath) };
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error instanceof SnapshotRefusal) return { kind: 'refused', reason: error.message };
|
|
170
|
+
if (error instanceof Error && 'code' in error && typeof error.code === 'string') {
|
|
171
|
+
return { kind: 'refused', reason: error.message };
|
|
172
|
+
}
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The store under `root`. A cowshed checkout keeps it on its build volume: until a refresh has linked it there,
|
|
179
|
+
* an empty directory is left for that refresh to adopt and the capture refuses.
|
|
180
|
+
*/
|
|
181
|
+
function snapshotStore(root: string): string {
|
|
182
|
+
const store = path.join(root, SNAPSHOT_STORE);
|
|
183
|
+
if (lstatSync(path.join(root, '.cowshed', 'build'), { throwIfNoEntry: false })?.isSymbolicLink() !== true) {
|
|
184
|
+
mkdirSync(store, { recursive: true });
|
|
185
|
+
return store;
|
|
186
|
+
}
|
|
187
|
+
const state = lstatSync(store, { throwIfNoEntry: false });
|
|
188
|
+
if (state?.isSymbolicLink() === true) return store;
|
|
189
|
+
if (state === undefined) mkdirSync(store, { recursive: true });
|
|
190
|
+
throw new SnapshotRefusal(
|
|
191
|
+
`${store} is not on this checkout's build volume: declare "${SNAPSHOT_STORE}" in .cowshed.toml [build] state and run \`cowshed exec <workspace> -- true\` so its refresh links it`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The consumer's installed nx-plugin probe, loaded when a capture first asks. An installed plugin without the
|
|
197
|
+
* read-only `probeBuilt` is refused before anything is called: its `ensureBuilt` builds, and a capture must never
|
|
198
|
+
* write the provider.
|
|
199
|
+
*/
|
|
200
|
+
function installedProbe(root: string): ProducerProbe {
|
|
201
|
+
return async (target, providerRoot) => {
|
|
202
|
+
// Dynamic by necessity: this file runs before anything is installed, and only a capture needs the plugin.
|
|
203
|
+
const specifier = Bun.resolveSync('@smoothbricks/nx-plugin/ensure-built', root);
|
|
204
|
+
const plugin: unknown = await import(specifier);
|
|
205
|
+
const probeBuilt = typeof plugin === 'object' && plugin !== null ? Reflect.get(plugin, 'probeBuilt') : undefined;
|
|
206
|
+
if (!isProbeBuilt(probeBuilt)) {
|
|
207
|
+
throw new SnapshotRefusal(
|
|
208
|
+
`the installed @smoothbricks/nx-plugin (${specifier}) has no read-only probeBuilt, so ${target} cannot be vouched for without building it; install a plugin that exports probeBuilt`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
return probeBuilt({ target, cwd: providerRoot });
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** What a snapshot of `source` would hold, read from the provider as it is now. */
|
|
216
|
+
function planSnapshot(source: string, name: string, declaration: SnapshotDeclaration): SnapshotPlan {
|
|
217
|
+
const directory = realpathSync(source);
|
|
218
|
+
const root = providerRoot(directory);
|
|
219
|
+
const cache = bunCacheRoot(root);
|
|
220
|
+
const nodes = new Map<string, PlanNode>();
|
|
221
|
+
const pending = [directory];
|
|
222
|
+
const packages = new Map<string, Record<string, unknown>>();
|
|
223
|
+
const entries = new Set<string>();
|
|
224
|
+
const inside = (target: string): string => {
|
|
225
|
+
const relative = path.relative(root, target);
|
|
226
|
+
if (!contained(relative)) throw new SnapshotRefusal(`${target} lies outside the provider checkout ${root}`);
|
|
227
|
+
return relative;
|
|
228
|
+
};
|
|
229
|
+
const addFile = (file: string, provenance: 'provider' | 'store') => {
|
|
230
|
+
nodes.set(inside(file), { type: 'file', source: file, provenance });
|
|
231
|
+
};
|
|
232
|
+
const bind = (at: string) => {
|
|
233
|
+
const linkPath = inside(at);
|
|
234
|
+
let target: string;
|
|
235
|
+
try {
|
|
236
|
+
target = realpathSync(at);
|
|
237
|
+
} catch {
|
|
238
|
+
throw new SnapshotRefusal(`${at} is a link to nothing`);
|
|
239
|
+
}
|
|
240
|
+
if (contained(path.relative(cache, target))) {
|
|
241
|
+
nodes.set(linkPath, { type: 'symlink', linkText: target });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const relative = path.relative(root, target);
|
|
245
|
+
if (!contained(relative)) {
|
|
246
|
+
throw new SnapshotRefusal(`${at} resolves to ${target}, outside the provider checkout and Bun's cache ${cache}`);
|
|
247
|
+
}
|
|
248
|
+
const segments = relative.split(path.sep);
|
|
249
|
+
const modules = segments.indexOf('node_modules');
|
|
250
|
+
if (modules === -1) {
|
|
251
|
+
pending.push(target);
|
|
252
|
+
} else if (modules === 0 && segments[1] === '.bun' && segments[2] !== undefined) {
|
|
253
|
+
addEntry(path.join(root, 'node_modules', '.bun', segments[2]));
|
|
254
|
+
} else {
|
|
255
|
+
throw new SnapshotRefusal(`${at} resolves to ${target}, an install layout a snapshot cannot carry`);
|
|
256
|
+
}
|
|
257
|
+
nodes.set(linkPath, { type: 'symlink', linkText: path.relative(path.dirname(linkPath), relative) });
|
|
258
|
+
};
|
|
259
|
+
const walk = (at: string) => {
|
|
260
|
+
for (const entry of readdirSync(at, { withFileTypes: true })) {
|
|
261
|
+
const child = path.join(at, entry.name);
|
|
262
|
+
if (entry.isSymbolicLink()) bind(child);
|
|
263
|
+
else if (entry.isDirectory()) walk(child);
|
|
264
|
+
else if (entry.isFile()) addFile(child, 'store');
|
|
265
|
+
else throw new SnapshotRefusal(`${child} is neither a file, a directory nor a link`);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
const addEntry = (entry: string) => {
|
|
269
|
+
if (entries.has(entry)) return;
|
|
270
|
+
entries.add(entry);
|
|
271
|
+
walk(entry);
|
|
272
|
+
};
|
|
273
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
274
|
+
if (packages.has(next)) continue;
|
|
275
|
+
const manifestPath = path.join(next, 'package.json');
|
|
276
|
+
const manifest = readManifest(manifestPath);
|
|
277
|
+
packages.set(next, manifest);
|
|
278
|
+
addFile(manifestPath, 'provider');
|
|
279
|
+
for (const file of packedFiles(next, manifest)) addFile(file, 'provider');
|
|
280
|
+
for (const config of tsconfigChain(path.join(next, 'tsconfig.json'))) addFile(config, 'provider');
|
|
281
|
+
for (const link of packageLinks(path.join(next, 'node_modules'))) bind(link);
|
|
282
|
+
}
|
|
283
|
+
addFile(path.join(root, 'package.json'), 'provider');
|
|
284
|
+
for (const specifier of declaration.artifactModules) addFile(nativeArtifact(specifier, directory), 'provider');
|
|
285
|
+
for (const [at, manifest] of packages) assertExportsPublished(at, manifest, root, nodes);
|
|
286
|
+
return { name, root, packagePath: path.relative(root, directory), nodes };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The files a package's manifest `files` publishes, absolute. Shared with the linked-package digest, so the two
|
|
291
|
+
* agree on what a source-linked package is made of. A package that publishes nothing refuses.
|
|
292
|
+
*/
|
|
293
|
+
export function packedFiles(
|
|
294
|
+
directory: string,
|
|
295
|
+
manifest: { readonly name?: unknown; readonly files?: unknown },
|
|
296
|
+
): string[] {
|
|
297
|
+
const name = typeof manifest.name === 'string' ? manifest.name : directory;
|
|
298
|
+
const packed = manifest.files;
|
|
299
|
+
if (
|
|
300
|
+
!Array.isArray(packed) ||
|
|
301
|
+
packed.length === 0 ||
|
|
302
|
+
!packed.every((pattern): pattern is string => typeof pattern === 'string')
|
|
303
|
+
) {
|
|
304
|
+
throw new SnapshotRefusal(`source-linked ${name} at ${directory} has no declared source/output file closure`);
|
|
305
|
+
}
|
|
306
|
+
const excluded = packed.filter((pattern) => pattern.startsWith('!')).map((pattern) => new Bun.Glob(pattern.slice(1)));
|
|
307
|
+
const files: string[] = [];
|
|
308
|
+
for (const pattern of packed.filter((pattern) => !pattern.startsWith('!'))) {
|
|
309
|
+
let glob = pattern;
|
|
310
|
+
if (!/[*?[\]{}]/.test(pattern) && statSync(path.join(directory, pattern)).isDirectory()) glob = `${pattern}/**/*`;
|
|
311
|
+
for (const file of new Bun.Glob(glob).scanSync({ cwd: directory, dot: true, onlyFiles: true })) {
|
|
312
|
+
if (excluded.some((exclude) => exclude.match(file))) continue;
|
|
313
|
+
files.push(path.resolve(directory, file));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (files.length === 0)
|
|
317
|
+
throw new SnapshotRefusal(`source-linked ${name} at ${directory} has no readable source/output files`);
|
|
318
|
+
return files;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The nearest directory at or above `directory` whose manifest declares workspaces, else the package itself. */
|
|
322
|
+
function providerRoot(directory: string): string {
|
|
323
|
+
for (let at = directory; ; at = path.dirname(at)) {
|
|
324
|
+
const manifestPath = path.join(at, 'package.json');
|
|
325
|
+
if (
|
|
326
|
+
lstatSync(manifestPath, { throwIfNoEntry: false }) !== undefined &&
|
|
327
|
+
readManifest(manifestPath).workspaces !== undefined
|
|
328
|
+
) {
|
|
329
|
+
return at;
|
|
330
|
+
}
|
|
331
|
+
if (path.dirname(at) === at) return directory;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Bun's immutable install cache as the provider's Bun names it. */
|
|
336
|
+
function bunCacheRoot(root: string): string {
|
|
337
|
+
const run = spawnSync('bun', ['pm', 'cache'], { cwd: root, encoding: 'utf8' });
|
|
338
|
+
if (run.error !== undefined || run.status !== 0) {
|
|
339
|
+
throw new SnapshotRefusal(
|
|
340
|
+
`cannot establish Bun's immutable cache root in ${root}: ${run.error?.message ?? run.stderr}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
return realpathSync(run.stdout.trim());
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function readManifest(manifestPath: string): Record<string, unknown> {
|
|
347
|
+
let manifest: unknown;
|
|
348
|
+
try {
|
|
349
|
+
manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
350
|
+
} catch (error) {
|
|
351
|
+
throw new SnapshotRefusal(`cannot read ${manifestPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
352
|
+
}
|
|
353
|
+
if (typeof manifest !== 'object' || manifest === null || Array.isArray(manifest)) {
|
|
354
|
+
throw new SnapshotRefusal(`${manifestPath} is not a package manifest`);
|
|
355
|
+
}
|
|
356
|
+
return Object.fromEntries(Object.entries(manifest));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** A package's `tsconfig.json` and every configuration it extends, by relative path only. */
|
|
360
|
+
function tsconfigChain(config: string): string[] {
|
|
361
|
+
if (lstatSync(config, { throwIfNoEntry: false }) === undefined) return [];
|
|
362
|
+
const chain: string[] = [];
|
|
363
|
+
const visit = (file: string) => {
|
|
364
|
+
if (chain.includes(file)) return;
|
|
365
|
+
chain.push(file);
|
|
366
|
+
let parsed: unknown;
|
|
367
|
+
try {
|
|
368
|
+
parsed = Bun.JSONC.parse(readFileSync(file, 'utf8'));
|
|
369
|
+
} catch (error) {
|
|
370
|
+
throw new SnapshotRefusal(`cannot read ${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
371
|
+
}
|
|
372
|
+
const extended = typeof parsed === 'object' && parsed !== null ? Reflect.get(parsed, 'extends') : undefined;
|
|
373
|
+
if (extended === undefined) return;
|
|
374
|
+
for (const base of Array.isArray(extended) ? extended : [extended]) {
|
|
375
|
+
if (typeof base !== 'string' || !(base.startsWith('./') || base.startsWith('../'))) {
|
|
376
|
+
throw new SnapshotRefusal(`${file} extends ${JSON.stringify(base)}, which a snapshot cannot carry`);
|
|
377
|
+
}
|
|
378
|
+
const resolved = path.resolve(path.dirname(file), base);
|
|
379
|
+
const named = path.extname(resolved) === '.json' ? resolved : `${resolved}.json`;
|
|
380
|
+
if (lstatSync(named, { throwIfNoEntry: false }) === undefined) {
|
|
381
|
+
throw new SnapshotRefusal(`${file} extends ${base}, which does not exist`);
|
|
382
|
+
}
|
|
383
|
+
visit(named);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
visit(config);
|
|
387
|
+
return chain;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Every link in a `node_modules` tree a package name could be: `name` and `@scope/name`. */
|
|
391
|
+
function packageLinks(tree: string): string[] {
|
|
392
|
+
const links: string[] = [];
|
|
393
|
+
for (const entry of directoryEntries(tree)) {
|
|
394
|
+
if (entry.name.startsWith('.')) continue;
|
|
395
|
+
const at = path.join(tree, entry.name);
|
|
396
|
+
if (entry.isSymbolicLink()) {
|
|
397
|
+
links.push(at);
|
|
398
|
+
} else if (entry.name.startsWith('@') && entry.isDirectory()) {
|
|
399
|
+
for (const scoped of directoryEntries(at)) {
|
|
400
|
+
const scopedAt = path.join(at, scoped.name);
|
|
401
|
+
if (!scoped.isSymbolicLink())
|
|
402
|
+
throw new SnapshotRefusal(`${scopedAt} is not a link: a snapshot binds installs by link`);
|
|
403
|
+
links.push(scopedAt);
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
throw new SnapshotRefusal(`${at} is not a link: a snapshot binds installs by link`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return links;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function directoryEntries(directory: string): Dirent[] {
|
|
413
|
+
try {
|
|
414
|
+
return readdirSync(directory, { withFileTypes: true });
|
|
415
|
+
} catch (error) {
|
|
416
|
+
if (error instanceof Error && 'code' in error && (error.code === 'ENOENT' || error.code === 'ENOTDIR')) return [];
|
|
417
|
+
throw error;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** The native library a provider module resolves for its own checkout: no consumer override reaches it. */
|
|
422
|
+
function nativeArtifact(specifier: string, directory: string): string {
|
|
423
|
+
let resolver: unknown;
|
|
424
|
+
try {
|
|
425
|
+
resolver = import.meta.require(Bun.resolveSync(specifier, directory));
|
|
426
|
+
} catch (error) {
|
|
427
|
+
throw new SnapshotRefusal(
|
|
428
|
+
`cannot load ${specifier} from ${directory}: ${error instanceof Error ? error.message : String(error)}`,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
const resolve =
|
|
432
|
+
typeof resolver === 'object' && resolver !== null ? Reflect.get(resolver, 'nativeArtifactPath') : undefined;
|
|
433
|
+
if (typeof resolve !== 'function') throw new SnapshotRefusal(`${specifier} exports no nativeArtifactPath()`);
|
|
434
|
+
let artifact: unknown;
|
|
435
|
+
try {
|
|
436
|
+
artifact = resolve({});
|
|
437
|
+
} catch (error) {
|
|
438
|
+
// The provider's own words: an unbuilt library names the target that builds it and where.
|
|
439
|
+
throw new SnapshotRefusal(error instanceof Error ? error.message : String(error));
|
|
440
|
+
}
|
|
441
|
+
if (typeof artifact !== 'string') throw new SnapshotRefusal(`${specifier} nativeArtifactPath() returned no path`);
|
|
442
|
+
return artifact;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** Every relative target a package's `exports` names must be a file the snapshot publishes. */
|
|
446
|
+
function assertExportsPublished(
|
|
447
|
+
directory: string,
|
|
448
|
+
manifest: Record<string, unknown>,
|
|
449
|
+
root: string,
|
|
450
|
+
nodes: ReadonlyMap<string, PlanNode>,
|
|
451
|
+
): void {
|
|
452
|
+
const visit = (value: unknown, at: string) => {
|
|
453
|
+
if (typeof value === 'string') {
|
|
454
|
+
if (!value.startsWith('./') || value.includes('*')) return;
|
|
455
|
+
if (nodes.get(path.relative(root, path.resolve(directory, value)))?.type !== 'file') {
|
|
456
|
+
throw new SnapshotRefusal(`${directory} exports ${at} as ${value}, which it does not publish`);
|
|
457
|
+
}
|
|
458
|
+
} else if (Array.isArray(value)) {
|
|
459
|
+
for (const item of value) visit(item, at);
|
|
460
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
461
|
+
for (const [key, item] of Object.entries(value)) visit(item, key.startsWith('.') ? key : `${at} (${key})`);
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
visit(manifest.exports, '.');
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Each planned file's identity as the filesystem states it: any write to it moves a field. */
|
|
468
|
+
function fingerprintOf(plan: SnapshotPlan): string {
|
|
469
|
+
const hash = createHash('sha256');
|
|
470
|
+
for (const [relative, node] of [...plan.nodes].sort(([left], [right]) => compare(left, right))) {
|
|
471
|
+
if (node.type === 'symlink') {
|
|
472
|
+
hash.update(`${relative}\0link\0${node.linkText}\n`);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
const stats = statSync(node.source, { bigint: true });
|
|
476
|
+
hash.update(
|
|
477
|
+
`${relative}\0file\0${stats.dev}:${stats.ino}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}:${stats.mode}\n`,
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
return hash.digest('hex');
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Copies `plan` into the store under its census name, after the provider's receipt vouches for every file. */
|
|
484
|
+
async function capture(
|
|
485
|
+
store: string,
|
|
486
|
+
plan: SnapshotPlan,
|
|
487
|
+
declaration: SnapshotDeclaration,
|
|
488
|
+
probe: ProducerProbe,
|
|
489
|
+
fingerprint: string,
|
|
490
|
+
): Promise<string> {
|
|
491
|
+
const { provenance, outputs } = await producerReceipt(plan, declaration, probe);
|
|
492
|
+
const tracked = trackedBlobs(plan, provenance.head);
|
|
493
|
+
const incoming = mkdtempSync(path.join(store, '.incoming-'));
|
|
494
|
+
try {
|
|
495
|
+
const census: CensusEntry[] = [];
|
|
496
|
+
for (const [relative, node] of [...plan.nodes].sort(([left], [right]) => compare(left, right))) {
|
|
497
|
+
const destination = path.join(incoming, relative);
|
|
498
|
+
mkdirSync(path.dirname(destination), { recursive: true });
|
|
499
|
+
if (node.type === 'symlink') {
|
|
500
|
+
symlinkSync(node.linkText, destination);
|
|
501
|
+
census.push({ path: relative, type: 'symlink', linkText: node.linkText });
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
copyFileSync(node.source, destination, constants.COPYFILE_FICLONE);
|
|
505
|
+
const executable = (lstatSync(destination).mode & 0o111) !== 0;
|
|
506
|
+
chmodSync(destination, executable ? 0o555 : 0o444);
|
|
507
|
+
const bytes = readFileSync(destination);
|
|
508
|
+
const digest = sha256(bytes);
|
|
509
|
+
if (node.provenance === 'provider')
|
|
510
|
+
attest(relative, bytes, digest, executable, tracked, outputs, plan, provenance);
|
|
511
|
+
census.push({ path: relative, type: 'file', executable, sha256: digest });
|
|
512
|
+
}
|
|
513
|
+
if (fingerprintOf(planSnapshot(path.join(plan.root, plan.packagePath), plan.name, declaration)) !== fingerprint) {
|
|
514
|
+
throw new SnapshotRefusal(
|
|
515
|
+
`${plan.root} changed while it was copied; nothing was published, so retry once it is quiet`,
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
const id = snapshotId(census, provenance);
|
|
519
|
+
const receipt: SnapshotReceipt = {
|
|
520
|
+
version: 1,
|
|
521
|
+
id,
|
|
522
|
+
name: plan.name,
|
|
523
|
+
packagePath: plan.packagePath,
|
|
524
|
+
provenance,
|
|
525
|
+
census,
|
|
526
|
+
};
|
|
527
|
+
writeFileSync(path.join(incoming, SNAPSHOT_RECEIPT), `${JSON.stringify(receipt)}\n`, { mode: 0o444 });
|
|
528
|
+
publish(incoming, path.join(store, id), id);
|
|
529
|
+
return id;
|
|
530
|
+
} finally {
|
|
531
|
+
rmSync(incoming, { recursive: true, force: true });
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Every declared target probed as a hit at one HEAD, and the outputs those hits inventory by path. A target that
|
|
537
|
+
* is not current is refused with the `nx run` that makes it so.
|
|
538
|
+
*/
|
|
539
|
+
async function producerReceipt(
|
|
540
|
+
plan: SnapshotPlan,
|
|
541
|
+
declaration: SnapshotDeclaration,
|
|
542
|
+
probe: ProducerProbe,
|
|
543
|
+
): Promise<{ readonly provenance: SnapshotProvenance; readonly outputs: ReadonlyMap<string, ProducerOutput> }> {
|
|
544
|
+
if (declaration.targets.length === 0)
|
|
545
|
+
throw new SnapshotRefusal(`${plan.name} declares no provider targets to vouch for its build`);
|
|
546
|
+
let head: { readonly head: string; readonly headTree: string } | undefined;
|
|
547
|
+
const tasks = new Map<string, string>();
|
|
548
|
+
const fileInputs = new Set<string>();
|
|
549
|
+
const outputs = new Map<string, ProducerOutput>();
|
|
550
|
+
for (const target of declaration.targets) {
|
|
551
|
+
const result = await probe(target, plan.root);
|
|
552
|
+
switch (result.disposition) {
|
|
553
|
+
case 'hit':
|
|
554
|
+
break;
|
|
555
|
+
case 'miss':
|
|
556
|
+
throw new SnapshotRefusal(
|
|
557
|
+
`${target} is not current in ${plan.root} (${result.reason.kind}: ${result.reason.taskId}); run \`nx run ${target}\` there`,
|
|
558
|
+
);
|
|
559
|
+
case 'failed':
|
|
560
|
+
throw new SnapshotRefusal(`probing ${target} in ${plan.root} failed: ${JSON.stringify(result.reason)}`);
|
|
561
|
+
default: {
|
|
562
|
+
const unreachable: never = result;
|
|
563
|
+
throw new Error(`unknown probe disposition: ${JSON.stringify(unreachable)}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const receipt = result.receipt;
|
|
567
|
+
if (realpathSync(receipt.workspaceRoot) !== plan.root) {
|
|
568
|
+
throw new SnapshotRefusal(`${target} was probed in ${receipt.workspaceRoot}, not ${plan.root}`);
|
|
569
|
+
}
|
|
570
|
+
head ??= { head: receipt.head, headTree: receipt.headTree };
|
|
571
|
+
if (receipt.head !== head.head || receipt.headTree !== head.headTree) {
|
|
572
|
+
throw new SnapshotRefusal(`${plan.root} moved from ${head.head} to ${receipt.head} while it was probed`);
|
|
573
|
+
}
|
|
574
|
+
fileInputs.add(receipt.fileInputProvenance);
|
|
575
|
+
for (const task of receipt.tasks) {
|
|
576
|
+
const known = tasks.get(task.id);
|
|
577
|
+
if (known !== undefined && known !== task.hash) {
|
|
578
|
+
throw new SnapshotRefusal(`${task.id} hashed ${known} and ${task.hash} in one capture`);
|
|
579
|
+
}
|
|
580
|
+
tasks.set(task.id, task.hash);
|
|
581
|
+
for (const output of task.outputs) {
|
|
582
|
+
const existing = outputs.get(output.path);
|
|
583
|
+
if (existing !== undefined && JSON.stringify(existing) !== JSON.stringify(output)) {
|
|
584
|
+
throw new SnapshotRefusal(`${output.path} is inventoried with different bytes by two tasks`);
|
|
585
|
+
}
|
|
586
|
+
outputs.set(output.path, output);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (head === undefined) throw new SnapshotRefusal(`${plan.name} has no provider receipt`);
|
|
591
|
+
return {
|
|
592
|
+
provenance: {
|
|
593
|
+
root: plan.root,
|
|
594
|
+
head: head.head,
|
|
595
|
+
headTree: head.headTree,
|
|
596
|
+
fileInputs: [...fileInputs].sort(compare),
|
|
597
|
+
tasks: [...tasks].sort(([left], [right]) => compare(left, right)).map(([id, hash]) => ({ id, hash })),
|
|
598
|
+
},
|
|
599
|
+
outputs,
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** The blob HEAD tracks at each planned provider path, with its mode. */
|
|
604
|
+
function trackedBlobs(
|
|
605
|
+
plan: SnapshotPlan,
|
|
606
|
+
head: string,
|
|
607
|
+
): ReadonlyMap<string, { readonly mode: string; readonly id: string }> {
|
|
608
|
+
const paths = [...plan.nodes]
|
|
609
|
+
.filter(([, node]) => node.type === 'file' && node.provenance === 'provider')
|
|
610
|
+
.map(([relative]) => relative);
|
|
611
|
+
const run = spawnSync('git', ['-C', plan.root, 'ls-tree', '-z', head, '--', ...paths], {
|
|
612
|
+
encoding: 'utf8',
|
|
613
|
+
maxBuffer: 1 << 28,
|
|
614
|
+
});
|
|
615
|
+
if (run.error !== undefined || run.status !== 0) {
|
|
616
|
+
throw new SnapshotRefusal(`cannot list ${plan.root} at ${head}: ${run.error?.message ?? run.stderr}`);
|
|
617
|
+
}
|
|
618
|
+
const blobs = new Map<string, { readonly mode: string; readonly id: string }>();
|
|
619
|
+
for (const line of run.stdout.split('\0')) {
|
|
620
|
+
if (line === '') continue;
|
|
621
|
+
const match = /^(\d+) blob ([0-9a-f]+)\t(.*)$/s.exec(line);
|
|
622
|
+
if (match?.[1] !== undefined && match[2] !== undefined && match[3] !== undefined) {
|
|
623
|
+
blobs.set(match[3], { mode: match[1], id: match[2] });
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return blobs;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** A provider file is HEAD's tracked blob or an output its producing task recorded, byte for byte. */
|
|
630
|
+
function attest(
|
|
631
|
+
relative: string,
|
|
632
|
+
bytes: Uint8Array,
|
|
633
|
+
digest: string,
|
|
634
|
+
executable: boolean,
|
|
635
|
+
tracked: ReadonlyMap<string, { readonly mode: string; readonly id: string }>,
|
|
636
|
+
outputs: ReadonlyMap<string, ProducerOutput>,
|
|
637
|
+
plan: SnapshotPlan,
|
|
638
|
+
provenance: SnapshotProvenance,
|
|
639
|
+
): void {
|
|
640
|
+
const blob = tracked.get(relative);
|
|
641
|
+
if (blob !== undefined) {
|
|
642
|
+
if (gitBlobId(bytes, blob.id.length) !== blob.id || (blob.mode === '100755') !== executable) {
|
|
643
|
+
throw new SnapshotRefusal(
|
|
644
|
+
`${path.join(plan.root, relative)} differs from HEAD ${provenance.head}: a snapshot is one commit's source, so commit or discard it`,
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
const output = outputs.get(relative);
|
|
650
|
+
if (output === undefined) {
|
|
651
|
+
throw new SnapshotRefusal(
|
|
652
|
+
`${path.join(plan.root, relative)} is neither tracked at HEAD ${provenance.head} nor an output Nx recorded for ${provenance.tasks.map(({ id }) => id).join(', ')}`,
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
if (output.type !== 'file' || output.sha256 !== digest) {
|
|
656
|
+
throw new SnapshotRefusal(
|
|
657
|
+
`${path.join(plan.root, relative)} is not the bytes Nx recorded for it: the provider's outputs are stale, so rebuild them`,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** Git's object id for `bytes` as a blob, in the repository's hash (SHA-1 ids are 40 digits, SHA-256 64). */
|
|
663
|
+
function gitBlobId(bytes: Uint8Array, length: number): string {
|
|
664
|
+
return createHash(length === 64 ? 'sha256' : 'sha1')
|
|
665
|
+
.update(`blob ${bytes.byteLength}\0`)
|
|
666
|
+
.update(bytes)
|
|
667
|
+
.digest('hex');
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** One digest over a census: every entry's path, type, executable bit, and bytes or link text. */
|
|
671
|
+
function censusId(census: readonly CensusEntry[]): string {
|
|
672
|
+
return sha256(
|
|
673
|
+
census
|
|
674
|
+
.map((entry) =>
|
|
675
|
+
entry.type === 'file'
|
|
676
|
+
? `${entry.path}\0file\0${entry.executable ? 'x' : '-'}\0${entry.sha256}`
|
|
677
|
+
: `${entry.path}\0symlink\0${entry.linkText}`,
|
|
678
|
+
)
|
|
679
|
+
.sort(compare)
|
|
680
|
+
.join('\n'),
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** The snapshot's name: its census and the producer receipt that vouched for it, as one digest. */
|
|
685
|
+
function snapshotId(census: readonly CensusEntry[], provenance: SnapshotProvenance): string {
|
|
686
|
+
return sha256(
|
|
687
|
+
JSON.stringify([
|
|
688
|
+
censusId(census),
|
|
689
|
+
provenance.root,
|
|
690
|
+
provenance.head,
|
|
691
|
+
provenance.headTree,
|
|
692
|
+
[...provenance.fileInputs].sort(compare),
|
|
693
|
+
[...provenance.tasks].sort((left, right) => compare(left.id, right.id)).map(({ id, hash }) => [id, hash]),
|
|
694
|
+
]),
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* Whole or not at all. A snapshot already published under the same name was copied from the same bytes under the
|
|
700
|
+
* same receipt; it is bound only once its sealed files still verify.
|
|
701
|
+
*/
|
|
702
|
+
function publish(incoming: string, sealed: string, id: string): void {
|
|
703
|
+
try {
|
|
704
|
+
renameSync(incoming, sealed);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
if (!(error instanceof Error && 'code' in error && (error.code === 'ENOTEMPTY' || error.code === 'EEXIST')))
|
|
707
|
+
throw error;
|
|
708
|
+
if (verifySnapshot(sealed).id !== id) throw new SnapshotRefusal(`${sealed} exists and is not snapshot ${id}`);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** A sealed snapshot's receipt, or null when `directory` holds none. Its census is not verified here. */
|
|
713
|
+
export function readSealed(directory: string): SnapshotReceipt | null {
|
|
714
|
+
let input: unknown;
|
|
715
|
+
try {
|
|
716
|
+
input = JSON.parse(readFileSync(path.join(directory, SNAPSHOT_RECEIPT), 'utf8'));
|
|
717
|
+
} catch (error) {
|
|
718
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return null;
|
|
719
|
+
throw error;
|
|
720
|
+
}
|
|
721
|
+
const receipt = parseReceipt(input, directory);
|
|
722
|
+
if (receipt.id !== path.basename(directory)) {
|
|
723
|
+
throw new SnapshotRefusal(`${directory} holds the receipt of snapshot ${receipt.id}`);
|
|
724
|
+
}
|
|
725
|
+
return receipt;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** The sealed snapshot `target` lies in, found by its receipt above it, or null. */
|
|
729
|
+
export function sealedSnapshotOf(
|
|
730
|
+
target: string,
|
|
731
|
+
): { readonly directory: string; readonly receipt: SnapshotReceipt } | null {
|
|
732
|
+
for (let at = path.dirname(target); ; at = path.dirname(at)) {
|
|
733
|
+
if (lstatSync(path.join(at, SNAPSHOT_RECEIPT), { throwIfNoEntry: false }) !== undefined) {
|
|
734
|
+
const receipt = readSealed(at);
|
|
735
|
+
return receipt === null ? null : { directory: at, receipt };
|
|
736
|
+
}
|
|
737
|
+
if (path.dirname(at) === at) return null;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* The snapshot's receipt once every entry under `directory` matches its census exactly: the same paths, types,
|
|
743
|
+
* executable bits and bytes or link text, and nothing else. Anything else refuses, naming the entry.
|
|
744
|
+
*/
|
|
745
|
+
export function verifySnapshot(directory: string): SnapshotReceipt {
|
|
746
|
+
const receipt = readSealed(directory);
|
|
747
|
+
if (receipt === null) throw new SnapshotRefusal(`${directory} is not a sealed snapshot`);
|
|
748
|
+
if (snapshotId(receipt.census, receipt.provenance) !== receipt.id)
|
|
749
|
+
throw new SnapshotRefusal(`${directory} has a census and receipt that are not its name`);
|
|
750
|
+
const expected = new Map(receipt.census.map((entry) => [entry.path, entry]));
|
|
751
|
+
let seen = 0;
|
|
752
|
+
const walk = (at: string) => {
|
|
753
|
+
for (const entry of readdirSync(at, { withFileTypes: true })) {
|
|
754
|
+
const absolute = path.join(at, entry.name);
|
|
755
|
+
const relative = path.relative(directory, absolute);
|
|
756
|
+
if (relative === SNAPSHOT_RECEIPT) continue;
|
|
757
|
+
if (entry.isDirectory()) {
|
|
758
|
+
walk(absolute);
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
const census = expected.get(relative);
|
|
762
|
+
if (census === undefined) throw new SnapshotRefusal(`${absolute} is not in snapshot ${receipt.id}'s census`);
|
|
763
|
+
seen += 1;
|
|
764
|
+
if (census.type === 'symlink') {
|
|
765
|
+
if (!entry.isSymbolicLink() || readlinkSync(absolute) !== census.linkText) {
|
|
766
|
+
throw new SnapshotRefusal(`${absolute} is not the link snapshot ${receipt.id} recorded`);
|
|
767
|
+
}
|
|
768
|
+
} else if (
|
|
769
|
+
!entry.isFile() ||
|
|
770
|
+
((lstatSync(absolute).mode & 0o111) !== 0) !== census.executable ||
|
|
771
|
+
sha256(readFileSync(absolute)) !== census.sha256
|
|
772
|
+
) {
|
|
773
|
+
throw new SnapshotRefusal(`${absolute} is not the bytes snapshot ${receipt.id} recorded`);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
walk(directory);
|
|
778
|
+
if (seen !== expected.size)
|
|
779
|
+
throw new SnapshotRefusal(`snapshot ${receipt.id} at ${directory} lost entries of its census`);
|
|
780
|
+
return receipt;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/** Hand-validated: this runs before Typia or any package is installed. Malformed is never treated as absent. */
|
|
784
|
+
function parseReceipt(input: unknown, directory: string): SnapshotReceipt {
|
|
785
|
+
const fail = (what: string): never => {
|
|
786
|
+
throw new SnapshotRefusal(`${path.join(directory, SNAPSHOT_RECEIPT)}: ${what}`);
|
|
787
|
+
};
|
|
788
|
+
if (typeof input !== 'object' || input === null) return fail('not a snapshot receipt');
|
|
789
|
+
const field = (name: string): unknown => Reflect.get(input, name);
|
|
790
|
+
const text = (value: unknown, what: string): string =>
|
|
791
|
+
typeof value === 'string' ? value : fail(`${what} is not text`);
|
|
792
|
+
if (field('version') !== 1) return fail('unknown receipt version');
|
|
793
|
+
const provenance = field('provenance');
|
|
794
|
+
if (typeof provenance !== 'object' || provenance === null) return fail('no provenance');
|
|
795
|
+
const tasks = Reflect.get(provenance, 'tasks');
|
|
796
|
+
const census = field('census');
|
|
797
|
+
if (!Array.isArray(tasks) || !Array.isArray(census)) return fail('no tasks or census');
|
|
798
|
+
return {
|
|
799
|
+
version: 1,
|
|
800
|
+
id: text(field('id'), 'id'),
|
|
801
|
+
name: text(field('name'), 'name'),
|
|
802
|
+
packagePath: text(field('packagePath'), 'packagePath'),
|
|
803
|
+
provenance: {
|
|
804
|
+
root: text(Reflect.get(provenance, 'root'), 'provenance.root'),
|
|
805
|
+
head: text(Reflect.get(provenance, 'head'), 'provenance.head'),
|
|
806
|
+
headTree: text(Reflect.get(provenance, 'headTree'), 'provenance.headTree'),
|
|
807
|
+
fileInputs: fileInputsOf(Reflect.get(provenance, 'fileInputs'), fail),
|
|
808
|
+
tasks: tasks.map((task: unknown) =>
|
|
809
|
+
typeof task === 'object' && task !== null
|
|
810
|
+
? { id: text(Reflect.get(task, 'id'), 'task id'), hash: text(Reflect.get(task, 'hash'), 'task hash') }
|
|
811
|
+
: fail('a task is not an object'),
|
|
812
|
+
),
|
|
813
|
+
},
|
|
814
|
+
census: census.map((entry: unknown): CensusEntry => {
|
|
815
|
+
if (typeof entry !== 'object' || entry === null) return fail('a census entry is not an object');
|
|
816
|
+
const entryPath = text(Reflect.get(entry, 'path'), 'census path');
|
|
817
|
+
const type = Reflect.get(entry, 'type');
|
|
818
|
+
if (type === 'symlink')
|
|
819
|
+
return { path: entryPath, type, linkText: text(Reflect.get(entry, 'linkText'), 'linkText') };
|
|
820
|
+
if (type !== 'file') return fail(`${entryPath} has an unknown type`);
|
|
821
|
+
const executable = Reflect.get(entry, 'executable');
|
|
822
|
+
if (typeof executable !== 'boolean') return fail(`${entryPath} has no executable bit`);
|
|
823
|
+
return { path: entryPath, type, executable, sha256: text(Reflect.get(entry, 'sha256'), 'sha256') };
|
|
824
|
+
}),
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function contained(relative: string): boolean {
|
|
829
|
+
return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function compare(left: string, right: string): number {
|
|
833
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
function sha256(input: string | Uint8Array): string {
|
|
837
|
+
return createHash('sha256').update(input).digest('hex');
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
function fileInputsOf(value: unknown, fail: (what: string) => never): string[] {
|
|
841
|
+
if (!Array.isArray(value) || !value.every((item): item is string => typeof item === 'string')) {
|
|
842
|
+
return fail('provenance.fileInputs is not a list of text');
|
|
843
|
+
}
|
|
844
|
+
return value;
|
|
845
|
+
}
|