Refactoring
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { FileMeta } from "../types.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
import { AssetBundle } from "../../plugos/asset_bundle/bundle.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
|
||||
constructor(
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { DenoKVSpacePrimitives } from "./deno_kv_space_primitives.ts";
|
||||
|
||||
Deno.test("deno_kv_space_primitives", async () => {
|
||||
const tempFile = await Deno.makeTempFile({ suffix: ".db" });
|
||||
const spacePrimitives = new DenoKVSpacePrimitives();
|
||||
await spacePrimitives.init(tempFile);
|
||||
await spacePrimitives.writeFile("test.txt", new TextEncoder().encode("test"));
|
||||
let result = await spacePrimitives.readFile("test.txt");
|
||||
assertEquals(result.data, new TextEncoder().encode("test"));
|
||||
let listing = await spacePrimitives.fetchFileList();
|
||||
assertEquals(listing.length, 1);
|
||||
await spacePrimitives.writeFile(
|
||||
"test.txt",
|
||||
new TextEncoder().encode("test2"),
|
||||
);
|
||||
result = await spacePrimitives.readFile("test.txt");
|
||||
assertEquals(result.data, new TextEncoder().encode("test2"));
|
||||
await spacePrimitives.deleteFile("test.txt");
|
||||
listing = await spacePrimitives.fetchFileList();
|
||||
try {
|
||||
await spacePrimitives.readFile("test.txt");
|
||||
throw new Error("Should not be here");
|
||||
} catch (e: any) {
|
||||
assertEquals(e.message, "Not found");
|
||||
}
|
||||
assertEquals(listing.length, 0);
|
||||
|
||||
spacePrimitives.close();
|
||||
await Deno.remove(tempFile);
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
/// <reference lib="deno.unstable" />
|
||||
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
import type { SpacePrimitives } from "./space_primitives.ts";
|
||||
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
|
||||
|
||||
export class DenoKVSpacePrimitives implements SpacePrimitives {
|
||||
private kv!: Deno.Kv;
|
||||
private dataAttribute = "file";
|
||||
private metaAttribute = "meta";
|
||||
|
||||
async init(path?: string) {
|
||||
this.kv = await Deno.openKv(path);
|
||||
}
|
||||
|
||||
close() {
|
||||
this.kv.close();
|
||||
}
|
||||
|
||||
async fetchFileList(): Promise<FileMeta[]> {
|
||||
const results: FileMeta[] = [];
|
||||
for await (
|
||||
const result of this.kv.list({
|
||||
prefix: [this.metaAttribute],
|
||||
})
|
||||
) {
|
||||
results.push(result.value as FileMeta);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
async readFile(name: string): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
||||
const [meta, data] = await this.kv.getMany([[this.metaAttribute, name], [
|
||||
this.dataAttribute,
|
||||
name,
|
||||
]]);
|
||||
if (!meta.value) {
|
||||
throw new Error("Not found");
|
||||
}
|
||||
return {
|
||||
data: data.value as Uint8Array,
|
||||
meta: meta.value as FileMeta,
|
||||
};
|
||||
}
|
||||
async getFileMeta(name: string): Promise<FileMeta> {
|
||||
const result = await this.kv.get([this.metaAttribute, name]);
|
||||
if (result.value) {
|
||||
return result.value as FileMeta;
|
||||
} else {
|
||||
throw new Error("Not found");
|
||||
}
|
||||
}
|
||||
async writeFile(
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
_selfUpdate?: boolean | undefined,
|
||||
suggestedMeta?: FileMeta | undefined,
|
||||
): Promise<FileMeta> {
|
||||
const meta: FileMeta = {
|
||||
name,
|
||||
lastModified: suggestedMeta?.lastModified || Date.now(),
|
||||
contentType: mime.getType(name) || "application/octet-stream",
|
||||
size: data.byteLength,
|
||||
perm: suggestedMeta?.perm || "rw",
|
||||
};
|
||||
const res = await this.kv.atomic()
|
||||
.set([this.dataAttribute, name], data)
|
||||
.set([this.metaAttribute, name], meta)
|
||||
.commit();
|
||||
if (!res.ok) {
|
||||
throw res;
|
||||
}
|
||||
return meta;
|
||||
}
|
||||
async deleteFile(name: string): Promise<void> {
|
||||
const res = await this.kv.atomic()
|
||||
.delete([this.dataAttribute, name])
|
||||
.delete([this.metaAttribute, name])
|
||||
.commit();
|
||||
if (!res.ok) {
|
||||
throw res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
// import { mkdir, readdir, readFile, stat, unlink, writeFile } from "fs/promises";
|
||||
import { path } from "../deps.ts";
|
||||
import { readAll } from "../deps.ts";
|
||||
import { FileMeta } from "../types.ts";
|
||||
import * as path from "https://deno.land/std@0.189.0/path/mod.ts";
|
||||
import { readAll } from "https://deno.land/std@0.165.0/streams/conversion.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
|
||||
import { walk } from "https://deno.land/std@0.198.0/fs/walk.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
function lookupContentType(path: string): string {
|
||||
return mime.getType(path) || "application/octet-stream";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
import { EventHook } from "../../plugos/hooks/event.ts";
|
||||
|
||||
import { FileMeta } from "../types.ts";
|
||||
import type { SpacePrimitives } from "./space_primitives.ts";
|
||||
|
||||
export class EventedSpacePrimitives implements SpacePrimitives {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { FileMeta } from "../types.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
import type { SpacePrimitives } from "./space_primitives.ts";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { FileMeta } from "../types.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
import type { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
// Enriches the file list listing with custom metadata from the page index
|
||||
export class FileMetaSpacePrimitives implements SpacePrimitives {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { FileMeta } from "../types.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
|
||||
export class FilteredSpacePrimitives implements SpacePrimitives {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { FileMeta } from "../types.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
import { flushCachesAndUnregisterServiceWorker } from "../sw_util.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
export class HttpSpacePrimitives implements SpacePrimitives {
|
||||
constructor(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FileMeta } from "../types.ts";
|
||||
import type { SpacePrimitives } from "./space_primitives.ts";
|
||||
import Dexie, { Table } from "dexie";
|
||||
import { mime } from "../deps.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
export type FileContent = {
|
||||
name: string;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { SpacePrimitives } from "../../common/spaces/space_primitives.ts";
|
||||
import { FileMeta } from "../../common/types.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
import {
|
||||
NamespaceOperation,
|
||||
PlugNamespaceHook,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { FileMeta } from "../types.ts";
|
||||
|
||||
// export type FileEncoding = "utf8" | "arraybuffer" | "dataurl";
|
||||
// export type FileData = ArrayBuffer | string;
|
||||
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
export interface SpacePrimitives {
|
||||
// Returns a list of file meta data as well as the timestamp of this snapshot
|
||||
fetchFileList(): Promise<FileMeta[]>;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { renderToText, replaceNodesMatching } from "../../plug-api/lib/tree.ts";
|
||||
import buildMarkdown from "../markdown_parser/parser.ts";
|
||||
import { parse } from "../markdown_parser/parse_tree.ts";
|
||||
import type { FileMeta } from "../types.ts";
|
||||
import { SpacePrimitives } from "./space_primitives.ts";
|
||||
import { EventEmitter } from "../../plugos/event.ts";
|
||||
import { FileMeta } from "$sb/types.ts";
|
||||
|
||||
type SyncHash = number;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user