export async function asyncIterableToArray(ite: AsyncIterable): Promise { const ret: T[] = []; for await (const e of ite) { ret.push(e); } 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: AsyncIterable; private interReadBuffer = new Uint8Array(); private isSourceEmpty = false; constructor(source: AsyncIterable) { this.source = source; } private async readMinimumOf(n: number): Promise { let ret = new Uint8Array(); console.log('))) ' + n ) for await(const piece of this.source){ console.log(piece.length) ret = concatUint8Array(ret, piece); if(ret.length >= n){ console.log('*******************************') return ret; } } console.log('---------------'); console.log(this.source); 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 { if(this.isSourceEmpty && this.interReadBuffer.length < 1){ console.log("done mother fucker"); return null; } const internalReadBufferTransferdSize = this.readFromInternalReadBuffer(buffer); const data = await this.readMinimumOf(buffer.length - internalReadBufferTransferdSize); const transferedSize = this.transferData(data.subarray(internalReadBufferTransferdSize), buffer); this.interReadBuffer = data.subarray(transferedSize); return internalReadBufferTransferdSize + transferedSize; } }