seroval 0.8.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 (91) hide show
  1. package/README.md +4 -287
  2. package/dist/cjs/development/index.cjs +3858 -1385
  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 +3611 -1136
  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 -11
  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 -228
  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 +7 -7
  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} +42 -133
  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 -8
  63. package/src/{tree → core}/shared.ts +17 -3
  64. package/src/{tree → core/tree}/async.ts +57 -34
  65. package/src/core/tree/context.ts +136 -0
  66. package/src/{tree → core/tree}/deserialize.ts +83 -54
  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 +124 -178
  70. package/src/{tree → core/tree}/sync.ts +60 -39
  71. package/src/{tree → core/tree}/web-api.ts +10 -50
  72. package/src/{tree → core}/types.ts +82 -54
  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 -61
  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 -154
  85. package/src/tree/constants.ts +0 -72
  86. package/src/tree/iterator.ts +0 -14
  87. package/src/tree/symbols.ts +0 -65
  88. /package/src/{assert.ts → core/assert.ts} +0 -0
  89. /package/src/{compat.ts → core/compat.ts} +0 -0
  90. /package/src/{get-identifier.ts → core/get-identifier.ts} +0 -0
  91. /package/src/{string.ts → core/string.ts} +0 -0
@@ -0,0 +1,154 @@
1
+ import { Feature } from '../compat';
2
+ import {
3
+ ROOT_REFERENCE,
4
+ } from '../keys';
5
+ // import type { SerovalNode } from '../types';
6
+ import parseAsync from './async';
7
+ import type {
8
+ CrossParserContextOptions,
9
+ CrossSerializerContext,
10
+ } from './context';
11
+ import {
12
+ createCrossParserContext,
13
+ createCrossSerializerContext,
14
+ createStreamingCrossParserContext,
15
+ } from './context';
16
+ import crossSerializeTree, { getRefExpr, resolvePatches } from './serialize';
17
+ import crossParseStream from './stream';
18
+ import parseSync from './sync';
19
+
20
+ function finalize(
21
+ ctx: CrossSerializerContext,
22
+ id: number | undefined,
23
+ result: string,
24
+ ): string {
25
+ const patches = resolvePatches(ctx);
26
+ if (patches) {
27
+ if (id == null) {
28
+ if (ctx.features & Feature.ArrowFunction) {
29
+ const params = '(' + ROOT_REFERENCE + ')';
30
+ const body = ROOT_REFERENCE + '=' + result + ',' + patches + ROOT_REFERENCE;
31
+ return '(' + params + '=>(' + body + '))()';
32
+ }
33
+ const params = '(' + ROOT_REFERENCE + ')';
34
+ const body = ROOT_REFERENCE + '=' + result + ',' + patches + ROOT_REFERENCE;
35
+ return '(function' + params + '{return ' + body + '})()';
36
+ }
37
+ return '(' + result + ',' + patches + getRefExpr(id) + ')';
38
+ }
39
+ return result;
40
+ }
41
+
42
+ export function crossSerialize<T>(
43
+ source: T,
44
+ options?: CrossParserContextOptions,
45
+ ): string {
46
+ const ctx = createCrossParserContext(options);
47
+ const tree = parseSync(ctx, source);
48
+ const serial = createCrossSerializerContext({
49
+ features: ctx.features,
50
+ });
51
+ const result = crossSerializeTree(serial, tree);
52
+ return finalize(
53
+ serial,
54
+ tree.i,
55
+ result,
56
+ );
57
+ }
58
+
59
+ export async function crossSerializeAsync<T>(
60
+ source: T,
61
+ options?: CrossParserContextOptions,
62
+ ): Promise<string> {
63
+ const ctx = createCrossParserContext(options);
64
+ const tree = await parseAsync(ctx, source);
65
+ const serial = createCrossSerializerContext({
66
+ features: ctx.features,
67
+ });
68
+ const result = crossSerializeTree(serial, tree);
69
+ return finalize(
70
+ serial,
71
+ tree.i,
72
+ result,
73
+ );
74
+ }
75
+
76
+ // export interface SerovalCrossJSON {
77
+ // t: SerovalNode;
78
+ // f: number;
79
+ // }
80
+
81
+ // export function toCrossJSON<T>(
82
+ // source: T,
83
+ // options?: CrossParserContextOptions,
84
+ // ): SerovalCrossJSON {
85
+ // const ctx = createCrossParserContext(options);
86
+ // return {
87
+ // t: parseSync(ctx, source),
88
+ // f: ctx.features,
89
+ // };
90
+ // }
91
+
92
+ // export async function toCrossJSONAsync<T>(
93
+ // source: T,
94
+ // options?: CrossParserContextOptions,
95
+ // ): Promise<SerovalCrossJSON> {
96
+ // const ctx = createCrossParserContext(options);
97
+ // return {
98
+ // t: await parseAsync(ctx, source),
99
+ // f: ctx.features,
100
+ // };
101
+ // }
102
+
103
+ // export function compileCrossJSON(source: SerovalCrossJSON): string {
104
+ // const serial = createCrossSerializerContext({
105
+ // features: source.f,
106
+ // });
107
+ // const result = crossSerializeTree(serial, source.t);
108
+ // return finalize(
109
+ // serial,
110
+ // source.t.i,
111
+ // result,
112
+ // );
113
+ // }
114
+
115
+ // export function fromJSON<T>(source: SerovalJSON): T {
116
+ // const serial = createDeserializerContext({
117
+ // markedRefs: source.m,
118
+ // });
119
+ // return deserializeTree(serial, source.t) as T;
120
+ // }
121
+
122
+ export interface CrossSerializeStreamOptions extends CrossParserContextOptions {
123
+ onSerialize: (data: string, initial: boolean) => void;
124
+ }
125
+
126
+ export function crossSerializeStream<T>(
127
+ source: T,
128
+ options: CrossSerializeStreamOptions,
129
+ ): () => void {
130
+ const ctx = createStreamingCrossParserContext({
131
+ refs: options.refs,
132
+ disabledFeatures: options.disabledFeatures,
133
+ onParse(node, initial) {
134
+ const serial = createCrossSerializerContext({
135
+ features: ctx.features,
136
+ });
137
+
138
+ options.onSerialize(
139
+ finalize(
140
+ serial,
141
+ node.i,
142
+ crossSerializeTree(serial, node),
143
+ ),
144
+ initial,
145
+ );
146
+ },
147
+ });
148
+
149
+ ctx.onParse(crossParseStream(ctx, source), true);
150
+
151
+ return () => {
152
+ ctx.alive = false;
153
+ };
154
+ }
@@ -0,0 +1,137 @@
1
+ import assert from '../assert';
2
+ import { Feature } from '../compat';
3
+ import type { CrossParserContext } from './context';
4
+ import type { BigIntTypedArrayValue, TypedArrayValue } from '../../types';
5
+ import UnsupportedTypeError from '../UnsupportedTypeError';
6
+ import { hasReferenceID } from '../reference';
7
+ import type {
8
+ SerovalBigIntTypedArrayNode,
9
+ SerovalTypedArrayNode,
10
+ SerovalDataViewNode,
11
+ SerovalNode,
12
+ SerovalIndexedValueNode,
13
+ SerovalReferenceNode,
14
+ SerovalWKSymbolNode,
15
+ } from '../types';
16
+ import type { WellKnownSymbols } from '../constants';
17
+ import { SerovalNodeType } from '../constants';
18
+ import {
19
+ createIndexedValueNode,
20
+ createArrayBufferNode,
21
+ createWKSymbolNode,
22
+ createReferenceNode,
23
+ } from '../base-primitives';
24
+
25
+ export function serializeArrayBuffer(
26
+ ctx: CrossParserContext,
27
+ current: ArrayBuffer,
28
+ ): SerovalNode {
29
+ const id = ctx.refs.get(current);
30
+ if (id != null) {
31
+ return createIndexedValueNode(id);
32
+ }
33
+ const newID = ctx.refs.size;
34
+ ctx.refs.set(current, newID);
35
+ return createArrayBufferNode(newID, current);
36
+ }
37
+
38
+ export function createTypedArrayNode(
39
+ ctx: CrossParserContext,
40
+ id: number,
41
+ current: TypedArrayValue,
42
+ ): SerovalTypedArrayNode {
43
+ assert(ctx.features & Feature.TypedArray, new UnsupportedTypeError(current));
44
+ return {
45
+ t: SerovalNodeType.TypedArray,
46
+ i: id,
47
+ s: undefined,
48
+ l: current.length,
49
+ c: current.constructor.name,
50
+ m: undefined,
51
+ p: undefined,
52
+ e: undefined,
53
+ a: undefined,
54
+ f: serializeArrayBuffer(ctx, current.buffer),
55
+ b: current.byteOffset,
56
+ o: undefined,
57
+ };
58
+ }
59
+
60
+ const BIGINT_FLAG = Feature.BigIntTypedArray | Feature.BigInt;
61
+
62
+ export function createBigIntTypedArrayNode(
63
+ ctx: CrossParserContext,
64
+ id: number,
65
+ current: BigIntTypedArrayValue,
66
+ ): SerovalBigIntTypedArrayNode {
67
+ assert(
68
+ (ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
69
+ new UnsupportedTypeError(current),
70
+ );
71
+ return {
72
+ t: SerovalNodeType.BigIntTypedArray,
73
+ i: id,
74
+ s: undefined,
75
+ l: current.length,
76
+ c: current.constructor.name,
77
+ m: undefined,
78
+ p: undefined,
79
+ e: undefined,
80
+ a: undefined,
81
+ f: serializeArrayBuffer(ctx, current.buffer),
82
+ b: current.byteOffset,
83
+ o: undefined,
84
+ };
85
+ }
86
+
87
+ export function createDataViewNode(
88
+ ctx: CrossParserContext,
89
+ id: number,
90
+ current: DataView,
91
+ ): SerovalDataViewNode {
92
+ return {
93
+ t: SerovalNodeType.DataView,
94
+ i: id,
95
+ s: undefined,
96
+ l: current.byteLength,
97
+ c: undefined,
98
+ m: undefined,
99
+ p: undefined,
100
+ e: undefined,
101
+ a: undefined,
102
+ f: serializeArrayBuffer(ctx, current.buffer),
103
+ b: current.byteOffset,
104
+ o: undefined,
105
+ };
106
+ }
107
+
108
+ export function createSymbolNode(
109
+ ctx: CrossParserContext,
110
+ current: symbol,
111
+ ): SerovalIndexedValueNode | SerovalReferenceNode | SerovalWKSymbolNode {
112
+ const id = ctx.refs.get(current);
113
+ if (id != null) {
114
+ return createIndexedValueNode(id);
115
+ }
116
+ const newID = ctx.refs.size;
117
+ ctx.refs.set(current, newID);
118
+ if (hasReferenceID(current)) {
119
+ return createReferenceNode(newID, current);
120
+ }
121
+ return createWKSymbolNode(ctx, newID, current as WellKnownSymbols);
122
+ }
123
+
124
+ export function createFunctionNode(
125
+ ctx: CrossParserContext,
126
+ // eslint-disable-next-line @typescript-eslint/ban-types
127
+ current: Function,
128
+ ): SerovalIndexedValueNode | SerovalReferenceNode {
129
+ assert(hasReferenceID(current), new Error('Cannot serialize function without reference ID.'));
130
+ const id = ctx.refs.get(current);
131
+ if (id != null) {
132
+ return createIndexedValueNode(id);
133
+ }
134
+ const newID = ctx.refs.size;
135
+ ctx.refs.set(current, newID);
136
+ return createReferenceNode(newID, current);
137
+ }