Skip to content

Commit

Permalink
feat: 🎸 improve read stream interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 19, 2023
1 parent c3a4188 commit 6d5de0c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
25 changes: 25 additions & 0 deletions src/fsa-to-node/FsaNodeWriteStream copy.ts
Original file line number Diff line number Diff line change
@@ -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) {}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
StatWatcher,
FSWatcher,
toUnixTimestamp,
IReadStream,
IWriteStream,
DirectoryJSON,
NestedDirectoryJSON,
} from './volume';
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 };
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/node/types/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ export interface IStatWatcher extends EventEmitter {
}

export interface IReadStream extends Readable {
new (path: PathLike, options: IReadStreamOptions);
open();
close(callback: TCallback<void>);
bytesRead: number;
path: string;
path: string | Buffer;
pending: boolean;
}

export interface IWriteStream extends Writable {
Expand Down
18 changes: 16 additions & 2 deletions src/node/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 7 additions & 25 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<void>);
bytesRead: number;
path: string;
}

var pool;

function allocNewPool(poolSize) {
Expand Down

0 comments on commit 6d5de0c

Please sign in to comment.