@fireproof/core 0.19.11-dev-dryrun → 0.19.11-dev-dryrun2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. package/{chunk-MAK4D54P.js → chunk-OFGPKRCM.js} +11 -1
  2. package/chunk-OFGPKRCM.js.map +1 -0
  3. package/{chunk-XINRLWR3.js → chunk-WS3YRPIA.js} +4 -4
  4. package/{gateway-VWWKLHUI.js → gateway-5FCWPX5W.js} +3 -3
  5. package/{gateway-7OM6OSYK.js → gateway-H7UD6TNB.js} +2 -2
  6. package/index.cjs +331 -378
  7. package/index.cjs.map +1 -1
  8. package/index.d.cts +9 -5
  9. package/index.d.ts +9 -5
  10. package/index.global.js +270 -318
  11. package/index.global.js.map +1 -1
  12. package/index.js +273 -329
  13. package/index.js.map +1 -1
  14. package/{key-bag-file-DFMW6ZM6.js → key-bag-file-WADZBHYG.js} +3 -3
  15. package/{key-bag-indexdb-R2RWGSQ4.js → key-bag-indexdb-PGVAI3FJ.js} +3 -3
  16. package/{mem-filesystem-BZQZLUR6.js → mem-filesystem-YPPJV7Q2.js} +3 -3
  17. package/metafile-cjs.json +1 -1
  18. package/metafile-esm.json +1 -1
  19. package/metafile-iife.json +1 -1
  20. package/{node-filesystem-7YZR3POJ.js → node-filesystem-INX4ZTHE.js} +3 -3
  21. package/package.json +1 -1
  22. package/tests/fireproof/all-gateway.test.ts +12 -13
  23. package/tests/fireproof/utils.test.ts +19 -0
  24. package/tests/www/todo-local.html +1 -1
  25. package/tests/www/todo.html +12 -15
  26. package/{utils-AISQB3PB.js → utils-QO2HIWGI.js} +3 -3
  27. package/chunk-MAK4D54P.js.map +0 -1
  28. package/tests/fireproof/convert_uint8.py +0 -27
  29. /package/{chunk-XINRLWR3.js.map → chunk-WS3YRPIA.js.map} +0 -0
  30. /package/{gateway-VWWKLHUI.js.map → gateway-5FCWPX5W.js.map} +0 -0
  31. /package/{gateway-7OM6OSYK.js.map → gateway-H7UD6TNB.js.map} +0 -0
  32. /package/{key-bag-file-DFMW6ZM6.js.map → key-bag-file-WADZBHYG.js.map} +0 -0
  33. /package/{key-bag-indexdb-R2RWGSQ4.js.map → key-bag-indexdb-PGVAI3FJ.js.map} +0 -0
  34. /package/{mem-filesystem-BZQZLUR6.js.map → mem-filesystem-YPPJV7Q2.js.map} +0 -0
  35. /package/{node-filesystem-7YZR3POJ.js.map → node-filesystem-INX4ZTHE.js.map} +0 -0
  36. /package/tests/fireproof/cars/{bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js → bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts} +0 -0
  37. /package/{utils-AISQB3PB.js.map → utils-QO2HIWGI.js.map} +0 -0
package/index.global.js CHANGED
@@ -12058,6 +12058,16 @@ ${end.comment}` : end.comment;
12058
12058
  bin
12059
12059
  };
12060
12060
  }
12061
+ timeOrderedNextId(now) {
12062
+ now = typeof now === "number" ? now : (/* @__PURE__ */ new Date()).getTime();
12063
+ const t = (281474976710656 + now).toString(16).replace(/^1/, "");
12064
+ const bin = this.crypto.randomBytes(10);
12065
+ bin[1] = bin[1] & 240 | (bin[1] | 8 && 11);
12066
+ const hex = Array.from(bin).map((i) => i.toString(16).padStart(2, "0")).join("");
12067
+ return {
12068
+ str: `${t.slice(0, 8)}-${t.slice(8)}-7${hex.slice(0, 3)}-${hex.slice(3, 7)}-${hex.slice(7, 19)}`
12069
+ };
12070
+ }
12061
12071
  start() {
12062
12072
  return Promise.resolve();
12063
12073
  }
@@ -26262,7 +26272,7 @@ You can use close({ resize: true }) to resize header`);
26262
26272
  }
