b059e49c1b985b44c671366888f71c4fc3ea9ba7a0a53e7a6b23cda71f9492c59b2b657d7e91192f726a045d3200d47257429286d97d533db654be92576e10 45 KB

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