release: v0.2.2 AriaEngine OPFS 自研存储后端 — 零依赖纯文件系统
This commit is contained in:
Vendored
+88
-1
@@ -2780,6 +2780,89 @@ class MemoryBackend {
|
||||
}
|
||||
}
|
||||
|
||||
class OPFSBackend {
|
||||
constructor() {
|
||||
this.root = null;
|
||||
this.dbDir = null;
|
||||
this.dbName = '';
|
||||
}
|
||||
async open(name) {
|
||||
this.dbName = name;
|
||||
this.root = await navigator.storage.getDirectory();
|
||||
this.dbDir = await this.root.getDirectoryHandle(name, { create: true });
|
||||
}
|
||||
async close() {
|
||||
this.dbDir = null;
|
||||
this.root = null;
|
||||
}
|
||||
isOpen() {
|
||||
return this.dbDir !== null;
|
||||
}
|
||||
async read(key) {
|
||||
if (!this.dbDir)
|
||||
return null;
|
||||
try {
|
||||
const fh = await this.dbDir.getFileHandle(key);
|
||||
const file = await fh.getFile();
|
||||
return await file.arrayBuffer();
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
async write(key, data) {
|
||||
if (!this.dbDir)
|
||||
return;
|
||||
const fh = await this.dbDir.getFileHandle(key, { create: true });
|
||||
const writable = await fh.createWritable();
|
||||
await writable.write(data);
|
||||
await writable.close();
|
||||
}
|
||||
async delete(key) {
|
||||
if (!this.dbDir)
|
||||
return;
|
||||
try {
|
||||
await this.dbDir.removeEntry(key);
|
||||
}
|
||||
catch {
|
||||
// 文件不存在则忽略
|
||||
}
|
||||
}
|
||||
async listKeys() {
|
||||
if (!this.dbDir)
|
||||
return [];
|
||||
const keys = [];
|
||||
// FileSystemDirectoryHandle.entries() 返回 AsyncIterable,使用 any 绕过 dts 生成限制
|
||||
const dir = this.dbDir;
|
||||
for await (const [name] of dir.entries()) {
|
||||
keys.push(name);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
async exists(key) {
|
||||
if (!this.dbDir)
|
||||
return false;
|
||||
try {
|
||||
await this.dbDir.getFileHandle(key);
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
async clear() {
|
||||
if (!this.dbDir)
|
||||
return;
|
||||
const dir = this.dbDir;
|
||||
for await (const [name] of dir.entries()) {
|
||||
try {
|
||||
await this.dbDir.removeEntry(name);
|
||||
}
|
||||
catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AriaEngine — 自研页面式存储引擎主类
|
||||
* @module engine/aria/index
|
||||
@@ -2817,7 +2900,10 @@ class AriaEngine {
|
||||
return;
|
||||
this.dbName = dbName;
|
||||
// 1. 存储后端
|
||||
if (this.config.storageBackend === 'indexeddb') {
|
||||
if (this.config.storageBackend === 'opfs') {
|
||||
this.backend = new OPFSBackend();
|
||||
}
|
||||
else if (this.config.storageBackend === 'indexeddb') {
|
||||
this.backend = new IndexedDBBackend();
|
||||
}
|
||||
else {
|
||||
@@ -5825,6 +5911,7 @@ exports.IndexedDBEngine = IndexedDBEngine;
|
||||
exports.MeSqlark = MeSqlark;
|
||||
exports.MemoryEngine = MemoryEngine;
|
||||
exports.MetonaSqlark = MetonaSqlark;
|
||||
exports.OPFSBackend = OPFSBackend;
|
||||
exports.OPFSEngine = OPFSEngine;
|
||||
exports.Table = Table;
|
||||
exports.VERSION = VERSION;
|
||||
|
||||
Reference in New Issue
Block a user