@anizoptera/publish-clean 0.7.0 → 0.7.1

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.
Files changed (3) hide show
  1. package/README.md +21 -0
  2. package/dist/cli.js +58 -25
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -61,6 +61,27 @@ supply-chain risk of its own, and this one has no transitive code to audit.
61
61
  `--provenance` additionally needs npm 11.5.1+ and a cloud CI runner. npm will not sign a
62
62
  publish that came from your laptop.
63
63
 
64
+ ### Running it under Bun packs a smaller tarball
65
+
66
+ gzip encoding belongs to whichever runtime executes the CLI, and Bun's (libdeflate) beats
67
+ Node's zlib on tar-shaped data: 0.17% smaller for this package, 0.28% for a sibling one, with
68
+ a byte-identical archive inside. To get it, pass Bun the **file path**:
69
+
70
+ ```sh
71
+ bun ./node_modules/.bin/publish-clean
72
+ ```
73
+
74
+ Naming the bin instead does not work, and does not say so: `bun publish-clean`,
75
+ `bun run publish-clean` and `bunx publish-clean` all hand the file to the OS, which honours
76
+ its `#!/usr/bin/env node` shebang and runs Node. They succeed, and you get Node's bytes.
77
+
78
+ Not `bunx --bun`, which forces Bun onto the `pnpm` this tool spawns as well; pnpm 11 needs
79
+ `node:sqlite`, which Bun does not implement, and that is the error you get.
80
+
81
+ Either runtime produces valid gzip with identical contents, so the choice is free. The one
82
+ rule is not to switch runtimes _within_ a version: if a release is re-run to repair its
83
+ artifacts, the bytes have to come out the same, and the two encoders do not agree.
84
+
64
85
  ## Package managers
65
86
 
66
87
  Why pnpm packs and npm publishes: pnpm is the only packer that resolves every workspace
