seroval 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/README.md +4 -287
  2. package/dist/cjs/development/index.cjs +3500 -1135
  3. package/dist/cjs/development/index.cjs.map +4 -4
  4. package/dist/cjs/production/index.cjs +12 -3
  5. package/dist/esm/development/index.mjs +3502 -1135
  6. package/dist/esm/development/index.mjs.map +4 -4
  7. package/dist/esm/production/index.mjs +12 -3
  8. package/dist/types/core/Serializer.d.ts +14 -0
  9. package/dist/types/{tree → core}/UnsupportedTypeError.d.ts +4 -3
  10. package/dist/types/{assert.d.ts → core/assert.d.ts} +1 -1
  11. package/dist/types/core/assignments.d.ts +33 -0
  12. package/dist/types/core/base-primitives.d.ts +12 -0
  13. package/dist/types/{compat.d.ts → core/compat.d.ts} +22 -22
  14. package/dist/types/core/constants.d.ts +89 -0
  15. package/dist/types/core/context.d.ts +9 -0
  16. package/dist/types/core/cross/async.d.ts +3 -0
  17. package/dist/types/core/cross/context.d.ts +30 -0
  18. package/dist/types/core/cross/index.d.ts +7 -0
  19. package/dist/types/core/cross/primitives.d.ts +9 -0
  20. package/dist/types/core/cross/serialize.d.ts +5 -0
  21. package/dist/types/core/cross/stream.d.ts +3 -0
  22. package/dist/types/core/cross/sync.d.ts +3 -0
  23. package/dist/types/core/cross/web-api.d.ts +4 -0
  24. package/dist/types/{get-identifier.d.ts → core/get-identifier.d.ts} +1 -1
  25. package/dist/types/core/keys.d.ts +12 -0
  26. package/dist/types/{tree/constants.d.ts → core/literals.d.ts} +9 -11
  27. package/dist/types/core/promise-to-result.d.ts +1 -0
  28. package/dist/types/{tree → core}/reference.d.ts +5 -6
  29. package/dist/types/{tree → core}/shared.d.ts +13 -13
  30. package/dist/types/{string.d.ts → core/string.d.ts} +3 -3
  31. package/dist/types/core/tree/async.d.ts +3 -0
  32. package/dist/types/core/tree/context.d.ts +45 -0
  33. package/dist/types/core/tree/deserialize.d.ts +3 -0
  34. package/dist/types/core/tree/index.d.ts +15 -0
  35. package/dist/types/core/tree/primitives.d.ts +9 -0
  36. package/dist/types/core/tree/serialize.d.ts +4 -0
  37. package/dist/types/core/tree/sync.d.ts +3 -0
  38. package/dist/types/core/tree/web-api.d.ts +4 -0
  39. package/dist/types/{tree → core}/types.d.ts +228 -238
  40. package/dist/types/core/web-api.d.ts +4 -0
  41. package/dist/types/index.d.ts +9 -19
  42. package/dist/types/types.d.ts +20 -20
  43. package/package.json +5 -5
  44. package/src/core/Serializer.ts +46 -0
  45. package/src/{tree → core}/UnsupportedTypeError.ts +1 -1
  46. package/src/core/assignments.ts +159 -0
  47. package/src/{tree/primitives.ts → core/base-primitives.ts} +33 -136
  48. package/src/core/constants.ts +169 -0
  49. package/src/core/context.ts +15 -0
  50. package/src/core/cross/async.ts +614 -0
  51. package/src/core/cross/context.ts +79 -0
  52. package/src/core/cross/index.ts +154 -0
  53. package/src/core/cross/primitives.ts +137 -0
  54. package/src/core/cross/serialize.ts +846 -0
  55. package/src/core/cross/stream.ts +740 -0
  56. package/src/core/cross/sync.ts +561 -0
  57. package/src/core/cross/web-api.ts +55 -0
  58. package/src/core/header.js +65 -0
  59. package/src/core/keys.ts +33 -0
  60. package/src/core/literals.ts +28 -0
  61. package/src/core/promise-to-result.ts +9 -0
  62. package/src/{tree → core}/reference.ts +6 -7
  63. package/src/{tree → core}/shared.ts +4 -4
  64. package/src/{tree → core/tree}/async.ts +46 -34
  65. package/src/core/tree/context.ts +136 -0
  66. package/src/{tree → core/tree}/deserialize.ts +68 -55
  67. package/src/core/tree/index.ts +146 -0
  68. package/src/core/tree/primitives.ts +132 -0
  69. package/src/{tree → core/tree}/serialize.ts +87 -193
  70. package/src/{tree → core/tree}/sync.ts +50 -39
  71. package/src/{tree → core/tree}/web-api.ts +8 -52
  72. package/src/{tree → core}/types.ts +76 -60
  73. package/src/core/web-api.ts +51 -0
  74. package/src/index.ts +7 -141
  75. package/src/types.ts +1 -1
  76. package/dist/types/context.d.ts +0 -67
  77. package/dist/types/tree/async.d.ts +0 -3
  78. package/dist/types/tree/deserialize.d.ts +0 -3
  79. package/dist/types/tree/primitives.d.ts +0 -19
  80. package/dist/types/tree/serialize.d.ts +0 -4
  81. package/dist/types/tree/symbols.d.ts +0 -33
  82. package/dist/types/tree/sync.d.ts +0 -3
  83. package/dist/types/tree/web-api.d.ts +0 -6
  84. package/src/context.ts +0 -164
  85. package/src/tree/constants.ts +0 -73
  86. package/src/tree/symbols.ts +0 -65
  87. /package/src/{assert.ts → core/assert.ts} +0 -0
  88. /package/src/{compat.ts → core/compat.ts} +0 -0
  89. /package/src/{get-identifier.ts → core/get-identifier.ts} +0 -0
  90. /package/src/{string.ts → core/string.ts} +0 -0
