1 | // Type definitions for JSZip 3.1
|
2 | // Project: http://stuk.github.com/jszip/, https://github.com/stuk/jszip
|
3 | // Definitions by: mzeiher <https://github.com/mzeiher>, forabi <https://github.com/forabi>
|
4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
5 | // TypeScript Version: 2.3
|
6 |
|
7 | /// <reference types="node" />
|
8 |
|
9 | interface JSZipSupport {
|
10 | arraybuffer: boolean;
|
11 | uint8array: boolean;
|
12 | blob: boolean;
|
13 | nodebuffer: boolean;
|
14 | }
|
15 |
|
16 | type Compression = 'STORE' | 'DEFLATE';
|
17 |
|
18 | /**
|
19 | * Depends on the compression type. With `STORE` (no compression), these options are ignored. With
|
20 | * `DEFLATE`, you can give the compression level between 1 (best speed) and 9 (best compression).
|
21 | */
|
22 | interface CompressionOptions {
|
23 | level: number;
|
24 | }
|
25 |
|
26 | interface InputByType {
|
27 | base64: string;
|
28 | string: string;
|
29 | text: string;
|
30 | binarystring: string;
|
31 | array: number[];
|
32 | uint8array: Uint8Array;
|
33 | arraybuffer: ArrayBuffer;
|
34 | blob: Blob;
|
35 | stream: NodeJS.ReadableStream;
|
36 | }
|
37 |
|
38 | interface OutputByType {
|
39 | base64: string;
|
40 | string: string;
|
41 | text: string;
|
42 | binarystring: string;
|
43 | array: number[];
|
44 | uint8array: Uint8Array;
|
45 | arraybuffer: ArrayBuffer;
|
46 | blob: Blob;
|
47 | nodebuffer: Buffer;
|
48 | }
|
49 |
|
50 | // This private `_data` property on a JSZipObject uses this interface.
|
51 | // If/when it is made public this should be uncommented.
|
52 | // interface CompressedObject {
|
53 | // compressedSize: number;
|
54 | // uncompressedSize: number;
|
55 | // crc32: number;
|
56 | // compression: object;
|
57 | // compressedContent: string|ArrayBuffer|Uint8Array|Buffer;
|
58 | // }
|
59 |
|
60 | type InputFileFormat = InputByType[keyof InputByType] | Promise<InputByType[keyof InputByType]>;
|
61 |
|
62 | declare namespace JSZip {
|
63 | type InputType = keyof InputByType;
|
64 |
|
65 | type OutputType = keyof OutputByType;
|
66 |
|
67 | interface JSZipMetadata {
|
68 | percent: number;
|
69 | currentFile: string | null;
|
70 | }
|
71 |
|
72 | type OnUpdateCallback = (metadata: JSZipMetadata) => void;
|
73 |
|
74 | interface JSZipObject {
|
75 | name: string;
|
76 | /**
|
77 | * Present for files loadded with `loadAsync`. May contain ".." path components that could
|
78 | * result in a zip-slip attack. See https://snyk.io/research/zip-slip-vulnerability
|
79 | */
|
80 | unsafeOriginalName?: string;
|
81 | dir: boolean;
|
82 | date: Date;
|
83 | comment: string;
|
84 | /** The UNIX permissions of the file, if any. */
|
85 | unixPermissions: number | string | null;
|
86 | /** The UNIX permissions of the file, if any. */
|
87 | dosPermissions: number | null;
|
88 | options: JSZipObjectOptions;
|
89 |
|
90 | /**
|
91 | * Prepare the content in the asked type.
|
92 | * @param type the type of the result.
|
93 | * @param onUpdate a function to call on each internal update.
|
94 | * @return Promise the promise of the result.
|
95 | */
|
96 | async<T extends OutputType>(type: T, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
|
97 | nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
|
98 | }
|
99 |
|
100 | interface JSZipFileOptions {
|
101 | /** Set to `true` if the data is `base64` encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option. */
|
102 | base64?: boolean;
|
103 | /**
|
104 | * Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used,
|
105 | * this defaults to `true`, if the data is not a `string`, this will be set to `true`.
|
106 | */
|
107 | binary?: boolean;
|
108 | /**
|
109 | * The last modification date, defaults to the current date.
|
110 | */
|
111 | date?: Date;
|
112 | /**
|
113 | * Sets per file compression. The `compressionOptions` parameter depends on the compression type.
|
114 | */
|
115 | compression?: Compression;
|
116 | /**
|
117 | * Sets per file compression level for `DEFLATE` compression.
|
118 | */
|
119 | compressionOptions?: null | CompressionOptions;
|
120 | comment?: string;
|
121 | /** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */
|
122 | optimizedBinaryString?: boolean;
|
123 | /** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */
|
124 | createFolders?: boolean;
|
125 | /** Set to `true` if this is a directory and content should be ignored. */
|
126 | dir?: boolean;
|
127 |
|
128 | /** 6 bits number. The DOS permissions of the file, if any. */
|
129 | dosPermissions?: number | null;
|
130 | /**
|
131 | * 16 bits number. The UNIX permissions of the file, if any.
|
132 | * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc.
|
133 | */
|
134 | unixPermissions?: number | string | null;
|
135 | }
|
136 |
|
137 | interface JSZipObjectOptions {
|
138 | compression: Compression;
|
139 | }
|
140 |
|
141 | interface JSZipGeneratorOptions<T extends OutputType = OutputType> {
|
142 | /**
|
143 | * Sets compression option for all entries that have not specified their own `compression` option
|
144 | */
|
145 | compression?: Compression;
|
146 | /**
|
147 | * Sets compression level for `DEFLATE` compression.
|
148 | */
|
149 | compressionOptions?: null | CompressionOptions;
|
150 | type?: T;
|
151 | comment?: string;
|
152 | /**
|
153 | * mime-type for the generated file.
|
154 | * Useful when you need to generate a file with a different extension, ie: “.ods”.
|
155 | * @default 'application/zip'
|
156 | */
|
157 | mimeType?: string;
|
158 | encodeFileName?(filename: string): string;
|
159 | /** Stream the files and create file descriptors */
|
160 | streamFiles?: boolean;
|
161 | /** DOS (default) or UNIX */
|
162 | platform?: 'DOS' | 'UNIX';
|
163 | }
|
164 |
|
165 | interface JSZipLoadOptions {
|
166 | base64?: boolean;
|
167 | checkCRC32?: boolean;
|
168 | optimizedBinaryString?: boolean;
|
169 | createFolders?: boolean;
|
170 | decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string;
|
171 | }
|
172 |
|
173 | type DataEventCallback<T> = (dataChunk: T, metadata: JSZipMetadata) => void
|
174 | type EndEventCallback = () => void
|
175 | type ErrorEventCallback = (error: Error) => void
|
176 |
|
177 | interface JSZipStreamHelper<T> {
|
178 | /**
|
179 | * Register a listener on an event
|
180 | */
|
181 | on(event: 'data', callback: DataEventCallback<T>): this;
|
182 | on(event: 'end', callback: EndEventCallback): this;
|
183 | on(event: 'error', callback: ErrorEventCallback): this;
|
184 |
|
185 | /**
|
186 | * Read the whole stream and call a callback with the complete content
|
187 | *
|
188 | * @param updateCallback The function called every time the stream updates
|
189 | * @return A Promise of the full content
|
190 | */
|
191 | accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise<T>;
|
192 |
|
193 | /**
|
194 | * Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again
|
195 | *
|
196 | * @return The current StreamHelper object, for chaining
|
197 | */
|
198 | resume(): this;
|
199 |
|
200 | /**
|
201 | * Pause the stream if the stream is running. Once paused, the stream stops sending data events
|
202 | *
|
203 | * @return The current StreamHelper object, for chaining
|
204 | */
|
205 | pause(): this;
|
206 | }
|
207 | }
|
208 |
|
209 | interface JSZip {
|
210 | files: {[key: string]: JSZip.JSZipObject};
|
211 |
|
212 | /**
|
213 | * Get a file from the archive
|
214 | *
|
215 | * @param Path relative path to file
|
216 | * @return File matching path, null if no file found
|
217 | */
|
218 | file(path: string): JSZip.JSZipObject | null;
|
219 |
|
220 | /**
|
221 | * Get files matching a RegExp from archive
|
222 | *
|
223 | * @param path RegExp to match
|
224 | * @return Return all matching files or an empty array
|
225 | */
|
226 | file(path: RegExp): JSZip.JSZipObject[];
|
227 |
|
228 | /**
|
229 | * Add a file to the archive
|
230 | *
|
231 | * @param path Relative path to file
|
232 | * @param data Content of the file
|
233 | * @param options Optional information about the file
|
234 | * @return JSZip object
|
235 | */
|
236 | file<T extends JSZip.InputType>(path: string, data: InputByType[T] | Promise<InputByType[T]>, options?: JSZip.JSZipFileOptions): this;
|
237 | file<T extends JSZip.InputType>(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this;
|
238 |
|
239 | /**
|
240 | * Returns an new JSZip instance with the given folder as root
|
241 | *
|
242 | * @param name Name of the folder
|
243 | * @return New JSZip object with the given folder as root or null
|
244 | */
|
245 | folder(name: string): JSZip | null;
|
246 |
|
247 | /**
|
248 | * Returns new JSZip instances with the matching folders as root
|
249 | *
|
250 | * @param name RegExp to match
|
251 | * @return New array of JSZipFile objects which match the RegExp
|
252 | */
|
253 | folder(name: RegExp): JSZip.JSZipObject[];
|
254 |
|
255 | /**
|
256 | * Call a callback function for each entry at this folder level.
|
257 | *
|
258 | * @param callback function
|
259 | */
|
260 | forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void;
|
261 |
|
262 | /**
|
263 | * Get all files which match the given filter function
|
264 | *
|
265 | * @param predicate Filter function
|
266 | * @return Array of matched elements
|
267 | */
|
268 | filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[];
|
269 |
|
270 | /**
|
271 | * Removes the file or folder from the archive
|
272 | *
|
273 | * @param path Relative path of file or folder
|
274 | * @return Returns the JSZip instance
|
275 | */
|
276 | remove(path: string): JSZip;
|
277 |
|
278 | /**
|
279 | * Generates a new archive asynchronously
|
280 | *
|
281 | * @param options Optional options for the generator
|
282 | * @param onUpdate The optional function called on each internal update with the metadata.
|
283 | * @return The serialized archive
|
284 | */
|
285 | generateAsync<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>, onUpdate?: JSZip.OnUpdateCallback): Promise<OutputByType[T]>;
|
286 |
|
287 | /**
|
288 | * Generates a new archive asynchronously
|
289 | *
|
290 | * @param options Optional options for the generator
|
291 | * @param onUpdate The optional function called on each internal update with the metadata.
|
292 | * @return A Node.js `ReadableStream`
|
293 | */
|
294 | generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: JSZip.OnUpdateCallback): NodeJS.ReadableStream;
|
295 |
|
296 | /**
|
297 | * Generates the complete zip file with the internal stream implementation
|
298 | *
|
299 | * @param options Optional options for the generator
|
300 | * @return a StreamHelper
|
301 | */
|
302 | generateInternalStream<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>): JSZip.JSZipStreamHelper<OutputByType[T]>;
|
303 |
|
304 | /**
|
305 | * Deserialize zip file asynchronously
|
306 | *
|
307 | * @param data Serialized zip file
|
308 | * @param options Options for deserializing
|
309 | * @return Returns promise
|
310 | */
|
311 | loadAsync(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): Promise<JSZip>;
|
312 |
|
313 | /**
|
314 | * Create JSZip instance
|
315 | */
|
316 | new(): this;
|
317 |
|
318 | (): JSZip;
|
319 |
|
320 | prototype: JSZip;
|
321 | support: JSZipSupport;
|
322 | external: {
|
323 | Promise: PromiseConstructorLike;
|
324 | };
|
325 | version: string;
|
326 | }
|
327 |
|
328 | declare var JSZip: JSZip;
|
329 |
|
330 | export = JSZip;
|