seroval 1.3.2 → 1.4.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 (112) hide show
  1. package/dist/cjs/development/index.cjs +2640 -2683
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -17
  4. package/dist/esm/development/index.mjs +2640 -2683
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -17
  7. package/dist/types/core/base-primitives.d.ts +1 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +1 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +1 -0
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +54 -0
  14. package/dist/types/core/constructors.d.ts.map +1 -0
  15. package/dist/types/core/context/async-parser.d.ts +19 -0
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -0
  17. package/dist/types/core/context/deserializer.d.ts +35 -38
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +32 -19
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts +38 -99
  22. package/dist/types/core/context/serializer.d.ts.map +1 -1
  23. package/dist/types/core/context/sync-parser.d.ts +59 -0
  24. package/dist/types/core/context/sync-parser.d.ts.map +1 -0
  25. package/dist/types/core/cross/index.d.ts +12 -13
  26. package/dist/types/core/cross/index.d.ts.map +1 -1
  27. package/dist/types/core/errors.d.ts.map +1 -1
  28. package/dist/types/core/function-string.d.ts +2 -2
  29. package/dist/types/core/function-string.d.ts.map +1 -1
  30. package/dist/types/core/plugin.d.ts +13 -11
  31. package/dist/types/core/plugin.d.ts.map +1 -1
  32. package/dist/types/core/reference.d.ts.map +1 -1
  33. package/dist/types/core/special-reference.d.ts +3 -2
  34. package/dist/types/core/special-reference.d.ts.map +1 -1
  35. package/dist/types/core/stream.d.ts +1 -2
  36. package/dist/types/core/stream.d.ts.map +1 -1
  37. package/dist/types/core/symbols.d.ts +14 -0
  38. package/dist/types/core/symbols.d.ts.map +1 -0
  39. package/dist/types/core/tree/index.d.ts +3 -2
  40. package/dist/types/core/tree/index.d.ts.map +1 -1
  41. package/dist/types/core/types.d.ts +3 -1
  42. package/dist/types/core/types.d.ts.map +1 -1
  43. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +10 -5
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/package.json +17 -19
  47. package/src/core/base-primitives.ts +0 -27
  48. package/src/core/compat.ts +2 -1
  49. package/src/core/constants.ts +28 -26
  50. package/src/core/constructors.ts +269 -0
  51. package/src/core/context/async-parser.ts +586 -0
  52. package/src/core/context/deserializer.ts +506 -316
  53. package/src/core/context/parser.ts +243 -180
  54. package/src/core/context/serializer.ts +1058 -827
  55. package/src/core/context/sync-parser.ts +833 -0
  56. package/src/core/cross/index.ts +63 -47
  57. package/src/core/errors.ts +73 -12
  58. package/src/core/function-string.ts +11 -10
  59. package/src/core/plugin.ts +20 -11
  60. package/src/core/reference.ts +8 -5
  61. package/src/core/special-reference.ts +18 -90
  62. package/src/core/stream.ts +18 -152
  63. package/src/core/symbols.ts +18 -0
  64. package/src/core/tree/index.ts +45 -31
  65. package/src/core/types.ts +7 -2
  66. package/src/core/utils/iterator-to-sequence.ts +6 -28
  67. package/src/index.ts +29 -11
  68. package/dist/types/core/context/parser/async.d.ts +0 -24
  69. package/dist/types/core/context/parser/async.d.ts.map +0 -1
  70. package/dist/types/core/context/parser/stream.d.ts +0 -43
  71. package/dist/types/core/context/parser/stream.d.ts.map +0 -1
  72. package/dist/types/core/context/parser/sync.d.ts +0 -30
  73. package/dist/types/core/context/parser/sync.d.ts.map +0 -1
  74. package/dist/types/core/cross/async.d.ts +0 -8
  75. package/dist/types/core/cross/async.d.ts.map +0 -1
  76. package/dist/types/core/cross/deserializer.d.ts +0 -9
  77. package/dist/types/core/cross/deserializer.d.ts.map +0 -1
  78. package/dist/types/core/cross/parser.d.ts +0 -6
  79. package/dist/types/core/cross/parser.d.ts.map +0 -1
  80. package/dist/types/core/cross/serializer.d.ts +0 -16
  81. package/dist/types/core/cross/serializer.d.ts.map +0 -1
  82. package/dist/types/core/cross/stream.d.ts +0 -8
  83. package/dist/types/core/cross/stream.d.ts.map +0 -1
  84. package/dist/types/core/cross/sync.d.ts +0 -8
  85. package/dist/types/core/cross/sync.d.ts.map +0 -1
  86. package/dist/types/core/tree/async.d.ts +0 -8
  87. package/dist/types/core/tree/async.d.ts.map +0 -1
  88. package/dist/types/core/tree/deserializer.d.ts +0 -13
  89. package/dist/types/core/tree/deserializer.d.ts.map +0 -1
  90. package/dist/types/core/tree/serializer.d.ts +0 -29
  91. package/dist/types/core/tree/serializer.d.ts.map +0 -1
  92. package/dist/types/core/tree/sync.d.ts +0 -8
  93. package/dist/types/core/tree/sync.d.ts.map +0 -1
  94. package/dist/types/core/utils/assert.d.ts +0 -2
  95. package/dist/types/core/utils/assert.d.ts.map +0 -1
  96. package/dist/types/core/utils/deferred.d.ts +0 -7
  97. package/dist/types/core/utils/deferred.d.ts.map +0 -1
  98. package/src/core/context/parser/async.ts +0 -495
  99. package/src/core/context/parser/stream.ts +0 -323
  100. package/src/core/context/parser/sync.ts +0 -404
  101. package/src/core/cross/async.ts +0 -9
  102. package/src/core/cross/deserializer.ts +0 -16
  103. package/src/core/cross/parser.ts +0 -7
  104. package/src/core/cross/serializer.ts +0 -72
  105. package/src/core/cross/stream.ts +0 -9
  106. package/src/core/cross/sync.ts +0 -9
  107. package/src/core/tree/async.ts +0 -9
  108. package/src/core/tree/deserializer.ts +0 -26
  109. package/src/core/tree/serializer.ts +0 -98
  110. package/src/core/tree/sync.ts +0 -9
  111. package/src/core/utils/assert.ts +0 -5
  112. package/src/core/utils/deferred.ts +0 -22