26263
26273
  async function prepareCarFiles(encoder2, threshold, rootBlock, t) {
26264
26274
  const carFiles = [];
26265
- threshold = threshold || 1e3 * 1e3;
26275
+ threshold = threshold || 128e3 * 8;
26266
26276
  let clonedt = new CarTransaction(t.parent, { add: false, noLoader: false });
26267
26277
  clonedt.putSync(rootBlock.cid, rootBlock.bytes);
26268
26278
  let newsize = blockLength(toCIDBlock(rootBlock));
@@ -26472,7 +26482,8 @@ You can use close({ resize: true }) to resize header`);
26472
26482
  carLog: this.carLog,
26473
26483
  carStore: fstore,
26474
26484
  WALStore: await this.WALStore(),
26475
- metaStore: await this.metaStore()
26485
+ metaStore: await this.metaStore(),
26486
+ threshold: this.ebOpts.threshold
26476
26487
  };
26477
26488
  return this.commitQueue.enqueue(async () => {
26478
26489
  await this.cacheTransaction(t);
@@ -26979,26 +26990,7 @@ You can use close({ resize: true }) to resize header`);
26979
26990
  if (this.innerGW.subscribe) {
26980
26991
  return this.innerGW.subscribe(url, callback);
26981
26992
  } else {
26982
- let lastData = void 0;
26983
- let interval = 100;
26984
- const fetchData = async () => {
26985
- const result = await this.innerGW.get(url);
26986
- if (result.isOk()) {
26987
- const data = result.Ok();
26988
- if (!lastData || !data.every((value, index2) => lastData && value === lastData[index2])) {
26989
- lastData = data;
26990
- callback(data);
26991
- interval = 100;
26992
- } else {
26993
- interval *= 2;
26994
- }
26995
- }
26996
- timeoutId = setTimeout(fetchData, interval);
26997
- };
26998
- let timeoutId = setTimeout(fetchData, interval);
26999
- return Result.Ok(() => {
27000
- clearTimeout(timeoutId);
27001
- });
26993
+ return Result.Err(this.logger.Error().Url(url).Msg("subscribe not supported").AsError());
27002
26994
  }
27003
26995
  }
