kiss-for-react 1.1.0 → 2.0.0
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/README.md +699 -156
- package/lib/cjs/ClassPersistor.js +177 -112
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
- package/lib/cjs/Esserializer/builtins.js +258 -0
- package/lib/cjs/Esserializer/constant.js +39 -131
- package/lib/cjs/Esserializer/deserializer.js +226 -313
- package/lib/cjs/Esserializer/general.js +91 -39
- package/lib/cjs/Esserializer/index.js +47 -43
- package/lib/cjs/Esserializer/serializer.js +168 -133
- package/lib/cjs/Evt.js +132 -0
- package/lib/cjs/Hooks.js +1472 -969
- package/lib/cjs/KissAction.js +4635 -1333
- package/lib/cjs/LoadPages.js +336 -0
- package/lib/cjs/Persistor.js +189 -120
- package/lib/cjs/ProcessPersistence.js +579 -262
- package/lib/cjs/Store.js +4278 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +88 -0
- package/lib/cjs/UserException.js +212 -218
- package/lib/cjs/Wait.js +309 -309
- package/lib/cjs/index.js +69 -42
- package/lib/cjs/server.js +54 -0
- package/lib/cjs/types/ClassPersistor.d.ts +143 -112
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
- package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/general.d.ts +28 -4
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/index.d.ts +33 -29
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/cjs/types/Evt.d.ts +124 -0
- package/lib/cjs/types/Evt.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +1174 -837
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +5199 -1398
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/LoadPages.d.ts +287 -0
- package/lib/cjs/types/LoadPages.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +187 -118
- package/lib/cjs/types/Persistor.d.ts.map +1 -1
- package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +2253 -1042
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreException.d.ts +97 -28
- package/lib/cjs/types/StoreException.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +80 -0
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +178 -169
- package/lib/cjs/types/UserException.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +12 -9
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/server.d.ts +12 -0
- package/lib/cjs/types/server.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +15 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +45 -7
- package/lib/esm/ClassPersistor.mjs +177 -0
- package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
- package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
- package/lib/esm/Esserializer/builtins.mjs +254 -0
- package/lib/esm/Esserializer/constant.mjs +28 -0
- package/lib/esm/Esserializer/deserializer.mjs +221 -0
- package/lib/esm/Esserializer/general.mjs +81 -0
- package/lib/esm/Esserializer/index.mjs +43 -0
- package/lib/esm/Esserializer/serializer.mjs +164 -0
- package/lib/esm/Evt.mjs +151 -0
- package/lib/esm/Hooks.mjs +1446 -0
- package/lib/esm/KissAction.mjs +4689 -0
- package/lib/esm/LoadPages.mjs +340 -0
- package/lib/esm/Persistor.mjs +180 -0
- package/lib/esm/ProcessPersistence.mjs +581 -0
- package/lib/esm/Store.mjs +4400 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +80 -0
- package/lib/esm/{UserException.js → UserException.mjs} +240 -246
- package/lib/esm/{Wait.js → Wait.mjs} +309 -309
- package/lib/esm/index.mjs +12 -9
- package/lib/esm/server.mjs +23 -0
- package/lib/esm/types/ClassPersistor.d.ts +143 -112
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
- package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +25 -49
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/general.d.ts +28 -4
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/index.d.ts +33 -29
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/esm/types/Evt.d.ts +124 -0
- package/lib/esm/types/Evt.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +1174 -837
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +5199 -1398
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/LoadPages.d.ts +287 -0
- package/lib/esm/types/LoadPages.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +187 -118
- package/lib/esm/types/Persistor.d.ts.map +1 -1
- package/lib/esm/types/ProcessPersistence.d.ts +197 -99
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +2253 -1042
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreException.d.ts +97 -28
- package/lib/esm/types/StoreException.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +80 -0
- package/lib/esm/types/StoreProvider.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +178 -169
- package/lib/esm/types/UserException.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +12 -9
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/server.d.ts +12 -0
- package/lib/esm/types/server.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +15 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.mjs +38 -0
- package/package.json +54 -30
- package/lib/cjs/UnmodifiableSetView.js +0 -53
- package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/ClassPersistor.js +0 -112
- package/lib/esm/Esserializer/constant.js +0 -81
- package/lib/esm/Esserializer/deserializer.js +0 -307
- package/lib/esm/Esserializer/general.js +0 -34
- package/lib/esm/Esserializer/index.js +0 -39
- package/lib/esm/Esserializer/serializer.js +0 -130
- package/lib/esm/Hooks.js +0 -955
- package/lib/esm/KissAction.js +0 -1459
- package/lib/esm/Persistor.js +0 -113
- package/lib/esm/ProcessPersistence.js +0 -262
- package/lib/esm/Store.js +0 -1606
- package/lib/esm/StoreException.js +0 -69
- package/lib/esm/UnmodifiableSetView.js +0 -50
- package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/utils.js +0 -3
|
@@ -1,313 +1,226 @@
|
|
|
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.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
return
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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`.
|
|
136
|
+
* Properties with a getter or a setter (own or inherited from the class), and read-only
|
|
137
|
+
* properties with primitive values, keep the value the constructor gave them. Own read-only
|
|
138
|
+
* properties with object values are updated in place (see `_copyInPlace`).
|
|
139
|
+
*/
|
|
140
|
+
function _copyDeserializedValues(instance, parsedObj, classMapping, options) {
|
|
141
|
+
const { ignoreProperties, rawProperties } = options;
|
|
142
|
+
for (const savedKey in parsedObj) {
|
|
143
|
+
if (savedKey === constant_1.TYPE_FIELD || savedKey === constant_1.VALUE_FIELD) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const key = (0, general_1.unescapeKey)(savedKey);
|
|
147
|
+
if (ignoreProperties?.includes(key)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const value = parsedObj[savedKey];
|
|
151
|
+
if (rawProperties?.includes(key)) {
|
|
152
|
+
(0, general_1.setOwnProperty)(instance, key, JSON.stringify(value));
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const descriptor = _findPropertyDescriptor(instance, key);
|
|
156
|
+
if (descriptor) {
|
|
157
|
+
// A getter or a setter. Setting a getter-only property would throw.
|
|
158
|
+
if (descriptor.get || descriptor.set) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (descriptor.writable === false) {
|
|
162
|
+
// An inherited object is shared by all instances, so it's not changed.
|
|
163
|
+
if (!(0, general_1.notObject)(value) && Object.prototype.hasOwnProperty.call(instance, key)) {
|
|
164
|
+
_copyInPlace(instance[key], value, classMapping);
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
(0, general_1.setOwnProperty)(instance, key, deserializeFromParsedObjWithClassMapping(value, classMapping));
|
|
170
|
+
}
|
|
171
|
+
return instance;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Copies the saved `source` into the existing `target`, in place: the built-in data (like the
|
|
175
|
+
* entries of a Map), and the fields, like `_copyDeserializedValues` does.
|
|
176
|
+
* If `target` is a primitive, it's kept as is.
|
|
177
|
+
*/
|
|
178
|
+
function _copyInPlace(target, source, classMapping) {
|
|
179
|
+
if ((0, general_1.notObject)(target)) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (constant_1.VALUE_FIELD in source) {
|
|
183
|
+
const deserializeNested = (value) => deserializeFromParsedObjWithClassMapping(value, classMapping);
|
|
184
|
+
(0, builtins_1.findInheritedBuiltinType)(target)?.fill?.(target, source[constant_1.VALUE_FIELD], deserializeNested);
|
|
185
|
+
}
|
|
186
|
+
_copyDeserializedValues(target, source, classMapping, {});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns the descriptor of the property `key` of `obj`, which may be its own, or inherited
|
|
190
|
+
* from its class. `Object.prototype` is not searched, so that its `__proto__` accessor doesn't
|
|
191
|
+
* hide a saved `__proto__` key.
|
|
192
|
+
*/
|
|
193
|
+
function _findPropertyDescriptor(obj, key) {
|
|
194
|
+
for (let current = obj; current !== null && current !== Object.prototype; current = Object.getPrototypeOf(current)) {
|
|
195
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
196
|
+
if (descriptor) {
|
|
197
|
+
return descriptor;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Maps each class to the name it's saved under. Items that are not classes (like enums) are
|
|
204
|
+
* ignored. Warns if two different classes are saved under the same name.
|
|
205
|
+
*
|
|
206
|
+
* @param classes Class definitions. They are typed as `any` because TypeScript has no type for classes.
|
|
207
|
+
*/
|
|
208
|
+
function getClassMappingFromClassArray(classes = []) {
|
|
209
|
+
const classMapping = {};
|
|
210
|
+
for (const classObj of classes) {
|
|
211
|
+
if (!(0, general_1.isClass)(classObj)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const className = (0, general_1.getClassKey)(classObj);
|
|
215
|
+
const previousClass = classMapping[className];
|
|
216
|
+
if (previousClass && previousClass !== classObj) {
|
|
217
|
+
console.warn('WARNING: Found class definition with the same name: ' + className);
|
|
218
|
+
}
|
|
219
|
+
classMapping[className] = classObj;
|
|
220
|
+
}
|
|
221
|
+
return classMapping;
|
|
222
|
+
}
|
|
223
|
+
/** Returns the name of the parent class of `classObj` (or `Object`, if it has none). */
|
|
224
|
+
function getParentClassName(classObj) {
|
|
225
|
+
return Object.getPrototypeOf(classObj.prototype).constructor.name;
|
|
226
|
+
}
|