@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
package/README.md
CHANGED
@@ -121,6 +121,13 @@ if you are in the browser you can use the following code to set the debug level:
|
|
121
121
|
this[Symbol.for("FP_ENV")].set("FP_DEBUG", "*");
|
122
122
|
```
|
123
123
|
|
124
|
+
```js
|
125
|
+
// vitest pass env
|
126
|
+
globalThis[Symbol.for("FP_PRESET_ENV")] = {
|
127
|
+
FP_DEBUG: "*",
|
128
|
+
};
|
129
|
+
```
|
130
|
+
|
124
131
|
## Thanks 🙏
|
125
132
|
|
126
133
|
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.
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/file/version.ts"],"sourcesContent":["export const FILESTORE_VERSION = \"v0.19-file\";\n"],"mappings":";AAAO,IAAM,oBAAoB;","names":[]}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
import {
|
2
|
+
getStore
|
3
|
+
} from "./chunk-YS4GL6OK.js";
|
4
|
+
|
5
|
+
// src/runtime/gateways/file/utils.ts
|
6
|
+
async function getFileSystem(url) {
|
7
|
+
const name = url.getParam("fs");
|
8
|
+
let fs;
|
9
|
+
switch (name) {
|
10
|
+
case "mem":
|
11
|
+
{
|
12
|
+
const { MemFileSystem } = await import("./mem-filesystem-B6C6QOIP.js");
|
13
|
+
fs = new MemFileSystem();
|
14
|
+
}
|
15
|
+
break;
|
16
|
+
case "node":
|
17
|
+
case "sys":
|
18
|
+
default: {
|
19
|
+
const { NodeFileSystem } = await import("./node-filesystem-5JLBSHKQ.js");
|
20
|
+
fs = new NodeFileSystem();
|
21
|
+
}
|
22
|
+
}
|
23
|
+
return fs.start();
|
24
|
+
}
|
25
|
+
function getPath(url, sthis) {
|
26
|
+
const basePath = url.pathname;
|
27
|
+
const name = url.getParam("name");
|
28
|
+
if (name) {
|
29
|
+
const version = url.getParam("version");
|
30
|
+
if (!version) throw sthis.logger.Error().Url(url).Msg(`version not found`).AsError();
|
31
|
+
return sthis.pathOps.join(basePath, version, name);
|
32
|
+
}
|
33
|
+
return sthis.pathOps.join(basePath);
|
34
|
+
}
|
35
|
+
function getFileName(url, sthis) {
|
36
|
+
const key = url.getParam("key");
|
37
|
+
if (!key) throw sthis.logger.Error().Url(url).Msg(`key not found`).AsError();
|
38
|
+
const res = getStore(url, sthis, (...a) => a.join("-"));
|
39
|
+
switch (res.store) {
|
40
|
+
case "data":
|
41
|
+
return sthis.pathOps.join(res.name, key + ".car");
|
42
|
+
case "wal":
|
43
|
+
case "meta":
|
44
|
+
return sthis.pathOps.join(res.name, key + ".json");
|
45
|
+
default:
|
46
|
+
throw sthis.logger.Error().Url(url).Msg(`unsupported store type`).AsError();
|
47
|
+
}
|
48
|
+
}
|
49
|
+
function toArrayBuffer(buffer) {
|
50
|
+
if (typeof buffer === "string") {
|
51
|
+
buffer = Buffer.from(buffer);
|
52
|
+
}
|
53
|
+
const ab = new ArrayBuffer(buffer.length);
|
54
|
+
const view = new Uint8Array(ab);
|
55
|
+
for (let i = 0; i < buffer.length; ++i) {
|
56
|
+
view[i] = buffer[i];
|
57
|
+
}
|
58
|
+
return view;
|
59
|
+
}
|
60
|
+
|
61
|
+
export {
|
62
|
+
getFileSystem,
|
63
|
+
getPath,
|
64
|
+
getFileName,
|
65
|
+
toArrayBuffer
|
66
|
+
};
|
67
|
+
//# sourceMappingURL=chunk-JO5AVWG7.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 \"node\":\n case \"sys\":\n default: {\n const { NodeFileSystem } = await import(\"./node-filesystem.js\");\n fs = new NodeFileSystem();\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) {\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,IACF,KAAK;AAAA,IACL,KAAK;AAAA,IACL,SAAS;AACP,YAAM,EAAE,eAAe,IAAI,MAAM,OAAO,+BAAsB;AAC9D,WAAK,IAAI,eAAe;AAAA,IAC1B;AAAA,EACF;AACA,SAAO,GAAG,MAAM;AAClB;AAEO,SAAS,QAAQ,KAAU,OAA0B;AAC1D,QAAM,WAAW,IAAI;AAIrB,QAAM,OAAO,IAAI,SAAS,MAAM;AAChC,MAAI,MAAM;AACR,UAAM,UAAU,IAAI,SAAS,SAAS;AACtC,QAAI,CAAC,QAAS,OAAM,MAAM,OAAO,MAAM,EAAE,IAAI,GAAG,EAAE,IAAI,mBAAmB,EAAE,QAAQ;AACnF,WAAO,MAAM,QAAQ,KAAK,UAAU,SAAS,IAAI;AAAA,EACnD;AACA,SAAO,MAAM,QAAQ,KAAK,QAAQ;AACpC;AAEO,SAAS,YAAY,KAAU,OAA0B;AAC9D,QAAM,MAAM,IAAI,SAAS,KAAK;AAC9B,MAAI,CAAC,IAAK,OAAM,MAAM,OAAO,MAAM,EAAE,IAAI,GAAG,EAAE,IAAI,eAAe,EAAE,QAAQ;AAC3E,QAAM,MAAM,SAAS,KAAK,OAAO,IAAI,MAAgB,EAAE,KAAK,GAAG,CAAC;AAChE,UAAQ,IAAI,OAAO;AAAA,IACjB,KAAK;AACH,aAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,MAAM,MAAM;AAAA,IAClD,KAAK;AAAA,IACL,KAAK;AACH,aAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,MAAM,OAAO;AAAA,IACnD;AACE,YAAM,MAAM,OAAO,MAAM,EAAE,IAAI,GAAG,EAAE,IAAI,wBAAwB,EAAE,QAAQ;AAAA,EAC9E;AACF;AAEO,SAAS,cAAc,QAAyB;AACrD,MAAI,OAAO,WAAW,UAAU;AAC9B,aAAS,OAAO,KAAK,MAAM;AAAA,EAC7B;AACA,QAAM,KAAK,IAAI,YAAY,OAAO,MAAM;AACxC,QAAM,OAAO,IAAI,WAAW,EAAE;AAC9B,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,EAAE,GAAG;AACtC,SAAK,CAAC,IAAI,OAAO,CAAC;AAAA,EACpB;AACA,SAAO;AACT;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/gateways/indexdb/version.ts"],"sourcesContent":["export const INDEXDB_VERSION = \"v0.19-indexdb\";\n"],"mappings":";AAAO,IAAM,kBAAkB;","names":[]}
|
@@ -0,0 +1,266 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __export = (target, all) => {
|
3
|
+
for (var name in all)
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
5
|
+
};
|
6
|
+
|
7
|
+
// src/utils.ts
|
8
|
+
import {
|
9
|
+
LoggerImpl,
|
10
|
+
IsLogger,
|
11
|
+
Result,
|
12
|
+
ResolveOnce,
|
13
|
+
isURL,
|
14
|
+
URI,
|
15
|
+
runtimeFn,
|
16
|
+
envFactory,
|
17
|
+
toCryptoRuntime
|
18
|
+
} from "@adviser/cement";
|
19
|
+
import { base58btc } from "multiformats/bases/base58";
|
20
|
+
var globalLogger = new LoggerImpl();
|
21
|
+
var registerFP_DEBUG = new ResolveOnce();
|
22
|
+
var superThis = class _superThis {
|
23
|
+
constructor(opts) {
|
24
|
+
this.logger = opts.logger;
|
25
|
+
this.env = opts.env;
|
26
|
+
this.crypto = opts.crypto;
|
27
|
+
this.pathOps = opts.pathOps;
|
28
|
+
this.txt = opts.txt;
|
29
|
+
this.ctx = { ...opts.ctx };
|
30
|
+
}
|
31
|
+
nextId(bytes = 6) {
|
32
|
+
const bin = this.crypto.randomBytes(bytes);
|
33
|
+
return {
|
34
|
+
str: base58btc.encode(bin),
|
35
|
+
bin
|
36
|
+
};
|
37
|
+
}
|
38
|
+
start() {
|
39
|
+
return Promise.resolve();
|
40
|
+
}
|
41
|
+
clone(override) {
|
42
|
+
return new _superThis({
|
43
|
+
logger: override.logger || this.logger,
|
44
|
+
env: envFactory(override.env) || this.env,
|
45
|
+
crypto: override.crypto || this.crypto,
|
46
|
+
pathOps: override.pathOps || this.pathOps,
|
47
|
+
txt: override.txt || this.txt,
|
48
|
+
ctx: { ...this.ctx, ...override.ctx }
|
49
|
+
});
|
50
|
+
}
|
51
|
+
};
|
52
|
+
function presetEnv() {
|
53
|
+
const penv = new Map([
|
54
|
+
// ["FP_DEBUG", "xxx"],
|
55
|
+
// ["FP_ENV", "development"],
|
56
|
+
...Array.from(
|
57
|
+
Object.entries(
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
59
|
+
globalThis[Symbol.for("FP_PRESET_ENV")] || {}
|
60
|
+
)
|
61
|
+
)
|
62
|
+
// .map(([k, v]) => [k, v as string])
|
63
|
+
]);
|
64
|
+
return penv;
|
65
|
+
}
|
66
|
+
var pathOpsImpl = class {
|
67
|
+
join(...paths) {
|
68
|
+
return paths.map((i) => i.replace(/\/+$/, "")).join("/");
|
69
|
+
}
|
70
|
+
dirname(path) {
|
71
|
+
return path.split("/").slice(0, -1).join("/");
|
72
|
+
}
|
73
|
+
// homedir() {
|
74
|
+
// throw new Error("SysContainer:homedir is not available in seeded state");
|
75
|
+
// }
|
76
|
+
};
|
77
|
+
var pathOps = new pathOpsImpl();
|
78
|
+
var txtOps = {
|
79
|
+
encode: (input) => new TextEncoder().encode(input),
|
80
|
+
decode: (input) => new TextDecoder().decode(input)
|
81
|
+
};
|
82
|
+
function ensureSuperThis(osthis) {
|
83
|
+
const env = envFactory({
|
84
|
+
symbol: osthis?.env?.symbol || "FP_ENV",
|
85
|
+
presetEnv: osthis?.env?.presetEnv || presetEnv()
|
86
|
+
});
|
87
|
+
return new superThis({
|
88
|
+
logger: osthis?.logger || globalLogger,
|
89
|
+
env,
|
90
|
+
crypto: osthis?.crypto || toCryptoRuntime(),
|
91
|
+
ctx: osthis?.ctx || {},
|
92
|
+
pathOps,
|
93
|
+
txt: osthis?.txt || txtOps
|
94
|
+
});
|
95
|
+
}
|
96
|
+
function ensureSuperLog(sthis, componentName, ctx) {
|
97
|
+
return sthis.clone({
|
98
|
+
logger: ensureLogger(sthis, componentName, ctx)
|
99
|
+
});
|
100
|
+
}
|
101
|
+
function ensureLogger(sthis, componentName, ctx) {
|
102
|
+
let logger = globalLogger;
|
103
|
+
if (IsLogger(sthis)) {
|
104
|
+
logger = sthis;
|
105
|
+
} else if (sthis && IsLogger(sthis.logger)) {
|
106
|
+
logger = sthis.logger;
|
107
|
+
}
|
108
|
+
const cLogger = logger.With().Module(componentName);
|
109
|
+
const debug = [];
|
110
|
+
let exposeStack = false;
|
111
|
+
if (ctx) {
|
112
|
+
if ("debug" in ctx) {
|
113
|
+
if (typeof ctx.debug === "string" && ctx.debug.length > 0) {
|
114
|
+
debug.push(ctx.debug);
|
115
|
+
} else {
|
116
|
+
debug.push(componentName);
|
117
|
+
}
|
118
|
+
delete ctx.debug;
|
119
|
+
}
|
120
|
+
if ("exposeStack" in ctx) {
|
121
|
+
exposeStack = true;
|
122
|
+
delete ctx.exposeStack;
|
123
|
+
}
|
124
|
+
if ("this" in ctx) {
|
125
|
+
cLogger.Str("this", sthis.nextId(4).str);
|
126
|
+
delete ctx.this;
|
127
|
+
}
|
128
|
+
for (const [key, value] of Object.entries(ctx)) {
|
129
|
+
switch (typeof value) {
|
130
|
+
case "string":
|
131
|
+
cLogger.Str(key, value);
|
132
|
+
break;
|
133
|
+
case "number":
|
134
|
+
cLogger.Uint64(key, value);
|
135
|
+
break;
|
136
|
+
default:
|
137
|
+
if (value instanceof Date) {
|
138
|
+
cLogger.Str(key, value.toISOString());
|
139
|
+
} else if (isURL(value)) {
|
140
|
+
cLogger.Str(key, value.toString());
|
141
|
+
} else if (typeof value === "function") {
|
142
|
+
cLogger.Ref(key, value);
|
143
|
+
} else {
|
144
|
+
cLogger.Any(key, value);
|
145
|
+
}
|
146
|
+
break;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
registerFP_DEBUG.once(async () => {
|
151
|
+
sthis.env.onSet(
|
152
|
+
(key, value) => {
|
153
|
+
switch (key) {
|
154
|
+
case "FP_DEBUG":
|
155
|
+
logger.SetDebug(value || []);
|
156
|
+
break;
|
157
|
+
case "FP_STACK":
|
158
|
+
logger.SetExposeStack(!!value);
|
159
|
+
break;
|
160
|
+
}
|
161
|
+
},
|
162
|
+
"FP_DEBUG",
|
163
|
+
"FP_STACK"
|
164
|
+
);
|
165
|
+
}).finally(() => {
|
166
|
+
});
|
167
|
+
if (debug.length > 0) {
|
168
|
+
logger.SetDebug(debug);
|
169
|
+
}
|
170
|
+
if (exposeStack) {
|
171
|
+
logger.SetExposeStack(true);
|
172
|
+
}
|
173
|
+
const out = cLogger.Logger();
|
174
|
+
return out;
|
175
|
+
}
|
176
|
+
function getStore(url, sthis, joiner) {
|
177
|
+
const store = url.getParam("store");
|
178
|
+
switch (store) {
|
179
|
+
case "data":
|
180
|
+
case "wal":
|
181
|
+
case "meta":
|
182
|
+
break;
|
183
|
+
default:
|
184
|
+
throw sthis.logger.Error().Url(url).Msg(`store not found`).AsError();
|
185
|
+
}
|
186
|
+
let name = store;
|
187
|
+
if (url.hasParam("index")) {
|
188
|
+
name = joiner(url.getParam("index") || "idx", name);
|
189
|
+
}
|
190
|
+
return { store, name };
|
191
|
+
}
|
192
|
+
function getKey(url, logger) {
|
193
|
+
const result = url.getParam("key");
|
194
|
+
if (!result) throw logger.Error().Str("url", url.toString()).Msg(`key not found`).AsError();
|
195
|
+
return result;
|
196
|
+
}
|
197
|
+
function getName(sthis, url) {
|
198
|
+
let result = url.getParam("name");
|
199
|
+
if (!result) {
|
200
|
+
result = sthis.pathOps.dirname(url.pathname);
|
201
|
+
if (result.length === 0) {
|
202
|
+
throw sthis.logger.Error().Str("url", url.toString()).Msg(`name not found`).AsError();
|
203
|
+
}
|
204
|
+
}
|
205
|
+
return result;
|
206
|
+
}
|
207
|
+
function exception2Result(fn) {
|
208
|
+
return fn().then((value) => Result.Ok(value)).catch((e) => Result.Err(e));
|
209
|
+
}
|
210
|
+
async function exceptionWrapper(fn) {
|
211
|
+
return fn().catch((e) => Result.Err(e));
|
212
|
+
}
|
213
|
+
var NotFoundError = class extends Error {
|
214
|
+
constructor() {
|
215
|
+
super(...arguments);
|
216
|
+
this.code = "ENOENT";
|
217
|
+
}
|
218
|
+
};
|
219
|
+
function isNotFoundError(e) {
|
220
|
+
if (Result.Is(e)) {
|
221
|
+
if (e.isOk()) return false;
|
222
|
+
e = e.Err();
|
223
|
+
}
|
224
|
+
if (e.code === "ENOENT") return true;
|
225
|
+
return false;
|
226
|
+
}
|
227
|
+
function dataDir(sthis, name, base) {
|
228
|
+
if (!base) {
|
229
|
+
if (!runtimeFn().isBrowser) {
|
230
|
+
const home = sthis.env.get("HOME") || "./";
|
231
|
+
base = sthis.env.get("FP_STORAGE_URL") || `file://${sthis.pathOps.join(home, ".fireproof")}`;
|
232
|
+
} else {
|
233
|
+
base = sthis.env.get("FP_STORAGE_URL") || `indexdb://fp`;
|
234
|
+
}
|
235
|
+
}
|
236
|
+
return URI.from(base.toString()).build().setParam("name", name || "").URI();
|
237
|
+
}
|
238
|
+
function UInt8ArrayEqual(a, b) {
|
239
|
+
if (a.length !== b.length) {
|
240
|
+
return false;
|
241
|
+
}
|
242
|
+
for (let i = 0; i < a.length; i++) {
|
243
|
+
if (a[i] !== b[i]) {
|
244
|
+
return false;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
return true;
|
248
|
+
}
|
249
|
+
|
250
|
+
export {
|
251
|
+
__export,
|
252
|
+
Result,
|
253
|
+
ensureSuperThis,
|
254
|
+
ensureSuperLog,
|
255
|
+
ensureLogger,
|
256
|
+
getStore,
|
257
|
+
getKey,
|
258
|
+
getName,
|
259
|
+
exception2Result,
|
260
|
+
exceptionWrapper,
|
261
|
+
NotFoundError,
|
262
|
+
isNotFoundError,
|
263
|
+
dataDir,
|
264
|
+
UInt8ArrayEqual
|
265
|
+
};
|
266
|
+
//# sourceMappingURL=chunk-YS4GL6OK.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} 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_DEBUG\":\n logger.SetDebug(value || []);\n break;\n case \"FP_STACK\":\n logger.SetExposeStack(!!value);\n break;\n }\n },\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\nexport 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) {\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,OAEK;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,MAAM,KAAK;AAChB,SAAK,SAAS,KAAK;AACnB,SAAK,UAAU,KAAK;AACpB,SAAK,MAAM,KAAK;AAChB,SAAK,MAAM,EAAE,GAAG,KAAK,IAAI;AAAA,EAE3B;AAAA,EAEA,OAAO,QAAQ,GAAqC;AAClD,UAAM,MAAM,KAAK,OAAO,YAAY,KAAK;AACzC,WAAO;AAAA,MACL,KAAK,UAAU,OAAO,GAAG;AAAA,MACzB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,QAAuB;AACrB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEA,MAAM,UAA6C;AACjD,WA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,KAAK,SAAS,CAAC,EAC5B,MAAM,EACN,SAAS,QAAQ,QAAQ,EAAE,EAC3B,IAAI;AACT;AAEO,SAAS,gBAAgB,GAAe,GAAe;AAC5D,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,23 +1,20 @@
|
|
1
1
|
import {
|
2
|
-
INDEXDB_VERSION
|
2
|
+
INDEXDB_VERSION
|
3
|
+
} from "./chunk-PB4BKL4O.js";
|
4
|
+
import {
|
3
5
|
NotFoundError,
|
4
6
|
ensureLogger,
|
5
7
|
exception2Result,
|
6
8
|
exceptionWrapper,
|
7
9
|
getKey,
|
8
10
|
getStore
|
9
|
-
} from "./chunk-
|
10
|
-
import {
|
11
|
-
SysContainer
|
12
|
-
} from "./chunk-JW2QT6BF.js";
|
11
|
+
} from "./chunk-YS4GL6OK.js";
|
13
12
|
|
14
|
-
// src/runtime/
|
13
|
+
// src/runtime/gateways/indexdb/gateway.ts
|
15
14
|
import { openDB } from "idb";
|
16
15
|
import { KeyedResolvOnce, Result } from "@adviser/cement";
|
17
16
|
function ensureVersion(url) {
|
18
|
-
|
19
|
-
ret.searchParams.set("version", url.searchParams.get("version") || INDEXDB_VERSION);
|
20
|
-
return ret;
|
17
|
+
return url.build().defParam("version", INDEXDB_VERSION).URI();
|
21
18
|
}
|
22
19
|
var onceIndexDB = new KeyedResolvOnce();
|
23
20
|
function sanitzeKey(key) {
|
@@ -26,8 +23,8 @@ function sanitzeKey(key) {
|
|
26
23
|
}
|
27
24
|
return key;
|
28
25
|
}
|
29
|
-
async function connectIdb(url,
|
30
|
-
const dbName = getIndexDBName(url,
|
26
|
+
async function connectIdb(url, sthis) {
|
27
|
+
const dbName = getIndexDBName(url, sthis);
|
31
28
|
const once = await onceIndexDB.get(dbName.fullDb).once(async () => {
|
32
29
|
const db = await openDB(dbName.fullDb, 1, {
|
33
30
|
upgrade(db2) {
|
@@ -39,27 +36,29 @@ async function connectIdb(url, logger) {
|
|
39
36
|
}
|
40
37
|
});
|
41
38
|
const found = await db.get("version", "version");
|
42
|
-
const version = url.
|
39
|
+
const version = ensureVersion(url).getParam("version");
|
43
40
|
if (!found) {
|
44
41
|
await db.put("version", { version }, "version");
|
45
42
|
} else if (found.version !== version) {
|
46
|
-
logger.Warn().Str("url", url.toString()).Str("version", version).Str("found", found.version).Msg("version mismatch");
|
43
|
+
sthis.logger.Warn().Str("url", url.toString()).Str("version", version).Str("found", found.version).Msg("version mismatch");
|
47
44
|
}
|
48
|
-
return { db, dbName, version };
|
45
|
+
return { db, dbName, version, url };
|
49
46
|
});
|
50
|
-
|
51
|
-
|
47
|
+
return {
|
48
|
+
...once,
|
49
|
+
url: url.build().setParam("version", once.version).URI()
|
50
|
+
};
|
52
51
|
}
|
53
52
|
function joinDBName(...names) {
|
54
53
|
return names.map((i) => i.replace(/^[^a-zA-Z0-9]+/g, "").replace(/[^a-zA-Z0-9]+/g, "_")).filter((i) => i.length).join(".");
|
55
54
|
}
|
56
|
-
function getIndexDBName(iurl,
|
55
|
+
function getIndexDBName(iurl, sthis) {
|
57
56
|
const url = ensureVersion(iurl);
|
58
57
|
const fullDb = url.pathname.replace(/^\/+/, "").replace(/\?.*$/, "");
|
59
|
-
const dbName = url.
|
60
|
-
if (!dbName) throw logger.Error().Str("url", url.toString()).Msg(`name not found`).AsError();
|
58
|
+
const dbName = url.getParam("name");
|
59
|
+
if (!dbName) throw sthis.logger.Error().Str("url", url.toString()).Msg(`name not found`).AsError();
|
61
60
|
const result = joinDBName(fullDb, dbName);
|
62
|
-
const objStore = getStore(url,
|
61
|
+
const objStore = getStore(url, sthis, joinDBName).name;
|
63
62
|
const connectionKey = [result, objStore].join(":");
|
64
63
|
return {
|
65
64
|
fullDb: result,
|
@@ -69,19 +68,19 @@ function getIndexDBName(iurl, logger) {
|
|
69
68
|
};
|
70
69
|
}
|
71
70
|
var IndexDBGateway = class {
|
72
|
-
constructor(
|
73
|
-
this.
|
74
|
-
this.logger =
|
75
|
-
|
76
|
-
idb() {
|
77
|
-
this.db;
|
71
|
+
constructor(sthis) {
|
72
|
+
this._db = {};
|
73
|
+
this.logger = ensureLogger(sthis, "IndexDBGateway");
|
74
|
+
this.sthis = sthis;
|
78
75
|
}
|
79
76
|
async start(baseURL) {
|
80
77
|
return exception2Result(async () => {
|
81
78
|
this.logger.Debug().Url(baseURL).Msg("starting");
|
82
|
-
await
|
83
|
-
|
84
|
-
this.
|
79
|
+
await this.sthis.start();
|
80
|
+
const ic = await connectIdb(baseURL, this.sthis);
|
81
|
+
this._db = ic.db;
|
82
|
+
this.logger.Debug().Url(ic.url).Msg("started");
|
83
|
+
return ic.url;
|
85
84
|
});
|
86
85
|
}
|
87
86
|
async close() {
|
@@ -89,8 +88,8 @@ var IndexDBGateway = class {
|
|
89
88
|
}
|
90
89
|
async destroy(baseUrl) {
|
91
90
|
return exception2Result(async () => {
|
92
|
-
const type = getStore(baseUrl, this.
|
93
|
-
const idb = this.
|
91
|
+
const type = getStore(baseUrl, this.sthis, joinDBName).name;
|
92
|
+
const idb = this._db;
|
94
93
|
const trans = idb.transaction(type, "readwrite");
|
95
94
|
const object_store = trans.objectStore(type);
|
96
95
|
const toDelete = [];
|
@@ -103,12 +102,15 @@ var IndexDBGateway = class {
|
|
103
102
|
await trans.done;
|
104
103
|
});
|
105
104
|
}
|
105
|
+
buildUrl(baseUrl, key) {
|
106
|
+
return Promise.resolve(Result.Ok(baseUrl.build().setParam("key", key).URI()));
|
107
|
+
}
|
106
108
|
async get(url) {
|
107
109
|
return exceptionWrapper(async () => {
|
108
110
|
const key = getKey(url, this.logger);
|
109
|
-
const store = getStore(url, this.
|
111
|
+
const store = getStore(url, this.sthis, joinDBName).name;
|
110
112
|
this.logger.Debug().Url(url).Str("key", key).Str("store", store).Msg("getting");
|
111
|
-
const tx = this.
|
113
|
+
const tx = this._db.transaction([store], "readonly");
|
112
114
|
const bytes = await tx.objectStore(store).get(sanitzeKey(key));
|
113
115
|
await tx.done;
|
114
116
|
if (!bytes) {
|
@@ -120,9 +122,9 @@ var IndexDBGateway = class {
|
|
120
122
|
async put(url, value) {
|
121
123
|
return exception2Result(async () => {
|
122
124
|
const key = getKey(url, this.logger);
|
123
|
-
const store = getStore(url, this.
|
125
|
+
const store = getStore(url, this.sthis, joinDBName).name;
|
124
126
|
this.logger.Debug().Url(url).Str("key", key).Str("store", store).Msg("putting");
|
125
|
-
const tx = this.
|
127
|
+
const tx = this._db.transaction([store], "readwrite");
|
126
128
|
await tx.objectStore(store).put(value, sanitzeKey(key));
|
127
129
|
await tx.done;
|
128
130
|
});
|
@@ -130,69 +132,35 @@ var IndexDBGateway = class {
|
|
130
132
|
async delete(url) {
|
131
133
|
return exception2Result(async () => {
|
132
134
|
const key = getKey(url, this.logger);
|
133
|
-
const store = getStore(url, this.
|
135
|
+
const store = getStore(url, this.sthis, joinDBName).name;
|
134
136
|
this.logger.Debug().Url(url).Str("key", key).Str("store", store).Msg("deleting");
|
135
|
-
const tx = this.
|
137
|
+
const tx = this._db.transaction([store], "readwrite");
|
136
138
|
await tx.objectStore(store).delete(sanitzeKey(key));
|
137
139
|
await tx.done;
|
138
140
|
return Result.Ok(void 0);
|
139
141
|
});
|
140
142
|
}
|
141
143
|
};
|
142
|
-
var IndexDBDataGateway = class extends IndexDBGateway {
|
143
|
-
constructor(logger) {
|
144
|
-
super(ensureLogger(logger, "IndexDBDataGateway", {}));
|
145
|
-
}
|
146
|
-
buildUrl(baseUrl, key) {
|
147
|
-
const url = new URL(baseUrl.toString());
|
148
|
-
url.searchParams.set("key", key);
|
149
|
-
return Promise.resolve(Result.Ok(url));
|
150
|
-
}
|
151
|
-
};
|
152
|
-
var IndexDBWalGateway = class extends IndexDBGateway {
|
153
|
-
constructor(logger) {
|
154
|
-
super(ensureLogger(logger, "IndexDBWalGateway", {}));
|
155
|
-
}
|
156
|
-
buildUrl(baseUrl, key) {
|
157
|
-
const url = new URL(baseUrl.toString());
|
158
|
-
url.searchParams.set("key", key);
|
159
|
-
return Promise.resolve(Result.Ok(url));
|
160
|
-
}
|
161
|
-
};
|
162
|
-
var IndexDBMetaGateway = class extends IndexDBGateway {
|
163
|
-
constructor(logger) {
|
164
|
-
super(ensureLogger(logger, "IndexDBDataGateway", {}));
|
165
|
-
this.branches = /* @__PURE__ */ new Set();
|
166
|
-
}
|
167
|
-
async buildUrl(baseUrl, key) {
|
168
|
-
const url = new URL(baseUrl.toString());
|
169
|
-
this.branches.add(key);
|
170
|
-
url.searchParams.set("key", key);
|
171
|
-
return Result.Ok(url);
|
172
|
-
}
|
173
|
-
};
|
174
|
-
var txtEncoder = new TextEncoder();
|
175
144
|
var IndexDBTestStore = class {
|
176
|
-
constructor(
|
177
|
-
this.
|
145
|
+
constructor(sthis) {
|
146
|
+
this.sthis = sthis;
|
147
|
+
this.logger = ensureLogger(sthis, "IndexDBTestStore", {});
|
178
148
|
}
|
179
149
|
async get(url, key) {
|
180
|
-
const
|
181
|
-
const store = getStore(url, this.
|
150
|
+
const ic = await connectIdb(url, this.sthis);
|
151
|
+
const store = getStore(ic.url, this.sthis, joinDBName).name;
|
182
152
|
this.logger.Debug().Str("key", key).Str("store", store).Msg("getting");
|
183
|
-
let bytes = await db.get(store, sanitzeKey(key));
|
153
|
+
let bytes = await ic.db.get(store, sanitzeKey(key));
|
184
154
|
this.logger.Debug().Str("key", key).Str("store", store).Int("len", bytes.length).Msg("got");
|
185
155
|
if (typeof bytes === "string") {
|
186
|
-
bytes =
|
156
|
+
bytes = this.sthis.txt.encode(bytes);
|
187
157
|
}
|
188
158
|
return bytes;
|
189
159
|
}
|
190
160
|
};
|
191
161
|
export {
|
192
|
-
|
193
|
-
IndexDBMetaGateway,
|
162
|
+
IndexDBGateway,
|
194
163
|
IndexDBTestStore,
|
195
|
-
IndexDBWalGateway,
|
196
164
|
getIndexDBName
|
197
165
|
};
|
198
|
-
//# sourceMappingURL=
|
166
|
+
//# sourceMappingURL=gateway-IZRHJWPE.js.map
|