| 1 | # node-tar
|
|---|
| 2 |
|
|---|
| 3 | Fast and full-featured Tar for Node.js
|
|---|
| 4 |
|
|---|
| 5 | The API is designed to mimic the behavior of `tar(1)` on unix systems.
|
|---|
| 6 | If you are familiar with how tar works, most of this will hopefully be
|
|---|
| 7 | straightforward for you. If not, then hopefully this module can teach
|
|---|
| 8 | you useful unix skills that may come in handy someday :)
|
|---|
| 9 |
|
|---|
| 10 | ## Background
|
|---|
| 11 |
|
|---|
| 12 | A "tar file" or "tarball" is an archive of file system entries
|
|---|
| 13 | (directories, files, links, etc.) The name comes from "tape archive".
|
|---|
| 14 | If you run `man tar` on almost any Unix command line, you'll learn
|
|---|
| 15 | quite a bit about what it can do, and its history.
|
|---|
| 16 |
|
|---|
| 17 | Tar has 5 main top-level commands:
|
|---|
| 18 |
|
|---|
| 19 | * `c` Create an archive
|
|---|
| 20 | * `r` Replace entries within an archive
|
|---|
| 21 | * `u` Update entries within an archive (ie, replace if they're newer)
|
|---|
| 22 | * `t` List out the contents of an archive
|
|---|
| 23 | * `x` Extract an archive to disk
|
|---|
| 24 |
|
|---|
| 25 | The other flags and options modify how this top level function works.
|
|---|
| 26 |
|
|---|
| 27 | ## High-Level API
|
|---|
| 28 |
|
|---|
| 29 | These 5 functions are the high-level API. All of them have a
|
|---|
| 30 | single-character name (for unix nerds familiar with `tar(1)`) as well
|
|---|
| 31 | as a long name (for everyone else).
|
|---|
| 32 |
|
|---|
| 33 | All the high-level functions take the following arguments, all three
|
|---|
| 34 | of which are optional and may be omitted.
|
|---|
| 35 |
|
|---|
| 36 | 1. `options` - An optional object specifying various options
|
|---|
| 37 | 2. `paths` - An array of paths to add or extract
|
|---|
| 38 | 3. `callback` - Called when the command is completed, if async. (If
|
|---|
| 39 | sync or no file specified, providing a callback throws a
|
|---|
| 40 | `TypeError`.)
|
|---|
| 41 |
|
|---|
| 42 | If the command is sync (ie, if `options.sync=true`), then the
|
|---|
| 43 | callback is not allowed, since the action will be completed immediately.
|
|---|
| 44 |
|
|---|
| 45 | If a `file` argument is specified, and the command is async, then a
|
|---|
| 46 | `Promise` is returned. In this case, if async, a callback may be
|
|---|
| 47 | provided which is called when the command is completed.
|
|---|
| 48 |
|
|---|
| 49 | If a `file` option is not specified, then a stream is returned. For
|
|---|
| 50 | `create`, this is a readable stream of the generated archive. For
|
|---|
| 51 | `list` and `extract` this is a writable stream that an archive should
|
|---|
| 52 | be written into. If a file is not specified, then a callback is not
|
|---|
| 53 | allowed, because you're already getting a stream to work with.
|
|---|
| 54 |
|
|---|
| 55 | `replace` and `update` only work on existing archives, and so require
|
|---|
| 56 | a `file` argument.
|
|---|
| 57 |
|
|---|
| 58 | Sync commands without a file argument return a stream that acts on its
|
|---|
| 59 | input immediately in the same tick. For readable streams, this means
|
|---|
| 60 | that all of the data is immediately available by calling
|
|---|
| 61 | `stream.read()`. For writable streams, it will be acted upon as soon
|
|---|
| 62 | as it is provided, but this can be at any time.
|
|---|
| 63 |
|
|---|
| 64 | ### Warnings and Errors
|
|---|
| 65 |
|
|---|
| 66 | Tar emits warnings and errors for recoverable and unrecoverable situations,
|
|---|
| 67 | respectively. In many cases, a warning only affects a single entry in an
|
|---|
| 68 | archive, or is simply informing you that it's modifying an entry to comply
|
|---|
| 69 | with the settings provided.
|
|---|
| 70 |
|
|---|
| 71 | Unrecoverable warnings will always raise an error (ie, emit `'error'` on
|
|---|
| 72 | streaming actions, throw for non-streaming sync actions, reject the
|
|---|
| 73 | returned Promise for non-streaming async operations, or call a provided
|
|---|
| 74 | callback with an `Error` as the first argument). Recoverable errors will
|
|---|
| 75 | raise an error only if `strict: true` is set in the options.
|
|---|
| 76 |
|
|---|
| 77 | Respond to (recoverable) warnings by listening to the `warn` event.
|
|---|
| 78 | Handlers receive 3 arguments:
|
|---|
| 79 |
|
|---|
| 80 | - `code` String. One of the error codes below. This may not match
|
|---|
| 81 | `data.code`, which preserves the original error code from fs and zlib.
|
|---|
| 82 | - `message` String. More details about the error.
|
|---|
| 83 | - `data` Metadata about the error. An `Error` object for errors raised by
|
|---|
| 84 | fs and zlib. All fields are attached to errors raisd by tar. Typically
|
|---|
| 85 | contains the following fields, as relevant:
|
|---|
| 86 | - `tarCode` The tar error code.
|
|---|
| 87 | - `code` Either the tar error code, or the error code set by the
|
|---|
| 88 | underlying system.
|
|---|
| 89 | - `file` The archive file being read or written.
|
|---|
| 90 | - `cwd` Working directory for creation and extraction operations.
|
|---|
| 91 | - `entry` The entry object (if it could be created) for `TAR_ENTRY_INFO`,
|
|---|
| 92 | `TAR_ENTRY_INVALID`, and `TAR_ENTRY_ERROR` warnings.
|
|---|
| 93 | - `header` The header object (if it could be created, and the entry could
|
|---|
| 94 | not be created) for `TAR_ENTRY_INFO` and `TAR_ENTRY_INVALID` warnings.
|
|---|
| 95 | - `recoverable` Boolean. If `false`, then the warning will emit an
|
|---|
| 96 | `error`, even in non-strict mode.
|
|---|
| 97 |
|
|---|
| 98 | #### Error Codes
|
|---|
| 99 |
|
|---|
| 100 | * `TAR_ENTRY_INFO` An informative error indicating that an entry is being
|
|---|
| 101 | modified, but otherwise processed normally. For example, removing `/` or
|
|---|
| 102 | `C:\` from absolute paths if `preservePaths` is not set.
|
|---|
| 103 |
|
|---|
| 104 | * `TAR_ENTRY_INVALID` An indication that a given entry is not a valid tar
|
|---|
| 105 | archive entry, and will be skipped. This occurs when:
|
|---|
| 106 | - a checksum fails,
|
|---|
| 107 | - a `linkpath` is missing for a link type, or
|
|---|
| 108 | - a `linkpath` is provided for a non-link type.
|
|---|
| 109 |
|
|---|
| 110 | If every entry in a parsed archive raises an `TAR_ENTRY_INVALID` error,
|
|---|
| 111 | then the archive is presumed to be unrecoverably broken, and
|
|---|
| 112 | `TAR_BAD_ARCHIVE` will be raised.
|
|---|
| 113 |
|
|---|
| 114 | * `TAR_ENTRY_ERROR` The entry appears to be a valid tar archive entry, but
|
|---|
| 115 | encountered an error which prevented it from being unpacked. This occurs
|
|---|
| 116 | when:
|
|---|
| 117 | - an unrecoverable fs error happens during unpacking,
|
|---|
| 118 | - an entry is trying to extract into an excessively deep
|
|---|
| 119 | location (by default, limited to 1024 subfolders),
|
|---|
| 120 | - an entry has `..` in the path and `preservePaths` is not set, or
|
|---|
| 121 | - an entry is extracting through a symbolic link, when `preservePaths` is
|
|---|
| 122 | not set.
|
|---|
| 123 |
|
|---|
| 124 | * `TAR_ENTRY_UNSUPPORTED` An indication that a given entry is
|
|---|
| 125 | a valid archive entry, but of a type that is unsupported, and so will be
|
|---|
| 126 | skipped in archive creation or extracting.
|
|---|
| 127 |
|
|---|
| 128 | * `TAR_ABORT` When parsing gzipped-encoded archives, the parser will
|
|---|
| 129 | abort the parse process raise a warning for any zlib errors encountered.
|
|---|
| 130 | Aborts are considered unrecoverable for both parsing and unpacking.
|
|---|
| 131 |
|
|---|
| 132 | * `TAR_BAD_ARCHIVE` The archive file is totally hosed. This can happen for
|
|---|
| 133 | a number of reasons, and always occurs at the end of a parse or extract:
|
|---|
| 134 |
|
|---|
| 135 | - An entry body was truncated before seeing the full number of bytes.
|
|---|
| 136 | - The archive contained only invalid entries, indicating that it is
|
|---|
| 137 | likely not an archive, or at least, not an archive this library can
|
|---|
| 138 | parse.
|
|---|
| 139 |
|
|---|
| 140 | `TAR_BAD_ARCHIVE` is considered informative for parse operations, but
|
|---|
| 141 | unrecoverable for extraction. Note that, if encountered at the end of an
|
|---|
| 142 | extraction, tar WILL still have extracted as much it could from the
|
|---|
| 143 | archive, so there may be some garbage files to clean up.
|
|---|
| 144 |
|
|---|
| 145 | Errors that occur deeper in the system (ie, either the filesystem or zlib)
|
|---|
| 146 | will have their error codes left intact, and a `tarCode` matching one of
|
|---|
| 147 | the above will be added to the warning metadata or the raised error object.
|
|---|
| 148 |
|
|---|
| 149 | Errors generated by tar will have one of the above codes set as the
|
|---|
| 150 | `error.code` field as well, but since errors originating in zlib or fs will
|
|---|
| 151 | have their original codes, it's better to read `error.tarCode` if you wish
|
|---|
| 152 | to see how tar is handling the issue.
|
|---|
| 153 |
|
|---|
| 154 | ### Examples
|
|---|
| 155 |
|
|---|
| 156 | The API mimics the `tar(1)` command line functionality, with aliases
|
|---|
| 157 | for more human-readable option and function names. The goal is that
|
|---|
| 158 | if you know how to use `tar(1)` in Unix, then you know how to use
|
|---|
| 159 | `require('tar')` in JavaScript.
|
|---|
| 160 |
|
|---|
| 161 | To replicate `tar czf my-tarball.tgz files and folders`, you'd do:
|
|---|
| 162 |
|
|---|
| 163 | ```js
|
|---|
| 164 | tar.c(
|
|---|
| 165 | {
|
|---|
| 166 | gzip: <true|gzip options>,
|
|---|
| 167 | file: 'my-tarball.tgz'
|
|---|
| 168 | },
|
|---|
| 169 | ['some', 'files', 'and', 'folders']
|
|---|
| 170 | ).then(_ => { .. tarball has been created .. })
|
|---|
| 171 | ```
|
|---|
| 172 |
|
|---|
| 173 | To replicate `tar cz files and folders > my-tarball.tgz`, you'd do:
|
|---|
| 174 |
|
|---|
| 175 | ```js
|
|---|
| 176 | tar.c( // or tar.create
|
|---|
| 177 | {
|
|---|
| 178 | gzip: <true|gzip options>
|
|---|
| 179 | },
|
|---|
| 180 | ['some', 'files', 'and', 'folders']
|
|---|
| 181 | ).pipe(fs.createWriteStream('my-tarball.tgz'))
|
|---|
| 182 | ```
|
|---|
| 183 |
|
|---|
| 184 | To replicate `tar xf my-tarball.tgz` you'd do:
|
|---|
| 185 |
|
|---|
| 186 | ```js
|
|---|
| 187 | tar.x( // or tar.extract(
|
|---|
| 188 | {
|
|---|
| 189 | file: 'my-tarball.tgz'
|
|---|
| 190 | }
|
|---|
| 191 | ).then(_=> { .. tarball has been dumped in cwd .. })
|
|---|
| 192 | ```
|
|---|
| 193 |
|
|---|
| 194 | To replicate `cat my-tarball.tgz | tar x -C some-dir --strip=1`:
|
|---|
| 195 |
|
|---|
| 196 | ```js
|
|---|
| 197 | fs.createReadStream('my-tarball.tgz').pipe(
|
|---|
| 198 | tar.x({
|
|---|
| 199 | strip: 1,
|
|---|
| 200 | C: 'some-dir' // alias for cwd:'some-dir', also ok
|
|---|
| 201 | })
|
|---|
| 202 | )
|
|---|
| 203 | ```
|
|---|
| 204 |
|
|---|
| 205 | To replicate `tar tf my-tarball.tgz`, do this:
|
|---|
| 206 |
|
|---|
| 207 | ```js
|
|---|
| 208 | tar.t({
|
|---|
| 209 | file: 'my-tarball.tgz',
|
|---|
| 210 | onentry: entry => { .. do whatever with it .. }
|
|---|
| 211 | })
|
|---|
| 212 | ```
|
|---|
| 213 |
|
|---|
| 214 | For example, to just get the list of filenames from an archive:
|
|---|
| 215 |
|
|---|
| 216 | ```js
|
|---|
| 217 | const getEntryFilenames = async tarballFilename => {
|
|---|
| 218 | const filenames = []
|
|---|
| 219 | await tar.t({
|
|---|
| 220 | file: tarballFilename,
|
|---|
| 221 | onentry: entry => filenames.push(entry.path),
|
|---|
| 222 | })
|
|---|
| 223 | return filenames
|
|---|
| 224 | }
|
|---|
| 225 | ```
|
|---|
| 226 |
|
|---|
| 227 | To replicate `cat my-tarball.tgz | tar t` do:
|
|---|
| 228 |
|
|---|
| 229 | ```js
|
|---|
| 230 | fs.createReadStream('my-tarball.tgz')
|
|---|
| 231 | .pipe(tar.t())
|
|---|
| 232 | .on('entry', entry => { .. do whatever with it .. })
|
|---|
| 233 | ```
|
|---|
| 234 |
|
|---|
| 235 | To do anything synchronous, add `sync: true` to the options. Note
|
|---|
| 236 | that sync functions don't take a callback and don't return a promise.
|
|---|
| 237 | When the function returns, it's already done. Sync methods without a
|
|---|
| 238 | file argument return a sync stream, which flushes immediately. But,
|
|---|
| 239 | of course, it still won't be done until you `.end()` it.
|
|---|
| 240 |
|
|---|
| 241 | ```js
|
|---|
| 242 | const getEntryFilenamesSync = tarballFilename => {
|
|---|
| 243 | const filenames = []
|
|---|
| 244 | tar.t({
|
|---|
| 245 | file: tarballFilename,
|
|---|
| 246 | onentry: entry => filenames.push(entry.path),
|
|---|
| 247 | sync: true,
|
|---|
| 248 | })
|
|---|
| 249 | return filenames
|
|---|
| 250 | }
|
|---|
| 251 | ```
|
|---|
| 252 |
|
|---|
| 253 | To filter entries, add `filter: <function>` to the options.
|
|---|
| 254 | Tar-creating methods call the filter with `filter(path, stat)`.
|
|---|
| 255 | Tar-reading methods (including extraction) call the filter with
|
|---|
| 256 | `filter(path, entry)`. The filter is called in the `this`-context of
|
|---|
| 257 | the `Pack` or `Unpack` stream object.
|
|---|
| 258 |
|
|---|
| 259 | The arguments list to `tar t` and `tar x` specify a list of filenames
|
|---|
| 260 | to extract or list, so they're equivalent to a filter that tests if
|
|---|
| 261 | the file is in the list.
|
|---|
| 262 |
|
|---|
| 263 | For those who _aren't_ fans of tar's single-character command names:
|
|---|
| 264 |
|
|---|
| 265 | ```
|
|---|
| 266 | tar.c === tar.create
|
|---|
| 267 | tar.r === tar.replace (appends to archive, file is required)
|
|---|
| 268 | tar.u === tar.update (appends if newer, file is required)
|
|---|
| 269 | tar.x === tar.extract
|
|---|
| 270 | tar.t === tar.list
|
|---|
| 271 | ```
|
|---|
| 272 |
|
|---|
| 273 | Keep reading for all the command descriptions and options, as well as
|
|---|
| 274 | the low-level API that they are built on.
|
|---|
| 275 |
|
|---|
| 276 | ### tar.c(options, fileList, callback) [alias: tar.create]
|
|---|
| 277 |
|
|---|
| 278 | Create a tarball archive.
|
|---|
| 279 |
|
|---|
| 280 | The `fileList` is an array of paths to add to the tarball. Adding a
|
|---|
| 281 | directory also adds its children recursively.
|
|---|
| 282 |
|
|---|
| 283 | An entry in `fileList` that starts with an `@` symbol is a tar archive
|
|---|
| 284 | whose entries will be added. To add a file that starts with `@`,
|
|---|
| 285 | prepend it with `./`.
|
|---|
| 286 |
|
|---|
| 287 | The following options are supported:
|
|---|
| 288 |
|
|---|
| 289 | - `file` Write the tarball archive to the specified filename. If this
|
|---|
| 290 | is specified, then the callback will be fired when the file has been
|
|---|
| 291 | written, and a promise will be returned that resolves when the file
|
|---|
| 292 | is written. If a filename is not specified, then a Readable Stream
|
|---|
| 293 | will be returned which will emit the file data. [Alias: `f`]
|
|---|
| 294 | - `sync` Act synchronously. If this is set, then any provided file
|
|---|
| 295 | will be fully written after the call to `tar.c`. If this is set,
|
|---|
| 296 | and a file is not provided, then the resulting stream will already
|
|---|
| 297 | have the data ready to `read` or `emit('data')` as soon as you
|
|---|
| 298 | request it.
|
|---|
| 299 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 300 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 301 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 302 | - `cwd` The current working directory for creating the archive.
|
|---|
| 303 | Defaults to `process.cwd()`. [Alias: `C`]
|
|---|
| 304 | - `prefix` A path portion to prefix onto the entries in the archive.
|
|---|
| 305 | - `gzip` Set to any truthy value to create a gzipped archive, or an
|
|---|
| 306 | object with settings for `zlib.Gzip()` [Alias: `z`]
|
|---|
| 307 | - `filter` A function that gets called with `(path, stat)` for each
|
|---|
| 308 | entry being added. Return `true` to add the entry to the archive,
|
|---|
| 309 | or `false` to omit it.
|
|---|
| 310 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 311 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 312 | that `mtime` is still included, because this is necessary for other
|
|---|
| 313 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 314 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 315 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 316 | from absolute paths. [Alias: `P`]
|
|---|
| 317 | - `mode` The mode to set on the created file archive
|
|---|
| 318 | - `noDirRecurse` Do not recursively archive the contents of
|
|---|
| 319 | directories. [Alias: `n`]
|
|---|
| 320 | - `follow` Set to true to pack the targets of symbolic links. Without
|
|---|
| 321 | this option, symbolic links are archived as such. [Alias: `L`, `h`]
|
|---|
| 322 | - `noPax` Suppress pax extended headers. Note that this means that
|
|---|
| 323 | long paths and linkpaths will be truncated, and large or negative
|
|---|
| 324 | numeric values may be interpreted incorrectly.
|
|---|
| 325 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 326 | Note that this prevents using other mtime-based features like
|
|---|
| 327 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 328 | [Alias: `m`, `no-mtime`]
|
|---|
| 329 | - `mtime` Set to a `Date` object to force a specific `mtime` for
|
|---|
| 330 | everything added to the archive. Overridden by `noMtime`.
|
|---|
| 331 |
|
|---|
| 332 | The following options are mostly internal, but can be modified in some
|
|---|
| 333 | advanced use cases, such as re-using caches between runs.
|
|---|
| 334 |
|
|---|
| 335 | - `linkCache` A Map object containing the device and inode value for
|
|---|
| 336 | any file whose nlink is > 1, to identify hard links.
|
|---|
| 337 | - `statCache` A Map object that caches calls `lstat`.
|
|---|
| 338 | - `readdirCache` A Map object that caches calls to `readdir`.
|
|---|
| 339 | - `jobs` A number specifying how many concurrent jobs to run.
|
|---|
| 340 | Defaults to 4.
|
|---|
| 341 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 342 | Defaults to 16 MB.
|
|---|
| 343 |
|
|---|
| 344 | ### tar.x(options, fileList, callback) [alias: tar.extract]
|
|---|
| 345 |
|
|---|
| 346 | Extract a tarball archive.
|
|---|
| 347 |
|
|---|
| 348 | The `fileList` is an array of paths to extract from the tarball. If
|
|---|
| 349 | no paths are provided, then all the entries are extracted.
|
|---|
| 350 |
|
|---|
| 351 | If the archive is gzipped, then tar will detect this and unzip it.
|
|---|
| 352 |
|
|---|
| 353 | Note that all directories that are created will be forced to be
|
|---|
| 354 | writable, readable, and listable by their owner, to avoid cases where
|
|---|
| 355 | a directory prevents extraction of child entries by virtue of its
|
|---|
| 356 | mode.
|
|---|
| 357 |
|
|---|
| 358 | Most extraction errors will cause a `warn` event to be emitted. If
|
|---|
| 359 | the `cwd` is missing, or not a directory, then the extraction will
|
|---|
| 360 | fail completely.
|
|---|
| 361 |
|
|---|
| 362 | The following options are supported:
|
|---|
| 363 |
|
|---|
| 364 | - `cwd` Extract files relative to the specified directory. Defaults
|
|---|
| 365 | to `process.cwd()`. If provided, this must exist and must be a
|
|---|
| 366 | directory. [Alias: `C`]
|
|---|
| 367 | - `file` The archive file to extract. If not specified, then a
|
|---|
| 368 | Writable stream is returned where the archive data should be
|
|---|
| 369 | written. [Alias: `f`]
|
|---|
| 370 | - `sync` Create files and directories synchronously.
|
|---|
| 371 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 372 | - `filter` A function that gets called with `(path, entry)` for each
|
|---|
| 373 | entry being unpacked. Return `true` to unpack the entry from the
|
|---|
| 374 | archive, or `false` to skip it.
|
|---|
| 375 | - `newer` Set to true to keep the existing file on disk if it's newer
|
|---|
| 376 | than the file in the archive. [Alias: `keep-newer`,
|
|---|
| 377 | `keep-newer-files`]
|
|---|
| 378 | - `keep` Do not overwrite existing files. In particular, if a file
|
|---|
| 379 | appears more than once in an archive, later copies will not
|
|---|
| 380 | overwrite earlier copies. [Alias: `k`, `keep-existing`]
|
|---|
| 381 | - `preservePaths` Allow absolute paths, paths containing `..`, and
|
|---|
| 382 | extracting through symbolic links. By default, `/` is stripped from
|
|---|
| 383 | absolute paths, `..` paths are not extracted, and any file whose
|
|---|
| 384 | location would be modified by a symbolic link is not extracted.
|
|---|
| 385 | [Alias: `P`]
|
|---|
| 386 | - `unlink` Unlink files before creating them. Without this option,
|
|---|
| 387 | tar overwrites existing files, which preserves existing hardlinks.
|
|---|
| 388 | With this option, existing hardlinks will be broken, as will any
|
|---|
| 389 | symlink that would affect the location of an extracted file. [Alias:
|
|---|
| 390 | `U`]
|
|---|
| 391 | - `strip` Remove the specified number of leading path elements.
|
|---|
| 392 | Pathnames with fewer elements will be silently skipped. Note that
|
|---|
| 393 | the pathname is edited after applying the filter, but before
|
|---|
| 394 | security checks. [Alias: `strip-components`, `stripComponents`]
|
|---|
| 395 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 396 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 397 | - `preserveOwner` If true, tar will set the `uid` and `gid` of
|
|---|
| 398 | extracted entries to the `uid` and `gid` fields in the archive.
|
|---|
| 399 | This defaults to true when run as root, and false otherwise. If
|
|---|
| 400 | false, then files and directories will be set with the owner and
|
|---|
| 401 | group of the user running the process. This is similar to `-p` in
|
|---|
| 402 | `tar(1)`, but ACLs and other system-specific data is never unpacked
|
|---|
| 403 | in this implementation, and modes are set by default already.
|
|---|
| 404 | [Alias: `p`]
|
|---|
| 405 | - `uid` Set to a number to force ownership of all extracted files and
|
|---|
| 406 | folders, and all implicitly created directories, to be owned by the
|
|---|
| 407 | specified user id, regardless of the `uid` field in the archive.
|
|---|
| 408 | Cannot be used along with `preserveOwner`. Requires also setting a
|
|---|
| 409 | `gid` option.
|
|---|
| 410 | - `gid` Set to a number to force ownership of all extracted files and
|
|---|
| 411 | folders, and all implicitly created directories, to be owned by the
|
|---|
| 412 | specified group id, regardless of the `gid` field in the archive.
|
|---|
| 413 | Cannot be used along with `preserveOwner`. Requires also setting a
|
|---|
| 414 | `uid` option.
|
|---|
| 415 | - `noMtime` Set to true to omit writing `mtime` value for extracted
|
|---|
| 416 | entries. [Alias: `m`, `no-mtime`]
|
|---|
| 417 | - `transform` Provide a function that takes an `entry` object, and
|
|---|
| 418 | returns a stream, or any falsey value. If a stream is provided,
|
|---|
| 419 | then that stream's data will be written instead of the contents of
|
|---|
| 420 | the archive entry. If a falsey value is provided, then the entry is
|
|---|
| 421 | written to disk as normal. (To exclude items from extraction, use
|
|---|
| 422 | the `filter` option described above.)
|
|---|
| 423 | - `onentry` A function that gets called with `(entry)` for each entry
|
|---|
| 424 | that passes the filter.
|
|---|
| 425 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 426 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 427 | - `noChmod` Set to true to omit calling `fs.chmod()` to ensure that the
|
|---|
| 428 | extracted file matches the entry mode. This also suppresses the call to
|
|---|
| 429 | `process.umask()` to determine the default umask value, since tar will
|
|---|
| 430 | extract with whatever mode is provided, and let the process `umask` apply
|
|---|
| 431 | normally.
|
|---|
| 432 | - `maxDepth` The maximum depth of subfolders to extract into. This
|
|---|
| 433 | defaults to 1024. Anything deeper than the limit will raise a
|
|---|
| 434 | warning and skip the entry. Set to `Infinity` to remove the
|
|---|
| 435 | limitation.
|
|---|
| 436 |
|
|---|
| 437 | The following options are mostly internal, but can be modified in some
|
|---|
| 438 | advanced use cases, such as re-using caches between runs.
|
|---|
| 439 |
|
|---|
| 440 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 441 | Defaults to 16 MB.
|
|---|
| 442 | - `umask` Filter the modes of entries like `process.umask()`.
|
|---|
| 443 | - `dmode` Default mode for directories
|
|---|
| 444 | - `fmode` Default mode for files
|
|---|
| 445 | - `dirCache` A Map object of which directories exist.
|
|---|
| 446 | - `maxMetaEntrySize` The maximum size of meta entries that is
|
|---|
| 447 | supported. Defaults to 1 MB.
|
|---|
| 448 |
|
|---|
| 449 | Note that using an asynchronous stream type with the `transform`
|
|---|
| 450 | option will cause undefined behavior in sync extractions.
|
|---|
| 451 | [MiniPass](http://npm.im/minipass)-based streams are designed for this
|
|---|
| 452 | use case.
|
|---|
| 453 |
|
|---|
| 454 | ### tar.t(options, fileList, callback) [alias: tar.list]
|
|---|
| 455 |
|
|---|
| 456 | List the contents of a tarball archive.
|
|---|
| 457 |
|
|---|
| 458 | The `fileList` is an array of paths to list from the tarball. If
|
|---|
| 459 | no paths are provided, then all the entries are listed.
|
|---|
| 460 |
|
|---|
| 461 | If the archive is gzipped, then tar will detect this and unzip it.
|
|---|
| 462 |
|
|---|
| 463 | If the `file` option is _not_ provided, then returns an event emitter that
|
|---|
| 464 | emits `entry` events with `tar.ReadEntry` objects. However, they don't
|
|---|
| 465 | emit `'data'` or `'end'` events. (If you want to get actual readable
|
|---|
| 466 | entries, use the `tar.Parse` class instead.)
|
|---|
| 467 |
|
|---|
| 468 | If a `file` option _is_ provided, then the return value will be a promise
|
|---|
| 469 | that resolves when the file has been fully traversed in async mode, or
|
|---|
| 470 | `undefined` if `sync: true` is set. Thus, you _must_ specify an `onentry`
|
|---|
| 471 | method in order to do anything useful with the data it parses.
|
|---|
| 472 |
|
|---|
| 473 | The following options are supported:
|
|---|
| 474 |
|
|---|
| 475 | - `file` The archive file to list. If not specified, then a
|
|---|
| 476 | Writable stream is returned where the archive data should be
|
|---|
| 477 | written. [Alias: `f`]
|
|---|
| 478 | - `sync` Read the specified file synchronously. (This has no effect
|
|---|
| 479 | when a file option isn't specified, because entries are emitted as
|
|---|
| 480 | fast as they are parsed from the stream anyway.)
|
|---|
| 481 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 482 | - `filter` A function that gets called with `(path, entry)` for each
|
|---|
| 483 | entry being listed. Return `true` to emit the entry from the
|
|---|
| 484 | archive, or `false` to skip it.
|
|---|
| 485 | - `onentry` A function that gets called with `(entry)` for each entry
|
|---|
| 486 | that passes the filter. This is important for when `file` is set,
|
|---|
| 487 | because there is no other way to do anything useful with this method.
|
|---|
| 488 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 489 | Defaults to 16 MB.
|
|---|
| 490 | - `noResume` By default, `entry` streams are resumed immediately after
|
|---|
| 491 | the call to `onentry`. Set `noResume: true` to suppress this
|
|---|
| 492 | behavior. Note that by opting into this, the stream will never
|
|---|
| 493 | complete until the entry data is consumed.
|
|---|
| 494 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 495 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 496 |
|
|---|
| 497 | ### tar.u(options, fileList, callback) [alias: tar.update]
|
|---|
| 498 |
|
|---|
| 499 | Add files to an archive if they are newer than the entry already in
|
|---|
| 500 | the tarball archive.
|
|---|
| 501 |
|
|---|
| 502 | The `fileList` is an array of paths to add to the tarball. Adding a
|
|---|
| 503 | directory also adds its children recursively.
|
|---|
| 504 |
|
|---|
| 505 | An entry in `fileList` that starts with an `@` symbol is a tar archive
|
|---|
| 506 | whose entries will be added. To add a file that starts with `@`,
|
|---|
| 507 | prepend it with `./`.
|
|---|
| 508 |
|
|---|
| 509 | The following options are supported:
|
|---|
| 510 |
|
|---|
| 511 | - `file` Required. Write the tarball archive to the specified
|
|---|
| 512 | filename. [Alias: `f`]
|
|---|
| 513 | - `sync` Act synchronously. If this is set, then any provided file
|
|---|
| 514 | will be fully written after the call to `tar.c`.
|
|---|
| 515 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 516 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 517 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 518 | - `cwd` The current working directory for adding entries to the
|
|---|
| 519 | archive. Defaults to `process.cwd()`. [Alias: `C`]
|
|---|
| 520 | - `prefix` A path portion to prefix onto the entries in the archive.
|
|---|
| 521 | - `gzip` Set to any truthy value to create a gzipped archive, or an
|
|---|
| 522 | object with settings for `zlib.Gzip()` [Alias: `z`]
|
|---|
| 523 | - `filter` A function that gets called with `(path, stat)` for each
|
|---|
| 524 | entry being added. Return `true` to add the entry to the archive,
|
|---|
| 525 | or `false` to omit it.
|
|---|
| 526 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 527 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 528 | that `mtime` is still included, because this is necessary for other
|
|---|
| 529 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 530 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 531 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 532 | from absolute paths. [Alias: `P`]
|
|---|
| 533 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 534 | Defaults to 16 MB.
|
|---|
| 535 | - `noDirRecurse` Do not recursively archive the contents of
|
|---|
| 536 | directories. [Alias: `n`]
|
|---|
| 537 | - `follow` Set to true to pack the targets of symbolic links. Without
|
|---|
| 538 | this option, symbolic links are archived as such. [Alias: `L`, `h`]
|
|---|
| 539 | - `noPax` Suppress pax extended headers. Note that this means that
|
|---|
| 540 | long paths and linkpaths will be truncated, and large or negative
|
|---|
| 541 | numeric values may be interpreted incorrectly.
|
|---|
| 542 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 543 | Note that this prevents using other mtime-based features like
|
|---|
| 544 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 545 | [Alias: `m`, `no-mtime`]
|
|---|
| 546 | - `mtime` Set to a `Date` object to force a specific `mtime` for
|
|---|
| 547 | everything added to the archive. Overridden by `noMtime`.
|
|---|
| 548 |
|
|---|
| 549 | ### tar.r(options, fileList, callback) [alias: tar.replace]
|
|---|
| 550 |
|
|---|
| 551 | Add files to an existing archive. Because later entries override
|
|---|
| 552 | earlier entries, this effectively replaces any existing entries.
|
|---|
| 553 |
|
|---|
| 554 | The `fileList` is an array of paths to add to the tarball. Adding a
|
|---|
| 555 | directory also adds its children recursively.
|
|---|
| 556 |
|
|---|
| 557 | An entry in `fileList` that starts with an `@` symbol is a tar archive
|
|---|
| 558 | whose entries will be added. To add a file that starts with `@`,
|
|---|
| 559 | prepend it with `./`.
|
|---|
| 560 |
|
|---|
| 561 | The following options are supported:
|
|---|
| 562 |
|
|---|
| 563 | - `file` Required. Write the tarball archive to the specified
|
|---|
| 564 | filename. [Alias: `f`]
|
|---|
| 565 | - `sync` Act synchronously. If this is set, then any provided file
|
|---|
| 566 | will be fully written after the call to `tar.c`.
|
|---|
| 567 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 568 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 569 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 570 | - `cwd` The current working directory for adding entries to the
|
|---|
| 571 | archive. Defaults to `process.cwd()`. [Alias: `C`]
|
|---|
| 572 | - `prefix` A path portion to prefix onto the entries in the archive.
|
|---|
| 573 | - `gzip` Set to any truthy value to create a gzipped archive, or an
|
|---|
| 574 | object with settings for `zlib.Gzip()` [Alias: `z`]
|
|---|
| 575 | - `filter` A function that gets called with `(path, stat)` for each
|
|---|
| 576 | entry being added. Return `true` to add the entry to the archive,
|
|---|
| 577 | or `false` to omit it.
|
|---|
| 578 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 579 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 580 | that `mtime` is still included, because this is necessary for other
|
|---|
| 581 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 582 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 583 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 584 | from absolute paths. [Alias: `P`]
|
|---|
| 585 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 586 | Defaults to 16 MB.
|
|---|
| 587 | - `noDirRecurse` Do not recursively archive the contents of
|
|---|
| 588 | directories. [Alias: `n`]
|
|---|
| 589 | - `follow` Set to true to pack the targets of symbolic links. Without
|
|---|
| 590 | this option, symbolic links are archived as such. [Alias: `L`, `h`]
|
|---|
| 591 | - `noPax` Suppress pax extended headers. Note that this means that
|
|---|
| 592 | long paths and linkpaths will be truncated, and large or negative
|
|---|
| 593 | numeric values may be interpreted incorrectly.
|
|---|
| 594 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 595 | Note that this prevents using other mtime-based features like
|
|---|
| 596 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 597 | [Alias: `m`, `no-mtime`]
|
|---|
| 598 | - `mtime` Set to a `Date` object to force a specific `mtime` for
|
|---|
| 599 | everything added to the archive. Overridden by `noMtime`.
|
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 | ## Low-Level API
|
|---|
| 603 |
|
|---|
| 604 | ### class tar.Pack
|
|---|
| 605 |
|
|---|
| 606 | A readable tar stream.
|
|---|
| 607 |
|
|---|
| 608 | Has all the standard readable stream interface stuff. `'data'` and
|
|---|
| 609 | `'end'` events, `read()` method, `pause()` and `resume()`, etc.
|
|---|
| 610 |
|
|---|
| 611 | #### constructor(options)
|
|---|
| 612 |
|
|---|
| 613 | The following options are supported:
|
|---|
| 614 |
|
|---|
| 615 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 616 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 617 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 618 | - `cwd` The current working directory for creating the archive.
|
|---|
| 619 | Defaults to `process.cwd()`.
|
|---|
| 620 | - `prefix` A path portion to prefix onto the entries in the archive.
|
|---|
| 621 | - `gzip` Set to any truthy value to create a gzipped archive, or an
|
|---|
| 622 | object with settings for `zlib.Gzip()`
|
|---|
| 623 | - `filter` A function that gets called with `(path, stat)` for each
|
|---|
| 624 | entry being added. Return `true` to add the entry to the archive,
|
|---|
| 625 | or `false` to omit it.
|
|---|
| 626 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 627 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 628 | that `mtime` is still included, because this is necessary for other
|
|---|
| 629 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 630 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 631 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 632 | from absolute paths.
|
|---|
| 633 | - `linkCache` A Map object containing the device and inode value for
|
|---|
| 634 | any file whose nlink is > 1, to identify hard links.
|
|---|
| 635 | - `statCache` A Map object that caches calls `lstat`.
|
|---|
| 636 | - `readdirCache` A Map object that caches calls to `readdir`.
|
|---|
| 637 | - `jobs` A number specifying how many concurrent jobs to run.
|
|---|
| 638 | Defaults to 4.
|
|---|
| 639 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 640 | Defaults to 16 MB.
|
|---|
| 641 | - `noDirRecurse` Do not recursively archive the contents of
|
|---|
| 642 | directories.
|
|---|
| 643 | - `follow` Set to true to pack the targets of symbolic links. Without
|
|---|
| 644 | this option, symbolic links are archived as such.
|
|---|
| 645 | - `noPax` Suppress pax extended headers. Note that this means that
|
|---|
| 646 | long paths and linkpaths will be truncated, and large or negative
|
|---|
| 647 | numeric values may be interpreted incorrectly.
|
|---|
| 648 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 649 | Note that this prevents using other mtime-based features like
|
|---|
| 650 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 651 | - `mtime` Set to a `Date` object to force a specific `mtime` for
|
|---|
| 652 | everything added to the archive. Overridden by `noMtime`.
|
|---|
| 653 |
|
|---|
| 654 | #### add(path)
|
|---|
| 655 |
|
|---|
| 656 | Adds an entry to the archive. Returns the Pack stream.
|
|---|
| 657 |
|
|---|
| 658 | #### write(path)
|
|---|
| 659 |
|
|---|
| 660 | Adds an entry to the archive. Returns true if flushed.
|
|---|
| 661 |
|
|---|
| 662 | #### end()
|
|---|
| 663 |
|
|---|
| 664 | Finishes the archive.
|
|---|
| 665 |
|
|---|
| 666 | ### class tar.Pack.Sync
|
|---|
| 667 |
|
|---|
| 668 | Synchronous version of `tar.Pack`.
|
|---|
| 669 |
|
|---|
| 670 | ### class tar.Unpack
|
|---|
| 671 |
|
|---|
| 672 | A writable stream that unpacks a tar archive onto the file system.
|
|---|
| 673 |
|
|---|
| 674 | All the normal writable stream stuff is supported. `write()` and
|
|---|
| 675 | `end()` methods, `'drain'` events, etc.
|
|---|
| 676 |
|
|---|
| 677 | Note that all directories that are created will be forced to be
|
|---|
| 678 | writable, readable, and listable by their owner, to avoid cases where
|
|---|
| 679 | a directory prevents extraction of child entries by virtue of its
|
|---|
| 680 | mode.
|
|---|
| 681 |
|
|---|
| 682 | `'close'` is emitted when it's done writing stuff to the file system.
|
|---|
| 683 |
|
|---|
| 684 | Most unpack errors will cause a `warn` event to be emitted. If the
|
|---|
| 685 | `cwd` is missing, or not a directory, then an error will be emitted.
|
|---|
| 686 |
|
|---|
| 687 | #### constructor(options)
|
|---|
| 688 |
|
|---|
| 689 | - `cwd` Extract files relative to the specified directory. Defaults
|
|---|
| 690 | to `process.cwd()`. If provided, this must exist and must be a
|
|---|
| 691 | directory.
|
|---|
| 692 | - `filter` A function that gets called with `(path, entry)` for each
|
|---|
| 693 | entry being unpacked. Return `true` to unpack the entry from the
|
|---|
| 694 | archive, or `false` to skip it.
|
|---|
| 695 | - `newer` Set to true to keep the existing file on disk if it's newer
|
|---|
| 696 | than the file in the archive.
|
|---|
| 697 | - `keep` Do not overwrite existing files. In particular, if a file
|
|---|
| 698 | appears more than once in an archive, later copies will not
|
|---|
| 699 | overwrite earlier copies.
|
|---|
| 700 | - `preservePaths` Allow absolute paths, paths containing `..`, and
|
|---|
| 701 | extracting through symbolic links. By default, `/` is stripped from
|
|---|
| 702 | absolute paths, `..` paths are not extracted, and any file whose
|
|---|
| 703 | location would be modified by a symbolic link is not extracted.
|
|---|
| 704 | - `unlink` Unlink files before creating them. Without this option,
|
|---|
| 705 | tar overwrites existing files, which preserves existing hardlinks.
|
|---|
| 706 | With this option, existing hardlinks will be broken, as will any
|
|---|
| 707 | symlink that would affect the location of an extracted file.
|
|---|
| 708 | - `strip` Remove the specified number of leading path elements.
|
|---|
| 709 | Pathnames with fewer elements will be silently skipped. Note that
|
|---|
| 710 | the pathname is edited after applying the filter, but before
|
|---|
| 711 | security checks.
|
|---|
| 712 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 713 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 714 | - `umask` Filter the modes of entries like `process.umask()`.
|
|---|
| 715 | - `dmode` Default mode for directories
|
|---|
| 716 | - `fmode` Default mode for files
|
|---|
| 717 | - `dirCache` A Map object of which directories exist.
|
|---|
| 718 | - `maxMetaEntrySize` The maximum size of meta entries that is
|
|---|
| 719 | supported. Defaults to 1 MB.
|
|---|
| 720 | - `preserveOwner` If true, tar will set the `uid` and `gid` of
|
|---|
| 721 | extracted entries to the `uid` and `gid` fields in the archive.
|
|---|
| 722 | This defaults to true when run as root, and false otherwise. If
|
|---|
| 723 | false, then files and directories will be set with the owner and
|
|---|
| 724 | group of the user running the process. This is similar to `-p` in
|
|---|
| 725 | `tar(1)`, but ACLs and other system-specific data is never unpacked
|
|---|
| 726 | in this implementation, and modes are set by default already.
|
|---|
| 727 | - `win32` True if on a windows platform. Causes behavior where
|
|---|
| 728 | filenames containing `<|>?` chars are converted to
|
|---|
| 729 | windows-compatible values while being unpacked.
|
|---|
| 730 | - `uid` Set to a number to force ownership of all extracted files and
|
|---|
| 731 | folders, and all implicitly created directories, to be owned by the
|
|---|
| 732 | specified user id, regardless of the `uid` field in the archive.
|
|---|
| 733 | Cannot be used along with `preserveOwner`. Requires also setting a
|
|---|
| 734 | `gid` option.
|
|---|
| 735 | - `gid` Set to a number to force ownership of all extracted files and
|
|---|
| 736 | folders, and all implicitly created directories, to be owned by the
|
|---|
| 737 | specified group id, regardless of the `gid` field in the archive.
|
|---|
| 738 | Cannot be used along with `preserveOwner`. Requires also setting a
|
|---|
| 739 | `uid` option.
|
|---|
| 740 | - `noMtime` Set to true to omit writing `mtime` value for extracted
|
|---|
| 741 | entries.
|
|---|
| 742 | - `transform` Provide a function that takes an `entry` object, and
|
|---|
| 743 | returns a stream, or any falsey value. If a stream is provided,
|
|---|
| 744 | then that stream's data will be written instead of the contents of
|
|---|
| 745 | the archive entry. If a falsey value is provided, then the entry is
|
|---|
| 746 | written to disk as normal. (To exclude items from extraction, use
|
|---|
| 747 | the `filter` option described above.)
|
|---|
| 748 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 749 | - `onentry` A function that gets called with `(entry)` for each entry
|
|---|
| 750 | that passes the filter.
|
|---|
| 751 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 752 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 753 | - `noChmod` Set to true to omit calling `fs.chmod()` to ensure that the
|
|---|
| 754 | extracted file matches the entry mode. This also suppresses the call to
|
|---|
| 755 | `process.umask()` to determine the default umask value, since tar will
|
|---|
| 756 | extract with whatever mode is provided, and let the process `umask` apply
|
|---|
| 757 | normally.
|
|---|
| 758 | - `maxDepth` The maximum depth of subfolders to extract into. This
|
|---|
| 759 | defaults to 1024. Anything deeper than the limit will raise a
|
|---|
| 760 | warning and skip the entry. Set to `Infinity` to remove the
|
|---|
| 761 | limitation.
|
|---|
| 762 |
|
|---|
| 763 | ### class tar.Unpack.Sync
|
|---|
| 764 |
|
|---|
| 765 | Synchronous version of `tar.Unpack`.
|
|---|
| 766 |
|
|---|
| 767 | Note that using an asynchronous stream type with the `transform`
|
|---|
| 768 | option will cause undefined behavior in sync unpack streams.
|
|---|
| 769 | [MiniPass](http://npm.im/minipass)-based streams are designed for this
|
|---|
| 770 | use case.
|
|---|
| 771 |
|
|---|
| 772 | ### class tar.Parse
|
|---|
| 773 |
|
|---|
| 774 | A writable stream that parses a tar archive stream. All the standard
|
|---|
| 775 | writable stream stuff is supported.
|
|---|
| 776 |
|
|---|
| 777 | If the archive is gzipped, then tar will detect this and unzip it.
|
|---|
| 778 |
|
|---|
| 779 | Emits `'entry'` events with `tar.ReadEntry` objects, which are
|
|---|
| 780 | themselves readable streams that you can pipe wherever.
|
|---|
| 781 |
|
|---|
| 782 | Each `entry` will not emit until the one before it is flushed through,
|
|---|
| 783 | so make sure to either consume the data (with `on('data', ...)` or
|
|---|
| 784 | `.pipe(...)`) or throw it away with `.resume()` to keep the stream
|
|---|
| 785 | flowing.
|
|---|
| 786 |
|
|---|
| 787 | #### constructor(options)
|
|---|
| 788 |
|
|---|
| 789 | Returns an event emitter that emits `entry` events with
|
|---|
| 790 | `tar.ReadEntry` objects.
|
|---|
| 791 |
|
|---|
| 792 | The following options are supported:
|
|---|
| 793 |
|
|---|
| 794 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 795 | - `filter` A function that gets called with `(path, entry)` for each
|
|---|
| 796 | entry being listed. Return `true` to emit the entry from the
|
|---|
| 797 | archive, or `false` to skip it.
|
|---|
| 798 | - `onentry` A function that gets called with `(entry)` for each entry
|
|---|
| 799 | that passes the filter.
|
|---|
| 800 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 801 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 802 |
|
|---|
| 803 | #### abort(error)
|
|---|
| 804 |
|
|---|
| 805 | Stop all parsing activities. This is called when there are zlib
|
|---|
| 806 | errors. It also emits an unrecoverable warning with the error provided.
|
|---|
| 807 |
|
|---|
| 808 | ### class tar.ReadEntry extends [MiniPass](http://npm.im/minipass)
|
|---|
| 809 |
|
|---|
| 810 | A representation of an entry that is being read out of a tar archive.
|
|---|
| 811 |
|
|---|
| 812 | It has the following fields:
|
|---|
| 813 |
|
|---|
| 814 | - `extended` The extended metadata object provided to the constructor.
|
|---|
| 815 | - `globalExtended` The global extended metadata object provided to the
|
|---|
| 816 | constructor.
|
|---|
| 817 | - `remain` The number of bytes remaining to be written into the
|
|---|
| 818 | stream.
|
|---|
| 819 | - `blockRemain` The number of 512-byte blocks remaining to be written
|
|---|
| 820 | into the stream.
|
|---|
| 821 | - `ignore` Whether this entry should be ignored.
|
|---|
| 822 | - `meta` True if this represents metadata about the next entry, false
|
|---|
| 823 | if it represents a filesystem object.
|
|---|
| 824 | - All the fields from the header, extended header, and global extended
|
|---|
| 825 | header are added to the ReadEntry object. So it has `path`, `type`,
|
|---|
| 826 | `size`, `mode`, and so on.
|
|---|
| 827 |
|
|---|
| 828 | #### constructor(header, extended, globalExtended)
|
|---|
| 829 |
|
|---|
| 830 | Create a new ReadEntry object with the specified header, extended
|
|---|
| 831 | header, and global extended header values.
|
|---|
| 832 |
|
|---|
| 833 | ### class tar.WriteEntry extends [MiniPass](http://npm.im/minipass)
|
|---|
| 834 |
|
|---|
| 835 | A representation of an entry that is being written from the file
|
|---|
| 836 | system into a tar archive.
|
|---|
| 837 |
|
|---|
| 838 | Emits data for the Header, and for the Pax Extended Header if one is
|
|---|
| 839 | required, as well as any body data.
|
|---|
| 840 |
|
|---|
| 841 | Creating a WriteEntry for a directory does not also create
|
|---|
| 842 | WriteEntry objects for all of the directory contents.
|
|---|
| 843 |
|
|---|
| 844 | It has the following fields:
|
|---|
| 845 |
|
|---|
| 846 | - `path` The path field that will be written to the archive. By
|
|---|
| 847 | default, this is also the path from the cwd to the file system
|
|---|
| 848 | object.
|
|---|
| 849 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 850 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 851 | that `mtime` is still included, because this is necessary for other
|
|---|
| 852 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 853 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 854 | - `myuid` If supported, the uid of the user running the current
|
|---|
| 855 | process.
|
|---|
| 856 | - `myuser` The `env.USER` string if set, or `''`. Set as the entry
|
|---|
| 857 | `uname` field if the file's `uid` matches `this.myuid`.
|
|---|
| 858 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 859 | Defaults to 1 MB.
|
|---|
| 860 | - `linkCache` A Map object containing the device and inode value for
|
|---|
| 861 | any file whose nlink is > 1, to identify hard links.
|
|---|
| 862 | - `statCache` A Map object that caches calls `lstat`.
|
|---|
| 863 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 864 | from absolute paths.
|
|---|
| 865 | - `cwd` The current working directory for creating the archive.
|
|---|
| 866 | Defaults to `process.cwd()`.
|
|---|
| 867 | - `absolute` The absolute path to the entry on the filesystem. By
|
|---|
| 868 | default, this is `path.resolve(this.cwd, this.path)`, but it can be
|
|---|
| 869 | overridden explicitly.
|
|---|
| 870 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 871 | - `win32` True if on a windows platform. Causes behavior where paths
|
|---|
| 872 | replace `\` with `/` and filenames containing the windows-compatible
|
|---|
| 873 | forms of `<|>?:` characters are converted to actual `<|>?:` characters
|
|---|
| 874 | in the archive.
|
|---|
| 875 | - `noPax` Suppress pax extended headers. Note that this means that
|
|---|
| 876 | long paths and linkpaths will be truncated, and large or negative
|
|---|
| 877 | numeric values may be interpreted incorrectly.
|
|---|
| 878 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 879 | Note that this prevents using other mtime-based features like
|
|---|
| 880 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 | #### constructor(path, options)
|
|---|
| 884 |
|
|---|
| 885 | `path` is the path of the entry as it is written in the archive.
|
|---|
| 886 |
|
|---|
| 887 | The following options are supported:
|
|---|
| 888 |
|
|---|
| 889 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 890 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 891 | that `mtime` is still included, because this is necessary for other
|
|---|
| 892 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 893 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 894 | - `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|---|
| 895 | Defaults to 1 MB.
|
|---|
| 896 | - `linkCache` A Map object containing the device and inode value for
|
|---|
| 897 | any file whose nlink is > 1, to identify hard links.
|
|---|
| 898 | - `statCache` A Map object that caches calls `lstat`.
|
|---|
| 899 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 900 | from absolute paths.
|
|---|
| 901 | - `cwd` The current working directory for creating the archive.
|
|---|
| 902 | Defaults to `process.cwd()`.
|
|---|
| 903 | - `absolute` The absolute path to the entry on the filesystem. By
|
|---|
| 904 | default, this is `path.resolve(this.cwd, this.path)`, but it can be
|
|---|
| 905 | overridden explicitly.
|
|---|
| 906 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 907 | - `win32` True if on a windows platform. Causes behavior where paths
|
|---|
| 908 | replace `\` with `/`.
|
|---|
| 909 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 910 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 911 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 912 | Note that this prevents using other mtime-based features like
|
|---|
| 913 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 914 | - `umask` Set to restrict the modes on the entries in the archive,
|
|---|
| 915 | somewhat like how umask works on file creation. Defaults to
|
|---|
| 916 | `process.umask()` on unix systems, or `0o22` on Windows.
|
|---|
| 917 |
|
|---|
| 918 | #### warn(message, data)
|
|---|
| 919 |
|
|---|
| 920 | If strict, emit an error with the provided message.
|
|---|
| 921 |
|
|---|
| 922 | Othewise, emit a `'warn'` event with the provided message and data.
|
|---|
| 923 |
|
|---|
| 924 | ### class tar.WriteEntry.Sync
|
|---|
| 925 |
|
|---|
| 926 | Synchronous version of tar.WriteEntry
|
|---|
| 927 |
|
|---|
| 928 | ### class tar.WriteEntry.Tar
|
|---|
| 929 |
|
|---|
| 930 | A version of tar.WriteEntry that gets its data from a tar.ReadEntry
|
|---|
| 931 | instead of from the filesystem.
|
|---|
| 932 |
|
|---|
| 933 | #### constructor(readEntry, options)
|
|---|
| 934 |
|
|---|
| 935 | `readEntry` is the entry being read out of another archive.
|
|---|
| 936 |
|
|---|
| 937 | The following options are supported:
|
|---|
| 938 |
|
|---|
| 939 | - `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|---|
| 940 | `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|---|
| 941 | that `mtime` is still included, because this is necessary for other
|
|---|
| 942 | time-based operations. Additionally, `mode` is set to a "reasonable
|
|---|
| 943 | default" for most unix systems, based on a `umask` value of `0o22`.
|
|---|
| 944 | - `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|---|
| 945 | from absolute paths.
|
|---|
| 946 | - `strict` Treat warnings as crash-worthy errors. Default false.
|
|---|
| 947 | - `onwarn` A function that will get called with `(code, message, data)` for
|
|---|
| 948 | any warnings encountered. (See "Warnings and Errors")
|
|---|
| 949 | - `noMtime` Set to true to omit writing `mtime` values for entries.
|
|---|
| 950 | Note that this prevents using other mtime-based features like
|
|---|
| 951 | `tar.update` or the `keepNewer` option with the resulting tar archive.
|
|---|
| 952 |
|
|---|
| 953 | ### class tar.Header
|
|---|
| 954 |
|
|---|
| 955 | A class for reading and writing header blocks.
|
|---|
| 956 |
|
|---|
| 957 | It has the following fields:
|
|---|
| 958 |
|
|---|
| 959 | - `nullBlock` True if decoding a block which is entirely composed of
|
|---|
| 960 | `0x00` null bytes. (Useful because tar files are terminated by
|
|---|
| 961 | at least 2 null blocks.)
|
|---|
| 962 | - `cksumValid` True if the checksum in the header is valid, false
|
|---|
| 963 | otherwise.
|
|---|
| 964 | - `needPax` True if the values, as encoded, will require a Pax
|
|---|
| 965 | extended header.
|
|---|
| 966 | - `path` The path of the entry.
|
|---|
| 967 | - `mode` The 4 lowest-order octal digits of the file mode. That is,
|
|---|
| 968 | read/write/execute permissions for world, group, and owner, and the
|
|---|
| 969 | setuid, setgid, and sticky bits.
|
|---|
| 970 | - `uid` Numeric user id of the file owner
|
|---|
| 971 | - `gid` Numeric group id of the file owner
|
|---|
| 972 | - `size` Size of the file in bytes
|
|---|
| 973 | - `mtime` Modified time of the file
|
|---|
| 974 | - `cksum` The checksum of the header. This is generated by adding all
|
|---|
| 975 | the bytes of the header block, treating the checksum field itself as
|
|---|
| 976 | all ascii space characters (that is, `0x20`).
|
|---|
| 977 | - `type` The human-readable name of the type of entry this represents,
|
|---|
| 978 | or the alphanumeric key if unknown.
|
|---|
| 979 | - `typeKey` The alphanumeric key for the type of entry this header
|
|---|
| 980 | represents.
|
|---|
| 981 | - `linkpath` The target of Link and SymbolicLink entries.
|
|---|
| 982 | - `uname` Human-readable user name of the file owner
|
|---|
| 983 | - `gname` Human-readable group name of the file owner
|
|---|
| 984 | - `devmaj` The major portion of the device number. Always `0` for
|
|---|
| 985 | files, directories, and links.
|
|---|
| 986 | - `devmin` The minor portion of the device number. Always `0` for
|
|---|
| 987 | files, directories, and links.
|
|---|
| 988 | - `atime` File access time.
|
|---|
| 989 | - `ctime` File change time.
|
|---|
| 990 |
|
|---|
| 991 | #### constructor(data, [offset=0])
|
|---|
| 992 |
|
|---|
| 993 | `data` is optional. It is either a Buffer that should be interpreted
|
|---|
| 994 | as a tar Header starting at the specified offset and continuing for
|
|---|
| 995 | 512 bytes, or a data object of keys and values to set on the header
|
|---|
| 996 | object, and eventually encode as a tar Header.
|
|---|
| 997 |
|
|---|
| 998 | #### decode(block, offset)
|
|---|
| 999 |
|
|---|
| 1000 | Decode the provided buffer starting at the specified offset.
|
|---|
| 1001 |
|
|---|
| 1002 | Buffer length must be greater than 512 bytes.
|
|---|
| 1003 |
|
|---|
| 1004 | #### set(data)
|
|---|
| 1005 |
|
|---|
| 1006 | Set the fields in the data object.
|
|---|
| 1007 |
|
|---|
| 1008 | #### encode(buffer, offset)
|
|---|
| 1009 |
|
|---|
| 1010 | Encode the header fields into the buffer at the specified offset.
|
|---|
| 1011 |
|
|---|
| 1012 | Returns `this.needPax` to indicate whether a Pax Extended Header is
|
|---|
| 1013 | required to properly encode the specified data.
|
|---|
| 1014 |
|
|---|
| 1015 | ### class tar.Pax
|
|---|
| 1016 |
|
|---|
| 1017 | An object representing a set of key-value pairs in an Pax extended
|
|---|
| 1018 | header entry.
|
|---|
| 1019 |
|
|---|
| 1020 | It has the following fields. Where the same name is used, they have
|
|---|
| 1021 | the same semantics as the tar.Header field of the same name.
|
|---|
| 1022 |
|
|---|
| 1023 | - `global` True if this represents a global extended header, or false
|
|---|
| 1024 | if it is for a single entry.
|
|---|
| 1025 | - `atime`
|
|---|
| 1026 | - `charset`
|
|---|
| 1027 | - `comment`
|
|---|
| 1028 | - `ctime`
|
|---|
| 1029 | - `gid`
|
|---|
| 1030 | - `gname`
|
|---|
| 1031 | - `linkpath`
|
|---|
| 1032 | - `mtime`
|
|---|
| 1033 | - `path`
|
|---|
| 1034 | - `size`
|
|---|
| 1035 | - `uid`
|
|---|
| 1036 | - `uname`
|
|---|
| 1037 | - `dev`
|
|---|
| 1038 | - `ino`
|
|---|
| 1039 | - `nlink`
|
|---|
| 1040 |
|
|---|
| 1041 | #### constructor(object, global)
|
|---|
| 1042 |
|
|---|
| 1043 | Set the fields set in the object. `global` is a boolean that defaults
|
|---|
| 1044 | to false.
|
|---|
| 1045 |
|
|---|
| 1046 | #### encode()
|
|---|
| 1047 |
|
|---|
| 1048 | Return a Buffer containing the header and body for the Pax extended
|
|---|
| 1049 | header entry, or `null` if there is nothing to encode.
|
|---|
| 1050 |
|
|---|
| 1051 | #### encodeBody()
|
|---|
| 1052 |
|
|---|
| 1053 | Return a string representing the body of the pax extended header
|
|---|
| 1054 | entry.
|
|---|
| 1055 |
|
|---|
| 1056 | #### encodeField(fieldName)
|
|---|
| 1057 |
|
|---|
| 1058 | Return a string representing the key/value encoding for the specified
|
|---|
| 1059 | fieldName, or `''` if the field is unset.
|
|---|
| 1060 |
|
|---|
| 1061 | ### tar.Pax.parse(string, extended, global)
|
|---|
| 1062 |
|
|---|
| 1063 | Return a new Pax object created by parsing the contents of the string
|
|---|
| 1064 | provided.
|
|---|
| 1065 |
|
|---|
| 1066 | If the `extended` object is set, then also add the fields from that
|
|---|
| 1067 | object. (This is necessary because multiple metadata entries can
|
|---|
| 1068 | occur in sequence.)
|
|---|
| 1069 |
|
|---|
| 1070 | ### tar.types
|
|---|
| 1071 |
|
|---|
| 1072 | A translation table for the `type` field in tar headers.
|
|---|
| 1073 |
|
|---|
| 1074 | #### tar.types.name.get(code)
|
|---|
| 1075 |
|
|---|
| 1076 | Get the human-readable name for a given alphanumeric code.
|
|---|
| 1077 |
|
|---|
| 1078 | #### tar.types.code.get(name)
|
|---|
| 1079 |
|
|---|
| 1080 | Get the alphanumeric code for a given human-readable name.
|
|---|