node-fsx.d.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 a string. Assumes UTF-8 encoding.
  17. * @param {string} filePath The path to the file to read.
  18. * @returns {Promise<string|undefined>} A promise that resolves with the contents of
  19. * the file or undefined if the file doesn't exist.
  20. * @throws {TypeError} If the file path is not a string.
  21. * @throws {RangeError} If the file path is empty.
  22. * @throws {RangeError} If the file path is not absolute.
  23. * @throws {RangeError} If the file path is not a file.
  24. * @throws {RangeError} If the file path is not readable.
  25. */
  26. text(filePath: string): Promise<string | undefined>;
  27. /**
  28. * Reads a file and returns the contents as a JSON object. Assumes UTF-8 encoding.
  29. * @param {string} filePath The path to the file to read.
  30. * @returns {Promise<object|undefined>} A promise that resolves with the contents of
  31. * the file or undefined if the file doesn't exist.
  32. * @throws {SyntaxError} If the file contents are not valid JSON.
  33. * @throws {Error} If the file cannot be read.
  34. * @throws {TypeError} If the file path is not a string.
  35. */
  36. json(filePath: string): Promise<object | undefined>;
  37. /**
  38. * Reads a file and returns the contents as an ArrayBuffer.
  39. * @param {string} filePath The path to the file to read.
  40. * @returns {Promise<ArrayBuffer|undefined>} A promise that resolves with the contents
  41. * of the file or undefined if the file doesn't exist.
  42. * @throws {Error} If the file cannot be read.
  43. * @throws {TypeError} If the file path is not a string.
  44. * @deprecated Use bytes() instead.
  45. */
  46. arrayBuffer(filePath: string): Promise<ArrayBuffer | undefined>;
  47. /**
  48. * Reads a file and returns the contents as an Uint8Array.
  49. * @param {string} filePath The path to the file to read.
  50. * @returns {Promise<Uint8Array|undefined>} A promise that resolves with the contents
  51. * of the file or undefined if the file doesn't exist.
  52. * @throws {Error} If the file cannot be read.
  53. * @throws {TypeError} If the file path is not a string.
  54. */
  55. bytes(filePath: string): Promise<Uint8Array | undefined>;
  56. /**
  57. * Writes a value to a file. If the value is a string, UTF-8 encoding is used.
  58. * @param {string} filePath The path to the file to write.
  59. * @param {string|ArrayBuffer|ArrayBufferView} contents The contents to write to the
  60. * file.
  61. * @returns {Promise<void>} A promise that resolves when the file is
  62. * written.
  63. * @throws {TypeError} If the file path is not a string.
  64. * @throws {Error} If the file cannot be written.
  65. */
  66. write(filePath: string, contents: string | ArrayBuffer | ArrayBufferView): Promise<void>;
  67. /**
  68. * Checks if a file exists.
  69. * @param {string} filePath The path to the file to check.
  70. * @returns {Promise<boolean>} A promise that resolves with true if the
  71. * file exists or false if it does not.
  72. * @throws {Error} If the operation fails with a code other than ENOENT.
  73. */
  74. isFile(filePath: string): Promise<boolean>;
  75. /**
  76. * Checks if a directory exists.
  77. * @param {string} dirPath The path to the directory to check.
  78. * @returns {Promise<boolean>} A promise that resolves with true if the
  79. * directory exists or false if it does not.
  80. * @throws {Error} If the operation fails with a code other than ENOENT.
  81. */
  82. isDirectory(dirPath: string): Promise<boolean>;
  83. /**
  84. * Creates a directory recursively.
  85. * @param {string} dirPath The path to the directory to create.
  86. * @returns {Promise<void>} A promise that resolves when the directory is
  87. * created.
  88. */
  89. createDirectory(dirPath: string): Promise<void>;
  90. /**
  91. * Deletes a file or empty directory.
  92. * @param {string} fileOrDirPath The path to the file or directory to
  93. * delete.
  94. * @returns {Promise<void>} A promise that resolves when the file or
  95. * directory is deleted.
  96. * @throws {TypeError} If the file or directory path is not a string.
  97. * @throws {Error} If the file or directory cannot be deleted.
  98. * @throws {Error} If the file or directory is not found.
  99. */
  100. delete(fileOrDirPath: string): Promise<void>;
  101. /**
  102. * Deletes a file or directory recursively.
  103. * @param {string} fileOrDirPath The path to the file or directory to
  104. * delete.
  105. * @returns {Promise<void>} A promise that resolves when the file or
  106. * directory is deleted.
  107. * @throws {TypeError} If the file or directory path is not a string.
  108. * @throws {Error} If the file or directory cannot be deleted.
  109. * @throws {Error} If the file or directory is not found.
  110. */
  111. deleteAll(fileOrDirPath: string): Promise<void>;
  112. /**
  113. * Returns a list of directory entries for the given path.
  114. * @param {string} dirPath The path to the directory to read.
  115. * @returns {AsyncIterable<HfsDirectoryEntry>} A promise that resolves with the
  116. * directory entries.
  117. * @throws {TypeError} If the directory path is not a string.
  118. * @throws {Error} If the directory cannot be read.
  119. */
  120. list(dirPath: string): AsyncIterable<HfsDirectoryEntry>;
  121. /**
  122. * Returns the size of a file.
  123. * @param {string} filePath The path to the file to read.
  124. * @returns {Promise<number|undefined>} A promise that resolves with the size of the
  125. * file in bytes or undefined if the file doesn't exist.
  126. */
  127. size(filePath: string): Promise<number | undefined>;
  128. #private;
  129. }
  130. /**
  131. * A class representing a file system utility library.
  132. * @implements {HfsImpl}
  133. */
  134. export class NodeHfs extends Hfs implements HfsImpl {
  135. /**
  136. * Creates a new instance.
  137. * @param {object} [options] The options for the instance.
  138. * @param {Fsp} [options.fsp] The file system module to use.
  139. */
  140. constructor({ fsp }?: {
  141. fsp?: Fsp;
  142. });
  143. }
  144. export const hfs: NodeHfs;
  145. export type HfsImpl = import("@humanfs/types").HfsImpl;
  146. export type HfsDirectoryEntry = import("@humanfs/types").HfsDirectoryEntry;
  147. export type Fsp = typeof nativeFsp;
  148. export type Dirent = import("fs").Dirent;
  149. import { Hfs } from "@humanfs/core";
  150. import nativeFsp from "node:fs/promises";