@fireproof/core 0.19.9-dev-frag → 0.19.11-dev-dryrun
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +27 -0
- package/{chunk-YS4GL6OK.js → chunk-MAK4D54P.js} +20 -6
- package/chunk-MAK4D54P.js.map +1 -0
- package/{chunk-JO5AVWG7.js → chunk-XINRLWR3.js} +14 -6
- package/chunk-XINRLWR3.js.map +1 -0
- package/deno.json +20 -0
- package/{gateway-IZRHJWPE.js → gateway-7OM6OSYK.js} +3 -4
- package/gateway-7OM6OSYK.js.map +1 -0
- package/{gateway-YSNUK2L3.js → gateway-VWWKLHUI.js} +4 -5
- package/{gateway-YSNUK2L3.js.map → gateway-VWWKLHUI.js.map} +1 -1
- package/index.cjs +549 -372
- package/index.cjs.map +1 -1
- package/index.d.cts +75 -51
- package/index.d.ts +75 -51
- package/index.global.js +21461 -15114
- package/index.global.js.map +1 -1
- package/index.js +465 -313
- package/index.js.map +1 -1
- package/{key-bag-file-NMEBFSPM.js → key-bag-file-DFMW6ZM6.js} +3 -3
- package/{key-bag-indexdb-X5V6GNBZ.js → key-bag-indexdb-R2RWGSQ4.js} +3 -3
- package/key-bag-indexdb-R2RWGSQ4.js.map +1 -0
- package/{mem-filesystem-B6C6QOIP.js → mem-filesystem-BZQZLUR6.js} +3 -3
- package/metafile-cjs.json +1 -1
- package/metafile-esm.json +1 -1
- package/metafile-iife.json +1 -1
- package/{node-filesystem-5JLBSHKQ.js → node-filesystem-7YZR3POJ.js} +8 -4
- package/node-filesystem-7YZR3POJ.js.map +1 -0
- package/package.json +8 -5
- package/tests/blockstore/keyed-crypto.test.ts +34 -4
- package/tests/blockstore/store.test.ts +18 -13
- package/tests/fireproof/all-gateway.test.ts +395 -0
- package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.car +0 -0
- package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js +316 -0
- package/tests/fireproof/convert_uint8.py +27 -0
- package/tests/fireproof/fireproof.test.ts +18 -18
- package/tests/fireproof/hello.test.ts +33 -19
- package/tests/helpers.ts +7 -4
- package/{utils-IZPK4QS7.js → utils-AISQB3PB.js} +3 -3
- package/chunk-JO5AVWG7.js.map +0 -1
- package/chunk-YS4GL6OK.js.map +0 -1
- package/gateway-IZRHJWPE.js.map +0 -1
- package/key-bag-indexdb-X5V6GNBZ.js.map +0 -1
- package/node-filesystem-5JLBSHKQ.js.map +0 -1
- /package/{key-bag-file-NMEBFSPM.js.map → key-bag-file-DFMW6ZM6.js.map} +0 -0
- /package/{mem-filesystem-B6C6QOIP.js.map → mem-filesystem-BZQZLUR6.js.map} +0 -0
- /package/{utils-IZPK4QS7.js.map → utils-AISQB3PB.js.map} +0 -0
package/README.md
CHANGED
@@ -128,6 +128,33 @@ globalThis[Symbol.for("FP_PRESET_ENV")] = {
|
|
128
128
|
};
|
129
129
|
```
|
130
130
|
|
131
|
+
### Log Formatting
|
132
|
+
|
133
|
+
It's possible to change the logformat by setting FP_FORMAT to:
|
134
|
+
|
135
|
+
- jsonice makes the log output in multiline json
|
136
|
+
- yaml makes the log output in yaml
|
137
|
+
- json makes the log output in singleline json (default)
|
138
|
+
|
139
|
+
### KeyBag
|
140
|
+
|
141
|
+
If you add `extractKey` with the value `_deprecated_internal_api` to the `FP_STORAGE_URL` url
|
142
|
+
you can bypass the security check to extract the key material. This is the default configuration,
|
143
|
+
but there is a warning emitted if you use this feature, and roadmap plans for more secure key management.
|
144
|
+
|
145
|
+
### Deno
|
146
|
+
|
147
|
+
Fireproof is compatible with Deno. To runit in Deno you need to add the following flags:
|
148
|
+
|
149
|
+
Currently the tests are not run with deno -- TODO
|
150
|
+
|
151
|
+
It might be that using our provided deno.json is somekind of odd
|
152
|
+
--- TODO is to add fireproof to jsr and deno.land
|
153
|
+
|
154
|
+
```shell
|
155
|
+
deno run --config node_modules/@fireproof/core/deno.json --allow-read --allow-write --allow-env --unstable-sloppy-imports ./node-test.ts
|
156
|
+
```
|
157
|
+
|
131
158
|
## Thanks 🙏
|
132
159
|
|
133
160
|
Fireproof is a synthesis of work done by people in the web community over the years. I couldn't even begin to name all the folks who made pivotal contributions. Without npm, React, and VS Code all this would have taken so much longer. Thanks to everyone who supported me getting into database development via Apache CouchDB, one of the original document databases. The distinguishing work on immutable data-structures comes from the years of consideration [IPFS](https://ipfs.tech), [IPLD](https://ipld.io), and the [Filecoin APIs](https://docs.filecoin.io) have enjoyed.
|
@@ -14,7 +14,9 @@ import {
|
|
14
14
|
URI,
|
15
15
|
runtimeFn,
|
16
16
|
envFactory,
|
17
|
-
toCryptoRuntime
|
17
|
+
toCryptoRuntime,
|
18
|
+
JSONFormatter,
|
19
|
+
YAMLFormatter
|
18
20
|
} from "@adviser/cement";
|
19
21
|
import { base58btc } from "multiformats/bases/base58";
|
20
22
|
var globalLogger = new LoggerImpl();
|
@@ -151,6 +153,21 @@ function ensureLogger(sthis, componentName, ctx) {
|
|
151
153
|
sthis.env.onSet(
|
152
154
|
(key, value) => {
|
153
155
|
switch (key) {
|
156
|
+
case "FP_FORMAT": {
|
157
|
+
switch (value) {
|
158
|
+
case "jsonice":
|
159
|
+
logger.SetFormatter(new JSONFormatter(logger.TxtEnDe(), 2));
|
160
|
+
break;
|
161
|
+
case "yaml":
|
162
|
+
logger.SetFormatter(new YAMLFormatter(logger.TxtEnDe(), 2));
|
163
|
+
break;
|
164
|
+
case "json":
|
165
|
+
default:
|
166
|
+
logger.SetFormatter(new JSONFormatter(logger.TxtEnDe()));
|
167
|
+
break;
|
168
|
+
}
|
169
|
+
break;
|
170
|
+
}
|
154
171
|
case "FP_DEBUG":
|
155
172
|
logger.SetDebug(value || []);
|
156
173
|
break;
|
@@ -159,6 +176,7 @@ function ensureLogger(sthis, componentName, ctx) {
|
|
159
176
|
break;
|
160
177
|
}
|
161
178
|
},
|
179
|
+
"FP_FORMAT",
|
162
180
|
"FP_DEBUG",
|
163
181
|
"FP_STACK"
|
164
182
|
);
|
@@ -204,9 +222,6 @@ function getName(sthis, url) {
|
|
204
222
|
}
|
205
223
|
return result;
|
206
224
|
}
|
207
|
-
function exception2Result(fn) {
|
208
|
-
return fn().then((value) => Result.Ok(value)).catch((e) => Result.Err(e));
|
209
|
-
}
|
210
225
|
async function exceptionWrapper(fn) {
|
211
226
|
return fn().catch((e) => Result.Err(e));
|
212
227
|
}
|
@@ -256,11 +271,10 @@ export {
|
|
256
271
|
getStore,
|
257
272
|
getKey,
|
258
273
|
getName,
|
259
|
-
exception2Result,
|
260
274
|
exceptionWrapper,
|
261
275
|
NotFoundError,
|
262
276
|
isNotFoundError,
|
263
277
|
dataDir,
|
264
278
|
UInt8ArrayEqual
|
265
279
|
};
|
266
|
-
//# sourceMappingURL=chunk-
|
280
|
+
//# sourceMappingURL=chunk-MAK4D54P.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/utils.ts"],"sourcesContent":["import {\n Logger,\n LoggerImpl,\n IsLogger,\n Result,\n ResolveOnce,\n isURL,\n URI,\n CoerceURI,\n runtimeFn,\n envFactory,\n Env,\n toCryptoRuntime,\n CryptoRuntime,\n JSONFormatter,\n YAMLFormatter,\n} from \"@adviser/cement\";\nimport { PathOps, StoreType, SuperThis, SuperThisOpts, TextEndeCoder } from \"./types\";\nimport { base58btc } from \"multiformats/bases/base58\";\n\nexport type { Logger };\nexport { Result };\n\nconst globalLogger: Logger = new LoggerImpl();\n\nconst registerFP_DEBUG = new ResolveOnce();\n\ninterface superThisOpts {\n readonly logger: Logger;\n readonly env: Env;\n readonly pathOps: PathOps;\n readonly crypto: CryptoRuntime;\n readonly ctx: Record<string, unknown>;\n readonly txt: TextEndeCoder;\n}\n\nclass superThis implements SuperThis {\n readonly logger: Logger;\n readonly env: Env;\n readonly pathOps: PathOps;\n readonly ctx: Record<string, unknown>;\n readonly txt: TextEndeCoder;\n readonly crypto: CryptoRuntime;\n\n constructor(opts: superThisOpts) {\n this.logger = opts.logger;\n this.env = opts.env;\n this.crypto = opts.crypto;\n this.pathOps = opts.pathOps;\n this.txt = opts.txt;\n this.ctx = { ...opts.ctx };\n // console.log(\"superThis\", this);\n }\n\n nextId(bytes = 6): { str: string; bin: Uint8Array } {\n const bin = this.crypto.randomBytes(bytes);\n return {\n str: base58btc.encode(bin),\n bin,\n };\n }\n\n start(): Promise<void> {\n return Promise.resolve();\n }\n\n clone(override: Partial<SuperThisOpts>): SuperThis {\n return new superThis({\n logger: override.logger || this.logger,\n env: envFactory(override.env) || this.env,\n crypto: override.crypto || this.crypto,\n pathOps: override.pathOps || this.pathOps,\n txt: override.txt || this.txt,\n ctx: { ...this.ctx, ...override.ctx },\n });\n }\n}\n\n// const pathOps =\nfunction presetEnv() {\n const penv = new Map([\n // [\"FP_DEBUG\", \"xxx\"],\n // [\"FP_ENV\", \"development\"],\n ...Array.from(\n Object.entries(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ((globalThis as any)[Symbol.for(\"FP_PRESET_ENV\")] || {}) as Record<string, string>,\n ),\n ), // .map(([k, v]) => [k, v as string])\n ]);\n // console.log(\">>>>>>\", penv)\n return penv;\n}\n// const envImpl = envFactory({\n// symbol: \"FP_ENV\",\n// presetEnv: presetEnv(),\n// });\nclass pathOpsImpl implements PathOps {\n join(...paths: string[]): string {\n return paths.map((i) => i.replace(/\\/+$/, \"\")).join(\"/\");\n }\n dirname(path: string) {\n return path.split(\"/\").slice(0, -1).join(\"/\");\n }\n // homedir() {\n // throw new Error(\"SysContainer:homedir is not available in seeded state\");\n // }\n}\nconst pathOps = new pathOpsImpl();\nconst txtOps = {\n encode: (input: string) => new TextEncoder().encode(input),\n decode: (input: Uint8Array) => new TextDecoder().decode(input),\n};\n\nexport function ensureSuperThis(osthis?: Partial<SuperThisOpts>): SuperThis {\n const env = envFactory({\n symbol: osthis?.env?.symbol || \"FP_ENV\",\n presetEnv: osthis?.env?.presetEnv || presetEnv(),\n });\n return new superThis({\n logger: osthis?.logger || globalLogger,\n env,\n crypto: osthis?.crypto || toCryptoRuntime(),\n ctx: osthis?.ctx || {},\n pathOps,\n txt: osthis?.txt || txtOps,\n });\n}\n\n// // eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport function ensureSuperLog(sthis: SuperThis, componentName: string, ctx?: Record<string, unknown>): SuperThis {\n return sthis.clone({\n logger: ensureLogger(sthis, componentName, ctx),\n });\n}\n\nexport function ensureLogger(\n sthis: SuperThis /* Partial<LoggerOpts> | Logger */,\n componentName: string,\n ctx?: Record<string, unknown>,\n): Logger {\n // if (!opts?.logger) {\n // throw new Error(\"logger is required\");\n // }\n let logger = globalLogger;\n if (IsLogger(sthis)) {\n logger = sthis;\n } else if (sthis && IsLogger(sthis.logger)) {\n logger = sthis.logger;\n }\n const cLogger = logger.With().Module(componentName); //.Str(\"this\", uuidv7());\n const debug: string[] = [];\n let exposeStack = false;\n if (ctx) {\n if (\"debug\" in ctx) {\n if (typeof ctx.debug === \"string\" && ctx.debug.length > 0) {\n debug.push(ctx.debug);\n } else {\n debug.push(componentName);\n }\n delete ctx.debug;\n }\n if (\"exposeStack\" in ctx) {\n exposeStack = true;\n delete ctx.exposeStack;\n }\n if (\"this\" in ctx) {\n cLogger.Str(\"this\", sthis.nextId(4).str);\n delete ctx.this;\n }\n for (const [key, value] of Object.entries(ctx)) {\n switch (typeof value) {\n case \"string\":\n cLogger.Str(key, value);\n break;\n case \"number\":\n cLogger.Uint64(key, value);\n break;\n default:\n if (value instanceof Date) {\n cLogger.Str(key, value.toISOString());\n } else if (isURL(value)) {\n cLogger.Str(key, value.toString());\n } else if (typeof value === \"function\") {\n cLogger.Ref(key, value);\n } else {\n cLogger.Any(key, value);\n }\n break;\n }\n }\n }\n registerFP_DEBUG\n .once(async () => {\n // console.log(\"registerFP_DEBUG\", SysContainer.env)\n sthis.env.onSet(\n (key, value) => {\n // console.log(\"FP_DEBUG\", key, value, debug)\n switch (key) {\n case \"FP_FORMAT\": {\n switch (value) {\n case \"jsonice\":\n logger.SetFormatter(new JSONFormatter(logger.TxtEnDe(), 2));\n break;\n case \"yaml\":\n logger.SetFormatter(new YAMLFormatter(logger.TxtEnDe(), 2));\n break;\n case \"json\":\n default:\n logger.SetFormatter(new JSONFormatter(logger.TxtEnDe()));\n break;\n }\n break;\n }\n case \"FP_DEBUG\":\n logger.SetDebug(value || []);\n break;\n case \"FP_STACK\":\n logger.SetExposeStack(!!value);\n break;\n }\n },\n \"FP_FORMAT\",\n \"FP_DEBUG\",\n \"FP_STACK\",\n );\n })\n .finally(() => {\n /* do nothing */\n });\n\n if (debug.length > 0) {\n logger.SetDebug(debug);\n }\n if (exposeStack) {\n logger.SetExposeStack(true);\n }\n const out = cLogger.Logger();\n // out.Debug().Msg(\"logger ready\");\n return out;\n}\n\nexport type Joiner = (...toJoin: string[]) => string;\n\nexport interface Store {\n readonly store: StoreType;\n readonly name: string;\n}\n\nexport function getStore(url: URI, sthis: SuperThis, joiner: Joiner): Store {\n const store = url.getParam(\"store\");\n switch (store) {\n case \"data\":\n case \"wal\":\n case \"meta\":\n break;\n default:\n throw sthis.logger.Error().Url(url).Msg(`store not found`).AsError();\n }\n let name: string = store;\n if (url.hasParam(\"index\")) {\n name = joiner(url.getParam(\"index\") || \"idx\", name);\n }\n return { store, name };\n}\n\nexport function getKey(url: URI, logger: Logger): string {\n const result = url.getParam(\"key\");\n if (!result) throw logger.Error().Str(\"url\", url.toString()).Msg(`key not found`).AsError();\n return result;\n}\n\nexport function getName(sthis: SuperThis, url: URI): string {\n let result = url.getParam(\"name\");\n if (!result) {\n result = sthis.pathOps.dirname(url.pathname);\n if (result.length === 0) {\n throw sthis.logger.Error().Str(\"url\", url.toString()).Msg(`name not found`).AsError();\n }\n }\n return result;\n}\n\n// export function exception2Result<T = void>(fn: () => Promise<T>): Promise<Result<T>> {\n// return fn()\n// .then((value) => Result.Ok(value))\n// .catch((e) => Result.Err(e));\n// }\n\nexport async function exceptionWrapper<T, E extends Error>(fn: () => Promise<Result<T, E>>): Promise<Result<T, E>> {\n return fn().catch((e) => Result.Err(e));\n}\n\n// // the big side effect party --- hate it\n// export function sanitizeURL(url: URL) {\n// url.searchParams.sort();\n// // const searchParams = Object.entries(url.searchParams).sort(([a], [b]) => a.localeCompare(b));\n// // console.log(\"searchParams\", searchParams);\n// // for (const [key] of searchParams) {\n// // url.searchParams.delete(key);\n// // }\n// // for (const [key, value] of searchParams) {\n// // url.searchParams.set(key, value);\n// // }\n// }\n\nexport class NotFoundError extends Error {\n readonly code = \"ENOENT\";\n}\n\nexport function isNotFoundError(e: Error | Result<unknown> | unknown): e is NotFoundError {\n if (Result.Is(e)) {\n if (e.isOk()) return false;\n e = e.Err();\n }\n if ((e as NotFoundError).code === \"ENOENT\") return true;\n return false;\n}\n\nexport function dataDir(sthis: SuperThis, name?: string, base?: CoerceURI): URI {\n if (!base) {\n if (!runtimeFn().isBrowser) {\n const home = sthis.env.get(\"HOME\") || \"./\";\n base = sthis.env.get(\"FP_STORAGE_URL\") || `file://${sthis.pathOps.join(home, \".fireproof\")}`;\n } else {\n base = sthis.env.get(\"FP_STORAGE_URL\") || `indexdb://fp`;\n }\n }\n return URI.from(base.toString())\n .build()\n .setParam(\"name\", name || \"\")\n .URI();\n}\n\nexport function UInt8ArrayEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) {\n return false;\n }\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) {\n return false;\n }\n }\n return true;\n}\n"],"mappings":";;;;;;;AAAA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,iBAAiB;AAK1B,IAAM,eAAuB,IAAI,WAAW;AAE5C,IAAM,mBAAmB,IAAI,YAAY;AAWzC,IAAM,YAAN,MAAM,WAA+B;AAAA,EAQnC,YAAY,MAAqB;AAC/B,SAAK,SAAS,KAAK;AACnB,SAAK,MA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,EAAlC;AAAA;AACL,SAAS,OAAO;AAAA;AAClB;AAEO,SAAS,gBAAgB,GAA0D;AACxF,MAAI,OAAO,GAAG,CAAC,GAAG;AAChB,QAAI,EAAE,KAAK,EAAG,QAAO;AACrB,QAAI,EAAE,IAAI;AAAA,EACZ;AACA,MAAK,EAAoB,SAAS,SAAU,QAAO;AACnD,SAAO;AACT;AAEO,SAAS,QAAQ,OAAkB,MAAe,MAAuB;AAC9E,MAAI,CAAC,MAAM;AACT,QAAI,CAAC,UAAU,EAAE,WAAW;AAC1B,YAAM,OAAO,MAAM,IAAI,IAAI,MAAM,KAAK;AACtC,aAAO,MAAM,IAAI,IAAI,gBAAgB,KAAK,UAAU,MAAM,QAAQ,KAAK,MAAM,YAAY,CAAC;AAAA,IAC5F,OAAO;AACL,aAAO,MAAM,IAAI,IAAI,gBAAgB,KAAK;AAAA,IAC5C;AAAA,EACF;AACA,SAAO,IAAI,KAAK,KAAK,SAAS,CAAC,EAC5B,MAAM,EACN,SAAS,QAAQ,QAAQ,EAAE,EAC3B,IAAI;AACT;AAEO,SAAS,gBAAgB,GAAe,GAAwB;AACrE,MAAI,EAAE,WAAW,EAAE,QAAQ;AACzB,WAAO;AAAA,EACT;AACA,WAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,QAAI,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG;AACjB,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import {
|
2
2
|
getStore
|
3
|
-
} from "./chunk-
|
3
|
+
} from "./chunk-MAK4D54P.js";
|
4
4
|
|
5
5
|
// src/runtime/gateways/file/utils.ts
|
6
6
|
async function getFileSystem(url) {
|
@@ -9,15 +9,23 @@ async function getFileSystem(url) {
|
|
9
9
|
switch (name) {
|
10
10
|
case "mem":
|
11
11
|
{
|
12
|
-
const { MemFileSystem } = await import("./mem-filesystem-
|
12
|
+
const { MemFileSystem } = await import("./mem-filesystem-BZQZLUR6.js");
|
13
13
|
fs = new MemFileSystem();
|
14
14
|
}
|
15
15
|
break;
|
16
|
-
case
|
16
|
+
// case 'deno': {
|
17
|
+
// const { DenoFileSystem } = await import("./deno-filesystem.js");
|
18
|
+
// fs = new DenoFileSystem();
|
19
|
+
// break;
|
20
|
+
// }
|
21
|
+
case "node": {
|
22
|
+
const { NodeFileSystem } = await import("./node-filesystem-7YZR3POJ.js");
|
23
|
+
fs = new NodeFileSystem();
|
24
|
+
break;
|
25
|
+
}
|
17
26
|
case "sys":
|
18
27
|
default: {
|
19
|
-
|
20
|
-
fs = new NodeFileSystem();
|
28
|
+
return getFileSystem(url.build().setParam("fs", "node").URI());
|
21
29
|
}
|
22
30
|
}
|
23
31
|
return fs.start();
|
@@ -64,4 +72,4 @@ export {
|
|
64
72
|
getFileName,
|
65
73
|
toArrayBuffer
|
66
74
|
};
|
67
|
-
//# sourceMappingURL=chunk-
|
75
|
+
//# sourceMappingURL=chunk-XINRLWR3.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/file/utils.ts"],"sourcesContent":["import { URI } from \"@adviser/cement\";\nimport { getStore } from \"../../../utils.js\";\nimport { SuperThis, SysFileSystem } from \"../../../types.js\";\n\nexport async function getFileSystem(url: URI): Promise<SysFileSystem> {\n const name = url.getParam(\"fs\");\n let fs: SysFileSystem;\n switch (name) {\n case \"mem\":\n {\n const { MemFileSystem } = await import(\"./mem-filesystem.js\");\n fs = new MemFileSystem();\n }\n break;\n // case 'deno': {\n // const { DenoFileSystem } = await import(\"./deno-filesystem.js\");\n // fs = new DenoFileSystem();\n // break;\n // }\n case \"node\": {\n const { NodeFileSystem } = await import(\"./node-filesystem.js\");\n fs = new NodeFileSystem();\n break;\n }\n case \"sys\":\n default: {\n // if (runtimeFn().isDeno) {\n // return getFileSystem(url.build().setParam(\"fs\", \"deno\").URI());\n // } else {\n return getFileSystem(url.build().setParam(\"fs\", \"node\").URI());\n // }\n }\n }\n return fs.start();\n}\n\nexport function getPath(url: URI, sthis: SuperThis): string {\n const basePath = url.pathname;\n // .toString()\n // .replace(new RegExp(`^${url.protocol}//`), \"\")\n // .replace(/\\?.*$/, \"\");\n const name = url.getParam(\"name\");\n if (name) {\n const version = url.getParam(\"version\");\n if (!version) throw sthis.logger.Error().Url(url).Msg(`version not found`).AsError();\n return sthis.pathOps.join(basePath, version, name);\n }\n return sthis.pathOps.join(basePath);\n}\n\nexport function getFileName(url: URI, sthis: SuperThis): string {\n const key = url.getParam(\"key\");\n if (!key) throw sthis.logger.Error().Url(url).Msg(`key not found`).AsError();\n const res = getStore(url, sthis, (...a: string[]) => a.join(\"-\"));\n switch (res.store) {\n case \"data\":\n return sthis.pathOps.join(res.name, key + \".car\");\n case \"wal\":\n case \"meta\":\n return sthis.pathOps.join(res.name, key + \".json\");\n default:\n throw sthis.logger.Error().Url(url).Msg(`unsupported store type`).AsError();\n }\n}\n\nexport function toArrayBuffer(buffer: Buffer | string): Uint8Array {\n if (typeof buffer === \"string\") {\n buffer = Buffer.from(buffer);\n }\n const ab = new ArrayBuffer(buffer.length);\n const view = new Uint8Array(ab);\n for (let i = 0; i < buffer.length; ++i) {\n view[i] = buffer[i];\n }\n return view;\n}\n"],"mappings":";;;;;AAIA,eAAsB,cAAc,KAAkC;AACpE,QAAM,OAAO,IAAI,SAAS,IAAI;AAC9B,MAAI;AACJ,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH;AACE,cAAM,EAAE,cAAc,IAAI,MAAM,OAAO,8BAAqB;AAC5D,aAAK,IAAI,cAAc;AAAA,MACzB;AACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMF,KAAK,QAAQ;AACX,YAAM,EAAE,eAAe,IAAI,MAAM,OAAO,+BAAsB;AAC9D,WAAK,IAAI,eAAe;AACxB;AAAA,IACF;AAAA,IACA,KAAK;AAAA,IACL,SAAS;AAIP,aAAO,cAAc,IAAI,MAAM,EAAE,SAAS,MAAM,MAA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C;AAAA,EACpB;AACA,SAAO;AACT;","names":[]}
|
package/deno.json
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"imports": {
|
3
|
+
"@fireproof/core": "./index.js",
|
4
|
+
"@adviser/cement": "npm:@adviser/cement@^0.2.31",
|
5
|
+
"multiformats": "npm:multiformats@^13.3.0",
|
6
|
+
"@ipld/unixfs": "npm:@ipld/unixfs@^3.0.0",
|
7
|
+
"@ipld/car": "npm:@ipld/car@^5.3.2",
|
8
|
+
"@ipld/dag-json": "npm:@ipld/dag-json@^10.2.2",
|
9
|
+
"@ipld/dag-cbor": "npm:@ipld/dag-cbor@^9.2.1",
|
10
|
+
"@web3-storage/pail": "npm:@web3-storage/pail@^0.6.0",
|
11
|
+
"cborg": "npm:cborg@^4.2.4",
|
12
|
+
"charwise": "npm:charwise@^3.0.1",
|
13
|
+
"prolly-trees": "npm:prolly-trees@^1.0.4",
|
14
|
+
"idb": "npm:idb@^8.0.0",
|
15
|
+
"ipfs-unixfs-exporter": "npm:ipfs-unixfs-exporter@^13.6.1",
|
16
|
+
"memfs": "npm:memfs@^4.12.0",
|
17
|
+
"p-limit": "npm:p-limit@^6.1.0",
|
18
|
+
"react": "npm:react@^18.3.1"
|
19
|
+
}
|
20
|
+
}
|
@@ -4,15 +4,14 @@ import {
|
|
4
4
|
import {
|
5
5
|
NotFoundError,
|
6
6
|
ensureLogger,
|
7
|
-
exception2Result,
|
8
7
|
exceptionWrapper,
|
9
8
|
getKey,
|
10
9
|
getStore
|
11
|
-
} from "./chunk-
|
10
|
+
} from "./chunk-MAK4D54P.js";
|
12
11
|
|
13
12
|
// src/runtime/gateways/indexdb/gateway.ts
|
14
13
|
import { openDB } from "idb";
|
15
|
-
import { KeyedResolvOnce, Result } from "@adviser/cement";
|
14
|
+
import { exception2Result, KeyedResolvOnce, Result } from "@adviser/cement";
|
16
15
|
function ensureVersion(url) {
|
17
16
|
return url.build().defParam("version", INDEXDB_VERSION).URI();
|
18
17
|
}
|
@@ -163,4 +162,4 @@ export {
|
|
163
162
|
IndexDBTestStore,
|
164
163
|
getIndexDBName
|
165
164
|
};
|
166
|
-
//# sourceMappingURL=gateway-
|
165
|
+
//# sourceMappingURL=gateway-7OM6OSYK.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/indexdb/gateway.ts"],"sourcesContent":["import { openDB, IDBPDatabase } from \"idb\";\nimport { exception2Result, KeyedResolvOnce, Logger, Result, URI } from \"@adviser/cement\";\n\nimport { INDEXDB_VERSION } from \"./version.js\";\nimport { ensureLogger, exceptionWrapper, getKey, getStore, NotFoundError } from \"../../../utils.js\";\nimport { Gateway, GetResult, 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): Promise<GetResult> {\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,kBAAkB,iBAAyB,cAAmB;AAOvE,SAAS,cAAc,KAAe;AACpC,SAAO,IAAI,MAAM,EAAE,SAAS,WAAW,eAAe,EAAE,IAAI;AAC9D;AAQA,IAAM,cAAc,IAAI,gBAAyB;AAEjD,SAAS,WAAW,KAA2C;AAC7D,MAAI,IAAI,WAAW,GAAG;AACpB,UAAM,IAAI,CAAC;AAAA,EACb;AACA,SAAO;AACT;AAEA,eAAe,WAAW,KAAU,OAAoC;AACtE,QAAM,SAAS,eAAe,KAAK,KAAK;AACxC,QAAM,OAAO,MAAM,YAAY,IAAI,OAAO,MAAM,EAAE,KAAK,YAAY;AACjE,UAAM,KAAK,MAAM,OAAO,OAAO,QAAQ,GAAG;AAAA,M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9
9
|
import {
|
10
10
|
NotFoundError,
|
11
11
|
ensureLogger,
|
12
|
-
exception2Result,
|
13
12
|
exceptionWrapper,
|
14
13
|
isNotFoundError
|
15
|
-
} from "./chunk-
|
14
|
+
} from "./chunk-MAK4D54P.js";
|
16
15
|
|
17
16
|
// src/runtime/gateways/file/gateway.ts
|
18
|
-
import { KeyedResolvOnce, Result } from "@adviser/cement";
|
17
|
+
import { exception2Result, KeyedResolvOnce, Result } from "@adviser/cement";
|
19
18
|
var versionFiles = new KeyedResolvOnce();
|
20
19
|
var FileGateway = class {
|
21
20
|
get fs() {
|
@@ -142,4 +141,4 @@ export {
|
|
142
141
|
FileGateway,
|
143
142
|
FileTestStore
|
144
143
|
};
|
145
|
-
//# sourceMappingURL=gateway-
|
144
|
+
//# sourceMappingURL=gateway-VWWKLHUI.js.map
|
@@ -1 +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,KAA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AAM,cAAc,GAAG,GAAG,SAAS,MAAM;AAC/D,SAAK,OAAO,MAAM,EAAE,IAAI,GAAG,EAAE,IAAI,UAAU,MAAM,EAAE,IAAI,MAAM,EAAE,IAAI,KAAK;AACxE,WAAO;AAAA,EACT;AACF;","names":[]}
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/file/gateway.ts"],"sourcesContent":["import { FILESTORE_VERSION } from \"./version.js\";\nimport { exception2Result, KeyedResolvOnce, Logger, Result, URI } from \"@adviser/cement\";\nimport { ensureLogger, 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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