s3db.js 7.3.4 → 7.3.6

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/dist/s3db.iife.js CHANGED
@@ -1,4 +1,4 @@
1
- var S3DB = (function (exports, nanoid, zlib, promisePool, web, promises, lodashEs, crypto, jsonStableStringify, clientS3, flat, FastestValidator) {
1
+ var S3DB = (function (exports, nanoid, zlib, promisePool, web, promises, crypto, lodashEs, jsonStableStringify, clientS3, flat, FastestValidator) {
2
2
  'use strict';
3
3
 
4
4
  const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
@@ -7070,6 +7070,12 @@ ${JSON.stringify(validation, null, 2)}`,
7070
7070
  }
7071
7071
  async _clear(prefix) {
7072
7072
  try {
7073
+ if (!await this._fileExists(this.directory)) {
7074
+ if (this.enableStats) {
7075
+ this.stats.clears++;
7076
+ }
7077
+ return true;
7078
+ }
7073
7079
  const files = await promises.readdir(this.directory);
7074
7080
  const cacheFiles = files.filter((file) => {
7075
7081
  if (!file.startsWith(this.prefix)) return false;
@@ -8340,7 +8346,7 @@ ${JSON.stringify(validation, null, 2)}`,
8340
8346
  }
8341
8347
  async setup(database) {
8342
8348
  this.database = database;
8343
- if (process.env.NODE_ENV === "test") return;
8349
+ if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return;
8344
8350
  const [ok, err] = await try_fn_default(async () => {
8345
8351
  const [ok1, err1, metricsResource] = await try_fn_default(() => database.createResource({
8346
8352
  name: "metrics",
@@ -8390,7 +8396,7 @@ ${JSON.stringify(validation, null, 2)}`,
8390
8396
  this.performanceResource = database.resources.performance_logs;
8391
8397
  }
8392
8398
  this.installMetricsHooks();
8393
- if (process.env.NODE_ENV !== "test") {
8399
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
8394
8400
  this.startFlushTimer();
8395
8401
  }
8396
8402
  }
@@ -8401,7 +8407,7 @@ ${JSON.stringify(validation, null, 2)}`,
8401
8407
  clearInterval(this.flushTimer);
8402
8408
  this.flushTimer = null;
8403
8409
  }
8404
- if (process.env.NODE_ENV !== "test") {
8410
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
8405
8411
  await this.flushMetrics();
8406
8412
  }
8407
8413
  }
@@ -8580,10 +8586,18 @@ ${JSON.stringify(validation, null, 2)}`,
8580
8586
  async flushMetrics() {
8581
8587
  if (!this.metricsResource) return;
8582
8588
  const [ok, err] = await try_fn_default(async () => {
8583
- const metadata = process.env.NODE_ENV === "test" ? {} : { global: "true" };
8584
- const perfMetadata = process.env.NODE_ENV === "test" ? {} : { perf: "true" };
8585
- const errorMetadata = process.env.NODE_ENV === "test" ? {} : { error: "true" };
8586
- const resourceMetadata = process.env.NODE_ENV === "test" ? {} : { resource: "true" };
8589
+ let metadata, perfMetadata, errorMetadata, resourceMetadata;
8590
+ if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
8591
+ metadata = {};
8592
+ perfMetadata = {};
8593
+ errorMetadata = {};
8594
+ resourceMetadata = {};
8595
+ } else {
8596
+ metadata = { global: "true" };
8597
+ perfMetadata = { perf: "true" };
8598
+ errorMetadata = { error: "true" };
8599
+ resourceMetadata = { resource: "true" };
8600
+ }
8587
8601
  for (const [operation, data] of Object.entries(this.metrics.operations)) {
8588
8602
  if (data.count > 0) {
8589
8603
  await this.metricsResource.insert({
@@ -8933,6 +8947,9 @@ ${JSON.stringify(validation, null, 2)}`,
8933
8947
  await super.initialize(database);
8934
8948
  const [ok, err, sdk] = await try_fn_default(() => import('@google-cloud/bigquery'));
8935
8949
  if (!ok) {
8950
+ if (this.config.verbose) {
8951
+ console.warn(`[BigqueryReplicator] Failed to import BigQuery SDK: ${err.message}`);
8952
+ }
8936
8953
  this.emit("initialization_error", { replicator: this.name, error: err.message });
8937
8954
  throw err;
8938
8955
  }
@@ -9002,19 +9019,28 @@ ${JSON.stringify(validation, null, 2)}`,
9002
9019
  const maxRetries = 2;
9003
9020
  let lastError = null;
9004
9021
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
9005
- try {
9022
+ const [ok2, error] = await try_fn_default(async () => {
9006
9023
  const [updateJob] = await this.bigqueryClient.createQueryJob({
9007
9024
  query,
9008
9025
  params,
9009
9026
  location: this.location
9010
9027
  });
9011
9028
  await updateJob.getQueryResults();
9012
- job = [updateJob];
9029
+ return [updateJob];
9030
+ });
9031
+ if (ok2) {
9032
+ job = ok2;
9013
9033
  break;
9014
- } catch (error) {
9034
+ } else {
9015
9035
  lastError = error;
9036
+ if (this.config.verbose) {
9037
+ console.warn(`[BigqueryReplicator] Update attempt ${attempt} failed: ${error.message}`);
9038
+ }
9016
9039
  if (error?.message?.includes("streaming buffer") && attempt < maxRetries) {
9017
9040
  const delaySeconds = 30;
9041
+ if (this.config.verbose) {
9042
+ console.warn(`[BigqueryReplicator] Retrying in ${delaySeconds} seconds due to streaming buffer issue`);
9043
+ }
9018
9044
  await new Promise((resolve) => setTimeout(resolve, delaySeconds * 1e3));
9019
9045
  continue;
9020
9046
  }
@@ -9081,6 +9107,9 @@ ${JSON.stringify(validation, null, 2)}`,
9081
9107
  };
9082
9108
  });
9083
9109
  if (ok) return result;
9110
+ if (this.config.verbose) {
9111
+ console.warn(`[BigqueryReplicator] Replication failed for ${resourceName}: ${err.message}`);
9112
+ }
9084
9113
  this.emit("replicator_error", {
9085
9114
  replicator: this.name,
9086
9115
  resourceName,
@@ -9101,8 +9130,14 @@ ${JSON.stringify(validation, null, 2)}`,
9101
9130
  record.id,
9102
9131
  record.beforeData
9103
9132
  ));
9104
- if (ok) results.push(res);
9105
- else errors.push({ id: record.id, error: err.message });
9133
+ if (ok) {
9134
+ results.push(res);
9135
+ } else {
9136
+ if (this.config.verbose) {
9137
+ console.warn(`[BigqueryReplicator] Batch replication failed for record ${record.id}: ${err.message}`);
9138
+ }
9139
+ errors.push({ id: record.id, error: err.message });
9140
+ }
9106
9141
  }
9107
9142
  return {
9108
9143
  success: errors.length === 0,
@@ -9118,6 +9153,9 @@ ${JSON.stringify(validation, null, 2)}`,
9118
9153
  return true;
9119
9154
  });
9120
9155
  if (ok) return true;
9156
+ if (this.config.verbose) {
9157
+ console.warn(`[BigqueryReplicator] Connection test failed: ${err.message}`);
9158
+ }
9121
9159
  this.emit("connection_error", { replicator: this.name, error: err.message });
9122
9160
  return false;
9123
9161
  }
@@ -9205,6 +9243,9 @@ ${JSON.stringify(validation, null, 2)}`,
9205
9243
  await super.initialize(database);
9206
9244
  const [ok, err, sdk] = await try_fn_default(() => import('pg'));
9207
9245
  if (!ok) {
9246
+ if (this.config.verbose) {
9247
+ console.warn(`[PostgresReplicator] Failed to import pg SDK: ${err.message}`);
9248
+ }
9208
9249
  this.emit("initialization_error", {
9209
9250
  replicator: this.name,
9210
9251
  error: err.message
@@ -9346,6 +9387,9 @@ ${JSON.stringify(validation, null, 2)}`,
9346
9387
  };
9347
9388
  });
