# tar Tar for JavaScript. This package reads and writes tar archives. It makes archives from files or from data in memory. It extracts archives to a file system, or gives you the entries one at a time. It reads and writes gzip, and in a server-side runtime also brotli and zstd. The package has two entry points: - `tar` is for server-side runtimes: Node.js, and each other runtime that has the `node:` modules, such as Bun, Deno, and Cloudflare. It uses `node:fs` and `node:zlib`, and it has sync commands. Most users want this entry point. - `tar/core` is for browsers and each other runtime. It uses no `node:` module. It runs in a browser, in a worker, and in each runtime that has web streams. It needs the `fs` option for work on files. The package has no runtime dependencies. The API uses promises, web streams, and `Uint8Array`. ## Install ```sh npm install tar ``` `tar` needs Node.js 22 or later, or another server-side runtime that has the `node:` modules. `tar/core` also runs in a browser. The package has ES module and CommonJS builds. You can use `import` or `require()`. ## Quick start These examples use `tar`, the server-side entry point. A command has two arguments: an options object, and for most commands a list of files. ### Create an archive ```js import { create } from 'tar' // A gzip archive of a directory and a file. await create({ file: 'demo.tgz', gzip: true }, ['src', 'package.json']) ``` With no `file` option, `create()` returns a `ReadableStream` of the bytes of the archive. ```js import { createWriteStream } from 'node:fs' import { Writable } from 'node:stream' import { create } from 'tar' const stream = create({ gzip: true }, ['src', 'package.json']) await stream.pipeTo(Writable.toWeb(createWriteStream('demo.tgz'))) ``` ### Extract an archive The directory to extract into must exist. The default is the current working directory. ```js import { mkdir } from 'node:fs/promises' import { extract } from 'tar' await mkdir('out', { recursive: true }) await extract({ file: 'archive.tgz', cwd: 'out' }) // Only some entries, with the first part of each path removed. await extract({ file: 'archive.tgz', cwd: 'out', strip: 1 }, ['src/lib']) ``` The `input` option gives the archive as data. It can be a `Uint8Array`, an `ArrayBuffer`, a `Blob`, a `ReadableStream`, or an iterable of `Uint8Array` chunks. ```js import { mkdir } from 'node:fs/promises' import { extract } from 'tar' const res = await fetch('https://example.com/archive.tgz') await mkdir('out', { recursive: true }) await extract({ input: res.body, cwd: 'out' }) ``` ### List the entries ```js import { list } from 'tar' await list({ file: 'archive.tgz', onEntry: entry => console.log(entry.type, entry.path, entry.size), }) ``` To read the content of the entries, use `entries()`. It is an async iterator. ```js import { readFile } from 'node:fs/promises' import { entries } from 'tar' for await (const entry of entries(await readFile('archive.tgz'))) { if (entry.path === 'package.json') { console.log(JSON.parse(await entry.text())) } } ``` ### Add files to an archive `replace()` adds entries to the end of an archive that is not compressed. `update()` does the same, but it adds a file only if it is newer than the entry of the same path in the archive. ```js import { replace, update } from 'tar' await replace({ file: 'archive.tar' }, ['notes.txt']) await update({ file: 'archive.tar' }, ['src', 'package.json']) ``` ### Sync commands Each command of `tar` has a sync form. A sync command returns when the work is done. `tar/core` has no sync commands. ```js import { mkdirSync } from 'node:fs' import { createSync, extractSync, listSync, deleteSync, replaceSync, updateSync, } from 'tar' createSync({ file: 'demo.tar' }, ['src']) replaceSync({ file: 'demo.tar' }, ['package.json']) updateSync({ file: 'demo.tar' }, ['README.md']) deleteSync({ file: 'demo.tar' }, ['package.json']) listSync({ file: 'demo.tar', onEntry: entry => console.log(entry.path), }) mkdirSync('out', { recursive: true }) extractSync({ file: 'demo.tar', cwd: 'out' }) ``` With no `file` option, `createSync()` returns an iterator of `Uint8Array` chunks. ## Entry points | Import | Use | | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `tar` | Server-side runtimes: Node.js, and each other runtime that has the `node:` modules (Bun, Deno, Cloudflare). All of the core, the defaults below, the sync commands, and the `node:zlib` codecs. | | `tar/core` | Browsers, and each other runtime. The core: no `node:` module, no `process`, no `Buffer`. The async commands and the classes. It needs the `fs` option for work on files. | | `tar/raw`, `tar/core/raw` | The same two entry points as separate modules that are not minified. | `tar` and `tar/core` are each one minified file. ### The defaults of `tar` Each command of `tar` calls the command of the core with these defaults: | Option | Default in `tar` | | -------------- | ---------------------------------------------------------------- | | `fs` | `node:fs/promises`. `node:fs` for a sync command. | | `cwd` | `process.cwd()`. A relative `cwd` is resolved from it. | | `platform` | `'win32'` if `process.platform` is `'win32'`. If not, `'posix'`. | | `processUid` | `process.getuid()` | | `processGid` | `process.getgid()` | | `processUname` | `process.env.USER` | | `processUmask` | `process.umask()`, only when `chmod` is set | | `codecs` | Your codecs, then gzip, brotli, and zstd from `node:zlib` | `list()` and `entries()` write no file. They get only the `fs`, `platform`, and `codecs` defaults. ### The core in a browser or another runtime `tar/core` is the core. It does not know the file system, the working directory, the platform, or the user of the process. You give them as options. The core needs these globals: `ReadableStream`, `TransformStream`, `Blob`, `TextEncoder`, `TextDecoder`, and `crypto.getRandomValues`. It needs `CompressionStream` and `DecompressionStream` for gzip. A command of the core works with no `fs` option, if it does not touch a file: - `entries(input)` and `list({ input })` read archive data. - `create()` with no `file` option, and with entries from memory, returns a stream. This example reads an archive from a `fetch()` response. It stops the download when it has the entry. ```js import { entries } from 'tar/core' const res = await fetch('https://example.com/archive.tgz') for await (const entry of entries(res.body)) { if (entry.path === 'package.json') { console.log(JSON.parse(await entry.text())) break } } ``` This example makes a gzip archive in memory, and reads it from a `Blob`. A `File` from a file input is also a `Blob`. ```js import { create, entries, WriteEntry } from 'tar/core' const stream = create({ gzip: true }, [ new WriteEntry({ path: 'hello.txt' }, 'hello\n'), new WriteEntry({ path: 'data.bin' }, new Uint8Array([1, 2, 3])), ]) const blob = await new Response(stream).blob() for await (const entry of entries(blob)) { console.log(entry.path, entry.size) } ``` #### The `input` option `list()` and `extract()` read the archive from `input`, or from the path `file` through `fs`. `entries()` takes the input as its first argument. The input can be: - a `Uint8Array` or an `ArrayBuffer` - a `Blob` - a `ReadableStream` - an async iterable or an iterable of `Uint8Array` chunks A sync command takes a `Uint8Array`, an `ArrayBuffer`, or an iterable of `Uint8Array` chunks. The readers do not copy the input. Do not change a chunk after you give it. When `input` is set, `file` is only the name of the archive. Warnings have it, and the readers use it to find a format by the file name. #### The `fs` option The `fs` option is an object with file system functions. Each function has the signature of the function of the same name in `node:fs/promises`. For a sync command, it is the function of `node:fs`. A command calls only the functions in its type. | Type | Commands | Functions | | ----------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `TarFS` | `list()` and `extract()` with `file`, and an `@archive` operand | `open(path, 'r')`. The handle has `read`, `stat`, and `close`. | | `ExtractFS` | `extract()` | `open`, `stat`, `lstat`, `mkdir`, `chmod`, `chown`, `lchown`, `utimes`, `lutimes`, `rmdir`, `unlink`, `rename`, `link`, `symlink`. The handle has `write`, `utimes`, `chown`, and `close`. `constants` is optional. | | `CreateFS` | `create()` | `open`, `lstat`, `stat`, `readdir`, `readlink`. The handle has `read`, `write`, `stat`, and `close`. `lstat`, `stat`, and `stat` of the handle can get the option `{ bigint: true }`. | | `ReplaceFS` | `replace()`, `update()` | The same as `CreateFS`. `open` gets the flags `'r+'`, `'w+'`, and `'r'`. | | `DeleteFS` | `delete()` | `TarFS` and `CreateFS`, and `rename` and `unlink`. `chmod` is optional. | The sync types are `TarFSSync`, `ExtractFSSync`, `CreateFSSync`, `ReplaceFSSync`, and `DeleteFSSync`. They have the functions of `node:fs` that work on a file descriptor: `openSync`, `fstatSync`, `readSync`, `writeSync`, `futimesSync`, `fchownSync`, `closeSync`, and the sync forms of the path functions above. Notes for an `ExtractFS`: - `mkdir` must fail if the path exists. `lstat` and `stat` must fail if the path does not exist. - If `fs.constants` has `O_CREAT`, `O_TRUNC`, `O_WRONLY`, and `O_NOFOLLOW`, then `open` gets a number as its flag. With `O_NOFOLLOW`, `open` does not follow a symbolic link at the path. Without `constants`, the flag is `'w'`. - `chown` and `lchown` are called only when the options set an owner. `chmod` is called only with the `chmod` option. `rename` is called only with `platform: 'win32'`. Notes for a `CreateFS`: - `create()` knows a file by the `dev` and `ino` numbers of its stat. Node.js gives the two values as numbers. On Windows, most `ino` values are too large for a number, and then two files can have the same number. - With the `bigint` option, each call of `lstat`, `stat`, and `stat` of the handle has the option `{ bigint: true }`. `lstat` and `stat` must then give all the fields of `CreateBigIntStats`, or ignore the option. `create()` makes no other call for exact values. The notes below are for a `create()` without the option. - A wrong match loses data in two cases: a file with more than one link is written as a hard link to a different file, and with `follow`, a directory is skipped as a directory in itself. Only in these cases, and only when `dev` or `ino` is not a safe integer, `create()` calls `lstat` (`stat` with `follow`) one more time with the option `{ bigint: true }`, and reads the `dev` and `ino` of the result. `node:fs` gives exact `bigint` values with this option. - A file with one link, and a directory without `follow`, never get that call before they are read. - Without `follow`, `create()` checks that a path is the same object after `open` and after `readdir`. For a directory, it calls `lstat` again, and compares the type and the numbers. A different directory with the same numbers is not found. For a file, it compares the numbers of the handle. When they are not safe integers, it also calls `stat` of the handle, and `lstat` if it does not have the exact values, with the option `{ bigint: true }`, and compares the exact values. - Your function can ignore the option and return the stat with numbers. `create()` then uses the numbers. Two files with the same `dev` and `ino` numbers, and more than one link, are then one file and a hard link to it. This example extracts an archive into a `Map`. ```js import { create, extract, WriteEntry } from 'tar/core' // path -> { type, mode, mtime, chunks, target } const tree = new Map([ ['/out', { type: 'dir', mode: 0o755, mtime: new Date() }], ]) const fail = (code, path) => Object.assign(new Error(`${code}: ${path}`), { code }) const get = path => { const node = tree.get(path) if (!node) throw fail('ENOENT', path) return node } const stats = node => ({ isDirectory: () => node.type === 'dir', isFile: () => node.type === 'file', isSymbolicLink: () => node.type === 'symlink', nlink: 1, mode: node.mode, mtime: node.mtime, }) const setTime = async (path, atime, mtime) => { get(path).mtime = mtime } const memfs = { stat: async path => stats(get(path)), lstat: async path => stats(get(path)), mkdir: async (path, { mode }) => { if (tree.has(path)) throw fail('EEXIST', path) tree.set(path, { type: 'dir', mode, mtime: new Date() }) return undefined }, open: async (path, flags, mode) => { const file = { type: 'file', mode, mtime: new Date(), chunks: [] } tree.set(path, file) return { // `data` is a view into the archive data. Keep a copy. write: async data => file.chunks.push(data.slice()), utimes: async (atime, mtime) => { file.mtime = mtime }, chown: async () => {}, close: async () => {}, } }, symlink: async (target, path) => { tree.set(path, { type: 'symlink', mode: 0o777, target }) }, link: async (target, path) => { tree.set(path, get(target)) }, unlink: async path => { tree.delete(path) }, rmdir: async path => { tree.delete(path) }, rename: async (from, to) => { tree.set(to, get(from)) tree.delete(from) }, chmod: async (path, mode) => { get(path).mode = mode }, chown: async () => {}, lchown: async () => {}, utimes: setTime, lutimes: setTime, } const archive = create({}, [ new WriteEntry({ path: 'docs/', type: 'Directory', mode: 0o755 }), new WriteEntry({ path: 'docs/hello.txt', mode: 0o644 }, 'hello\n'), new WriteEntry({ path: 'docs/deep/more.txt' }, 'more\n'), ]) await extract({ input: archive, fs: memfs, cwd: '/out' }) for (const [path, node] of tree) console.log(node.type, path) console.log(await new Blob(tree.get('/out/docs/hello.txt').chunks).text()) ``` #### Electron and `asar` files In Electron, the `fs` module is changed: a file with a name that ends in `.asar` is a directory. With that module, `extract()` does not write an `app.asar` entry as a file, and `create()` reads the paths in the `asar` file and not the file. Electron has the module `original-fs`, which is `node:fs` with no change. Give it as the `fs` option. An async command needs the functions that return promises: `originalFs.promises`. A sync command needs `originalFs`. This example is for the main process of Electron. It is not tested, because the tests of this package do not run in Electron. ```javascript import originalFs from 'original-fs' import { create, extract, extractSync } from 'tar' await create({ file: 'app.tgz', gzip: true, fs: originalFs.promises }, [ 'MyApp.app', ]) await extract({ file: 'app.tgz', cwd: 'out', fs: originalFs.promises }) extractSync({ file: 'app.tgz', cwd: 'out', fs: originalFs }) ``` #### The process options - `cwd` The directory that `extract()` writes into, and the directory that the paths of `create()` are relative to. In `tar/core` it must be an absolute path, by the rules of `platform`. A path that is not absolute is a `TypeError`. `extract()` needs it. `create()` needs it only to add a path. - `platform` `'posix'` (default) or `'win32'`. With `'win32'`: each `\` in a path is a separator, and the path rules of Windows apply. `extract()` changes the characters `<|>?:` in a name to private-use characters, renames a file before it removes it, and extracts one entry at a time. `create()` changes those private-use characters back. - `processUid`, `processGid` The user ID and the group ID of the process. `extract()` uses them to know if it must set an owner. If `processUid` is 0, the default of `preserveOwner` is true. - `processUname` `create()` writes it as the `uname` of each file that `processUid` owns. - `processUmask` The umask of the process. `extract()` uses it only with `chmod`. Default `0o22`. ## Entries ### `ReadEntry` `entries()`, `onEntry`, `filter`, and `transform` give you a `ReadEntry`. It has the shape of a `Response`: the metadata is in the fields, and the content is the body. | Field | Value | | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `path` | The path in the archive. | | `type` | `'File'`, `'Directory'`, `'SymbolicLink'`, `'Link'` (a hard link), `'CharacterDevice'`, `'BlockDevice'`, `'FIFO'`, `'OldFile'`, `'ContiguousFile'`, `'GNUDumpDir'`, or `'SparseFile'`. | | `size` | The number of bytes in the body. | | `mode` | The permission bits. | | `mtime`, `atime`, `ctime` | `Date` objects. | | `uid`, `gid`, `uname`, `gname` | The owner. | | `linkpath` | The target of a link. | | `devmaj`, `devmin` | The device numbers. | | `dev`, `ino`, `nlink` | Set only if a PAX header has them. | | `header` | The `Header` object of the entry. | | `extended`, `globalExtended` | The PAX headers that apply to the entry. | | `absolute` | The absolute path. `extract()` sets it. | | `bodyUsed` | True when the body started to be read. | A field is `undefined` if the archive does not have it. The values of PAX headers and GNU long name headers are applied. Read the body with one of these: - `entry.body` A `ReadableStream`. - `await entry.bytes()` A new `Uint8Array`. - `await entry.arrayBuffer()` A new `ArrayBuffer`. - `await entry.text()` A string, decoded as UTF-8. As with `Response.text()`, a byte order mark at the start is not in the string. - `await entry.blob()` A new `Blob`. Rules for the body: - You can read the body one time, in one way. A second read throws a `TypeError`. - Read the body before you ask for the next entry. When the reader moves to the next entry, it skips the rest of the body. After that, a read throws a `TypeError`. - You do not have to read the body. - A chunk of `entry.body` is a view into the input. It is not a copy. The readers get input only when you ask for an entry or for more of a body. ```js import { readFile } from 'node:fs/promises' import { entries } from 'tar' for await (const entry of entries(await readFile('archive.tgz'))) { if (entry.type !== 'File') continue if (entry.path.endsWith('.json')) { console.log(entry.path, JSON.parse(await entry.text())) } else { let bytes = 0 for await (const chunk of entry.body) bytes += chunk.length console.log(entry.path, bytes) } } ``` ### `onEntry` is awaited `list()` and `extract()` call `onEntry` with each entry. If it returns a promise, the command waits for it before it reads the next entry. In `list()`, you can read the body in `onEntry`. ```js import { list } from 'tar' await list({ file: 'archive.tgz', onEntry: async entry => { if (entry.path === 'README.md') console.log(await entry.text()) }, }) ``` In `extract()`, `onEntry` gets each entry before the checks and before the entry is written. If `onEntry` reads the body, the entry is not extracted, because its content is used. ### `ReadEntrySync` The sync commands give you a `ReadEntrySync`. It has the same fields. `bytes()`, `arrayBuffer()`, and `text()` return the value, not a promise. `body` is an iterator of `Uint8Array` chunks. There is no `blob()`. ```js import { readFileSync } from 'node:fs' import { entriesSync } from 'tar' for (const entry of entriesSync(readFileSync('archive.tgz'))) { if (entry.path === 'package.json') console.log(entry.text()) } ``` ### `WriteEntry` A `WriteEntry` is an entry to write. It has the shape of a `Request`: the metadata is in the fields, and the content is `body`. ```js import { WriteEntry } from 'tar' const entry = new WriteEntry( { path: 'hello.txt', mode: 0o644, mtime: new Date() }, 'hello\n', ) console.log(entry.type, entry.size) ``` `new WriteEntry(data, body)`: - `data.path` is required. - `data.type` Default `'File'`. - `data.mode`, `uid`, `gid`, `uname`, `gname`, `mtime`, `atime`, `ctime`, `linkpath`, `dev`, `ino`, `nlink` are optional. - `data.size` The number of bytes in the body. The default is the length of a body that is a string, a `Uint8Array`, or a `Blob`. - `body` A string, a `Uint8Array`, a `Blob`, a `ReadableStream`, or an iterable or async iterable of `Uint8Array` chunks. A string is written as UTF-8. Set `data.size` for a body that is a stream or an iterable. Without it, the constructor throws a `TypeError`, because the header has the size and comes before the content. The body must have `size` bytes. If it does not, the pack stops with an error. A sync command takes only a string, a `Uint8Array`, or a sync iterable as the body. ### Add entries from memory to an archive The list of files of `create()`, `replace()`, and `update()` can have: - a path - a path that starts with `@`, such as `'@other.tar'`: the rest is the path of an archive, and its entries are added - a `WriteEntry` - a `ReadEntry` of an archive that you read - an iterable or async iterable of entries, such as the result of `entries()` ```js import { create, WriteEntry } from 'tar' await create({ file: 'bundle.tgz', gzip: true }, [ 'src', new WriteEntry( { path: 'build-info.json', mode: 0o644, mtime: new Date() }, JSON.stringify({ built: new Date().toISOString() }), ), new WriteEntry({ path: 'empty/', type: 'Directory', mode: 0o755 }), ]) ``` ### Create an archive from a stream A file of an archive can come from a `ReadableStream`, such as the body of a `fetch()` response. Make a `WriteEntry` with the stream as its body, and give it to `create()`. You must set `size`, the number of bytes that the stream gives. A tar header has the size of the file, and the header comes before the content. `create()` cannot write the entry without it. If you do not know the size, read the stream into memory first, for example with `await new Response(stream).bytes()`, and give the bytes as the body. ```js import { create, WriteEntry } from 'tar' const text = new TextEncoder().encode('the content of the stream\n') // A stream of two chunks. A stream from a file or from the network // works in the same way. const stream = new ReadableStream({ start: controller => { controller.enqueue(text.subarray(0, 10)) controller.enqueue(text.subarray(10)) controller.close() }, }) await create({ file: 'from-stream.tgz', gzip: true }, [ new WriteEntry({ path: 'stream.txt', size: text.length }, stream), ]) ``` A Node.js `Readable` is an async iterable of chunks, so it can also be the body. `Readable.toWeb(readable)` makes a `ReadableStream` of it. This example copies an archive, and removes some entries. ```js import { readFile } from 'node:fs/promises' import { create, entries } from 'tar' await create( { file: 'no-docs.tgz', gzip: true, filter: path => !path.endsWith('.md'), }, [entries(await readFile('archive.tgz'))], ) ``` ### Remove an entry from an archive `delete()` removes entries from an archive file. `delete` is a reserved word, so import it with a different name, or call it as a property. ```js import * as tar from 'tar' import { delete as del, deleteSync } from 'tar' await tar.delete({ file: 'archive.tar' }, ['README.md']) // A directory, and all that is in it. await del({ file: 'archive.tar' }, ['src/lib']) // Each entry for which the filter returns true. deleteSync({ file: 'archive.tar', filter: path => path.endsWith('.map'), }) ``` ## Commands Each command is `command(options, files)`. The aliases `c`, `x`, `t`, `r`, and `u` are also exported. `delete()` has no alias. These options are common to each command: - `strict` Make each warning an error. See [Warnings and errors](#warnings-and-errors). For example, a path of `create()` that does not exist, and a path in `files` of `extract()` or `list()` that is not in the archive, are warnings without it. - `onwarn` A function `(code, message, data)`. It gets each warning when `strict` is not set. - `file` The path of the archive file. - `fs`, `platform` See [Entry points](#entry-points). ### Option aliases Each command takes the option aliases of version 7. An option that is set wins over its aliases. `entries()` and the classes take only the names of the options. | Option | Aliases | | --------------- | --------------------------------------------------------------- | | `cwd` | `C` | | `file` | `f` | | `gzip` | `z` | | `preservePaths` | `P` | | `unlink` | `U` | | `strip` | `strip-components`, `stripComponents` | | `newer` | `keep-newer`, `keepNewer`, `keep-newer-files`, `keepNewerFiles` | | `keep` | `k`, `keep-existing`, `keepExisting` | | `noMtime` | `m`, `no-mtime` | | `preserveOwner` | `p` | | `follow` | `L`, `h` | | `onEntry` | `onentry`, `onReadEntry` | | `chmod` | `noChmod: false` is `chmod: true`, if `chmod` is not set | ```js import { c, t } from 'tar' await c({ f: 'alias.tgz', z: true, C: '.' }, ['src']) await t({ f: 'alias.tgz', onentry: entry => console.log(entry.path) }) ``` ### `create(options, files)` Make an archive of `files`. A directory is added with all that is in it. With `file`, the archive is written to that path, and the result is a `Promise`. With no `file`, the result is a `ReadableStream`. `createSync()` returns `undefined` or an iterator of chunks. The files are read only when the stream or the iterator is read. `create()` throws a `TypeError` when `files` is empty. A path that does not exist is a `TAR_ENTRY_ERROR` warning, as it is for bsdtar. The warning has the `ENOENT` error of the file system, and the other paths are added. This is also true for a path in a directory that is removed after the directory is read. With `strict`, it is an error. Each other error from the file system stops the command. With `file`, the archive file that is not complete stays on disk. `create()` writes the size that `lstat` gives for a file, and then reads that number of bytes. If the file has fewer bytes or more bytes, the command stops with an error that has the `code` `'EOF'`, the `path`, and a message that starts with `encountered unexpected EOF` or `did not encounter expected EOF`. The cause is a file that changed while it was read, or a file system that gives a wrong size, such as the `fs` module of Electron for a path in an `asar` file. See [Electron and `asar` files](#electron-and-asar-files). Options: - `file` The path of the archive to write. It is not relative to `cwd`. - `mode` The mode of the archive file. Default `0o666`. - `cwd` The directory that the paths in `files` are relative to. - `prefix` A directory to put before the path of each entry. - `gzip` Compress with gzip. In `tar`, an object is the options of `zlib.createGzip()`. - `brotli` Compress with brotli. Not in `tar/core`. An object is the options of `zlib.createBrotliCompress()`. - `zstd` Compress with zstd. Not in `tar/core`. An object is the options of `zlib.createZstdCompress()`. - `compress` Compress with a `Codec` of your own: `codec`, or `[codec, options]`. See [Compression](#compression). - `codecs` More compression formats. See [Compression](#compression). - `filter` A function `(path, stat)`. If it returns false, the path is not added. For a directory, the paths in it are also not added. For an entry of another archive, the second argument is the `ReadEntry`. It is not called for a `WriteEntry`. - `onWriteEntry` A function that gets the `WriteEntry` of each entry before its header is written. It can change the fields. The async commands wait for a promise. - `portable` Leave out the data that is specific to one system: `uid`, `gid`, `uname`, `gname`, `atime`, `ctime`, `dev`, `ino`, `nlink`, and the `mtime` of a directory. Remove the write permission of the group and of others. With `gzip`, the header of the gzip data does not name the system. - `follow` Add the target of a symbolic link, not the link. - `bigint` Call `lstat`, `stat`, and `stat` of a file handle with the option `{ bigint: true }`, to get the exact `dev` and `ino` of each path. Default: true on Windows (with the `platform` `'win32'`), false on other systems. Set it to true for a file system with large `ino` values on another system, such as an NTFS volume on Linux. With it: - No added call is made to get exact values. - A `filter` gets a stat with numbers that `create()` makes. It has the fields of `CreateStats`, and it is not the object of the file system. Without a `filter`, `create()` does not make it. - A time can be 1 ms less than the time without the option. - `noDirRecurse` Add a directory without the entries in it. - `preservePaths` Keep a root at the start of a path, such as `/`. By default the root is removed, with a `TAR_ENTRY_INFO` warning. - `noPax` Write no PAX header. A PAX header holds the fields that do not fit the basic header, such as a long path. Other tar programs can need it. - `noMtime` Write no `mtime`. - `mtime` A `Date`. The `mtime` of each entry. - `maxReadSize` The most bytes to read from a file at one time. Default 16 MiB. - `jobs` The number of paths that are read ahead at one time. Default 4. Async only. - `maxReadSize` and `jobs` take a positive integer, or `Infinity`. 0 gives the default. Each other value is a `TypeError`. - `processUid`, `processUname` See [The process options](#the-process-options). - `strict`, `onwarn`, `fs`, `platform` The common options. The archive has these entry types: files, directories, symbolic links, and hard links. A file with more than one link is a hard link entry the second time that the pack finds it. Each other object, such as a socket, a FIFO, or a device, is skipped with a `TAR_ENTRY_UNSUPPORTED` warning. The object is not opened. A path that the `filter` refuses gives no warning. ```js import { create } from 'tar' await create( { file: 'release.tgz', gzip: { level: 9 }, prefix: 'package', portable: true, filter: path => !path.endsWith('.test.js'), onWriteEntry: entry => { entry.mtime = new Date('2020-01-01') }, }, ['src', 'package.json'], ) ``` ### Reproducible archives `create()` gives the same tar bytes for the same content, on each system and at each time, if you do these things: - Set `portable: true`. It leaves out the data of one system: `uid`, `gid`, `uname`, `gname`, `atime`, `ctime`, `dev`, `ino`, `nlink`, and the `mtime` of a directory. It removes the write permission of the group and of others from each mode. - Set `mtime` to a fixed `Date`. Each file and each link then has that time, and not the time of the file on disk. Without `mtime`, the bytes change when a file is written again with the same content. Some programs do not accept a date before 1980. - Give the same paths in the same order, with the same `cwd` layout and `prefix`. The names in a directory need no work: `create()` writes them in sorted order on each runtime and file system. - Use the same version of this package. Two things come from the file system, and `portable` does not change them: - The mode. Windows does not keep the permission bits, so a file has another mode there. To get the same bytes on each system, set `entry.mode` in `onWriteEntry`. - Hard links. A file with more than one link is a hard link entry the second time that the pack finds it. A copy of the tree without the links gives other bytes. Compression has its own rules. With `portable`, the header of gzip data does not name the system: byte 9 is 255, "unknown". The compressed bytes can still differ, because they come from the zlib library of the runtime. A different runtime, a different version of zlib, or a different `level` can give other bytes for the same input. The tar bytes in the compressed data do not differ. To compare two archives, or to make a digest that must not change, use the tar bytes. ```js import { createHash } from 'node:crypto' import { create } from 'tar' const options = { portable: true, mtime: new Date('1985-10-26T08:15:00.000Z'), onWriteEntry: entry => { entry.mode = entry.type === 'Directory' ? 0o755 : 0o644 }, } const digest = async () => { const hash = createHash('sha256') for await (const chunk of create(options, ['src', 'package.json'])) { hash.update(chunk) } return hash.digest('hex') } console.log((await digest()) === (await digest())) ``` ### `extract(options, files)` Extract the entries of an archive into the directory `cwd`. If `files` is set, extract only the entries that it lists, and the entries in the directories that it lists. The result is a `Promise`. It resolves when all the file system work is done. A problem with one entry is a warning, and the extraction continues. A path in `files` that no entry of the archive matches is a `TAR_ENTRY_ERROR` warning when the archive ends, as it is for `tar xf archive.tar not-found`. With `strict`, it is an error, and the entries that match are extracted first. See [Warnings and errors](#warnings-and-errors). See [Security](#security) before you extract an archive that you do not trust. Options: - `file` The path of the archive to read. - `input` The archive data. Set `input` or `file`. - `cwd` The directory to extract into. It must exist and be a directory. If not, the command fails with a `CwdError`. - `strip` Remove this number of leading parts from each path. An entry with no part left is skipped, with no warning. Thus `strip: 1` does not apply the mode, the owner, or the times of the top directory of the archive to the extraction directory. `files` and `filter` see the path before this change. - `filter` A function `(path, entry)`. If it returns false, the entry is not extracted. - `onEntry` A function that gets each `ReadEntry` before it is extracted. A promise is awaited. An entry whose body `onEntry` reads is not extracted. - `transform` A function that gets each file entry. It returns a `TransformStream` that changes the content, or `undefined`. In `extractSync()`, it returns a function `(chunk) => chunk`, or `undefined`. With `transform`, one entry is extracted at a time. The result can be each object that has a `readable` and a `writable` web stream. For a Node.js `Transform` stream, return `codecStream(stream)`. See [Migration from version 7](#migration-from-version-7). - `keep` Do not write over an existing file. - `newer` Do not write over a file that is newer than the entry. - `unlink` Remove a file before the entry is written. By default the content of an existing file is replaced in place, and the file keeps its mode. Also remove an object in the path to an entry that is not a directory, and make a directory there. - `noMtime` Do not set the times of the entries. - `preserveOwner` Set the owner of each entry to the `uid` and `gid` in the archive. Default: true if `processUid` is 0 and `uid` is not set. - `uid`, `gid` Set the owner of each entry to this user and group. You must set the two. You cannot set them with `preserveOwner`. Each must be an integer from 0 to 4294967295. Any other number is a `TypeError`. - `forceChown` Set the owner of each entry, also when it is the owner of the process. - `chmod` Call `fs.chmod()` on each directory, so that the umask of the process does not change its mode. It has no effect on files. - `processUmask` The umask of the process. Only used with `chmod`. - `umask` The mask for the default modes `dmode` and `fmode`. Default: `processUmask` with `chmod`, 0 without it. It does not change the mode of an entry that has one. - `dmode` The mode of a directory that has no mode in the archive, and of a parent directory that is not in the archive. Default `0o777 & ~umask`. - `fmode` The mode of a file that has no mode in the archive. Default `0o666 & ~umask`. - `preservePaths` Permit absolute paths, `..` in paths, and extraction through symbolic links. This is not safe with an archive that you do not trust. - `maxDepth` The most parts that a path or a link target can have. Default 1024. `Infinity` removes the limit. - `jobs` The most entries to extract at one time. Default 16. Async only. - `maxReadSize` The most bytes to read from the archive file at one time. Default 16 MiB. - `jobs` and `maxReadSize` take a positive integer, or `Infinity`. 0 gives the default. Each other value is a `TypeError`. - `maxMetaEntrySize`, `maxDecompressionRatio`, `codecs` The options of [`entries()`](#entriesinput-options). - `processUid`, `processGid` See [The process options](#the-process-options). - `strict`, `onwarn`, `fs`, `platform` The common options. Files, directories, symbolic links, and hard links are extracted. Devices, FIFOs, and sparse files are skipped with a `TAR_ENTRY_UNSUPPORTED` warning. A sparse file is an entry of the GNU type `S`, or a file with a `GNU.sparse.` field in its PAX header. Its body is a map and the parts of the file that are not holes, so it is not the content of the file. `list()` and `entries()` give the entry with the type `'SparseFile'`, and with the path and the size that are in the archive. `create()` does not add a sparse entry of another archive. ```js import { mkdir } from 'node:fs/promises' import { extract } from 'tar' await mkdir('out', { recursive: true }) await extract({ file: 'archive.tgz', cwd: 'out', // No links. filter: (path, entry) => entry.type !== 'SymbolicLink' && entry.type !== 'Link', // Make the text of each .md file upper case. transform: entry => { if (!entry.path.endsWith('.md')) return undefined const decoder = new TextDecoder() const encoder = new TextEncoder() return new TransformStream({ transform: (chunk, controller) => { const text = decoder.decode(chunk, { stream: true }) controller.enqueue(encoder.encode(text.toUpperCase())) }, }) }, onwarn: (code, message) => console.error(code, message), }) ``` ### `list(options, files)` Call `onEntry` with each entry of an archive. If `files` is set, only with the entries that it lists, and the entries in the directories that it lists. The result is a `Promise`. Options: - `file` The path of the archive to read. - `input` The archive data. Set `input` or `file`. - `onEntry` A function that gets each `ReadEntry`. A promise is awaited. - `filter` A function `(path, entry)`. If it returns false, `onEntry` does not get the entry. - `maxReadSize` The most bytes to read from the archive file at one time. Default 16 MiB. A positive integer, or `Infinity`. 0 gives the default. Each other value is a `TypeError`. - `maxMetaEntrySize`, `maxDecompressionRatio`, `codecs` The options of [`entries()`](#entriesinput-options). - `strict`, `onwarn`, `fs`, `platform` The common options. ```js import { listSync } from 'tar' const paths = [] listSync( { file: 'archive.tgz', onEntry: entry => paths.push(entry.path) }, ['src'], ) console.log(paths) ``` A path in `files` matches an entry if it is the path of the entry, or the path of one of its parent directories. Trailing slashes are ignored. A path in `files` that no entry matches is a `TAR_ENTRY_ERROR` warning when the archive ends. The message is `: not found in archive`, and `data.path` is the path. An entry that `filter` refuses still matches its path. With `strict`, the warning is an error, after `onEntry` got the entries that match. ### `replace(options, files)` Add `files` to the end of an archive file. The archive must not be compressed. If the file does not exist, it is made. The result is a `Promise`. The new entries go after the last complete entry of the archive. If a path is in the archive two times, a reader gets the old entry and then the new entry. An extraction writes the new entry last. Options: - `file` The path of the archive. Required. - `mtimeCache` A `Map`. The command sets the path and the `mtime` of each entry that the archive has. - All the options of `create()`, but not `gzip`, `brotli`, `zstd`, or `compress`. ### `delete(options, files)` Remove entries from an archive file. An entry is removed if `files` has its path, or the path of one of its parent directories. Each entry with that path is removed. The result is a `Promise`. The command writes the entries that stay to a new file next to the archive, and then renames the new file to the path of the archive. If the command fails, the archive does not change. Options: - `file` The path of the archive. Required. - `filter` A function `(path, entry)`. Return true to remove the entry. With `files`, an entry is removed only if `files` matches it and the filter returns true. With no `files`, the filter is required. - `codecs`, `maxDecompressionRatio`, `maxReadSize`, and the other options of `entries()`. Notes: - A path in `files` that no entry matches is a `TAR_ENTRY_ERROR` warning, `: not found in archive`. With `strict`, it is an error, and the archive does not change. - The entries that stay have the same content, the same order, and the same fields. The headers are written again, so their bytes can differ: for example, a GNU long name header becomes a PAX header with the same path. - The new file has the compression of the archive, with the default options of the codec, and the permission bits of the archive. With a custom `fs` that has no `chmod`, the bits can differ: `open` removes the bits of the process umask, and an archive with no bits gets the default mode. - The disk must have space for the two files until the rename. A hard link to the old archive file keeps the old content. ### `update(options, files)` The same as `replace()`, with the same options. A path is added only if its `mtime` is later than the `mtime` of the last entry of the archive that has that path. A directory is always added again, and each path in it is checked. So a changed file in a directory is added, also when the `mtime` of the directory did not change. A basic tar header holds the `mtime` in whole seconds. For such an entry, the file is newer only if its `mtime` is in a later second. A change in the same second as the entry is not seen. ### `entries(input, options)` Read the entries of an archive. The result is an async iterator of `ReadEntry` objects. `entriesSync()` returns an iterator of `ReadEntrySync` objects. `tar/core` does not have `entriesSync()`. Entries that are not an object of a file system, such as PAX headers, are not given. Options: - `filter` A function `(path, entry)`. If it returns false, the entry is skipped. - `maxMetaEntrySize` The largest PAX header or GNU long name entry to read, in bytes. A larger one stops the archive with a `TAR_BAD_ARCHIVE` error. Default 1 MiB. - `maxDecompressionRatio` Stop with an error when the decompressed size is more than this many times the compressed size. Default 1000. `Infinity` removes the limit. - `codecs` More compression formats. See [Compression](#compression). - `file` The name of the archive. Warnings have it, and the reader uses it to find a format by the file name. - `strict`, `onwarn`, `platform` The common options. ## Compression The readers find the format of the input. You set no option to read a compressed archive. To write a compressed archive, set `gzip`, `brotli`, or `zstd` in the options of `create()`. To write with a codec of your own, set `compress`. You can set only one of the four. | Format | `tar` | `tar/core` | | ------ | ------------------------------------------------------ | -------------------------------------------------------------------------------- | | gzip | Yes, with `node:zlib`. Options of `zlib.createGzip()`. | Yes, with `CompressionStream` and `DecompressionStream`. Async only. No options. | | brotli | Yes, if `node:zlib` of the runtime has it | No | | zstd | Yes, if `node:zlib` of the runtime has it | No | ```js import { create, list } from 'tar' await create({ file: 'src.tar.zst', zstd: true }, ['src']) await create({ file: 'src.tar.br', brotli: true }, ['src']) await create({ file: 'src.tgz', gzip: { level: 9 } }, ['src']) for (const file of ['src.tar.zst', 'src.tar.br', 'src.tgz']) { await list({ file, onEntry: entry => console.log(file, entry.path), }) } ``` ### How a format is detected 1. The reader reads the first 512 bytes of the input, or more if a codec needs more. 2. It uses the first codec whose `detect()` function returns true. Gzip data starts with the bytes `1f 8b`. Zstd data starts with the bytes `28 b5 2f fd`. 3. If no codec detects the data, and the data does not start with a tar header, the reader uses the first codec that has an ending of the `file` option in its `extensions`. 4. If there is no such codec, the input is read as a tar archive. Brotli data has no fixed first bytes. The reader uses brotli only if the name of the archive ends in `.tar.br` or `.tbr`. To read brotli from `input`, also set `file`. ```js import { readFile } from 'node:fs/promises' import { create, entries } from 'tar' await create({ file: 'src.tar.br', brotli: true }, ['src']) const data = await readFile('src.tar.br') for await (const entry of entries(data, { file: 'src.tar.br' })) { console.log(entry.path) } ``` `replace()` and `update()` refuse an archive that a codec detects, and a `file` name that ends in `.br` or `.tbr`. ### `maxDecompressionRatio` A small compressed file can hold a very large quantity of data. The readers count the compressed bytes and the decompressed bytes. When the decompressed count is more than `maxDecompressionRatio` times the compressed count, the reader stops with a `TAR_ABORT` error. The default is 1000. The async codecs of `tar` give at most 64 KiB of output at a time, and only when the reader asks. Thus the reader stops soon after the limit. The sync codecs of `tar` give the limit to `node:zlib` as `maxOutputLength`. ### The `Codec` type A codec is one compression format. Give codecs in the `codecs` option, to add a format or to replace the built-in gzip. Your codecs are tried before the built-in codecs. ```ts type Codec = { name: string /** The number of bytes that `detect` needs. */ headLength: number /** True if the input is in this format. */ detect(head: Uint8Array): boolean /** File name endings, for a format that `detect` cannot identify. */ extensions?: string[] /** Make a stream that decompresses. */ decompress(): CodecStream /** Make a stream that compresses. */ compress?(options?: Record): CodecStream /** Decompress all of `data`. The sync readers need it. */ decompressSync?( data: Uint8Array, limit: { maxOutputLength: number }, ): Uint8Array /** Compress all of `data`. The sync commands need it. */ compressSync?( data: Uint8Array, options?: Record, ): Uint8Array } type CodecStream = { readable: ReadableStream writable: WritableStream } ``` A `TransformStream` is a `CodecStream`. To compress with a codec, give it in the `compress` option of `create()`. The codec does not need to be in `codecs`. To give options to the `compress()` function of the codec, set `compress` to `[codec, options]`. With `portable`, a codec that has the name `gzip` also gets `portable: true` in its options. ```js import { create, gzip } from 'tar' // The built-in gzip of the core, not the gzip of `node:zlib`. await create({ file: 'core.tgz', compress: gzip }, ['src']) ``` The options `gzip`, `brotli`, and `zstd` select the first codec of `codecs` that has that name, then the built-in codec. An option with the name of another codec selects nothing. `decompressSync()` must throw before it makes more than `limit.maxOutputLength` bytes. That number is `Infinity` when the caller sets `maxDecompressionRatio: Infinity`. A sync command with compression holds the complete archive in memory. `createSync()` compresses the archive in one call. The sync readers decompress all of the input in one call. The exports for codecs are: - `gzip` The built-in codec of the core. - `zlibCodecs(zlib)` Makes the gzip, brotli, and zstd codecs of a `node:zlib` module. Not in `tar/core`. - `codecStream(stream)` Makes a `CodecStream` from a `node:zlib` stream, or from another Node.js `Transform` stream. Not in `tar/core`. ### Recipe: bzip2 This recipe adds bzip2 with the package [`node-bzip2`](https://www.npmjs.com/package/node-bzip2). That package is a native addon. It is not a dependency of `tar`. `node-bzip2` works on complete buffers. It has no stream interface and no output limit. Thus this codec has two limits: - The async form collects all of the input in memory before it compresses or decompresses. - All of the data is decompressed before the ratio limit is checked. `maxDecompressionRatio` cannot stop a large output before it is in memory. Do not use this codec for input that you do not trust, unless you limit the size of the input and you accept the memory use. ```js import bzip2 from 'node-bzip2' import { mkdir } from 'node:fs/promises' import { create, extract, list } from 'tar' // A stream that collects all of its input. At the end it gives the // result of `fn` as one chunk. const whole = fn => { const parts = [] return new TransformStream({ transform: chunk => { parts.push(chunk) }, flush: async controller => { controller.enqueue(await fn(Buffer.concat(parts))) }, }) } const bz2 = { name: 'bzip2', // Bzip2 data starts with the characters "BZh". headLength: 3, detect: head => head[0] === 0x42 && head[1] === 0x5a && head[2] === 0x68, decompress: () => whole(data => bzip2.decompressAsync(data)), compress: options => whole(data => bzip2.compressAsync(data, options)), decompressSync: (data, { maxOutputLength }) => { // The limit is checked after the data is decompressed. const out = bzip2.decompress(data) if (out.length > maxOutputLength) { throw new Error('bzip2: the output is larger than the limit') } return out }, compressSync: (data, options) => bzip2.compress(data, options), } // The second item is the options of the `compress` functions of // node-bzip2. await create({ file: 'src.tar.bz2', compress: [bz2, { level: 9 }] }, [ 'src', ]) await list({ file: 'src.tar.bz2', codecs: [bz2], onEntry: entry => console.log(entry.path), }) await mkdir('out', { recursive: true }) await extract({ file: 'src.tar.bz2', codecs: [bz2], cwd: 'out' }) ``` ## Lower-level classes The commands use these classes. They are exported from `tar` and from `tar/core`. The sync classes are exported only from `tar`. | Export | What it is | | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | `Parser` | A tar parser that you pull. It does not decompress, and it reads no input by itself. | | `TarReader`, `TarReaderSync` | The iterators that `entries()` and `entriesSync()` return. They add decompression and the entry bodies to a `Parser`. | | `EntryCursor` | Gets entries and the parts of their bodies from a `Parser`. | | `EntryData` | The metadata of an entry. The base class of `ReadEntry` and `ReadEntrySync`. | | `Pack`, `PackSync` | Make an archive. `add()` paths and entries, then `end()`. A `Pack` is the async iterator of the bytes of the archive. A `PackSync` is the iterator. | | `PackBase`, `PackJob` | The parts that `Pack` and `PackSync` share, and a path in the queue of a pack. | | `Unpack`, `UnpackSync` | Extract an archive. `new Unpack(options).extract(input)` returns a promise. | | `UnpackBase` | The checks of each entry, with no use of the file system. | | `Header` | Encodes and decodes one 512 byte header block. | | `Pax` | Encodes and parses a PAX extended header. | | `types` | `types.name` is a `Map` from a type code to a type name. `types.code` is the opposite `Map`. | | `AppendScan` | Finds the place in an archive file where `replace()` adds entries. | | `PathReservations` | Keeps the entries that use the same paths in order during an async extraction. | | `CwdError`, `SymlinkError` | The error classes of an extraction directory that is not usable, and of a path that goes through a symbolic link. | `Pack`, `Unpack`, and `Parser` are not streams. They do not have `write()`, `pipe()`, or events. ### The `Parser` state machine Give the parser input with `push()`. Then call `next()` for each event until it returns `undefined`. Then give it more input, or call `end()` and call `next()` again. The events are: - `{ type: 'entry', entry }` An entry. If `entry.ignore` is true, no other event follows for this entry. - `{ type: 'body', data }` A part of the body of the entry. `data` is a view into the input. - `{ type: 'entryEnd' }` The end of the body of the entry. - `{ type: 'end' }` The end of the archive. ```js import { Pack, Parser, WriteEntry } from 'tar' // Some archive data, in chunks. const pack = new Pack() .add(new WriteEntry({ path: 'a.txt' }, 'the content of a')) .add(new WriteEntry({ path: 'b.txt' }, 'b')) .end() const parser = new Parser() const read = () => { for (let ev; (ev = parser.next()); ) { if (ev.type === 'entry') { console.log('entry', ev.entry.path, ev.entry.size) } else if (ev.type === 'body') { console.log(' body', ev.data.length) } else if (ev.type === 'entryEnd') { console.log(' end of entry') } else { console.log('end of archive') } } } for await (const chunk of pack) { parser.push(chunk) read() } parser.end() read() ``` The options of a `Parser` are `filter`, `maxMetaEntrySize`, `platform`, `strict`, `onwarn`, and `file`. `parser.position` is the number of input bytes up to the end of the last complete entry. ## Warnings and errors A warning is a problem that does not stop the command. In most cases it is about one entry. An error stops the command: an async command rejects its promise, a sync command throws, and a stream or an iterator fails at the next read. `onwarn(code, message, data)` gets each warning: - `code` One of the codes below. - `message` The text of the warning. - `data` An object with more data. If the cause is an `Error`, `data` is that error. It can have these fields: - `tarCode` The code below. - `code` The code of the file system or of the codec, if the cause has one. If not, the same as `tarCode`. - `file` The `file` option. - `cwd` The extraction directory. - `entry` The entry. - `header` The header, if there is no entry. - `recoverable` False if the problem is always an error. An error that comes from a warning has the same fields. ```js import { list } from 'tar' await list({ // This is not an archive. input: new TextEncoder().encode('x'.repeat(1024)), onwarn: (code, message, data) => { console.log(code, message, data.tarCode) }, }) ``` ### Codes - `TAR_ENTRY_INFO` The entry is changed, and then used. For example, the root is removed from an absolute path. - `TAR_ENTRY_INVALID` The header is not valid, and the entry is skipped: the checksum is wrong, the path is missing, or a link has no `linkpath`. - `TAR_ENTRY_ERROR` The entry is valid, but it cannot be extracted or added. In an extraction: an error of the file system, a path with `..`, a path or a link target that goes out of the extraction directory, a path that goes through a symbolic link, or a path that is too deep or too long. In `create()`: a path that does not exist, a file or a directory that changed while it was read, or with `follow`, a directory that is in itself. In `extract()` and `list()`: a path in `files` that no entry of the archive matches. - `TAR_ENTRY_UNSUPPORTED` The type of the entry is not supported, and the entry is skipped. `extract()` reports devices, FIFOs, and sparse files. `create()` reports a sparse entry of another archive. `create()` reports each path that is not a file, a directory, or a symbolic link, such as a socket. `data.path` is the path. - `TAR_BAD_ARCHIVE` The archive is damaged: the body of the last entry is cut off, or no header is valid. An archive with no entries also gets this warning. This is a warning for `list()`, `entries()`, and `Parser`. It is always an error for `extract()`, because the extraction cannot be complete. It is an error for each command when the size or the name of an entry is not sure. See [Archives that tar programs read in different ways](#archives-that-tar-programs-read-in-different-ways). - `TAR_ABORT` The data cannot be decompressed, or the decompression ratio is more than the limit. This is always an error. If the input ends in the middle of a header block, there is no warning. An error of the file system or of a codec keeps its `code`, such as `ENOENT`. Read `tarCode` to get the code of this package. ### `strict` With `strict: true`, each warning is thrown as an error. This includes `TAR_ENTRY_INFO`. In strict mode, `extract()` stops at the first error. The entries before it are extracted. The entries after it are not. ```js import { mkdir, readdir } from 'node:fs/promises' import { create, extract, WriteEntry } from 'tar' // An archive with a path that goes out of the extraction directory. const archive = create({ preservePaths: true }, [ new WriteEntry({ path: 'first.txt' }, '1'), new WriteEntry({ path: '../outside.txt' }, '2'), new WriteEntry({ path: 'last.txt' }, '3'), ]) const data = await new Response(archive).arrayBuffer() // By default, the bad entry is a warning. await mkdir('loose') await extract({ input: data, cwd: 'loose', onwarn: (code, message) => console.log(code, message), }) console.log(await readdir('loose')) // first.txt, last.txt // In strict mode, it is an error. await mkdir('strict') await extract({ input: data, cwd: 'strict', strict: true }).catch(er => console.log(er.tarCode, er.message), ) console.log(await readdir('strict')) // first.txt ``` Some problems are an error also when `strict` is not set: - The extraction directory does not exist or is not a directory (`CwdError`). - The archive cannot be read, or cannot be decompressed. - `TAR_BAD_ARCHIVE` in `extract()`. - `TAR_BAD_ARCHIVE` for an entry whose size or name is not sure. - `onEntry` or `filter` throws. - In `create()`, a path cannot be read, for a cause other than that it does not exist. ## Security This package is used to extract archives that unknown persons publish, such as the packages of the npm registry. It has many protections against archives that try to write outside of the extraction directory, or to use too many resources. `preservePaths: true` turns off the path protections. Do not set it for an archive that