27004
26996
  async delete(url) {
@@ -27016,6 +27008,109 @@ You can use close({ resize: true }) to resize header`);
27016
27008
  }
27017
27009
  };
27018
27010
 
27011
+ // src/blockstore/meta-key-helper.ts
27012
+ init_cement();
27013
+ async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
27014
+ const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
27015
+ if (!crdtEntries.length) {
27016
+ sthis.logger.Debug().Str("byteHeads", new TextDecoder().decode(byteHeads)).Msg("No CRDT entries found");
27017
+ return [];
27018
+ }
27019
+ if (!crdtEntries.map) {
27020
+ sthis.logger.Debug().Str("crdtEntries", JSON.stringify(crdtEntries)).Msg("No data in CRDT entries");
27021
+ return [];
27022
+ }
27023
+ return Promise.all(
27024
+ crdtEntries.map(async (crdtEntry) => {
27025
+ const eventBlock = await decodeEventBlock(base64pad.decode(crdtEntry.data));
27026
+ const dbMeta = parse3(sthis.txt.decode(eventBlock.value.data.dbMeta));
27027
+ return {
27028
+ eventCid: eventBlock.cid,
27029
+ parents: crdtEntry.parents,
27030
+ dbMeta
27031
+ };
27032
+ })
27033
+ );
27034
+ }
27035
+ async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
27036
+ try {
27037
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
27038
+ const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
27039
+ if (keyInfo.length) {
27040
+ const dbMeta = keyInfo[0].dbMeta;
27041
+ if (dbMeta.key) {
27042
+ const kb = await getKeyBag(sthis);
27043
+ const keyName = getStoreKeyName(uri);
27044
+ const res = await kb.setNamedKey(keyName, dbMeta.key);
27045
+ if (res.isErr()) {
27046
+ sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
27047
+ throw res.Err();
27048
+ }
27049
+ }
27050
+ sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
27051
+ return Result.Ok(dbMeta);
27052
+ }
27053
+ sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
27054
+ return Result.Ok(void 0);
27055
+ } catch (error) {
27056
+ sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
27057
+ return Result.Err(error);
27058
+ }
27059
+ }
27060
+ async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
27061
+ try {
27062
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
27063
+ const keyName = getStoreKeyName(uri);
27064
+ const kb = await getKeyBag(sthis);
27065
+ const res = await kb.getNamedExtractableKey(keyName, true);
27066
+ if (res.isErr()) {
27067
+ sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
27068
+ throw res.Err();
27069
+ }
27070
+ const keyData = await res.Ok().extract();
27071
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
27072
+ const { dbMeta, parents } = dbMetas[0];
27073
+ const parentLinks = parents.map((p) => CID.parse(p));
27074
+ dbMeta.key = keyData.keyStr;
27075
+ const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
27076
+ const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
27077
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
27078
+ return Result.Ok(encoded);
27079
+ } catch (error) {
27080
+ sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
27081
+ return Result.Err(error);
27082
+ }
27083
+ }
27084
+ function getStoreKeyName(url) {
27085
+ const storeKeyName = [url.getParam("localName") || url.getParam("name")];
27086
+ const idx = url.getParam("index");
27087
+ if (idx) {
27088
+ storeKeyName.push(idx);
27089
+ }
27090
+ storeKeyName.push("data");
27091
+ return `@${storeKeyName.join(":")}@`;
27092
+ }
27093
+ async function createDbMetaEventBlock(sthis, dbMeta, parents) {
27094
+ const event = await EventBlock.create(
27095
+ {
27096
+ dbMeta: sthis.txt.encode(format2(dbMeta))
27097
+ },
27098
+ parents
27099
+ );
27100
+ return event;
27101
+ }
27102
+ async function encodeEventsWithParents(sthis, events, parents) {
27103
+ const crdtEntries = events.map((event) => {
27104
+ const base64String = base64pad.encode(event.bytes);
27105
+ return {
27106
+ cid: event.cid.toString(),
27107
+ data: base64String,
27108
+ parents: parents.map((p) => p.toString())
27109
+ };
27110
+ });
27111
+ return sthis.txt.encode(JSON.stringify(crdtEntries));
27112
+ }
27113
+
27019
27114
  // src/blockstore/store.ts
27020
27115
  function guardVersion(url) {
27021
27116
  if (!url.hasParam("version")) {
@@ -27044,6 +27139,9 @@ You can use close({ resize: true }) to resize header`);
27044
27139
  onClosed(fn) {
27045
27140
  this._onClosed.push(fn);
27046
27141
  }
27142
+ async ready() {
27143
+ return;
27144
+ }
27047
27145
  async keyedCrypto() {
27048
27146
  return keyedCryptoFactory(this._url, await this.keybag(), this.sthis);
27049
27147
  }
