fauxbase 0.5.5 → 0.5.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/index.cjs CHANGED
@@ -748,7 +748,11 @@ var LocalDriver = class {
748
748
  authProvider = null;
749
749
  _ready;
750
750
  _isReady;
751
+ latencyMs;
752
+ errorRate;
751
753
  constructor(config) {
754
+ this.latencyMs = config.latency ?? 0;
755
+ this.errorRate = config.errorRate ?? 0;
752
756
  if (config.persist === "indexeddb") {
753
757
  const backend = new IndexedDBBackend(config.dbName ?? "fauxbase");
754
758
  this.storage = backend;
@@ -762,6 +766,27 @@ var LocalDriver = class {
762
766
  this._ready = Promise.resolve();
763
767
  }
764
768
  }
769
+ async simulate() {
770
+ if (this.errorRate > 0 && Math.random() < this.errorRate) {
771
+ const errors = [
772
+ () => new NetworkError("Simulated network failure"),
773
+ () => new TimeoutError("Simulated request timeout"),
774
+ () => new NetworkError("Simulated connection refused")
775
+ ];
776
+ const delay2 = this.getLatency();
777
+ if (delay2 > 0) await new Promise((r) => setTimeout(r, delay2 / 2));
778
+ throw errors[Math.floor(Math.random() * errors.length)]();
779
+ }
780
+ const delay = this.getLatency();
781
+ if (delay > 0) {
782
+ await new Promise((r) => setTimeout(r, delay));
783
+ }
784
+ }
785
+ getLatency() {
786
+ if (typeof this.latencyMs === "number") return this.latencyMs;
787
+ const { min, max } = this.latencyMs;
788
+ return Math.floor(Math.random() * (max - min + 1)) + min;
789
+ }
765
790
  get ready() {
766
791
  return this._ready;
767
792
  }
@@ -778,12 +803,14 @@ var LocalDriver = class {
778
803
  this.entityClasses.set(resource, entityClass);
779
804
  }
780
805
  async list(resource, query) {
806
+ await this.simulate();
781
807
  const items = this.storage.getAll(resource);
782
808
  const entityClass = this.entityClasses.get(resource);
783
809
  const processed = entityClass ? items.map((item) => applyComputedFields(item, entityClass)) : items;
784
810
  return executeQuery(processed, query);
785
811
  }
786
812
  async get(resource, id) {
813
+ await this.simulate();
787
814
  const item = this.storage.getById(resource, id);
788
815
  if (!item || item.deletedAt) {
789
816
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -793,6 +820,7 @@ var LocalDriver = class {
793
820
  return { data };
794
821
  }
795
822
  async create(resource, data) {
823
+ await this.simulate();
796
824
  const entityClass = this.entityClasses.get(resource);
797
825
  const now = (/* @__PURE__ */ new Date()).toISOString();
798
826
  const authContext = this.authProvider?.();
@@ -819,6 +847,7 @@ var LocalDriver = class {
819
847
  return { data: result };
820
848
  }
821
849
  async update(resource, id, data) {
850
+ await this.simulate();
822
851
  const existing = this.storage.getById(resource, id);
823
852
  if (!existing || existing.deletedAt) {
824
853
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -845,6 +874,7 @@ var LocalDriver = class {
845
874
  return { data: result };
846
875
  }
847
876
  async delete(resource, id) {
877
+ await this.simulate();
848
878
  const existing = this.storage.getById(resource, id);
849
879
  if (!existing || existing.deletedAt) {
850
880
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -867,6 +897,7 @@ var LocalDriver = class {
867
897
  return { data: record };
868
898
  }
869
899
  async count(resource, filter) {
900
+ await this.simulate();
870
901
  let items = this.storage.getAll(resource).filter((item) => !item.deletedAt);
871
902
  if (filter) {
872
903
  items = applyFilters(items, filter);