kiss-for-react 1.0.3 → 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 +55 -31
- 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,39 +1,91 @@
|
|
|
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
|
-
exports.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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.getValueFromToStringResult = getValueFromToStringResult;
|
|
6
|
+
exports.notObject = notObject;
|
|
7
|
+
exports.isClass = isClass;
|
|
8
|
+
exports.isNativeFunction = isNativeFunction;
|
|
9
|
+
exports.getClassKey = getClassKey;
|
|
10
|
+
exports.escapeKey = escapeKey;
|
|
11
|
+
exports.unescapeKey = unescapeKey;
|
|
12
|
+
exports.setOwnProperty = setOwnProperty;
|
|
13
|
+
exports.setHiddenProperty = setHiddenProperty;
|
|
14
|
+
const constant_1 = require("./constant");
|
|
15
|
+
/** Returns true for primitives (including `null`), and for functions. */
|
|
16
|
+
function notObject(target) {
|
|
17
|
+
return target === null || typeof target !== 'object';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts the text form of a number JSON can't represent back into the number.
|
|
21
|
+
* Returns `null` for any other text.
|
|
22
|
+
*/
|
|
23
|
+
function getValueFromToStringResult(text) {
|
|
24
|
+
switch (text) {
|
|
25
|
+
case 'Infinity':
|
|
26
|
+
return Infinity;
|
|
27
|
+
case '-Infinity':
|
|
28
|
+
return -Infinity;
|
|
29
|
+
case 'NaN':
|
|
30
|
+
return NaN;
|
|
31
|
+
case '-0':
|
|
32
|
+
return -0;
|
|
33
|
+
default:
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns the name a class is saved under: its own static `typeName`, if it has one,
|
|
39
|
+
* or else its class name. A `typeName` inherited from a parent class is ignored.
|
|
40
|
+
*/
|
|
41
|
+
function getClassKey(classObj) {
|
|
42
|
+
if (Object.prototype.hasOwnProperty.call(classObj, 'typeName')) {
|
|
43
|
+
return classObj.typeName;
|
|
44
|
+
}
|
|
45
|
+
return classObj.name;
|
|
46
|
+
}
|
|
47
|
+
/** Built-in classes that may be registered for deserialization, as if they were user classes. */
|
|
48
|
+
const SUPPORTED_BUILTIN_CLASSES = new Set([Date]);
|
|
49
|
+
/** Returns true if `target` can be called with `new` (a class, or a function-style class). */
|
|
50
|
+
function isClass(target) {
|
|
51
|
+
if (SUPPORTED_BUILTIN_CLASSES.has(target)) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
// `Reflect.construct` throws if `target` can't be used as a constructor.
|
|
55
|
+
// See https://stackoverflow.com/a/46759625/707451
|
|
56
|
+
try {
|
|
57
|
+
Reflect.construct(String, [], target);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Returns true if `fn` is implemented by the JavaScript engine, not by user code. */
|
|
65
|
+
function isNativeFunction(fn) {
|
|
66
|
+
return /\{\s*\[native code\]\s*\}\s*$/.test(Function.prototype.toString.call(fn));
|
|
67
|
+
}
|
|
68
|
+
/** Escapes a user key, so that it's not confused with the metadata fields. */
|
|
69
|
+
function escapeKey(key) {
|
|
70
|
+
return key.startsWith(constant_1.KEY_ESCAPE) ? constant_1.KEY_ESCAPE + key : key;
|
|
71
|
+
}
|
|
72
|
+
/** Reverses `escapeKey`. */
|
|
73
|
+
function unescapeKey(savedKey) {
|
|
74
|
+
return savedKey.startsWith(constant_1.KEY_ESCAPE) ? savedKey.substring(constant_1.KEY_ESCAPE.length) : savedKey;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Sets an own property. Unlike `obj[key] = value`, this also works for the key `__proto__`,
|
|
78
|
+
* which would otherwise change the prototype of `obj`, instead of creating a property.
|
|
79
|
+
*/
|
|
80
|
+
function setOwnProperty(obj, key, value) {
|
|
81
|
+
if (key === '__proto__') {
|
|
82
|
+
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
obj[key] = value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Sets a non-enumerable own property, like the `message` of an error. */
|
|
89
|
+
function setHiddenProperty(obj, key, value) {
|
|
90
|
+
Object.defineProperty(obj, key, { value, writable: true, enumerable: false, configurable: true });
|
|
91
|
+
}
|
|
@@ -1,43 +1,47 @@
|
|
|
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.ESSerializer = void 0;
|
|
6
|
-
const serializer_1 = require("./serializer");
|
|
7
|
-
const deserializer_1 = require("./deserializer");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
static
|
|
16
|
-
this.registeredClasses.push(
|
|
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
|
-
|
|
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.ESSerializer = void 0;
|
|
6
|
+
const serializer_1 = require("./serializer");
|
|
7
|
+
const deserializer_1 = require("./deserializer");
|
|
8
|
+
/**
|
|
9
|
+
* Serializes values into JSON text, keeping their classes, and deserializes them back
|
|
10
|
+
* into instances of those classes. Also supports built-in types JSON doesn't, like
|
|
11
|
+
* `undefined`, `NaN`, BigInt, Date, Map, Set, typed arrays, errors and Intl objects.
|
|
12
|
+
*/
|
|
13
|
+
class ESSerializer {
|
|
14
|
+
/** Globally registers classes or enums. */
|
|
15
|
+
static registerClasses(classesOrEnums) {
|
|
16
|
+
this.registeredClasses.push(...classesOrEnums);
|
|
17
|
+
}
|
|
18
|
+
/** Globally registers a class or enum. */
|
|
19
|
+
static registerClass(classOrEnum) {
|
|
20
|
+
this.registeredClasses.push(classOrEnum);
|
|
21
|
+
}
|
|
22
|
+
/** Clears all globally registered classes. */
|
|
23
|
+
static clearRegisteredClasses() {
|
|
24
|
+
this.registeredClasses = [];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Converts `target` into JSON text. The `target` is not changed.
|
|
28
|
+
* @param target The value to serialize.
|
|
29
|
+
* @param options Serialization options.
|
|
30
|
+
*/
|
|
31
|
+
static serialize(target, options = {}) {
|
|
32
|
+
return JSON.stringify((0, serializer_1.getSerializeValueWithClassName)(target, options));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Converts JSON text created by `serialize` back into the original value.
|
|
36
|
+
* @param serializedText The JSON text.
|
|
37
|
+
* @param classes Classes needed to deserialize the text, in addition to the registered ones.
|
|
38
|
+
* @param options Deserialization options.
|
|
39
|
+
*/
|
|
40
|
+
static deserialize(serializedText, classes = [], options = {}) {
|
|
41
|
+
//
|
|
42
|
+
return (0, deserializer_1.deserializeFromParsedObj)(JSON.parse(serializedText), this.registeredClasses.concat(classes), options);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.ESSerializer = ESSerializer;
|
|
46
|
+
/** Classes available to every `deserialize` call, in addition to the ones passed to it. */
|
|
47
|
+
ESSerializer.registeredClasses = [];
|
|
@@ -1,133 +1,168 @@
|
|
|
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.getSerializeValueWithClassName =
|
|
6
|
-
const constant_1 = require("./constant");
|
|
7
|
-
const general_1 = require("./general");
|
|
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
|
-
serializedObj
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
serializedObj
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
serializedObj[constant_1.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
serializedObj
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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.getSerializeValueWithClassName = getSerializeValueWithClassName;
|
|
6
|
+
const constant_1 = require("./constant");
|
|
7
|
+
const general_1 = require("./general");
|
|
8
|
+
const builtins_1 = require("./builtins");
|
|
9
|
+
/**
|
|
10
|
+
* Converts `target` into a plain JSON-compatible value. Objects get a `*type` field with
|
|
11
|
+
* their class name, so that they can be deserialized back into instances of that class.
|
|
12
|
+
*
|
|
13
|
+
* The `options` apply only to the top-level object, not to nested ones.
|
|
14
|
+
* The `target` itself is never changed.
|
|
15
|
+
*
|
|
16
|
+
* Throws for values that can't be deserialized back: functions and symbols (except as
|
|
17
|
+
* object properties, which are skipped), circular references, and built-in classes that
|
|
18
|
+
* are not supported (like `WeakMap` or `Promise`).
|
|
19
|
+
*/
|
|
20
|
+
function getSerializeValueWithClassName(target, options = {}) {
|
|
21
|
+
return new Serializer().serialize(target, options);
|
|
22
|
+
}
|
|
23
|
+
class Serializer {
|
|
24
|
+
constructor() {
|
|
25
|
+
/** The objects being serialized, from the top-level one down to the current one. */
|
|
26
|
+
this.ancestors = new Set();
|
|
27
|
+
/** Serializes a value. Arrow function, so that it can be passed around as a callback. */
|
|
28
|
+
this.serializeNested = (target) => this.serialize(target);
|
|
29
|
+
}
|
|
30
|
+
serialize(target, options = {}) {
|
|
31
|
+
if (typeof target !== 'object' || target === null) {
|
|
32
|
+
return _serializePrimitive(target);
|
|
33
|
+
}
|
|
34
|
+
if (this.ancestors.has(target)) {
|
|
35
|
+
throw new Error('Cannot serialize a circular reference.');
|
|
36
|
+
}
|
|
37
|
+
this.ancestors.add(target);
|
|
38
|
+
try {
|
|
39
|
+
return Array.isArray(target) && Object.getPrototypeOf(target) === Array.prototype
|
|
40
|
+
? this._serializeArray(target)
|
|
41
|
+
: this._serializeObject(target, options);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
this.ancestors.delete(target);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Saves holes of sparse arrays as `{"*type": "@hole"}`, as JSON would turn them into null. */
|
|
48
|
+
_serializeArray(arr) {
|
|
49
|
+
const result = new Array(arr.length);
|
|
50
|
+
for (let index = 0; index < arr.length; index++) {
|
|
51
|
+
result[index] = index in arr ? this.serialize(arr[index]) : { [constant_1.TYPE_FIELD]: constant_1.TYPE_HOLE };
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
_serializeObject(target, options) {
|
|
56
|
+
// An object with no prototype.
|
|
57
|
+
if (Object.getPrototypeOf(target) === null) {
|
|
58
|
+
const serializedObj = this._serializeProperties(target, options);
|
|
59
|
+
serializedObj[constant_1.TYPE_FIELD] = constant_1.TYPE_NULL_PROTOTYPE;
|
|
60
|
+
return serializedObj;
|
|
61
|
+
}
|
|
62
|
+
const constructor = _getConstructor(target);
|
|
63
|
+
// A plain object (or one whose prototype has no constructor).
|
|
64
|
+
if (constructor === undefined || constructor === Object) {
|
|
65
|
+
return this._serializeProperties(target, options);
|
|
66
|
+
}
|
|
67
|
+
// A built-in class.
|
|
68
|
+
const builtinType = (0, builtins_1.getBuiltinTypeByConstructor)(constructor);
|
|
69
|
+
if (builtinType) {
|
|
70
|
+
const serializedObj = builtinType.hasItemProperties
|
|
71
|
+
? {}
|
|
72
|
+
: this._serializeProperties(target, options, builtinType.keysSavedInValue);
|
|
73
|
+
serializedObj[constant_1.TYPE_FIELD] = constant_1.BUILTIN_PREFIX + builtinType.name;
|
|
74
|
+
serializedObj[constant_1.VALUE_FIELD] = builtinType.encode(target, this.serializeNested);
|
|
75
|
+
return serializedObj;
|
|
76
|
+
}
|
|
77
|
+
if ((0, general_1.isNativeFunction)(constructor)) {
|
|
78
|
+
throw new Error(`Cannot serialize a ${constructor.name}: this class is not supported.`);
|
|
79
|
+
}
|
|
80
|
+
// A user class.
|
|
81
|
+
const className = (0, general_1.getClassKey)(constructor);
|
|
82
|
+
if (className.startsWith(constant_1.BUILTIN_PREFIX)) {
|
|
83
|
+
throw new Error(`Cannot serialize class "${className}": class names can't start with "${constant_1.BUILTIN_PREFIX}".`);
|
|
84
|
+
}
|
|
85
|
+
// A user class that extends a built-in class also saves the built-in data, like the entries of a Map.
|
|
86
|
+
const inheritedType = (0, builtins_1.findInheritedBuiltinType)(target);
|
|
87
|
+
if (inheritedType && !inheritedType.fill) {
|
|
88
|
+
throw new Error(`Cannot serialize class "${className}": subclasses of ${inheritedType.name} are not supported.`);
|
|
89
|
+
}
|
|
90
|
+
const serializedObj = this._serializeProperties(target, options, inheritedType?.keysSavedInValue);
|
|
91
|
+
serializedObj[constant_1.TYPE_FIELD] = className;
|
|
92
|
+
if (inheritedType) {
|
|
93
|
+
serializedObj[constant_1.VALUE_FIELD] = inheritedType.encode(target, this.serializeNested);
|
|
94
|
+
}
|
|
95
|
+
return serializedObj;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Serializes the enumerable properties of `source`, skipping functions and symbols.
|
|
99
|
+
* Properties in `options.ignoreProperties` are skipped, and properties in
|
|
100
|
+
* `options.interceptProperties` are replaced by the result of their interceptor.
|
|
101
|
+
* The `keysSavedInValue` are skipped, as they are saved in `*value`.
|
|
102
|
+
*/
|
|
103
|
+
_serializeProperties(source, options, keysSavedInValue = []) {
|
|
104
|
+
const ignored = options.ignoreProperties;
|
|
105
|
+
const interceptors = options.interceptProperties;
|
|
106
|
+
const serializedObj = {};
|
|
107
|
+
const copy = (key, value) => {
|
|
108
|
+
if (typeof value !== 'function' && typeof value !== 'symbol') {
|
|
109
|
+
(0, general_1.setOwnProperty)(serializedObj, (0, general_1.escapeKey)(key), this.serialize(value));
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
// The items of an array subclass are saved apart, in `*value`.
|
|
113
|
+
const isArray = Array.isArray(source);
|
|
114
|
+
for (const key in source) {
|
|
115
|
+
if (ignored?.includes(key) || keysSavedInValue.includes(key) || (isArray && _isArrayIndex(key))) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const hasInterceptor = interceptors !== undefined && Object.prototype.hasOwnProperty.call(interceptors, key);
|
|
119
|
+
copy(key, hasInterceptor ? interceptors[key].call(source, source[key]) : source[key]);
|
|
120
|
+
}
|
|
121
|
+
// Interceptors may also add properties the object doesn't have.
|
|
122
|
+
if (interceptors) {
|
|
123
|
+
for (const key in interceptors) {
|
|
124
|
+
if (!(key in source) && !ignored?.includes(key)) {
|
|
125
|
+
copy(key, interceptors[key].call(source, undefined));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return serializedObj;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Returns the class of `target`, which must have a prototype. Returns undefined if the
|
|
134
|
+
* prototype has no constructor.
|
|
135
|
+
* Usually it's the constructor of the prototype, but when that is `Object`, it's the
|
|
136
|
+
* `constructor` property of the object itself, as in Big from big.js. That property is used
|
|
137
|
+
* only if it's a function, as a plain object may have a `constructor` key with any value.
|
|
138
|
+
*/
|
|
139
|
+
function _getConstructor(target) {
|
|
140
|
+
const constructor = Object.getPrototypeOf(target).constructor;
|
|
141
|
+
if (constructor === Object) {
|
|
142
|
+
const ownConstructor = target.constructor;
|
|
143
|
+
return typeof ownConstructor === 'function' ? ownConstructor : Object;
|
|
144
|
+
}
|
|
145
|
+
return constructor;
|
|
146
|
+
}
|
|
147
|
+
function _isArrayIndex(key) {
|
|
148
|
+
return /^(0|[1-9]\d*)$/.test(key);
|
|
149
|
+
}
|
|
150
|
+
/** Serializes primitives. Values JSON can't represent become objects with a special `*type`. */
|
|
151
|
+
function _serializePrimitive(target) {
|
|
152
|
+
switch (typeof target) {
|
|
153
|
+
case 'undefined':
|
|
154
|
+
return { [constant_1.TYPE_FIELD]: constant_1.TYPE_UNDEFINED };
|
|
155
|
+
case 'number':
|
|
156
|
+
if (!Number.isFinite(target) || Object.is(target, -0)) {
|
|
157
|
+
return { [constant_1.TYPE_FIELD]: constant_1.TYPE_NUMBER, [constant_1.VALUE_FIELD]: Object.is(target, -0) ? '-0' : target.toString() };
|
|
158
|
+
}
|
|
159
|
+
return target;
|
|
160
|
+
case 'bigint':
|
|
161
|
+
return { [constant_1.TYPE_FIELD]: constant_1.TYPE_BIGINT, [constant_1.VALUE_FIELD]: target.toString() };
|
|
162
|
+
case 'function':
|
|
163
|
+
case 'symbol':
|
|
164
|
+
throw new Error(`Cannot serialize a ${typeof target}.`);
|
|
165
|
+
default:
|
|
166
|
+
return target; // string, boolean or null.
|
|
167
|
+
}
|
|
168
|
+
}
|