9348
9389
  if (ok) return result;
9390
+ if (this.config.verbose) {
9391
+ console.warn(`[PostgresReplicator] Replication failed for ${resourceName}: ${err.message}`);
9392
+ }
9349
9393
  this.emit("replicator_error", {
9350
9394
  replicator: this.name,
9351
9395
  resourceName,
@@ -9366,8 +9410,14 @@ ${JSON.stringify(validation, null, 2)}`,
9366
9410
  record.id,
9367
9411
  record.beforeData
9368
9412
  ));
9369
- if (ok) results.push(res);
9370
- else errors.push({ id: record.id, error: err.message });
9413
+ if (ok) {
9414
+ results.push(res);
9415
+ } else {
9416
+ if (this.config.verbose) {
9417
+ console.warn(`[PostgresReplicator] Batch replication failed for record ${record.id}: ${err.message}`);
9418
+ }
9419
+ errors.push({ id: record.id, error: err.message });
9420
+ }
9371
9421
  }
9372
9422
  return {
9373
9423
  success: errors.length === 0,
@@ -9382,6 +9432,9 @@ ${JSON.stringify(validation, null, 2)}`,
9382
9432
  return true;
9383
9433
  });
9384
9434
  if (ok) return true;
9435
+ if (this.config.verbose) {
9436
+ console.warn(`[PostgresReplicator] Connection test failed: ${err.message}`);
9437
+ }
9385
9438
  this.emit("connection_error", { replicator: this.name, error: err.message });
9386
9439
  return false;
9387
9440
  }
