From 6d5de0c6f2edfc44e7f25af4c836aed8bf72f5e2 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 19 Jun 2023 18:09:52 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20improve=20read=20stream?= =?UTF-8?q?=20interfaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fsa-to-node/FsaNodeWriteStream copy.ts | 25 +++++++++++++++++ src/index.ts | 4 +-- src/node/types/misc.ts | 6 ++-- src/node/types/options.ts | 18 ++++++++++-- src/volume.ts | 32 +++++----------------- 5 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 src/fsa-to-node/FsaNodeWriteStream copy.ts diff --git a/src/fsa-to-node/FsaNodeWriteStream copy.ts b/src/fsa-to-node/FsaNodeWriteStream copy.ts new file mode 100644 index 000000000..de25dbba4 --- /dev/null +++ b/src/fsa-to-node/FsaNodeWriteStream copy.ts @@ -0,0 +1,25 @@ +import { Readable } from 'stream'; +import type { IReadStream } from '../node/types/misc'; + +export class FsaNodeReadStream extends Readable implements IReadStream { + public constructor() { + super(); + } + + // -------------------------------------------------------------- IReadStream + public get bytesRead(): number { + throw new Error('Method not implemented.'); + } + + public get path(): string | Buffer { + throw new Error('Method not implemented.'); + } + + public get pending(): boolean { + throw new Error('Method not implemented.'); + } + + // ----------------------------------------------------------------- Readable + + _read(size: number) {} +} diff --git a/src/index.ts b/src/index.ts index 5404e7e34..41a167001 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,6 @@ import { StatWatcher, FSWatcher, toUnixTimestamp, - IReadStream, IWriteStream, DirectoryJSON, NestedDirectoryJSON, @@ -13,6 +12,7 @@ import { const { fsSyncMethods, fsAsyncMethods } = require('fs-monkey/lib/util/lists'); import { constants } from './constants'; import type { FsPromisesApi } from './node/types'; +import type * as misc from './node/types/misc'; const { F_OK, R_OK, W_OK, X_OK } = constants; export { DirectoryJSON, NestedDirectoryJSON }; @@ -27,7 +27,7 @@ export interface IFs extends _Volume { Dirent: new (...args) => Dirent; StatWatcher: new () => StatWatcher; FSWatcher: new () => FSWatcher; - ReadStream: new (...args) => IReadStream; + ReadStream: new (...args) => misc.IReadStream; WriteStream: new (...args) => IWriteStream; promises: FsPromisesApi; _toUnixTimestamp; diff --git a/src/node/types/misc.ts b/src/node/types/misc.ts index 4ee3f1e64..231e4a1b2 100644 --- a/src/node/types/misc.ts +++ b/src/node/types/misc.ts @@ -83,11 +83,9 @@ export interface IStatWatcher extends EventEmitter { } export interface IReadStream extends Readable { - new (path: PathLike, options: IReadStreamOptions); - open(); - close(callback: TCallback); bytesRead: number; - path: string; + path: string | Buffer; + pending: boolean; } export interface IWriteStream extends Writable { diff --git a/src/node/types/options.ts b/src/node/types/options.ts index b37a4abb2..5b4edacef 100644 --- a/src/node/types/options.ts +++ b/src/node/types/options.ts @@ -61,13 +61,27 @@ export interface IWatchFileOptions { } export interface IReadStreamOptions { + /** Defaults to `'r'`. */ flags?: TFlags; - encoding?: BufferEncoding; - fd?: number; + /** Defaults to `null`. */ + encoding?: BufferEncoding | null; + /** Defaults to `null`. */ + fd?: number | IFileHandle | null; + /** Defaults to 0o666 */ mode?: TMode; + /** Defaults to `true`. */ autoClose?: boolean; + /** Defaults to `true`. */ + emitClose?: boolean; start?: number; + /** Defaults to `Infinity`. */ end?: number; + /** Defaults to `64 * 1024`. */ + highWaterMark?: number; + /** Defaults to `null`. */ + fs?: object | null; + /** Defaults to `null`. */ + signal?: AbortSignal | null; } export interface IWriteStreamOptions { diff --git a/src/volume.ts b/src/volume.ts index e082e1303..c5f783d30 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -11,6 +11,7 @@ import { constants } from './constants'; import { EventEmitter } from 'events'; import { TEncodingExtended, TDataOut, strToEncoding, ENCODING_UTF8 } from './encoding'; import * as util from 'util'; +import * as misc from './node/types/misc'; import * as opts from './node/types/options'; import { createPromisesApi } from './node/promises'; import { ERRSTR, FLAGS, MODE } from './node/constants'; @@ -51,7 +52,7 @@ import { getWriteSyncArgs, } from './node/util'; import type { PathLike, symlink } from 'fs'; -import { WritevCallback } from './node/types/callback'; +import { FsCallbackApi, WritevCallback } from './node/types/callback'; const resolveCrossPlatform = pathModule.resolve; const { @@ -128,17 +129,6 @@ export interface IWatchFileOptions { interval?: number; } -// Options for `fs.createReadStream` -export interface IReadStreamOptions { - flags?: TFlags; - encoding?: BufferEncoding; - fd?: number; - mode?: TMode; - autoClose?: boolean; - start?: number; - end?: number; -} - // Options for `fs.createWriteStream` export interface IWriteStreamOptions { flags?: TFlags; @@ -271,7 +261,7 @@ function flattenJSON(nestedJSON: NestedDirectoryJSON): DirectoryJSON { /** * `Volume` represents a file system. */ -export class Volume { +export class Volume implements FsCallbackApi { static fromJSON(json: DirectoryJSON, cwd?: string): Volume { const vol = new Volume(); vol.fromJSON(json, cwd); @@ -323,7 +313,7 @@ export class Volume { openFiles = 0; StatWatcher: new () => StatWatcher; - ReadStream: new (...args) => IReadStream; + ReadStream: new (...args) => misc.IReadStream; WriteStream: new (...args) => IWriteStream; FSWatcher: new () => FSWatcher; @@ -354,12 +344,12 @@ export class Volume { } }; - const _ReadStream: new (...args) => IReadStream = FsReadStream as any; + const _ReadStream: new (...args) => misc.IReadStream = FsReadStream as any; this.ReadStream = class extends _ReadStream { constructor(...args) { super(self, ...args); } - } as any as new (...args) => IReadStream; + } as any as new (...args) => misc.IReadStream; const _WriteStream: new (...args) => IWriteStream = FsWriteStream as any; this.WriteStream = class extends _WriteStream { @@ -1900,7 +1890,7 @@ export class Volume { } } - createReadStream(path: PathLike, options?: IReadStreamOptions | string): IReadStream { + createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream { return new this.ReadStream(path, options); } @@ -1998,14 +1988,6 @@ export class StatWatcher extends EventEmitter { /* tslint:disable no-var-keyword prefer-const */ // ---------------------------------------- ReadStream -export interface IReadStream extends Readable { - new (path: PathLike, options: IReadStreamOptions); - open(); - close(callback: TCallback); - bytesRead: number; - path: string; -} - var pool; function allocNewPool(poolSize) {