@anizoptera/publish-clean 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +29 -17
  2. package/dist/cli.js +304 -196
  3. package/package.json +1 -6
package/README.md CHANGED
@@ -181,14 +181,14 @@ publish-clean packages/my-lib -- --access public --tag next
181
181
 
182
182
  ```mermaid
183
183
  flowchart TD
184
- A[Your package directory] -->|pnpm pack| B[Tarball 1 - pnpm decides the file set]
184
+ A[Your package directory] -->|pnpm pack| B[Tarball - pnpm decides the file set]
185
185
  B --> C[Extract into a temp directory]
186
186
  C --> D{No leaked files?}
187
- D --> E[Rewrite package.json for consumers]
187
+ D --> E[Clean package.json for consumers]
188
188
  E --> F{Every declared main/exports/bin/types path exists?}
189
- F -->|npm pack --ignore-scripts| G[Tarball 2 - the bytes that ship]
190
- G --> H{Same files, same manifest, still no leaks?}
191
- H --> I[npm publish tarball 2]
189
+ F -->|replace the package.json member| G[Same tarball, cleaned manifest]
190
+ G --> H{Same files, expected manifest, still no leaks?}
191
+ H --> I[npm publish that tarball]
192
192
  ```
193
193
 
194
194
  Any gate that fails exits non-zero and nothing is published. `--dry-run` and
@@ -279,20 +279,32 @@ The obvious alternative is what a lot of hand-rolled release scripts do: edit
279
279
  and then your repo is sitting on a manifest nobody meant to keep. It also can't be run
280
280
  twice at once. Cleaning a copy has neither problem and costs a directory in `/tmp`.
281
281
 
282
- ### Why npm packs the cleaned copy a second time
282
+ ### Why the tarball is edited instead of packed again
283
283
 
284
284
  Give npm a directory and it packs during the upload, so the bytes that reach the registry
285
- only come into existence after the last check has run.
286
-
287
- Packing first means they exist before anything is uploaded. They can be listed, compared
288
- against the cleaned directory, scanned for leaks again, kept with `--tarball-out` for a
289
- build attestation, and then published as themselves. What you inspect is what ships. If
290
- cleaning ever dropped a real file, you find out here instead of from a bug report.
291
-
292
- The second pack uses `--ignore-scripts`. The first one doesn't, deliberately: `pnpm pack`
293
- runs your `prepare` and `prepack` scripts, which is how your build output gets into the
294
- package at all. Running them again on the cleaned copy could only change it after it was
295
- checked.
285
+ only come into existence after the last check has run. Packing first means they exist
286
+ before anything is uploaded: they can be listed, scanned for leaks, kept with
287
+ `--tarball-out` for a build attestation, and then published as themselves. What you
288
+ inspect is what ships.
289
+
290
+ So the manifest has to be cleaned inside a tarball that already exists. The obvious way is
291
+ to unpack, edit, and pack again — and it is wrong. `files` is a packing instruction, and
292
+ cleaning removes it, because it is useless to anyone installing the package. A second pack
293
+ then has nothing left to select with, falls back to your `.gitignore`/`.npmignore` for
294
+ exclusion, and quietly drops files the first pack included. A package shipping a
295
+ `.gitignore` that excludes any other shipped file loses it.
296
+
297
+ Editing avoids the whole question. Only the `package/package.json` member is replaced;
298
+ every other entry is copied without being decoded, so the file set stays exactly what pnpm
299
+ chose and entry shapes this tool does not model (pax headers for long paths, prefix
300
+ splitting) pass through untouched. It also keeps pnpm's normalised metadata — owner `0:0`,
301
+ a fixed timestamp, mode 644 — which a plain `tar` invocation would replace with the build
302
+ machine's own user and group names.
303
+
304
+ Lifecycle scripts run once, at the first pack. `pnpm pack` runs your `prepare` and
305
+ `prepack`, which is how build output reaches the package at all. Nothing runs afterwards:
306
+ npm skips `prepack`/`postpack` when it is handed a tarball rather than a directory, so
307
+ nothing can alter the artifact after it was checked.
296
308
 
297
309
  ### Why npm does the publishing
298
310
 
package/dist/cli.js CHANGED
@@ -1,12 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
  import { execFileSync } from "node:child_process";
3
3
  import { readFileSync, statSync } from "node:fs";
