diff --git a/.dockerignore b/.dockerignore index 4d9b1a2..3a44840 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,4 @@ ** !Dockerfile +!patches/ +!patches/*.patch diff --git a/.github/scripts/npm-archive-smoke.sh b/.github/scripts/npm-archive-smoke.sh new file mode 100644 index 0000000..a96969b --- /dev/null +++ b/.github/scripts/npm-archive-smoke.sh @@ -0,0 +1,51 @@ +#!/bin/sh +set -eu + +workdir=$(mktemp -d) +trap 'rm -rf "$workdir"' EXIT +export npm_config_cache="$workdir/npm-cache" +cd "$workdir" + +mkdir package modified consumer +printf '%s\n' '{"name":"npm-archive-smoke","version":"1.0.0","main":"index.js","bin":"index.js"}' > package/package.json +printf '%s\n' '#!/usr/bin/env node' 'module.exports = "archive smoke";' > package/index.js +npm pack --offline --ignore-scripts --pack-destination "$workdir" ./package +archive="$workdir/npm-archive-smoke-1.0.0.tgz" + +printf '%s\n' '{"name":"npm-archive-consumer","version":"1.0.0","private":true}' > consumer/package.json +npm install --prefix consumer --offline --ignore-scripts --no-audit --no-fund "$archive" +node -e 'if (require("./consumer/node_modules/npm-archive-smoke") !== "archive smoke") process.exit(1)' +test -x consumer/node_modules/npm-archive-smoke/index.js + +cp package/package.json modified/package.json +printf '%s\n' '#!/usr/bin/env node' 'module.exports = "second archive";' > modified/index.js +npm pack --offline --ignore-scripts --pack-destination "$workdir/modified" ./modified +npm diff --offline --ignore-scripts --diff-name-only \ + --diff="file:$archive" --diff="file:$workdir/modified/$(basename "$archive")" > diff.txt +grep -Fx index.js diff.txt + +npm_cli=$(readlink -f "$(command -v npm)") +npm_dir=$(dirname "$(dirname "$npm_cli")") +npm ls --prefix "$npm_dir" tar --all +# npm 10 resolves a separate pacote bundle from its audit calculator. +node --input-type=module - "$npm_dir" "$archive" <<'JS' +import assert from 'node:assert/strict' +import { readFile } from 'node:fs/promises' +import { createRequire } from 'node:module' +import { resolve } from 'node:path' + +const [npmDir, archivePath] = process.argv.slice(2) +const calculatorRequire = createRequire(`${npmDir}/node_modules/@npmcli/metavuln-calculator/package.json`) +const pacote = calculatorRequire('pacote') +const destination = resolve('calculator-pacote') +await pacote.extract(`file:${archivePath}`, destination, { cache: process.env.npm_config_cache }) +assert.match(await readFile(`${destination}/index.js`, 'utf8'), /archive smoke/) +JS +node_version=$(node -p 'process.versions.node') +header="node-v$node_version/include/node/node.h" +mkdir -p "headers/$(dirname "$header")" +printf '%s\n' '#define BEEVELOP_NPM_ARCHIVE_SMOKE 1' > "headers/$header" +tar -C headers -czf node-headers-smoke.tgz "node-v$node_version" +node "$npm_dir/node_modules/node-gyp/bin/node-gyp.js" install "$node_version" \ + --devdir="$workdir/gyp-cache" --tarball="$workdir/node-headers-smoke.tgz" +grep -Fx '#define BEEVELOP_NPM_ARCHIVE_SMOKE 1' "gyp-cache/$node_version/include/node/node.h" diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index aef6ab4..031cc81 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -96,6 +96,10 @@ jobs: virtualenv --no-download /tmp/virtualenv /tmp/virtualenv/bin/python -m pip --version SH + - name: Smoke-test npm package archives + env: + PLATFORM: ${{ matrix.platform }} + run: docker run --rm --network none --platform "$PLATFORM" -i nodejs-python:smoke sh < .github/scripts/npm-archive-smoke.sh publish: needs: validate diff --git a/Dockerfile b/Dockerfile index 721d770..fce9c7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,25 +9,51 @@ FROM node-24 AS node-arm64 FROM node:22.23.5-bookworm-slim@sha256:be979e5523c76fa4d5ca4ebd3099078f220aa47ec39c48ad5b5df9314debd6eb AS node-arm FROM node-${TARGETARCH} AS runtime +ARG TARGETARCH LABEL org.opencontainers.image.title="Beevelop Node.js and Python" \ org.opencontainers.image.description="Node.js, npm, Yarn Classic, and Debian Python tooling" \ org.opencontainers.image.source="https://github.com/beevelop/docker-nodejs-python" \ org.opencontainers.image.licenses="MIT" -RUN apt-get update \ - && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ +# Keep these version-specific ports until the official Node images bundle npm with tar 8. +RUN --mount=type=bind,source=patches,target=/tmp/npm-patches \ + set -eu; \ + apt-get update; \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ ca-certificates \ curl \ gnupg \ lsb-release \ + patch \ python-is-python3 \ python3 \ python3-dev \ python3-pip \ python3-venv \ - python3-virtualenv \ - && rm -rf /var/lib/apt/lists/* + python3-virtualenv; \ + case "$TARGETARCH" in \ + amd64|arm64) npm_version=11.21.3 ;; \ + arm) npm_version=10.9.9 ;; \ + *) echo "Unsupported npm archive patch architecture: $TARGETARCH" >&2; exit 1 ;; \ + esac; \ + npm_dir=/usr/local/lib/node_modules/npm; \ + npm_tar_dir="$npm_dir/node_modules/tar"; \ + test "$(npm --version)" = "$npm_version"; \ + test "$(node -p "require('$npm_tar_dir/package.json').version")" = 7.5.22; \ + tmp_dir=$(mktemp -d); \ + trap 'rm -rf "$tmp_dir"' EXIT; \ + curl --fail --silent --show-error --location --retry 4 \ + https://registry.npmjs.org/tar/-/tar-8.1.2.tgz \ + --output "$tmp_dir/npm-tar.tgz"; \ + printf '%s %s\n' cb37600ca3ea445944601b532553fdd614f6cf24f069d4730002e519c7261a1358b9c576889c928502d3752021fd863d79f2e68cd8e492db2f03085583a4a27f "$tmp_dir/npm-tar.tgz" \ + | sha512sum --check --strict; \ + patch --batch --forward --fuzz=0 -p1 -d "$npm_dir" < "/tmp/npm-patches/npm-$npm_version-tar-8.patch"; \ + rm -rf "$npm_tar_dir"; \ + mkdir "$npm_tar_dir"; \ + tar -xzf "$tmp_dir/npm-tar.tgz" -C "$npm_tar_dir" --strip-components=1 --no-same-owner; \ + apt-get purge -y patch; \ + rm -rf /var/lib/apt/lists/* ENV TERM=xterm diff --git a/README.md b/README.md index 76baa36..6461352 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ Use Node.js, npm, Yarn Classic, and Python in the same build environment. The im The 32-bit ARM build uses the maintained Node.js 22 Docker Official Image. Node.js 22 is scheduled to reach end of life on April 30, 2027. Plan to move ARM builds to `linux/arm64` before then. [Node.js image platforms](https://github.com/nodejs/docker-node/blob/main/versions.json) ยท [Node.js release schedule](https://github.com/nodejs/Release/blob/main/schedule.json) +The image keeps npm 11.21.3 on Node.js 24 and npm 10.9.9 on Node.js 22. Both bundles use the SHA-512-verified tar 8.1.2 package and version-specific archive API patches. The patches can be removed separately when the official images for each Node.js line include npm with tar 8 support. [Upstream tar advisory](https://github.com/advisories/GHSA-7c32-79xj-j3jp) + ## Use the image ```sh @@ -33,9 +35,10 @@ From the cloned repository: ```sh docker buildx build --load -t nodejs-python:local . docker run --rm nodejs-python:local bash -ec 'node --version; npm --version; yarn --version; python --version; python -m venv /tmp/venv; /tmp/venv/bin/python -m pip --version' +docker run --rm --network none -i nodejs-python:local sh < .github/scripts/npm-archive-smoke.sh ``` -Set `--platform linux/arm/v7` on both commands to verify the 32-bit ARM image on a machine configured for that architecture or emulation. +Set `--platform linux/arm/v7` on the build and run commands to verify the 32-bit ARM image on a machine configured for that architecture or emulation. ## Publishing diff --git a/patches/npm-10.9.9-tar-8.patch b/patches/npm-10.9.9-tar-8.patch new file mode 100644 index 0000000..e6f9614 --- /dev/null +++ b/patches/npm-10.9.9-tar-8.patch @@ -0,0 +1,449 @@ +--- a/lib/utils/tar.js ++++ b/lib/utils/tar.js +@@ -54,8 +54,9 @@ + let totalEntrySize = 0 + + // reads contents of tarball +- const stream = tar.t({ +- onentry (entry) { ++ await tar.list({ ++ input: tarball, ++ onEntry (entry) { + totalEntries++ + totalEntrySize += entry.size + const p = entry.path +@@ -70,7 +71,6 @@ + }) + }, + }) +- stream.end(tarball) + + const integrity = ssri.fromData(tarball, { + algorithms: ['sha1', 'sha512'], +--- a/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/dir.js ++++ b/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/dir.js +@@ -1,4 +1,5 @@ + const { resolve } = require('node:path') ++const { Readable } = require('node:stream') + const packlist = require('npm-packlist') + const runScript = require('@npmcli/run-script') + const tar = require('tar') +@@ -78,7 +79,7 @@ + } + return packlist(this.tree, { path: this.resolved, prefix, workspaces }) + }) +- .then(files => tar.c(tarCreateOptions(this.package), files) ++ .then(files => Readable.fromWeb(tar.create(tarCreateOptions(this.package), files)) + .on('error', er => stream.emit('error', er)).pipe(stream)) + .catch(er => stream.emit('error', er)) + return stream +--- a/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/fetcher.js ++++ b/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/fetcher.js +@@ -381,28 +381,40 @@ + return this.#toFile(dest) + } + +- #extract (dest, tarball) { +- const extractor = tar.x(this.#tarxOptions({ cwd: dest })) +- const p = new Promise((resolve, reject) => { +- extractor.on('end', () => { +- resolve({ +- resolved: this.resolved, +- integrity: this.integrity && String(this.integrity), +- from: this.from, +- }) ++ async #extract (dest, tarball) { ++ // tar reads the tarball as an async iterable. Detect stream errors between reads. ++ let tarballError ++ const failed = new Promise((_resolve, reject) => { ++ tarball.on('error', er => { ++ tarballError = er ++ reject(er) + }) +- +- extractor.on('error', er => { ++ }) ++ const extracted = tar.extract({ ++ ...this.#tarxOptions({ cwd: dest }), ++ input: tarball, ++ }).catch(er => { ++ if (!tarballError) { + log.warn('tar', er.message) + log.silly('tar', er) +- reject(er) +- }) +- +- tarball.on('error', er => reject(er)) ++ } ++ throw er + }) + +- tarball.pipe(extractor) +- return p ++ await Promise.race([extracted, failed]) ++ // Read through stream end so pacote verifies integrity and caches all input. ++ const drained = (async () => { ++ const chunks = tarball[Symbol.asyncIterator]() ++ while (!(await chunks.next()).done) { ++ // The archive data has already been extracted. ++ } ++ })() ++ await Promise.race([drained, failed]) ++ return { ++ resolved: this.resolved, ++ integrity: this.integrity && String(this.integrity), ++ from: this.from, ++ } + } + + // always ensure that entries are at least as permissive as our configured +@@ -422,7 +434,6 @@ + const sawIgnores = new Set() + return { + cwd, +- noChmod: true, + noMtime: true, + filter: (name, entry) => { + if (/Link$/.test(entry.type)) { +--- a/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/util/tar-create-options.js ++++ b/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/lib/util/tar-create-options.js +@@ -23,6 +23,12 @@ + return true + }, + ++ onWriteEntry: (entry) => { ++ if (isPackageBin(manifest, entry.path)) { ++ entry.mode |= 0o111 ++ } ++ }, ++ + // Provide a specific date in the 1980s for the benefit of zip, + // which is confounded by files dated at the Unix epoch 0. + mtime: new Date('1985-10-26T08:15:00.000Z'), +--- a/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/package.json ++++ b/node_modules/@npmcli/metavuln-calculator/node_modules/pacote/package.json +@@ -61,7 +61,7 @@ + "promise-retry": "^2.0.1", + "sigstore": "^3.0.0", + "ssri": "^12.0.0", +- "tar": "^7.5.10" ++ "tar": "8.1.2" + }, + "engines": { + "node": "^18.17.0 || >=20.5.0" +--- a/node_modules/cacache/package.json ++++ b/node_modules/cacache/package.json +@@ -56,7 +56,7 @@ + "minipass-pipeline": "^1.2.4", + "p-map": "^7.0.2", + "ssri": "^12.0.0", +- "tar": "^7.4.3", ++ "tar": "8.1.2", + "unique-filename": "^4.0.0" + }, + "devDependencies": { +--- a/node_modules/libnpmdiff/lib/tarball.js ++++ b/node_modules/libnpmdiff/lib/tarball.js +@@ -1,4 +1,5 @@ + const { relative } = require('node:path') ++const { buffer } = require('node:stream/consumers') + + const Arborist = require('@npmcli/arborist') + const npa = require('npm-package-arg') +@@ -15,7 +16,7 @@ + files.map(file => relative(manifest._resolved, file)) + ) + .then(files => +- tar.c(tarCreateOptions(manifest), files).concat() ++ buffer(tar.create(tarCreateOptions(manifest), files)) + ) + + const tarball = (manifest, opts) => { +--- a/node_modules/libnpmdiff/lib/untar.js ++++ b/node_modules/libnpmdiff/lib/untar.js +@@ -7,58 +7,32 @@ + + // files and refs are mutating params + // filterFiles, item, prefix and opts are read-only options +-const untar = ({ files, refs }, { filterFiles, item, prefix }) => { +- tar.list({ +- filter: (path, entry) => { +- const fileMatch = () => +- (!filterFiles.length || +- filterFiles.some(f => { +- const pattern = normalizeMatch(f) +- return minimatch( +- normalizeMatch(path), +- `{package/,}${pattern}`, +- { matchBase: pattern.startsWith('*') } +- ) +- })) ++const untar = ({ files, refs }, { filterFiles, item, prefix }) => tar.list({ ++ input: item, ++ async onEntry (entry) { ++ const path = normalizeMatch(entry.path) ++ const matches = !filterFiles.length || filterFiles.some(pattern => { ++ const match = normalizeMatch(pattern) ++ return minimatch(path, `{package/,}${match}`, { matchBase: match.startsWith('*') }) || ++ path.startsWith(match) || path.startsWith(`package/${match}`) ++ }) ++ if (entry.type !== 'File' || !matches) { ++ return ++ } + +- // expands usage of simple path filters, e.g: lib or src/ +- const folderMatch = () => +- filterFiles.some(f => +- normalizeMatch(path).startsWith(normalizeMatch(f)) || +- normalizeMatch(path).startsWith(`package/${normalizeMatch(f)}`)) +- +- if ( +- entry.type === 'File' && +- (fileMatch() || folderMatch()) +- ) { +- const key = path.replace(/^[^/]+\/?/, '') +- files.add(key) +- +- // should skip reading file when using --name-only option +- let content +- try { +- content = entry.concat() +- } catch (e) { +- /* istanbul ignore next */ +- throw Object.assign( +- new Error('failed to read files'), +- { code: 'EDIFFUNTAR' } +- ) +- } +- +- refs.set(`${prefix}${key}`, { +- content, +- mode: `100${entry.mode.toString(8)}`, +- }) +- return true +- } +- }, +- }) +- .on('error', /* istanbul ignore next */ e => { +- throw e +- }) +- .end(item) +-} ++ const key = entry.path.replace(/^[^/]+\/?/, '') ++ files.add(key) ++ try { ++ refs.set(`${prefix}${key}`, { ++ content: Buffer.from(await entry.bytes()).toString('utf8'), ++ mode: `100${entry.mode.toString(8)}`, ++ }) ++ } catch (e) { ++ /* istanbul ignore next */ ++ throw Object.assign(new Error('failed to read files'), { code: 'EDIFFUNTAR' }) ++ } ++ }, ++}) + + const readTarballs = async (tarballs, opts = {}) => { + const files = new Set() +@@ -68,7 +42,7 @@ + const filterFiles = opts.diffFiles || [] + + for (const i of arr) { +- untar({ ++ await untar({ + files, + refs, + }, { +@@ -78,14 +52,6 @@ + }) + } + +- // await to read all content from included files +- const allRefs = [...refs.values()] +- const contents = await Promise.all(allRefs.map(async ref => ref.content)) +- +- contents.forEach((content, index) => { +- allRefs[index].content = content.toString('utf8') +- }) +- + return { + files, + refs, +--- a/node_modules/libnpmdiff/package.json ++++ b/node_modules/libnpmdiff/package.json +@@ -54,7 +54,7 @@ + "minimatch": "^9.0.4", + "npm-package-arg": "^12.0.0", + "pacote": "^19.0.0", +- "tar": "^7.5.11" ++ "tar": "8.1.2" + }, + "templateOSS": { + "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", +--- a/node_modules/node-gyp/lib/install.js ++++ b/node_modules/node-gyp/lib/install.js +@@ -223,21 +223,28 @@ + throw new Error(`${res.status} response downloading ${release.tarballUrl}`) + } + ++ const archive = path.join(tarExtractDir, 'node-headers.tgz') ++ // Hash the full download before opening the archive. + await pipeline( + res.body, +- // content checksum + new ShaSum((_, checksum) => { + const filename = path.basename(release.tarballUrl).trim() + contentShasums[filename] = checksum + log.verbose('content checksum', filename, checksum) + }), +- tar.extract({ ++ createWriteStream(archive) ++ ) ++ try { ++ await tar.extract({ ++ file: archive, + strip: 1, + cwd: tarExtractDir, + filter: isValid, + onwarn + }) +- ) ++ } finally { ++ await fs.rm(archive, { force: true }) ++ } + } catch (err) { + // something went wrong downloading the tarball? + if (err.code === 'ENOTFOUND') { +--- a/node_modules/node-gyp/package.json ++++ b/node_modules/node-gyp/package.json +@@ -29,7 +29,7 @@ + "nopt": "^8.0.0", + "proc-log": "^5.0.0", + "semver": "^7.3.5", +- "tar": "^7.4.3", ++ "tar": "8.1.2", + "tinyglobby": "^0.2.12", + "which": "^5.0.0" + }, +--- a/node_modules/pacote/lib/dir.js ++++ b/node_modules/pacote/lib/dir.js +@@ -1,4 +1,5 @@ + const { resolve } = require('node:path') ++const { Readable } = require('node:stream') + const packlist = require('npm-packlist') + const runScript = require('@npmcli/run-script') + const tar = require('tar') +@@ -75,7 +76,7 @@ + } + return packlist(this.tree, { path: this.resolved, prefix, workspaces }) + }) +- .then(files => tar.c(tarCreateOptions(this.package), files) ++ .then(files => Readable.fromWeb(tar.create(tarCreateOptions(this.package), files)) + .on('error', er => stream.emit('error', er)).pipe(stream)) + .catch(er => stream.emit('error', er)) + return stream +--- a/node_modules/pacote/lib/fetcher.js ++++ b/node_modules/pacote/lib/fetcher.js +@@ -381,28 +381,40 @@ + return this.#toFile(dest) + } + +- #extract (dest, tarball) { +- const extractor = tar.x(this.#tarxOptions({ cwd: dest })) +- const p = new Promise((resolve, reject) => { +- extractor.on('end', () => { +- resolve({ +- resolved: this.resolved, +- integrity: this.integrity && String(this.integrity), +- from: this.from, +- }) ++ async #extract (dest, tarball) { ++ // tar reads the tarball as an async iterable. Detect stream errors between reads. ++ let tarballError ++ const failed = new Promise((_resolve, reject) => { ++ tarball.on('error', er => { ++ tarballError = er ++ reject(er) + }) +- +- extractor.on('error', er => { ++ }) ++ const extracted = tar.extract({ ++ ...this.#tarxOptions({ cwd: dest }), ++ input: tarball, ++ }).catch(er => { ++ if (!tarballError) { + log.warn('tar', er.message) + log.silly('tar', er) +- reject(er) +- }) +- +- tarball.on('error', er => reject(er)) ++ } ++ throw er + }) + +- tarball.pipe(extractor) +- return p ++ await Promise.race([extracted, failed]) ++ // Read through stream end so pacote verifies integrity and caches all input. ++ const drained = (async () => { ++ const chunks = tarball[Symbol.asyncIterator]() ++ while (!(await chunks.next()).done) { ++ // The archive data has already been extracted. ++ } ++ })() ++ await Promise.race([drained, failed]) ++ return { ++ resolved: this.resolved, ++ integrity: this.integrity && String(this.integrity), ++ from: this.from, ++ } + } + + // always ensure that entries are at least as permissive as our configured +@@ -422,7 +434,6 @@ + const sawIgnores = new Set() + return { + cwd, +- noChmod: true, + noMtime: true, + filter: (name, entry) => { + if (/Link$/.test(entry.type)) { +--- a/node_modules/pacote/lib/util/tar-create-options.js ++++ b/node_modules/pacote/lib/util/tar-create-options.js +@@ -23,6 +23,12 @@ + return true + }, + ++ onWriteEntry: (entry) => { ++ if (isPackageBin(manifest, entry.path)) { ++ entry.mode |= 0o111 ++ } ++ }, ++ + // Provide a specific date in the 1980s for the benefit of zip, + // which is confounded by files dated at the Unix epoch 0. + mtime: new Date('1985-10-26T08:15:00.000Z'), +--- a/node_modules/pacote/package.json ++++ b/node_modules/pacote/package.json +@@ -61,7 +61,7 @@ + "promise-retry": "^2.0.1", + "sigstore": "^3.0.0", + "ssri": "^12.0.0", +- "tar": "^7.5.10" ++ "tar": "8.1.2" + }, + "engines": { + "node": "^18.17.0 || >=20.5.0" +--- a/package.json ++++ b/package.json +@@ -112,7 +112,7 @@ + "spdx-expression-parse": "^4.0.0", + "ssri": "^12.0.0", + "supports-color": "^9.4.0", +- "tar": "^7.5.22", ++ "tar": "8.1.2", + "text-table": "~0.2.0", + "tiny-relative-date": "^1.3.0", + "treeverse": "^3.0.0", diff --git a/patches/npm-11.21.3-tar-8.patch b/patches/npm-11.21.3-tar-8.patch new file mode 100644 index 0000000..86d4b84 --- /dev/null +++ b/patches/npm-11.21.3-tar-8.patch @@ -0,0 +1,356 @@ +--- a/lib/commands/stage/download.js ++++ b/lib/commands/stage/download.js +@@ -45,21 +45,14 @@ + + async #readManifestFromTarball (tarballData) { + let manifestJson +- const stream = tar.t({ +- onentry (entry) { ++ await tar.list({ ++ input: tarballData, ++ async onEntry (entry) { + if (entry.path === 'package/package.json') { +- const chunks = [] +- entry.on('data', c => chunks.push(c)) +- entry.on('end', () => { +- manifestJson = JSON.parse(Buffer.concat(chunks).toString()) +- }) +- } else { +- entry.resume() ++ manifestJson = JSON.parse(Buffer.from(await entry.bytes()).toString('utf8')) + } + }, + }) +- // node-tar uses Minipass which processes synchronously on .end() +- stream.end(tarballData) + if (!manifestJson) { + throw new Error('Could not read package.json from tarball') + } +--- a/lib/utils/tar.js ++++ b/lib/utils/tar.js +@@ -54,8 +54,9 @@ + let totalEntrySize = 0 + + // reads contents of tarball +- const stream = tar.t({ +- onentry (entry) { ++ await tar.list({ ++ input: tarball, ++ onEntry (entry) { + totalEntries++ + totalEntrySize += entry.size + const p = entry.path +@@ -70,7 +71,6 @@ + }) + }, + }) +- stream.end(tarball) + + const integrity = ssri.fromData(tarball, { + algorithms: ['sha1', 'sha512'], +--- a/node_modules/libnpmdiff/lib/tarball.js ++++ b/node_modules/libnpmdiff/lib/tarball.js +@@ -1,4 +1,5 @@ + const { relative } = require('node:path') ++const { buffer } = require('node:stream/consumers') + + const Arborist = require('@npmcli/arborist') + const npa = require('npm-package-arg') +@@ -15,7 +16,7 @@ + files.map(file => relative(manifest._resolved, file)) + ) + .then(files => +- tar.c(tarCreateOptions(manifest), files).concat() ++ buffer(tar.create(tarCreateOptions(manifest), files)) + ) + + const tarball = (manifest, opts) => { +--- a/node_modules/libnpmdiff/lib/untar.js ++++ b/node_modules/libnpmdiff/lib/untar.js +@@ -7,58 +7,32 @@ + + // files and refs are mutating params + // filterFiles, item, prefix and opts are read-only options +-const untar = ({ files, refs }, { filterFiles, item, prefix }) => { +- tar.list({ +- filter: (path, entry) => { +- const fileMatch = () => +- (!filterFiles.length || +- filterFiles.some(f => { +- const pattern = normalizeMatch(f) +- return minimatch( +- normalizeMatch(path), +- `{package/,}${pattern}`, +- { matchBase: pattern.startsWith('*') } +- ) +- })) ++const untar = ({ files, refs }, { filterFiles, item, prefix }) => tar.list({ ++ input: item, ++ async onEntry (entry) { ++ const path = normalizeMatch(entry.path) ++ const matches = !filterFiles.length || filterFiles.some(pattern => { ++ const match = normalizeMatch(pattern) ++ return minimatch(path, `{package/,}${match}`, { matchBase: match.startsWith('*') }) || ++ path.startsWith(match) || path.startsWith(`package/${match}`) ++ }) ++ if (entry.type !== 'File' || !matches) { ++ return ++ } + +- // expands usage of simple path filters, e.g: lib or src/ +- const folderMatch = () => +- filterFiles.some(f => +- normalizeMatch(path).startsWith(normalizeMatch(f)) || +- normalizeMatch(path).startsWith(`package/${normalizeMatch(f)}`)) +- +- if ( +- entry.type === 'File' && +- (fileMatch() || folderMatch()) +- ) { +- const key = path.replace(/^[^/]+\/?/, '') +- files.add(key) +- +- // should skip reading file when using --name-only option +- let content +- try { +- content = entry.concat() +- } catch (e) { +- /* istanbul ignore next */ +- throw Object.assign( +- new Error('failed to read files'), +- { code: 'EDIFFUNTAR' } +- ) +- } +- +- refs.set(`${prefix}${key}`, { +- content, +- mode: `100${entry.mode.toString(8)}`, +- }) +- return true +- } +- }, +- }) +- .on('error', /* istanbul ignore next */ e => { +- throw e +- }) +- .end(item) +-} ++ const key = entry.path.replace(/^[^/]+\/?/, '') ++ files.add(key) ++ try { ++ refs.set(`${prefix}${key}`, { ++ content: Buffer.from(await entry.bytes()).toString('utf8'), ++ mode: `100${entry.mode.toString(8)}`, ++ }) ++ } catch (e) { ++ /* istanbul ignore next */ ++ throw Object.assign(new Error('failed to read files'), { code: 'EDIFFUNTAR' }) ++ } ++ }, ++}) + + const readTarballs = async (tarballs, opts = {}) => { + const files = new Set() +@@ -68,7 +42,7 @@ + const filterFiles = opts.diffFiles || [] + + for (const i of arr) { +- untar({ ++ await untar({ + files, + refs, + }, { +@@ -78,15 +52,6 @@ + }) + } + +- // await to read all content from included files +- // TODO this feels like it could be one in one pass instead of three (values, map, forEach) +- const allRefs = [...refs.values()] +- const contents = await Promise.all(allRefs.map(async ref => ref.content)) +- +- contents.forEach((content, index) => { +- allRefs[index].content = content.toString('utf8') +- }) +- + return { + files, + refs, +--- a/node_modules/libnpmdiff/package.json ++++ b/node_modules/libnpmdiff/package.json +@@ -54,7 +54,7 @@ + "minimatch": "^10.0.3", + "npm-package-arg": "^13.0.0", + "pacote": "^21.0.2", +- "tar": "^7.5.1" ++ "tar": "8.1.2" + }, + "templateOSS": { + "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", +--- a/node_modules/node-gyp/lib/install.js ++++ b/node_modules/node-gyp/lib/install.js +@@ -223,21 +223,28 @@ + throw new Error(`${res.status} response downloading ${release.tarballUrl}`) + } + ++ const archive = path.join(tarExtractDir, 'node-headers.tgz') ++ // Hash the full download before opening the archive. + await pipeline( + res.body, +- // content checksum + new ShaSum((_, checksum) => { + const filename = path.basename(release.tarballUrl).trim() + contentShasums[filename] = checksum + log.verbose('content checksum', filename, checksum) + }), +- tar.extract({ ++ createWriteStream(archive) ++ ) ++ try { ++ await tar.extract({ ++ file: archive, + strip: 1, + cwd: tarExtractDir, + filter: isValid, + onwarn + }) +- ) ++ } finally { ++ await fs.rm(archive, { force: true }) ++ } + } catch (err) { + // something went wrong downloading the tarball? + if (err.code === 'ENOTFOUND') { +--- a/node_modules/node-gyp/package.json ++++ b/node_modules/node-gyp/package.json +@@ -28,7 +28,7 @@ + "nopt": "^9.0.0", + "proc-log": "^6.0.0", + "semver": "^7.3.5", +- "tar": "^7.5.4", ++ "tar": "8.1.2", + "tinyglobby": "^0.2.12", + "undici": "^6.25.0", + "which": "^6.0.0" +--- a/node_modules/pacote/lib/dir.js ++++ b/node_modules/pacote/lib/dir.js +@@ -1,4 +1,5 @@ + const { resolve } = require('node:path') ++const { Readable } = require('node:stream') + const packlist = require('npm-packlist') + const runScript = require('@npmcli/run-script') + const tar = require('tar') +@@ -78,7 +79,7 @@ + } + return packlist(this.tree, { path: this.resolved, prefix, workspaces }) + }) +- .then(files => tar.c(tarCreateOptions(this.package), files) ++ .then(files => Readable.fromWeb(tar.create(tarCreateOptions(this.package), files)) + .on('error', er => stream.emit('error', er)).pipe(stream)) + .catch(er => stream.emit('error', er)) + return stream +--- a/node_modules/pacote/lib/fetcher.js ++++ b/node_modules/pacote/lib/fetcher.js +@@ -381,28 +381,40 @@ + return this.#toFile(dest) + } + +- #extract (dest, tarball) { +- const extractor = tar.x(this.#tarxOptions({ cwd: dest })) +- const p = new Promise((resolve, reject) => { +- extractor.on('end', () => { +- resolve({ +- resolved: this.resolved, +- integrity: this.integrity && String(this.integrity), +- from: this.from, +- }) ++ async #extract (dest, tarball) { ++ // tar reads the tarball as an async iterable. Detect stream errors between reads. ++ let tarballError ++ const failed = new Promise((_resolve, reject) => { ++ tarball.on('error', er => { ++ tarballError = er ++ reject(er) + }) +- +- extractor.on('error', er => { ++ }) ++ const extracted = tar.extract({ ++ ...this.#tarxOptions({ cwd: dest }), ++ input: tarball, ++ }).catch(er => { ++ if (!tarballError) { + log.warn('tar', er.message) + log.silly('tar', er) +- reject(er) +- }) +- +- tarball.on('error', er => reject(er)) ++ } ++ throw er + }) + +- tarball.pipe(extractor) +- return p ++ await Promise.race([extracted, failed]) ++ // Read through stream end so pacote verifies integrity and caches all input. ++ const drained = (async () => { ++ const chunks = tarball[Symbol.asyncIterator]() ++ while (!(await chunks.next()).done) { ++ // The archive data has already been extracted. ++ } ++ })() ++ await Promise.race([drained, failed]) ++ return { ++ resolved: this.resolved, ++ integrity: this.integrity && String(this.integrity), ++ from: this.from, ++ } + } + + // always ensure that entries are at least as permissive as our configured +@@ -422,7 +434,6 @@ + const sawIgnores = new Set() + return { + cwd, +- noChmod: true, + noMtime: true, + filter: (name, entry) => { + if (/Link$/.test(entry.type)) { +--- a/node_modules/pacote/lib/util/tar-create-options.js ++++ b/node_modules/pacote/lib/util/tar-create-options.js +@@ -23,6 +23,12 @@ + return true + }, + ++ onWriteEntry: (entry) => { ++ if (isPackageBin(manifest, entry.path)) { ++ entry.mode |= 0o111 ++ } ++ }, ++ + // Provide a specific date in the 1980s for the benefit of zip, + // which is confounded by files dated at the Unix epoch 0. + mtime: new Date('1985-10-26T08:15:00.000Z'), +--- a/node_modules/pacote/package.json ++++ b/node_modules/pacote/package.json +@@ -62,7 +62,7 @@ + "proc-log": "^6.0.0", + "sigstore": "^4.0.0", + "ssri": "^13.0.0", +- "tar": "^7.4.3" ++ "tar": "8.1.2" + }, + "engines": { + "node": "^20.17.0 || >=22.9.0" +--- a/package.json ++++ b/package.json +@@ -110,7 +110,7 @@ + "spdx-expression-parse": "^4.0.0", + "ssri": "^13.0.1", + "supports-color": "^10.2.2", +- "tar": "^7.5.22", ++ "tar": "8.1.2", + "text-table": "~0.2.0", + "tiny-relative-date": "^2.0.2", + "treeverse": "^3.0.0",