@twin.org/core 0.0.1-next.51 → 0.0.1-next.53
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/cjs/index.cjs +225 -75
- package/dist/esm/index.mjs +225 -76
- package/dist/types/index.d.ts +2 -0
- package/dist/types/models/II18nShared.d.ts +29 -0
- package/dist/types/utils/asyncCache.d.ts +2 -1
- package/dist/types/utils/sharedStore.d.ts +23 -0
- package/docs/changelog.md +14 -0
- package/docs/reference/classes/AlreadyExistsError.md +7 -7
- package/docs/reference/classes/ArrayHelper.md +3 -3
- package/docs/reference/classes/AsyncCache.md +21 -9
- package/docs/reference/classes/Base32.md +3 -3
- package/docs/reference/classes/Base58.md +3 -3
- package/docs/reference/classes/Base64.md +3 -3
- package/docs/reference/classes/Base64Url.md +3 -3
- package/docs/reference/classes/BaseError.md +5 -5
- package/docs/reference/classes/BitString.md +5 -5
- package/docs/reference/classes/Coerce.md +7 -5
- package/docs/reference/classes/Compression.md +3 -3
- package/docs/reference/classes/ConflictError.md +7 -7
- package/docs/reference/classes/Converter.md +6 -6
- package/docs/reference/classes/EnvHelper.md +7 -5
- package/docs/reference/classes/ErrorHelper.md +3 -3
- package/docs/reference/classes/Factory.md +17 -7
- package/docs/reference/classes/FilenameHelper.md +3 -3
- package/docs/reference/classes/GeneralError.md +7 -7
- package/docs/reference/classes/GuardError.md +7 -7
- package/docs/reference/classes/Guards.md +18 -8
- package/docs/reference/classes/HexHelper.md +3 -3
- package/docs/reference/classes/I18n.md +6 -6
- package/docs/reference/classes/Is.md +21 -9
- package/docs/reference/classes/JsonHelper.md +10 -6
- package/docs/reference/classes/NotFoundError.md +7 -7
- package/docs/reference/classes/NotImplementedError.md +7 -7
- package/docs/reference/classes/NotSupportedError.md +7 -7
- package/docs/reference/classes/ObjectHelper.md +40 -18
- package/docs/reference/classes/RandomHelper.md +3 -3
- package/docs/reference/classes/SharedStore.md +94 -0
- package/docs/reference/classes/StringHelper.md +3 -3
- package/docs/reference/classes/Uint8ArrayHelper.md +3 -3
- package/docs/reference/classes/UnauthorizedError.md +7 -7
- package/docs/reference/classes/UnprocessableError.md +7 -7
- package/docs/reference/classes/Url.md +7 -7
- package/docs/reference/classes/Urn.md +9 -9
- package/docs/reference/classes/Validation.md +36 -28
- package/docs/reference/classes/ValidationError.md +7 -7
- package/docs/reference/index.md +2 -0
- package/docs/reference/interfaces/IComponent.md +3 -3
- package/docs/reference/interfaces/IError.md +1 -1
- package/docs/reference/interfaces/II18nShared.md +47 -0
- package/docs/reference/interfaces/IKeyValue.md +3 -1
- package/docs/reference/interfaces/ILabelledValue.md +3 -1
- package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
- package/docs/reference/type-aliases/CoerceType.md +1 -1
- package/docs/reference/type-aliases/CompressionType.md +1 -1
- package/package.json +2 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -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
|
-
|
|
1887
|
-
|
|
1928
|
+
const factories = Factory.getFactories();
|
|
1929
|
+
if (Is.undefined(factories[typeName])) {
|
|
1930
|
+
factories[typeName] = new Factory(typeName, autoInstance, matcher);
|
|
1888
1931
|
}
|
|
1889
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
1903
|
-
|
|
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
|
-
|
|
1911
|
-
|
|
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
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2905
|
-
for (const callback in
|
|
2906
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (!
|
|
3006
|
+
if (!i18nShared.localeDictionaries[cl]) {
|
|
2967
3007
|
return `!!Missing ${cl}`;
|
|
2968
3008
|
}
|
|
2969
|
-
if (!
|
|
3009
|
+
if (!i18nShared.localeDictionaries[cl][key]) {
|
|
2970
3010
|
return `!!Missing ${cl}.${key}`;
|
|
2971
3011
|
}
|
|
2972
|
-
if (
|
|
3012
|
+
if (i18nShared.currentLocale === "debug-k") {
|
|
2973
3013
|
return key;
|
|
2974
3014
|
}
|
|
2975
|
-
let ret = new intlMessageformat.IntlMessageFormat(
|
|
2976
|
-
if (
|
|
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
|
-
|
|
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,29 +4001,93 @@ 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.
|
|
3953
4007
|
* @param ttlMs The TTL of the entry in the cache.
|
|
3954
4008
|
* @param requestMethod The method to call if not cached.
|
|
4009
|
+
* @param cacheFailures Cache failure results, defaults to false.
|
|
3955
4010
|
* @returns The response.
|
|
3956
4011
|
*/
|
|
3957
|
-
static exec(key, ttlMs, requestMethod) {
|
|
4012
|
+
static exec(key, ttlMs, requestMethod, cacheFailures) {
|
|
3958
4013
|
const cacheEnabled = Is.integer(ttlMs) && ttlMs >= 0;
|
|
3959
4014
|
if (cacheEnabled) {
|
|
3960
4015
|
AsyncCache.cleanupExpired();
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
4016
|
+
const cache = AsyncCache.getSharedCache();
|
|
4017
|
+
// Do we have a cache entry for the key
|
|
4018
|
+
if (cache[key]) {
|
|
4019
|
+
if (!Is.empty(cache[key].result)) {
|
|
4020
|
+
// If the cache has already resulted in a value, resolve it
|
|
4021
|
+
return Promise.resolve(cache[key].result);
|
|
4022
|
+
}
|
|
4023
|
+
else if (!Is.empty(cache[key].error)) {
|
|
4024
|
+
// If the cache has already resulted in an error, reject it
|
|
4025
|
+
return Promise.reject(cache[key].error);
|
|
4026
|
+
}
|
|
4027
|
+
// Otherwise create a promise to return and store the resolver
|
|
4028
|
+
// and rejector in the cache entry, so that we can call then
|
|
4029
|
+
// when the request is done
|
|
4030
|
+
let storedResolve;
|
|
4031
|
+
let storedReject;
|
|
4032
|
+
const wait = new Promise((resolve, reject) => {
|
|
4033
|
+
storedResolve = resolve;
|
|
4034
|
+
storedReject = reject;
|
|
4035
|
+
});
|
|
4036
|
+
if (!Is.empty(storedResolve) && !Is.empty(storedReject)) {
|
|
4037
|
+
cache[key].promiseQueue.push({
|
|
4038
|
+
requestMethod,
|
|
4039
|
+
resolve: storedResolve,
|
|
4040
|
+
reject: storedReject
|
|
4041
|
+
});
|
|
4042
|
+
}
|
|
4043
|
+
return wait;
|
|
3966
4044
|
}
|
|
3967
|
-
|
|
4045
|
+
// If we don't have a cache entry, create a new one
|
|
4046
|
+
cache[key] = {
|
|
4047
|
+
promiseQueue: [],
|
|
4048
|
+
expires: ttlMs === 0 ? 0 : Date.now() + ttlMs
|
|
4049
|
+
};
|
|
4050
|
+
// Return a promise that wraps the original request method
|
|
4051
|
+
// so that we can store any results or errors in the cache
|
|
4052
|
+
return new Promise((resolve, reject) => {
|
|
4053
|
+
// Call the request method and store the result
|
|
4054
|
+
requestMethod()
|
|
4055
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
4056
|
+
.then(res => {
|
|
4057
|
+
// If the request was successful, store the result
|
|
4058
|
+
cache[key].result = res;
|
|
4059
|
+
// and resolve both this promise and all the waiters
|
|
4060
|
+
resolve(res);
|
|
4061
|
+
for (const wait of cache[key].promiseQueue) {
|
|
4062
|
+
wait.resolve(res);
|
|
4063
|
+
}
|
|
4064
|
+
return res;
|
|
4065
|
+
})
|
|
4066
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
4067
|
+
.catch((err) => {
|
|
4068
|
+
// Reject the promise
|
|
4069
|
+
reject(err);
|
|
4070
|
+
// Handle the waiters based on the cacheFailures flag
|
|
4071
|
+
if (cacheFailures ?? false) {
|
|
4072
|
+
// If we are caching failures, store the error and reject the waiters
|
|
4073
|
+
cache[key].error = err;
|
|
4074
|
+
for (const wait of cache[key].promiseQueue) {
|
|
4075
|
+
wait.reject(err);
|
|
4076
|
+
}
|
|
4077
|
+
// Clear the waiters so we don't call them again
|
|
4078
|
+
cache[key].promiseQueue = [];
|
|
4079
|
+
}
|
|
4080
|
+
else {
|
|
4081
|
+
// If not caching failures for any queued requests we
|
|
4082
|
+
// have no value to either resolve or reject, so we
|
|
4083
|
+
// just resolve with the original request method
|
|
4084
|
+
for (const wait of cache[key].promiseQueue) {
|
|
4085
|
+
wait.resolve(wait.requestMethod());
|
|
4086
|
+
}
|
|
4087
|
+
delete cache[key];
|
|
4088
|
+
}
|
|
4089
|
+
});
|
|
4090
|
+
});
|
|
3968
4091
|
}
|
|
3969
4092
|
}
|
|
3970
4093
|
/**
|
|
@@ -3973,7 +4096,15 @@ class AsyncCache {
|
|
|
3973
4096
|
* @returns The item from the cache if it exists.
|
|
3974
4097
|
*/
|
|
3975
4098
|
static async get(key) {
|
|
3976
|
-
|
|
4099
|
+
const cache = AsyncCache.getSharedCache();
|
|
4100
|
+
if (!Is.empty(cache[key].result)) {
|
|
4101
|
+
// If the cache has already resulted in a value, resolve it
|
|
4102
|
+
return cache[key].result;
|
|
4103
|
+
}
|
|
4104
|
+
else if (!Is.empty(cache[key].error)) {
|
|
4105
|
+
// If the cache has already resulted in an error, reject it
|
|
4106
|
+
throw cache[key].error;
|
|
4107
|
+
}
|
|
3977
4108
|
}
|
|
3978
4109
|
/**
|
|
3979
4110
|
* Set an entry into the cache.
|
|
@@ -3983,8 +4114,10 @@ class AsyncCache {
|
|
|
3983
4114
|
* @returns Nothing.
|
|
3984
4115
|
*/
|
|
3985
4116
|
static async set(key, value, ttlMs) {
|
|
3986
|
-
|
|
3987
|
-
|
|
4117
|
+
const cache = AsyncCache.getSharedCache();
|
|
4118
|
+
cache[key] = {
|
|
4119
|
+
result: value,
|
|
4120
|
+
promiseQueue: [],
|
|
3988
4121
|
expires: Date.now() + (ttlMs ?? 1000)
|
|
3989
4122
|
};
|
|
3990
4123
|
}
|
|
@@ -3993,34 +4126,50 @@ class AsyncCache {
|
|
|
3993
4126
|
* @param key The key to remove from the cache.
|
|
3994
4127
|
*/
|
|
3995
4128
|
static remove(key) {
|
|
3996
|
-
|
|
4129
|
+
const cache = AsyncCache.getSharedCache();
|
|
4130
|
+
delete cache[key];
|
|
3997
4131
|
}
|
|
3998
4132
|
/**
|
|
3999
4133
|
* Clear the cache.
|
|
4000
4134
|
* @param prefix Optional prefix to clear only entries with that prefix.
|
|
4001
4135
|
*/
|
|
4002
4136
|
static clearCache(prefix) {
|
|
4137
|
+
const cache = AsyncCache.getSharedCache();
|
|
4003
4138
|
if (Is.stringValue(prefix)) {
|
|
4004
|
-
for (const entry in
|
|
4139
|
+
for (const entry in cache) {
|
|
4005
4140
|
if (entry.startsWith(prefix)) {
|
|
4006
|
-
delete
|
|
4141
|
+
delete cache[entry];
|
|
4007
4142
|
}
|
|
4008
4143
|
}
|
|
4009
4144
|
}
|
|
4010
4145
|
else {
|
|
4011
|
-
|
|
4146
|
+
SharedStore.set("asyncCache", {});
|
|
4012
4147
|
}
|
|
4013
4148
|
}
|
|
4014
4149
|
/**
|
|
4015
4150
|
* Perform a cleanup of the expired entries in the cache.
|
|
4016
4151
|
*/
|
|
4017
4152
|
static cleanupExpired() {
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4153
|
+
const cache = AsyncCache.getSharedCache();
|
|
4154
|
+
for (const entry in cache) {
|
|
4155
|
+
if (cache[entry].expires > 0 && cache[entry].expires < Date.now()) {
|
|
4156
|
+
delete cache[entry];
|
|
4021
4157
|
}
|
|
4022
4158
|
}
|
|
4023
4159
|
}
|
|
4160
|
+
/**
|
|
4161
|
+
* Get the shared cache.
|
|
4162
|
+
* @returns The shared cache.
|
|
4163
|
+
* @internal
|
|
4164
|
+
*/
|
|
4165
|
+
static getSharedCache() {
|
|
4166
|
+
let sharedCache = SharedStore.get("asyncCache");
|
|
4167
|
+
if (Is.undefined(sharedCache)) {
|
|
4168
|
+
sharedCache = {};
|
|
4169
|
+
SharedStore.set("asyncCache", sharedCache);
|
|
4170
|
+
}
|
|
4171
|
+
return sharedCache;
|
|
4172
|
+
}
|
|
4024
4173
|
}
|
|
4025
4174
|
|
|
4026
4175
|
/**
|
|
@@ -4876,6 +5025,7 @@ exports.NotImplementedError = NotImplementedError;
|
|
|
4876
5025
|
exports.NotSupportedError = NotSupportedError;
|
|
4877
5026
|
exports.ObjectHelper = ObjectHelper;
|
|
4878
5027
|
exports.RandomHelper = RandomHelper;
|
|
5028
|
+
exports.SharedStore = SharedStore;
|
|
4879
5029
|
exports.StringHelper = StringHelper;
|
|
4880
5030
|
exports.Uint8ArrayHelper = Uint8ArrayHelper;
|
|
4881
5031
|
exports.UnauthorizedError = UnauthorizedError;
|