109
110
write(buffer: Uint8Array): string {
111
>
return this.iconvLiteDecoder.write(buffer);
encoding.ts
112
>
}
113
114
end(): string | undefined {
116
>
}
117
}
118
119
export function toDecodeStream(source: VSBufferReadableStream, options: IDecodeStreamOptions): Promise<IDecodeStreamResult> {
120
>
const minBytesRequiredForDetection = options.minBytesRequiredForDetection ?? (options.guessEncoding ? AUTO_ENCODING_GUESS_MIN_BYTES : NO_ENCODING_GUESS_MIN_BYTES);
encoding.ts
121
>
122
>
return new Promise<IDecodeStreamResult>((resolve, reject) => {
123
>
const target = newWriteableStream<string>(strings => strings.join(''));
124
>
125
>
const bufferedChunks: VSBuffer[] = [];
126
>
let bytesBuffered = 0;
127
>
128
>
let decoder: IDecoderStream | undefined = undefined;
129
>
130
>
const cts = new CancellationTokenSource();
131
>
132
>
const createDecoder = async () => {
133
>
try {
134
>
135
>
// detect encoding from buffer
136
>
const detected = await detectEncodingFromBuffer({
137
>
buffer: VSBuffer.concat(bufferedChunks),
138
>
bytesRead: bytesBuffered
139
>
}, options.guessEncoding, options.candidateGuessEncodings);
140
>
141
>
// throw early if the source seems binary and
142
>
// we are instructed to only accept text
143
>
if (detected.seemsBinary && options.acceptTextOnly) {
144
throw new DecodeStreamError('Stream is binary but only text is accepted for decoding', DecodeStreamErrorKind.STREAM_IS_BINARY);
145
}
147
>
// ensure to respect overwrite of encoding
148
>
detected.encoding = await options.overwriteEncoding(detected.encoding);
149
>
150
>
// decode and write buffered content
151
>
decoder = await DecoderStream.create(detected.encoding);
152
>
const decoded = decoder.write(VSBuffer.concat(bufferedChunks).buffer);
153
>
target.write(decoded);
154
>
155
>
bufferedChunks.length = 0;
156
>
bytesBuffered = 0;
157
>
158
>
// signal to the outside our detected encoding and final decoder stream
159
>
resolve({
160
>
stream: target,
161
>
detected
162
>
});
163
>
} catch (error) {
164
165
// Stop handling anything from the source and target