node-hfs.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /// <reference types="node" resolution-mode="require"/>
  2. /**
  3. * A class representing the Node.js implementation of Hfs.
  4. * @implements {HfsImpl}
  5. */
  6. export class NodeHfsImpl implements HfsImpl {
  7. /**
  8. * Creates a new instance.
  9. * @param {object} [options] The options for the instance.
  10. * @param {Fsp} [options.fsp] The file system module to use.
  11. */
  12. constructor({ fsp }?: {
  13. fsp?: Fsp;
  14. });
  15. /**
  16. * Reads a file and returns the contents as an Uint8Array.
  17. * @param {string|URL} filePath The path to the file to read.
  18. * @returns {Promise<Uint8Array|undefined>} A promise that resolves with the contents
  19. * of the file or undefined if the file doesn't exist.
  20. * @throws {Error} If the file cannot be read.
  21. * @throws {TypeError} If the file path is not a string.
  22. */
  23. bytes(filePath: string | URL): Promise<Uint8Array | undefined>;
  24. /**
  25. * Writes a value to a file. If the value is a string, UTF-8 encoding is used.
  26. * @param {string|URL} filePath The path to the file to write.
  27. * @param {Uint8Array} contents The contents to write to the
  28. * file.
  29. * @returns {Promise<void>} A promise that resolves when the file is
  30. * written.
  31. * @throws {TypeError} If the file path is not a string.
  32. * @throws {Error} If the file cannot be written.
  33. */
  34. write(filePath: string | URL, contents: Uint8Array): Promise<void>;
  35. /**
  36. * Appends a value to a file. If the value is a string, UTF-8 encoding is used.
  37. * @param {string|URL} filePath The path to the file to append to.
  38. * @param {Uint8Array} contents The contents to append to the
  39. * file.
  40. * @returns {Promise<void>} A promise that resolves when the file is
  41. * written.
  42. * @throws {TypeError} If the file path is not a string.
  43. * @throws {Error} If the file cannot be appended to.
  44. */
  45. append(filePath: string | URL, contents: Uint8Array): Promise<void>;
  46. /**
  47. * Checks if a file exists.
  48. * @param {string|URL} filePath The path to the file to check.
  49. * @returns {Promise<boolean>} A promise that resolves with true if the
  50. * file exists or false if it does not.
  51. * @throws {Error} If the operation fails with a code other than ENOENT.
  52. */
  53. isFile(filePath: string | URL): Promise<boolean>;
  54. /**
  55. * Checks if a directory exists.
  56. * @param {string|URL} dirPath The path to the directory to check.
  57. * @returns {Promise<boolean>} A promise that resolves with true if the
  58. * directory exists or false if it does not.
  59. * @throws {Error} If the operation fails with a code other than ENOENT.
  60. */
  61. isDirectory(dirPath: string | URL): Promise<boolean>;
  62. /**
  63. * Creates a directory recursively.
  64. * @param {string|URL} dirPath The path to the directory to create.
  65. * @returns {Promise<void>} A promise that resolves when the directory is
  66. * created.
  67. */
  68. createDirectory(dirPath: string | URL): Promise<void>;
  69. /**
  70. * Deletes a file or empty directory.
  71. * @param {string|URL} fileOrDirPath The path to the file or directory to
  72. * delete.
  73. * @returns {Promise<boolean>} A promise that resolves when the file or
  74. * directory is deleted, true if the file or directory is deleted, false
  75. * if the file or directory does not exist.
  76. * @throws {TypeError} If the file or directory path is not a string.
  77. * @throws {Error} If the file or directory cannot be deleted.
  78. */
  79. delete(fileOrDirPath: string | URL): Promise<boolean>;
  80. /**
  81. * Deletes a file or directory recursively.
  82. * @param {string|URL} fileOrDirPath The path to the file or directory to
  83. * delete.
  84. * @returns {Promise<boolean>} A promise that resolves when the file or
  85. * directory is deleted, true if the file or directory is deleted, false
  86. * if the file or directory does not exist.
  87. * @throws {TypeError} If the file or directory path is not a string.
  88. * @throws {Error} If the file or directory cannot be deleted.
  89. */
  90. deleteAll(fileOrDirPath: string | URL): Promise<boolean>;
  91. /**
  92. * Returns a list of directory entries for the given path.
  93. * @param {string|URL} dirPath The path to the directory to read.
  94. * @returns {AsyncIterable<HfsDirectoryEntry>} A promise that resolves with the
  95. * directory entries.
  96. * @throws {TypeError} If the directory path is not a string.
  97. * @throws {Error} If the directory cannot be read.
  98. */
  99. list(dirPath: string | URL): AsyncIterable<HfsDirectoryEntry>;
  100. /**
  101. * Returns the size of a file. This method handles ENOENT errors
  102. * and returns undefined in that case.
  103. * @param {string|URL} filePath The path to the file to read.
  104. * @returns {Promise<number|undefined>} A promise that resolves with the size of the
  105. * file in bytes or undefined if the file doesn't exist.
  106. */
  107. size(filePath: string | URL): Promise<number | undefined>;
  108. /**
  109. * Returns the last modified date of a file or directory. This method handles ENOENT errors
  110. * and returns undefined in that case.
  111. * @param {string|URL} fileOrDirPath The path to the file to read.
  112. * @returns {Promise<Date|undefined>} A promise that resolves with the last modified
  113. * date of the file or directory, or undefined if the file doesn't exist.
  114. */
  115. lastModified(fileOrDirPath: string | URL): Promise<Date | undefined>;
  116. /**
  117. * Copies a file from one location to another.
  118. * @param {string|URL} source The path to the file to copy.
  119. * @param {string|URL} destination The path to copy the file to.
  120. * @returns {Promise<void>} A promise that resolves when the file is copied.
  121. * @throws {Error} If the source file does not exist.
  122. * @throws {Error} If the source file is a directory.
  123. * @throws {Error} If the destination file is a directory.
  124. */
  125. copy(source: string | URL, destination: string | URL): Promise<void>;
  126. /**
  127. * Copies a file or directory from one location to another.
  128. * @param {string|URL} source The path to the file or directory to copy.
  129. * @param {string|URL} destination The path to copy the file or directory to.
  130. * @returns {Promise<void>} A promise that resolves when the file or directory is
  131. * copied.
  132. * @throws {Error} If the source file or directory does not exist.
  133. * @throws {Error} If the destination file or directory is a directory.
  134. */
  135. copyAll(source: string | URL, destination: string | URL): Promise<void>;
  136. /**
  137. * Moves a file from the source path to the destination path.
  138. * @param {string|URL} source The location of the file to move.
  139. * @param {string|URL} destination The destination of the file to move.
  140. * @returns {Promise<void>} A promise that resolves when the move is complete.
  141. * @throws {TypeError} If the file paths are not strings.
  142. * @throws {Error} If the file cannot be moved.
  143. */
  144. move(source: string | URL, destination: string | URL): Promise<void>;
  145. /**
  146. * Moves a file or directory from the source path to the destination path.
  147. * @param {string|URL} source The location of the file or directory to move.
  148. * @param {string|URL} destination The destination of the file or directory to move.
  149. * @returns {Promise<void>} A promise that resolves when the move is complete.
  150. * @throws {TypeError} If the file paths are not strings.
  151. * @throws {Error} If the file or directory cannot be moved.
  152. */
  153. moveAll(source: string | URL, destination: string | URL): Promise<void>;
  154. #private;
  155. }
  156. /**
  157. * A class representing a file system utility library.
  158. * @implements {HfsImpl}
  159. */
  160. export class NodeHfs extends Hfs implements HfsImpl {
  161. /**
  162. * Creates a new instance.
  163. * @param {object} [options] The options for the instance.
  164. * @param {Fsp} [options.fsp] The file system module to use.
  165. */
  166. constructor({ fsp }?: {
  167. fsp?: Fsp;
  168. });
  169. }
  170. export const hfs: NodeHfs;
  171. export type HfsImpl = import("@humanfs/types").HfsImpl;
  172. export type HfsDirectoryEntry = import("@humanfs/types").HfsDirectoryEntry;
  173. export type Fsp = typeof nativeFsp;
  174. export type Dirent = import("fs").Dirent;
  175. import { Hfs } from "@humanfs/core";
  176. import nativeFsp from "node:fs/promises";