Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
Split options into main options and file reader options
Browse files Browse the repository at this point in the history
  • Loading branch information
sekogan committed Apr 5, 2018
1 parent 82f6fd7 commit 95f1f42
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions source/lib/readable_file_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import Multistream = require('multistream');
import Stream = require('readable-stream');
import Fs = require('fs');

const Defaults: Options = {
const DefaultOptions: Options = {
bufferSize: 1*1024*1024,
fileReaderHighWaterMark: 128*1024,
}

export interface Options
{
bufferSize: number;
fileReaderHighWaterMark: number;
}

const DefaultFileReadStreamOptions = {
highWaterMark: 128*1024,
}

export interface FileReadStreamOptions
{
highWaterMark: number;
}

const enum State
Expand All @@ -29,11 +36,11 @@ export class ReadableFileWriter extends Stream.Writable
});
private error: Error|undefined;
private state = State.Created;
private buffer = new BufferedChunks(this.options.bufferSize || Defaults.bufferSize);
private buffer = new BufferedChunks(this.options.bufferSize);

constructor(
public readonly path: string,
private readonly options: Partial<Options> = {}
private readonly options = DefaultOptions
)
{
super();
Expand All @@ -42,9 +49,12 @@ export class ReadableFileWriter extends Stream.Writable
this.fileWriter.on('error', error => this.handleError(error));
}

createReadStream(): NodeJS.ReadableStream
createReadStream(
options = DefaultFileReadStreamOptions
): NodeJS.ReadableStream
{
const reader: Reader = {
options,
bytesAdded: 0,
needMoreData: false,
push: null,
Expand Down Expand Up @@ -163,7 +173,7 @@ export class ReadableFileWriter extends Stream.Writable
encoding: undefined,
start,
end: end - 1, // end is inclusive
highWaterMark: this.options.fileReaderHighWaterMark,
...reader.options
}));
}
}
Expand All @@ -185,6 +195,7 @@ interface MultistreamCallback

interface Reader
{
readonly options: FileReadStreamOptions;
bytesAdded: number;
needMoreData: boolean;
push: MultistreamCallback|null;
Expand Down

0 comments on commit 95f1f42

Please sign in to comment.