Skip to content

zlib: add tar archive support - #67975

Open
mcollina wants to merge 4 commits into
nodejs:mainfrom
mcollina:zlib-tar
Open

mcollina wants to merge 4 commits into
nodejs:mainfrom
mcollina:zlib-tar

Conversation

@mcollina

@mcollina mcollina commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Adds experimental tar support to node:zlib, next to the ZIP API: reading (listTar(), extractTar(), createTarExtractStream(), tarEntries(), TarParser) and writing (TarEntry.create*(), createTarArchive(), tarFiles()), with sync variants.

tar is the basis of npm: every package is a tarball, so every install unpacks them and every publish creates one. Having it in core benefits the whole ecosystem.

The API is compatible with the one of node-tar: the extraction options (cwd, strip, filter, preservePaths, preserveOwner, onwarn, ...) and the entry fields and type names are the same.


AI generated, reviewed by me

Add an experimental tar reader to node:zlib, ported from fast-tar:
listTar(), extractTar(), createTarExtractStream(), tarEntries(), their
synchronous variants, and the TarParser they are built on.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Add TarEntry.create(), createStream(), createSymlink() and createLink(),
and createTarArchive(), createTarArchiveSync() and tarFiles() to write
them, mirroring the ZIP creation API.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originating in Node.js core. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies. labels Dec 2, 2026
@MikeMcC399

This comment was marked as resolved.

@mcollina

mcollina commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Not public, and it does not need to be, sorry for the mistake.

@mcollina
mcollina requested a review from pipobscure December 2, 2026 19:23
@mcollina
mcollina marked this pull request as ready for review December 2, 2026 19:23
@mcollina

mcollina commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

@nodejs/tsc this is a big PR and it needs your approval.

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.94275% with 328 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.63%. Comparing base (40c40fa) to head (aea6873).
⚠️ Report is 96 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/tar/unpack.js 85.59% 127 Missing and 8 partials ⚠️
lib/internal/tar/source.js 90.86% 57 Missing and 6 partials ⚠️
lib/internal/tar/unpack-sync.js 88.40% 37 Missing ⚠️
lib/internal/tar/archive.js 95.98% 30 Missing and 3 partials ⚠️
lib/internal/tar/entries.js 92.30% 23 Missing and 1 partial ⚠️
lib/internal/tar/header.js 97.22% 10 Missing ⚠️
lib/internal/tar/unpack-common.js 96.87% 8 Missing ⚠️
lib/internal/tar/pax.js 97.31% 4 Missing ⚠️
lib/internal/tar/entry.js 98.57% 2 Missing and 1 partial ⚠️
lib/internal/tar/paths.js 98.05% 2 Missing and 1 partial ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67975      +/-   ##
==========================================
+ Coverage   90.53%   90.63%   +0.10%     
==========================================
  Files         805      817      +12     
  Lines      285915   292360    +6445     
  Branches    55213    56855    +1642     
==========================================
+ Hits       258841   264981    +6140     
- Misses      17186    17459     +273     
- Partials     9888     9920      +32     
Files with missing lines Coverage Δ
lib/internal/errors.js 98.42% <100.00%> (+0.29%) ⬆️
lib/internal/tar.js 100.00% <100.00%> (ø)
lib/zlib.js 98.49% <100.00%> (+0.07%) ⬆️
lib/internal/tar/api.js 99.68% <99.68%> (ø)
lib/internal/tar/decompress.js 99.00% <99.00%> (ø)
lib/internal/tar/parser.js 99.60% <99.60%> (ø)
lib/internal/tar/entry.js 98.57% <98.57%> (ø)
lib/internal/tar/paths.js 98.05% <98.05%> (ø)
lib/internal/tar/pool.js 95.65% <95.65%> (ø)
lib/internal/tar/pax.js 97.31% <97.31%> (ø)
... and 7 more

... and 106 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aduh95 aduh95 added the large-pr PRs subject to the large-PR policy. label Dec 2, 2026
@bakkot

bakkot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Have you read https://astral.sh/blog/tar-codec? It's a good overview of some of the pitfalls with tar parsing, and they have tests you could borrow.

There's also this other large test suite. It recommends a policy of rejecting ambiguities in tar files that could make implementations extract different files, which I'd love to see node follow.

(Alternatively, you could compile tar-codec to wasm and use that, instead of writing and maintaining your own. That approach has much to recommend it.)

Edit: also I note that you added support for extracting to disk, which the zip support deliberately omitted. Nothing inherently wrong with that but a little odd for them to be asymmetric, and there's many more pitfalls when extracting rather than merely reading. I like the factoring in tar-codec, where there's a generic interface for archives and then a separate consumer of that interface for extracting; with that factoring you could make both tar and zip extract using the same code.

Small files are read whole, several at a time, in input order. The
compression option writes independent gzip members compressed on the
thread pool.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Directories have no body, a file whose path ends with a slash is a
directory, numeric fields end at the first invalid byte, the last of
a GNU long name and a pax path wins, global headers do not set paths,
and entries without a path or with a misplaced link path are dropped.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Comment thread doc/api/zlib.md
@@ -1009,6 +1009,484 @@ changes:

Decompress a raw deflate stream.

## Class: `zlib.TarEntry`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if it was a mistake not to go with zlib/zip, zlib/tar, etc

Comment thread doc/api/zlib.md
[`zlib.tarEntries()`][]. The static methods of this class create the entries
that [`zlib.createTarArchive()`][] writes.

The v7, ustar, pax and GNU formats are understood. Pax extended headers and GNU

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helpful if these were links

Comment thread doc/api/zlib.md Resolved
Comment thread doc/api/zlib.md
string, decoded with `encoding`. **Default:** `'utf8'`.
* `skip()` Returns: {Promise} Fulfills once the rest of the content has been
skipped.
* `done` {boolean} `true` once the content has been read, or skipped, to its

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels.... odd

@jasnell jasnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there's tons here that can be refined and I haven't gone through everything, but it's a start and as long as there are some good refinement passes before it graduates, LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

errors Issues and PRs related to JavaScript errors originating in Node.js core. large-pr PRs subject to the large-PR policy. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants