Removed AsyncIterableReader which can be replaced by readerFromIterable available in the std

This commit is contained in:
didier amyot committed 2023-06-13 07:43:07 -04:00
1 parent 789fb49e8b
commit f7a8d494f5
3 files changed
-111

No files matched your search

-66
View File
@@ -5,69 +5,3 @@ export async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[
}
return ret;
}
export function concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array {
const mergedArray = new Uint8Array(arr1.length + arr2.length);
mergedArray.set(arr1);
mergedArray.set(arr2, arr1.length);
return mergedArray;
}
export class AsyncIterableReader implements Deno.Reader {
private readonly source: AsyncIterator<Uint8Array>;
private interReadBuffer = new Uint8Array();
private isSourceEmpty = false;
constructor(source: AsyncIterable<Uint8Array>) {
this.source = source[Symbol.asyncIterator]();
}
private async readMinimumOf(n: number): Promise<Uint8Array> {
let ret = new Uint8Array();
if(this.isSourceEmpty){
return ret;
}
let ite = await this.source.next();
while(!ite.done){
const piece = ite.value;
ret = concatUint8Array(ret, piece);
if(ret.length >= n){
return ret;
}
ite = await this.source.next();
}
this.isSourceEmpty = true;
return ret;
}
private transferData(source: Uint8Array, dest: Uint8Array): number {
const limit = Math.min(source.length, dest.length);
for(let i = 0; i < limit; i ++){
dest[i] = source[i];
}
return limit;
}
private readFromInternalReadBuffer(buffer: Uint8Array): number {
const n = this.transferData(this.interReadBuffer, buffer);
this.interReadBuffer = this.interReadBuffer.subarray(n);
return n;
}
async read(buffer: Uint8Array): Promise<number | null> {
if(this.isSourceEmpty && this.interReadBuffer.length < 1){
return null;
}
const internalReadBufferTransferdSize = this.readFromInternalReadBuffer(buffer);
if(internalReadBufferTransferdSize == buffer.length){
return internalReadBufferTransferdSize;
}
const data = await this.readMinimumOf(buffer.length - internalReadBufferTransferdSize);
const transferedSize = this.transferData(data, buffer.subarray(internalReadBufferTransferdSize));
this.interReadBuffer = data.subarray(transferedSize);
return internalReadBufferTransferdSize + transferedSize;
}
}