web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1042 lines
44 KiB

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