package/dist/cli.js CHANGED
@@ -45,9 +45,9 @@ function stableValue(value) {
45
45
  //#region src/artifact.ts
46
46
  /**
47
47
  * What the published tarball may carry, and what it must. Every rule here judges a file list —
48
- * the paths `tar` reads out of the archive, with the `package/` prefix already stripped — never
49
- * a directory on disk, because that list is what a consumer's installer unpacks and it needs no
50
- * filesystem to exist.
48
+ * the archive's entry paths with the `package/` prefix already stripped — never a directory on
49
+ * disk, because that list is what a consumer's installer unpacks and it needs no filesystem to
50
+ * exist.
51
51
  *
52
52
  * Ambient inputs arrive as parameters and never by reaching for them — no process, filesystem or
53
53
  * argv here. `cli.ts` owns the effects.
@@ -95,8 +95,10 @@ function validatePackedFiles(files, skipSuspicious) {
95
95
  * dropped file: the leak checks only ask what is present, and a package missing a file installs
96
96
  * fine and then fails at import, on a version the registry keeps forever.
97
97
  *
98
- * Both lists are read out of the archives by `tar`, an instrument that cannot share a mistake
99
- * with the walk being checked.
98
+ * The two lists come from the same reader, so this cannot catch a defect in the reader itself —
99
+ * only one in the rewrite, which is what it is for. Independence from the reader is bought
100
+ * elsewhere, by the case in test/cli.test.ts that compares the list this reader prints against
101
+ * `tar tzf` of the same tarball — an instrument that cannot share a mistake with this one.
100
102
  */
101
103
  function assertSameEntries(packed, published) {
102
104
  let before = new Set(packed), after = new Set(published), changed = [...packed.filter((file) => !after.has(file)).map((file) => `- ${file}`), ...published.filter((file) => !before.has(file)).map((file) => `+ ${file}`)];
@@ -196,20 +198,22 @@ const TOOL_PROBE_TIMEOUT_MS = 1e4;
196
198
  *
197
199
  * `/d` skips registry AutoRun commands, which would otherwise execute inside a publish. Neither
198
200
  * `/s` nor `windowsVerbatimArguments` is used: without them Node applies its ordinary C-runtime
199
- * quoting to every argument, exactly as on any other platform, and there is no second escaping
200
- * layer to get wrong. The one residual is cmd's `%VAR%` expansion inside an argument, which no
201
- * argument this tool generates contains; a caller writing one after `--` gets it expanded.
201
+ * quoting to every argument, exactly as on any other platform, so this file owns no escaping.
202
+ * What it owns instead is the refusal below, which is what keeps that true.
202
203
  */
203
- function spawnArgs(command, args) {
204
- return process.platform === "win32" ? ["cmd.exe", [
204
+ function spawnArgs(command, args, platform) {
205
+ if (platform !== "win32") return [command, [...args]];
206
+ let unsafe = [command, ...args].filter((argument) => /[&|<>^%()]/.test(argument));
207
+ if (unsafe.length > 0) throw new PublishCleanError(`On Windows these arguments cannot be passed safely, because cmd.exe would interpret them:\n${unsafe.join("\n")}\nMove the package to a path without & | < > ^ % ( ), or publish from Linux or macOS.`);
208
+ return ["cmd.exe", [
205
209
  "/d",
206
210
  "/c",
207
211
  command,
208
212
  ...args
209
- ]] : [command, [...args]];
213
+ ]];
210
214
  }
211
215
  function run(command, args, cwd) {
212
- return execFileSync(...spawnArgs(command, args), {
216
+ return execFileSync(...spawnArgs(command, args, process.platform), {
213
217
  cwd,
214
218
  encoding: "utf8",
215
219
  stdio: [
@@ -230,7 +234,7 @@ function run(command, args, cwd) {
230
234
  * that says nothing about the one action that cannot be undone.
231
235
  */
232
236
  function runAttached(command, args, cwd) {
233
- execFileSync(...spawnArgs(command, args), {
237
+ execFileSync(...spawnArgs(command, args, process.platform), {
234
238
  cwd,
235
239
  stdio: [
236
240
  "ignore",
@@ -263,7 +267,7 @@ function toolFailureReason(cause) {
263
267
  }
264
268
  function requireTool(name) {
265
269
  try {
266
- execFileSync(...spawnArgs(name, ["--version"]), {
270
+ execFileSync(...spawnArgs(name, ["--version"], process.platform), {
267
271
  stdio: [
268
272
  "ignore",
269
273
  "ignore",
@@ -535,6 +539,28 @@ function entrySize(block) {
535
539
  return size;
536
540
  }
537
541
  /**
542
+ * Verifies a header block against the checksum tar stores inside it.
543
+ *
544
+ * This is the format's own integrity check over the 512 bytes that say where an entry starts,
545
+ * how long it is and what it is called — the fields a walk trusts absolutely. The gzip CRC
546
+ * already covers every byte of the archive, so on a packed tarball this is redundant; it is
547
+ * here because the rewrite AUTHORS one of these blocks, and a checksum nothing verifies is a
548
+ * value vouching for itself. Reading the written artifact back runs this over that block.
549
+ *
550
+ * Both sums are accepted because tar has two historical conventions: the standard sums the
551
+ * bytes unsigned, while older implementations summed them as signed chars. The two agree for
552
+ * every all-ASCII header and diverge the moment a name carries a byte above 127, so refusing
553
+ * the signed form would reject valid archives holding non-ASCII filenames.
554
+ */
555
+ function assertChecksum(header, name) {
556
+ let stored = Number.parseInt(field(header, 148, 8).trim(), 8), unsigned = 0, signed = 0;
557
+ for (let [index, byte] of header.entries()) {
558
+ let value = index >= 148 && index < 156 ? 32 : byte;
559
+ unsigned += value, signed += value > 127 ? value - 256 : value;
560
+ }
561
+ if (stored !== unsigned && stored !== signed) throw new PublishCleanError(`Tarball entry ${name} has a corrupt header checksum.`);
562
+ }
563
+ /**
538
564
  * Rejects a pax header that renames another entry onto the manifest path.
539
565
  *
540
566
  * Without this the archive could extract a `package.json` that no check ever saw: the guards
@@ -573,6 +599,7 @@ function readArchive(gzipped) {
573
599
  tail = tar.subarray(offset);
574
600
  break;
575
601
  }
602
+ assertChecksum(header, name);
576
603
  let size = entrySize(header), end = offset + 512 + 512 * Math.ceil(size / 512);
577
604
  if (end > tar.length) throw new PublishCleanError(`Tarball entry ${name} runs past the end of the archive.`);
578
605
  let type = field(header, 156, 1), body = tar.subarray(offset + 512, offset + 512 + size);
@@ -702,9 +729,19 @@ function readJson(file) {
702
729
  if (!isObject(parsed)) throw new PublishCleanError(`${file} must contain a JSON object.`);
703
730
  return parsed;
704
731
  }
705
- function npmVersion() {
706
- let raw = run("npm", ["--version"], process.cwd()).trim(), parts = raw.split(".").map((part) => Number.parseInt(part, 10));
707
- if (parts.length < 3 || parts.some((part) => Number.isNaN(part))) throw new PublishCleanError(`Unable to parse npm version: ${raw}`);
732
+ /**
733
+ * Asks an executable on PATH what version it is, e.g. `24.15.0` from node's `v24.15.0`.
734
+ *
735
+ * Both provenance floors are asked of PATH rather than read from this process, because the
736
+ * process that has to satisfy them is npm's, not this one. `npm` is a `#!/usr/bin/env node`
737
+ * script, so the Node enforcing the floor is whichever one PATH resolves — and that is not
738
+ * necessarily the one executing this file. Under Bun the gap is total: `process.versions.node`
739
+ * is a compatibility claim (1.3.14 reports 24.3.0) about a runtime that never runs npm at all,
740
+ * so reading it would answer a question nobody asked while looking exactly like a check.
741
+ */
742
+ function toolVersion(command) {
743
+ let raw = run(command, ["--version"], process.cwd()).trim().replace(/^v/, ""), parts = raw.split(".").map((part) => Number.parseInt(part, 10));
744
+ if (parts.length < 3 || parts.some((part) => Number.isNaN(part))) throw new PublishCleanError(`Unable to parse ${command} version: ${raw}`);
708
745
  return [
709
746
  parts[0] ?? 0,
710
747
  parts[1] ?? 0,
@@ -725,14 +762,10 @@ function publishEnv() {
725
762
  }
726
763
  function assertTrustedPublishingRuntime(pkg, publishArgs) {
727
764
  if (!wantsTrustedPublish(pkg, publishArgs, publishEnv())) return;
728
- let actual = npmVersion();
729
- if (!isAtLeast(actual, MIN_TRUSTED_NPM_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires npm ${MIN_TRUSTED_NPM_VERSION.join(".")} or newer; found ${actual.join(".")}.`);
730
- let node = process.versions.node.split(".").map(Number);
731
- if (!isAtLeast([
732
- node[0] ?? 0,
733
- node[1] ?? 0,
734
- node[2] ?? 0
735
- ], MIN_TRUSTED_NODE_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires Node.js ${MIN_TRUSTED_NODE_VERSION.join(".")} or newer; found ${process.versions.node}.`);
765
+ let npm = toolVersion("npm");
766
+ if (!isAtLeast(npm, MIN_TRUSTED_NPM_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires npm ${MIN_TRUSTED_NPM_VERSION.join(".")} or newer; found ${npm.join(".")}.`);
767
+ let node = toolVersion("node");
768
+ if (!isAtLeast(node, MIN_TRUSTED_NODE_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires Node.js ${MIN_TRUSTED_NODE_VERSION.join(".")} or newer; found ${node.join(".")}.`);
736
769
  }
737
770
  function warnIfNonPnpmLifecycle() {
738
771
  let userAgent = process.env.npm_config_user_agent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anizoptera/publish-clean",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Pre-publish npm package cleaner that strips common useless noise and crap from package.json; prevents accidental publication of unwanted things and unresolved dependencies. No deps, one JS file.",
5
5
  "keywords": [
6
6
  "bun",