seroval 0.11.6 → 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 +3072 -4875
  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 +3069 -4874
  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 -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 +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 -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
@@ -1,93 +1,44 @@
1
- import { Feature } from '../compat';
2
- import { SerovalNodeType } from '../constants';
1
+ import type { PluginAccessOptions } from '../plugin';
3
2
  import type { SerovalNode } from '../types';
4
- import parseAsync from './async';
5
- import type {
6
- ParserOptions,
7
- SerializerContext,
8
- } from './context';
9
- import {
10
- createDeserializerContext,
11
- createParserContext,
12
- createSerializerContext,
13
- getRefParam,
14
- } from './context';
15
- import deserializeTree from './deserialize';
16
- import serializeTree, { resolvePatches } from './serialize';
17
- import parseSync from './sync';
18
-
19
- function finalize(
20
- ctx: SerializerContext,
21
- rootID: number | undefined,
22
- isObject: boolean,
23
- result: string,
24
- ): string {
25
- // Shared references detected
26
- if (rootID != null && ctx.vars.length) {
27
- const patches = resolvePatches(ctx);
28
- let body = result;
29
- if (patches) {
30
- // Get (or create) a ref from the source
31
- const index = getRefParam(ctx, rootID);
32
- body = result + ',' + patches + index;
33
- if (!result.startsWith(index + '=')) {
34
- body = index + '=' + body;
35
- }
36
- }
37
- let params = ctx.vars.length > 1
38
- ? ctx.vars.join(',')
39
- : ctx.vars[0];
40
- // Source is probably already assigned
41
- if (ctx.features & Feature.ArrowFunction) {
42
- params = ctx.vars.length > 1 || ctx.vars.length === 0
43
- ? '(' + params + ')'
44
- : params;
45
- return '(' + params + '=>(' + body + '))()';
46
- }
47
- return '(function(' + params + '){return ' + body + '})()';
48
- }
49
- if (isObject) {
50
- return '(' + result + ')';
51
- }
52
- return result;
53
- }
3
+ import type { AsyncParserContextOptions } from './async';
4
+ import AsyncParserContext from './async';
5
+ import VanillaDeserializerContext from './deserialize';
6
+ import VanillaSerializerContext from './serialize';
7
+ import type { SyncParserContextOptions } from './sync';
8
+ import SyncParserContext from './sync';
54
9
 
