Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
@@ -0,0 +1,69 @@
|
||||
export async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]> {
|
||||
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<Uint8Array>;
|
||||
private interReadBuffer = new Uint8Array();
|
||||
private isSourceEmpty = false;
|
||||
constructor(source: AsyncIterable<Uint8Array>) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
private async readMinimumOf(n: number): Promise<Uint8Array> {
|
||||
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<number | null> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user