seroval 0.11.5 → 0.12.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.
Files changed (122) hide show
  1. package/README.md +29 -16
  2. package/dist/cjs/development/index.cjs +3012 -4790
  3. package/dist/cjs/development/index.cjs.map +4 -4
  4. package/dist/cjs/production/index.cjs +3 -3
  5. package/dist/esm/development/index.mjs +3010 -4790
  6. package/dist/esm/development/index.mjs.map +4 -4
  7. package/dist/esm/production/index.mjs +3 -3
  8. package/dist/types/core/Serializer.d.ts +2 -0
  9. package/dist/types/core/Serializer.d.ts.map +1 -0
  10. package/dist/types/core/UnsupportedTypeError.d.ts +1 -0
  11. package/dist/types/core/UnsupportedTypeError.d.ts.map +1 -0
  12. package/dist/types/core/assert.d.ts +1 -0
  13. package/dist/types/core/assert.d.ts.map +1 -0
  14. package/dist/types/core/base/async.d.ts +30 -0
  15. package/dist/types/core/base/async.d.ts.map +1 -0
  16. package/dist/types/core/base/stream.d.ts +40 -0
  17. package/dist/types/core/base/stream.d.ts.map +1 -0
  18. package/dist/types/core/base/sync.d.ts +32 -0
  19. package/dist/types/core/base/sync.d.ts.map +1 -0
  20. package/dist/types/core/base-primitives.d.ts +3 -1
  21. package/dist/types/core/base-primitives.d.ts.map +1 -0
  22. package/dist/types/core/compat.d.ts +1 -0
  23. package/dist/types/core/compat.d.ts.map +1 -0
  24. package/dist/types/core/constants.d.ts +19 -4
  25. package/dist/types/core/constants.d.ts.map +1 -0
  26. package/dist/types/core/constructors.d.ts +1 -0
  27. package/dist/types/core/constructors.d.ts.map +1 -0
  28. package/dist/types/core/cross/async.d.ts +8 -3
  29. package/dist/types/core/cross/async.d.ts.map +1 -0
  30. package/dist/types/core/cross/cross-parser.d.ts +8 -0
  31. package/dist/types/core/cross/cross-parser.d.ts.map +1 -0
  32. package/dist/types/core/cross/index.d.ts +12 -5
  33. package/dist/types/core/cross/index.d.ts.map +1 -0
  34. package/dist/types/core/cross/serialize.d.ts +23 -5
  35. package/dist/types/core/cross/serialize.d.ts.map +1 -0
  36. package/dist/types/core/cross/stream.d.ts +8 -3
  37. package/dist/types/core/cross/stream.d.ts.map +1 -0
  38. package/dist/types/core/cross/sync.d.ts +8 -3
  39. package/dist/types/core/cross/sync.d.ts.map +1 -0
  40. package/dist/types/core/get-identifier.d.ts +1 -0
  41. package/dist/types/core/get-identifier.d.ts.map +1 -0
  42. package/dist/types/core/keys.d.ts +1 -0
  43. package/dist/types/core/keys.d.ts.map +1 -0
  44. package/dist/types/core/literals.d.ts +1 -0
  45. package/dist/types/core/literals.d.ts.map +1 -0
  46. package/dist/types/core/parser-context.d.ts +23 -0
  47. package/dist/types/core/parser-context.d.ts.map +1 -0
  48. package/dist/types/core/plugin.d.ts +26 -0
  49. package/dist/types/core/plugin.d.ts.map +1 -0
  50. package/dist/types/core/promise-to-result.d.ts +1 -0
  51. package/dist/types/core/promise-to-result.d.ts.map +1 -0
  52. package/dist/types/core/reference.d.ts +1 -0
  53. package/dist/types/core/reference.d.ts.map +1 -0
  54. package/dist/types/core/serializer-context.old.d.ts +165 -0
  55. package/dist/types/core/serializer-context.old.d.ts.map +1 -0
  56. package/dist/types/core/shared.d.ts +4 -7
  57. package/dist/types/core/shared.d.ts.map +1 -0
  58. package/dist/types/core/string.d.ts +1 -0
  59. package/dist/types/core/string.d.ts.map +1 -0
  60. package/dist/types/core/tree/async.d.ts +8 -3
  61. package/dist/types/core/tree/async.d.ts.map +1 -0
  62. package/dist/types/core/tree/deserialize.d.ts +49 -2
  63. package/dist/types/core/tree/deserialize.d.ts.map +1 -0
  64. package/dist/types/core/tree/index.d.ts +10 -7
  65. package/dist/types/core/tree/index.d.ts.map +1 -0
  66. package/dist/types/core/tree/serialize.d.ts +37 -4
  67. package/dist/types/core/tree/serialize.d.ts.map +1 -0
  68. package/dist/types/core/tree/sync.d.ts +8 -3
  69. package/dist/types/core/tree/sync.d.ts.map +1 -0
  70. package/dist/types/core/types.d.ts +10 -3
  71. package/dist/types/core/types.d.ts.map +1 -0
  72. package/dist/types/core/web-api.d.ts +1 -0
  73. package/dist/types/core/web-api.d.ts.map +1 -0
  74. package/dist/types/index.d.ts +3 -0
  75. package/dist/types/index.d.ts.map +1 -0
  76. package/dist/types/types.d.ts +1 -0
  77. package/dist/types/types.d.ts.map +1 -0
  78. package/package.json +2 -2
  79. package/src/core/Serializer.ts +2 -0
  80. package/src/core/base/async.ts +804 -0
  81. package/src/core/base/stream.ts +496 -0
  82. package/src/core/base/sync.ts +675 -0
  83. package/src/core/base-primitives.ts +22 -0
  84. package/src/core/constants.ts +61 -45
  85. package/src/core/cross/async.ts +6 -743
  86. package/src/core/cross/cross-parser.ts +9 -0
  87. package/src/core/cross/index.ts +62 -84
  88. package/src/core/cross/serialize.ts +71 -862
  89. package/src/core/cross/stream.ts +6 -868
  90. package/src/core/cross/sync.ts +6 -623
  91. package/src/core/is-serializable.new.ts +208 -0
  92. package/src/core/keys.ts +1 -1
  93. package/src/core/parser-context.ts +131 -0
  94. package/src/core/plugin.ts +55 -0
  95. package/src/core/serializer-context.new.ts +1168 -0
  96. package/src/core/serializer-context.old.ts +1126 -0
  97. package/src/core/shared.ts +46 -124
  98. package/src/core/tree/async.ts +6 -742
  99. package/src/core/tree/deserialize.ts +423 -420
  100. package/src/core/tree/index.ts +49 -91
  101. package/src/core/tree/parser.ts +45 -0
  102. package/src/core/tree/serialize.ts +122 -828
  103. package/src/core/tree/sync.ts +6 -623
  104. package/src/core/types.ts +13 -2
  105. package/src/example.ts +28 -0
  106. package/src/index.ts +3 -0
  107. package/dist/types/core/assignments.d.ts +0 -33
  108. package/dist/types/core/context.d.ts +0 -9
  109. package/dist/types/core/cross/context.d.ts +0 -37
  110. package/dist/types/core/cross/primitives.d.ts +0 -9
  111. package/dist/types/core/cross/web-api.d.ts +0 -4
  112. package/dist/types/core/tree/context.d.ts +0 -44
  113. package/dist/types/core/tree/primitives.d.ts +0 -9
  114. package/dist/types/core/tree/web-api.d.ts +0 -4
  115. package/src/core/assignments.ts +0 -159
  116. package/src/core/context.ts +0 -15
  117. package/src/core/cross/context.ts +0 -102
  118. package/src/core/cross/primitives.ts +0 -128
  119. package/src/core/cross/web-api.ts +0 -50
  120. package/src/core/tree/context.ts +0 -125
  121. package/src/core/tree/primitives.ts +0 -123
  122. package/src/core/tree/web-api.ts +0 -50
