@twin.org/core 0.0.1-next.51 → 0.0.1-next.52

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.
Files changed (54) hide show
  1. package/dist/cjs/index.cjs +144 -70
  2. package/dist/esm/index.mjs +144 -71
  3. package/dist/types/index.d.ts +2 -0
  4. package/dist/types/models/II18nShared.d.ts +29 -0
  5. package/dist/types/utils/sharedStore.d.ts +23 -0
  6. package/docs/changelog.md +7 -0
  7. package/docs/reference/classes/AlreadyExistsError.md +7 -7
  8. package/docs/reference/classes/ArrayHelper.md +3 -3
  9. package/docs/reference/classes/AsyncCache.md +14 -8
  10. package/docs/reference/classes/Base32.md +3 -3
  11. package/docs/reference/classes/Base58.md +3 -3
  12. package/docs/reference/classes/Base64.md +3 -3
  13. package/docs/reference/classes/Base64Url.md +3 -3
  14. package/docs/reference/classes/BaseError.md +5 -5
  15. package/docs/reference/classes/BitString.md +5 -5
  16. package/docs/reference/classes/Coerce.md +7 -5
  17. package/docs/reference/classes/Compression.md +3 -3
  18. package/docs/reference/classes/ConflictError.md +7 -7
  19. package/docs/reference/classes/Converter.md +6 -6
  20. package/docs/reference/classes/EnvHelper.md +7 -5
  21. package/docs/reference/classes/ErrorHelper.md +3 -3
  22. package/docs/reference/classes/Factory.md +17 -7
  23. package/docs/reference/classes/FilenameHelper.md +3 -3
  24. package/docs/reference/classes/GeneralError.md +7 -7
  25. package/docs/reference/classes/GuardError.md +7 -7
  26. package/docs/reference/classes/Guards.md +18 -8
  27. package/docs/reference/classes/HexHelper.md +3 -3
  28. package/docs/reference/classes/I18n.md +6 -6
  29. package/docs/reference/classes/Is.md +21 -9
  30. package/docs/reference/classes/JsonHelper.md +10 -6
  31. package/docs/reference/classes/NotFoundError.md +7 -7
  32. package/docs/reference/classes/NotImplementedError.md +7 -7
  33. package/docs/reference/classes/NotSupportedError.md +7 -7
  34. package/docs/reference/classes/ObjectHelper.md +40 -18
  35. package/docs/reference/classes/RandomHelper.md +3 -3
  36. package/docs/reference/classes/SharedStore.md +94 -0
  37. package/docs/reference/classes/StringHelper.md +3 -3
  38. package/docs/reference/classes/Uint8ArrayHelper.md +3 -3
  39. package/docs/reference/classes/UnauthorizedError.md +7 -7
  40. package/docs/reference/classes/UnprocessableError.md +7 -7
  41. package/docs/reference/classes/Url.md +7 -7
  42. package/docs/reference/classes/Urn.md +9 -9
  43. package/docs/reference/classes/Validation.md +36 -28
  44. package/docs/reference/classes/ValidationError.md +7 -7
  45. package/docs/reference/index.md +2 -0
  46. package/docs/reference/interfaces/IComponent.md +3 -3
  47. package/docs/reference/interfaces/IError.md +1 -1
  48. package/docs/reference/interfaces/II18nShared.md +47 -0
  49. package/docs/reference/interfaces/IKeyValue.md +3 -1
  50. package/docs/reference/interfaces/ILabelledValue.md +3 -1
  51. package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
  52. package/docs/reference/type-aliases/CoerceType.md +1 -1
  53. package/docs/reference/type-aliases/CompressionType.md +1 -1
  54. package/package.json +2 -2
@@ -1816,6 +1816,53 @@ class Guards {
1816
1816
  }
1817
1817
  }
1818
1818
 
