@twin.org/core 0.0.4-next.7 → 0.0.4-next.9
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/es/encoding/base64.js +1 -1
- package/dist/es/encoding/base64.js.map +1 -1
- package/dist/es/errors/conflictError.js +1 -1
- package/dist/es/errors/conflictError.js.map +1 -1
- package/dist/es/errors/generalError.js +1 -1
- package/dist/es/errors/generalError.js.map +1 -1
- package/dist/es/errors/notImplementedError.js +1 -1
- package/dist/es/errors/notImplementedError.js.map +1 -1
- package/dist/es/errors/validationError.js +1 -1
- package/dist/es/errors/validationError.js.map +1 -1
- package/dist/es/helpers/arrayHelper.js +1 -1
- package/dist/es/helpers/arrayHelper.js.map +1 -1
- package/dist/es/helpers/errorHelper.js +1 -1
- package/dist/es/helpers/errorHelper.js.map +1 -1
- package/dist/es/helpers/jsonHelper.js +1 -1
- package/dist/es/helpers/jsonHelper.js.map +1 -1
- package/dist/es/helpers/objectHelper.js +2 -2
- package/dist/es/helpers/objectHelper.js.map +1 -1
- package/dist/es/helpers/stringHelper.js +2 -2
- package/dist/es/helpers/stringHelper.js.map +1 -1
- package/dist/es/index.js +4 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IComponent.js.map +1 -1
- package/dist/es/models/ILocale.js.map +1 -1
- package/dist/es/models/ISharedObjectBufferOptions.js +4 -0
- package/dist/es/models/ISharedObjectBufferOptions.js.map +1 -0
- package/dist/es/models/ISharedObjectBufferWorkerMessage.js +2 -0
- package/dist/es/models/ISharedObjectBufferWorkerMessage.js.map +1 -0
- package/dist/es/models/IValidationFailure.js.map +1 -1
- package/dist/es/models/sharedObjectBufferMessageTypes.js +13 -0
- package/dist/es/models/sharedObjectBufferMessageTypes.js.map +1 -0
- package/dist/es/utils/asyncCache.js +1 -1
- package/dist/es/utils/asyncCache.js.map +1 -1
- package/dist/es/utils/coerce.js +10 -20
- package/dist/es/utils/coerce.js.map +1 -1
- package/dist/es/utils/compression.js +1 -1
- package/dist/es/utils/compression.js.map +1 -1
- package/dist/es/utils/is.js +9 -9
- package/dist/es/utils/is.js.map +1 -1
- package/dist/es/utils/sharedObjectBuffer.js +305 -0
- package/dist/es/utils/sharedObjectBuffer.js.map +1 -0
- package/dist/es/utils/sharedStore.js +2 -2
- package/dist/es/utils/sharedStore.js.map +1 -1
- package/dist/types/errors/conflictError.d.ts +1 -1
- package/dist/types/errors/generalError.d.ts +1 -1
- package/dist/types/errors/notImplementedError.d.ts +1 -1
- package/dist/types/errors/validationError.d.ts +1 -1
- package/dist/types/helpers/arrayHelper.d.ts +1 -1
- package/dist/types/helpers/errorHelper.d.ts +1 -1
- package/dist/types/helpers/jsonHelper.d.ts +1 -1
- package/dist/types/helpers/objectHelper.d.ts +2 -2
- package/dist/types/helpers/stringHelper.d.ts +2 -2
- package/dist/types/index.d.ts +4 -0
- package/dist/types/models/IComponent.d.ts +2 -2
- package/dist/types/models/ILocale.d.ts +1 -1
- package/dist/types/models/ISharedObjectBufferOptions.d.ts +17 -0
- package/dist/types/models/ISharedObjectBufferWorkerMessage.d.ts +29 -0
- package/dist/types/models/IValidationFailure.d.ts +1 -1
- package/dist/types/models/sharedObjectBufferMessageTypes.d.ts +13 -0
- package/dist/types/utils/asyncCache.d.ts +1 -1
- package/dist/types/utils/coerce.d.ts +10 -20
- package/dist/types/utils/compression.d.ts +1 -1
- package/dist/types/utils/is.d.ts +9 -9
- package/dist/types/utils/sharedObjectBuffer.d.ts +74 -0
- package/dist/types/utils/sharedStore.d.ts +2 -2
- package/docs/changelog.md +39 -0
- package/docs/reference/classes/ArrayHelper.md +1 -1
- package/docs/reference/classes/AsyncCache.md +1 -1
- package/docs/reference/classes/Coerce.md +10 -50
- package/docs/reference/classes/Compression.md +1 -1
- package/docs/reference/classes/ConflictError.md +1 -1
- package/docs/reference/classes/ErrorHelper.md +1 -1
- package/docs/reference/classes/GeneralError.md +1 -1
- package/docs/reference/classes/Is.md +9 -9
- package/docs/reference/classes/JsonHelper.md +1 -1
- package/docs/reference/classes/NotImplementedError.md +1 -1
- package/docs/reference/classes/ObjectHelper.md +2 -2
- package/docs/reference/classes/SharedObjectBuffer.md +192 -0
- package/docs/reference/classes/SharedStore.md +2 -2
- package/docs/reference/classes/StringHelper.md +16 -13
- package/docs/reference/classes/ValidationError.md +1 -1
- package/docs/reference/index.md +5 -0
- package/docs/reference/interfaces/IComponent.md +2 -2
- package/docs/reference/interfaces/ILocale.md +1 -1
- package/docs/reference/interfaces/ISharedObjectBufferOptions.md +33 -0
- package/docs/reference/interfaces/ISharedObjectBufferWorkerMessage.md +44 -0
- package/docs/reference/interfaces/IValidationFailure.md +1 -1
- package/docs/reference/type-aliases/SharedObjectBufferMessageTypes.md +5 -0
- package/docs/reference/variables/SharedObjectBufferMessageTypes.md +13 -0
- package/locales/en.json +5 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ISharedObjectBufferOptions.js","sourceRoot":"","sources":["../../../src/models/ISharedObjectBufferOptions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Options for configuring buffer capacity when creating a shared object buffer.\n */\nexport interface ISharedObjectBufferOptions {\n\t/**\n\t * Initial payload capacity hint in bytes.\n\t * Only honoured when the buffer does not yet exist; ignored on subsequent writes.\n\t * @default 1 MiB.\n\t */\n\tinitialCapacityBytes?: number;\n\n\t/**\n\t * Maximum allowed payload capacity in bytes. The buffer will never grow beyond this limit.\n\t * Only honoured when the buffer does not yet exist; ignored on subsequent writes.\n\t * @default 256 MiB.\n\t */\n\tmaxCapacityBytes?: number;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ISharedObjectBufferWorkerMessage.js","sourceRoot":"","sources":["../../../src/models/ISharedObjectBufferWorkerMessage.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { MessagePort } from \"node:worker_threads\";\nimport type { ISharedObjectBufferOptions } from \"./ISharedObjectBufferOptions.js\";\nimport type { SharedObjectBufferMessageTypes } from \"./sharedObjectBufferMessageTypes.js\";\n\n/**\n * Message sent from a worker thread to the main thread to request the SharedArrayBuffer for an object.\n */\nexport interface ISharedObjectBufferWorkerMessage {\n\t/**\n\t * The message type discriminant.\n\t */\n\ttype: typeof SharedObjectBufferMessageTypes.GetBuffer;\n\n\t/**\n\t * The object id name that identifies which buffer is being requested.\n\t */\n\tobjectId: string;\n\n\t/**\n\t * One-shot SharedArrayBuffer used for the Atomics.wait/notify handshake so the\n\t * worker can block synchronously until the main thread has posted the response.\n\t */\n\tsignal: SharedArrayBuffer;\n\n\t/**\n\t * MessagePort through which the main thread returns the object buffer.\n\t */\n\tport: MessagePort;\n\n\t/**\n\t * Options for creating or fetching the buffer.\n\t */\n\toptions?: ISharedObjectBufferOptions;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IValidationFailure.js","sourceRoot":"","sources":["../../../src/models/IValidationFailure.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/**\n * Interface describing the reason a validation failed.\n */\nexport interface IValidationFailure {\n\t/**\n\t * The property that failed validation.\n\t */\n\tproperty: string;\n\n\t/**\n\t * The reason the validation failed as an
|
|
1
|
+
{"version":3,"file":"IValidationFailure.js","sourceRoot":"","sources":["../../../src/models/IValidationFailure.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/**\n * Interface describing the reason a validation failed.\n */\nexport interface IValidationFailure {\n\t/**\n\t * The property that failed validation.\n\t */\n\tproperty: string;\n\n\t/**\n\t * The reason the validation failed as an i18n resource key.\n\t */\n\treason: string;\n\n\t/**\n\t * Additional properties for the validation failure.\n\t */\n\tproperties?: { [id: string]: unknown };\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2026 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
/**
|
|
4
|
+
* Message type constants for the SharedObjectBuffer worker-to-main-thread protocol.
|
|
5
|
+
*/
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
7
|
+
export const SharedObjectBufferMessageTypes = {
|
|
8
|
+
/**
|
|
9
|
+
* Worker requests the SharedArrayBuffer for a named object from the main thread.
|
|
10
|
+
*/
|
|
11
|
+
GetBuffer: "twin:sharedObjectBuffer:getBuffer"
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=sharedObjectBufferMessageTypes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sharedObjectBufferMessageTypes.js","sourceRoot":"","sources":["../../../src/models/sharedObjectBufferMessageTypes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;GAEG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC7C;;OAEG;IACH,SAAS,EAAE,mCAAmC;CACrC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Message type constants for the SharedObjectBuffer worker-to-main-thread protocol.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const SharedObjectBufferMessageTypes = {\n\t/**\n\t * Worker requests the SharedArrayBuffer for a named object from the main thread.\n\t */\n\tGetBuffer: \"twin:sharedObjectBuffer:getBuffer\"\n} as const;\n\n/**\n * Union of all SharedObjectBuffer message type strings.\n */\nexport type SharedObjectBufferMessageTypes =\n\t(typeof SharedObjectBufferMessageTypes)[keyof typeof SharedObjectBufferMessageTypes];\n"]}
|
|
@@ -157,7 +157,7 @@ export class AsyncCache {
|
|
|
157
157
|
* @param key The key to set in the cache.
|
|
158
158
|
* @param value The value to set in the cache.
|
|
159
159
|
* @param ttlMs The TTL of the entry in the cache in milliseconds. Defaults to 1000 (1 second).
|
|
160
|
-
* @returns
|
|
160
|
+
* @returns A promise that resolves when the entry has been stored.
|
|
161
161
|
*/
|
|
162
162
|
static async set(key, value, ttlMs) {
|
|
163
163
|
const expires = Date.now() + (ttlMs ?? 1000);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncCache.js","sourceRoot":"","sources":["../../../src/utils/asyncCache.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,UAAU;IACtB;;;OAGG;IACK,MAAM,CAAU,UAAU,GAAG,YAAY,CAAC;IAElD;;;OAGG;IACK,MAAM,CAAU,sBAAsB,GAAG,yBAAyB,CAAC;IAE3E;;;OAGG;IACK,MAAM,CAAU,iBAAiB,GAAG,uBAAuB,CAAC;IAEpE;;;OAGG;IACK,MAAM,CAAU,oBAAoB,GAAG,IAAI,CAAC;IAEpD;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,CAAC,IAAI,CACvB,GAAW,EACX,KAAyB,EACzB,aAA+B,EAC/B,aAAuB;QAEvB,MAAM,YAAY,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;QACpD,IAAI,CAAC,YAAY,EAAE,CAAC;YACnB,8CAA8C;YAC9C,OAAO,aAAa,EAAE,CAAC;QACxB,CAAC;QAED,UAAU,CAAC,cAAc,EAAE,CAAC;QAC5B,8EAA8E;QAC9E,uGAAuG;QACvG,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;QAE/B,MAAM,KAAK,GAAG,UAAU,CAAC,cAAc,EAAK,CAAC;QAC7C,MAAM,WAAW,GAAG,KAA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AsB,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;IAED;;;;;;OAMG;IACK,MAAM,CAAC,cAAc,CAAC,GAAW;QACxC,MAAM,KAAK,GAAG,UAAU,CAAC,cAAc,EAAE,CAAC;QAC1C,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QACzB,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,GAAG,CAAC,IAAI,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;YAC9F,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,IAAI,CAAC;QACb,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,aAAa;QAC3B,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAS,UAAU,CAAC,sBAAsB,CAAC,CAAC;QAC3E,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,MAAM,KAAK,GAAG,UAAU,CAAC,cAAc,EAAE,CAAC;QAC1C,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;QACjC,OAAO,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;IAC5D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { SharedStore } from \"./sharedStore.js\";\n\n/**\n * Cache the results from asynchronous requests.\n */\nexport class AsyncCache {\n\t/**\n\t * Cache key for the shared cache object in the SharedStore.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_KEY = \"asyncCache\";\n\n\t/**\n\t * Cache key for the entry key of the soonest-expiring cache entry, used to optimize cleanup.\n\t * @internal\n\t */\n\tprivate static readonly _NEXT_EXPIRY_CACHE_KEY = \"asyncCacheNextExpiryKey\";\n\n\t/**\n\t * Cache key for the timestamp of the last full cleanup scan.\n\t * @internal\n\t */\n\tprivate static readonly _LAST_CLEANUP_KEY = \"asyncCacheLastCleanup\";\n\n\t/**\n\t * Minimum interval in ms between full cleanup scans.\n\t * @internal\n\t */\n\tprivate static readonly _CLEANUP_INTERVAL_MS = 5000;\n\n\t/**\n\t * Execute an async request and cache the result.\n\t * @param key The key for the entry in the cache.\n\t * @param ttlMs The TTL of the entry in the cache.\n\t * @param requestMethod The method to call if not cached.\n\t * @param cacheFailures Cache failure results, defaults to false.\n\t * @returns The response.\n\t */\n\tpublic static async exec<T = unknown>(\n\t\tkey: string,\n\t\tttlMs: number | undefined,\n\t\trequestMethod: () => Promise<T>,\n\t\tcacheFailures?: boolean\n\t): Promise<T> {\n\t\tconst cacheEnabled = Is.integer(ttlMs) && ttlMs > 0;\n\t\tif (!cacheEnabled) {\n\t\t\t// No caching, just execute the request method\n\t\t\treturn requestMethod();\n\t\t}\n\n\t\tAsyncCache.cleanupExpired();\n\t\t// Cleanup will not necessarily remove the entry for the key we are requesting\n\t\t// as it is throttled, so we also check and evict if expired here to ensure we don't return stale data.\n\t\tAsyncCache.evictIfExpired(key);\n\n\t\tconst cache = AsyncCache.getSharedCache<T>();\n\t\tconst cachedEntry = cache[key];\n\n\t\t// Do we have a cache entry for the key\n\t\tif (cachedEntry) {\n\t\t\tif (!Is.empty(cachedEntry.result)) {\n\t\t\t\t// If the cache has already resulted in a value, resolve it\n\t\t\t\treturn Promise.resolve(cachedEntry.result);\n\t\t\t} else if (!Is.empty(cachedEntry.error)) {\n\t\t\t\t// If the cache has already resulted in an error, reject it\n\t\t\t\treturn Promise.reject(cachedEntry.error);\n\t\t\t}\n\n\t\t\t// Otherwise create a promise to return and store the resolver\n\t\t\t// and rejector in the cache entry, so that we can call then\n\t\t\t// when the request is done\n\n\t\t\tlet storedResolve: ((value: T | PromiseLike<T>) => void) | undefined;\n\t\t\tlet storedReject: ((reason?: unknown) => void) | undefined;\n\t\t\tconst wait = new Promise<T>((resolve, reject) => {\n\t\t\t\tstoredResolve = resolve;\n\t\t\t\tstoredReject = reject;\n\t\t\t});\n\t\t\tif (!Is.empty(storedResolve) && !Is.empty(storedReject)) {\n\t\t\t\tcachedEntry.promiseQueue.push({\n\t\t\t\t\trequestMethod,\n\t\t\t\t\tresolve: storedResolve,\n\t\t\t\t\treject: storedReject\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn wait;\n\t\t}\n\n\t\t// If we don't have a cache entry, create a new one\n\t\tconst expires = Date.now() + ttlMs;\n\t\tconst cacheEntry: {\n\t\t\tresult?: T;\n\t\t\terror?: unknown;\n\t\t\tinProgress?: boolean;\n\t\t\tpromiseQueue: {\n\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t}[];\n\t\t\texpires: number;\n\t\t} = {\n\t\t\tinProgress: true,\n\t\t\tpromiseQueue: [],\n\t\t\texpires\n\t\t};\n\t\tcache[key] = cacheEntry;\n\t\tAsyncCache.updateNextExpiryKey(key, expires);\n\n\t\t// Return a promise that wraps the original request method\n\t\t// so that we can store any results or errors in the cache\n\t\treturn new Promise((resolve, reject) => {\n\t\t\t// Call the request method and store the result\n\t\t\trequestMethod()\n\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then\n\t\t\t\t.then(res => {\n\t\t\t\t\t// If the request was successful, store the result\n\t\t\t\t\tcacheEntry.inProgress = false;\n\t\t\t\t\tcacheEntry.result = res;\n\n\t\t\t\t\t// and resolve both this promise and all the waiters\n\t\t\t\t\tresolve(res);\n\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\twait.resolve(res);\n\t\t\t\t\t}\n\t\t\t\t\tcacheEntry.promiseQueue = [];\n\t\t\t\t\treturn res;\n\t\t\t\t})\n\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then\n\t\t\t\t.catch((err: unknown) => {\n\t\t\t\t\t// Reject the promise\n\t\t\t\t\treject(err);\n\t\t\t\t\tcacheEntry.inProgress = false;\n\n\t\t\t\t\t// Handle the waiters based on the cacheFailures flag\n\t\t\t\t\tif (cacheFailures ?? false) {\n\t\t\t\t\t\t// If we are caching failures, store the error and reject the waiters\n\t\t\t\t\t\tcacheEntry.error = err;\n\t\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\t\twait.reject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Clear the waiters so we don't call them again\n\t\t\t\t\t\tcacheEntry.promiseQueue = [];\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If not caching failures for any queued requests we\n\t\t\t\t\t\t// have no value to either resolve or reject, so we\n\t\t\t\t\t\t// just resolve with the original request method\n\t\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-floating-promises\n\t\t\t\t\t\t\tAsyncCache.resolveWaiter(wait.requestMethod, wait.resolve, wait.reject);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (cache[key] === cacheEntry) {\n\t\t\t\t\t\t\tdelete cache[key];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Get an entry from the cache.\n\t * @param key The key to get from the cache.\n\t * @returns The item from the cache if it exists, or undefined if the key is missing, expired, or\n\t * its request is still in-progress. Throws if a cached failure exists for the key.\n\t */\n\tpublic static async get<T = unknown>(key: string): Promise<T | undefined> {\n\t\tif (AsyncCache.evictIfExpired(key)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst cache = AsyncCache.getSharedCache<T>();\n\t\tconst entry = cache[key];\n\n\t\tif (!Is.empty(entry?.result)) {\n\t\t\t// If the cache has already resulted in a value, resolve it\n\t\t\treturn entry.result;\n\t\t}\n\n\t\tif (!Is.empty(entry?.error)) {\n\t\t\t// If the cache has already resulted in an error, reject it\n\t\t\tthrow entry.error as Error;\n\t\t}\n\t}\n\n\t/**\n\t * Set an entry into the cache.\n\t * @param key The key to set in the cache.\n\t * @param value The value to set in the cache.\n\t * @param ttlMs The TTL of the entry in the cache in milliseconds. Defaults to 1000 (1 second).\n\t * @returns Nothing.\n\t */\n\tpublic static async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n\t\tconst expires = Date.now() + (ttlMs ?? 1000);\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tcache[key] = {\n\t\t\tresult: value,\n\t\t\tpromiseQueue: [],\n\t\t\texpires\n\t\t};\n\t\tAsyncCache.updateNextExpiryKey(key, expires);\n\t}\n\n\t/**\n\t * Remove an entry from the cache.\n\t * @param key The key to remove from the cache.\n\t */\n\tpublic static remove(key: string): void {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tdelete cache[key];\n\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\tif (nextKey === key) {\n\t\t\tAsyncCache.recalculateNextKey();\n\t\t}\n\t}\n\n\t/**\n\t * Clear the cache.\n\t * @param prefix Optional prefix to clear only entries with that prefix.\n\t */\n\tpublic static clearCache(prefix?: string): void {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tif (Is.stringValue(prefix)) {\n\t\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\t\tlet nextKeyRemoved = false;\n\t\t\tfor (const entry in cache) {\n\t\t\t\tif (entry.startsWith(prefix)) {\n\t\t\t\t\tif (entry === nextKey) {\n\t\t\t\t\t\tnextKeyRemoved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdelete cache[entry];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (nextKeyRemoved) {\n\t\t\t\tAsyncCache.recalculateNextKey();\n\t\t\t}\n\t\t} else {\n\t\t\tSharedStore.set(AsyncCache._CACHE_KEY, {});\n\t\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, undefined);\n\t\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, Date.now());\n\t\t}\n\t}\n\n\t/**\n\t * Perform a cleanup of the expired entries in the cache.\n\t */\n\tpublic static cleanupExpired(): void {\n\t\tconst now = Date.now();\n\t\tconst lastCleanup = SharedStore.get<number>(AsyncCache._LAST_CLEANUP_KEY) ?? 0;\n\t\tif (now - lastCleanup < AsyncCache._CLEANUP_INTERVAL_MS) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst expiry = AsyncCache.getNextExpiry();\n\t\tif (expiry !== undefined && expiry > now) {\n\t\t\t// Nothing is expired yet. Stamp the time so the throttle suppresses further checks\n\t\t\t// for the next interval. Per-key freshness is still guaranteed because exec() and\n\t\t\t// get() call evictIfExpired() directly before reading the cache.\n\t\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, now);\n\t\t\treturn;\n\t\t}\n\n\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, now);\n\t\tAsyncCache.recalculateNextKey();\n\t}\n\n\t/**\n\t * Get the shared cache.\n\t * @returns The shared cache.\n\t * @internal\n\t */\n\tprivate static getSharedCache<T = unknown>(): {\n\t\t[url: string]: {\n\t\t\tresult?: T;\n\t\t\terror?: unknown;\n\t\t\tinProgress?: boolean;\n\t\t\tpromiseQueue: {\n\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t}[];\n\t\t\texpires: number;\n\t\t};\n\t} {\n\t\tlet sharedCache = SharedStore.get<{\n\t\t\t[url: string]: {\n\t\t\t\tresult?: T;\n\t\t\t\terror?: unknown;\n\t\t\t\tinProgress?: boolean;\n\t\t\t\tpromiseQueue: {\n\t\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t\t}[];\n\t\t\t\texpires: number;\n\t\t\t};\n\t\t}>(AsyncCache._CACHE_KEY);\n\n\t\tif (Is.undefined(sharedCache)) {\n\t\t\tsharedCache = {};\n\t\t\tSharedStore.set(AsyncCache._CACHE_KEY, sharedCache);\n\t\t}\n\n\t\treturn sharedCache;\n\t}\n\n\t/**\n\t * Resolve a waiter by re-running its request method safely.\n\t * @param requestMethod The method to execute.\n\t * @param resolve The resolver for the waiter.\n\t * @param reject The rejector for the waiter.\n\t * @internal\n\t */\n\tprivate static async resolveWaiter<T>(\n\t\trequestMethod: () => Promise<T>,\n\t\tresolve: (value: T | PromiseLike<T>) => void,\n\t\treject: (reason?: unknown) => void\n\t): Promise<void> {\n\t\ttry {\n\t\t\tresolve(await requestMethod());\n\t\t} catch (waitErr) {\n\t\t\treject(waitErr);\n\t\t}\n\t}\n\n\t/**\n\t * Scan all cache entries, delete any that have expired, and record the key of the\n\t * soonest-expiring remaining entry.\n\t * @internal\n\t */\n\tprivate static recalculateNextKey(): void {\n\t\tconst now = Date.now();\n\t\tconst cache = AsyncCache.getSharedCache();\n\n\t\tif (Object.keys(cache).length === 0) {\n\t\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, undefined);\n\t\t\treturn;\n\t\t}\n\n\t\tlet newNextKey: string | undefined;\n\t\tlet newNextExpiry = Number.MAX_SAFE_INTEGER;\n\t\tfor (const entryKey in cache) {\n\t\t\tconst { expires, inProgress } = cache[entryKey];\n\t\t\tif (expires > 0 && expires < now && inProgress !== true) {\n\t\t\t\tdelete cache[entryKey];\n\t\t\t} else if (expires > 0 && expires < newNextExpiry) {\n\t\t\t\tnewNextExpiry = expires;\n\t\t\t\tnewNextKey = entryKey;\n\t\t\t}\n\t\t}\n\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, newNextKey);\n\t}\n\n\t/**\n\t * Update the tracked next-expiry entry key if the given entry expires sooner than the current one.\n\t * @param key The cache entry key.\n\t * @param expires The expiry timestamp of the entry.\n\t * @internal\n\t */\n\tprivate static updateNextExpiryKey(key: string, expires: number): void {\n\t\tconst expiry = AsyncCache.getNextExpiry();\n\t\tif (expiry !== undefined && expiry <= expires) {\n\t\t\treturn;\n\t\t}\n\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, key);\n\t}\n\n\t/**\n\t * Deletes the given key from the cache if it has expired and is not in-progress.\n\t * Returns true if the entry was evicted.\n\t * @param key The cache entry key to check.\n\t * @returns True if the expired entry was removed; otherwise, false.\n\t * @internal\n\t */\n\tprivate static evictIfExpired(key: string): boolean {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tconst entry = cache[key];\n\t\tif (!Is.empty(entry) && entry.expires > 0 && entry.expires < Date.now() && !entry.inProgress) {\n\t\t\tdelete cache[key];\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Returns the expiry timestamp of the tracked next-expiry entry, or undefined if there\n\t * is no tracked entry or it is no longer present in the cache.\n\t * @returns The next tracked expiry timestamp, if one is still available.\n\t * @internal\n\t */\n\tprivate static getNextExpiry(): number | undefined {\n\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\tif (Is.empty(nextKey)) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tconst nextEntry = cache[nextKey];\n\t\treturn Is.empty(nextEntry) ? undefined : nextEntry.expires;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"asyncCache.js","sourceRoot":"","sources":["../../../src/utils/asyncCache.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,UAAU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;;;;;OAKG;IACK,MAAM,CAAC,aAAa;QAC3B,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAS,UAAU,CAAC,sBAAsB,CAAC,CAAC;QAC3E,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,MAAM,KAAK,GAAG,UAAU,CAAC,cAAc,EAAE,CAAC;QAC1C,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;QACjC,OAAO,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;IAC5D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { SharedStore } from \"./sharedStore.js\";\n\n/**\n * Cache the results from asynchronous requests.\n */\nexport class AsyncCache {\n\t/**\n\t * Cache key for the shared cache object in the SharedStore.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_KEY = \"asyncCache\";\n\n\t/**\n\t * Cache key for the entry key of the soonest-expiring cache entry, used to optimize cleanup.\n\t * @internal\n\t */\n\tprivate static readonly _NEXT_EXPIRY_CACHE_KEY = \"asyncCacheNextExpiryKey\";\n\n\t/**\n\t * Cache key for the timestamp of the last full cleanup scan.\n\t * @internal\n\t */\n\tprivate static readonly _LAST_CLEANUP_KEY = \"asyncCacheLastCleanup\";\n\n\t/**\n\t * Minimum interval in ms between full cleanup scans.\n\t * @internal\n\t */\n\tprivate static readonly _CLEANUP_INTERVAL_MS = 5000;\n\n\t/**\n\t * Execute an async request and cache the result.\n\t * @param key The key for the entry in the cache.\n\t * @param ttlMs The TTL of the entry in the cache.\n\t * @param requestMethod The method to call if not cached.\n\t * @param cacheFailures Cache failure results, defaults to false.\n\t * @returns The response.\n\t */\n\tpublic static async exec<T = unknown>(\n\t\tkey: string,\n\t\tttlMs: number | undefined,\n\t\trequestMethod: () => Promise<T>,\n\t\tcacheFailures?: boolean\n\t): Promise<T> {\n\t\tconst cacheEnabled = Is.integer(ttlMs) && ttlMs > 0;\n\t\tif (!cacheEnabled) {\n\t\t\t// No caching, just execute the request method\n\t\t\treturn requestMethod();\n\t\t}\n\n\t\tAsyncCache.cleanupExpired();\n\t\t// Cleanup will not necessarily remove the entry for the key we are requesting\n\t\t// as it is throttled, so we also check and evict if expired here to ensure we don't return stale data.\n\t\tAsyncCache.evictIfExpired(key);\n\n\t\tconst cache = AsyncCache.getSharedCache<T>();\n\t\tconst cachedEntry = cache[key];\n\n\t\t// Do we have a cache entry for the key\n\t\tif (cachedEntry) {\n\t\t\tif (!Is.empty(cachedEntry.result)) {\n\t\t\t\t// If the cache has already resulted in a value, resolve it\n\t\t\t\treturn Promise.resolve(cachedEntry.result);\n\t\t\t} else if (!Is.empty(cachedEntry.error)) {\n\t\t\t\t// If the cache has already resulted in an error, reject it\n\t\t\t\treturn Promise.reject(cachedEntry.error);\n\t\t\t}\n\n\t\t\t// Otherwise create a promise to return and store the resolver\n\t\t\t// and rejector in the cache entry, so that we can call then\n\t\t\t// when the request is done\n\n\t\t\tlet storedResolve: ((value: T | PromiseLike<T>) => void) | undefined;\n\t\t\tlet storedReject: ((reason?: unknown) => void) | undefined;\n\t\t\tconst wait = new Promise<T>((resolve, reject) => {\n\t\t\t\tstoredResolve = resolve;\n\t\t\t\tstoredReject = reject;\n\t\t\t});\n\t\t\tif (!Is.empty(storedResolve) && !Is.empty(storedReject)) {\n\t\t\t\tcachedEntry.promiseQueue.push({\n\t\t\t\t\trequestMethod,\n\t\t\t\t\tresolve: storedResolve,\n\t\t\t\t\treject: storedReject\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn wait;\n\t\t}\n\n\t\t// If we don't have a cache entry, create a new one\n\t\tconst expires = Date.now() + ttlMs;\n\t\tconst cacheEntry: {\n\t\t\tresult?: T;\n\t\t\terror?: unknown;\n\t\t\tinProgress?: boolean;\n\t\t\tpromiseQueue: {\n\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t}[];\n\t\t\texpires: number;\n\t\t} = {\n\t\t\tinProgress: true,\n\t\t\tpromiseQueue: [],\n\t\t\texpires\n\t\t};\n\t\tcache[key] = cacheEntry;\n\t\tAsyncCache.updateNextExpiryKey(key, expires);\n\n\t\t// Return a promise that wraps the original request method\n\t\t// so that we can store any results or errors in the cache\n\t\treturn new Promise((resolve, reject) => {\n\t\t\t// Call the request method and store the result\n\t\t\trequestMethod()\n\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then\n\t\t\t\t.then(res => {\n\t\t\t\t\t// If the request was successful, store the result\n\t\t\t\t\tcacheEntry.inProgress = false;\n\t\t\t\t\tcacheEntry.result = res;\n\n\t\t\t\t\t// and resolve both this promise and all the waiters\n\t\t\t\t\tresolve(res);\n\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\twait.resolve(res);\n\t\t\t\t\t}\n\t\t\t\t\tcacheEntry.promiseQueue = [];\n\t\t\t\t\treturn res;\n\t\t\t\t})\n\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then\n\t\t\t\t.catch((err: unknown) => {\n\t\t\t\t\t// Reject the promise\n\t\t\t\t\treject(err);\n\t\t\t\t\tcacheEntry.inProgress = false;\n\n\t\t\t\t\t// Handle the waiters based on the cacheFailures flag\n\t\t\t\t\tif (cacheFailures ?? false) {\n\t\t\t\t\t\t// If we are caching failures, store the error and reject the waiters\n\t\t\t\t\t\tcacheEntry.error = err;\n\t\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\t\twait.reject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Clear the waiters so we don't call them again\n\t\t\t\t\t\tcacheEntry.promiseQueue = [];\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If not caching failures for any queued requests we\n\t\t\t\t\t\t// have no value to either resolve or reject, so we\n\t\t\t\t\t\t// just resolve with the original request method\n\t\t\t\t\t\tfor (const wait of cacheEntry.promiseQueue) {\n\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-floating-promises\n\t\t\t\t\t\t\tAsyncCache.resolveWaiter(wait.requestMethod, wait.resolve, wait.reject);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (cache[key] === cacheEntry) {\n\t\t\t\t\t\t\tdelete cache[key];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Get an entry from the cache.\n\t * @param key The key to get from the cache.\n\t * @returns The item from the cache if it exists, or undefined if the key is missing, expired, or\n\t * its request is still in-progress. Throws if a cached failure exists for the key.\n\t */\n\tpublic static async get<T = unknown>(key: string): Promise<T | undefined> {\n\t\tif (AsyncCache.evictIfExpired(key)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst cache = AsyncCache.getSharedCache<T>();\n\t\tconst entry = cache[key];\n\n\t\tif (!Is.empty(entry?.result)) {\n\t\t\t// If the cache has already resulted in a value, resolve it\n\t\t\treturn entry.result;\n\t\t}\n\n\t\tif (!Is.empty(entry?.error)) {\n\t\t\t// If the cache has already resulted in an error, reject it\n\t\t\tthrow entry.error as Error;\n\t\t}\n\t}\n\n\t/**\n\t * Set an entry into the cache.\n\t * @param key The key to set in the cache.\n\t * @param value The value to set in the cache.\n\t * @param ttlMs The TTL of the entry in the cache in milliseconds. Defaults to 1000 (1 second).\n\t * @returns A promise that resolves when the entry has been stored.\n\t */\n\tpublic static async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n\t\tconst expires = Date.now() + (ttlMs ?? 1000);\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tcache[key] = {\n\t\t\tresult: value,\n\t\t\tpromiseQueue: [],\n\t\t\texpires\n\t\t};\n\t\tAsyncCache.updateNextExpiryKey(key, expires);\n\t}\n\n\t/**\n\t * Remove an entry from the cache.\n\t * @param key The key to remove from the cache.\n\t */\n\tpublic static remove(key: string): void {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tdelete cache[key];\n\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\tif (nextKey === key) {\n\t\t\tAsyncCache.recalculateNextKey();\n\t\t}\n\t}\n\n\t/**\n\t * Clear the cache.\n\t * @param prefix Optional prefix to clear only entries with that prefix.\n\t */\n\tpublic static clearCache(prefix?: string): void {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tif (Is.stringValue(prefix)) {\n\t\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\t\tlet nextKeyRemoved = false;\n\t\t\tfor (const entry in cache) {\n\t\t\t\tif (entry.startsWith(prefix)) {\n\t\t\t\t\tif (entry === nextKey) {\n\t\t\t\t\t\tnextKeyRemoved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdelete cache[entry];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (nextKeyRemoved) {\n\t\t\t\tAsyncCache.recalculateNextKey();\n\t\t\t}\n\t\t} else {\n\t\t\tSharedStore.set(AsyncCache._CACHE_KEY, {});\n\t\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, undefined);\n\t\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, Date.now());\n\t\t}\n\t}\n\n\t/**\n\t * Perform a cleanup of the expired entries in the cache.\n\t */\n\tpublic static cleanupExpired(): void {\n\t\tconst now = Date.now();\n\t\tconst lastCleanup = SharedStore.get<number>(AsyncCache._LAST_CLEANUP_KEY) ?? 0;\n\t\tif (now - lastCleanup < AsyncCache._CLEANUP_INTERVAL_MS) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst expiry = AsyncCache.getNextExpiry();\n\t\tif (expiry !== undefined && expiry > now) {\n\t\t\t// Nothing is expired yet. Stamp the time so the throttle suppresses further checks\n\t\t\t// for the next interval. Per-key freshness is still guaranteed because exec() and\n\t\t\t// get() call evictIfExpired() directly before reading the cache.\n\t\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, now);\n\t\t\treturn;\n\t\t}\n\n\t\tSharedStore.set(AsyncCache._LAST_CLEANUP_KEY, now);\n\t\tAsyncCache.recalculateNextKey();\n\t}\n\n\t/**\n\t * Get the shared cache.\n\t * @returns The shared cache.\n\t * @internal\n\t */\n\tprivate static getSharedCache<T = unknown>(): {\n\t\t[url: string]: {\n\t\t\tresult?: T;\n\t\t\terror?: unknown;\n\t\t\tinProgress?: boolean;\n\t\t\tpromiseQueue: {\n\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t}[];\n\t\t\texpires: number;\n\t\t};\n\t} {\n\t\tlet sharedCache = SharedStore.get<{\n\t\t\t[url: string]: {\n\t\t\t\tresult?: T;\n\t\t\t\terror?: unknown;\n\t\t\t\tinProgress?: boolean;\n\t\t\t\tpromiseQueue: {\n\t\t\t\t\trequestMethod: () => Promise<T>;\n\t\t\t\t\tresolve: (value: T | PromiseLike<T>) => void;\n\t\t\t\t\treject: (reason?: unknown) => void;\n\t\t\t\t}[];\n\t\t\t\texpires: number;\n\t\t\t};\n\t\t}>(AsyncCache._CACHE_KEY);\n\n\t\tif (Is.undefined(sharedCache)) {\n\t\t\tsharedCache = {};\n\t\t\tSharedStore.set(AsyncCache._CACHE_KEY, sharedCache);\n\t\t}\n\n\t\treturn sharedCache;\n\t}\n\n\t/**\n\t * Resolve a waiter by re-running its request method safely.\n\t * @param requestMethod The method to execute.\n\t * @param resolve The resolver for the waiter.\n\t * @param reject The rejector for the waiter.\n\t * @internal\n\t */\n\tprivate static async resolveWaiter<T>(\n\t\trequestMethod: () => Promise<T>,\n\t\tresolve: (value: T | PromiseLike<T>) => void,\n\t\treject: (reason?: unknown) => void\n\t): Promise<void> {\n\t\ttry {\n\t\t\tresolve(await requestMethod());\n\t\t} catch (waitErr) {\n\t\t\treject(waitErr);\n\t\t}\n\t}\n\n\t/**\n\t * Scan all cache entries, delete any that have expired, and record the key of the\n\t * soonest-expiring remaining entry.\n\t * @internal\n\t */\n\tprivate static recalculateNextKey(): void {\n\t\tconst now = Date.now();\n\t\tconst cache = AsyncCache.getSharedCache();\n\n\t\tif (Object.keys(cache).length === 0) {\n\t\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, undefined);\n\t\t\treturn;\n\t\t}\n\n\t\tlet newNextKey: string | undefined;\n\t\tlet newNextExpiry = Number.MAX_SAFE_INTEGER;\n\t\tfor (const entryKey in cache) {\n\t\t\tconst { expires, inProgress } = cache[entryKey];\n\t\t\tif (expires > 0 && expires < now && inProgress !== true) {\n\t\t\t\tdelete cache[entryKey];\n\t\t\t} else if (expires > 0 && expires < newNextExpiry) {\n\t\t\t\tnewNextExpiry = expires;\n\t\t\t\tnewNextKey = entryKey;\n\t\t\t}\n\t\t}\n\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, newNextKey);\n\t}\n\n\t/**\n\t * Update the tracked next-expiry entry key if the given entry expires sooner than the current one.\n\t * @param key The cache entry key.\n\t * @param expires The expiry timestamp of the entry.\n\t * @internal\n\t */\n\tprivate static updateNextExpiryKey(key: string, expires: number): void {\n\t\tconst expiry = AsyncCache.getNextExpiry();\n\t\tif (expiry !== undefined && expiry <= expires) {\n\t\t\treturn;\n\t\t}\n\t\tSharedStore.set(AsyncCache._NEXT_EXPIRY_CACHE_KEY, key);\n\t}\n\n\t/**\n\t * Deletes the given key from the cache if it has expired and is not in-progress.\n\t * Returns true if the entry was evicted.\n\t * @param key The cache entry key to check.\n\t * @returns True if the expired entry was removed; otherwise, false.\n\t * @internal\n\t */\n\tprivate static evictIfExpired(key: string): boolean {\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tconst entry = cache[key];\n\t\tif (!Is.empty(entry) && entry.expires > 0 && entry.expires < Date.now() && !entry.inProgress) {\n\t\t\tdelete cache[key];\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Returns the expiry timestamp of the tracked next-expiry entry, or undefined if there\n\t * is no tracked entry or it is no longer present in the cache.\n\t * @returns The next tracked expiry timestamp, if one is still available.\n\t * @internal\n\t */\n\tprivate static getNextExpiry(): number | undefined {\n\t\tconst nextKey = SharedStore.get<string>(AsyncCache._NEXT_EXPIRY_CACHE_KEY);\n\t\tif (Is.empty(nextKey)) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst cache = AsyncCache.getSharedCache();\n\t\tconst nextEntry = cache[nextKey];\n\t\treturn Is.empty(nextEntry) ? undefined : nextEntry.expires;\n\t}\n}\n"]}
|
package/dist/es/utils/coerce.js
CHANGED
|
@@ -10,8 +10,7 @@ export class Coerce {
|
|
|
10
10
|
/**
|
|
11
11
|
* Coerce the value to a string.
|
|
12
12
|
* @param value The value to coerce.
|
|
13
|
-
* @
|
|
14
|
-
* @returns The value if it can be coerced.
|
|
13
|
+
* @returns The coerced string, or undefined if the value cannot be coerced.
|
|
15
14
|
*/
|
|
16
15
|
static string(value) {
|
|
17
16
|
if (Is.undefined(value)) {
|
|
@@ -33,8 +32,7 @@ export class Coerce {
|
|
|
33
32
|
/**
|
|
34
33
|
* Coerce the value to a number.
|
|
35
34
|
* @param value The value to coerce.
|
|
36
|
-
* @
|
|
37
|
-
* @returns The value if it can be coerced.
|
|
35
|
+
* @returns The coerced number, or undefined if the value cannot be coerced.
|
|
38
36
|
*/
|
|
39
37
|
static number(value) {
|
|
40
38
|
if (Is.undefined(value)) {
|
|
@@ -59,8 +57,7 @@ export class Coerce {
|
|
|
59
57
|
/**
|
|
60
58
|
* Coerce the value to an integer.
|
|
61
59
|
* @param value The value to coerce.
|
|
62
|
-
* @
|
|
63
|
-
* @returns The value if it can be coerced.
|
|
60
|
+
* @returns The coerced integer, or undefined if the value cannot be coerced.
|
|
64
61
|
*/
|
|
65
62
|
static integer(value) {
|
|
66
63
|
const num = Coerce.number(value);
|
|
@@ -71,8 +68,7 @@ export class Coerce {
|
|
|
71
68
|
/**
|
|
72
69
|
* Coerce the value to a bigint.
|
|
73
70
|
* @param value The value to coerce.
|
|
74
|
-
* @
|
|
75
|
-
* @returns The value if it can be coerced.
|
|
71
|
+
* @returns The coerced bigint, or undefined if the value cannot be coerced.
|
|
76
72
|
*/
|
|
77
73
|
static bigint(value) {
|
|
78
74
|
if (Is.undefined(value)) {
|
|
@@ -97,8 +93,7 @@ export class Coerce {
|
|
|
97
93
|
/**
|
|
98
94
|
* Coerce the value to a boolean.
|
|
99
95
|
* @param value The value to coerce.
|
|
100
|
-
* @
|
|
101
|
-
* @returns The value if it can be coerced.
|
|
96
|
+
* @returns The coerced boolean, or undefined if the value cannot be coerced.
|
|
102
97
|
*/
|
|
103
98
|
static boolean(value) {
|
|
104
99
|
if (Is.undefined(value)) {
|
|
@@ -123,8 +118,7 @@ export class Coerce {
|
|
|
123
118
|
/**
|
|
124
119
|
* Coerce the value to a date.
|
|
125
120
|
* @param value The value to coerce.
|
|
126
|
-
* @
|
|
127
|
-
* @returns The value if it can be coerced.
|
|
121
|
+
* @returns The coerced date, or undefined if the value cannot be coerced.
|
|
128
122
|
*/
|
|
129
123
|
static date(value) {
|
|
130
124
|
if (Is.undefined(value)) {
|
|
@@ -147,8 +141,7 @@ export class Coerce {
|
|
|
147
141
|
/**
|
|
148
142
|
* Coerce the value to a date/time.
|
|
149
143
|
* @param value The value to coerce.
|
|
150
|
-
* @
|
|
151
|
-
* @returns The value if it can be coerced.
|
|
144
|
+
* @returns The coerced date/time, or undefined if the value cannot be coerced.
|
|
152
145
|
*/
|
|
153
146
|
static dateTime(value) {
|
|
154
147
|
if (Is.undefined(value)) {
|
|
@@ -171,8 +164,7 @@ export class Coerce {
|
|
|
171
164
|
/**
|
|
172
165
|
* Coerce the value to a time.
|
|
173
166
|
* @param value The value to coerce.
|
|
174
|
-
* @
|
|
175
|
-
* @returns The value if it can be coerced.
|
|
167
|
+
* @returns The coerced time, or undefined if the value cannot be coerced.
|
|
176
168
|
*/
|
|
177
169
|
static time(value) {
|
|
178
170
|
if (Is.undefined(value)) {
|
|
@@ -197,8 +189,7 @@ export class Coerce {
|
|
|
197
189
|
/**
|
|
198
190
|
* Coerce the value to an object.
|
|
199
191
|
* @param value The value to coerce.
|
|
200
|
-
* @
|
|
201
|
-
* @returns The value if it can be coerced.
|
|
192
|
+
* @returns The coerced object, or undefined if the value cannot be coerced.
|
|
202
193
|
*/
|
|
203
194
|
static object(value) {
|
|
204
195
|
if (Is.undefined(value)) {
|
|
@@ -217,8 +208,7 @@ export class Coerce {
|
|
|
217
208
|
/**
|
|
218
209
|
* Coerce the value to a Uint8Array.
|
|
219
210
|
* @param value The value to coerce.
|
|
220
|
-
* @
|
|
221
|
-
* @returns The value if it can be coerced.
|
|
211
|
+
* @returns The coerced Uint8Array, or undefined if the value cannot be coerced.
|
|
222
212
|
*/
|
|
223
213
|
static uint8Array(value) {
|
|
224
214
|
if (Is.undefined(value)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coerce.js","sourceRoot":"","sources":["../../../src/utils/coerce.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc;QAClC,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAC;Q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AC,UAAU,CAAC,KAAK,CAAC,CAAC;YACjC;gBACC,OAAO,KAAK,CAAC;QACf,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter } from \"./converter.js\";\nimport { Is } from \"./is.js\";\nimport { CoerceType } from \"../models/coerceType.js\";\n\n/**\n * Coerce an object from one type to another.\n */\nexport class Coerce {\n\t/**\n\t * Coerce the value to a string.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static string(value: unknown): string | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn value.toString();\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? \"true\" : \"false\";\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value.toISOString();\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a number.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static number(value: unknown): number | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst parsed = Number.parseFloat(value);\n\t\t\tif (Is.number(parsed)) {\n\t\t\t\treturn parsed;\n\t\t\t}\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? 1 : 0;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value.getTime();\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to an integer.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static integer(value: unknown): number | undefined {\n\t\tconst num = Coerce.number(value);\n\t\tif (!Is.undefined(num)) {\n\t\t\treturn Math.trunc(num);\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a bigint.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static bigint(value: unknown): bigint | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.bigint(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn BigInt(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst parsed = Number.parseFloat(value);\n\t\t\tif (Is.integer(parsed)) {\n\t\t\t\treturn BigInt(parsed);\n\t\t\t}\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? 1n : 0n;\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a boolean.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static boolean(value: unknown): boolean | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\t// eslint-disable-next-line no-unneeded-ternary\n\t\t\treturn value ? true : false;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tif (/true/i.test(value)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (/false/i.test(value)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a date.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static date(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn new Date(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate());\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a date/time.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static dateTime(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn new Date(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(\n\t\t\t\t\tdt.getUTCFullYear(),\n\t\t\t\t\tdt.getUTCMonth(),\n\t\t\t\t\tdt.getUTCDate(),\n\t\t\t\t\tdt.getUTCHours(),\n\t\t\t\t\tdt.getUTCMinutes(),\n\t\t\t\t\tdt.getUTCSeconds(),\n\t\t\t\t\tdt.getUTCMilliseconds()\n\t\t\t\t);\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a time.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static time(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tdt.setFullYear(1970, 0, 1);\n\t\t\treturn dt;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(\n\t\t\t\t\t1970,\n\t\t\t\t\t0,\n\t\t\t\t\t1,\n\t\t\t\t\tdt.getUTCHours(),\n\t\t\t\t\tdt.getUTCMinutes(),\n\t\t\t\t\tdt.getUTCSeconds(),\n\t\t\t\t\tdt.getUTCMilliseconds()\n\t\t\t\t);\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to an object.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static object<T = unknown>(value: unknown): T | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.object<T>(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.stringValue(value)) {\n\t\t\ttry {\n\t\t\t\treturn JSON.parse(value) as T;\n\t\t\t} catch {}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a Uint8Array.\n\t * @param value The value to coerce.\n\t * @throws TypeError If the value can not be coerced.\n\t * @returns The value if it can be coerced.\n\t */\n\tpublic static uint8Array(value: unknown): Uint8Array | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tif (Is.stringHex(value.toLowerCase(), true)) {\n\t\t\t\treturn Converter.hexToBytes(value.toLowerCase());\n\t\t\t}\n\t\t\tif (Is.stringBase64(value)) {\n\t\t\t\treturn Converter.base64ToBytes(value);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerces a value based on the coercion type.\n\t * @param value The value to coerce.\n\t * @param type The coercion type to perform.\n\t * @returns The coerced value.\n\t */\n\tpublic static byType(value: unknown, type?: CoerceType): unknown {\n\t\tswitch (type) {\n\t\t\tcase CoerceType.String:\n\t\t\t\treturn Coerce.string(value);\n\t\t\tcase CoerceType.Number:\n\t\t\t\treturn Coerce.number(value);\n\t\t\tcase CoerceType.Integer:\n\t\t\t\treturn Coerce.integer(value);\n\t\t\tcase CoerceType.BigInt:\n\t\t\t\treturn Coerce.bigint(value);\n\t\t\tcase CoerceType.Boolean:\n\t\t\t\treturn Coerce.boolean(value);\n\t\t\tcase CoerceType.Date:\n\t\t\t\treturn Coerce.date(value);\n\t\t\tcase CoerceType.DateTime:\n\t\t\t\treturn Coerce.dateTime(value);\n\t\t\tcase CoerceType.Time:\n\t\t\t\treturn Coerce.time(value);\n\t\t\tcase CoerceType.Object:\n\t\t\t\treturn Coerce.object(value);\n\t\t\tcase CoerceType.Uint8Array:\n\t\t\t\treturn Coerce.uint8Array(value);\n\t\t\tdefault:\n\t\t\t\treturn value;\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"coerce.js","sourceRoot":"","sources":["../../../src/utils/coerce.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc;QAClC,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;QACD,IAAI,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;QACjC,CAAC;Q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AC;QACF,CAAC;IACF,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc,EAAE,IAAiB;QACrD,QAAQ,IAAI,EAAE,CAAC;YACd,KAAK,UAAU,CAAC,MAAM;gBACrB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,UAAU,CAAC,MAAM;gBACrB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,UAAU,CAAC,OAAO;gBACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC9B,KAAK,UAAU,CAAC,MAAM;gBACrB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,UAAU,CAAC,OAAO;gBACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC9B,KAAK,UAAU,CAAC,IAAI;gBACnB,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC3B,KAAK,UAAU,CAAC,QAAQ;gBACvB,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YAC/B,KAAK,UAAU,CAAC,IAAI;gBACnB,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC3B,KAAK,UAAU,CAAC,MAAM;gBACrB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,UAAU,CAAC,UAAU;gBACzB,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;YACjC;gBACC,OAAO,KAAK,CAAC;QACf,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter } from \"./converter.js\";\nimport { Is } from \"./is.js\";\nimport { CoerceType } from \"../models/coerceType.js\";\n\n/**\n * Coerce an object from one type to another.\n */\nexport class Coerce {\n\t/**\n\t * Coerce the value to a string.\n\t * @param value The value to coerce.\n\t * @returns The coerced string, or undefined if the value cannot be coerced.\n\t */\n\tpublic static string(value: unknown): string | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn value.toString();\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? \"true\" : \"false\";\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value.toISOString();\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a number.\n\t * @param value The value to coerce.\n\t * @returns The coerced number, or undefined if the value cannot be coerced.\n\t */\n\tpublic static number(value: unknown): number | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst parsed = Number.parseFloat(value);\n\t\t\tif (Is.number(parsed)) {\n\t\t\t\treturn parsed;\n\t\t\t}\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? 1 : 0;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value.getTime();\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to an integer.\n\t * @param value The value to coerce.\n\t * @returns The coerced integer, or undefined if the value cannot be coerced.\n\t */\n\tpublic static integer(value: unknown): number | undefined {\n\t\tconst num = Coerce.number(value);\n\t\tif (!Is.undefined(num)) {\n\t\t\treturn Math.trunc(num);\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a bigint.\n\t * @param value The value to coerce.\n\t * @returns The coerced bigint, or undefined if the value cannot be coerced.\n\t */\n\tpublic static bigint(value: unknown): bigint | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.bigint(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn BigInt(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst parsed = Number.parseFloat(value);\n\t\t\tif (Is.integer(parsed)) {\n\t\t\t\treturn BigInt(parsed);\n\t\t\t}\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value ? 1n : 0n;\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a boolean.\n\t * @param value The value to coerce.\n\t * @returns The coerced boolean, or undefined if the value cannot be coerced.\n\t */\n\tpublic static boolean(value: unknown): boolean | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.boolean(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\t// eslint-disable-next-line no-unneeded-ternary\n\t\t\treturn value ? true : false;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tif (/true/i.test(value)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (/false/i.test(value)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a date.\n\t * @param value The value to coerce.\n\t * @returns The coerced date, or undefined if the value cannot be coerced.\n\t */\n\tpublic static date(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn new Date(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate());\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a date/time.\n\t * @param value The value to coerce.\n\t * @returns The coerced date/time, or undefined if the value cannot be coerced.\n\t */\n\tpublic static dateTime(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\treturn new Date(value);\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(\n\t\t\t\t\tdt.getUTCFullYear(),\n\t\t\t\t\tdt.getUTCMonth(),\n\t\t\t\t\tdt.getUTCDate(),\n\t\t\t\t\tdt.getUTCHours(),\n\t\t\t\t\tdt.getUTCMinutes(),\n\t\t\t\t\tdt.getUTCSeconds(),\n\t\t\t\t\tdt.getUTCMilliseconds()\n\t\t\t\t);\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a time.\n\t * @param value The value to coerce.\n\t * @returns The coerced time, or undefined if the value cannot be coerced.\n\t */\n\tpublic static time(value: unknown): Date | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.date(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.number(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tdt.setFullYear(1970, 0, 1);\n\t\t\treturn dt;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tconst dt = new Date(value);\n\t\t\tif (!Number.isNaN(dt.getTime())) {\n\t\t\t\tconst utc = Date.UTC(\n\t\t\t\t\t1970,\n\t\t\t\t\t0,\n\t\t\t\t\t1,\n\t\t\t\t\tdt.getUTCHours(),\n\t\t\t\t\tdt.getUTCMinutes(),\n\t\t\t\t\tdt.getUTCSeconds(),\n\t\t\t\t\tdt.getUTCMilliseconds()\n\t\t\t\t);\n\t\t\t\treturn new Date(utc);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to an object.\n\t * @param value The value to coerce.\n\t * @returns The coerced object, or undefined if the value cannot be coerced.\n\t */\n\tpublic static object<T = unknown>(value: unknown): T | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.object<T>(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.stringValue(value)) {\n\t\t\ttry {\n\t\t\t\treturn JSON.parse(value) as T;\n\t\t\t} catch {}\n\t\t}\n\t}\n\n\t/**\n\t * Coerce the value to a Uint8Array.\n\t * @param value The value to coerce.\n\t * @returns The coerced Uint8Array, or undefined if the value cannot be coerced.\n\t */\n\tpublic static uint8Array(value: unknown): Uint8Array | undefined {\n\t\tif (Is.undefined(value)) {\n\t\t\treturn value;\n\t\t}\n\t\tif (Is.string(value)) {\n\t\t\tif (Is.stringHex(value.toLowerCase(), true)) {\n\t\t\t\treturn Converter.hexToBytes(value.toLowerCase());\n\t\t\t}\n\t\t\tif (Is.stringBase64(value)) {\n\t\t\t\treturn Converter.base64ToBytes(value);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Coerces a value based on the coercion type.\n\t * @param value The value to coerce.\n\t * @param type The coercion type to perform.\n\t * @returns The coerced value.\n\t */\n\tpublic static byType(value: unknown, type?: CoerceType): unknown {\n\t\tswitch (type) {\n\t\t\tcase CoerceType.String:\n\t\t\t\treturn Coerce.string(value);\n\t\t\tcase CoerceType.Number:\n\t\t\t\treturn Coerce.number(value);\n\t\t\tcase CoerceType.Integer:\n\t\t\t\treturn Coerce.integer(value);\n\t\t\tcase CoerceType.BigInt:\n\t\t\t\treturn Coerce.bigint(value);\n\t\t\tcase CoerceType.Boolean:\n\t\t\t\treturn Coerce.boolean(value);\n\t\t\tcase CoerceType.Date:\n\t\t\t\treturn Coerce.date(value);\n\t\t\tcase CoerceType.DateTime:\n\t\t\t\treturn Coerce.dateTime(value);\n\t\t\tcase CoerceType.Time:\n\t\t\t\treturn Coerce.time(value);\n\t\t\tcase CoerceType.Object:\n\t\t\t\treturn Coerce.object(value);\n\t\t\tcase CoerceType.Uint8Array:\n\t\t\t\treturn Coerce.uint8Array(value);\n\t\t\tdefault:\n\t\t\t\treturn value;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -9,7 +9,7 @@ export class Compression {
|
|
|
9
9
|
*/
|
|
10
10
|
static CLASS_NAME = "Compression";
|
|
11
11
|
/**
|
|
12
|
-
* Compress bytes using
|
|
12
|
+
* Compress bytes using the specified compression type.
|
|
13
13
|
* @param bytes The bytes to compress.
|
|
14
14
|
* @param type The type of compression to use.
|
|
15
15
|
* @returns The compressed bytes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compression.js","sourceRoot":"","sources":["../../../src/utils/compression.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;OAEG;IACI,MAAM,CAAU,UAAU,iBAAiC;IAElE;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAiB,EAAE,IAAqB;QACpE,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAChE,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,UAAgB,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC;QAE9F,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC/C,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,CAAC;QACtD,MAAM,eAAe,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;QACrE,MAAM,cAAc,GAAG,MAAM,IAAI,QAAQ,CAAC,eAAe,CAAC,CAAC,IAAI,EAAE,CAAC;QAElE,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,MAAM,cAAc,CAAC,WAAW,EAAE,CAAC,CAAC;QAE3E,yDAAyD;QACzD,kEAAkE;QAClE,sCAAsC;QACtC,IAAI,IAAI,KAAK,eAAe,CAAC,IAAI,IAAI,eAAe,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC;YACnE,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;QAED,OAAO,eAAe,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,UAAU,CAC7B,eAA2B,EAC3B,IAAqB;QAErB,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,qBAA2B,eAAe,CAAC,CAAC;QACpF,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,UAAgB,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC;QAE9F,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,IAAI,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;QACzD,MAAM,mBAAmB,GAAG,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;QAC1D,MAAM,iBAAiB,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC;QACzE,MAAM,gBAAgB,GAAG,MAAM,IAAI,QAAQ,CAAC,iBAAiB,CAAC,CAAC,IAAI,EAAE,CAAC;QAEtE,OAAO,IAAI,UAAU,CAAC,MAAM,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { Guards } from \"./guards.js\";\nimport { CompressionType } from \"../models/compressionType.js\";\n\n/**\n * A class to handle compression.\n */\nexport class Compression {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Compression>();\n\n\t/**\n\t * Compress bytes using
|
|
1
|
+
{"version":3,"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B;QAErB,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,qBAA2B,eAAe,CAAC,CAAC;QACpF,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,UAAgB,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC;QAE9F,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,IAAI,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;QACzD,MAAM,mBAAmB,GAAG,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;QAC1D,MAAM,iBAAiB,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC;QACzE,MAAM,gBAAgB,GAAG,MAAM,IAAI,QAAQ,CAAC,iBAAiB,CAAC,CAAC,IAAI,EAAE,CAAC;QAEtE,OAAO,IAAI,UAAU,CAAC,MAAM,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { Guards } from \"./guards.js\";\nimport { CompressionType } from \"../models/compressionType.js\";\n\n/**\n * A class to handle compression.\n */\nexport class Compression {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Compression>();\n\n\t/**\n\t * Compress bytes using the specified compression type.\n\t * @param bytes The bytes to compress.\n\t * @param type The type of compression to use.\n\t * @returns The compressed bytes.\n\t */\n\tpublic static async compress(bytes: Uint8Array, type: CompressionType): Promise<Uint8Array> {\n\t\tGuards.uint8Array(Compression.CLASS_NAME, nameof(bytes), bytes);\n\t\tGuards.arrayOneOf(Compression.CLASS_NAME, nameof(type), type, Object.values(CompressionType));\n\n\t\tconst blob = new Blob([new Uint8Array(bytes)]);\n\t\tconst compressionStream = new CompressionStream(type);\n\t\tconst compressionPipe = blob.stream().pipeThrough(compressionStream);\n\t\tconst compressedBlob = await new Response(compressionPipe).blob();\n\n\t\tconst compressedBytes = new Uint8Array(await compressedBlob.arrayBuffer());\n\n\t\t// GZIP header contains a byte which specifies the OS the\n\t\t// compression was performed on. We set this to 3 (Unix) to ensure\n\t\t// that we produce consistent results.\n\t\tif (type === CompressionType.Gzip && compressedBytes.length >= 10) {\n\t\t\tcompressedBytes[9] = 3;\n\t\t}\n\n\t\treturn compressedBytes;\n\t}\n\n\t/**\n\t * Decompress a gzipped compressed byte array.\n\t * @param compressedBytes The compressed bytes.\n\t * @param type The type of compression to use.\n\t * @returns The decompressed bytes.\n\t */\n\tpublic static async decompress(\n\t\tcompressedBytes: Uint8Array,\n\t\ttype: CompressionType\n\t): Promise<Uint8Array> {\n\t\tGuards.uint8Array(Compression.CLASS_NAME, nameof(compressedBytes), compressedBytes);\n\t\tGuards.arrayOneOf(Compression.CLASS_NAME, nameof(type), type, Object.values(CompressionType));\n\n\t\tconst blob = new Blob([new Uint8Array(compressedBytes)]);\n\t\tconst decompressionStream = new DecompressionStream(type);\n\t\tconst decompressionPipe = blob.stream().pipeThrough(decompressionStream);\n\t\tconst decompressedBlob = await new Response(decompressionPipe).blob();\n\n\t\treturn new Uint8Array(await decompressedBlob.bytes());\n\t}\n}\n"]}
|
package/dist/es/utils/is.js
CHANGED
|
@@ -8,7 +8,7 @@ export class Is {
|
|
|
8
8
|
/**
|
|
9
9
|
* Is the property undefined.
|
|
10
10
|
* @param value The value to test.
|
|
11
|
-
* @returns True if the value is
|
|
11
|
+
* @returns True if the value is undefined.
|
|
12
12
|
*/
|
|
13
13
|
static undefined(value) {
|
|
14
14
|
return value === undefined;
|
|
@@ -16,7 +16,7 @@ export class Is {
|
|
|
16
16
|
/**
|
|
17
17
|
* Is the property null.
|
|
18
18
|
* @param value The value to test.
|
|
19
|
-
* @returns True if the value is
|
|
19
|
+
* @returns True if the value is null.
|
|
20
20
|
*/
|
|
21
21
|
static null(value) {
|
|
22
22
|
return value === null;
|
|
@@ -24,15 +24,15 @@ export class Is {
|
|
|
24
24
|
/**
|
|
25
25
|
* Is the property null or undefined.
|
|
26
26
|
* @param value The value to test.
|
|
27
|
-
* @returns True if the value is
|
|
27
|
+
* @returns True if the value is null or undefined.
|
|
28
28
|
*/
|
|
29
29
|
static empty(value) {
|
|
30
30
|
return value === null || value === undefined;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
* Is the property
|
|
33
|
+
* Is the property not null or undefined.
|
|
34
34
|
* @param value The value to test.
|
|
35
|
-
* @returns True if the value is
|
|
35
|
+
* @returns True if the value is not null or undefined.
|
|
36
36
|
*/
|
|
37
37
|
static notEmpty(value) {
|
|
38
38
|
return value !== null && value !== undefined;
|
|
@@ -46,9 +46,9 @@ export class Is {
|
|
|
46
46
|
return typeof value === "string";
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
* Is the value a string.
|
|
49
|
+
* Is the value a non-empty string.
|
|
50
50
|
* @param value The value to test.
|
|
51
|
-
* @returns True if the value is a string.
|
|
51
|
+
* @returns True if the value is a non-empty string.
|
|
52
52
|
*/
|
|
53
53
|
static stringValue(value) {
|
|
54
54
|
return Is.string(value) && value.trim().length > 0;
|
|
@@ -210,7 +210,7 @@ export class Is {
|
|
|
210
210
|
/**
|
|
211
211
|
* Is the value a timestamp in seconds.
|
|
212
212
|
* @param value The value to test.
|
|
213
|
-
* @returns True if the value is a
|
|
213
|
+
* @returns True if the value is a timestamp in seconds.
|
|
214
214
|
*/
|
|
215
215
|
static timestampSeconds(value) {
|
|
216
216
|
if (!Is.integer(value)) {
|
|
@@ -221,7 +221,7 @@ export class Is {
|
|
|
221
221
|
/**
|
|
222
222
|
* Is the value a timestamp in milliseconds.
|
|
223
223
|
* @param value The value to test.
|
|
224
|
-
* @returns True if the value is a
|
|
224
|
+
* @returns True if the value is a timestamp in milliseconds.
|
|
225
225
|
*/
|
|
226
226
|
static timestampMilliseconds(value) {
|
|
227
227
|
if (!Is.integer(value)) {
|
package/dist/es/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K,CAAC,CAC/E,CAAC;IACH,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is a empty.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is a empty.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is a empty.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property is not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is a not empty.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^([A-Za-z0-9-_])*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^[A-HJ-NP-Za-km-z1-9]*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"is.js","sourceRoot":"","sources":["../../../src/utils/is.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD;;GAEG;AACH,MAAM,OAAO,EAAE;IACd;;;;OAIG;IACI,MAAM,CAAC,SAAS,CAAC,KAAc;QACrC,OAAO,KAAK,KAAK,SAAS,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,IAAI,CAAC,KAAc;QAChC,OAAO,KAAK,KAAK,IAAI,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,KAAc;QACjC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,KAAc;QACpC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc;QAClC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,WAAW,CAAC,KAAc;QACvC,OAAO,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,IAAI,CAAC,KAAc;QAChC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO,KAAK,CAAC;QACd,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,IAA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;QAC1F,CAAC;QAED,OAAO,CACN,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC;YACrB,mEAAmE,CAAC,IAAI,CAAC,KAAK,CAAC,CAC/E,CAAC;IACH,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is undefined.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is null.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is null or undefined.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is not null or undefined.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a non-empty string.\n\t * @param value The value to test.\n\t * @returns True if the value is a non-empty string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^([A-Za-z0-9-_])*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^[A-HJ-NP-Za-km-z1-9]*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in seconds.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in milliseconds.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n}\n"]}
|