Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
+61
-18
@@ -1,22 +1,31 @@
|
||||
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";
|
||||
|
||||
|
||||
export interface FileService {
|
||||
|
||||
list(): AsyncIterable<FileMetaData>;
|
||||
|
||||
createFromFullBody(
|
||||
path: string,
|
||||
body: string | Uint8Array,
|
||||
body: string | Uint8Array | Deno.Reader,
|
||||
): Promise<FileMetaData>;
|
||||
list(): AsyncIterable<FileMetaData>;
|
||||
deleteByPath(path: string): Promise<void>;
|
||||
createFromReader(path: string, body: Deno.Reader): Promise<FileMetaData>;
|
||||
|
||||
|
||||
getFullBodyByPath(path: string): Promise<string | null>;
|
||||
getRawBodyByPath(path: string): Promise<Uint8Array | null>;
|
||||
|
||||
getMetaDataByID(id: string): Promise<FileMetaData | null>;
|
||||
|
||||
renameById(id: string, newPath: string): Promise<FileMetaData | null>;
|
||||
renameByPath(
|
||||
originalPath: string,
|
||||
newPath: string,
|
||||
): Promise<FileMetaData | null>;
|
||||
|
||||
deleteByPath(path: string): Promise<void>;
|
||||
}
|
||||
|
||||
interface SerializedFile {
|
||||
@@ -70,28 +79,26 @@ export class DenoKVBlobService {
|
||||
|
||||
}
|
||||
|
||||
async persistBlob(index: string, body: Uint8Array): Promise<void> {
|
||||
async persistBlob(index: string, body: Uint8Array): Promise<number> {
|
||||
let blobCount = 0;
|
||||
let size = 0;
|
||||
for (const txt of this.splitText(body)) {
|
||||
await this.unsafePersistBlob(index, blobCount, txt);
|
||||
size += txt.length;
|
||||
blobCount += 1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
async persistReader(index: string, body: Deno.Reader): Promise<void>{
|
||||
async persistReader(index: string, body: Deno.Reader): Promise<number>{
|
||||
let i = 0;
|
||||
let size = 0;
|
||||
for await (const chunk of iterateReader(body, {bufSize: this.blobSize})){
|
||||
this.unsafePersistBlob(index, i, chunk);
|
||||
size += chunk.length;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// todo: remove
|
||||
private concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array {
|
||||
const mergedArray = new Uint8Array(arr1.length + arr2.length);
|
||||
mergedArray.set(arr1);
|
||||
mergedArray.set(arr2, arr1.length);
|
||||
return mergedArray;
|
||||
return size;
|
||||
}
|
||||
|
||||
async getBlob(index: string): Promise<Uint8Array> {
|
||||
@@ -105,11 +112,25 @@ export class DenoKVBlobService {
|
||||
`Inconsistant blob index for key "${foo.value}", this is very bad.`,
|
||||
);
|
||||
}
|
||||
ret = this.concatUint8Array(ret, foo.value);
|
||||
ret = concatUint8Array(ret, foo.value);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private async *iterRawBlobs(index: string): AsyncIterable<Uint8Array>{
|
||||
const prefix = ['blob_index', index];
|
||||
for await (const itm of this.kv.list<string>({ prefix })) {
|
||||
const foo = await this.kv.get<Uint8Array>(['blob', itm.value]);
|
||||
if (foo.value === null) {
|
||||
throw new Error(
|
||||
`Inconsistant blob index for key "${foo.value}", this is very bad.`,
|
||||
);
|
||||
}
|
||||
yield foo.value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
async getNumberOfBlobs(index: string): Promise<number> {
|
||||
let ret = 0;
|
||||
for await (
|
||||
@@ -119,6 +140,10 @@ export class DenoKVBlobService {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
streamBlob(index: string): Deno.Reader{
|
||||
return new AsyncIterableReader(this.iterRawBlobs(index));
|
||||
}
|
||||
}
|
||||
|
||||
export class DenoKVFileService implements FileService {
|
||||
@@ -135,17 +160,35 @@ export class DenoKVFileService implements FileService {
|
||||
body: string | Uint8Array,
|
||||
): Promise<FileMetaData> {
|
||||
const newId = crypto.randomUUID();
|
||||
const bodyBytes: Uint8Array = body instanceof Uint8Array ? body : new TextEncoder().encode(body);
|
||||
const size = await this.blobService.persistBlob(newId, bodyBytes);
|
||||
|
||||
const serializedFile: SerializedFile = {
|
||||
id: newId,
|
||||
path,
|
||||
bodySize: body.length,
|
||||
bodySize: size,
|
||||
};
|
||||
const idx: SerializedFilePathIndex = { fileId: newId };
|
||||
await this.kv.set(['file', newId], serializedFile);
|
||||
await this.kv.set(['file_index_path', path], idx);
|
||||
const bodyBytes: Uint8Array = body instanceof Uint8Array ? body : new TextEncoder().encode(body);
|
||||
await this.blobService.persistBlob(newId, bodyBytes);
|
||||
return { id: newId, path, bodySize: body.length };
|
||||
|
||||
return { id: newId, path, bodySize: size };
|
||||
}
|
||||
|
||||
async createFromReader(path: string, body: Deno.Reader): Promise<FileMetaData>{
|
||||
const newId = crypto.randomUUID();
|
||||
const size = await this.blobService.persistReader(newId, body);
|
||||
const serializedFile: SerializedFile = {
|
||||
id: newId,
|
||||
path,
|
||||
bodySize: size,
|
||||
};
|
||||
const idx: SerializedFilePathIndex = { fileId: newId };
|
||||
await this.kv.set(['file', newId], serializedFile);
|
||||
await this.kv.set(['file_index_path', path], idx);
|
||||
|
||||
return { id: newId, path, bodySize: size };
|
||||
|
||||
}
|
||||
|
||||
async *list(): AsyncIterable<FileMetaData> {
|
||||
|
||||
Reference in new issue
Block a user