hfs.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Error to represent when a method is missing on an impl.
  3. */
  4. export class NoSuchMethodError extends Error {
  5. /**
  6. * Creates a new instance.
  7. * @param {string} methodName The name of the method that was missing.
  8. */
  9. constructor(methodName: string);
  10. }
  11. /**
  12. * Error to represent when a method is not supported on an impl. This happens
  13. * when a method on `Hfs` is called with one name and the corresponding method
  14. * on the impl has a different name. (Example: `text()` and `bytes()`.)
  15. */
  16. export class MethodNotSupportedError extends Error {
  17. /**
  18. * Creates a new instance.
  19. * @param {string} methodName The name of the method that was missing.
  20. */
  21. constructor(methodName: string);
  22. }
  23. /**
  24. * Error to represent when an impl is already set.
  25. */
  26. export class ImplAlreadySetError extends Error {
  27. /**
  28. * Creates a new instance.
  29. */
  30. constructor();
  31. }
  32. /**
  33. * A class representing a log entry.
  34. */
  35. export class LogEntry {
  36. /**
  37. * Creates a new instance.
  38. * @param {string} type The type of log entry.
  39. * @param {any} [data] The data associated with the log entry.
  40. */
  41. constructor(type: string, data?: any);
  42. /**
  43. * The type of log entry.
  44. * @type {string}
  45. */
  46. type: string;
  47. /**
  48. * The data associated with the log entry.
  49. * @type {any}
  50. */
  51. data: any;
  52. /**
  53. * The time at which the log entry was created.
  54. * @type {number}
  55. */
  56. timestamp: number;
  57. }
  58. /**
  59. * A class representing a file system utility library.
  60. * @implements {HfsImpl}
  61. */
  62. export class Hfs implements HfsImpl {
  63. /**
  64. * Creates a new instance.
  65. * @param {object} options The options for the instance.
  66. * @param {HfsImpl} options.impl The implementation to use.
  67. */
  68. constructor({ impl }: {
  69. impl: HfsImpl;
  70. });
  71. /**
  72. * Starts a new log with the given name.
  73. * @param {string} name The name of the log to start;
  74. * @returns {void}
  75. * @throws {Error} When the log already exists.
  76. * @throws {TypeError} When the name is not a non-empty string.
  77. */
  78. logStart(name: string): void;
  79. /**
  80. * Ends a log with the given name and returns the entries.
  81. * @param {string} name The name of the log to end.
  82. * @returns {Array<LogEntry>} The entries in the log.
  83. * @throws {Error} When the log does not exist.
  84. */
  85. logEnd(name: string): Array<LogEntry>;
  86. /**
  87. * Determines if the current implementation is the base implementation.
  88. * @returns {boolean} True if the current implementation is the base implementation.
  89. */
  90. isBaseImpl(): boolean;
  91. /**
  92. * Sets the implementation for this instance.
  93. * @param {object} impl The implementation to use.
  94. * @returns {void}
  95. */
  96. setImpl(impl: object): void;
  97. /**
  98. * Resets the implementation for this instance back to its original.
  99. * @returns {void}
  100. */
  101. resetImpl(): void;
  102. /**
  103. * Reads the given file and returns the contents as text. Assumes UTF-8 encoding.
  104. * @param {string|URL} filePath The file to read.
  105. * @returns {Promise<string|undefined>} The contents of the file.
  106. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  107. * @throws {TypeError} When the file path is not a non-empty string.
  108. */
  109. text(filePath: string | URL): Promise<string | undefined>;
  110. /**
  111. * Reads the given file and returns the contents as JSON. Assumes UTF-8 encoding.
  112. * @param {string|URL} filePath The file to read.
  113. * @returns {Promise<any|undefined>} The contents of the file as JSON.
  114. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  115. * @throws {SyntaxError} When the file contents are not valid JSON.
  116. * @throws {TypeError} When the file path is not a non-empty string.
  117. */
  118. json(filePath: string | URL): Promise<any | undefined>;
  119. /**
  120. * Reads the given file and returns the contents as an ArrayBuffer.
  121. * @param {string|URL} filePath The file to read.
  122. * @returns {Promise<ArrayBuffer|undefined>} The contents of the file as an ArrayBuffer.
  123. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  124. * @throws {TypeError} When the file path is not a non-empty string.
  125. * @deprecated Use bytes() instead.
  126. */
  127. arrayBuffer(filePath: string | URL): Promise<ArrayBuffer | undefined>;
  128. /**
  129. * Reads the given file and returns the contents as an Uint8Array.
  130. * @param {string|URL} filePath The file to read.
  131. * @returns {Promise<Uint8Array|undefined>} The contents of the file as an Uint8Array.
  132. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  133. * @throws {TypeError} When the file path is not a non-empty string.
  134. */
  135. bytes(filePath: string | URL): Promise<Uint8Array | undefined>;
  136. /**
  137. * Writes the given data to the given file. Creates any necessary directories along the way.
  138. * If the data is a string, UTF-8 encoding is used.
  139. * @param {string|URL} filePath The file to write.
  140. * @param {string|ArrayBuffer|ArrayBufferView} contents The data to write.
  141. * @returns {Promise<void>} A promise that resolves when the file is written.
  142. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  143. * @throws {TypeError} When the file path is not a non-empty string.
  144. */
  145. write(filePath: string | URL, contents: string | ArrayBuffer | ArrayBufferView): Promise<void>;
  146. /**
  147. * Appends the given data to the given file. Creates any necessary directories along the way.
  148. * If the data is a string, UTF-8 encoding is used.
  149. * @param {string|URL} filePath The file to append to.
  150. * @param {string|ArrayBuffer|ArrayBufferView} contents The data to append.
  151. * @returns {Promise<void>} A promise that resolves when the file is appended to.
  152. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  153. * @throws {TypeError} When the file path is not a non-empty string.
  154. * @throws {TypeError} When the file contents are not a string or ArrayBuffer.
  155. * @throws {Error} When the file cannot be appended to.
  156. */
  157. append(filePath: string | URL, contents: string | ArrayBuffer | ArrayBufferView): Promise<void>;
  158. /**
  159. * Determines if the given file exists.
  160. * @param {string|URL} filePath The file to check.
  161. * @returns {Promise<boolean>} True if the file exists.
  162. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  163. * @throws {TypeError} When the file path is not a non-empty string.
  164. */
  165. isFile(filePath: string | URL): Promise<boolean>;
  166. /**
  167. * Determines if the given directory exists.
  168. * @param {string|URL} dirPath The directory to check.
  169. * @returns {Promise<boolean>} True if the directory exists.
  170. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  171. * @throws {TypeError} When the directory path is not a non-empty string.
  172. */
  173. isDirectory(dirPath: string | URL): Promise<boolean>;
  174. /**
  175. * Creates the given directory.
  176. * @param {string|URL} dirPath The directory to create.
  177. * @returns {Promise<void>} A promise that resolves when the directory is created.
  178. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  179. * @throws {TypeError} When the directory path is not a non-empty string.
  180. */
  181. createDirectory(dirPath: string | URL): Promise<void>;
  182. /**
  183. * Deletes the given file or empty directory.
  184. * @param {string|URL} filePath The file to delete.
  185. * @returns {Promise<boolean>} A promise that resolves when the file or
  186. * directory is deleted, true if the file or directory is deleted, false
  187. * if the file or directory does not exist.
  188. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  189. * @throws {TypeError} When the file path is not a non-empty string.
  190. */
  191. delete(filePath: string | URL): Promise<boolean>;
  192. /**
  193. * Deletes the given file or directory recursively.
  194. * @param {string|URL} dirPath The directory to delete.
  195. * @returns {Promise<boolean>} A promise that resolves when the file or
  196. * directory is deleted, true if the file or directory is deleted, false
  197. * if the file or directory does not exist.
  198. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  199. * @throws {TypeError} When the directory path is not a non-empty string.
  200. */
  201. deleteAll(dirPath: string | URL): Promise<boolean>;
  202. /**
  203. * Returns a list of directory entries for the given path.
  204. * @param {string|URL} dirPath The path to the directory to read.
  205. * @returns {AsyncIterable<HfsDirectoryEntry>} A promise that resolves with the
  206. * directory entries.
  207. * @throws {TypeError} If the directory path is not a string or URL.
  208. * @throws {Error} If the directory cannot be read.
  209. */
  210. list(dirPath: string | URL): AsyncIterable<HfsDirectoryEntry>;
  211. /**
  212. * Walks a directory using a depth-first traversal and returns the entries
  213. * from the traversal.
  214. * @param {string|URL} dirPath The path to the directory to walk.
  215. * @param {Object} [options] The options for the walk.
  216. * @param {(entry:HfsWalkEntry) => Promise<boolean>|boolean} [options.directoryFilter] A filter function to determine
  217. * if a directory's entries should be included in the walk.
  218. * @param {(entry:HfsWalkEntry) => Promise<boolean>|boolean} [options.entryFilter] A filter function to determine if
  219. * an entry should be included in the walk.
  220. * @returns {AsyncIterable<HfsWalkEntry>} A promise that resolves with the
  221. * directory entries.
  222. * @throws {TypeError} If the directory path is not a string or URL.
  223. * @throws {Error} If the directory cannot be read.
  224. */
  225. walk(dirPath: string | URL, { directoryFilter, entryFilter }?: {
  226. directoryFilter?: (entry: HfsWalkEntry) => Promise<boolean> | boolean;
  227. entryFilter?: (entry: HfsWalkEntry) => Promise<boolean> | boolean;
  228. }): AsyncIterable<HfsWalkEntry>;
  229. /**
  230. * Returns the size of the given file.
  231. * @param {string|URL} filePath The path to the file to read.
  232. * @returns {Promise<number>} A promise that resolves with the size of the file.
  233. * @throws {TypeError} If the file path is not a string or URL.
  234. * @throws {Error} If the file cannot be read.
  235. */
  236. size(filePath: string | URL): Promise<number>;
  237. /**
  238. * Returns the last modified timestamp of the given file or directory.
  239. * @param {string|URL} fileOrDirPath The path to the file or directory.
  240. * @returns {Promise<Date|undefined>} A promise that resolves with the last modified date
  241. * or undefined if the file or directory does not exist.
  242. * @throws {TypeError} If the path is not a string or URL.
  243. */
  244. lastModified(fileOrDirPath: string | URL): Promise<Date | undefined>;
  245. /**
  246. * Copys a file from one location to another.
  247. * @param {string|URL} source The path to the file to copy.
  248. * @param {string|URL} destination The path to the new file.
  249. * @returns {Promise<void>} A promise that resolves when the file is copied.
  250. * @throws {TypeError} If the file path is not a string or URL.
  251. * @throws {Error} If the file cannot be copied.
  252. */
  253. copy(source: string | URL, destination: string | URL): Promise<void>;
  254. /**
  255. * Copies a file or directory from one location to another.
  256. * @param {string|URL} source The path to the file or directory to copy.
  257. * @param {string|URL} destination The path to copy the file or directory to.
  258. * @returns {Promise<void>} A promise that resolves when the file or directory is
  259. * copied.
  260. * @throws {TypeError} If the directory path is not a string or URL.
  261. * @throws {Error} If the directory cannot be copied.
  262. */
  263. copyAll(source: string | URL, destination: string | URL): Promise<void>;
  264. /**
  265. * Moves a file from the source path to the destination path.
  266. * @param {string|URL} source The location of the file to move.
  267. * @param {string|URL} destination The destination of the file to move.
  268. * @returns {Promise<void>} A promise that resolves when the move is complete.
  269. * @throws {TypeError} If the file or directory paths are not strings.
  270. * @throws {Error} If the file or directory cannot be moved.
  271. */
  272. move(source: string | URL, destination: string | URL): Promise<void>;
  273. /**
  274. * Moves a file or directory from one location to another.
  275. * @param {string|URL} source The path to the file or directory to move.
  276. * @param {string|URL} destination The path to move the file or directory to.
  277. * @returns {Promise<void>} A promise that resolves when the file or directory is
  278. * moved.
  279. * @throws {TypeError} If the source is not a string or URL.
  280. * @throws {TypeError} If the destination is not a string or URL.
  281. * @throws {Error} If the file or directory cannot be moved.
  282. */
  283. moveAll(source: string | URL, destination: string | URL): Promise<void>;
  284. #private;
  285. }
  286. export type HfsImpl = import("@humanfs/types").HfsImpl;
  287. export type HfsDirectoryEntry = import("@humanfs/types").HfsDirectoryEntry;
  288. export type HfsWalkEntry = import("@humanfs/types").HfsWalkEntry;