@twin.org/core 0.0.1-next.4 → 0.0.1-next.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1379 -738
- package/dist/esm/index.mjs +1376 -739
- package/dist/types/encoding/base58.d.ts +18 -0
- package/dist/types/factories/factory.d.ts +20 -1
- package/dist/types/helpers/envHelper.d.ts +16 -0
- package/dist/types/helpers/jsonHelper.d.ts +29 -0
- package/dist/types/helpers/objectHelper.d.ts +33 -0
- package/dist/types/helpers/uint8ArrayHelper.d.ts +11 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/models/IComponent.d.ts +12 -3
- package/dist/types/models/coerceType.d.ts +49 -0
- package/dist/types/models/compressionType.d.ts +1 -1
- package/dist/types/utils/coerce.d.ts +22 -0
- package/dist/types/utils/converter.d.ts +12 -0
- package/dist/types/utils/guards.d.ts +16 -0
- package/dist/types/utils/is.d.ts +12 -0
- package/dist/types/utils/validation.d.ts +2 -0
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/AlreadyExistsError.md +67 -25
- package/docs/reference/classes/ArrayHelper.md +6 -2
- package/docs/reference/classes/AsyncCache.md +18 -6
- package/docs/reference/classes/Base32.md +6 -2
- package/docs/reference/classes/Base58.md +61 -0
- package/docs/reference/classes/Base64.md +9 -3
- package/docs/reference/classes/Base64Url.md +6 -2
- package/docs/reference/classes/BaseError.md +65 -23
- package/docs/reference/classes/BitString.md +18 -6
- package/docs/reference/classes/Coerce.md +104 -8
- package/docs/reference/classes/Compression.md +12 -4
- package/docs/reference/classes/ConflictError.md +70 -26
- package/docs/reference/classes/Converter.md +104 -20
- package/docs/reference/classes/EnvHelper.md +43 -0
- package/docs/reference/classes/ErrorHelper.md +9 -3
- package/docs/reference/classes/Factory.md +78 -10
- package/docs/reference/classes/FilenameHelper.md +3 -1
- package/docs/reference/classes/GeneralError.md +65 -25
- package/docs/reference/classes/GuardError.md +70 -26
- package/docs/reference/classes/Guards.md +284 -72
- package/docs/reference/classes/HexHelper.md +15 -5
- package/docs/reference/classes/I18n.md +42 -16
- package/docs/reference/classes/Is.md +156 -40
- package/docs/reference/classes/JsonHelper.md +133 -5
- package/docs/reference/classes/NotFoundError.md +67 -25
- package/docs/reference/classes/NotImplementedError.md +61 -23
- package/docs/reference/classes/NotSupportedError.md +64 -24
- package/docs/reference/classes/ObjectHelper.md +188 -20
- package/docs/reference/classes/RandomHelper.md +3 -1
- package/docs/reference/classes/StringHelper.md +51 -17
- package/docs/reference/classes/Uint8ArrayHelper.md +35 -0
- package/docs/reference/classes/UnauthorizedError.md +64 -24
- package/docs/reference/classes/UnprocessableError.md +65 -25
- package/docs/reference/classes/Url.md +30 -10
- package/docs/reference/classes/Urn.md +54 -18
- package/docs/reference/classes/Validation.md +313 -107
- package/docs/reference/classes/ValidationError.md +64 -24
- package/docs/reference/index.md +5 -0
- package/docs/reference/interfaces/IComponent.md +30 -8
- package/docs/reference/interfaces/IError.md +1 -1
- package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
- package/docs/reference/interfaces/IValidationFailure.md +1 -1
- package/docs/reference/type-aliases/CoerceType.md +5 -0
- package/docs/reference/variables/CoerceType.md +67 -0
- package/docs/reference/variables/CompressionType.md +1 -1
- package/locales/en.json +18 -1
- package/package.json +6 -6
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IntlMessageFormat } from 'intl-messageformat';
|
|
2
1
|
import { createPatch, applyPatch } from 'rfc6902';
|
|
2
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
3
3
|
|
|
4
4
|
// Copyright 2024 IOTA Stiftung.
|
|
5
5
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -111,7 +111,12 @@ class Is {
|
|
|
111
111
|
}
|
|
112
112
|
try {
|
|
113
113
|
const json = JSON.parse(value);
|
|
114
|
-
return
|
|
114
|
+
return (Is.object(json) ||
|
|
115
|
+
Is.array(json) ||
|
|
116
|
+
Is.string(json) ||
|
|
117
|
+
Is.number(json) ||
|
|
118
|
+
Is.boolean(json) ||
|
|
119
|
+
Is.null(json));
|
|
115
120
|
}
|
|
116
121
|
catch {
|
|
117
122
|
return false;
|
|
@@ -137,6 +142,16 @@ class Is {
|
|
|
137
142
|
// eslint-disable-next-line unicorn/better-regex
|
|
138
143
|
/^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=)?$/.test(value));
|
|
139
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Is the value a base58 string.
|
|
147
|
+
* @param value The value to test.
|
|
148
|
+
* @returns True if the value is a base58 string.
|
|
149
|
+
*/
|
|
150
|
+
static stringBase58(value) {
|
|
151
|
+
return (Is.stringValue(value) &&
|
|
152
|
+
// eslint-disable-next-line unicorn/better-regex
|
|
153
|
+
/^[A-HJ-NP-Za-km-z1-9]*$/.test(value));
|
|
154
|
+
}
|
|
140
155
|
/**
|
|
141
156
|
* Is the value a hex string.
|
|
142
157
|
* @param value The value to test.
|
|
@@ -349,6 +364,14 @@ class Is {
|
|
|
349
364
|
static promise(value) {
|
|
350
365
|
return value instanceof Promise;
|
|
351
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* Is the value a regexp.
|
|
369
|
+
* @param value The value to test.
|
|
370
|
+
* @returns True if the value is a regexp.
|
|
371
|
+
*/
|
|
372
|
+
static regexp(value) {
|
|
373
|
+
return value instanceof RegExp;
|
|
374
|
+
}
|
|
352
375
|
}
|
|
353
376
|
|
|
354
377
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -898,6 +921,127 @@ class Base32 {
|
|
|
898
921
|
}
|
|
899
922
|
}
|
|
900
923
|
|
|
924
|
+
/**
|
|
925
|
+
* Class to help with base58 Encoding/Decoding.
|
|
926
|
+
*/
|
|
927
|
+
class Base58 {
|
|
928
|
+
/**
|
|
929
|
+
* Runtime name for the class.
|
|
930
|
+
* @internal
|
|
931
|
+
*/
|
|
932
|
+
static _CLASS_NAME = "Base58";
|
|
933
|
+
/**
|
|
934
|
+
* Alphabet table for encoding.
|
|
935
|
+
* @internal
|
|
936
|
+
*/
|
|
937
|
+
static _ALPHABET =
|
|
938
|
+
// cspell:disable-next-line
|
|
939
|
+
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
940
|
+
/**
|
|
941
|
+
* Reverse map for decoding.
|
|
942
|
+
* @internal
|
|
943
|
+
*/
|
|
944
|
+
static _ALPHABET_REVERSE = [
|
|
945
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
946
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
947
|
+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1,
|
|
948
|
+
17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
|
|
949
|
+
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
|
|
950
|
+
57, -1, -1, -1, -1, -1
|
|
951
|
+
];
|
|
952
|
+
/**
|
|
953
|
+
* Convert the base 58 string to a byte array.
|
|
954
|
+
* @param base58 The base58 string to convert.
|
|
955
|
+
* @returns The byte array.
|
|
956
|
+
* @throws If the input string contains a character not in the Base58 alphabet.
|
|
957
|
+
*/
|
|
958
|
+
static decode(base58) {
|
|
959
|
+
let zeroes = 0;
|
|
960
|
+
for (let i = 0; i < base58.length; i++) {
|
|
961
|
+
if (base58[i] !== "1") {
|
|
962
|
+
break;
|
|
963
|
+
}
|
|
964
|
+
zeroes += 1;
|
|
965
|
+
}
|
|
966
|
+
const size = Math.trunc((base58.length * 733) / 1000) + 1;
|
|
967
|
+
const b256 = new Uint8Array(size).fill(0);
|
|
968
|
+
let length = 0;
|
|
969
|
+
for (let i = zeroes; i < base58.length; i++) {
|
|
970
|
+
const ch = base58.charCodeAt(i);
|
|
971
|
+
if (ch & 0xff80) {
|
|
972
|
+
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
973
|
+
}
|
|
974
|
+
const val = Base58._ALPHABET_REVERSE[ch];
|
|
975
|
+
if (val === -1) {
|
|
976
|
+
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
977
|
+
}
|
|
978
|
+
let carry = val;
|
|
979
|
+
let j = 0;
|
|
980
|
+
for (let k = size - 1; k >= 0; k--, j++) {
|
|
981
|
+
if (carry === 0 && j >= length) {
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
carry += b256[k] * 58;
|
|
985
|
+
b256[k] = carry;
|
|
986
|
+
carry >>>= 8;
|
|
987
|
+
}
|
|
988
|
+
length = j;
|
|
989
|
+
}
|
|
990
|
+
const out = new Uint8Array(zeroes + length);
|
|
991
|
+
let j;
|
|
992
|
+
for (j = 0; j < zeroes; j++) {
|
|
993
|
+
out[j] = 0;
|
|
994
|
+
}
|
|
995
|
+
let i = size - length;
|
|
996
|
+
while (i < size) {
|
|
997
|
+
out[j++] = b256[i++];
|
|
998
|
+
}
|
|
999
|
+
return out;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Convert a byte array to base 58.
|
|
1003
|
+
* @param bytes The byte array to encode.
|
|
1004
|
+
* @returns The data as base58 string.
|
|
1005
|
+
*/
|
|
1006
|
+
static encode(bytes) {
|
|
1007
|
+
let zeroes = 0;
|
|
1008
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
1009
|
+
if (bytes[i] !== 0) {
|
|
1010
|
+
break;
|
|
1011
|
+
}
|
|
1012
|
+
zeroes += 1;
|
|
1013
|
+
}
|
|
1014
|
+
const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
|
|
1015
|
+
const b58 = new Uint8Array(size).fill(0);
|
|
1016
|
+
let length = 0;
|
|
1017
|
+
for (let i = zeroes; i < bytes.length; i++) {
|
|
1018
|
+
let carry = bytes[i];
|
|
1019
|
+
let j = 0;
|
|
1020
|
+
for (let k = size - 1; k >= 0; k--, j++) {
|
|
1021
|
+
if (carry === 0 && j >= length) {
|
|
1022
|
+
break;
|
|
1023
|
+
}
|
|
1024
|
+
carry += b58[k] * 256;
|
|
1025
|
+
b58[k] = carry % 58;
|
|
1026
|
+
carry = Math.trunc(carry / 58);
|
|
1027
|
+
}
|
|
1028
|
+
length = j;
|
|
1029
|
+
}
|
|
1030
|
+
let i = size - length;
|
|
1031
|
+
while (i < size && b58[i] === 0) {
|
|
1032
|
+
i += 1;
|
|
1033
|
+
}
|
|
1034
|
+
let str = "";
|
|
1035
|
+
for (let j = 0; j < zeroes; j++) {
|
|
1036
|
+
str += "1";
|
|
1037
|
+
}
|
|
1038
|
+
while (i < size) {
|
|
1039
|
+
str += Base58._ALPHABET[b58[i++]];
|
|
1040
|
+
}
|
|
1041
|
+
return str;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
901
1045
|
// Copyright 2024 IOTA Stiftung.
|
|
902
1046
|
// SPDX-License-Identifier: Apache-2.0.
|
|
903
1047
|
/* eslint-disable no-bitwise */
|
|
@@ -1052,7 +1196,7 @@ class Base64 {
|
|
|
1052
1196
|
const maxChunkLength = 16383; // must be multiple of 3
|
|
1053
1197
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
1054
1198
|
for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
1055
|
-
parts.push(Base64.encodeChunk(bytes, i, i + maxChunkLength
|
|
1199
|
+
parts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));
|
|
1056
1200
|
}
|
|
1057
1201
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
1058
1202
|
if (extraBytes === 1) {
|
|
@@ -1393,6 +1537,18 @@ class Guards {
|
|
|
1393
1537
|
throw new GuardError(source, "guard.stringEmpty", property, value);
|
|
1394
1538
|
}
|
|
1395
1539
|
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Is the property a JSON value.
|
|
1542
|
+
* @param source The source of the error.
|
|
1543
|
+
* @param property The name of the property.
|
|
1544
|
+
* @param value The value to test.
|
|
1545
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1546
|
+
*/
|
|
1547
|
+
static json(source, property, value) {
|
|
1548
|
+
if (!Is.json(value)) {
|
|
1549
|
+
throw new GuardError(source, "guard.stringJson", property, value);
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1396
1552
|
/**
|
|
1397
1553
|
* Is the property a base64 string.
|
|
1398
1554
|
* @param source The source of the error.
|
|
@@ -1417,6 +1573,18 @@ class Guards {
|
|
|
1417
1573
|
throw new GuardError(source, "guard.base64Url", property, value);
|
|
1418
1574
|
}
|
|
1419
1575
|
}
|
|
1576
|
+
/**
|
|
1577
|
+
* Is the property a base58 string.
|
|
1578
|
+
* @param source The source of the error.
|
|
1579
|
+
* @param property The name of the property.
|
|
1580
|
+
* @param value The value to test.
|
|
1581
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1582
|
+
*/
|
|
1583
|
+
static stringBase58(source, property, value) {
|
|
1584
|
+
if (!Is.stringBase58(value)) {
|
|
1585
|
+
throw new GuardError(source, "guard.base58", property, value);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1420
1588
|
/**
|
|
1421
1589
|
* Is the property a string with a hex value.
|
|
1422
1590
|
* @param source The source of the error.
|
|
@@ -1718,6 +1886,29 @@ class Factory {
|
|
|
1718
1886
|
}
|
|
1719
1887
|
return Factory._factories[typeName];
|
|
1720
1888
|
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Get all the factories.
|
|
1891
|
+
* @returns All the factories.
|
|
1892
|
+
*/
|
|
1893
|
+
static getFactories() {
|
|
1894
|
+
return Factory._factories;
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* Reset all the factories, which removes any created instances, but not the registrations.
|
|
1898
|
+
*/
|
|
1899
|
+
static resetFactories() {
|
|
1900
|
+
for (const typeName in Factory._factories) {
|
|
1901
|
+
Factory._factories[typeName].reset();
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
/**
|
|
1905
|
+
* Clear all the factories, which removes anything registered with the factories.
|
|
1906
|
+
*/
|
|
1907
|
+
static clearFactories() {
|
|
1908
|
+
for (const typeName in Factory._factories) {
|
|
1909
|
+
Factory._factories[typeName].clear();
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1721
1912
|
/**
|
|
1722
1913
|
* Register a new generator.
|
|
1723
1914
|
* @param name The name of the generator.
|
|
@@ -1789,7 +1980,7 @@ class Factory {
|
|
|
1789
1980
|
}
|
|
1790
1981
|
}
|
|
1791
1982
|
/**
|
|
1792
|
-
*
|
|
1983
|
+
* Remove all the instances and leave the generators intact.
|
|
1793
1984
|
*/
|
|
1794
1985
|
reset() {
|
|
1795
1986
|
for (const name in this._generators) {
|
|
@@ -1797,6 +1988,14 @@ class Factory {
|
|
|
1797
1988
|
}
|
|
1798
1989
|
this._instances = {};
|
|
1799
1990
|
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Remove all the instances and the generators.
|
|
1993
|
+
*/
|
|
1994
|
+
clear() {
|
|
1995
|
+
this._instances = {};
|
|
1996
|
+
this._generators = {};
|
|
1997
|
+
this._orderCounter = 0;
|
|
1998
|
+
}
|
|
1800
1999
|
/**
|
|
1801
2000
|
* Get all the instances as a map.
|
|
1802
2001
|
* @returns The instances as a map.
|
|
@@ -1902,244 +2101,252 @@ class ArrayHelper {
|
|
|
1902
2101
|
|
|
1903
2102
|
// Copyright 2024 IOTA Stiftung.
|
|
1904
2103
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2104
|
+
/* eslint-disable no-bitwise */
|
|
1905
2105
|
/**
|
|
1906
|
-
*
|
|
2106
|
+
* Convert arrays to and from different formats.
|
|
1907
2107
|
*/
|
|
1908
|
-
class
|
|
1909
|
-
/**
|
|
1910
|
-
* The default translation.
|
|
1911
|
-
*/
|
|
1912
|
-
static DEFAULT_LOCALE = "en";
|
|
1913
|
-
/**
|
|
1914
|
-
* Dictionaries for lookups.
|
|
1915
|
-
* @internal
|
|
1916
|
-
*/
|
|
1917
|
-
static _localeDictionaries = {};
|
|
1918
|
-
/**
|
|
1919
|
-
* The current locale.
|
|
1920
|
-
* @internal
|
|
1921
|
-
*/
|
|
1922
|
-
static _currentLocale = I18n.DEFAULT_LOCALE;
|
|
2108
|
+
class Converter {
|
|
1923
2109
|
/**
|
|
1924
|
-
*
|
|
2110
|
+
* Lookup table for encoding.
|
|
1925
2111
|
* @internal
|
|
1926
2112
|
*/
|
|
1927
|
-
static
|
|
2113
|
+
static _ENCODE_LOOKUP;
|
|
1928
2114
|
/**
|
|
1929
|
-
*
|
|
2115
|
+
* Lookup table for decoding.
|
|
1930
2116
|
* @internal
|
|
1931
2117
|
*/
|
|
1932
|
-
static
|
|
2118
|
+
static _DECODE_LOOKUP;
|
|
1933
2119
|
/**
|
|
1934
|
-
*
|
|
1935
|
-
* @param
|
|
2120
|
+
* Encode a raw array to UTF8 string.
|
|
2121
|
+
* @param array The bytes to encode.
|
|
2122
|
+
* @param startIndex The index to start in the bytes.
|
|
2123
|
+
* @param length The length of bytes to read.
|
|
2124
|
+
* @returns The array formatted as UTF8.
|
|
1936
2125
|
*/
|
|
1937
|
-
static
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
2126
|
+
static bytesToUtf8(array, startIndex, length) {
|
|
2127
|
+
const start = startIndex ?? 0;
|
|
2128
|
+
const len = length ?? array.length;
|
|
2129
|
+
let str = "";
|
|
2130
|
+
for (let i = start; i < start + len; i++) {
|
|
2131
|
+
const value = array[i];
|
|
2132
|
+
if (value < 0x80) {
|
|
2133
|
+
str += String.fromCharCode(value);
|
|
2134
|
+
}
|
|
2135
|
+
else if (value > 0xbf && value < 0xe0) {
|
|
2136
|
+
str += String.fromCharCode(((value & 0x1f) << 6) | (array[i + 1] & 0x3f));
|
|
2137
|
+
i += 1;
|
|
2138
|
+
}
|
|
2139
|
+
else if (value > 0xdf && value < 0xf0) {
|
|
2140
|
+
str += String.fromCharCode(((value & 0x0f) << 12) | ((array[i + 1] & 0x3f) << 6) | (array[i + 2] & 0x3f));
|
|
2141
|
+
i += 2;
|
|
2142
|
+
}
|
|
2143
|
+
else {
|
|
2144
|
+
// surrogate pair
|
|
2145
|
+
const charCode = (((value & 0x07) << 18) |
|
|
2146
|
+
((array[i + 1] & 0x3f) << 12) |
|
|
2147
|
+
((array[i + 2] & 0x3f) << 6) |
|
|
2148
|
+
(array[i + 3] & 0x3f)) -
|
|
2149
|
+
0x010000;
|
|
2150
|
+
str += String.fromCharCode((charCode >> 10) | 0xd800, (charCode & 0x03ff) | 0xdc00);
|
|
2151
|
+
i += 3;
|
|
2152
|
+
}
|
|
1941
2153
|
}
|
|
2154
|
+
return str;
|
|
1942
2155
|
}
|
|
1943
2156
|
/**
|
|
1944
|
-
*
|
|
1945
|
-
* @
|
|
2157
|
+
* Convert a UTF8 string to raw array.
|
|
2158
|
+
* @param utf8 The text to decode.
|
|
2159
|
+
* @returns The array.
|
|
1946
2160
|
*/
|
|
1947
|
-
static
|
|
1948
|
-
|
|
2161
|
+
static utf8ToBytes(utf8) {
|
|
2162
|
+
const bytes = [];
|
|
2163
|
+
for (let i = 0; i < utf8.length; i++) {
|
|
2164
|
+
let charCode = utf8.charCodeAt(i);
|
|
2165
|
+
if (charCode < 0x80) {
|
|
2166
|
+
bytes.push(charCode);
|
|
2167
|
+
}
|
|
2168
|
+
else if (charCode < 0x800) {
|
|
2169
|
+
bytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
|
|
2170
|
+
}
|
|
2171
|
+
else if (charCode < 0xd800 || charCode >= 0xe000) {
|
|
2172
|
+
bytes.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
|
|
2173
|
+
}
|
|
2174
|
+
else {
|
|
2175
|
+
// surrogate pair
|
|
2176
|
+
i++;
|
|
2177
|
+
// UTF-16 encodes 0x10000-0x10FFFF by
|
|
2178
|
+
// subtracting 0x10000 and splitting the
|
|
2179
|
+
// 20 bits of 0x0-0xFFFFF into two halves
|
|
2180
|
+
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (utf8.charCodeAt(i) & 0x3ff));
|
|
2181
|
+
bytes.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
return Uint8Array.from(bytes);
|
|
1949
2185
|
}
|
|
1950
2186
|
/**
|
|
1951
|
-
*
|
|
1952
|
-
* @param
|
|
1953
|
-
* @param
|
|
2187
|
+
* Encode a raw array to hex string.
|
|
2188
|
+
* @param array The bytes to encode.
|
|
2189
|
+
* @param includePrefix Include the 0x prefix on the returned hex.
|
|
2190
|
+
* @param startIndex The index to start in the bytes.
|
|
2191
|
+
* @param length The length of bytes to read.
|
|
2192
|
+
* @param reverse Reverse the combine direction.
|
|
2193
|
+
* @returns The array formatted as hex.
|
|
1954
2194
|
*/
|
|
1955
|
-
static
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
2195
|
+
static bytesToHex(array, includePrefix = false, startIndex, length, reverse) {
|
|
2196
|
+
let hex = "";
|
|
2197
|
+
this.buildHexLookups();
|
|
2198
|
+
if (Converter._ENCODE_LOOKUP) {
|
|
2199
|
+
const len = length ?? array.length;
|
|
2200
|
+
const start = startIndex ?? 0;
|
|
2201
|
+
if (reverse) {
|
|
2202
|
+
for (let i = 0; i < len; i++) {
|
|
2203
|
+
hex = Converter._ENCODE_LOOKUP[array[start + i]] + hex;
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
else {
|
|
2207
|
+
for (let i = 0; i < len; i++) {
|
|
2208
|
+
hex += Converter._ENCODE_LOOKUP[array[start + i]];
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
1961
2211
|
}
|
|
2212
|
+
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
1962
2213
|
}
|
|
1963
2214
|
/**
|
|
1964
|
-
*
|
|
1965
|
-
* @param
|
|
1966
|
-
* @
|
|
2215
|
+
* Decode a hex string to raw array.
|
|
2216
|
+
* @param hex The hex to decode.
|
|
2217
|
+
* @param reverse Store the characters in reverse.
|
|
2218
|
+
* @returns The array.
|
|
1967
2219
|
*/
|
|
1968
|
-
static
|
|
1969
|
-
|
|
2220
|
+
static hexToBytes(hex, reverse) {
|
|
2221
|
+
const strippedHex = HexHelper.stripPrefix(hex);
|
|
2222
|
+
const sizeof = strippedHex.length >> 1;
|
|
2223
|
+
const length = sizeof << 1;
|
|
2224
|
+
const array = new Uint8Array(sizeof);
|
|
2225
|
+
this.buildHexLookups();
|
|
2226
|
+
if (Converter._DECODE_LOOKUP) {
|
|
2227
|
+
let i = 0;
|
|
2228
|
+
let n = 0;
|
|
2229
|
+
while (i < length) {
|
|
2230
|
+
array[n++] =
|
|
2231
|
+
(Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)] << 4) |
|
|
2232
|
+
Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)];
|
|
2233
|
+
}
|
|
2234
|
+
if (reverse) {
|
|
2235
|
+
array.reverse();
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
return array;
|
|
1970
2239
|
}
|
|
1971
2240
|
/**
|
|
1972
|
-
*
|
|
1973
|
-
* @
|
|
2241
|
+
* Convert the UTF8 to hex.
|
|
2242
|
+
* @param utf8 The text to convert.
|
|
2243
|
+
* @param includePrefix Include the 0x prefix on the returned hex.
|
|
2244
|
+
* @returns The hex version of the bytes.
|
|
1974
2245
|
*/
|
|
1975
|
-
static
|
|
1976
|
-
|
|
2246
|
+
static utf8ToHex(utf8, includePrefix = false) {
|
|
2247
|
+
const hex = Converter.bytesToHex(Converter.utf8ToBytes(utf8));
|
|
2248
|
+
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
1977
2249
|
}
|
|
1978
2250
|
/**
|
|
1979
|
-
*
|
|
1980
|
-
* @param
|
|
1981
|
-
* @
|
|
2251
|
+
* Convert the hex text to text.
|
|
2252
|
+
* @param hex The hex to convert.
|
|
2253
|
+
* @returns The UTF8 version of the bytes.
|
|
1982
2254
|
*/
|
|
1983
|
-
static
|
|
1984
|
-
|
|
2255
|
+
static hexToUtf8(hex) {
|
|
2256
|
+
return Converter.bytesToUtf8(Converter.hexToBytes(HexHelper.stripPrefix(hex)));
|
|
1985
2257
|
}
|
|
1986
2258
|
/**
|
|
1987
|
-
*
|
|
1988
|
-
* @param
|
|
2259
|
+
* Convert bytes to binary string.
|
|
2260
|
+
* @param bytes The bytes to convert.
|
|
2261
|
+
* @returns A binary string of the bytes.
|
|
1989
2262
|
*/
|
|
1990
|
-
static
|
|
1991
|
-
|
|
2263
|
+
static bytesToBinary(bytes) {
|
|
2264
|
+
const b = [];
|
|
2265
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2266
|
+
b.push(bytes[i].toString(2).padStart(8, "0"));
|
|
2267
|
+
}
|
|
2268
|
+
return b.join("");
|
|
1992
2269
|
}
|
|
1993
2270
|
/**
|
|
1994
|
-
*
|
|
1995
|
-
* @param
|
|
1996
|
-
* @
|
|
2271
|
+
* Convert a binary string to bytes.
|
|
2272
|
+
* @param binary The binary string.
|
|
2273
|
+
* @returns The bytes.
|
|
1997
2274
|
*/
|
|
1998
|
-
static
|
|
1999
|
-
|
|
2275
|
+
static binaryToBytes(binary) {
|
|
2276
|
+
const bytes = new Uint8Array(Math.ceil(binary.length / 8));
|
|
2277
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2278
|
+
bytes[i] = Number.parseInt(binary.slice(i * 8, (i + 1) * 8), 2);
|
|
2279
|
+
}
|
|
2280
|
+
return bytes;
|
|
2000
2281
|
}
|
|
2001
2282
|
/**
|
|
2002
|
-
*
|
|
2003
|
-
* @param
|
|
2283
|
+
* Convert bytes to base64 string.
|
|
2284
|
+
* @param bytes The bytes to convert.
|
|
2285
|
+
* @returns A base64 string of the bytes.
|
|
2004
2286
|
*/
|
|
2005
|
-
static
|
|
2006
|
-
|
|
2287
|
+
static bytesToBase64(bytes) {
|
|
2288
|
+
return Base64.encode(bytes);
|
|
2007
2289
|
}
|
|
2008
2290
|
/**
|
|
2009
|
-
*
|
|
2010
|
-
* @param
|
|
2011
|
-
* @
|
|
2012
|
-
* @param overrideLocale Override the locale.
|
|
2013
|
-
* @returns The formatted string.
|
|
2291
|
+
* Convert a base64 string to bytes.
|
|
2292
|
+
* @param base64 The base64 string.
|
|
2293
|
+
* @returns The bytes.
|
|
2014
2294
|
*/
|
|
2015
|
-
static
|
|
2016
|
-
|
|
2017
|
-
if (cl.startsWith("debug-")) {
|
|
2018
|
-
cl = I18n.DEFAULT_LOCALE;
|
|
2019
|
-
}
|
|
2020
|
-
if (!I18n._localeDictionaries[cl]) {
|
|
2021
|
-
return `!!Missing ${cl}`;
|
|
2022
|
-
}
|
|
2023
|
-
if (!I18n._localeDictionaries[cl][key]) {
|
|
2024
|
-
return `!!Missing ${cl}.${key}`;
|
|
2025
|
-
}
|
|
2026
|
-
if (I18n._currentLocale === "debug-k") {
|
|
2027
|
-
return key;
|
|
2028
|
-
}
|
|
2029
|
-
let ret = new IntlMessageFormat(I18n._localeDictionaries[cl][key], cl).format(values);
|
|
2030
|
-
if (I18n._currentLocale === "debug-x") {
|
|
2031
|
-
ret = ret.replace(/[a-z]/g, "x").replace(/[A-Z]/g, "x").replace(/\d/g, "n");
|
|
2032
|
-
}
|
|
2033
|
-
return ret;
|
|
2295
|
+
static base64ToBytes(base64) {
|
|
2296
|
+
return Base64.decode(base64);
|
|
2034
2297
|
}
|
|
2035
2298
|
/**
|
|
2036
|
-
*
|
|
2037
|
-
* @param
|
|
2038
|
-
* @returns
|
|
2299
|
+
* Convert bytes to base64 url string.
|
|
2300
|
+
* @param bytes The bytes to convert.
|
|
2301
|
+
* @returns A base64 url string of the bytes.
|
|
2039
2302
|
*/
|
|
2040
|
-
static
|
|
2041
|
-
return
|
|
2303
|
+
static bytesToBase64Url(bytes) {
|
|
2304
|
+
return Base64Url.encode(bytes);
|
|
2042
2305
|
}
|
|
2043
2306
|
/**
|
|
2044
|
-
*
|
|
2045
|
-
* @param
|
|
2046
|
-
* @
|
|
2047
|
-
* @param mergedKeys The merged keys dictionary to populate.
|
|
2048
|
-
* @internal
|
|
2307
|
+
* Convert a base64 url string to bytes.
|
|
2308
|
+
* @param base64Url The base64 url string.
|
|
2309
|
+
* @returns The bytes.
|
|
2049
2310
|
*/
|
|
2050
|
-
static
|
|
2051
|
-
|
|
2052
|
-
const val = translation[key];
|
|
2053
|
-
const mergedPath = propertyPath.length > 0 ? `${propertyPath}.${key}` : key;
|
|
2054
|
-
if (Is.string(val)) {
|
|
2055
|
-
mergedKeys[mergedPath] = val;
|
|
2056
|
-
}
|
|
2057
|
-
else if (Is.object(val)) {
|
|
2058
|
-
I18n.flattenTranslationKeys(val, mergedPath, mergedKeys);
|
|
2059
|
-
}
|
|
2060
|
-
}
|
|
2311
|
+
static base64UrlToBytes(base64Url) {
|
|
2312
|
+
return Base64Url.decode(base64Url);
|
|
2061
2313
|
}
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
// Copyright 2024 IOTA Stiftung.
|
|
2065
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
2066
|
-
/**
|
|
2067
|
-
* Error helper functions.
|
|
2068
|
-
*/
|
|
2069
|
-
class ErrorHelper {
|
|
2070
2314
|
/**
|
|
2071
|
-
*
|
|
2072
|
-
* @param
|
|
2073
|
-
* @returns
|
|
2315
|
+
* Convert bytes to base58 string.
|
|
2316
|
+
* @param bytes The bytes to convert.
|
|
2317
|
+
* @returns A base58 string of the bytes.
|
|
2074
2318
|
*/
|
|
2075
|
-
static
|
|
2076
|
-
return
|
|
2319
|
+
static bytesToBase58(bytes) {
|
|
2320
|
+
return Base58.encode(bytes);
|
|
2077
2321
|
}
|
|
2078
2322
|
/**
|
|
2079
|
-
*
|
|
2080
|
-
* @param
|
|
2081
|
-
* @returns The
|
|
2323
|
+
* Convert a base58 string to bytes.
|
|
2324
|
+
* @param base58 The base58 string.
|
|
2325
|
+
* @returns The bytes.
|
|
2082
2326
|
*/
|
|
2083
|
-
static
|
|
2084
|
-
|
|
2085
|
-
if (Is.notEmpty(error)) {
|
|
2086
|
-
const errors = BaseError.flatten(error);
|
|
2087
|
-
for (const err of errors) {
|
|
2088
|
-
const errorNameKey = `errorNames.${StringHelper.camelCase(err.name)}`;
|
|
2089
|
-
const errorMessageKey = `error.${err.message}`;
|
|
2090
|
-
// If there is no error message then it is probably
|
|
2091
|
-
// from a 3rd party lib, so don't format it just display
|
|
2092
|
-
const hasErrorName = I18n.hasMessage(errorNameKey);
|
|
2093
|
-
const hasErrorMessage = I18n.hasMessage(errorMessageKey);
|
|
2094
|
-
const localizedError = {
|
|
2095
|
-
name: I18n.formatMessage(hasErrorName ? errorNameKey : "errorNames.error"),
|
|
2096
|
-
message: hasErrorMessage
|
|
2097
|
-
? I18n.formatMessage(errorMessageKey, err.properties)
|
|
2098
|
-
: err.message
|
|
2099
|
-
};
|
|
2100
|
-
if (Is.stringValue(err.source)) {
|
|
2101
|
-
localizedError.source = err.source;
|
|
2102
|
-
}
|
|
2103
|
-
if (Is.stringValue(err.stack)) {
|
|
2104
|
-
// Remove the first line from the stack traces as they
|
|
2105
|
-
// just have the error type and message duplicated
|
|
2106
|
-
const lines = err.stack.split("\n");
|
|
2107
|
-
lines.shift();
|
|
2108
|
-
localizedError.stack = lines.join("\n");
|
|
2109
|
-
}
|
|
2110
|
-
const additional = ErrorHelper.formatValidationErrors(err);
|
|
2111
|
-
if (Is.stringValue(additional)) {
|
|
2112
|
-
localizedError.additional = additional;
|
|
2113
|
-
}
|
|
2114
|
-
formattedErrors.push(localizedError);
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
return formattedErrors;
|
|
2327
|
+
static base58ToBytes(base58) {
|
|
2328
|
+
return Base58.decode(base58);
|
|
2118
2329
|
}
|
|
2119
2330
|
/**
|
|
2120
|
-
*
|
|
2121
|
-
* @
|
|
2122
|
-
* @returns The localized version of the errors flattened.
|
|
2331
|
+
* Build the static lookup tables.
|
|
2332
|
+
* @internal
|
|
2123
2333
|
*/
|
|
2124
|
-
static
|
|
2125
|
-
if (
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
v += ` = ${JSON.stringify(validationFailure.properties.value)}`;
|
|
2334
|
+
static buildHexLookups() {
|
|
2335
|
+
if (!Converter._ENCODE_LOOKUP || !Converter._DECODE_LOOKUP) {
|
|
2336
|
+
const alphabet = "0123456789abcdef";
|
|
2337
|
+
Converter._ENCODE_LOOKUP = [];
|
|
2338
|
+
Converter._DECODE_LOOKUP = [];
|
|
2339
|
+
for (let i = 0; i < 256; i++) {
|
|
2340
|
+
Converter._ENCODE_LOOKUP[i] = alphabet[(i >> 4) & 0xf] + alphabet[i & 0xf];
|
|
2341
|
+
if (i < 16) {
|
|
2342
|
+
if (i < 10) {
|
|
2343
|
+
Converter._DECODE_LOOKUP[0x30 + i] = i;
|
|
2344
|
+
}
|
|
2345
|
+
else {
|
|
2346
|
+
Converter._DECODE_LOOKUP[0x61 - 10 + i] = i;
|
|
2347
|
+
}
|
|
2139
2348
|
}
|
|
2140
|
-
validationErrors.push(v);
|
|
2141
2349
|
}
|
|
2142
|
-
return validationErrors.join("\n");
|
|
2143
2350
|
}
|
|
2144
2351
|
}
|
|
2145
2352
|
}
|
|
@@ -2147,707 +2354,1071 @@ class ErrorHelper {
|
|
|
2147
2354
|
// Copyright 2024 IOTA Stiftung.
|
|
2148
2355
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2149
2356
|
/**
|
|
2150
|
-
*
|
|
2357
|
+
* Helpers methods for JSON objects.
|
|
2151
2358
|
*/
|
|
2152
|
-
class
|
|
2359
|
+
class JsonHelper {
|
|
2153
2360
|
/**
|
|
2154
|
-
*
|
|
2155
|
-
*
|
|
2156
|
-
* @
|
|
2157
|
-
* @returns The
|
|
2361
|
+
* Serializes in canonical format.
|
|
2362
|
+
* Based on https://www.rfc-editor.org/rfc/rfc8785.
|
|
2363
|
+
* @param object The object to be serialized.
|
|
2364
|
+
* @returns The serialized object.
|
|
2158
2365
|
*/
|
|
2159
|
-
static
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
if (Is.number(value)) {
|
|
2167
|
-
return value.toString();
|
|
2366
|
+
static canonicalize(object) {
|
|
2367
|
+
const buffer = [];
|
|
2368
|
+
if (object === null ||
|
|
2369
|
+
typeof object !== "object" ||
|
|
2370
|
+
("toJSON" in object && object.toJSON instanceof Function)) {
|
|
2371
|
+
// Primitive data type
|
|
2372
|
+
buffer.push(JSON.stringify(object));
|
|
2168
2373
|
}
|
|
2169
|
-
if (
|
|
2170
|
-
|
|
2374
|
+
else if (Array.isArray(object)) {
|
|
2375
|
+
// Array maintain element order
|
|
2376
|
+
const parts = [];
|
|
2377
|
+
for (const element of object) {
|
|
2378
|
+
if (element === undefined) {
|
|
2379
|
+
parts.push("null");
|
|
2380
|
+
}
|
|
2381
|
+
else {
|
|
2382
|
+
parts.push(JsonHelper.canonicalize(element));
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
buffer.push(`[${parts.join(",")}]`);
|
|
2171
2386
|
}
|
|
2172
|
-
|
|
2173
|
-
|
|
2387
|
+
else {
|
|
2388
|
+
// Object sort properties
|
|
2389
|
+
const props = [];
|
|
2390
|
+
const keys = Object.keys(object).sort();
|
|
2391
|
+
const o = object;
|
|
2392
|
+
for (const key of keys) {
|
|
2393
|
+
if (o[key] !== undefined) {
|
|
2394
|
+
props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
buffer.push(`{${props.join(",")}}`);
|
|
2174
2398
|
}
|
|
2399
|
+
return buffer.join("");
|
|
2175
2400
|
}
|
|
2176
2401
|
/**
|
|
2177
|
-
*
|
|
2178
|
-
*
|
|
2179
|
-
* @
|
|
2180
|
-
* @
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
if (Is.number(value)) {
|
|
2187
|
-
return value;
|
|
2188
|
-
}
|
|
2189
|
-
if (Is.string(value)) {
|
|
2190
|
-
const parsed = Number.parseFloat(value);
|
|
2191
|
-
if (Is.number(parsed)) {
|
|
2192
|
-
return parsed;
|
|
2193
|
-
}
|
|
2194
|
-
}
|
|
2195
|
-
if (Is.boolean(value)) {
|
|
2196
|
-
return value ? 1 : 0;
|
|
2197
|
-
}
|
|
2198
|
-
if (Is.date(value)) {
|
|
2199
|
-
return value.getTime();
|
|
2200
|
-
}
|
|
2402
|
+
* Creates a RFC 6902 diff set.
|
|
2403
|
+
* Based on https://www.rfc-editor.org/rfc/rfc6902.
|
|
2404
|
+
* @param object1 The first object.
|
|
2405
|
+
* @param object2 The second object.
|
|
2406
|
+
* @returns The list of patches.
|
|
2407
|
+
*/
|
|
2408
|
+
static diff(object1, object2) {
|
|
2409
|
+
const operations = createPatch(object1, object2);
|
|
2410
|
+
return operations;
|
|
2201
2411
|
}
|
|
2202
2412
|
/**
|
|
2203
|
-
*
|
|
2204
|
-
*
|
|
2205
|
-
* @
|
|
2206
|
-
* @
|
|
2413
|
+
* Applies a RFC 6902 diff set to an object.
|
|
2414
|
+
* Based on https://www.rfc-editor.org/rfc/rfc6902.
|
|
2415
|
+
* @param object The object to patch.
|
|
2416
|
+
* @param patches The second object.
|
|
2417
|
+
* @returns The updated object.
|
|
2207
2418
|
*/
|
|
2208
|
-
static
|
|
2209
|
-
|
|
2210
|
-
return value;
|
|
2211
|
-
}
|
|
2212
|
-
if (Is.bigint(value)) {
|
|
2213
|
-
return value;
|
|
2214
|
-
}
|
|
2215
|
-
if (Is.number(value)) {
|
|
2216
|
-
return BigInt(value);
|
|
2217
|
-
}
|
|
2218
|
-
if (Is.string(value)) {
|
|
2219
|
-
const parsed = Number.parseFloat(value);
|
|
2220
|
-
if (Is.integer(parsed)) {
|
|
2221
|
-
return BigInt(parsed);
|
|
2222
|
-
}
|
|
2223
|
-
}
|
|
2224
|
-
if (Is.boolean(value)) {
|
|
2225
|
-
return value ? 1n : 0n;
|
|
2226
|
-
}
|
|
2419
|
+
static patch(object, patches) {
|
|
2420
|
+
return applyPatch(object, patches);
|
|
2227
2421
|
}
|
|
2228
2422
|
/**
|
|
2229
|
-
*
|
|
2230
|
-
* @param
|
|
2231
|
-
* @
|
|
2232
|
-
* @returns The
|
|
2423
|
+
* Stringify the JSON with support for extended data types date/bigint/uint8array.
|
|
2424
|
+
* @param object The object to stringify.
|
|
2425
|
+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
|
|
2426
|
+
* @returns The stringified object.
|
|
2233
2427
|
*/
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2428
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2429
|
+
static stringifyEx(object, space) {
|
|
2430
|
+
// We want to keep the 'this' intact for the replacer
|
|
2431
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
2432
|
+
return JSON.stringify(object, JsonHelper.stringifyExReplacer, space);
|
|
2433
|
+
}
|
|
2434
|
+
/**
|
|
2435
|
+
* Parse the JSON string with support for extended data types date/bigint/uint8array.
|
|
2436
|
+
* @param json The object to pause.
|
|
2437
|
+
* @returns The object.
|
|
2438
|
+
*/
|
|
2439
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2440
|
+
static parseEx(json) {
|
|
2441
|
+
// We want to keep the 'this' intact for the reviver
|
|
2442
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
2443
|
+
return JSON.parse(json, JsonHelper.parseExReviver);
|
|
2444
|
+
}
|
|
2445
|
+
/**
|
|
2446
|
+
* Replacer function to handle extended data types.
|
|
2447
|
+
* @param this The object.
|
|
2448
|
+
* @param key The key.
|
|
2449
|
+
* @param value The value.
|
|
2450
|
+
* @returns The value.
|
|
2451
|
+
*/
|
|
2452
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2453
|
+
static stringifyExReplacer(key, value) {
|
|
2454
|
+
const rawValue = this[key];
|
|
2455
|
+
if (Is.bigint(rawValue)) {
|
|
2456
|
+
return {
|
|
2457
|
+
"@ext": "bigint",
|
|
2458
|
+
value: rawValue.toString()
|
|
2459
|
+
};
|
|
2460
|
+
}
|
|
2461
|
+
else if (Is.date(rawValue)) {
|
|
2462
|
+
return {
|
|
2463
|
+
"@ext": "date",
|
|
2464
|
+
value: rawValue.getTime()
|
|
2465
|
+
};
|
|
2466
|
+
}
|
|
2467
|
+
else if (Is.uint8Array(rawValue)) {
|
|
2468
|
+
return {
|
|
2469
|
+
"@ext": "uint8array",
|
|
2470
|
+
value: Converter.bytesToBase64(rawValue)
|
|
2471
|
+
};
|
|
2472
|
+
}
|
|
2473
|
+
return value;
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* Reviver function to handle extended data types.
|
|
2477
|
+
* @param this The object.
|
|
2478
|
+
* @param key The key.
|
|
2479
|
+
* @param value The value.
|
|
2480
|
+
* @returns The value.
|
|
2481
|
+
*/
|
|
2482
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2483
|
+
static parseExReviver(key, value) {
|
|
2484
|
+
if (Is.object(value)) {
|
|
2485
|
+
if (value["@ext"] === "bigint") {
|
|
2486
|
+
return BigInt(value.value);
|
|
2248
2487
|
}
|
|
2249
|
-
if (
|
|
2250
|
-
return
|
|
2488
|
+
else if (value["@ext"] === "date") {
|
|
2489
|
+
return new Date(value.value);
|
|
2490
|
+
}
|
|
2491
|
+
else if (value["@ext"] === "uint8array") {
|
|
2492
|
+
return Converter.base64ToBytes(value.value);
|
|
2251
2493
|
}
|
|
2252
2494
|
}
|
|
2495
|
+
return value;
|
|
2253
2496
|
}
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
/**
|
|
2500
|
+
* Class to help with objects.
|
|
2501
|
+
*/
|
|
2502
|
+
class ObjectHelper {
|
|
2254
2503
|
/**
|
|
2255
|
-
*
|
|
2256
|
-
* @
|
|
2257
|
-
* @throws TypeError If the value can not be coerced.
|
|
2258
|
-
* @returns The value if it can be coerced.
|
|
2504
|
+
* Runtime name for the class.
|
|
2505
|
+
* @internal
|
|
2259
2506
|
*/
|
|
2260
|
-
static
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
if (Is.string(value)) {
|
|
2271
|
-
const dt = new Date(value);
|
|
2272
|
-
if (!Number.isNaN(dt.getTime())) {
|
|
2273
|
-
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate());
|
|
2274
|
-
return new Date(utc);
|
|
2275
|
-
}
|
|
2507
|
+
static _CLASS_NAME = "ObjectHelper";
|
|
2508
|
+
/**
|
|
2509
|
+
* Convert an object to bytes.
|
|
2510
|
+
* @param obj The object to convert.
|
|
2511
|
+
* @param format Format the JSON content.
|
|
2512
|
+
* @returns The object as bytes.
|
|
2513
|
+
*/
|
|
2514
|
+
static toBytes(obj, format = false) {
|
|
2515
|
+
if (obj === undefined) {
|
|
2516
|
+
return new Uint8Array();
|
|
2276
2517
|
}
|
|
2518
|
+
const json = format ? JSON.stringify(obj, undefined, "\t") : JSON.stringify(obj);
|
|
2519
|
+
return Converter.utf8ToBytes(json);
|
|
2277
2520
|
}
|
|
2278
2521
|
/**
|
|
2279
|
-
*
|
|
2280
|
-
* @param
|
|
2281
|
-
* @
|
|
2282
|
-
* @
|
|
2522
|
+
* Convert a bytes to an object.
|
|
2523
|
+
* @param bytes The bytes to convert to an object.
|
|
2524
|
+
* @returns The object.
|
|
2525
|
+
* @throws GeneralError if there was an error parsing the JSON.
|
|
2283
2526
|
*/
|
|
2284
|
-
static
|
|
2285
|
-
if (Is.
|
|
2286
|
-
return
|
|
2287
|
-
}
|
|
2288
|
-
if (Is.date(value)) {
|
|
2289
|
-
return value;
|
|
2527
|
+
static fromBytes(bytes) {
|
|
2528
|
+
if (Is.empty(bytes) || bytes.length === 0) {
|
|
2529
|
+
return undefined;
|
|
2290
2530
|
}
|
|
2291
|
-
|
|
2292
|
-
|
|
2531
|
+
try {
|
|
2532
|
+
const utf8 = Converter.bytesToUtf8(bytes);
|
|
2533
|
+
return JSON.parse(utf8);
|
|
2293
2534
|
}
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
if (!Number.isNaN(dt.getTime())) {
|
|
2297
|
-
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate(), dt.getUTCHours(), dt.getUTCMinutes(), dt.getUTCSeconds(), dt.getUTCMilliseconds());
|
|
2298
|
-
return new Date(utc);
|
|
2299
|
-
}
|
|
2535
|
+
catch (err) {
|
|
2536
|
+
throw new GeneralError(ObjectHelper._CLASS_NAME, "failedBytesToJSON", undefined, err);
|
|
2300
2537
|
}
|
|
2301
2538
|
}
|
|
2302
2539
|
/**
|
|
2303
|
-
*
|
|
2304
|
-
* @param
|
|
2305
|
-
* @
|
|
2306
|
-
* @returns The value if it can be coerced.
|
|
2540
|
+
* Make a deep clone of an object.
|
|
2541
|
+
* @param obj The object to clone.
|
|
2542
|
+
* @returns The objects clone.
|
|
2307
2543
|
*/
|
|
2308
|
-
static
|
|
2309
|
-
if (Is.undefined(
|
|
2310
|
-
return
|
|
2544
|
+
static clone(obj) {
|
|
2545
|
+
if (Is.undefined(obj)) {
|
|
2546
|
+
return undefined;
|
|
2311
2547
|
}
|
|
2312
|
-
|
|
2313
|
-
|
|
2548
|
+
return structuredClone(obj);
|
|
2549
|
+
}
|
|
2550
|
+
/**
|
|
2551
|
+
* Deep merge objects.
|
|
2552
|
+
* @param obj1 The first object to merge.
|
|
2553
|
+
* @param obj2 The second object to merge.
|
|
2554
|
+
* @returns The combined deep merge of the objects.
|
|
2555
|
+
*/
|
|
2556
|
+
static merge(obj1, obj2) {
|
|
2557
|
+
if (Is.empty(obj1)) {
|
|
2558
|
+
return ObjectHelper.clone(obj2);
|
|
2314
2559
|
}
|
|
2315
|
-
if (Is.
|
|
2316
|
-
|
|
2317
|
-
dt.setFullYear(1970, 0, 1);
|
|
2318
|
-
return dt;
|
|
2560
|
+
if (Is.empty(obj2)) {
|
|
2561
|
+
return ObjectHelper.clone(obj1);
|
|
2319
2562
|
}
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2563
|
+
const obj1Clone = ObjectHelper.clone(obj1);
|
|
2564
|
+
if (Is.object(obj1Clone) && Is.object(obj2)) {
|
|
2565
|
+
const keys = Object.keys(obj2);
|
|
2566
|
+
for (const key of keys) {
|
|
2567
|
+
if (Is.object(obj1Clone[key]) && Is.object(obj2[key])) {
|
|
2568
|
+
ObjectHelper.propertySet(obj1Clone, key, ObjectHelper.merge(obj1Clone[key], obj2[key]));
|
|
2569
|
+
}
|
|
2570
|
+
else {
|
|
2571
|
+
ObjectHelper.propertySet(obj1Clone, key, obj2[key]);
|
|
2572
|
+
}
|
|
2325
2573
|
}
|
|
2326
2574
|
}
|
|
2575
|
+
return obj1Clone;
|
|
2327
2576
|
}
|
|
2328
2577
|
/**
|
|
2329
|
-
*
|
|
2330
|
-
* @param
|
|
2331
|
-
* @
|
|
2332
|
-
* @
|
|
2578
|
+
* Does one object equal another.
|
|
2579
|
+
* @param obj1 The first object to compare.
|
|
2580
|
+
* @param obj2 The second object to compare.
|
|
2581
|
+
* @param strictPropertyOrder Should the properties be in the same order, defaults to true.
|
|
2582
|
+
* @returns True is the objects are equal.
|
|
2333
2583
|
*/
|
|
2334
|
-
static
|
|
2335
|
-
if (
|
|
2336
|
-
return
|
|
2337
|
-
}
|
|
2338
|
-
if (Is.object(value)) {
|
|
2339
|
-
return value;
|
|
2584
|
+
static equal(obj1, obj2, strictPropertyOrder) {
|
|
2585
|
+
if (strictPropertyOrder ?? true) {
|
|
2586
|
+
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
2340
2587
|
}
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2588
|
+
return JsonHelper.canonicalize(obj1) === JsonHelper.canonicalize(obj2);
|
|
2589
|
+
}
|
|
2590
|
+
/**
|
|
2591
|
+
* Get the property of an unknown object.
|
|
2592
|
+
* @param obj The object to get the property from.
|
|
2593
|
+
* @param property The property to get, can be separated by dots for nested path.
|
|
2594
|
+
* @returns The property.
|
|
2595
|
+
*/
|
|
2596
|
+
static propertyGet(obj, property) {
|
|
2597
|
+
const pathParts = property.split(".");
|
|
2598
|
+
let pathValue = obj;
|
|
2599
|
+
for (const pathPart of pathParts) {
|
|
2600
|
+
// Is the path part numeric i.e. an array index.
|
|
2601
|
+
const arrayMatch = /^(\d+)$/.exec(pathPart);
|
|
2602
|
+
if (arrayMatch) {
|
|
2603
|
+
const arrayIndex = Number.parseInt(arrayMatch[1], 10);
|
|
2604
|
+
if (Is.arrayValue(pathValue) && arrayIndex < pathValue.length) {
|
|
2605
|
+
// There is no prop name so this is a direct array index on the current object
|
|
2606
|
+
pathValue = pathValue[arrayIndex];
|
|
2607
|
+
}
|
|
2608
|
+
else {
|
|
2609
|
+
// Array index for non array object so return
|
|
2610
|
+
return undefined;
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
else if (Is.object(pathValue)) {
|
|
2614
|
+
// No array part in path so assume object sub property
|
|
2615
|
+
pathValue = pathValue[pathPart];
|
|
2616
|
+
}
|
|
2617
|
+
else {
|
|
2618
|
+
return undefined;
|
|
2344
2619
|
}
|
|
2345
|
-
catch { }
|
|
2346
2620
|
}
|
|
2621
|
+
return pathValue;
|
|
2347
2622
|
}
|
|
2348
|
-
}
|
|
2349
|
-
|
|
2350
|
-
// Copyright 2024 IOTA Stiftung.
|
|
2351
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
2352
|
-
/**
|
|
2353
|
-
* Class to help with filenames.
|
|
2354
|
-
*/
|
|
2355
|
-
class FilenameHelper {
|
|
2356
2623
|
/**
|
|
2357
|
-
*
|
|
2358
|
-
* @param
|
|
2359
|
-
* @
|
|
2624
|
+
* Set the property of an unknown object.
|
|
2625
|
+
* @param obj The object to set the property from.
|
|
2626
|
+
* @param property The property to set.
|
|
2627
|
+
* @param value The value to set.
|
|
2628
|
+
* @throws GeneralError if the property target is not an object.
|
|
2360
2629
|
*/
|
|
2361
|
-
static
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2630
|
+
static propertySet(obj, property, value) {
|
|
2631
|
+
const pathParts = property.split(".");
|
|
2632
|
+
let pathValue = obj;
|
|
2633
|
+
let parentObj;
|
|
2634
|
+
for (let i = 0; i < pathParts.length; i++) {
|
|
2635
|
+
const pathPart = pathParts[i];
|
|
2636
|
+
// Is the path part numeric i.e. an array index.
|
|
2637
|
+
const arrayMatch = /^(\d+)$/.exec(pathPart);
|
|
2638
|
+
const arrayIndex = arrayMatch ? Number.parseInt(arrayMatch[1], 10) : -1;
|
|
2639
|
+
if (i === pathParts.length - 1) {
|
|
2640
|
+
// Last part of path so set the value
|
|
2641
|
+
if (arrayIndex >= 0) {
|
|
2642
|
+
if (Is.array(pathValue)) {
|
|
2643
|
+
pathValue[arrayIndex] = value;
|
|
2644
|
+
}
|
|
2645
|
+
else {
|
|
2646
|
+
throw new GeneralError(ObjectHelper._CLASS_NAME, "cannotSetArrayIndex", {
|
|
2647
|
+
property,
|
|
2648
|
+
index: arrayIndex
|
|
2649
|
+
});
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
else if (Is.object(pathValue)) {
|
|
2653
|
+
pathValue[pathPart] = value;
|
|
2654
|
+
}
|
|
2655
|
+
else {
|
|
2656
|
+
throw new GeneralError(ObjectHelper._CLASS_NAME, "cannotSetProperty", { property });
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
else {
|
|
2660
|
+
parentObj = pathValue;
|
|
2661
|
+
if (Is.object(pathValue)) {
|
|
2662
|
+
pathValue = pathValue[pathPart];
|
|
2663
|
+
}
|
|
2664
|
+
else if (Is.array(pathValue)) {
|
|
2665
|
+
pathValue = pathValue[arrayIndex];
|
|
2666
|
+
}
|
|
2667
|
+
if (Is.empty(pathValue)) {
|
|
2668
|
+
const nextArrayMatch = /^(\d+)$/.exec(pathParts[i + 1]);
|
|
2669
|
+
const nextArrayIndex = nextArrayMatch ? Number.parseInt(nextArrayMatch[1], 10) : -1;
|
|
2670
|
+
if (nextArrayIndex >= 0) {
|
|
2671
|
+
pathValue = [];
|
|
2672
|
+
}
|
|
2673
|
+
else {
|
|
2674
|
+
pathValue = {};
|
|
2675
|
+
}
|
|
2676
|
+
if (Is.object(parentObj)) {
|
|
2677
|
+
parentObj[pathPart] = pathValue;
|
|
2678
|
+
}
|
|
2679
|
+
else if (Is.array(parentObj)) {
|
|
2680
|
+
parentObj[arrayIndex] = pathValue;
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2365
2684
|
}
|
|
2366
|
-
// Common non filename characters
|
|
2367
|
-
safe = safe.replace(/["*/:<>?\\|]/g, "_");
|
|
2368
|
-
// Windows non filename characters
|
|
2369
|
-
safe = safe.replace(/^(con|prn|aux|nul|com\d|lpt\d)$/i, "_");
|
|
2370
|
-
// Control characters
|
|
2371
|
-
safe = safe.replace(/[\u0000-\u001F\u0080-\u009F]/g, "_");
|
|
2372
|
-
// Relative paths
|
|
2373
|
-
safe = safe.replace(/^\.+/, "_");
|
|
2374
|
-
// Trailing periods
|
|
2375
|
-
safe = safe.replace(/\.+$/, "");
|
|
2376
|
-
return safe;
|
|
2377
2685
|
}
|
|
2378
|
-
}
|
|
2379
|
-
|
|
2380
|
-
// Copyright 2024 IOTA Stiftung.
|
|
2381
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
2382
|
-
/**
|
|
2383
|
-
* Helpers methods for JSON objects.
|
|
2384
|
-
*/
|
|
2385
|
-
class JsonHelper {
|
|
2386
2686
|
/**
|
|
2387
|
-
*
|
|
2388
|
-
*
|
|
2389
|
-
* @param
|
|
2390
|
-
* @returns The serialized object.
|
|
2687
|
+
* Delete the property of an unknown object.
|
|
2688
|
+
* @param obj The object to set the property from.
|
|
2689
|
+
* @param property The property to set
|
|
2391
2690
|
*/
|
|
2392
|
-
static
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
typeof object !== "object" ||
|
|
2396
|
-
("toJSON" in object && object.toJSON instanceof Function)) {
|
|
2397
|
-
// Primitive data type
|
|
2398
|
-
buffer.push(JSON.stringify(object));
|
|
2691
|
+
static propertyDelete(obj, property) {
|
|
2692
|
+
if (Is.object(obj)) {
|
|
2693
|
+
delete obj[property];
|
|
2399
2694
|
}
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2695
|
+
}
|
|
2696
|
+
/**
|
|
2697
|
+
* Extract a property from the object, providing alternative names.
|
|
2698
|
+
* @param obj The object to extract from.
|
|
2699
|
+
* @param propertyNames The possible names for the property.
|
|
2700
|
+
* @param removeProperties Remove the properties from the object, defaults to true.
|
|
2701
|
+
* @returns The property if available.
|
|
2702
|
+
*/
|
|
2703
|
+
static extractProperty(obj, propertyNames, removeProperties = true) {
|
|
2704
|
+
let retVal;
|
|
2705
|
+
if (Is.object(obj)) {
|
|
2706
|
+
const names = Is.string(propertyNames) ? [propertyNames] : propertyNames;
|
|
2707
|
+
for (const prop of names) {
|
|
2708
|
+
retVal ??= ObjectHelper.propertyGet(obj, prop);
|
|
2709
|
+
if (removeProperties) {
|
|
2710
|
+
ObjectHelper.propertyDelete(obj, prop);
|
|
2409
2711
|
}
|
|
2410
2712
|
}
|
|
2411
|
-
buffer.push(`[${parts.join(",")}]`);
|
|
2412
2713
|
}
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2714
|
+
return retVal;
|
|
2715
|
+
}
|
|
2716
|
+
/**
|
|
2717
|
+
* Pick a subset of properties from an object.
|
|
2718
|
+
* @param obj The object to pick the properties from.
|
|
2719
|
+
* @param keys The property keys to pick.
|
|
2720
|
+
* @returns The partial object.
|
|
2721
|
+
*/
|
|
2722
|
+
static pick(obj, keys) {
|
|
2723
|
+
if (Is.object(obj) && Is.arrayValue(keys)) {
|
|
2724
|
+
const result = {};
|
|
2418
2725
|
for (const key of keys) {
|
|
2419
|
-
|
|
2420
|
-
props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
|
|
2421
|
-
}
|
|
2726
|
+
result[key] = obj[key];
|
|
2422
2727
|
}
|
|
2423
|
-
|
|
2728
|
+
return result;
|
|
2424
2729
|
}
|
|
2425
|
-
return
|
|
2730
|
+
return obj;
|
|
2426
2731
|
}
|
|
2427
2732
|
/**
|
|
2428
|
-
*
|
|
2429
|
-
*
|
|
2430
|
-
* @param
|
|
2431
|
-
* @
|
|
2432
|
-
* @returns The list of patches.
|
|
2733
|
+
* Omit a subset of properties from an object.
|
|
2734
|
+
* @param obj The object to omit the properties from.
|
|
2735
|
+
* @param keys The property keys to omit.
|
|
2736
|
+
* @returns The partial object.
|
|
2433
2737
|
*/
|
|
2434
|
-
static
|
|
2435
|
-
|
|
2436
|
-
|
|
2738
|
+
static omit(obj, keys) {
|
|
2739
|
+
if (Is.object(obj) && Is.arrayValue(keys)) {
|
|
2740
|
+
const result = { ...obj };
|
|
2741
|
+
for (const key of keys) {
|
|
2742
|
+
delete result[key];
|
|
2743
|
+
}
|
|
2744
|
+
return result;
|
|
2745
|
+
}
|
|
2746
|
+
return obj;
|
|
2437
2747
|
}
|
|
2438
2748
|
/**
|
|
2439
|
-
*
|
|
2440
|
-
*
|
|
2441
|
-
* @
|
|
2442
|
-
* @param patches The second object.
|
|
2443
|
-
* @returns The updated object.
|
|
2749
|
+
* Converter the non JSON primitives to extended types.
|
|
2750
|
+
* @param obj The object to convert.
|
|
2751
|
+
* @returns The object with extended properties.
|
|
2444
2752
|
*/
|
|
2445
|
-
|
|
2446
|
-
|
|
2753
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2754
|
+
static toExtended(obj) {
|
|
2755
|
+
const jsonExtended = JsonHelper.stringifyEx(obj);
|
|
2756
|
+
return JSON.parse(jsonExtended);
|
|
2757
|
+
}
|
|
2758
|
+
/**
|
|
2759
|
+
* Converter the extended types to non JSON primitives.
|
|
2760
|
+
* @param obj The object to convert.
|
|
2761
|
+
* @returns The object with regular properties.
|
|
2762
|
+
*/
|
|
2763
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2764
|
+
static fromExtended(obj) {
|
|
2765
|
+
const jsonExtended = JsonHelper.stringifyEx(obj);
|
|
2766
|
+
return JsonHelper.parseEx(jsonExtended);
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Remove empty properties from an object.
|
|
2770
|
+
* @param obj The object to remove the empty properties from.
|
|
2771
|
+
* @param options The options for the removal.
|
|
2772
|
+
* @param options.removeUndefined Remove undefined properties, defaults to true.
|
|
2773
|
+
* @param options.removeNull Remove null properties, defaults to false.
|
|
2774
|
+
* @returns The object with empty properties removed.
|
|
2775
|
+
*/
|
|
2776
|
+
static removeEmptyProperties(obj, options) {
|
|
2777
|
+
if (Is.object(obj)) {
|
|
2778
|
+
const removeUndefined = options?.removeUndefined ?? true;
|
|
2779
|
+
const removeNull = options?.removeNull ?? false;
|
|
2780
|
+
const newObj = {};
|
|
2781
|
+
const keys = Object.keys(obj);
|
|
2782
|
+
for (const key of keys) {
|
|
2783
|
+
if (!((removeUndefined && Is.undefined(obj[key])) || (removeNull && Is.null(obj[key])))) {
|
|
2784
|
+
newObj[key] = ObjectHelper.removeEmptyProperties(obj[key], options);
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
return newObj;
|
|
2788
|
+
}
|
|
2789
|
+
else if (Is.array(obj)) {
|
|
2790
|
+
const arr = [];
|
|
2791
|
+
for (const element of obj) {
|
|
2792
|
+
arr.push(ObjectHelper.removeEmptyProperties(element, options));
|
|
2793
|
+
}
|
|
2794
|
+
return arr;
|
|
2795
|
+
}
|
|
2796
|
+
return obj;
|
|
2447
2797
|
}
|
|
2448
2798
|
}
|
|
2449
2799
|
|
|
2450
2800
|
// Copyright 2024 IOTA Stiftung.
|
|
2451
2801
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2452
|
-
/* eslint-disable no-bitwise */
|
|
2453
2802
|
/**
|
|
2454
|
-
*
|
|
2803
|
+
* Environment variable helper.
|
|
2455
2804
|
*/
|
|
2456
|
-
class
|
|
2805
|
+
class EnvHelper {
|
|
2806
|
+
/**
|
|
2807
|
+
* Get the environment variable as an object with camel cased names.
|
|
2808
|
+
* @param envVars The environment variables.
|
|
2809
|
+
* @param prefix The prefix of the environment variables, if not provided gets all.
|
|
2810
|
+
* @returns The object with camel cased names.
|
|
2811
|
+
*/
|
|
2812
|
+
static envToJson(envVars, prefix) {
|
|
2813
|
+
const result = {};
|
|
2814
|
+
if (!Is.empty(envVars)) {
|
|
2815
|
+
if (Is.empty(prefix)) {
|
|
2816
|
+
for (const envVar in envVars) {
|
|
2817
|
+
if (Is.stringValue(envVars[envVar])) {
|
|
2818
|
+
const camelCaseName = StringHelper.camelCase(envVar.toLowerCase());
|
|
2819
|
+
ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
else {
|
|
2824
|
+
for (const envVar in envVars) {
|
|
2825
|
+
if (envVar.startsWith(prefix) && Is.stringValue(envVars[envVar])) {
|
|
2826
|
+
const camelCaseName = StringHelper.camelCase(envVar.replace(prefix, "").toLowerCase());
|
|
2827
|
+
ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
|
|
2828
|
+
}
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
return result;
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2837
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2838
|
+
/**
|
|
2839
|
+
* Class to perform internationalization.
|
|
2840
|
+
*/
|
|
2841
|
+
class I18n {
|
|
2457
2842
|
/**
|
|
2458
|
-
*
|
|
2843
|
+
* The default translation.
|
|
2844
|
+
*/
|
|
2845
|
+
static DEFAULT_LOCALE = "en";
|
|
2846
|
+
/**
|
|
2847
|
+
* Dictionaries for lookups.
|
|
2459
2848
|
* @internal
|
|
2460
2849
|
*/
|
|
2461
|
-
static
|
|
2850
|
+
static _localeDictionaries = {};
|
|
2462
2851
|
/**
|
|
2463
|
-
*
|
|
2852
|
+
* The current locale.
|
|
2464
2853
|
* @internal
|
|
2465
2854
|
*/
|
|
2466
|
-
static
|
|
2855
|
+
static _currentLocale = I18n.DEFAULT_LOCALE;
|
|
2467
2856
|
/**
|
|
2468
|
-
*
|
|
2469
|
-
* @
|
|
2470
|
-
* @param startIndex The index to start in the bytes.
|
|
2471
|
-
* @param length The length of bytes to read.
|
|
2472
|
-
* @returns The array formatted as UTF8.
|
|
2857
|
+
* Change handler for the locale being updated.
|
|
2858
|
+
* @internal
|
|
2473
2859
|
*/
|
|
2474
|
-
static
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
str += String.fromCharCode(((value & 0x0f) << 12) | ((array[i + 1] & 0x3f) << 6) | (array[i + 2] & 0x3f));
|
|
2489
|
-
i += 2;
|
|
2490
|
-
}
|
|
2491
|
-
else {
|
|
2492
|
-
// surrogate pair
|
|
2493
|
-
const charCode = (((value & 0x07) << 18) |
|
|
2494
|
-
((array[i + 1] & 0x3f) << 12) |
|
|
2495
|
-
((array[i + 2] & 0x3f) << 6) |
|
|
2496
|
-
(array[i + 3] & 0x3f)) -
|
|
2497
|
-
0x010000;
|
|
2498
|
-
str += String.fromCharCode((charCode >> 10) | 0xd800, (charCode & 0x03ff) | 0xdc00);
|
|
2499
|
-
i += 3;
|
|
2500
|
-
}
|
|
2860
|
+
static _localeChangedHandlers = {};
|
|
2861
|
+
/**
|
|
2862
|
+
* Change handler for the dictionaries being updated.
|
|
2863
|
+
* @internal
|
|
2864
|
+
*/
|
|
2865
|
+
static _dictionaryChangedHandlers = {};
|
|
2866
|
+
/**
|
|
2867
|
+
* Set the locale.
|
|
2868
|
+
* @param locale The new locale.
|
|
2869
|
+
*/
|
|
2870
|
+
static setLocale(locale) {
|
|
2871
|
+
I18n._currentLocale = locale;
|
|
2872
|
+
for (const callback in I18n._localeChangedHandlers) {
|
|
2873
|
+
I18n._localeChangedHandlers[callback](I18n._currentLocale);
|
|
2501
2874
|
}
|
|
2502
|
-
return str;
|
|
2503
2875
|
}
|
|
2504
2876
|
/**
|
|
2505
|
-
*
|
|
2506
|
-
* @
|
|
2507
|
-
* @returns The array.
|
|
2877
|
+
* Get the locale.
|
|
2878
|
+
* @returns The current locale.
|
|
2508
2879
|
*/
|
|
2509
|
-
static
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2880
|
+
static getLocale() {
|
|
2881
|
+
return I18n._currentLocale;
|
|
2882
|
+
}
|
|
2883
|
+
/**
|
|
2884
|
+
* Add a locale dictionary.
|
|
2885
|
+
* @param locale The locale.
|
|
2886
|
+
* @param dictionary The dictionary to add.
|
|
2887
|
+
*/
|
|
2888
|
+
static addDictionary(locale, dictionary) {
|
|
2889
|
+
const mergedKeys = {};
|
|
2890
|
+
I18n.flattenTranslationKeys(dictionary, "", mergedKeys);
|
|
2891
|
+
I18n._localeDictionaries[locale] = mergedKeys;
|
|
2892
|
+
for (const callback in I18n._dictionaryChangedHandlers) {
|
|
2893
|
+
I18n._dictionaryChangedHandlers[callback](I18n._currentLocale);
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
/**
|
|
2897
|
+
* Get a locale dictionary.
|
|
2898
|
+
* @param locale The locale.
|
|
2899
|
+
* @returns The dictionary of undefined if it does not exist.
|
|
2900
|
+
*/
|
|
2901
|
+
static getDictionary(locale) {
|
|
2902
|
+
return I18n._localeDictionaries[locale];
|
|
2903
|
+
}
|
|
2904
|
+
/**
|
|
2905
|
+
* Get all the locale dictionaries.
|
|
2906
|
+
* @returns The dictionaries.
|
|
2907
|
+
*/
|
|
2908
|
+
static getAllDictionaries() {
|
|
2909
|
+
return I18n._localeDictionaries;
|
|
2910
|
+
}
|
|
2911
|
+
/**
|
|
2912
|
+
* Add a locale changed handler.
|
|
2913
|
+
* @param id The id of the handler.
|
|
2914
|
+
* @param handler The handler to add.
|
|
2915
|
+
*/
|
|
2916
|
+
static addLocaleHandler(id, handler) {
|
|
2917
|
+
I18n._localeChangedHandlers[id] = handler;
|
|
2918
|
+
}
|
|
2919
|
+
/**
|
|
2920
|
+
* Remove a locale changed handler.
|
|
2921
|
+
* @param id The id of the handler.
|
|
2922
|
+
*/
|
|
2923
|
+
static removeLocaleHandler(id) {
|
|
2924
|
+
delete I18n._localeChangedHandlers[id];
|
|
2925
|
+
}
|
|
2926
|
+
/**
|
|
2927
|
+
* Add a dictionary changed handler.
|
|
2928
|
+
* @param id The id of the handler.
|
|
2929
|
+
* @param handler The handler to add.
|
|
2930
|
+
*/
|
|
2931
|
+
static addDictionaryHandler(id, handler) {
|
|
2932
|
+
I18n._dictionaryChangedHandlers[id] = handler;
|
|
2933
|
+
}
|
|
2934
|
+
/**
|
|
2935
|
+
* Remove a dictionary changed handler.
|
|
2936
|
+
* @param id The id of the handler.
|
|
2937
|
+
*/
|
|
2938
|
+
static removeDictionaryHandler(id) {
|
|
2939
|
+
delete I18n._dictionaryChangedHandlers[id];
|
|
2940
|
+
}
|
|
2941
|
+
/**
|
|
2942
|
+
* Format a message.
|
|
2943
|
+
* @param key The key of the message to format.
|
|
2944
|
+
* @param values The values to substitute into the message.
|
|
2945
|
+
* @param overrideLocale Override the locale.
|
|
2946
|
+
* @returns The formatted string.
|
|
2947
|
+
*/
|
|
2948
|
+
static formatMessage(key, values, overrideLocale) {
|
|
2949
|
+
let cl = overrideLocale ?? I18n._currentLocale;
|
|
2950
|
+
if (cl.startsWith("debug-")) {
|
|
2951
|
+
cl = I18n.DEFAULT_LOCALE;
|
|
2952
|
+
}
|
|
2953
|
+
if (!I18n._localeDictionaries[cl]) {
|
|
2954
|
+
return `!!Missing ${cl}`;
|
|
2955
|
+
}
|
|
2956
|
+
if (!I18n._localeDictionaries[cl][key]) {
|
|
2957
|
+
return `!!Missing ${cl}.${key}`;
|
|
2958
|
+
}
|
|
2959
|
+
if (I18n._currentLocale === "debug-k") {
|
|
2960
|
+
return key;
|
|
2961
|
+
}
|
|
2962
|
+
let ret = new IntlMessageFormat(I18n._localeDictionaries[cl][key], cl).format(values);
|
|
2963
|
+
if (I18n._currentLocale === "debug-x") {
|
|
2964
|
+
ret = ret.replace(/[a-z]/g, "x").replace(/[A-Z]/g, "x").replace(/\d/g, "n");
|
|
2965
|
+
}
|
|
2966
|
+
return ret;
|
|
2967
|
+
}
|
|
2968
|
+
/**
|
|
2969
|
+
* Check if the dictionaries have a message for the given key.
|
|
2970
|
+
* @param key The key to check for existence.
|
|
2971
|
+
* @returns True if the key exists.
|
|
2972
|
+
*/
|
|
2973
|
+
static hasMessage(key) {
|
|
2974
|
+
return Is.string(I18n._localeDictionaries[I18n._currentLocale]?.[key]);
|
|
2975
|
+
}
|
|
2976
|
+
/**
|
|
2977
|
+
* Flatten the translation property paths for faster lookup.
|
|
2978
|
+
* @param translation The translation to merge.
|
|
2979
|
+
* @param propertyPath The current root path.
|
|
2980
|
+
* @param mergedKeys The merged keys dictionary to populate.
|
|
2981
|
+
* @internal
|
|
2982
|
+
*/
|
|
2983
|
+
static flattenTranslationKeys(translation, propertyPath, mergedKeys) {
|
|
2984
|
+
for (const key in translation) {
|
|
2985
|
+
const val = translation[key];
|
|
2986
|
+
const mergedPath = propertyPath.length > 0 ? `${propertyPath}.${key}` : key;
|
|
2987
|
+
if (Is.string(val)) {
|
|
2988
|
+
mergedKeys[mergedPath] = val;
|
|
2518
2989
|
}
|
|
2519
|
-
else if (
|
|
2520
|
-
|
|
2990
|
+
else if (Is.object(val)) {
|
|
2991
|
+
I18n.flattenTranslationKeys(val, mergedPath, mergedKeys);
|
|
2521
2992
|
}
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
|
|
2997
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2998
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2999
|
+
/**
|
|
3000
|
+
* Error helper functions.
|
|
3001
|
+
*/
|
|
3002
|
+
class ErrorHelper {
|
|
3003
|
+
/**
|
|
3004
|
+
* Format Errors and returns just their messages.
|
|
3005
|
+
* @param error The error to format.
|
|
3006
|
+
* @returns The error formatted including any inner errors.
|
|
3007
|
+
*/
|
|
3008
|
+
static formatErrors(error) {
|
|
3009
|
+
return ErrorHelper.localizeErrors(error).map(e => e.message);
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Localize the content of an error and any inner errors.
|
|
3013
|
+
* @param error The error to format.
|
|
3014
|
+
* @returns The localized version of the errors flattened.
|
|
3015
|
+
*/
|
|
3016
|
+
static localizeErrors(error) {
|
|
3017
|
+
const formattedErrors = [];
|
|
3018
|
+
if (Is.notEmpty(error)) {
|
|
3019
|
+
const errors = BaseError.flatten(error);
|
|
3020
|
+
for (const err of errors) {
|
|
3021
|
+
const errorNameKey = `errorNames.${StringHelper.camelCase(err.name)}`;
|
|
3022
|
+
const errorMessageKey = `error.${err.message}`;
|
|
3023
|
+
// If there is no error message then it is probably
|
|
3024
|
+
// from a 3rd party lib, so don't format it just display
|
|
3025
|
+
const hasErrorName = I18n.hasMessage(errorNameKey);
|
|
3026
|
+
const hasErrorMessage = I18n.hasMessage(errorMessageKey);
|
|
3027
|
+
const localizedError = {
|
|
3028
|
+
name: I18n.formatMessage(hasErrorName ? errorNameKey : "errorNames.error"),
|
|
3029
|
+
message: hasErrorMessage
|
|
3030
|
+
? I18n.formatMessage(errorMessageKey, err.properties)
|
|
3031
|
+
: err.message
|
|
3032
|
+
};
|
|
3033
|
+
if (Is.stringValue(err.source)) {
|
|
3034
|
+
localizedError.source = err.source;
|
|
3035
|
+
}
|
|
3036
|
+
if (Is.stringValue(err.stack)) {
|
|
3037
|
+
// Remove the first line from the stack traces as they
|
|
3038
|
+
// just have the error type and message duplicated
|
|
3039
|
+
const lines = err.stack.split("\n");
|
|
3040
|
+
lines.shift();
|
|
3041
|
+
localizedError.stack = lines.join("\n");
|
|
3042
|
+
}
|
|
3043
|
+
const additional = ErrorHelper.formatValidationErrors(err);
|
|
3044
|
+
if (Is.stringValue(additional)) {
|
|
3045
|
+
localizedError.additional = additional;
|
|
3046
|
+
}
|
|
3047
|
+
formattedErrors.push(localizedError);
|
|
2530
3048
|
}
|
|
2531
3049
|
}
|
|
2532
|
-
return
|
|
3050
|
+
return formattedErrors;
|
|
2533
3051
|
}
|
|
2534
3052
|
/**
|
|
2535
|
-
*
|
|
2536
|
-
* @param
|
|
2537
|
-
* @
|
|
2538
|
-
* @param startIndex The index to start in the bytes.
|
|
2539
|
-
* @param length The length of bytes to read.
|
|
2540
|
-
* @param reverse Reverse the combine direction.
|
|
2541
|
-
* @returns The array formatted as hex.
|
|
3053
|
+
* Localize the content of an error and any inner errors.
|
|
3054
|
+
* @param error The error to format.
|
|
3055
|
+
* @returns The localized version of the errors flattened.
|
|
2542
3056
|
*/
|
|
2543
|
-
static
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
const
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
3057
|
+
static formatValidationErrors(error) {
|
|
3058
|
+
if (Is.object(error.properties) &&
|
|
3059
|
+
Object.keys(error.properties).length > 0 &&
|
|
3060
|
+
Is.object(error.properties) &&
|
|
3061
|
+
Is.arrayValue(error.properties.validationFailures)) {
|
|
3062
|
+
const validationErrors = [];
|
|
3063
|
+
for (const validationFailure of error.properties.validationFailures) {
|
|
3064
|
+
const errorI18n = `error.${validationFailure.reason}`;
|
|
3065
|
+
const errorMessage = I18n.hasMessage(errorI18n)
|
|
3066
|
+
? I18n.formatMessage(errorI18n, validationFailure.properties)
|
|
3067
|
+
: errorI18n;
|
|
3068
|
+
let v = `${validationFailure.property}: ${errorMessage}`;
|
|
3069
|
+
if (Is.object(validationFailure.properties) &&
|
|
3070
|
+
Is.notEmpty(validationFailure.properties.value)) {
|
|
3071
|
+
v += ` = ${JSON.stringify(validationFailure.properties.value)}`;
|
|
2557
3072
|
}
|
|
3073
|
+
validationErrors.push(v);
|
|
2558
3074
|
}
|
|
3075
|
+
return validationErrors.join("\n");
|
|
2559
3076
|
}
|
|
2560
|
-
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
2561
3077
|
}
|
|
3078
|
+
}
|
|
3079
|
+
|
|
3080
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3081
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3082
|
+
/**
|
|
3083
|
+
* The types the extracted data can be coerced to.
|
|
3084
|
+
*/
|
|
3085
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3086
|
+
const CoerceType = {
|
|
2562
3087
|
/**
|
|
2563
|
-
*
|
|
2564
|
-
* @param hex The hex to decode.
|
|
2565
|
-
* @param reverse Store the characters in reverse.
|
|
2566
|
-
* @returns The array.
|
|
3088
|
+
* String.
|
|
2567
3089
|
*/
|
|
2568
|
-
|
|
2569
|
-
const strippedHex = HexHelper.stripPrefix(hex);
|
|
2570
|
-
const sizeof = strippedHex.length >> 1;
|
|
2571
|
-
const length = sizeof << 1;
|
|
2572
|
-
const array = new Uint8Array(sizeof);
|
|
2573
|
-
this.buildHexLookups();
|
|
2574
|
-
if (Converter._DECODE_LOOKUP) {
|
|
2575
|
-
let i = 0;
|
|
2576
|
-
let n = 0;
|
|
2577
|
-
while (i < length) {
|
|
2578
|
-
array[n++] =
|
|
2579
|
-
(Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)] << 4) |
|
|
2580
|
-
Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)];
|
|
2581
|
-
}
|
|
2582
|
-
if (reverse) {
|
|
2583
|
-
array.reverse();
|
|
2584
|
-
}
|
|
2585
|
-
}
|
|
2586
|
-
return array;
|
|
2587
|
-
}
|
|
3090
|
+
String: "string",
|
|
2588
3091
|
/**
|
|
2589
|
-
*
|
|
2590
|
-
* @param utf8 The text to convert.
|
|
2591
|
-
* @param includePrefix Include the 0x prefix on the returned hex.
|
|
2592
|
-
* @returns The hex version of the bytes.
|
|
3092
|
+
* Number.
|
|
2593
3093
|
*/
|
|
2594
|
-
|
|
2595
|
-
const hex = Converter.bytesToHex(Converter.utf8ToBytes(utf8));
|
|
2596
|
-
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
2597
|
-
}
|
|
3094
|
+
Number: "number",
|
|
2598
3095
|
/**
|
|
2599
|
-
*
|
|
2600
|
-
* @param hex The hex to convert.
|
|
2601
|
-
* @returns The UTF8 version of the bytes.
|
|
3096
|
+
* Integer.
|
|
2602
3097
|
*/
|
|
2603
|
-
|
|
2604
|
-
return Converter.bytesToUtf8(Converter.hexToBytes(HexHelper.stripPrefix(hex)));
|
|
2605
|
-
}
|
|
3098
|
+
Integer: "integer",
|
|
2606
3099
|
/**
|
|
2607
|
-
*
|
|
2608
|
-
* @param bytes The bytes to convert.
|
|
2609
|
-
* @returns A binary string of the bytes.
|
|
3100
|
+
* Boolean.
|
|
2610
3101
|
*/
|
|
2611
|
-
|
|
2612
|
-
const b = [];
|
|
2613
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
2614
|
-
b.push(bytes[i].toString(2).padStart(8, "0"));
|
|
2615
|
-
}
|
|
2616
|
-
return b.join("");
|
|
2617
|
-
}
|
|
3102
|
+
Boolean: "boolean",
|
|
2618
3103
|
/**
|
|
2619
|
-
*
|
|
2620
|
-
* @param binary The binary string.
|
|
2621
|
-
* @returns The bytes.
|
|
3104
|
+
* Big Integer.
|
|
2622
3105
|
*/
|
|
2623
|
-
|
|
2624
|
-
const bytes = new Uint8Array(Math.ceil(binary.length / 8));
|
|
2625
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
2626
|
-
bytes[i] = Number.parseInt(binary.slice(i * 8, (i + 1) * 8), 2);
|
|
2627
|
-
}
|
|
2628
|
-
return bytes;
|
|
2629
|
-
}
|
|
3106
|
+
BigInt: "bigint",
|
|
2630
3107
|
/**
|
|
2631
|
-
*
|
|
2632
|
-
* @param bytes The bytes to convert.
|
|
2633
|
-
* @returns A base64 string of the bytes.
|
|
3108
|
+
* Date.
|
|
2634
3109
|
*/
|
|
2635
|
-
|
|
2636
|
-
return Base64.encode(bytes);
|
|
2637
|
-
}
|
|
3110
|
+
Date: "date",
|
|
2638
3111
|
/**
|
|
2639
|
-
*
|
|
2640
|
-
* @param base64 The base64 string.
|
|
2641
|
-
* @returns The bytes.
|
|
3112
|
+
* Date Time.
|
|
2642
3113
|
*/
|
|
2643
|
-
|
|
2644
|
-
return Base64.decode(base64);
|
|
2645
|
-
}
|
|
3114
|
+
DateTime: "datetime",
|
|
2646
3115
|
/**
|
|
2647
|
-
*
|
|
2648
|
-
* @param bytes The bytes to convert.
|
|
2649
|
-
* @returns A base64 url string of the bytes.
|
|
3116
|
+
* Time.
|
|
2650
3117
|
*/
|
|
2651
|
-
|
|
2652
|
-
return Base64Url.encode(bytes);
|
|
2653
|
-
}
|
|
3118
|
+
Time: "time",
|
|
2654
3119
|
/**
|
|
2655
|
-
*
|
|
2656
|
-
* @param base64Url The base64 url string.
|
|
2657
|
-
* @returns The bytes.
|
|
3120
|
+
* Object.
|
|
2658
3121
|
*/
|
|
2659
|
-
|
|
2660
|
-
return Base64Url.decode(base64Url);
|
|
2661
|
-
}
|
|
3122
|
+
Object: "object",
|
|
2662
3123
|
/**
|
|
2663
|
-
*
|
|
2664
|
-
* @internal
|
|
3124
|
+
* Uint8Array.
|
|
2665
3125
|
*/
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
const alphabet = "0123456789abcdef";
|
|
2669
|
-
Converter._ENCODE_LOOKUP = [];
|
|
2670
|
-
Converter._DECODE_LOOKUP = [];
|
|
2671
|
-
for (let i = 0; i < 256; i++) {
|
|
2672
|
-
Converter._ENCODE_LOOKUP[i] = alphabet[(i >> 4) & 0xf] + alphabet[i & 0xf];
|
|
2673
|
-
if (i < 16) {
|
|
2674
|
-
if (i < 10) {
|
|
2675
|
-
Converter._DECODE_LOOKUP[0x30 + i] = i;
|
|
2676
|
-
}
|
|
2677
|
-
else {
|
|
2678
|
-
Converter._DECODE_LOOKUP[0x61 - 10 + i] = i;
|
|
2679
|
-
}
|
|
2680
|
-
}
|
|
2681
|
-
}
|
|
2682
|
-
}
|
|
2683
|
-
}
|
|
2684
|
-
}
|
|
3126
|
+
Uint8Array: "uint8array"
|
|
3127
|
+
};
|
|
2685
3128
|
|
|
3129
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3130
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2686
3131
|
/**
|
|
2687
|
-
*
|
|
3132
|
+
* Coerce an object from one type to another.
|
|
2688
3133
|
*/
|
|
2689
|
-
class
|
|
3134
|
+
class Coerce {
|
|
2690
3135
|
/**
|
|
2691
|
-
*
|
|
2692
|
-
* @
|
|
3136
|
+
* Coerce the value to a string.
|
|
3137
|
+
* @param value The value to coerce.
|
|
3138
|
+
* @throws TypeError If the value can not be coerced.
|
|
3139
|
+
* @returns The value if it can be coerced.
|
|
2693
3140
|
*/
|
|
2694
|
-
static
|
|
3141
|
+
static string(value) {
|
|
3142
|
+
if (Is.undefined(value)) {
|
|
3143
|
+
return value;
|
|
3144
|
+
}
|
|
3145
|
+
if (Is.string(value)) {
|
|
3146
|
+
return value;
|
|
3147
|
+
}
|
|
3148
|
+
if (Is.number(value)) {
|
|
3149
|
+
return value.toString();
|
|
3150
|
+
}
|
|
3151
|
+
if (Is.boolean(value)) {
|
|
3152
|
+
return value ? "true" : "false";
|
|
3153
|
+
}
|
|
3154
|
+
if (Is.date(value)) {
|
|
3155
|
+
return value.toISOString();
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
2695
3158
|
/**
|
|
2696
|
-
*
|
|
2697
|
-
* @param
|
|
2698
|
-
* @
|
|
2699
|
-
* @returns The
|
|
3159
|
+
* Coerce the value to a number.
|
|
3160
|
+
* @param value The value to coerce.
|
|
3161
|
+
* @throws TypeError If the value can not be coerced.
|
|
3162
|
+
* @returns The value if it can be coerced.
|
|
2700
3163
|
*/
|
|
2701
|
-
static
|
|
2702
|
-
if (
|
|
2703
|
-
return
|
|
3164
|
+
static number(value) {
|
|
3165
|
+
if (Is.undefined(value)) {
|
|
3166
|
+
return value;
|
|
3167
|
+
}
|
|
3168
|
+
if (Is.number(value)) {
|
|
3169
|
+
return value;
|
|
3170
|
+
}
|
|
3171
|
+
if (Is.string(value)) {
|
|
3172
|
+
const parsed = Number.parseFloat(value);
|
|
3173
|
+
if (Is.number(parsed)) {
|
|
3174
|
+
return parsed;
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
if (Is.boolean(value)) {
|
|
3178
|
+
return value ? 1 : 0;
|
|
3179
|
+
}
|
|
3180
|
+
if (Is.date(value)) {
|
|
3181
|
+
return value.getTime();
|
|
2704
3182
|
}
|
|
2705
|
-
const json = format ? JSON.stringify(obj, undefined, "\t") : JSON.stringify(obj);
|
|
2706
|
-
return Converter.utf8ToBytes(json);
|
|
2707
3183
|
}
|
|
2708
3184
|
/**
|
|
2709
|
-
*
|
|
2710
|
-
* @param
|
|
2711
|
-
* @
|
|
2712
|
-
* @
|
|
3185
|
+
* Coerce the value to an integer.
|
|
3186
|
+
* @param value The value to coerce.
|
|
3187
|
+
* @throws TypeError If the value can not be coerced.
|
|
3188
|
+
* @returns The value if it can be coerced.
|
|
2713
3189
|
*/
|
|
2714
|
-
static
|
|
2715
|
-
|
|
2716
|
-
|
|
3190
|
+
static integer(value) {
|
|
3191
|
+
const num = Coerce.number(value);
|
|
3192
|
+
if (!Is.undefined(num)) {
|
|
3193
|
+
return Math.trunc(num);
|
|
2717
3194
|
}
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
3195
|
+
}
|
|
3196
|
+
/**
|
|
3197
|
+
* Coerce the value to a bigint.
|
|
3198
|
+
* @param value The value to coerce.
|
|
3199
|
+
* @throws TypeError If the value can not be coerced.
|
|
3200
|
+
* @returns The value if it can be coerced.
|
|
3201
|
+
*/
|
|
3202
|
+
static bigint(value) {
|
|
3203
|
+
if (Is.undefined(value)) {
|
|
3204
|
+
return value;
|
|
2721
3205
|
}
|
|
2722
|
-
|
|
2723
|
-
|
|
3206
|
+
if (Is.bigint(value)) {
|
|
3207
|
+
return value;
|
|
3208
|
+
}
|
|
3209
|
+
if (Is.number(value)) {
|
|
3210
|
+
return BigInt(value);
|
|
3211
|
+
}
|
|
3212
|
+
if (Is.string(value)) {
|
|
3213
|
+
const parsed = Number.parseFloat(value);
|
|
3214
|
+
if (Is.integer(parsed)) {
|
|
3215
|
+
return BigInt(parsed);
|
|
3216
|
+
}
|
|
3217
|
+
}
|
|
3218
|
+
if (Is.boolean(value)) {
|
|
3219
|
+
return value ? 1n : 0n;
|
|
2724
3220
|
}
|
|
2725
3221
|
}
|
|
2726
3222
|
/**
|
|
2727
|
-
*
|
|
2728
|
-
* @param
|
|
2729
|
-
* @
|
|
3223
|
+
* Coerce the value to a boolean.
|
|
3224
|
+
* @param value The value to coerce.
|
|
3225
|
+
* @throws TypeError If the value can not be coerced.
|
|
3226
|
+
* @returns The value if it can be coerced.
|
|
2730
3227
|
*/
|
|
2731
|
-
static
|
|
2732
|
-
if (Is.undefined(
|
|
2733
|
-
return
|
|
3228
|
+
static boolean(value) {
|
|
3229
|
+
if (Is.undefined(value)) {
|
|
3230
|
+
return value;
|
|
3231
|
+
}
|
|
3232
|
+
if (Is.boolean(value)) {
|
|
3233
|
+
return value;
|
|
3234
|
+
}
|
|
3235
|
+
if (Is.number(value)) {
|
|
3236
|
+
// eslint-disable-next-line no-unneeded-ternary
|
|
3237
|
+
return value ? true : false;
|
|
3238
|
+
}
|
|
3239
|
+
if (Is.string(value)) {
|
|
3240
|
+
if (/true/i.test(value)) {
|
|
3241
|
+
return true;
|
|
3242
|
+
}
|
|
3243
|
+
if (/false/i.test(value)) {
|
|
3244
|
+
return false;
|
|
3245
|
+
}
|
|
2734
3246
|
}
|
|
2735
|
-
return structuredClone(obj);
|
|
2736
3247
|
}
|
|
2737
3248
|
/**
|
|
2738
|
-
*
|
|
2739
|
-
* @param
|
|
2740
|
-
* @
|
|
2741
|
-
* @returns The
|
|
3249
|
+
* Coerce the value to a date.
|
|
3250
|
+
* @param value The value to coerce.
|
|
3251
|
+
* @throws TypeError If the value can not be coerced.
|
|
3252
|
+
* @returns The value if it can be coerced.
|
|
2742
3253
|
*/
|
|
2743
|
-
static
|
|
2744
|
-
if (Is.
|
|
2745
|
-
return
|
|
3254
|
+
static date(value) {
|
|
3255
|
+
if (Is.undefined(value)) {
|
|
3256
|
+
return value;
|
|
2746
3257
|
}
|
|
2747
|
-
if (Is.
|
|
2748
|
-
return
|
|
3258
|
+
if (Is.date(value)) {
|
|
3259
|
+
return value;
|
|
2749
3260
|
}
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
ObjectHelper.propertySet(obj1Clone, key, obj2[key]);
|
|
2759
|
-
}
|
|
3261
|
+
if (Is.number(value)) {
|
|
3262
|
+
return new Date(value);
|
|
3263
|
+
}
|
|
3264
|
+
if (Is.string(value)) {
|
|
3265
|
+
const dt = new Date(value);
|
|
3266
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3267
|
+
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate());
|
|
3268
|
+
return new Date(utc);
|
|
2760
3269
|
}
|
|
2761
3270
|
}
|
|
2762
|
-
return obj1Clone;
|
|
2763
3271
|
}
|
|
2764
3272
|
/**
|
|
2765
|
-
*
|
|
2766
|
-
* @param
|
|
2767
|
-
* @
|
|
2768
|
-
* @
|
|
2769
|
-
* @returns True is the objects are equal.
|
|
3273
|
+
* Coerce the value to a date/time.
|
|
3274
|
+
* @param value The value to coerce.
|
|
3275
|
+
* @throws TypeError If the value can not be coerced.
|
|
3276
|
+
* @returns The value if it can be coerced.
|
|
2770
3277
|
*/
|
|
2771
|
-
static
|
|
2772
|
-
if (
|
|
2773
|
-
return
|
|
3278
|
+
static dateTime(value) {
|
|
3279
|
+
if (Is.undefined(value)) {
|
|
3280
|
+
return value;
|
|
3281
|
+
}
|
|
3282
|
+
if (Is.date(value)) {
|
|
3283
|
+
return value;
|
|
3284
|
+
}
|
|
3285
|
+
if (Is.number(value)) {
|
|
3286
|
+
return new Date(value);
|
|
3287
|
+
}
|
|
3288
|
+
if (Is.string(value)) {
|
|
3289
|
+
const dt = new Date(value);
|
|
3290
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3291
|
+
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate(), dt.getUTCHours(), dt.getUTCMinutes(), dt.getUTCSeconds(), dt.getUTCMilliseconds());
|
|
3292
|
+
return new Date(utc);
|
|
3293
|
+
}
|
|
2774
3294
|
}
|
|
2775
|
-
return JsonHelper.canonicalize(obj1) === JsonHelper.canonicalize(obj2);
|
|
2776
3295
|
}
|
|
2777
3296
|
/**
|
|
2778
|
-
*
|
|
2779
|
-
* @param
|
|
2780
|
-
* @
|
|
2781
|
-
* @returns The
|
|
3297
|
+
* Coerce the value to a time.
|
|
3298
|
+
* @param value The value to coerce.
|
|
3299
|
+
* @throws TypeError If the value can not be coerced.
|
|
3300
|
+
* @returns The value if it can be coerced.
|
|
2782
3301
|
*/
|
|
2783
|
-
static
|
|
2784
|
-
if (
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
if (Is.object(value)) {
|
|
2789
|
-
value = value[part];
|
|
2790
|
-
}
|
|
2791
|
-
else {
|
|
2792
|
-
return undefined;
|
|
2793
|
-
}
|
|
2794
|
-
}
|
|
3302
|
+
static time(value) {
|
|
3303
|
+
if (Is.undefined(value)) {
|
|
3304
|
+
return value;
|
|
3305
|
+
}
|
|
3306
|
+
if (Is.date(value)) {
|
|
2795
3307
|
return value;
|
|
2796
3308
|
}
|
|
2797
|
-
|
|
3309
|
+
if (Is.number(value)) {
|
|
3310
|
+
const dt = new Date(value);
|
|
3311
|
+
dt.setFullYear(1970, 0, 1);
|
|
3312
|
+
return dt;
|
|
3313
|
+
}
|
|
3314
|
+
if (Is.string(value)) {
|
|
3315
|
+
const dt = new Date(value);
|
|
3316
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3317
|
+
const utc = Date.UTC(1970, 0, 1, dt.getUTCHours(), dt.getUTCMinutes(), dt.getUTCSeconds(), dt.getUTCMilliseconds());
|
|
3318
|
+
return new Date(utc);
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
2798
3321
|
}
|
|
2799
3322
|
/**
|
|
2800
|
-
*
|
|
2801
|
-
* @param
|
|
2802
|
-
* @
|
|
2803
|
-
* @
|
|
3323
|
+
* Coerce the value to an object.
|
|
3324
|
+
* @param value The value to coerce.
|
|
3325
|
+
* @throws TypeError If the value can not be coerced.
|
|
3326
|
+
* @returns The value if it can be coerced.
|
|
2804
3327
|
*/
|
|
2805
|
-
static
|
|
2806
|
-
if (Is.
|
|
2807
|
-
|
|
3328
|
+
static object(value) {
|
|
3329
|
+
if (Is.undefined(value)) {
|
|
3330
|
+
return value;
|
|
3331
|
+
}
|
|
3332
|
+
if (Is.object(value)) {
|
|
3333
|
+
return value;
|
|
3334
|
+
}
|
|
3335
|
+
if (Is.stringValue(value)) {
|
|
3336
|
+
try {
|
|
3337
|
+
return JSON.parse(value);
|
|
3338
|
+
}
|
|
3339
|
+
catch { }
|
|
2808
3340
|
}
|
|
2809
3341
|
}
|
|
2810
3342
|
/**
|
|
2811
|
-
*
|
|
2812
|
-
* @param
|
|
2813
|
-
* @
|
|
3343
|
+
* Coerce the value to a Uint8Array.
|
|
3344
|
+
* @param value The value to coerce.
|
|
3345
|
+
* @throws TypeError If the value can not be coerced.
|
|
3346
|
+
* @returns The value if it can be coerced.
|
|
2814
3347
|
*/
|
|
2815
|
-
static
|
|
2816
|
-
if (Is.
|
|
2817
|
-
|
|
3348
|
+
static uint8Array(value) {
|
|
3349
|
+
if (Is.undefined(value)) {
|
|
3350
|
+
return value;
|
|
3351
|
+
}
|
|
3352
|
+
if (Is.string(value)) {
|
|
3353
|
+
if (Is.stringHex(value.toLowerCase(), true)) {
|
|
3354
|
+
return Converter.hexToBytes(value.toLowerCase());
|
|
3355
|
+
}
|
|
3356
|
+
if (Is.stringBase64(value)) {
|
|
3357
|
+
return Converter.base64ToBytes(value);
|
|
3358
|
+
}
|
|
2818
3359
|
}
|
|
2819
3360
|
}
|
|
2820
3361
|
/**
|
|
2821
|
-
*
|
|
2822
|
-
* @param
|
|
2823
|
-
* @param
|
|
2824
|
-
* @returns The
|
|
2825
|
-
*/
|
|
2826
|
-
static
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
3362
|
+
* Coerces a value based on the coercion type.
|
|
3363
|
+
* @param value The value to coerce.
|
|
3364
|
+
* @param type The coercion type to perform.
|
|
3365
|
+
* @returns The coerced value.
|
|
3366
|
+
*/
|
|
3367
|
+
static byType(value, type) {
|
|
3368
|
+
switch (type) {
|
|
3369
|
+
case CoerceType.String:
|
|
3370
|
+
return Coerce.string(value);
|
|
3371
|
+
case CoerceType.Number:
|
|
3372
|
+
return Coerce.number(value);
|
|
3373
|
+
case CoerceType.Integer:
|
|
3374
|
+
return Coerce.integer(value);
|
|
3375
|
+
case CoerceType.BigInt:
|
|
3376
|
+
return Coerce.bigint(value);
|
|
3377
|
+
case CoerceType.Boolean:
|
|
3378
|
+
return Coerce.boolean(value);
|
|
3379
|
+
case CoerceType.Date:
|
|
3380
|
+
return Coerce.date(value);
|
|
3381
|
+
case CoerceType.DateTime:
|
|
3382
|
+
return Coerce.dateTime(value);
|
|
3383
|
+
case CoerceType.Time:
|
|
3384
|
+
return Coerce.time(value);
|
|
3385
|
+
case CoerceType.Object:
|
|
3386
|
+
return Coerce.object(value);
|
|
3387
|
+
case CoerceType.Uint8Array:
|
|
3388
|
+
return Coerce.uint8Array(value);
|
|
3389
|
+
default:
|
|
3390
|
+
return value;
|
|
2833
3391
|
}
|
|
2834
|
-
return obj;
|
|
2835
3392
|
}
|
|
3393
|
+
}
|
|
3394
|
+
|
|
3395
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3396
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3397
|
+
/**
|
|
3398
|
+
* Class to help with filenames.
|
|
3399
|
+
*/
|
|
3400
|
+
class FilenameHelper {
|
|
2836
3401
|
/**
|
|
2837
|
-
*
|
|
2838
|
-
* @param
|
|
2839
|
-
* @
|
|
2840
|
-
* @returns The partial object.
|
|
3402
|
+
* Replaces any unsafe characters in the filename.
|
|
3403
|
+
* @param filename The filename to make safe.
|
|
3404
|
+
* @returns The safe filename.
|
|
2841
3405
|
*/
|
|
2842
|
-
static
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
delete result[key];
|
|
2847
|
-
}
|
|
2848
|
-
return result;
|
|
3406
|
+
static safeFilename(filename) {
|
|
3407
|
+
let safe = Coerce.string(filename);
|
|
3408
|
+
if (Is.empty(safe)) {
|
|
3409
|
+
return "";
|
|
2849
3410
|
}
|
|
2850
|
-
|
|
3411
|
+
// Common non filename characters
|
|
3412
|
+
safe = safe.replace(/["*/:<>?\\|]/g, "_");
|
|
3413
|
+
// Windows non filename characters
|
|
3414
|
+
safe = safe.replace(/^(con|prn|aux|nul|com\d|lpt\d)$/i, "_");
|
|
3415
|
+
// Control characters
|
|
3416
|
+
safe = safe.replace(/[\u0000-\u001F\u0080-\u009F]/g, "_");
|
|
3417
|
+
// Relative paths
|
|
3418
|
+
safe = safe.replace(/^\.+/, "_");
|
|
3419
|
+
// Trailing periods
|
|
3420
|
+
safe = safe.replace(/\.+$/, "");
|
|
3421
|
+
return safe;
|
|
2851
3422
|
}
|
|
2852
3423
|
}
|
|
2853
3424
|
|
|
@@ -2869,6 +3440,32 @@ class RandomHelper {
|
|
|
2869
3440
|
}
|
|
2870
3441
|
}
|
|
2871
3442
|
|
|
3443
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3444
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3445
|
+
/**
|
|
3446
|
+
* Class to help with uint8 arrays.
|
|
3447
|
+
*/
|
|
3448
|
+
class Uint8ArrayHelper {
|
|
3449
|
+
/**
|
|
3450
|
+
* Concatenate multiple arrays.
|
|
3451
|
+
* @param arrays The array to concatenate.
|
|
3452
|
+
* @returns The combined array.
|
|
3453
|
+
*/
|
|
3454
|
+
static concat(arrays) {
|
|
3455
|
+
let totalLength = 0;
|
|
3456
|
+
for (const array of arrays) {
|
|
3457
|
+
totalLength += array.length;
|
|
3458
|
+
}
|
|
3459
|
+
const concatBytes = new Uint8Array(totalLength);
|
|
3460
|
+
let offset = 0;
|
|
3461
|
+
for (const array of arrays) {
|
|
3462
|
+
concatBytes.set(array, offset);
|
|
3463
|
+
offset += array.length;
|
|
3464
|
+
}
|
|
3465
|
+
return concatBytes;
|
|
3466
|
+
}
|
|
3467
|
+
}
|
|
3468
|
+
|
|
2872
3469
|
// Copyright 2024 IOTA Stiftung.
|
|
2873
3470
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2874
3471
|
/**
|
|
@@ -2881,7 +3478,7 @@ const CompressionType = {
|
|
|
2881
3478
|
*/
|
|
2882
3479
|
Gzip: "gzip",
|
|
2883
3480
|
/**
|
|
2884
|
-
*
|
|
3481
|
+
* Deflate.
|
|
2885
3482
|
*/
|
|
2886
3483
|
Deflate: "deflate"
|
|
2887
3484
|
};
|
|
@@ -3419,11 +4016,10 @@ class Compression {
|
|
|
3419
4016
|
Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
|
|
3420
4017
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
3421
4018
|
const blob = new Blob([bytes]);
|
|
3422
|
-
const
|
|
3423
|
-
const
|
|
3424
|
-
const compressedBlob = await new Response(
|
|
3425
|
-
const
|
|
3426
|
-
const compressedBytes = new Uint8Array(ab);
|
|
4019
|
+
const compressionStream = new CompressionStream(type);
|
|
4020
|
+
const compressionPipe = blob.stream().pipeThrough(compressionStream);
|
|
4021
|
+
const compressedBlob = await new Response(compressionPipe).blob();
|
|
4022
|
+
const compressedBytes = await compressedBlob.bytes();
|
|
3427
4023
|
// GZIP header contains a byte which specifies the OS the
|
|
3428
4024
|
// compression was performed on. We set this to 3 (Unix) to ensure
|
|
3429
4025
|
// that we produce consistent results.
|
|
@@ -3442,11 +4038,10 @@ class Compression {
|
|
|
3442
4038
|
Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
|
|
3443
4039
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
3444
4040
|
const blob = new Blob([compressedBytes]);
|
|
3445
|
-
const
|
|
3446
|
-
const
|
|
3447
|
-
const decompressedBlob = await new Response(
|
|
3448
|
-
|
|
3449
|
-
return new Uint8Array(ab);
|
|
4041
|
+
const decompressionStream = new DecompressionStream(type);
|
|
4042
|
+
const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
|
|
4043
|
+
const decompressedBlob = await new Response(decompressionPipe).blob();
|
|
4044
|
+
return decompressedBlob.bytes();
|
|
3450
4045
|
}
|
|
3451
4046
|
}
|
|
3452
4047
|
|
|
@@ -3504,6 +4099,7 @@ class Validation {
|
|
|
3504
4099
|
* @param options Additional options for the validation.
|
|
3505
4100
|
* @param options.minLength The minimum length of the string.
|
|
3506
4101
|
* @param options.maxLength The maximum length of the string.
|
|
4102
|
+
* @param options.format Specific format to check.
|
|
3507
4103
|
* @returns True if the value is a valid string.
|
|
3508
4104
|
*/
|
|
3509
4105
|
static string(property, value, failures, fieldNameResource, options) {
|
|
@@ -3522,6 +4118,47 @@ class Validation {
|
|
|
3522
4118
|
const maxLimitDefined = Is.integer(maxLength);
|
|
3523
4119
|
const belowMin = minLimitDefined && value.length < minLength;
|
|
3524
4120
|
const aboveMax = maxLimitDefined && value.length > maxLength;
|
|
4121
|
+
if (options?.format === "base58" && !Is.stringBase58(value)) {
|
|
4122
|
+
failures.push({
|
|
4123
|
+
property,
|
|
4124
|
+
reason: "validation.beTextBase58",
|
|
4125
|
+
properties: {
|
|
4126
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4127
|
+
value
|
|
4128
|
+
}
|
|
4129
|
+
});
|
|
4130
|
+
}
|
|
4131
|
+
else if (options?.format === "base64" && !Is.stringBase64(value)) {
|
|
4132
|
+
failures.push({
|
|
4133
|
+
property,
|
|
4134
|
+
reason: "validation.beTextBase64",
|
|
4135
|
+
properties: {
|
|
4136
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4137
|
+
value
|
|
4138
|
+
}
|
|
4139
|
+
});
|
|
4140
|
+
}
|
|
4141
|
+
else if (options?.format === "hex" && !Is.stringHex(value)) {
|
|
4142
|
+
failures.push({
|
|
4143
|
+
property,
|
|
4144
|
+
reason: "validation.beTextHex",
|
|
4145
|
+
properties: {
|
|
4146
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4147
|
+
value
|
|
4148
|
+
}
|
|
4149
|
+
});
|
|
4150
|
+
}
|
|
4151
|
+
else if (Is.regexp(options?.format) && !options.format.test(value)) {
|
|
4152
|
+
failures.push({
|
|
4153
|
+
property,
|
|
4154
|
+
reason: "validation.beTextRegExp",
|
|
4155
|
+
properties: {
|
|
4156
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4157
|
+
value,
|
|
4158
|
+
format: options?.format
|
|
4159
|
+
}
|
|
4160
|
+
});
|
|
4161
|
+
}
|
|
3525
4162
|
if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
|
|
3526
4163
|
failures.push({
|
|
3527
4164
|
property,
|
|
@@ -4181,4 +4818,4 @@ class Validation {
|
|
|
4181
4818
|
}
|
|
4182
4819
|
}
|
|
4183
4820
|
|
|
4184
|
-
export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base64, Base64Url, BaseError, BitString, Coerce, ComponentFactory, Compression, CompressionType, ConflictError, Converter, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
|
|
4821
|
+
export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, CoerceType, ComponentFactory, Compression, CompressionType, ConflictError, Converter, EnvHelper, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, Uint8ArrayHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
|