UNPKG

5.21 kBTypeScriptView Raw
1// Type definitions for archiver 5.3
2// Project: https://github.com/archiverjs/node-archiver
3// Definitions by: Esri
4// Dolan Miu <https://github.com/dolanmiu>
5// Crevil <https://github.com/crevil>
6// Piotr Błażejewicz <https://github.com/peterblazejewicz>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9import * as fs from 'fs';
10import * as stream from 'stream';
11import * as glob from 'glob';
12import { ZlibOptions } from 'zlib';
13
14type Partial<T> = {
15 [P in keyof T]?: T[P];
16};
17
18// tslint:disable-next-line:ban-types support for ConstructorFn function and classes
19type ConstructorFn<T> = Function | (new (...params: any[]) => T);
20
21declare function archiver(format: archiver.Format, options?: archiver.ArchiverOptions): archiver.Archiver;
22
23declare namespace archiver {
24 type Format = 'zip' | 'tar';
25
26 function create(format: string, options?: ArchiverOptions): Archiver;
27
28 /** Check if the format is already registered. */
29 function isRegisteredFormat(format: string): boolean;
30 // tslint:disable-next-line:ban-types Function
31 function registerFormat(format: string, module: Function): void;
32
33 interface EntryData {
34 /** Sets the entry name including internal path */
35 name: string;
36 /** Sets the entry date */
37 date?: Date | string | undefined;
38 /** Sets the entry permissions */
39 mode?: number | undefined;
40 /**
41 * Sets a path prefix for the entry name.
42 * Useful when working with methods like `directory` or `glob`
43 */
44 prefix?: string | undefined;
45 /**
46 * Sets the fs stat data for this entry allowing
47 * for reduction of fs stat calls when stat data is already known
48 */
49 stats?: fs.Stats | undefined;
50 }
51
52 interface ZipEntryData extends EntryData {
53 /** Sets the compression method to STORE */
54 store?: boolean | undefined;
55 }
56
57 type TarEntryData = EntryData;
58
59 interface ProgressData {
60 entries: {
61 total: number;
62 processed: number;
63 };
64 fs: {
65 totalBytes: number;
66 processedBytes: number;
67 };
68 }
69
70 /** A function that lets you either opt out of including an entry (by returning false), or modify the contents of an entry as it is added (by returning an EntryData) */
71 type EntryDataFunction = (entry: EntryData) => false | EntryData;
72
73 class ArchiverError extends Error {
74 code: string; // Since archiver format support is modular, we cannot enumerate all possible error codes, as the modules can throw arbitrary ones.
75 data: any;
76 path?: any;
77
78 constructor(code: string, data: any);
79 }
80
81 interface Archiver extends stream.Transform {
82 abort(): this;
83 append(source: stream.Readable | Buffer | string, data?: EntryData | ZipEntryData | TarEntryData): this;
84
85 /** if false is passed for destpath, the path of a chunk of data in the archive is set to the root */
86 directory(dirpath: string, destpath: false | string, data?: Partial<EntryData> | EntryDataFunction): this;
87 file(filename: string, data: EntryData): this;
88 glob(pattern: string, options?: glob.IOptions, data?: Partial<EntryData>): this;
89 finalize(): Promise<void>;
90
91 setFormat(format: string): this;
92 // tslint:disable-next-line:ban-types Function
93 setModule(module: Function): this;
94
95 pointer(): number;
96 // tslint:disable-next-line:ban-types Function
97 use(plugin: Function): this;
98
99 symlink(filepath: string, target: string, mode?: number): this;
100
101 on(event: 'error' | 'warning', listener: (error: ArchiverError) => void): this;
102 on(event: 'data', listener: (data: Buffer) => void): this;
103 on(event: 'progress', listener: (progress: ProgressData) => void): this;
104 on(event: 'close' | 'drain' | 'finish', listener: () => void): this;
105 on(event: 'pipe' | 'unpipe', listener: (src: stream.Readable) => void): this;
106 on(event: 'entry', listener: (entry: EntryData) => void): this;
107 on(event: string, listener: (...args: any[]) => void): this;
108 }
109
110 type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions;
111
112 interface CoreOptions {
113 statConcurrency?: number | undefined;
114 }
115
116 interface TransformOptions {
117 allowHalfOpen?: boolean | undefined;
118 readableObjectMode?: boolean | undefined;
119 writeableObjectMode?: boolean | undefined;
120 decodeStrings?: boolean | undefined;
121 encoding?: string | undefined;
122 highWaterMark?: number | undefined;
123 objectmode?: boolean | undefined;
124 }
125
126 interface ZipOptions {
127 comment?: string | undefined;
128 forceLocalTime?: boolean | undefined;
129 forceZip64?: boolean | undefined;
130 /** @default false */
131 namePrependSlash?: boolean | undefined;
132 store?: boolean | undefined;
133 zlib?: ZlibOptions | undefined;
134 }
135
136 interface TarOptions {
137 gzip?: boolean | undefined;
138 gzipOptions?: ZlibOptions | undefined;
139 }
140}
141
142export = archiver;