@@ -13116,7 +13169,7 @@ ${JSON.stringify(validation, null, 2)}`,
13116
13169
  super();
13117
13170
  this.version = "1";
13118
13171
  this.s3dbVersion = (() => {
13119
- const [ok, err, version] = try_fn_default(() => true ? "7.3.4" : "latest");
13172
+ const [ok, err, version] = try_fn_default(() => true ? "7.3.6" : "latest");
13120
13173
  return ok ? version : "latest";
13121
13174
  })();
13122
13175
  this.resources = {};
@@ -13159,14 +13212,16 @@ ${JSON.stringify(validation, null, 2)}`,
13159
13212
  this.keyPrefix = this.client.keyPrefix;
13160
13213
  if (!this._exitListenerRegistered) {
13161
13214
  this._exitListenerRegistered = true;
13162
- process.on("exit", async () => {
13163
- if (this.isConnected()) {
13164
- try {
13165
- await this.disconnect();
13166
- } catch (err) {
13215
+ if (typeof process !== "undefined") {
13216
+ process.on("exit", async () => {
13217
+ if (this.isConnected()) {
13218
+ try {
13219
+ await this.disconnect();
13220
+ } catch (err) {
13221
+ }
13167
13222
  }
13168
- }
13169
- });
13223
+ });
13224
+ }
13170
13225
  }
13171
13226
  }
13172
13227
  async connect() {
@@ -13618,9 +13673,8 @@ ${JSON.stringify(validation, null, 2)}`,
13618
13673
  const map = {};
13619
13674
  for (const res of resources) {
13620
13675
  if (typeof res === "string") map[normalizeResourceName$1(res)] = res;
13621
- else if (Array.isArray(res) && typeof res[0] === "string") map[normalizeResourceName$1(res[0])] = res;
13622
13676
  else if (typeof res === "object" && res.resource) {
13623
- map[normalizeResourceName$1(res.resource)] = { ...res };
13677
+ map[normalizeResourceName$1(res.resource)] = res;
13624
13678
  }
13625
13679
  }
13626
13680
  return map;
@@ -13633,15 +13687,14 @@ ${JSON.stringify(validation, null, 2)}`,
13633
13687
  else if (Array.isArray(dest)) {
13634
13688
  map[normSrc] = dest.map((item) => {
13635
13689
  if (typeof item === "string") return item;
13636
- if (typeof item === "function") return item;
13637
13690
  if (typeof item === "object" && item.resource) {
13638
- return { ...item };
13691
+ return item;
13639
13692
  }
13640
13693
  return item;
13641
13694
  });
13642
13695
  } else if (typeof dest === "function") map[normSrc] = dest;
13643
13696
  else if (typeof dest === "object" && dest.resource) {
13644
- map[normSrc] = { ...dest };
13697
+ map[normSrc] = dest;
13645
13698
  }
13646
13699
  }
13647
13700
  return map;
@@ -13649,10 +13702,6 @@ ${JSON.stringify(validation, null, 2)}`,
13649
13702
  if (typeof resources === "function") {
13650
13703
  return resources;
13651
13704
  }
13652
- if (typeof resources === "string") {
13653
- const map = { [normalizeResourceName$1(resources)]: resources };
13654
- return map;
13655
- }
13656
13705
  return {};
13657
13706
  }
13658
13707
  validateConfig() {
@@ -13666,8 +13715,8 @@ ${JSON.stringify(validation, null, 2)}`,
13666
13715
  return { isValid: errors.length === 0, errors };
13667
13716
  }