@@ -1,143 +1,33 @@
1
- import { BIGINT_FLAG, Feature } from './compat';
2
1
  import type {
3
2
  ErrorValue,
4
3
  } from '../types';
5
- import { SerovalObjectFlags } from './constants';
6
- import type { BaseParserContext } from './context';
4
+ import { Feature } from './compat';
5
+ import {
6
+ ERROR_CONSTRUCTOR_STRING,
7
+ ErrorConstructorTag,
8
+ SerovalObjectFlags,
9
+ } from './constants';
7
10
 
8
- export function getErrorConstructorName(error: ErrorValue): string {
11
+ export function getErrorConstructor(error: ErrorValue): ErrorConstructorTag {
9
12
  if (error instanceof EvalError) {
10
- return 'EvalError';
13
+ return ErrorConstructorTag.EvalError;
11
14
  }
12
15
  if (error instanceof RangeError) {
13
- return 'RangeError';
16
+ return ErrorConstructorTag.RangeError;
14
17
  }
15
18
  if (error instanceof ReferenceError) {
16
- return 'ReferenceError';
19
+ return ErrorConstructorTag.ReferenceError;
17
20
  }
18
21
  if (error instanceof SyntaxError) {
19
- return 'SyntaxError';
22
+ return ErrorConstructorTag.SyntaxError;
20
23
  }
21
24
  if (error instanceof TypeError) {
22
- return 'TypeError';
25
+ return ErrorConstructorTag.TypeError;
23
26
  }
24
27
  if (error instanceof URIError) {
25
- return 'URIError';
28
+ return ErrorConstructorTag.URIError;
26
29
  }
27
- return 'Error';
28
- }
29
-
30
- type ErrorConstructors =
31
- | ErrorConstructor
32
- | EvalErrorConstructor
33
- | RangeErrorConstructor
34
- | ReferenceErrorConstructor
35
- | SyntaxErrorConstructor
36
- | TypeErrorConstructor
37
- | URIErrorConstructor;
38
-
39
- export function getErrorConstructor(errorName: string): ErrorConstructors {
40
- switch (errorName) {
41
- case 'Error': return Error;
42
- case 'EvalError': return EvalError;
43
- case 'RangeError': return RangeError;
44
- case 'ReferenceError': return ReferenceError;
45
- case 'SyntaxError': return SyntaxError;
46
- case 'TypeError': return TypeError;
47
- case 'URIError': return URIError;
48
- default:
49
- throw new Error(`Unknown Error constructor "${errorName}"`);
50
- }
51
- }
52
-
53
- export function getErrorOptions(
54
- ctx: BaseParserContext,
55
- error: Error,
56
- ): Record<string, unknown> | undefined {
57
- let options: Record<string, unknown> | undefined;
58
- const constructor = getErrorConstructorName(error);
59
- // Name has been modified
60
- if (error.name !== constructor) {
61
- options = { name: error.name };
62
- } else if (error.constructor.name !== constructor) {
63
- // Otherwise, name is overriden because
64
- // the Error class is extended
65
- options = { name: error.constructor.name };
66
- }
67
- const names = Object.getOwnPropertyNames(error);
68
- for (const name of names) {
69
- if (name !== 'name' && name !== 'message') {
70
- if (name === 'stack') {
71
- if (ctx.features & Feature.ErrorPrototypeStack) {
72
- options = options || {};
73
- options[name] = error[name as keyof Error];
74
- }
75
- } else {
76
- options = options || {};
77
- options[name] = error[name as keyof Error];
78
- }
79
- }
80
- }
81
- return options;
82
- }
83
-
84
- export function isIterable(
85
- ctx: BaseParserContext,
86
- value: unknown,
87
- ): value is Iterable<unknown> {
88
- if (!value || typeof value !== 'object') {
89
- return false;
90
- }
91
- if (Array.isArray(value)) {
92
- return false;
93
- }
94
- const currentClass = value.constructor;
95
- if (ctx.features & Feature.TypedArray) {
96
- switch (currentClass) {
97
- case Int8Array:
98
- case Int16Array:
99
- case Int32Array:
100
- case Uint8Array:
101
- case Uint16Array:
102
- case Uint32Array:
103
- case Uint8ClampedArray:
104
- case Float32Array:
105
- case Float64Array:
106
- return false;
107
- default:
108
- break;
109
- }
110
- }
111
- // BigInt Typed Arrays
112
- if ((ctx.features & BIGINT_FLAG) === BIGINT_FLAG) {
113
- switch (currentClass) {
114
- case BigInt64Array:
115
- case BigUint64Array:
116
- return false;
117
- default:
118
- break;
119
- }
120
- }
121
- // ES Collection
122
- if (ctx.features & Feature.Map && currentClass === Map) {
123
- return false;
124
- }
125
- if (ctx.features & Feature.Set && currentClass === Set) {
126
- return false;
127
- }
128
- if (ctx.features & Feature.WebAPI) {
129
- switch (currentClass) {
130
- case Headers:
131
- case File:
132
- return false;
133
- default:
134
- break;
135
- }
136
- }
137
- if (ctx.features & Feature.Symbol) {
138
- return Symbol.iterator in value;
139
- }
140
- return false;
30
+ return ErrorConstructorTag.Error;
141
31
  }
142
32
 
143
33
  type TypedArrayConstructor =
@@ -195,3 +85,35 @@ export function getObjectFlag(obj: unknown): SerovalObjectFlags {
195
85
  }
196
86
  return SerovalObjectFlags.NonExtensible;
197
87
  }
88
+
89
+ export function getErrorOptions(
90
+ error: Error,
91
+ features: number,
92
+ ): Record<string, unknown> | undefined {
93
+ let options: Record<string, unknown> | undefined;
94
+ const constructor = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
95
+ // Name has been modified
96
+ if (error.name !== constructor) {
97
+ options = { name: error.name };
98
+ } else if (error.constructor.name !== constructor) {
99
+ // Otherwise, name is overriden because
100
+ // the Error class is extended
101
+ options = { name: error.constructor.name };
102
+ }
103
+ const names = Object.getOwnPropertyNames(error);
104
+ for (let i = 0, len = names.length, name: string; i < len; i++) {
105
+ name = names[i];
106
+ if (name !== 'name' && name !== 'message') {
107
+ if (name === 'stack') {
108
+ if (features & Feature.ErrorPrototypeStack) {
109
+ options = options || {};
110
+ options[name] = error[name as keyof Error];
111
+ }
112
+ } else {
113
+ options = options || {};
114
+ options[name] = error[name as keyof Error];
115
+ }
116
+ }
117
+ }
118
+ return options;
119
+ }