kiss-for-react 1.1.0 → 2.0.1

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.
Files changed (155) hide show
  1. package/README.md +699 -156
  2. package/lib/cjs/ClassPersistor.js +177 -112
  3. package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
  4. package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
  5. package/lib/cjs/Esserializer/builtins.js +258 -0
  6. package/lib/cjs/Esserializer/constant.js +39 -131
  7. package/lib/cjs/Esserializer/deserializer.js +293 -313
  8. package/lib/cjs/Esserializer/general.js +91 -39
  9. package/lib/cjs/Esserializer/index.js +47 -43
  10. package/lib/cjs/Esserializer/serializer.js +168 -133
  11. package/lib/cjs/Evt.js +132 -0
  12. package/lib/cjs/Hooks.js +1474 -969
  13. package/lib/cjs/KissAction.js +4762 -1333
  14. package/lib/cjs/LoadPages.js +336 -0
  15. package/lib/cjs/Persistor.js +189 -120
  16. package/lib/cjs/ProcessPersistence.js +579 -262
  17. package/lib/cjs/Store.js +4342 -1534
  18. package/lib/cjs/StoreException.js +162 -75
  19. package/lib/cjs/StoreProvider.js +166 -0
  20. package/lib/cjs/UserException.js +212 -218
  21. package/lib/cjs/Wait.js +309 -309
  22. package/lib/cjs/index.js +69 -42
  23. package/lib/cjs/server.js +54 -0
  24. package/lib/cjs/types/ClassPersistor.d.ts +143 -112
  25. package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
  26. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
  27. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  28. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
  29. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  30. package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
  31. package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
  32. package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
  33. package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
  34. package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
  35. package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
  36. package/lib/cjs/types/Esserializer/general.d.ts +28 -4
  37. package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
  38. package/lib/cjs/types/Esserializer/index.d.ts +33 -29
  39. package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
  40. package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
  41. package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
  42. package/lib/cjs/types/Evt.d.ts +124 -0
  43. package/lib/cjs/types/Evt.d.ts.map +1 -0
  44. package/lib/cjs/types/Hooks.d.ts +1174 -837
  45. package/lib/cjs/types/Hooks.d.ts.map +1 -1
  46. package/lib/cjs/types/KissAction.d.ts +5257 -1398
  47. package/lib/cjs/types/KissAction.d.ts.map +1 -1
  48. package/lib/cjs/types/LoadPages.d.ts +287 -0
  49. package/lib/cjs/types/LoadPages.d.ts.map +1 -0
  50. package/lib/cjs/types/Persistor.d.ts +187 -118
  51. package/lib/cjs/types/Persistor.d.ts.map +1 -1
  52. package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
  53. package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
  54. package/lib/cjs/types/Store.d.ts +2266 -1042
  55. package/lib/cjs/types/Store.d.ts.map +1 -1
  56. package/lib/cjs/types/StoreException.d.ts +97 -28
  57. package/lib/cjs/types/StoreException.d.ts.map +1 -1
  58. package/lib/cjs/types/StoreProvider.d.ts +95 -0
  59. package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
  60. package/lib/cjs/types/UserException.d.ts +178 -169
  61. package/lib/cjs/types/UserException.d.ts.map +1 -1
  62. package/lib/cjs/types/index.d.ts +12 -9
  63. package/lib/cjs/types/index.d.ts.map +1 -1
  64. package/lib/cjs/types/server.d.ts +12 -0
  65. package/lib/cjs/types/server.d.ts.map +1 -0
  66. package/lib/cjs/types/utils.d.ts +15 -1
  67. package/lib/cjs/types/utils.d.ts.map +1 -1
  68. package/lib/cjs/utils.js +45 -7
  69. package/lib/esm/ClassPersistor.mjs +177 -0
  70. package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
  71. package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
  72. package/lib/esm/Esserializer/builtins.mjs +254 -0
  73. package/lib/esm/Esserializer/constant.mjs +28 -0
  74. package/lib/esm/Esserializer/deserializer.mjs +288 -0
  75. package/lib/esm/Esserializer/general.mjs +81 -0
  76. package/lib/esm/Esserializer/index.mjs +43 -0
  77. package/lib/esm/Esserializer/serializer.mjs +164 -0
  78. package/lib/esm/Evt.mjs +151 -0
  79. package/lib/esm/Hooks.mjs +1448 -0
  80. package/lib/esm/KissAction.mjs +4816 -0
  81. package/lib/esm/LoadPages.mjs +340 -0
  82. package/lib/esm/Persistor.mjs +180 -0
  83. package/lib/esm/ProcessPersistence.mjs +581 -0
  84. package/lib/esm/Store.mjs +4464 -0
  85. package/lib/esm/StoreException.mjs +153 -0
  86. package/lib/esm/StoreProvider.mjs +162 -0
  87. package/lib/esm/{UserException.js → UserException.mjs} +240 -246
  88. package/lib/esm/{Wait.js → Wait.mjs} +309 -309
  89. package/lib/esm/index.mjs +12 -9
  90. package/lib/esm/server.mjs +23 -0
  91. package/lib/esm/types/ClassPersistor.d.ts +143 -112
  92. package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
  93. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
  94. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  95. package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
  96. package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  97. package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
  98. package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
  99. package/lib/esm/types/Esserializer/constant.d.ts +25 -49
  100. package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
  101. package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
  102. package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
  103. package/lib/esm/types/Esserializer/general.d.ts +28 -4
  104. package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
  105. package/lib/esm/types/Esserializer/index.d.ts +33 -29
  106. package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
  107. package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
  108. package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
  109. package/lib/esm/types/Evt.d.ts +124 -0
  110. package/lib/esm/types/Evt.d.ts.map +1 -0
  111. package/lib/esm/types/Hooks.d.ts +1174 -837
  112. package/lib/esm/types/Hooks.d.ts.map +1 -1
  113. package/lib/esm/types/KissAction.d.ts +5257 -1398
  114. package/lib/esm/types/KissAction.d.ts.map +1 -1
  115. package/lib/esm/types/LoadPages.d.ts +287 -0
  116. package/lib/esm/types/LoadPages.d.ts.map +1 -0
  117. package/lib/esm/types/Persistor.d.ts +187 -118
  118. package/lib/esm/types/Persistor.d.ts.map +1 -1
  119. package/lib/esm/types/ProcessPersistence.d.ts +197 -99
  120. package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
  121. package/lib/esm/types/Store.d.ts +2266 -1042
  122. package/lib/esm/types/Store.d.ts.map +1 -1
  123. package/lib/esm/types/StoreException.d.ts +97 -28
  124. package/lib/esm/types/StoreException.d.ts.map +1 -1
  125. package/lib/esm/types/StoreProvider.d.ts +95 -0
  126. package/lib/esm/types/StoreProvider.d.ts.map +1 -0
  127. package/lib/esm/types/UserException.d.ts +178 -169
  128. package/lib/esm/types/UserException.d.ts.map +1 -1
  129. package/lib/esm/types/index.d.ts +12 -9
  130. package/lib/esm/types/index.d.ts.map +1 -1
  131. package/lib/esm/types/server.d.ts +12 -0
  132. package/lib/esm/types/server.d.ts.map +1 -0
  133. package/lib/esm/types/utils.d.ts +15 -1
  134. package/lib/esm/types/utils.d.ts.map +1 -1
  135. package/lib/esm/utils.mjs +38 -0
  136. package/package.json +54 -30
  137. package/lib/cjs/UnmodifiableSetView.js +0 -53
  138. package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
  139. package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
  140. package/lib/esm/ClassPersistor.js +0 -112
  141. package/lib/esm/Esserializer/constant.js +0 -81
  142. package/lib/esm/Esserializer/deserializer.js +0 -307
  143. package/lib/esm/Esserializer/general.js +0 -34
  144. package/lib/esm/Esserializer/index.js +0 -39
  145. package/lib/esm/Esserializer/serializer.js +0 -130
  146. package/lib/esm/Hooks.js +0 -955
  147. package/lib/esm/KissAction.js +0 -1459
  148. package/lib/esm/Persistor.js +0 -113
  149. package/lib/esm/ProcessPersistence.js +0 -262
  150. package/lib/esm/Store.js +0 -1606
  151. package/lib/esm/StoreException.js +0 -69
  152. package/lib/esm/UnmodifiableSetView.js +0 -50
  153. package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
  154. package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
  155. package/lib/esm/utils.js +0 -3
