index.d.ts 11 KB

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