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:
didier amyot committed 2023-06-10 08:43:57 -04:00
1 parent ce7b6ae523
commit 8ce2dadc47
4 files changed
+224 -164

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
{
"fmt": {
"lineWidth": 120,
"indentWidth": 4,
"semiColons": true,
"singleQuote": true,
"proseWrap": "preserve",
"include": ["src/", "test/"]
}
}
+115 -67
View File
@@ -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 { export interface FileService {
createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData>; createFromFullBody(
list(): AsyncIterable<FileMetaData>; path: string,
deleteByPath(path: string): Promise<void>; body: string | Uint8Array,
getFullBodyByPath(path: string): Promise<string|null>; ): Promise<FileMetaData>;
getRawBodyByPath(path: string): Promise<Uint8Array|null>; list(): AsyncIterable<FileMetaData>;
getMetaDataByID(id: string): Promise<FileMetaData| null>; deleteByPath(path: string): Promise<void>;
renameById(id: string, newPath: string): Promise<FileMetaData|null>; getFullBodyByPath(path: string): Promise<string | null>;
renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|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 { interface SerializedFile {
id: string, id: string;
path: string, path: string;
bodySize: number, bodySize: number;
} }
export interface FileMetaData { export interface FileMetaData {
path: string, path: string;
id: string, id: string;
bodySize: number, bodySize: number;
} }
interface SerializedFilePathIndex { interface SerializedFilePathIndex {
fileId: string; fileId: string;
} }
export class DenoKVBlobService { export class DenoKVBlobService {
private readonly kv: Deno.Kv; private readonly kv: Deno.Kv;
private readonly blobSize: number; private readonly blobSize: number;
constructor(kv: Deno.Kv, blobSize: number){ constructor(kv: Deno.Kv, blobSize: number) {
this.kv = kv; this.kv = kv;
this.blobSize = blobSize; this.blobSize = blobSize;
} }
private *splitText(body: Uint8Array): Generator<Uint8Array>{ private *splitText(body: Uint8Array): Generator<Uint8Array> {
let blobCount = 0; let blobCount = 0;
let txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize); let txt = body.slice(
while(txt.length > 0 ){ blobCount * this.blobSize,
(blobCount + 1) * this.blobSize,
);
while (txt.length > 0) {
yield txt; yield txt;
blobCount += 1; 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; let blobCount = 0;
for(const txt of this.splitText(body)){ for (const txt of this.splitText(body)) {
const blob_id = sha1(txt, "utf8", "hex"); const blob_id = sha1(txt, 'utf8', 'hex');
await this.kv.set(['blob_index', index, blobCount], blob_id); await this.kv.set(['blob_index', index, blobCount], blob_id);
await this.kv.set(['blob', blob_id], txt); await this.kv.set(['blob', blob_id], txt);
blobCount += 1; blobCount += 1;
@@ -67,88 +76,115 @@ export class DenoKVBlobService {
return mergedArray; return mergedArray;
} }
async getBlob(index: string): Promise<Uint8Array>{ async getBlob(index: string): Promise<Uint8Array> {
let ret = new Uint8Array(); let ret = new Uint8Array();
for await(const itm of this.kv.list<string>({prefix: ['blob_index', index]})){ for await (
const foo = await this.kv.get<Uint8Array>(['blob', itm.value]) const itm of this.kv.list<string>({ prefix: ['blob_index', index] })
if(foo.value === null){ ) {
throw new Error(`Inconsistant blob index for key "${foo.value}", this is very bad.`); 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); ret = this.concatUint8Array(ret, foo.value);
} }
return ret; return ret;
} }
async getNumberOfBlobs(index: string): Promise<number>{ async getNumberOfBlobs(index: string): Promise<number> {
let ret = 0; 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; ret += 1;
} }
return ret; return ret;
} }
} }
export class DenoKVFileService implements FileService { export class DenoKVFileService implements FileService {
private readonly kv: Deno.Kv; private readonly kv: Deno.Kv;
private readonly blobService: DenoKVBlobService; private readonly blobService: DenoKVBlobService;
constructor(kv: Deno.Kv){ constructor(kv: Deno.Kv) {
this.kv = kv; this.kv = kv;
this.blobService = new DenoKVBlobService(kv, 500); 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 newId = crypto.randomUUID();
const serializedFile: SerializedFile = {id: newId, path, bodySize: body.length}; const serializedFile: SerializedFile = {
const idx: SerializedFilePathIndex = {fileId: newId}; id: newId,
path,
bodySize: body.length,
};
const idx: SerializedFilePathIndex = { fileId: newId };
await this.kv.set(['file', newId], serializedFile); await this.kv.set(['file', newId], serializedFile);
await this.kv.set(['file_index_path', path], idx); 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); await this.blobService.persistBlob(newId, bodyBytes);
return {id: newId, path, bodySize: body.length}; return { id: newId, path, bodySize: body.length };
} }
async* list(): AsyncIterable<FileMetaData> { async *list(): AsyncIterable<FileMetaData> {
for await (const entry of this.kv.list<SerializedFilePathIndex>({ prefix: ["file_index_path"] })) { for await (
const ret = await this.kv.get<SerializedFile>(['file', entry.value.fileId]); const entry of this.kv.list<SerializedFilePathIndex>({
if(ret.value !== null) { prefix: ['file_index_path'],
yield {path: ret.value.path, id: ret.value.id, bodySize: ret.value.bodySize}; })
) {
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> { async deleteByPath(path: string): Promise<void> {
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]); const idx = await this.kv.get<SerializedFilePathIndex>([
if(idx.value === null){ 'file_index_path',
path,
]);
if (idx.value === null) {
return; return;
} }
// todo, this will fail silently if transaction fails. we might want to put that into a loop. // todo, this will fail silently if transaction fails. we might want to put that into a loop.
await this.kv.atomic() await this.kv.atomic()
.check(idx) .check(idx)
.delete(["file_index_path", path]) .delete(['file_index_path', path])
.delete(idx.key) .delete(idx.key)
.commit(); .commit();
} }
async getFullBodyByPath(path: string): Promise<string|null> { async getFullBodyByPath(path: string): Promise<string | null> {
const ret = await this.getRawBodyByPath(path); const ret = await this.getRawBodyByPath(path);
return ret == null ? null : new TextDecoder().decode(ret); return ret == null ? null : new TextDecoder().decode(ret);
} }
async getRawBodyByPath(path: string): Promise<Uint8Array|null> { async getRawBodyByPath(path: string): Promise<Uint8Array | null> {
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]); const idx = await this.kv.get<SerializedFilePathIndex>([
if(idx.value === null){ 'file_index_path',
path,
]);
if (idx.value === null) {
return null; return null;
} }
return this.blobService.getBlob(idx.value.fileId); return this.blobService.getBlob(idx.value.fileId);
} }
async getMetaDataByID(id: string): Promise<FileMetaData | null>{ async getMetaDataByID(id: string): Promise<FileMetaData | null> {
const fileObject = await this.kv.get<SerializedFile>(['file', id]) const fileObject = await this.kv.get<SerializedFile>(['file', id]);
if(fileObject.value === null){ if (fileObject.value === null) {
return null; return null;
} }
return { 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]); const fileObject = await this.kv.get<SerializedFile>(['file', id]);
if(fileObject.value === null){ if (fileObject.value === null) {
return null; return null;
} }
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", fileObject.value.path]); const idx = await this.kv.get<SerializedFilePathIndex>([
const newFile: FileMetaData = {path: newPath, id: fileObject.value.id, bodySize: fileObject.value.bodySize}; 'file_index_path',
fileObject.value.path,
]);
const newFile: FileMetaData = {
path: newPath,
id: fileObject.value.id,
bodySize: fileObject.value.bodySize,
};
await this.kv.atomic() await this.kv.atomic()
.check(fileObject) .check(fileObject)
.check(idx) .check(idx)
.delete(idx.key) .delete(idx.key)
.set(["file_index_path", newPath], idx.value) .set(['file_index_path', newPath], idx.value)
.set(fileObject.key, newFile) .set(fileObject.key, newFile)
.commit() .commit();
return this.getMetaDataByID(id); return this.getMetaDataByID(id);
} }
async renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|null> { async renameByPath(
originalPath: string,
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", originalPath]); newPath: string,
if(idx.value == null){ ): Promise<FileMetaData | null> {
const idx = await this.kv.get<SerializedFilePathIndex>([
'file_index_path',
originalPath,
]);
if (idx.value == null) {
return null; return null;
} }
return this.renameById(idx.value.fileId, newPath); return this.renameById(idx.value.fileId, newPath);
+2 -2
View File
@@ -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(); const app = new Application();
app.use((ctx) => { app.use((ctx) => {
ctx.response.body = "Hello World!"; ctx.response.body = 'Hello World!';
}); });
await app.listen({ port: 9000 }); await app.listen({ port: 9000 });
+97 -95
View File
@@ -1,6 +1,11 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import {DenoKVFileService, FileMetaData, DenoKVBlobService, FileService} from "../src/fileservice.ts"; import {
DenoKVBlobService,
DenoKVFileService,
FileMetaData,
FileService,
} from "../src/fileservice.ts";
function generateLargeText(seed: number, size: number): string { function generateLargeText(seed: number, size: number): string {
const words = [ const words = [
@@ -28,128 +33,125 @@ function generateLargeText(seed: number, size: number): string {
"Solomon Islands", "Solomon Islands",
"Grenada", "Grenada",
"Hungary", "Hungary",
"Korea, South" "Korea, South",
] ];
let ret: string[] = []; let ret: string[] = [];
for(let i = 0; i < size; i ++){ for (let i = 0; i < size; i++) {
seed = (seed * 4931 + 2753) % words.length; seed = (seed * 4931 + 2753) % words.length;
ret.push(words[seed]); ret.push(words[seed]);
} }
return ret.join(' '); return ret.join(" ");
} }
async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]>{ async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]> {
const ret: T[] = []; const ret: T[] = [];
for await(const e of ite){ for await (const e of ite) {
ret.push(e); ret.push(e);
} }
return ret; return ret;
} }
Deno.test("DenoKVBlobService happy path", async (t) => { Deno.test("DenoKVBlobService happy path", async (t) => {
localStorage.clear(); localStorage.clear();
const kv = await Deno.openKv(":memory:"); const kv = await Deno.openKv(":memory:");
const bs = new DenoKVBlobService(kv, 50); const bs = new DenoKVBlobService(kv, 50);
function toUint8Array(s: string): Uint8Array{ function toUint8Array(s: string): Uint8Array {
return new TextEncoder().encode(s); return new TextEncoder().encode(s);
} }
await t.step("read write small text", async () => { await t.step("read write small text", async () => {
await bs.persistBlob('0002', toUint8Array('hello world')); await bs.persistBlob("0002", toUint8Array("hello world"));
const body = await bs.getBlob('0002'); const body = await bs.getBlob("0002");
assertEquals(body, toUint8Array('hello world')); assertEquals(body, toUint8Array("hello world"));
assertEquals(await bs.getNumberOfBlobs('0002'), 1); assertEquals(await bs.getNumberOfBlobs("0002"), 1);
}); });
await t.step("read write large text", async () => { await t.step("read write large text", async () => {
const largeText = toUint8Array(generateLargeText(33, 100)); const largeText = toUint8Array(generateLargeText(33, 100));
await bs.persistBlob('0001', largeText); await bs.persistBlob("0001", largeText);
const body = await bs.getBlob('0001'); const body = await bs.getBlob("0001");
assertEquals(body, largeText); assertEquals(body, largeText);
assertEquals(await bs.getNumberOfBlobs('0001'), 21); assertEquals(await bs.getNumberOfBlobs("0001"), 21);
}); });
kv.close();
kv.close();
}); });
Deno.test("DenoKVFileService happy path", async (t) => { Deno.test("DenoKVFileService happy path", async (t) => {
const kv = await Deno.openKv(":memory:"); const kv = await Deno.openKv(":memory:");
const fs: FileService = new DenoKVFileService(kv); const fs: FileService = new DenoKVFileService(kv);
const fileName1 = "didier/file1.txt"; const fileName1 = "didier/file1.txt";
const fileName2 = "didier/file2.txt"; const fileName2 = "didier/file2.txt";
let file1 : FileMetaData|undefined; let file1: FileMetaData | undefined;
let file2: FileMetaData|undefined; let file2: FileMetaData | undefined;
await t.step("Create 2 files", async () => {
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
file2 = await fs.createFromFullBody(fileName2, "le geant vert ☀️");
await t.step("Create 2 files", async () => { assertEquals(file1.path, fileName1);
file1 = await fs.createFromFullBody(fileName1, "bob le chef"); assertEquals(file2.bodySize, "le geant vert ".length + 2);
file2 = await fs.createFromFullBody(fileName2, "le geant vert ☀️"); });
assertEquals(file1.path, fileName1); await t.step("Get file metadata", async () => {
assertEquals(file2.bodySize, "le geant vert ".length + 2); const ret = await fs.getMetaDataByID(file1?.id ?? "404");
}); assertEquals(ret, file1);
});
await t.step("Get file metadata", async () => { await t.step("List files", async () => {
const ret = await fs.getMetaDataByID(file1?.id ?? '404'); const expected = [file1, file2];
assertEquals(ret, file1) const ret = await fs.list();
}); assertEquals(await asyncIterableToArray(ret), expected);
});
await t.step("List files", async () => { await t.step("Get File by path", async () => {
const expected = [file1, file2]; assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
const ret = await fs.list(); });
assertEquals(await asyncIterableToArray(ret), expected);
});
await t.step("Get File by path", async () => { await t.step("Get File by Id", async () => {
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef"); assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
}); });
await t.step("Get File by Id", async () => { await t.step("Delete File", async () => {
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef"); await fs.deleteByPath(fileName1);
}); await fs.deleteByPath("404 not found");
});
await t.step("Delete File", async () => { await t.step("List after delete", async () => {
await fs.deleteByPath(fileName1); const expected = [file2];
await fs.deleteByPath("404 not found"); const ret = await fs.list();
}); assertEquals(await asyncIterableToArray(ret), expected);
});
await t.step("List after delete", async () => { await t.step("Rename file", async () => {
const expected = [file2]; const newPath = "boris/file2.txt";
const ret = await fs.list(); const ret = await fs.renameById(file2?.id ?? "404", newPath);
assertEquals(await asyncIterableToArray(ret), expected); assertEquals(ret?.path, newPath);
});
await t.step("Rename file", async () => { assertEquals(
const newPath = "boris/file2.txt"; (await fs.getFullBodyByPath(newPath))?.length,
const ret = await fs.renameById(file2?.id ?? '404', newPath); file2?.bodySize ?? -1,
assertEquals(ret?.path, newPath); );
});
assertEquals((await fs.getFullBodyByPath(newPath))?.length, file2?.bodySize?? -1); kv.close();
});
kv.close();
}); });
Deno.test("DenoKVFileService happy path with byte array", async (t) => { Deno.test("DenoKVFileService happy path with byte array", async (t) => {
const kv = await Deno.openKv(":memory:"); const kv = await Deno.openKv(":memory:");
const fs: FileService = new DenoKVFileService(kv); const fs: FileService = new DenoKVFileService(kv);
await t.step("Create and read 1 binary file", async () => { await t.step("Create and read 1 binary file", async () => {
const inputBody = new Uint8Array([240]); const inputBody = new Uint8Array([240]);
const file = await fs.createFromFullBody('hello', inputBody); const file = await fs.createFromFullBody("hello", inputBody);
assertEquals(file.path, 'hello'); assertEquals(file.path, "hello");
assertEquals(file.bodySize, inputBody.length); assertEquals(file.bodySize, inputBody.length);
const bodyString = await fs.getFullBodyByPath('hello'); const bodyString = await fs.getFullBodyByPath("hello");
assertEquals(bodyString, new TextDecoder().decode(inputBody)); assertEquals(bodyString, new TextDecoder().decode(inputBody));
}); });
kv.close();
kv.close();
}); });