@fireproof/core 0.19.8-dev-global → 0.19.9-dev-frag
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +7 -0
- package/chunk-7EWIAXTM.js +7 -0
- package/chunk-7EWIAXTM.js.map +1 -0
- package/chunk-JO5AVWG7.js +67 -0
- package/chunk-JO5AVWG7.js.map +1 -0
- package/chunk-PB4BKL4O.js +7 -0
- package/chunk-PB4BKL4O.js.map +1 -0
- package/chunk-YS4GL6OK.js +266 -0
- package/chunk-YS4GL6OK.js.map +1 -0
- package/{store-indexdb-WLRSICCB.js → gateway-IZRHJWPE.js} +48 -80
- package/gateway-IZRHJWPE.js.map +1 -0
- package/gateway-YSNUK2L3.js +145 -0
- package/gateway-YSNUK2L3.js.map +1 -0
- package/index.cjs +2132 -1783
- package/index.cjs.map +1 -1
- package/index.d.cts +613 -513
- package/index.d.ts +613 -513
- package/index.global.js +19366 -20107
- package/index.global.js.map +1 -1
- package/index.js +1512 -1022
- package/index.js.map +1 -1
- package/key-bag-file-NMEBFSPM.js +54 -0
- package/key-bag-file-NMEBFSPM.js.map +1 -0
- package/key-bag-indexdb-X5V6GNBZ.js +50 -0
- package/key-bag-indexdb-X5V6GNBZ.js.map +1 -0
- package/mem-filesystem-B6C6QOIP.js +41 -0
- package/mem-filesystem-B6C6QOIP.js.map +1 -0
- package/metafile-cjs.json +1 -1
- package/metafile-esm.json +1 -1
- package/metafile-iife.json +1 -1
- package/node-filesystem-5JLBSHKQ.js +41 -0
- package/node-filesystem-5JLBSHKQ.js.map +1 -0
- package/package.json +8 -7
- package/tests/blockstore/fragment-gateway.test.ts +107 -0
- package/tests/blockstore/keyed-crypto.test.ts +302 -0
- package/tests/blockstore/loader.test.ts +24 -19
- package/tests/blockstore/store.test.ts +34 -28
- package/tests/blockstore/transaction.test.ts +19 -15
- package/tests/fireproof/config.test.ts +94 -78
- package/tests/fireproof/crdt.test.ts +34 -28
- package/tests/fireproof/database.test.ts +22 -14
- package/tests/fireproof/fireproof.test.fixture.ts +133 -0
- package/tests/fireproof/fireproof.test.ts +331 -219
- package/tests/fireproof/hello.test.ts +6 -4
- package/tests/fireproof/indexer.test.ts +34 -27
- package/tests/fireproof/utils.test.ts +65 -0
- package/tests/helpers.ts +25 -57
- package/utils-IZPK4QS7.js +14 -0
- package/utils-IZPK4QS7.js.map +1 -0
- package/chunk-BNL4PVBF.js +0 -314
- package/chunk-BNL4PVBF.js.map +0 -1
- package/chunk-JW2QT6BF.js +0 -184
- package/chunk-JW2QT6BF.js.map +0 -1
- package/node-sys-container-MIEX6ELJ.js +0 -29
- package/node-sys-container-MIEX6ELJ.js.map +0 -1
- package/store-file-VJ6BI4II.js +0 -191
- package/store-file-VJ6BI4II.js.map +0 -1
- package/store-indexdb-WLRSICCB.js.map +0 -1
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/indexdb/gateway.ts"],"sourcesContent":["import { openDB, IDBPDatabase } from \"idb\";\nimport { KeyedResolvOnce, Logger, Result, URI } from \"@adviser/cement\";\n\nimport { INDEXDB_VERSION } from \"./version.js\";\nimport { ensureLogger, exception2Result, exceptionWrapper, getKey, getStore, NotFoundError } from \"../../../utils.js\";\nimport { Gateway, TestGateway } from \"../../../blockstore/gateway.js\";\nimport { SuperThis } from \"../../../types.js\";\n\nfunction ensureVersion(url: URI): URI {\n return url.build().defParam(\"version\", INDEXDB_VERSION).URI();\n}\n\ninterface IDBConn {\n readonly db: IDBPDatabase<unknown>;\n readonly dbName: DbName;\n readonly version: string;\n readonly url: URI;\n}\nconst onceIndexDB = new KeyedResolvOnce<IDBConn>();\n\nfunction sanitzeKey(key: string | string[]): string | string[] {\n if (key.length === 1) {\n key = key[0];\n }\n return key;\n}\n\nasync function connectIdb(url: URI, sthis: SuperThis): Promise<IDBConn> {\n const dbName = getIndexDBName(url, sthis);\n const once = await onceIndexDB.get(dbName.fullDb).once(async () => {\n const db = await openDB(dbName.fullDb, 1, {\n upgrade(db) {\n [\"version\", \"data\", \"wal\", \"meta\", \"idx.data\", \"idx.wal\", \"idx.meta\"].map((store) => {\n db.createObjectStore(store, {\n autoIncrement: false,\n });\n });\n },\n });\n const found = await db.get(\"version\", \"version\");\n const version = ensureVersion(url).getParam(\"version\") as string;\n if (!found) {\n await db.put(\"version\", { version }, \"version\");\n } else if (found.version !== version) {\n sthis.logger.Warn().Str(\"url\", url.toString()).Str(\"version\", version).Str(\"found\", found.version).Msg(\"version mismatch\");\n }\n return { db, dbName, version, url };\n });\n return {\n ...once,\n url: url.build().setParam(\"version\", once.version).URI(),\n };\n}\n\nexport interface DbName {\n readonly fullDb: string;\n readonly objStore: string;\n readonly connectionKey: string;\n readonly dbName: string;\n}\n\nfunction joinDBName(...names: string[]): string {\n return names\n .map((i) => i.replace(/^[^a-zA-Z0-9]+/g, \"\").replace(/[^a-zA-Z0-9]+/g, \"_\"))\n .filter((i) => i.length)\n .join(\".\");\n}\n\n// const schemaVersion = new Map<string, number>();\nexport function getIndexDBName(iurl: URI, sthis: SuperThis): DbName {\n const url = ensureVersion(iurl);\n const fullDb = url.pathname.replace(/^\\/+/, \"\").replace(/\\?.*$/, \"\"); // cut leading slashes\n // const type = getStore(url);\n // const storageVersion = url.searchParams.get(\"version\");\n // not nice but we need to pass the version to the db name\n // url.searchParams.set(\"version\", storageVersion);\n // console.log(\"getIndexDBName:\", url.toString(), { fullDb, type, branch });\n // const dbName = fullDb.replace(new RegExp(`^fp.${storageVersion}.`), \"\"); // cut fp prefix\n const dbName = url.getParam(\"name\");\n if (!dbName) throw sthis.logger.Error().Str(\"url\", url.toString()).Msg(`name not found`).AsError();\n const result = joinDBName(fullDb, dbName);\n const objStore = getStore(url, sthis, joinDBName).name;\n const connectionKey = [result, objStore].join(\":\");\n return {\n fullDb: result,\n objStore,\n connectionKey,\n dbName,\n };\n}\n\nexport class IndexDBGateway implements Gateway {\n readonly logger: Logger;\n readonly sthis: SuperThis;\n constructor(sthis: SuperThis) {\n this.logger = ensureLogger(sthis, \"IndexDBGateway\");\n this.sthis = sthis;\n }\n _db: IDBPDatabase<unknown> = {} as IDBPDatabase<unknown>;\n\n async start(baseURL: URI): Promise<Result<URI>> {\n return exception2Result(async () => {\n this.logger.Debug().Url(baseURL).Msg(\"starting\");\n await this.sthis.start();\n const ic = await connectIdb(baseURL, this.sthis);\n this._db = ic.db;\n this.logger.Debug().Url(ic.url).Msg(\"started\");\n return ic.url;\n });\n }\n async close(): Promise<Result<void>> {\n return Result.Ok(undefined);\n }\n async destroy(baseUrl: URI): Promise<Result<void>> {\n return exception2Result(async () => {\n // return deleteDB(getIndexDBName(this.url).fullDb);\n const type = getStore(baseUrl, this.sthis, joinDBName).name;\n // console.log(\"IndexDBDataStore:destroy\", type);\n const idb = this._db;\n const trans = idb.transaction(type, \"readwrite\");\n const object_store = trans.objectStore(type);\n const toDelete = [];\n for (let cursor = await object_store.openCursor(); cursor; cursor = await cursor.continue()) {\n toDelete.push(cursor.primaryKey);\n }\n for (const key of toDelete) {\n await trans.db.delete(type, key);\n }\n await trans.done;\n });\n }\n\n buildUrl(baseUrl: URI, key: string): Promise<Result<URI>> {\n return Promise.resolve(Result.Ok(baseUrl.build().setParam(\"key\", key).URI()));\n }\n\n async get(url: URI) {\n return exceptionWrapper(async () => {\n const key = getKey(url, this.logger);\n const store = getStore(url, this.sthis, joinDBName).name;\n this.logger.Debug().Url(url).Str(\"key\", key).Str(\"store\", store).Msg(\"getting\");\n const tx = this._db.transaction([store], \"readonly\");\n const bytes = await tx.objectStore(store).get(sanitzeKey(key));\n await tx.done;\n if (!bytes) {\n return Result.Err(new NotFoundError(`missing ${key}`));\n }\n return Result.Ok(bytes as Uint8Array);\n });\n }\n async put(url: URI, value: Uint8Array) {\n return exception2Result(async () => {\n const key = getKey(url, this.logger);\n const store = getStore(url, this.sthis, joinDBName).name;\n this.logger.Debug().Url(url).Str(\"key\", key).Str(\"store\", store).Msg(\"putting\");\n const tx = this._db.transaction([store], \"readwrite\");\n await tx.objectStore(store).put(value, sanitzeKey(key));\n await tx.done;\n });\n }\n async delete(url: URI) {\n return exception2Result(async () => {\n const key = getKey(url, this.logger);\n const store = getStore(url, this.sthis, joinDBName).name;\n this.logger.Debug().Url(url).Str(\"key\", key).Str(\"store\", store).Msg(\"deleting\");\n const tx = this._db.transaction([store], \"readwrite\");\n await tx.objectStore(store).delete(sanitzeKey(key));\n await tx.done;\n return Result.Ok(undefined);\n });\n }\n}\n\n// export class IndexDBDataGateway extends IndexDBGateway {\n// readonly storeType = \"data\";\n// constructor(logger: Logger) {\n// super(ensureLogger(logger, \"IndexDBDataGateway\"));\n// }\n// }\n\n// export class IndexDBWalGateway extends IndexDBGateway {\n// readonly storeType = \"wal\";\n// constructor(logger: Logger) {\n// super(ensureLogger(logger, \"IndexDBWalGateway\"));\n// }\n// }\n// export class IndexDBMetaGateway extends IndexDBGateway {\n// readonly storeType = \"meta\";\n// constructor(logger: Logger) {\n// super(ensureLogger(logger, \"IndexDBMetaGateway\"));\n// }\n// }\n\nexport class IndexDBTestStore implements TestGateway {\n readonly logger: Logger;\n readonly sthis: SuperThis;\n constructor(sthis: SuperThis) {\n this.sthis = sthis;\n this.logger = ensureLogger(sthis, \"IndexDBTestStore\", {});\n }\n async get(url: URI, key: string) {\n const ic = await connectIdb(url, this.sthis);\n const store = getStore(ic.url, this.sthis, joinDBName).name;\n this.logger.Debug().Str(\"key\", key).Str(\"store\", store).Msg(\"getting\");\n let bytes = await ic.db.get(store, sanitzeKey(key));\n this.logger.Debug().Str(\"key\", key).Str(\"store\", store).Int(\"len\", bytes.length).Msg(\"got\");\n if (typeof bytes === \"string\") {\n bytes = this.sthis.txt.encode(bytes);\n }\n return bytes as Uint8Array;\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAAA,SAAS,cAA4B;AACrC,SAAS,iBAAyB,cAAmB;AAOrD,SAAS,cAAc,KAAe;AACpC,SAAO,IAAI,MAAM,EAAE,SAAS,WAAW,eAAe,EAAE,IAAI;AAC9D;AAQA,IAAM,cAAc,IAAI,gBAAyB;AAEjD,SAAS,WAA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AM,EAAE,IAAI,KAAK;AAC1F,QAAI,OAAO,UAAU,UAAU;AAC7B,cAAQ,KAAK,MAAM,IAAI,OAAO,KAAK;AAAA,IACrC;AACA,WAAO;AAAA,EACT;AACF;","names":["db"]}
|
@@ -0,0 +1,145 @@
|
|
1
|
+
import {
|
2
|
+
FILESTORE_VERSION
|
3
|
+
} from "./chunk-7EWIAXTM.js";
|
4
|
+
import {
|
5
|
+
getFileName,
|
6
|
+
getFileSystem,
|
7
|
+
getPath
|
8
|
+
} from "./chunk-JO5AVWG7.js";
|
9
|
+
import {
|
10
|
+
NotFoundError,
|
11
|
+
ensureLogger,
|
12
|
+
exception2Result,
|
13
|
+
exceptionWrapper,
|
14
|
+
isNotFoundError
|
15
|
+
} from "./chunk-YS4GL6OK.js";
|
16
|
+
|
17
|
+
// src/runtime/gateways/file/gateway.ts
|
18
|
+
import { KeyedResolvOnce, Result } from "@adviser/cement";
|
19
|
+
var versionFiles = new KeyedResolvOnce();
|
20
|
+
var FileGateway = class {
|
21
|
+
get fs() {
|
22
|
+
if (!this._fs) throw this.logger.Error().Msg("fs not initialized").AsError();
|
23
|
+
return this._fs;
|
24
|
+
}
|
25
|
+
constructor(sthis) {
|
26
|
+
this.sthis = sthis;
|
27
|
+
this.logger = sthis.logger;
|
28
|
+
}
|
29
|
+
async getVersionFromFile(path, logger) {
|
30
|
+
return versionFiles.get(path).once(async () => {
|
31
|
+
await this.fs.mkdir(path, { recursive: true });
|
32
|
+
const vFile = this.sthis.pathOps.join(path, "version");
|
33
|
+
const vFileStat = await this.fs.stat(vFile).catch(() => void 0);
|
34
|
+
if (!vFileStat) {
|
35
|
+
await this.fs.writefile(this.sthis.pathOps.join(path, "version"), FILESTORE_VERSION);
|
36
|
+
return FILESTORE_VERSION;
|
37
|
+
} else if (!vFileStat.isFile()) {
|
38
|
+
throw logger.Error().Str("file", vFile).Msg(`version file is a directory`).AsError();
|
39
|
+
}
|
40
|
+
const v = await this.fs.readfile(vFile);
|
41
|
+
const vStr = new TextDecoder().decode(v);
|
42
|
+
if (vStr !== FILESTORE_VERSION) {
|
43
|
+
logger.Warn().Str("file", vFile).Str("from", vStr).Str("expected", FILESTORE_VERSION).Msg(`version mismatch`);
|
44
|
+
}
|
45
|
+
return vStr;
|
46
|
+
});
|
47
|
+
}
|
48
|
+
start(baseURL) {
|
49
|
+
return exception2Result(async () => {
|
50
|
+
this._fs = await getFileSystem(baseURL);
|
51
|
+
await this.fs.start();
|
52
|
+
const url = baseURL.build();
|
53
|
+
url.defParam("version", FILESTORE_VERSION);
|
54
|
+
const dbUrl = await this.buildUrl(url.URI(), "dummy");
|
55
|
+
const dbdirFile = this.getFilePath(dbUrl.Ok());
|
56
|
+
await this.fs.mkdir(this.sthis.pathOps.dirname(dbdirFile), { recursive: true });
|
57
|
+
const dbroot = this.sthis.pathOps.dirname(dbdirFile);
|
58
|
+
this.logger.Debug().Url(url.URI()).Str("dbroot", dbroot).Msg("start");
|
59
|
+
url.setParam("version", await this.getVersionFromFile(dbroot, this.logger));
|
60
|
+
return url.URI();
|
61
|
+
});
|
62
|
+
}
|
63
|
+
async buildUrl(baseUrl, key) {
|
64
|
+
return Result.Ok(baseUrl.build().setParam("key", key).URI());
|
65
|
+
}
|
66
|
+
async close() {
|
67
|
+
return Result.Ok(void 0);
|
68
|
+
}
|
69
|
+
// abstract buildUrl(baseUrl: URL, key: string): Promise<Result<URL>>;
|
70
|
+
getFilePath(url) {
|
71
|
+
const key = url.getParam("key");
|
72
|
+
if (!key) throw this.logger.Error().Url(url).Msg(`key not found`).AsError();
|
73
|
+
return this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));
|
74
|
+
}
|
75
|
+
async put(url, body) {
|
76
|
+
return exception2Result(async () => {
|
77
|
+
const file = await this.getFilePath(url);
|
78
|
+
this.logger.Debug().Str("url", url.toString()).Str("file", file).Msg("put");
|
79
|
+
await this.fs.writefile(file, body);
|
80
|
+
});
|
81
|
+
}
|
82
|
+
async get(url) {
|
83
|
+
return exceptionWrapper(async () => {
|
84
|
+
const file = this.getFilePath(url);
|
85
|
+
try {
|
86
|
+
const res = await this.fs.readfile(file);
|
87
|
+
this.logger.Debug().Url(url.asURL()).Str("file", file).Msg("get");
|
88
|
+
return Result.Ok(new Uint8Array(res));
|
89
|
+
} catch (e) {
|
90
|
+
if (isNotFoundError(e)) {
|
91
|
+
return Result.Err(new NotFoundError(`file not found: ${file}`));
|
92
|
+
}
|
93
|
+
return Result.Err(e);
|
94
|
+
}
|
95
|
+
});
|
96
|
+
}
|
97
|
+
async delete(url) {
|
98
|
+
return exception2Result(async () => {
|
99
|
+
await this.fs.unlink(this.getFilePath(url));
|
100
|
+
});
|
101
|
+
}
|
102
|
+
async destroy(baseURL) {
|
103
|
+
const url = await this.buildUrl(baseURL, "x");
|
104
|
+
if (url.isErr()) return url;
|
105
|
+
const filepath = this.sthis.pathOps.dirname(this.getFilePath(url.Ok()));
|
106
|
+
let files = [];
|
107
|
+
try {
|
108
|
+
files = await this.fs.readdir(filepath);
|
109
|
+
} catch (e) {
|
110
|
+
if (!isNotFoundError(e)) {
|
111
|
+
throw this.logger.Error().Err(e).Str("dir", filepath).Msg("destroy:readdir").AsError();
|
112
|
+
}
|
113
|
+
}
|
114
|
+
for (const file of files) {
|
115
|
+
const pathed = this.sthis.pathOps.join(filepath, file);
|
116
|
+
try {
|
117
|
+
await this.fs.unlink(pathed);
|
118
|
+
} catch (e) {
|
119
|
+
if (!isNotFoundError(e)) {
|
120
|
+
throw this.logger.Error().Err(e).Str("file", pathed).Msg("destroy:unlink").AsError();
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return Result.Ok(void 0);
|
125
|
+
}
|
126
|
+
};
|
127
|
+
var FileTestStore = class {
|
128
|
+
constructor(sthis) {
|
129
|
+
this.logger = ensureLogger(sthis, "FileTestStore");
|
130
|
+
this.sthis = sthis;
|
131
|
+
}
|
132
|
+
async get(iurl, key) {
|
133
|
+
const url = iurl.build().setParam("key", key).URI();
|
134
|
+
const dbFile = this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));
|
135
|
+
this.logger.Debug().Url(url).Str("dbFile", dbFile).Msg("get");
|
136
|
+
const buffer = await (await getFileSystem(url)).readfile(dbFile);
|
137
|
+
this.logger.Debug().Url(url).Str("dbFile", dbFile).Len(buffer).Msg("got");
|
138
|
+
return buffer;
|
139
|
+
}
|
140
|
+
};
|
141
|
+
export {
|
142
|
+
FileGateway,
|
143
|
+
FileTestStore
|
144
|
+
};
|
145
|
+
//# sourceMappingURL=gateway-YSNUK2L3.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/file/gateway.ts"],"sourcesContent":["import { FILESTORE_VERSION } from \"./version.js\";\nimport { KeyedResolvOnce, Logger, Result, URI } from \"@adviser/cement\";\nimport { ensureLogger, exception2Result, exceptionWrapper, isNotFoundError, NotFoundError } from \"../../../utils.js\";\nimport { Gateway, GetResult, TestGateway } from \"../../../blockstore/gateway.js\";\nimport { getFileName, getFileSystem, getPath } from \"./utils.js\";\nimport { SuperThis, SysFileSystem } from \"../../../types.js\";\n\nconst versionFiles = new KeyedResolvOnce<string>();\n\nexport class FileGateway implements Gateway {\n // abstract readonly storeType: StoreType;\n readonly logger: Logger;\n readonly sthis: SuperThis;\n\n _fs?: SysFileSystem;\n\n get fs(): SysFileSystem {\n if (!this._fs) throw this.logger.Error().Msg(\"fs not initialized\").AsError();\n return this._fs;\n }\n\n constructor(sthis: SuperThis) {\n this.sthis = sthis;\n this.logger = sthis.logger;\n }\n\n async getVersionFromFile(path: string, logger: Logger): Promise<string> {\n return versionFiles.get(path).once(async () => {\n await this.fs.mkdir(path, { recursive: true });\n const vFile = this.sthis.pathOps.join(path, \"version\");\n const vFileStat = await this.fs.stat(vFile).catch(() => undefined);\n if (!vFileStat) {\n await this.fs.writefile(this.sthis.pathOps.join(path, \"version\"), FILESTORE_VERSION);\n return FILESTORE_VERSION;\n } else if (!vFileStat.isFile()) {\n throw logger.Error().Str(\"file\", vFile).Msg(`version file is a directory`).AsError();\n }\n const v = await this.fs.readfile(vFile);\n const vStr = new TextDecoder().decode(v);\n if (vStr !== FILESTORE_VERSION) {\n logger.Warn().Str(\"file\", vFile).Str(\"from\", vStr).Str(\"expected\", FILESTORE_VERSION).Msg(`version mismatch`);\n }\n return vStr;\n });\n }\n\n start(baseURL: URI): Promise<Result<URI>> {\n return exception2Result(async () => {\n this._fs = await getFileSystem(baseURL);\n await this.fs.start();\n const url = baseURL.build();\n url.defParam(\"version\", FILESTORE_VERSION);\n // url.defParam(\"store\", this.storeType);\n const dbUrl = await this.buildUrl(url.URI(), \"dummy\");\n const dbdirFile = this.getFilePath(dbUrl.Ok());\n await this.fs.mkdir(this.sthis.pathOps.dirname(dbdirFile), { recursive: true });\n const dbroot = this.sthis.pathOps.dirname(dbdirFile);\n this.logger.Debug().Url(url.URI()).Str(\"dbroot\", dbroot).Msg(\"start\");\n url.setParam(\"version\", await this.getVersionFromFile(dbroot, this.logger));\n return url.URI();\n });\n }\n\n async buildUrl(baseUrl: URI, key: string): Promise<Result<URI>> {\n return Result.Ok(baseUrl.build().setParam(\"key\", key).URI());\n }\n\n async close(): Promise<Result<void>> {\n return Result.Ok(undefined);\n }\n // abstract buildUrl(baseUrl: URL, key: string): Promise<Result<URL>>;\n\n getFilePath(url: URI): string {\n const key = url.getParam(\"key\");\n if (!key) throw this.logger.Error().Url(url).Msg(`key not found`).AsError();\n return this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));\n }\n\n async put(url: URI, body: Uint8Array): Promise<Result<void>> {\n return exception2Result(async () => {\n const file = await this.getFilePath(url);\n this.logger.Debug().Str(\"url\", url.toString()).Str(\"file\", file).Msg(\"put\");\n await this.fs.writefile(file, body);\n });\n }\n\n async get(url: URI): Promise<GetResult> {\n return exceptionWrapper(async () => {\n const file = this.getFilePath(url);\n try {\n const res = await this.fs.readfile(file);\n this.logger.Debug().Url(url.asURL()).Str(\"file\", file).Msg(\"get\");\n return Result.Ok(new Uint8Array(res));\n } catch (e: unknown) {\n // this.logger.Error().Err(e).Str(\"file\", file).Msg(\"get\");\n if (isNotFoundError(e)) {\n return Result.Err(new NotFoundError(`file not found: ${file}`));\n }\n return Result.Err(e as Error);\n }\n });\n }\n\n async delete(url: URI): Promise<Result<void>> {\n return exception2Result(async () => {\n await this.fs.unlink(this.getFilePath(url));\n });\n }\n\n async destroy(baseURL: URI): Promise<Result<void>> {\n const url = await this.buildUrl(baseURL, \"x\");\n if (url.isErr()) return url;\n const filepath = this.sthis.pathOps.dirname(this.getFilePath(url.Ok()));\n let files: string[] = [];\n try {\n files = await this.fs.readdir(filepath);\n } catch (e: unknown) {\n if (!isNotFoundError(e)) {\n throw this.logger.Error().Err(e).Str(\"dir\", filepath).Msg(\"destroy:readdir\").AsError();\n }\n }\n for (const file of files) {\n const pathed = this.sthis.pathOps.join(filepath, file);\n try {\n await this.fs.unlink(pathed);\n } catch (e: unknown) {\n if (!isNotFoundError(e)) {\n throw this.logger.Error().Err(e).Str(\"file\", pathed).Msg(\"destroy:unlink\").AsError();\n }\n }\n }\n return Result.Ok(undefined);\n }\n}\n\n// export class FileWALGateway extends FileGateway {\n// readonly storeType = \"wal\";\n// constructor(logger: Logger) {\n// super(ensureLogger(logger, \"FileWALGateway\"));\n// }\n// }\n\n// export class FileMetaGateway extends FileGateway {\n// readonly storeType = \"meta\";\n// constructor(logger: Logger) {\n// super(ensureLogger(logger, \"FileMetaGateway\"));\n// }\n// }\n\n// export class FileDataGateway extends FileGateway {\n// readonly storeType = \"data\";\n// readonly branches = new Set<string>();\n// constructor(logger: Logger) {\n// // console.log(\"FileDataGateway->\", logger);\n// super(ensureLogger(logger, \"FileDataGateway\"));\n// }\n// }\n\nexport class FileTestStore implements TestGateway {\n readonly logger: Logger;\n readonly sthis: SuperThis;\n constructor(sthis: SuperThis) {\n this.logger = ensureLogger(sthis, \"FileTestStore\");\n this.sthis = sthis;\n }\n\n async get(iurl: URI, key: string) {\n const url = iurl.build().setParam(\"key\", key).URI();\n const dbFile = this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));\n this.logger.Debug().Url(url).Str(\"dbFile\", dbFile).Msg(\"get\");\n const buffer = await (await getFileSystem(url)).readfile(dbFile);\n this.logger.Debug().Url(url).Str(\"dbFile\", dbFile).Len(buffer).Msg(\"got\");\n return buffer;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AACA,SAAS,iBAAyB,cAAmB;AAMrD,IAAM,eAAe,IAAI,gBAAwB;AAE1C,IAAM,cAAN,MAAqC;AAAA,EAO1C,IAAI,KAAoB;AACtB,QAAI,CAAC,KAAK,IAAK,OAAM,KAAK,OAAO,MAAM,EAAE,IAAI,oBAAoB,EAAE,QAAQ;AAC3E,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,YAAY,OAAkB;AAC5B,SAAK,QAAQ;AACb,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,MAAM,mBAAmB,MAAc,QAAiC;AACtE,WAAO,aAAa,IAAI,IAAI,EAAE,KAAK,YAAY;AAC7C,YAAM,KAAK,GAAG,MAAM,MAAM,EAAE,WAAW,KAAK,CAAC;AAC7C,YAAM,QAAQ,KAAK,MAAM,QAAQ,KAAK,MAAM,SAAS;AACrD,YAAM,YAAY,MAAM,KAAK,GAAG,KAAK,KAAK,EAAE,MAAM,MAAM,MAAS;AACjE,UAAI,CAAC,WAAW;AACd,cAAM,KAAK,GAAG,UAAU,KAAK,MAAM,QAAQ,KAAK,MAAM,SAAS,GAAG,iBAAiB;AACnF,eAAO;AAAA,MACT,WAAW,CAAC,UAAU,OAAO,GAAG;AAC9B,cAAM,OAAO,MAAM,EAAE,IAAI,QAAQ,KAAK,EAAE,IAAI,6BAA6B,EAAE,QAAQ;AAAA,MACrF;AACA,YAAM,IAAI,MAAM,KAAK,GAAG,SAAS,KAAK;AACtC,YAAM,OAAO,IAAI,YAAY,EAAE,OAAO,CAAC;AACvC,UA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