@@ -27089,18 +27187,87 @@ You can use close({ resize: true }) to resize header`);
27089
27187
  return version;
27090
27188
  }
27091
27189
  };
27190
+ var MetaStoreImpl = class extends BaseStoreImpl {
27191
+ // remote: boolean;
27192
+ constructor(sthis, name6, url, opts) {
27193
+ super(name6, url, { ...opts }, sthis, ensureLogger(sthis, "MetaStoreImpl"));
27194
+ this.storeType = "meta";
27195
+ this.subscribers = /* @__PURE__ */ new Map();
27196
+ this.parents = [];
27197
+ if (
27198
+ /*this.remote && */
27199
+ opts.gateway.subscribe
27200
+ ) {
27201
+ this.onStarted(async () => {
27202
+ this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
27203
+ opts.gateway.subscribe?.(this.url(), async (message2) => {
27204
+ this.logger.Debug().Msg("Received message from gateway");
27205
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message2);
27206
+ await Promise.all(
27207
+ dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
27208
+ );
27209
+ this.updateParentsFromDbMetas(dbMetas);
27210
+ });
27211
+ });
27212
+ }
27213
+ }
27214
+ updateParentsFromDbMetas(dbMetas) {
27215
+ const cids = dbMetas.map((m) => m.eventCid);
27216
+ const dbMetaParents = dbMetas.flatMap((m) => m.parents);
27217
+ const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
27218
+ const dbMetaParentsSet = new Set(dbMetaParents.map((p) => p.toString()));
27219
+ this.parents = Array.from(uniqueParentsMap.values()).filter((p) => !dbMetaParentsSet.has(p.toString()));
27220
+ }
27221
+ async handleByteHeads(byteHeads) {
27222
+ return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
27223
+ }
27224
+ async load() {
27225
+ const branch = "main";
27226
+ const url = await this.gateway.buildUrl(this.url(), branch);
27227
+ if (url.isErr()) {
27228
+ throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
27229
+ }
27230
+ const bytes = await this.gateway.get(url.Ok());
27231
+ if (bytes.isErr()) {
27232
+ if (isNotFoundError(bytes)) {
27233
+ return void 0;
27234
+ }
27235
+ throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
27236
+ }
27237
+ const dbMetas = await this.handleByteHeads(bytes.Ok());
27238
+ await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
27239
+ this.updateParentsFromDbMetas(dbMetas);
27240
+ return dbMetas.map((m) => m.dbMeta);
27241
+ }
27242
+ async save(meta, branch) {
27243
+ branch = branch || "main";
27244
+ this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
27245
+ const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
27246
+ const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
27247
+ const url = await this.gateway.buildUrl(this.url(), branch);
27248
+ if (url.isErr()) {
27249
+ throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
27250
+ }
27251
+ this.parents = [event.cid];
27252
+ const res = await this.gateway.put(url.Ok(), bytes);
27253
+ if (res.isErr()) {
27254
+ throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
27255
+ }
27256
+ return res;
27257
+ }
27258
+ async close() {
27259
+ await this.gateway.close(this.url());
27260
+ this._onClosed.forEach((fn) => fn());
27261
+ return Result.Ok(void 0);
27262
+ }
27263
+ async destroy() {
27264
+ return this.gateway.destroy(this.url());
27265
+ }
27266
+ };
27092
27267
  var DataStoreImpl = class extends BaseStoreImpl {
27093
27268
  // readonly tag: string = "car-base";
27094
27269
  constructor(sthis, name6, url, opts) {
27095
- super(
27096
- name6,
27097
- url,
27098
- {
27099
- ...opts
27100
- },
27101
- sthis,
27102
- ensureLogger(sthis, "DataStoreImpl")
27103
- );
27270
+ super(name6, url, { ...opts }, sthis, ensureLogger(sthis, "DataStoreImpl"));
27104
27271
  this.storeType = "data";
27105
27272
  }
27106
27273
  async load(cid) {
@@ -27146,46 +27313,43 @@ You can use close({ resize: true }) to resize header`);
27146
27313
  };
