@twin.org/core 0.0.2-next.3 → 0.0.2-next.5
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 +893 -852
- package/dist/esm/index.mjs +893 -852
- package/dist/types/errors/alreadyExistsError.d.ts +2 -2
- package/dist/types/errors/baseError.d.ts +18 -5
- package/dist/types/errors/conflictError.d.ts +2 -2
- package/dist/types/errors/generalError.d.ts +2 -2
- package/dist/types/errors/notFoundError.d.ts +2 -2
- package/dist/types/errors/notSupportedError.d.ts +2 -2
- package/dist/types/errors/unauthorizedError.d.ts +2 -2
- package/dist/types/errors/unprocessableError.d.ts +2 -2
- package/dist/types/factories/factory.d.ts +1 -1
- package/dist/types/helpers/errorHelper.d.ts +3 -3
- package/dist/types/models/IComponent.d.ts +6 -15
- package/dist/types/models/IError.d.ts +2 -2
- package/docs/changelog.md +34 -0
- package/docs/reference/classes/AlreadyExistsError.md +66 -8
- package/docs/reference/classes/BaseError.md +62 -8
- package/docs/reference/classes/ConflictError.md +66 -8
- package/docs/reference/classes/EnvHelper.md +1 -1
- package/docs/reference/classes/ErrorHelper.md +3 -3
- package/docs/reference/classes/Factory.md +2 -2
- package/docs/reference/classes/GeneralError.md +66 -8
- package/docs/reference/classes/GuardError.md +63 -5
- package/docs/reference/classes/Guards.md +2 -2
- package/docs/reference/classes/I18n.md +2 -2
- package/docs/reference/classes/Is.md +2 -2
- package/docs/reference/classes/NotFoundError.md +66 -8
- package/docs/reference/classes/NotImplementedError.md +63 -5
- package/docs/reference/classes/NotSupportedError.md +66 -8
- package/docs/reference/classes/UnauthorizedError.md +66 -8
- package/docs/reference/classes/UnprocessableError.md +66 -8
- package/docs/reference/classes/Validation.md +1 -1
- package/docs/reference/classes/ValidationError.md +63 -5
- package/docs/reference/interfaces/IComponent.md +9 -21
- package/docs/reference/interfaces/IError.md +3 -3
- package/package.json +2 -2
package/dist/esm/index.mjs
CHANGED
|
@@ -629,31 +629,33 @@ class BaseError extends Error {
|
|
|
629
629
|
*/
|
|
630
630
|
properties;
|
|
631
631
|
/**
|
|
632
|
-
* The
|
|
632
|
+
* The cause of the error.
|
|
633
633
|
*/
|
|
634
|
-
|
|
634
|
+
cause;
|
|
635
635
|
/**
|
|
636
636
|
* Create a new instance of BaseError.
|
|
637
637
|
* @param name The name of the error.
|
|
638
638
|
* @param source The source of the error.
|
|
639
639
|
* @param message The message as a code.
|
|
640
640
|
* @param properties Any additional information for the error.
|
|
641
|
-
* @param
|
|
641
|
+
* @param cause The cause of error if we have wrapped another error.
|
|
642
642
|
*/
|
|
643
|
-
constructor(name, source, message, properties,
|
|
643
|
+
constructor(name, source, message, properties, cause) {
|
|
644
644
|
super(message);
|
|
645
645
|
this.name = name;
|
|
646
646
|
this.source = source;
|
|
647
|
+
this.cause = Is.notEmpty(cause) ? BaseError.fromError(cause).toJsonObject(true) : undefined;
|
|
648
|
+
this.properties = properties;
|
|
647
649
|
// If the message is camel case but has no namespace then prefix it
|
|
648
|
-
// with the source name in camel case
|
|
650
|
+
// with the source name in camel case.
|
|
649
651
|
if (Is.stringValue(source) &&
|
|
650
652
|
Is.stringValue(message) &&
|
|
651
653
|
!message.includes(".") &&
|
|
654
|
+
// This comparison checks that it is most likely a camel case name
|
|
655
|
+
// and not a free text error with a dot in it
|
|
652
656
|
StringHelper.camelCase(message) === message) {
|
|
653
657
|
this.message = `${StringHelper.camelCase(source)}.${message}`;
|
|
654
658
|
}
|
|
655
|
-
this.properties = properties;
|
|
656
|
-
this.inner = inner ? BaseError.fromError(inner).toJsonObject() : undefined;
|
|
657
659
|
}
|
|
658
660
|
/**
|
|
659
661
|
* Construct an error from an existing one.
|
|
@@ -665,7 +667,7 @@ class BaseError extends Error {
|
|
|
665
667
|
let message;
|
|
666
668
|
let source;
|
|
667
669
|
let properties;
|
|
668
|
-
let
|
|
670
|
+
let cause;
|
|
669
671
|
let stack;
|
|
670
672
|
if (Is.object(err) && Is.stringValue(err.error)) {
|
|
671
673
|
message = err.error;
|
|
@@ -683,8 +685,12 @@ class BaseError extends Error {
|
|
|
683
685
|
if (Is.notEmpty(err.properties)) {
|
|
684
686
|
properties = err.properties;
|
|
685
687
|
}
|
|
686
|
-
if (
|
|
687
|
-
|
|
688
|
+
if (BaseError.isAggregateError(err)) {
|
|
689
|
+
properties ??= {};
|
|
690
|
+
properties.errors = err.errors;
|
|
691
|
+
}
|
|
692
|
+
if (Is.notEmpty(err.cause)) {
|
|
693
|
+
cause = err.cause;
|
|
688
694
|
}
|
|
689
695
|
if (Is.notEmpty(err.stack)) {
|
|
690
696
|
stack = err.stack;
|
|
@@ -696,7 +702,7 @@ class BaseError extends Error {
|
|
|
696
702
|
else {
|
|
697
703
|
message = JSON.stringify(err);
|
|
698
704
|
}
|
|
699
|
-
const baseError = new BaseError(name, source ?? "", message ?? "", properties,
|
|
705
|
+
const baseError = new BaseError(name, source ?? "", message ?? "", properties, cause);
|
|
700
706
|
baseError.stack = stack;
|
|
701
707
|
return baseError;
|
|
702
708
|
}
|
|
@@ -709,10 +715,10 @@ class BaseError extends Error {
|
|
|
709
715
|
const flattened = [];
|
|
710
716
|
let e = BaseError.fromError(err).toJsonObject(true);
|
|
711
717
|
while (e) {
|
|
712
|
-
const
|
|
713
|
-
e.
|
|
718
|
+
const cause = e.cause;
|
|
719
|
+
e.cause = undefined;
|
|
714
720
|
flattened.push(e);
|
|
715
|
-
e =
|
|
721
|
+
e = cause;
|
|
716
722
|
}
|
|
717
723
|
return flattened;
|
|
718
724
|
}
|
|
@@ -727,8 +733,8 @@ class BaseError extends Error {
|
|
|
727
733
|
first = errors[0];
|
|
728
734
|
let current = first;
|
|
729
735
|
for (let i = 1; i < errors.length; i++) {
|
|
730
|
-
current.
|
|
731
|
-
current = current.
|
|
736
|
+
current.cause = errors[i];
|
|
737
|
+
current = current.cause;
|
|
732
738
|
}
|
|
733
739
|
}
|
|
734
740
|
return first;
|
|
@@ -802,7 +808,7 @@ class BaseError extends Error {
|
|
|
802
808
|
return BaseError.flatten(error).some(e => BaseError.isErrorCode(e, code));
|
|
803
809
|
}
|
|
804
810
|
/**
|
|
805
|
-
* Is the error empty.
|
|
811
|
+
* Is the error empty, i.e. does it have no message, source, properties, or cause?
|
|
806
812
|
* @param err The error to check for being empty.
|
|
807
813
|
* @returns True if the error is empty.
|
|
808
814
|
*/
|
|
@@ -810,7 +816,27 @@ class BaseError extends Error {
|
|
|
810
816
|
return (!Is.stringValue(err.message) &&
|
|
811
817
|
!Is.stringValue(err.source) &&
|
|
812
818
|
!Is.objectValue(err.properties) &&
|
|
813
|
-
Is.empty(err.
|
|
819
|
+
Is.empty(err.cause));
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Is the error an aggregate error.
|
|
823
|
+
* @param err The error to check for being an aggregate error.
|
|
824
|
+
* @returns True if the error is an aggregate error.
|
|
825
|
+
*/
|
|
826
|
+
static isAggregateError(err) {
|
|
827
|
+
return err instanceof AggregateError;
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Convert the aggregate error to an array of errors.
|
|
831
|
+
* @param err The error to convert.
|
|
832
|
+
* @param includeStackTrace Whether to include the error stack in the model, defaults to false.
|
|
833
|
+
* @returns The array of errors.
|
|
834
|
+
*/
|
|
835
|
+
static fromAggregate(err, includeStackTrace) {
|
|
836
|
+
if (BaseError.isAggregateError(err)) {
|
|
837
|
+
return err.errors.map(e => BaseError.fromError(e).toJsonObject(includeStackTrace));
|
|
838
|
+
}
|
|
839
|
+
return [BaseError.fromError(err).toJsonObject(includeStackTrace)];
|
|
814
840
|
}
|
|
815
841
|
/**
|
|
816
842
|
* Serialize the error to the error model.
|
|
@@ -834,8 +860,8 @@ class BaseError extends Error {
|
|
|
834
860
|
if ((includeStackTrace ?? false) && Is.stringValue(this.stack)) {
|
|
835
861
|
err.stack = this.stack;
|
|
836
862
|
}
|
|
837
|
-
if (Is.notEmpty(this.
|
|
838
|
-
err.
|
|
863
|
+
if (Is.notEmpty(this.cause)) {
|
|
864
|
+
err.cause = BaseError.fromError(this.cause).toJsonObject(includeStackTrace);
|
|
839
865
|
}
|
|
840
866
|
return err;
|
|
841
867
|
}
|
|
@@ -854,1062 +880,1073 @@ class GeneralError extends BaseError {
|
|
|
854
880
|
* @param source The source of the error.
|
|
855
881
|
* @param message The message as a code.
|
|
856
882
|
* @param properties Any additional information for the error.
|
|
857
|
-
* @param
|
|
883
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
858
884
|
*/
|
|
859
|
-
constructor(source, message, properties,
|
|
860
|
-
super(GeneralError.CLASS_NAME, source, message, properties,
|
|
885
|
+
constructor(source, message, properties, cause) {
|
|
886
|
+
super(GeneralError.CLASS_NAME, source, message, properties, cause);
|
|
861
887
|
}
|
|
862
888
|
}
|
|
863
889
|
|
|
864
|
-
// Copyright 2024 IOTA Stiftung.
|
|
865
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
866
|
-
/* eslint-disable no-bitwise */
|
|
867
890
|
/**
|
|
868
|
-
* Class to
|
|
891
|
+
* Class to handle errors which are triggered by data guards.
|
|
869
892
|
*/
|
|
870
|
-
class
|
|
893
|
+
class GuardError extends BaseError {
|
|
871
894
|
/**
|
|
872
895
|
* Runtime name for the class.
|
|
873
|
-
* @internal
|
|
874
|
-
*/
|
|
875
|
-
static _CLASS_NAME = "Base32";
|
|
876
|
-
/**
|
|
877
|
-
* Alphabet table for encoding.
|
|
878
|
-
* @internal
|
|
879
896
|
*/
|
|
880
|
-
static
|
|
881
|
-
/**
|
|
882
|
-
* Convert the base 32 string to a byte array.
|
|
883
|
-
* @param base32 The base32 string to convert.
|
|
884
|
-
* @returns The byte array.
|
|
885
|
-
* @throws If the input string contains a character not in the Base32 alphabet.
|
|
886
|
-
*/
|
|
887
|
-
static decode(base32) {
|
|
888
|
-
let bits = 0;
|
|
889
|
-
let value = 0;
|
|
890
|
-
base32 = base32.replace(/=+$/, "");
|
|
891
|
-
let index = 0;
|
|
892
|
-
const output = new Uint8Array(Math.trunc((base32.length * 5) / 8));
|
|
893
|
-
for (let i = 0; i < base32.length; i++) {
|
|
894
|
-
const idx = Base32._ALPHABET.indexOf(base32[i]);
|
|
895
|
-
if (idx === -1) {
|
|
896
|
-
throw new GeneralError(Base32._CLASS_NAME, "invalidCharacter", {
|
|
897
|
-
invalidCharacter: base32[i]
|
|
898
|
-
});
|
|
899
|
-
}
|
|
900
|
-
value = (value << 5) | idx;
|
|
901
|
-
bits += 5;
|
|
902
|
-
if (bits >= 8) {
|
|
903
|
-
output[index++] = (value >>> (bits - 8)) & 255;
|
|
904
|
-
bits -= 8;
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
return output;
|
|
908
|
-
}
|
|
897
|
+
static CLASS_NAME = "GuardError";
|
|
909
898
|
/**
|
|
910
|
-
*
|
|
911
|
-
* @param
|
|
912
|
-
* @
|
|
899
|
+
* Create a new instance of GuardError.
|
|
900
|
+
* @param source The source of the error.
|
|
901
|
+
* @param message The message as a code.
|
|
902
|
+
* @param propertyName The property which triggered the guard error for the item.
|
|
903
|
+
* @param propertyValue The property value which triggered the guard error for the item.
|
|
904
|
+
* @param propertyOptions The property options which might be allowed.
|
|
913
905
|
*/
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
bits += 8;
|
|
921
|
-
while (bits >= 5) {
|
|
922
|
-
output += Base32._ALPHABET[(value >>> (bits - 5)) & 31];
|
|
923
|
-
bits -= 5;
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
if (bits > 0) {
|
|
927
|
-
output += Base32._ALPHABET[(value << (5 - bits)) & 31];
|
|
928
|
-
}
|
|
929
|
-
while (output.length % 8 !== 0) {
|
|
930
|
-
output += "=";
|
|
931
|
-
}
|
|
932
|
-
return output;
|
|
906
|
+
constructor(source, message, propertyName, propertyValue, propertyOptions) {
|
|
907
|
+
super(GuardError.CLASS_NAME, source, message, {
|
|
908
|
+
property: propertyName ?? "property",
|
|
909
|
+
value: Is.undefined(propertyValue) ? "undefined" : propertyValue,
|
|
910
|
+
options: propertyOptions
|
|
911
|
+
});
|
|
933
912
|
}
|
|
934
913
|
}
|
|
935
914
|
|
|
936
915
|
/**
|
|
937
|
-
* Class to help with
|
|
916
|
+
* Class to help with arrays.
|
|
938
917
|
*/
|
|
939
|
-
class
|
|
940
|
-
/**
|
|
941
|
-
* Runtime name for the class.
|
|
942
|
-
* @internal
|
|
943
|
-
*/
|
|
944
|
-
static _CLASS_NAME = "Base58";
|
|
945
|
-
/**
|
|
946
|
-
* Alphabet table for encoding.
|
|
947
|
-
* @internal
|
|
948
|
-
*/
|
|
949
|
-
static _ALPHABET =
|
|
950
|
-
// cspell:disable-next-line
|
|
951
|
-
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
952
|
-
/**
|
|
953
|
-
* Reverse map for decoding.
|
|
954
|
-
* @internal
|
|
955
|
-
*/
|
|
956
|
-
static _ALPHABET_REVERSE = [
|
|
957
|
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
958
|
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
959
|
-
-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,
|
|
960
|
-
17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
|
|
961
|
-
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
|
|
962
|
-
57, -1, -1, -1, -1, -1
|
|
963
|
-
];
|
|
918
|
+
class ArrayHelper {
|
|
964
919
|
/**
|
|
965
|
-
*
|
|
966
|
-
* @param
|
|
967
|
-
* @
|
|
968
|
-
* @
|
|
920
|
+
* Do the two arrays match.
|
|
921
|
+
* @param arr1 The first array.
|
|
922
|
+
* @param arr2 The second array.
|
|
923
|
+
* @returns True if both arrays are empty of have the same values.
|
|
969
924
|
*/
|
|
970
|
-
static
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
if (base58[i] !== "1") {
|
|
974
|
-
break;
|
|
975
|
-
}
|
|
976
|
-
zeroes += 1;
|
|
925
|
+
static matches(arr1, arr2) {
|
|
926
|
+
if (Is.empty(arr1) && Is.empty(arr2)) {
|
|
927
|
+
return true;
|
|
977
928
|
}
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
let length = 0;
|
|
981
|
-
for (let i = zeroes; i < base58.length; i++) {
|
|
982
|
-
const ch = base58.charCodeAt(i);
|
|
983
|
-
if (ch & 0xff80) {
|
|
984
|
-
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
985
|
-
}
|
|
986
|
-
const val = Base58._ALPHABET_REVERSE[ch];
|
|
987
|
-
if (val === -1) {
|
|
988
|
-
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
989
|
-
}
|
|
990
|
-
let carry = val;
|
|
991
|
-
let j = 0;
|
|
992
|
-
for (let k = size - 1; k >= 0; k--, j++) {
|
|
993
|
-
if (carry === 0 && j >= length) {
|
|
994
|
-
break;
|
|
995
|
-
}
|
|
996
|
-
carry += b256[k] * 58;
|
|
997
|
-
b256[k] = carry;
|
|
998
|
-
carry >>>= 8;
|
|
999
|
-
}
|
|
1000
|
-
length = j;
|
|
929
|
+
if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
|
|
930
|
+
return false;
|
|
1001
931
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
for (j = 0; j < zeroes; j++) {
|
|
1005
|
-
out[j] = 0;
|
|
932
|
+
if (arr1.length !== arr2.length) {
|
|
933
|
+
return false;
|
|
1006
934
|
}
|
|
1007
|
-
let i =
|
|
1008
|
-
|
|
1009
|
-
|
|
935
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
936
|
+
if (arr1[i] !== arr2[i]) {
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
1010
939
|
}
|
|
1011
|
-
return
|
|
940
|
+
return true;
|
|
1012
941
|
}
|
|
1013
942
|
/**
|
|
1014
|
-
* Convert
|
|
1015
|
-
* @param
|
|
1016
|
-
* @returns The
|
|
943
|
+
* Convert an object or array to an array.
|
|
944
|
+
* @param value The object or array to convert.
|
|
945
|
+
* @returns The array.
|
|
1017
946
|
*/
|
|
1018
|
-
static
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
if (bytes[i] !== 0) {
|
|
1022
|
-
break;
|
|
1023
|
-
}
|
|
1024
|
-
zeroes += 1;
|
|
1025
|
-
}
|
|
1026
|
-
const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
|
|
1027
|
-
const b58 = new Uint8Array(size).fill(0);
|
|
1028
|
-
let length = 0;
|
|
1029
|
-
for (let i = zeroes; i < bytes.length; i++) {
|
|
1030
|
-
let carry = bytes[i];
|
|
1031
|
-
let j = 0;
|
|
1032
|
-
for (let k = size - 1; k >= 0; k--, j++) {
|
|
1033
|
-
if (carry === 0 && j >= length) {
|
|
1034
|
-
break;
|
|
1035
|
-
}
|
|
1036
|
-
carry += b58[k] * 256;
|
|
1037
|
-
b58[k] = carry % 58;
|
|
1038
|
-
carry = Math.trunc(carry / 58);
|
|
1039
|
-
}
|
|
1040
|
-
length = j;
|
|
1041
|
-
}
|
|
1042
|
-
let i = size - length;
|
|
1043
|
-
while (i < size && b58[i] === 0) {
|
|
1044
|
-
i += 1;
|
|
1045
|
-
}
|
|
1046
|
-
let str = "";
|
|
1047
|
-
for (let j = 0; j < zeroes; j++) {
|
|
1048
|
-
str += "1";
|
|
947
|
+
static fromObjectOrArray(value) {
|
|
948
|
+
if (Is.empty(value)) {
|
|
949
|
+
return undefined;
|
|
1049
950
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
951
|
+
if (Is.array(value)) {
|
|
952
|
+
return value;
|
|
1052
953
|
}
|
|
1053
|
-
return
|
|
954
|
+
return [value];
|
|
1054
955
|
}
|
|
1055
956
|
}
|
|
1056
957
|
|
|
1057
958
|
// Copyright 2024 IOTA Stiftung.
|
|
1058
959
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1059
|
-
/* eslint-disable no-bitwise */
|
|
1060
|
-
/* eslint-disable no-mixed-operators */
|
|
1061
960
|
/**
|
|
1062
|
-
* Class to
|
|
1063
|
-
* Sourced from https://github.com/beatgammit/base64-js.
|
|
961
|
+
* Class to handle guard operations for parameters.
|
|
1064
962
|
*/
|
|
1065
|
-
class
|
|
963
|
+
class Guards {
|
|
1066
964
|
/**
|
|
1067
|
-
*
|
|
1068
|
-
* @
|
|
965
|
+
* Is the property defined.
|
|
966
|
+
* @param source The source of the error.
|
|
967
|
+
* @param property The name of the property.
|
|
968
|
+
* @param value The value to test.
|
|
969
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1069
970
|
*/
|
|
1070
|
-
static
|
|
971
|
+
static defined(source, property, value) {
|
|
972
|
+
if (Is.undefined(value)) {
|
|
973
|
+
throw new GuardError(source, "guard.undefined", property, value);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
1071
976
|
/**
|
|
1072
|
-
*
|
|
1073
|
-
* @
|
|
977
|
+
* Is the property a string.
|
|
978
|
+
* @param source The source of the error.
|
|
979
|
+
* @param property The name of the property.
|
|
980
|
+
* @param value The value to test.
|
|
981
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1074
982
|
*/
|
|
1075
|
-
static
|
|
983
|
+
static string(source, property, value) {
|
|
984
|
+
if (!Is.string(value)) {
|
|
985
|
+
throw new GuardError(source, "guard.string", property, value);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
1076
988
|
/**
|
|
1077
|
-
*
|
|
1078
|
-
* @
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
"45": 62,
|
|
1083
|
-
"47": 63,
|
|
1084
|
-
"48": 52,
|
|
1085
|
-
"49": 53,
|
|
1086
|
-
"50": 54,
|
|
1087
|
-
"51": 55,
|
|
1088
|
-
"52": 56,
|
|
1089
|
-
"53": 57,
|
|
1090
|
-
"54": 58,
|
|
1091
|
-
"55": 59,
|
|
1092
|
-
"56": 60,
|
|
1093
|
-
"57": 61,
|
|
1094
|
-
"65": 0,
|
|
1095
|
-
"66": 1,
|
|
1096
|
-
"67": 2,
|
|
1097
|
-
"68": 3,
|
|
1098
|
-
"69": 4,
|
|
1099
|
-
"70": 5,
|
|
1100
|
-
"71": 6,
|
|
1101
|
-
"72": 7,
|
|
1102
|
-
"73": 8,
|
|
1103
|
-
"74": 9,
|
|
1104
|
-
"75": 10,
|
|
1105
|
-
"76": 11,
|
|
1106
|
-
"77": 12,
|
|
1107
|
-
"78": 13,
|
|
1108
|
-
"79": 14,
|
|
1109
|
-
"80": 15,
|
|
1110
|
-
"81": 16,
|
|
1111
|
-
"82": 17,
|
|
1112
|
-
"83": 18,
|
|
1113
|
-
"84": 19,
|
|
1114
|
-
"85": 20,
|
|
1115
|
-
"86": 21,
|
|
1116
|
-
"87": 22,
|
|
1117
|
-
"88": 23,
|
|
1118
|
-
"89": 24,
|
|
1119
|
-
"90": 25,
|
|
1120
|
-
"95": 63,
|
|
1121
|
-
"97": 26,
|
|
1122
|
-
"98": 27,
|
|
1123
|
-
"99": 28,
|
|
1124
|
-
"100": 29,
|
|
1125
|
-
"101": 30,
|
|
1126
|
-
"102": 31,
|
|
1127
|
-
"103": 32,
|
|
1128
|
-
"104": 33,
|
|
1129
|
-
"105": 34,
|
|
1130
|
-
"106": 35,
|
|
1131
|
-
"107": 36,
|
|
1132
|
-
"108": 37,
|
|
1133
|
-
"109": 38,
|
|
1134
|
-
"110": 39,
|
|
1135
|
-
"111": 40,
|
|
1136
|
-
"112": 41,
|
|
1137
|
-
"113": 42,
|
|
1138
|
-
"114": 43,
|
|
1139
|
-
"115": 44,
|
|
1140
|
-
"116": 45,
|
|
1141
|
-
"117": 46,
|
|
1142
|
-
"118": 47,
|
|
1143
|
-
"119": 48,
|
|
1144
|
-
"120": 49,
|
|
1145
|
-
"121": 50,
|
|
1146
|
-
"122": 51
|
|
1147
|
-
};
|
|
1148
|
-
/**
|
|
1149
|
-
* Get the byte length of the data.
|
|
1150
|
-
* @param base64 The base64 string.
|
|
1151
|
-
* @returns The byte length of the data.
|
|
989
|
+
* Is the property a string with a value.
|
|
990
|
+
* @param source The source of the error.
|
|
991
|
+
* @param property The name of the property.
|
|
992
|
+
* @param value The value to test.
|
|
993
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1152
994
|
*/
|
|
1153
|
-
static
|
|
1154
|
-
|
|
1155
|
-
|
|
995
|
+
static stringValue(source, property, value) {
|
|
996
|
+
if (!Is.string(value)) {
|
|
997
|
+
throw new GuardError(source, "guard.string", property, value);
|
|
998
|
+
}
|
|
999
|
+
if (value.length === 0) {
|
|
1000
|
+
throw new GuardError(source, "guard.stringEmpty", property, value);
|
|
1001
|
+
}
|
|
1156
1002
|
}
|
|
1157
1003
|
/**
|
|
1158
|
-
*
|
|
1159
|
-
* @param
|
|
1160
|
-
* @
|
|
1004
|
+
* Is the property a JSON value.
|
|
1005
|
+
* @param source The source of the error.
|
|
1006
|
+
* @param property The name of the property.
|
|
1007
|
+
* @param value The value to test.
|
|
1008
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1161
1009
|
*/
|
|
1162
|
-
static
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
const validLen = lens[0];
|
|
1166
|
-
const placeHoldersLen = lens[1];
|
|
1167
|
-
const arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));
|
|
1168
|
-
let curByte = 0;
|
|
1169
|
-
// if there are placeholders, only get up to the last complete 4 chars
|
|
1170
|
-
const len = placeHoldersLen > 0 ? validLen - 4 : validLen;
|
|
1171
|
-
let i;
|
|
1172
|
-
for (i = 0; i < len; i += 4) {
|
|
1173
|
-
tmp =
|
|
1174
|
-
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |
|
|
1175
|
-
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |
|
|
1176
|
-
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |
|
|
1177
|
-
Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];
|
|
1178
|
-
arr[curByte++] = (tmp >> 16) & 0xff;
|
|
1179
|
-
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
1180
|
-
arr[curByte++] = tmp & 0xff;
|
|
1010
|
+
static json(source, property, value) {
|
|
1011
|
+
if (!Is.json(value)) {
|
|
1012
|
+
throw new GuardError(source, "guard.stringJson", property, value);
|
|
1181
1013
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Is the property a base64 string.
|
|
1017
|
+
* @param source The source of the error.
|
|
1018
|
+
* @param property The name of the property.
|
|
1019
|
+
* @param value The value to test.
|
|
1020
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1021
|
+
*/
|
|
1022
|
+
static stringBase64(source, property, value) {
|
|
1023
|
+
if (!Is.stringBase64(value)) {
|
|
1024
|
+
throw new GuardError(source, "guard.base64", property, value);
|
|
1187
1025
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Is the property a base64 url string.
|
|
1029
|
+
* @param source The source of the error.
|
|
1030
|
+
* @param property The name of the property.
|
|
1031
|
+
* @param value The value to test.
|
|
1032
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1033
|
+
*/
|
|
1034
|
+
static stringBase64Url(source, property, value) {
|
|
1035
|
+
if (!Is.stringBase64Url(value)) {
|
|
1036
|
+
throw new GuardError(source, "guard.base64Url", property, value);
|
|
1195
1037
|
}
|
|
1196
|
-
return arr;
|
|
1197
1038
|
}
|
|
1198
1039
|
/**
|
|
1199
|
-
*
|
|
1200
|
-
* @param
|
|
1201
|
-
* @
|
|
1040
|
+
* Is the property a base58 string.
|
|
1041
|
+
* @param source The source of the error.
|
|
1042
|
+
* @param property The name of the property.
|
|
1043
|
+
* @param value The value to test.
|
|
1044
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1202
1045
|
*/
|
|
1203
|
-
static
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
|
|
1207
|
-
const parts = [];
|
|
1208
|
-
const maxChunkLength = 16383; // must be multiple of 3
|
|
1209
|
-
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
1210
|
-
for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
1211
|
-
parts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));
|
|
1046
|
+
static stringBase58(source, property, value) {
|
|
1047
|
+
if (!Is.stringBase58(value)) {
|
|
1048
|
+
throw new GuardError(source, "guard.base58", property, value);
|
|
1212
1049
|
}
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Is the property a string with a hex value.
|
|
1053
|
+
* @param source The source of the error.
|
|
1054
|
+
* @param property The name of the property.
|
|
1055
|
+
* @param value The value to test.
|
|
1056
|
+
* @param allowPrefix Allow the hex to have the 0x prefix.
|
|
1057
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1058
|
+
*/
|
|
1059
|
+
static stringHex(source, property, value, allowPrefix = false) {
|
|
1060
|
+
Guards.stringValue(source, property, value);
|
|
1061
|
+
if (!HexHelper.isHex(value, allowPrefix)) {
|
|
1062
|
+
throw new GuardError(source, "guard.stringHex", property, value);
|
|
1217
1063
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Is the property a string with a hex value with fixed length.
|
|
1067
|
+
* @param source The source of the error.
|
|
1068
|
+
* @param property The name of the property.
|
|
1069
|
+
* @param value The value to test.
|
|
1070
|
+
* @param length The length of the string to match.
|
|
1071
|
+
* @param allowPrefix Allow the hex to have the 0x prefix.
|
|
1072
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1073
|
+
*/
|
|
1074
|
+
static stringHexLength(source, property, value, length, allowPrefix = false) {
|
|
1075
|
+
Guards.stringHex(source, property, value, allowPrefix);
|
|
1076
|
+
if (HexHelper.stripPrefix(value).length !== length) {
|
|
1077
|
+
throw new GuardError(source, "guard.stringHexLength", property, value.length, length.toString());
|
|
1221
1078
|
}
|
|
1222
|
-
return parts.join("");
|
|
1223
1079
|
}
|
|
1224
1080
|
/**
|
|
1225
|
-
*
|
|
1226
|
-
* @param
|
|
1227
|
-
* @param
|
|
1228
|
-
* @
|
|
1229
|
-
* @
|
|
1081
|
+
* Is the property a number.
|
|
1082
|
+
* @param source The source of the error.
|
|
1083
|
+
* @param property The name of the property.
|
|
1084
|
+
* @param value The value to test.
|
|
1085
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1230
1086
|
*/
|
|
1231
|
-
static
|
|
1232
|
-
|
|
1087
|
+
static number(source, property, value) {
|
|
1088
|
+
if (!Is.number(value)) {
|
|
1089
|
+
throw new GuardError(source, "guard.number", property, value);
|
|
1090
|
+
}
|
|
1233
1091
|
}
|
|
1234
1092
|
/**
|
|
1235
|
-
*
|
|
1236
|
-
* @param
|
|
1237
|
-
* @
|
|
1238
|
-
* @
|
|
1093
|
+
* Is the property an integer.
|
|
1094
|
+
* @param source The source of the error.
|
|
1095
|
+
* @param property The name of the property.
|
|
1096
|
+
* @param value The value to test.
|
|
1097
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1239
1098
|
*/
|
|
1240
|
-
static
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
throw new GeneralError(Base64._CLASS_NAME, "length4Multiple", { value: len });
|
|
1099
|
+
static integer(source, property, value) {
|
|
1100
|
+
if (!Is.integer(value)) {
|
|
1101
|
+
throw new GuardError(source, "guard.integer", property, value);
|
|
1244
1102
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Is the property a bigint.
|
|
1106
|
+
* @param source The source of the error.
|
|
1107
|
+
* @param property The name of the property.
|
|
1108
|
+
* @param value The value to test.
|
|
1109
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1110
|
+
*/
|
|
1111
|
+
static bigint(source, property, value) {
|
|
1112
|
+
if (!Is.bigint(value)) {
|
|
1113
|
+
throw new GuardError(source, "guard.bigint", property, value);
|
|
1250
1114
|
}
|
|
1251
|
-
const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);
|
|
1252
|
-
return [validLen, placeHoldersLen];
|
|
1253
1115
|
}
|
|
1254
1116
|
/**
|
|
1255
|
-
*
|
|
1256
|
-
* @param
|
|
1257
|
-
* @
|
|
1258
|
-
* @
|
|
1117
|
+
* Is the property a boolean.
|
|
1118
|
+
* @param source The source of the error.
|
|
1119
|
+
* @param property The name of the property.
|
|
1120
|
+
* @param value The value to test.
|
|
1121
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1259
1122
|
*/
|
|
1260
|
-
static
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
Base64._LOOKUP[num & 0x3f]);
|
|
1123
|
+
static boolean(source, property, value) {
|
|
1124
|
+
if (!Is.boolean(value)) {
|
|
1125
|
+
throw new GuardError(source, "guard.boolean", property, value);
|
|
1126
|
+
}
|
|
1265
1127
|
}
|
|
1266
1128
|
/**
|
|
1267
|
-
*
|
|
1268
|
-
* @param
|
|
1269
|
-
* @param
|
|
1270
|
-
* @param
|
|
1271
|
-
* @
|
|
1272
|
-
* @internal
|
|
1129
|
+
* Is the property a date.
|
|
1130
|
+
* @param source The source of the error.
|
|
1131
|
+
* @param property The name of the property.
|
|
1132
|
+
* @param value The value to test.
|
|
1133
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1273
1134
|
*/
|
|
1274
|
-
static
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
for (let i = start; i < end; i += 3) {
|
|
1278
|
-
tmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);
|
|
1279
|
-
output.push(Base64.tripletToBase64(tmp));
|
|
1135
|
+
static date(source, property, value) {
|
|
1136
|
+
if (!Is.date(value)) {
|
|
1137
|
+
throw new GuardError(source, "guard.date", property, value);
|
|
1280
1138
|
}
|
|
1281
|
-
return output.join("");
|
|
1282
1139
|
}
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
// Copyright 2024 IOTA Stiftung.
|
|
1286
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
1287
|
-
/**
|
|
1288
|
-
* Class to help with base64 URL Encoding/Decoding.
|
|
1289
|
-
* https://www.rfc-editor.org/rfc/rfc4648#section-5.
|
|
1290
|
-
*/
|
|
1291
|
-
class Base64Url {
|
|
1292
1140
|
/**
|
|
1293
|
-
*
|
|
1294
|
-
* @param
|
|
1295
|
-
* @
|
|
1141
|
+
* Is the property a timestamp in milliseconds.
|
|
1142
|
+
* @param source The source of the error.
|
|
1143
|
+
* @param property The name of the property.
|
|
1144
|
+
* @param value The value to test.
|
|
1145
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1296
1146
|
*/
|
|
1297
|
-
static
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
if (base64.length > 0 && !base64.endsWith("=")) {
|
|
1301
|
-
const placeHoldersLen = 4 - (base64.length % 4);
|
|
1302
|
-
if (placeHoldersLen > 0 && placeHoldersLen < 4) {
|
|
1303
|
-
base64 = base64.padEnd(base64.length + placeHoldersLen, "=");
|
|
1304
|
-
}
|
|
1147
|
+
static timestampMilliseconds(source, property, value) {
|
|
1148
|
+
if (!Is.timestampMilliseconds(value)) {
|
|
1149
|
+
throw new GuardError(source, "guard.timestampMilliseconds", property, value);
|
|
1305
1150
|
}
|
|
1306
|
-
base64 = base64.replace(/-/g, "+").replace(/_/g, "/");
|
|
1307
|
-
return Base64.decode(base64);
|
|
1308
1151
|
}
|
|
1309
1152
|
/**
|
|
1310
|
-
*
|
|
1311
|
-
* @param
|
|
1312
|
-
* @
|
|
1153
|
+
* Is the property a timestamp in seconds.
|
|
1154
|
+
* @param source The source of the error.
|
|
1155
|
+
* @param property The name of the property.
|
|
1156
|
+
* @param value The value to test.
|
|
1157
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1313
1158
|
*/
|
|
1314
|
-
static
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1159
|
+
static timestampSeconds(source, property, value) {
|
|
1160
|
+
if (!Is.timestampSeconds(value)) {
|
|
1161
|
+
throw new GuardError(source, "guard.timestampSeconds", property, value);
|
|
1162
|
+
}
|
|
1318
1163
|
}
|
|
1319
|
-
}
|
|
1320
|
-
|
|
1321
|
-
/**
|
|
1322
|
-
* Class to handle errors which are triggered by data already existing.
|
|
1323
|
-
*/
|
|
1324
|
-
class AlreadyExistsError extends BaseError {
|
|
1325
1164
|
/**
|
|
1326
|
-
*
|
|
1165
|
+
* Is the property an object.
|
|
1166
|
+
* @param source The source of the error.
|
|
1167
|
+
* @param property The name of the property.
|
|
1168
|
+
* @param value The value to test.
|
|
1169
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1327
1170
|
*/
|
|
1328
|
-
static
|
|
1171
|
+
static object(source, property, value) {
|
|
1172
|
+
if (Is.undefined(value)) {
|
|
1173
|
+
throw new GuardError(source, "guard.objectUndefined", property, value);
|
|
1174
|
+
}
|
|
1175
|
+
if (!Is.object(value)) {
|
|
1176
|
+
throw new GuardError(source, "guard.object", property, value);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1329
1179
|
/**
|
|
1330
|
-
*
|
|
1180
|
+
* Is the property is an object with at least one property.
|
|
1331
1181
|
* @param source The source of the error.
|
|
1332
|
-
* @param
|
|
1333
|
-
* @param
|
|
1334
|
-
* @
|
|
1182
|
+
* @param property The name of the property.
|
|
1183
|
+
* @param value The value to test.
|
|
1184
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1335
1185
|
*/
|
|
1336
|
-
|
|
1337
|
-
|
|
1186
|
+
static objectValue(source, property, value) {
|
|
1187
|
+
if (Is.undefined(value)) {
|
|
1188
|
+
throw new GuardError(source, "guard.objectUndefined", property, value);
|
|
1189
|
+
}
|
|
1190
|
+
if (!Is.object(value)) {
|
|
1191
|
+
throw new GuardError(source, "guard.object", property, value);
|
|
1192
|
+
}
|
|
1193
|
+
if (Object.keys(value || {}).length === 0) {
|
|
1194
|
+
throw new GuardError(source, "guard.objectValue", property, value);
|
|
1195
|
+
}
|
|
1338
1196
|
}
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* Class to handle errors which are triggered by conflicting data.
|
|
1343
|
-
*/
|
|
1344
|
-
class ConflictError extends BaseError {
|
|
1345
1197
|
/**
|
|
1346
|
-
*
|
|
1198
|
+
* Is the property is an array.
|
|
1199
|
+
* @param source The source of the error.
|
|
1200
|
+
* @param property The name of the property.
|
|
1201
|
+
* @param value The value to test.
|
|
1202
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1347
1203
|
*/
|
|
1348
|
-
static
|
|
1204
|
+
static array(source, property, value) {
|
|
1205
|
+
if (!Is.array(value)) {
|
|
1206
|
+
throw new GuardError(source, "guard.array", property, value);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1349
1209
|
/**
|
|
1350
|
-
*
|
|
1210
|
+
* Is the property is an array with at least one item.
|
|
1351
1211
|
* @param source The source of the error.
|
|
1352
|
-
* @param
|
|
1353
|
-
* @param
|
|
1354
|
-
* @
|
|
1355
|
-
* @param inner The inner error if we have wrapped another error.
|
|
1212
|
+
* @param property The name of the property.
|
|
1213
|
+
* @param value The value to test.
|
|
1214
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1356
1215
|
*/
|
|
1357
|
-
|
|
1358
|
-
|
|
1216
|
+
static arrayValue(source, property, value) {
|
|
1217
|
+
if (!Is.array(value)) {
|
|
1218
|
+
throw new GuardError(source, "guard.array", property, value);
|
|
1219
|
+
}
|
|
1220
|
+
if (value.length === 0) {
|
|
1221
|
+
throw new GuardError(source, "guard.arrayValue", property, value);
|
|
1222
|
+
}
|
|
1359
1223
|
}
|
|
1360
|
-
}
|
|
1361
|
-
|
|
1362
|
-
/**
|
|
1363
|
-
* Class to handle errors which are triggered by data guards.
|
|
1364
|
-
*/
|
|
1365
|
-
class GuardError extends BaseError {
|
|
1366
1224
|
/**
|
|
1367
|
-
*
|
|
1225
|
+
* Is the property one of a list of items.
|
|
1226
|
+
* @param source The source of the error.
|
|
1227
|
+
* @param property The name of the property.
|
|
1228
|
+
* @param value The value to test.
|
|
1229
|
+
* @param options The options the value must be one of.
|
|
1230
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1368
1231
|
*/
|
|
1369
|
-
static
|
|
1232
|
+
static arrayOneOf(source, property, value, options) {
|
|
1233
|
+
if (!Is.array(options)) {
|
|
1234
|
+
throw new GuardError(source, "guard.array", property, value);
|
|
1235
|
+
}
|
|
1236
|
+
if (!options.includes(value)) {
|
|
1237
|
+
throw new GuardError(source, "guard.arrayOneOf", property, value, options.join(", "));
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1370
1240
|
/**
|
|
1371
|
-
*
|
|
1241
|
+
* Does the array start with the specified data.
|
|
1372
1242
|
* @param source The source of the error.
|
|
1373
|
-
* @param
|
|
1374
|
-
* @param
|
|
1375
|
-
* @param
|
|
1376
|
-
* @
|
|
1243
|
+
* @param property The name of the property.
|
|
1244
|
+
* @param value The value to test.
|
|
1245
|
+
* @param startValues The values that must start the array.
|
|
1246
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1377
1247
|
*/
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1248
|
+
static arrayStartsWith(source, property, value, startValues) {
|
|
1249
|
+
if (!Is.arrayValue(value)) {
|
|
1250
|
+
throw new GuardError(source, "guard.array", property, value);
|
|
1251
|
+
}
|
|
1252
|
+
const startValuesArray = ArrayHelper.fromObjectOrArray(startValues);
|
|
1253
|
+
if (!Is.arrayValue(startValuesArray)) {
|
|
1254
|
+
throw new GuardError(source, "guard.array", property, startValuesArray);
|
|
1255
|
+
}
|
|
1256
|
+
for (let i = 0; i < startValuesArray.length; i++) {
|
|
1257
|
+
if (value[i] !== startValuesArray[i]) {
|
|
1258
|
+
throw new GuardError(source, "guard.arrayStartsWith", property, value, startValuesArray.join(", "));
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1384
1261
|
}
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
/**
|
|
1388
|
-
* Class to handle errors which are triggered by data not being found.
|
|
1389
|
-
*/
|
|
1390
|
-
class NotFoundError extends BaseError {
|
|
1391
1262
|
/**
|
|
1392
|
-
*
|
|
1263
|
+
* Does the array end with the specified data.
|
|
1264
|
+
* @param source The source of the error.
|
|
1265
|
+
* @param property The name of the property.
|
|
1266
|
+
* @param value The value to test.
|
|
1267
|
+
* @param endValues The values that must end the array.
|
|
1268
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1393
1269
|
*/
|
|
1394
|
-
static
|
|
1270
|
+
static arrayEndsWith(source, property, value, endValues) {
|
|
1271
|
+
if (!Is.arrayValue(value)) {
|
|
1272
|
+
throw new GuardError(source, "guard.array", property, value);
|
|
1273
|
+
}
|
|
1274
|
+
const endValuesArray = ArrayHelper.fromObjectOrArray(endValues);
|
|
1275
|
+
if (!Is.arrayValue(endValuesArray)) {
|
|
1276
|
+
throw new GuardError(source, "guard.array", property, endValuesArray);
|
|
1277
|
+
}
|
|
1278
|
+
for (let i = 0; i < endValuesArray.length; i++) {
|
|
1279
|
+
if (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {
|
|
1280
|
+
throw new GuardError(source, "guard.arrayEndsWith", property, value, endValuesArray.join(", "));
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1395
1284
|
/**
|
|
1396
|
-
*
|
|
1285
|
+
* Is the property a Uint8Array.
|
|
1397
1286
|
* @param source The source of the error.
|
|
1398
|
-
* @param
|
|
1399
|
-
* @param
|
|
1400
|
-
* @
|
|
1287
|
+
* @param property The name of the property.
|
|
1288
|
+
* @param value The value to test.
|
|
1289
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1401
1290
|
*/
|
|
1402
|
-
|
|
1403
|
-
|
|
1291
|
+
static uint8Array(source, property, value) {
|
|
1292
|
+
if (!Is.uint8Array(value)) {
|
|
1293
|
+
throw new GuardError(source, "guard.uint8Array", property, value);
|
|
1294
|
+
}
|
|
1404
1295
|
}
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
/**
|
|
1408
|
-
* Class to handle errors.
|
|
1409
|
-
*/
|
|
1410
|
-
class NotImplementedError extends BaseError {
|
|
1411
1296
|
/**
|
|
1412
|
-
*
|
|
1297
|
+
* Is the property a function.
|
|
1298
|
+
* @param source The source of the error.
|
|
1299
|
+
* @param property The name of the property.
|
|
1300
|
+
* @param value The value to test.
|
|
1301
|
+
* @returns True if the value is a function.
|
|
1302
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1413
1303
|
*/
|
|
1414
|
-
static
|
|
1304
|
+
static function(source, property, value) {
|
|
1305
|
+
if (!Is.function(value)) {
|
|
1306
|
+
throw new GuardError(source, "guard.function", property, value);
|
|
1307
|
+
}
|
|
1308
|
+
return true;
|
|
1309
|
+
}
|
|
1415
1310
|
/**
|
|
1416
|
-
*
|
|
1311
|
+
* Is the property a string formatted as an email address.
|
|
1417
1312
|
* @param source The source of the error.
|
|
1418
|
-
* @param
|
|
1313
|
+
* @param property The name of the property.
|
|
1314
|
+
* @param value The value to test.
|
|
1315
|
+
* @throws GuardError If the value does not match the assertion.
|
|
1419
1316
|
*/
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
}
|
|
1317
|
+
static email(source, property, value) {
|
|
1318
|
+
if (!Is.email(value)) {
|
|
1319
|
+
throw new GuardError(source, "guard.email", property, value);
|
|
1320
|
+
}
|
|
1424
1321
|
}
|
|
1425
1322
|
}
|
|
1426
1323
|
|
|
1324
|
+
// Copyright 2024 IOTA Stiftung.
|
|
1325
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
1326
|
+
/* eslint-disable no-bitwise */
|
|
1427
1327
|
/**
|
|
1428
|
-
* Class to
|
|
1328
|
+
* Class to help with base63 Encoding/Decoding.
|
|
1429
1329
|
*/
|
|
1430
|
-
class
|
|
1330
|
+
class Base32 {
|
|
1431
1331
|
/**
|
|
1432
1332
|
* Runtime name for the class.
|
|
1333
|
+
* @internal
|
|
1433
1334
|
*/
|
|
1434
|
-
static
|
|
1335
|
+
static _CLASS_NAME = "Base32";
|
|
1435
1336
|
/**
|
|
1436
|
-
*
|
|
1437
|
-
* @
|
|
1438
|
-
* @param message The message as a code.
|
|
1439
|
-
* @param inner The inner error if we have wrapped another error.
|
|
1337
|
+
* Alphabet table for encoding.
|
|
1338
|
+
* @internal
|
|
1440
1339
|
*/
|
|
1441
|
-
|
|
1442
|
-
super(NotSupportedError.CLASS_NAME, source, message, undefined, inner);
|
|
1443
|
-
}
|
|
1444
|
-
}
|
|
1445
|
-
|
|
1446
|
-
/**
|
|
1447
|
-
* Class to handle errors which are triggered by access not being unauthorized.
|
|
1448
|
-
*/
|
|
1449
|
-
class UnauthorizedError extends BaseError {
|
|
1340
|
+
static _ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
1450
1341
|
/**
|
|
1451
|
-
*
|
|
1342
|
+
* Convert the base 32 string to a byte array.
|
|
1343
|
+
* @param base32 The base32 string to convert.
|
|
1344
|
+
* @returns The byte array.
|
|
1345
|
+
* @throws If the input string contains a character not in the Base32 alphabet.
|
|
1452
1346
|
*/
|
|
1453
|
-
static
|
|
1347
|
+
static decode(base32) {
|
|
1348
|
+
Guards.string(Base32._CLASS_NAME, "base32", base32);
|
|
1349
|
+
let bits = 0;
|
|
1350
|
+
let value = 0;
|
|
1351
|
+
base32 = base32.replace(/=+$/, "");
|
|
1352
|
+
let index = 0;
|
|
1353
|
+
const output = new Uint8Array(Math.trunc((base32.length * 5) / 8));
|
|
1354
|
+
for (let i = 0; i < base32.length; i++) {
|
|
1355
|
+
const idx = Base32._ALPHABET.indexOf(base32[i]);
|
|
1356
|
+
if (idx === -1) {
|
|
1357
|
+
throw new GeneralError(Base32._CLASS_NAME, "invalidCharacter", {
|
|
1358
|
+
invalidCharacter: base32[i]
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
value = (value << 5) | idx;
|
|
1362
|
+
bits += 5;
|
|
1363
|
+
if (bits >= 8) {
|
|
1364
|
+
output[index++] = (value >>> (bits - 8)) & 255;
|
|
1365
|
+
bits -= 8;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
return output;
|
|
1369
|
+
}
|
|
1454
1370
|
/**
|
|
1455
|
-
*
|
|
1456
|
-
* @param
|
|
1457
|
-
* @
|
|
1458
|
-
* @param inner The inner error if we have wrapped another error.
|
|
1371
|
+
* Convert a byte array to base 32.
|
|
1372
|
+
* @param bytes The byte array to convert.
|
|
1373
|
+
* @returns The data as base32 string.
|
|
1459
1374
|
*/
|
|
1460
|
-
|
|
1461
|
-
|
|
1375
|
+
static encode(bytes) {
|
|
1376
|
+
Guards.uint8Array(Base32._CLASS_NAME, "bytes", bytes);
|
|
1377
|
+
let bits = 0;
|
|
1378
|
+
let value = 0;
|
|
1379
|
+
let output = "";
|
|
1380
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
1381
|
+
value = (value << 8) | bytes[i];
|
|
1382
|
+
bits += 8;
|
|
1383
|
+
while (bits >= 5) {
|
|
1384
|
+
output += Base32._ALPHABET[(value >>> (bits - 5)) & 31];
|
|
1385
|
+
bits -= 5;
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
if (bits > 0) {
|
|
1389
|
+
output += Base32._ALPHABET[(value << (5 - bits)) & 31];
|
|
1390
|
+
}
|
|
1391
|
+
while (output.length % 8 !== 0) {
|
|
1392
|
+
output += "=";
|
|
1393
|
+
}
|
|
1394
|
+
return output;
|
|
1462
1395
|
}
|
|
1463
1396
|
}
|
|
1464
1397
|
|
|
1465
1398
|
/**
|
|
1466
|
-
* Class to
|
|
1399
|
+
* Class to help with base58 Encoding/Decoding.
|
|
1467
1400
|
*/
|
|
1468
|
-
class
|
|
1401
|
+
class Base58 {
|
|
1469
1402
|
/**
|
|
1470
1403
|
* Runtime name for the class.
|
|
1404
|
+
* @internal
|
|
1471
1405
|
*/
|
|
1472
|
-
static
|
|
1473
|
-
/**
|
|
1474
|
-
* Create a new instance of UnprocessableError.
|
|
1475
|
-
* @param source The source of the error.
|
|
1476
|
-
* @param message The message as a code.
|
|
1477
|
-
* @param properties Any additional information for the error.
|
|
1478
|
-
* @param inner The inner error if we have wrapped another error.
|
|
1479
|
-
*/
|
|
1480
|
-
constructor(source, message, properties, inner) {
|
|
1481
|
-
super(UnprocessableError.CLASS_NAME, source, message, properties, inner);
|
|
1482
|
-
}
|
|
1483
|
-
}
|
|
1484
|
-
|
|
1485
|
-
/**
|
|
1486
|
-
* Class to handle errors which are triggered by entity validation.
|
|
1487
|
-
*/
|
|
1488
|
-
class ValidationError extends BaseError {
|
|
1406
|
+
static _CLASS_NAME = "Base58";
|
|
1489
1407
|
/**
|
|
1490
|
-
*
|
|
1408
|
+
* Alphabet table for encoding.
|
|
1409
|
+
* @internal
|
|
1491
1410
|
*/
|
|
1492
|
-
static
|
|
1411
|
+
static _ALPHABET =
|
|
1412
|
+
// cspell:disable-next-line
|
|
1413
|
+
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
1493
1414
|
/**
|
|
1494
|
-
*
|
|
1495
|
-
* @
|
|
1496
|
-
* @param validationObject The object that failed validation.
|
|
1497
|
-
* @param validationFailures The validation failures.
|
|
1415
|
+
* Reverse map for decoding.
|
|
1416
|
+
* @internal
|
|
1498
1417
|
*/
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
/**
|
|
1508
|
-
* Class to help with arrays.
|
|
1509
|
-
*/
|
|
1510
|
-
class ArrayHelper {
|
|
1418
|
+
static _ALPHABET_REVERSE = [
|
|
1419
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
1420
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
1421
|
+
-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,
|
|
1422
|
+
17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
|
|
1423
|
+
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
|
|
1424
|
+
57, -1, -1, -1, -1, -1
|
|
1425
|
+
];
|
|
1511
1426
|
/**
|
|
1512
|
-
*
|
|
1513
|
-
* @param
|
|
1514
|
-
* @
|
|
1515
|
-
* @
|
|
1427
|
+
* Convert the base 58 string to a byte array.
|
|
1428
|
+
* @param base58 The base58 string to convert.
|
|
1429
|
+
* @returns The byte array.
|
|
1430
|
+
* @throws If the input string contains a character not in the Base58 alphabet.
|
|
1516
1431
|
*/
|
|
1517
|
-
static
|
|
1518
|
-
|
|
1519
|
-
|
|
1432
|
+
static decode(base58) {
|
|
1433
|
+
Guards.string(Base58._CLASS_NAME, "base58", base58);
|
|
1434
|
+
let zeroes = 0;
|
|
1435
|
+
for (let i = 0; i < base58.length; i++) {
|
|
1436
|
+
if (base58[i] !== "1") {
|
|
1437
|
+
break;
|
|
1438
|
+
}
|
|
1439
|
+
zeroes += 1;
|
|
1520
1440
|
}
|
|
1521
|
-
|
|
1522
|
-
|
|
1441
|
+
const size = Math.trunc((base58.length * 733) / 1000) + 1;
|
|
1442
|
+
const b256 = new Uint8Array(size).fill(0);
|
|
1443
|
+
let length = 0;
|
|
1444
|
+
for (let i = zeroes; i < base58.length; i++) {
|
|
1445
|
+
const ch = base58.charCodeAt(i);
|
|
1446
|
+
if (ch & 0xff80) {
|
|
1447
|
+
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
1448
|
+
}
|
|
1449
|
+
const val = Base58._ALPHABET_REVERSE[ch];
|
|
1450
|
+
if (val === -1) {
|
|
1451
|
+
throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
|
|
1452
|
+
}
|
|
1453
|
+
let carry = val;
|
|
1454
|
+
let j = 0;
|
|
1455
|
+
for (let k = size - 1; k >= 0; k--, j++) {
|
|
1456
|
+
if (carry === 0 && j >= length) {
|
|
1457
|
+
break;
|
|
1458
|
+
}
|
|
1459
|
+
carry += b256[k] * 58;
|
|
1460
|
+
b256[k] = carry;
|
|
1461
|
+
carry >>>= 8;
|
|
1462
|
+
}
|
|
1463
|
+
length = j;
|
|
1523
1464
|
}
|
|
1524
|
-
|
|
1525
|
-
|
|
1465
|
+
const out = new Uint8Array(zeroes + length);
|
|
1466
|
+
let j;
|
|
1467
|
+
for (j = 0; j < zeroes; j++) {
|
|
1468
|
+
out[j] = 0;
|
|
1526
1469
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
}
|
|
1470
|
+
let i = size - length;
|
|
1471
|
+
while (i < size) {
|
|
1472
|
+
out[j++] = b256[i++];
|
|
1531
1473
|
}
|
|
1532
|
-
return
|
|
1474
|
+
return out;
|
|
1533
1475
|
}
|
|
1534
1476
|
/**
|
|
1535
|
-
* Convert
|
|
1536
|
-
* @param
|
|
1537
|
-
* @returns The
|
|
1477
|
+
* Convert a byte array to base 58.
|
|
1478
|
+
* @param bytes The byte array to encode.
|
|
1479
|
+
* @returns The data as base58 string.
|
|
1538
1480
|
*/
|
|
1539
|
-
static
|
|
1540
|
-
|
|
1541
|
-
|
|
1481
|
+
static encode(bytes) {
|
|
1482
|
+
Guards.uint8Array(Base58._CLASS_NAME, "bytes", bytes);
|
|
1483
|
+
let zeroes = 0;
|
|
1484
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
1485
|
+
if (bytes[i] !== 0) {
|
|
1486
|
+
break;
|
|
1487
|
+
}
|
|
1488
|
+
zeroes += 1;
|
|
1542
1489
|
}
|
|
1543
|
-
|
|
1544
|
-
|
|
1490
|
+
const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
|
|
1491
|
+
const b58 = new Uint8Array(size).fill(0);
|
|
1492
|
+
let length = 0;
|
|
1493
|
+
for (let i = zeroes; i < bytes.length; i++) {
|
|
1494
|
+
let carry = bytes[i];
|
|
1495
|
+
let j = 0;
|
|
1496
|
+
for (let k = size - 1; k >= 0; k--, j++) {
|
|
1497
|
+
if (carry === 0 && j >= length) {
|
|
1498
|
+
break;
|
|
1499
|
+
}
|
|
1500
|
+
carry += b58[k] * 256;
|
|
1501
|
+
b58[k] = carry % 58;
|
|
1502
|
+
carry = Math.trunc(carry / 58);
|
|
1503
|
+
}
|
|
1504
|
+
length = j;
|
|
1545
1505
|
}
|
|
1546
|
-
|
|
1506
|
+
let i = size - length;
|
|
1507
|
+
while (i < size && b58[i] === 0) {
|
|
1508
|
+
i += 1;
|
|
1509
|
+
}
|
|
1510
|
+
let str = "";
|
|
1511
|
+
for (let j = 0; j < zeroes; j++) {
|
|
1512
|
+
str += "1";
|
|
1513
|
+
}
|
|
1514
|
+
while (i < size) {
|
|
1515
|
+
str += Base58._ALPHABET[b58[i++]];
|
|
1516
|
+
}
|
|
1517
|
+
return str;
|
|
1547
1518
|
}
|
|
1548
1519
|
}
|
|
1549
1520
|
|
|
1550
1521
|
// Copyright 2024 IOTA Stiftung.
|
|
1551
1522
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1523
|
+
/* eslint-disable no-bitwise */
|
|
1524
|
+
/* eslint-disable no-mixed-operators */
|
|
1552
1525
|
/**
|
|
1553
|
-
* Class to
|
|
1526
|
+
* Class to help with base64 Encoding/Decoding.
|
|
1527
|
+
* Sourced from https://github.com/beatgammit/base64-js.
|
|
1554
1528
|
*/
|
|
1555
|
-
class
|
|
1529
|
+
class Base64 {
|
|
1556
1530
|
/**
|
|
1557
|
-
*
|
|
1558
|
-
* @
|
|
1559
|
-
* @param property The name of the property.
|
|
1560
|
-
* @param value The value to test.
|
|
1561
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1531
|
+
* Runtime name for the class.
|
|
1532
|
+
* @internal
|
|
1562
1533
|
*/
|
|
1563
|
-
static
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1534
|
+
static _CLASS_NAME = "Base64";
|
|
1535
|
+
/**
|
|
1536
|
+
* Alphabet table for encoding.
|
|
1537
|
+
* @internal
|
|
1538
|
+
*/
|
|
1539
|
+
static _LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1540
|
+
/**
|
|
1541
|
+
* Alphabet table for decoding.
|
|
1542
|
+
* @internal
|
|
1543
|
+
*/
|
|
1544
|
+
static _REVERSE_LOOKUP = {
|
|
1545
|
+
"43": 62,
|
|
1546
|
+
"45": 62,
|
|
1547
|
+
"47": 63,
|
|
1548
|
+
"48": 52,
|
|
1549
|
+
"49": 53,
|
|
1550
|
+
"50": 54,
|
|
1551
|
+
"51": 55,
|
|
1552
|
+
"52": 56,
|
|
1553
|
+
"53": 57,
|
|
1554
|
+
"54": 58,
|
|
1555
|
+
"55": 59,
|
|
1556
|
+
"56": 60,
|
|
1557
|
+
"57": 61,
|
|
1558
|
+
"65": 0,
|
|
1559
|
+
"66": 1,
|
|
1560
|
+
"67": 2,
|
|
1561
|
+
"68": 3,
|
|
1562
|
+
"69": 4,
|
|
1563
|
+
"70": 5,
|
|
1564
|
+
"71": 6,
|
|
1565
|
+
"72": 7,
|
|
1566
|
+
"73": 8,
|
|
1567
|
+
"74": 9,
|
|
1568
|
+
"75": 10,
|
|
1569
|
+
"76": 11,
|
|
1570
|
+
"77": 12,
|
|
1571
|
+
"78": 13,
|
|
1572
|
+
"79": 14,
|
|
1573
|
+
"80": 15,
|
|
1574
|
+
"81": 16,
|
|
1575
|
+
"82": 17,
|
|
1576
|
+
"83": 18,
|
|
1577
|
+
"84": 19,
|
|
1578
|
+
"85": 20,
|
|
1579
|
+
"86": 21,
|
|
1580
|
+
"87": 22,
|
|
1581
|
+
"88": 23,
|
|
1582
|
+
"89": 24,
|
|
1583
|
+
"90": 25,
|
|
1584
|
+
"95": 63,
|
|
1585
|
+
"97": 26,
|
|
1586
|
+
"98": 27,
|
|
1587
|
+
"99": 28,
|
|
1588
|
+
"100": 29,
|
|
1589
|
+
"101": 30,
|
|
1590
|
+
"102": 31,
|
|
1591
|
+
"103": 32,
|
|
1592
|
+
"104": 33,
|
|
1593
|
+
"105": 34,
|
|
1594
|
+
"106": 35,
|
|
1595
|
+
"107": 36,
|
|
1596
|
+
"108": 37,
|
|
1597
|
+
"109": 38,
|
|
1598
|
+
"110": 39,
|
|
1599
|
+
"111": 40,
|
|
1600
|
+
"112": 41,
|
|
1601
|
+
"113": 42,
|
|
1602
|
+
"114": 43,
|
|
1603
|
+
"115": 44,
|
|
1604
|
+
"116": 45,
|
|
1605
|
+
"117": 46,
|
|
1606
|
+
"118": 47,
|
|
1607
|
+
"119": 48,
|
|
1608
|
+
"120": 49,
|
|
1609
|
+
"121": 50,
|
|
1610
|
+
"122": 51
|
|
1611
|
+
};
|
|
1568
1612
|
/**
|
|
1569
|
-
*
|
|
1570
|
-
* @param
|
|
1571
|
-
* @
|
|
1572
|
-
* @param value The value to test.
|
|
1573
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1613
|
+
* Get the byte length of the data.
|
|
1614
|
+
* @param base64 The base64 string.
|
|
1615
|
+
* @returns The byte length of the data.
|
|
1574
1616
|
*/
|
|
1575
|
-
static
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
}
|
|
1617
|
+
static byteLength(base64) {
|
|
1618
|
+
const lens = Base64.getLengths(base64);
|
|
1619
|
+
return Base64.calcByteLength(lens[0], lens[1]);
|
|
1579
1620
|
}
|
|
1580
1621
|
/**
|
|
1581
|
-
*
|
|
1582
|
-
* @param
|
|
1583
|
-
* @
|
|
1584
|
-
* @param value The value to test.
|
|
1585
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1622
|
+
* Convert the base 64 string to a byte array.
|
|
1623
|
+
* @param base64 The base64 string to convert.
|
|
1624
|
+
* @returns The byte array.
|
|
1586
1625
|
*/
|
|
1587
|
-
static
|
|
1588
|
-
|
|
1589
|
-
|
|
1626
|
+
static decode(base64) {
|
|
1627
|
+
Guards.string(Base64._CLASS_NAME, "base64", base64);
|
|
1628
|
+
let tmp;
|
|
1629
|
+
const lens = Base64.getLengths(base64);
|
|
1630
|
+
const validLen = lens[0];
|
|
1631
|
+
const placeHoldersLen = lens[1];
|
|
1632
|
+
const arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));
|
|
1633
|
+
let curByte = 0;
|
|
1634
|
+
// if there are placeholders, only get up to the last complete 4 chars
|
|
1635
|
+
const len = placeHoldersLen > 0 ? validLen - 4 : validLen;
|
|
1636
|
+
let i;
|
|
1637
|
+
for (i = 0; i < len; i += 4) {
|
|
1638
|
+
tmp =
|
|
1639
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |
|
|
1640
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |
|
|
1641
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |
|
|
1642
|
+
Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];
|
|
1643
|
+
arr[curByte++] = (tmp >> 16) & 0xff;
|
|
1644
|
+
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
1645
|
+
arr[curByte++] = tmp & 0xff;
|
|
1590
1646
|
}
|
|
1591
|
-
if (
|
|
1592
|
-
|
|
1647
|
+
if (placeHoldersLen === 2) {
|
|
1648
|
+
tmp =
|
|
1649
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |
|
|
1650
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);
|
|
1651
|
+
arr[curByte++] = tmp & 0xff;
|
|
1593
1652
|
}
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
*/
|
|
1602
|
-
static json(source, property, value) {
|
|
1603
|
-
if (!Is.json(value)) {
|
|
1604
|
-
throw new GuardError(source, "guard.stringJson", property, value);
|
|
1653
|
+
if (placeHoldersLen === 1) {
|
|
1654
|
+
tmp =
|
|
1655
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |
|
|
1656
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |
|
|
1657
|
+
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);
|
|
1658
|
+
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
1659
|
+
arr[curByte++] = tmp & 0xff;
|
|
1605
1660
|
}
|
|
1661
|
+
return arr;
|
|
1606
1662
|
}
|
|
1607
1663
|
/**
|
|
1608
|
-
*
|
|
1609
|
-
* @param
|
|
1610
|
-
* @
|
|
1611
|
-
* @param value The value to test.
|
|
1612
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1664
|
+
* Convert a byte array to base 64.
|
|
1665
|
+
* @param bytes The byte array to convert.
|
|
1666
|
+
* @returns The data as base64 string.
|
|
1613
1667
|
*/
|
|
1614
|
-
static
|
|
1615
|
-
|
|
1616
|
-
|
|
1668
|
+
static encode(bytes) {
|
|
1669
|
+
Guards.uint8Array(Base64._CLASS_NAME, "bytes", bytes);
|
|
1670
|
+
let tmp;
|
|
1671
|
+
const len = bytes.length;
|
|
1672
|
+
const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
|
|
1673
|
+
const parts = [];
|
|
1674
|
+
const maxChunkLength = 16383; // must be multiple of 3
|
|
1675
|
+
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
1676
|
+
for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
1677
|
+
parts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));
|
|
1678
|
+
}
|
|
1679
|
+
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
1680
|
+
if (extraBytes === 1) {
|
|
1681
|
+
tmp = bytes[len - 1];
|
|
1682
|
+
parts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);
|
|
1683
|
+
}
|
|
1684
|
+
else if (extraBytes === 2) {
|
|
1685
|
+
tmp = (bytes[len - 2] << 8) + bytes[len - 1];
|
|
1686
|
+
parts.push(`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`);
|
|
1617
1687
|
}
|
|
1688
|
+
return parts.join("");
|
|
1618
1689
|
}
|
|
1619
1690
|
/**
|
|
1620
|
-
*
|
|
1621
|
-
* @param
|
|
1622
|
-
* @param
|
|
1623
|
-
* @
|
|
1624
|
-
* @
|
|
1691
|
+
* Calculate the byte length.
|
|
1692
|
+
* @param validLen The valid length.
|
|
1693
|
+
* @param placeHoldersLen The placeholder length.
|
|
1694
|
+
* @returns The length.
|
|
1695
|
+
* @internal
|
|
1625
1696
|
*/
|
|
1626
|
-
static
|
|
1627
|
-
|
|
1628
|
-
throw new GuardError(source, "guard.base64Url", property, value);
|
|
1629
|
-
}
|
|
1697
|
+
static calcByteLength(validLen, placeHoldersLen) {
|
|
1698
|
+
return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
|
|
1630
1699
|
}
|
|
1631
1700
|
/**
|
|
1632
|
-
*
|
|
1633
|
-
* @param
|
|
1634
|
-
* @
|
|
1635
|
-
* @
|
|
1636
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1701
|
+
* Get the valid and placeholder lengths from a bas64 string.
|
|
1702
|
+
* @param base64 The base64 string.
|
|
1703
|
+
* @returns The lengths.
|
|
1704
|
+
* @internal
|
|
1637
1705
|
*/
|
|
1638
|
-
static
|
|
1639
|
-
|
|
1640
|
-
|
|
1706
|
+
static getLengths(base64) {
|
|
1707
|
+
const len = base64.length;
|
|
1708
|
+
if (len % 4 > 0) {
|
|
1709
|
+
throw new GeneralError(Base64._CLASS_NAME, "length4Multiple", { value: len });
|
|
1710
|
+
}
|
|
1711
|
+
// Trim off extra bytes after placeholder bytes are found
|
|
1712
|
+
// See: https://github.com/beatgammit/base64-js/issues/42
|
|
1713
|
+
let validLen = base64.indexOf("=");
|
|
1714
|
+
if (validLen === -1) {
|
|
1715
|
+
validLen = len;
|
|
1641
1716
|
}
|
|
1717
|
+
const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);
|
|
1718
|
+
return [validLen, placeHoldersLen];
|
|
1642
1719
|
}
|
|
1643
1720
|
/**
|
|
1644
|
-
*
|
|
1645
|
-
* @param
|
|
1646
|
-
* @
|
|
1647
|
-
* @
|
|
1648
|
-
* @param allowPrefix Allow the hex to have the 0x prefix.
|
|
1649
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1721
|
+
* Convert the triplet to base 64.
|
|
1722
|
+
* @param num The number to convert.
|
|
1723
|
+
* @returns The base64 encoding.
|
|
1724
|
+
* @internal
|
|
1650
1725
|
*/
|
|
1651
|
-
static
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1726
|
+
static tripletToBase64(num) {
|
|
1727
|
+
return (Base64._LOOKUP[(num >> 18) & 0x3f] +
|
|
1728
|
+
Base64._LOOKUP[(num >> 12) & 0x3f] +
|
|
1729
|
+
Base64._LOOKUP[(num >> 6) & 0x3f] +
|
|
1730
|
+
Base64._LOOKUP[num & 0x3f]);
|
|
1656
1731
|
}
|
|
1657
1732
|
/**
|
|
1658
|
-
*
|
|
1659
|
-
* @param
|
|
1660
|
-
* @param
|
|
1661
|
-
* @param
|
|
1662
|
-
* @
|
|
1663
|
-
* @
|
|
1664
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1733
|
+
* Encode a chunk.
|
|
1734
|
+
* @param bytes The byte array.
|
|
1735
|
+
* @param start The start index in the buffer.
|
|
1736
|
+
* @param end The end index in the buffer.
|
|
1737
|
+
* @returns The encoded chunk.
|
|
1738
|
+
* @internal
|
|
1665
1739
|
*/
|
|
1666
|
-
static
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1740
|
+
static encodeChunk(bytes, start, end) {
|
|
1741
|
+
let tmp;
|
|
1742
|
+
const output = [];
|
|
1743
|
+
for (let i = start; i < end; i += 3) {
|
|
1744
|
+
tmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);
|
|
1745
|
+
output.push(Base64.tripletToBase64(tmp));
|
|
1670
1746
|
}
|
|
1747
|
+
return output.join("");
|
|
1671
1748
|
}
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/**
|
|
1752
|
+
* Class to help with base64 URL Encoding/Decoding.
|
|
1753
|
+
* https://www.rfc-editor.org/rfc/rfc4648#section-5.
|
|
1754
|
+
*/
|
|
1755
|
+
class Base64Url {
|
|
1672
1756
|
/**
|
|
1673
|
-
*
|
|
1674
|
-
* @
|
|
1675
|
-
* @param property The name of the property.
|
|
1676
|
-
* @param value The value to test.
|
|
1677
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1757
|
+
* Runtime name for the class.
|
|
1758
|
+
* @internal
|
|
1678
1759
|
*/
|
|
1679
|
-
static
|
|
1680
|
-
if (!Is.number(value)) {
|
|
1681
|
-
throw new GuardError(source, "guard.number", property, value);
|
|
1682
|
-
}
|
|
1683
|
-
}
|
|
1760
|
+
static _CLASS_NAME = "Base64";
|
|
1684
1761
|
/**
|
|
1685
|
-
*
|
|
1686
|
-
* @param
|
|
1687
|
-
* @
|
|
1688
|
-
* @param value The value to test.
|
|
1689
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1762
|
+
* Convert the base 64 string to a byte array.
|
|
1763
|
+
* @param base64Url The base64 url string to convert.
|
|
1764
|
+
* @returns The byte array.
|
|
1690
1765
|
*/
|
|
1691
|
-
static
|
|
1692
|
-
|
|
1693
|
-
|
|
1766
|
+
static decode(base64Url) {
|
|
1767
|
+
Guards.string(Base64Url._CLASS_NAME, "base64Url", base64Url);
|
|
1768
|
+
let base64 = base64Url;
|
|
1769
|
+
// Base 64 url can have padding removed, so add it back if it is missing.
|
|
1770
|
+
if (base64.length > 0 && !base64.endsWith("=")) {
|
|
1771
|
+
const placeHoldersLen = 4 - (base64.length % 4);
|
|
1772
|
+
if (placeHoldersLen > 0 && placeHoldersLen < 4) {
|
|
1773
|
+
base64 = base64.padEnd(base64.length + placeHoldersLen, "=");
|
|
1774
|
+
}
|
|
1694
1775
|
}
|
|
1776
|
+
base64 = base64.replace(/-/g, "+").replace(/_/g, "/");
|
|
1777
|
+
return Base64.decode(base64);
|
|
1695
1778
|
}
|
|
1696
1779
|
/**
|
|
1697
|
-
*
|
|
1698
|
-
* @param
|
|
1699
|
-
* @
|
|
1700
|
-
* @param value The value to test.
|
|
1701
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1780
|
+
* Convert a byte array to base 64 url.
|
|
1781
|
+
* @param bytes The byte array to convert.
|
|
1782
|
+
* @returns The data as base64 url string.
|
|
1702
1783
|
*/
|
|
1703
|
-
static
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1784
|
+
static encode(bytes) {
|
|
1785
|
+
Guards.uint8Array(Base64Url._CLASS_NAME, "bytes", bytes);
|
|
1786
|
+
const base64 = Base64.encode(bytes);
|
|
1787
|
+
// Base 64 url can have padding removed, so remove it.
|
|
1788
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
1707
1789
|
}
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
/**
|
|
1793
|
+
* Class to handle errors which are triggered by data already existing.
|
|
1794
|
+
*/
|
|
1795
|
+
class AlreadyExistsError extends BaseError {
|
|
1796
|
+
/**
|
|
1797
|
+
* Runtime name for the class.
|
|
1798
|
+
*/
|
|
1799
|
+
static CLASS_NAME = "AlreadyExistsError";
|
|
1708
1800
|
/**
|
|
1709
|
-
*
|
|
1801
|
+
* Create a new instance of AlreadyExistsError.
|
|
1710
1802
|
* @param source The source of the error.
|
|
1711
|
-
* @param
|
|
1712
|
-
* @param
|
|
1713
|
-
* @
|
|
1803
|
+
* @param message The message as a code.
|
|
1804
|
+
* @param existingId The id for the item.
|
|
1805
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
1714
1806
|
*/
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
throw new GuardError(source, "guard.boolean", property, value);
|
|
1718
|
-
}
|
|
1807
|
+
constructor(source, message, existingId, cause) {
|
|
1808
|
+
super(AlreadyExistsError.CLASS_NAME, source, message, { existingId });
|
|
1719
1809
|
}
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
/**
|
|
1813
|
+
* Class to handle errors which are triggered by conflicting data.
|
|
1814
|
+
*/
|
|
1815
|
+
class ConflictError extends BaseError {
|
|
1720
1816
|
/**
|
|
1721
|
-
*
|
|
1722
|
-
* @param source The source of the error.
|
|
1723
|
-
* @param property The name of the property.
|
|
1724
|
-
* @param value The value to test.
|
|
1725
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1817
|
+
* Runtime name for the class.
|
|
1726
1818
|
*/
|
|
1727
|
-
static
|
|
1728
|
-
if (!Is.date(value)) {
|
|
1729
|
-
throw new GuardError(source, "guard.date", property, value);
|
|
1730
|
-
}
|
|
1731
|
-
}
|
|
1819
|
+
static CLASS_NAME = "ConflictError";
|
|
1732
1820
|
/**
|
|
1733
|
-
*
|
|
1821
|
+
* Create a new instance of ConflictError.
|
|
1734
1822
|
* @param source The source of the error.
|
|
1735
|
-
* @param
|
|
1736
|
-
* @param
|
|
1737
|
-
* @
|
|
1823
|
+
* @param message The message as a code.
|
|
1824
|
+
* @param conflictId The id that has conflicts.
|
|
1825
|
+
* @param conflicts The conflicts that occurred.
|
|
1826
|
+
* @param cause The cause or the error if we have wrapped another error.
|
|
1738
1827
|
*/
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
throw new GuardError(source, "guard.timestampMilliseconds", property, value);
|
|
1742
|
-
}
|
|
1828
|
+
constructor(source, message, conflictId, conflicts, cause) {
|
|
1829
|
+
super(ConflictError.CLASS_NAME, source, message, { conflictId, conflicts }, cause);
|
|
1743
1830
|
}
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
/**
|
|
1834
|
+
* Class to handle errors which are triggered by data not being found.
|
|
1835
|
+
*/
|
|
1836
|
+
class NotFoundError extends BaseError {
|
|
1744
1837
|
/**
|
|
1745
|
-
*
|
|
1746
|
-
* @param source The source of the error.
|
|
1747
|
-
* @param property The name of the property.
|
|
1748
|
-
* @param value The value to test.
|
|
1749
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1838
|
+
* Runtime name for the class.
|
|
1750
1839
|
*/
|
|
1751
|
-
static
|
|
1752
|
-
if (!Is.timestampSeconds(value)) {
|
|
1753
|
-
throw new GuardError(source, "guard.timestampSeconds", property, value);
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1840
|
+
static CLASS_NAME = "NotFoundError";
|
|
1756
1841
|
/**
|
|
1757
|
-
*
|
|
1842
|
+
* Create a new instance of NotFoundError.
|
|
1758
1843
|
* @param source The source of the error.
|
|
1759
|
-
* @param
|
|
1760
|
-
* @param
|
|
1761
|
-
* @
|
|
1844
|
+
* @param message The message as a code.
|
|
1845
|
+
* @param notFoundId The id for the item.
|
|
1846
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
1762
1847
|
*/
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
throw new GuardError(source, "guard.objectUndefined", property, value);
|
|
1766
|
-
}
|
|
1767
|
-
if (!Is.object(value)) {
|
|
1768
|
-
throw new GuardError(source, "guard.object", property, value);
|
|
1769
|
-
}
|
|
1848
|
+
constructor(source, message, notFoundId, cause) {
|
|
1849
|
+
super(NotFoundError.CLASS_NAME, source, message, { notFoundId }, cause);
|
|
1770
1850
|
}
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
/**
|
|
1854
|
+
* Class to handle errors.
|
|
1855
|
+
*/
|
|
1856
|
+
class NotImplementedError extends BaseError {
|
|
1771
1857
|
/**
|
|
1772
|
-
*
|
|
1773
|
-
* @param source The source of the error.
|
|
1774
|
-
* @param property The name of the property.
|
|
1775
|
-
* @param value The value to test.
|
|
1776
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1858
|
+
* Runtime name for the class.
|
|
1777
1859
|
*/
|
|
1778
|
-
static
|
|
1779
|
-
if (Is.undefined(value)) {
|
|
1780
|
-
throw new GuardError(source, "guard.objectUndefined", property, value);
|
|
1781
|
-
}
|
|
1782
|
-
if (!Is.object(value)) {
|
|
1783
|
-
throw new GuardError(source, "guard.object", property, value);
|
|
1784
|
-
}
|
|
1785
|
-
if (Object.keys(value || {}).length === 0) {
|
|
1786
|
-
throw new GuardError(source, "guard.objectValue", property, value);
|
|
1787
|
-
}
|
|
1788
|
-
}
|
|
1860
|
+
static CLASS_NAME = "NotImplementedError";
|
|
1789
1861
|
/**
|
|
1790
|
-
*
|
|
1862
|
+
* Create a new instance of NotImplementedError.
|
|
1791
1863
|
* @param source The source of the error.
|
|
1792
|
-
* @param
|
|
1793
|
-
* @param value The value to test.
|
|
1794
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1864
|
+
* @param method The method for the error.
|
|
1795
1865
|
*/
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
}
|
|
1866
|
+
constructor(source, method) {
|
|
1867
|
+
super(NotImplementedError.CLASS_NAME, source, "common.notImplementedMethod", {
|
|
1868
|
+
method
|
|
1869
|
+
});
|
|
1800
1870
|
}
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
/**
|
|
1874
|
+
* Class to handle errors when a feature is unsupported.
|
|
1875
|
+
*/
|
|
1876
|
+
class NotSupportedError extends BaseError {
|
|
1801
1877
|
/**
|
|
1802
|
-
*
|
|
1803
|
-
* @param source The source of the error.
|
|
1804
|
-
* @param property The name of the property.
|
|
1805
|
-
* @param value The value to test.
|
|
1806
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1878
|
+
* Runtime name for the class.
|
|
1807
1879
|
*/
|
|
1808
|
-
static
|
|
1809
|
-
if (!Is.array(value)) {
|
|
1810
|
-
throw new GuardError(source, "guard.array", property, value);
|
|
1811
|
-
}
|
|
1812
|
-
if (value.length === 0) {
|
|
1813
|
-
throw new GuardError(source, "guard.arrayValue", property, value);
|
|
1814
|
-
}
|
|
1815
|
-
}
|
|
1880
|
+
static CLASS_NAME = "NotSupportedError";
|
|
1816
1881
|
/**
|
|
1817
|
-
*
|
|
1882
|
+
* Create a new instance of NotSupportedError.
|
|
1818
1883
|
* @param source The source of the error.
|
|
1819
|
-
* @param
|
|
1820
|
-
* @param
|
|
1821
|
-
* @param options The options the value must be one of.
|
|
1822
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1884
|
+
* @param message The message as a code.
|
|
1885
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
1823
1886
|
*/
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
throw new GuardError(source, "guard.array", property, value);
|
|
1827
|
-
}
|
|
1828
|
-
if (!options.includes(value)) {
|
|
1829
|
-
throw new GuardError(source, "guard.arrayOneOf", property, value, options.join(", "));
|
|
1830
|
-
}
|
|
1887
|
+
constructor(source, message, cause) {
|
|
1888
|
+
super(NotSupportedError.CLASS_NAME, source, message, undefined, cause);
|
|
1831
1889
|
}
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
/**
|
|
1893
|
+
* Class to handle errors which are triggered by access not being unauthorized.
|
|
1894
|
+
*/
|
|
1895
|
+
class UnauthorizedError extends BaseError {
|
|
1832
1896
|
/**
|
|
1833
|
-
*
|
|
1834
|
-
* @param source The source of the error.
|
|
1835
|
-
* @param property The name of the property.
|
|
1836
|
-
* @param value The value to test.
|
|
1837
|
-
* @param startValues The values that must start the array.
|
|
1838
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1897
|
+
* Runtime name for the class.
|
|
1839
1898
|
*/
|
|
1840
|
-
static
|
|
1841
|
-
if (!Is.arrayValue(value)) {
|
|
1842
|
-
throw new GuardError(source, "guard.array", property, value);
|
|
1843
|
-
}
|
|
1844
|
-
const startValuesArray = ArrayHelper.fromObjectOrArray(startValues);
|
|
1845
|
-
if (!Is.arrayValue(startValuesArray)) {
|
|
1846
|
-
throw new GuardError(source, "guard.array", property, startValuesArray);
|
|
1847
|
-
}
|
|
1848
|
-
for (let i = 0; i < startValuesArray.length; i++) {
|
|
1849
|
-
if (value[i] !== startValuesArray[i]) {
|
|
1850
|
-
throw new GuardError(source, "guard.arrayStartsWith", property, value, startValuesArray.join(", "));
|
|
1851
|
-
}
|
|
1852
|
-
}
|
|
1853
|
-
}
|
|
1899
|
+
static CLASS_NAME = "UnauthorizedError";
|
|
1854
1900
|
/**
|
|
1855
|
-
*
|
|
1901
|
+
* Create a new instance of UnauthorizedError.
|
|
1856
1902
|
* @param source The source of the error.
|
|
1857
|
-
* @param
|
|
1858
|
-
* @param
|
|
1859
|
-
* @param endValues The values that must end the array.
|
|
1860
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1903
|
+
* @param message The message as a code.
|
|
1904
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
1861
1905
|
*/
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
throw new GuardError(source, "guard.array", property, value);
|
|
1865
|
-
}
|
|
1866
|
-
const endValuesArray = ArrayHelper.fromObjectOrArray(endValues);
|
|
1867
|
-
if (!Is.arrayValue(endValuesArray)) {
|
|
1868
|
-
throw new GuardError(source, "guard.array", property, endValuesArray);
|
|
1869
|
-
}
|
|
1870
|
-
for (let i = 0; i < endValuesArray.length; i++) {
|
|
1871
|
-
if (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {
|
|
1872
|
-
throw new GuardError(source, "guard.arrayEndsWith", property, value, endValuesArray.join(", "));
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1906
|
+
constructor(source, message, cause) {
|
|
1907
|
+
super(UnauthorizedError.CLASS_NAME, source, message, undefined, cause);
|
|
1875
1908
|
}
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
/**
|
|
1912
|
+
* Class to handle errors when some data can not be processed.
|
|
1913
|
+
*/
|
|
1914
|
+
class UnprocessableError extends BaseError {
|
|
1876
1915
|
/**
|
|
1877
|
-
*
|
|
1878
|
-
* @param source The source of the error.
|
|
1879
|
-
* @param property The name of the property.
|
|
1880
|
-
* @param value The value to test.
|
|
1881
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1916
|
+
* Runtime name for the class.
|
|
1882
1917
|
*/
|
|
1883
|
-
static
|
|
1884
|
-
if (!Is.uint8Array(value)) {
|
|
1885
|
-
throw new GuardError(source, "guard.uint8Array", property, value);
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1918
|
+
static CLASS_NAME = "UnprocessableError";
|
|
1888
1919
|
/**
|
|
1889
|
-
*
|
|
1920
|
+
* Create a new instance of UnprocessableError.
|
|
1890
1921
|
* @param source The source of the error.
|
|
1891
|
-
* @param
|
|
1892
|
-
* @param
|
|
1893
|
-
* @
|
|
1894
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1922
|
+
* @param message The message as a code.
|
|
1923
|
+
* @param properties Any additional information for the error.
|
|
1924
|
+
* @param cause The cause of the error if we have wrapped another error.
|
|
1895
1925
|
*/
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
throw new GuardError(source, "guard.function", property, value);
|
|
1899
|
-
}
|
|
1900
|
-
return true;
|
|
1926
|
+
constructor(source, message, properties, cause) {
|
|
1927
|
+
super(UnprocessableError.CLASS_NAME, source, message, properties, cause);
|
|
1901
1928
|
}
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
/**
|
|
1932
|
+
* Class to handle errors which are triggered by entity validation.
|
|
1933
|
+
*/
|
|
1934
|
+
class ValidationError extends BaseError {
|
|
1902
1935
|
/**
|
|
1903
|
-
*
|
|
1936
|
+
* Runtime name for the class.s
|
|
1937
|
+
*/
|
|
1938
|
+
static CLASS_NAME = "ValidationError";
|
|
1939
|
+
/**
|
|
1940
|
+
* Create a new instance of ValidationError.
|
|
1904
1941
|
* @param source The source of the error.
|
|
1905
|
-
* @param
|
|
1906
|
-
* @param
|
|
1907
|
-
* @throws GuardError If the value does not match the assertion.
|
|
1942
|
+
* @param validationObject The object that failed validation.
|
|
1943
|
+
* @param validationFailures The validation failures.
|
|
1908
1944
|
*/
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1945
|
+
constructor(source, validationObject, validationFailures) {
|
|
1946
|
+
super(ValidationError.CLASS_NAME, source, "common.validation", {
|
|
1947
|
+
validationObject,
|
|
1948
|
+
validationFailures
|
|
1949
|
+
});
|
|
1913
1950
|
}
|
|
1914
1951
|
}
|
|
1915
1952
|
|
|
@@ -2102,6 +2139,7 @@ class Factory {
|
|
|
2102
2139
|
* @throws GeneralError if no item exists to get.
|
|
2103
2140
|
*/
|
|
2104
2141
|
get(name) {
|
|
2142
|
+
Guards.stringValue(Factory._CLASS_NAME, "name", name);
|
|
2105
2143
|
const instance = this.getIfExists(name);
|
|
2106
2144
|
if (!instance) {
|
|
2107
2145
|
throw new GeneralError(Factory._CLASS_NAME, "noGet", {
|
|
@@ -2117,6 +2155,9 @@ class Factory {
|
|
|
2117
2155
|
* @returns An instance of the item or undefined if it does not exist.
|
|
2118
2156
|
*/
|
|
2119
2157
|
getIfExists(name) {
|
|
2158
|
+
if (Is.empty(name)) {
|
|
2159
|
+
return;
|
|
2160
|
+
}
|
|
2120
2161
|
Guards.stringValue(Factory._CLASS_NAME, "name", name);
|
|
2121
2162
|
const matchName = this._matcher(Object.keys(this._generators), name);
|
|
2122
2163
|
if (Is.stringValue(matchName) && this._generators[matchName]) {
|
|
@@ -3145,7 +3186,7 @@ class ErrorHelper {
|
|
|
3145
3186
|
* Format Errors and returns just their messages.
|
|
3146
3187
|
* @param error The error to format.
|
|
3147
3188
|
* @param includeDetails Whether to include error details, defaults to false.
|
|
3148
|
-
* @returns The error formatted including any
|
|
3189
|
+
* @returns The error formatted including any causes errors.
|
|
3149
3190
|
*/
|
|
3150
3191
|
static formatErrors(error, includeDetails) {
|
|
3151
3192
|
const localizedErrors = ErrorHelper.localizeErrors(error);
|
|
@@ -3163,7 +3204,7 @@ class ErrorHelper {
|
|
|
3163
3204
|
return localizedErrors.map(e => e.message);
|
|
3164
3205
|
}
|
|
3165
3206
|
/**
|
|
3166
|
-
* Localize the content of an error and any
|
|
3207
|
+
* Localize the content of an error and any causes.
|
|
3167
3208
|
* @param error The error to format.
|
|
3168
3209
|
* @returns The localized version of the errors flattened.
|
|
3169
3210
|
*/
|
|
@@ -3204,7 +3245,7 @@ class ErrorHelper {
|
|
|
3204
3245
|
return formattedErrors;
|
|
3205
3246
|
}
|
|
3206
3247
|
/**
|
|
3207
|
-
* Localize the content of an error and any
|
|
3248
|
+
* Localize the content of an error and any causes.
|
|
3208
3249
|
* @param error The error to format.
|
|
3209
3250
|
* @returns The localized version of the errors flattened.
|
|
3210
3251
|
*/
|
|
@@ -4272,7 +4313,7 @@ class Compression {
|
|
|
4272
4313
|
static async compress(bytes, type) {
|
|
4273
4314
|
Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
|
|
4274
4315
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
4275
|
-
const blob = new Blob([bytes]);
|
|
4316
|
+
const blob = new Blob([new Uint8Array(bytes)]);
|
|
4276
4317
|
const compressionStream = new CompressionStream(type);
|
|
4277
4318
|
const compressionPipe = blob.stream().pipeThrough(compressionStream);
|
|
4278
4319
|
const compressedBlob = await new Response(compressionPipe).blob();
|
|
@@ -4294,7 +4335,7 @@ class Compression {
|
|
|
4294
4335
|
static async decompress(compressedBytes, type) {
|
|
4295
4336
|
Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
|
|
4296
4337
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
4297
|
-
const blob = new Blob([compressedBytes]);
|
|
4338
|
+
const blob = new Blob([new Uint8Array(compressedBytes)]);
|
|
4298
4339
|
const decompressionStream = new DecompressionStream(type);
|
|
4299
4340
|
const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
|
|
4300
4341
|
const decompressedBlob = await new Response(decompressionPipe).blob();
|