FileHandle.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. /// <reference types="node" />
  5. /// <reference types="node" />
  6. import type { BigIntStats, ReadStream, StatOptions, Stats, WriteStream, WriteVResult } from 'fs';
  7. import type { CreateReadStreamOptions, CreateWriteStreamOptions, FakeFS } from '../FakeFS';
  8. import type { Path } from '../path';
  9. interface ObjectEncodingOptions {
  10. encoding?: BufferEncoding | null | undefined;
  11. }
  12. interface FlagAndOpenMode {
  13. mode?: Mode | undefined;
  14. flag?: OpenMode | undefined;
  15. }
  16. type OpenMode = number | string;
  17. type Mode = number | string;
  18. interface FileReadResult<T extends ArrayBufferView> {
  19. bytesRead: number;
  20. buffer: T;
  21. }
  22. interface FileReadOptions<T extends ArrayBufferView = Buffer> {
  23. buffer?: T;
  24. offset?: number | null;
  25. length?: number | null;
  26. position?: number | null;
  27. }
  28. interface ReadVResult {
  29. bytesRead: number;
  30. buffers: Array<NodeJS.ArrayBufferView>;
  31. }
  32. interface AbortSignal {
  33. readonly aborted: boolean;
  34. }
  35. interface Abortable {
  36. signal?: AbortSignal | undefined;
  37. }
  38. type WriteArgsBuffer<TBuffer extends Uint8Array> = [
  39. buffer: TBuffer,
  40. offset?: number | null,
  41. length?: number | null,
  42. position?: number | null
  43. ];
  44. type WriteArgsString = [
  45. data: string,
  46. position?: number | null,
  47. encoding?: BufferEncoding | null
  48. ];
  49. declare const kBaseFs: unique symbol;
  50. declare const kFd: unique symbol;
  51. declare const kClosePromise: unique symbol;
  52. declare const kCloseResolve: unique symbol;
  53. declare const kCloseReject: unique symbol;
  54. declare const kRefs: unique symbol;
  55. declare const kRef: unique symbol;
  56. declare const kUnref: unique symbol;
  57. export declare class FileHandle<P extends Path> {
  58. [kBaseFs]: FakeFS<P>;
  59. [kFd]: number;
  60. [kRefs]: number;
  61. [kClosePromise]: Promise<void> | undefined;
  62. [kCloseResolve]: (() => void) | undefined;
  63. [kCloseReject]: (() => void) | undefined;
  64. constructor(fd: number, baseFs: FakeFS<P>);
  65. get fd(): number;
  66. appendFile(data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding | null): Promise<void>;
  67. chown(uid: number, gid: number): Promise<void>;
  68. chmod(mode: number): Promise<void>;
  69. createReadStream(options?: CreateReadStreamOptions): ReadStream;
  70. createWriteStream(options?: CreateWriteStreamOptions): WriteStream;
  71. datasync(): Promise<void>;
  72. sync(): Promise<void>;
  73. read(options?: FileReadOptions<Buffer>): Promise<FileReadResult<Buffer>>;
  74. read(buffer: Buffer, offset?: number | null, length?: number | null, position?: number | null): Promise<FileReadResult<Buffer>>;
  75. readFile(options?: {
  76. encoding?: null | undefined;
  77. flag?: OpenMode | undefined;
  78. } | null): Promise<Buffer>;
  79. readFile(options: {
  80. encoding: BufferEncoding;
  81. flag?: OpenMode | undefined;
  82. } | BufferEncoding): Promise<string>;
  83. readLines(options?: CreateReadStreamOptions): import("readline").Interface;
  84. stat(opts?: StatOptions & {
  85. bigint?: false | undefined;
  86. }): Promise<Stats>;
  87. stat(opts: StatOptions & {
  88. bigint: true;
  89. }): Promise<BigIntStats>;
  90. truncate(len?: number): Promise<void>;
  91. utimes(atime: string | number | Date, mtime: string | number | Date): Promise<void>;
  92. writeFile(data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode & Abortable) | BufferEncoding | null): Promise<void>;
  93. write(...args: WriteArgsString): Promise<{
  94. bytesWritten: number;
  95. buffer: string;
  96. }>;
  97. write<TBuffer extends Uint8Array>(...args: WriteArgsBuffer<TBuffer>): Promise<{
  98. bytesWritten: number;
  99. buffer: TBuffer;
  100. }>;
  101. writev(buffers: Array<NodeJS.ArrayBufferView>, position?: number): Promise<WriteVResult>;
  102. readv(buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<ReadVResult>;
  103. close(): Promise<void>;
  104. [kRef](caller: Function): void;
  105. [kUnref](): void;
  106. }
  107. export {};