mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
change semantics
Update README deployment steps and add deploy:secrets script to package.json update local inference script and README update lockfile reconfigure package scripts for development update test execution pass server tests Update README with revised Bun commands and workspace details remove pnpm package manager designator create bun server
This commit is contained in:

committed by
Geoff Seemueller

parent
1055cda2f1
commit
497eb22ad8
100
packages/server/storage/BunSqliteKVNamespace.ts
Normal file
100
packages/server/storage/BunSqliteKVNamespace.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import type {
|
||||
KVNamespace,
|
||||
KVNamespaceGetOptions,
|
||||
KVNamespaceListOptions,
|
||||
KVNamespaceListResult, KVNamespacePutOptions
|
||||
} from "@cloudflare/workers-types";
|
||||
import {BunSqliteKeyValue} from "bun-sqlite-key-value";
|
||||
import {OPEN_GSIO_DATA_DIR} from "../constants";
|
||||
|
||||
interface BaseKV extends KVNamespace {
|
||||
|
||||
}
|
||||
|
||||
|
||||
interface Options {
|
||||
namespace: string;
|
||||
path: string
|
||||
}
|
||||
|
||||
const defaultOptions = {
|
||||
namespace: "open-gsio",
|
||||
path: OPEN_GSIO_DATA_DIR
|
||||
};
|
||||
|
||||
export class BunSqliteKVNamespace implements BaseKV {
|
||||
private db: any;
|
||||
|
||||
constructor(options?: { namespace?: string, path?: string }) {
|
||||
const merged = {...defaultOptions, ...options};
|
||||
const {namespace, path} = merged;
|
||||
|
||||
this.db = new BunSqliteKeyValue(`${path}/${namespace}`);
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
await this.db.delete(key);
|
||||
}
|
||||
|
||||
async get(key: string | Array<string>, options?: Partial<KVNamespaceGetOptions<undefined>> | "text" | "json" | "arrayBuffer" | "stream" | KVNamespaceGetOptions<"text"> | KVNamespaceGetOptions<"json"> | KVNamespaceGetOptions<"arrayBuffer"> | KVNamespaceGetOptions<"stream"> | "text" | "json"): Promise<any> {
|
||||
if (Array.isArray(key)) {
|
||||
const result = new Map();
|
||||
for (const k of key) {
|
||||
const value = await this.db.get(k);
|
||||
result.set(k, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const value = await this.db.get(key);
|
||||
if (value === undefined) return null;
|
||||
|
||||
if (!options || options === "text" || (typeof options === "object" && options.type === "text")) {
|
||||
return value;
|
||||
}
|
||||
if (options === "json" || (typeof options === "object" && options.type === "json")) {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
if (options === "arrayBuffer" || (typeof options === "object" && options.type === "arrayBuffer")) {
|
||||
return new TextEncoder().encode(value).buffer;
|
||||
}
|
||||
if (options === "stream" || (typeof options === "object" && options.type === "stream")) {
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode(value));
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
getWithMetadata(key: string | Array<string>, options?: any): any {
|
||||
return null;
|
||||
}
|
||||
|
||||
async list<Metadata = unknown>(options?: KVNamespaceListOptions): Promise<KVNamespaceListResult<Metadata, string>> {
|
||||
const keys = await this.db.keys();
|
||||
return {
|
||||
keys: keys.map(key => ({name: key})),
|
||||
list_complete: true,
|
||||
cursor: ''
|
||||
};
|
||||
}
|
||||
|
||||
async put(key: string, value: string | ArrayBuffer | ArrayBufferView | ReadableStream, options?: KVNamespacePutOptions): Promise<void> {
|
||||
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
||||
value = new TextDecoder().decode(value);
|
||||
} else if (value instanceof ReadableStream) {
|
||||
const reader = value.getReader();
|
||||
const chunks = [];
|
||||
while (true) {
|
||||
const {done, value} = await reader.read();
|
||||
if (done) break;
|
||||
chunks.push(value);
|
||||
}
|
||||
value = new TextDecoder().decode(new Uint8Array(Buffer.concat(chunks)));
|
||||
}
|
||||
await this.db.set(key, value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user