All files / src/node FileHandle.ts

96.29% Statements 26/27
100% Branches 0/0
95.45% Functions 21/22
95.65% Lines 22/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 11745x         45x           1052x 1052x       2x       2x       2x       1035x       2x       1x   1x 1x 1x           2x       2x       3x       2x               9x       2x                 1009x                   2x       2x                                              
import { promisify } from './util';
import type * as opts from './types/options';
import type { IFileHandle, IStats, TData, TDataOut, TMode, TTime } from './types/misc';
import type { FsCallbackApi } from './types';
 
export class FileHandle implements IFileHandle {
  private fs: FsCallbackApi;
 
  fd: number;
 
  constructor(fs: FsCallbackApi, fd: number) {
    this.fs = fs;
    this.fd = fd;
  }
 
  appendFile(data: TData, options?: opts.IAppendFileOptions | string): Promise<void> {
    return promisify(this.fs, 'appendFile')(this.fd, data, options);
  }
 
  chmod(mode: TMode): Promise<void> {
    return promisify(this.fs, 'fchmod')(this.fd, mode);
  }
 
  chown(uid: number, gid: number): Promise<void> {
    return promisify(this.fs, 'fchown')(this.fd, uid, gid);
  }
 
  close(): Promise<void> {
    return promisify(this.fs, 'close')(this.fd);
  }
 
  datasync(): Promise<void> {
    return promisify(this.fs, 'fdatasync')(this.fd);
  }
 
  readableWebStream(options?: opts.IReadableWebStreamOptions): ReadableStream {
    return new ReadableStream({
      pull: async controller => {
        const data = await this.readFile();
        controller.enqueue(data);
        controller.close();
      },
    });
  }
 
  read(buffer: Buffer | Uint8Array, offset: number, length: number, position: number): Promise<TFileHandleReadResult> {
    return promisify(this.fs, 'read', bytesRead => ({ bytesRead, buffer }))(this.fd, buffer, offset, length, position);
  }
 
  readv(buffers: ArrayBufferView[], position?: number | null | undefined): Promise<TFileHandleReadvResult> {
    return promisify(this.fs, 'readv', bytesRead => ({ bytesRead, buffers }))(this.fd, buffers, position);
  }
 
  readFile(options?: opts.IReadFileOptions | string): Promise<TDataOut> {
    return promisify(this.fs, 'readFile')(this.fd, options);
  }
 
  stat(options?: opts.IFStatOptions): Promise<IStats> {
    return promisify(this.fs, 'fstat')(this.fd, options);
  }
 
  sync(): Promise<void> {
    return promisify(this.fs, 'fsync')(this.fd);
  }
 
  truncate(len?: number): Promise<void> {
    return promisify(this.fs, 'ftruncate')(this.fd, len);
  }
 
  utimes(atime: TTime, mtime: TTime): Promise<void> {
    return promisify(this.fs, 'futimes')(this.fd, atime, mtime);
  }
 
  write(
    buffer: Buffer | Uint8Array,
    offset?: number,
    length?: number,
    position?: number,
  ): Promise<TFileHandleWriteResult> {
    return promisify(this.fs, 'write', bytesWritten => ({ bytesWritten, buffer }))(
      this.fd,
      buffer,
      offset,
      length,
      position,
    );
  }
 
  writev(buffers: ArrayBufferView[], position?: number | null | undefined): Promise<TFileHandleWritevResult> {
    return promisify(this.fs, 'writev', bytesWritten => ({ bytesWritten, buffers }))(this.fd, buffers, position);
  }
 
  writeFile(data: TData, options?: opts.IWriteFileOptions): Promise<void> {
    return promisify(this.fs, 'writeFile')(this.fd, data, options);
  }
}
 
export interface TFileHandleReadResult {
  bytesRead: number;
  buffer: Buffer | Uint8Array;
}
 
export interface TFileHandleWriteResult {
  bytesWritten: number;
  buffer: Buffer | Uint8Array;
}
 
export interface TFileHandleReadvResult {
  bytesRead: number;
  buffers: ArrayBufferView[];
}
 
export interface TFileHandleWritevResult {
  bytesWritten: number;
  buffers: ArrayBufferView[];
}