@fireproof/core 0.20.0-dev-preview-53 → 0.20.0-dev-preview-55

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/index.js CHANGED
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/ledger.ts
8
- import { BuildURI as BuildURI2, KeyedResolvOnce as KeyedResolvOnce6, ResolveOnce as ResolveOnce7, URI as URI13 } from "@adviser/cement";
8
+ import { BuildURI as BuildURI2, KeyedResolvOnce as KeyedResolvOnce5, ResolveOnce as ResolveOnce7, URI as URI13 } from "@adviser/cement";
9
9
 
10
10
  // src/utils.ts
11
11
  import {
@@ -511,70 +511,34 @@ __export(files_exports, {
511
511
  decodeFile: () => decodeFile,
512
512
  encodeFile: () => encodeFile
513
513
  });
514
- import * as UnixFS from "@ipld/unixfs";
515
514
  import * as raw from "multiformats/codecs/raw";
516
- import { withMaxChunkSize } from "@ipld/unixfs/file/chunker/fixed";
517
- import { withWidth } from "@ipld/unixfs/file/layout/balanced";
518
- import { exporter } from "ipfs-unixfs-exporter";
519
- var queuingStrategy = UnixFS.withCapacity();
520
- var settings = UnixFS.configure({
521
- fileChunkEncoder: raw,
522
- smallFileEncoder: raw,
523
- chunker: withMaxChunkSize(1024 * 1024),
524
- fileLayout: withWidth(1024)
525
- });
526
- async function collect(collectable) {
527
- const chunks = [];
528
- await collectable.pipeTo(
529
- new WritableStream({
530
- write(chunk) {
531
- chunks.push(chunk);
532
- }
533
- })
534
- );
535
- return chunks;
536
- }
515
+ import { CID } from "multiformats/cid";
516
+ import { sha256 as hasher } from "multiformats/hashes/sha2";
517
+ import { exception2Result, Result as Result2, top_uint8 } from "@adviser/cement";
537
518
  async function encodeFile(blob) {
538
- const readable = createFileEncoderStream(blob);
539
- const blocks = await collect(readable);
540
- return { cid: blocks.at(-1).cid, blocks };
519
+ const data = await top_uint8(blob);
520
+ const bytes = raw.encode(data);
521
+ const hash = await hasher.digest(bytes);
522
+ const cid = CID.create(1, raw.code, hash);
523
+ const block = { cid, bytes };
524
+ return { cid, blocks: [block] };
541
525
  }
542
- async function decodeFile(blocks, cid, meta) {
543
- const entry = await exporter(cid.toString(), blocks, { length: meta.size });
544
- const chunks = [];
545
- for await (const chunk of entry.content()) {
546
- chunks.push(chunk);
547
- }
548
- return new File(chunks, entry.name, { type: meta.type, lastModified: 0 });
526
+ function isHasBlockAGet(obj) {
527
+ return typeof obj.get === "function";
549
528
  }
550
- function createFileEncoderStream(blob) {
551
- const { readable, writable } = new TransformStream({}, queuingStrategy);
552
- const unixfsWriter = UnixFS.createWriter({ writable, settings });
553
- const fileBuilder = new UnixFSFileBuilder("", blob);
554
- void (async () => {
555
- await fileBuilder.finalize(unixfsWriter);
556
- await unixfsWriter.close();
557
- })();
558
- return readable;
529
+ async function decodeFile(blocks, cid, meta) {
530
+ if (!isHasBlockAGet(blocks)) {
531
+ return Result2.Err(new Error("Invalid block storage"));
532
+ }
533
+ return exception2Result(async () => {
534
+ const bytes = await blocks.get(cid);
535
+ const data = raw.decode(bytes);
536
+ return new File([data], "file", {
537
+ type: meta.type,
538
+ lastModified: meta.lastModified || 0
539
+ });
540
+ });
559
541
  }
560
- var UnixFSFileBuilder = class {
561
- #file;
562
- constructor(name, file) {
563
- this.name = name;
564
- this.#file = file;
565
- }
566
- async finalize(writer) {
567
- const unixfsFileWriter = UnixFS.createFileWriter(writer);
568
- await this.#file.stream().pipeTo(
569
- new WritableStream({
570
- async write(chunk) {
571
- await unixfsFileWriter.write(chunk);
572
- }
573
- })
574
- );
575
- return await unixfsFileWriter.close();
576
- }
577
- };
578
542
 
579
543
  // src/runtime/key-bag.ts
580
544
  var key_bag_exports = {};
@@ -591,7 +555,7 @@ import {
591
555
  KeyedResolvOnce,
592
556
  ResolveOnce as ResolveOnce2,
593
557
  ResolveSeq,
594
- Result as Result2,
558
+ Result as Result3,
595
559
  runtimeFn as runtimeFn2,
596
560
  toCryptoRuntime as toCryptoRuntime2,
597
561
  URI as URI3
@@ -721,7 +685,7 @@ async function toKeyWithFingerPrint(keybag, materialStrOrUint8) {
721
685
  }
722
686
  const key = await keybag.subtleKey(material);
723
687
  const fpr = await keybag.rt.crypto.digestSHA256(material);
724
- return Result2.Ok({
688
+ return Result3.Ok({
725
689
  key,
726
690
  fingerPrint: base58btc2.encode(new Uint8Array(fpr)),
727
691
  extract: async () => {
@@ -777,13 +741,13 @@ var keysByFingerprint = class _keysByFingerprint {
777
741
  def = !!def;
778
742
  const rKfp = await toKeyWithFingerPrint(this.keybag, materialStrOrUint8);
779
743
  if (rKfp.isErr()) {
780
- return Result2.Err(rKfp);
744
+ return Result3.Err(rKfp);
781
745
  }
782
746
  const kfp = rKfp.Ok();
783
747
  let found = this.keys[kfp.fingerPrint];
784
748
  if (found) {
785
749
  if (found.default === def) {
786
- return Result2.Ok({
750
+ return Result3.Ok({
787
751
  modified: false,
788
752
  kfp: found
789
753
  });
@@ -803,7 +767,7 @@ var keysByFingerprint = class _keysByFingerprint {
803
767
  if (keyBagAction) {
804
768
  this.keybag._upsertNamedKey(this);
805
769
  }
806
- return Result2.Ok({
770
+ return Result3.Ok({
807
771
  modified: keyBagAction && true,
808
772
  kfp: found
809
773
  });
@@ -882,24 +846,24 @@ var KeyBag = class {
882
846
  async ensureKeyFromUrl(url, keyFactory) {
883
847
  const storeKey = url.getParam(PARAM.STORE_KEY);
884
848
  if (storeKey === "insecure") {
885
- return Result2.Ok(url);
849
+ return Result3.Ok(url);
886
850
  }
887
851
  if (!storeKey) {
888
852
  const keyName = `@${keyFactory()}@`;
889
853
  const ret = await this.getNamedKey(keyName);
890
854
  if (ret.isErr()) {
891
- return Result2.Err(ret);
855
+ return Result3.Err(ret);
892
856
  }
893
857
  const urb = url.build().setParam(PARAM.STORE_KEY, keyName);
894
- return Result2.Ok(urb.URI());
858
+ return Result3.Ok(urb.URI());
895
859
  }
896
860
  if (storeKey.startsWith("@") && storeKey.endsWith("@")) {
897
861
  const ret = await this.getNamedKey(storeKey);
898
862
  if (ret.isErr()) {
899
- return Result2.Err(ret);
863
+ return Result3.Err(ret);
900
864
  }
901
865
  }
902
- return Result2.Ok(url);
866
+ return Result3.Ok(url);
903
867
  }
904
868
  async toKeysItem(ki) {
905
869
  if (!ki) return void 0;
@@ -958,7 +922,7 @@ var KeyBag = class {
958
922
  this._namedKeyCache.unget(ksi.name);
959
923
  }
960
924
  await bag.set(await ksi.asKeysItem());
961
- return Result2.Ok(ksi);
925
+ return Result3.Ok(ksi);
962
926
  });
963
927
  }
964
928
  async _getNamedKey(name, failIfNotFound, material) {
@@ -968,7 +932,7 @@ var KeyBag = class {
968
932
  const named = await this.toKeysItem(await bag.get(name));
969
933
  if (named) {
970
934
  this.logger.Debug().Str("id", id).Str("name", name).Any("fprs", Object.keys(named.keys)).Msg("fingerPrint getNamedKey");
971
- return Result2.Ok(await keysByFingerprint.from(this, named));
935
+ return Result3.Ok(await keysByFingerprint.from(this, named));
972
936
  }
973
937
  if (!named && failIfNotFound) {
974
938
  this._namedKeyCache.unget(name);
@@ -989,10 +953,10 @@ var KeyBag = class {
989
953
  }
990
954
  const res = await kp.upsert(keyMaterial, true);
991
955
  if (res.isErr()) {
992
- return Result2.Err(res);
956
+ return Result3.Err(res);
993
957
  }
994
958
  this.logger.Debug().Str("id", id).Str("name", name).Str("fpr", res.Ok().kfp.fingerPrint).Msg("createKey getNamedKey-post");
995
- return Result2.Ok(kp);
959
+ return Result3.Ok(kp);
996
960
  });
997
961
  }
998
962
  async getNamedKey(name, failIfNotFound = false, material) {
@@ -1110,10 +1074,9 @@ var FILESTORE_VERSION = "v0.19-file";
1110
1074
  var INDEXEDDB_VERSION = "v0.19-indexeddb";
1111
1075
 
1112
1076
  // src/runtime/gateways/file/gateway-impl.ts
1113
- import { exception2Result, KeyedResolvOnce as KeyedResolvOnce2, Result as Result3 } from "@adviser/cement";
1077
+ import { exception2Result as exception2Result2, KeyedResolvOnce as KeyedResolvOnce2, Result as Result4 } from "@adviser/cement";
1114
1078
 
1115
1079
  // src/runtime/gateways/file/utils.ts
1116
- import { getStore as getStore2 } from "@fireproof/core";
1117
1080
  function getPath(url, sthis) {
1118
1081
  const basePath = url.pathname;
1119
1082
  const name = url.getParam("name");
@@ -1125,7 +1088,7 @@ function getPath(url, sthis) {
1125
1088
  function getFileName(url, sthis) {
1126
1089
  const key = url.getParam("key");
1127
1090
  if (!key) throw sthis.logger.Error().Url(url).Msg(`key not found`).AsError();
1128
- const res = getStore2(url, sthis, (...a) => a.join("-"));
1091
+ const res = getStore(url, sthis, (...a) => a.join("-"));
1129
1092
  switch (res.fromUrl) {
1130
1093
  case "file":
1131
1094
  return sthis.pathOps.join(res.name, key);
@@ -1165,7 +1128,7 @@ var FileGateway = class {
1165
1128
  });
1166
1129
  }
1167
1130
  start(baseURL, sthis) {
1168
- return exception2Result(async () => {
1131
+ return exception2Result2(async () => {
1169
1132
  await this.fs.start();
1170
1133
  const url = baseURL.build();
1171
1134
  url.defParam(PARAM.VERSION, FILESTORE_VERSION);
@@ -1179,10 +1142,10 @@ var FileGateway = class {
1179
1142
  });
1180
1143
  }
1181
1144
  async buildUrl(baseUrl, key) {
1182
- return Result3.Ok(baseUrl.build().setParam(PARAM.KEY, key).URI());
1145
+ return Result4.Ok(baseUrl.build().setParam(PARAM.KEY, key).URI());
1183
1146
  }
1184
1147
  async close() {
1185
- return Result3.Ok(void 0);
1148
+ return Result4.Ok(void 0);
1186
1149
  }
1187
1150
  getFilePath(url, sthis) {
1188
1151
  const key = url.getParam(PARAM.KEY);
@@ -1190,7 +1153,7 @@ var FileGateway = class {
1190
1153
  return sthis.pathOps.join(getPath(url, sthis), getFileName(url, sthis));
1191
1154
  }
1192
1155
  async put(url, bytes, sthis) {
1193
- return exception2Result(async () => {
1156
+ return exception2Result2(async () => {
1194
1157
  const file = await this.getFilePath(url, sthis);
1195
1158
  sthis.logger.Debug().Str("url", url.toString()).Str("file", file).Msg("put");
1196
1159
  await this.fs.writefile(file, bytes);
@@ -1202,17 +1165,17 @@ var FileGateway = class {
1202
1165
  try {
1203
1166
  sthis.logger.Debug().Url(url).Str("file", file).Msg("get");
1204
1167
  const res = await this.fs.readfile(file);
1205
- return Result3.Ok(res);
1168
+ return Result4.Ok(res);
1206
1169
  } catch (e) {
1207
1170
  if (isNotFoundError(e)) {
1208
- return Result3.Err(new NotFoundError(`file not found: ${file}`));
1171
+ return Result4.Err(new NotFoundError(`file not found: ${file}`));
1209
1172
  }
1210
- return Result3.Err(e);
1173
+ return Result4.Err(e);
1211
1174
  }
1212
1175
  });
1213
1176
  }
1214
1177
  async delete(url, sthis) {
1215
- return exception2Result(async () => {
1178
+ return exception2Result2(async () => {
1216
1179
  await this.fs.unlink(this.getFilePath(url, sthis));
1217
1180
  });
1218
1181
  }
@@ -1238,7 +1201,7 @@ var FileGateway = class {
1238
1201
  }
1239
1202
  }
1240
1203
  }
1241
- return Result3.Ok(void 0);
1204
+ return Result4.Ok(void 0);
1242
1205
  }
1243
1206
  async getPlain(iurl, key, sthis) {
1244
1207
  const url = iurl.build().setParam(PARAM.KEY, key).URI();
@@ -1246,7 +1209,7 @@ var FileGateway = class {
1246
1209
  sthis.logger.Debug().Url(url).Str("dbFile", dbFile).Msg("get");
1247
1210
  const buffer = await this.fs.readfile(dbFile);
1248
1211
  sthis.logger.Debug().Url(url).Str("dbFile", dbFile).Len(buffer).Msg("got");
1249
- return Result3.Ok(buffer);
1212
+ return Result4.Ok(buffer);
1250
1213
  }
1251
1214
  };
1252
1215
 
@@ -1255,7 +1218,7 @@ var gateway_exports = {};
1255
1218
  __export(gateway_exports, {
1256
1219
  MemoryGateway: () => MemoryGateway
1257
1220
  });
1258
- import { Result as Result4 } from "@adviser/cement";
1221
+ import { Result as Result5 } from "@adviser/cement";
1259
1222
 
1260
1223
  // src/runtime/gateways/memory/version.ts
1261
1224
  var MEMORY_VERSION = "v0.19-memory";
@@ -1268,14 +1231,14 @@ var MemoryGateway = class {
1268
1231
  this.sthis = sthis;
1269
1232
  }
1270
1233
  buildUrl(baseUrl, key) {
1271
- return Promise.resolve(Result4.Ok(baseUrl.build().setParam(PARAM.KEY, key).URI()));
1234
+ return Promise.resolve(Result5.Ok(baseUrl.build().setParam(PARAM.KEY, key).URI()));
1272
1235
  }
1273
1236
  start(baseUrl) {
1274
- return Promise.resolve(Result4.Ok(baseUrl.build().setParam(PARAM.VERSION, MEMORY_VERSION).URI()));
1237
+ return Promise.resolve(Result5.Ok(baseUrl.build().setParam(PARAM.VERSION, MEMORY_VERSION).URI()));
1275
1238
  }
1276
1239
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
1277
1240
  close(baseUrl) {
1278
- return Promise.resolve(Result4.Ok(void 0));
1241
+ return Promise.resolve(Result5.Ok(void 0));
1279
1242
  }
1280
1243
  destroy(baseUrl) {
1281
1244
  const keyUrl = baseUrl.toString();
@@ -1285,38 +1248,38 @@ var MemoryGateway = class {
1285
1248
  }
1286
1249
  }
1287
1250
  this.memorys.clear();
1288
- return Promise.resolve(Result4.Ok(void 0));
1251
+ return Promise.resolve(Result5.Ok(void 0));
1289
1252
  }
1290
1253
  async put(url, bytes) {
1291
1254
  this.memorys.set(url.toString(), bytes);
1292
- return Result4.Ok(void 0);
1255
+ return Result5.Ok(void 0);
1293
1256
  }
1294
1257
  // get could return a NotFoundError if the key is not found
1295
1258
  get(url) {
1296
1259
  const x = this.memorys.get(url.toString());
1297
1260
  if (!x) {
1298
- return Promise.resolve(Result4.Err(new NotFoundError(`not found: ${url.toString()}`)));
1261
+ return Promise.resolve(Result5.Err(new NotFoundError(`not found: ${url.toString()}`)));
1299
1262
  }
1300
- return Promise.resolve(Result4.Ok(x));
1263
+ return Promise.resolve(Result5.Ok(x));
1301
1264
  }
1302
1265
  delete(url) {
1303
1266
  this.memorys.delete(url.toString());
1304
- return Promise.resolve(Result4.Ok(void 0));
1267
+ return Promise.resolve(Result5.Ok(void 0));
1305
1268
  }
1306
1269
  async getPlain(url, key) {
1307
1270
  const x = this.memorys.get(url.build().setParam(PARAM.KEY, key).toString());
1308
1271
  if (!x) {
1309
- return Result4.Err(new NotFoundError("not found"));
1272
+ return Result5.Err(new NotFoundError("not found"));
1310
1273
  }
1311
- return Result4.Ok(x);
1274
+ return Result5.Ok(x);
1312
1275
  }
1313
1276
  };
1314
1277
 
1315
1278
  // src/runtime/gateways/def-serde-gateway.ts
1316
- import { Result as Result7 } from "@adviser/cement";
1279
+ import { Result as Result8 } from "@adviser/cement";
1317
1280
 
1318
1281
  // src/blockstore/fp-envelope.ts
1319
- import { Result as Result5 } from "@adviser/cement";
1282
+ import { Result as Result6 } from "@adviser/cement";
1320
1283
  var FPEnvelopeTypes = {
1321
1284
  CAR: "car",
1322
1285
  FILE: "file",
@@ -1324,17 +1287,17 @@ var FPEnvelopeTypes = {
1324
1287
  WAL: "wal"
1325
1288
  };
1326
1289
  function Car2FPMsg(fpcar) {
1327
- return Result5.Ok({ type: FPEnvelopeTypes.CAR, payload: fpcar });
1290
+ return Result6.Ok({ type: FPEnvelopeTypes.CAR, payload: fpcar });
1328
1291
  }
1329
1292
  function File2FPMsg(fpfile) {
1330
- return Result5.Ok({ type: FPEnvelopeTypes.FILE, payload: fpfile });
1293
+ return Result6.Ok({ type: FPEnvelopeTypes.FILE, payload: fpfile });
1331
1294
  }
1332
1295
 
1333
1296
  // src/runtime/gateways/fp-envelope-serialize.ts
1334
- import { exception2Result as exception2Result2, Result as Result6 } from "@adviser/cement";
1297
+ import { exception2Result as exception2Result3, Result as Result7 } from "@adviser/cement";
1335
1298
  import { decodeEventBlock, EventBlock } from "@fireproof/vendor/@web3-storage/pail/clock";
1336
1299
  import { base64pad } from "multiformats/bases/base64";
1337
- import { CID } from "multiformats";
1300
+ import { CID as CID2 } from "multiformats";
1338
1301
  import { fromJSON } from "multiformats/link";
1339
1302
  import { format, parse } from "@ipld/dag-json";
1340
1303
  async function dbMetaEvent2Serialized(sthis, dbEvents) {
@@ -1370,10 +1333,10 @@ function WALState2Serialized(sthis, wal) {
1370
1333
  return serializedWAL;
1371
1334
  }
1372
1335
  var defaultEncoder = {
1373
- car: async (sthis, payload) => Result6.Ok(payload),
1374
- file: async (sthis, payload) => Result6.Ok(payload),
1375
- meta: async (sthis, payload) => Result6.Ok(sthis.txt.encode(JSON.stringify(payload))),
1376
- wal: async (sthis, payload) => Result6.Ok(sthis.txt.encode(JSON.stringify(payload)))
1336
+ car: async (sthis, payload) => Result7.Ok(payload),
1337
+ file: async (sthis, payload) => Result7.Ok(payload),
1338
+ meta: async (sthis, payload) => Result7.Ok(sthis.txt.encode(JSON.stringify(payload))),
1339
+ wal: async (sthis, payload) => Result7.Ok(sthis.txt.encode(JSON.stringify(payload)))
1377
1340
  };
1378
1341
  async function fpSerialize(sthis, env, pencoder) {
1379
1342
  const encoder = {
@@ -1395,20 +1358,20 @@ async function fpSerialize(sthis, env, pencoder) {
1395
1358
  }
1396
1359
  async function decode2DbMetaEvents(sthis, rserializedMeta) {
1397
1360
  if (rserializedMeta.isErr()) {
1398
- return Result6.Err(rserializedMeta.Err());
1361
+ return Result7.Err(rserializedMeta.Err());
1399
1362
  }
1400
1363
  const serializedMeta = rserializedMeta.unwrap();
1401
1364
  if (!Array.isArray(serializedMeta)) {
1402
1365
  return sthis.logger.Debug().Any("metaEntries", serializedMeta).Msg("No data in MetaEntries").ResultError();
1403
1366
  }
1404
- return Result6.Ok(
1367
+ return Result7.Ok(
1405
1368
  await Promise.all(
1406
1369
  serializedMeta.map(async (metaEntry) => {
1407
1370
  const eventBlock = await decodeEventBlock(base64pad.decode(metaEntry.data));
1408
1371
  const dbMeta = parse(sthis.txt.decode(eventBlock.value.data.dbMeta));
1409
1372
  return {
1410
1373
  eventCid: eventBlock.cid,
1411
- parents: metaEntry.parents.map((i) => CID.parse(i)),
1374
+ parents: metaEntry.parents.map((i) => CID2.parse(i)),
1412
1375
  dbMeta
1413
1376
  };
1414
1377
  })
@@ -1417,16 +1380,16 @@ async function decode2DbMetaEvents(sthis, rserializedMeta) {
1417
1380
  }
1418
1381
  function toCid(sthis, link) {
1419
1382
  if (typeof link === "string") {
1420
- return CID.parse(link);
1383
+ return CID2.parse(link);
1421
1384
  }
1422
1385
  return fromJSON(link);
1423
1386
  }
1424
1387
  async function decode2WalState(sthis, rserializedWAL) {
1425
1388
  if (rserializedWAL.isErr()) {
1426
- return Result6.Err(rserializedWAL.Err());
1389
+ return Result7.Err(rserializedWAL.Err());
1427
1390
  }
1428
1391
  const serializedWAL = rserializedWAL.unwrap();
1429
- return Result6.Ok({
1392
+ return Result7.Ok({
1430
1393
  fileOperations: (serializedWAL.fileOperations || []).map((fop) => ({
1431
1394
  cid: toCid(sthis, fop.cid),
1432
1395
  public: !!fop.public
@@ -1440,16 +1403,16 @@ async function decode2WalState(sthis, rserializedWAL) {
1440
1403
  });
1441
1404
  }
1442
1405
  var defaultDecoder = {
1443
- car: async (sthis, payload) => Result6.Ok(payload),
1444
- file: async (sthis, payload) => Result6.Ok(payload),
1445
- meta: async (sthis, payload) => exception2Result2(() => JSON.parse(sthis.txt.decode(payload))),
1446
- wal: async (sthis, payload) => exception2Result2(() => JSON.parse(sthis.txt.decode(payload)))
1406
+ car: async (sthis, payload) => Result7.Ok(payload),
1407
+ file: async (sthis, payload) => Result7.Ok(payload),
1408
+ meta: async (sthis, payload) => exception2Result3(() => JSON.parse(sthis.txt.decode(payload))),
1409
+ wal: async (sthis, payload) => exception2Result3(() => JSON.parse(sthis.txt.decode(payload)))
1447
1410
  };
1448
1411
  function makeFPEnvelope(type, payload) {
1449
1412
  if (payload.isErr()) {
1450
- return Result6.Err(payload.Err());
1413
+ return Result7.Err(payload.Err());
1451
1414
  }
1452
- return Result6.Ok({
1415
+ return Result7.Ok({
1453
1416
  type,
1454
1417
  payload: payload.unwrap()
1455
1418
  });
@@ -1457,7 +1420,7 @@ function makeFPEnvelope(type, payload) {
1457
1420
  async function fpDeserialize(sthis, url, intoRaw, pdecoder) {
1458
1421
  const rraw = await coercePromiseIntoUint8(intoRaw);
1459
1422
  if (rraw.isErr()) {
1460
- return Result6.Err(rraw.Err());
1423
+ return Result7.Err(rraw.Err());
1461
1424
  }
1462
1425
  const raw2 = rraw.unwrap();
1463
1426
  const decoder = {
@@ -1508,12 +1471,12 @@ var DefSerdeGateway = class {
1508
1471
  }
1509
1472
  async get({ loader: { sthis }, decoder }, url) {
1510
1473
  const res = await this.gw.get(url, sthis);
1511
- if (res.isErr()) return Result7.Err(res.Err());
1474
+ if (res.isErr()) return Result8.Err(res.Err());
1512
1475
  return fpDeserialize(sthis, url, res, decoder);
1513
1476
  }
1514
1477
  async subscribe({ loader: { sthis }, decoder }, url, callback) {
1515
1478
  function rawCallback(raw2) {
1516
- return fpDeserialize(sthis, url, Result7.Ok(raw2), decoder).then((res) => {
1479
+ return fpDeserialize(sthis, url, Result8.Ok(raw2), decoder).then((res) => {
1517
1480
  if (res.isErr()) {
1518
1481
  sthis.logger.Error().Err(res).Msg("Failed to deserialize");
1519
1482
  return;
@@ -1523,12 +1486,12 @@ var DefSerdeGateway = class {
1523
1486
  }
1524
1487
  if (!this.gw.subscribe) {
1525
1488
  if (!url.hasParam(PARAM.SELF_REFLECT)) {
1526
- return Result7.Ok(() => {
1489
+ return Result8.Ok(() => {
1527
1490
  });
1528
1491
  }
1529
1492
  const urlWithoutKey = url.build().delParam(PARAM.KEY).delParam(PARAM.SELF_REFLECT).toString();
1530
1493
  this.subscribeFn.set(urlWithoutKey, rawCallback);
1531
- return Result7.Ok(() => {
1494
+ return Result8.Ok(() => {
1532
1495
  this.subscribeFn.delete(urlWithoutKey);
1533
1496
  });
1534
1497
  }
@@ -1649,58 +1612,58 @@ __export(block_exports, {
1649
1612
  Block: () => Block,
1650
1613
  create: () => create,
1651
1614
  createUnsafe: () => createUnsafe,
1652
- decode: () => decode,
1653
- encode: () => encode
1615
+ decode: () => decode2,
1616
+ encode: () => encode2
1654
1617
  });
1655
- import { bytes as binary, CID as CID2 } from "multiformats";
1618
+ import { bytes as binary, CID as CID3 } from "multiformats";
1656
1619
  import { Block as mfBlock } from "multiformats/block";
1657
1620
  var Block = mfBlock;
1658
- async function decode({
1621
+ async function decode2({
1659
1622
  bytes,
1660
1623
  codec: codec3,
1661
- hasher: hasher6
1624
+ hasher: hasher7
1662
1625
  }) {
1663
1626
  if (bytes == null) throw new Error('Missing required argument "bytes"');
1664
- if (codec3 == null || hasher6 == null) throw new Error("Missing required argument: codec or hasher");
1627
+ if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
1665
1628
  const value = await Promise.resolve(codec3.decode(bytes));
1666
1629
  let toHash = bytes;
1667
1630
  if (codec3.valueToHashBytes) {
1668
1631
  toHash = await Promise.resolve(codec3.valueToHashBytes(value));
1669
1632
  }
1670
- const hash = await hasher6.digest(toHash);
1671
- const cid = CID2.create(1, codec3.code, hash);
1633
+ const hash = await hasher7.digest(toHash);
1634
+ const cid = CID3.create(1, codec3.code, hash);
1672
1635
  return new mfBlock({ value, bytes: toHash, cid });
1673
1636
  }
1674
- async function encode({
1637
+ async function encode2({
1675
1638
  value,
1676
1639
  codec: codec3,
1677
- hasher: hasher6
1640
+ hasher: hasher7
1678
1641
  }) {
1679
1642
  if (typeof value === "undefined") throw new Error('Missing required argument "value"');
1680
- if (codec3 == null || hasher6 == null) throw new Error("Missing required argument: codec or hasher");
1643
+ if (codec3 == null || hasher7 == null) throw new Error("Missing required argument: codec or hasher");
1681
1644
  let bytes;
1682
1645
  let hash;
1683
1646
  if (codec3.bytesToHash) {
1684
1647
  const hashable = await Promise.resolve(codec3.bytesToHash(value));
1685
- hash = await hasher6.digest(hashable);
1648
+ hash = await hasher7.digest(hashable);
1686
1649
  bytes = await Promise.resolve(codec3.encode(value));
1687
1650
  } else {
1688
1651
  bytes = await Promise.resolve(codec3.encode(value));
1689
- hash = await hasher6.digest(bytes);
1652
+ hash = await hasher7.digest(bytes);
1690
1653
  }
1691
- const cid = CID2.create(1, codec3.code, hash);
1654
+ const cid = CID3.create(1, codec3.code, hash);
1692
1655
  return new Block({ value, bytes, cid });
1693
1656
  }
1694
1657
  async function create({
1695
1658
  bytes,
1696
1659
  cid,
1697
- hasher: hasher6,
1660
+ hasher: hasher7,
1698
1661
  codec: codec3
1699
1662
  }) {
1700
1663
  if (bytes == null) throw new Error('Missing required argument "bytes"');
1701
- if (hasher6 == null) throw new Error('Missing required argument "hasher"');
1664
+ if (hasher7 == null) throw new Error('Missing required argument "hasher"');
1702
1665
  const value = await Promise.resolve(codec3.decode(bytes));
1703
- const hash = await hasher6.digest(bytes);
1666
+ const hash = await hasher7.digest(bytes);
1704
1667
  if (!binary.equals(cid.multihash.bytes, hash.bytes)) {
1705
1668
  throw new Error("CID hash does not match bytes");
1706
1669
  }
@@ -1727,7 +1690,7 @@ async function createUnsafe({
1727
1690
  }
1728
1691
 
1729
1692
  // src/indexer-helpers.ts
1730
- import { sha256 as hasher } from "multiformats/hashes/sha2";
1693
+ import { sha256 as hasher2 } from "multiformats/hashes/sha2";
1731
1694
  import * as codec from "@ipld/dag-cbor";
1732
1695
  import charwise from "charwise";
1733
1696
  import * as DbIndex from "prolly-trees/db-index";
@@ -1748,8 +1711,8 @@ function compare(a, b) {
1748
1711
  if (comp !== 0) return comp;
1749
1712
  return refCompare(aRef, bRef);
1750
1713
  }
1751
- var byKeyOpts = { cache, chunker: bf(30), codec, hasher, compare };
1752
- var byIdOpts = { cache, chunker: bf(30), codec, hasher, compare: simpleCompare };
1714
+ var byKeyOpts = { cache, chunker: bf(30), codec, hasher: hasher2, compare };
1715
+ var byIdOpts = { cache, chunker: bf(30), codec, hasher: hasher2, compare: simpleCompare };
1753
1716
  function indexEntriesForChanges(changes, mapFn) {
1754
1717
  const indexEntries = [];
1755
1718
  changes.forEach(({ id: key, value, del }) => {
@@ -1777,7 +1740,7 @@ function makeProllyGetBlock(blocks) {
1777
1740
  const block = await blocks.get(address);
1778
1741
  if (!block) throw new Error(`Missing block ${address.toString()}`);
1779
1742
  const { cid, bytes } = block;
1780
- return create({ cid, bytes, hasher, codec });
1743
+ return create({ cid, bytes, hasher: hasher2, codec });
1781
1744
  };
1782
1745
  }
1783
1746
  async function bulkIndex(logger, tblocks, inIndex, indexEntries, opts) {
@@ -2150,6 +2113,9 @@ var DatabaseImpl = class {
2150
2113
  const result = await this.ledger.writeQueue.push({ id, del: true });
2151
2114
  return { id, clock: result?.head, name: this.name };
2152
2115
  }
2116
+ async remove(id) {
2117
+ return this.del(id);
2118
+ }
2153
2119
  async changes(since = [], opts = {}) {
2154
2120
  await this.ready();
2155
2121
  this.logger.Debug().Any("since", since).Any("opts", opts).Msg("changes");
@@ -2216,6 +2182,7 @@ __export(blockstore_exports, {
2216
2182
  MetaActiveStore: () => MetaActiveStore,
2217
2183
  PassThroughGateway: () => PassThroughGateway,
2218
2184
  TaskManager: () => TaskManager,
2185
+ URIInterceptor: () => URIInterceptor,
2219
2186
  WALActiveStore: () => WALActiveStore,
2220
2187
  createAttachedStores: () => createAttachedStores,
2221
2188
  createDbMetaEvent: () => createDbMetaEvent,
@@ -2223,7 +2190,7 @@ __export(blockstore_exports, {
2223
2190
  ensureStoreEnDeFile: () => ensureStoreEnDeFile,
2224
2191
  getDefaultURI: () => getDefaultURI,
2225
2192
  getGatewayFactoryItem: () => getGatewayFactoryItem,
2226
- getStartedGateway: () => getStartedGateway,
2193
+ getInterceptableGateway: () => getInterceptableGateway,
2227
2194
  parseCarFile: () => parseCarFile,
2228
2195
  registerStoreProtocol: () => registerStoreProtocol,
2229
2196
  toCIDBlock: () => toCIDBlock,
@@ -2283,10 +2250,10 @@ var WALActiveStore = class extends BaseActiveStore {
2283
2250
  };
2284
2251
 
2285
2252
  // src/blockstore/store-factory.ts
2286
- import { KeyedResolvOnce as KeyedResolvOnce5, Result as Result10 } from "@adviser/cement";
2253
+ import { Result as Result11 } from "@adviser/cement";
2287
2254
 
2288
2255
  // src/blockstore/store.ts
2289
- import { exception2Result as exception2Result3, ResolveOnce as ResolveOnce4, Result as Result9 } from "@adviser/cement";
2256
+ import { exception2Result as exception2Result4, ResolveOnce as ResolveOnce4, Result as Result9 } from "@adviser/cement";
2290
2257
 
2291
2258
  // src/blockstore/loader.ts
2292
2259
  import pLimit from "p-limit";
@@ -2294,13 +2261,13 @@ import { CarReader } from "@ipld/car/reader";
2294
2261
  import { KeyedResolvOnce as KeyedResolvOnce4, LRUSet, ResolveOnce as ResolveOnce3 } from "@adviser/cement";
2295
2262
 
2296
2263
  // src/blockstore/loader-helpers.ts
2297
- import { sha256 as hasher2 } from "multiformats/hashes/sha2";
2264
+ import { sha256 as hasher3 } from "multiformats/hashes/sha2";
2298
2265
  import * as dagCodec from "@ipld/dag-cbor";
2299
2266
  async function parseCarFile(reader, logger) {
2300
2267
  const roots = await reader.roots;
2301
2268
  const header = reader.blocks.find((i) => i.cid.equals(roots[0]));
2302
2269
  if (!header) throw logger.Error().Msg("missing header block").AsError();
2303
- const dec = await decode({ bytes: header.bytes, hasher: hasher2, codec: dagCodec });
2270
+ const dec = await decode2({ bytes: header.bytes, hasher: hasher3, codec: dagCodec });
2304
2271
  const fpvalue = dec.value;
2305
2272
  if (fpvalue && !fpvalue.fp) {
2306
2273
  throw logger.Error().Msg("missing fp").AsError();
@@ -2611,7 +2578,7 @@ async function encodeCarFile(roots, t, codec3) {
2611
2578
  writer.write({ cid, bytes });
2612
2579
  }
2613
2580
  writer.close();
2614
- return await encode({ value: writer.bytes, hasher: sha256, codec: codec3 });
2581
+ return await encode2({ value: writer.bytes, hasher: sha256, codec: codec3 });
2615
2582
  }
2616
2583
  async function createCarFile(encoder, cid, t) {
2617
2584
  return encodeCarFile([cid], t, encoder);
@@ -2649,7 +2616,7 @@ function makeCarHeader(meta, cars, compact = false) {
2649
2616
  return { ...coreHeader, meta };
2650
2617
  }
2651
2618
  async function encodeCarHeader(fp) {
2652
- return await encode({
2619
+ return await encode2({
2653
2620
  value: { fp },
2654
2621
  hasher: sha256,
2655
2622
  codec: dagCodec2
@@ -2694,7 +2661,7 @@ async function prepareCarFiles(encoder, threshold, rootBlock, t) {
2694
2661
  }
2695
2662
 
2696
2663
  // src/blockstore/loader.ts
2697
- import { sha256 as hasher3 } from "multiformats/hashes/sha2";
2664
+ import { sha256 as hasher4 } from "multiformats/hashes/sha2";
2698
2665
 
2699
2666
  // src/blockstore/task-manager.ts
2700
2667
  var TaskManager = class {
@@ -3374,7 +3341,7 @@ var Loader = class {
3374
3341
  if (!loadedCar) {
3375
3342
  throw this.logger.Error().Url(store.local().url()).Str("cid", carCidStr).Msg("missing car files").AsError();
3376
3343
  }
3377
- const bytes = await decode({ bytes: loadedCar.bytes, hasher: hasher3, codec: (await activeStore.keyedCrypto()).codec() });
3344
+ const bytes = await decode2({ bytes: loadedCar.bytes, hasher: hasher4, codec: (await activeStore.keyedCrypto()).codec() });
3378
3345
  const rawReader = await CarReader.fromBytes(bytes.value.data);
3379
3346
  const blocks = [];
3380
3347
  for await (const block of rawReader.blocks()) {
@@ -3415,7 +3382,7 @@ __export(keyed_crypto_exports, {
3415
3382
  keyedCryptoFactory: () => keyedCryptoFactory
3416
3383
  });
3417
3384
  import { base58btc as base58btc3 } from "multiformats/bases/base58";
3418
- import { sha256 as hasher4 } from "multiformats/hashes/sha2";
3385
+ import { sha256 as hasher5 } from "multiformats/hashes/sha2";
3419
3386
  import * as CBOR from "cborg";
3420
3387
  var generateIV = {
3421
3388
  random: {
@@ -3430,7 +3397,7 @@ var generateIV = {
3430
3397
  },
3431
3398
  hash: {
3432
3399
  calc: async (ko, crypto, data) => {
3433
- const hash = await hasher4.digest(data);
3400
+ const hash = await hasher5.digest(data);
3434
3401
  const hashBytes = new Uint8Array(hash.bytes);
3435
3402
  const hashArray = new Uint8Array(ko.ivLength);
3436
3403
  for (let i = 0; i < hashBytes.length; i++) {
@@ -3616,146 +3583,6 @@ import { EventBlock as EventBlock2 } from "@fireproof/vendor/@web3-storage/pail/
3616
3583
  import { format as format2 } from "@ipld/dag-json";
3617
3584
  import pRetry from "p-retry";
3618
3585
  import pMap from "p-map";
3619
-
3620
- // src/blockstore/interceptor-gateway.ts
3621
- import { Result as Result8 } from "@adviser/cement";
3622
- var PassThroughGateway = class {
3623
- async buildUrl(ctx, url, key) {
3624
- const op = { url, key };
3625
- return Result8.Ok({ op });
3626
- }
3627
- async start(ctx, url) {
3628
- const op = { url };
3629
- return Result8.Ok({ op });
3630
- }
3631
- async close(ctx, url) {
3632
- const op = { url };
3633
- return Result8.Ok({ op });
3634
- }
3635
- async delete(ctx, url) {
3636
- const op = { url };
3637
- return Result8.Ok({ op });
3638
- }
3639
- async destroy(ctx, url) {
3640
- const op = { url };
3641
- return Result8.Ok({ op });
3642
- }
3643
- async put(ctx, url, body) {
3644
- const op = { url, body };
3645
- return Result8.Ok({ op });
3646
- }
3647
- async get(ctx, url) {
3648
- const op = { url };
3649
- return Result8.Ok({ op });
3650
- }
3651
- async subscribe(ctx, url, callback) {
3652
- const op = { url, callback };
3653
- return Result8.Ok({ op });
3654
- }
3655
- };
3656
- var passThrougthGateway = new PassThroughGateway();
3657
- var InterceptorGateway = class {
3658
- constructor(sthis, innerGW, interceptor) {
3659
- this.innerGW = innerGW;
3660
- this.interceptor = interceptor || passThrougthGateway;
3661
- }
3662
- async buildUrl(ctx, baseUrl, key) {
3663
- const rret = await this.interceptor.buildUrl(ctx, baseUrl, key);
3664
- if (rret.isErr()) {
3665
- return Result8.Err(rret.Err());
3666
- }
3667
- const ret = rret.unwrap();
3668
- if (ret.stop && ret.value) {
3669
- return ret.value;
3670
- }
3671
- return this.innerGW.buildUrl(ctx, ret.op.url, ret.op.key);
3672
- }
3673
- async destroy(ctx, iurl) {
3674
- const rret = await this.interceptor.destroy(ctx, iurl);
3675
- if (rret.isErr()) {
3676
- return Result8.Err(rret.Err());
3677
- }
3678
- const ret = rret.unwrap();
3679
- if (ret.stop && ret.value) {
3680
- return ret.value;
3681
- }
3682
- return this.innerGW.destroy(ctx, ret.op.url);
3683
- }
3684
- async start(ctx, url) {
3685
- const rret = await this.interceptor.start(ctx, url);
3686
- if (rret.isErr()) {
3687
- return Result8.Err(rret.Err());
3688
- }
3689
- const ret = rret.unwrap();
3690
- if (ret.stop && ret.value) {
3691
- return ret.value;
3692
- }
3693
- return await this.innerGW.start(ctx, ret.op.url);
3694
- }
3695
- async close(ctx, url) {
3696
- const rret = await this.interceptor.close(ctx, url);
3697
- if (rret.isErr()) {
3698
- return Result8.Err(rret.Err());
3699
- }
3700
- const ret = rret.unwrap();
3701
- if (ret.stop && ret.value) {
3702
- return ret.value;
3703
- }
3704
- return await this.innerGW.close(ctx, ret.op.url);
3705
- }
3706
- async put(ctx, url, fpEnv) {
3707
- const rret = await this.interceptor.put(ctx, url, fpEnv);
3708
- if (rret.isErr()) {
3709
- return Result8.Err(rret.Err());
3710
- }
3711
- const ret = rret.unwrap();
3712
- if (ret.stop && ret.value) {
3713
- return ret.value;
3714
- }
3715
- return this.innerGW.put(ctx, ret.op.url, ret.op.body);
3716
- }
3717
- async get(ctx, url) {
3718
- const rret = await this.interceptor.get(ctx, url);
3719
- if (rret.isErr()) {
3720
- return Result8.Err(rret.Err());
3721
- }
3722
- const ret = rret.unwrap();
3723
- if (ret.stop && ret.value) {
3724
- return ret.value;
3725
- }
3726
- return this.innerGW.get(ctx, ret.op.url);
3727
- }
3728
- async subscribe(ctx, url, callback) {
3729
- if (!this.innerGW.subscribe) {
3730
- return Result8.Err(ctx.loader.sthis.logger.Error().Url(url).Msg("subscribe not supported").AsError());
3731
- }
3732
- const rret = await this.interceptor.subscribe(ctx, url, callback);
3733
- if (rret.isErr()) {
3734
- return Result8.Err(rret.Err());
3735
- }
3736
- const ret = rret.unwrap();
3737
- if (ret.stop && ret.value) {
3738
- return ret.value;
3739
- }
3740
- return this.innerGW.subscribe(ctx, ret.op.url, ret.op.callback);
3741
- }
3742
- async delete(ctx, url) {
3743
- const rret = await this.interceptor.delete(ctx, url);
3744
- if (rret.isErr()) {
3745
- return Result8.Err(rret.Err());
3746
- }
3747
- const ret = rret.unwrap();
3748
- if (ret.stop && ret.value) {
3749
- return ret.value;
3750
- }
3751
- return this.innerGW.delete(ctx, url);
3752
- }
3753
- async getPlain(ctx, url, key) {
3754
- return this.innerGW.getPlain(ctx, url, key);
3755
- }
3756
- };
3757
-
3758
- // src/blockstore/store.ts
3759
3586
  function guardVersion(url) {
3760
3587
  if (!url.hasParam("version")) {
3761
3588
  return Result9.Err(`missing version: ${url.toString()}`);
@@ -3776,8 +3603,10 @@ var BaseStoreImpl = class {
3776
3603
  throw logger.Error().Url(this._url).Msg("missing name").AsError();
3777
3604
  }
3778
3605
  this.logger = logger.With().Str("this", this.sthis.nextId().str).Ref("url", () => this._url.toString()).Logger();
3779
- this.realGateway = opts.gateway;
3780
- this.gateway = new InterceptorGateway(this.sthis, opts.gateway, opts.gatewayInterceptor);
3606
+ this.gateway = opts.gateway;
3607
+ }
3608
+ get realGateway() {
3609
+ return this.gateway.innerGW;
3781
3610
  }
3782
3611
  url() {
3783
3612
  return this._url;
@@ -3820,7 +3649,7 @@ var BaseStoreImpl = class {
3820
3649
  }
3821
3650
  if (this.ready) {
3822
3651
  const fn = this.ready.bind(this);
3823
- const ready = await exception2Result3(fn);
3652
+ const ready = await exception2Result4(fn);
3824
3653
  if (ready.isErr()) {
3825
3654
  await this.close();
3826
3655
  return ready;
@@ -4081,7 +3910,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
4081
3910
  const retryableUpload = (fn, description) => pRetry(fn, {
4082
3911
  retries: 5,
4083
3912
  onFailedAttempt: (error) => {
4084
- this.logger.Warn().Msg(`Attempt ${error.attemptNumber} failed for ${description}. There are ${error.retriesLeft} retries left.`);
3913
+ this.logger.Warn().Any("error", error).Any("fn", fn.toString()).Msg(`Attempt ${error.attemptNumber} failed for ${description}. There are ${error.retriesLeft} retries left.`);
4085
3914
  }
4086
3915
  });
4087
3916
  try {
@@ -4204,81 +4033,200 @@ var WALStoreImpl = class extends BaseStoreImpl {
4204
4033
  }
4205
4034
  };
4206
4035
 
4207
- // src/blockstore/store-factory.ts
4208
- var onceGateway = new KeyedResolvOnce5();
4209
- var gatewayInstances = new KeyedResolvOnce5();
4210
- async function getStartedGateway(ctx, url) {
4211
- return onceGateway.get(url.toString()).once(async () => {
4212
- const item = getGatewayFactoryItem(url.protocol);
4213
- if (item) {
4214
- const ret = {
4215
- url,
4216
- ...await gatewayInstances.get(url.protocol).once(async () => ({
4217
- gateway: await item.serdegateway(ctx.loader.sthis)
4218
- }))
4219
- };
4220
- const res = await ret.gateway.start(ctx, url);
4221
- if (res.isErr()) {
4222
- return Result10.Err(ctx.loader.sthis.logger.Error().Result("start", res).Msg("start failed").AsError());
4223
- }
4224
- ret.url = res.Ok();
4225
- return Result10.Ok(ret);
4036
+ // src/blockstore/interceptor-gateway.ts
4037
+ import { Result as Result10 } from "@adviser/cement";
4038
+ var PassThroughGateway = class {
4039
+ async buildUrl(ctx, url, key) {
4040
+ const op = { url, key };
4041
+ return Result10.Ok({ op });
4042
+ }
4043
+ async start(ctx, url) {
4044
+ const op = { url };
4045
+ return Result10.Ok({ op });
4046
+ }
4047
+ async close(ctx, url) {
4048
+ const op = { url };
4049
+ return Result10.Ok({ op });
4050
+ }
4051
+ async delete(ctx, url) {
4052
+ const op = { url };
4053
+ return Result10.Ok({ op });
4054
+ }
4055
+ async destroy(ctx, url) {
4056
+ const op = { url };
4057
+ return Result10.Ok({ op });
4058
+ }
4059
+ async put(ctx, url, body) {
4060
+ const op = { url, body };
4061
+ return Result10.Ok({ op });
4062
+ }
4063
+ async get(ctx, url) {
4064
+ const op = { url };
4065
+ return Result10.Ok({ op });
4066
+ }
4067
+ async subscribe(ctx, url, callback) {
4068
+ const op = { url, callback };
4069
+ return Result10.Ok({ op });
4070
+ }
4071
+ };
4072
+ var passThrougthGateway = new PassThroughGateway();
4073
+ var InterceptorGateway = class {
4074
+ constructor(sthis, innerGW, interceptor) {
4075
+ this.innerGW = innerGW;
4076
+ this.interceptor = interceptor || passThrougthGateway;
4077
+ }
4078
+ async buildUrl(ctx, baseUrl, key) {
4079
+ const rret = await this.interceptor.buildUrl(ctx, baseUrl, key);
4080
+ if (rret.isErr()) {
4081
+ return Result10.Err(rret.Err());
4226
4082
  }
4227
- return Result10.Err(ctx.loader.sthis.logger.Warn().Url(url).Msg("unsupported protocol").AsError());
4228
- });
4083
+ const ret = rret.unwrap();
4084
+ if (ret.stop && ret.value) {
4085
+ return ret.value;
4086
+ }
4087
+ return this.innerGW.buildUrl(ctx, ret.op.url, ret.op.key);
4088
+ }
4089
+ async destroy(ctx, iurl) {
4090
+ const rret = await this.interceptor.destroy(ctx, iurl);
4091
+ if (rret.isErr()) {
4092
+ return Result10.Err(rret.Err());
4093
+ }
4094
+ const ret = rret.unwrap();
4095
+ if (ret.stop && ret.value) {
4096
+ return ret.value;
4097
+ }
4098
+ return this.innerGW.destroy(ctx, ret.op.url);
4099
+ }
4100
+ async start(ctx, url) {
4101
+ const rret = await this.interceptor.start(ctx, url);
4102
+ if (rret.isErr()) {
4103
+ return Result10.Err(rret.Err());
4104
+ }
4105
+ const ret = rret.unwrap();
4106
+ if (ret.stop && ret.value) {
4107
+ return ret.value;
4108
+ }
4109
+ return await this.innerGW.start(ctx, ret.op.url);
4110
+ }
4111
+ async close(ctx, url) {
4112
+ const rret = await this.interceptor.close(ctx, url);
4113
+ if (rret.isErr()) {
4114
+ return Result10.Err(rret.Err());
4115
+ }
4116
+ const ret = rret.unwrap();
4117
+ if (ret.stop && ret.value) {
4118
+ return ret.value;
4119
+ }
4120
+ return await this.innerGW.close(ctx, ret.op.url);
4121
+ }
4122
+ async put(ctx, url, fpEnv) {
4123
+ const rret = await this.interceptor.put(ctx, url, fpEnv);
4124
+ if (rret.isErr()) {
4125
+ return Result10.Err(rret.Err());
4126
+ }
4127
+ const ret = rret.unwrap();
4128
+ if (ret.stop && ret.value) {
4129
+ return ret.value;
4130
+ }
4131
+ return this.innerGW.put(ctx, ret.op.url, ret.op.body);
4132
+ }
4133
+ async get(ctx, url) {
4134
+ const rret = await this.interceptor.get(ctx, url);
4135
+ if (rret.isErr()) {
4136
+ return Result10.Err(rret.Err());
4137
+ }
4138
+ const ret = rret.unwrap();
4139
+ if (ret.stop && ret.value) {
4140
+ return ret.value;
4141
+ }
4142
+ return this.innerGW.get(ctx, ret.op.url);
4143
+ }
4144
+ async subscribe(ctx, url, callback) {
4145
+ if (!this.innerGW.subscribe) {
4146
+ return Result10.Err(ctx.loader.sthis.logger.Error().Url(url).Msg("subscribe not supported").AsError());
4147
+ }
4148
+ const rret = await this.interceptor.subscribe(ctx, url, callback);
4149
+ if (rret.isErr()) {
4150
+ return Result10.Err(rret.Err());
4151
+ }
4152
+ const ret = rret.unwrap();
4153
+ if (ret.stop && ret.value) {
4154
+ return ret.value;
4155
+ }
4156
+ return this.innerGW.subscribe(ctx, ret.op.url, ret.op.callback);
4157
+ }
4158
+ async delete(ctx, url) {
4159
+ const rret = await this.interceptor.delete(ctx, url);
4160
+ if (rret.isErr()) {
4161
+ return Result10.Err(rret.Err());
4162
+ }
4163
+ const ret = rret.unwrap();
4164
+ if (ret.stop && ret.value) {
4165
+ return ret.value;
4166
+ }
4167
+ return this.innerGW.delete(ctx, url);
4168
+ }
4169
+ async getPlain(ctx, url, key) {
4170
+ return this.innerGW.getPlain(ctx, url, key);
4171
+ }
4172
+ };
4173
+
4174
+ // src/blockstore/store-factory.ts
4175
+ async function getInterceptableGateway(ctx, url, opt) {
4176
+ const item = getGatewayFactoryItem(url.protocol);
4177
+ if (item) {
4178
+ return Result11.Ok(new InterceptorGateway(ctx.loader.sthis, await item.serdegateway(ctx.loader.sthis), opt.gatewayInterceptor));
4179
+ }
4180
+ return Result11.Err(ctx.loader.sthis.logger.Warn().Url(url).Msg("unsupported protocol").AsError());
4229
4181
  }
4230
4182
  async function carStoreFactory(ctx, uai) {
4231
4183
  const storeUrl = uai.url.build().setParam(PARAM.STORE, "car").URI();
4232
- const rgateway = await getStartedGateway(ctx, storeUrl);
4184
+ const rgateway = await getInterceptableGateway(ctx, storeUrl, uai);
4233
4185
  if (rgateway.isErr()) {
4234
4186
  throw ctx.loader.sthis.logger.Error().Result("err", rgateway).Url(uai.url).Msg("notfound").AsError();
4235
4187
  }
4236
4188
  const gateway = rgateway.Ok();
4237
- const store = new CarStoreImpl(ctx.loader.sthis, gateway.url, {
4238
- gateway: gateway.gateway,
4239
- gatewayInterceptor: uai.gatewayInterceptor,
4189
+ const store = new CarStoreImpl(ctx.loader.sthis, uai.url, {
4190
+ gateway,
4240
4191
  loader: ctx.loader
4241
4192
  });
4242
4193
  return store;
4243
4194
  }
4244
4195
  async function fileStoreFactory(ctx, uai) {
4245
4196
  const storeUrl = uai.url.build().setParam(PARAM.STORE, "file").URI();
4246
- const rgateway = await getStartedGateway(ctx, storeUrl);
4197
+ const rgateway = await getInterceptableGateway(ctx, storeUrl, uai);
4247
4198
  if (rgateway.isErr()) {
4248
4199
  throw ctx.loader.sthis.logger.Error().Result("err", rgateway).Url(uai.url).Msg("notfound").AsError();
4249
4200
  }
4250
4201
  const gateway = rgateway.Ok();
4251
- const store = new FileStoreImpl(ctx.loader.sthis, gateway.url, {
4252
- gateway: gateway.gateway,
4253
- gatewayInterceptor: uai.gatewayInterceptor,
4202
+ const store = new FileStoreImpl(ctx.loader.sthis, uai.url, {
4203
+ gateway,
4254
4204
  loader: ctx.loader
4255
4205
  });
4256
4206
  return store;
4257
4207
  }
4258
4208
  async function metaStoreFactory(ctx, uai) {
4259
4209
  const storeUrl = uai.url.build().setParam(PARAM.STORE, "meta").URI();
4260
- const rgateway = await getStartedGateway(ctx, storeUrl);
4210
+ const rgateway = await getInterceptableGateway(ctx, storeUrl, uai);
4261
4211
  if (rgateway.isErr()) {
4262
4212
  throw ctx.loader.sthis.logger.Error().Result("err", rgateway).Url(uai.url).Msg("notfound").AsError();
4263
4213
  }
4264
4214
  const gateway = rgateway.Ok();
4265
- const store = new MetaStoreImpl(ctx.loader.sthis, gateway.url, {
4266
- gateway: gateway.gateway,
4267
- gatewayInterceptor: uai.gatewayInterceptor,
4215
+ const store = new MetaStoreImpl(ctx.loader.sthis, uai.url, {
4216
+ gateway,
4268
4217
  loader: ctx.loader
4269
4218
  });
4270
4219
  return store;
4271
4220
  }
4272
4221
  async function WALStoreFactory(ctx, uai) {
4273
4222
  const storeUrl = uai.url.build().setParam(PARAM.STORE, "wal").URI();
4274
- const rgateway = await getStartedGateway(ctx, storeUrl);
4223
+ const rgateway = await getInterceptableGateway(ctx, storeUrl, uai);
4275
4224
  if (rgateway.isErr()) {
4276
4225
  throw ctx.loader.sthis.logger.Error().Result("err", rgateway).Url(uai.url).Msg("notfound").AsError();
4277
4226
  }
4278
4227
  const gateway = rgateway.Ok();
4279
- const store = new WALStoreImpl(ctx.loader.sthis, gateway.url, {
4280
- gateway: gateway.gateway,
4281
- gatewayInterceptor: uai.gatewayInterceptor,
4228
+ const store = new WALStoreImpl(ctx.loader.sthis, uai.url, {
4229
+ gateway,
4282
4230
  loader: ctx.loader
4283
4231
  });
4284
4232
  return store;
@@ -4326,9 +4274,60 @@ function toStoreRuntime(sthis, endeOpts = {}) {
4326
4274
  };
4327
4275
  }
4328
4276
 
4277
+ // src/blockstore/uri-interceptor.ts
4278
+ var URIInterceptor = class _URIInterceptor extends PassThroughGateway {
4279
+ static withMapper(mapper) {
4280
+ return new _URIInterceptor().addMapper(mapper);
4281
+ }
4282
+ #uriMapper = /* @__PURE__ */ new Set();
4283
+ addMapper(mapper) {
4284
+ this.#uriMapper.add(mapper);
4285
+ return this;
4286
+ }
4287
+ async #map(uri) {
4288
+ let ret = uri;
4289
+ for (const mapper of this.#uriMapper) {
4290
+ ret = await mapper(ret);
4291
+ }
4292
+ return ret;
4293
+ }
4294
+ async buildUrl(ctx, url, key) {
4295
+ const ret = await super.buildUrl(ctx, await this.#map(url), key);
4296
+ return ret;
4297
+ }
4298
+ async start(ctx, url) {
4299
+ const ret = await super.start(ctx, await this.#map(url));
4300
+ return ret;
4301
+ }
4302
+ async close(ctx, url) {
4303
+ const ret = await super.close(ctx, await this.#map(url));
4304
+ return ret;
4305
+ }
4306
+ async delete(ctx, url) {
4307
+ const ret = await super.delete(ctx, await this.#map(url));
4308
+ return ret;
4309
+ }
4310
+ async destroy(ctx, url) {
4311
+ const ret = await super.destroy(ctx, await this.#map(url));
4312
+ return ret;
4313
+ }
4314
+ async put(ctx, url, body) {
4315
+ const ret = await super.put(ctx, await this.#map(url), body);
4316
+ return ret;
4317
+ }
4318
+ async get(ctx, url) {
4319
+ const ret = await super.get(ctx, await this.#map(url));
4320
+ return ret;
4321
+ }
4322
+ async subscribe(ctx, url, callback) {
4323
+ const ret = await super.subscribe(ctx, await this.#map(url), callback);
4324
+ return ret;
4325
+ }
4326
+ };
4327
+
4329
4328
  // src/crdt-helpers.ts
4330
4329
  import { parse as parse2 } from "multiformats/link";
4331
- import { sha256 as hasher5 } from "multiformats/hashes/sha2";
4330
+ import { sha256 as hasher6 } from "multiformats/hashes/sha2";
4332
4331
  import * as codec2 from "@ipld/dag-cbor";
4333
4332
  import { put, get, entries, root } from "@fireproof/vendor/@web3-storage/pail/crdt";
4334
4333
  import { EventFetcher, vis } from "@fireproof/vendor/@web3-storage/pail/clock";
@@ -4415,7 +4414,7 @@ async function writeDocContent(store, blocks, update, logger) {
4415
4414
  await processFiles(store, blocks, update.value, logger);
4416
4415
  value = { doc: update.value };
4417
4416
  }
4418
- const block = await encode({ value, hasher: hasher5, codec: codec2 });
4417
+ const block = await encode2({ value, hasher: hasher6, codec: codec2 });
4419
4418
  blocks.putSync(block.cid, block.bytes);
4420
4419
  return block.cid;
4421
4420
  }
@@ -4446,11 +4445,11 @@ async function processFileset(logger, store, blocks, files) {
4446
4445
  for (const block of fileBlocks) {
4447
4446
  t.putSync(block.cid, block.bytes);
4448
4447
  }
4449
- files[filename] = { cid, type: file.type, size: file.size };
4448
+ files[filename] = { cid, type: file.type, size: file.size, lastModified: file.lastModified };
4450
4449
  } else {
4451
- const { cid, type, size, car } = files[filename];
4450
+ const { cid, type, size, car, lastModified } = files[filename];
4452
4451
  if (cid && type && size && car) {
4453
- files[filename] = { cid, type, size, car };
4452
+ files[filename] = { cid, type, size, car, lastModified };
4454
4453
  }
4455
4454
  }
4456
4455
  }
@@ -4490,15 +4489,21 @@ function readFileset(blocks, files, isPublic = false) {
4490
4489
  fileMeta.url = `https://${fileMeta.cid.toString()}.ipfs.w3s.link/`;
4491
4490
  }
4492
4491
  if (fileMeta.car) {
4493
- fileMeta.file = async () => await blocks.ebOpts.storeRuntime.decodeFile(
4494
- {
4495
- get: async (cid) => {
4496
- return await blocks.getFile(throwFalsy(fileMeta.car), cid);
4497
- }
4498
- },
4499
- fileMeta.cid,
4500
- fileMeta
4501
- );
4492
+ fileMeta.file = async () => {
4493
+ const result = await blocks.ebOpts.storeRuntime.decodeFile(
4494
+ {
4495
+ get: async (cid) => {
4496
+ return await blocks.getFile(throwFalsy(fileMeta.car), cid);
4497
+ }
4498
+ },
4499
+ fileMeta.cid,
4500
+ fileMeta
4501
+ );
4502
+ if (result.isErr()) {
4503
+ throw blocks.logger.Error().Any("error", result.Err()).Any("cid", fileMeta.cid).Msg("Error decoding file").AsError();
4504
+ }
4505
+ return result.unwrap();
4506
+ };
4502
4507
  }
4503
4508
  }
4504
4509
  files[filename] = fileMeta;
@@ -4507,7 +4512,7 @@ function readFileset(blocks, files, isPublic = false) {
4507
4512
  async function getValueFromLink(blocks, link, logger) {
4508
4513
  const block = await blocks.get(link);
4509
4514
  if (!block) throw logger.Error().Str("link", link.toString()).Msg(`Missing linked block`).AsError();
4510
- const { value } = await decode({ bytes: block.bytes, hasher: hasher5, codec: codec2 });
4515
+ const { value } = await decode2({ bytes: block.bytes, hasher: hasher6, codec: codec2 });
4511
4516
  const cvalue = {
4512
4517
  ...value,
4513
4518
  cid: link
@@ -4633,7 +4638,7 @@ async function doCompact(blockLog, head, logger) {
4633
4638
  async function getBlock(blocks, cidString) {
4634
4639
  const block = await blocks.get(parse2(cidString));
4635
4640
  if (!block) throw new Error(`Missing block ${cidString}`);
4636
- const { cid, value } = await decode({ bytes: block.bytes, codec: codec2, hasher: hasher5 });
4641
+ const { cid, value } = await decode2({ bytes: block.bytes, codec: codec2, hasher: hasher6 });
4637
4642
  return new Block({ cid, value, bytes: block.bytes });
4638
4643
  }
4639
4644
 
@@ -4973,7 +4978,7 @@ var Context = class {
4973
4978
  };
4974
4979
 
4975
4980
  // src/ledger.ts
4976
- var ledgers = new KeyedResolvOnce6();
4981
+ var ledgers = new KeyedResolvOnce5();
4977
4982
  function keyConfigOpts(sthis, name, opts) {
4978
4983
  return JSON.stringify(
4979
4984
  toSortedArray({
@@ -5257,7 +5262,7 @@ __export(file_exports, {
5257
5262
 
5258
5263
  // src/version.ts
5259
5264
  var PACKAGE_VERSION = Object.keys({
5260
- "0.20.0-dev-preview-53": "xxxx"
5265
+ "0.20.0-dev-preview-55": "xxxx"
5261
5266
  })[0];
5262
5267
  export {
5263
5268
  CRDTImpl,