ZipFS.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. import { Libzip } from '@yarnpkg/libzip';
  5. import { ReadStream, WriteStream } from 'fs';
  6. import { WatchOptions, WatchCallback, Watcher, Dir, Stats, BigIntStats, StatSyncOptions, StatOptions } from './FakeFS';
  7. import { FakeFS, MkdirOptions, RmdirOptions, WriteFileOptions, OpendirOptions } from './FakeFS';
  8. import { CreateReadStreamOptions, CreateWriteStreamOptions, BasePortableFakeFS, ExtractHintOptions, WatchFileCallback, WatchFileOptions, StatWatcher } from './FakeFS';
  9. import * as errors from './errors';
  10. import { FSPath, PortablePath, Filename } from './path';
  11. import * as statUtils from './statUtils';
  12. export type ZipCompression = `mixed` | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
  13. export declare const DEFAULT_COMPRESSION_LEVEL: ZipCompression;
  14. export type ZipBufferOptions = {
  15. libzip: Libzip;
  16. readOnly?: boolean;
  17. stats?: Stats;
  18. level?: ZipCompression;
  19. };
  20. export type ZipPathOptions = ZipBufferOptions & {
  21. baseFs?: FakeFS<PortablePath>;
  22. create?: boolean;
  23. };
  24. export declare function makeEmptyArchive(): Buffer;
  25. export declare class ZipFS extends BasePortableFakeFS {
  26. private readonly libzip;
  27. private readonly baseFs;
  28. private readonly path;
  29. private readonly stats;
  30. private readonly zip;
  31. private readonly lzSource;
  32. private readonly level;
  33. private readonly listings;
  34. private readonly entries;
  35. /**
  36. * A cache of indices mapped to file sources.
  37. * Populated by `setFileSource` calls.
  38. * Required for supporting read after write.
  39. */
  40. private readonly fileSources;
  41. private symlinkCount;
  42. private readonly fds;
  43. private nextFd;
  44. private ready;
  45. private readOnly;
  46. constructor(p: PortablePath, opts: ZipPathOptions);
  47. /**
  48. * Create a ZipFS in memory
  49. * @param data If null; an empty zip file will be created
  50. */
  51. constructor(data: Buffer | null, opts: ZipBufferOptions);
  52. makeLibzipError(error: number): errors.LibzipError;
  53. getExtractHint(hints: ExtractHintOptions): boolean;
  54. getAllFiles(): PortablePath[];
  55. getRealPath(): PortablePath;
  56. getBufferAndClose(): Buffer;
  57. private prepareClose;
  58. saveAndClose(): void;
  59. discardAndClose(): void;
  60. resolve(p: PortablePath): PortablePath;
  61. openPromise(p: PortablePath, flags: string, mode?: number): Promise<number>;
  62. openSync(p: PortablePath, flags: string, mode?: number): number;
  63. hasOpenFileHandles(): boolean;
  64. opendirPromise(p: PortablePath, opts?: OpendirOptions): Promise<Dir<PortablePath>>;
  65. opendirSync(p: PortablePath, opts?: OpendirOptions): Dir<PortablePath>;
  66. readPromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number | null): Promise<number>;
  67. readSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number | null): 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, { encoding }?: CreateReadStreamOptions): ReadStream;
  75. createWriteStream(p: PortablePath | null, { encoding }?: CreateWriteStreamOptions): 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. statSync(p: PortablePath, opts?: StatSyncOptions): Stats | BigIntStats | undefined;
  110. fstatPromise(fd: number): Promise<Stats>;
  111. fstatPromise(fd: number, opts: {
  112. bigint: true;
  113. }): Promise<BigIntStats>;
  114. fstatPromise(fd: number, opts?: {
  115. bigint: boolean;
  116. }): Promise<BigIntStats | Stats>;
  117. fstatSync(fd: number): Stats;
  118. fstatSync(fd: number, opts: {
  119. bigint: true;
  120. }): BigIntStats;
  121. fstatSync(fd: number, opts?: {
  122. bigint: boolean;
  123. }): BigIntStats | Stats;
  124. lstatPromise(p: PortablePath): Promise<Stats>;
  125. lstatPromise(p: PortablePath, opts: (StatOptions & {
  126. bigint?: false | undefined;
  127. }) | undefined): Promise<Stats>;
  128. lstatPromise(p: PortablePath, opts: StatOptions & {
  129. bigint: true;
  130. }): Promise<BigIntStats>;
  131. lstatPromise(p: PortablePath, opts?: StatOptions): Promise<Stats | BigIntStats>;
  132. lstatSync(p: PortablePath): Stats;
  133. lstatSync(p: PortablePath, opts?: StatSyncOptions & {
  134. bigint?: false | undefined;
  135. throwIfNoEntry: false;
  136. }): Stats | undefined;
  137. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  138. bigint: true;
  139. throwIfNoEntry: false;
  140. }): BigIntStats | undefined;
  141. lstatSync(p: PortablePath, opts?: StatSyncOptions & {
  142. bigint?: false | undefined;
  143. }): Stats;
  144. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  145. bigint: true;
  146. }): BigIntStats;
  147. lstatSync(p: PortablePath, opts: StatSyncOptions & {
  148. bigint: boolean;
  149. throwIfNoEntry?: false | undefined;
  150. }): Stats | BigIntStats;
  151. lstatSync(p: PortablePath, opts?: StatSyncOptions): Stats | BigIntStats | undefined;
  152. private statImpl;
  153. private getUnixMode;
  154. private registerListing;
  155. private registerEntry;
  156. private unregisterListing;
  157. private unregisterEntry;
  158. private deleteEntry;
  159. private resolveFilename;
  160. private allocateBuffer;
  161. private allocateUnattachedSource;
  162. private allocateSource;
  163. private setFileSource;
  164. private isSymbolicLink;
  165. private getFileSource;
  166. fchmodPromise(fd: number, mask: number): Promise<void>;
  167. fchmodSync(fd: number, mask: number): void;
  168. chmodPromise(p: PortablePath, mask: number): Promise<void>;
  169. chmodSync(p: PortablePath, mask: number): void;
  170. fchownPromise(fd: number, uid: number, gid: number): Promise<void>;
  171. fchownSync(fd: number, uid: number, gid: number): void;
  172. chownPromise(p: PortablePath, uid: number, gid: number): Promise<void>;
  173. chownSync(p: PortablePath, uid: number, gid: number): void;
  174. renamePromise(oldP: PortablePath, newP: PortablePath): Promise<never>;
  175. renameSync(oldP: PortablePath, newP: PortablePath): never;
  176. copyFilePromise(sourceP: PortablePath, destP: PortablePath, flags?: number): Promise<void>;
  177. copyFileSync(sourceP: PortablePath, destP: PortablePath, flags?: number): void;
  178. private prepareCopyFile;
  179. appendFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  180. appendFileSync(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  181. private fdToPath;
  182. writeFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  183. writeFileSync(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  184. private prepareWriteFile;
  185. unlinkPromise(p: PortablePath): Promise<void>;
  186. unlinkSync(p: PortablePath): void;
  187. utimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
  188. utimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): void;
  189. lutimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
  190. lutimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number): void;
  191. private utimesImpl;
  192. mkdirPromise(p: PortablePath, opts?: MkdirOptions): Promise<string | undefined>;
  193. mkdirSync(p: PortablePath, { mode, recursive }?: MkdirOptions): string | undefined;
  194. rmdirPromise(p: PortablePath, opts?: RmdirOptions): Promise<void>;
  195. rmdirSync(p: PortablePath, { recursive }?: RmdirOptions): void;
  196. private hydrateDirectory;
  197. linkPromise(existingP: PortablePath, newP: PortablePath): Promise<void>;
  198. linkSync(existingP: PortablePath, newP: PortablePath): void;
  199. symlinkPromise(target: PortablePath, p: PortablePath): Promise<void>;
  200. symlinkSync(target: PortablePath, p: PortablePath): void;
  201. readFilePromise(p: FSPath<PortablePath>, encoding: 'utf8'): Promise<string>;
  202. readFilePromise(p: FSPath<PortablePath>, encoding?: string): Promise<Buffer>;
  203. readFileSync(p: FSPath<PortablePath>, encoding: 'utf8'): string;
  204. readFileSync(p: FSPath<PortablePath>, encoding?: string): Buffer;
  205. private readFileBuffer;
  206. readdirPromise(p: PortablePath): Promise<Array<Filename>>;
  207. readdirPromise(p: PortablePath, opts: {
  208. withFileTypes: false;
  209. } | null): Promise<Array<Filename>>;
  210. readdirPromise(p: PortablePath, opts: {
  211. withFileTypes: true;
  212. }): Promise<Array<statUtils.DirEntry>>;
  213. readdirPromise(p: PortablePath, opts: {
  214. withFileTypes: boolean;
  215. }): Promise<Array<Filename> | Array<statUtils.DirEntry>>;
  216. readdirSync(p: PortablePath): Array<Filename>;
  217. readdirSync(p: PortablePath, opts: {
  218. withFileTypes: false;
  219. } | null): Array<Filename>;
  220. readdirSync(p: PortablePath, opts: {
  221. withFileTypes: true;
  222. }): Array<statUtils.DirEntry>;
  223. readdirSync(p: PortablePath, opts: {
  224. withFileTypes: boolean;
  225. }): Array<Filename> | Array<statUtils.DirEntry>;
  226. readlinkPromise(p: PortablePath): Promise<PortablePath>;
  227. readlinkSync(p: PortablePath): PortablePath;
  228. private prepareReadlink;
  229. truncatePromise(p: PortablePath, len?: number): Promise<void>;
  230. truncateSync(p: PortablePath, len?: number): void;
  231. ftruncatePromise(fd: number, len?: number): Promise<void>;
  232. ftruncateSync(fd: number, len?: number): void;
  233. watch(p: PortablePath, cb?: WatchCallback): Watcher;
  234. watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
  235. watchFile(p: PortablePath, cb: WatchFileCallback): StatWatcher;
  236. watchFile(p: PortablePath, opts: WatchFileOptions, cb: WatchFileCallback): StatWatcher;
  237. unwatchFile(p: PortablePath, cb?: WatchFileCallback): void;
  238. }