ZipOpenFS.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. /// <reference types="node" />
  5. import { Libzip } from '@yarnpkg/libzip';
  6. import { BigIntStats, Stats } from 'fs';
  7. import { WatchOptions, WatchCallback, Watcher, StatOptions, StatSyncOptions } from './FakeFS';
  8. import { FakeFS, MkdirOptions, RmdirOptions, WriteFileOptions, OpendirOptions } from './FakeFS';
  9. import { Dirent, SymlinkType } from './FakeFS';
  10. import { CreateReadStreamOptions, CreateWriteStreamOptions, BasePortableFakeFS, ExtractHintOptions, WatchFileOptions, WatchFileCallback, StatWatcher } from './FakeFS';
  11. import { Filename, FSPath, PortablePath } from './path';
  12. /**
  13. * Extracts the archive part (ending in the first instance of `extension`) from a path.
  14. *
  15. * The indexOf-based implementation is ~3.7x faster than a RegExp-based implementation.
  16. */
  17. export declare const getArchivePart: (path: string, extension: string) => PortablePath | null;
  18. export type ZipOpenFSOptions = {
  19. baseFs?: FakeFS<PortablePath>;
  20. filter?: RegExp | null;
  21. libzip: Libzip | (() => Libzip);
  22. maxOpenFiles?: number;
  23. readOnlyArchives?: boolean;
  24. useCache?: boolean;
  25. /**
  26. * Maximum age in ms.
  27. * ZipFS instances are pruned from the cache if they aren't accessed within this amount of time.
  28. */
  29. maxAge?: number;
  30. /**
  31. * Which file extensions will be interpreted as zip files. Useful for supporting other formats
  32. * packaged as zips, such as .docx.
  33. *
  34. * If not provided, defaults to only accepting `.zip`.
  35. */
  36. fileExtensions?: Array<string> | null;
  37. };
  38. export declare class ZipOpenFS extends BasePortableFakeFS {
  39. static openPromise<T>(fn: (zipOpenFs: ZipOpenFS) => Promise<T>, opts: ZipOpenFSOptions): Promise<T>;
  40. private libzipFactory;
  41. private libzipInstance?;
  42. private get libzip();
  43. private readonly baseFs;
  44. private readonly zipInstances;
  45. private readonly fdMap;
  46. private nextFd;
  47. private readonly filter;
  48. private readonly maxOpenFiles;
  49. private readonly maxAge;
  50. private readonly readOnlyArchives;
  51. private readonly fileExtensions;
  52. private isZip;
  53. private notZip;
  54. private realPaths;
  55. constructor({ libzip, baseFs, filter, maxOpenFiles, readOnlyArchives, useCache, maxAge, fileExtensions }: ZipOpenFSOptions);
  56. getExtractHint(hints: ExtractHintOptions): boolean;
  57. getRealPath(): PortablePath;
  58. saveAndClose(): void;
  59. discardAndClose(): void;
  60. resolve(p: PortablePath): PortablePath;
  61. private remapFd;
  62. openPromise(p: PortablePath, flags: string, mode?: number): Promise<number>;
  63. openSync(p: PortablePath, flags: string, mode?: number): number;
  64. opendirPromise(p: PortablePath, opts?: OpendirOptions): Promise<import("./FakeFS").Dir<PortablePath>>;
  65. opendirSync(p: PortablePath, opts?: OpendirOptions): import("./FakeFS").Dir<PortablePath>;
  66. readPromise(fd: number, buffer: Buffer, offset: number, length: number, position: number): Promise<number>;
  67. readSync(fd: number, buffer: Buffer, offset: number, length: number, position: number): number;
  68. writePromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): Promise<number>;
  69. writePromise(fd: number, buffer: string, position?: number): Promise<number>;
  70. writeSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): number;
  71. writeSync(fd: number, buffer: string, position?: number): number;
  72. closePromise(fd: number): Promise<void>;
  73. closeSync(fd: number): void;
  74. createReadStream(p: PortablePath | null, opts?: CreateReadStreamOptions): import("fs").ReadStream;
  75. createWriteStream(p: PortablePath | null, opts?: CreateWriteStreamOptions): import("fs").WriteStream;
  76. realpathPromise(p: PortablePath): Promise<PortablePath>;
  77. realpathSync(p: PortablePath): PortablePath;
  78. existsPromise(p: PortablePath): Promise<boolean>;
  79. existsSync(p: PortablePath): boolean;
  80. accessPromise(p: PortablePath, mode?: number): Promise<void>;
  81. accessSync(p: PortablePath, mode?: number): void;
  82. statPromise(p: PortablePath): Promise<Stats>;
  83. statPromise(p: PortablePath, opts: (StatOptions & {
  84. bigint?: false | undefined;
  85. }) | undefined): Promise<Stats>;
  86. statPromise(p: PortablePath, opts: StatOptions & {
  87. bigint: true;
  88. }): Promise<BigIntStats>;
  89. statPromise(p: PortablePath, opts?: StatOptions): Promise<Stats | BigIntStats>;
  90. statSync(p: PortablePath): Stats;
  91. statSync(p: PortablePath, opts?: StatSyncOptions & {
  92. bigint?: false | undefined;
  93. throwIfNoEntry: false;
  94. }): Stats | undefined;
  95. statSync(p: PortablePath, opts: StatSyncOptions & {
  96. bigint: true;
  97. throwIfNoEntry: false;
  98. }): BigIntStats | undefined;
  99. statSync(p: PortablePath, opts?: StatSyncOptions & {
  100. bigint?: false | undefined;
  101. }): Stats;
  102. statSync(p: PortablePath, opts: StatSyncOptions & {
  103. bigint: true;
  104. }): BigIntStats;
  105. statSync(p: PortablePath, opts: StatSyncOptions & {
  106. bigint: boolean;
  107. throwIfNoEntry?: false | undefined;
  108. }): Stats | BigIntStats;
  109. fstatPromise(fd: number): Promise<Stats>;
  110. fstatPromise(fd: number, opts: {
  111. bigint: true;
  112. }): Promise<BigIntStats>;
  113. fstatPromise(fd: number, opts?: {
  114. bigint: boolean;
  115. }): Promise<BigIntStats | Stats>;
  116. fstatSync(fd: number): Stats;
  117. fstatSync(fd: number, opts: {
  118. bigint: true;
  119. }): BigIntStats;
  120. fstatSync(fd: number, opts?: {
  121. bigint: boolean;
  122. }): BigIntStats | Stats;
  123. lstatPromise(p: PortablePath): Promise<Stats>;
  124. lstatPromise(p: PortablePath, opts: (StatOptions & {
  125. bigint?: false | undefined;
  126. }) | undefined): Promise<Stats>;
  127. lstatPromise(p: PortablePath, opts: StatOptions & {
  128. bigint: true;
  129. }): Promise<BigIntStats>;
  130. lstatSync(p: PortablePath): Stats;
  131. lstatSync(p: PortablePath, opts?: StatSyncOptions & {
  132. bigint?: false | undefined;
  133. throwIfNoEntry: false;
  134. }): Stats | undefined;
  135. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  136. bigint: true;
  137. throwIfNoEntry: false;
  138. }): BigIntStats | undefined;
  139. lstatSync(p: PortablePath, opts?: StatSyncOptions & {
  140. bigint?: false | undefined;
  141. }): Stats;
  142. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  143. bigint: true;
  144. }): BigIntStats;
  145. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  146. bigint: boolean;
  147. throwIfNoEntry?: false | undefined;
  148. }): Stats | BigIntStats;
  149. fchmodPromise(fd: number, mask: number): Promise<void>;
  150. fchmodSync(fd: number, mask: number): void;
  151. chmodPromise(p: PortablePath, mask: number): Promise<void>;
  152. chmodSync(p: PortablePath, mask: number): void;
  153. fchownPromise(fd: number, uid: number, gid: number): Promise<void>;
  154. fchownSync(fd: number, uid: number, gid: number): void;
  155. chownPromise(p: PortablePath, uid: number, gid: number): Promise<void>;
  156. chownSync(p: PortablePath, uid: number, gid: number): void;
  157. renamePromise(oldP: PortablePath, newP: PortablePath): Promise<void>;
  158. renameSync(oldP: PortablePath, newP: PortablePath): void;
  159. copyFilePromise(sourceP: PortablePath, destP: PortablePath, flags?: number): Promise<void>;
  160. copyFileSync(sourceP: PortablePath, destP: PortablePath, flags?: number): void;
  161. appendFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  162. appendFileSync(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  163. writeFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  164. writeFileSync(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  165. unlinkPromise(p: PortablePath): Promise<void>;
  166. unlinkSync(p: PortablePath): void;
  167. utimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
  168. utimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): void;
  169. mkdirPromise(p: PortablePath, opts?: MkdirOptions): Promise<string | undefined>;
  170. mkdirSync(p: PortablePath, opts?: MkdirOptions): string | undefined;
  171. rmdirPromise(p: PortablePath, opts?: RmdirOptions): Promise<void>;
  172. rmdirSync(p: PortablePath, opts?: RmdirOptions): void;
  173. linkPromise(existingP: PortablePath, newP: PortablePath): Promise<void>;
  174. linkSync(existingP: PortablePath, newP: PortablePath): void;
  175. symlinkPromise(target: PortablePath, p: PortablePath, type?: SymlinkType): Promise<void>;
  176. symlinkSync(target: PortablePath, p: PortablePath, type?: SymlinkType): void;
  177. readFilePromise(p: FSPath<PortablePath>, encoding: 'utf8'): Promise<string>;
  178. readFilePromise(p: FSPath<PortablePath>, encoding?: string): Promise<Buffer>;
  179. readFileSync(p: FSPath<PortablePath>, encoding: 'utf8'): string;
  180. readFileSync(p: FSPath<PortablePath>, encoding?: string): Buffer;
  181. readdirPromise(p: PortablePath): Promise<Array<Filename>>;
  182. readdirPromise(p: PortablePath, opts: {
  183. withFileTypes: false;
  184. } | null): Promise<Array<Filename>>;
  185. readdirPromise(p: PortablePath, opts: {
  186. withFileTypes: true;
  187. }): Promise<Array<Dirent>>;
  188. readdirPromise(p: PortablePath, opts: {
  189. withFileTypes: boolean;
  190. }): Promise<Array<Filename> | Array<Dirent>>;
  191. readdirSync(p: PortablePath): Array<Filename>;
  192. readdirSync(p: PortablePath, opts: {
  193. withFileTypes: false;
  194. } | null): Array<Filename>;
  195. readdirSync(p: PortablePath, opts: {
  196. withFileTypes: true;
  197. }): Array<Dirent>;
  198. readdirSync(p: PortablePath, opts: {
  199. withFileTypes: boolean;
  200. }): Array<Filename> | Array<Dirent>;
  201. readlinkPromise(p: PortablePath): Promise<PortablePath>;
  202. readlinkSync(p: PortablePath): PortablePath;
  203. truncatePromise(p: PortablePath, len?: number): Promise<void>;
  204. truncateSync(p: PortablePath, len?: number): void;
  205. ftruncatePromise(fd: number, len?: number): Promise<void>;
  206. ftruncateSync(fd: number, len?: number): void;
  207. watch(p: PortablePath, cb?: WatchCallback): Watcher;
  208. watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
  209. watchFile(p: PortablePath, cb: WatchFileCallback): StatWatcher;
  210. watchFile(p: PortablePath, opts: WatchFileOptions, cb: WatchFileCallback): StatWatcher;
  211. unwatchFile(p: PortablePath, cb?: WatchFileCallback): void;
  212. private makeCallPromise;
  213. private makeCallSync;
  214. private findZip;
  215. private limitOpenFilesTimeout;
  216. private limitOpenFiles;
  217. private getZipPromise;
  218. private getZipSync;
  219. }