27147
27314
  var WALStoreImpl = class extends BaseStoreImpl {
27148
27315
  constructor(loader, url, opts) {
27149
- super(
27150
- loader.name,
27151
- url,
27152
- {
27153
- ...opts
27154
- },
27155
- loader.sthis,
27156
- ensureLogger(loader.sthis, "WALStoreImpl")
27157
- );
27316
+ super(loader.name, url, { ...opts }, loader.sthis, ensureLogger(loader.sthis, "WALStoreImpl"));
27158
27317
  this.storeType = "wal";
27159
27318
  this._ready = new ResolveOnce();
27160
27319
  this.walState = { operations: [], noLoaderOps: [], fileOperations: [] };
27161
27320
  this.processing = void 0;
27162
27321
  this.processQueue = new CommitQueue();
27163
- this.ready = async () => {
27164
- return this._ready.once(async () => {
27165
- const walState = await this.load().catch((e) => {
27166
- this.logger.Error().Any("error", e).Msg("error loading wal");
27167
- return void 0;
27168
- });
27169
- if (!walState) {
27170
- this.walState.operations = [];
27171
- this.walState.fileOperations = [];
27172
- } else {
27173
- this.walState.operations = walState.operations || [];
27174
- this.walState.fileOperations = walState.fileOperations || [];
27175
- }
27176
- });
27177
- };
27178
27322
  this.loader = loader;
27179
27323
  }
27324
+ async ready() {
27325
+ return this._ready.once(async () => {
27326
+ const walState = await this.load().catch((e) => {
27327
+ this.logger.Error().Any("error", e).Msg("error loading wal");
27328
+ return void 0;
27329
+ });
27330
+ if (!walState) {
27331
+ this.walState.operations = [];
27332
+ this.walState.fileOperations = [];
27333
+ } else {
27334
+ this.walState.operations = walState.operations || [];
27335
+ this.walState.fileOperations = walState.fileOperations || [];
27336
+ }
27337
+ });
27338
+ }
27180
27339
  async enqueue(dbMeta, opts) {
27181
27340
  await this.ready();
27182
- if (opts.noLoader) {
27341
+ if (opts.compact) {
27342
+ this.walState.operations = [];
27343
+ this.walState.noLoaderOps = [dbMeta];
27344
+ } else if (opts.noLoader) {
27183
27345
  this.walState.noLoaderOps.push(dbMeta);
27184
27346
  } else {
27185
27347
  this.walState.operations.push(dbMeta);
27186
27348
  }
27187
27349
  await this.save(this.walState);
27188
- void this.process();
27350
+ if (!opts.noLoader) {
27351
+ void this.process();
27352
+ }
27189
27353
  }
27190
27354
  async enqueueFile(fileCid, publicFile = false) {
27191
27355
  await this.ready();
@@ -27212,7 +27376,7 @@ You can use close({ resize: true }) to resize header`);
27212
27376
  const fileOperations = [...this.walState.fileOperations];
27213
27377
  const uploads = [];
27214
27378
  const noLoaderOps = [...this.walState.noLoaderOps];
27215
- const limit = pLimit(5);
27379
+ const limit = pLimit(3);
27216
27380
  if (operations.length + fileOperations.length + noLoaderOps.length === 0) return;
27217
27381
  for (const dbMeta of noLoaderOps) {
27218
27382
  const uploadP = limit(async () => {
@@ -27319,259 +27483,6 @@ You can use close({ resize: true }) to resize header`);
27319
27483
  }
27320
27484
  };
27321
27485
 
