UNPKG

23.1 kBTypeScriptView Raw
1/// <reference types="node" />
2import { type GzipOptions, type ZlibOptions } from 'minizlib';
3import { type Stats } from 'node:fs';
4import { type ReadEntry } from './read-entry.js';
5import { type WarnData } from './warn-method.js';
6import { WriteEntry } from './write-entry.js';
7/**
8 * The options that can be provided to tar commands.
9 *
10 * Note that some of these are only relevant for certain commands, since
11 * they are specific to reading or writing.
12 *
13 * Aliases are provided in the {@link TarOptionsWithAliases} type.
14 */
15export interface TarOptions {
16 /**
17 * Perform all I/O operations synchronously. If the stream is ended
18 * immediately, then it will be processed entirely synchronously.
19 */
20 sync?: boolean;
21 /**
22 * The tar file to be read and/or written. When this is set, a stream
23 * is not returned. Asynchronous commands will return a promise indicating
24 * when the operation is completed, and synchronous commands will return
25 * immediately.
26 */
27 file?: string;
28 /**
29 * Treat warnings as crash-worthy errors. Defaults false.
30 */
31 strict?: boolean;
32 /**
33 * The effective current working directory for this tar command
34 */
35 cwd?: string;
36 /**
37 * When creating a tar archive, this can be used to compress it as well.
38 * Set to `true` to use the default gzip options, or customize them as
39 * needed.
40 *
41 * When reading, if this is unset, then the compression status will be
42 * inferred from the archive data. This is generally best, unless you are
43 * sure of the compression settings in use to create the archive, and want to
44 * fail if the archive doesn't match expectations.
45 */
46 gzip?: boolean | GzipOptions;
47 /**
48 * When creating archives, preserve absolute and `..` paths in the archive,
49 * rather than sanitizing them under the cwd.
50 *
51 * When extracting, allow absolute paths, paths containing `..`, and
52 * extracting through symbolic links. By default, the root `/` is stripped
53 * from absolute paths (eg, turning `/x/y/z` into `x/y/z`), paths containing
54 * `..` are not extracted, and any file whose location would be modified by a
55 * symbolic link is not extracted.
56 *
57 * **WARNING** This is almost always unsafe, and must NEVER be used on
58 * archives from untrusted sources, such as user input, and every entry must
59 * be validated to ensure it is safe to write. Even if the input is not
60 * malicious, mistakes can cause a lot of damage!
61 */
62 preservePaths?: boolean;
63 /**
64 * When extracting, do not set the `mtime` value for extracted entries to
65 * match the `mtime` in the archive.
66 *
67 * When creating archives, do not store the `mtime` value in the entry. Note
68 * that this prevents properly using other mtime-based features (such as
69 * `tar.update` or the `newer` option) with the resulting archive.
70 */
71 noMtime?: boolean;
72 /**
73 * Set to `true` or an object with settings for `zlib.BrotliCompress()` to
74 * create a brotli-compressed archive
75 *
76 * When extracting, this will cause the archive to be treated as a
77 * brotli-compressed file if set to `true` or a ZlibOptions object.
78 *
79 * If set `false`, then brotli options will not be used.
80 *
81 * If both this and the `gzip` option are left `undefined`, then tar will
82 * attempt to infer the brotli compression status, but can only do so based
83 * on the filename. If the filename ends in `.tbr` or `.tar.br`, and the
84 * first 512 bytes are not a valid tar header, then brotli decompression
85 * will be attempted.
86 */
87 brotli?: boolean | ZlibOptions;
88 /**
89 * A function that is called with `(path, stat)` when creating an archive, or
90 * `(path, entry)` when extracting. Return true to process the file/entry, or
91 * false to exclude it.
92 */
93 filter?: (path: string, entry: Stats | ReadEntry) => boolean;
94 /**
95 * A function that gets called for any warning encountered.
96 *
97 * Note: if `strict` is set, then the warning will throw, and this method
98 * will not be called.
99 */
100 onwarn?: (code: string, message: string, data: WarnData) => any;
101 /**
102 * When extracting, unlink files before creating them. Without this option,
103 * tar overwrites existing files, which preserves existing hardlinks. With
104 * this option, existing hardlinks will be broken, as will any symlink that
105 * would affect the location of an extracted file.
106 */
107 unlink?: boolean;
108 /**
109 * When extracting, strip the specified number of path portions from the
110 * entry path. For example, with `{strip: 2}`, the entry `a/b/c/d` would be
111 * extracted to `{cwd}/c/d`.
112 *
113 * Any entry whose entire path is stripped will be excluded.
114 */
115 strip?: number;
116 /**
117 * When extracting, keep the existing file on disk if it's newer than the
118 * file in the archive.
119 */
120 newer?: boolean;
121 /**
122 * When extracting, do not overwrite existing files at all.
123 */
124 keep?: boolean;
125 /**
126 * When extracting, set the `uid` and `gid` of extracted entries to the `uid`
127 * and `gid` fields in the archive. Defaults to true when run as root, and
128 * false otherwise.
129 *
130 * If false, then files and directories will be set with the owner and group
131 * of the user running the process. This is similar to `-p` in `tar(1)`, but
132 * ACLs and other system-specific data is never unpacked in this
133 * implementation, and modes are set by default already.
134 */
135 preserveOwner?: boolean;
136 /**
137 * The maximum depth of subfolders to extract into. This defaults to 1024.
138 * Anything deeper than the limit will raise a warning and skip the entry.
139 * Set to `Infinity` to remove the limitation.
140 */
141 maxDepth?: number;
142 /**
143 * When extracting, force all created files and directories, and all
144 * implicitly created directories, to be owned by the specified user id,
145 * regardless of the `uid` field in the archive.
146 *
147 * Cannot be used along with `preserveOwner`. Requires also setting the `gid`
148 * option.
149 */
150 uid?: number;
151 /**
152 * When extracting, force all created files and directories, and all
153 * implicitly created directories, to be owned by the specified group id,
154 * regardless of the `gid` field in the archive.
155 *
156 * Cannot be used along with `preserveOwner`. Requires also setting the `uid`
157 * option.
158 */
159 gid?: number;
160 /**
161 * When extracting, provide a function that takes an `entry` object, and
162 * returns a stream, or any falsey value. If a stream is provided, then that
163 * stream's data will be written instead of the contents of the archive
164 * entry. If a falsey value is provided, then the entry is written to disk as
165 * normal.
166 *
167 * To exclude items from extraction, use the `filter` option.
168 *
169 * Note that using an asynchronous stream type with the `transform` option
170 * will cause undefined behavior in synchronous extractions.
171 * [MiniPass](http://npm.im/minipass)-based streams are designed for this use
172 * case.
173 */
174 transform?: (entry: ReadEntry) => any;
175 /**
176 * Call `chmod()` to ensure that extracted files match the entry's mode
177 * field. Without this field set, all mode fields in archive entries are a
178 * best effort attempt only.
179 *
180 * Setting this necessitates a call to the deprecated `process.umask()`
181 * method to determine the default umask value, unless a `processUmask`
182 * config is provided as well.
183 *
184 * If not set, tar will attempt to create file system entries with whatever
185 * mode is provided, and let the implicit process `umask` apply normally, but
186 * if a file already exists to be written to, then its existing mode will not
187 * be modified.
188 *
189 * When setting `chmod: true`, it is highly recommend to set the
190 * {@link TarOptions#processUmask} option as well, to avoid the call to the
191 * deprecated (and thread-unsafe) `process.umask()` method.
192 */
193 chmod?: boolean;
194 /**
195 * When setting the {@link TarOptions#chmod} option to `true`, you may
196 * provide a value here to avoid having to call the deprecated and
197 * thread-unsafe `process.umask()` method.
198 *
199 * This has no effect with `chmod` is not set to true, as mode values are not
200 * set explicitly anyway. If `chmod` is set to `true`, and a value is not
201 * provided here, then `process.umask()` must be called, which will result in
202 * deprecation warnings.
203 *
204 * The most common values for this are `0o22` (resulting in directories
205 * created with mode `0o755` and files with `0o644` by default) and `0o2`
206 * (resulting in directores created with mode `0o775` and files `0o664`, so
207 * they are group-writable).
208 */
209 processUmask?: number;
210 /**
211 * When parsing/listing archives, `entry` streams are by default resumed
212 * (set into "flowing" mode) immediately after the call to `onReadEntry()`.
213 * Set `noResume: true` to suppress this behavior.
214 *
215 * Note that when this is set, the stream will never complete until the
216 * data is consumed somehow.
217 *
218 * Set automatically in extract operations, since the entry is piped to
219 * a file system entry right away. Only relevant when parsing.
220 */
221 noResume?: boolean;
222 /**
223 * When creating, updating, or replacing within archives, this method will
224 * be called with each WriteEntry that is created.
225 */
226 onWriteEntry?: (entry: WriteEntry) => any;
227 /**
228 * When extracting or listing archives, this method will be called with
229 * each entry that is not excluded by a `filter`.
230 *
231 * Important when listing archives synchronously from a file, because there
232 * is otherwise no way to interact with the data!
233 */
234 onReadEntry?: (entry: ReadEntry) => any;
235 /**
236 * Pack the targets of symbolic links rather than the link itself.
237 */
238 follow?: boolean;
239 /**
240 * When creating archives, omit any metadata that is system-specific:
241 * `ctime`, `atime`, `uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and
242 * `nlink`. Note that `mtime` is still included, because this is necessary
243 * for other time-based operations such as `tar.update`. Additionally, `mode`
244 * is set to a "reasonable default" for mose unix systems, based on an
245 * effective `umask` of `0o22`.
246 *
247 * This also defaults the `portable` option in the gzip configs when creating
248 * a compressed archive, in order to produce deterministic archives that are
249 * not operating-system specific.
250 */
251 portable?: boolean;
252 /**
253 * When creating archives, do not recursively archive the contents of
254 * directories. By default, archiving a directory archives all of its
255 * contents as well.
256 */
257 noDirRecurse?: boolean;
258 /**
259 * Suppress Pax extended headers when creating archives. Note that this means
260 * long paths and linkpaths will be truncated, and large or negative numeric
261 * values may be interpreted incorrectly.
262 */
263 noPax?: boolean;
264 /**
265 * Set to a `Date` object to force a specific `mtime` value for everything
266 * written to an archive.
267 *
268 * This is useful when creating archives that are intended to be
269 * deterministic based on their contents, irrespective of the file's last
270 * modification time.
271 *
272 * Overridden by `noMtime`.
273 */
274 mtime?: Date;
275 /**
276 * A path portion to prefix onto the entries added to an archive.
277 */
278 prefix?: string;
279 /**
280 * The mode to set on any created file archive, defaults to 0o666
281 * masked by the process umask, often resulting in 0o644.
282 *
283 * This does *not* affect the mode fields of individual entries, or the
284 * mode status of extracted entries on the filesystem.
285 */
286 mode?: number;
287 /**
288 * A cache of mtime values, to avoid having to stat the same file repeatedly.
289 *
290 * @internal
291 */
292 mtimeCache?: Map<string, Date>;
293 /**
294 * maximum buffer size for `fs.read()` operations.
295 *
296 * @internal
297 */
298 maxReadSize?: number;
299 /**
300 * Filter modes of entries being unpacked, like `process.umask()`
301 *
302 * @internal
303 */
304 umask?: number;
305 /**
306 * Default mode for directories. Used for all implicitly created directories,
307 * and any directories in the archive that do not have a mode field.
308 *
309 * @internal
310 */
311 dmode?: number;
312 /**
313 * default mode for files
314 *
315 * @internal
316 */
317 fmode?: number;
318 /**
319 * Map that tracks which directories already exist, for extraction
320 *
321 * @internal
322 */
323 dirCache?: Map<string, boolean>;
324 /**
325 * maximum supported size of meta entries. Defaults to 1MB
326 *
327 * @internal
328 */
329 maxMetaEntrySize?: number;
330 /**
331 * A Map object containing the device and inode value for any file whose
332 * `nlink` value is greater than 1, to identify hard links when creating
333 * archives.
334 *
335 * @internal
336 */
337 linkCache?: Map<LinkCacheKey, string>;
338 /**
339 * A map object containing the results of `fs.readdir()` calls.
340 *
341 * @internal
342 */
343 readdirCache?: Map<string, string[]>;
344 /**
345 * A cache of all `lstat` results, for use in creating archives.
346 *
347 * @internal
348 */
349 statCache?: Map<string, Stats>;
350 /**
351 * Number of concurrent jobs to run when creating archives.
352 *
353 * Defaults to 4.
354 *
355 * @internal
356 */
357 jobs?: number;
358 /**
359 * Automatically set to true on Windows systems.
360 *
361 * When extracting, causes behavior where filenames containing `<|>?:`
362 * characters are converted to windows-compatible escape sequences in the
363 * created filesystem entries.
364 *
365 * When packing, causes behavior where paths replace `\` with `/`, and
366 * filenames containing the windows-compatible escaped forms of `<|>?:` are
367 * converted to actual `<|>?:` characters in the archive.
368 *
369 * @internal
370 */
371 win32?: boolean;
372 /**
373 * For `WriteEntry` objects, the absolute path to the entry on the
374 * filesystem. By default, this is `resolve(cwd, entry.path)`, but it can be
375 * overridden explicitly.
376 *
377 * @internal
378 */
379 absolute?: string;
380 /**
381 * Used with Parser stream interface, to attach and take over when the
382 * stream is completely parsed. If this is set, then the prefinish,
383 * finish, and end events will not fire, and are the responsibility of
384 * the ondone method to emit properly.
385 *
386 * @internal
387 */
388 ondone?: () => void;
389 /**
390 * Mostly for testing, but potentially useful in some cases.
391 * Forcibly trigger a chown on every entry, no matter what.
392 */
393 forceChown?: boolean;
394 /**
395 * ambiguous deprecated name for {@link onReadEntry}
396 *
397 * @deprecated
398 */
399 onentry?: (entry: ReadEntry) => any;
400}
401export type TarOptionsSync = TarOptions & {
402 sync: true;
403};
404export type TarOptionsAsync = TarOptions & {
405 sync?: false;
406};
407export type TarOptionsFile = TarOptions & {
408 file: string;
409};
410export type TarOptionsNoFile = TarOptions & {
411 file?: undefined;
412};
413export type TarOptionsSyncFile = TarOptionsSync & TarOptionsFile;
414export type TarOptionsAsyncFile = TarOptionsAsync & TarOptionsFile;
415export type TarOptionsSyncNoFile = TarOptionsSync & TarOptionsNoFile;
416export type TarOptionsAsyncNoFile = TarOptionsAsync & TarOptionsNoFile;
417export type LinkCacheKey = `${number}:${number}`;
418export interface TarOptionsWithAliases extends TarOptions {
419 /**
420 * The effective current working directory for this tar command
421 */
422 C?: TarOptions['cwd'];
423 /**
424 * The tar file to be read and/or written. When this is set, a stream
425 * is not returned. Asynchronous commands will return a promise indicating
426 * when the operation is completed, and synchronous commands will return
427 * immediately.
428 */
429 f?: TarOptions['file'];
430 /**
431 * When creating a tar archive, this can be used to compress it as well.
432 * Set to `true` to use the default gzip options, or customize them as
433 * needed.
434 *
435 * When reading, if this is unset, then the compression status will be
436 * inferred from the archive data. This is generally best, unless you are
437 * sure of the compression settings in use to create the archive, and want to
438 * fail if the archive doesn't match expectations.
439 */
440 z?: TarOptions['gzip'];
441 /**
442 * When creating archives, preserve absolute and `..` paths in the archive,
443 * rather than sanitizing them under the cwd.
444 *
445 * When extracting, allow absolute paths, paths containing `..`, and
446 * extracting through symbolic links. By default, the root `/` is stripped
447 * from absolute paths (eg, turning `/x/y/z` into `x/y/z`), paths containing
448 * `..` are not extracted, and any file whose location would be modified by a
449 * symbolic link is not extracted.
450 *
451 * **WARNING** This is almost always unsafe, and must NEVER be used on
452 * archives from untrusted sources, such as user input, and every entry must
453 * be validated to ensure it is safe to write. Even if the input is not
454 * malicious, mistakes can cause a lot of damage!
455 */
456 P?: TarOptions['preservePaths'];
457 /**
458 * When extracting, unlink files before creating them. Without this option,
459 * tar overwrites existing files, which preserves existing hardlinks. With
460 * this option, existing hardlinks will be broken, as will any symlink that
461 * would affect the location of an extracted file.
462 */
463 U?: TarOptions['unlink'];
464 /**
465 * When extracting, strip the specified number of path portions from the
466 * entry path. For example, with `{strip: 2}`, the entry `a/b/c/d` would be
467 * extracted to `{cwd}/c/d`.
468 */
469 'strip-components'?: TarOptions['strip'];
470 /**
471 * When extracting, strip the specified number of path portions from the
472 * entry path. For example, with `{strip: 2}`, the entry `a/b/c/d` would be
473 * extracted to `{cwd}/c/d`.
474 */
475 stripComponents?: TarOptions['strip'];
476 /**
477 * When extracting, keep the existing file on disk if it's newer than the
478 * file in the archive.
479 */
480 'keep-newer'?: TarOptions['newer'];
481 /**
482 * When extracting, keep the existing file on disk if it's newer than the
483 * file in the archive.
484 */
485 keepNewer?: TarOptions['newer'];
486 /**
487 * When extracting, keep the existing file on disk if it's newer than the
488 * file in the archive.
489 */
490 'keep-newer-files'?: TarOptions['newer'];
491 /**
492 * When extracting, keep the existing file on disk if it's newer than the
493 * file in the archive.
494 */
495 keepNewerFiles?: TarOptions['newer'];
496 /**
497 * When extracting, do not overwrite existing files at all.
498 */
499 k?: TarOptions['keep'];
500 /**
501 * When extracting, do not overwrite existing files at all.
502 */
503 'keep-existing'?: TarOptions['keep'];
504 /**
505 * When extracting, do not overwrite existing files at all.
506 */
507 keepExisting?: TarOptions['keep'];
508 /**
509 * When extracting, do not set the `mtime` value for extracted entries to
510 * match the `mtime` in the archive.
511 *
512 * When creating archives, do not store the `mtime` value in the entry. Note
513 * that this prevents properly using other mtime-based features (such as
514 * `tar.update` or the `newer` option) with the resulting archive.
515 */
516 m?: TarOptions['noMtime'];
517 /**
518 * When extracting, do not set the `mtime` value for extracted entries to
519 * match the `mtime` in the archive.
520 *
521 * When creating archives, do not store the `mtime` value in the entry. Note
522 * that this prevents properly using other mtime-based features (such as
523 * `tar.update` or the `newer` option) with the resulting archive.
524 */
525 'no-mtime'?: TarOptions['noMtime'];
526 /**
527 * When extracting, set the `uid` and `gid` of extracted entries to the `uid`
528 * and `gid` fields in the archive. Defaults to true when run as root, and
529 * false otherwise.
530 *
531 * If false, then files and directories will be set with the owner and group
532 * of the user running the process. This is similar to `-p` in `tar(1)`, but
533 * ACLs and other system-specific data is never unpacked in this
534 * implementation, and modes are set by default already.
535 */
536 p?: TarOptions['preserveOwner'];
537 /**
538 * Pack the targets of symbolic links rather than the link itself.
539 */
540 L?: TarOptions['follow'];
541 /**
542 * Pack the targets of symbolic links rather than the link itself.
543 */
544 h?: TarOptions['follow'];
545 /**
546 * Deprecated option. Set explicitly false to set `chmod: true`. Ignored
547 * if {@link TarOptions#chmod} is set to any boolean value.
548 *
549 * @deprecated
550 */
551 noChmod?: boolean;
552}
553export type TarOptionsWithAliasesSync = TarOptionsWithAliases & {
554 sync: true;
555};
556export type TarOptionsWithAliasesAsync = TarOptionsWithAliases & {
557 sync?: false;
558};
559export type TarOptionsWithAliasesFile = (TarOptionsWithAliases & {
560 file: string;
561}) | (TarOptionsWithAliases & {
562 f: string;
563});
564export type TarOptionsWithAliasesSyncFile = TarOptionsWithAliasesSync & TarOptionsWithAliasesFile;
565export type TarOptionsWithAliasesAsyncFile = TarOptionsWithAliasesAsync & TarOptionsWithAliasesFile;
566export type TarOptionsWithAliasesNoFile = TarOptionsWithAliases & {
567 f?: undefined;
568 file?: undefined;
569};
570export type TarOptionsWithAliasesSyncNoFile = TarOptionsWithAliasesSync & TarOptionsWithAliasesNoFile;
571export type TarOptionsWithAliasesAsyncNoFile = TarOptionsWithAliasesAsync & TarOptionsWithAliasesNoFile;
572export declare const isSyncFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
573 sync: true;
574} & {
575 file: string;
576};
577export declare const isAsyncFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
578 sync?: false | undefined;
579} & {
580 file: string;
581};
582export declare const isSyncNoFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
583 sync: true;
584} & {
585 file?: undefined;
586};
587export declare const isAsyncNoFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
588 sync?: false | undefined;
589} & {
590 file?: undefined;
591};
592export declare const isSync: <O extends TarOptions>(o: O) => o is O & TarOptions & {
593 sync: true;
594};
595export declare const isAsync: <O extends TarOptions>(o: O) => o is O & TarOptions & {
596 sync?: false | undefined;
597};
598export declare const isFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
599 file: string;
600};
601export declare const isNoFile: <O extends TarOptions>(o: O) => o is O & TarOptions & {
602 file?: undefined;
603};
604export declare const dealias: (opt?: TarOptionsWithAliases) => TarOptions;
605//# sourceMappingURL=options.d.ts.map
\No newline at end of file