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 {
|
export interface FileService {
|
||||||
createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData>;
|
createFromFullBody(
|
||||||
|
path: string,
|
||||||
|
body: string | Uint8Array,
|
||||||
|
): Promise<FileMetaData>;
|
||||||
list(): AsyncIterable<FileMetaData>;
|
list(): AsyncIterable<FileMetaData>;
|
||||||
deleteByPath(path: string): Promise<void>;
|
deleteByPath(path: string): Promise<void>;
|
||||||
getFullBodyByPath(path: string): Promise<string | null>;
|
getFullBodyByPath(path: string): Promise<string | null>;
|
||||||
getRawBodyByPath(path: string): Promise<Uint8Array | null>;
|
getRawBodyByPath(path: string): Promise<Uint8Array | null>;
|
||||||
getMetaDataByID(id: string): Promise<FileMetaData | null>;
|
getMetaDataByID(id: string): Promise<FileMetaData | null>;
|
||||||
renameById(id: string, newPath: 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 {
|
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;
|
||||||
@@ -40,19 +44,24 @@ export class DenoKVBlobService {
|
|||||||
|
|
||||||
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(
|
||||||
|
blobCount * this.blobSize,
|
||||||
|
(blobCount + 1) * this.blobSize,
|
||||||
|
);
|
||||||
while (txt.length > 0) {
|
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;
|
||||||
@@ -69,10 +78,14 @@ export class DenoKVBlobService {
|
|||||||
|
|
||||||
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] })
|
||||||
|
) {
|
||||||
|
const foo = await this.kv.get<Uint8Array>(['blob', itm.value]);
|
||||||
if (foo.value === null) {
|
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);
|
ret = this.concatUint8Array(ret, foo.value);
|
||||||
}
|
}
|
||||||
@@ -81,14 +94,13 @@ export class DenoKVBlobService {
|
|||||||
|
|
||||||
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 {
|
||||||
@@ -100,9 +112,16 @@ export class DenoKVFileService implements FileService {
|
|||||||
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 = {
|
||||||
|
id: newId,
|
||||||
|
path,
|
||||||
|
bodySize: body.length,
|
||||||
|
};
|
||||||
const idx: SerializedFilePathIndex = { fileId: newId };
|
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);
|
||||||
@@ -112,23 +131,37 @@ export class DenoKVFileService implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>({
|
||||||
|
prefix: ['file_index_path'],
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
const ret = await this.kv.get<SerializedFile>([
|
||||||
|
'file',
|
||||||
|
entry.value.fileId,
|
||||||
|
]);
|
||||||
if (ret.value !== null) {
|
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> {
|
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) {
|
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();
|
||||||
}
|
}
|
||||||
@@ -139,7 +172,10 @@ export class DenoKVFileService implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>([
|
||||||
|
'file_index_path',
|
||||||
|
path,
|
||||||
|
]);
|
||||||
if (idx.value === null) {
|
if (idx.value === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -147,7 +183,7 @@ export class DenoKVFileService implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -163,21 +199,33 @@ export class DenoKVFileService implements FileService {
|
|||||||
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,
|
||||||
|
): Promise<FileMetaData | null> {
|
||||||
|
const idx = await this.kv.get<SerializedFilePathIndex>([
|
||||||
|
'file_index_path',
|
||||||
|
originalPath,
|
||||||
|
]);
|
||||||
if (idx.value == null) {
|
if (idx.value == null) {
|
||||||
return 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();
|
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 });
|
||||||
+26
-24
@@ -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,14 +33,14 @@ 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[]> {
|
||||||
@@ -44,7 +49,6 @@ async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]>{
|
|||||||
ret.push(e);
|
ret.push(e);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Deno.test("DenoKVBlobService happy path", async (t) => {
|
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 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) => {
|
||||||
@@ -84,7 +87,6 @@ Deno.test("DenoKVFileService happy path", async (t) => {
|
|||||||
let file1: FileMetaData | undefined;
|
let file1: FileMetaData | undefined;
|
||||||
let file2: FileMetaData | undefined;
|
let file2: FileMetaData | undefined;
|
||||||
|
|
||||||
|
|
||||||
await t.step("Create 2 files", async () => {
|
await t.step("Create 2 files", async () => {
|
||||||
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
|
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
|
||||||
file2 = await fs.createFromFullBody(fileName2, "le geant vert ☀️");
|
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 () => {
|
await t.step("Get file metadata", async () => {
|
||||||
const ret = await fs.getMetaDataByID(file1?.id ?? '404');
|
const ret = await fs.getMetaDataByID(file1?.id ?? "404");
|
||||||
assertEquals(ret, file1)
|
assertEquals(ret, file1);
|
||||||
});
|
});
|
||||||
|
|
||||||
await t.step("List files", async () => {
|
await t.step("List files", async () => {
|
||||||
@@ -125,31 +127,31 @@ Deno.test("DenoKVFileService happy path", async (t) => {
|
|||||||
|
|
||||||
await t.step("Rename file", async () => {
|
await t.step("Rename file", async () => {
|
||||||
const newPath = "boris/file2.txt";
|
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(ret?.path, newPath);
|
||||||
|
|
||||||
assertEquals((await fs.getFullBodyByPath(newPath))?.length, file2?.bodySize?? -1);
|
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();
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user