4
- import { copyFile, mkdir, mkdtemp, readdir, rm, stat, writeFile } from "node:fs/promises";
4
+ import { copyFile, mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
5
5
  import { tmpdir } from "node:os";
6
6
  import path from "node:path";
7
7
  import { parseArgs } from "node:util";
8
- //#region src/cli.ts
9
- const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.catalogs.patchedDependencies.private.stylelint.trustedDependencies.wireit.devDependencies.overrides.resolutions.ava.c8.jest.mocha.nyc.tap.vitest.babel.biome.browserslist.eslintConfig.eslintIgnore.prettier.remarkConfig.xo.commitlint.husky.lint-staged.simple-git-hooks.clean-publish.publish-clean.semantic-release.packageManager.pnpm.volta.workspaces.nx.turbo".split(".")), CONSUMER_SCRIPTS = /* @__PURE__ */ new Set([
8
+ import { gunzipSync, gzipSync } from "node:zlib";
9
+ //#region src/rules.ts
10
+ /**
11
+ * Every decision this tool makes about what a published package carries, with no process,
12
+ * filesystem or argv in reach. The split is not cosmetic: these are the rules that say what
13
+ * ships, and each is worth exercising directly, one input to one output, rather than through
14
+ * two package managers and a tarball. cli.ts owns the effects and wires them to these.
15
+ *
16
+ * Ambient inputs arrive as parameters, never by reaching for them: reading `process.env` from
17
+ * here would make these cases depend on state a test can only set process-wide, which is both
18
+ * a mock and a race against every case running beside it.
19
+ */
20
+ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.catalogs.patchedDependencies.private.files.stylelint.trustedDependencies.wireit.devDependencies.overrides.resolutions.ava.c8.jest.mocha.nyc.tap.vitest.babel.biome.browserslist.eslintConfig.eslintIgnore.prettier.remarkConfig.xo.commitlint.husky.lint-staged.simple-git-hooks.clean-publish.publish-clean.semantic-release.packageManager.pnpm.volta.workspaces.nx.turbo".split(".")), CONSUMER_SCRIPTS = /* @__PURE__ */ new Set([
10
21
  "preinstall",
11
22
  "install",
12
23
  "postinstall",
@@ -33,7 +44,6 @@ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.cata
33
44
  "cpu",
34
45
  "engines",
35
46
  "exports",
36
- "files",
37
47
  "imports",
38
48
  "libc",
39
49
  "license",
@@ -75,19 +85,6 @@ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.cata
75
85
  "unpkg"
76
86
  ]);
77
87
  /**
78
- * Names the fields that survived cleaning without anyone recognising them.
79
- *
80
- * The strip list can only ever describe tools that existed when it was written, so every
81
- * new `package.json` key some tool invents ships to consumers until a human notices. This
82
- * cannot be fixed by keeping only recognised fields: that would silently drop a key some
83
- * consumer genuinely resolves, and the breakage would surface in a stranger's project
84
- * rather than here. So the field ships, and the maintainer is told it did, with the exact
85
- * config that would drop it next time.
86
- *
87
- * Reported on stderr because it is advice about the package, not an error in it. Nothing
88
- * about the published artifact depends on whether anyone reads this.
89
- */
90
- /**
91
88
  * Refuses a published manifest that lost a field the source declared and consumers read.
92
89
  *
93
90
  * Every other check here asks whether something got in that should not have. This asks the
@@ -112,9 +109,24 @@ function assertNoLostConsumerFields(sourcePkg, finalPkg, removedOnRequest) {
112
109
  ]), lost = Object.keys(sourcePkg).filter((field) => (RUNTIME_MANIFEST_FIELDS.has(field) || REGISTRY_MANIFEST_FIELDS.has(field)) && !expected.has(field) && !(field in finalPkg));
113
110
  if (lost.length > 0) throw new PublishCleanError(`The published manifest is missing fields the source declared and consumers read:\n${lost.join("\n")}`);
114
111
  }
115
- function reportUnrecognizedFields(pkg, kept) {
112
+ /**
113
+ * Names the fields that survived cleaning without anyone recognising them.
114
+ *
115
+ * The strip list can only ever describe tools that existed when it was written, so every new
116
+ * `package.json` key some tool invents ships to consumers until a human notices. This cannot
117
+ * be fixed by keeping only recognised fields: that would silently drop a key some consumer
118
+ * genuinely resolves, and the breakage would surface in a stranger's project rather than here.
119
+ * So the field ships, and the maintainer is told it did, with the exact config that would drop
120
+ * it next time. It is advice about the package, not an error in it — nothing about the
121
+ * published artifact depends on whether anyone reads it.
122
+ *
123
+ * Returned rather than printed, so the message is this function's value and can be asserted
124
+ * whole. Writing to `console` from here would make that assertion a spy on a process-wide
125
+ * object, which two cases running side by side can clobber.
126
+ */
127
+ function unrecognizedFieldsReport(pkg, kept) {
116
128
  let acknowledged = new Set(kept), unrecognized = Object.keys(pkg).filter((field) => !RUNTIME_MANIFEST_FIELDS.has(field) && !REGISTRY_MANIFEST_FIELDS.has(field) && !acknowledged.has(field));
117
- unrecognized.length !== 0 && console.warn(`publish-clean: these manifest fields are not recognised and were published as-is:\n${unrecognized.map((field) => ` ${field}`).join("\n")}\nStrip the ones consumers do not read, and acknowledge the ones they do:\n "publish-clean": { "devFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }\n "publish-clean": { "keepFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }`);
129
+ return unrecognized.length === 0 ? null : `publish-clean: these manifest fields are not recognised and were published as-is:\n${unrecognized.map((field) => ` ${field}`).join("\n")}\nStrip the ones consumers do not read, and acknowledge the ones they do:\n "publish-clean": { "devFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }\n "publish-clean": { "keepFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }`;
118
130
  }
