@twin.org/core 0.0.1-next.3 → 0.0.1-next.30
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 +1294 -730
- package/dist/esm/index.mjs +1292 -731
- 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 +9 -0
- package/dist/types/index.d.ts +3 -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 +102 -20
- package/docs/reference/classes/RandomHelper.md +3 -1
- package/docs/reference/classes/StringHelper.md +51 -17
- 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 +4 -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 -32
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,1019 @@ 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
|
-
static
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2430
|
+
static stringifyEx(object, space) {
|
|
2431
|
+
// We want to keep the 'this' intact for the replacer
|
|
2432
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
2433
|
+
return JSON.stringify(object, JsonHelper.stringifyExReplacer, space);
|
|
2434
|
+
}
|
|
2435
|
+
/**
|
|
2436
|
+
* Parse the JSON string with support for extended data types date/bigint/uint8array.
|
|
2437
|
+
* @param json The object to pause.
|
|
2438
|
+
* @returns The object.
|
|
2439
|
+
*/
|
|
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);
|
|
2250
2487
|
}
|
|
2251
|
-
if (
|
|
2252
|
-
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);
|
|
2253
2493
|
}
|
|
2254
2494
|
}
|
|
2495
|
+
return value;
|
|
2255
2496
|
}
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
/**
|
|
2500
|
+
* Class to help with objects.
|
|
2501
|
+
*/
|
|
2502
|
+
class ObjectHelper {
|
|
2256
2503
|
/**
|
|
2257
|
-
*
|
|
2258
|
-
* @
|
|
2259
|
-
* @throws TypeError If the value can not be coerced.
|
|
2260
|
-
* @returns The value if it can be coerced.
|
|
2504
|
+
* Runtime name for the class.
|
|
2505
|
+
* @internal
|
|
2261
2506
|
*/
|
|
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
|
-
}
|
|
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();
|
|
2278
2517
|
}
|
|
2518
|
+
const json = format ? JSON.stringify(obj, undefined, "\t") : JSON.stringify(obj);
|
|
2519
|
+
return Converter.utf8ToBytes(json);
|
|
2279
2520
|
}
|
|
2280
2521
|
/**
|
|
2281
|
-
*
|
|
2282
|
-
* @param
|
|
2283
|
-
* @
|
|
2284
|
-
* @
|
|
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.
|
|
2285
2526
|
*/
|
|
2286
|
-
static
|
|
2287
|
-
if (Is.
|
|
2288
|
-
return
|
|
2289
|
-
}
|
|
2290
|
-
if (Is.date(value)) {
|
|
2291
|
-
return value;
|
|
2527
|
+
static fromBytes(bytes) {
|
|
2528
|
+
if (Is.empty(bytes) || bytes.length === 0) {
|
|
2529
|
+
return undefined;
|
|
2292
2530
|
}
|
|
2293
|
-
|
|
2294
|
-
|
|
2531
|
+
try {
|
|
2532
|
+
const utf8 = Converter.bytesToUtf8(bytes);
|
|
2533
|
+
return JSON.parse(utf8);
|
|
2295
2534
|
}
|
|
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
|
-
}
|
|
2535
|
+
catch (err) {
|
|
2536
|
+
throw new GeneralError(ObjectHelper._CLASS_NAME, "failedBytesToJSON", undefined, err);
|
|
2302
2537
|
}
|
|
2303
2538
|
}
|
|
2304
2539
|
/**
|
|
2305
|
-
*
|
|
2306
|
-
* @param
|
|
2307
|
-
* @
|
|
2308
|
-
* @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.
|
|
2309
2543
|
*/
|
|
2310
|
-
static
|
|
2311
|
-
if (Is.undefined(
|
|
2312
|
-
return
|
|
2544
|
+
static clone(obj) {
|
|
2545
|
+
if (Is.undefined(obj)) {
|
|
2546
|
+
return undefined;
|
|
2313
2547
|
}
|
|
2314
|
-
|
|
2315
|
-
|
|
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);
|
|
2316
2559
|
}
|
|
2317
|
-
if (Is.
|
|
2318
|
-
|
|
2319
|
-
dt.setFullYear(1970, 0, 1);
|
|
2320
|
-
return dt;
|
|
2560
|
+
if (Is.empty(obj2)) {
|
|
2561
|
+
return ObjectHelper.clone(obj1);
|
|
2321
2562
|
}
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
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
|
+
}
|
|
2327
2573
|
}
|
|
2328
2574
|
}
|
|
2575
|
+
return obj1Clone;
|
|
2329
2576
|
}
|
|
2330
2577
|
/**
|
|
2331
|
-
*
|
|
2332
|
-
* @param
|
|
2333
|
-
* @
|
|
2334
|
-
* @
|
|
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.
|
|
2335
2583
|
*/
|
|
2336
|
-
static
|
|
2337
|
-
if (
|
|
2338
|
-
return
|
|
2339
|
-
}
|
|
2340
|
-
if (Is.object(value)) {
|
|
2341
|
-
return value;
|
|
2584
|
+
static equal(obj1, obj2, strictPropertyOrder) {
|
|
2585
|
+
if (strictPropertyOrder ?? true) {
|
|
2586
|
+
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
2342
2587
|
}
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
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;
|
|
2346
2619
|
}
|
|
2347
|
-
catch { }
|
|
2348
2620
|
}
|
|
2621
|
+
return pathValue;
|
|
2349
2622
|
}
|
|
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
2623
|
/**
|
|
2359
|
-
*
|
|
2360
|
-
* @param
|
|
2361
|
-
* @
|
|
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.
|
|
2362
2629
|
*/
|
|
2363
|
-
static
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
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
|
+
}
|
|
2367
2684
|
}
|
|
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
2685
|
}
|
|
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
2686
|
/**
|
|
2389
|
-
*
|
|
2390
|
-
*
|
|
2391
|
-
* @param
|
|
2392
|
-
* @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
|
|
2393
2690
|
*/
|
|
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));
|
|
2691
|
+
static propertyDelete(obj, property) {
|
|
2692
|
+
if (Is.object(obj)) {
|
|
2693
|
+
delete obj[property];
|
|
2401
2694
|
}
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
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);
|
|
2411
2711
|
}
|
|
2412
2712
|
}
|
|
2413
|
-
buffer.push(`[${parts.join(",")}]`);
|
|
2414
2713
|
}
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
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 = {};
|
|
2420
2725
|
for (const key of keys) {
|
|
2421
|
-
|
|
2422
|
-
props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
|
|
2423
|
-
}
|
|
2726
|
+
result[key] = obj[key];
|
|
2424
2727
|
}
|
|
2425
|
-
|
|
2728
|
+
return result;
|
|
2426
2729
|
}
|
|
2427
|
-
return
|
|
2730
|
+
return obj;
|
|
2428
2731
|
}
|
|
2429
2732
|
/**
|
|
2430
|
-
*
|
|
2431
|
-
*
|
|
2432
|
-
* @param
|
|
2433
|
-
* @
|
|
2434
|
-
* @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.
|
|
2435
2737
|
*/
|
|
2436
|
-
static
|
|
2437
|
-
|
|
2438
|
-
|
|
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;
|
|
2439
2747
|
}
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2751
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2752
|
+
/**
|
|
2753
|
+
* Environment variable helper.
|
|
2754
|
+
*/
|
|
2755
|
+
class EnvHelper {
|
|
2756
|
+
/**
|
|
2757
|
+
* Get the environment variable as an object with camel cased names.
|
|
2758
|
+
* @param envVars The environment variables.
|
|
2759
|
+
* @param prefix The prefix of the environment variables, if not provided gets all.
|
|
2760
|
+
* @returns The object with camel cased names.
|
|
2761
|
+
*/
|
|
2762
|
+
static envToJson(envVars, prefix) {
|
|
2763
|
+
const result = {};
|
|
2764
|
+
if (!Is.empty(envVars)) {
|
|
2765
|
+
if (Is.empty(prefix)) {
|
|
2766
|
+
for (const envVar in envVars) {
|
|
2767
|
+
if (Is.stringValue(envVars[envVar])) {
|
|
2768
|
+
const camelCaseName = StringHelper.camelCase(envVar.toLowerCase());
|
|
2769
|
+
ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2772
|
+
}
|
|
2773
|
+
else {
|
|
2774
|
+
for (const envVar in envVars) {
|
|
2775
|
+
if (envVar.startsWith(prefix) && Is.stringValue(envVars[envVar])) {
|
|
2776
|
+
const camelCaseName = StringHelper.camelCase(envVar.replace(prefix, "").toLowerCase());
|
|
2777
|
+
ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
|
|
2778
|
+
}
|
|
2779
|
+
}
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
return result;
|
|
2449
2783
|
}
|
|
2450
2784
|
}
|
|
2451
2785
|
|
|
2452
2786
|
// Copyright 2024 IOTA Stiftung.
|
|
2453
2787
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2454
|
-
/* eslint-disable no-bitwise */
|
|
2455
2788
|
/**
|
|
2456
|
-
*
|
|
2789
|
+
* Class to perform internationalization.
|
|
2457
2790
|
*/
|
|
2458
|
-
class
|
|
2791
|
+
class I18n {
|
|
2459
2792
|
/**
|
|
2460
|
-
*
|
|
2793
|
+
* The default translation.
|
|
2794
|
+
*/
|
|
2795
|
+
static DEFAULT_LOCALE = "en";
|
|
2796
|
+
/**
|
|
2797
|
+
* Dictionaries for lookups.
|
|
2461
2798
|
* @internal
|
|
2462
2799
|
*/
|
|
2463
|
-
static
|
|
2800
|
+
static _localeDictionaries = {};
|
|
2464
2801
|
/**
|
|
2465
|
-
*
|
|
2802
|
+
* The current locale.
|
|
2466
2803
|
* @internal
|
|
2467
2804
|
*/
|
|
2468
|
-
static
|
|
2805
|
+
static _currentLocale = I18n.DEFAULT_LOCALE;
|
|
2469
2806
|
/**
|
|
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.
|
|
2807
|
+
* Change handler for the locale being updated.
|
|
2808
|
+
* @internal
|
|
2475
2809
|
*/
|
|
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
|
-
}
|
|
2810
|
+
static _localeChangedHandlers = {};
|
|
2811
|
+
/**
|
|
2812
|
+
* Change handler for the dictionaries being updated.
|
|
2813
|
+
* @internal
|
|
2814
|
+
*/
|
|
2815
|
+
static _dictionaryChangedHandlers = {};
|
|
2816
|
+
/**
|
|
2817
|
+
* Set the locale.
|
|
2818
|
+
* @param locale The new locale.
|
|
2819
|
+
*/
|
|
2820
|
+
static setLocale(locale) {
|
|
2821
|
+
I18n._currentLocale = locale;
|
|
2822
|
+
for (const callback in I18n._localeChangedHandlers) {
|
|
2823
|
+
I18n._localeChangedHandlers[callback](I18n._currentLocale);
|
|
2503
2824
|
}
|
|
2504
|
-
return str;
|
|
2505
2825
|
}
|
|
2506
2826
|
/**
|
|
2507
|
-
*
|
|
2508
|
-
* @
|
|
2509
|
-
* @returns The array.
|
|
2827
|
+
* Get the locale.
|
|
2828
|
+
* @returns The current locale.
|
|
2510
2829
|
*/
|
|
2511
|
-
static
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2830
|
+
static getLocale() {
|
|
2831
|
+
return I18n._currentLocale;
|
|
2832
|
+
}
|
|
2833
|
+
/**
|
|
2834
|
+
* Add a locale dictionary.
|
|
2835
|
+
* @param locale The locale.
|
|
2836
|
+
* @param dictionary The dictionary to add.
|
|
2837
|
+
*/
|
|
2838
|
+
static addDictionary(locale, dictionary) {
|
|
2839
|
+
const mergedKeys = {};
|
|
2840
|
+
I18n.flattenTranslationKeys(dictionary, "", mergedKeys);
|
|
2841
|
+
I18n._localeDictionaries[locale] = mergedKeys;
|
|
2842
|
+
for (const callback in I18n._dictionaryChangedHandlers) {
|
|
2843
|
+
I18n._dictionaryChangedHandlers[callback](I18n._currentLocale);
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
/**
|
|
2847
|
+
* Get a locale dictionary.
|
|
2848
|
+
* @param locale The locale.
|
|
2849
|
+
* @returns The dictionary of undefined if it does not exist.
|
|
2850
|
+
*/
|
|
2851
|
+
static getDictionary(locale) {
|
|
2852
|
+
return I18n._localeDictionaries[locale];
|
|
2853
|
+
}
|
|
2854
|
+
/**
|
|
2855
|
+
* Get all the locale dictionaries.
|
|
2856
|
+
* @returns The dictionaries.
|
|
2857
|
+
*/
|
|
2858
|
+
static getAllDictionaries() {
|
|
2859
|
+
return I18n._localeDictionaries;
|
|
2860
|
+
}
|
|
2861
|
+
/**
|
|
2862
|
+
* Add a locale changed handler.
|
|
2863
|
+
* @param id The id of the handler.
|
|
2864
|
+
* @param handler The handler to add.
|
|
2865
|
+
*/
|
|
2866
|
+
static addLocaleHandler(id, handler) {
|
|
2867
|
+
I18n._localeChangedHandlers[id] = handler;
|
|
2868
|
+
}
|
|
2869
|
+
/**
|
|
2870
|
+
* Remove a locale changed handler.
|
|
2871
|
+
* @param id The id of the handler.
|
|
2872
|
+
*/
|
|
2873
|
+
static removeLocaleHandler(id) {
|
|
2874
|
+
delete I18n._localeChangedHandlers[id];
|
|
2875
|
+
}
|
|
2876
|
+
/**
|
|
2877
|
+
* Add a dictionary changed handler.
|
|
2878
|
+
* @param id The id of the handler.
|
|
2879
|
+
* @param handler The handler to add.
|
|
2880
|
+
*/
|
|
2881
|
+
static addDictionaryHandler(id, handler) {
|
|
2882
|
+
I18n._dictionaryChangedHandlers[id] = handler;
|
|
2883
|
+
}
|
|
2884
|
+
/**
|
|
2885
|
+
* Remove a dictionary changed handler.
|
|
2886
|
+
* @param id The id of the handler.
|
|
2887
|
+
*/
|
|
2888
|
+
static removeDictionaryHandler(id) {
|
|
2889
|
+
delete I18n._dictionaryChangedHandlers[id];
|
|
2890
|
+
}
|
|
2891
|
+
/**
|
|
2892
|
+
* Format a message.
|
|
2893
|
+
* @param key The key of the message to format.
|
|
2894
|
+
* @param values The values to substitute into the message.
|
|
2895
|
+
* @param overrideLocale Override the locale.
|
|
2896
|
+
* @returns The formatted string.
|
|
2897
|
+
*/
|
|
2898
|
+
static formatMessage(key, values, overrideLocale) {
|
|
2899
|
+
let cl = overrideLocale ?? I18n._currentLocale;
|
|
2900
|
+
if (cl.startsWith("debug-")) {
|
|
2901
|
+
cl = I18n.DEFAULT_LOCALE;
|
|
2902
|
+
}
|
|
2903
|
+
if (!I18n._localeDictionaries[cl]) {
|
|
2904
|
+
return `!!Missing ${cl}`;
|
|
2905
|
+
}
|
|
2906
|
+
if (!I18n._localeDictionaries[cl][key]) {
|
|
2907
|
+
return `!!Missing ${cl}.${key}`;
|
|
2908
|
+
}
|
|
2909
|
+
if (I18n._currentLocale === "debug-k") {
|
|
2910
|
+
return key;
|
|
2911
|
+
}
|
|
2912
|
+
let ret = new intlMessageformat.IntlMessageFormat(I18n._localeDictionaries[cl][key], cl).format(values);
|
|
2913
|
+
if (I18n._currentLocale === "debug-x") {
|
|
2914
|
+
ret = ret.replace(/[a-z]/g, "x").replace(/[A-Z]/g, "x").replace(/\d/g, "n");
|
|
2915
|
+
}
|
|
2916
|
+
return ret;
|
|
2917
|
+
}
|
|
2918
|
+
/**
|
|
2919
|
+
* Check if the dictionaries have a message for the given key.
|
|
2920
|
+
* @param key The key to check for existence.
|
|
2921
|
+
* @returns True if the key exists.
|
|
2922
|
+
*/
|
|
2923
|
+
static hasMessage(key) {
|
|
2924
|
+
return Is.string(I18n._localeDictionaries[I18n._currentLocale]?.[key]);
|
|
2925
|
+
}
|
|
2926
|
+
/**
|
|
2927
|
+
* Flatten the translation property paths for faster lookup.
|
|
2928
|
+
* @param translation The translation to merge.
|
|
2929
|
+
* @param propertyPath The current root path.
|
|
2930
|
+
* @param mergedKeys The merged keys dictionary to populate.
|
|
2931
|
+
* @internal
|
|
2932
|
+
*/
|
|
2933
|
+
static flattenTranslationKeys(translation, propertyPath, mergedKeys) {
|
|
2934
|
+
for (const key in translation) {
|
|
2935
|
+
const val = translation[key];
|
|
2936
|
+
const mergedPath = propertyPath.length > 0 ? `${propertyPath}.${key}` : key;
|
|
2937
|
+
if (Is.string(val)) {
|
|
2938
|
+
mergedKeys[mergedPath] = val;
|
|
2520
2939
|
}
|
|
2521
|
-
else if (
|
|
2522
|
-
|
|
2940
|
+
else if (Is.object(val)) {
|
|
2941
|
+
I18n.flattenTranslationKeys(val, mergedPath, mergedKeys);
|
|
2523
2942
|
}
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2948
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2949
|
+
/**
|
|
2950
|
+
* Error helper functions.
|
|
2951
|
+
*/
|
|
2952
|
+
class ErrorHelper {
|
|
2953
|
+
/**
|
|
2954
|
+
* Format Errors and returns just their messages.
|
|
2955
|
+
* @param error The error to format.
|
|
2956
|
+
* @returns The error formatted including any inner errors.
|
|
2957
|
+
*/
|
|
2958
|
+
static formatErrors(error) {
|
|
2959
|
+
return ErrorHelper.localizeErrors(error).map(e => e.message);
|
|
2960
|
+
}
|
|
2961
|
+
/**
|
|
2962
|
+
* Localize the content of an error and any inner errors.
|
|
2963
|
+
* @param error The error to format.
|
|
2964
|
+
* @returns The localized version of the errors flattened.
|
|
2965
|
+
*/
|
|
2966
|
+
static localizeErrors(error) {
|
|
2967
|
+
const formattedErrors = [];
|
|
2968
|
+
if (Is.notEmpty(error)) {
|
|
2969
|
+
const errors = BaseError.flatten(error);
|
|
2970
|
+
for (const err of errors) {
|
|
2971
|
+
const errorNameKey = `errorNames.${StringHelper.camelCase(err.name)}`;
|
|
2972
|
+
const errorMessageKey = `error.${err.message}`;
|
|
2973
|
+
// If there is no error message then it is probably
|
|
2974
|
+
// from a 3rd party lib, so don't format it just display
|
|
2975
|
+
const hasErrorName = I18n.hasMessage(errorNameKey);
|
|
2976
|
+
const hasErrorMessage = I18n.hasMessage(errorMessageKey);
|
|
2977
|
+
const localizedError = {
|
|
2978
|
+
name: I18n.formatMessage(hasErrorName ? errorNameKey : "errorNames.error"),
|
|
2979
|
+
message: hasErrorMessage
|
|
2980
|
+
? I18n.formatMessage(errorMessageKey, err.properties)
|
|
2981
|
+
: err.message
|
|
2982
|
+
};
|
|
2983
|
+
if (Is.stringValue(err.source)) {
|
|
2984
|
+
localizedError.source = err.source;
|
|
2985
|
+
}
|
|
2986
|
+
if (Is.stringValue(err.stack)) {
|
|
2987
|
+
// Remove the first line from the stack traces as they
|
|
2988
|
+
// just have the error type and message duplicated
|
|
2989
|
+
const lines = err.stack.split("\n");
|
|
2990
|
+
lines.shift();
|
|
2991
|
+
localizedError.stack = lines.join("\n");
|
|
2992
|
+
}
|
|
2993
|
+
const additional = ErrorHelper.formatValidationErrors(err);
|
|
2994
|
+
if (Is.stringValue(additional)) {
|
|
2995
|
+
localizedError.additional = additional;
|
|
2996
|
+
}
|
|
2997
|
+
formattedErrors.push(localizedError);
|
|
2532
2998
|
}
|
|
2533
2999
|
}
|
|
2534
|
-
return
|
|
3000
|
+
return formattedErrors;
|
|
2535
3001
|
}
|
|
2536
3002
|
/**
|
|
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.
|
|
3003
|
+
* Localize the content of an error and any inner errors.
|
|
3004
|
+
* @param error The error to format.
|
|
3005
|
+
* @returns The localized version of the errors flattened.
|
|
2544
3006
|
*/
|
|
2545
|
-
static
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
const
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
3007
|
+
static formatValidationErrors(error) {
|
|
3008
|
+
if (Is.object(error.properties) &&
|
|
3009
|
+
Object.keys(error.properties).length > 0 &&
|
|
3010
|
+
Is.object(error.properties) &&
|
|
3011
|
+
Is.arrayValue(error.properties.validationFailures)) {
|
|
3012
|
+
const validationErrors = [];
|
|
3013
|
+
for (const validationFailure of error.properties.validationFailures) {
|
|
3014
|
+
const errorI18n = `error.${validationFailure.reason}`;
|
|
3015
|
+
const errorMessage = I18n.hasMessage(errorI18n)
|
|
3016
|
+
? I18n.formatMessage(errorI18n, validationFailure.properties)
|
|
3017
|
+
: errorI18n;
|
|
3018
|
+
let v = `${validationFailure.property}: ${errorMessage}`;
|
|
3019
|
+
if (Is.object(validationFailure.properties) &&
|
|
3020
|
+
Is.notEmpty(validationFailure.properties.value)) {
|
|
3021
|
+
v += ` = ${JSON.stringify(validationFailure.properties.value)}`;
|
|
2559
3022
|
}
|
|
3023
|
+
validationErrors.push(v);
|
|
2560
3024
|
}
|
|
3025
|
+
return validationErrors.join("\n");
|
|
2561
3026
|
}
|
|
2562
|
-
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
2563
3027
|
}
|
|
3028
|
+
}
|
|
3029
|
+
|
|
3030
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3031
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3032
|
+
/**
|
|
3033
|
+
* The types the extracted data can be coerced to.
|
|
3034
|
+
*/
|
|
3035
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3036
|
+
const CoerceType = {
|
|
2564
3037
|
/**
|
|
2565
|
-
*
|
|
2566
|
-
* @param hex The hex to decode.
|
|
2567
|
-
* @param reverse Store the characters in reverse.
|
|
2568
|
-
* @returns The array.
|
|
3038
|
+
* String.
|
|
2569
3039
|
*/
|
|
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
|
-
}
|
|
3040
|
+
String: "string",
|
|
2590
3041
|
/**
|
|
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.
|
|
3042
|
+
* Number.
|
|
2595
3043
|
*/
|
|
2596
|
-
|
|
2597
|
-
const hex = Converter.bytesToHex(Converter.utf8ToBytes(utf8));
|
|
2598
|
-
return includePrefix ? HexHelper.addPrefix(hex) : hex;
|
|
2599
|
-
}
|
|
3044
|
+
Number: "number",
|
|
2600
3045
|
/**
|
|
2601
|
-
*
|
|
2602
|
-
* @param hex The hex to convert.
|
|
2603
|
-
* @returns The UTF8 version of the bytes.
|
|
3046
|
+
* Integer.
|
|
2604
3047
|
*/
|
|
2605
|
-
|
|
2606
|
-
return Converter.bytesToUtf8(Converter.hexToBytes(HexHelper.stripPrefix(hex)));
|
|
2607
|
-
}
|
|
3048
|
+
Integer: "integer",
|
|
2608
3049
|
/**
|
|
2609
|
-
*
|
|
2610
|
-
* @param bytes The bytes to convert.
|
|
2611
|
-
* @returns A binary string of the bytes.
|
|
3050
|
+
* Boolean.
|
|
2612
3051
|
*/
|
|
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
|
-
}
|
|
3052
|
+
Boolean: "boolean",
|
|
2620
3053
|
/**
|
|
2621
|
-
*
|
|
2622
|
-
* @param binary The binary string.
|
|
2623
|
-
* @returns The bytes.
|
|
3054
|
+
* Big Integer.
|
|
2624
3055
|
*/
|
|
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
|
-
}
|
|
3056
|
+
BigInt: "bigint",
|
|
2632
3057
|
/**
|
|
2633
|
-
*
|
|
2634
|
-
* @param bytes The bytes to convert.
|
|
2635
|
-
* @returns A base64 string of the bytes.
|
|
3058
|
+
* Date.
|
|
2636
3059
|
*/
|
|
2637
|
-
|
|
2638
|
-
return Base64.encode(bytes);
|
|
2639
|
-
}
|
|
3060
|
+
Date: "date",
|
|
2640
3061
|
/**
|
|
2641
|
-
*
|
|
2642
|
-
* @param base64 The base64 string.
|
|
2643
|
-
* @returns The bytes.
|
|
3062
|
+
* Date Time.
|
|
2644
3063
|
*/
|
|
2645
|
-
|
|
2646
|
-
return Base64.decode(base64);
|
|
2647
|
-
}
|
|
3064
|
+
DateTime: "datetime",
|
|
2648
3065
|
/**
|
|
2649
|
-
*
|
|
2650
|
-
* @param bytes The bytes to convert.
|
|
2651
|
-
* @returns A base64 url string of the bytes.
|
|
3066
|
+
* Time.
|
|
2652
3067
|
*/
|
|
2653
|
-
|
|
2654
|
-
return Base64Url.encode(bytes);
|
|
2655
|
-
}
|
|
3068
|
+
Time: "time",
|
|
2656
3069
|
/**
|
|
2657
|
-
*
|
|
2658
|
-
* @param base64Url The base64 url string.
|
|
2659
|
-
* @returns The bytes.
|
|
3070
|
+
* Object.
|
|
2660
3071
|
*/
|
|
2661
|
-
|
|
2662
|
-
return Base64Url.decode(base64Url);
|
|
2663
|
-
}
|
|
3072
|
+
Object: "object",
|
|
2664
3073
|
/**
|
|
2665
|
-
*
|
|
2666
|
-
* @internal
|
|
3074
|
+
* Uint8Array.
|
|
2667
3075
|
*/
|
|
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
|
-
}
|
|
3076
|
+
Uint8Array: "uint8array"
|
|
3077
|
+
};
|
|
2687
3078
|
|
|
3079
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3080
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
2688
3081
|
/**
|
|
2689
|
-
*
|
|
3082
|
+
* Coerce an object from one type to another.
|
|
2690
3083
|
*/
|
|
2691
|
-
class
|
|
3084
|
+
class Coerce {
|
|
2692
3085
|
/**
|
|
2693
|
-
*
|
|
2694
|
-
* @
|
|
3086
|
+
* Coerce the value to a string.
|
|
3087
|
+
* @param value The value to coerce.
|
|
3088
|
+
* @throws TypeError If the value can not be coerced.
|
|
3089
|
+
* @returns The value if it can be coerced.
|
|
2695
3090
|
*/
|
|
2696
|
-
static
|
|
3091
|
+
static string(value) {
|
|
3092
|
+
if (Is.undefined(value)) {
|
|
3093
|
+
return value;
|
|
3094
|
+
}
|
|
3095
|
+
if (Is.string(value)) {
|
|
3096
|
+
return value;
|
|
3097
|
+
}
|
|
3098
|
+
if (Is.number(value)) {
|
|
3099
|
+
return value.toString();
|
|
3100
|
+
}
|
|
3101
|
+
if (Is.boolean(value)) {
|
|
3102
|
+
return value ? "true" : "false";
|
|
3103
|
+
}
|
|
3104
|
+
if (Is.date(value)) {
|
|
3105
|
+
return value.toISOString();
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
2697
3108
|
/**
|
|
2698
|
-
*
|
|
2699
|
-
* @param
|
|
2700
|
-
* @
|
|
2701
|
-
* @returns The
|
|
3109
|
+
* Coerce the value to a number.
|
|
3110
|
+
* @param value The value to coerce.
|
|
3111
|
+
* @throws TypeError If the value can not be coerced.
|
|
3112
|
+
* @returns The value if it can be coerced.
|
|
2702
3113
|
*/
|
|
2703
|
-
static
|
|
2704
|
-
if (
|
|
2705
|
-
return
|
|
3114
|
+
static number(value) {
|
|
3115
|
+
if (Is.undefined(value)) {
|
|
3116
|
+
return value;
|
|
3117
|
+
}
|
|
3118
|
+
if (Is.number(value)) {
|
|
3119
|
+
return value;
|
|
3120
|
+
}
|
|
3121
|
+
if (Is.string(value)) {
|
|
3122
|
+
const parsed = Number.parseFloat(value);
|
|
3123
|
+
if (Is.number(parsed)) {
|
|
3124
|
+
return parsed;
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
if (Is.boolean(value)) {
|
|
3128
|
+
return value ? 1 : 0;
|
|
3129
|
+
}
|
|
3130
|
+
if (Is.date(value)) {
|
|
3131
|
+
return value.getTime();
|
|
2706
3132
|
}
|
|
2707
|
-
const json = format ? JSON.stringify(obj, undefined, "\t") : JSON.stringify(obj);
|
|
2708
|
-
return Converter.utf8ToBytes(json);
|
|
2709
3133
|
}
|
|
2710
3134
|
/**
|
|
2711
|
-
*
|
|
2712
|
-
* @param
|
|
2713
|
-
* @
|
|
2714
|
-
* @
|
|
3135
|
+
* Coerce the value to an integer.
|
|
3136
|
+
* @param value The value to coerce.
|
|
3137
|
+
* @throws TypeError If the value can not be coerced.
|
|
3138
|
+
* @returns The value if it can be coerced.
|
|
2715
3139
|
*/
|
|
2716
|
-
static
|
|
2717
|
-
|
|
2718
|
-
|
|
3140
|
+
static integer(value) {
|
|
3141
|
+
const num = Coerce.number(value);
|
|
3142
|
+
if (!Is.undefined(num)) {
|
|
3143
|
+
return Math.trunc(num);
|
|
2719
3144
|
}
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
3145
|
+
}
|
|
3146
|
+
/**
|
|
3147
|
+
* Coerce the value to a bigint.
|
|
3148
|
+
* @param value The value to coerce.
|
|
3149
|
+
* @throws TypeError If the value can not be coerced.
|
|
3150
|
+
* @returns The value if it can be coerced.
|
|
3151
|
+
*/
|
|
3152
|
+
static bigint(value) {
|
|
3153
|
+
if (Is.undefined(value)) {
|
|
3154
|
+
return value;
|
|
3155
|
+
}
|
|
3156
|
+
if (Is.bigint(value)) {
|
|
3157
|
+
return value;
|
|
3158
|
+
}
|
|
3159
|
+
if (Is.number(value)) {
|
|
3160
|
+
return BigInt(value);
|
|
2723
3161
|
}
|
|
2724
|
-
|
|
2725
|
-
|
|
3162
|
+
if (Is.string(value)) {
|
|
3163
|
+
const parsed = Number.parseFloat(value);
|
|
3164
|
+
if (Is.integer(parsed)) {
|
|
3165
|
+
return BigInt(parsed);
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
if (Is.boolean(value)) {
|
|
3169
|
+
return value ? 1n : 0n;
|
|
2726
3170
|
}
|
|
2727
3171
|
}
|
|
2728
3172
|
/**
|
|
2729
|
-
*
|
|
2730
|
-
* @param
|
|
2731
|
-
* @
|
|
3173
|
+
* Coerce the value to a boolean.
|
|
3174
|
+
* @param value The value to coerce.
|
|
3175
|
+
* @throws TypeError If the value can not be coerced.
|
|
3176
|
+
* @returns The value if it can be coerced.
|
|
2732
3177
|
*/
|
|
2733
|
-
static
|
|
2734
|
-
if (Is.undefined(
|
|
2735
|
-
return
|
|
3178
|
+
static boolean(value) {
|
|
3179
|
+
if (Is.undefined(value)) {
|
|
3180
|
+
return value;
|
|
3181
|
+
}
|
|
3182
|
+
if (Is.boolean(value)) {
|
|
3183
|
+
return value;
|
|
3184
|
+
}
|
|
3185
|
+
if (Is.number(value)) {
|
|
3186
|
+
// eslint-disable-next-line no-unneeded-ternary
|
|
3187
|
+
return value ? true : false;
|
|
3188
|
+
}
|
|
3189
|
+
if (Is.string(value)) {
|
|
3190
|
+
if (/true/i.test(value)) {
|
|
3191
|
+
return true;
|
|
3192
|
+
}
|
|
3193
|
+
if (/false/i.test(value)) {
|
|
3194
|
+
return false;
|
|
3195
|
+
}
|
|
2736
3196
|
}
|
|
2737
|
-
return structuredClone(obj);
|
|
2738
3197
|
}
|
|
2739
3198
|
/**
|
|
2740
|
-
*
|
|
2741
|
-
* @param
|
|
2742
|
-
* @
|
|
2743
|
-
* @returns The
|
|
3199
|
+
* Coerce the value to a date.
|
|
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.
|
|
2744
3203
|
*/
|
|
2745
|
-
static
|
|
2746
|
-
if (Is.
|
|
2747
|
-
return
|
|
3204
|
+
static date(value) {
|
|
3205
|
+
if (Is.undefined(value)) {
|
|
3206
|
+
return value;
|
|
2748
3207
|
}
|
|
2749
|
-
if (Is.
|
|
2750
|
-
return
|
|
3208
|
+
if (Is.date(value)) {
|
|
3209
|
+
return value;
|
|
2751
3210
|
}
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
ObjectHelper.propertySet(obj1Clone, key, obj2[key]);
|
|
2761
|
-
}
|
|
3211
|
+
if (Is.number(value)) {
|
|
3212
|
+
return new Date(value);
|
|
3213
|
+
}
|
|
3214
|
+
if (Is.string(value)) {
|
|
3215
|
+
const dt = new Date(value);
|
|
3216
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3217
|
+
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate());
|
|
3218
|
+
return new Date(utc);
|
|
2762
3219
|
}
|
|
2763
3220
|
}
|
|
2764
|
-
return obj1Clone;
|
|
2765
3221
|
}
|
|
2766
3222
|
/**
|
|
2767
|
-
*
|
|
2768
|
-
* @param
|
|
2769
|
-
* @
|
|
2770
|
-
* @
|
|
2771
|
-
* @returns True is the objects are equal.
|
|
3223
|
+
* Coerce the value to a date/time.
|
|
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.
|
|
2772
3227
|
*/
|
|
2773
|
-
static
|
|
2774
|
-
if (
|
|
2775
|
-
return
|
|
3228
|
+
static dateTime(value) {
|
|
3229
|
+
if (Is.undefined(value)) {
|
|
3230
|
+
return value;
|
|
3231
|
+
}
|
|
3232
|
+
if (Is.date(value)) {
|
|
3233
|
+
return value;
|
|
3234
|
+
}
|
|
3235
|
+
if (Is.number(value)) {
|
|
3236
|
+
return new Date(value);
|
|
3237
|
+
}
|
|
3238
|
+
if (Is.string(value)) {
|
|
3239
|
+
const dt = new Date(value);
|
|
3240
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3241
|
+
const utc = Date.UTC(dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate(), dt.getUTCHours(), dt.getUTCMinutes(), dt.getUTCSeconds(), dt.getUTCMilliseconds());
|
|
3242
|
+
return new Date(utc);
|
|
3243
|
+
}
|
|
2776
3244
|
}
|
|
2777
|
-
return JsonHelper.canonicalize(obj1) === JsonHelper.canonicalize(obj2);
|
|
2778
3245
|
}
|
|
2779
3246
|
/**
|
|
2780
|
-
*
|
|
2781
|
-
* @param
|
|
2782
|
-
* @
|
|
2783
|
-
* @returns The
|
|
3247
|
+
* Coerce the value to a time.
|
|
3248
|
+
* @param value The value to coerce.
|
|
3249
|
+
* @throws TypeError If the value can not be coerced.
|
|
3250
|
+
* @returns The value if it can be coerced.
|
|
2784
3251
|
*/
|
|
2785
|
-
static
|
|
2786
|
-
if (
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
if (Is.object(value)) {
|
|
2791
|
-
value = value[part];
|
|
2792
|
-
}
|
|
2793
|
-
else {
|
|
2794
|
-
return undefined;
|
|
2795
|
-
}
|
|
2796
|
-
}
|
|
3252
|
+
static time(value) {
|
|
3253
|
+
if (Is.undefined(value)) {
|
|
3254
|
+
return value;
|
|
3255
|
+
}
|
|
3256
|
+
if (Is.date(value)) {
|
|
2797
3257
|
return value;
|
|
2798
3258
|
}
|
|
2799
|
-
|
|
3259
|
+
if (Is.number(value)) {
|
|
3260
|
+
const dt = new Date(value);
|
|
3261
|
+
dt.setFullYear(1970, 0, 1);
|
|
3262
|
+
return dt;
|
|
3263
|
+
}
|
|
3264
|
+
if (Is.string(value)) {
|
|
3265
|
+
const dt = new Date(value);
|
|
3266
|
+
if (!Number.isNaN(dt.getTime())) {
|
|
3267
|
+
const utc = Date.UTC(1970, 0, 1, dt.getUTCHours(), dt.getUTCMinutes(), dt.getUTCSeconds(), dt.getUTCMilliseconds());
|
|
3268
|
+
return new Date(utc);
|
|
3269
|
+
}
|
|
3270
|
+
}
|
|
2800
3271
|
}
|
|
2801
3272
|
/**
|
|
2802
|
-
*
|
|
2803
|
-
* @param
|
|
2804
|
-
* @
|
|
2805
|
-
* @
|
|
3273
|
+
* Coerce the value to an object.
|
|
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.
|
|
2806
3277
|
*/
|
|
2807
|
-
static
|
|
2808
|
-
if (Is.
|
|
2809
|
-
|
|
3278
|
+
static object(value) {
|
|
3279
|
+
if (Is.undefined(value)) {
|
|
3280
|
+
return value;
|
|
3281
|
+
}
|
|
3282
|
+
if (Is.object(value)) {
|
|
3283
|
+
return value;
|
|
3284
|
+
}
|
|
3285
|
+
if (Is.stringValue(value)) {
|
|
3286
|
+
try {
|
|
3287
|
+
return JSON.parse(value);
|
|
3288
|
+
}
|
|
3289
|
+
catch { }
|
|
2810
3290
|
}
|
|
2811
3291
|
}
|
|
2812
3292
|
/**
|
|
2813
|
-
*
|
|
2814
|
-
* @param
|
|
2815
|
-
* @
|
|
3293
|
+
* Coerce the value to a Uint8Array.
|
|
3294
|
+
* @param value The value to coerce.
|
|
3295
|
+
* @throws TypeError If the value can not be coerced.
|
|
3296
|
+
* @returns The value if it can be coerced.
|
|
2816
3297
|
*/
|
|
2817
|
-
static
|
|
2818
|
-
if (Is.
|
|
2819
|
-
|
|
3298
|
+
static uint8Array(value) {
|
|
3299
|
+
if (Is.undefined(value)) {
|
|
3300
|
+
return value;
|
|
3301
|
+
}
|
|
3302
|
+
if (Is.string(value)) {
|
|
3303
|
+
if (Is.stringHex(value.toLowerCase(), true)) {
|
|
3304
|
+
return Converter.hexToBytes(value.toLowerCase());
|
|
3305
|
+
}
|
|
3306
|
+
if (Is.stringBase64(value)) {
|
|
3307
|
+
return Converter.base64ToBytes(value);
|
|
3308
|
+
}
|
|
2820
3309
|
}
|
|
2821
3310
|
}
|
|
2822
3311
|
/**
|
|
2823
|
-
*
|
|
2824
|
-
* @param
|
|
2825
|
-
* @param
|
|
2826
|
-
* @returns The
|
|
2827
|
-
*/
|
|
2828
|
-
static
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
3312
|
+
* Coerces a value based on the coercion type.
|
|
3313
|
+
* @param value The value to coerce.
|
|
3314
|
+
* @param type The coercion type to perform.
|
|
3315
|
+
* @returns The coerced value.
|
|
3316
|
+
*/
|
|
3317
|
+
static byType(value, type) {
|
|
3318
|
+
switch (type) {
|
|
3319
|
+
case CoerceType.String:
|
|
3320
|
+
return Coerce.string(value);
|
|
3321
|
+
case CoerceType.Number:
|
|
3322
|
+
return Coerce.number(value);
|
|
3323
|
+
case CoerceType.Integer:
|
|
3324
|
+
return Coerce.integer(value);
|
|
3325
|
+
case CoerceType.BigInt:
|
|
3326
|
+
return Coerce.bigint(value);
|
|
3327
|
+
case CoerceType.Boolean:
|
|
3328
|
+
return Coerce.boolean(value);
|
|
3329
|
+
case CoerceType.Date:
|
|
3330
|
+
return Coerce.date(value);
|
|
3331
|
+
case CoerceType.DateTime:
|
|
3332
|
+
return Coerce.dateTime(value);
|
|
3333
|
+
case CoerceType.Time:
|
|
3334
|
+
return Coerce.time(value);
|
|
3335
|
+
case CoerceType.Object:
|
|
3336
|
+
return Coerce.object(value);
|
|
3337
|
+
case CoerceType.Uint8Array:
|
|
3338
|
+
return Coerce.uint8Array(value);
|
|
3339
|
+
default:
|
|
3340
|
+
return value;
|
|
2835
3341
|
}
|
|
2836
|
-
return obj;
|
|
2837
3342
|
}
|
|
3343
|
+
}
|
|
3344
|
+
|
|
3345
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3346
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3347
|
+
/**
|
|
3348
|
+
* Class to help with filenames.
|
|
3349
|
+
*/
|
|
3350
|
+
class FilenameHelper {
|
|
2838
3351
|
/**
|
|
2839
|
-
*
|
|
2840
|
-
* @param
|
|
2841
|
-
* @
|
|
2842
|
-
* @returns The partial object.
|
|
3352
|
+
* Replaces any unsafe characters in the filename.
|
|
3353
|
+
* @param filename The filename to make safe.
|
|
3354
|
+
* @returns The safe filename.
|
|
2843
3355
|
*/
|
|
2844
|
-
static
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
delete result[key];
|
|
2849
|
-
}
|
|
2850
|
-
return result;
|
|
3356
|
+
static safeFilename(filename) {
|
|
3357
|
+
let safe = Coerce.string(filename);
|
|
3358
|
+
if (Is.empty(safe)) {
|
|
3359
|
+
return "";
|
|
2851
3360
|
}
|
|
2852
|
-
|
|
3361
|
+
// Common non filename characters
|
|
3362
|
+
safe = safe.replace(/["*/:<>?\\|]/g, "_");
|
|
3363
|
+
// Windows non filename characters
|
|
3364
|
+
safe = safe.replace(/^(con|prn|aux|nul|com\d|lpt\d)$/i, "_");
|
|
3365
|
+
// Control characters
|
|
3366
|
+
safe = safe.replace(/[\u0000-\u001F\u0080-\u009F]/g, "_");
|
|
3367
|
+
// Relative paths
|
|
3368
|
+
safe = safe.replace(/^\.+/, "_");
|
|
3369
|
+
// Trailing periods
|
|
3370
|
+
safe = safe.replace(/\.+$/, "");
|
|
3371
|
+
return safe;
|
|
2853
3372
|
}
|
|
2854
3373
|
}
|
|
2855
3374
|
|
|
@@ -2883,7 +3402,7 @@ const CompressionType = {
|
|
|
2883
3402
|
*/
|
|
2884
3403
|
Gzip: "gzip",
|
|
2885
3404
|
/**
|
|
2886
|
-
*
|
|
3405
|
+
* Deflate.
|
|
2887
3406
|
*/
|
|
2888
3407
|
Deflate: "deflate"
|
|
2889
3408
|
};
|
|
@@ -3506,6 +4025,7 @@ class Validation {
|
|
|
3506
4025
|
* @param options Additional options for the validation.
|
|
3507
4026
|
* @param options.minLength The minimum length of the string.
|
|
3508
4027
|
* @param options.maxLength The maximum length of the string.
|
|
4028
|
+
* @param options.format Specific format to check.
|
|
3509
4029
|
* @returns True if the value is a valid string.
|
|
3510
4030
|
*/
|
|
3511
4031
|
static string(property, value, failures, fieldNameResource, options) {
|
|
@@ -3524,6 +4044,47 @@ class Validation {
|
|
|
3524
4044
|
const maxLimitDefined = Is.integer(maxLength);
|
|
3525
4045
|
const belowMin = minLimitDefined && value.length < minLength;
|
|
3526
4046
|
const aboveMax = maxLimitDefined && value.length > maxLength;
|
|
4047
|
+
if (options?.format === "base58" && !Is.stringBase58(value)) {
|
|
4048
|
+
failures.push({
|
|
4049
|
+
property,
|
|
4050
|
+
reason: "validation.beTextBase58",
|
|
4051
|
+
properties: {
|
|
4052
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4053
|
+
value
|
|
4054
|
+
}
|
|
4055
|
+
});
|
|
4056
|
+
}
|
|
4057
|
+
else if (options?.format === "base64" && !Is.stringBase64(value)) {
|
|
4058
|
+
failures.push({
|
|
4059
|
+
property,
|
|
4060
|
+
reason: "validation.beTextBase64",
|
|
4061
|
+
properties: {
|
|
4062
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4063
|
+
value
|
|
4064
|
+
}
|
|
4065
|
+
});
|
|
4066
|
+
}
|
|
4067
|
+
else if (options?.format === "hex" && !Is.stringHex(value)) {
|
|
4068
|
+
failures.push({
|
|
4069
|
+
property,
|
|
4070
|
+
reason: "validation.beTextHex",
|
|
4071
|
+
properties: {
|
|
4072
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4073
|
+
value
|
|
4074
|
+
}
|
|
4075
|
+
});
|
|
4076
|
+
}
|
|
4077
|
+
else if (Is.regexp(options?.format) && !options.format.test(value)) {
|
|
4078
|
+
failures.push({
|
|
4079
|
+
property,
|
|
4080
|
+
reason: "validation.beTextRegExp",
|
|
4081
|
+
properties: {
|
|
4082
|
+
fieldName: fieldNameResource ?? "validation.defaultFieldName",
|
|
4083
|
+
value,
|
|
4084
|
+
format: options?.format
|
|
4085
|
+
}
|
|
4086
|
+
});
|
|
4087
|
+
}
|
|
3527
4088
|
if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
|
|
3528
4089
|
failures.push({
|
|
3529
4090
|
property,
|
|
@@ -4187,16 +4748,19 @@ exports.AlreadyExistsError = AlreadyExistsError;
|
|
|
4187
4748
|
exports.ArrayHelper = ArrayHelper;
|
|
4188
4749
|
exports.AsyncCache = AsyncCache;
|
|
4189
4750
|
exports.Base32 = Base32;
|
|
4751
|
+
exports.Base58 = Base58;
|
|
4190
4752
|
exports.Base64 = Base64;
|
|
4191
4753
|
exports.Base64Url = Base64Url;
|
|
4192
4754
|
exports.BaseError = BaseError;
|
|
4193
4755
|
exports.BitString = BitString;
|
|
4194
4756
|
exports.Coerce = Coerce;
|
|
4757
|
+
exports.CoerceType = CoerceType;
|
|
4195
4758
|
exports.ComponentFactory = ComponentFactory;
|
|
4196
4759
|
exports.Compression = Compression;
|
|
4197
4760
|
exports.CompressionType = CompressionType;
|
|
4198
4761
|
exports.ConflictError = ConflictError;
|
|
4199
4762
|
exports.Converter = Converter;
|
|
4763
|
+
exports.EnvHelper = EnvHelper;
|
|
4200
4764
|
exports.ErrorHelper = ErrorHelper;
|
|
4201
4765
|
exports.Factory = Factory;
|
|
4202
4766
|
exports.FilenameHelper = FilenameHelper;
|