@fireproof/core 0.19.121-dev → 0.19.122
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/deno.json +3 -3
- package/index.cjs +600 -1004
- package/index.cjs.map +1 -1
- package/index.d.cts +26 -60
- package/index.d.ts +26 -60
- package/index.js +392 -97
- package/index.js.map +1 -1
- package/metafile-cjs.json +1 -1
- package/metafile-esm.json +1 -1
- package/node/index.cjs +374 -0
- package/node/index.cjs.map +1 -0
- package/node/index.d.cts +43 -0
- package/node/index.d.ts +43 -0
- package/node/index.js +279 -0
- package/node/index.js.map +1 -0
- package/node/{mem-filesystem.js → mem-filesystem-LPPT7QV5.js} +1 -1
- package/node/metafile-cjs.json +1 -1
- package/node/metafile-esm.json +1 -1
- package/package.json +17 -15
- package/react/index.cjs +91 -26
- package/react/index.cjs.map +1 -1
- package/react/index.d.cts +16 -1
- package/react/index.d.ts +16 -1
- package/react/index.js +92 -27
- package/react/index.js.map +1 -1
- package/react/metafile-cjs.json +1 -1
- package/react/metafile-esm.json +1 -1
- package/tests/blockstore/keyed-crypto.test.ts +4 -2
- package/tests/fireproof/config.test.ts +133 -142
- package/web/{gateway-impl.cjs → index.cjs} +58 -9
- package/web/index.cjs.map +1 -0
- package/web/index.d.cts +44 -0
- package/web/index.d.ts +44 -0
- package/web/{gateway-impl.js → index.js} +52 -5
- package/web/index.js.map +1 -0
- package/web/metafile-cjs.json +1 -1
- package/web/metafile-esm.json +1 -1
- package/chunk-7EWIAXTM.js +0 -7
- package/chunk-7EWIAXTM.js.map +0 -1
- package/chunk-F4FC6B2T.js +0 -63
- package/chunk-F4FC6B2T.js.map +0 -1
- package/chunk-PZ5AY32C.js +0 -10
- package/chunk-PZ5AY32C.js.map +0 -1
- package/chunk-RXC4JGJT.js +0 -301
- package/chunk-RXC4JGJT.js.map +0 -1
- package/gateway-C62S56GY.js +0 -66
- package/gateway-C62S56GY.js.map +0 -1
- package/gateway-VVS4QWDA.js +0 -145
- package/gateway-VVS4QWDA.js.map +0 -1
- package/key-bag-file-PWZ3QE7B.js +0 -55
- package/key-bag-file-PWZ3QE7B.js.map +0 -1
- package/key-bag-indexdb-SYG3YD4D.js +0 -51
- package/key-bag-indexdb-SYG3YD4D.js.map +0 -1
- package/node/mem-filesystem.cjs +0 -72
- package/node/mem-filesystem.cjs.map +0 -1
- package/node/mem-filesystem.d.cts +0 -25
- package/node/mem-filesystem.d.ts +0 -25
- package/node/node-filesystem.cjs +0 -86
- package/node/node-filesystem.cjs.map +0 -1
- package/node/node-filesystem.d.cts +0 -35
- package/node/node-filesystem.d.ts +0 -35
- package/node/node-filesystem.js +0 -44
- package/node/node-filesystem.js.map +0 -1
- package/utils-ZVVGAXFE.js +0 -13
- package/utils-ZVVGAXFE.js.map +0 -1
- package/web/gateway-impl.cjs.map +0 -1
- package/web/gateway-impl.d.cts +0 -31
- package/web/gateway-impl.d.ts +0 -31
- package/web/gateway-impl.js.map +0 -1
- /package/node/{mem-filesystem.js.map → mem-filesystem-LPPT7QV5.js.map} +0 -0
package/index.cjs
CHANGED
@@ -5,9 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __esm = (fn, res) => function __init() {
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
10
|
-
};
|
11
8
|
var __export = (target, all) => {
|
12
9
|
for (var name in all)
|
13
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -30,70 +27,409 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
30
27
|
));
|
31
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
32
29
|
|
33
|
-
// src/
|
34
|
-
|
35
|
-
|
30
|
+
// src/index.ts
|
31
|
+
var index_exports = {};
|
32
|
+
__export(index_exports, {
|
33
|
+
CRDT: () => CRDT,
|
34
|
+
Database: () => Database,
|
35
|
+
Index: () => Index,
|
36
|
+
NotFoundError: () => NotFoundError,
|
37
|
+
PACKAGE_VERSION: () => PACKAGE_VERSION,
|
38
|
+
Result: () => import_cement.Result,
|
39
|
+
UInt8ArrayEqual: () => UInt8ArrayEqual,
|
40
|
+
blockstore: () => blockstore_exports,
|
41
|
+
bs: () => blockstore_exports,
|
42
|
+
dataDir: () => dataDir,
|
43
|
+
ensureLogger: () => ensureLogger,
|
44
|
+
ensureSuperLog: () => ensureSuperLog,
|
45
|
+
ensureSuperThis: () => ensureSuperThis,
|
46
|
+
exceptionWrapper: () => exceptionWrapper,
|
47
|
+
falsyToUndef: () => falsyToUndef,
|
48
|
+
fireproof: () => fireproof,
|
49
|
+
getKey: () => getKey,
|
50
|
+
getName: () => getName,
|
51
|
+
getStore: () => getStore,
|
52
|
+
index: () => index,
|
53
|
+
isFalsy: () => isFalsy,
|
54
|
+
isNotFoundError: () => isNotFoundError,
|
55
|
+
onSuperThis: () => onSuperThis,
|
56
|
+
rt: () => runtime_exports,
|
57
|
+
runtime: () => runtime_exports,
|
58
|
+
throwFalsy: () => throwFalsy
|
59
|
+
});
|
60
|
+
module.exports = __toCommonJS(index_exports);
|
61
|
+
|
62
|
+
// src/database.ts
|
63
|
+
var import_cement12 = require("@adviser/cement");
|
64
|
+
|
65
|
+
// src/write-queue.ts
|
66
|
+
function writeQueue(worker, payload = Infinity, unbounded = false) {
|
67
|
+
const queue = [];
|
68
|
+
let isProcessing = false;
|
69
|
+
async function process() {
|
70
|
+
if (isProcessing || queue.length === 0) return;
|
71
|
+
isProcessing = true;
|
72
|
+
const tasksToProcess = queue.splice(0, payload);
|
73
|
+
const updates = tasksToProcess.map((item) => item.task);
|
74
|
+
if (unbounded) {
|
75
|
+
const promises = updates.map(async (update, index2) => {
|
76
|
+
try {
|
77
|
+
const result = await worker([update]);
|
78
|
+
tasksToProcess[index2].resolve(result);
|
79
|
+
} catch (error) {
|
80
|
+
tasksToProcess[index2].reject(error);
|
81
|
+
}
|
82
|
+
});
|
83
|
+
await Promise.all(promises);
|
84
|
+
} else {
|
85
|
+
try {
|
86
|
+
const result = await worker(updates);
|
87
|
+
tasksToProcess.forEach((task) => task.resolve(result));
|
88
|
+
} catch (error) {
|
89
|
+
tasksToProcess.forEach((task) => task.reject(error));
|
90
|
+
}
|
91
|
+
}
|
92
|
+
isProcessing = false;
|
93
|
+
void process();
|
94
|
+
}
|
95
|
+
return {
|
96
|
+
push(task) {
|
97
|
+
return new Promise((resolve, reject) => {
|
98
|
+
queue.push({ task, resolve, reject });
|
99
|
+
void process();
|
100
|
+
});
|
101
|
+
}
|
102
|
+
};
|
36
103
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
104
|
+
|
105
|
+
// src/crdt.ts
|
106
|
+
var import_cement11 = require("@adviser/cement");
|
107
|
+
|
108
|
+
// src/runtime/wait-pr-multiformats/block.ts
|
109
|
+
var block_exports = {};
|
110
|
+
__export(block_exports, {
|
111
|
+
Block: () => Block,
|
112
|
+
create: () => create,
|
113
|
+
createUnsafe: () => createUnsafe,
|
114
|
+
decode: () => decode,
|
115
|
+
encode: () => encode
|
116
|
+
});
|
117
|
+
var import_multiformats = require("multiformats");
|
118
|
+
var import_block = require("multiformats/block");
|
119
|
+
var Block = import_block.Block;
|
120
|
+
async function decode({
|
121
|
+
bytes,
|
122
|
+
codec: codec3,
|
123
|
+
hasher: hasher7
|
124
|
+
}) {
|
125
|
+
if (bytes == null) throw new Error('Missing required argument "bytes"');
|
126
|
+
if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
|
127
|
+
const value = await Promise.resolve(codec3.decode(bytes));
|
128
|
+
const hash = await hasher7.digest(bytes);
|
129
|
+
const cid = import_multiformats.CID.create(1, codec3.code, hash);
|
130
|
+
return new import_block.Block({ value, bytes, cid });
|
50
131
|
}
|
51
|
-
function
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
132
|
+
async function encode({
|
133
|
+
value,
|
134
|
+
codec: codec3,
|
135
|
+
hasher: hasher7
|
136
|
+
}) {
|
137
|
+
if (typeof value === "undefined") throw new Error('Missing required argument "value"');
|
138
|
+
if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
|
139
|
+
const bytes = await Promise.resolve(codec3.encode(value));
|
140
|
+
const hash = await hasher7.digest(bytes);
|
141
|
+
const cid = import_multiformats.CID.create(1, codec3.code, hash);
|
142
|
+
return new import_block.Block({ value, bytes, cid });
|
57
143
|
}
|
58
|
-
function
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
144
|
+
async function create({
|
145
|
+
bytes,
|
146
|
+
cid,
|
147
|
+
hasher: hasher7,
|
148
|
+
codec: codec3
|
149
|
+
}) {
|
150
|
+
if (bytes == null) throw new Error('Missing required argument "bytes"');
|
151
|
+
if (hasher7 == null) throw new Error('Missing required argument "hasher"');
|
152
|
+
const value = await Promise.resolve(codec3.decode(bytes));
|
153
|
+
const hash = await hasher7.digest(bytes);
|
154
|
+
if (!import_multiformats.bytes.equals(cid.multihash.bytes, hash.bytes)) {
|
155
|
+
throw new Error("CID hash does not match bytes");
|
156
|
+
}
|
157
|
+
return createUnsafe({
|
158
|
+
bytes,
|
159
|
+
cid,
|
160
|
+
value,
|
161
|
+
codec: codec3
|
70
162
|
});
|
71
|
-
_onSuperThis.forEach((fn) => fn(ret));
|
72
|
-
return ret;
|
73
163
|
}
|
74
|
-
function
|
75
|
-
|
76
|
-
|
164
|
+
async function createUnsafe({
|
165
|
+
bytes,
|
166
|
+
cid,
|
167
|
+
value: maybeValue,
|
168
|
+
codec: codec3
|
169
|
+
}) {
|
170
|
+
const value = await Promise.resolve(maybeValue !== void 0 ? maybeValue : codec3?.decode(bytes));
|
171
|
+
if (value === void 0) throw new Error('Missing required argument, must either provide "value" or "codec"');
|
172
|
+
return new Block({
|
173
|
+
cid,
|
174
|
+
bytes,
|
175
|
+
value
|
77
176
|
});
|
78
177
|
}
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
178
|
+
|
179
|
+
// src/crdt-helpers.ts
|
180
|
+
var import_link = require("multiformats/link");
|
181
|
+
var import_sha25 = require("multiformats/hashes/sha2");
|
182
|
+
var codec = __toESM(require("@fireproof/vendor/@ipld/dag-cbor"), 1);
|
183
|
+
var import_crdt = require("@fireproof/vendor/@web3-storage/pail/crdt");
|
184
|
+
var import_clock2 = require("@fireproof/vendor/@web3-storage/pail/clock");
|
185
|
+
var Batch = __toESM(require("@fireproof/vendor/@web3-storage/pail/crdt/batch"), 1);
|
186
|
+
|
187
|
+
// src/blockstore/index.ts
|
188
|
+
var blockstore_exports = {};
|
189
|
+
__export(blockstore_exports, {
|
190
|
+
BaseBlockstore: () => BaseBlockstore,
|
191
|
+
CarTransaction: () => CarTransaction,
|
192
|
+
CompactionFetcher: () => CompactionFetcher,
|
193
|
+
ConnectionBase: () => ConnectionBase,
|
194
|
+
EncryptedBlockstore: () => EncryptedBlockstore,
|
195
|
+
FragmentGateway: () => FragmentGateway,
|
196
|
+
Loader: () => Loader,
|
197
|
+
addCryptoKeyToGatewayMetaPayload: () => addCryptoKeyToGatewayMetaPayload,
|
198
|
+
ensureStart: () => ensureStart,
|
199
|
+
getGatewayFromURL: () => getGatewayFromURL,
|
200
|
+
parseCarFile: () => parseCarFile,
|
201
|
+
registerStoreProtocol: () => registerStoreProtocol,
|
202
|
+
setCryptoKeyFromGatewayMetaPayload: () => setCryptoKeyFromGatewayMetaPayload,
|
203
|
+
testStoreFactory: () => testStoreFactory,
|
204
|
+
toCIDBlock: () => toCIDBlock,
|
205
|
+
toStoreRuntime: () => toStoreRuntime
|
206
|
+
});
|
207
|
+
|
208
|
+
// src/blockstore/types.ts
|
209
|
+
function toCIDBlock(block) {
|
210
|
+
return block;
|
211
|
+
}
|
212
|
+
|
213
|
+
// src/blockstore/store-factory.ts
|
214
|
+
var import_cement9 = require("@adviser/cement");
|
215
|
+
|
216
|
+
// src/runtime/files.ts
|
217
|
+
var files_exports = {};
|
218
|
+
__export(files_exports, {
|
219
|
+
decodeFile: () => decodeFile,
|
220
|
+
encodeFile: () => encodeFile
|
221
|
+
});
|
222
|
+
var UnixFS = __toESM(require("@ipld/unixfs"), 1);
|
223
|
+
var raw = __toESM(require("multiformats/codecs/raw"), 1);
|
224
|
+
var import_fixed = require("@ipld/unixfs/file/chunker/fixed");
|
225
|
+
var import_balanced = require("@ipld/unixfs/file/layout/balanced");
|
226
|
+
var import_ipfs_unixfs_exporter = require("@fireproof/vendor/ipfs-unixfs-exporter");
|
227
|
+
var queuingStrategy = UnixFS.withCapacity();
|
228
|
+
var settings = UnixFS.configure({
|
229
|
+
fileChunkEncoder: raw,
|
230
|
+
smallFileEncoder: raw,
|
231
|
+
chunker: (0, import_fixed.withMaxChunkSize)(1024 * 1024),
|
232
|
+
fileLayout: (0, import_balanced.withWidth)(1024)
|
233
|
+
});
|
234
|
+
async function collect(collectable) {
|
235
|
+
const chunks = [];
|
236
|
+
await collectable.pipeTo(
|
237
|
+
new WritableStream({
|
238
|
+
write(chunk) {
|
239
|
+
chunks.push(chunk);
|
240
|
+
}
|
241
|
+
})
|
242
|
+
);
|
243
|
+
return chunks;
|
244
|
+
}
|
245
|
+
async function encodeFile(blob) {
|
246
|
+
const readable = createFileEncoderStream(blob);
|
247
|
+
const blocks = await collect(readable);
|
248
|
+
return { cid: blocks.at(-1).cid, blocks };
|
249
|
+
}
|
250
|
+
async function decodeFile(blocks, cid, meta) {
|
251
|
+
const entry = await (0, import_ipfs_unixfs_exporter.exporter)(cid.toString(), blocks, { length: meta.size });
|
252
|
+
const chunks = [];
|
253
|
+
for await (const chunk of entry.content()) {
|
254
|
+
chunks.push(chunk);
|
255
|
+
}
|
256
|
+
return new File(chunks, entry.name, { type: meta.type, lastModified: 0 });
|
257
|
+
}
|
258
|
+
function createFileEncoderStream(blob) {
|
259
|
+
const { readable, writable } = new TransformStream({}, queuingStrategy);
|
260
|
+
const unixfsWriter = UnixFS.createWriter({ writable, settings });
|
261
|
+
const fileBuilder = new UnixFSFileBuilder("", blob);
|
262
|
+
void (async () => {
|
263
|
+
await fileBuilder.finalize(unixfsWriter);
|
264
|
+
await unixfsWriter.close();
|
265
|
+
})();
|
266
|
+
return readable;
|
267
|
+
}
|
268
|
+
var UnixFSFileBuilder = class {
|
269
|
+
#file;
|
270
|
+
constructor(name, file) {
|
271
|
+
this.name = name;
|
272
|
+
this.#file = file;
|
273
|
+
}
|
274
|
+
async finalize(writer) {
|
275
|
+
const unixfsFileWriter = UnixFS.createFileWriter(writer);
|
276
|
+
await this.#file.stream().pipeTo(
|
277
|
+
new WritableStream({
|
278
|
+
async write(chunk) {
|
279
|
+
await unixfsFileWriter.write(chunk);
|
280
|
+
}
|
281
|
+
})
|
282
|
+
);
|
283
|
+
return await unixfsFileWriter.close();
|
284
|
+
}
|
285
|
+
};
|
286
|
+
|
287
|
+
// src/blockstore/store.ts
|
288
|
+
var import_dag_json2 = require("@fireproof/vendor/@ipld/dag-json");
|
289
|
+
var import_cement8 = require("@adviser/cement");
|
290
|
+
|
291
|
+
// src/types.ts
|
292
|
+
function isFalsy(value) {
|
293
|
+
return value === false && value === null && value === void 0;
|
294
|
+
}
|
295
|
+
function throwFalsy(value) {
|
296
|
+
if (isFalsy(value)) {
|
297
|
+
throw new Error("value is Falsy");
|
298
|
+
}
|
299
|
+
return value;
|
300
|
+
}
|
301
|
+
function falsyToUndef(value) {
|
302
|
+
if (isFalsy(value)) {
|
303
|
+
return void 0;
|
304
|
+
}
|
305
|
+
return value;
|
306
|
+
}
|
307
|
+
|
308
|
+
// src/utils.ts
|
309
|
+
var import_cement = require("@adviser/cement");
|
310
|
+
var import_base58 = require("multiformats/bases/base58");
|
311
|
+
var _globalLogger = new import_cement.ResolveOnce();
|
312
|
+
function globalLogger() {
|
313
|
+
return _globalLogger.once(() => new import_cement.LoggerImpl());
|
314
|
+
}
|
315
|
+
var registerFP_DEBUG = new import_cement.ResolveOnce();
|
316
|
+
var SuperThisImpl = class _SuperThisImpl {
|
317
|
+
constructor(opts) {
|
318
|
+
this.logger = opts.logger;
|
319
|
+
this.env = opts.env;
|
320
|
+
this.crypto = opts.crypto;
|
321
|
+
this.pathOps = opts.pathOps;
|
322
|
+
this.txt = opts.txt;
|
323
|
+
this.ctx = { ...opts.ctx };
|
324
|
+
}
|
325
|
+
nextId(bytes = 6) {
|
326
|
+
const bin = this.crypto.randomBytes(bytes);
|
327
|
+
return {
|
328
|
+
str: import_base58.base58btc.encode(bin),
|
329
|
+
bin
|
330
|
+
};
|
331
|
+
}
|
332
|
+
timeOrderedNextId(now) {
|
333
|
+
now = typeof now === "number" ? now : (/* @__PURE__ */ new Date()).getTime();
|
334
|
+
const t = (281474976710656 + now).toString(16).replace(/^1/, "");
|
335
|
+
const bin = this.crypto.randomBytes(10);
|
336
|
+
bin[1] = bin[1] & 240 | (bin[1] | 8 && 11);
|
337
|
+
const hex = Array.from(bin).map((i) => i.toString(16).padStart(2, "0")).join("");
|
338
|
+
return {
|
339
|
+
str: `${t.slice(0, 8)}-${t.slice(8)}-7${hex.slice(0, 3)}-${hex.slice(3, 7)}-${hex.slice(7, 19)}`
|
340
|
+
};
|
341
|
+
}
|
342
|
+
start() {
|
343
|
+
return Promise.resolve();
|
344
|
+
}
|
345
|
+
clone(override) {
|
346
|
+
return new _SuperThisImpl({
|
347
|
+
logger: override.logger || this.logger,
|
348
|
+
env: (0, import_cement.envFactory)(override.env) || this.env,
|
349
|
+
crypto: override.crypto || this.crypto,
|
350
|
+
pathOps: override.pathOps || this.pathOps,
|
351
|
+
txt: override.txt || this.txt,
|
352
|
+
ctx: { ...this.ctx, ...override.ctx }
|
353
|
+
});
|
354
|
+
}
|
355
|
+
};
|
356
|
+
function presetEnv() {
|
357
|
+
const penv = new Map([
|
358
|
+
// ["FP_DEBUG", "xxx"],
|
359
|
+
// ["FP_ENV", "development"],
|
360
|
+
...Array.from(
|
361
|
+
Object.entries(
|
362
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
363
|
+
globalThis[Symbol.for("FP_PRESET_ENV")] || {}
|
364
|
+
)
|
365
|
+
)
|
366
|
+
// .map(([k, v]) => [k, v as string])
|
367
|
+
]);
|
368
|
+
return penv;
|
369
|
+
}
|
370
|
+
var pathOpsImpl = class {
|
371
|
+
join(...paths) {
|
372
|
+
return paths.map((i) => i.replace(/\/+$/, "")).join("/");
|
373
|
+
}
|
374
|
+
dirname(path) {
|
375
|
+
return path.split("/").slice(0, -1).join("/");
|
376
|
+
}
|
377
|
+
// homedir() {
|
378
|
+
// throw new Error("SysContainer:homedir is not available in seeded state");
|
379
|
+
// }
|
380
|
+
};
|
381
|
+
var pathOps = new pathOpsImpl();
|
382
|
+
var txtOps = {
|
383
|
+
// eslint-disable-next-line no-restricted-globals
|
384
|
+
encode: (input) => new TextEncoder().encode(input),
|
385
|
+
// eslint-disable-next-line no-restricted-globals
|
386
|
+
decode: (input) => new TextDecoder().decode(input)
|
387
|
+
};
|
388
|
+
var _onSuperThis = /* @__PURE__ */ new Map();
|
389
|
+
function onSuperThis(fn) {
|
390
|
+
const key = `onSuperThis-${Math.random().toString(36).slice(2)}`;
|
391
|
+
_onSuperThis.set(key, fn);
|
392
|
+
return () => {
|
393
|
+
_onSuperThis.delete(key);
|
394
|
+
};
|
395
|
+
}
|
396
|
+
function ensureSuperThis(osthis) {
|
397
|
+
const env = (0, import_cement.envFactory)({
|
398
|
+
symbol: osthis?.env?.symbol || "FP_ENV",
|
399
|
+
presetEnv: osthis?.env?.presetEnv || presetEnv()
|
400
|
+
});
|
401
|
+
const ret = new SuperThisImpl({
|
402
|
+
logger: osthis?.logger || globalLogger(),
|
403
|
+
env,
|
404
|
+
crypto: osthis?.crypto || (0, import_cement.toCryptoRuntime)(),
|
405
|
+
ctx: osthis?.ctx || {},
|
406
|
+
pathOps,
|
407
|
+
txt: osthis?.txt || txtOps
|
408
|
+
});
|
409
|
+
_onSuperThis.forEach((fn) => fn(ret));
|
410
|
+
return ret;
|
411
|
+
}
|
412
|
+
function ensureSuperLog(sthis, componentName, ctx) {
|
413
|
+
return sthis.clone({
|
414
|
+
logger: ensureLogger(sthis, componentName, ctx)
|
415
|
+
});
|
416
|
+
}
|
417
|
+
function ensureLogger(sthis, componentName, ctx) {
|
418
|
+
let logger;
|
419
|
+
if (sthis && (0, import_cement.IsLogger)(sthis.logger)) {
|
420
|
+
logger = sthis.logger;
|
421
|
+
} else {
|
422
|
+
logger = globalLogger();
|
423
|
+
}
|
424
|
+
const cLogger = logger.With().Module(componentName);
|
425
|
+
const debug = [];
|
426
|
+
let exposeStack = false;
|
427
|
+
if (ctx) {
|
428
|
+
if ("debug" in ctx) {
|
429
|
+
if (typeof ctx.debug === "string" && ctx.debug.length > 0) {
|
430
|
+
debug.push(ctx.debug);
|
431
|
+
} else {
|
432
|
+
debug.push(componentName);
|
97
433
|
}
|
98
434
|
delete ctx.debug;
|
99
435
|
}
|
@@ -142,865 +478,108 @@ function ensureLogger(sthis, componentName, ctx) {
|
|
142
478
|
case "json":
|
143
479
|
default:
|
144
480
|
logger.SetFormatter(new import_cement.JSONFormatter(logger.TxtEnDe()));
|
145
|
-
break;
|
146
|
-
}
|
147
|
-
break;
|
148
|
-
}
|
149
|
-
case "FP_DEBUG":
|
150
|
-
logger.SetDebug(value || []);
|
151
|
-
break;
|
152
|
-
case "FP_STACK":
|
153
|
-
logger.SetExposeStack(!!value);
|
154
|
-
break;
|
155
|
-
}
|
156
|
-
},
|
157
|
-
"FP_FORMAT",
|
158
|
-
"FP_DEBUG",
|
159
|
-
"FP_STACK"
|
160
|
-
);
|
161
|
-
}).finally(() => {
|
162
|
-
});
|
163
|
-
if (debug.length > 0) {
|
164
|
-
logger.SetDebug(debug);
|
165
|
-
}
|
166
|
-
if (exposeStack) {
|
167
|
-
logger.SetExposeStack(true);
|
168
|
-
}
|
169
|
-
const out = cLogger.Logger();
|
170
|
-
return out;
|
171
|
-
}
|
172
|
-
function getStore(url, sthis, joiner) {
|
173
|
-
const store = url.getParam("store");
|
174
|
-
switch (store) {
|
175
|
-
case "data":
|
176
|
-
case "wal":
|
177
|
-
case "meta":
|
178
|
-
break;
|
179
|
-
default:
|
180
|
-
throw sthis.logger.Error().Url(url).Msg(`store not found`).AsError();
|
181
|
-
}
|
182
|
-
let name = store;
|
183
|
-
if (url.hasParam("index")) {
|
184
|
-
name = joiner(url.getParam("index") || "idx", name);
|
185
|
-
}
|
186
|
-
return { store, name };
|
187
|
-
}
|
188
|
-
function getKey(url, logger) {
|
189
|
-
const result = url.getParam("key");
|
190
|
-
if (!result) throw logger.Error().Str("url", url.toString()).Msg(`key not found`).AsError();
|
191
|
-
return result;
|
192
|
-
}
|
193
|
-
function getName(sthis, url) {
|
194
|
-
let result = url.getParam("name");
|
195
|
-
if (!result) {
|
196
|
-
result = sthis.pathOps.dirname(url.pathname);
|
197
|
-
if (result.length === 0) {
|
198
|
-
throw sthis.logger.Error().Str("url", url.toString()).Msg(`name not found`).AsError();
|
199
|
-
}
|
200
|
-
}
|
201
|
-
return result;
|
202
|
-
}
|
203
|
-
async function exceptionWrapper(fn) {
|
204
|
-
return fn().catch((e) => import_cement.Result.Err(e));
|
205
|
-
}
|
206
|
-
function isNotFoundError(e) {
|
207
|
-
if (import_cement.Result.Is(e)) {
|
208
|
-
if (e.isOk()) return false;
|
209
|
-
e = e.Err();
|
210
|
-
}
|
211
|
-
if (e.code === "ENOENT") return true;
|
212
|
-
return false;
|
213
|
-
}
|
214
|
-
function dataDir(sthis, name, base) {
|
215
|
-
if (!base) {
|
216
|
-
if (!(0, import_cement.runtimeFn)().isBrowser) {
|
217
|
-
const home = sthis.env.get("HOME") || "./";
|
218
|
-
base = sthis.env.get("FP_STORAGE_URL") || `file://${sthis.pathOps.join(home, ".fireproof")}`;
|
219
|
-
} else {
|
220
|
-
base = sthis.env.get("FP_STORAGE_URL") || `indexdb://fp`;
|
221
|
-
}
|
222
|
-
}
|
223
|
-
return import_cement.URI.from(base.toString()).build().setParam("name", name || "").URI();
|
224
|
-
}
|
225
|
-
function UInt8ArrayEqual(a, b) {
|
226
|
-
if (a.length !== b.length) {
|
227
|
-
return false;
|
228
|
-
}
|
229
|
-
for (let i = 0; i < a.length; i++) {
|
230
|
-
if (a[i] !== b[i]) {
|
231
|
-
return false;
|
232
|
-
}
|
233
|
-
}
|
234
|
-
return true;
|
235
|
-
}
|
236
|
-
var import_cement, import_base58, _globalLogger, registerFP_DEBUG, SuperThisImpl, pathOpsImpl, pathOps, txtOps, _onSuperThis, NotFoundError;
|
237
|
-
var init_utils = __esm({
|
238
|
-
"src/utils.ts"() {
|
239
|
-
"use strict";
|
240
|
-
import_cement = require("@adviser/cement");
|
241
|
-
import_base58 = require("multiformats/bases/base58");
|
242
|
-
_globalLogger = new import_cement.ResolveOnce();
|
243
|
-
registerFP_DEBUG = new import_cement.ResolveOnce();
|
244
|
-
SuperThisImpl = class _SuperThisImpl {
|
245
|
-
constructor(opts) {
|
246
|
-
this.logger = opts.logger;
|
247
|
-
this.env = opts.env;
|
248
|
-
this.crypto = opts.crypto;
|
249
|
-
this.pathOps = opts.pathOps;
|
250
|
-
this.txt = opts.txt;
|
251
|
-
this.ctx = { ...opts.ctx };
|
252
|
-
}
|
253
|
-
nextId(bytes = 6) {
|
254
|
-
const bin = this.crypto.randomBytes(bytes);
|
255
|
-
return {
|
256
|
-
str: import_base58.base58btc.encode(bin),
|
257
|
-
bin
|
258
|
-
};
|
259
|
-
}
|
260
|
-
timeOrderedNextId(now) {
|
261
|
-
now = typeof now === "number" ? now : (/* @__PURE__ */ new Date()).getTime();
|
262
|
-
const t = (281474976710656 + now).toString(16).replace(/^1/, "");
|
263
|
-
const bin = this.crypto.randomBytes(10);
|
264
|
-
bin[1] = bin[1] & 240 | (bin[1] | 8 && 11);
|
265
|
-
const hex = Array.from(bin).map((i) => i.toString(16).padStart(2, "0")).join("");
|
266
|
-
return {
|
267
|
-
str: `${t.slice(0, 8)}-${t.slice(8)}-7${hex.slice(0, 3)}-${hex.slice(3, 7)}-${hex.slice(7, 19)}`
|
268
|
-
};
|
269
|
-
}
|
270
|
-
start() {
|
271
|
-
return Promise.resolve();
|
272
|
-
}
|
273
|
-
clone(override) {
|
274
|
-
return new _SuperThisImpl({
|
275
|
-
logger: override.logger || this.logger,
|
276
|
-
env: (0, import_cement.envFactory)(override.env) || this.env,
|
277
|
-
crypto: override.crypto || this.crypto,
|
278
|
-
pathOps: override.pathOps || this.pathOps,
|
279
|
-
txt: override.txt || this.txt,
|
280
|
-
ctx: { ...this.ctx, ...override.ctx }
|
281
|
-
});
|
282
|
-
}
|
283
|
-
};
|
284
|
-
pathOpsImpl = class {
|
285
|
-
join(...paths) {
|
286
|
-
return paths.map((i) => i.replace(/\/+$/, "")).join("/");
|
287
|
-
}
|
288
|
-
dirname(path) {
|
289
|
-
return path.split("/").slice(0, -1).join("/");
|
290
|
-
}
|
291
|
-
// homedir() {
|
292
|
-
// throw new Error("SysContainer:homedir is not available in seeded state");
|
293
|
-
// }
|
294
|
-
};
|
295
|
-
pathOps = new pathOpsImpl();
|
296
|
-
txtOps = {
|
297
|
-
// eslint-disable-next-line no-restricted-globals
|
298
|
-
encode: (input) => new TextEncoder().encode(input),
|
299
|
-
// eslint-disable-next-line no-restricted-globals
|
300
|
-
decode: (input) => new TextDecoder().decode(input)
|
301
|
-
};
|
302
|
-
_onSuperThis = /* @__PURE__ */ new Map();
|
303
|
-
NotFoundError = class extends Error {
|
304
|
-
constructor() {
|
305
|
-
super(...arguments);
|
306
|
-
this.code = "ENOENT";
|
307
|
-
}
|
308
|
-
};
|
309
|
-
}
|
310
|
-
});
|
311
|
-
|
312
|
-
// src/runtime/gateways/file/get-file-system-static.ts
|
313
|
-
async function getFileSystem(url) {
|
314
|
-
const name = url.getParam("fs", "node");
|
315
|
-
let fs;
|
316
|
-
switch (name) {
|
317
|
-
case "mem":
|
318
|
-
fs = await externalLoaders.get(name).once(async () => {
|
319
|
-
const { MemFileSystem } = await import("@fireproof/core/mem");
|
320
|
-
return new MemFileSystem();
|
321
|
-
});
|
322
|
-
break;
|
323
|
-
// case 'deno': {
|
324
|
-
// const { DenoFileSystem } = await import("./deno-filesystem.js");
|
325
|
-
// fs = new DenoFileSystem();
|
326
|
-
// break;
|
327
|
-
// }
|
328
|
-
default:
|
329
|
-
fs = await externalLoaders.get(name).once(async () => {
|
330
|
-
const { NodeFileSystem } = await import("@fireproof/core/node");
|
331
|
-
return new NodeFileSystem();
|
332
|
-
});
|
333
|
-
}
|
334
|
-
return fs.start();
|
335
|
-
}
|
336
|
-
var import_cement4, externalLoaders;
|
337
|
-
var init_get_file_system_static = __esm({
|
338
|
-
"src/runtime/gateways/file/get-file-system-static.ts"() {
|
339
|
-
"use strict";
|
340
|
-
import_cement4 = require("@adviser/cement");
|
341
|
-
externalLoaders = new import_cement4.KeyedResolvOnce();
|
342
|
-
}
|
343
|
-
});
|
344
|
-
|
345
|
-
// src/runtime/gateways/file/utils.ts
|
346
|
-
var utils_exports = {};
|
347
|
-
__export(utils_exports, {
|
348
|
-
getFileName: () => getFileName,
|
349
|
-
getFileSystem: () => getFileSystem,
|
350
|
-
getPath: () => getPath
|
351
|
-
});
|
352
|
-
function getPath(url, sthis) {
|
353
|
-
const basePath = url.pathname;
|
354
|
-
const name = url.getParam("name");
|
355
|
-
if (name) {
|
356
|
-
const version = url.getParam("version");
|
357
|
-
if (!version) throw sthis.logger.Error().Url(url).Msg(`version not found`).AsError();
|
358
|
-
return sthis.pathOps.join(basePath, version, name);
|
359
|
-
}
|
360
|
-
return sthis.pathOps.join(basePath);
|
361
|
-
}
|
362
|
-
function getFileName(url, sthis) {
|
363
|
-
const key = url.getParam("key");
|
364
|
-
if (!key) throw sthis.logger.Error().Url(url).Msg(`key not found`).AsError();
|
365
|
-
const res = getStore(url, sthis, (...a) => a.join("-"));
|
366
|
-
switch (res.store) {
|
367
|
-
case "data":
|
368
|
-
return sthis.pathOps.join(res.name, key + ".car");
|
369
|
-
case "wal":
|
370
|
-
case "meta":
|
371
|
-
return sthis.pathOps.join(res.name, key + ".json");
|
372
|
-
default:
|
373
|
-
throw sthis.logger.Error().Url(url).Msg(`unsupported store type`).AsError();
|
374
|
-
}
|
375
|
-
}
|
376
|
-
var init_utils2 = __esm({
|
377
|
-
"src/runtime/gateways/file/utils.ts"() {
|
378
|
-
"use strict";
|
379
|
-
init_utils();
|
380
|
-
init_get_file_system_static();
|
381
|
-
}
|
382
|
-
});
|
383
|
-
|
384
|
-
// src/runtime/key-bag-file.ts
|
385
|
-
var key_bag_file_exports = {};
|
386
|
-
__export(key_bag_file_exports, {
|
387
|
-
KeyBagProviderFile: () => KeyBagProviderFile
|
388
|
-
});
|
389
|
-
var KeyBagProviderFile;
|
390
|
-
var init_key_bag_file = __esm({
|
391
|
-
"src/runtime/key-bag-file.ts"() {
|
392
|
-
"use strict";
|
393
|
-
init_utils();
|
394
|
-
KeyBagProviderFile = class {
|
395
|
-
async _prepare(id) {
|
396
|
-
await this.sthis.start();
|
397
|
-
let sysFS;
|
398
|
-
switch (this.url.protocol) {
|
399
|
-
case "file:": {
|
400
|
-
const { getFileSystem: getFileSystem2 } = await Promise.resolve().then(() => (init_utils2(), utils_exports));
|
401
|
-
sysFS = await getFileSystem2(this.url);
|
402
|
-
break;
|
403
|
-
}
|
404
|
-
default:
|
405
|
-
throw this.logger.Error().Url(this.url).Msg("unsupported protocol").AsError();
|
406
|
-
}
|
407
|
-
const dirName = this.url.pathname;
|
408
|
-
await sysFS.mkdir(dirName, { recursive: true });
|
409
|
-
return {
|
410
|
-
dirName,
|
411
|
-
sysFS,
|
412
|
-
fName: this.sthis.pathOps.join(dirName, `${id.replace(/[^a-zA-Z0-9]/g, "_")}.json`)
|
413
|
-
};
|
414
|
-
}
|
415
|
-
constructor(url, sthis) {
|
416
|
-
this.url = url;
|
417
|
-
this.sthis = sthis;
|
418
|
-
this.logger = sthis.logger;
|
419
|
-
}
|
420
|
-
async get(id) {
|
421
|
-
const ctx = await this._prepare(id);
|
422
|
-
try {
|
423
|
-
const p = await ctx.sysFS.readfile(ctx.fName);
|
424
|
-
const ki = JSON.parse(this.sthis.txt.decode(p));
|
425
|
-
return ki;
|
426
|
-
} catch (e) {
|
427
|
-
if (isNotFoundError(e)) {
|
428
|
-
return void 0;
|
429
|
-
}
|
430
|
-
throw this.logger.Error().Err(e).Str("file", ctx.dirName).Msg("read bag failed").AsError();
|
431
|
-
}
|
432
|
-
}
|
433
|
-
async set(id, item) {
|
434
|
-
const ctx = await this._prepare(id);
|
435
|
-
const p = this.sthis.txt.encode(JSON.stringify(item, null, 2));
|
436
|
-
await ctx.sysFS.writefile(ctx.fName, p);
|
437
|
-
}
|
438
|
-
};
|
439
|
-
}
|
440
|
-
});
|
441
|
-
|
442
|
-
// src/runtime/key-bag-indexdb.ts
|
443
|
-
var key_bag_indexdb_exports = {};
|
444
|
-
__export(key_bag_indexdb_exports, {
|
445
|
-
KeyBagProviderIndexDB: () => KeyBagProviderIndexDB
|
446
|
-
});
|
447
|
-
var import_idb, import_cement5, KeyBagProviderIndexDB;
|
448
|
-
var init_key_bag_indexdb = __esm({
|
449
|
-
"src/runtime/key-bag-indexdb.ts"() {
|
450
|
-
"use strict";
|
451
|
-
import_idb = require("idb");
|
452
|
-
init_utils2();
|
453
|
-
import_cement5 = require("@adviser/cement");
|
454
|
-
KeyBagProviderIndexDB = class {
|
455
|
-
constructor(url, sthis) {
|
456
|
-
this._db = new import_cement5.ResolveOnce();
|
457
|
-
this.sthis = sthis;
|
458
|
-
this.logger = sthis.logger;
|
459
|
-
this.url = url;
|
460
|
-
this.dbName = getPath(this.url, this.sthis);
|
461
|
-
}
|
462
|
-
async _prepare() {
|
463
|
-
return this._db.once(async () => {
|
464
|
-
return await (0, import_idb.openDB)(this.dbName, 1, {
|
465
|
-
upgrade(db) {
|
466
|
-
["bag"].map((store) => {
|
467
|
-
db.createObjectStore(store, {
|
468
|
-
autoIncrement: false
|
469
|
-
});
|
470
|
-
});
|
471
|
-
}
|
472
|
-
});
|
473
|
-
});
|
474
|
-
}
|
475
|
-
async get(id) {
|
476
|
-
const db = await this._prepare();
|
477
|
-
const tx = db.transaction(["bag"], "readonly");
|
478
|
-
const keyItem = await tx.objectStore("bag").get(id);
|
479
|
-
await tx.done;
|
480
|
-
if (!keyItem) {
|
481
|
-
return void 0;
|
482
|
-
}
|
483
|
-
return keyItem;
|
484
|
-
}
|
485
|
-
async set(id, item) {
|
486
|
-
const db = await this._prepare();
|
487
|
-
const tx = db.transaction(["bag"], "readwrite");
|
488
|
-
await tx.objectStore("bag").put(item, id);
|
489
|
-
await tx.done;
|
490
|
-
}
|
491
|
-
};
|
492
|
-
}
|
493
|
-
});
|
494
|
-
|
495
|
-
// src/runtime/gateways/file/version.ts
|
496
|
-
var FILESTORE_VERSION;
|
497
|
-
var init_version = __esm({
|
498
|
-
"src/runtime/gateways/file/version.ts"() {
|
499
|
-
"use strict";
|
500
|
-
FILESTORE_VERSION = "v0.19-file";
|
501
|
-
}
|
502
|
-
});
|
503
|
-
|
504
|
-
// src/runtime/gateways/file/gateway.ts
|
505
|
-
var gateway_exports = {};
|
506
|
-
__export(gateway_exports, {
|
507
|
-
FileGateway: () => FileGateway,
|
508
|
-
FileTestStore: () => FileTestStore
|
509
|
-
});
|
510
|
-
var import_cement11, versionFiles, FileGateway, FileTestStore;
|
511
|
-
var init_gateway = __esm({
|
512
|
-
"src/runtime/gateways/file/gateway.ts"() {
|
513
|
-
"use strict";
|
514
|
-
init_version();
|
515
|
-
import_cement11 = require("@adviser/cement");
|
516
|
-
init_utils();
|
517
|
-
init_utils2();
|
518
|
-
init_get_file_system_static();
|
519
|
-
versionFiles = new import_cement11.KeyedResolvOnce();
|
520
|
-
FileGateway = class {
|
521
|
-
get fs() {
|
522
|
-
if (!this._fs) throw this.logger.Error().Msg("fs not initialized").AsError();
|
523
|
-
return this._fs;
|
524
|
-
}
|
525
|
-
constructor(sthis) {
|
526
|
-
this.sthis = sthis;
|
527
|
-
this.logger = sthis.logger;
|
528
|
-
}
|
529
|
-
async getVersionFromFile(path, logger) {
|
530
|
-
return versionFiles.get(path).once(async () => {
|
531
|
-
await this.fs.mkdir(path, { recursive: true });
|
532
|
-
const vFile = this.sthis.pathOps.join(path, "version");
|
533
|
-
const vFileStat = await this.fs.stat(vFile).catch(() => void 0);
|
534
|
-
if (!vFileStat) {
|
535
|
-
await this.fs.writefile(this.sthis.pathOps.join(path, "version"), FILESTORE_VERSION);
|
536
|
-
return FILESTORE_VERSION;
|
537
|
-
} else if (!vFileStat.isFile()) {
|
538
|
-
throw logger.Error().Str("file", vFile).Msg(`version file is a directory`).AsError();
|
539
|
-
}
|
540
|
-
const v = await this.fs.readfile(vFile);
|
541
|
-
const vStr = this.sthis.txt.decode(v);
|
542
|
-
if (vStr !== FILESTORE_VERSION) {
|
543
|
-
logger.Warn().Str("file", vFile).Str("from", vStr).Str("expected", FILESTORE_VERSION).Msg(`version mismatch`);
|
544
|
-
}
|
545
|
-
return vStr;
|
546
|
-
});
|
547
|
-
}
|
548
|
-
start(baseURL) {
|
549
|
-
return (0, import_cement11.exception2Result)(async () => {
|
550
|
-
this._fs = await getFileSystem(baseURL);
|
551
|
-
await this.fs.start();
|
552
|
-
const url = baseURL.build();
|
553
|
-
url.defParam("version", FILESTORE_VERSION);
|
554
|
-
const dbUrl = await this.buildUrl(url.URI(), "dummy");
|
555
|
-
const dbdirFile = this.getFilePath(dbUrl.Ok());
|
556
|
-
await this.fs.mkdir(this.sthis.pathOps.dirname(dbdirFile), { recursive: true });
|
557
|
-
const dbroot = this.sthis.pathOps.dirname(dbdirFile);
|
558
|
-
this.logger.Debug().Url(url.URI()).Str("dbroot", dbroot).Msg("start");
|
559
|
-
url.setParam("version", await this.getVersionFromFile(dbroot, this.logger));
|
560
|
-
return url.URI();
|
561
|
-
});
|
562
|
-
}
|
563
|
-
async buildUrl(baseUrl, key) {
|
564
|
-
return import_cement11.Result.Ok(baseUrl.build().setParam("key", key).URI());
|
565
|
-
}
|
566
|
-
async close() {
|
567
|
-
return import_cement11.Result.Ok(void 0);
|
568
|
-
}
|
569
|
-
// abstract buildUrl(baseUrl: URL, key: string): Promise<Result<URL>>;
|
570
|
-
getFilePath(url) {
|
571
|
-
const key = url.getParam("key");
|
572
|
-
if (!key) throw this.logger.Error().Url(url).Msg(`key not found`).AsError();
|
573
|
-
return this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));
|
574
|
-
}
|
575
|
-
async put(url, body) {
|
576
|
-
return (0, import_cement11.exception2Result)(async () => {
|
577
|
-
const file = await this.getFilePath(url);
|
578
|
-
this.logger.Debug().Str("url", url.toString()).Str("file", file).Msg("put");
|
579
|
-
await this.fs.writefile(file, body);
|
580
|
-
});
|
581
|
-
}
|
582
|
-
async get(url) {
|
583
|
-
return exceptionWrapper(async () => {
|
584
|
-
const file = this.getFilePath(url);
|
585
|
-
try {
|
586
|
-
const res = await this.fs.readfile(file);
|
587
|
-
this.logger.Debug().Url(url.asURL()).Str("file", file).Msg("get");
|
588
|
-
return import_cement11.Result.Ok(new Uint8Array(res));
|
589
|
-
} catch (e) {
|
590
|
-
if (isNotFoundError(e)) {
|
591
|
-
return import_cement11.Result.Err(new NotFoundError(`file not found: ${file}`));
|
592
|
-
}
|
593
|
-
return import_cement11.Result.Err(e);
|
594
|
-
}
|
595
|
-
});
|
596
|
-
}
|
597
|
-
async delete(url) {
|
598
|
-
return (0, import_cement11.exception2Result)(async () => {
|
599
|
-
await this.fs.unlink(this.getFilePath(url));
|
600
|
-
});
|
601
|
-
}
|
602
|
-
async destroy(baseURL) {
|
603
|
-
const url = await this.buildUrl(baseURL, "x");
|
604
|
-
if (url.isErr()) return url;
|
605
|
-
const filepath = this.sthis.pathOps.dirname(this.getFilePath(url.Ok()));
|
606
|
-
let files = [];
|
607
|
-
try {
|
608
|
-
files = await this.fs.readdir(filepath);
|
609
|
-
} catch (e) {
|
610
|
-
if (!isNotFoundError(e)) {
|
611
|
-
throw this.logger.Error().Err(e).Str("dir", filepath).Msg("destroy:readdir").AsError();
|
612
|
-
}
|
613
|
-
}
|
614
|
-
for (const file of files) {
|
615
|
-
const pathed = this.sthis.pathOps.join(filepath, file);
|
616
|
-
try {
|
617
|
-
await this.fs.unlink(pathed);
|
618
|
-
} catch (e) {
|
619
|
-
if (!isNotFoundError(e)) {
|
620
|
-
throw this.logger.Error().Err(e).Str("file", pathed).Msg("destroy:unlink").AsError();
|
621
|
-
}
|
622
|
-
}
|
623
|
-
}
|
624
|
-
return import_cement11.Result.Ok(void 0);
|
625
|
-
}
|
626
|
-
};
|
627
|
-
FileTestStore = class {
|
628
|
-
constructor(sthis) {
|
629
|
-
this.logger = ensureLogger(sthis, "FileTestStore");
|
630
|
-
this.sthis = sthis;
|
631
|
-
}
|
632
|
-
async get(iurl, key) {
|
633
|
-
const url = iurl.build().setParam("key", key).URI();
|
634
|
-
const dbFile = this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));
|
635
|
-
this.logger.Debug().Url(url).Str("dbFile", dbFile).Msg("get");
|
636
|
-
const buffer = await (await getFileSystem(url)).readfile(dbFile);
|
637
|
-
this.logger.Debug().Url(url).Str("dbFile", dbFile).Len(buffer).Msg("got");
|
638
|
-
return buffer;
|
639
|
-
}
|
640
|
-
};
|
641
|
-
}
|
642
|
-
});
|
643
|
-
|
644
|
-
// src/runtime/gateways/indexdb/gateway-import-static.ts
|
645
|
-
function gatewayImport() {
|
646
|
-
return import("@fireproof/core/web");
|
647
|
-
}
|
648
|
-
var init_gateway_import_static = __esm({
|
649
|
-
"src/runtime/gateways/indexdb/gateway-import-static.ts"() {
|
650
|
-
"use strict";
|
651
|
-
}
|
652
|
-
});
|
653
|
-
|
654
|
-
// src/runtime/gateways/indexdb/gateway.ts
|
655
|
-
var gateway_exports2 = {};
|
656
|
-
__export(gateway_exports2, {
|
657
|
-
IndexDBGateway: () => IndexDBGateway,
|
658
|
-
IndexDBTestStore: () => IndexDBTestStore
|
659
|
-
});
|
660
|
-
var import_cement12, loadExternal, IndexDBGateway, IndexDBTestStore;
|
661
|
-
var init_gateway2 = __esm({
|
662
|
-
"src/runtime/gateways/indexdb/gateway.ts"() {
|
663
|
-
"use strict";
|
664
|
-
import_cement12 = require("@adviser/cement");
|
665
|
-
init_gateway_import_static();
|
666
|
-
loadExternal = new import_cement12.ResolveOnce();
|
667
|
-
IndexDBGateway = class {
|
668
|
-
constructor(sthis) {
|
669
|
-
this.sthis = sthis;
|
670
|
-
}
|
671
|
-
getGateway() {
|
672
|
-
return loadExternal.once(() => {
|
673
|
-
return gatewayImport().then(({ IndexDBGatewayImpl }) => new IndexDBGatewayImpl(this.sthis));
|
674
|
-
});
|
675
|
-
}
|
676
|
-
buildUrl(baseUrl, key) {
|
677
|
-
return this.getGateway().then((gw) => gw.buildUrl(baseUrl, key));
|
678
|
-
}
|
679
|
-
start(baseUrl) {
|
680
|
-
return this.getGateway().then((gw) => gw.start(baseUrl));
|
681
|
-
}
|
682
|
-
close(baseUrl) {
|
683
|
-
return this.getGateway().then((gw) => gw.close(baseUrl));
|
684
|
-
}
|
685
|
-
destroy(baseUrl) {
|
686
|
-
return this.getGateway().then((gw) => gw.destroy(baseUrl));
|
687
|
-
}
|
688
|
-
put(url, body) {
|
689
|
-
return this.getGateway().then((gw) => gw.put(url, body));
|
690
|
-
}
|
691
|
-
get(url) {
|
692
|
-
return this.getGateway().then((gw) => gw.get(url));
|
693
|
-
}
|
694
|
-
delete(url) {
|
695
|
-
return this.getGateway().then((gw) => gw.delete(url));
|
696
|
-
}
|
697
|
-
// subscribe?(url: URI, callback: (meta: Uint8Array) => void): Promise<UnsubscribeResult> {
|
698
|
-
// // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
699
|
-
// return this.getGateway().then(gw => gw.subscribe!(url, callback));
|
700
|
-
// }
|
701
|
-
};
|
702
|
-
IndexDBTestStore = class {
|
703
|
-
constructor(sthis) {
|
704
|
-
this.loadExternal = new import_cement12.ResolveOnce();
|
705
|
-
this.sthis = sthis;
|
706
|
-
}
|
707
|
-
getGateway() {
|
708
|
-
return this.loadExternal.once(() => {
|
709
|
-
return gatewayImport().then(({ IndexDBTestStore: IndexDBTestStore2 }) => new IndexDBTestStore2(this.sthis));
|
710
|
-
});
|
711
|
-
}
|
712
|
-
get(url, key) {
|
713
|
-
return this.getGateway().then((gw) => gw.get(url, key));
|
714
|
-
}
|
715
|
-
};
|
716
|
-
}
|
717
|
-
});
|
718
|
-
|
719
|
-
// src/index.ts
|
720
|
-
var index_exports = {};
|
721
|
-
__export(index_exports, {
|
722
|
-
CRDT: () => CRDT,
|
723
|
-
Database: () => Database,
|
724
|
-
Index: () => Index,
|
725
|
-
NotFoundError: () => NotFoundError,
|
726
|
-
PACKAGE_VERSION: () => PACKAGE_VERSION,
|
727
|
-
Result: () => import_cement.Result,
|
728
|
-
UInt8ArrayEqual: () => UInt8ArrayEqual,
|
729
|
-
blockstore: () => blockstore_exports,
|
730
|
-
bs: () => blockstore_exports,
|
731
|
-
dataDir: () => dataDir,
|
732
|
-
ensureLogger: () => ensureLogger,
|
733
|
-
ensureSuperLog: () => ensureSuperLog,
|
734
|
-
ensureSuperThis: () => ensureSuperThis,
|
735
|
-
exceptionWrapper: () => exceptionWrapper,
|
736
|
-
falsyToUndef: () => falsyToUndef,
|
737
|
-
fireproof: () => fireproof,
|
738
|
-
getKey: () => getKey,
|
739
|
-
getName: () => getName,
|
740
|
-
getStore: () => getStore,
|
741
|
-
index: () => index,
|
742
|
-
isFalsy: () => isFalsy,
|
743
|
-
isNotFoundError: () => isNotFoundError,
|
744
|
-
onSuperThis: () => onSuperThis,
|
745
|
-
rt: () => runtime_exports,
|
746
|
-
runtime: () => runtime_exports,
|
747
|
-
throwFalsy: () => throwFalsy
|
748
|
-
});
|
749
|
-
module.exports = __toCommonJS(index_exports);
|
750
|
-
|
751
|
-
// src/database.ts
|
752
|
-
var import_cement16 = require("@adviser/cement");
|
753
|
-
|
754
|
-
// src/write-queue.ts
|
755
|
-
function writeQueue(worker, payload = Infinity, unbounded = false) {
|
756
|
-
const queue = [];
|
757
|
-
let isProcessing = false;
|
758
|
-
async function process() {
|
759
|
-
if (isProcessing || queue.length === 0) return;
|
760
|
-
isProcessing = true;
|
761
|
-
const tasksToProcess = queue.splice(0, payload);
|
762
|
-
const updates = tasksToProcess.map((item) => item.task);
|
763
|
-
if (unbounded) {
|
764
|
-
const promises = updates.map(async (update, index2) => {
|
765
|
-
try {
|
766
|
-
const result = await worker([update]);
|
767
|
-
tasksToProcess[index2].resolve(result);
|
768
|
-
} catch (error) {
|
769
|
-
tasksToProcess[index2].reject(error);
|
770
|
-
}
|
771
|
-
});
|
772
|
-
await Promise.all(promises);
|
773
|
-
} else {
|
774
|
-
try {
|
775
|
-
const result = await worker(updates);
|
776
|
-
tasksToProcess.forEach((task) => task.resolve(result));
|
777
|
-
} catch (error) {
|
778
|
-
tasksToProcess.forEach((task) => task.reject(error));
|
779
|
-
}
|
780
|
-
}
|
781
|
-
isProcessing = false;
|
782
|
-
void process();
|
783
|
-
}
|
784
|
-
return {
|
785
|
-
push(task) {
|
786
|
-
return new Promise((resolve, reject) => {
|
787
|
-
queue.push({ task, resolve, reject });
|
788
|
-
void process();
|
789
|
-
});
|
790
|
-
}
|
791
|
-
};
|
792
|
-
}
|
793
|
-
|
794
|
-
// src/crdt.ts
|
795
|
-
var import_cement15 = require("@adviser/cement");
|
796
|
-
|
797
|
-
// src/runtime/wait-pr-multiformats/block.ts
|
798
|
-
var block_exports = {};
|
799
|
-
__export(block_exports, {
|
800
|
-
Block: () => Block,
|
801
|
-
create: () => create,
|
802
|
-
createUnsafe: () => createUnsafe,
|
803
|
-
decode: () => decode,
|
804
|
-
encode: () => encode
|
805
|
-
});
|
806
|
-
var import_multiformats = require("multiformats");
|
807
|
-
var import_block = require("multiformats/block");
|
808
|
-
var Block = import_block.Block;
|
809
|
-
async function decode({
|
810
|
-
bytes,
|
811
|
-
codec: codec3,
|
812
|
-
hasher: hasher7
|
813
|
-
}) {
|
814
|
-
if (bytes == null) throw new Error('Missing required argument "bytes"');
|
815
|
-
if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
|
816
|
-
const value = await Promise.resolve(codec3.decode(bytes));
|
817
|
-
const hash = await hasher7.digest(bytes);
|
818
|
-
const cid = import_multiformats.CID.create(1, codec3.code, hash);
|
819
|
-
return new import_block.Block({ value, bytes, cid });
|
820
|
-
}
|
821
|
-
async function encode({
|
822
|
-
value,
|
823
|
-
codec: codec3,
|
824
|
-
hasher: hasher7
|
825
|
-
}) {
|
826
|
-
if (typeof value === "undefined") throw new Error('Missing required argument "value"');
|
827
|
-
if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
|
828
|
-
const bytes = await Promise.resolve(codec3.encode(value));
|
829
|
-
const hash = await hasher7.digest(bytes);
|
830
|
-
const cid = import_multiformats.CID.create(1, codec3.code, hash);
|
831
|
-
return new import_block.Block({ value, bytes, cid });
|
832
|
-
}
|
833
|
-
async function create({
|
834
|
-
bytes,
|
835
|
-
cid,
|
836
|
-
hasher: hasher7,
|
837
|
-
codec: codec3
|
838
|
-
}) {
|
839
|
-
if (bytes == null) throw new Error('Missing required argument "bytes"');
|
840
|
-
if (hasher7 == null) throw new Error('Missing required argument "hasher"');
|
841
|
-
const value = await Promise.resolve(codec3.decode(bytes));
|
842
|
-
const hash = await hasher7.digest(bytes);
|
843
|
-
if (!import_multiformats.bytes.equals(cid.multihash.bytes, hash.bytes)) {
|
844
|
-
throw new Error("CID hash does not match bytes");
|
845
|
-
}
|
846
|
-
return createUnsafe({
|
847
|
-
bytes,
|
848
|
-
cid,
|
849
|
-
value,
|
850
|
-
codec: codec3
|
851
|
-
});
|
852
|
-
}
|
853
|
-
async function createUnsafe({
|
854
|
-
bytes,
|
855
|
-
cid,
|
856
|
-
value: maybeValue,
|
857
|
-
codec: codec3
|
858
|
-
}) {
|
859
|
-
const value = await Promise.resolve(maybeValue !== void 0 ? maybeValue : codec3?.decode(bytes));
|
860
|
-
if (value === void 0) throw new Error('Missing required argument, must either provide "value" or "codec"');
|
861
|
-
return new Block({
|
862
|
-
cid,
|
863
|
-
bytes,
|
864
|
-
value
|
481
|
+
break;
|
482
|
+
}
|
483
|
+
break;
|
484
|
+
}
|
485
|
+
case "FP_DEBUG":
|
486
|
+
logger.SetDebug(value || []);
|
487
|
+
break;
|
488
|
+
case "FP_STACK":
|
489
|
+
logger.SetExposeStack(!!value);
|
490
|
+
break;
|
491
|
+
}
|
492
|
+
},
|
493
|
+
"FP_FORMAT",
|
494
|
+
"FP_DEBUG",
|
495
|
+
"FP_STACK"
|
496
|
+
);
|
497
|
+
}).finally(() => {
|
865
498
|
});
|
499
|
+
if (debug.length > 0) {
|
500
|
+
logger.SetDebug(debug);
|
501
|
+
}
|
502
|
+
if (exposeStack) {
|
503
|
+
logger.SetExposeStack(true);
|
504
|
+
}
|
505
|
+
const out = cLogger.Logger();
|
506
|
+
return out;
|
866
507
|
}
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
ConnectionBase: () => ConnectionBase,
|
883
|
-
EncryptedBlockstore: () => EncryptedBlockstore,
|
884
|
-
FragmentGateway: () => FragmentGateway,
|
885
|
-
Loader: () => Loader,
|
886
|
-
addCryptoKeyToGatewayMetaPayload: () => addCryptoKeyToGatewayMetaPayload,
|
887
|
-
ensureStart: () => ensureStart,
|
888
|
-
getGatewayFromURL: () => getGatewayFromURL,
|
889
|
-
parseCarFile: () => parseCarFile,
|
890
|
-
registerStoreProtocol: () => registerStoreProtocol,
|
891
|
-
setCryptoKeyFromGatewayMetaPayload: () => setCryptoKeyFromGatewayMetaPayload,
|
892
|
-
testStoreFactory: () => testStoreFactory,
|
893
|
-
toCIDBlock: () => toCIDBlock,
|
894
|
-
toStoreRuntime: () => toStoreRuntime
|
895
|
-
});
|
896
|
-
|
897
|
-
// src/blockstore/types.ts
|
898
|
-
function toCIDBlock(block) {
|
899
|
-
return block;
|
900
|
-
}
|
901
|
-
|
902
|
-
// src/blockstore/store-factory.ts
|
903
|
-
var import_cement13 = require("@adviser/cement");
|
904
|
-
|
905
|
-
// src/runtime/files.ts
|
906
|
-
var files_exports = {};
|
907
|
-
__export(files_exports, {
|
908
|
-
decodeFile: () => decodeFile,
|
909
|
-
encodeFile: () => encodeFile
|
910
|
-
});
|
911
|
-
var UnixFS = __toESM(require("@ipld/unixfs"), 1);
|
912
|
-
var raw = __toESM(require("multiformats/codecs/raw"), 1);
|
913
|
-
var import_fixed = require("@ipld/unixfs/file/chunker/fixed");
|
914
|
-
var import_balanced = require("@ipld/unixfs/file/layout/balanced");
|
915
|
-
var import_ipfs_unixfs_exporter = require("@fireproof/vendor/ipfs-unixfs-exporter");
|
916
|
-
var queuingStrategy = UnixFS.withCapacity();
|
917
|
-
var settings = UnixFS.configure({
|
918
|
-
fileChunkEncoder: raw,
|
919
|
-
smallFileEncoder: raw,
|
920
|
-
chunker: (0, import_fixed.withMaxChunkSize)(1024 * 1024),
|
921
|
-
fileLayout: (0, import_balanced.withWidth)(1024)
|
922
|
-
});
|
923
|
-
async function collect(collectable) {
|
924
|
-
const chunks = [];
|
925
|
-
await collectable.pipeTo(
|
926
|
-
new WritableStream({
|
927
|
-
write(chunk) {
|
928
|
-
chunks.push(chunk);
|
929
|
-
}
|
930
|
-
})
|
931
|
-
);
|
932
|
-
return chunks;
|
508
|
+
function getStore(url, sthis, joiner) {
|
509
|
+
const store = url.getParam("store");
|
510
|
+
switch (store) {
|
511
|
+
case "data":
|
512
|
+
case "wal":
|
513
|
+
case "meta":
|
514
|
+
break;
|
515
|
+
default:
|
516
|
+
throw sthis.logger.Error().Url(url).Msg(`store not found`).AsError();
|
517
|
+
}
|
518
|
+
let name = store;
|
519
|
+
if (url.hasParam("index")) {
|
520
|
+
name = joiner(url.getParam("index") || "idx", name);
|
521
|
+
}
|
522
|
+
return { store, name };
|
933
523
|
}
|
934
|
-
|
935
|
-
const
|
936
|
-
|
937
|
-
return
|
524
|
+
function getKey(url, logger) {
|
525
|
+
const result = url.getParam("key");
|
526
|
+
if (!result) throw logger.Error().Str("url", url.toString()).Msg(`key not found`).AsError();
|
527
|
+
return result;
|
938
528
|
}
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
529
|
+
function getName(sthis, url) {
|
530
|
+
let result = url.getParam("name");
|
531
|
+
if (!result) {
|
532
|
+
result = sthis.pathOps.dirname(url.pathname);
|
533
|
+
if (result.length === 0) {
|
534
|
+
throw sthis.logger.Error().Str("url", url.toString()).Msg(`name not found`).AsError();
|
535
|
+
}
|
944
536
|
}
|
945
|
-
return
|
537
|
+
return result;
|
946
538
|
}
|
947
|
-
function
|
948
|
-
|
949
|
-
const unixfsWriter = UnixFS.createWriter({ writable, settings });
|
950
|
-
const fileBuilder = new UnixFSFileBuilder("", blob);
|
951
|
-
void (async () => {
|
952
|
-
await fileBuilder.finalize(unixfsWriter);
|
953
|
-
await unixfsWriter.close();
|
954
|
-
})();
|
955
|
-
return readable;
|
539
|
+
async function exceptionWrapper(fn) {
|
540
|
+
return fn().catch((e) => import_cement.Result.Err(e));
|
956
541
|
}
|
957
|
-
var
|
958
|
-
|
959
|
-
|
960
|
-
this.
|
961
|
-
this.#file = file;
|
962
|
-
}
|
963
|
-
async finalize(writer) {
|
964
|
-
const unixfsFileWriter = UnixFS.createFileWriter(writer);
|
965
|
-
await this.#file.stream().pipeTo(
|
966
|
-
new WritableStream({
|
967
|
-
async write(chunk) {
|
968
|
-
await unixfsFileWriter.write(chunk);
|
969
|
-
}
|
970
|
-
})
|
971
|
-
);
|
972
|
-
return await unixfsFileWriter.close();
|
542
|
+
var NotFoundError = class extends Error {
|
543
|
+
constructor() {
|
544
|
+
super(...arguments);
|
545
|
+
this.code = "ENOENT";
|
973
546
|
}
|
974
547
|
};
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
return value === false && value === null && value === void 0;
|
548
|
+
function isNotFoundError(e) {
|
549
|
+
if (import_cement.Result.Is(e)) {
|
550
|
+
if (e.isOk()) return false;
|
551
|
+
e = e.Err();
|
552
|
+
}
|
553
|
+
if (e.code === "ENOENT") return true;
|
554
|
+
return false;
|
983
555
|
}
|
984
|
-
function
|
985
|
-
if (
|
986
|
-
|
556
|
+
function dataDir(sthis, name, base) {
|
557
|
+
if (!base) {
|
558
|
+
if (!(0, import_cement.runtimeFn)().isBrowser) {
|
559
|
+
const home = sthis.env.get("HOME") || "./";
|
560
|
+
base = sthis.env.get("FP_STORAGE_URL") || `file://${sthis.pathOps.join(home, ".fireproof")}`;
|
561
|
+
} else {
|
562
|
+
base = sthis.env.get("FP_STORAGE_URL") || `indexdb://fp`;
|
563
|
+
}
|
987
564
|
}
|
988
|
-
return
|
565
|
+
return import_cement.URI.from(base.toString()).build().setParam("name", name || "").URI();
|
989
566
|
}
|
990
|
-
function
|
991
|
-
if (
|
992
|
-
return
|
567
|
+
function UInt8ArrayEqual(a, b) {
|
568
|
+
if (a.length !== b.length) {
|
569
|
+
return false;
|
993
570
|
}
|
994
|
-
|
571
|
+
for (let i = 0; i < a.length; i++) {
|
572
|
+
if (a[i] !== b[i]) {
|
573
|
+
return false;
|
574
|
+
}
|
575
|
+
}
|
576
|
+
return true;
|
995
577
|
}
|
996
578
|
|
997
|
-
// src/blockstore/store.ts
|
998
|
-
init_utils();
|
999
|
-
|
1000
579
|
// src/blockstore/loader.ts
|
1001
580
|
var import_p_limit = __toESM(require("p-limit"), 1);
|
1002
581
|
var import_reader = require("@fireproof/vendor/@ipld/car/reader");
|
1003
|
-
var
|
582
|
+
var import_cement5 = require("@adviser/cement");
|
1004
583
|
|
1005
584
|
// src/blockstore/loader-helpers.ts
|
1006
585
|
var import_sha2 = require("multiformats/hashes/sha2");
|
@@ -1020,7 +599,6 @@ async function parseCarFile(reader, logger) {
|
|
1020
599
|
// src/blockstore/transaction.ts
|
1021
600
|
var import_block3 = require("@fireproof/vendor/@web3-storage/pail/block");
|
1022
601
|
var import_cement2 = require("@adviser/cement");
|
1023
|
-
init_utils();
|
1024
602
|
var CarTransaction = class extends import_block3.MemoryBlockstore {
|
1025
603
|
constructor(parent, opts = { add: true, noLoader: false }) {
|
1026
604
|
super();
|
@@ -1289,14 +867,13 @@ __export(key_bag_exports, {
|
|
1289
867
|
getKeyBag: () => getKeyBag,
|
1290
868
|
registerKeyBagProviderFactory: () => registerKeyBagProviderFactory
|
1291
869
|
});
|
1292
|
-
var
|
1293
|
-
init_utils();
|
870
|
+
var import_cement4 = require("@adviser/cement");
|
1294
871
|
var import_base582 = require("multiformats/bases/base58");
|
1295
872
|
var KeyBag = class {
|
1296
873
|
constructor(rt) {
|
1297
874
|
this.rt = rt;
|
1298
|
-
this._warnOnce = new
|
1299
|
-
this._seq = new
|
875
|
+
this._warnOnce = new import_cement4.ResolveOnce();
|
876
|
+
this._seq = new import_cement4.ResolveSeq();
|
1300
877
|
this.logger = ensureLogger(rt.sthis, "KeyBag");
|
1301
878
|
this.logger.Debug().Msg("KeyBag created");
|
1302
879
|
}
|
@@ -1320,7 +897,7 @@ var KeyBag = class {
|
|
1320
897
|
async ensureKeyFromUrl(url, keyFactory) {
|
1321
898
|
const storeKey = url.getParam("storekey");
|
1322
899
|
if (storeKey === "insecure") {
|
1323
|
-
return
|
900
|
+
return import_cement4.Result.Ok(url);
|
1324
901
|
}
|
1325
902
|
if (!storeKey) {
|
1326
903
|
const keyName = `@${keyFactory()}@`;
|
@@ -1329,7 +906,7 @@ var KeyBag = class {
|
|
1329
906
|
return ret;
|
1330
907
|
}
|
1331
908
|
const urb = url.build().setParam("storekey", keyName);
|
1332
|
-
return
|
909
|
+
return import_cement4.Result.Ok(urb.URI());
|
1333
910
|
}
|
1334
911
|
if (storeKey.startsWith("@") && storeKey.endsWith("@")) {
|
1335
912
|
const ret = await this.getNamedKey(storeKey);
|
@@ -1337,13 +914,13 @@ var KeyBag = class {
|
|
1337
914
|
return ret;
|
1338
915
|
}
|
1339
916
|
}
|
1340
|
-
return
|
917
|
+
return import_cement4.Result.Ok(url);
|
1341
918
|
}
|
1342
919
|
async toKeyWithFingerPrint(keyStr) {
|
1343
920
|
const material = import_base582.base58btc.decode(keyStr);
|
1344
921
|
const key = await this.subtleKey(keyStr);
|
1345
922
|
const fpr = await this.rt.crypto.digestSHA256(material);
|
1346
|
-
return
|
923
|
+
return import_cement4.Result.Ok({
|
1347
924
|
key,
|
1348
925
|
fingerPrint: import_base582.base58btc.encode(new Uint8Array(fpr))
|
1349
926
|
});
|
@@ -1368,7 +945,7 @@ var KeyBag = class {
|
|
1368
945
|
return ret;
|
1369
946
|
}
|
1370
947
|
const named = ret.Ok();
|
1371
|
-
return
|
948
|
+
return import_cement4.Result.Ok({
|
1372
949
|
...named,
|
1373
950
|
extract: async () => {
|
1374
951
|
const ext = new Uint8Array(await this.rt.crypto.exportKey("raw", named.key));
|
@@ -1391,7 +968,7 @@ var KeyBag = class {
|
|
1391
968
|
}
|
1392
969
|
if (failIfNotFound) {
|
1393
970
|
this.logger.Debug().Str("id", id).Str("name", name).Msg("failIfNotFound getNamedKey");
|
1394
|
-
return
|
971
|
+
return import_cement4.Result.Err(new Error(`Key not found: ${name}`));
|
1395
972
|
}
|
1396
973
|
const ret = await this._setNamedKey(name, import_base582.base58btc.encode(this.rt.crypto.randomBytes(this.rt.keyLength)));
|
1397
974
|
this.logger.Debug().Str("id", id).Str("name", name).Result("fpr", ret).Msg("createKey getNamedKey-post");
|
@@ -1404,15 +981,15 @@ var keyBagProviderFactories = new Map(
|
|
1404
981
|
{
|
1405
982
|
protocol: "file:",
|
1406
983
|
factory: async (url, sthis) => {
|
1407
|
-
const {
|
1408
|
-
return new
|
984
|
+
const { KeyBagProviderImpl } = await import("@fireproof/core/node");
|
985
|
+
return new KeyBagProviderImpl(url, sthis);
|
1409
986
|
}
|
1410
987
|
},
|
1411
988
|
{
|
1412
989
|
protocol: "indexdb:",
|
1413
990
|
factory: async (url, sthis) => {
|
1414
|
-
const {
|
1415
|
-
return new
|
991
|
+
const { KeyBagProviderImpl } = await import("@fireproof/core/web");
|
992
|
+
return new KeyBagProviderImpl(url, sthis);
|
1416
993
|
}
|
1417
994
|
}
|
1418
995
|
].map((i) => [i.protocol, i])
|
@@ -1427,15 +1004,15 @@ function registerKeyBagProviderFactory(item) {
|
|
1427
1004
|
function defaultKeyBagUrl(sthis) {
|
1428
1005
|
let bagFnameOrUrl = sthis.env.get("FP_KEYBAG_URL");
|
1429
1006
|
let url;
|
1430
|
-
if ((0,
|
1431
|
-
url =
|
1007
|
+
if ((0, import_cement4.runtimeFn)().isBrowser) {
|
1008
|
+
url = import_cement4.URI.from(bagFnameOrUrl || "indexdb://fp-keybag");
|
1432
1009
|
} else {
|
1433
1010
|
if (!bagFnameOrUrl) {
|
1434
1011
|
const home = sthis.env.get("HOME");
|
1435
1012
|
bagFnameOrUrl = `${home}/.fireproof/keybag`;
|
1436
|
-
url =
|
1013
|
+
url = import_cement4.URI.from(`file://${bagFnameOrUrl}`);
|
1437
1014
|
} else {
|
1438
|
-
url =
|
1015
|
+
url = import_cement4.URI.from(bagFnameOrUrl);
|
1439
1016
|
}
|
1440
1017
|
}
|
1441
1018
|
const logger = ensureLogger(sthis, "defaultKeyBagUrl");
|
@@ -1449,7 +1026,7 @@ function defaultKeyBagOpts(sthis, kbo) {
|
|
1449
1026
|
const logger = ensureLogger(sthis, "KeyBag");
|
1450
1027
|
let url;
|
1451
1028
|
if (kbo.url) {
|
1452
|
-
url =
|
1029
|
+
url = import_cement4.URI.from(kbo.url);
|
1453
1030
|
logger.Debug().Url(url).Msg("from opts");
|
1454
1031
|
} else {
|
1455
1032
|
url = defaultKeyBagUrl(sthis);
|
@@ -1464,7 +1041,7 @@ function defaultKeyBagOpts(sthis, kbo) {
|
|
1464
1041
|
}
|
1465
1042
|
return {
|
1466
1043
|
url,
|
1467
|
-
crypto: kbo.crypto || (0,
|
1044
|
+
crypto: kbo.crypto || (0, import_cement4.toCryptoRuntime)({}),
|
1468
1045
|
sthis,
|
1469
1046
|
logger,
|
1470
1047
|
keyLength: kbo.keyLength || 16,
|
@@ -1474,7 +1051,7 @@ function defaultKeyBagOpts(sthis, kbo) {
|
|
1474
1051
|
}
|
1475
1052
|
};
|
1476
1053
|
}
|
1477
|
-
var _keyBags = new
|
1054
|
+
var _keyBags = new import_cement4.KeyedResolvOnce();
|
1478
1055
|
async function getKeyBag(sthis, kbo = {}) {
|
1479
1056
|
await sthis.start();
|
1480
1057
|
const rt = defaultKeyBagOpts(sthis, kbo);
|
@@ -1587,7 +1164,6 @@ async function prepareCarFiles(encoder, threshold, rootBlock, t) {
|
|
1587
1164
|
var import_sha23 = require("multiformats/hashes/sha2");
|
1588
1165
|
|
1589
1166
|
// src/blockstore/task-manager.ts
|
1590
|
-
init_utils();
|
1591
1167
|
var TaskManager = class {
|
1592
1168
|
constructor(sthis, callback) {
|
1593
1169
|
// we need to remove the events after some time
|
@@ -1659,7 +1235,7 @@ var Loader = class {
|
|
1659
1235
|
this.getBlockCache = /* @__PURE__ */ new Map();
|
1660
1236
|
this.seenMeta = /* @__PURE__ */ new Set();
|
1661
1237
|
this.writeLimit = (0, import_p_limit.default)(1);
|
1662
|
-
this.onceReady = new
|
1238
|
+
this.onceReady = new import_cement5.ResolveOnce();
|
1663
1239
|
this.name = name;
|
1664
1240
|
this.sthis = sthis;
|
1665
1241
|
this.ebOpts = defaultedBlockstoreRuntime(
|
@@ -1984,7 +1560,6 @@ __export(keyed_crypto_exports, {
|
|
1984
1560
|
BlockIvKeyIdCodec: () => BlockIvKeyIdCodec,
|
1985
1561
|
keyedCryptoFactory: () => keyedCryptoFactory
|
1986
1562
|
});
|
1987
|
-
init_utils();
|
1988
1563
|
var import_base583 = require("multiformats/bases/base58");
|
1989
1564
|
var import_sha24 = require("multiformats/hashes/sha2");
|
1990
1565
|
var CBOR = __toESM(require("@fireproof/vendor/cborg"), 1);
|
@@ -2152,10 +1727,9 @@ async function keyedCryptoFactory(url, kb, sthis) {
|
|
2152
1727
|
}
|
2153
1728
|
|
2154
1729
|
// src/blockstore/fragment-gateway.ts
|
2155
|
-
var
|
1730
|
+
var import_cement6 = require("@adviser/cement");
|
2156
1731
|
var import_base584 = require("multiformats/bases/base58");
|
2157
1732
|
var import_cborg = require("@fireproof/vendor/cborg");
|
2158
|
-
init_utils();
|
2159
1733
|
function getFragSize(url) {
|
2160
1734
|
const fragSize = url.getParam("fragSize");
|
2161
1735
|
let ret = 0;
|
@@ -2176,7 +1750,7 @@ async function getFrags(url, innerGW, headerSize, logger) {
|
|
2176
1750
|
}
|
2177
1751
|
const data = res.unwrap();
|
2178
1752
|
return [
|
2179
|
-
|
1753
|
+
import_cement6.Result.Ok({
|
2180
1754
|
fid: new Uint8Array(0),
|
2181
1755
|
ofs: 0,
|
2182
1756
|
len: data.length,
|
@@ -2190,7 +1764,7 @@ async function getFrags(url, innerGW, headerSize, logger) {
|
|
2190
1764
|
}
|
2191
1765
|
const firstFragment = (0, import_cborg.decode)(firstRaw.unwrap());
|
2192
1766
|
const blockSize = firstFragment.data.length;
|
2193
|
-
const ops = [Promise.resolve(
|
1767
|
+
const ops = [Promise.resolve(import_cement6.Result.Ok(firstFragment))];
|
2194
1768
|
const fidStr = import_base584.base58btc.encode(firstFragment.fid);
|
2195
1769
|
const fragUrl = url.build().setParam("fid", fidStr).setParam("len", firstFragment.len.toString()).setParam("headerSize", headerSize.toString());
|
2196
1770
|
for (let ofs = blockSize; ofs < firstFragment.len; ofs += blockSize) {
|
@@ -2202,12 +1776,12 @@ async function getFrags(url, innerGW, headerSize, logger) {
|
|
2202
1776
|
}
|
2203
1777
|
const fragment = (0, import_cborg.decode)(raw2.unwrap());
|
2204
1778
|
if (import_base584.base58btc.encode(fragment.fid) !== fidStr) {
|
2205
|
-
return
|
1779
|
+
return import_cement6.Result.Err(logger.Error().Msg("Fragment fid mismatch").AsError());
|
2206
1780
|
}
|
2207
1781
|
if (fragment.ofs !== ofs2) {
|
2208
|
-
return
|
1782
|
+
return import_cement6.Result.Err(logger.Error().Uint64("ofs", ofs2).Msg("Fragment ofs mismatch").AsError());
|
2209
1783
|
}
|
2210
|
-
return
|
1784
|
+
return import_cement6.Result.Ok(fragment);
|
2211
1785
|
})(fragUrl.setParam("ofs", ofs.toString()).URI(), ofs)
|
2212
1786
|
);
|
2213
1787
|
}
|
@@ -2269,40 +1843,40 @@ var FragmentGateway = class {
|
|
2269
1843
|
}
|
2270
1844
|
async put(url, body) {
|
2271
1845
|
await Promise.all(this.slicer(url, body));
|
2272
|
-
return
|
1846
|
+
return import_cement6.Result.Ok(void 0);
|
2273
1847
|
}
|
2274
1848
|
async get(url) {
|
2275
1849
|
const rfrags = await getFrags(url, this.innerGW, this.headerSize, this.logger);
|
2276
1850
|
let buffer = void 0;
|
2277
1851
|
for (const rfrag of rfrags) {
|
2278
1852
|
if (rfrag.isErr()) {
|
2279
|
-
return
|
1853
|
+
return import_cement6.Result.Err(rfrag.Err());
|
2280
1854
|
}
|
2281
1855
|
const frag = rfrag.Ok();
|
2282
1856
|
buffer = buffer || new Uint8Array(frag.len);
|
2283
1857
|
buffer.set(frag.data, frag.ofs);
|
2284
1858
|
}
|
2285
|
-
return
|
1859
|
+
return import_cement6.Result.Ok(buffer || new Uint8Array(0));
|
2286
1860
|
}
|
2287
1861
|
async subscribe(url, callback) {
|
2288
1862
|
if (this.innerGW.subscribe) {
|
2289
1863
|
return this.innerGW.subscribe(url, callback);
|
2290
1864
|
} else {
|
2291
|
-
return
|
1865
|
+
return import_cement6.Result.Err(this.logger.Error().Url(url).Msg("subscribe not supported").AsError());
|
2292
1866
|
}
|
2293
1867
|
}
|
2294
1868
|
async delete(url) {
|
2295
1869
|
const rfrags = await getFrags(url, this.innerGW, this.headerSize, this.logger);
|
2296
1870
|
for (const rfrag of rfrags) {
|
2297
1871
|
if (rfrag.isErr()) {
|
2298
|
-
return
|
1872
|
+
return import_cement6.Result.Err(rfrag.Err());
|
2299
1873
|
}
|
2300
1874
|
const frag = rfrag.Ok();
|
2301
1875
|
const fidStr = import_base584.base58btc.encode(frag.fid);
|
2302
1876
|
const fragUrl = url.build().setParam("fid", fidStr).setParam("len", frag.len.toString()).setParam("headerSize", this.headerSize.toString()).URI();
|
2303
1877
|
await this.innerGW.delete(fragUrl);
|
2304
1878
|
}
|
2305
|
-
return
|
1879
|
+
return import_cement6.Result.Ok(void 0);
|
2306
1880
|
}
|
2307
1881
|
};
|
2308
1882
|
|
@@ -2311,8 +1885,7 @@ var import_dag_json = require("@fireproof/vendor/@ipld/dag-json");
|
|
2311
1885
|
var import_clock = require("@fireproof/vendor/@web3-storage/pail/clock");
|
2312
1886
|
var import_multiformats2 = require("multiformats");
|
2313
1887
|
var import_base64 = require("multiformats/bases/base64");
|
2314
|
-
var
|
2315
|
-
init_utils();
|
1888
|
+
var import_cement7 = require("@adviser/cement");
|
2316
1889
|
async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
|
2317
1890
|
const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
|
2318
1891
|
if (!Array.isArray(crdtEntries)) {
|
@@ -2358,13 +1931,13 @@ async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
|
|
2358
1931
|
}
|
2359
1932
|
}
|
2360
1933
|
sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
|
2361
|
-
return
|
1934
|
+
return import_cement7.Result.Ok(dbMeta);
|
2362
1935
|
}
|
2363
1936
|
sthis.logger.Debug().Any("data", data).Msg("No crypto in gateway meta payload");
|
2364
|
-
return
|
1937
|
+
return import_cement7.Result.Ok(void 0);
|
2365
1938
|
} catch (error) {
|
2366
1939
|
sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
|
2367
|
-
return
|
1940
|
+
return import_cement7.Result.Err(error);
|
2368
1941
|
}
|
2369
1942
|
}
|
2370
1943
|
async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
|
@@ -2385,10 +1958,10 @@ async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
|
|
2385
1958
|
const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
|
2386
1959
|
const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
|
2387
1960
|
sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
|
2388
|
-
return
|
1961
|
+
return import_cement7.Result.Ok(encoded);
|
2389
1962
|
} catch (error) {
|
2390
1963
|
sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
|
2391
|
-
return
|
1964
|
+
return import_cement7.Result.Err(error);
|
2392
1965
|
}
|
2393
1966
|
}
|
2394
1967
|
function getStoreKeyName(url) {
|
@@ -2426,9 +1999,9 @@ var import_p_retry = __toESM(require("p-retry"), 1);
|
|
2426
1999
|
var import_p_map = __toESM(require("p-map"), 1);
|
2427
2000
|
function guardVersion(url) {
|
2428
2001
|
if (!url.hasParam("version")) {
|
2429
|
-
return
|
2002
|
+
return import_cement8.Result.Err(`missing version: ${url.toString()}`);
|
2430
2003
|
}
|
2431
|
-
return
|
2004
|
+
return import_cement8.Result.Ok(url);
|
2432
2005
|
}
|
2433
2006
|
var BaseStoreImpl = class {
|
2434
2007
|
constructor(name, url, opts, sthis, logger) {
|
@@ -2488,7 +2061,7 @@ var BaseStoreImpl = class {
|
|
2488
2061
|
}
|
2489
2062
|
if (this.ready) {
|
2490
2063
|
const fn = this.ready.bind(this);
|
2491
|
-
const ready = await (0,
|
2064
|
+
const ready = await (0, import_cement8.exception2Result)(fn);
|
2492
2065
|
if (ready.isErr()) {
|
2493
2066
|
await this.close();
|
2494
2067
|
return ready;
|
@@ -2570,7 +2143,7 @@ var MetaStoreImpl = class extends BaseStoreImpl {
|
|
2570
2143
|
async close() {
|
2571
2144
|
await this.gateway.close(this.url());
|
2572
2145
|
this._onClosed.forEach((fn) => fn());
|
2573
|
-
return
|
2146
|
+
return import_cement8.Result.Ok(void 0);
|
2574
2147
|
}
|
2575
2148
|
async destroy() {
|
2576
2149
|
return this.gateway.destroy(this.url());
|
@@ -2617,7 +2190,7 @@ var DataStoreImpl = class extends BaseStoreImpl {
|
|
2617
2190
|
async close() {
|
2618
2191
|
await this.gateway.close(this.url());
|
2619
2192
|
this._onClosed.forEach((fn) => fn());
|
2620
|
-
return
|
2193
|
+
return import_cement8.Result.Ok(void 0);
|
2621
2194
|
}
|
2622
2195
|
destroy() {
|
2623
2196
|
return this.gateway.destroy(this.url());
|
@@ -2627,7 +2200,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
|
|
2627
2200
|
constructor(loader, url, opts) {
|
2628
2201
|
super(loader.name, url, { ...opts }, loader.sthis, ensureLogger(loader.sthis, "WALStoreImpl"));
|
2629
2202
|
this.storeType = "wal";
|
2630
|
-
this._ready = new
|
2203
|
+
this._ready = new import_cement8.ResolveOnce();
|
2631
2204
|
this.walState = { operations: [], noLoaderOps: [], fileOperations: [] };
|
2632
2205
|
this.processing = void 0;
|
2633
2206
|
this.processQueue = new CommitQueue();
|
@@ -2798,7 +2371,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
|
|
2798
2371
|
async close() {
|
2799
2372
|
await this.gateway.close(this.url());
|
2800
2373
|
this._onClosed.forEach((fn) => fn());
|
2801
|
-
return
|
2374
|
+
return import_cement8.Result.Ok(void 0);
|
2802
2375
|
}
|
2803
2376
|
destroy() {
|
2804
2377
|
return this.gateway.destroy(this.url());
|
@@ -2806,7 +2379,6 @@ var WALStoreImpl = class extends BaseStoreImpl {
|
|
2806
2379
|
};
|
2807
2380
|
|
2808
2381
|
// src/blockstore/store-factory.ts
|
2809
|
-
init_utils();
|
2810
2382
|
function ensureIsIndex(url, isIndex) {
|
2811
2383
|
if (isIndex) {
|
2812
2384
|
return url.build().setParam("index", isIndex).URI();
|
@@ -2825,15 +2397,15 @@ function buildURL(optURL, loader) {
|
|
2825
2397
|
const obuItem = Array.from(storeFactory.values()).find((items) => items.overrideBaseURL);
|
2826
2398
|
let obuUrl;
|
2827
2399
|
if (obuItem && obuItem.overrideBaseURL) {
|
2828
|
-
obuUrl =
|
2400
|
+
obuUrl = import_cement9.URI.from(obuItem.overrideBaseURL);
|
2829
2401
|
}
|
2830
2402
|
const ret = ensureIsIndex(
|
2831
|
-
|
2403
|
+
import_cement9.URI.from(optURL || obuUrl || dataDir(loader.sthis, loader.name, storeOpts.stores?.base)),
|
2832
2404
|
storeOpts.isIndex
|
2833
2405
|
);
|
2834
2406
|
return ret;
|
2835
2407
|
}
|
2836
|
-
var onceGateway = new
|
2408
|
+
var onceGateway = new import_cement9.KeyedResolvOnce();
|
2837
2409
|
async function getGatewayFromURL(url, sthis) {
|
2838
2410
|
return onceGateway.get(url.toString()).once(async () => {
|
2839
2411
|
const item = storeFactory.get(url.protocol);
|
@@ -2875,7 +2447,7 @@ function registerStoreProtocol(item) {
|
|
2875
2447
|
storeFactory.delete(protocol);
|
2876
2448
|
};
|
2877
2449
|
}
|
2878
|
-
var onceDataStoreFactory = new
|
2450
|
+
var onceDataStoreFactory = new import_cement9.KeyedResolvOnce();
|
2879
2451
|
async function dataStoreFactory(loader) {
|
2880
2452
|
const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.data, loader)).build().setParam("store", "data").URI();
|
2881
2453
|
const sthis = ensureSuperLog(loader.sthis, "dataStoreFactory", { url: url.toString() });
|
@@ -2893,7 +2465,7 @@ async function dataStoreFactory(loader) {
|
|
2893
2465
|
return store;
|
2894
2466
|
});
|
2895
2467
|
}
|
2896
|
-
var onceMetaStoreFactory = new
|
2468
|
+
var onceMetaStoreFactory = new import_cement9.KeyedResolvOnce();
|
2897
2469
|
async function metaStoreFactory(loader) {
|
2898
2470
|
const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.meta, loader)).build().setParam("store", "meta").URI();
|
2899
2471
|
const sthis = ensureSuperLog(loader.sthis, "metaStoreFactory", { url: () => url.toString() });
|
@@ -2912,7 +2484,7 @@ async function metaStoreFactory(loader) {
|
|
2912
2484
|
return store;
|
2913
2485
|
});
|
2914
2486
|
}
|
2915
|
-
var onceRemoteWalFactory = new
|
2487
|
+
var onceRemoteWalFactory = new import_cement9.KeyedResolvOnce();
|
2916
2488
|
async function remoteWalFactory(loader) {
|
2917
2489
|
const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.wal, loader)).build().setParam("store", "wal").URI();
|
2918
2490
|
const sthis = ensureSuperLog(loader.sthis, "remoteWalFactory", { url: url.toString() });
|
@@ -2966,28 +2538,32 @@ function toStoreRuntime(opts, sthis) {
|
|
2966
2538
|
decodeFile: opts.decodeFile || decodeFile
|
2967
2539
|
};
|
2968
2540
|
}
|
2969
|
-
|
2970
|
-
|
2971
|
-
|
2972
|
-
|
2973
|
-
|
2974
|
-
|
2975
|
-
|
2976
|
-
|
2977
|
-
|
2978
|
-
|
2979
|
-
}
|
2980
|
-
|
2981
|
-
|
2982
|
-
|
2983
|
-
|
2984
|
-
|
2985
|
-
|
2986
|
-
|
2987
|
-
|
2988
|
-
|
2989
|
-
|
2990
|
-
});
|
2541
|
+
if ((0, import_cement9.runtimeFn)().isNodeIsh || (0, import_cement9.runtimeFn)().isDeno) {
|
2542
|
+
registerStoreProtocol({
|
2543
|
+
protocol: "file:",
|
2544
|
+
gateway: async (sthis) => {
|
2545
|
+
const { GatewayImpl } = await import("@fireproof/core/node");
|
2546
|
+
return new GatewayImpl(sthis);
|
2547
|
+
},
|
2548
|
+
test: async (sthis) => {
|
2549
|
+
const { GatewayTestImpl } = await import("@fireproof/core/node");
|
2550
|
+
return new GatewayTestImpl(sthis);
|
2551
|
+
}
|
2552
|
+
});
|
2553
|
+
}
|
2554
|
+
if ((0, import_cement9.runtimeFn)().isBrowser) {
|
2555
|
+
registerStoreProtocol({
|
2556
|
+
protocol: "indexdb:",
|
2557
|
+
gateway: async (sthis) => {
|
2558
|
+
const { GatewayImpl } = await import("@fireproof/core/web");
|
2559
|
+
return new GatewayImpl(sthis);
|
2560
|
+
},
|
2561
|
+
test: async (sthis) => {
|
2562
|
+
const { GatewayTestImpl } = await import("@fireproof/core/web");
|
2563
|
+
return new GatewayTestImpl(sthis);
|
2564
|
+
}
|
2565
|
+
});
|
2566
|
+
}
|
2991
2567
|
|
2992
2568
|
// src/blockstore/store-remote.ts
|
2993
2569
|
async function RemoteDataStore(sthis, name, url, opts) {
|
@@ -3362,7 +2938,7 @@ var import_sha26 = require("multiformats/hashes/sha2");
|
|
3362
2938
|
var codec2 = __toESM(require("@fireproof/vendor/@ipld/dag-cbor"), 1);
|
3363
2939
|
var import_charwise = __toESM(require("charwise"), 1);
|
3364
2940
|
var DbIndex = __toESM(require("prolly-trees/db-index"), 1);
|
3365
|
-
var
|
2941
|
+
var import_utils9 = require("prolly-trees/utils");
|
3366
2942
|
var import_cache = require("prolly-trees/cache");
|
3367
2943
|
var IndexTree = class {
|
3368
2944
|
};
|
@@ -3370,17 +2946,17 @@ function refCompare(aRef, bRef) {
|
|
3370
2946
|
if (Number.isNaN(aRef)) return -1;
|
3371
2947
|
if (Number.isNaN(bRef)) throw new Error("ref may not be Infinity or NaN");
|
3372
2948
|
if (aRef === Infinity) return 1;
|
3373
|
-
return (0,
|
2949
|
+
return (0, import_utils9.simpleCompare)(aRef, bRef);
|
3374
2950
|
}
|
3375
2951
|
function compare(a, b) {
|
3376
2952
|
const [aKey, aRef] = a;
|
3377
2953
|
const [bKey, bRef] = b;
|
3378
|
-
const comp = (0,
|
2954
|
+
const comp = (0, import_utils9.simpleCompare)(aKey, bKey);
|
3379
2955
|
if (comp !== 0) return comp;
|
3380
2956
|
return refCompare(aRef, bRef);
|
3381
2957
|
}
|
3382
|
-
var byKeyOpts = { cache: import_cache.nocache, chunker: (0,
|
3383
|
-
var byIdOpts = { cache: import_cache.nocache, chunker: (0,
|
2958
|
+
var byKeyOpts = { cache: import_cache.nocache, chunker: (0, import_utils9.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare };
|
2959
|
+
var byIdOpts = { cache: import_cache.nocache, chunker: (0, import_utils9.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare: import_utils9.simpleCompare };
|
3384
2960
|
function indexEntriesForChanges(changes, mapFn) {
|
3385
2961
|
const indexEntries = [];
|
3386
2962
|
changes.forEach(({ id: key, value, del }) => {
|
@@ -3479,7 +3055,6 @@ function encodeKey(key) {
|
|
3479
3055
|
}
|
3480
3056
|
|
3481
3057
|
// src/indexer.ts
|
3482
|
-
init_utils();
|
3483
3058
|
function index(sthis, { _crdt }, name, mapFn, meta) {
|
3484
3059
|
if (mapFn && meta) throw _crdt.logger.Error().Msg("cannot provide both mapFn and meta").AsError();
|
3485
3060
|
if (mapFn && mapFn.constructor.name !== "Function") throw _crdt.logger.Error().Msg("mapFn must be a function").AsError();
|
@@ -3688,7 +3263,7 @@ var Index = class {
|
|
3688
3263
|
// src/crdt-clock.ts
|
3689
3264
|
var import_clock3 = require("@fireproof/vendor/@web3-storage/pail/clock");
|
3690
3265
|
var import_crdt2 = require("@fireproof/vendor/@web3-storage/pail/crdt");
|
3691
|
-
var
|
3266
|
+
var import_cement10 = require("@adviser/cement");
|
3692
3267
|
|
3693
3268
|
// src/apply-head-queue.ts
|
3694
3269
|
function applyHeadQueue(worker, logger) {
|
@@ -3736,7 +3311,6 @@ function applyHeadQueue(worker, logger) {
|
|
3736
3311
|
}
|
3737
3312
|
|
3738
3313
|
// src/crdt-clock.ts
|
3739
|
-
init_utils();
|
3740
3314
|
var CRDTClock = class {
|
3741
3315
|
constructor(blockstore) {
|
3742
3316
|
// todo: track local and remote clocks independently, merge on read
|
@@ -3746,7 +3320,7 @@ var CRDTClock = class {
|
|
3746
3320
|
this.zoomers = /* @__PURE__ */ new Set();
|
3747
3321
|
this.watchers = /* @__PURE__ */ new Set();
|
3748
3322
|
this.emptyWatchers = /* @__PURE__ */ new Set();
|
3749
|
-
this._ready = new
|
3323
|
+
this._ready = new import_cement10.ResolveOnce();
|
3750
3324
|
this.blockstore = blockstore;
|
3751
3325
|
this.logger = ensureLogger(blockstore.sthis, "CRDTClock");
|
3752
3326
|
this.applyHeadQueue = applyHeadQueue(this.int_applyHead.bind(this), this.logger);
|
@@ -3856,11 +3430,10 @@ async function advanceBlocks(logger, newHead, tblocks, head) {
|
|
3856
3430
|
}
|
3857
3431
|
|
3858
3432
|
// src/crdt.ts
|
3859
|
-
init_utils();
|
3860
3433
|
var CRDT = class {
|
3861
3434
|
constructor(sthis, name, opts = {}) {
|
3862
3435
|
this.indexers = /* @__PURE__ */ new Map();
|
3863
|
-
this.onceReady = new
|
3436
|
+
this.onceReady = new import_cement11.ResolveOnce();
|
3864
3437
|
this.sthis = sthis;
|
3865
3438
|
this.name = name;
|
3866
3439
|
this.logger = ensureLogger(sthis, "CRDT");
|
@@ -3974,14 +3547,13 @@ var CRDT = class {
|
|
3974
3547
|
};
|
3975
3548
|
|
3976
3549
|
// src/database.ts
|
3977
|
-
init_utils();
|
3978
3550
|
var Database = class {
|
3979
3551
|
constructor(name, opts) {
|
3980
3552
|
this.opts = {};
|
3981
3553
|
this._listening = false;
|
3982
3554
|
this._listeners = /* @__PURE__ */ new Set();
|
3983
3555
|
this._noupdate_listeners = /* @__PURE__ */ new Set();
|
3984
|
-
this._ready = new
|
3556
|
+
this._ready = new import_cement12.ResolveOnce();
|
3985
3557
|
this.name = name;
|
3986
3558
|
this.opts = opts || this.opts;
|
3987
3559
|
this.sthis = ensureSuperThis(this.opts);
|
@@ -4188,14 +3760,39 @@ __export(runtime_exports, {
|
|
4188
3760
|
INDEXDB_VERSION: () => INDEXDB_VERSION,
|
4189
3761
|
files: () => files_exports,
|
4190
3762
|
getFileName: () => getFileName,
|
4191
|
-
getFileSystem: () => getFileSystem,
|
4192
3763
|
getPath: () => getPath,
|
4193
3764
|
kb: () => key_bag_exports,
|
4194
3765
|
kc: () => keyed_crypto_exports,
|
4195
3766
|
mf: () => wait_pr_multiformats_exports,
|
4196
|
-
runtimeFn: () =>
|
3767
|
+
runtimeFn: () => import_cement13.runtimeFn
|
4197
3768
|
});
|
4198
|
-
|
3769
|
+
|
3770
|
+
// src/runtime/gateways/file/node/utils.ts
|
3771
|
+
var import_core = require("@fireproof/core");
|
3772
|
+
function getPath(url, sthis) {
|
3773
|
+
const basePath = url.pathname;
|
3774
|
+
const name = url.getParam("name");
|
3775
|
+
if (name) {
|
3776
|
+
const version = url.getParam("version");
|
3777
|
+
if (!version) throw sthis.logger.Error().Url(url).Msg(`version not found`).AsError();
|
3778
|
+
return sthis.pathOps.join(basePath, version, name);
|
3779
|
+
}
|
3780
|
+
return sthis.pathOps.join(basePath);
|
3781
|
+
}
|
3782
|
+
function getFileName(url, sthis) {
|
3783
|
+
const key = url.getParam("key");
|
3784
|
+
if (!key) throw sthis.logger.Error().Url(url).Msg(`key not found`).AsError();
|
3785
|
+
const res = (0, import_core.getStore)(url, sthis, (...a) => a.join("-"));
|
3786
|
+
switch (res.store) {
|
3787
|
+
case "data":
|
3788
|
+
return sthis.pathOps.join(res.name, key + ".car");
|
3789
|
+
case "wal":
|
3790
|
+
case "meta":
|
3791
|
+
return sthis.pathOps.join(res.name, key + ".json");
|
3792
|
+
default:
|
3793
|
+
throw sthis.logger.Error().Url(url).Msg(`unsupported store type`).AsError();
|
3794
|
+
}
|
3795
|
+
}
|
4199
3796
|
|
4200
3797
|
// src/runtime/wait-pr-multiformats/index.ts
|
4201
3798
|
var wait_pr_multiformats_exports = {};
|
@@ -4208,17 +3805,16 @@ __export(wait_pr_multiformats_exports, {
|
|
4208
3805
|
var codec_interface_exports = {};
|
4209
3806
|
|
4210
3807
|
// src/runtime/index.ts
|
4211
|
-
var
|
4212
|
-
|
3808
|
+
var import_cement13 = require("@adviser/cement");
|
3809
|
+
|
3810
|
+
// src/runtime/gateways/file/version.ts
|
3811
|
+
var FILESTORE_VERSION = "v0.19-file";
|
4213
3812
|
|
4214
3813
|
// src/runtime/gateways/indexdb/version.ts
|
4215
3814
|
var INDEXDB_VERSION = "v0.19-indexdb";
|
4216
3815
|
|
4217
|
-
// src/index.ts
|
4218
|
-
init_utils();
|
4219
|
-
|
4220
3816
|
// src/version.ts
|
4221
3817
|
var PACKAGE_VERSION = Object.keys({
|
4222
|
-
"0.19.
|
3818
|
+
"0.19.122": "xxxx"
|
4223
3819
|
})[0];
|
4224
3820
|
//# sourceMappingURL=index.cjs.map
|