@hg-ts/serialize 0.0.15

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 (96) hide show
  1. package/.eslintcache +1 -0
  2. package/.eslintrc.json +1 -0
  3. package/.mocharc.cjs +1 -0
  4. package/dist/additional-classes.d.ts +3 -0
  5. package/dist/additional-classes.d.ts.map +1 -0
  6. package/dist/additional-classes.js +9 -0
  7. package/dist/additional-classes.js.map +1 -0
  8. package/dist/deserialize.d.ts +3 -0
  9. package/dist/deserialize.d.ts.map +1 -0
  10. package/dist/deserialize.js +58 -0
  11. package/dist/deserialize.js.map +1 -0
  12. package/dist/exceptions/deserialize-unknown-prototype.exception.d.ts +5 -0
  13. package/dist/exceptions/deserialize-unknown-prototype.exception.d.ts.map +1 -0
  14. package/dist/exceptions/deserialize-unknown-prototype.exception.js +11 -0
  15. package/dist/exceptions/deserialize-unknown-prototype.exception.js.map +1 -0
  16. package/dist/exceptions/deserialize.exception.d.ts +6 -0
  17. package/dist/exceptions/deserialize.exception.d.ts.map +1 -0
  18. package/dist/exceptions/deserialize.exception.js +11 -0
  19. package/dist/exceptions/deserialize.exception.js.map +1 -0
  20. package/dist/exceptions/index.d.ts +4 -0
  21. package/dist/exceptions/index.d.ts.map +1 -0
  22. package/dist/exceptions/index.js +7 -0
  23. package/dist/exceptions/index.js.map +1 -0
  24. package/dist/exceptions/serialize.exception.d.ts +5 -0
  25. package/dist/exceptions/serialize.exception.d.ts.map +1 -0
  26. package/dist/exceptions/serialize.exception.js +11 -0
  27. package/dist/exceptions/serialize.exception.js.map +1 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +11 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/serialize.d.ts +4 -0
  33. package/dist/serialize.d.ts.map +1 -0
  34. package/dist/serialize.js +104 -0
  35. package/dist/serialize.js.map +1 -0
  36. package/dist/tests/serialization.test.d.ts +10 -0
  37. package/dist/tests/serialization.test.d.ts.map +1 -0
  38. package/dist/tests/serialization.test.js +64 -0
  39. package/dist/tests/serialization.test.js.map +1 -0
  40. package/dist/types.d.ts +49 -0
  41. package/dist/types.d.ts.map +1 -0
  42. package/dist/types.js +3 -0
  43. package/dist/types.js.map +1 -0
  44. package/dist-esm/additional-classes.d.ts +3 -0
  45. package/dist-esm/additional-classes.d.ts.map +1 -0
  46. package/dist-esm/additional-classes.js +5 -0
  47. package/dist-esm/additional-classes.js.map +1 -0
  48. package/dist-esm/deserialize.d.ts +3 -0
  49. package/dist-esm/deserialize.d.ts.map +1 -0
  50. package/dist-esm/deserialize.js +54 -0
  51. package/dist-esm/deserialize.js.map +1 -0
  52. package/dist-esm/exceptions/deserialize-unknown-prototype.exception.d.ts +5 -0
  53. package/dist-esm/exceptions/deserialize-unknown-prototype.exception.d.ts.map +1 -0
  54. package/dist-esm/exceptions/deserialize-unknown-prototype.exception.js +7 -0
  55. package/dist-esm/exceptions/deserialize-unknown-prototype.exception.js.map +1 -0
  56. package/dist-esm/exceptions/deserialize.exception.d.ts +6 -0
  57. package/dist-esm/exceptions/deserialize.exception.d.ts.map +1 -0
  58. package/dist-esm/exceptions/deserialize.exception.js +7 -0
  59. package/dist-esm/exceptions/deserialize.exception.js.map +1 -0
  60. package/dist-esm/exceptions/index.d.ts +4 -0
  61. package/dist-esm/exceptions/index.d.ts.map +1 -0
  62. package/dist-esm/exceptions/index.js +4 -0
  63. package/dist-esm/exceptions/index.js.map +1 -0
  64. package/dist-esm/exceptions/serialize.exception.d.ts +5 -0
  65. package/dist-esm/exceptions/serialize.exception.d.ts.map +1 -0
  66. package/dist-esm/exceptions/serialize.exception.js +7 -0
  67. package/dist-esm/exceptions/serialize.exception.js.map +1 -0
  68. package/dist-esm/index.d.ts +6 -0
  69. package/dist-esm/index.d.ts.map +1 -0
  70. package/dist-esm/index.js +6 -0
  71. package/dist-esm/index.js.map +1 -0
  72. package/dist-esm/serialize.d.ts +4 -0
  73. package/dist-esm/serialize.d.ts.map +1 -0
  74. package/dist-esm/serialize.js +99 -0
  75. package/dist-esm/serialize.js.map +1 -0
  76. package/dist-esm/tests/serialization.test.d.ts +10 -0
  77. package/dist-esm/tests/serialization.test.d.ts.map +1 -0
  78. package/dist-esm/tests/serialization.test.js +61 -0
  79. package/dist-esm/tests/serialization.test.js.map +1 -0
  80. package/dist-esm/types.d.ts +49 -0
  81. package/dist-esm/types.d.ts.map +1 -0
  82. package/dist-esm/types.js +2 -0
  83. package/dist-esm/types.js.map +1 -0
  84. package/package.json +39 -0
  85. package/src/additional-classes.ts +5 -0
  86. package/src/deserialize.ts +69 -0
  87. package/src/exceptions/deserialize-unknown-prototype.exception.ts +7 -0
  88. package/src/exceptions/deserialize.exception.ts +9 -0
  89. package/src/exceptions/index.ts +3 -0
  90. package/src/exceptions/serialize.exception.ts +7 -0
  91. package/src/index.ts +7 -0
  92. package/src/serialize.ts +114 -0
  93. package/src/tests/serialization.test.ts +47 -0
  94. package/src/types.ts +71 -0
  95. package/tsconfig.esm.json +9 -0
  96. package/tsconfig.json +10 -0