13668
13717
  async initialize(database) {
13669
- try {
13670
- await super.initialize(database);
13718
+ await super.initialize(database);
13719
+ const [ok, err] = await try_fn_default(async () => {
13671
13720
  if (this.client) {
13672
13721
  this.targetDatabase = this.client;
13673
13722
  } else if (this.connectionString) {
@@ -13686,7 +13735,11 @@ ${JSON.stringify(validation, null, 2)}`,
13686
13735
  replicator: this.name,
13687
13736
  target: this.connectionString || "client-provided"
13688
13737
  });
13689
- } catch (err) {
13738
+ });
13739
+ if (!ok) {
13740
+ if (this.config.verbose) {
13741
+ console.warn(`[S3dbReplicator] Initialization failed: ${err.message}`);
13742
+ }
13690
13743
  throw err;
13691
13744
  }
13692
13745
  }
@@ -13705,18 +13758,77 @@ ${JSON.stringify(validation, null, 2)}`,
13705
13758
  id = recordId;
13706
13759
  }
13707
13760
  const normResource = normalizeResourceName$1(resource);
13708
- const destResource = this._resolveDestResource(normResource, payload);
13709
- const destResourceObj = this._getDestResourceObj(destResource);
13710
- const transformedData = this._applyTransformer(normResource, payload);
13761
+ const entry = this.resourcesMap[normResource];
13762
+ if (!entry) {
13763
+ throw new Error(`[S3dbReplicator] Resource not configured: ${resource}`);
13764
+ }
13765
+ if (Array.isArray(entry)) {
13766
+ const results = [];
13767
+ for (const destConfig of entry) {
13768
+ const [ok, error, result] = await try_fn_default(async () => {
13769
+ return await this._replicateToSingleDestination(destConfig, normResource, op, payload, id);
13770
+ });
13771
+ if (!ok) {
13772
+ if (this.config && this.config.verbose) {
13773
+ console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(destConfig)}: ${error.message}`);
13774
+ }
13775
+ throw error;
13776
+ }
13777
+ results.push(result);
13778
+ }
13779
+ return results;
13780
+ } else {
13781
+ const [ok, error, result] = await try_fn_default(async () => {
13782
+ return await this._replicateToSingleDestination(entry, normResource, op, payload, id);
13783
+ });
13784
+ if (!ok) {
13785
+ if (this.config && this.config.verbose) {
13786
+ console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(entry)}: ${error.message}`);
13787
+ }
13788
+ throw error;
13789
+ }
13790
+ return result;
13791
+ }
13792
+ }
13793
+ async _replicateToSingleDestination(destConfig, sourceResource, operation, data, recordId) {
13794
+ let destResourceName;
13795
+ if (typeof destConfig === "string") {
13796
+ destResourceName = destConfig;
13797
+ } else if (typeof destConfig === "object" && destConfig.resource) {
13798
+ destResourceName = destConfig.resource;
13799
+ } else {
13800
+ destResourceName = sourceResource;
13801
+ }
13802
+ if (typeof destConfig === "object" && destConfig.actions && Array.isArray(destConfig.actions)) {
13803
+ if (!destConfig.actions.includes(operation)) {
13804
+ return { skipped: true, reason: "action_not_supported", action: operation, destination: destResourceName };
13805
+ }
13806
+ }
13807
+ const destResourceObj = this._getDestResourceObj(destResourceName);
13808
+ let transformedData;
13809
+ if (typeof destConfig === "object" && destConfig.transform && typeof destConfig.transform === "function") {
13810
+ transformedData = destConfig.transform(data);
13811
+ if (transformedData && data && data.id && !transformedData.id) {
13812
+ transformedData.id = data.id;
13813
+ }
13814
+ } else if (typeof destConfig === "object" && destConfig.transformer && typeof destConfig.transformer === "function") {
13815
+ transformedData = destConfig.transformer(data);
13816
+ if (transformedData && data && data.id && !transformedData.id) {
13817
+ transformedData.id = data.id;
13818
+ }
13819
+ } else {
13820
+ transformedData = data;
13821
+ }
13822
+ if (!transformedData && data) transformedData = data;
13711
13823
  let result;
13712
- if (op === "insert") {
13824
+ if (operation === "insert") {
13713
13825
  result = await destResourceObj.insert(transformedData);
13714
- } else if (op === "update") {
13715
- result = await destResourceObj.update(id, transformedData);
13716
- } else if (op === "delete") {
13717
- result = await destResourceObj.delete(id);
13826
+ } else if (operation === "update") {
13827
+ result = await destResourceObj.update(recordId, transformedData);
13828
+ } else if (operation === "delete") {
13829
+ result = await destResourceObj.delete(recordId);
13718
13830
  } else {
13719
- throw new Error(`Invalid operation: ${op}. Supported operations are: insert, update, delete`);
13831
+ throw new Error(`Invalid operation: ${operation}. Supported operations are: insert, update, delete`);
13720
13832
  }
13721
13833
  return result;
13722
13834
  }
@@ -13725,13 +13837,25 @@ ${JSON.stringify(validation, null, 2)}`,
13725
13837
  const entry = this.resourcesMap[normResource];
13726
13838
  let result;
13727
13839
  if (!entry) return data;
13728
- if (Array.isArray(entry) && typeof entry[1] === "function") {
13729
- result = entry[1](data);
13840
+ if (Array.isArray(entry)) {
13841
+ for (const item of entry) {
13842
+ if (typeof item === "object" && item.transform && typeof item.transform === "function") {
13843
+ result = item.transform(data);
13844
+ break;
13845
+ } else if (typeof item === "object" && item.transformer && typeof item.transformer === "function") {
13846
+ result = item.transformer(data);
13847
+ break;
13848
+ }
13849
+ }
13850
+ if (!result) result = data;
13851
+ } else if (typeof entry === "object") {
13852
+ if (typeof entry.transform === "function") {
13853
+ result = entry.transform(data);
13854
+ } else if (typeof entry.transformer === "function") {
13855
+ result = entry.transformer(data);
13856
+ }
13730
13857
  } else if (typeof entry === "function") {
13731
13858
  result = entry(data);
13732
- } else if (typeof entry === "object") {
13733
- if (typeof entry.transform === "function") result = entry.transform(data);
13734
- else if (typeof entry.transformer === "function") result = entry.transformer(data);
13735
13859
  } else {
13736
13860
  result = data;
13737
13861
  }
@@ -13744,18 +13868,19 @@ ${JSON.stringify(validation, null, 2)}`,
13744
13868
  const entry = this.resourcesMap[normResource];
13745
13869
  if (!entry) return resource;
13746
13870
  if (Array.isArray(entry)) {
13747
- if (typeof entry[0] === "string") return entry[0];
13748
- if (typeof entry[0] === "object" && entry[0].resource) return entry[0].resource;
13749
- if (typeof entry[0] === "function") return resource;
13871
+ for (const item of entry) {
13872
+ if (typeof item === "string") return item;
13873
+ if (typeof item === "object" && item.resource) return item.resource;
13874
+ }
13875
+ return resource;
13750
13876
  }
13751
13877
  if (typeof entry === "string") return entry;
13752
- if (resource && !targetResourceName) targetResourceName = resource;
13878
+ if (typeof entry === "function") return resource;
13753
13879
  if (typeof entry === "object" && entry.resource) return entry.resource;
13754
13880
  return resource;
13755
13881
  }
13756
13882
  _getDestResourceObj(resource) {
13757
- if (!this.client || !this.client.resources) return null;
13758
- const available = Object.keys(this.client.resources);
13883
+ const available = Object.keys(this.client.resources || {});
13759
13884
  const norm = normalizeResourceName$1(resource);
13760
13885
  const found = available.find((r) => normalizeResourceName$1(r) === norm);
13761
13886
  if (!found) {
@@ -13777,8 +13902,14 @@ ${JSON.stringify(validation, null, 2)}`,
13777
13902
  data: record.data,
13778
13903
  beforeData: record.beforeData
13779
13904
  }));
13780
- if (ok) results.push(result);
13781
- else errors.push({ id: record.id, error: err.message });
13905
+ if (ok) {
13906
+ results.push(result);
13907
+ } else {
13908
+ if (this.config.verbose) {
13909
+ console.warn(`[S3dbReplicator] Batch replication failed for record ${record.id}: ${err.message}`);
13910
+ }
13911
+ errors.push({ id: record.id, error: err.message });
13912
+ }
13782
13913
  }
13783
13914
  this.emit("batch_replicated", {
13784
13915
  replicator: this.name,
@@ -13796,18 +13927,20 @@ ${JSON.stringify(validation, null, 2)}`,
13796
13927
  }
13797
13928
  async testConnection() {
13798
13929
  const [ok, err] = await try_fn_default(async () => {
13799
- if (!this.targetDatabase) {
13800
- await this.initialize(this.database);
13930
+ if (!this.targetDatabase) throw new Error("No target database configured");
13931
+ if (typeof this.targetDatabase.connect === "function") {
13932
+ await this.targetDatabase.connect();
13801
13933
  }
13802
- await this.targetDatabase.listResources();
13803
13934
  return true;
13804
13935
  });
13805
- if (ok) return true;
13806
- this.emit("connection_error", {
13807
- replicator: this.name,
13808
- error: err.message
13809
- });
13810
- return false;
13936
+ if (!ok) {
13937
+ if (this.config.verbose) {
13938
+ console.warn(`[S3dbReplicator] Connection test failed: ${err.message}`);
13939
+ }
13940
+ this.emit("connection_error", { replicator: this.name, error: err.message });
13941
+ return false;
13942
+ }
13943
+ return true;
13811
13944
  }
13812
13945
  async getStatus() {
13813
13946
  const baseStatus = await super.getStatus();
@@ -13839,7 +13972,7 @@ ${JSON.stringify(validation, null, 2)}`,
13839
13972
  } else {
13840
13973
  return true;
13841
13974
  }
13842
- } else if (typeof item === "string" || typeof item === "function") {
13975
+ } else if (typeof item === "string") {
13843
13976
  return true;
13844
13977
  }
13845
13978
  }
@@ -13966,6 +14099,9 @@ ${JSON.stringify(validation, null, 2)}`,
13966
14099
  if (!this.sqsClient) {
13967
14100
  const [ok, err, sdk] = await try_fn_default(() => import('@aws-sdk/client-sqs'));
13968
14101
  if (!ok) {
14102
+ if (this.config.verbose) {
14103
+ console.warn(`[SqsReplicator] Failed to import SQS SDK: ${err.message}`);
14104
+ }
13969
14105
  this.emit("initialization_error", {
13970
14106
  replicator: this.name,
13971
14107
  error: err.message
@@ -14017,6 +14153,9 @@ ${JSON.stringify(validation, null, 2)}`,
14017
14153
  return { success: true, results };
14018
14154
  });
14019
14155
  if (ok) return result;
14156
+ if (this.config.verbose) {
14157
+ console.warn(`[SqsReplicator] Replication failed for ${resource}: ${err.message}`);
14158
+ }
14020
14159
  this.emit("replicator_error", {
14021
14160
  replicator: this.name,
14022
14161
  resource,
@@ -14089,6 +14228,9 @@ ${JSON.stringify(validation, null, 2)}`,
14089
14228
  });
14090
14229
  if (ok) return result;
14091
14230
  const errorMessage = err?.message || err || "Unknown error";
14231
+ if (this.config.verbose) {
14232
+ console.warn(`[SqsReplicator] Batch replication failed for ${resource}: ${errorMessage}`);
14233
+ }
14092
14234
  this.emit("batch_replicator_error", {
14093
14235
  replicator: this.name,
14094
14236
  resource,
@@ -14110,6 +14252,9 @@ ${JSON.stringify(validation, null, 2)}`,
14110
14252
  return true;
14111
14253
  });
14112
14254
  if (ok) return true;
14255
+ if (this.config.verbose) {
14256
+ console.warn(`[SqsReplicator] Connection test failed: ${err.message}`);
14257
+ }
14113
14258
  this.emit("connection_error", {
14114
14259
  replicator: this.name,
14115
14260
  error: err.message
@@ -14206,25 +14351,37 @@ ${JSON.stringify(validation, null, 2)}`,
14206
14351
  return;
14207
14352
  }
14208
14353
  resource.on("insert", async (data) => {
14209
- try {
14354
+ const [ok, error] = await try_fn_default(async () => {
14210
14355
  const completeData = { ...data, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
14211
14356
  await plugin.processReplicatorEvent("insert", resource.name, completeData.id, completeData);
14212
- } catch (error) {
14357
+ });
14358
+ if (!ok) {
14359
+ if (this.config.verbose) {
14360
+ console.warn(`[ReplicatorPlugin] Insert event failed for resource ${resource.name}: ${error.message}`);
14361
+ }
14213
14362
  this.emit("error", { operation: "insert", error: error.message, resource: resource.name });
14214
14363
  }
14215
14364
  });
14216
14365
  resource.on("update", async (data, beforeData) => {
14217
- try {
14366
+ const [ok, error] = await try_fn_default(async () => {
14218
14367
  const completeData = { ...data, updatedAt: (/* @__PURE__ */ new Date()).toISOString() };
14219
14368
  await plugin.processReplicatorEvent("update", resource.name, completeData.id, completeData, beforeData);
14220
- } catch (error) {
14369
+ });
14370
+ if (!ok) {
14371
+ if (this.config.verbose) {
14372
+ console.warn(`[ReplicatorPlugin] Update event failed for resource ${resource.name}: ${error.message}`);
14373
+ }
14221
14374
  this.emit("error", { operation: "update", error: error.message, resource: resource.name });
14222
14375
  }
14223
14376
  });
14224
14377
  resource.on("delete", async (data) => {
14225
- try {
14378
+ const [ok, error] = await try_fn_default(async () => {
14226
14379
  await plugin.processReplicatorEvent("delete", resource.name, data.id, data);
14227
- } catch (error) {
14380
+ });
14381
+ if (!ok) {
14382
+ if (this.config.verbose) {
14383
+ console.warn(`[ReplicatorPlugin] Delete event failed for resource ${resource.name}: ${error.message}`);
14384
+ }
14228
14385
  this.emit("error", { operation: "delete", error: error.message, resource: resource.name });
14229
14386
  }
14230
14387
  });
@@ -14240,13 +14397,17 @@ ${JSON.stringify(validation, null, 2)}`,
14240
14397
  }
14241
14398
  async setup(database) {
14242
14399
  this.database = database;
14243
- try {
14400
+ const [initOk, initError] = await try_fn_default(async () => {
14244
14401
  await this.initializeReplicators(database);
14245
- } catch (error) {
14246
- this.emit("error", { operation: "setup", error: error.message });
14247
- throw error;
14402
+ });
14403
+ if (!initOk) {
14404
+ if (this.config.verbose) {
14405
+ console.warn(`[ReplicatorPlugin] Replicator initialization failed: ${initError.message}`);
14406
+ }
14407
+ this.emit("error", { operation: "setup", error: initError.message });
14408
+ throw initError;
14248
14409
  }
14249
- try {
14410
+ const [logOk, logError] = await try_fn_default(async () => {
14250
14411
  if (this.config.replicatorLogResource) {
14251
14412
  const logRes = await database.createResource({
14252
14413
  name: this.config.replicatorLogResource,
@@ -14263,7 +14424,15 @@ ${JSON.stringify(validation, null, 2)}`,
14263
14424
  }
14264
14425
  });
14265
14426
  }
14266
- } catch (error) {
14427
+ });
14428
+ if (!logOk) {
14429
+ if (this.config.verbose) {
14430
+ console.warn(`[ReplicatorPlugin] Failed to create log resource ${this.config.replicatorLogResource}: ${logError.message}`);
14431
+ }
14432
+ this.emit("replicator_log_resource_creation_error", {
14433
+ resourceName: this.config.replicatorLogResource,
14434
+ error: logError.message
14435
+ });
14267
14436
  }
14268
14437
  await this.uploadMetadataFile(database);
14269
14438
  const originalCreateResource = database.createResource.bind(database);
@@ -14298,49 +14467,36 @@ ${JSON.stringify(validation, null, 2)}`,
14298
14467
  }
14299
14468
  async stop() {
14300
14469
  }
14301
- filterInternalFields(data) {
14302
- if (!data || typeof data !== "object") return data;
14303
- const filtered = {};
14304
- for (const [key, value] of Object.entries(data)) {
14305
- if (!key.startsWith("_") && !key.startsWith("$")) {
14306
- filtered[key] = value;
14307
- }
14308
- }
14309
- return filtered;
14310
- }
14311
14470
  async uploadMetadataFile(database) {
14312
14471
  if (typeof database.uploadMetadataFile === "function") {
14313
14472
  await database.uploadMetadataFile();
14314
14473
  }
14315
14474
  }
14316
- async getCompleteData(resource, data) {
14317
- try {
14318
- const [ok, err, record] = await try_fn_default(() => resource.get(data.id));
14319
- if (ok && record) {
14320
- return record;
14321
- }
14322
- } catch (error) {
14323
- }
14324
- return data;
14325
- }
14326
14475
  async retryWithBackoff(operation, maxRetries = 3) {
14327
14476
  let lastError;
14328
14477
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
14329
- try {
14330
- return await operation();
14331
- } catch (error) {
14478
+ const [ok, error] = await try_fn_default(operation);
14479
+ if (ok) {
14480
+ return ok;
14481
+ } else {
14332
14482
  lastError = error;
14483
+ if (this.config.verbose) {
14484
+ console.warn(`[ReplicatorPlugin] Retry attempt ${attempt}/${maxRetries} failed: ${error.message}`);
14485
+ }
14333
14486
  if (attempt === maxRetries) {
14334
14487
  throw error;
14335
14488
  }
14336
14489
  const delay = Math.pow(2, attempt - 1) * 1e3;
14490
+ if (this.config.verbose) {
14491
+ console.warn(`[ReplicatorPlugin] Waiting ${delay}ms before retry...`);
14492
+ }
14337
14493
  await new Promise((resolve) => setTimeout(resolve, delay));
14338
14494
  }
14339
14495
  }
14340
14496
  throw lastError;
14341
14497
  }
14342
14498
  async logError(replicator, resourceName, operation, recordId, data, error) {
14343
- try {
14499
+ const [ok, logError] = await try_fn_default(async () => {
14344
14500
  const logResourceName = this.config.replicatorLogResource;
14345
14501
  if (this.database && this.database.resources && this.database.resources[logResourceName]) {
14346
14502
  const logResource = this.database.resources[logResourceName];
@@ -14355,7 +14511,19 @@ ${JSON.stringify(validation, null, 2)}`,
14355
14511
  status: "error"
14356
14512
  });
14357
14513
  }
14358
- } catch (logError) {
14514
+ });
14515
+ if (!ok) {
14516
+ if (this.config.verbose) {
14517
+ console.warn(`[ReplicatorPlugin] Failed to log error for ${resourceName}: ${logError.message}`);
14518
+ }
14519
+ this.emit("replicator_log_error", {
14520
+ replicator: replicator.name || replicator.id,
14521
+ resourceName,
14522
+ operation,
14523
+ recordId,
14524
+ originalError: error.message,
14525
+ logError: logError.message
14526
+ });
14359
14527
  }
14360
14528
  }
