fsx.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @fileoverview The main file for the hfs package.
  3. * @author Nicholas C. Zakas
  4. */
  5. /** @typedef{import("@humanfs/types").HfsImpl} HfsImpl */
  6. /** @typedef{import("@humanfs/types").HfsDirectoryEntry} HfsDirectoryEntry */
  7. /**
  8. * Error to represent when a method is missing on an impl.
  9. */
  10. export class NoSuchMethodError extends Error {
  11. /**
  12. * Creates a new instance.
  13. * @param {string} methodName The name of the method that was missing.
  14. */
  15. constructor(methodName: string);
  16. }
  17. /**
  18. * Error to represent when an impl is already set.
  19. */
  20. export class ImplAlreadySetError extends Error {
  21. /**
  22. * Creates a new instance.
  23. */
  24. constructor();
  25. }
  26. /**
  27. * A class representing a log entry.
  28. */
  29. export class LogEntry {
  30. /**
  31. * Creates a new instance.
  32. * @param {string} type The type of log entry.
  33. * @param {any} [data] The data associated with the log entry.
  34. */
  35. constructor(type: string, data?: any);
  36. /**
  37. * The time at which the log entry was created.
  38. * @type {number}
  39. */
  40. timestamp: number;
  41. methodName: string;
  42. data: any;
  43. #private;
  44. }
  45. /**
  46. * A class representing a file system utility library.
  47. * @implements {HfsImpl}
  48. */
  49. export class Hfs implements HfsImpl {
  50. /**
  51. * Creates a new instance.
  52. * @param {object} options The options for the instance.
  53. * @param {HfsImpl} options.impl The implementation to use.
  54. */
  55. constructor({ impl }: {
  56. impl: HfsImpl;
  57. });
  58. /**
  59. * Starts a new log with the given name.
  60. * @param {string} name The name of the log to start;
  61. * @returns {void}
  62. * @throws {Error} When the log already exists.
  63. * @throws {TypeError} When the name is not a non-empty string.
  64. */
  65. logStart(name: string): void;
  66. /**
  67. * Ends a log with the given name and returns the entries.
  68. * @param {string} name The name of the log to end.
  69. * @returns {Array<LogEntry>} The entries in the log.
  70. * @throws {Error} When the log does not exist.
  71. */
  72. logEnd(name: string): Array<LogEntry>;
  73. /**
  74. * Determines if the current implementation is the base implementation.
  75. * @returns {boolean} True if the current implementation is the base implementation.
  76. */
  77. isBaseImpl(): boolean;
  78. /**
  79. * Sets the implementation for this instance.
  80. * @param {object} impl The implementation to use.
  81. * @returns {void}
  82. */
  83. setImpl(impl: object): void;
  84. /**
  85. * Resets the implementation for this instance back to its original.
  86. * @returns {void}
  87. */
  88. resetImpl(): void;
  89. /**
  90. * Reads the given file and returns the contents as text. Assumes UTF-8 encoding.
  91. * @param {string} filePath The file to read.
  92. * @returns {Promise<string|undefined>} The contents of the file.
  93. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  94. * @throws {TypeError} When the file path is not a non-empty string.
  95. */
  96. text(filePath: string): Promise<string | undefined>;
  97. /**
  98. * Reads the given file and returns the contents as JSON. Assumes UTF-8 encoding.
  99. * @param {string} filePath The file to read.
  100. * @returns {Promise<any|undefined>} The contents of the file as JSON.
  101. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  102. * @throws {SyntaxError} When the file contents are not valid JSON.
  103. * @throws {TypeError} When the file path is not a non-empty string.
  104. */
  105. json(filePath: string): Promise<any | undefined>;
  106. /**
  107. * Reads the given file and returns the contents as an ArrayBuffer.
  108. * @param {string} filePath The file to read.
  109. * @returns {Promise<ArrayBuffer|undefined>} The contents of the file as an ArrayBuffer.
  110. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  111. * @throws {TypeError} When the file path is not a non-empty string.
  112. * @deprecated Use bytes() instead.
  113. */
  114. arrayBuffer(filePath: string): Promise<ArrayBuffer | undefined>;
  115. /**
  116. * Reads the given file and returns the contents as an Uint8Array.
  117. * @param {string} filePath The file to read.
  118. * @returns {Promise<Uint8Array|undefined>} The contents of the file as an Uint8Array.
  119. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  120. * @throws {TypeError} When the file path is not a non-empty string.
  121. */
  122. bytes(filePath: string): Promise<Uint8Array | undefined>;
  123. /**
  124. * Writes the given data to the given file. Creates any necessary directories along the way.
  125. * If the data is a string, UTF-8 encoding is used.
  126. * @param {string} filePath The file to write.
  127. * @param {any} contents The data to write.
  128. * @returns {Promise<void>} A promise that resolves when the file is written.
  129. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  130. * @throws {TypeError} When the file path is not a non-empty string.
  131. */
  132. write(filePath: string, contents: any): Promise<void>;
  133. /**
  134. * Determines if the given file exists.
  135. * @param {string} filePath The file to check.
  136. * @returns {Promise<boolean>} True if the file exists.
  137. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  138. * @throws {TypeError} When the file path is not a non-empty string.
  139. */
  140. isFile(filePath: string): Promise<boolean>;
  141. /**
  142. * Determines if the given directory exists.
  143. * @param {string} dirPath The directory to check.
  144. * @returns {Promise<boolean>} True if the directory exists.
  145. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  146. * @throws {TypeError} When the directory path is not a non-empty string.
  147. */
  148. isDirectory(dirPath: string): Promise<boolean>;
  149. /**
  150. * Creates the given directory.
  151. * @param {string} dirPath The directory to create.
  152. * @returns {Promise<void>} A promise that resolves when the directory is created.
  153. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  154. * @throws {TypeError} When the directory path is not a non-empty string.
  155. */
  156. createDirectory(dirPath: string): Promise<void>;
  157. /**
  158. * Deletes the given file.
  159. * @param {string} filePath The file to delete.
  160. * @returns {Promise<void>} A promise that resolves when the file is deleted.
  161. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  162. * @throws {TypeError} When the file path is not a non-empty string.
  163. */
  164. delete(filePath: string): Promise<void>;
  165. /**
  166. * Deletes the given directory.
  167. * @param {string} dirPath The directory to delete.
  168. * @returns {Promise<void>} A promise that resolves when the directory is deleted.
  169. * @throws {NoSuchMethodError} When the method does not exist on the current implementation.
  170. * @throws {TypeError} When the directory path is not a non-empty string.
  171. */
  172. deleteAll(dirPath: string): Promise<void>;
  173. /**
  174. * Returns a list of directory entries for the given path.
  175. * @param {string} dirPath The path to the directory to read.
  176. * @returns {AsyncIterable<HfsDirectoryEntry>} A promise that resolves with the
  177. * directory entries.
  178. * @throws {TypeError} If the directory path is not a string.
  179. * @throws {Error} If the directory cannot be read.
  180. */
  181. list(dirPath: string): AsyncIterable<HfsDirectoryEntry>;
  182. /**
  183. * Returns the size of the given file.
  184. * @param {string} filePath The path to the file to read.
  185. * @returns {Promise<number>} A promise that resolves with the size of the file.
  186. * @throws {TypeError} If the file path is not a string.
  187. * @throws {Error} If the file cannot be read.
  188. */
  189. size(filePath: string): Promise<number>;
  190. #private;
  191. }
  192. export type HfsImpl = import("@humanfs/types").HfsImpl;
  193. export type HfsDirectoryEntry = import("@humanfs/types").HfsDirectoryEntry;