版博士V2.0程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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