@intentius/chant 0.124.85 → 0.124.87
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/dist/bundle-check.d.ts +102 -0
- package/dist/bundle-check.d.ts.map +1 -0
- package/dist/bundle-check.js +310 -0
- package/dist/chant-install.d.ts +100 -0
- package/dist/chant-install.d.ts.map +1 -0
- package/dist/chant-install.js +158 -0
- package/dist/ci-commit.d.ts +19 -6
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +23 -4
- package/dist/ci-evidence-jobs.d.ts +78 -0
- package/dist/ci-evidence-jobs.d.ts.map +1 -0
- package/dist/ci-evidence-jobs.js +85 -0
- package/dist/ci-image.d.ts +274 -0
- package/dist/ci-image.d.ts.map +1 -0
- package/dist/ci-image.js +635 -0
- package/dist/ci-login.d.ts +150 -0
- package/dist/ci-login.d.ts.map +1 -0
- package/dist/ci-login.js +240 -0
- package/dist/ci-product.d.ts +16 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +17 -0
- package/dist/cli/commands/import-index.js +2 -2
- package/dist/cli/handlers/build.d.ts +8 -0
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +22 -4
- package/dist/cli/handlers/ci-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-check.js +61 -2
- package/dist/cli/handlers/ci-evidence.d.ts +3 -0
- package/dist/cli/handlers/ci-evidence.d.ts.map +1 -0
- package/dist/cli/handlers/ci-evidence.js +174 -0
- package/dist/cli/handlers/components.js +3 -3
- package/dist/cli/handlers/dead-guards.d.ts +3 -0
- package/dist/cli/handlers/dead-guards.d.ts.map +1 -0
- package/dist/cli/handlers/dead-guards.js +35 -0
- package/dist/cli/handlers/graph.js +4 -4
- package/dist/cli/handlers/lifecycle.d.ts +2 -1
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +5 -4
- package/dist/cli/handlers/local-chant.d.ts +16 -0
- package/dist/cli/handlers/local-chant.d.ts.map +1 -0
- package/dist/cli/handlers/local-chant.js +109 -0
- package/dist/cli/handlers/operator.js +2 -2
- package/dist/cli/handlers/search.js +5 -5
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +38 -0
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +28 -0
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/check-hooks.d.ts +49 -0
- package/dist/components/check-hooks.d.ts.map +1 -1
- package/dist/components/check-hooks.js +64 -4
- package/dist/components/check-steps.d.ts +17 -0
- package/dist/components/check-steps.d.ts.map +1 -1
- package/dist/components/check-steps.js +37 -5
- package/dist/components/product-pipeline.d.ts +23 -0
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +6 -2
- package/dist/components/product-plan.d.ts +143 -0
- package/dist/components/product-plan.d.ts.map +1 -0
- package/dist/components/product-plan.js +145 -0
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +2 -2
- package/dist/config.d.ts +39 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -3
- package/dist/dead-guards.d.ts +82 -0
- package/dist/dead-guards.d.ts.map +1 -0
- package/dist/dead-guards.js +436 -0
- package/dist/discovery/files.d.ts +3 -1
- package/dist/discovery/files.d.ts.map +1 -1
- package/dist/discovery/files.js +5 -3
- package/dist/drift-issue.d.ts +152 -0
- package/dist/drift-issue.d.ts.map +1 -0
- package/dist/drift-issue.js +167 -0
- package/dist/floor-table.d.ts +30 -0
- package/dist/floor-table.d.ts.map +1 -0
- package/dist/floor-table.data.d.ts +3 -0
- package/dist/floor-table.data.d.ts.map +1 -0
- package/dist/floor-table.data.js +179 -0
- package/dist/floor-table.js +56 -0
- package/dist/forge-flags.d.ts +90 -0
- package/dist/forge-flags.d.ts.map +1 -0
- package/dist/forge-flags.js +184 -0
- package/dist/lexicon.d.ts +69 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.d.ts +7 -1
- package/dist/lifecycle/gated-op.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.js +7 -4
- package/dist/local-chant.d.ts +98 -0
- package/dist/local-chant.d.ts.map +1 -0
- package/dist/local-chant.js +307 -0
- package/dist/op/activities/predict-behaviour.js +2 -2
- package/dist/op/op-waves-evidence-spec.d.ts +13 -0
- package/dist/op/op-waves-evidence-spec.d.ts.map +1 -0
- package/dist/op/op-waves-evidence-spec.js +21 -0
- package/dist/op/op-waves-evidence.d.ts +40 -0
- package/dist/op/op-waves-evidence.d.ts.map +1 -0
- package/dist/op/op-waves-evidence.js +58 -0
- package/dist/op/op-waves-run.d.ts +4 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +24 -1
- package/dist/op/op-waves.d.ts +17 -1
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -0
- package/dist/op/schedule-overdue.d.ts +41 -9
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +154 -25
- package/dist/plan-summary.d.ts +25 -0
- package/dist/plan-summary.d.ts.map +1 -1
- package/dist/plan-summary.js +31 -0
- package/dist/pr-plan-note.d.ts +35 -0
- package/dist/pr-plan-note.d.ts.map +1 -1
- package/dist/pr-plan-note.js +10 -0
- package/dist/pr-post.d.ts +23 -0
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +125 -3
- package/dist/release-floors.d.ts +33 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +33 -1
- package/dist/state-migration.d.ts +24 -0
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +100 -5
- package/dist/wave-runtime.mjs +309 -302
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +4 -3
- package/dist/workspace/lexicon-outputs.d.ts.map +1 -1
- package/dist/workspace/lexicon-outputs.js +21 -7
- package/dist/workspace/reason-codes.d.ts +3 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/reason-codes.js +4 -0
- package/dist/workspace/ref-gate.schema.json +85 -0
- package/dist/workspace/search-contract.d.ts +1 -1
- package/dist/workspace/search-contract.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence-report.d.ts +58 -0
- package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence-report.js +109 -0
- package/dist/workspace/trust/check-evidence.d.ts +160 -0
- package/dist/workspace/trust/check-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence.js +309 -0
- package/dist/workspace/trust/ci-tag-statement.d.ts +2 -2
- package/dist/workspace/trust/ci-tag-statement.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.d.ts +40 -2
- package/dist/workspace/trust/dsse.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.js +34 -2
- package/dist/workspace/trust/evidence.d.ts +2 -2
- package/dist/workspace/trust/evidence.d.ts.map +1 -1
- package/dist/workspace/trust/keyless-fixture.d.ts +41 -0
- package/dist/workspace/trust/keyless-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/keyless-fixture.js +161 -0
- package/dist/workspace/trust/keyless.d.ts +202 -0
- package/dist/workspace/trust/keyless.d.ts.map +1 -0
- package/dist/workspace/trust/keyless.js +488 -0
- package/dist/workspace/trust/local-evidence.d.ts +123 -0
- package/dist/workspace/trust/local-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/local-evidence.js +259 -0
- package/dist/workspace/trust/op-run-statement.d.ts +2 -2
- package/dist/workspace/trust/op-run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/policy.d.ts +140 -1
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +197 -3
- package/dist/workspace/trust/ref-gate-cli.d.ts +33 -0
- package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-cli.js +234 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts +38 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-fixture.js +97 -0
- package/dist/workspace/trust/ref-gate.d.ts +187 -0
- package/dist/workspace/trust/ref-gate.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate.js +432 -0
- package/dist/workspace/trust/run-statement.d.ts +3 -3
- package/dist/workspace/trust/run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/runner-identity.d.ts +56 -0
- package/dist/workspace/trust/runner-identity.d.ts.map +1 -0
- package/dist/workspace/trust/runner-identity.js +65 -0
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +25 -1
- package/package.json +1 -1
- package/src/bundle-check.test.ts +140 -0
- package/src/bundle-check.ts +352 -0
- package/src/chant-install.test.ts +112 -0
- package/src/chant-install.ts +179 -0
- package/src/ci-commit.test.ts +45 -0
- package/src/ci-commit.ts +38 -4
- package/src/ci-evidence-jobs.ts +105 -0
- package/src/ci-image.test.ts +506 -0
- package/src/ci-image.ts +822 -0
- package/src/ci-login.test.ts +235 -0
- package/src/ci-login.ts +307 -0
- package/src/ci-product.ts +18 -0
- package/src/cli/commands/__fixtures__/source-dir-include/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-root/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.test.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/app.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/infra/net.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/import-index.ts +2 -2
- package/src/cli/handlers/build-source-dir.test.ts +64 -0
- package/src/cli/handlers/build.ts +21 -4
- package/src/cli/handlers/ci-check.ts +60 -3
- package/src/cli/handlers/ci-evidence.ts +167 -0
- package/src/cli/handlers/components.ts +3 -3
- package/src/cli/handlers/dead-guards.ts +37 -0
- package/src/cli/handlers/graph.ts +4 -4
- package/src/cli/handlers/lifecycle.ts +5 -4
- package/src/cli/handlers/local-chant.ts +106 -0
- package/src/cli/handlers/operator.ts +2 -2
- package/src/cli/handlers/search.ts +5 -5
- package/src/cli/main.test.ts +7 -0
- package/src/cli/main.ts +38 -0
- package/src/cli/parse-args.ts +21 -0
- package/src/cli/registry.ts +16 -0
- package/src/components/check-evidence-stage.test.ts +80 -0
- package/src/components/check-hooks.ts +84 -4
- package/src/components/check-local-evidence-stage.test.ts +141 -0
- package/src/components/check-steps.ts +56 -6
- package/src/components/product-pipeline.ts +28 -3
- package/src/components/product-plan.test.ts +87 -0
- package/src/components/product-plan.ts +230 -0
- package/src/components/release-compile.ts +2 -2
- package/src/config.ts +44 -3
- package/src/dead-guards.test.ts +206 -0
- package/src/dead-guards.ts +446 -0
- package/src/discovery/files.ts +5 -3
- package/src/drift-issue-bundle.test.ts +31 -0
- package/src/drift-issue.test.ts +206 -0
- package/src/drift-issue.ts +274 -0
- package/src/fit-note.test.ts +44 -0
- package/src/floor-table.data.ts +181 -0
- package/src/floor-table.ts +77 -0
- package/src/forge-flags.test.ts +110 -0
- package/src/forge-flags.ts +208 -0
- package/src/gate-decision-bundle.test.ts +1 -0
- package/src/lexicon.ts +68 -0
- package/src/lifecycle/gated-op.ts +10 -5
- package/src/local-chant.e2e.test.ts +94 -0
- package/src/local-chant.test.ts +162 -0
- package/src/local-chant.ts +341 -0
- package/src/op/activities/predict-behaviour.ts +2 -2
- package/src/op/op-waves-evidence-spec.ts +24 -0
- package/src/op/op-waves-evidence.test.ts +130 -0
- package/src/op/op-waves-evidence.ts +58 -0
- package/src/op/op-waves-run.ts +25 -1
- package/src/op/op-waves.ts +31 -1
- package/src/op/schedule-overdue.test.ts +85 -0
- package/src/op/schedule-overdue.ts +162 -26
- package/src/plan-summary.ts +40 -0
- package/src/pr-plan-note.ts +34 -0
- package/src/pr-post-plan-note.test.ts +146 -0
- package/src/pr-post.ts +127 -3
- package/src/release-floors.ts +47 -1
- package/src/stage-hooks-bundle.test.ts +1 -0
- package/src/state-migration.test.ts +52 -1
- package/src/state-migration.ts +107 -5
- package/src/workspace/ci-green.ts +4 -3
- package/src/workspace/lexicon-outputs.ts +23 -6
- package/src/workspace/reason-codes.test.ts +3 -0
- package/src/workspace/reason-codes.ts +4 -0
- package/src/workspace/ref-gate.schema.json +85 -0
- package/src/workspace/trust/check-evidence-report.ts +115 -0
- package/src/workspace/trust/check-evidence.test.ts +133 -0
- package/src/workspace/trust/check-evidence.ts +327 -0
- package/src/workspace/trust/ci-tag-statement.ts +2 -2
- package/src/workspace/trust/dsse.ts +48 -4
- package/src/workspace/trust/evidence.ts +2 -2
- package/src/workspace/trust/keyless-fixture.ts +214 -0
- package/src/workspace/trust/keyless.test.ts +220 -0
- package/src/workspace/trust/keyless.ts +557 -0
- package/src/workspace/trust/local-evidence.test.ts +127 -0
- package/src/workspace/trust/local-evidence.ts +268 -0
- package/src/workspace/trust/op-run-statement.ts +2 -2
- package/src/workspace/trust/policy.ts +263 -3
- package/src/workspace/trust/ref-gate-cli.ts +244 -0
- package/src/workspace/trust/ref-gate-fixture.ts +125 -0
- package/src/workspace/trust/ref-gate.e2e.test.ts +76 -0
- package/src/workspace/trust/ref-gate.test.ts +247 -0
- package/src/workspace/trust/ref-gate.ts +539 -0
- package/src/workspace/trust/run-statement.ts +3 -3
- package/src/workspace/trust/runner-identity.ts +92 -0
- package/src/workspace/trust/trust-files.test.ts +22 -0
- package/src/workspace/trust/trust-files.ts +54 -1
|
@@ -175,7 +175,7 @@ export function trustConfigProblems(raw) {
|
|
|
175
175
|
if (!ok)
|
|
176
176
|
problems.push(`${field}: ${message}`);
|
|
177
177
|
};
|
|
178
|
-
check("/", only(raw, ["schema", "signers", "roles", "adopted", "runners", "passkeys", "admitted"]), "unknown key");
|
|
178
|
+
check("/", only(raw, ["schema", "signers", "roles", "adopted", "runners", "keyless", "passkeys", "admitted", "refs", "local"]), "unknown key");
|
|
179
179
|
check("schema", raw.schema === 1, "expected 1");
|
|
180
180
|
check("signers", optional(raw.signers, isRepoPath), "a path inside the repository, with / separators and no ..");
|
|
181
181
|
check("roles", optional(raw.roles, (r) => isObject(r) && Object.entries(r).every(([k, v]) => /^[a-z][a-z0-9-]*$/.test(k) && Array.isArray(v) && v.every(nonEmpty))), "role names to lists of principals");
|
|
@@ -183,6 +183,13 @@ export function trustConfigProblems(raw) {
|
|
|
183
183
|
a.every((e) => isObject(e) && only(e, ["from", "to", "note"]) && optional(e.from, (f) => typeof f === "string" && FULL_COMMIT.test(f)) && typeof e.to === "string" && FULL_COMMIT.test(e.to) && optional(e.note, (n) => typeof n === "string"))), "commit ranges of full commit ids");
|
|
184
184
|
check("runners", optional(raw.runners, (a) => Array.isArray(a) &&
|
|
185
185
|
a.every((e) => isObject(e) && only(e, ["principal", "class", "key"]) && nonEmpty(e.principal) && (e.class === "runner" || e.class === "service") && typeof e.key === "string" && SSH_ED25519.test(e.key))), "runner keys: a principal, a class and an ssh-ed25519 key");
|
|
186
|
+
const httpUrl = (v) => typeof v === "string" && /^https?:\/\/[^\s]+$/.test(v);
|
|
187
|
+
check("keyless", optional(raw.keyless, (k) => isObject(k) && only(k, ["trustedRoot", "identities", "since", "fulcio", "timestamp", "audience"]) &&
|
|
188
|
+
isRepoPath(k.trustedRoot) &&
|
|
189
|
+
optional(k.since, (f) => typeof f === "string" && FULL_COMMIT.test(f)) &&
|
|
190
|
+
optional(k.fulcio, httpUrl) && optional(k.timestamp, httpUrl) && optional(k.audience, nonEmpty) &&
|
|
191
|
+
Array.isArray(k.identities) && k.identities.length > 0 &&
|
|
192
|
+
k.identities.every((e) => isObject(e) && only(e, ["principal", "class", "issuer", "subject"]) && nonEmpty(e.principal) && (e.class === "runner" || e.class === "service") && httpUrl(e.issuer) && nonEmpty(e.subject))), "keyless identities (#4869): a trusted root path and identities, each a principal, a class, an issuer URL and a subject pattern");
|
|
186
193
|
const b64url = (v) => typeof v === "string" && /^[A-Za-z0-9_-]+$/.test(v);
|
|
187
194
|
check("passkeys", optional(raw.passkeys, (a) => Array.isArray(a) &&
|
|
188
195
|
a.every((e) => isObject(e) && only(e, ["principal", "id", "publicKey", "rpId", "origins"]) &&
|
|
@@ -194,5 +201,22 @@ export function trustConfigProblems(raw) {
|
|
|
194
201
|
optional(e.note, (n) => typeof n === "string") &&
|
|
195
202
|
Array.isArray(e.signers) && e.signers.length > 0 &&
|
|
196
203
|
e.signers.every((s) => isObject(s) && only(s, ["principal", "key"]) && nonEmpty(s.principal) && typeof s.key === "string" && SSH_KEY.test(s.key)))), "admitted signers, by return id");
|
|
204
|
+
check("refs", optional(raw.refs, (a) => Array.isArray(a) &&
|
|
205
|
+
a.every((e) => isObject(e) && only(e, ["ref", "checks", "status", "overrides"]) &&
|
|
206
|
+
typeof e.ref === "string" && /^refs\/[^\s\\]+$/.test(e.ref) &&
|
|
207
|
+
optional(e.status, nonEmpty) && optional(e.overrides, (o) => o === "ledger" || o === "sealed") &&
|
|
208
|
+
Array.isArray(e.checks) && e.checks.length > 0 &&
|
|
209
|
+
e.checks.every((c) => isObject(c) && only(c, ["check", "action", "command"]) &&
|
|
210
|
+
typeof c.check === "string" && /^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(c.check) &&
|
|
211
|
+
optional(c.action, (x) => x === "block" || x === "warn") && nonEmpty(c.command)))), "ref rules: a ref pattern and the checks it requires, each with a command");
|
|
212
|
+
// #4868: keep in step with LOCAL_EVIDENCE_NEVER in ./policy.ts.
|
|
213
|
+
const never = ["plan", "policy", "apply", "drift", "release", "gate", "approval", "confirm"];
|
|
214
|
+
check("local", optional(raw.local, (l) => isObject(l) && only(l, ["keys", "checks"]) &&
|
|
215
|
+
optional(l.keys, (a) => Array.isArray(a) && a.every((e) => isObject(e) && only(e, ["principal", "class", "key"]) && nonEmpty(e.principal) && (e.class === "person" || e.class === "agent") && typeof e.key === "string" && SSH_ED25519.test(e.key))) &&
|
|
216
|
+
optional(l.checks, (a) => Array.isArray(a) &&
|
|
217
|
+
a.every((c) => isObject(c) && only(c, ["check", "from"]) &&
|
|
218
|
+
typeof c.check === "string" && /^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(c.check) &&
|
|
219
|
+
!never.some((n) => c.check === n || c.check.startsWith(`${n}:`)) &&
|
|
220
|
+
optional(c.from, (f) => Array.isArray(f) && f.length > 0 && f.every((x) => x === "person" || x === "agent"))))), "local evidence: person or agent keys, and the checks it stands in for (never plan, policy, apply, drift, release, gate, approval, confirm)");
|
|
197
221
|
return problems;
|
|
198
222
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { bundleImports, checkBundle, chantExternalPlugin, chantRuntimeTarget, type BundleCheckConfig } from "./bundle-check";
|
|
6
|
+
|
|
7
|
+
/** #4787: a product checked by chant's bundle check instead of its own scripts/bundle-check.mjs. */
|
|
8
|
+
|
|
9
|
+
const write = (file: string, body: string | object) => {
|
|
10
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
11
|
+
writeFileSync(file, typeof body === "string" ? body : JSON.stringify(body, null, 2));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** A product with one workspace package, `packages/app`, and an installed chant 1.2.3. */
|
|
15
|
+
function product(opts: { bundle?: string; deps?: Record<string, string>; installed?: object; config?: Partial<BundleCheckConfig> } = {}) {
|
|
16
|
+
const root = mkdtempSync(join(tmpdir(), "chant-4787-bundle-"));
|
|
17
|
+
write(join(root, "package.json"), { name: "product", private: true, workspaces: ["packages/*"], devDependencies: { "@intentius/chant": "1.2.3" } });
|
|
18
|
+
const chant = join(root, "node_modules/@intentius/chant");
|
|
19
|
+
write(join(chant, "package.json"), {
|
|
20
|
+
name: "@intentius/chant",
|
|
21
|
+
version: "1.2.3",
|
|
22
|
+
exports: {
|
|
23
|
+
".": { types: "./dist/index.d.ts", import: "./dist/index.js", default: "./src/index.ts" },
|
|
24
|
+
"./ts-only": { import: "./src/ts-only.ts" },
|
|
25
|
+
"./*": { import: "./dist/*.js", default: "./src/*.ts" },
|
|
26
|
+
},
|
|
27
|
+
...opts.installed,
|
|
28
|
+
});
|
|
29
|
+
write(join(chant, "dist/index.js"), "export {};\n");
|
|
30
|
+
write(join(chant, "dist/notify.js"), "export {};\n");
|
|
31
|
+
write(join(chant, "src/ts-only.ts"), "export {};\n");
|
|
32
|
+
const app = join(root, "packages/app");
|
|
33
|
+
write(join(app, "package.json"), {
|
|
34
|
+
name: "@example/app",
|
|
35
|
+
version: "0.1.0",
|
|
36
|
+
dependencies: opts.deps ?? { "@intentius/chant": "1.2.3" },
|
|
37
|
+
chantBundleCheck: { bundle: "dist/app.mjs", ...opts.config },
|
|
38
|
+
});
|
|
39
|
+
write(join(app, "dist/app.mjs"), opts.bundle ?? 'import { readFileSync } from "node:fs";\nimport { a } from "@intentius/chant";\nconst n = await import("@intentius/chant/notify");\nimport("./local.mjs");\n');
|
|
40
|
+
return { root, app, config: { bundle: "dist/app.mjs", ...opts.config } as BundleCheckConfig };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe("chantRuntimeTarget (#4787)", () => {
|
|
44
|
+
it("names the compiled file for a subpath the installed chant serves as JS", () => {
|
|
45
|
+
const { app, root } = product();
|
|
46
|
+
expect(chantRuntimeTarget("@intentius/chant", app)).toBe(join(root, "node_modules/@intentius/chant/dist/index.js"));
|
|
47
|
+
expect(chantRuntimeTarget("@intentius/chant/notify", app)).toBe(join(root, "node_modules/@intentius/chant/dist/notify.js"));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("answers undefined for TypeScript, a missing file, another package, or no install", () => {
|
|
51
|
+
const { app } = product();
|
|
52
|
+
expect(chantRuntimeTarget("@intentius/chant/ts-only", app)).toBeUndefined();
|
|
53
|
+
expect(chantRuntimeTarget("@intentius/chant/absent", app)).toBeUndefined();
|
|
54
|
+
expect(chantRuntimeTarget("left-pad", app)).toBeUndefined();
|
|
55
|
+
expect(chantRuntimeTarget("@intentius/chant-lexicon-k8s", app)).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("the esbuild plugin leaves only compiled chant imports external", () => {
|
|
59
|
+
const { app } = product();
|
|
60
|
+
const external = new Set<string>();
|
|
61
|
+
let cb: ((a: { path: string }) => unknown) | undefined;
|
|
62
|
+
chantExternalPlugin(app, external).setup({ onResolve: (_o, f) => { cb = f; } });
|
|
63
|
+
expect(cb!({ path: "@intentius/chant/notify" })).toEqual({ path: "@intentius/chant/notify", external: true });
|
|
64
|
+
expect(cb!({ path: "@intentius/chant/ts-only" })).toBeUndefined();
|
|
65
|
+
expect([...external]).toEqual(["@intentius/chant/notify"]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("bundleImports (#4787)", () => {
|
|
70
|
+
it("reads static, dynamic and require imports and skips relative ones and junk strings", () => {
|
|
71
|
+
const text = 'import a from "x";export{b}from"y/z";import"side";const c=import("@s/p/q");require("node:fs");import("./rel.mjs");x="import \'not a spec\'"';
|
|
72
|
+
expect(bundleImports(text).sort()).toEqual(["@s/p/q", "node:fs", "side", "x", "y/z"]);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("checkBundle (#4787)", () => {
|
|
77
|
+
it("passes a bundle that imports Node and chant served as compiled JS, on the root's pin", () => {
|
|
78
|
+
const { app, config } = product();
|
|
79
|
+
const r = checkBundle(app, config);
|
|
80
|
+
expect(r.problems).toEqual([]);
|
|
81
|
+
expect(r.chantImports.sort()).toEqual(["@intentius/chant", "@intentius/chant/notify"]);
|
|
82
|
+
expect(r.summary).toContain("chant 1.2.3");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("fails a chant import served only as TypeScript and an import of a package that is not a dependency", () => {
|
|
86
|
+
const { app, config } = product({ bundle: 'import "@intentius/chant/ts-only";\nimport "left-pad";\n' });
|
|
87
|
+
const r = checkBundle(app, config);
|
|
88
|
+
expect(r.ok).toBe(false);
|
|
89
|
+
expect(r.problems.join("\n")).toContain("'@intentius/chant/ts-only', which the installed package's exports do not serve as compiled JS");
|
|
90
|
+
expect(r.problems.join("\n")).toContain("'left-pad', which is neither a Node module nor a runtime dependency");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("lets an optional package through", () => {
|
|
94
|
+
const { app, config } = product({ bundle: 'import "@intentius/chant";\nimport("@cdktn/hcl2json");\n', config: { optional: ["@cdktn/hcl2json"] } });
|
|
95
|
+
expect(checkBundle(app, config).problems).toEqual([]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("fails a dependency nothing loads, and takes one that needs names", () => {
|
|
99
|
+
const deps = { "@intentius/chant": "1.2.3", "@intentius/chant-lexicon-k8s": "1.2.3" };
|
|
100
|
+
const unused = product({ deps });
|
|
101
|
+
expect(checkBundle(unused.app, unused.config).problems).toContain("@intentius/chant-lexicon-k8s is a runtime dependency that the bundle does not import and chantBundleCheck.needs does not name");
|
|
102
|
+
const needed = product({ deps, config: { needs: ["@intentius/chant-lexicon-k8s", "@intentius/chant-lexicon-gitlab"] } });
|
|
103
|
+
const problems = checkBundle(needed.app, needed.config).problems;
|
|
104
|
+
expect(problems).toContain("@intentius/chant-lexicon-k8s is not installed; run npm ci");
|
|
105
|
+
expect(problems).toContain("@intentius/chant-lexicon-gitlab is loaded at run time (chantBundleCheck.needs names it) but is not in dependencies");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("fails a chant dependency off the root's pin", () => {
|
|
109
|
+
const { app, config } = product({ deps: { "@intentius/chant": "^1.2.0" } });
|
|
110
|
+
expect(checkBundle(app, config).problems).toContain("@intentius/chant is '^1.2.0' in dependencies, not the root's chant pin 1.2.3");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("fails an installed chant off the pin unless chant dev local marked it", () => {
|
|
114
|
+
const off = product({ installed: { version: "1.3.0" } });
|
|
115
|
+
expect(checkBundle(off.app, off.config).problems).toContain("the installed @intentius/chant is 1.3.0, not the pin 1.2.3; run npm ci");
|
|
116
|
+
const local = product({ installed: { version: "1.3.0", chantLocal: { commit: "abcdef0123456789", dir: "/x/chant" } } });
|
|
117
|
+
const r = checkBundle(local.app, local.config);
|
|
118
|
+
expect(r.problems).toEqual([]);
|
|
119
|
+
expect(r.chant).toBe("local chant 1.3.0 (abcdef012345)");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("fails a bundle over its ceiling", () => {
|
|
123
|
+
const { app, config } = product({ config: { maxBytes: 10 } });
|
|
124
|
+
expect(checkBundle(app, config).problems.join("\n")).toMatch(/the bundle is \d+ KB, over the 0 KB ceiling/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("fails a denied metafile input, and a deny list without a metafile", () => {
|
|
128
|
+
const deny = [{ pattern: "^typescript/", what: "the TypeScript compiler" }];
|
|
129
|
+
const { app, config } = product({ config: { metafile: "meta.json", deny } });
|
|
130
|
+
write(join(app, "meta.json"), { inputs: { "src/a.ts": {}, "../../node_modules/typescript/lib/typescript.js": {} } });
|
|
131
|
+
expect(checkBundle(app, config).problems).toEqual(["the bundle includes the TypeScript compiler (1 inputs): typescript/lib/typescript.js"]);
|
|
132
|
+
const none = product({ config: { deny } });
|
|
133
|
+
expect(checkBundle(none.app, none.config).problems[0]).toContain("chantBundleCheck.deny needs the build's esbuild metafile");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("fails a missing bundle", () => {
|
|
137
|
+
const { app } = product();
|
|
138
|
+
expect(checkBundle(app, { bundle: "dist/none.mjs" }).problems).toContain("no bundle at dist/none.mjs; build it first");
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@intentius/chant/bundle-check` (#4787): how a product's bundle treats its
|
|
3
|
+
* chant imports, and the check that holds the bundle to that shape.
|
|
4
|
+
*
|
|
5
|
+
* A product (terragucci, plywood, sql-yodeler) bundles its own code into one
|
|
6
|
+
* file and imports chant from the installed, pinned package at run time,
|
|
7
|
+
* wherever that package serves the import as compiled JS under Node's
|
|
8
|
+
* `import` condition (#4591). Each product carried its own copy of this:
|
|
9
|
+
* `chantRuntimeTarget` and an esbuild plugin that leaves such imports
|
|
10
|
+
* external, and a bundle check that the bundle imports nothing else, that the
|
|
11
|
+
* package's runtime dependencies match the root's chant pin, and that the
|
|
12
|
+
* bundle stays under its size ceiling. They live here so a product only
|
|
13
|
+
* names its bundle, its ceiling and its denied inputs (`chantBundleCheck` in
|
|
14
|
+
* the package's package.json, read by `chant dev bundle-check`).
|
|
15
|
+
*
|
|
16
|
+
* This module imports only Node's own modules, so a product's build script
|
|
17
|
+
* can import it from plain Node.
|
|
18
|
+
*/
|
|
19
|
+
import { execFileSync } from "node:child_process";
|
|
20
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
21
|
+
import { builtinModules } from "node:module";
|
|
22
|
+
import { dirname, join, resolve } from "node:path";
|
|
23
|
+
|
|
24
|
+
/** Any `@intentius/chant` package, with or without a subpath: core and every lexicon. */
|
|
25
|
+
const CHANT = /^(@intentius\/chant(?:-[a-z0-9-]+)?)(\/.*)?$/;
|
|
26
|
+
const CONDITIONS = ["import", "node", "default"];
|
|
27
|
+
|
|
28
|
+
/** The target Node's ESM conditions pick from one exports value, in key order. */
|
|
29
|
+
function pick(value: unknown): string | undefined {
|
|
30
|
+
if (typeof value === "string") return value;
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
for (const v of value) {
|
|
33
|
+
const t = pick(v);
|
|
34
|
+
if (t !== undefined) return t;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (value && typeof value === "object") {
|
|
39
|
+
for (const [key, v] of Object.entries(value)) {
|
|
40
|
+
if (CONDITIONS.includes(key)) return pick(v);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The file `subpath` (`.` or `./x`) of the package at `pkgDir` names under Node's ESM conditions, or undefined. */
|
|
47
|
+
function exportsTarget(pkgDir: string, subpath: string): string | undefined {
|
|
48
|
+
const map = (JSON.parse(readFileSync(join(pkgDir, "package.json"), "utf8")) as { exports?: unknown }).exports;
|
|
49
|
+
if (typeof map === "string") return subpath === "." ? join(pkgDir, map) : undefined;
|
|
50
|
+
if (!map || typeof map !== "object" || Array.isArray(map)) return undefined;
|
|
51
|
+
const entries = map as Record<string, unknown>;
|
|
52
|
+
// A map of conditions alone is the root's entry.
|
|
53
|
+
if (!Object.keys(entries).some((k) => k.startsWith("."))) {
|
|
54
|
+
const t = subpath === "." ? pick(entries) : undefined;
|
|
55
|
+
return t === undefined ? undefined : join(pkgDir, t);
|
|
56
|
+
}
|
|
57
|
+
if (subpath in entries) {
|
|
58
|
+
const t = pick(entries[subpath]);
|
|
59
|
+
return t === undefined ? undefined : join(pkgDir, t);
|
|
60
|
+
}
|
|
61
|
+
// Node's pattern match: of the keys with one `*` that match, the longest prefix wins.
|
|
62
|
+
let best: { prefix: string; star: string; value: unknown } | undefined;
|
|
63
|
+
for (const key of Object.keys(entries)) {
|
|
64
|
+
const at = key.indexOf("*");
|
|
65
|
+
if (at < 0 || key.indexOf("*", at + 1) >= 0) continue;
|
|
66
|
+
const prefix = key.slice(0, at);
|
|
67
|
+
const suffix = key.slice(at + 1);
|
|
68
|
+
if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix) || subpath.length < key.length - 1) continue;
|
|
69
|
+
if (best && prefix.length <= best.prefix.length) continue;
|
|
70
|
+
best = { prefix, star: subpath.slice(prefix.length, subpath.length - suffix.length), value: entries[key] };
|
|
71
|
+
}
|
|
72
|
+
if (!best) return undefined;
|
|
73
|
+
const t = pick(best.value);
|
|
74
|
+
return t === undefined ? undefined : join(pkgDir, t.split("*").join(best.star));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The directory of `name` as Node finds it from `fromDir`, walking up the node_modules folders, or undefined. */
|
|
78
|
+
export function installedPackageDir(name: string, fromDir: string): string | undefined {
|
|
79
|
+
for (let dir = resolve(fromDir); ; dir = dirname(dir)) {
|
|
80
|
+
const pkgDir = join(dir, "node_modules", name);
|
|
81
|
+
if (existsSync(join(pkgDir, "package.json"))) return pkgDir;
|
|
82
|
+
if (dirname(dir) === dir) return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The compiled file Node would load for the chant `specifier` (core or a
|
|
88
|
+
* lexicon, with or without a subpath) imported from a file in `fromDir`, or
|
|
89
|
+
* undefined: the specifier is not chant's, the installed package does not
|
|
90
|
+
* export it, exports only TypeScript for it, or is not installed. Plain Node
|
|
91
|
+
* does not load TypeScript from node_modules, so only a compiled target can
|
|
92
|
+
* stay external.
|
|
93
|
+
*/
|
|
94
|
+
export function chantRuntimeTarget(specifier: string, fromDir: string): string | undefined {
|
|
95
|
+
const m = CHANT.exec(specifier);
|
|
96
|
+
if (!m) return undefined;
|
|
97
|
+
const pkgDir = installedPackageDir(m[1], fromDir);
|
|
98
|
+
if (pkgDir === undefined) return undefined;
|
|
99
|
+
const file = exportsTarget(pkgDir, m[2] ? `.${m[2]}` : ".");
|
|
100
|
+
return file !== undefined && /\.(?:m?js|cjs)$/.test(file) && existsSync(file) ? file : undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Whether the chant installed for the package at `pkgDir` serves its root as compiled JS (#4591 and later). */
|
|
104
|
+
export function chantServesCompiled(pkgDir: string): boolean {
|
|
105
|
+
return chantRuntimeTarget("@intentius/chant", pkgDir) !== undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The part of esbuild's plugin API {@link chantExternalPlugin} uses, so this module needs no esbuild types. */
|
|
109
|
+
export interface EsbuildPluginLike {
|
|
110
|
+
name: string;
|
|
111
|
+
setup(build: {
|
|
112
|
+
onResolve(options: { filter: RegExp }, callback: (args: { path: string }) => { path: string; external: boolean } | undefined): void;
|
|
113
|
+
}): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* An esbuild plugin that leaves an `@intentius/chant*` import external when
|
|
118
|
+
* the chant installed for `pkgDir` (where the bin runs) serves it as compiled
|
|
119
|
+
* JS, so the bundle runs the installed, pinned chant. Any other chant import
|
|
120
|
+
* goes on to the next plugin and esbuild, which bundle it. `external`, when
|
|
121
|
+
* given, collects the specifiers left external.
|
|
122
|
+
*/
|
|
123
|
+
export function chantExternalPlugin(pkgDir: string, external?: Set<string>): EsbuildPluginLike {
|
|
124
|
+
return {
|
|
125
|
+
name: "chant-external",
|
|
126
|
+
setup(b) {
|
|
127
|
+
b.onResolve({ filter: CHANT }, (args) => {
|
|
128
|
+
if (chantRuntimeTarget(args.path, pkgDir) === undefined) return undefined;
|
|
129
|
+
external?.add(args.path);
|
|
130
|
+
return { path: args.path, external: true };
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The bare specifiers a bundle imports: static `import` and `export ... from`,
|
|
138
|
+
* `import()` and `require()` of a string. Relative imports are left out, and
|
|
139
|
+
* so are strings in minified code that the pattern also matches but that hold
|
|
140
|
+
* spaces, commas, quotes or template braces.
|
|
141
|
+
*/
|
|
142
|
+
export function bundleImports(text: string): string[] {
|
|
143
|
+
const out = new Set<string>();
|
|
144
|
+
const re = /(?:^|[;\s}])(?:import|export)\s*(?:[\w*{}\s,$]+from\s*)?["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)|require\(\s*["']([^"']+)["']\s*\)/gm;
|
|
145
|
+
for (const m of text.matchAll(re)) {
|
|
146
|
+
const s = m[1] ?? m[2] ?? m[3];
|
|
147
|
+
if (s !== undefined && /^[\w@][\w@./:-]*$/.test(s)) out.add(s);
|
|
148
|
+
}
|
|
149
|
+
return [...out];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The package a bare specifier names: `@scope/name` or `name`. */
|
|
153
|
+
export function packageOf(specifier: string): string {
|
|
154
|
+
return specifier.split("/").slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** An input the bundle must not include: a pattern matched against each metafile input with everything up to its last `node_modules/` removed. */
|
|
158
|
+
export interface DeniedInput {
|
|
159
|
+
pattern: string;
|
|
160
|
+
/** What the input is, in the failure: "the TypeScript compiler". */
|
|
161
|
+
what: string;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A product's bundle check, as `chantBundleCheck` in its package's package.json (#4787). Paths are relative to the package. */
|
|
165
|
+
export interface BundleCheckConfig {
|
|
166
|
+
/** The bundle file, such as `dist/terragucci.mjs`. */
|
|
167
|
+
bundle: string;
|
|
168
|
+
/** The most bytes the bundle file may hold. */
|
|
169
|
+
maxBytes?: number;
|
|
170
|
+
/** The most bytes the package's `npm pack` tarball may hold (`npm pack --dry-run --ignore-scripts`). */
|
|
171
|
+
maxPackBytes?: number;
|
|
172
|
+
/**
|
|
173
|
+
* Packages the code loads at run time that no import in the bundle names,
|
|
174
|
+
* such as `@intentius/chant` for a bin it spawns, or a lexicon chant's
|
|
175
|
+
* generator loads by name. Each must be a runtime dependency.
|
|
176
|
+
*/
|
|
177
|
+
needs?: string[];
|
|
178
|
+
/** Packages the bundle may import that are not dependencies: optional ones it loads when present. */
|
|
179
|
+
optional?: string[];
|
|
180
|
+
/** esbuild's metafile for the bundle, to check its inputs against `deny`. */
|
|
181
|
+
metafile?: string;
|
|
182
|
+
deny?: DeniedInput[];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface BundleCheckResult {
|
|
186
|
+
ok: boolean;
|
|
187
|
+
problems: string[];
|
|
188
|
+
/** One line saying what was checked, for a passing check. */
|
|
189
|
+
summary: string;
|
|
190
|
+
bundleBytes: number;
|
|
191
|
+
packBytes?: number;
|
|
192
|
+
/** The chant imports the bundle leaves to the installed chant. */
|
|
193
|
+
chantImports: string[];
|
|
194
|
+
/** The installed chant's version, with the commit when it is a local build (`chantLocal`). */
|
|
195
|
+
chant: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface Manifest {
|
|
199
|
+
name?: string;
|
|
200
|
+
version?: string;
|
|
201
|
+
workspaces?: unknown;
|
|
202
|
+
dependencies?: Record<string, string>;
|
|
203
|
+
devDependencies?: Record<string, string>;
|
|
204
|
+
chantBundleCheck?: BundleCheckConfig;
|
|
205
|
+
chantLocal?: { commit?: string; dir?: string };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const readManifest = (file: string): Manifest => JSON.parse(readFileSync(file, "utf8")) as Manifest;
|
|
209
|
+
|
|
210
|
+
/** The nearest directory from `pkgDir` up whose package.json declares workspaces, else `pkgDir`: where the product pins chant. */
|
|
211
|
+
export function productRoot(pkgDir: string): string {
|
|
212
|
+
for (let dir = resolve(pkgDir); ; dir = dirname(dir)) {
|
|
213
|
+
const f = join(dir, "package.json");
|
|
214
|
+
if (existsSync(f) && readManifest(f).workspaces !== undefined) return dir;
|
|
215
|
+
if (dirname(dir) === dir) return resolve(pkgDir);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The `chantBundleCheck` field of the package at `pkgDir`, or undefined when it has none. */
|
|
220
|
+
export function readBundleCheckConfig(pkgDir: string): BundleCheckConfig | undefined {
|
|
221
|
+
const f = join(pkgDir, "package.json");
|
|
222
|
+
if (!existsSync(f)) return undefined;
|
|
223
|
+
return readManifest(f).chantBundleCheck;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The size of the tarball `npm pack` would write for the package at `pkgDir`, without running its scripts. */
|
|
227
|
+
function packSize(pkgDir: string): number {
|
|
228
|
+
const out = execFileSync("npm", ["pack", "--dry-run", "--json", "--ignore-scripts"], { cwd: pkgDir, stdio: ["ignore", "pipe", "pipe"] }).toString();
|
|
229
|
+
const [packed] = JSON.parse(out) as { size: number }[];
|
|
230
|
+
return packed.size;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Check the package at `pkgDir` against `config` (#4787):
|
|
235
|
+
*
|
|
236
|
+
* - the bundle imports only Node's modules, its runtime dependencies and the
|
|
237
|
+
* `optional` packages, and each chant import is one the installed chant
|
|
238
|
+
* serves as compiled JS ({@link chantRuntimeTarget});
|
|
239
|
+
* - the runtime dependencies are exactly the packages the bundle imports and
|
|
240
|
+
* the `needs`, and every `@intentius/chant*` among them and among the
|
|
241
|
+
* devDependencies is pinned to the root's `@intentius/chant`, an exact version;
|
|
242
|
+
* - each installed chant package is that version, or a local build marked
|
|
243
|
+
* `chantLocal` (`chant dev local`);
|
|
244
|
+
* - the bundle and the tarball stay within `maxBytes` and `maxPackBytes`;
|
|
245
|
+
* - no metafile input matches a `deny` pattern.
|
|
246
|
+
*/
|
|
247
|
+
export function checkBundle(pkgDir: string, config: BundleCheckConfig): BundleCheckResult {
|
|
248
|
+
const dir = resolve(pkgDir);
|
|
249
|
+
const problems: string[] = [];
|
|
250
|
+
const pkg = readManifest(join(dir, "package.json"));
|
|
251
|
+
const root = productRoot(dir);
|
|
252
|
+
const rootPkg = readManifest(join(root, "package.json"));
|
|
253
|
+
const pin = rootPkg.dependencies?.["@intentius/chant"] ?? rootPkg.devDependencies?.["@intentius/chant"];
|
|
254
|
+
if (pin === undefined) problems.push(`${join(root, "package.json")} does not name @intentius/chant`);
|
|
255
|
+
else if (!/^\d+\.\d+\.\d+(?:-[\w.]+)?$/.test(pin)) problems.push(`@intentius/chant is '${pin}' in ${join(root, "package.json")}, not an exact version`);
|
|
256
|
+
|
|
257
|
+
const bundleFile = join(dir, config.bundle);
|
|
258
|
+
let text = "";
|
|
259
|
+
let bundleBytes = 0;
|
|
260
|
+
if (!existsSync(bundleFile)) problems.push(`no bundle at ${config.bundle}; build it first`);
|
|
261
|
+
else {
|
|
262
|
+
text = readFileSync(bundleFile, "utf8");
|
|
263
|
+
bundleBytes = statSync(bundleFile).size;
|
|
264
|
+
if (config.maxBytes !== undefined && bundleBytes > config.maxBytes) {
|
|
265
|
+
problems.push(`the bundle is ${Math.round(bundleBytes / 1024)} KB, over the ${Math.round(config.maxBytes / 1024)} KB ceiling`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const deps = pkg.dependencies ?? {};
|
|
270
|
+
const optional = new Set(config.optional ?? []);
|
|
271
|
+
const node = new Set([...builtinModules, ...builtinModules.map((m) => `node:${m}`)]);
|
|
272
|
+
const used = new Map<string, string>();
|
|
273
|
+
for (const n of config.needs ?? []) used.set(n, "chantBundleCheck.needs names it");
|
|
274
|
+
const chantImports: string[] = [];
|
|
275
|
+
for (const s of bundleImports(text)) {
|
|
276
|
+
if (node.has(s) || optional.has(s) || optional.has(packageOf(s))) continue;
|
|
277
|
+
const name = packageOf(s);
|
|
278
|
+
if (!(name in deps)) {
|
|
279
|
+
problems.push(`the bundle imports '${s}', which is neither a Node module nor a runtime dependency`);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (!used.has(name)) used.set(name, `the bundle imports ${s}`);
|
|
283
|
+
if (CHANT.test(s)) {
|
|
284
|
+
if (chantRuntimeTarget(s, dir) === undefined) problems.push(`the bundle imports '${s}', which the installed package's exports do not serve as compiled JS under the import condition`);
|
|
285
|
+
else chantImports.push(s);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (const [name, why] of used) {
|
|
289
|
+
if (!(name in deps)) problems.push(`${name} is loaded at run time (${why}) but is not in dependencies${name in (pkg.devDependencies ?? {}) ? " (it is a devDependency, which npm install --omit=dev leaves out)" : ""}`);
|
|
290
|
+
}
|
|
291
|
+
for (const name of Object.keys(deps)) {
|
|
292
|
+
if (!used.has(name)) problems.push(`${name} is a runtime dependency that the bundle does not import and chantBundleCheck.needs does not name`);
|
|
293
|
+
}
|
|
294
|
+
if (pin !== undefined) {
|
|
295
|
+
for (const [kind, list] of [["dependencies", deps], ["devDependencies", pkg.devDependencies ?? {}]] as const) {
|
|
296
|
+
if (dir === root && kind === "devDependencies") continue;
|
|
297
|
+
for (const [name, v] of Object.entries(list)) {
|
|
298
|
+
if (CHANT.test(name) && v !== pin) problems.push(`${name} is '${v}' in ${kind}, not the root's chant pin ${pin}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// The installed copies: the pin, or a local build `chant dev local` marked.
|
|
304
|
+
let chant = "not installed";
|
|
305
|
+
const coreDir = installedPackageDir("@intentius/chant", dir);
|
|
306
|
+
if (coreDir === undefined) problems.push("@intentius/chant is not installed; run npm ci");
|
|
307
|
+
else {
|
|
308
|
+
const core = readManifest(join(coreDir, "package.json"));
|
|
309
|
+
chant = core.chantLocal ? `local chant ${core.version} (${String(core.chantLocal.commit).slice(0, 12)})` : `chant ${core.version}`;
|
|
310
|
+
}
|
|
311
|
+
for (const name of new Set([...Object.keys(deps), ...Object.keys(pkg.devDependencies ?? {})])) {
|
|
312
|
+
if (!CHANT.test(name) || pin === undefined) continue;
|
|
313
|
+
const at = installedPackageDir(name, dir);
|
|
314
|
+
if (at === undefined) {
|
|
315
|
+
if (name in deps) problems.push(`${name} is not installed; run npm ci`);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const installed = readManifest(join(at, "package.json"));
|
|
319
|
+
if (!installed.chantLocal && installed.version !== pin) problems.push(`the installed ${name} is ${installed.version}, not the pin ${pin}; run npm ci`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (config.deny?.length) {
|
|
323
|
+
const metafile = config.metafile === undefined ? undefined : join(dir, config.metafile);
|
|
324
|
+
if (metafile === undefined || !existsSync(metafile)) problems.push(`chantBundleCheck.deny needs the build's esbuild metafile${config.metafile ? ` at ${config.metafile}` : " (chantBundleCheck.metafile)"}`);
|
|
325
|
+
else {
|
|
326
|
+
const inputs = Object.keys((JSON.parse(readFileSync(metafile, "utf8")) as { inputs?: Record<string, unknown> }).inputs ?? {});
|
|
327
|
+
const rules = config.deny.map((d) => ({ re: new RegExp(d.pattern), what: d.what }));
|
|
328
|
+
const hits = new Map<string, string[]>();
|
|
329
|
+
for (const input of inputs) {
|
|
330
|
+
const rel = input.replace(/^.*node_modules\//, "");
|
|
331
|
+
for (const r of rules) if (r.re.test(rel)) hits.set(r.what, [...(hits.get(r.what) ?? []), rel]);
|
|
332
|
+
}
|
|
333
|
+
for (const [what, list] of hits) {
|
|
334
|
+
problems.push(`the bundle includes ${what} (${list.length} inputs): ${list.slice(0, 3).join(", ")}${list.length > 3 ? `, and ${list.length - 3} more` : ""}`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let packBytes: number | undefined;
|
|
340
|
+
if (config.maxPackBytes !== undefined) {
|
|
341
|
+
packBytes = packSize(dir);
|
|
342
|
+
if (packBytes > config.maxPackBytes) problems.push(`the tarball is ${Math.round(packBytes / 1024)} KB, over the ${Math.round(config.maxPackBytes / 1024)} KB ceiling`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const sizes = [
|
|
346
|
+
`bundle ${Math.round(bundleBytes / 1024)} KB${config.maxBytes !== undefined ? ` (ceiling ${Math.round(config.maxBytes / 1024)} KB)` : ""}`,
|
|
347
|
+
...(packBytes !== undefined ? [`tarball ${Math.round(packBytes / 1024)} KB (ceiling ${Math.round(config.maxPackBytes! / 1024)} KB)`] : []),
|
|
348
|
+
];
|
|
349
|
+
const summary = `${pkg.name ?? dir}: ${sizes.join(", ")}, ${Object.keys(deps).length} runtime dependencies at chant ${pin}, ${chantImports.length} chant imports served as compiled JS by ${chant}`;
|
|
350
|
+
return { ok: problems.length === 0, problems, summary, bundleBytes, packBytes, chantImports, chant };
|
|
351
|
+
}
|
|
352
|
+
|