27322
- // src/blockstore/store-meta.ts
27323
- init_cement();
27324
- init_utils();
27325
-
27326
- // src/runtime/index.ts
27327
- var runtime_exports = {};
27328
- __export(runtime_exports, {
27329
- FILESTORE_VERSION: () => FILESTORE_VERSION,
27330
- INDEXDB_VERSION: () => INDEXDB_VERSION,
27331
- files: () => files_exports,
27332
- getFileName: () => getFileName,
27333
- getFileSystem: () => getFileSystem,
27334
- getPath: () => getPath,
27335
- kb: () => key_bag_exports,
27336
- kc: () => keyed_crypto_exports,
27337
- mf: () => wait_pr_multiformats_exports,
27338
- runtimeFn: () => runtimeFn,
27339
- toArrayBuffer: () => toArrayBuffer
27340
- });
27341
- init_utils2();
27342
-
27343
- // src/runtime/wait-pr-multiformats/index.ts
27344
- var wait_pr_multiformats_exports = {};
27345
- __export(wait_pr_multiformats_exports, {
27346
- block: () => block_exports,
27347
- codec: () => codec_interface_exports
27348
- });
27349
-
27350
- // src/runtime/wait-pr-multiformats/codec-interface.ts
27351
- var codec_interface_exports = {};
27352
-
27353
- // src/runtime/index.ts
27354
- init_cement();
27355
-
27356
- // src/runtime/gateways/file/version.ts
27357
- var FILESTORE_VERSION = "v0.19-file";
27358
-
27359
- // src/runtime/index.ts
27360
- init_version();
27361
-
27362
- // src/blockstore/store-meta.ts
27363
- async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
27364
- const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
27365
- return Promise.all(
27366
- crdtEntries.map(async (crdtEntry) => {
27367
- const eventBlock = await decodeEventBlock(decodeFromBase64(crdtEntry.data));
27368
- const dbMeta = parse3(sthis.txt.decode(eventBlock.value.data.dbMeta));
27369
- return {
27370
- eventCid: eventBlock.cid,
27371
- parents: crdtEntry.parents,
27372
- dbMeta
27373
- };
27374
- })
27375
- );
27376
- }
27377
- async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
27378
- try {
27379
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
27380
- const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
27381
- if (keyInfo.length) {
27382
- const dbMeta = keyInfo[0].dbMeta;
27383
- if (dbMeta.key) {
27384
- const kb = await key_bag_exports.getKeyBag(sthis);
27385
- const keyName = getStoreKeyName(uri);
27386
- const res = await kb.setNamedKey(keyName, dbMeta.key);
27387
- if (res.isErr()) {
27388
- sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
27389
- throw res.Err();
27390
- }
27391
- }
27392
- sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
27393
- return Result.Ok(dbMeta);
27394
- }
27395
- sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
27396
- return Result.Ok(void 0);
27397
- } catch (error) {
27398
- sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
27399
- return Result.Err(error);
27400
- }
27401
- }
27402
- async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
27403
- try {
27404
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
27405
- const keyName = getStoreKeyName(uri);
27406
- const kb = await key_bag_exports.getKeyBag(sthis);
27407
- const res = await kb.getNamedExtractableKey(keyName, true);
27408
- if (res.isErr()) {
27409
- sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
27410
- throw res.Err();
27411
- }
27412
- const keyData = await res.Ok().extract();
27413
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
27414
- const { dbMeta, parents } = dbMetas[0];
27415
- const parentLinks = parents.map((p) => CID.parse(p));
27416
- dbMeta.key = keyData.keyStr;
27417
- const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
27418
- const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
27419
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
27420
- return Result.Ok(encoded);
27421
- } catch (error) {
27422
- sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
27423
- return Result.Err(error);
27424
- }
27425
- }
27426
- function getStoreKeyName(url) {
27427
- const storeKeyName = [url.getParam("localName") || url.getParam("name")];
27428
- const idx = url.getParam("index");
27429
- if (idx) {
27430
- storeKeyName.push(idx);
27431
- }
27432
- storeKeyName.push("data");
27433
- return `@${storeKeyName.join(":")}@`;
27434
- }
27435
- async function createDbMetaEventBlock(sthis, dbMeta, parents) {
27436
- const event = await EventBlock.create(
27437
- {
27438
- dbMeta: sthis.txt.encode(format2(dbMeta))
27439
- },
27440
- parents
27441
- );
27442
- return event;
27443
- }
27444
- async function encodeEventsWithParents(sthis, events, parents) {
27445
- const crdtEntries = events.map((event) => {
27446
- const base64String = encodeToBase64(event.bytes);
27447
- return {
27448
- cid: event.cid.toString(),
27449
- data: base64String,
27450
- parents: parents.map((p) => p.toString())
27451
- };
27452
- });
27453
- return sthis.txt.encode(JSON.stringify(crdtEntries));
27454
- }
27455
- var MetaStoreImpl = class extends BaseStoreImpl {
27456
- constructor(sthis, name6, url, opts, remote) {
27457
- super(
27458
- name6,
27459
- url,
27460
- {
27461
- ...opts
27462
- },
27463
- sthis,
27464
- ensureLogger(sthis, "MetaStoreImpl")
27465
- );
27466
- this.storeType = "meta";
27467
- this.subscribers = /* @__PURE__ */ new Map();
27468
- this.parents = [];
27469
- if (remote && opts.gateway.subscribe) {
27470
- this.onStarted(async () => {
27471
- this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
27472
- opts.gateway.subscribe?.(this.url(), async (message2) => {
27473
- this.logger.Debug().Msg("Received message from gateway");
27474
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message2);
27475
- await Promise.all(
27476
- dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
27477
- );
27478
- });
27479
- });
27480
- }
27481
- }
27482
- async handleByteHeads(byteHeads) {
27483
- return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
27484
- }
27485
- async load() {
27486
- const branch = "main";
27487
- const url = await this.gateway.buildUrl(this.url(), branch);
27488
- if (url.isErr()) {
27489
- throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
27490
- }
27491
- const bytes = await this.gateway.get(url.Ok());
27492
- if (bytes.isErr()) {
27493
- if (isNotFoundError(bytes)) {
27494
- return void 0;
27495
- }
27496
- throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
27497
- }
27498
- const dbMetas = await this.handleByteHeads(bytes.Ok());
27499
- await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
27500
- const cids = dbMetas.map((m) => m.eventCid);
27501
- const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
27502
- this.parents = Array.from(uniqueParentsMap.values());
27503
- return dbMetas.map((m) => m.dbMeta);
27504
- }
27505
- async save(meta, branch) {
27506
- branch = branch || "main";
27507
- this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
27508
- const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
27509
- const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
27510
- const url = await this.gateway.buildUrl(this.url(), branch);
27511
- if (url.isErr()) {
27512
- throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
27513
- }
27514
- const res = await this.gateway.put(url.Ok(), bytes);
27515
- if (res.isErr()) {
27516
- throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
27517
- }
27518
- await this.loader?.handleDbMetasFromStore([meta]);
27519
- this.parents = [event.cid];
27520
- return res;
27521
- }
27522
- async close() {
27523
- await this.gateway.close(this.url());
27524
- this._onClosed.forEach((fn) => fn());
27525
- return Result.Ok(void 0);
27526
- }
27527
- async destroy() {
27528
- return this.gateway.destroy(this.url());
27529
- }
27530
- };
27531
- function encodeToBase64(bytes) {
27532
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27533
- let base642 = "";
27534
- let i;
27535
- for (i = 0; i < bytes.length - 2; i += 3) {
27536
- base642 += chars[bytes[i] >> 2];
27537
- base642 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
27538
- base642 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
27539
- base642 += chars[bytes[i + 2] & 63];
27540
- }
27541
- if (i < bytes.length) {
27542
- base642 += chars[bytes[i] >> 2];
27543
- if (i === bytes.length - 1) {
27544
- base642 += chars[(bytes[i] & 3) << 4];
27545
- base642 += "==";
27546
- } else {
27547
- base642 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
27548
- base642 += chars[(bytes[i + 1] & 15) << 2];
27549
- base642 += "=";
27550
- }
27551
- }
27552
- return base642;
27553
- }
27554
- function decodeFromBase64(base642) {
27555
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27556
- const bytes = new Uint8Array(base642.length * 3 / 4);
27557
- let i;
27558
- let j = 0;
27559
- for (i = 0; i < base642.length; i += 4) {
27560
- const a = chars.indexOf(base642[i]);
27561
- const b = chars.indexOf(base642[i + 1]);
27562
- const c = chars.indexOf(base642[i + 2]);
27563
- const d = chars.indexOf(base642[i + 3]);
27564
- bytes[j++] = a << 2 | b >> 4;
27565
- if (base642[i + 2] !== "=") {
27566
- bytes[j++] = (b & 15) << 4 | c >> 2;
27567
- }
27568
- if (base642[i + 3] !== "=") {
27569
- bytes[j++] = (c & 3) << 6 | d;
27570
- }
27571
- }
27572
- return bytes.slice(0, j);
27573
- }
27574
-
27575
27486
  // src/blockstore/store-factory.ts
