Lint code.
Added a deno.json and set linting rules. Obviously they are the perfect rules and no one should argue with them in any fashion ever.
This commit is contained in:
1 parent
ce7b6ae523
commit
8ce2dadc47
4 files changed
+225
-165
No files matched your search
+115
-67
@@ -1,58 +1,67 @@
|
||||
import { sha1 } from "https://denopkg.com/chiefbiiko/sha1/mod.ts";
|
||||
|
||||
import { sha1 } from 'https://denopkg.com/chiefbiiko/sha1/mod.ts';
|
||||
|
||||
export interface FileService {
|
||||
createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData>;
|
||||
list(): AsyncIterable<FileMetaData>;
|
||||
deleteByPath(path: string): Promise<void>;
|
||||
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>;
|
||||
createFromFullBody(
|
||||
path: string,
|
||||
body: string | Uint8Array,
|
||||
): Promise<FileMetaData>;
|
||||
list(): AsyncIterable<FileMetaData>;
|
||||
deleteByPath(path: string): Promise<void>;
|
||||
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>;
|
||||
}
|
||||
|
||||
interface SerializedFile {
|
||||
id: string,
|
||||
path: string,
|
||||
bodySize: number,
|
||||
id: string;
|
||||
path: string;
|
||||
bodySize: number;
|
||||
}
|
||||
|
||||
export interface FileMetaData {
|
||||
path: string,
|
||||
id: string,
|
||||
bodySize: number,
|
||||
path: string;
|
||||
id: string;
|
||||
bodySize: number;
|
||||
}
|
||||
|
||||
interface SerializedFilePathIndex {
|
||||
fileId: string;
|
||||
}
|
||||
|
||||
|
||||
export class DenoKVBlobService {
|
||||
private readonly kv: Deno.Kv;
|
||||
private readonly blobSize: number;
|
||||
|
||||
constructor(kv: Deno.Kv, blobSize: number){
|
||||
constructor(kv: Deno.Kv, blobSize: number) {
|
||||
this.kv = kv;
|
||||
this.blobSize = blobSize;
|
||||
}
|
||||
|
||||
private *splitText(body: Uint8Array): Generator<Uint8Array>{
|
||||
private *splitText(body: Uint8Array): Generator<Uint8Array> {
|
||||
let blobCount = 0;
|
||||
let txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
while(txt.length > 0 ){
|
||||
let txt = body.slice(
|
||||
blobCount * this.blobSize,
|
||||
(blobCount + 1) * this.blobSize,
|
||||
);
|
||||
while (txt.length > 0) {
|
||||
yield txt;
|
||||
blobCount += 1;
|
||||
txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
txt = body.slice(
|
||||
blobCount * this.blobSize,
|
||||
(blobCount + 1) * this.blobSize,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async persistBlob(index: string, body: Uint8Array): Promise<void>{
|
||||
async persistBlob(index: string, body: Uint8Array): Promise<void> {
|
||||
let blobCount = 0;
|
||||
for(const txt of this.splitText(body)){
|
||||
const blob_id = sha1(txt, "utf8", "hex");
|
||||
for (const txt of this.splitText(body)) {
|
||||
const blob_id = sha1(txt, 'utf8', 'hex');
|
||||
await this.kv.set(['blob_index', index, blobCount], blob_id);
|
||||
await this.kv.set(['blob', blob_id], txt);
|
||||
blobCount += 1;
|
||||
@@ -67,88 +76,115 @@ export class DenoKVBlobService {
|
||||
return mergedArray;
|
||||
}
|
||||
|
||||
async getBlob(index: string): Promise<Uint8Array>{
|
||||
async getBlob(index: string): Promise<Uint8Array> {
|
||||
let ret = new Uint8Array();
|
||||
for await(const itm of this.kv.list<string>({prefix: ['blob_index', index]})){
|
||||
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.`);
|
||||
for await (
|
||||
const itm of this.kv.list<string>({ prefix: ['blob_index', index] })
|
||||
) {
|
||||
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.`,
|
||||
);
|
||||
}
|
||||
ret = this.concatUint8Array(ret, foo.value);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
async getNumberOfBlobs(index: string): Promise<number>{
|
||||
async getNumberOfBlobs(index: string): Promise<number> {
|
||||
let ret = 0;
|
||||
for await(const _itm of this.kv.list<string>({prefix: ['blob_index', index]})){
|
||||
for await (
|
||||
const _itm of this.kv.list<string>({ prefix: ['blob_index', index] })
|
||||
) {
|
||||
ret += 1;
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export class DenoKVFileService implements FileService {
|
||||
private readonly kv: Deno.Kv;
|
||||
private readonly blobService: DenoKVBlobService;
|
||||
|
||||
constructor(kv: Deno.Kv){
|
||||
constructor(kv: Deno.Kv) {
|
||||
this.kv = kv;
|
||||
this.blobService = new DenoKVBlobService(kv, 500);
|
||||
}
|
||||
|
||||
async createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData> {
|
||||
async createFromFullBody(
|
||||
path: string,
|
||||
body: string | Uint8Array,
|
||||
): Promise<FileMetaData> {
|
||||
const newId = crypto.randomUUID();
|
||||
const serializedFile: SerializedFile = {id: newId, path, bodySize: body.length};
|
||||
const idx: SerializedFilePathIndex = {fileId: newId};
|
||||
const serializedFile: SerializedFile = {
|
||||
id: newId,
|
||||
path,
|
||||
bodySize: body.length,
|
||||
};
|
||||
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);
|
||||
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: body.length };
|
||||
}
|
||||
|
||||
async* list(): AsyncIterable<FileMetaData> {
|
||||
for await (const entry of this.kv.list<SerializedFilePathIndex>({ prefix: ["file_index_path"] })) {
|
||||
const ret = await this.kv.get<SerializedFile>(['file', entry.value.fileId]);
|
||||
if(ret.value !== null) {
|
||||
yield {path: ret.value.path, id: ret.value.id, bodySize: ret.value.bodySize};
|
||||
async *list(): AsyncIterable<FileMetaData> {
|
||||
for await (
|
||||
const entry of this.kv.list<SerializedFilePathIndex>({
|
||||
prefix: ['file_index_path'],
|
||||
})
|
||||
) {
|
||||
const ret = await this.kv.get<SerializedFile>([
|
||||
'file',
|
||||
entry.value.fileId,
|
||||
]);
|
||||
if (ret.value !== null) {
|
||||
yield {
|
||||
path: ret.value.path,
|
||||
id: ret.value.id,
|
||||
bodySize: ret.value.bodySize,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteByPath(path: string): Promise<void> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]);
|
||||
if(idx.value === null){
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||
'file_index_path',
|
||||
path,
|
||||
]);
|
||||
if (idx.value === null) {
|
||||
return;
|
||||
}
|
||||
// todo, this will fail silently if transaction fails. we might want to put that into a loop.
|
||||
await this.kv.atomic()
|
||||
.check(idx)
|
||||
.delete(["file_index_path", path])
|
||||
.delete(['file_index_path', path])
|
||||
.delete(idx.key)
|
||||
.commit();
|
||||
}
|
||||
|
||||
async getFullBodyByPath(path: string): Promise<string|null> {
|
||||
async getFullBodyByPath(path: string): Promise<string | null> {
|
||||
const ret = await this.getRawBodyByPath(path);
|
||||
return ret == null ? null : new TextDecoder().decode(ret);
|
||||
}
|
||||
|
||||
async getRawBodyByPath(path: string): Promise<Uint8Array|null> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]);
|
||||
if(idx.value === null){
|
||||
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.getBlob(idx.value.fileId);
|
||||
}
|
||||
|
||||
async getMetaDataByID(id: string): Promise<FileMetaData | null>{
|
||||
const fileObject = await this.kv.get<SerializedFile>(['file', id])
|
||||
if(fileObject.value === null){
|
||||
async getMetaDataByID(id: string): Promise<FileMetaData | null> {
|
||||
const fileObject = await this.kv.get<SerializedFile>(['file', id]);
|
||||
if (fileObject.value === null) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
@@ -158,27 +194,39 @@ export class DenoKVFileService implements FileService {
|
||||
};
|
||||
}
|
||||
|
||||
async renameById(id: string, newPath: string): Promise<FileMetaData|null> {
|
||||
async renameById(id: string, newPath: string): Promise<FileMetaData | null> {
|
||||
const fileObject = await this.kv.get<SerializedFile>(['file', id]);
|
||||
if(fileObject.value === null){
|
||||
if (fileObject.value === null) {
|
||||
return null;
|
||||
}
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", fileObject.value.path]);
|
||||
const newFile: FileMetaData = {path: newPath, id: fileObject.value.id, bodySize: fileObject.value.bodySize};
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||
'file_index_path',
|
||||
fileObject.value.path,
|
||||
]);
|
||||
const newFile: FileMetaData = {
|
||||
path: newPath,
|
||||
id: fileObject.value.id,
|
||||
bodySize: fileObject.value.bodySize,
|
||||
};
|
||||
await this.kv.atomic()
|
||||
.check(fileObject)
|
||||
.check(idx)
|
||||
.delete(idx.key)
|
||||
.set(["file_index_path", newPath], idx.value)
|
||||
.set(['file_index_path', newPath], idx.value)
|
||||
.set(fileObject.key, newFile)
|
||||
.commit()
|
||||
.commit();
|
||||
return this.getMetaDataByID(id);
|
||||
}
|
||||
|
||||
async renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|null> {
|
||||
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", originalPath]);
|
||||
if(idx.value == null){
|
||||
async renameByPath(
|
||||
originalPath: string,
|
||||
newPath: string,
|
||||
): Promise<FileMetaData | null> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||
'file_index_path',
|
||||
originalPath,
|
||||
]);
|
||||
if (idx.value == null) {
|
||||
return null;
|
||||
}
|
||||
return this.renameById(idx.value.fileId, newPath);
|
||||
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
import { Application } from "https://deno.land/x/oak/mod.ts";
|
||||
import { Application } from 'https://deno.land/x/oak/mod.ts';
|
||||
|
||||
const app = new Application();
|
||||
|
||||
app.use((ctx) => {
|
||||
ctx.response.body = "Hello World!";
|
||||
ctx.response.body = 'Hello World!';
|
||||
});
|
||||
|
||||
await app.listen({ port: 9000 });
|
||||
Reference in new issue
Block a user