@ensnode/ensnode-sdk 1.3.0 → 1.3.1

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
@@ -793,183 +793,67 @@ function addDuration(timestamp, duration) {
793
793
  return deserializeUnixTimestamp(timestamp + duration, "UnixTimestamp");
794
794
  }
795
795
 
796
- // src/shared/cache/background-revalidation-scheduler.ts
797
- var BackgroundRevalidationScheduler = class {
798
- activeSchedules = /* @__PURE__ */ new Map();
799
- /**
800
- * Schedule a revalidation function to run on a recurring interval.
801
- *
802
- * @param config Configuration object for the schedule
803
- * @returns The revalidate function that can be passed to `cancel()` to stop the schedule
804
- */
805
- schedule(config) {
806
- const { revalidate, interval, initialDelay = 0, onError } = config;
807
- if (this.activeSchedules.has(revalidate)) {
808
- return revalidate;
796
+ // src/shared/cache/swr-cache.ts
797
+ var SWRCache = class {
798
+ constructor(options) {
799
+ this.options = options;
800
+ if (options.proactiveRevalidationInterval) {
801
+ this.backgroundInterval = setInterval(
802
+ () => this.revalidate(),
803
+ (0, import_date_fns.secondsToMilliseconds)(options.proactiveRevalidationInterval)
804
+ );
809
805
  }
810
- const metadata = {
811
- config,
812
- timeoutId: null,
813
- inProgress: false
814
- };
815
- this.activeSchedules.set(revalidate, metadata);
816
- const executeRevalidation = async () => {
817
- if (metadata.inProgress) return;
818
- metadata.inProgress = true;
819
- try {
820
- await revalidate();
821
- } catch (error) {
822
- onError?.(error);
823
- } finally {
824
- metadata.inProgress = false;
825
- }
826
- };
827
- const scheduleNext = () => {
828
- if (!this.activeSchedules.has(revalidate)) return;
829
- metadata.timeoutId = setTimeout(() => {
830
- if (this.activeSchedules.has(revalidate)) {
831
- executeRevalidation().then(() => scheduleNext());
832
- }
833
- }, interval);
834
- };
835
- if (initialDelay > 0) {
836
- metadata.timeoutId = setTimeout(() => {
837
- if (this.activeSchedules.has(revalidate)) {
838
- executeRevalidation().then(() => scheduleNext());
806
+ if (options.proactivelyInitialize) this.revalidate();
807
+ }
808
+ cache = null;
809
+ inProgressRevalidate = null;
810
+ backgroundInterval = null;
811
+ async revalidate() {
812
+ if (!this.inProgressRevalidate) {
813
+ this.inProgressRevalidate = this.options.fn().then((result) => {
814
+ this.cache = {
815
+ result,
816
+ updatedAt: (0, import_getUnixTime.getUnixTime)(/* @__PURE__ */ new Date())
817
+ };
818
+ }).catch((error) => {
819
+ if (!this.cache) {
820
+ this.cache = {
821
+ // ensure thrown value is always an Error instance
822
+ result: error instanceof Error ? error : new Error(String(error)),
823
+ updatedAt: (0, import_getUnixTime.getUnixTime)(/* @__PURE__ */ new Date())
824
+ };
839
825
  }
840
- }, initialDelay);
841
- } else {
842
- scheduleNext();
826
+ }).finally(() => {
827
+ this.inProgressRevalidate = null;
828
+ });
843
829
  }
844
- return revalidate;
830
+ return this.inProgressRevalidate;
845
831
  }
846
832
  /**
847
- * Cancel a scheduled revalidation by its revalidate function.
833
+ * Read the most recently cached result from the `SWRCache`.
848
834
  *
849
- * @param revalidate The revalidation function returned from `schedule()`
835
+ * @returns a `ValueType` that was most recently successfully returned by `fn` or `Error` if `fn`
836
+ * has never successfully returned.
850
837
  */
851
- cancel(revalidate) {
852
- const metadata = this.activeSchedules.get(revalidate);
853
- if (!metadata) return;
854
- if (metadata.timeoutId !== null) {
855
- clearTimeout(metadata.timeoutId);
838
+ async read() {
839
+ if (!this.cache) await this.revalidate();
840
+ if (!this.cache) throw new Error("never");
841
+ if (durationBetween(this.cache.updatedAt, (0, import_getUnixTime.getUnixTime)(/* @__PURE__ */ new Date())) > this.options.ttl) {
842
+ this.revalidate();
856
843
  }
857
- this.activeSchedules.delete(revalidate);
844
+ return this.cache.result;
858
845
  }
859
846
  /**
860
- * Cancel all active schedules.
847
+ * Destroys the background revalidation interval, if exists.
861
848
  */
862
- cancelAll() {
863
- for (const [, metadata] of this.activeSchedules) {
864
- if (metadata.timeoutId !== null) {
865
- clearTimeout(metadata.timeoutId);
866
- }
849
+ destroy() {
850
+ if (this.backgroundInterval) {
851
+ clearInterval(this.backgroundInterval);
852
+ this.backgroundInterval = null;
867
853
  }
868
- this.activeSchedules.clear();
869
- }
870
- /**
871
- * Get the count of active schedules.
872
- * Useful for debugging and monitoring.
873
- *
874
- * @returns The number of currently active schedules
875
- */
876
- getActiveScheduleCount() {
877
- return this.activeSchedules.size;
878
854
  }
879
855
  };
880
856
 
881
- // src/shared/cache/swr-cache.ts
882
- var bgRevalidationScheduler = new BackgroundRevalidationScheduler();
883
- var SWRCache = class _SWRCache {
884
- options;
885
- cache;
886
- /**
887
- * Optional promise of the current in-progress attempt to revalidate the `cache`.
888
- *
889
- * If null, no revalidation attempt is currently in progress.
890
- * If not null, identifies the revalidation attempt that is currently in progress.
891
- *
892
- * Used to enforce no concurrent revalidation attempts.
893
- */
894
- inProgressRevalidate;
895
- /**
896
- * The callback function being managed by `BackgroundRevalidationScheduler`.
897
- *
898
- * If null, no background revalidation is scheduled.
899
- * If not null, identifies the background revalidation that is currently scheduled.
900
- *
901
- * Used to enforce no concurrent background revalidation attempts.
902
- */
903
- scheduledBackgroundRevalidate;
904
- constructor(options) {
905
- this.cache = null;
906
- this.inProgressRevalidate = null;
907
- this.scheduledBackgroundRevalidate = null;
908
- this.options = options;
909
- }
910
- /**
911
- * Asynchronously create a new `SWRCache` instance.
912
- *
913
- * @param options - The {@link SWRCacheOptions} for the SWR cache.
914
- * @returns a new `SWRCache` instance.
915
- */
916
- static async create(options) {
917
- const cache = new _SWRCache(options);
918
- if (cache.options.proactivelyInitialize) {
919
- cache.readCache();
920
- }
921
- return cache;
922
- }
923
- revalidate = async () => {
924
- if (this.inProgressRevalidate) {
925
- return this.inProgressRevalidate;
926
- }
927
- return this.options.fn().then((value) => {
928
- this.cache = {
929
- value,
930
- updatedAt: (0, import_getUnixTime.getUnixTime)(/* @__PURE__ */ new Date())
931
- };
932
- return this.cache;
933
- }).catch(() => {
934
- return null;
935
- }).finally(() => {
936
- this.inProgressRevalidate = null;
937
- if (this.options.revalidationInterval === void 0) {
938
- return;
939
- }
940
- if (this.scheduledBackgroundRevalidate) {
941
- bgRevalidationScheduler.cancel(this.scheduledBackgroundRevalidate);
942
- }
943
- const backgroundRevalidate = async () => {
944
- this.revalidate();
945
- };
946
- this.scheduledBackgroundRevalidate = bgRevalidationScheduler.schedule({
947
- revalidate: backgroundRevalidate,
948
- interval: (0, import_date_fns.secondsToMilliseconds)(this.options.revalidationInterval)
949
- });
950
- });
951
- };
952
- /**
953
- * Read the most recently cached `CachedValue` from the `SWRCache`.
954
- *
955
- * @returns a `CachedValue` holding a `value` of `ValueType` that was most recently successfully returned by `fn`
956
- * or `null` if `fn` has never successfully returned and has always thrown an error,
957
- */
958
- readCache = async () => {
959
- if (!this.cache) {
960
- this.inProgressRevalidate = this.revalidate();
961
- return this.inProgressRevalidate;
962
- }
963
- if (durationBetween(this.cache.updatedAt, (0, import_getUnixTime.getUnixTime)(/* @__PURE__ */ new Date())) <= this.options.ttl) {
964
- return this.cache;
965
- }
966
- if (!this.inProgressRevalidate) {
967
- this.inProgressRevalidate = this.revalidate();
968
- }
969
- return this.cache;
970
- };
971
- };
972
-
973
857
  // src/shared/cache/ttl-cache.ts
974
858
  var import_getUnixTime2 = require("date-fns/getUnixTime");
975
859
  var TtlCache = class {
@@ -4179,7 +4063,11 @@ var translateDefaultableChainIdToChainId = (chainId, namespaceId) => {
4179
4063
  // src/resolution/resolver-records-selection.ts
4180
4064
  var isSelectionEmpty = (selection) => !selection.name && !selection.addresses?.length && !selection.texts?.length;
4181
4065
 
4182
- // src/tracing/index.ts
4066
+ // src/tracing/ens-protocol-tracing.ts
4067
+ var PROTOCOL_ATTRIBUTE_PREFIX = "ens";
4068
+ var ATTR_PROTOCOL_NAME = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol`;
4069
+ var ATTR_PROTOCOL_STEP = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol.step`;
4070
+ var ATTR_PROTOCOL_STEP_RESULT = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol.step.result`;
4183
4071
  var TraceableENSProtocol = /* @__PURE__ */ ((TraceableENSProtocol2) => {
4184
4072
  TraceableENSProtocol2["ForwardResolution"] = "forward-resolution";
4185
4073
  TraceableENSProtocol2["ReverseResolution"] = "reverse-resolution";
@@ -4204,8 +4092,4 @@ var ReverseResolutionProtocolStep = /* @__PURE__ */ ((ReverseResolutionProtocolS
4204
4092
  ReverseResolutionProtocolStep2["VerifyResolvedAddressMatchesAddress"] = "verify-resolved-address-matches-address";
4205
4093
  return ReverseResolutionProtocolStep2;
4206
4094
  })(ReverseResolutionProtocolStep || {});
4207
- var PROTOCOL_ATTRIBUTE_PREFIX = "ens";
4208
- var ATTR_PROTOCOL_NAME = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol`;
4209
- var ATTR_PROTOCOL_STEP = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol.step`;
4210
- var ATTR_PROTOCOL_STEP_RESULT = `${PROTOCOL_ATTRIBUTE_PREFIX}.protocol.step.result`;
4211
4095
  //# sourceMappingURL=index.cjs.map