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
+158 -98

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/"]
}
}
+114 -66
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 {
createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData>;
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>;
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
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();
app.use((ctx) => {
ctx.response.body = "Hello World!";
ctx.response.body = 'Hello World!';
});
await app.listen({ port: 9000 });
+32 -30
View File
@@ -1,6 +1,11 @@
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 {
const words = [
@@ -28,23 +33,22 @@ function generateLargeText(seed: number, size: number): string {
"Solomon Islands",
"Grenada",
"Hungary",
"Korea, South"
]
"Korea, South",
];
let ret: string[] = [];
for(let i = 0; i < size; i ++){
for (let i = 0; i < size; i++) {
seed = (seed * 4931 + 2753) % words.length;
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[] = [];
for await(const e of ite){
for await (const e of ite) {
ret.push(e);
}
return ret;
}
Deno.test("DenoKVBlobService happy path", async (t) => {
@@ -53,27 +57,26 @@ Deno.test("DenoKVBlobService happy path", async (t) => {
const bs = new DenoKVBlobService(kv, 50);
function toUint8Array(s: string): Uint8Array{
function toUint8Array(s: string): Uint8Array {
return new TextEncoder().encode(s);
}
await t.step("read write small text", async () => {
await bs.persistBlob('0002', toUint8Array('hello world'));
const body = await bs.getBlob('0002');
assertEquals(body, toUint8Array('hello world'));
assertEquals(await bs.getNumberOfBlobs('0002'), 1);
await bs.persistBlob("0002", toUint8Array("hello world"));
const body = await bs.getBlob("0002");
assertEquals(body, toUint8Array("hello world"));
assertEquals(await bs.getNumberOfBlobs("0002"), 1);
});
await t.step("read write large text", async () => {
const largeText = toUint8Array(generateLargeText(33, 100));
await bs.persistBlob('0001', largeText);
const body = await bs.getBlob('0001');
await bs.persistBlob("0001", largeText);
const body = await bs.getBlob("0001");
assertEquals(body, largeText);
assertEquals(await bs.getNumberOfBlobs('0001'), 21);
assertEquals(await bs.getNumberOfBlobs("0001"), 21);
});
kv.close();
});
Deno.test("DenoKVFileService happy path", async (t) => {
@@ -81,9 +84,8 @@ Deno.test("DenoKVFileService happy path", async (t) => {
const fs: FileService = new DenoKVFileService(kv);
const fileName1 = "didier/file1.txt";
const fileName2 = "didier/file2.txt";
let file1 : FileMetaData|undefined;
let file2: FileMetaData|undefined;
let file1: FileMetaData | undefined;
let file2: FileMetaData | undefined;
await t.step("Create 2 files", async () => {
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
@@ -94,8 +96,8 @@ Deno.test("DenoKVFileService happy path", async (t) => {
});
await t.step("Get file metadata", async () => {
const ret = await fs.getMetaDataByID(file1?.id ?? '404');
assertEquals(ret, file1)
const ret = await fs.getMetaDataByID(file1?.id ?? "404");
assertEquals(ret, file1);
});
await t.step("List files", async () => {
@@ -125,31 +127,31 @@ Deno.test("DenoKVFileService happy path", async (t) => {
await t.step("Rename file", async () => {
const newPath = "boris/file2.txt";
const ret = await fs.renameById(file2?.id ?? '404', newPath);
const ret = await fs.renameById(file2?.id ?? "404", newPath);
assertEquals(ret?.path, newPath);
assertEquals((await fs.getFullBodyByPath(newPath))?.length, file2?.bodySize?? -1);
assertEquals(
(await fs.getFullBodyByPath(newPath))?.length,
file2?.bodySize ?? -1,
);
});
kv.close();
});
Deno.test("DenoKVFileService happy path with byte array", async (t) => {
const kv = await Deno.openKv(":memory:");
const fs: FileService = new DenoKVFileService(kv);
await t.step("Create and read 1 binary file", async () => {
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);
const bodyString = await fs.getFullBodyByPath('hello');
const bodyString = await fs.getFullBodyByPath("hello");
assertEquals(bodyString, new TextDecoder().decode(inputBody));
});
kv.close();
});