UNPKG

21.9 kBTypeScriptView Raw
1// Type definitions for tar 4.0
2// Project: https://github.com/npm/node-tar
3// Definitions by: Maxime LUCE <https://github.com/SomaticIT>, Connor Peet <https://github.com/connor4312>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TODO: When/if typings for [fstream](https://github.com/npm/fstream) are written, refactor this typing to use it for the various streams.
6
7/// <reference types="node" />
8
9import stream = require('stream');
10import events = require('events');
11import zlib = require('zlib');
12import MiniPass = require('minipass');
13
14// #region Interfaces
15
16export interface HeaderProperties {
17 path: string;
18 mode?: number;
19 noProprietary?: boolean;
20 uid?: number;
21 gid?: number;
22 size?: number;
23 mtime?: number;
24 type?: string;
25 uname?: string;
26 gname?: string;
27 devmaj?: number;
28 devmin?: number;
29}
30
31export interface ExtractOptions {
32 type?: string;
33 Directory?: boolean;
34 path?: string;
35 strip?: number;
36}
37
38export interface ParseStream extends NodeJS.ReadWriteStream {
39 position: number;
40
41 _stream: stream.Stream;
42 _ended: boolean;
43
44 _streamEnd(): void;
45 _process(c: Buffer): void;
46 _startEntry(c: Buffer): void;
47}
48
49export interface PackStream extends NodeJS.ReadWriteStream {
50 writable: boolean;
51 readable: boolean;
52
53 _noProprietary: boolean;
54 _global: HeaderProperties;
55 _buffer: stream.Stream[];
56 _currentEntry: any;
57 _processing: boolean;
58 _pipeRoot: stream.Stream;
59 _needDrain: boolean;
60 _paused: boolean;
61
62 addGlobal(props: HeaderProperties): void;
63 add(stream: stream.Stream): boolean;
64 destroy(): void;
65
66 _process(): void;
67}
68
69// #endregion
70
71// #region Enums
72
73export interface Fields {
74 path: number;
75 mode: number;
76 uid: number;
77 gid: number;
78 size: number;
79 mtime: number;
80 cksum: number;
81 type: number;
82 linkpath: number;
83 ustar: number;
84 ustarvar: number;
85 uname: number;
86 gname: number;
87 devmaj: number;
88 devmin: number;
89 prefix: number;
90 fill: number;
91}
92
93export type fields = Fields; // alias for backwards compatbility
94
95export const fieldSize: number[];
96export const fieldOffs: number[];
97export const fieldEnds: number[];
98
99/**
100 * Different values of the 'type' field
101 * paths match the values of Stats.isX() functions, where appropriate
102 */
103export const types: {
104 0: string;
105 "\0": string;
106 "": string;
107 1: string;
108 2: string;
109 3: string;
110 4: string;
111 5: string;
112 6: string;
113 7: string;
114 g: string;
115 x: string;
116 A: string;
117 D: string;
118 I: string;
119 K: string;
120 L: string;
121 M: string;
122 N: string;
123 S: string;
124 V: string;
125 X: string;
126 File: string;
127 OldFile: string;
128 Link: string;
129 SymbolicLick: string;
130 CharacterDevice: string;
131 BlockDevice: string;
132 Directory: string;
133 FIFO: string;
134 ContiguousFile: string;
135 GlobalExtendedHeader: string;
136 ExtendedHeader: string;
137 SolarisACL: string;
138 GNUDumpDir: string;
139 INode: string;
140 NextFileHasLonLinkPath: string;
141 NextFileHasLongPath: string;
142 ContinuationFile: string;
143 TapeVolumeHeader: string;
144 OldExtendedHeader: string;
145};
146
147/**
148 * Values for the mode field
149 */
150export const modes: {
151 suid: number;
152 sgid: number;
153 svtx: number;
154 uread: number;
155 uwrite: number;
156 uexec: number;
157 gread: number;
158 gwrite: number;
159 gexec: number;
160 oread: number;
161 owrite: number;
162 oexec: number;
163};
164
165export const numeric: {
166 mode: boolean;
167 uid: boolean;
168 gid: boolean;
169 size: boolean;
170 mtime: boolean;
171 devmaj: boolean;
172 devmin: boolean;
173 cksum: boolean;
174 atime: boolean;
175 ctime: boolean;
176 dev: boolean;
177 ino: boolean;
178 nlink: boolean;
179};
180
181export const knownExtended: {
182 atime: boolean;
183 charset: boolean;
184 comment: boolean;
185 ctime: boolean;
186 gid: boolean;
187 gname: boolean;
188 linkpat: boolean;
189 mtime: boolean;
190 path: boolean;
191 realtime: boolean;
192 security: boolean;
193 size: boolean;
194 uid: boolean;
195 uname: boolean;
196};
197
198export const headerSize: number;
199export const blockSize: number;
200
201//#endregion
202
203//#region Global Methods
204
205/**
206 * Returns a writable stream. Write tar data to it and it will emit entry events for each entry parsed from the tarball. This is used by tar.Extract.
207 */
208export function Parse(): ParseStream;
209/**
210 * Returns a through stream. Use fstream to write files into the pack stream and you will receive tar archive data from the pack stream.
211 * This only works with directories, it does not work with individual files.
212 * The optional properties object are used to set properties in the tar 'Global Extended Header'.
213 */
214export function Pack(props?: HeaderProperties): PackStream;
215
216/**
217 * Returns a through stream. Write tar data to the stream and the files in the tarball will be extracted onto the filesystem.
218 * options can be:
219 * ```
220 * {
221 * path: '/path/to/extract/tar/into',
222 * strip: 0, // how many path segments to strip from the root when extracting
223 * }
224 * ```
225 * options also get passed to the fstream.Writer instance that tar uses internally.
226 */
227export function Extract(opts: ExtractOptions | string): ParseStream;
228
229export interface FileStat extends stream.Readable, Fields {
230 header: HeaderProperties;
231 startBlockSize: number;
232 blockRemain: number;
233 remain: number;
234 meta: boolean;
235 ignore: boolean;
236 size: number;
237}
238
239export interface ReadEntry extends MiniPass, HeaderProperties {
240 /** The extended metadata object provided to the constructor. */
241 extended: any;
242 /** The global extended metadata object provided to the constructor. */
243 globalExtended: any;
244 /** The number of bytes remaining to be written into the stream. */
245 remain: number;
246 /** The number of 512-byte blocks remaining to be written into the stream. */
247 blockRemain: number;
248 /** Whether this entry should be ignored. */
249 ignore: boolean;
250 /**
251 * True if this represents metadata about the next entry, false if it
252 * represents a filesystem object.
253 */
254 meta: boolean;
255}
256
257export interface CreateOptions {
258 /**
259 * A function that will get called with (message, data)
260 * for any warnings encountered.
261 */
262 onwarn?(message: string, data: Buffer): void;
263
264 /**
265 * Treat warnings as crash-worthy errors. Default false.
266 */
267 strict?: boolean;
268
269 /**
270 * The current working directory for creating the archive. Defaults to process.cwd().
271 */
272 cwd?: string;
273
274 /**
275 * Alias for cwd.
276 */
277 C?: string;
278
279 /**
280 * Set to any truthy value to create a gzipped archive,
281 * or an object with settings for zlib.Gzip()
282 */
283 gzip?: boolean | zlib.ZlibOptions;
284
285 /**
286 * Alias for gzip.
287 */
288 z?: boolean | zlib.ZlibOptions;
289
290 /**
291 * A function that gets called with (path, stat) for each entry being
292 * added. Return true to add the entry to the archive, or false to omit it.
293 */
294 filter?(path: string, stat: FileStat): boolean;
295
296 /**
297 * Omit metadata that is system-specific: ctime, atime, uid, gid, uname,
298 * gname, dev, ino, and nlink. Note that mtime is still included,
299 * because this is necessary other time-based operations.
300 */
301 portable?: boolean;
302
303 /**
304 * Allow absolute paths. By default, / is stripped from absolute paths.
305 */
306 preservePaths?: boolean;
307
308 /**
309 * Alias for presevePaths.
310 */
311 P?: boolean;
312
313 /**
314 * The mode to set on the created file archive.
315 */
316 mode?: number;
317
318 /**
319 * Do not recursively archive the contents of directories.
320 */
321 noDirRecurse?: boolean;
322
323 /**
324 * Set to true to pack the targets of symbolic links. Without this
325 * option, symbolic links are archived as such.
326 */
327 follow?: boolean;
328
329 /**
330 * Alias for follow.
331 */
332 L?: boolean;
333
334 /**
335 * Alias for follow.
336 */
337 h?: boolean;
338
339 /**
340 * Suppress pax extended headers. Note that this means that long paths and
341 * linkpaths will be truncated, and large or negative numeric values
342 * may be interpreted incorrectly.
343 */
344 noPax?: boolean;
345
346 /**
347 * A path portion to prefix onto the entries in the archive.
348 */
349 prefix?: string;
350}
351
352export interface ExtractOptions {
353 /**
354 * A function that will get called with (message, data)
355 * for any warnings encountered.
356 */
357 onwarn?(message: string, data: Buffer): void;
358
359 /**
360 * Treat warnings as crash-worthy errors. Default false.
361 */
362 strict?: boolean;
363
364 /**
365 * Extract files relative to the specified directory. Defaults to
366 * process.cwd(). If provided, this must exist and must be a directory.
367 */
368 cwd?: string;
369
370 /**
371 * Alias for cwd.
372 */
373 C?: string;
374
375 /**
376 * A function that gets called with (path, stat) for each entry being
377 * added. Return true to emit the entry from the archive, or false to skip it.
378 */
379 filter?(path: string, stat: FileStat): boolean;
380
381 /**
382 * Set to true to keep the existing file on disk if it's newer than
383 * the file in the archive.
384 */
385 newer?: boolean;
386
387 /**
388 * Alias for newer.
389 */
390 'keep-newer'?: boolean;
391
392 /**
393 * Alias for newer.
394 */
395 'keep-newer-files'?: boolean;
396
397 /**
398 * Do not overwrite existing files. In particular, if a file appears more
399 * than once in an archive, later copies will not overwrite earlier copies
400 */
401 keep?: boolean;
402
403 /**
404 * Alias for keep.
405 */
406 k?: boolean;
407
408 /**
409 * Alias for keep.
410 */
411 'keep-existing'?: boolean;
412
413 /**
414 * Unlink files before creating them. Without this option, tar overwrites
415 * existing files, which preserves existing hardlinks. With this option,
416 * existing hardlinks will be broken, as will any symlink that would
417 * affect the location of an extracted file.
418 */
419 unlink?: boolean;
420
421 /**
422 * Remove the specified number of leading path elements. Pathnames with
423 * fewer elements will be silently skipped. Note that the pathname
424 * is edited after applying the filter, but before security checks.
425 */
426 strip?: number;
427
428 /**
429 * Alias for strip.
430 */
431 'strip-components'?: number;
432
433 /**
434 * Alias for strip.
435 */
436 stripComponents?: number;
437
438 /**
439 * If true, tar will set the uid and gid of extracted entries to the uid
440 * and gid fields in the archive. This defaults to true when run as root,
441 * and false otherwise. If false, then files and directories will be set
442 * with the owner and group of the user running the process. This is
443 * similar to -p in tar(1), but ACLs and other system-specific data is
444 * never unpacked in this implementation, and modes
445 * are set by default already.
446 */
447 preserveOwner?: boolean;
448
449 /**
450 * Alias for preserveOwner.
451 */
452 p?: boolean;
453
454 /**
455 * Set to a number to force ownership of all extracted files and folders,
456 * and all implicitly created directories, to be owned by the specified
457 * user id, regardless of the uid field in the archive. Cannot be used
458 * along with preserveOwner. Requires also setting a gid option.
459 */
460 uid?: number;
461
462 /**
463 * Set to a number to force ownership of all extracted files and folders,
464 * and all implicitly created directories, to be owned by the specified
465 * group id, regardless of the gid field in the archive. Cannot be used
466 * along with preserveOwner. Requires also setting a uid option
467 */
468 gid?: number;
469
470 /**
471 * Set to true to omit writing mtime value for extracted entries.
472 * [Alias: m, no-mtime]
473 */
474 noMtime?: boolean;
475 m?: boolean;
476 'no-mtime'?: boolean;
477
478 /**
479 * Provide a function that takes an entry object, and returns a stream,
480 * or any falsey value. If a stream is provided, then that stream's data
481 * will be written instead of the contents of the archive entry. If a
482 * falsey value is provided, then the entry is written to disk as normal.
483 * (To exclude items from extraction, use the filter option described above.)
484 */
485 transform?(entry: ReadEntry): NodeJS.WritableStream | undefined | false | null;
486
487 /**
488 * A function that gets called with (entry) for each entry that passes the
489 * filter.
490 */
491 onentry?(entry: ReadEntry): void;
492
493 // The following options are mostly internal, but can be modified in some
494 // advanced use cases, such as re-using caches between runs.
495
496 /**
497 * The maximum buffer size for fs.read() operations (in bytes). Defaults to 16 MB.
498 */
499 maxReadSize?: number;
500
501 /**
502 * The maximum size of meta entries that is supported. Defaults to 1 MB.
503 */
504 maxMetaEntrySize?: number;
505}
506
507export interface ListOptions {
508 /**
509 * Treat warnings as crash-worthy errors. Default false.
510 */
511 strict?: boolean;
512
513 /**
514 * Extract files relative to the specified directory. Defaults to
515 * process.cwd(). If provided, this must exist and must be a directory.
516 */
517 cwd?: string;
518
519 /**
520 * Alias for cwd.
521 */
522 C?: string;
523
524 /**
525 * A function that gets called with (path, stat) for each entry being
526 * added. Return true to emit the entry from the archive, or false to skip it.
527 */
528 filter?(path: string, entry: FileStat): boolean;
529
530 /**
531 * A function that gets called with (entry) for each entry that passes the
532 * filter. This is important for when both file and sync are set, because
533 * it will be called synchronously.
534 */
535 onentry?(entry: FileStat): void;
536
537 /**
538 * The maximum buffer size for fs.read() operations. Defaults to 16 MB.
539 */
540 maxReadSize?: number;
541
542 /**
543 * By default, entry streams are resumed immediately after the call to
544 * onentry. Set noResume: true to suppress this behavior. Note that by
545 * opting into this, the stream will never complete until the entry
546 * data is consumed.
547 */
548 noResume?: boolean;
549}
550
551export interface ReplaceOptions {
552 /**
553 * Required. Write the tarball archive to the specified filename.
554 */
555 file: string;
556
557 /**
558 * Act synchronously. If this is set, then any provided file will be
559 * fully written after the call to tar.c.
560 */
561 sync?: boolean;
562
563 /**
564 * A function that will get called with (message, data)
565 * for any warnings encountered.
566 */
567 onwarn?(message: string, data: Buffer): void;
568
569 /**
570 * Treat warnings as crash-worthy errors. Default false.
571 */
572 strict?: boolean;
573
574 /**
575 * Extract files relative to the specified directory. Defaults to
576 * process.cwd(). If provided, this must exist and must be a directory.
577 */
578 cwd?: string;
579
580 /**
581 * Alias for cwd.
582 */
583 C?: string;
584
585 /**
586 * A path portion to prefix onto the entries in the archive.
587 */
588 prefix?: string;
589
590 /**
591 * Set to any truthy value to create a gzipped archive,
592 * or an object with settings for zlib.Gzip()
593 */
594 gzip?: boolean | zlib.ZlibOptions;
595
596 /**
597 * A function that gets called with (path, stat) for each entry being
598 * added. Return true to emit the entry from the archive, or false to skip it.
599 */
600 filter?(path: string, stat: FileStat): boolean;
601
602 /**
603 * Allow absolute paths. By default, / is stripped from absolute paths.
604 */
605 preservePaths?: boolean;
606
607 /**
608 * The maximum buffer size for fs.read() operations. Defaults to 16 MB.
609 */
610 maxReadSize?: number;
611
612 /**
613 * Do not recursively archive the contents of directories.
614 */
615 noDirRecurse?: boolean;
616
617 /**
618 * Set to true to pack the targets of symbolic links. Without this
619 * option, symbolic links are archived as such.
620 */
621 follow?: boolean;
622
623 /**
624 * Alias for follow.
625 */
626 L?: boolean;
627
628 /**
629 * Alias for follow.
630 */
631 h?: boolean;
632
633 /**
634 * uppress pax extended headers. Note that this means that long paths and
635 * linkpaths will be truncated, and large or negative numeric values
636 * may be interpreted incorrectly.
637 */
638 noPax?: boolean;
639}
640
641export interface FileOptions {
642 /**
643 * Uses the given file as the input or output of this function.
644 */
645 file?: string;
646
647 /**
648 * Alias for file.
649 */
650 f?: string;
651}
652
653/**
654 * Create a tarball archive. The fileList is an array of paths to add to the
655 * tarball. Adding a directory also adds its children recursively. An entry in
656 * fileList that starts with an @ symbol is a tar archive whose entries will
657 * be added. To add a file that starts with @, prepend it with `./`.
658 *
659 * Archive data may be read from the returned stream.
660 */
661export function create(options: CreateOptions, fileList: ReadonlyArray<string>, callback?: (err?: Error) => void): stream.Readable;
662
663/**
664 * Create a tarball archive. The fileList is an array of paths to add to the
665 * tarball. Adding a directory also adds its children recursively. An entry in
666 * fileList that starts with an @ symbol is a tar archive whose entries will
667 * be added. To add a file that starts with @, prepend it with `./`.
668 */
669export function create(options: CreateOptions & FileOptions, fileList: ReadonlyArray<string>): Promise<void>;
670export function create(options: CreateOptions & FileOptions & { sync: true }, fileList: ReadonlyArray<string>): void;
671export function create(options: CreateOptions & FileOptions, fileList: ReadonlyArray<string>, callback: (err?: Error) => void): void;
672
673/**
674 * Alias for create
675 */
676export const c: typeof create;
677
678/**
679 * Extract a tarball archive. The fileList is an array of paths to extract
680 * from the tarball. If no paths are provided, then all the entries are
681 * extracted. If the archive is gzipped, then tar will detect this and unzip
682 * it. Note that all directories that are created will be forced to be
683 * writable, readable, and listable by their owner, to avoid cases where a
684 * directory prevents extraction of child entries by virtue of its mode. Most
685 * extraction errors will cause a warn event to be emitted. If the cwd is
686 * missing, or not a directory, then the extraction will fail completely.
687 *
688 * Archive data should be written to the returned stream.
689 */
690export function extract(options: ExtractOptions, fileList?: ReadonlyArray<string>, callback?: (err?: Error) => void): stream.Writable;
691
692/**
693 * Extract a tarball archive. The fileList is an array of paths to extract
694 * from the tarball. If no paths are provided, then all the entries are
695 * extracted. If the archive is gzipped, then tar will detect this and unzip
696 * it. Note that all directories that are created will be forced to be
697 * writable, readable, and listable by their owner, to avoid cases where a
698 * directory prevents extraction of child entries by virtue of its mode. Most
699 * extraction errors will cause a warn event to be emitted. If the cwd is
700 * missing, or not a directory, then the extraction will fail completely.
701 */
702export function extract(options: ExtractOptions & FileOptions, fileList?: ReadonlyArray<string>): Promise<void>;
703export function extract(options: ExtractOptions & FileOptions & { sync: true }, fileList?: ReadonlyArray<string>): void;
704export function extract(options: ExtractOptions & FileOptions, fileList: ReadonlyArray<string> | undefined, callback: (err?: Error) => void): void;
705
706/**
707 * Alias for extract
708 */
709export const x: typeof extract;
710
711/**
712 * List the contents of a tarball archive. The fileList is an array of paths
713 * to list from the tarball. If no paths are provided, then all the entries
714 * are listed. If the archive is gzipped, then tar will detect this and unzip
715 * it.
716 *
717 * Archive data should be written to the returned stream.
718 */
719export function list(options?: ListOptions & FileOptions, fileList?: ReadonlyArray<string>, callback?: (err?: Error) => void): stream.Writable;
720
721/**
722 * List the contents of a tarball archive. The fileList is an array of paths
723 * to list from the tarball. If no paths are provided, then all the entries
724 * are listed. If the archive is gzipped, then tar will detect this and unzip
725 * it.
726 */
727export function list(options: ListOptions & FileOptions, fileList?: ReadonlyArray<string>): Promise<void>;
728export function list(options: ListOptions & FileOptions & { sync: true }, fileList?: ReadonlyArray<string>): void;
729
730/**
731 * Alias for list
732 */
733export const t: typeof list;
734
735/**
736 * Add files to an existing archive. Because later entries override earlier
737 * entries, this effectively replaces any existing entries. The fileList is an
738 * array of paths to add to the tarball. Adding a directory also adds its
739 * children recursively. An entry in fileList that starts with an @ symbol is
740 * a tar archive whose entries will be added. To add a file that
741 * starts with @, prepend it with ./.
742 */
743export function replace(options: ReplaceOptions, fileList?: ReadonlyArray<string>): Promise<void>;
744export function replace(options: ReplaceOptions, fileList: ReadonlyArray<string> | undefined, callback: (err?: Error) => void): Promise<void>;
745
746/**
747 * Alias for replace
748 */
749export const r: typeof replace;
750
751/**
752 * Add files to an archive if they are newer than the entry already in the
753 * tarball archive. The fileList is an array of paths to add to the tarball.
754 * Adding a directory also adds its children recursively. An entry in fileList
755 * that starts with an @ symbol is a tar archive whose entries will be added.
756 * To add a file that starts with @, prepend it with ./.
757 */
758export function update(options: ReplaceOptions, fileList?: ReadonlyArray<string>): Promise<void>;
759export function update(options: ReplaceOptions, fileList: ReadonlyArray<string> | undefined, callback: (err?: Error) => void): Promise<void>;
760
761/**
762 * Alias for update
763 */
764export const u: typeof update;