55
10
  export function serialize<T>(
56
11
  source: T,
57
- options?: Partial<ParserOptions>,
12
+ options: SyncParserContextOptions = {},
58
13
  ): string {
59
- const ctx = createParserContext(options);
60
- const tree = parseSync(ctx, source);
61
- const serial = createSerializerContext({
62
- markedRefs: ctx.reference.marked,
14
+ const ctx = new SyncParserContext({
15
+ plugins: options.plugins,
16
+ disabledFeatures: options.disabledFeatures,
17
+ });
18
+ const tree = ctx.parse(source);
19
+ const serial = new VanillaSerializerContext({
20
+ plugins: options.plugins,
63
21
  features: ctx.features,
22
+ markedRefs: ctx.marked,
64
23
  });
65
- const result = serializeTree(serial, tree);
66
- return finalize(
67
- serial,
68
- tree.i,
69
- tree.t === SerovalNodeType.Object,
70
- result,
71
- );
24
+ return serial.serializeTop(tree);
72
25
  }
73
26
 
74
27
  export async function serializeAsync<T>(
75
28
  source: T,
76
- options?: Partial<ParserOptions>,
29
+ options: AsyncParserContextOptions = {},
77
30
  ): Promise<string> {
78
- const ctx = createParserContext(options);
79
- const tree = await parseAsync(ctx, source);
80
- const serial = createSerializerContext({
81
- markedRefs: ctx.reference.marked,
31
+ const ctx = new AsyncParserContext({
32
+ plugins: options.plugins,
33
+ disabledFeatures: options.disabledFeatures,
34
+ });
35
+ const tree = await ctx.parse(source);
36
+ const serial = new VanillaSerializerContext({
37
+ plugins: options.plugins,
82
38
  features: ctx.features,
39
+ markedRefs: ctx.marked,
83
40
  });
84
- const result = serializeTree(serial, tree);
85
- return finalize(
86
- serial,
87
- tree.i,
88
- tree.t === SerovalNodeType.Object,
89
- result,
90
- );
41
+ return serial.serializeTop(tree);
91
42
  }
92
43
 
93
44
  export function deserialize<T>(source: string): T {
@@ -103,40 +54,47 @@ export interface SerovalJSON {
103
54
 
104
55
  export function toJSON<T>(
105
56
  source: T,
106
- options?: Partial<ParserOptions>,
57
+ options: SyncParserContextOptions = {},
107
58
  ): SerovalJSON {
108
- const ctx = createParserContext(options);
59
+ const ctx = new SyncParserContext({
60
+ plugins: options.plugins,
61
+ disabledFeatures: options.disabledFeatures,
62
+ });
109
63
  return {
110
- t: parseSync(ctx, source),
64
+ t: ctx.parse(source),
111
65
  f: ctx.features,
112
- m: Array.from(ctx.reference.marked),
66
+ m: Array.from(ctx.marked),
113
67
  };
114
68
  }
115
69
 
116
70
  export async function toJSONAsync<T>(
117
71
  source: T,
118
- options?: Partial<ParserOptions>,
72
+ options: AsyncParserContextOptions = {},
119
73
  ): Promise<SerovalJSON> {
120
- const ctx = createParserContext(options);
74
+ const ctx = new AsyncParserContext({
75
+ plugins: options.plugins,
76
+ disabledFeatures: options.disabledFeatures,
77
+ });
121
78
  return {
122
- t: await parseAsync(ctx, source),
79
+ t: await ctx.parse(source),
123
80
  f: ctx.features,
124
- m: Array.from(ctx.reference.marked),
81
+ m: Array.from(ctx.marked),
125
82
  };
126
83
  }
127
84
 
128
- export function compileJSON(source: SerovalJSON): string {
129
- const serial = createSerializerContext({
85
+ export function compileJSON(source: SerovalJSON, options: PluginAccessOptions = {}): string {
86
+ const ctx = new VanillaSerializerContext({
87
+ plugins: options.plugins,
130
88
  features: source.f,
131
89
  markedRefs: source.m,
132
90
  });
133
- const result = serializeTree(serial, source.t);
134
- return finalize(serial, source.t.i, source.t.i === SerovalNodeType.Object, result);
91
+ return ctx.serializeTop(source.t);
135
92
  }
136
93
 
137
- export function fromJSON<T>(source: SerovalJSON): T {
138
- const serial = createDeserializerContext({
94
+ export function fromJSON<T>(source: SerovalJSON, options: PluginAccessOptions = {}): T {
95
+ const ctx = new VanillaDeserializerContext({
96
+ plugins: options.plugins,
139
97
  markedRefs: source.m,
140
98
  });
141
- return deserializeTree(serial, source.t) as T;
99
+ return ctx.deserialize(source.t) as T;
142
100
  }
@@ -0,0 +1,45 @@
1
+ import { createIndexedValueNode, createReferenceNode } from '../base-primitives';
2
+ import { hasReferenceID } from '../reference';
3
+ import type { SerovalIndexedValueNode, SerovalReferenceNode } from '../types';
4
+
5
+ function createIndexedValue<T>(
6
+ ids: Map<unknown, number>,
7
+ marked: Set<number>,
8
+ current: T,
9
+ ): number {
10
+ const ref = ids.get(current);
11
+ if (ref == null) {
12
+ const id = ids.size;
13
+ ids.set(current, id);
14
+ return id;
15
+ }
16
+ marked.add(ref);
17
+ return ref;
18
+ }
19
+
20
+ export function getVanillaReference<T>(
21
+ ids: Map<unknown, number>,
22
+ marked: Set<number>,
23
+ current: T,
24
+ ): number | SerovalIndexedValueNode | SerovalReferenceNode {
25
+ const id = createIndexedValue(ids, marked, current);
26
+ if (marked.has(id)) {
27
+ return createIndexedValueNode(id);
28
+ }
29
+ if (hasReferenceID(current)) {
30
+ return createReferenceNode(id, current);
31
+ }
32
+ return id;
33
+ }
34
+
35
+ export function getStrictVanillaReference<T>(
36
+ ids: Map<unknown, number>,
37
+ marked: Set<number>,
38
+ current: T,
39
+ ): SerovalIndexedValueNode | SerovalReferenceNode {
40
+ const id = createIndexedValue(ids, marked, current);
41
+ if (marked.has(id)) {
42
+ return createIndexedValueNode(id);
43
+ }
44
+ return createReferenceNode(id, current);
45
+ }