14361
14529
  async processReplicatorEvent(operation, resourceName, recordId, data, beforeData = null) {
@@ -14368,8 +14536,8 @@ ${JSON.stringify(validation, null, 2)}`,
14368
14536
  return;
14369
14537
  }
14370
14538
  const promises = applicableReplicators.map(async (replicator) => {
14371
- try {
14372
- const result = await this.retryWithBackoff(
14539
+ const [ok, error, result] = await try_fn_default(async () => {
14540
+ const result2 = await this.retryWithBackoff(
14373
14541
  () => replicator.replicate(resourceName, operation, data, recordId, beforeData),
14374
14542
  this.config.maxRetries
14375
14543
  );
@@ -14378,11 +14546,17 @@ ${JSON.stringify(validation, null, 2)}`,
14378
14546
  resourceName,
14379
14547
  operation,
14380
14548
  recordId,
14381
- result,
14549
+ result: result2,
14382
14550
  success: true
14383
14551
  });
14552
+ return result2;
14553
+ });
14554
+ if (ok) {
14384
14555
  return result;
14385
- } catch (error) {
14556
+ } else {
14557
+ if (this.config.verbose) {
14558
+ console.warn(`[ReplicatorPlugin] Replication failed for ${replicator.name || replicator.id} on ${resourceName}: ${error.message}`);
14559
+ }
14386
14560
  this.emit("replicator_error", {
14387
14561
  replicator: replicator.name || replicator.id,
14388
14562
  resourceName,
@@ -14407,11 +14581,14 @@ ${JSON.stringify(validation, null, 2)}`,
14407
14581
  return;
14408
14582
  }
14409
14583
  const promises = applicableReplicators.map(async (replicator) => {
14410
- try {
14584
+ const [wrapperOk, wrapperError] = await try_fn_default(async () => {
14411
14585
  const [ok, err, result] = await try_fn_default(
14412
14586
  () => replicator.replicate(item.resourceName, item.operation, item.data, item.recordId, item.beforeData)
14413
14587
  );
14414
14588
  if (!ok) {
14589
+ if (this.config.verbose) {
14590
+ console.warn(`[ReplicatorPlugin] Replicator item processing failed for ${replicator.name || replicator.id} on ${item.resourceName}: ${err.message}`);
14591
+ }
14415
14592
  this.emit("replicator_error", {
14416
14593
  replicator: replicator.name || replicator.id,
14417
14594
  resourceName: item.resourceName,
@@ -14433,18 +14610,24 @@ ${JSON.stringify(validation, null, 2)}`,
14433
14610
  success: true
14434
14611
  });
14435
14612
  return { success: true, result };
14436
- } catch (error) {
14613
+ });
14614
+ if (wrapperOk) {
14615
+ return wrapperOk;
14616
+ } else {
14617
+ if (this.config.verbose) {
14618
+ console.warn(`[ReplicatorPlugin] Wrapper processing failed for ${replicator.name || replicator.id} on ${item.resourceName}: ${wrapperError.message}`);
14619
+ }
14437
14620
  this.emit("replicator_error", {
14438
14621
  replicator: replicator.name || replicator.id,
14439
14622
  resourceName: item.resourceName,
14440
14623
  operation: item.operation,
14441
14624
  recordId: item.recordId,
14442
- error: error.message
14625
+ error: wrapperError.message
14443
14626
  });
14444
14627
  if (this.config.logErrors && this.database) {
14445
- await this.logError(replicator, item.resourceName, item.operation, item.recordId, item.data, error);
14628
+ await this.logError(replicator, item.resourceName, item.operation, item.recordId, item.data, wrapperError);
14446
14629
  }
14447
- return { success: false, error: error.message };
14630
+ return { success: false, error: wrapperError.message };
14448
14631
  }