@@ -1,9 +0,0 @@
1
- import BaseAsyncParserContext from '../context/parser/async';
2
- import type { SerovalMode } from '../plugin';
3
- import type { CrossParserContextOptions } from './parser';
4
-
5
- export type CrossAsyncParserContextOptions = CrossParserContextOptions;
6
-
7
- export default class CrossAsyncParserContext extends BaseAsyncParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,16 +0,0 @@
1
- import type { BaseDeserializerOptions } from '../context/deserializer';
2
- import BaseDeserializerContext from '../context/deserializer';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type CrossDeserializerContextOptions = BaseDeserializerOptions;
6
-
7
- export default class CrossDeserializerContext extends BaseDeserializerContext {
8
- readonly mode: SerovalMode = 'cross';
9
-
10
- assignIndexedValue<T>(index: number, value: T): T {
11
- if (!this.refs.has(index)) {
12
- this.refs.set(index, value);
13
- }
14
- return value;
15
- }
16
- }
@@ -1,7 +0,0 @@
1
- import type { BaseParserContextOptions } from '../context/parser';
2
-
3
- export type CrossParserContextOptions = BaseParserContextOptions;
4
-
5
- export interface CrossContextOptions {
6
- scopeId?: string;
7
- }
@@ -1,72 +0,0 @@
1
- import { SerovalNodeType } from '../constants';
2
- import type { BaseSerializerContextOptions } from '../context/serializer';
3
- import BaseSerializerContext from '../context/serializer';
4
- import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
5
- import type { SerovalMode } from '../plugin';
6
- import { serializeString } from '../string';
7
- import type { SerovalNode } from '../types';
8
- import type { CrossContextOptions } from './parser';
9
-
10
- export interface CrossSerializerContextOptions
11
- extends BaseSerializerContextOptions,
12
- CrossContextOptions {}
13
-
14
- export default class CrossSerializerContext extends BaseSerializerContext {
15
- readonly mode: SerovalMode = 'cross';
16
-
17
- scopeId?: string;
18
-
19
- constructor(options: CrossSerializerContextOptions) {
20
- super(options);
21
- this.scopeId = options.scopeId;
22
- }
23
-
24
- getRefParam(id: number): string {
25
- return GLOBAL_CONTEXT_REFERENCES + '[' + id + ']';
26
- }
27
-
28
- protected assignIndexedValue(index: number, value: string): string {
29
- // In cross-reference, we have to assume that
30
- // every reference are going to be referenced
31
- // in the future, and so we need to store
32
- // all of it into the reference array.
33
- return this.getRefParam(index) + '=' + value;
34
- }
35
-
36
- serializeTop(tree: SerovalNode): string {
37
- // Get the serialized result
38
- const result = this.serialize(tree);
39
- // If the node is a non-reference, return
40
- // the result immediately
41
- const id = tree.i;
42
- if (id == null) {
43
- return result;
44
- }
45
- // Get the patches
46
- const patches = this.resolvePatches();
47
- // Get the variable that represents the root
48
- const ref = this.getRefParam(id);
49
- // Parameters needed for scoping
50
- const params = this.scopeId == null ? '' : GLOBAL_CONTEXT_REFERENCES;
51
- // If there are patches, append it after the result
52
- const body = patches ? '(' + result + ',' + patches + ref + ')' : result;
53
- // If there are no params, there's no need to generate a function
54
- if (params === '') {
55
- if (tree.t === SerovalNodeType.Object && !patches) {
56
- return '(' + body + ')';
57
- }
58
- return body;
59
- }
60
- // Get the arguments for the IIFE
61
- const args =
62
- this.scopeId == null
63
- ? '()'
64
- : '(' +
65
- GLOBAL_CONTEXT_REFERENCES +
66
- '["' +
67
- serializeString(this.scopeId) +
68
- '"])';
69
- // Create the IIFE
70
- return '(' + this.createFunction([params], body) + ')' + args;
71
- }
72
- }
@@ -1,9 +0,0 @@
1
- import type { BaseStreamParserContextOptions } from '../context/parser/stream';
2
- import BaseStreamParserContext from '../context/parser/stream';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type CrossStreamParserContextOptions = BaseStreamParserContextOptions;
6
-
7
- export default class CrossStreamParserContext extends BaseStreamParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,9 +0,0 @@
1
- import BaseSyncParserContext from '../context/parser/sync';
2
- import type { SerovalMode } from '../plugin';
3
- import type { CrossParserContextOptions } from './parser';
4
-
5
- export type CrossSyncParserContextOptions = CrossParserContextOptions;
6
-
7
- export default class CrossSyncParserContext extends BaseSyncParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,9 +0,0 @@
1
- import type { BaseParserContextOptions } from '../context/parser';
2
- import BaseAsyncParserContext from '../context/parser/async';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
6
-
7
- export default class AsyncParserContext extends BaseAsyncParserContext {
8
- readonly mode: SerovalMode = 'vanilla';
9
- }
@@ -1,26 +0,0 @@
1
- import type { BaseDeserializerOptions } from '../context/deserializer';
2
- import BaseDeserializerContext from '../context/deserializer';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export interface VanillaDeserializerContextOptions
6
- extends Omit<BaseDeserializerOptions, 'refs'> {
7
- markedRefs: number[] | Set<number>;
8
- }
9
-
10
- export default class VanillaDeserializerContext extends BaseDeserializerContext {
11
- readonly mode: SerovalMode = 'vanilla';
12
-
13
- marked: Set<number>;
14
-
15
- constructor(options: VanillaDeserializerContextOptions) {
16
- super(options);
17
- this.marked = new Set(options.markedRefs);
18
- }
19
-
20
- assignIndexedValue<T>(index: number, value: T): T {
21
- if (this.marked.has(index)) {
22
- this.refs.set(index, value);
23
- }
24
- return value;
25
- }
26
- }
@@ -1,98 +0,0 @@
1
- import { SerovalNodeType } from '../constants';
2
- import type { BaseSerializerContextOptions } from '../context/serializer';
3
- import BaseSerializerContext from '../context/serializer';
4
- import { SerovalUnsupportedNodeError } from '../errors';
5
- import type { SerovalMode } from '../plugin';
6
- import type {
7
- SerovalNode,
8
- SerovalPromiseConstructorNode,
9
- SerovalPromiseRejectNode,
10
- SerovalPromiseResolveNode,
11
- } from '../types';
12
- import getIdentifier from '../utils/get-identifier';
13
-
14
- export type VanillaSerializerContextOptions = BaseSerializerContextOptions;
15
-
16
- export default class VanillaSerializerContext extends BaseSerializerContext {
17
- readonly mode: SerovalMode = 'vanilla';
18
-
19
- /**
20
- * Map tree refs to actual refs
21
- * @private
22
- */
23
- valid = new Map<number, number>();
24
-
25
- /**
26
- * Variables
27
- * @private
28
- */
29
- vars: string[] = [];
30
-
31
- /**
32
- * Creates the reference param (identifier) from the given reference ID
33
- * Calling this function means the value has been referenced somewhere
34
- */
35
- getRefParam(index: number): string {
36
- /**
37
- * Creates a new reference ID from a given reference ID
38
- * This new reference ID means that the reference itself
39
- * has been referenced at least once, and is used to generate
40
- * the variables
41
- */
42
- let actualIndex = this.valid.get(index);
43
- if (actualIndex == null) {
44
- actualIndex = this.valid.size;
45
- this.valid.set(index, actualIndex);
46
- }
47
- let identifier = this.vars[actualIndex];
48
- if (identifier == null) {
49
- identifier = getIdentifier(actualIndex);
50
- this.vars[actualIndex] = identifier;
51
- }
52
- return identifier;
53
- }
54
-
55
- protected assignIndexedValue(index: number, value: string): string {
56
- if (this.isMarked(index)) {
57
- return this.getRefParam(index) + '=' + value;
58
- }
59
- return value;
60
- }
61
-
62
- protected serializePromiseConstructor(
63
- node: SerovalPromiseConstructorNode,
64
- ): string {
65
- throw new SerovalUnsupportedNodeError(node);
66
- }
67
-
68
- protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
69
- throw new SerovalUnsupportedNodeError(node);
70
- }
71
-
72
- protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
73
- throw new SerovalUnsupportedNodeError(node);
74
- }
75
-
76
- serializeTop(tree: SerovalNode): string {
77
- const result = this.serialize(tree);
78
- // Shared references detected
79
- if (tree.i != null && this.vars.length) {
80
- const patches = this.resolvePatches();
81
- let body = result;
82
- if (patches) {
83
- // Get (or create) a ref from the source
84
- const index = this.getRefParam(tree.i);
85
- body = result + ',' + patches + index;
86
- if (!result.startsWith(index + '=')) {
87
- body = index + '=' + body;
88
- }
89
- body = '(' + body + ')';
90
- }
91
- return '(' + this.createFunction(this.vars, body) + ')()';
92
- }
93
- if (tree.t === SerovalNodeType.Object) {
94
- return '(' + result + ')';
95
- }
96
- return result;
97
- }
98
- }
@@ -1,9 +0,0 @@
1
- import BaseSyncParserContext from '../context/parser/sync';
2
- import type { BaseParserContextOptions } from '../context/parser';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
6
-
7
- export default class SyncParserContext extends BaseSyncParserContext {
8
- readonly mode: SerovalMode = 'vanilla';
9
- }
@@ -1,5 +0,0 @@
1
- export default function assert(cond: unknown, error: Error): asserts cond {
2
- if (!cond) {
3
- throw error;
4
- }
5
- }
@@ -1,22 +0,0 @@
1
- export interface Deferred {
2
- promise: Promise<unknown>;
3
- resolve(value: unknown): void;
4
- reject(value: unknown): void;
5
- }
6
-
7
- export function createDeferred(): Deferred {
8
- let resolve: Deferred['resolve'];
9
- let reject: Deferred['reject'];
10
- return {
11
- promise: new Promise<unknown>((res, rej) => {
12
- resolve = res;
13
- reject = rej;
14
- }),
15
- resolve(value): void {
16
- resolve(value);
17
- },
18
- reject(value): void {
19
- reject(value);
20
- },
21
- };
22
- }