add provider to readable-stream logic, #41985

This commit is contained in:
Johannes Rieken
2018-04-10 16:59:11 +02:00
parent 36708c0f5d
commit f686a20863
3 changed files with 72 additions and 59 deletions
+16 -18
View File
@@ -18,12 +18,21 @@ export const UTF16be = 'utf16be';
export const UTF16le = 'utf16le';
export interface IDecodeStreamOptions {
minBytesRequiredForDetection: number;
guessEncoding: boolean;
overwriteEncoding(detected: string): string;
guessEncoding?: boolean;
minBytesRequiredForDetection?: number;
overwriteEncoding?(detected: string): string;
}
export function toDecodeStream(readable: Readable, opts: IDecodeStreamOptions): TPromise<{ detected: IDetectedEncodingResult, stream: NodeJS.ReadableStream }> {
export function toDecodeStream(readable: Readable, options: IDecodeStreamOptions): TPromise<{ detected: IDetectedEncodingResult, stream: NodeJS.ReadableStream }> {
if (!options.minBytesRequiredForDetection) {
options.minBytesRequiredForDetection = options.guessEncoding ? AUTO_GUESS_BUFFER_MAX_LEN : NO_GUESS_BUFFER_MAX_LEN;
}
if (!options.overwriteEncoding) {
options.overwriteEncoding = detected => detected || UTF8;
}
return new TPromise<{ detected: IDetectedEncodingResult, stream: NodeJS.ReadableStream }>((resolve, reject) => {
readable.pipe(new class extends Writable {
@@ -55,7 +64,7 @@ export function toDecodeStream(readable: Readable, opts: IDecodeStreamOptions):
// waiting for the decoder to be ready
this._decodeStreamConstruction.then(_ => callback(), err => callback(err));
} else if (this._bytesBuffered >= opts.minBytesRequiredForDetection) {
} else if (this._bytesBuffered >= options.minBytesRequiredForDetection) {
// buffered enough data, create stream and forward data
this._startDecodeStream(callback);
@@ -69,8 +78,8 @@ export function toDecodeStream(readable: Readable, opts: IDecodeStreamOptions):
this._decodeStreamConstruction = TPromise.as(detectEncodingFromBuffer({
buffer: Buffer.concat(this._buffer), bytesRead: this._bytesBuffered
}, opts.guessEncoding)).then(detected => {
detected.encoding = opts.overwriteEncoding(detected.encoding); // default encoding
}, options.guessEncoding)).then(detected => {
detected.encoding = options.overwriteEncoding(detected.encoding); // default encoding
this._decodeStream = decodeStream(detected.encoding);
for (const buffer of this._buffer) {
this._decodeStream.write(buffer);
@@ -256,17 +265,6 @@ const ZERO_BYTE_DETECTION_BUFFER_MAX_LEN = 512; // number of bytes to look at to
const NO_GUESS_BUFFER_MAX_LEN = 512; // when not auto guessing the encoding, small number of bytes are enough
const AUTO_GUESS_BUFFER_MAX_LEN = 512 * 8; // with auto guessing we want a lot more content to be read for guessing
export function maxEncodingDetectionBufferLen(arg1?: DetectEncodingOption | boolean): number {
let autoGuessEncoding: boolean;
if (typeof arg1 === 'boolean') {
autoGuessEncoding = arg1;
} else {
autoGuessEncoding = arg1 && arg1.autoGuessEncoding;
}
return autoGuessEncoding ? AUTO_GUESS_BUFFER_MAX_LEN : NO_GUESS_BUFFER_MAX_LEN;
}
export interface IDetectedEncodingResult {
encoding: string;
seemsBinary: boolean;