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
+124
-64
No files matched your search
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"fmt": {
|
||||
"lineWidth": 120,
|
||||
"indentWidth": 4,
|
||||
"semiColons": true,
|
||||
"singleQuote": true,
|
||||
"proseWrap": "preserve",
|
||||
"include": ["src/", "test/"]
|
||||
}
|
||||
}
|
||||
+86
-38
@@ -1,34 +1,38 @@
|
||||
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>;
|
||||
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;
|
||||
@@ -40,19 +44,24 @@ export class DenoKVBlobService {
|
||||
|
||||
private *splitText(body: Uint8Array): Generator<Uint8Array> {
|
||||
let blobCount = 0;
|
||||
let txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
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> {
|
||||
let blobCount = 0;
|
||||
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', blob_id], txt);
|
||||
blobCount += 1;
|
||||
@@ -69,10 +78,14 @@ export class DenoKVBlobService {
|
||||
|
||||
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])
|
||||
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.`);
|
||||
throw new Error(
|
||||
`Inconsistant blob index for key "${foo.value}", this is very bad.`,
|
||||
);
|
||||
}
|
||||
ret = this.concatUint8Array(ret, foo.value);
|
||||
}
|
||||
@@ -81,14 +94,13 @@ export class DenoKVBlobService {
|
||||
|
||||
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 {
|
||||
@@ -100,9 +112,16 @@ export class DenoKVFileService implements FileService {
|
||||
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 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);
|
||||
@@ -112,23 +131,37 @@ export class DenoKVFileService implements FileService {
|
||||
}
|
||||
|
||||
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]);
|
||||
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};
|
||||
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]);
|
||||
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();
|
||||
}
|
||||
@@ -139,7 +172,10 @@ export class DenoKVFileService implements FileService {
|
||||
}
|
||||
|
||||
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>([
|
||||
'file_index_path',
|
||||
path,
|
||||
]);
|
||||
if (idx.value === null) {
|
||||
return null;
|
||||
}
|
||||
@@ -147,7 +183,7 @@ export class DenoKVFileService implements FileService {
|
||||
}
|
||||
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
@@ -163,21 +199,33 @@ export class DenoKVFileService implements FileService {
|
||||
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]);
|
||||
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;
|
||||
}
|
||||
|
||||
+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 });
|
||||
+26
-24
@@ -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,14 +33,14 @@ 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++) {
|
||||
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[]> {
|
||||
@@ -44,7 +49,6 @@ async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]>{
|
||||
ret.push(e);
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
Deno.test("DenoKVBlobService happy path", async (t) => {
|
||||
@@ -58,22 +62,21 @@ Deno.test("DenoKVBlobService happy path", async (t) => {
|
||||
}
|
||||
|
||||
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) => {
|
||||
@@ -84,7 +87,6 @@ Deno.test("DenoKVFileService happy path", async (t) => {
|
||||
let file1: 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 ☀️");
|
||||
@@ -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();
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user