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> {
|
||||
|
||||
Reference in new issue
Block a user