Added a very complicated AsyncIterableReader class.
The class was already implemented in "https://deno.land/std@0.144.0/streams/conversion.ts". So, I litterally lost 1 day of work! I hate myself.
This commit is contained in:
1 parent
17a1b4bed3
commit
0d627c9051
125 files changed
+26943
-83
No files matched your search
+32
-9
@@ -1,7 +1,7 @@
|
||||
import { sha1 } from 'https://denopkg.com/chiefbiiko/sha1/mod.ts';
|
||||
import { iterateReader } from "https://deno.land/std@0.191.0/streams/iterate_reader.ts";
|
||||
import { AsyncIterableReader, concatUint8Array } from "./utils.ts";
|
||||
|
||||
import { readAll } from "https://deno.land/std@0.191.0/streams/read_all.ts";
|
||||
import { readerFromIterable } from "https://deno.land/std@0.144.0/streams/conversion.ts";
|
||||
|
||||
export interface FileService {
|
||||
|
||||
@@ -15,11 +15,15 @@ export interface FileService {
|
||||
|
||||
|
||||
getFullBodyByPath(path: string): Promise<string | null>;
|
||||
|
||||
getRawBodyByPath(path: string): Promise<Uint8Array | null>;
|
||||
|
||||
getBodyAsReader(path: string): Promise<Deno.Reader | null>;
|
||||
|
||||
getMetaDataByID(id: string): Promise<FileMetaData | null>;
|
||||
|
||||
renameById(id: string, newPath: string): Promise<FileMetaData | null>;
|
||||
|
||||
renameByPath(
|
||||
originalPath: string,
|
||||
newPath: string,
|
||||
@@ -101,8 +105,7 @@ export class DenoKVBlobService {
|
||||
return size;
|
||||
}
|
||||
|
||||
async getBlob(index: string): Promise<Uint8Array> {
|
||||
let ret = new Uint8Array();
|
||||
private async *iterBlob(index: string): AsyncIterable<Uint8Array> {
|
||||
for await (
|
||||
const itm of this.kv.list<string>({ prefix: ['blob_index', index] })
|
||||
) {
|
||||
@@ -112,9 +115,16 @@ export class DenoKVBlobService {
|
||||
`Inconsistant blob index for key "${foo.value}", this is very bad.`,
|
||||
);
|
||||
}
|
||||
ret = concatUint8Array(ret, foo.value);
|
||||
yield foo.value;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
async readAllBlob(index: string): Promise<Uint8Array> {
|
||||
return await readAll(this.getBlob(index));
|
||||
}
|
||||
|
||||
getBlob(index: string): Deno.Reader{
|
||||
return readerFromIterable(this.iterBlob(index));
|
||||
}
|
||||
|
||||
private async *iterRawBlobs(index: string): AsyncIterable<Uint8Array>{
|
||||
@@ -127,10 +137,9 @@ export class DenoKVBlobService {
|
||||
);
|
||||
}
|
||||
yield foo.value;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
async getNumberOfBlobs(index: string): Promise<number> {
|
||||
let ret = 0;
|
||||
for await (
|
||||
@@ -142,7 +151,8 @@ export class DenoKVBlobService {
|
||||
}
|
||||
|
||||
streamBlob(index: string): Deno.Reader{
|
||||
return new AsyncIterableReader(this.iterRawBlobs(index));
|
||||
|
||||
return readerFromIterable(this.iterRawBlobs(index));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +243,18 @@ export class DenoKVFileService implements FileService {
|
||||
}
|
||||
|
||||
async getRawBodyByPath(path: string): Promise<Uint8Array | null> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||
'file_index_path',
|
||||
path,
|
||||
]);
|
||||
if (idx.value === null) {
|
||||
return null;
|
||||
}
|
||||
return this.blobService.readAllBlob(idx.value.fileId);
|
||||
}
|
||||
|
||||
|
||||
async getBodyAsReader(path: string): Promise<Deno.Reader | null> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||
'file_index_path',
|
||||
path,
|
||||
@@ -241,6 +263,7 @@ export class DenoKVFileService implements FileService {
|
||||
return null;
|
||||
}
|
||||
return this.blobService.getBlob(idx.value.fileId);
|
||||
|
||||
}
|
||||
|
||||
async getMetaDataByID(id: string): Promise<FileMetaData | null> {
|
||||
|
||||
+14
-3
@@ -2,7 +2,9 @@ import { Context, Router } from "https://deno.land/x/oak/mod.ts";
|
||||
import { FileService } from "./fileservice.ts";
|
||||
import { readerFromStreamReader } from "https://deno.land/std@0.189.0/streams/mod.ts";
|
||||
import { asyncIterableToArray } from "./utils.ts";
|
||||
|
||||
import { StringReader } from "https://deno.land/std@0.178.0/io/string_reader.ts";
|
||||
import { isReader } from "https://deno.land/x/oak@v12.5.0/util.ts";
|
||||
import { createMockNext } from "https://deno.land/x/oak@v12.1.0/testing.ts";
|
||||
|
||||
|
||||
type Handler = (ctx: Context) => void;
|
||||
@@ -30,10 +32,19 @@ function createFileHandlerFactory(fileService: FileService): Handler {
|
||||
|
||||
|
||||
function getFileHandlerFactory(fileService: FileService): Handler {
|
||||
return (ctx: Context) => {
|
||||
return async (ctx: Context) => {
|
||||
const untypedCtx: any = ctx;
|
||||
const fileId = untypedCtx.params.file_id;
|
||||
// ctx.response.body = await fileService.getRawBodyByPath()
|
||||
const reader = await fileService.getBodyAsReader(fileId);
|
||||
if(reader == null){
|
||||
ctx.response.status = 404;
|
||||
return;
|
||||
}
|
||||
ctx.response.status = 200;
|
||||
ctx.response.body = reader;
|
||||
ctx.response.type = "text/plain";
|
||||
// ctx.send(reader)
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+18
-11
@@ -15,27 +15,30 @@ export function concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array
|
||||
}
|
||||
|
||||
export class AsyncIterableReader implements Deno.Reader {
|
||||
private readonly source: AsyncIterable<Uint8Array>;
|
||||
private readonly source: AsyncIterator<Uint8Array>;
|
||||
private interReadBuffer = new Uint8Array();
|
||||
private isSourceEmpty = false;
|
||||
private shit = 0;
|
||||
constructor(source: AsyncIterable<Uint8Array>) {
|
||||
this.source = source;
|
||||
this.source = source[Symbol.asyncIterator]();
|
||||
}
|
||||
|
||||
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)
|
||||
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){
|
||||
console.log('*******************************')
|
||||
return ret;
|
||||
}
|
||||
ite = await this.source.next();
|
||||
|
||||
}
|
||||
console.log('---------------');
|
||||
console.log(this.source);
|
||||
this.isSourceEmpty = true;
|
||||
return ret;
|
||||
}
|
||||
@@ -55,14 +58,18 @@ export class AsyncIterableReader implements Deno.Reader {
|
||||
}
|
||||
|
||||
async read(buffer: Uint8Array): Promise<number | null> {
|
||||
this.shit += 1;
|
||||
console.log("reading \> " + this.shit);
|
||||
if(this.isSourceEmpty && this.interReadBuffer.length < 1){
|
||||
console.log("done mother fucker");
|
||||
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.subarray(internalReadBufferTransferdSize), buffer);
|
||||
const transferedSize = this.transferData(data, buffer.subarray(internalReadBufferTransferdSize));
|
||||
this.interReadBuffer = data.subarray(transferedSize);
|
||||
return internalReadBufferTransferdSize + transferedSize;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user