@@ -1,313 +1,293 @@
1
- "use strict";
2
- // The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
3
- // All credit goes to him. See: https://www.npmjs.com/package/esserializer
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.getParentClassName = exports.getClassMappingFromClassArray = exports.deserializeFromParsedObjWithClassMapping = exports.deserializeFromParsedObj = void 0;
6
- const general_1 = require("./general");
7
- const constant_1 = require("./constant");
8
- const REGEXP_BEGIN_WITH_CLASS = /^\s*class\s+/;
9
- function deserializeFromParsedObj(parsedObj, classes, options) {
10
- //
11
- return deserializeFromParsedObjWithClassMapping(parsedObj, getClassMappingFromClassArray(classes), options);
12
- }
13
- exports.deserializeFromParsedObj = deserializeFromParsedObj;
14
- function deserializeFromParsedObjWithClassMapping(parsedObj, classMapping, options = {}) {
15
- if ((0, general_1.notObject)(parsedObj)) {
16
- return parsedObj;
17
- }
18
- if (Array.isArray(parsedObj)) {
19
- return _deserializeArray(parsedObj, classMapping);
20
- }
21
- const classNameInParsedObj = parsedObj[constant_1.CLASS_NAME_FIELD];
22
- const deserializedValueForBuiltinType = _deserializeBuiltinTypes(classNameInParsedObj, parsedObj, classMapping);
23
- if (deserializedValueForBuiltinType !== constant_1.ESSERIALIZER_NULL) {
24
- return deserializedValueForBuiltinType;
25
- }
26
- if (classNameInParsedObj && !classMapping[classNameInParsedObj]) {
27
- throw new Error(`Class "${classNameInParsedObj}" not found during deserialization. You must register it with: "ESSerializer.registerClass(${classNameInParsedObj});"`);
28
- }
29
- let constructorParameters = [];
30
- if (options.fieldsForConstructorParameters) {
31
- constructorParameters = options.fieldsForConstructorParameters.map((field) => {
32
- if (field in parsedObj) {
33
- return parsedObj[field];
34
- }
35
- return {}; // Prevent passing undefined to constructor
36
- });
37
- }
38
- const deserializedObj = deserializeClassProperty(classMapping[classNameInParsedObj], constructorParameters);
39
- return deserializeValuesWithClassMapping(deserializedObj, parsedObj, classMapping, options);
40
- }
41
- exports.deserializeFromParsedObjWithClassMapping = deserializeFromParsedObjWithClassMapping;
42
- function _deserializeArray(parsedObj, classMapping) {
43
- return parsedObj.map((item) => {
44
- return deserializeFromParsedObjWithClassMapping(item, classMapping);
45
- });
46
- }
47
- function _deserializeBuiltinTypes(classNameInParsedObj, parsedObj, classMapping) {
48
- switch (classNameInParsedObj) {
49
- case constant_1.BUILTIN_CLASS_INT8ARRAY:
50
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Int8Array);
51
- case constant_1.BUILTIN_CLASS_UINT8ARRAY:
52
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Uint8Array);
53
- case constant_1.BUILTIN_CLASS_UINT8CLAMPEDARRAY:
54
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Uint8ClampedArray);
55
- case constant_1.BUILTIN_CLASS_INT16ARRAY:
56
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Int16Array);
57
- case constant_1.BUILTIN_CLASS_UINT16ARRAY:
58
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Uint16Array);
59
- case constant_1.BUILTIN_CLASS_INT32ARRAY:
60
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Int32Array);
61
- case constant_1.BUILTIN_CLASS_UINT32ARRAY:
62
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Uint32Array);
63
- case constant_1.BUILTIN_CLASS_FLOAT32ARRAY:
64
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Float32Array);
65
- case constant_1.BUILTIN_CLASS_FLOAT64ARRAY:
66
- return _deserializeArrayInstance(parsedObj[constant_1.ARRAY_FIELD], Float64Array);
67
- case constant_1.BUILTIN_CLASS_BIGINT64ARRAY:
68
- return _deserializeBigIntArrayInstance(parsedObj[constant_1.ARRAY_FIELD], BigInt64Array);
69
- case constant_1.BUILTIN_CLASS_BIGUINT64ARRAY:
70
- return _deserializeBigIntArrayInstance(parsedObj[constant_1.ARRAY_FIELD], BigUint64Array);
71
- case constant_1.BUILTIN_TYPE_BIG_INT:
72
- return deserializeBigInt(parsedObj[constant_1.TO_STRING_FIELD]);
73
- case constant_1.BUILTIN_TYPE_UNDEFINED:
74
- return undefined;
75
- case constant_1.BUILTIN_TYPE_NOT_FINITE:
76
- return (0, general_1.getValueFromToStringResult)(parsedObj[constant_1.TO_STRING_FIELD]);
77
- case constant_1.BUILTIN_CLASS_ARRAYBUFFER:
78
- return _deserializeArrayBuffer(parsedObj[constant_1.ARRAY_FIELD]);
79
- case constant_1.BUILTIN_CLASS_SHAREDARRAYBUFFER:
80
- return _deserializeSharedArrayBuffer(parsedObj[constant_1.ARRAY_FIELD]);
81
- case constant_1.BUILTIN_CLASS_BOOLEAN:
82
- return deserializeBoolean(parsedObj);
83
- case constant_1.BUILTIN_CLASS_DATAVIEW:
84
- return _deserializeDataView(parsedObj[constant_1.ARRAY_FIELD]);
85
- case constant_1.BUILTIN_CLASS_DATE:
86
- return deserializeDate(parsedObj);
87
- case constant_1.BUILTIN_CLASS_INTL_COLLATOR:
88
- return _deserializeIntlInstance(parsedObj, Intl.Collator);
89
- case constant_1.BUILTIN_CLASS_INTL_DATETIMEFORMAT:
90
- return _deserializeIntlInstance(parsedObj, Intl.DateTimeFormat);
91
- case constant_1.BUILTIN_CLASS_INTL_LISTFORMAT:
92
- // @ts-ignore
93
- return _deserializeIntlInstance(parsedObj, Intl.ListFormat);
94
- case constant_1.BUILTIN_CLASS_INTL_LOCALE:
95
- // @ts-ignore
96
- return new Intl.Locale(parsedObj[constant_1.TO_STRING_FIELD]);
97
- case constant_1.BUILTIN_CLASS_INTL_NUMBERFORMAT:
98
- return _deserializeIntlInstance(parsedObj, Intl.NumberFormat);
99
- case constant_1.BUILTIN_CLASS_INTL_PLURALRULES:
100
- return _deserializeIntlInstance(parsedObj, Intl.PluralRules);
101
- case constant_1.BUILTIN_CLASS_INTL_RELATIVETIMEFORMAT:
102
- // @ts-ignore
103
- return _deserializeIntlInstance(parsedObj, Intl.RelativeTimeFormat);
104
- case constant_1.BUILTIN_CLASS_REGEXP:
105
- return deserializeRegExp(parsedObj);
106
- case constant_1.BUILTIN_CLASS_SET:
107
- return _deserializeSet(parsedObj, classMapping);
108
- case constant_1.BUILTIN_CLASS_STRING:
109
- return deserializeString(parsedObj);
110
- case constant_1.BUILTIN_CLASS_ERROR:
111
- return deserializeError(parsedObj, Error);
112
- case constant_1.BUILTIN_CLASS_EVAL_ERROR:
113
- return deserializeError(parsedObj, EvalError);
114
- case constant_1.BUILTIN_CLASS_RANGE_ERROR:
115
- return deserializeError(parsedObj, RangeError);
116
- case constant_1.BUILTIN_CLASS_REFERENCE_ERROR:
117
- return deserializeError(parsedObj, ReferenceError);
118
- case constant_1.BUILTIN_CLASS_SYNTAX_ERROR:
119
- return deserializeError(parsedObj, SyntaxError);
120
- case constant_1.BUILTIN_CLASS_TYPE_ERROR:
121
- return deserializeError(parsedObj, TypeError);
122
- case constant_1.BUILTIN_CLASS_URI_ERROR:
123
- return deserializeError(parsedObj, URIError);
124
- case constant_1.BUILTIN_CLASS_AGGREGATE_ERROR:
125
- return deserializeError(parsedObj, AggregateError);
126
- default:
127
- return constant_1.ESSERIALIZER_NULL;
128
- }
129
- }
130
- function _deserializeArrayBuffer(byteArray) {
131
- return new Uint8Array(byteArray).buffer;
132
- }
133
- function _deserializeSharedArrayBuffer(byteArray) {
134
- const sab = new SharedArrayBuffer(byteArray.length);
135
- const sabViewArray = new Uint8Array(sab);
136
- byteArray.forEach((byte, index) => {
137
- sabViewArray[index] = byte;
138
- });
139
- return sab;
140
- }
141
- function _deserializeDataView(byteArray) {
142
- return new DataView(new Uint8Array(byteArray).buffer);
143
- }
144
- function _deserializeArrayInstance(arr, ArrayClass) {
145
- return new ArrayClass(arr);
146
- }
147
- function _deserializeBigIntArrayInstance(arr, ArrayClass) {
148
- return new ArrayClass(arr.map((biObj) => {
149
- return deserializeBigInt(biObj[constant_1.TO_STRING_FIELD]);
150
- }));
151
- }
152
- function deserializeBigInt(str) {
153
- return BigInt(str);
154
- }
155
- function deserializeBoolean(parsedObj) {
156
- // noinspection JSPrimitiveTypeWrapperUsage
157
- return new Boolean(parsedObj[constant_1.BOOLEAN_FIELD]);
158
- }
159
- function deserializeDate(parsedObj) {
160
- return typeof parsedObj[constant_1.TIMESTAMP_FIELD] === 'number' ? new Date(parsedObj[constant_1.TIMESTAMP_FIELD]) : null;
161
- }
162
- function _deserializeIntlInstance(parsedObj, IntlClass) {
163
- const options = parsedObj[constant_1.OPTIONS_FIELD];
164
- const locale = options.locale;
165
- delete options.locale;
166
- return new IntlClass(locale, options);
167
- }
168
- function deserializeRegExp(parsedObj) {
169
- const regExpStr = parsedObj[constant_1.TO_STRING_FIELD];
170
- const lastIndexOfSlash = regExpStr.lastIndexOf('/');
171
- return new RegExp(regExpStr.substring(1, lastIndexOfSlash), regExpStr.substring(lastIndexOfSlash + 1));
172
- }
173
- function _deserializeSet(parsedObj, classMapping) {
174
- return new Set(_deserializeArray(parsedObj[constant_1.ARRAY_FIELD], classMapping));
175
- }
176
- function deserializeString(parsedObj) {
177
- // noinspection JSPrimitiveTypeWrapperUsage
178
- return new String(parsedObj[constant_1.TO_STRING_FIELD]);
179
- }
180
- function deserializeError(parsedObj, ErrorClass) {
181
- let error;
182
- if (parsedObj.message) {
183
- // @ts-ignore
184
- error = new ErrorClass(parsedObj.message);
185
- }
186
- else {
187
- // @ts-ignore
188
- error = new ErrorClass();
189
- }
190
- delete error.stack;
191
- if (parsedObj.name) {
192
- error.name = parsedObj.name;
193
- }
194
- if (parsedObj.stack) {
195
- error.stack = parsedObj.stack;
196
- }
197
- if (ErrorClass === AggregateError) {
198
- error.errors = deserializeFromParsedObjWithClassMapping(parsedObj.errors, {});
199
- }
200
- return error;
201
- }
202
- function deserializeClassProperty(classObj, constructorParameters) {
203
- if (!classObj) {
204
- return {};
205
- }
206
- const additionalParameterNumber = Math.max(classObj.length - constructorParameters.length, 0);
207
- const FALLBACK_PARAMETERS = [{}, 0, '', null];
208
- let fallbackParam = FALLBACK_PARAMETERS.shift();
209
- let deserializedObj;
210
- while (!deserializedObj && fallbackParam !== undefined) {
211
- deserializedObj = createDeserializedObj(classObj, constructorParameters.concat(Array.from(Array(additionalParameterNumber), () => (fallbackParam))));
212
- if (additionalParameterNumber === 0) {
213
- fallbackParam = undefined;
214
- }
215
- else {
216
- fallbackParam = FALLBACK_PARAMETERS.shift();
217
- }
218
- }
219
- if (!deserializedObj) {
220
- deserializedObj = {};
221
- Object.setPrototypeOf(deserializedObj, classObj ? classObj.prototype : Object.prototype);
222
- }
223
- return deserializedObj;
224
- }
225
- function createDeserializedObj(classObj, allConstructorParameters) {
226
- let deserializedObj;
227
- try {
228
- if (REGEXP_BEGIN_WITH_CLASS.test(classObj.toString())) {
229
- deserializedObj = new classObj(...allConstructorParameters);
230
- }
231
- else { // It's class in function style.
232
- let objectConstructor = classObj.prototype.constructor;
233
- if (objectConstructor.name === 'Object') {
234
- objectConstructor = classObj; // In case object's constructor is not in its prototype, such as Big in big.js
235
- }
236
- deserializedObj = Object.create(objectConstructor.prototype);
237
- objectConstructor.call(deserializedObj, allConstructorParameters);
238
- }
239
- }
240
- catch (e) {
241
- deserializedObj = null;
242
- }
243
- return deserializedObj;
244
- }
245
- function deserializeValuesWithClassMapping(deserializedObj, parsedObj, classMapping, options) {
246
- for (const k in parsedObj) {
247
- const v = parsedObj[k];
248
- if (options.ignoreProperties && options.ignoreProperties.includes(k)) {
249
- continue;
250
- }
251
- if (options.rawProperties && options.rawProperties.includes(k)) {
252
- deserializedObj[k] = JSON.stringify(v);
253
- continue;
254
- }
255
- const descriptor = Object.getOwnPropertyDescriptor(deserializedObj, k);
256
- if (canSkipCopyingValue(k, v, descriptor)) {
257
- continue;
258
- }
259
- if (descriptor && descriptor.writable === false && typeof v === 'object') {
260
- assignWritableField(deserializedObj[k], v, classMapping);
261
- continue;
262
- }
263
- deserializedObj[k] = deserializeFromParsedObjWithClassMapping(v, classMapping);
264
- }
265
- return deserializedObj;
266
- }
267
- function canSkipCopyingValue(keyOfParsedObj, valueOfParsedObj, descriptorOfDeserializedObjProperty) {
268
- if (keyOfParsedObj === constant_1.CLASS_NAME_FIELD) {
269
- return true;
270
- }
271
- return descriptorOfDeserializedObjProperty && (typeof descriptorOfDeserializedObjProperty.set === 'function' ||
272
- (descriptorOfDeserializedObjProperty.writable === false && typeof valueOfParsedObj !== 'object'));
273
- }
274
- function assignWritableField(targetObj, sourceObj, classMapping) {
275
- for (const field in sourceObj) {
276
- const descriptor = Object.getOwnPropertyDescriptor(targetObj, field);
277
- if (descriptor && !(descriptor.writable === true || typeof descriptor.set === 'function')) {
278
- continue;
279
- }
280
- targetObj[field] = deserializeFromParsedObjWithClassMapping(sourceObj[field], classMapping);
281
- }
282
- }
283
- /**
284
- *
285
- * @param classes It's an array of Class definition. "any" is used in code only
286
- * because there is no TypeScript type definition for Class.
287
- */
288
- function getClassMappingFromClassArray(classes = []) {
289
- const classMapping = {};
290
- classes.forEach((c) => {
291
- if (!(0, general_1.isClass)(c)) {
292
- return;
293
- }
294
- const className = c.name;
295
- const previousClass = classMapping[className];
296
- if (previousClass && previousClass !== c) {
297
- console.warn('WARNING: Found class definition with the same name: ' + className);
298
- }
299
- // @ts-ignore
300
- classMapping[className] = c;
301
- });
302
- return classMapping;
303
- }
304
- exports.getClassMappingFromClassArray = getClassMappingFromClassArray;
305
- /**
306
- *
307
- * @param classObj It's a Class definition. "any" is used in code only
308
- * because there is no TypeScript type definition for Class.
309
- */
310
- function getParentClassName(classObj) {
311
- return classObj.prototype.__proto__.constructor.name;
312
- }
313
- exports.getParentClassName = getParentClassName;
1
+ "use strict";
2
+ // The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
3
+ // All credit goes to him. See: https://www.npmjs.com/package/esserializer
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.deserializeFromParsedObj = deserializeFromParsedObj;
6
+ exports.deserializeFromParsedObjWithClassMapping = deserializeFromParsedObjWithClassMapping;
7
+ exports.getClassMappingFromClassArray = getClassMappingFromClassArray;
8
+ exports.getParentClassName = getParentClassName;
9
+ const general_1 = require("./general");
10
+ const constant_1 = require("./constant");
11
+ const builtins_1 = require("./builtins");
12
+ const REGEXP_BEGIN_WITH_CLASS = /^\s*class\s+/;
13
+ /**
14
+ * Values tried, in order, for the constructor parameters that have no saved value.
15
+ * Some constructors throw for some values (for example, `{}` where a string is expected),
16
+ * so we try each one until the constructor works.
17
+ */
18
+ const FALLBACK_CONSTRUCTOR_PARAMETERS = [{}, 0, '', null];
19
+ /**
20
+ * Turns a parsed JSON value back into the original value, creating instances of the
21
+ * given `classes` where the JSON has their names in the `*type` field.
22
+ */
23
+ function deserializeFromParsedObj(parsedObj, classes, options) {
24
+ //
25
+ return deserializeFromParsedObjWithClassMapping(parsedObj, getClassMappingFromClassArray(classes), options);
26
+ }
27
+ /**
28
+ * Same as `deserializeFromParsedObj`, but with the classes already mapped by name.
29
+ * The `options` apply only to the top-level object, not to nested ones.
30
+ */
31
+ function deserializeFromParsedObjWithClassMapping(parsedObj, classMapping, options = {}) {
32
+ if ((0, general_1.notObject)(parsedObj)) {
33
+ return parsedObj;
34
+ }
35
+ const deserializeNested = (value) => deserializeFromParsedObjWithClassMapping(value, classMapping);
36
+ if (Array.isArray(parsedObj)) {
37
+ return _deserializeArray(parsedObj, deserializeNested);
38
+ }
39
+ const type = parsedObj[constant_1.TYPE_FIELD];
40
+ // A plain object.
41
+ if (type === undefined) {
42
+ return _copyDeserializedValues({}, parsedObj, classMapping, options);
43
+ }
44
+ // A built-in type.
45
+ if (type.startsWith(constant_1.BUILTIN_PREFIX)) {
46
+ const value = parsedObj[constant_1.VALUE_FIELD];
47
+ switch (type) {
48
+ case constant_1.TYPE_UNDEFINED:
49
+ return undefined;
50
+ case constant_1.TYPE_NUMBER:
51
+ return (0, general_1.getValueFromToStringResult)(value);
52
+ case constant_1.TYPE_BIGINT:
53
+ return BigInt(value);
54
+ case constant_1.TYPE_NULL_PROTOTYPE:
55
+ return _copyDeserializedValues(Object.create(null), parsedObj, classMapping, options);
56
+ }
57
+ const builtinType = (0, builtins_1.getBuiltinTypeBySavedType)(type);
58
+ if (!builtinType) {
59
+ throw new Error(`Unknown built-in type "${type}" found during deserialization.`);
60
+ }
61
+ const instance = builtinType.decode(value, deserializeNested);
62
+ return _copyDeserializedValues(instance, parsedObj, classMapping, options);
63
+ }
64
+ // A user class.
65
+ const classObj = Object.prototype.hasOwnProperty.call(classMapping, type) ? classMapping[type] : undefined;
66
+ if (!classObj) {
67
+ throw new Error(`Class "${type}" not found during deserialization. You must register it with: "ESSerializer.registerClass(${type});"`);
68
+ }
69
+ const constructorParameters = (options.fieldsForConstructorParameters ?? []).map((field) => {
70
+ const savedKey = (0, general_1.escapeKey)(field);
71
+ // Pass `{}` for missing fields, instead of undefined.
72
+ return savedKey in parsedObj ? parsedObj[savedKey] : {};
73
+ });
74
+ const instance = _createInstance(classObj, constructorParameters);
75
+ // A user class that extends a built-in class also has the built-in data, like the entries of a Map.
76
+ if (constant_1.VALUE_FIELD in parsedObj) {
77
+ (0, builtins_1.findInheritedBuiltinType)(instance)?.fill?.(instance, parsedObj[constant_1.VALUE_FIELD], deserializeNested);
78
+ }
79
+ return _copyDeserializedValues(instance, parsedObj, classMapping, options);
80
+ }
81
+ /** Deserializes the items of an array. Holes saved as `{"*type": "@hole"}` are kept as holes. */
82
+ function _deserializeArray(parsedArray, deserializeNested) {
83
+ const result = new Array(parsedArray.length);
84
+ parsedArray.forEach((item, index) => {
85
+ if (item?.[constant_1.TYPE_FIELD] !== constant_1.TYPE_HOLE) {
86
+ result[index] = deserializeNested(item);
87
+ }
88
+ });
89
+ return result;
90
+ }
91
+ /**
92
+ * Creates an instance of `classObj`, to receive the
93
+ * saved values. Constructor parameters not given in `constructorParameters` are filled with
94
+ * fallback values. If the constructor throws for all of them, the instance is created without
95
+ * calling the constructor.
96
+ */
97
+ function _createInstance(classObj, constructorParameters) {
98
+ // Not `classObj.toString()`, which may be a static method of the class.
99
+ const isEs6Class = REGEXP_BEGIN_WITH_CLASS.test(Function.prototype.toString.call(classObj));
100
+ const missingParameterCount = Math.max(classObj.length - constructorParameters.length, 0);
101
+ // When no parameter is missing, the fallback values are not used, so we try only once.
102
+ const fallbacks = missingParameterCount === 0
103
+ ? FALLBACK_CONSTRUCTOR_PARAMETERS.slice(0, 1)
104
+ : FALLBACK_CONSTRUCTOR_PARAMETERS;
105
+ for (const fallback of fallbacks) {
106
+ const parameters = constructorParameters.concat(new Array(missingParameterCount).fill(fallback));
107
+ const instance = _tryToConstruct(classObj, isEs6Class, parameters);
108
+ if (instance) {
109
+ return instance;
110
+ }
111
+ }
112
+ return Object.create(classObj.prototype);
113
+ }
114
+ /** Calls the constructor. Returns `null` if it throws. */
115
+ function _tryToConstruct(classObj, isEs6Class, parameters) {
116
+ try {
117
+ if (isEs6Class) {
118
+ return new classObj(...parameters);
119
+ }
120
+ // A function-style class.
121
+ let constructor = classObj.prototype.constructor;
122
+ if (constructor === Object) {
123
+ // In case the constructor is not in the prototype, such as Big in big.js.
124
+ constructor = classObj;
125
+ }
126
+ const instance = Object.create(constructor.prototype);
127
+ constructor.apply(instance, parameters);
128
+ return instance;
129
+ }
130
+ catch {
131
+ return null;
132
+ }
133
+ }
134
+ /**
135
+ * Copies the deserialized values of `parsedObj` into `instance`, and returns it.
136
+ *
137
+ * Properties with a getter or a setter (own or inherited from the class), and read-only
138
+ * properties inherited from the class, keep the value the constructor gave them. Own read-only
139
+ * properties with object values are updated in place (see `_copyInPlace`).
140
+ *
141
+ * Some saved values can't be set in `instance`: a primitive value in an own read-only property
142
+ * (for example, when the constructor calls `Object.freeze(this)`), or a new property in an
143
+ * object that can't get new properties. Then a copy of `instance` with these values is returned
144
+ * instead (see `_copyWithValues`).
145
+ */
146
+ function _copyDeserializedValues(instance, parsedObj, classMapping, options) {
147
+ const { ignoreProperties, rawProperties } = options;
148
+ // The values that can't be set in `instance`.
149
+ const valuesForCopy = new Map();
150
+ const setValue = (key, value) => {
151
+ const own = Object.getOwnPropertyDescriptor(instance, key);
152
+ const canSet = own ? own.writable !== false : Object.isExtensible(instance);
153
+ if (canSet) {
154
+ (0, general_1.setOwnProperty)(instance, key, value);
155
+ }
156
+ else if (!own || !Object.is(own.value, value)) {
157
+ valuesForCopy.set(key, value);
158
+ }
159
+ };
160
+ for (const savedKey in parsedObj) {
161
+ if (savedKey === constant_1.TYPE_FIELD || savedKey === constant_1.VALUE_FIELD) {
162
+ continue;
163
+ }
164
+ const key = (0, general_1.unescapeKey)(savedKey);
165
+ if (ignoreProperties?.includes(key)) {
166
+ continue;
167
+ }
168
+ const value = parsedObj[savedKey];
169
+ if (rawProperties?.includes(key)) {
170
+ setValue(key, JSON.stringify(value));
171
+ continue;
172
+ }
173
+ const descriptor = _findPropertyDescriptor(instance, key);
174
+ if (descriptor) {
175
+ // A getter or a setter. Setting a getter-only property would throw.
176
+ if (descriptor.get || descriptor.set) {
177
+ continue;
178
+ }
179
+ if (descriptor.writable === false) {
180
+ // An inherited value is shared by all instances, so it's not changed.
181
+ if (Object.prototype.hasOwnProperty.call(instance, key)) {
182
+ const current = instance[key];
183
+ setValue(key, (0, general_1.notObject)(current) || _isSavedPrimitive(value)
184
+ ? deserializeFromParsedObjWithClassMapping(value, classMapping)
185
+ : _copyInPlace(current, value, classMapping));
186
+ }
187
+ continue;
188
+ }
189
+ }
190
+ setValue(key, deserializeFromParsedObjWithClassMapping(value, classMapping));
191
+ }
192
+ return valuesForCopy.size === 0 ? instance : _copyWithValues(instance, valuesForCopy);
193
+ }
194
+ /** Returns true if the saved `value` is a primitive, or a primitive JSON can't represent, like `NaN`. */
195
+ function _isSavedPrimitive(value) {
196
+ if ((0, general_1.notObject)(value)) {
197
+ return true;
198
+ }
199
+ const type = value[constant_1.TYPE_FIELD];
200
+ return type === constant_1.TYPE_UNDEFINED || type === constant_1.TYPE_NUMBER || type === constant_1.TYPE_BIGINT;
201
+ }
202
+ /**
203
+ * Copies the saved `source` into the existing object `target`, in place: the built-in data
204
+ * (like the entries of a Map), and the fields, like `_copyDeserializedValues` does.
205
+ * Returns `target`, or a copy of it, if some saved values can't be set in it
206
+ * (see `_copyDeserializedValues`).
207
+ */
208
+ function _copyInPlace(target, source, classMapping) {
209
+ if (constant_1.VALUE_FIELD in source) {
210
+ const deserializeNested = (value) => deserializeFromParsedObjWithClassMapping(value, classMapping);
211
+ (0, builtins_1.findInheritedBuiltinType)(target)?.fill?.(target, source[constant_1.VALUE_FIELD], deserializeNested);
212
+ }
213
+ return _copyDeserializedValues(target, source, classMapping, {});
214
+ }
215
+ /**
216
+ * Returns a copy of `instance`, with the given `values` in place of its own properties, or as
217
+ * new properties. The copy has the same prototype and the same property attributes, and it's
218
+ * frozen, sealed, or can't get new properties, if `instance` is.
219
+ *
220
+ * The copy is created without calling the constructor, as `_createInstance` does when the
221
+ * constructor throws. So it doesn't have the private fields (`#field`) of the class.
222
+ *
223
+ * An instance of a class that extends a built-in class (like a Map) keeps its built-in data
224
+ * where a copy can't have it. So it is returned as is, without the `values`.
225
+ */
226
+ function _copyWithValues(instance, values) {
227
+ if ((0, builtins_1.findInheritedBuiltinType)(instance)) {
228
+ return instance;
229
+ }
230
+ const copy = Object.create(Object.getPrototypeOf(instance));
231
+ const descriptors = Object.getOwnPropertyDescriptors(instance);
232
+ for (const key of Reflect.ownKeys(descriptors)) {
233
+ const descriptor = descriptors[key];
234
+ if (typeof key === 'string' && values.has(key)) {
235
+ descriptor.value = values.get(key);
236
+ }
237
+ Object.defineProperty(copy, key, descriptor);
238
+ }
239
+ for (const [key, value] of values) {
240
+ if (!Object.prototype.hasOwnProperty.call(descriptors, key)) {
241
+ Object.defineProperty(copy, key, { value, writable: true, enumerable: true, configurable: true });
242
+ }
243
+ }
244
+ if (Object.isFrozen(instance)) {
245
+ Object.freeze(copy);
246
+ }
247
+ else if (Object.isSealed(instance)) {
248
+ Object.seal(copy);
249
+ }
250
+ else if (!Object.isExtensible(instance)) {
251
+ Object.preventExtensions(copy);
252
+ }
253
+ return copy;
254
+ }
255
+ /**
256
+ * Returns the descriptor of the property `key` of `obj`, which may be its own, or inherited
257
+ * from its class. `Object.prototype` is not searched, so that its `__proto__` accessor doesn't
258
+ * hide a saved `__proto__` key.
259
+ */
260
+ function _findPropertyDescriptor(obj, key) {
261
+ for (let current = obj; current !== null && current !== Object.prototype; current = Object.getPrototypeOf(current)) {
262
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
263
+ if (descriptor) {
264
+ return descriptor;
265
+ }
266
+ }
267
+ return undefined;
268
+ }
269
+ /**
270
+ * Maps each class to the name it's saved under. Items that are not classes (like enums) are
271
+ * ignored. Warns if two different classes are saved under the same name.
272
+ *
273
+ * @param classes Class definitions. They are typed as `any` because TypeScript has no type for classes.
274
+ */
275
+ function getClassMappingFromClassArray(classes = []) {
276
+ const classMapping = {};
277
+ for (const classObj of classes) {
278
+ if (!(0, general_1.isClass)(classObj)) {
279
+ continue;
280
+ }
281
+ const className = (0, general_1.getClassKey)(classObj);
282
+ const previousClass = classMapping[className];
283
+ if (previousClass && previousClass !== classObj) {
284
+ console.warn('WARNING: Found class definition with the same name: ' + className);
285
+ }
286
+ classMapping[className] = classObj;
287
+ }
288
+ return classMapping;
289
+ }
290
+ /** Returns the name of the parent class of `classObj` (or `Object`, if it has none). */
291
+ function getParentClassName(classObj) {
292
+ return Object.getPrototypeOf(classObj.prototype).constructor.name;
293
+ }