seroval 0.9.0 → 0.10.2

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 +3529 -1140
  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 +3533 -1142
  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 +15 -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 +37 -0
  18. package/dist/types/core/cross/index.d.ts +8 -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 +13 -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 +44 -0
  33. package/dist/types/core/tree/deserialize.d.ts +3 -0
  34. package/dist/types/core/tree/index.d.ts +14 -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 +48 -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 +102 -0
  52. package/src/core/cross/index.ts +169 -0
  53. package/src/core/cross/primitives.ts +137 -0
  54. package/src/core/cross/serialize.ts +847 -0
  55. package/src/core/cross/stream.ts +745 -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 +61 -0
  59. package/src/core/keys.ts +41 -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 +125 -0
  66. package/src/{tree → core/tree}/deserialize.ts +68 -55
  67. package/src/core/tree/index.ts +142 -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 { 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;
41
+ }
42
+
43
+ export function crossSerialize<T>(
44
+ source: T,
45
+ options?: CrossParserContextOptions,
46
+ ): string {
47
+ const ctx = createCrossParserContext(options);
48
+ const tree = parseSync(ctx, source);
49
+ const serial = createCrossSerializerContext({
50
+ features: ctx.features,
51
+ });
52
+ const result = crossSerializeTree(serial, tree);
53
+ return finalize(
54
+ serial,
55
+ ctx.scopeId,
56
+ tree.i,
57
+ result,
58
+ );
59
+ }
60
+
61
+ export async function crossSerializeAsync<T>(
62
+ source: T,
63
+ options?: CrossParserContextOptions,
64
+ ): Promise<string> {
65
+ const ctx = createCrossParserContext(options);
66
+ const tree = await parseAsync(ctx, source);
67
+ const serial = createCrossSerializerContext({
68
+ features: ctx.features,
69
+ });
70
+ const result = crossSerializeTree(serial, tree);
71
+ return finalize(
72
+ serial,
73
+ ctx.scopeId,
74
+ tree.i,
75
+ result,
76
+ );
77
+ }
78
+
79
+ // export interface SerovalCrossJSON {
80
+ // t: SerovalNode;
81
+ // f: number;
82
+ // }
83
+
84
+ // export function toCrossJSON<T>(
85
+ // source: T,
86
+ // options?: CrossParserContextOptions,
87
+ // ): SerovalCrossJSON {
88
+ // const ctx = createCrossParserContext(options);
89
+ // return {
90
+ // t: parseSync(ctx, source),
91
+ // f: ctx.features,
92
+ // };
93
+ // }
94
+
95
+ // export async function toCrossJSONAsync<T>(
96
+ // source: T,
97
+ // options?: CrossParserContextOptions,
98
+ // ): Promise<SerovalCrossJSON> {
99
+ // const ctx = createCrossParserContext(options);
100
+ // return {
101
+ // t: await parseAsync(ctx, source),
102
+ // f: ctx.features,
103
+ // };
104
+ // }
105
+
106
+ // export function compileCrossJSON(source: SerovalCrossJSON): string {
107
+ // const serial = createCrossSerializerContext({
108
+ // features: source.f,
109
+ // });
110
+ // const result = crossSerializeTree(serial, source.t);
111
+ // return finalize(
112
+ // serial,
113
+ // source.t.i,
114
+ // result,
115
+ // );
116
+ // }
117
+
118
+ // export function fromJSON<T>(source: SerovalJSON): T {
119
+ // const serial = createDeserializerContext({
120
+ // markedRefs: source.m,
121
+ // });
122
+ // return deserializeTree(serial, source.t) as T;
123
+ // }
124
+
125
+ export interface CrossSerializeStreamOptions extends CrossParserContextOptions {
126
+ onSerialize: (data: string, initial: boolean) => void;
127
+ onDone?: () => void;
128
+ }
129
+
130
+ export function crossSerializeStream<T>(
131
+ source: T,
132
+ options: CrossSerializeStreamOptions,
133
+ ): () => void {
134
+ const ctx = createStreamingCrossParserContext({
135
+ scopeId: options.scopeId,
136
+ refs: options.refs,
137
+ disabledFeatures: options.disabledFeatures,
138
+ onParse(node, initial) {
139
+ const serial = createCrossSerializerContext({
140
+ features: ctx.features,
141
+ });
142
+
143
+ options.onSerialize(
144
+ finalize(
145
+ serial,
146
+ ctx.scopeId,
147
+ node.i,
148
+ crossSerializeTree(serial, node),
149
+ ),
150
+ initial,
151
+ );
152
+ },
153
+ onDone: options.onDone,
154
+ });
155
+
156
+ ctx.onParse(crossParseStream(ctx, source), true);
157
+
158
+ if (ctx.pending <= 0) {
159
+ ctx.onDone();
160
+ ctx.alive = false;
161
+ }
162
+
163
+ return () => {
164
+ if (ctx.alive) {
165
+ ctx.alive = false;
166
+ ctx.onDone();
167
+ }
168
+ };
169
+ }
@@ -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
+ }