1819
+ // Copyright 2024 IOTA Stiftung.
1820
+ // SPDX-License-Identifier: Apache-2.0.
1821
+ /**
1822
+ * Provide a store for shared objects which can be accesses through multiple
1823
+ * instance loads of a packages.
1824
+ */
1825
+ class SharedStore {
1826
+ /**
1827
+ * Get a property from the shared store.
1828
+ * @param prop The name of the property to get.
1829
+ * @returns The property if it exists.
1830
+ */
1831
+ static get(prop) {
1832
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1833
+ const shared = globalThis.__TWIN_SHARED__;
1834
+ if (Is.undefined(shared)) {
1835
+ return;
1836
+ }
1837
+ return shared[prop];
1838
+ }
1839
+ /**
1840
+ * Set the property in the shared store.
1841
+ * @param prop The name of the property to set.
1842
+ * @param value The value to set.
1843
+ */
1844
+ static set(prop, value) {
1845
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1846
+ if (Is.undefined(globalThis.__TWIN_SHARED__)) {
1847
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1848
+ globalThis.__TWIN_SHARED__ = {};
1849
+ }
1850
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1851
+ globalThis.__TWIN_SHARED__[prop] = value;
1852
+ }
1853
+ /**
1854
+ * Remove a property from the shared store.
1855
+ * @param prop The name of the property to remove.
1856
+ */
1857
+ static remove(prop) {
1858
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1859
+ const shared = globalThis.__TWIN_SHARED__;
1860
+ if (!Is.undefined(shared)) {
1861
+ delete shared[prop];
1862
+ }
1863
+ }
1864
+ }
1865
+
1819
1866
  /**
1820
1867
  * Factory for creating implementation of generic types.
1821
1868
  */