@@ -0,0 +1,169 @@
1
+ import type { SerovalConstantNode } from './types';
2
+
3
+ export const enum SerovalConstant {
4
+ Null = 0,
5
+ Undefined = 1,
6
+ True = 2,
7
+ False = 3,
8
+ NegativeZero = 4,
9
+ Infinity = 5,
10
+ NegativeInfinity = 6,
11
+ NaN = 7,
12
+ }
13
+
14
+ export const enum SerovalNodeType {
15
+ Number = 0,
16
+ String = 1,
17
+ Constant = 2,
18
+ BigInt = 3,
19
+ IndexedValue = 4,
20
+ Date = 5,
21
+ RegExp = 6,
22
+ Set = 7,
23
+ Map = 8,
24
+ Array = 9,
25
+ Object = 10,
26
+ NullConstructor = 11,
27
+ Promise = 12,
28
+ Error = 13,
29
+ AggregateError = 14,
30
+ TypedArray = 15,
31
+ BigIntTypedArray = 16,
32
+ WKSymbol = 17,
33
+ URL = 18,
34
+ URLSearchParams = 19,
35
+ Reference = 20,
36
+ ArrayBuffer = 21,
37
+ DataView = 22,
38
+ Blob = 23,
39
+ File = 24,
40
+ Headers = 25,
41
+ FormData = 26,
42
+ Boxed = 27,
43
+ PromiseConstructor = 28,
44
+ PromiseResolve = 29,
45
+ PromiseReject = 30,
46
+ ReadableStreamConstructor = 31,
47
+ ReadableStreamEnqueue = 32,
48
+ ReadableStreamClose = 33,
49
+ ReadableStreamError = 34,
50
+ }
51
+
52
+ export const enum SerovalObjectFlags {
53
+ None = 0,
54
+ NonExtensible = 1,
55
+ Sealed = 2,
56
+ Frozen = 3,
57
+ }
58
+
59
+ export const enum Symbols {
60
+ AsyncIterator = 0,
61
+ HasInstance = 1,
62
+ IsConcatSpreadable = 2,
63
+ Iterator = 3,
64
+ Match = 4,
65
+ MatchAll = 5,
66
+ Replace = 6,
67
+ Search = 7,
68
+ Species = 8,
69
+ Split = 9,
70
+ ToPrimitive = 10,
71
+ ToStringTag = 11,
72
+ Unscopables = 12,
73
+ }
74
+
75
+ export const SYMBOL_STRING: Record<Symbols, string> = {
76
+ [Symbols.AsyncIterator]: 'Symbol.asyncIterator',
77
+ [Symbols.HasInstance]: 'Symbol.hasInstance',
78
+ [Symbols.IsConcatSpreadable]: 'Symbol.isConcatSpreadable',
79
+ [Symbols.Iterator]: 'Symbol.iterator',
80
+ [Symbols.Match]: 'Symbol.match',
81
+ [Symbols.MatchAll]: 'Symbol.matchAll',
82
+ [Symbols.Replace]: 'Symbol.replace',
83
+ [Symbols.Search]: 'Symbol.search',
84
+ [Symbols.Species]: 'Symbol.species',
85
+ [Symbols.Split]: 'Symbol.split',
86
+ [Symbols.ToPrimitive]: 'Symbol.toPrimitive',
87
+ [Symbols.ToStringTag]: 'Symbol.toStringTag',
88
+ [Symbols.Unscopables]: 'Symbol.unscopables',
89
+ };
90
+
91
+ export const INV_SYMBOL_REF = {
92
+ [Symbol.asyncIterator]: Symbols.AsyncIterator,
93
+ [Symbol.hasInstance]: Symbols.HasInstance,
94
+ [Symbol.isConcatSpreadable]: Symbols.IsConcatSpreadable,
95
+ [Symbol.iterator]: Symbols.Iterator,
96
+ [Symbol.match]: Symbols.Match,
97
+ [Symbol.matchAll]: Symbols.MatchAll,
98
+ [Symbol.replace]: Symbols.Replace,
99
+ [Symbol.search]: Symbols.Search,
100
+ [Symbol.species]: Symbols.Species,
101
+ [Symbol.split]: Symbols.Split,
102
+ [Symbol.toPrimitive]: Symbols.ToPrimitive,
103
+ [Symbol.toStringTag]: Symbols.ToStringTag,
104
+ [Symbol.unscopables]: Symbols.Unscopables,
105
+ };
106
+
107
+ export type WellKnownSymbols = keyof typeof INV_SYMBOL_REF;
108
+
109
+ export const SYMBOL_REF: Record<Symbols, WellKnownSymbols> = {
110
+ [Symbols.AsyncIterator]: Symbol.asyncIterator,
111
+ [Symbols.HasInstance]: Symbol.hasInstance,
112
+ [Symbols.IsConcatSpreadable]: Symbol.isConcatSpreadable,
113
+ [Symbols.Iterator]: Symbol.iterator,
114
+ [Symbols.Match]: Symbol.match,
115
+ [Symbols.MatchAll]: Symbol.matchAll,
116
+ [Symbols.Replace]: Symbol.replace,
117
+ [Symbols.Search]: Symbol.search,
118
+ [Symbols.Species]: Symbol.species,
119
+ [Symbols.Split]: Symbol.split,
120
+ [Symbols.ToPrimitive]: Symbol.toPrimitive,
121
+ [Symbols.ToStringTag]: Symbol.toStringTag,
122
+ [Symbols.Unscopables]: Symbol.unscopables,
123
+ };
124
+
125
+ export function serializeConstant(node: SerovalConstantNode): string {
126
+ switch (node.s) {
127
+ case SerovalConstant.True:
128
+ return '!0';
129
+ case SerovalConstant.False:
130
+ return '!1';
131
+ case SerovalConstant.Undefined:
132
+ return 'void 0';
133
+ case SerovalConstant.Null:
134
+ return 'null';
135
+ case SerovalConstant.NegativeZero:
136
+ return '-0';
137
+ case SerovalConstant.Infinity:
138
+ return '1/0';
139
+ case SerovalConstant.NegativeInfinity:
140
+ return '-1/0';
141
+ case SerovalConstant.NaN:
142
+ return 'NaN';
143
+ default:
144
+ throw new Error('invariant');
145
+ }
146
+ }
147
+
148
+ export function deserializeConstant(node: SerovalConstantNode): unknown {
149
+ switch (node.s) {
150
+ case SerovalConstant.True:
151
+ return true;
152
+ case SerovalConstant.False:
153
+ return false;
154
+ case SerovalConstant.Undefined:
155
+ return undefined;
156
+ case SerovalConstant.Null:
157
+ return null;
158
+ case SerovalConstant.NegativeZero:
159
+ return -0;
160
+ case SerovalConstant.Infinity:
161
+ return Infinity;
162
+ case SerovalConstant.NegativeInfinity:
163
+ return -Infinity;
164
+ case SerovalConstant.NaN:
165
+ return NaN;
166
+ default:
167
+ throw new Error('invariant');
168
+ }
169
+ }
@@ -0,0 +1,15 @@
1
+ import type { Assignment, FlaggedObject } from './assignments';
2
+
3
+ export interface BaseParserContext {
4
+ // Supported features
5
+ features: number;
6
+ }
7
+
8
+ export interface BaseSerializerContext extends BaseParserContext {
9
+ // To check if an object is synchronously referencing itself
10
+ stack: number[];
11
+ // Array of object mutations
12
+ flags: FlaggedObject[];
13
+ // Array of assignments to be done (used for recursion)
14
+ assignments: Assignment[];
15
+ }