package/.eslintcache ADDED
@@ -0,0 +1 @@
1
+ [{"/Volumes/Work/ts/hg/framework/packages/serialize/src/deserialize.ts":"1","/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/deserialize-unknown-prototype.exception.ts":"2","/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/deserialize.exception.ts":"3","/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/index.ts":"4","/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/serialize.exception.ts":"5","/Volumes/Work/ts/hg/framework/packages/serialize/src/index.ts":"6","/Volumes/Work/ts/hg/framework/packages/serialize/src/serialize.ts":"7","/Volumes/Work/ts/hg/framework/packages/serialize/src/tests/serialization.test.ts":"8","/Volumes/Work/ts/hg/framework/packages/serialize/src/types.ts":"9","/Volumes/Work/ts/hg/framework/packages/serialize/src/additional-classes.ts":"10"},{"size":1736,"mtime":1672627753878,"results":"11","hashOfConfig":"12"},{"size":245,"mtime":1660867598754,"results":"13","hashOfConfig":"12"},{"size":276,"mtime":1660867598771,"results":"14","hashOfConfig":"12"},{"size":139,"mtime":1658007643599,"results":"15","hashOfConfig":"12"},{"size":204,"mtime":1660867598746,"results":"16","hashOfConfig":"12"},{"size":176,"mtime":1672627688191,"results":"17","hashOfConfig":"12"},{"size":2365,"mtime":1672620997161,"results":"18","hashOfConfig":"12"},{"size":977,"mtime":1672772281848,"results":"19","hashOfConfig":"12"},{"size":1180,"mtime":1657995855637,"results":"20","hashOfConfig":"12"},{"size":175,"mtime":1672627688197,"results":"21","hashOfConfig":"12"},{"filePath":"22","messages":"23","suppressedMessages":"24","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"12qr3tm",{"filePath":"25","messages":"26","suppressedMessages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"28","messages":"29","suppressedMessages":"30","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"31","messages":"32","suppressedMessages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","suppressedMessages":"36","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"37","messages":"38","suppressedMessages":"39","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","suppressedMessages":"42","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"43","messages":"44","suppressedMessages":"45","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"46","messages":"47","suppressedMessages":"48","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"49","messages":"50","suppressedMessages":"51","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Volumes/Work/ts/hg/framework/packages/serialize/src/deserialize.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/deserialize-unknown-prototype.exception.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/deserialize.exception.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/exceptions/serialize.exception.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/serialize.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/tests/serialization.test.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/serialize/src/types.ts",[],["52"],"/Volumes/Work/ts/hg/framework/packages/serialize/src/additional-classes.ts",[],[],{"ruleId":"53","severity":2,"message":"54","line":61,"column":2,"nodeType":"55","messageId":"56","endLine":71,"endColumn":20,"fix":"57","suppressions":"58"},"@typescript-eslint/sort-type-union-intersection-members","Union type SerializedValue members must be sorted.","TSUnionType","notSortedNamed",{"range":"59","text":"60"},["61"],[970,1178],"SerializedArray | SerializedBigint | SerializedBoolean | SerializedBuffer | SerializedDate | SerializedMap | SerializedNull | SerializedNumber | SerializedObject | SerializedSet | SerializedString",{"kind":"62","justification":"63"},"directive",""]
package/.eslintrc.json ADDED
@@ -0,0 +1 @@
1
+ { "extends": "@hg-ts-config/eslint-config" }
package/.mocharc.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('@hg-ts/tests/config/common.cjs');
@@ -0,0 +1,3 @@
1
+ export declare const classes: Map<string, Class<unknown, unknown[]>>;
2
+ export declare function addAdditionalClass(classToTransform: Class): void;
3
+ //# sourceMappingURL=additional-classes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"additional-classes.d.ts","sourceRoot":"","sources":["../src/additional-classes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,wCAA2B,CAAC;AAEhD,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,KAAK,GAAG,IAAI,CAEhE"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addAdditionalClass = exports.classes = void 0;
4
+ exports.classes = new Map();
5
+ function addAdditionalClass(classToTransform) {
6
+ exports.classes.set(classToTransform.name, classToTransform);
7
+ }
8
+ exports.addAdditionalClass = addAdditionalClass;
9
+ //# sourceMappingURL=additional-classes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"additional-classes.js","sourceRoot":"","sources":["../src/additional-classes.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;AAEhD,SAAgB,kBAAkB,CAAC,gBAAuB;IACzD,eAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;AACtD,CAAC;AAFD,gDAEC"}
@@ -0,0 +1,3 @@
1
+ import type { SerializedValue } from './types';
2
+ export declare function deserialize(value: SerializedValue): unknown;
3
+ //# sourceMappingURL=deserialize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.d.ts","sourceRoot":"","sources":["../src/deserialize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEX,eAAe,EACf,MAAM,SAAS,CAAC;AA8DjB,wBAAgB,WAAW,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAE3D"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deserialize = void 0;
4
+ const exceptions_1 = require("./exceptions");
5
+ const additional_classes_1 = require("./additional-classes");
6
+ function deserializeObject({ value, prototypeNameList }) {
7
+ let targetClass = Object;
8
+ const [preferredClassName = null] = prototypeNameList.filter(name => name !== Object.name);
9
+ if (preferredClassName) {
10
+ const foundCtor = additional_classes_1.classes.get(preferredClassName);
11
+ if (!foundCtor) {
12
+ throw new exceptions_1.DeserializeUnknownPrototypeException(preferredClassName);
13
+ }
14
+ targetClass = foundCtor;
15
+ }
16
+ const result = Object.create(targetClass.prototype);
17
+ return Object.keys(value)
18
+ .reduce((result, key) => {
19
+ result[key] = deserializeValue(value[key]);
20
+ return result;
21
+ }, result);
22
+ }
23
+ function deserializeValue(value) {
24
+ switch (value.type) {
25
+ case 'boolean':
26
+ case 'string':
27
+ case 'number':
28
+ return value.value;
29
+ case 'buffer':
30
+ return Buffer.from(value.value, 'hex');
31
+ case 'bigint':
32
+ return BigInt(value.value);
33
+ case 'null':
34
+ return null;
35
+ case 'date':
36
+ return new Date(value.value);
37
+ case 'array':
38
+ return value.value.map(item => deserializeValue(item));
39
+ case 'set':
40
+ return new Set(value.value.map(item => deserializeValue(item)));
41
+ case 'map':
42
+ return new Map(value.value.map(item => [
43
+ deserializeValue(item.key),
44
+ deserializeValue(item.value),
45
+ ]));
46
+ case 'object':
47
+ return deserializeObject(value);
48
+ default: {
49
+ const error = value;
50
+ throw new exceptions_1.DeserializeException(error);
51
+ }
52
+ }
53
+ }
54
+ function deserialize(value) {
55
+ return deserializeValue(value);
56
+ }
57
+ exports.deserialize = deserialize;
58
+ //# sourceMappingURL=deserialize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../src/deserialize.ts"],"names":[],"mappings":";;;AAAA,6CAA0F;AAK1F,6DAA+C;AAE/C,SAAS,iBAAiB,CACzB,EAAE,KAAK,EAAE,iBAAiB,EAAoB;IAE9C,IAAI,WAAW,GAAU,MAAM,CAAC;IAChC,MAAM,CAAC,kBAAkB,GAAG,IAAI,CAAC,GAAG,iBAAiB,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC;IAE3F,IAAI,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,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjE,KAAK,KAAK;YACT,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;gBAC1B,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC;aAC5B,CAAC,CAAC,CAAC;QACL,KAAK,QAAQ;YACZ,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAEjC,OAAO,CAAC,CAAC;YACR,MAAM,KAAK,GAAU,KAAK,CAAC;YAC3B,MAAM,IAAI,iCAAoB,CAAC,KAAK,CAAC,CAAC;SACtC;KACA;AACF,CAAC;AAED,SAAgB,WAAW,CAAC,KAAsB;IACjD,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAFD,kCAEC"}
@@ -0,0 +1,5 @@
1
+ import { BaseException } from '@hg-ts/exception';
2
+ export declare class DeserializeUnknownPrototypeException extends BaseException {
3
+ constructor(ctorName: string);
4
+ }
5
+ //# sourceMappingURL=deserialize-unknown-prototype.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize-unknown-prototype.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/deserialize-unknown-prototype.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,qBAAa,oCAAqC,SAAQ,aAAa;gBACnD,QAAQ,EAAE,MAAM;CAGnC"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeserializeUnknownPrototypeException = void 0;
4
+ const exception_1 = require("@hg-ts/exception");
5
+ class DeserializeUnknownPrototypeException extends exception_1.BaseException {
6
+ constructor(ctorName) {
7
+ super(`Deserialize error. Constructor with name ${ctorName} not found`);
8
+ }
9
+ }
10
+ exports.DeserializeUnknownPrototypeException = DeserializeUnknownPrototypeException;
11
+ //# sourceMappingURL=deserialize-unknown-prototype.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize-unknown-prototype.exception.js","sourceRoot":"","sources":["../../src/exceptions/deserialize-unknown-prototype.exception.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAEjD,MAAa,oCAAqC,SAAQ,yBAAa;IACtE,YAAmB,QAAgB;QAClC,KAAK,CAAC,4CAA4C,QAAQ,YAAY,CAAC,CAAC;IACzE,CAAC;CACD;AAJD,oFAIC"}
@@ -0,0 +1,6 @@
1
+ import { BaseException } from '@hg-ts/exception';
2
+ import type { SerializedValue } from '../types';
3
+ export declare class DeserializeException extends BaseException {
4
+ constructor(value: SerializedValue);
5
+ }
6
+ //# sourceMappingURL=deserialize.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/deserialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,qBAAa,oBAAqB,SAAQ,aAAa;gBACnC,KAAK,EAAE,eAAe;CAGzC"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeserializeException = void 0;
4
+ const exception_1 = require("@hg-ts/exception");
5
+ class DeserializeException extends exception_1.BaseException {
6
+ constructor(value) {
7
+ super(`Error for deserialize value: ${JSON.stringify(value)}`);
8
+ }
9
+ }
10
+ exports.DeserializeException = DeserializeException;
11
+ //# sourceMappingURL=deserialize.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.exception.js","sourceRoot":"","sources":["../../src/exceptions/deserialize.exception.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAIjD,MAAa,oBAAqB,SAAQ,yBAAa;IACtD,YAAmB,KAAsB;QACxC,KAAK,CAAC,gCAAgC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC;CACD;AAJD,oDAIC"}
@@ -0,0 +1,4 @@
1
+ export * from './deserialize-unknown-prototype.exception';
2
+ export * from './deserialize.exception';
3
+ export * from './serialize.exception';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./deserialize-unknown-prototype.exception"), exports);
5
+ tslib_1.__exportStar(require("./deserialize.exception"), exports);
6
+ tslib_1.__exportStar(require("./serialize.exception"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exceptions/index.ts"],"names":[],"mappings":";;;AAAA,oFAA0D;AAC1D,kEAAwC;AACxC,gEAAsC"}
@@ -0,0 +1,5 @@
1
+ import { BaseException } from '@hg-ts/exception';
2
+ export declare class SerializeException extends BaseException {
3
+ constructor(type: string);
4
+ }
5
+ //# sourceMappingURL=serialize.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialize.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/serialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,qBAAa,kBAAmB,SAAQ,aAAa;gBACjC,IAAI,EAAE,MAAM;CAG/B"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SerializeException = void 0;
4
+ const exception_1 = require("@hg-ts/exception");
5
+ class SerializeException extends exception_1.BaseException {
6
+ constructor(type) {
7
+ super(`Unsupported type for serialization: ${type}`);
8
+ }
9
+ }
10
+ exports.SerializeException = SerializeException;
11
+ //# sourceMappingURL=serialize.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialize.exception.js","sourceRoot":"","sources":["../../src/exceptions/serialize.exception.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAEjD,MAAa,kBAAmB,SAAQ,yBAAa;IACpD,YAAmB,IAAY;QAC9B,KAAK,CAAC,uCAAuC,IAAI,EAAE,CAAC,CAAC;IACtD,CAAC;CACD;AAJD,gDAIC"}
@@ -0,0 +1,6 @@
1
+ export * from './types';
2
+ export { addAdditionalClass } from './additional-classes';
3
+ export * from './serialize';
4
+ export * from './deserialize';
5
+ export * from './exceptions';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAE9B,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addAdditionalClass = void 0;
4
+ const tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./types"), exports);
6
+ var additional_classes_1 = require("./additional-classes");
7
+ Object.defineProperty(exports, "addAdditionalClass", { enumerable: true, get: function () { return additional_classes_1.addAdditionalClass; } });
8
+ tslib_1.__exportStar(require("./serialize"), exports);
9
+ tslib_1.__exportStar(require("./deserialize"), exports);
10
+ tslib_1.__exportStar(require("./exceptions"), exports);
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,kDAAwB;AAExB,2DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,sDAA4B;AAC5B,wDAA8B;AAE9B,uDAA6B"}
@@ -0,0 +1,4 @@
1
+ import type { SerializedValue } from './types';
2
+ export declare function getPrototypeListNames(value: Record<string | symbol, unknown>): string[];
3
+ export declare function serialize(value: unknown): SerializedValue;
4
+ //# sourceMappingURL=serialize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,eAAe,EACf,MAAM,SAAS,CAAC;AAGjB,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,EAAE,CAWvF;AA6FD,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CAEzD"}
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serialize = exports.getPrototypeListNames = void 0;
4
+ const exceptions_1 = require("./exceptions");
5
+ function getPrototypeListNames(value) {
6
+ const result = [];
7
+ let nextPrototype = Object.getPrototypeOf(value);
8
+ while (nextPrototype !== null) {
9
+ result.push(nextPrototype.constructor.name);
10
+ nextPrototype = Object.getPrototypeOf(nextPrototype);
11
+ }
12
+ return result;
13
+ }
14
+ exports.getPrototypeListNames = getPrototypeListNames;
15
+ function serializeArray(value) {
16
+ return {
17
+ type: 'array',
18
+ value: value.map(item => serializeValue(item)),
19
+ };
20
+ }
21
+ function serializeRecord(value) {
22
+ const fields = Object.keys(value)
23
+ .reduce((result, key) => {
24
+ if (typeof key === 'symbol') {
25
+ throw new exceptions_1.SerializeException('symbol key');
26
+ }
27
+ result[key] = serializeValue(value[key]);
28
+ return result;
29
+ }, {});
30
+ return {
31
+ type: 'object',
32
+ prototypeNameList: getPrototypeListNames(value),
33
+ value: fields,
34
+ };
35
+ }
36
+ function serializeObject(value) {
37
+ if (value === null) {
38
+ return { type: 'null' };
39
+ }
40
+ if (value instanceof Date) {
41
+ return {
42
+ type: 'date',
43
+ value: value.getTime(),
44
+ };
45
+ }
46
+ if (value instanceof Buffer) {
47
+ return {
48
+ type: 'buffer',
49
+ value: value.toString('hex'),
50
+ };
51
+ }
52
+ if (value instanceof Map) {
53
+ return {
54
+ type: 'map',
55
+ value: [...value.entries()].map(([key, value]) => ({
56
+ key: serializeValue(key),
57
+ value: serializeValue(value),
58
+ })),
59
+ };
60
+ }
61
+ if (value instanceof Set) {
62
+ return {
63
+ type: 'set',
64
+ value: [...value.values()].map(item => serializeValue(item)),
65
+ };
66
+ }
67
+ if (Array.isArray(value)) {
68
+ return serializeArray(value);
69
+ }
70
+ return serializeRecord(value);
71
+ }
72
+ function serializeValue(value) {
73
+ switch (typeof value) {
74
+ case 'boolean':
75
+ return {
76
+ value,
77
+ type: 'boolean',
78
+ };
79
+ case 'bigint':
80
+ return {
81
+ type: 'bigint',
82
+ value: String(value),
83
+ };
84
+ case 'string':
85
+ return {
86
+ value,
87
+ type: 'string',
88
+ };
89
+ case 'number':
90
+ return {
91
+ value,
92
+ type: 'number',
93
+ };
94
+ case 'object':
95
+ return serializeObject(value);
96
+ default:
97
+ throw new exceptions_1.SerializeException(typeof value);
98
+ }
99
+ }
100
+ function serialize(value) {
101
+ return serializeValue(value);
102
+ }
103
+ exports.serialize = serialize;
104
+ //# sourceMappingURL=serialize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialize.js","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":";;;AAKA,6CAAkD;AAElD,SAAgB,qBAAqB,CAAC,KAAuC;IAC5E,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAEjD,OAAO,aAAa,KAAK,IAA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C;YACC,MAAM,IAAI,+BAAkB,CAAC,OAAO,KAAK,CAAC,CAAC;KAC3C;AACF,CAAC;AAED,SAAgB,SAAS,CAAC,KAAc;IACvC,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAFD,8BAEC"}
@@ -0,0 +1,10 @@
1
+ import '@hg-ts/types';
2
+ import { TestSuite } from '@hg-ts/tests';
3
+ export declare class SerializationTestSuite extends TestSuite {
4
+ serializeNull(): Promise<void>;
5
+ serializeNumber(): Promise<void>;
6
+ serializeBigint(): Promise<void>;
7
+ serializeBuffer(): Promise<void>;
8
+ protected test(expected: unknown): void;
9
+ }
10
+ //# sourceMappingURL=serialization.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.test.d.ts","sourceRoot":"","sources":["../../src/tests/serialization.test.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,EAEN,SAAS,EAGT,MAAM,cAAc,CAAC;AAItB,qBACa,sBAAuB,SAAQ,SAAS;IAEvC,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAShC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAUhC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7C,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI;CAKvC"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SerializationTestSuite = void 0;
4
+ const tslib_1 = require("tslib");
5
+ require("@hg-ts/types");
6
+ const tests_1 = require("@hg-ts/tests");
7
+ const serialize_1 = require("../serialize");
8
+ const deserialize_1 = require("../deserialize");
9
+ let SerializationTestSuite = class SerializationTestSuite extends tests_1.TestSuite {
10
+ async serializeNull() {
11
+ this.test(null);
12
+ }
13
+ async serializeNumber() {
14
+ this.test(123);
15
+ this.test(12.35);
16
+ this.test(0);
17
+ this.test(-12.35);
18
+ this.test(-123);
19
+ }
20
+ async serializeBigint() {
21
+ this.test(1235n * 1000000000000000000000000n);
22
+ this.test(1235n);
23
+ this.test(123n);
24
+ this.test(0n);
25
+ this.test(-123n);
26
+ this.test(-1235n * 1000000000000000000000000n);
27
+ }
28
+ async serializeBuffer() {
29
+ this.test(Buffer.from('sdfsdgdfgdfg'));
30
+ }
31
+ test(expected) {
32
+ const value = (0, deserialize_1.deserialize)((0, serialize_1.serialize)(expected));
33
+ (0, tests_1.expect)(value).toEqual(expected);
34
+ }
35
+ };
36
+ tslib_1.__decorate([
37
+ (0, tests_1.Test)(),
38
+ tslib_1.__metadata("design:type", Function),
39
+ tslib_1.__metadata("design:paramtypes", []),
40
+ tslib_1.__metadata("design:returntype", Promise)
41
+ ], SerializationTestSuite.prototype, "serializeNull", null);
42
+ tslib_1.__decorate([
43
+ (0, tests_1.Test)(),
44
+ tslib_1.__metadata("design:type", Function),
45
+ tslib_1.__metadata("design:paramtypes", []),
46
+ tslib_1.__metadata("design:returntype", Promise)
47
+ ], SerializationTestSuite.prototype, "serializeNumber", null);
48
+ tslib_1.__decorate([
49
+ (0, tests_1.Test)(),
50
+ tslib_1.__metadata("design:type", Function),
51
+ tslib_1.__metadata("design:paramtypes", []),
52
+ tslib_1.__metadata("design:returntype", Promise)
53
+ ], SerializationTestSuite.prototype, "serializeBigint", null);
54
+ tslib_1.__decorate([
55
+ (0, tests_1.Test)(),
56
+ tslib_1.__metadata("design:type", Function),
57
+ tslib_1.__metadata("design:paramtypes", []),
58
+ tslib_1.__metadata("design:returntype", Promise)
59
+ ], SerializationTestSuite.prototype, "serializeBuffer", null);
60
+ SerializationTestSuite = tslib_1.__decorate([
61
+ (0, tests_1.Describe)()
62
+ ], SerializationTestSuite);
63
+ exports.SerializationTestSuite = SerializationTestSuite;
64
+ //# sourceMappingURL=serialization.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.test.js","sourceRoot":"","sources":["../../src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YAAI,GAAE;;;;2DAGN;AAGD;IADC,IAAA,YAAI,GAAE;;;;6DAON;AAGD;IADC,IAAA,YAAI,GAAE;;;;6DAQN;AAGD;IADC,IAAA,YAAI,GAAE;;;;6DAGN;AA5BW,sBAAsB;IADlC,IAAA,gBAAQ,GAAE;GACE,sBAAsB,CAmClC;AAnCY,wDAAsB"}
@@ -0,0 +1,49 @@
1
+ export declare type SerializedNumber = {
2
+ type: 'number';
3
+ value: number;
4
+ };
5
+ export declare type SerializedString = {
6
+ type: 'string';
7
+ value: string;
8
+ };
9
+ export declare type SerializedBoolean = {
10
+ type: 'boolean';
11
+ value: boolean;
12
+ };
13
+ export declare type SerializedBigint = {
14
+ type: 'bigint';
15
+ value: string;
16
+ };
17
+ export declare type SerializedDate = {
18
+ type: 'date';
19
+ value: number;
20
+ };
21
+ export declare type SerializedArray = {
22
+ type: 'array';
23
+ value: SerializedValue[];
24
+ };
25
+ export declare type SerializedBuffer = {
26
+ type: 'buffer';
27
+ value: string;
28
+ };
29
+ export declare type SerializedObject = {
30
+ type: 'object';
31
+ prototypeNameList: string[];
32
+ value: Record<string, SerializedValue>;
33
+ };
34
+ export declare type SerializedMap = {
35
+ type: 'map';
36
+ value: {
37
+ key: SerializedValue;
38
+ value: SerializedValue;
39
+ }[];
40
+ };
41
+ export declare type SerializedSet = {
42
+ type: 'set';
43
+ value: SerializedValue[];
44
+ };
45
+ export declare type SerializedNull = {
46
+ type: 'null';
47
+ };
48
+ export declare type SerializedValue = SerializedArray | SerializedBigint | SerializedBoolean | SerializedDate | SerializedMap | SerializedNull | SerializedBuffer | SerializedNumber | SerializedObject | SerializedSet | SerializedString;
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;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gBAAgB,GAChB,aAAa,GACb,gBAAgB,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export declare const classes: Map<string, Class<unknown, unknown[]>>;
2
+ export declare function addAdditionalClass(classToTransform: Class): void;
3
+ //# sourceMappingURL=additional-classes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"additional-classes.d.ts","sourceRoot":"","sources":["../src/additional-classes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,wCAA2B,CAAC;AAEhD,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,KAAK,GAAG,IAAI,CAEhE"}
@@ -0,0 +1,5 @@
1
+ export const classes = new Map();
2
+ export function addAdditionalClass(classToTransform) {
3
+ classes.set(classToTransform.name, classToTransform);
4
+ }
5
+ //# sourceMappingURL=additional-classes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"additional-classes.js","sourceRoot":"","sources":["../src/additional-classes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;AAEhD,MAAM,UAAU,kBAAkB,CAAC,gBAAuB;IACzD,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { SerializedValue } from './types';
2
+ export declare function deserialize(value: SerializedValue): unknown;
3
+ //# sourceMappingURL=deserialize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.d.ts","sourceRoot":"","sources":["../src/deserialize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEX,eAAe,EACf,MAAM,SAAS,CAAC;AA8DjB,wBAAgB,WAAW,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAE3D"}
@@ -0,0 +1,54 @@
1
+ import { DeserializeException, DeserializeUnknownPrototypeException } from './exceptions';
2
+ import { classes } from './additional-classes';
3
+ function deserializeObject({ value, prototypeNameList }) {
4
+ let targetClass = Object;
5
+ const [preferredClassName = null] = prototypeNameList.filter(name => name !== Object.name);
6
+ if (preferredClassName) {
7
+ const foundCtor = classes.get(preferredClassName);
8
+ if (!foundCtor) {
9
+ throw new DeserializeUnknownPrototypeException(preferredClassName);
10
+ }
11
+ targetClass = foundCtor;
12
+ }
13
+ const result = Object.create(targetClass.prototype);
14
+ return Object.keys(value)
15
+ .reduce((result, key) => {
16
+ result[key] = deserializeValue(value[key]);
17
+ return result;
18
+ }, result);
19
+ }
20
+ function deserializeValue(value) {
21
+ switch (value.type) {
22
+ case 'boolean':
23
+ case 'string':
24
+ case 'number':
25
+ return value.value;
26
+ case 'buffer':
27
+ return Buffer.from(value.value, 'hex');
28
+ case 'bigint':
29
+ return BigInt(value.value);
30
+ case 'null':
31
+ return null;
32
+ case 'date':
33
+ return new Date(value.value);
34
+ case 'array':
35
+ return value.value.map(item => deserializeValue(item));
36
+ case 'set':
37
+ return new Set(value.value.map(item => deserializeValue(item)));
38
+ case 'map':
39
+ return new Map(value.value.map(item => [
40
+ deserializeValue(item.key),
41
+ deserializeValue(item.value),
42
+ ]));
43
+ case 'object':
44
+ return deserializeObject(value);
45
+ default: {
46
+ const error = value;
47
+ throw new DeserializeException(error);
48
+ }
49
+ }
50
+ }
51
+ export function deserialize(value) {
52
+ return deserializeValue(value);
53
+ }
54
+ //# sourceMappingURL=deserialize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../src/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,oCAAoC,EAAE,MAAM,cAAc,CAAC;AAK1F,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;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,KAAK,CAAC,KAAK,CAAC,CAAC;QAC9B,KAAK,OAAO;YACX,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;QACxD,KAAK,KAAK;YACT,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjE,KAAK,KAAK;YACT,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;gBAC1B,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC;aAC5B,CAAC,CAAC,CAAC;QACL,KAAK,QAAQ;YACZ,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAEjC,OAAO,CAAC,CAAC;YACR,MAAM,KAAK,GAAU,KAAK,CAAC;YAC3B,MAAM,IAAI,oBAAoB,CAAC,KAAK,CAAC,CAAC;SACtC;KACA;AACF,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAsB;IACjD,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { BaseException } from '@hg-ts/exception';
2
+ export declare class DeserializeUnknownPrototypeException extends BaseException {
3
+ constructor(ctorName: string);
4
+ }
5
+ //# sourceMappingURL=deserialize-unknown-prototype.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deserialize-unknown-prototype.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/deserialize-unknown-prototype.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,qBAAa,oCAAqC,SAAQ,aAAa;gBACnD,QAAQ,EAAE,MAAM;CAGnC"}