@@ -1825,11 +1872,6 @@ class Factory {
1825
1872
  * @internal
1826
1873
  */
1827
1874
  static _CLASS_NAME = "Factory";
1828
- /**
1829
- * Store all the created factories.
1830
- * @internal
1831
- */
1832
- static _factories = {};
1833
1875
  /**
1834
1876
  * Type name for the instances.
1835
1877
  * @internal
@@ -1883,32 +1925,40 @@ class Factory {
1883
1925
  * @returns The factory instance.
1884
1926
  */
1885
1927
  static createFactory(typeName, autoInstance = false, matcher) {
1886
- if (Is.undefined(Factory._factories[typeName])) {
1887
- Factory._factories[typeName] = new Factory(typeName, autoInstance, matcher);
1928
+ const factories = Factory.getFactories();
1929
+ if (Is.undefined(factories[typeName])) {
1930
+ factories[typeName] = new Factory(typeName, autoInstance, matcher);
1888
1931
  }
1889
- return Factory._factories[typeName];
1932
+ return factories[typeName];
1890
1933
  }
1891
1934
  /**
1892
1935
  * Get all the factories.
1893
1936
  * @returns All the factories.
1894
1937
  */
1895
1938
  static getFactories() {
1896
- return Factory._factories;
1939
+ let factories = SharedStore.get("factories");
1940
+ if (Is.undefined(factories)) {
1941
+ factories = {};
1942
+ SharedStore.set("factories", factories);
1943
+ }
1944
+ return factories;
1897
1945
  }
1898
1946
  /**
1899
1947
  * Reset all the factories, which removes any created instances, but not the registrations.
1900
1948
  */
1901
1949
  static resetFactories() {
1902
- for (const typeName in Factory._factories) {
1903
- Factory._factories[typeName].reset();
1950
+ const factories = Factory.getFactories();
1951
+ for (const typeName in factories) {
1952
+ factories[typeName].reset();
1904
1953
  }
1905
1954
  }
1906
1955
  /**
1907
1956
  * Clear all the factories, which removes anything registered with the factories.
1908
1957
  */
1909
1958
  static clearFactories() {
1910
- for (const typeName in Factory._factories) {
1911
- Factory._factories[typeName].clear();
1959
+ const factories = Factory.getFactories();
1960
+ for (const typeName in factories) {
1961
+ factories[typeName].clear();
1912
1962
  }
1913
1963
  }
1914
1964
  /**
@@ -2856,34 +2906,15 @@ class I18n {
2856
2906
  * The default translation.
2857
2907
  */
2858
2908
  static DEFAULT_LOCALE = "en";
2859
- /**
2860
- * Dictionaries for lookups.
2861
- * @internal
2862
- */
2863
- static _localeDictionaries = {};
2864
- /**
2865
- * The current locale.
2866
- * @internal
2867
- */
2868
- static _currentLocale = I18n.DEFAULT_LOCALE;
2869
- /**
2870
- * Change handler for the locale being updated.
2871
- * @internal
2872
- */
2873
- static _localeChangedHandlers = {};
2874
- /**
2875
- * Change handler for the dictionaries being updated.
2876
- * @internal
2877
- */
2878
- static _dictionaryChangedHandlers = {};
2879
2909
  /**
2880
2910
  * Set the locale.
2881
2911
  * @param locale The new locale.
2882
2912
  */
2883
2913
  static setLocale(locale) {
2884
- I18n._currentLocale = locale;
2885
- for (const callback in I18n._localeChangedHandlers) {
2886
- I18n._localeChangedHandlers[callback](I18n._currentLocale);
2914
+ const i18nShared = I18n.getI18nShared();
2915
+ i18nShared.currentLocale = locale;
2916
+ for (const callback in i18nShared.localeChangedHandlers) {
2917
+ i18nShared.localeChangedHandlers[callback](i18nShared.currentLocale);
2887
2918
  }
2888
2919
  }
2889
2920
  /**
@@ -2891,7 +2922,8 @@ class I18n {
2891
2922
  * @returns The current locale.
2892
2923
  */
2893
2924
  static getLocale() {
2894
- return I18n._currentLocale;
2925
+ const i18nShared = I18n.getI18nShared();
2926
+ return i18nShared.currentLocale;
2895
2927
  }
2896
2928
  /**
2897
2929
  * Add a locale dictionary.
@@ -2899,11 +2931,12 @@ class I18n {
2899
2931
  * @param dictionary The dictionary to add.
2900
2932
  */
2901
2933
  static addDictionary(locale, dictionary) {
2934
+ const i18nShared = I18n.getI18nShared();
2902
2935
  const mergedKeys = {};
2903
2936
  I18n.flattenTranslationKeys(dictionary, "", mergedKeys);
2904
- I18n._localeDictionaries[locale] = mergedKeys;
2905
- for (const callback in I18n._dictionaryChangedHandlers) {
2906
- I18n._dictionaryChangedHandlers[callback](I18n._currentLocale);
2937
+ i18nShared.localeDictionaries[locale] = mergedKeys;
2938
+ for (const callback in i18nShared.dictionaryChangedHandlers) {
2939
+ i18nShared.dictionaryChangedHandlers[callback](i18nShared.currentLocale);
2907
2940
  }
2908
2941
  }
2909
2942
  /**
@@ -2912,14 +2945,16 @@ class I18n {
2912
2945
  * @returns The dictionary of undefined if it does not exist.
2913
2946
  */
2914
2947
  static getDictionary(locale) {
2915
- return I18n._localeDictionaries[locale];
2948
+ const i18nShared = I18n.getI18nShared();
2949
+ return i18nShared.localeDictionaries[locale];
2916
2950
  }
2917
2951
  /**
2918
2952
  * Get all the locale dictionaries.
2919
2953
  * @returns The dictionaries.
2920
2954
  */
2921
2955
  static getAllDictionaries() {
2922
- return I18n._localeDictionaries;
2956
+ const i18nShared = I18n.getI18nShared();
2957
+ return i18nShared.localeDictionaries;
2923
2958
  }
2924
2959
  /**
2925
2960
  * Add a locale changed handler.
@@ -2927,14 +2962,16 @@ class I18n {
2927
2962
  * @param handler The handler to add.
2928
2963
  */
2929
2964
  static addLocaleHandler(id, handler) {
2930
- I18n._localeChangedHandlers[id] = handler;
2965
+ const i18nShared = I18n.getI18nShared();
2966
+ i18nShared.localeChangedHandlers[id] = handler;
2931
2967
  }
2932
2968
  /**
2933
2969
  * Remove a locale changed handler.
2934
2970
  * @param id The id of the handler.
2935
2971
  */
2936
2972
  static removeLocaleHandler(id) {
2937
- delete I18n._localeChangedHandlers[id];
2973
+ const i18nShared = I18n.getI18nShared();
2974
+ delete i18nShared.localeChangedHandlers[id];
2938
2975
  }
2939
2976
  /**
2940
2977
  * Add a dictionary changed handler.
@@ -2942,14 +2979,16 @@ class I18n {
2942
2979
  * @param handler The handler to add.
2943
2980
  */
2944
2981
  static addDictionaryHandler(id, handler) {
2945
- I18n._dictionaryChangedHandlers[id] = handler;
2982
+ const i18nShared = I18n.getI18nShared();
2983
+ i18nShared.dictionaryChangedHandlers[id] = handler;
2946
2984
  }
2947
2985
  /**
2948
2986
  * Remove a dictionary changed handler.
2949
2987
  * @param id The id of the handler.
2950
2988
  */
2951
2989
  static removeDictionaryHandler(id) {
2952
- delete I18n._dictionaryChangedHandlers[id];
2990
+ const i18nShared = I18n.getI18nShared();
2991
+ delete i18nShared.dictionaryChangedHandlers[id];
2953
2992
  }
2954
2993
  /**
2955
2994
  * Format a message.
@@ -2959,21 +2998,22 @@ class I18n {
2959
2998
  * @returns The formatted string.
2960
2999
  */
2961
3000
  static formatMessage(key, values, overrideLocale) {
2962
- let cl = overrideLocale ?? I18n._currentLocale;
3001
+ const i18nShared = I18n.getI18nShared();
3002
+ let cl = overrideLocale ?? i18nShared.currentLocale;
2963
3003
  if (cl.startsWith("debug-")) {
2964
3004
  cl = I18n.DEFAULT_LOCALE;
2965
3005
  }
2966
- if (!I18n._localeDictionaries[cl]) {
3006
+ if (!i18nShared.localeDictionaries[cl]) {
2967
3007
  return `!!Missing ${cl}`;
2968
3008
  }
2969
- if (!I18n._localeDictionaries[cl][key]) {
3009
+ if (!i18nShared.localeDictionaries[cl][key]) {
2970
3010
  return `!!Missing ${cl}.${key}`;
2971
3011
  }
2972
- if (I18n._currentLocale === "debug-k") {
3012
+ if (i18nShared.currentLocale === "debug-k") {
2973
3013
  return key;
2974
3014
  }
2975
- let ret = new intlMessageformat.IntlMessageFormat(I18n._localeDictionaries[cl][key], cl).format(values);
2976
- if (I18n._currentLocale === "debug-x") {
3015
+ let ret = new intlMessageformat.IntlMessageFormat(i18nShared.localeDictionaries[cl][key], cl).format(values);
3016
+ if (i18nShared.currentLocale === "debug-x") {
2977
3017
  ret = ret.replace(/[a-z]/g, "x").replace(/[A-Z]/g, "x").replace(/\d/g, "n");
2978
3018
  }
2979
3019
  return ret;
@@ -2984,7 +3024,8 @@ class I18n {
2984
3024
  * @returns True if the key exists.
2985
3025
  */
2986
3026
  static hasMessage(key) {
2987
- return Is.string(I18n._localeDictionaries[I18n._currentLocale]?.[key]);
3027
+ const i18nShared = I18n.getI18nShared();
3028
+ return Is.string(i18nShared.localeDictionaries[i18nShared.currentLocale]?.[key]);
2988
3029
  }
2989
3030
  /**
2990
3031
  * Flatten the translation property paths for faster lookup.
@@ -3005,6 +3046,24 @@ class I18n {
3005
3046
  }
3006
3047
  }
3007
3048
  }
3049
+ /**
3050
+ * Get the I18n shared data.
3051
+ * @returns The I18n shared data.
3052
+ * @internal
3053
+ */
3054
+ static getI18nShared() {
3055
+ let i18nShared = SharedStore.get("i18n");
3056
+ if (Is.undefined(i18nShared)) {
3057
+ i18nShared = {
3058
+ localeDictionaries: {},
3059
+ currentLocale: I18n.DEFAULT_LOCALE,
3060
+ localeChangedHandlers: {},
3061
+ dictionaryChangedHandlers: {}
3062
+ };
3063
+ SharedStore.set("i18n", i18nShared);
3064
+ }
3065
+ return i18nShared;
3066
+ }
3008
3067
  }
3009
3068
 
3010
3069
  // Copyright 2024 IOTA Stiftung.
@@ -3942,11 +4001,6 @@ class Urn {
3942
4001
  * Cache the results from asynchronous requests.
3943
4002
  */
3944
4003
  class AsyncCache {
3945
- /**
3946
- * Cache for the fetch requests.
3947
- * @internal
3948
- */
3949
- static _cache = {};
3950
4004
  /**
3951
4005
  * Execute an async request and cache the result.
3952
4006
  * @param key The key for the entry in the cache.
@@ -3958,13 +4012,14 @@ class AsyncCache {
3958
4012
  const cacheEnabled = Is.integer(ttlMs) && ttlMs >= 0;
3959
4013
  if (cacheEnabled) {
3960
4014
  AsyncCache.cleanupExpired();
3961
- if (!this._cache[key]) {
3962
- this._cache[key] = {
4015
+ const cache = AsyncCache.getSharedCache();
4016
+ if (!cache[key]) {
4017
+ cache[key] = {
3963
4018
  response: requestMethod(),
3964
4019
  expires: ttlMs === 0 ? 0 : Date.now() + ttlMs
3965
4020
  };
3966
4021
  }
3967
- return this._cache[key].response;
4022
+ return cache[key].response;
3968
4023
  }
3969
4024
  }
3970
4025
  /**
@@ -3973,7 +4028,8 @@ class AsyncCache {
3973
4028
  * @returns The item from the cache if it exists.
3974
4029
  */
3975
4030
  static async get(key) {
3976
- return AsyncCache._cache[key]?.response;
4031
+ const cache = AsyncCache.getSharedCache();
4032
+ return cache[key]?.response;
3977
4033
  }
3978
4034
  /**
3979
4035
  * Set an entry into the cache.
@@ -3983,7 +4039,8 @@ class AsyncCache {
3983
4039
  * @returns Nothing.
3984
4040
  */
3985
4041
  static async set(key, value, ttlMs) {
3986
- AsyncCache._cache[key] = {
4042
+ const cache = AsyncCache.getSharedCache();
4043
+ cache[key] = {
3987
4044
  response: Promise.resolve(value),
3988
4045
  expires: Date.now() + (ttlMs ?? 1000)
3989
4046
  };
@@ -3993,34 +4050,50 @@ class AsyncCache {
3993
4050
  * @param key The key to remove from the cache.
3994
4051
  */
3995
4052
  static remove(key) {
3996
- delete AsyncCache._cache[key];
4053
+ const cache = AsyncCache.getSharedCache();
4054
+ delete cache[key];
3997
4055
  }
3998
4056
  /**
3999
4057
  * Clear the cache.
4000
4058
  * @param prefix Optional prefix to clear only entries with that prefix.
4001
4059
  */
4002
4060
  static clearCache(prefix) {
4061
+ const cache = AsyncCache.getSharedCache();
4003
4062
  if (Is.stringValue(prefix)) {
4004
- for (const entry in this._cache) {
4063
+ for (const entry in cache) {
4005
4064
  if (entry.startsWith(prefix)) {
4006
- delete this._cache[entry];
4065
+ delete cache[entry];
4007
4066
  }
4008
4067
  }
4009
4068
  }
4010
4069
  else {
4011
- AsyncCache._cache = {};
4070
+ SharedStore.set("asyncCache", {});
4012
4071
  }
4013
4072
  }
4014
4073
  /**
4015
4074
  * Perform a cleanup of the expired entries in the cache.
4016
4075
  */
4017
4076
  static cleanupExpired() {
4018
- for (const entry in this._cache) {
4019
- if (AsyncCache._cache[entry].expires > 0 && AsyncCache._cache[entry].expires < Date.now()) {
4020
- delete this._cache[entry];
4077
+ const cache = AsyncCache.getSharedCache();
4078
+ for (const entry in cache) {
4079
+ if (cache[entry].expires > 0 && cache[entry].expires < Date.now()) {
4080
+ delete cache[entry];
4021
4081
  }
4022
4082
  }
4023
4083
  }
4084
+ /**
4085
+ * Get the shared cache.
4086
+ * @returns The shared cache.
4087
+ * @internal
4088
+ */
4089
+ static getSharedCache() {
4090
+ let sharedCache = SharedStore.get("asyncCache");
4091
+ if (Is.undefined(sharedCache)) {
4092
+ sharedCache = {};
4093
+ SharedStore.set("asyncCache", sharedCache);
4094
+ }
4095
+ return sharedCache;
4096
+ }
4024
4097
  }
4025
4098
 
4026
4099
  /**
@@ -4876,6 +4949,7 @@ exports.NotImplementedError = NotImplementedError;
4876
4949
  exports.NotSupportedError = NotSupportedError;
4877
4950
  exports.ObjectHelper = ObjectHelper;
4878
4951
  exports.RandomHelper = RandomHelper;
4952
+ exports.SharedStore = SharedStore;
4879
4953
  exports.StringHelper = StringHelper;
4880
4954
  exports.Uint8ArrayHelper = Uint8ArrayHelper;
4881
4955
  exports.UnauthorizedError = UnauthorizedError;