Skip to content

Commit 39b625e

Browse files
reggiCopilot
andauthored
fix: key stage download --json output by package name (#9381)
Backports #9380 to `release/v11` to keep `npm stage download --json` output consistent across branches. On `release/v11`, the `key == null` guard in `lib/utils/tar.js` was emitting the bare object (no wrapper key). This change adds a key so the output matches `latest` and aligns with `npm publish --json` / `npm pack --json`. ### Before (on release/v11) ```json { "name": "polo-meow-meow-meow", "version": "1.0.3", ... } ``` ### After ```json { "polo-meow-meow-meow": { "name": "polo-meow-meow-meow", "version": "1.0.3", ... } } ``` ### Background The `key == null` fallback in `lib/utils/tar.js` (still present on `release/v11`) was removed from `latest` in #9247 ("fix: sync json output of pack and publish") as a `BREAKING CHANGE`. Per that PR: > BREAKING CHANGE: the --json output of `npm pack` and `npm publish` have changed. They are now always consistent, and in the same format. > > Previously, `npm pack` would output an array of entries and `npm publish` an object. The `npm publish` object also changed forms depending on if workspaces were being published. > > Now, the output is always an object with the package name as the top level index. That breaking change was intentionally not backported to v11 (maintenance branch), so the fallback still exists here. This backport keeps the call-site contract aligned across branches so consumers get the same `{ "<pkg>": {...} }` shape on both. Clean cherry-pick of 9f7834d from #9380. --------- Co-authored-by: Copilot <[email protected]>
1 parent 0d5d899 commit 39b625e

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎lib/commands/stage/download.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StageDownload extends BaseCommand {
3131

3232
const manifest = await this.#readManifestFromTarball(data)
3333
const pkgContents = await getContents(manifest, data)
34-
logTar(pkgContents, { unicode, json })
34+
logTar(pkgContents, { unicode, json, key: pkgContents.name })
3535

3636
const safeName = pkgContents.name.replace('@', '').replace('/', '-')
3737
const filename = `${safeName}-${pkgContents.version}-${stageId}.tgz`

‎test/lib/utils/tar.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ t.test('should log tarball contents with unicode', async (t) => {
109109
t.end()
110110
})
111111

112+
t.test('logTar with json and no key emits bare tarball object', async (t) => {
113+
const buffered = []
114+
const logTar = tmock(t, '{LIB}/utils/tar.js', {
115+
'proc-log': {
116+
log: { notice: () => {} },
117+
output: { buffer: (data) => buffered.push(data) },
118+
},
119+
}).logTar
120+
121+
const tarball = { name: 'my-pkg', version: '1.0.0' }
122+
logTar(tarball, { json: true })
123+
t.strictSame(buffered, [tarball], 'buffers the bare tarball when key is omitted')
124+
})
125+
112126
t.test('should getContents of a tarball with only a package.json', async (t) => {
113127
const testDir = t.testdir({
114128
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)