27576
27487
  init_utils();
27577
27488
  function ensureIsIndex(url, isIndex) {
@@ -27763,7 +27674,13 @@ You can use close({ resize: true }) to resize header`);
27763
27674
  return ds;
27764
27675
  }
27765
27676
  async function RemoteMetaStore(sthis, name6, url, opts) {
27766
- const ms = new MetaStoreImpl(sthis, name6, url, opts, true);
27677
+ const ms = new MetaStoreImpl(
27678
+ sthis,
27679
+ name6,
27680
+ url,
27681
+ opts
27682
+ /* , true*/
27683
+ );
27767
27684
  await ms.start();
27768
27685
  return ms;
27769
27686
  }
@@ -29650,7 +29567,7 @@ You can use close({ resize: true }) to resize header`);
29650
29567
  queue.sort((a, b) => b.updates ? 1 : -1);
29651
29568
  const task = queue.shift();
29652
29569
  if (!task) continue;
29653
- await worker(task.newHead, task.prevHead, task.updates !== null).catch((e) => {
29570
+ await worker(task.newHead, task.prevHead, task.updates !== void 0).catch((e) => {
29654
29571
  throw logger.Error().Err(e).Msg("int_applyHead worker error").AsError();
29655
29572
  });
29656
29573
  if (task.updates) {
@@ -29741,6 +29658,7 @@ You can use close({ resize: true }) to resize header`);
29741
29658
  this.zoomers.add(fn);
29742
29659
  }
29743
29660
  async int_applyHead(newHead, prevHead, localUpdates) {
29661
+ const noLoader = !localUpdates;
29744
29662
  const ogHead = sortClockHead(this.head);
29745
29663
  newHead = sortClockHead(newHead);
29746
29664
  if (compareClockHeads(ogHead, newHead)) {
@@ -29751,7 +29669,6 @@ You can use close({ resize: true }) to resize header`);
29751
29669
  this.setHead(newHead);
29752
29670
  return;
29753
29671
  }
29754
- const noLoader = !localUpdates;
29755
29672
  if (!this.blockstore) {
29756
29673
  throw this.logger.Error().Msg("missing blockstore").AsError();
29757
29674
  }
@@ -29871,8 +29788,7 @@ You can use close({ resize: true }) to resize header`);
29871
29788
  try {
29872
29789
  await Promise.all([this.blockstore.ready(), this.indexBlockstore.ready(), this.clock.ready()]);
29873
29790
  } catch (e) {
29874
- const ee = e;
29875
- throw this.logger.Error().Err(e).Msg(`CRDT is not ready: ${ee.stack}`).AsError();
29791
+ throw this.logger.Error().Err(e).Msg(`CRDT is not ready`).AsError();
29876
29792
  }
29877
29793
  });
29878
29794
  }
@@ -29976,7 +29892,7 @@ You can use close({ resize: true }) to resize header`);
29976
29892
  await this.ready();