14449
14632
  });
14450
14633
  return Promise.allSettled(promises);
@@ -14466,9 +14649,13 @@ ${JSON.stringify(validation, null, 2)}`,
14466
14649
  timestamp: typeof item.timestamp === "number" ? item.timestamp : Date.now(),
14467
14650
  createdAt: item.createdAt || (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)
14468
14651
  };
14469
- try {
14652
+ const [ok, err] = await try_fn_default(async () => {
14470
14653
  await logRes.insert(logItem);
14471
- } catch (err) {
14654
+ });
14655
+ if (!ok) {
14656
+ if (this.config.verbose) {
14657
+ console.warn(`[ReplicatorPlugin] Failed to log replicator item: ${err.message}`);
14658
+ }
14472
14659
  this.emit("replicator.log.failed", { error: err, item });
14473
14660
  }
14474
14661
  }
@@ -14574,14 +14761,23 @@ ${JSON.stringify(validation, null, 2)}`,
14574
14761
  this.emit("replicator.sync.completed", { replicatorId, stats: this.stats });
14575
14762
  }
14576
14763
  async cleanup() {
14577
- try {
14764
+ const [ok, error] = await try_fn_default(async () => {
14578
14765
  if (this.replicators && this.replicators.length > 0) {
14579
14766
  const cleanupPromises = this.replicators.map(async (replicator) => {
14580
- try {
14767
+ const [replicatorOk, replicatorError] = await try_fn_default(async () => {
14581
14768
  if (replicator && typeof replicator.cleanup === "function") {
14582
14769
  await replicator.cleanup();
14583
14770
  }
14584
- } catch (error) {
14771
+ });
14772
+ if (!replicatorOk) {
14773
+ if (this.config.verbose) {
14774
+ console.warn(`[ReplicatorPlugin] Failed to cleanup replicator ${replicator.name || replicator.id}: ${replicatorError.message}`);
14775
+ }
14776
+ this.emit("replicator_cleanup_error", {
14777
+ replicator: replicator.name || replicator.id || "unknown",
14778
+ driver: replicator.driver || "unknown",
14779
+ error: replicatorError.message
14780
+ });
14585
14781
  }
14586
14782
  });
14587
14783
  await Promise.allSettled(cleanupPromises);
@@ -14590,7 +14786,14 @@ ${JSON.stringify(validation, null, 2)}`,
14590
14786
  this.database = null;
14591
14787
  this.eventListenersInstalled.clear();
14592
14788
  this.removeAllListeners();
14593
- } catch (error) {
14789
+ });
14790
+ if (!ok) {
14791
+ if (this.config.verbose) {
14792
+ console.warn(`[ReplicatorPlugin] Failed to cleanup plugin: ${error.message}`);
14793
+ }
14794
+ this.emit("replicator_plugin_cleanup_error", {
14795
+ error: error.message
14796
+ });
14594
14797
  }
14595
14798
  }
14596
14799
  }
@@ -14666,4 +14869,4 @@ ${JSON.stringify(validation, null, 2)}`,
14666
14869
 
14667
14870
  return exports;
14668
14871
 
14669
- })({}, nanoid, zlib, PromisePool, streams, promises, _, crypto, stringify, AWS, flat, FastestValidator);
14872
+ })({}, nanoid, zlib, PromisePool, streams, promises, crypto, _, stringify, AWS, flat, FastestValidator);