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