119
131
  /**
120
132
  * Content that must never reach a registry. Matched against tarball paths with the
@@ -147,7 +159,7 @@ const CRITICAL_PATTERNS = [
147
159
  11,
148
160
  5,
149
161
  1
150
- ], TOOL_PROBE_TIMEOUT_MS = 1e4;
162
+ ];
151
163
  var PublishCleanError = class extends Error {
152
164
  constructor(message, options) {
153
165
  super(message, options), this.name = "PublishCleanError";
@@ -156,6 +168,251 @@ var PublishCleanError = class extends Error {
156
168
  function isObject(value) {
157
169
  return typeof value == "object" && !!value && !Array.isArray(value);
158
170
  }
171
+ function stringifyJson(value) {
172
+ return `${JSON.stringify(value, null, 2)}\n`;
173
+ }
174
+ function stableJson(value) {
175
+ return JSON.stringify(stableValue(value));
176
+ }
177
+ function stableValue(value) {
178
+ return Array.isArray(value) ? value.map(stableValue) : isObject(value) ? Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => [key, stableValue(child)])) : value;
179
+ }
180
+ function isAtLeast(actual, minimum) {
181
+ for (let index of [
182
+ 0,
183
+ 1,
184
+ 2
185
+ ]) {
186
+ if (actual[index] > minimum[index]) return !0;
187
+ if (actual[index] < minimum[index]) return !1;
188
+ }
189
+ return !0;
190
+ }
191
+ function wantsTrustedPublish(pkg, publishArgs, env) {
192
+ return publishArgs.includes("--provenance") || isObject(pkg.publishConfig) && pkg.publishConfig.provenance === !0 ? !0 : env.GITHUB_ACTIONS === "true" && typeof env.ACTIONS_ID_TOKEN_REQUEST_URL == "string";
193
+ }
194
+ function assertPublicPackage(pkg) {
195
+ if (pkg.private === !0) throw new PublishCleanError("Refusing to publish a package with private: true.");
196
+ }
197
+ function repositoryUrl(pkg) {
198
+ return typeof pkg.repository == "string" ? pkg.repository : isObject(pkg.repository) && typeof pkg.repository.url == "string" ? pkg.repository.url : null;
199
+ }
200
+ function githubRepositorySlug(url) {
201
+ let withoutGitPrefix = url.startsWith("git+") ? url.slice(4) : url;
202
+ if (withoutGitPrefix.startsWith("git@github.com:")) {
203
+ let slug = withoutGitPrefix.slice(15);
204
+ return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
205
+ }
206
+ try {
207
+ let parsed = new URL(withoutGitPrefix);
208
+ if (parsed.hostname !== "github.com") return null;
209
+ let slug = parsed.pathname.replace(/^\/+/, "");
210
+ return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
211
+ } catch {
212
+ return null;
213
+ }
214
+ }
215
+ function assertRepositoryForTrustedPublish(pkg, publishArgs, env) {
216
+ if (!wantsTrustedPublish(pkg, publishArgs, env) || env.GITHUB_ACTIONS !== "true" || typeof env.GITHUB_REPOSITORY != "string") return;
217
+ let repoUrl = repositoryUrl(pkg);
218
+ if (!repoUrl) throw new PublishCleanError("GitHub trusted publishing requires package.json repository.url to match GITHUB_REPOSITORY.");
219
+ if (githubRepositorySlug(repoUrl) !== env.GITHUB_REPOSITORY) throw new PublishCleanError(`package.json repository.url must match GITHUB_REPOSITORY (${env.GITHUB_REPOSITORY}); found ${repoUrl}.`);
220
+ }
221
+ function stripManifest(pkg, extraDevFields) {
222
+ let stripped = { ...pkg };
223
+ for (let field of [...DEV_FIELDS, ...extraDevFields]) delete stripped[field];
224
+ let scripts = isObject(pkg.scripts) ? pkg.scripts : null;
225
+ if (scripts) {
226
+ let kept = {};
227
+ for (let [name, value] of Object.entries(scripts)) CONSUMER_SCRIPTS.has(name) && typeof value == "string" && (kept[name] = value);
228
+ Object.keys(kept).length > 0 ? stripped.scripts = kept : delete stripped.scripts;
229
+ }
230
+ return stripped;
231
+ }
232
+ /**
233
+ * Pins the published manifest to a registry, when one was chosen.
234
+ *
235
+ * Written into `publishConfig` rather than passed only on the command line, because that is
236
+ * where npm looks when someone later republishes the same tarball by hand — the artifact then
237
+ * carries its own destination instead of depending on whoever runs the command. Any other
238
+ * `publishConfig` keys the author set are consumer-facing and survive untouched.
239
+ *
240
+ * Returns the manifest unchanged when no registry was chosen, so the caller has no branch and
241
+ * cannot forget one.
242
+ */
243
+ function withRegistry(pkg, registry) {
244
+ if (registry === null) return pkg;
245
+ let publishConfig = isObject(pkg.publishConfig) ? { ...pkg.publishConfig } : {};
246
+ return publishConfig.registry = registry, {
247
+ ...pkg,
248
+ publishConfig
249
+ };
250
+ }
251
+ /**
252
+ * Refuses a manifest that still carries a spec only a workspace can resolve. Published
253
+ * with one, the package is uninstallable for everyone, and the version cannot be taken
254
+ * back.
255
+ *
256
+ * Both call sites pass an already-stripped manifest, so the dev-only members of
257
+ * DEP_FIELDS are absent by construction. They are still checked, because a guard whose
258
+ * correctness depends on the order it happens to be called in fails silently the day
259
+ * someone reorders it.
260
+ */
261
+ function assertNoMonorepoProtocols(pkg) {
262
+ let failures = [];
263
+ for (let field of DEP_FIELDS) {
264
+ let map = pkg[field];
265
+ if (isObject(map)) for (let [name, spec] of Object.entries(map)) typeof spec == "string" && MONOREPO_PROTOCOLS.some((prefix) => spec.includes(prefix)) && failures.push(`${field}.${name}: ${spec}`);
266
+ }
267
+ if (failures.length > 0) throw new PublishCleanError(`Packed manifest contains unresolved monorepo-only dependency specs:\n${failures.join("\n")}\n${PUBLISH_ADVISORY}`);
268
+ }
269
+ function customDevFields(config) {
270
+ if (!Array.isArray(config.devFields)) return [];
271
+ let fields = config.devFields.filter((field) => typeof field == "string"), unsafe = fields.filter((field) => RUNTIME_MANIFEST_FIELDS.has(field));
272
+ if (unsafe.length > 0) throw new PublishCleanError(`publish-clean.devFields cannot remove runtime/public manifest fields:\n${unsafe.join("\n")}`);
273
+ return fields;
274
+ }
275
+ /**
276
+ * Fields the maintainer has confirmed belong in the published package, so they stop being
277
+ * reported as unrecognised.
278
+ *
279
+ * Without this the report has only one resolution, `devFields`, which deletes the field.
280
+ * That is the wrong answer for every package whose ecosystem this tool has never heard of:
281
+ * a VS Code extension needs `contributes` and `publisher` in the artifact to function at
282
+ * all. Its author would face a warning on every release that can only be silenced by
283
+ * breaking the extension, and a warning nobody can act on trains everyone to ignore the
284
+ * next one, including a real leak.
285
+ */
286
+ function keptFields(config) {
287
+ return Array.isArray(config.keepFields) ? config.keepFields.filter((field) => typeof field == "string") : [];
288
+ }
289
+ function assertFilesField(pkg, skip) {
290
+ if (!skip && (!Array.isArray(pkg.files) || pkg.files.length === 0)) throw new PublishCleanError("Package manifest must define a non-empty \"files\" array.");
291
+ }
292
+ function validatePackedFiles(files, skipSuspicious) {
293
+ let critical = files.filter((file) => CRITICAL_PATTERNS.some((pattern) => pattern.test(file)));
294
+ if (critical.length > 0) throw new PublishCleanError(`Critical files must not be published:\n${critical.join("\n")}`);
295
+ if (skipSuspicious) return;
296
+ let suspicious = files.filter((file) => SUSPICIOUS_PATTERNS.some((pattern) => pattern.test(file)));
297
+ if (suspicious.length > 0) throw new PublishCleanError(`Suspicious files in package artifact:\n${suspicious.join("\n")}`);
298
+ }
299
+ /**
300
+ * Walks a manifest value of any shape and collects the strings that name a file inside
301
+ * the package, so the caller can prove each one exists in the tarball.
302
+ *
303
+ * `mode` exists because the two families of path-bearing fields disagree about what a
304
+ * bare string means, and reading one by the other's rules produces wrong answers in both
305
+ * directions:
306
+ *
307
+ * - `"every-string"` for `main`, `module`, `types`, `typings`, `bin` and `typesVersions`,
308
+ * where every string is a path into this package.
309
+ * - `"relative-only"` for `exports`, `imports`, `sideEffects` and the object form of
310
+ * `browser`, where a string may just as well be an external package name, a condition
311
+ * target or a glob. Only a `./` or `../` prefix marks it as a path here. Collecting the
312
+ * rest would report a missing file for something that was never a file.
313
+ *
314
+ * Booleans are skipped rather than ignored by accident: `sideEffects: false` and
315
+ * `exports` condition values legitimately hold them.
316
+ */
317
+ function collectDeclaredPaths(value, out, mode) {
318
+ if (typeof value == "string") {
319
+ (mode === "every-string" || value.startsWith("./") || value.startsWith("../")) && out.push(value);
320
+ return;
321
+ }
322
+ if (typeof value != "boolean") {
323
+ if (Array.isArray(value)) {
324
+ for (let item of value) collectDeclaredPaths(item, out, mode);
325
+ return;
326
+ }
327
+ if (isObject(value)) for (let item of Object.values(value)) collectDeclaredPaths(item, out, mode);
328
+ }
329
+ }
330
+ function normalizeDeclaredPath(declared) {
331
+ let withoutDot = declared.startsWith("./") ? declared.slice(2) : declared;
332
+ if (!withoutDot || path.posix.isAbsolute(withoutDot)) return null;
333
+ let normalized = path.posix.normalize(withoutDot);
334
+ return normalized === "." || normalized === ".." || normalized.startsWith("../") ? null : normalized;
335
+ }
336
+ function packageConfig(pkg) {
337
+ let config = pkg["publish-clean"];
338
+ return isObject(config) ? config : {};
339
+ }
340
+ //#endregion
341
+ //#region src/tarball.ts
342
+ /**
343
+ * Replaces the manifest inside a packed tarball without repacking it.
344
+ *
345
+ * The pipeline packs once, with pnpm, because that packer decides the file set. Cleaning the
346
+ * manifest by unpacking and packing again hands that decision to a second packer, which
347
+ * re-derives it from the very field being cleaned and silently disagrees; rewriting the one
348
+ * member that changed keeps the first packer's selection, and its normalisation (uid/gid 0,
349
+ * a fixed mtime, mode 644) that a naive `tar` invocation leaks the build machine's identity
350
+ * into.
351
+ *
352
+ * Every entry other than the manifest is copied as opaque bytes and never decoded, so entry
353
+ * shapes this code does not model — pax extended headers, ustar prefix splitting, GNU long
354
+ * names — survive untouched. Only walking is universal: an entry's length is always its
355
+ * header block plus its size rounded up to a block, whatever the type.
356
+ */
357
+ const MANIFEST_PATH = "package/package.json", PAX_TYPES = /* @__PURE__ */ new Set(["x", "g"]);
358
+ function field(block, offset, length) {
359
+ return block.toString("utf8", offset, offset + length).replace(/\0.*/s, "");
360
+ }
361
+ function entrySize(block) {
362
+ if ((block[124] ?? 0) & 128) throw new PublishCleanError("Tarball uses base-256 entry sizes, which this tool cannot rewrite.");
363
+ let size = Number.parseInt(field(block, 124, 12).trim(), 8);
364
+ if (!Number.isFinite(size) || size < 0) throw new PublishCleanError("Tarball entry has an unreadable size field.");
365
+ return size;
366
+ }
367
+ /**
368
+ * Rejects a pax header that renames another entry onto the manifest path.
369
+ *
370
+ * Without this the archive could extract a `package.json` that no check ever saw: the guards
371
+ * validate the entry rewritten here, while an extractor honours the pax override and writes
372
+ * a different one over it. The header's own name field is `PaxHeader`, so nothing about the
373
+ * substitution is visible from the walk alone.
374
+ */
375
+ function assertNoManifestOverride(payload) {
376
+ for (let record of payload.toString("utf8").split("\n")) if (/^\d+ path=/.test(record) && record.slice(record.indexOf("=") + 1) === MANIFEST_PATH) throw new PublishCleanError(`Tarball contains a pax header renaming an entry to ${MANIFEST_PATH}.`);
377
+ }
378
+ /** Rebuilds a header for a changed payload size, keeping every other field of the original. */
379
+ function reheader(original, size) {
380
+ let header = Buffer.from(original);
381
+ header.write(size.toString(8).padStart(11, "0") + "\0", 124, 12, "ascii"), header.write(" ".repeat(8), 148, 8, "ascii");
382
+ let sum = 0;
383
+ for (let byte of header) sum += byte;
384
+ return header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, 8, "ascii"), header;
385
+ }
386
+ /**
387
+ * Returns the gzipped tarball with `package/package.json` replaced by `manifest`.
388
+ *
389
+ * Refuses anything it cannot rewrite unambiguously. Duplicate manifest entries are a refusal
390
+ * rather than a last-one-wins guess, because extractors disagree about which survives and the
391
+ * published manifest would then depend on the extractor rather than on this tool.
392
+ */
393
+ function replaceTarballManifest(archive, manifest) {
394
+ let tar = gunzipSync(archive), parts = [], replaced = !1;
395
+ for (let offset = 0; offset < tar.length;) {
396
+ let header = tar.subarray(offset, offset + 512), name = field(header, 0, 100);
397
+ if (name === "") {
398
+ parts.push(tar.subarray(offset));
399
+ break;
400
+ }
401
+ let size = entrySize(header), end = offset + 512 + 512 * Math.ceil(size / 512), type = field(header, 156, 1);
402
+ if (PAX_TYPES.has(type) && assertNoManifestOverride(tar.subarray(offset + 512, offset + 512 + size)), name === MANIFEST_PATH) {
403
+ if (replaced) throw new PublishCleanError(`Tarball contains ${MANIFEST_PATH} more than once.`);
404
+ if (type !== "0" && type !== "") throw new PublishCleanError(`${MANIFEST_PATH} is not a regular file in the tarball.`);
405
+ let body = Buffer.from(manifest, "utf8");
406
+ parts.push(reheader(header, body.length), body, Buffer.alloc(512 * Math.ceil(body.length / 512) - body.length)), replaced = !0;
407
+ } else parts.push(tar.subarray(offset, end));
408
+ offset = end;
409
+ }
410
+ if (!replaced) throw new PublishCleanError(`Tarball does not contain ${MANIFEST_PATH}.`);
411
+ return gzipSync(Buffer.concat(parts));
412
+ }
413
+ //#endregion
414
+ //#region src/cli.ts
415
+ const TOOL_PROBE_TIMEOUT_MS = 1e4;
159
416
  function readJson(file) {
160
417
  let parsed;
161
418
  try {
@@ -166,15 +423,6 @@ function readJson(file) {
166
423
  if (!isObject(parsed)) throw new PublishCleanError(`${file} must contain a JSON object.`);
167
424
  return parsed;
168
425
  }
169
- function stringifyJson(value) {
170
- return `${JSON.stringify(value, null, 2)}\n`;
171
- }
172
- function stableJson(value) {
173
- return JSON.stringify(stableValue(value));
174
- }
175
- function stableValue(value) {
176
- return Array.isArray(value) ? value.map(stableValue) : isObject(value) ? Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => [key, stableValue(child)])) : value;
177
- }
178
426
  function run(command, args, cwd) {
179
427
  return execFileSync(command, [...args], {
180
428
  cwd,
@@ -256,22 +504,20 @@ function npmVersion() {
256
504
  parts[2] ?? 0
257
505
  ];
258
506
  }
259
- function isAtLeast(actual, minimum) {
260
- for (let index of [
261
- 0,
262
- 1,
263
- 2
264
- ]) {
265
- if (actual[index] > minimum[index]) return !0;
266
- if (actual[index] < minimum[index]) return !1;
267
- }
268
- return !0;
269
- }
270
- function wantsTrustedPublish(pkg, publishArgs) {
271
- return publishArgs.includes("--provenance") || isObject(pkg.publishConfig) && pkg.publishConfig.provenance === !0 ? !0 : process.env.GITHUB_ACTIONS === "true" && typeof process.env.ACTIONS_ID_TOKEN_REQUEST_URL == "string";
507
+ /**
508
+ * The single place the ambient environment is read for publish decisions. Naming the three
509
+ * variables here keeps `process` out of the rules and makes the whole environmental surface of
510
+ * a publish visible at once, rather than spread across the guards that consult it.
511
+ */
512
+ function publishEnv() {
513
+ return {
514
+ ACTIONS_ID_TOKEN_REQUEST_URL: process.env.ACTIONS_ID_TOKEN_REQUEST_URL,
515
+ GITHUB_ACTIONS: process.env.GITHUB_ACTIONS,
516
+ GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY
517
+ };
272
518
  }
273
519
  function assertTrustedPublishingRuntime(pkg, publishArgs) {
274
- if (!wantsTrustedPublish(pkg, publishArgs)) return;
520
+ if (!wantsTrustedPublish(pkg, publishArgs, publishEnv())) return;
275
521
  let actual = npmVersion();
276
522
  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(".")}.`);
277
523
  let node = process.versions.node.split(".").map(Number);
@@ -302,82 +548,6 @@ function assertCleanGit(packageDir, skip) {
302
548
  throw error instanceof PublishCleanError ? error : new PublishCleanError("Unable to verify source git status.", { cause: error });
303
549
  }
304
550
  }
305
- function assertPublicPackage(pkg) {
306
- if (pkg.private === !0) throw new PublishCleanError("Refusing to publish a package with private: true.");
307
- }
308
- function repositoryUrl(pkg) {
309
- return typeof pkg.repository == "string" ? pkg.repository : isObject(pkg.repository) && typeof pkg.repository.url == "string" ? pkg.repository.url : null;
310
- }
311
- function githubRepositorySlug(url) {
312
- let withoutGitPrefix = url.startsWith("git+") ? url.slice(4) : url;
313
- if (withoutGitPrefix.startsWith("git@github.com:")) {
314
- let slug = withoutGitPrefix.slice(15);
315
- return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
316
- }
317
- try {
318
- let parsed = new URL(withoutGitPrefix);
319
- if (parsed.hostname !== "github.com") return null;
320
- let slug = parsed.pathname.replace(/^\/+/, "");
321
- return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
322
- } catch {
323
- return null;
324
- }
325
- }
326
- function assertRepositoryForTrustedPublish(pkg, publishArgs) {
327
- if (!wantsTrustedPublish(pkg, publishArgs) || process.env.GITHUB_ACTIONS !== "true" || typeof process.env.GITHUB_REPOSITORY != "string") return;
328
- let repoUrl = repositoryUrl(pkg);
329
- if (!repoUrl) throw new PublishCleanError("GitHub trusted publishing requires package.json repository.url to match GITHUB_REPOSITORY.");
330
- if (githubRepositorySlug(repoUrl) !== process.env.GITHUB_REPOSITORY) throw new PublishCleanError(`package.json repository.url must match GITHUB_REPOSITORY (${process.env.GITHUB_REPOSITORY}); found ${repoUrl}.`);
331
- }
332
- function stripManifest(pkg, extraDevFields) {
333
- let stripped = { ...pkg };
334
- for (let field of [...DEV_FIELDS, ...extraDevFields]) delete stripped[field];
335
- let scripts = isObject(pkg.scripts) ? pkg.scripts : null;
336
- if (scripts) {
337
- let kept = {};
338
- for (let [name, value] of Object.entries(scripts)) CONSUMER_SCRIPTS.has(name) && typeof value == "string" && (kept[name] = value);
339
- Object.keys(kept).length > 0 ? stripped.scripts = kept : delete stripped.scripts;
340
- }
341
- return stripped;
342
- }
343
- /**
344
- * Refuses a manifest that still carries a spec only a workspace can resolve. Published
345
- * with one, the package is uninstallable for everyone, and the version cannot be taken
346
- * back.
347
- *
348
- * Both call sites pass an already-stripped manifest, so the dev-only members of
349
- * DEP_FIELDS are absent by construction. They are still checked, because a guard whose
350
- * correctness depends on the order it happens to be called in fails silently the day
351
- * someone reorders it.
352
- */
353
- function assertNoMonorepoProtocols(pkg) {
354
- let failures = [];
355
- for (let field of DEP_FIELDS) {
356
- let map = pkg[field];
357
- if (isObject(map)) for (let [name, spec] of Object.entries(map)) typeof spec == "string" && MONOREPO_PROTOCOLS.some((prefix) => spec.includes(prefix)) && failures.push(`${field}.${name}: ${spec}`);
358
- }
359
- if (failures.length > 0) throw new PublishCleanError(`Packed manifest contains unresolved monorepo-only dependency specs:\n${failures.join("\n")}\n${PUBLISH_ADVISORY}`);
360
- }
361
- function customDevFields(config) {
362
- if (!Array.isArray(config.devFields)) return [];
363
- let fields = config.devFields.filter((field) => typeof field == "string"), unsafe = fields.filter((field) => RUNTIME_MANIFEST_FIELDS.has(field));
364
- if (unsafe.length > 0) throw new PublishCleanError(`publish-clean.devFields cannot remove runtime/public manifest fields:\n${unsafe.join("\n")}`);
365
- return fields;
366
- }
367
- /**
368
- * Fields the maintainer has confirmed belong in the published package, so they stop being
369
- * reported as unrecognised.
370
- *
371
- * Without this the report has only one resolution, `devFields`, which deletes the field.
372
- * That is the wrong answer for every package whose ecosystem this tool has never heard of:
373
- * a VS Code extension needs `contributes` and `publisher` in the artifact to function at
374
- * all. Its author would face a warning on every release that can only be silenced by
375
- * breaking the extension, and a warning nobody can act on trains everyone to ignore the
376
- * next one, including a real leak.
377
- */
378
- function keptFields(config) {
379
- return Array.isArray(config.keepFields) ? config.keepFields.filter((field) => typeof field == "string") : [];
380
- }
381
551
  async function walkFiles(dir, root, files) {
382
552
  let entries = await readdir(dir, { withFileTypes: !0 });
383
553
  for (let entry of entries) {
@@ -389,16 +559,6 @@ async function walkFiles(dir, root, files) {
389
559
  entry.isFile() && files.push(relative);
390
560
  }
391
561
  }
392
- function assertFilesField(pkg, skip) {
393
- if (!skip && (!Array.isArray(pkg.files) || pkg.files.length === 0)) throw new PublishCleanError("Package manifest must define a non-empty \"files\" array.");
394
- }
395
- function validatePackedFiles(files, skipSuspicious) {
396
- let critical = files.filter((file) => CRITICAL_PATTERNS.some((pattern) => pattern.test(file)));
397
- if (critical.length > 0) throw new PublishCleanError(`Critical files must not be published:\n${critical.join("\n")}`);
398
- if (skipSuspicious) return;
399
- let suspicious = files.filter((file) => SUSPICIOUS_PATTERNS.some((pattern) => pattern.test(file)));
400
- if (suspicious.length > 0) throw new PublishCleanError(`Suspicious files in package artifact:\n${suspicious.join("\n")}`);
401
- }
402
562
  function listTarballFiles(tarball, cwd) {
403
563
  return run("tar", ["tzf", tarball], cwd).trim().split("\n").filter(Boolean).map((file) => file.replaceAll(path.sep, "/")).map((file) => file.startsWith("package/") ? file.slice(8) : file).filter((file) => file.length > 0 && !file.endsWith("/")).sort();
404
564
  }
@@ -434,47 +594,6 @@ async function soleTarball(packRoot, packer) {
434
594
  if (tarballs.length !== 1 || !tarball) throw new PublishCleanError(`${packer} left ${tarballs.length} tarballs in ${packRoot}; expected exactly one.`);
435
595
  return path.join(packRoot, tarball);
436
596
  }
437
- function assertFinalTarballIncludesCleanedFiles(cleanedFiles, finalFiles) {
438
- let missing = cleanedFiles.filter((file) => !finalFiles.includes(file)).sort();
439
- if (missing.length !== 0) throw new PublishCleanError(`Final npm tarball dropped files from the cleaned package:\n${missing.join("\n")}`);
440
- }
441
- /**
442
- * Walks a manifest value of any shape and collects the strings that name a file inside
443
- * the package, so the caller can prove each one exists in the tarball.
444
- *
445
- * `mode` exists because the two families of path-bearing fields disagree about what a
446
- * bare string means, and reading one by the other's rules produces wrong answers in both
447
- * directions:
448
- *
449
- * - `"every-string"` for `main`, `module`, `types`, `typings`, `bin` and `typesVersions`,
450
- * where every string is a path into this package.
451
- * - `"relative-only"` for `exports`, `imports`, `sideEffects` and the object form of
452
- * `browser`, where a string may just as well be an external package name, a condition
453
- * target or a glob. Only a `./` or `../` prefix marks it as a path here. Collecting the
454
- * rest would report a missing file for something that was never a file.
455
- *
456
- * Booleans are skipped rather than ignored by accident: `sideEffects: false` and
457
- * `exports` condition values legitimately hold them.
458
- */
459
- function collectDeclaredPaths(value, out, mode) {
460
- if (typeof value == "string") {
461
- (mode === "every-string" || value.startsWith("./") || value.startsWith("../")) && out.push(value);
462
- return;
463
- }
464
- if (typeof value != "boolean") {
465
- if (Array.isArray(value)) {
466
- for (let item of value) collectDeclaredPaths(item, out, mode);
467
- return;
468
- }
469
- if (isObject(value)) for (let item of Object.values(value)) collectDeclaredPaths(item, out, mode);
470
- }
471
- }
472
- function normalizeDeclaredPath(declared) {
473
- let withoutDot = declared.startsWith("./") ? declared.slice(2) : declared;
474
- if (!withoutDot || path.posix.isAbsolute(withoutDot)) return null;
475
- let normalized = path.posix.normalize(withoutDot);
476
- return normalized === "." || normalized === ".." || normalized.startsWith("../") ? null : normalized;
477
- }
478
597
  function assertDeclaredFiles(pkg, packageDir) {
479
598
  let paths = [];
480
599
  for (let field of [
@@ -506,10 +625,6 @@ function exists(file) {
506
625
  return !1;
507
626
  }
508
627
  }
509
- function packageConfig(pkg) {
510
- let config = pkg["publish-clean"];
511
- return isObject(config) ? config : {};
512
- }
513
628
  async function packAndClean(packageDir, opts) {
514
629
  requireTool("pnpm"), requireTool("npm"), requireTool("tar"), warnIfNonPnpmLifecycle();
515
630
  let sourcePkg = readJson(path.join(packageDir, "package.json"));
@@ -522,9 +637,11 @@ async function packAndClean(packageDir, opts) {
522
637
  "pack",
523
638
  "--pack-destination",
524
639
  root
525
- ], packageDir), run("tar", [
640
+ ], packageDir);
641
+ let tarball = await soleTarball(root, "pnpm pack");
642
+ run("tar", [
526
643
  "xzf",
527
- await soleTarball(root, "pnpm pack"),
644
+ tarball,
528
645
  "-C",
529
646
  root
530
647
  ], packageDir);
@@ -532,25 +649,16 @@ async function packAndClean(packageDir, opts) {
532
649
  if (!(await stat(extracted)).isDirectory()) throw new PublishCleanError("Extracted tarball did not contain package/ directory.");
533
650
  let files = [];
534
651
  await walkFiles(extracted, extracted, files), validatePackedFiles(files, skipFileCheck);
535
- let packedPkgPath = path.join(extracted, "package.json"), cleanedPkg = stripManifest(readJson(packedPkgPath), extraDevFields);
536
- if (registry) {
537
- let publishConfig = isObject(cleanedPkg.publishConfig) ? { ...cleanedPkg.publishConfig } : {};
538
- publishConfig.registry = registry, cleanedPkg.publishConfig = publishConfig;
539
- }
540
- assertNoMonorepoProtocols(cleanedPkg), reportUnrecognizedFields(cleanedPkg, keepFields), await writeFile(packedPkgPath, stringifyJson(cleanedPkg)), assertDeclaredFiles(cleanedPkg, extracted);
541
- let cleanedFiles = [];
542
- await walkFiles(extracted, extracted, cleanedFiles);
543
- let finalPackRoot = path.join(root, "npm-pack");
544
- await mkdir(finalPackRoot), run("npm", [
545
- "pack",
546
- "--ignore-scripts",
547
- "--pack-destination",
548
- finalPackRoot
549
- ], extracted);
550
- let finalTarball = await soleTarball(finalPackRoot, "npm pack"), finalFiles = listTarballFiles(finalTarball, extracted);
551
- validatePackedFiles(finalFiles, skipFileCheck), assertFinalTarballIncludesCleanedFiles(cleanedFiles, finalFiles);
552
- let finalPkg = readTarballJson(finalTarball, "package.json", extracted);
553
- if (assertNoMonorepoProtocols(finalPkg), assertNoLostConsumerFields(sourcePkg, finalPkg, extraDevFields), stableJson(finalPkg) !== stableJson(cleanedPkg)) throw new PublishCleanError("Final npm tarball manifest differs from the cleaned manifest.");
652
+ let packedPkgPath = path.join(extracted, "package.json"), cleanedPkg = withRegistry(stripManifest(readJson(packedPkgPath), extraDevFields), registry);
653
+ assertNoMonorepoProtocols(cleanedPkg);
654
+ let unrecognized = unrecognizedFieldsReport(cleanedPkg, keepFields);
655
+ unrecognized && console.warn(unrecognized), assertDeclaredFiles(cleanedPkg, extracted), await writeFile(packedPkgPath, stringifyJson(cleanedPkg));
656
+ let publishRoot = path.join(root, "publish");
657
+ await mkdir(publishRoot);
658
+ let finalTarball = path.join(publishRoot, path.basename(tarball));
659
+ await writeFile(finalTarball, replaceTarballManifest(await readFile(tarball), stringifyJson(cleanedPkg))), validatePackedFiles(listTarballFiles(finalTarball, root), skipFileCheck);
660
+ let finalPkg = readTarballJson(finalTarball, "package.json", root);
661
+ if (assertNoMonorepoProtocols(finalPkg), assertNoLostConsumerFields(sourcePkg, finalPkg, extraDevFields), stableJson(finalPkg) !== stableJson(cleanedPkg)) throw new PublishCleanError("Rewritten tarball manifest differs from the cleaned manifest.");
554
662
  if (opts.tarballOut !== null) {
555
663
  await mkdir(opts.tarballOut, { recursive: !0 });
556
664
  let kept = path.join(opts.tarballOut, path.basename(finalTarball));
@@ -561,7 +669,7 @@ async function packAndClean(packageDir, opts) {
561
669
  keepRoot = !0, console.log(`[dry-run] Extracted package at: ${extracted}`), console.log(`[dry-run] Final tarball at: ${finalTarball}`);
562
670
  return;
563
671
  }
564
- assertRepositoryForTrustedPublish(cleanedPkg, opts.publishArgs), runAttached("npm", registry ? [
672
+ assertRepositoryForTrustedPublish(cleanedPkg, opts.publishArgs, publishEnv()), runAttached("npm", registry ? [
565
673
  "publish",
566
674
  finalTarball,
567
675
  "--registry",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anizoptera/publish-clean",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Publish npm packages with a consumer-only package.json. Refuses to ship a .env, a private key, or an unresolved workspace dependency.",
5
5
  "keywords": [
6
6
  "bun",
@@ -32,11 +32,6 @@
32
32
  "bin": {
33
33
  "publish-clean": "./dist/cli.js"
34
34
  },
35
- "files": [
36
- "dist",
37
- "README.md",
38
- "LICENSE"
39
- ],
40
35
  "type": "module",
41
36
  "sideEffects": false,
42
37
  "exports": {