@fireproof/core 0.19.99 → 0.19.100

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -252,7 +252,6 @@ var UnixFSFileBuilder = class {
252
252
  };
253
253
 
254
254
  // src/blockstore/store.ts
255
- import pLimit2 from "p-limit";
256
255
  import { format as format2, parse as parse2 } from "@ipld/dag-json";
257
256
  import { exception2Result, ResolveOnce as ResolveOnce3, Result as Result5 } from "@adviser/cement";
258
257
 
@@ -1667,6 +1666,8 @@ async function encodeEventsWithParents(sthis, events, parents) {
1667
1666
  }
1668
1667
 
1669
1668
  // src/blockstore/store.ts
1669
+ import pRetry from "p-retry";
1670
+ import pMap from "p-map";
1670
1671
  function guardVersion(url) {
1671
1672
  if (!url.hasParam("version")) {
1672
1673
  return Result5.Err(`missing version: ${url.toString()}`);
@@ -1926,72 +1927,82 @@ var WALStoreImpl = class extends BaseStoreImpl {
1926
1927
  }
1927
1928
  async _doProcess() {
1928
1929
  if (!this.loader.remoteCarStore) return;
1929
- const rmlp = (async () => {
1930
- const operations = [...this.walState.operations];
1931
- const fileOperations = [...this.walState.fileOperations];
1932
- const uploads = [];
1933
- const noLoaderOps = [...this.walState.noLoaderOps];
1934
- const limit = pLimit2(3);
1935
- if (operations.length + fileOperations.length + noLoaderOps.length === 0) return;
1936
- for (const dbMeta of noLoaderOps) {
1937
- const uploadP = limit(async () => {
1938
- for (const cid of dbMeta.cars) {
1939
- const car = await (await this.loader.carStore()).load(cid);
1940
- if (!car) {
1941
- if (carLogIncludesGroup(this.loader.carLog, dbMeta.cars))
1942
- throw this.logger.Error().Ref("cid", cid).Msg("missing local car").AsError();
1943
- } else {
1944
- await throwFalsy(this.loader.remoteCarStore).save(car);
1930
+ const operations = [...this.walState.operations];
1931
+ const noLoaderOps = [...this.walState.noLoaderOps];
1932
+ const fileOperations = [...this.walState.fileOperations];
1933
+ if (operations.length + noLoaderOps.length + fileOperations.length === 0) return;
1934
+ const concurrencyLimit = 3;
1935
+ const retryableUpload = (fn, description) => pRetry(fn, {
1936
+ retries: 5,
1937
+ onFailedAttempt: (error) => {
1938
+ this.logger.Warn().Msg(`Attempt ${error.attemptNumber} failed for ${description}. There are ${error.retriesLeft} retries left.`);
1939
+ }
1940
+ });
1941
+ try {
1942
+ await pMap(
1943
+ noLoaderOps,
1944
+ async (dbMeta) => {
1945
+ await retryableUpload(async () => {
1946
+ for (const cid of dbMeta.cars) {
1947
+ const car = await (await this.loader.carStore()).load(cid);
1948
+ if (!car) {
1949
+ if (carLogIncludesGroup(this.loader.carLog, dbMeta.cars)) {
1950
+ throw this.logger.Error().Ref("cid", cid).Msg("missing local car").AsError();
1951
+ }
1952
+ } else {
1953
+ await throwFalsy(this.loader.remoteCarStore).save(car);
1954
+ }
1945
1955
  }
1946
1956
  this.walState.noLoaderOps = this.walState.noLoaderOps.filter((op) => op !== dbMeta);
1947
- }
1948
- });
1949
- uploads.push(uploadP);
1950
- }
1951
- for (const dbMeta of operations) {
1952
- const uploadP = limit(async () => {
1953
- for (const cid of dbMeta.cars) {
1954
- const car = await (await this.loader.carStore()).load(cid).catch(() => null);
1955
- if (!car) {
1956
- if (carLogIncludesGroup(this.loader.carLog, dbMeta.cars))
1957
- throw this.logger.Error().Ref("cid", cid).Msg(`missing local car`).AsError();
1958
- } else {
1959
- await throwFalsy(this.loader.remoteCarStore).save(car);
1957
+ }, `noLoaderOp with dbMeta.cars=${dbMeta.cars.toString()}`);
1958
+ },
1959
+ { concurrency: concurrencyLimit }
1960
+ );
1961
+ await pMap(
1962
+ operations,
1963
+ async (dbMeta) => {
1964
+ await retryableUpload(async () => {
1965
+ for (const cid of dbMeta.cars) {
1966
+ const car = await (await this.loader.carStore()).load(cid);
1967
+ if (!car) {
1968
+ if (carLogIncludesGroup(this.loader.carLog, dbMeta.cars)) {
1969
+ throw this.logger.Error().Ref("cid", cid).Msg(`missing local car`).AsError();
1970
+ }
1971
+ } else {
1972
+ await throwFalsy(this.loader.remoteCarStore).save(car);
1973
+ }
1960
1974
  }
1961
- }
1962
- this.walState.operations = this.walState.operations.filter((op) => op !== dbMeta);
1963
- });
1964
- uploads.push(uploadP);
1965
- }
1966
- if (fileOperations.length) {
1967
- const dbLoader = this.loader;
1968
- for (const { cid: fileCid, public: publicFile } of fileOperations) {
1969
- const uploadP = limit(async () => {
1970
- const fileBlock = await (await dbLoader.fileStore()).load(fileCid);
1971
- await dbLoader.remoteFileStore?.save(fileBlock, { public: publicFile });
1975
+ this.walState.operations = this.walState.operations.filter((op) => op !== dbMeta);
1976
+ }, `operation with dbMeta.cars=${dbMeta.cars.toString()}`);
1977
+ },
1978
+ { concurrency: concurrencyLimit }
1979
+ );
1980
+ await pMap(
1981
+ fileOperations,
1982
+ async ({ cid: fileCid, public: publicFile }) => {
1983
+ await retryableUpload(async () => {
1984
+ const fileBlock = await (await this.loader.fileStore()).load(fileCid);
1985
+ if (!fileBlock) {
1986
+ throw this.logger.Error().Ref("cid", fileCid).Msg("missing file block").AsError();
1987
+ }
1988
+ await this.loader.remoteFileStore?.save(fileBlock, { public: publicFile });
1972
1989
  this.walState.fileOperations = this.walState.fileOperations.filter((op) => op.cid !== fileCid);
1973
- });
1974
- uploads.push(uploadP);
1975
- }
1976
- }
1977
- try {
1978
- const res = await Promise.allSettled(uploads);
1979
- const errors = res.filter((r) => r.status === "rejected");
1980
- if (errors.length) {
1981
- throw this.logger.Error().Any("errors", errors).Msg("error uploading").AsError();
1982
- }
1983
- if (operations.length) {
1984
- const lastOp = operations[operations.length - 1];
1985
- await this.loader.remoteMetaStore?.save(lastOp).catch((e) => {
1986
- this.walState.operations.push(lastOp);
1987
- throw this.logger.Error().Any("error", e).Msg("error saving remote meta").AsError();
1988
- });
1989
- }
1990
- } finally {
1991
- await this.save(this.walState);
1992
- }
1993
- })();
1994
- await rmlp;
1990
+ }, `fileOperation with cid=${fileCid.toString()}`);
1991
+ },
1992
+ { concurrency: concurrencyLimit }
1993
+ );
1994
+ if (operations.length) {
1995
+ const lastOp = operations[operations.length - 1];
1996
+ await retryableUpload(async () => {
1997
+ await this.loader.remoteMetaStore?.save(lastOp);
1998
+ }, `remoteMetaStore save with dbMeta.cars=${lastOp.cars.toString()}`);
1999
+ }
2000
+ } catch (error) {
2001
+ this.logger.Error().Any("error", error).Msg("Processing failed");
2002
+ return;
2003
+ } finally {
2004
+ await this.save(this.walState);
2005
+ }
1995
2006
  }
1996
2007
  async load() {
1997
2008
  this.logger.Debug().Msg("loading");
@@ -3416,7 +3427,7 @@ import { runtimeFn as runtimeFn2 } from "@adviser/cement";
3416
3427
 
3417
3428
  // src/version.ts
3418
3429
  var PACKAGE_VERSION = Object.keys({
3419
- "0.19.99": "xxxx"
3430
+ "0.19.100": "xxxx"
3420
3431
  })[0];
3421
3432
  export {
3422
3433
  CRDT,