seroval 0.11.6 → 0.12.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 (122) hide show
  1. package/README.md +29 -16
  2. package/dist/cjs/development/index.cjs +3088 -4876
  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 +3085 -4875
  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 +6 -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 +20 -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 -751
  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 -873
  90. package/src/core/cross/sync.ts +6 -624
  91. package/src/core/is-serializable.new.ts +208 -0
  92. package/src/core/keys.ts +5 -3
  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 -123
  98. package/src/core/tree/async.ts +6 -750
  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 -624
  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
@@ -0,0 +1,9 @@
1
+ import type { BaseParserContextOptions } from '../parser-context';
2
+
3
+ export interface CrossParserContextOptions extends BaseParserContextOptions {
4
+ refs?: Map<unknown, number>;
5
+ }
6
+
7
+ export interface CrossContextOptions {
8
+ scopeId?: string;
9
+ }
@@ -1,79 +1,56 @@
1
- import { Feature } from '../compat';
2
- import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
3
- import { serializeString } from '../string';
4
- // import type { SerovalNode } from '../types';
5
- import parseAsync from './async';
6
- import type {
7
- CrossParserContextOptions,
8
- CrossSerializerContext,
9
- } from './context';
10
- import {
11
- createCrossParserContext,
12
- createCrossSerializerContext,
13
- createStreamingCrossParserContext,
14
- } from './context';
15
- import crossSerializeTree, { getRefExpr, resolvePatches } from './serialize';
16
- import crossParseStream from './stream';
17
- import parseSync from './sync';
18
-
19
- function finalize(
20
- ctx: CrossSerializerContext,
21
- scopeId: string | undefined,
22
- id: number | undefined,
23
- result: string,
24
- ): string {
25
- if (id == null) {
26
- return result;
27
- }
28
- const patches = resolvePatches(ctx);
29
- const ref = getRefExpr(id);
30
- const params = scopeId == null ? '' : GLOBAL_CONTEXT_REFERENCES;
31
- const mainBody = patches ? result + ',' + patches : result;
32
- if (params === '') {
33
- return patches ? '(' + mainBody + ref + ')' : mainBody;
34
- }
35
- const args = scopeId == null ? '()' : '(' + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(scopeId) + '"])';
36
- const body = mainBody + (patches ? ref : '');
37
- if (ctx.features & Feature.ArrowFunction) {
38
- return '(' + params + '=>(' + body + '))' + args;
39
- }
40
- return '(function(' + params + '){return ' + body + '})' + args;
1
+ import type { CrossAsyncParserContextOptions } from './async';
2
+ import AsyncCrossParserContext from './async';
3
+ import type { CrossContextOptions } from './cross-parser';
4
+ import CrossSerializerContext from './serialize';
5
+ import type { CrossStreamParserContextOptions } from './stream';
6
+ import StreamCrossParserContext from './stream';
7
+ import type { CrossSyncParserContextOptions } from './sync';
8
+ import SyncCrossParserContext from './sync';
9
+
10
+ export interface CrossSerializeOptions
11
+ extends CrossSyncParserContextOptions, CrossContextOptions {
41
12
  }
42
13
 
43
14
  export function crossSerialize<T>(
44
15
  source: T,
45
- options?: CrossParserContextOptions,
16
+ options: CrossSerializeOptions = {},
46
17
  ): string {
47
- const ctx = createCrossParserContext(options);
48
- const tree = parseSync(ctx, source);
49
- const serial = createCrossSerializerContext({
18
+ const ctx = new SyncCrossParserContext({
19
+ plugins: options.plugins,
20
+ disabledFeatures: options.disabledFeatures,
21
+ refs: options.refs,
22
+ });
23
+ const tree = ctx.parse(source);
24
+ const serial = new CrossSerializerContext({
25
+ plugins: options.plugins,
50
26
  features: ctx.features,
27
+ scopeId: options.scopeId,
28
+ markedRefs: ctx.marked,
51
29
  });
52
- const result = crossSerializeTree(serial, tree);
53
- return finalize(
54
- serial,
55
- ctx.scopeId,
56
- tree.i,
57
- result,
58
- );
30
+ return serial.serializeTop(tree);
31
+ }
32
+
33
+ export interface CrossSerializeAsyncOptions
34
+ extends CrossAsyncParserContextOptions, CrossContextOptions {
59
35
  }
60
36
 
61
37
  export async function crossSerializeAsync<T>(
62
38
  source: T,
63
- options?: CrossParserContextOptions,
39
+ options: CrossSerializeAsyncOptions = {},
64
40
  ): Promise<string> {
65
- const ctx = createCrossParserContext(options);
66
- const tree = await parseAsync(ctx, source);
67
- const serial = createCrossSerializerContext({
41
+ const ctx = new AsyncCrossParserContext({
42
+ plugins: options.plugins,
43
+ disabledFeatures: options.disabledFeatures,
44
+ refs: options.refs,
45
+ });
46
+ const tree = await ctx.parse(source);
47
+ const serial = new CrossSerializerContext({
48
+ plugins: options.plugins,
68
49
  features: ctx.features,
50
+ scopeId: options.scopeId,
51
+ markedRefs: ctx.marked,
69
52
  });
70
- const result = crossSerializeTree(serial, tree);
71
- return finalize(
72
- serial,
73
- ctx.scopeId,
74
- tree.i,
75
- result,
76
- );
53
+ return serial.serializeTop(tree);
77
54
  }
78
55
 
79
56
  // export interface SerovalCrossJSON {
@@ -122,48 +99,49 @@ export async function crossSerializeAsync<T>(
122
99
  // return deserializeTree(serial, source.t) as T;
123
100
  // }
124
101
 
125
- export interface CrossSerializeStreamOptions extends CrossParserContextOptions {
102
+ export interface CrossSerializeStreamOptions
103
+ extends Omit<CrossStreamParserContextOptions, 'onParse'>, CrossContextOptions {
126
104
  onSerialize: (data: string, initial: boolean) => void;
127
- onDone?: () => void;
128
105
  }
129
106
 
130
107
  export function crossSerializeStream<T>(
131
108
  source: T,
132
109
  options: CrossSerializeStreamOptions,
133
110
  ): () => void {
134
- const ctx = createStreamingCrossParserContext({
135
- scopeId: options.scopeId,
111
+ const ctx = new StreamCrossParserContext({
136
112
  refs: options.refs,
137
113
  disabledFeatures: options.disabledFeatures,
138
- onParse(node, initial) {
139
- const serial = createCrossSerializerContext({
114
+ onParse(node, initial): void {
115
+ const serial = new CrossSerializerContext({
116
+ plugins: options.plugins,
140
117
  features: ctx.features,
118
+ scopeId: options.scopeId,
119
+ markedRefs: ctx.marked,
141
120
  });
142
121
 
122
+ let serialized: string;
123
+
124
+ try {
125
+ serialized = serial.serializeTop(node);
126
+ } catch (err) {
127
+ if (options.onError) {
128
+ options.onError(err);
129
+ }
130
+ return;
131
+ }
132
+
143
133
  options.onSerialize(
144
- finalize(
145
- serial,
146
- ctx.scopeId,
147
- node.i,
148
- crossSerializeTree(serial, node),
149
- ),
134
+ serialized,
150
135
  initial,
151
136
  );
152
137
  },
138
+ onError: options.onError,
153
139
  onDone: options.onDone,
154
140
  });
155
141
 
156
- ctx.onParse(crossParseStream(ctx, source), true);
157
-
158
- if (ctx.pending <= 0) {
159
- ctx.onDone();
160
- ctx.alive = false;
161
- }
142
+ ctx.start(source);
162
143
 
163
144
  return () => {
164
- if (ctx.alive) {
165
- ctx.alive = false;
166
- ctx.onDone();
167
- }
145
+ ctx.destroy();
168
146
  };
169
147
  }