29977
29893
  this.logger.Debug().Str("id", doc._id).Msg("put");
29978
29894
  const { _id, ...value } = doc;
29979
- const docId = _id || this.sthis.nextId().str;
29895
+ const docId = _id || this.sthis.timeOrderedNextId().str;
29980
29896
  const result = await this._writeQueue.push({
29981
29897
  id: docId,
29982
29898
  value: {
@@ -30105,12 +30021,48 @@ You can use close({ resize: true }) to resize header`);
30105
30021
  }
30106
30022
  }
30107
30023
 
30024
+ // src/runtime/index.ts
30025
+ var runtime_exports = {};
30026
+ __export(runtime_exports, {
30027
+ FILESTORE_VERSION: () => FILESTORE_VERSION,
30028
+ INDEXDB_VERSION: () => INDEXDB_VERSION,
30029
+ files: () => files_exports,
30030
+ getFileName: () => getFileName,
30031
+ getFileSystem: () => getFileSystem,
30032
+ getPath: () => getPath,
30033
+ kb: () => key_bag_exports,
30034
+ kc: () => keyed_crypto_exports,
30035
+ mf: () => wait_pr_multiformats_exports,
30036
+ runtimeFn: () => runtimeFn,
30037
+ toArrayBuffer: () => toArrayBuffer
30038
+ });
30039
+ init_utils2();
30040
+
30041
+ // src/runtime/wait-pr-multiformats/index.ts
30042
+ var wait_pr_multiformats_exports = {};
30043
+ __export(wait_pr_multiformats_exports, {
30044
+ block: () => block_exports,
30045
+ codec: () => codec_interface_exports
30046
+ });
30047
+
30048
+ // src/runtime/wait-pr-multiformats/codec-interface.ts
30049
+ var codec_interface_exports = {};
30050
+
30051
+ // src/runtime/index.ts
30052
+ init_cement();
30053
+
30054
+ // src/runtime/gateways/file/version.ts
30055
+ var FILESTORE_VERSION = "v0.19-file";
30056
+
30057
+ // src/runtime/index.ts
30058
+ init_version();
30059
+
30108
30060
  // src/index.ts
30109
30061
  init_utils();
30110
30062
 
30111
30063
  // src/version.ts
30112
30064
  var PACKAGE_VERSION = Object.keys({
30113
- "0.19.11-dev-dryrun": "xxxx"
30065
+ "0.19.11-dev-dryrun2": "xxxx"
30114
30066
  })[0];
30115
30067
  return __toCommonJS(src_exports6);
30116
30068
  })();