seroval 1.5.6 → 1.6.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 (111) hide show
  1. package/dist/index.d.ts +761 -0
  2. package/dist/index.js +2928 -0
  3. package/package.json +14 -23
  4. package/dist/cjs/development/index.cjs +0 -4232
  5. package/dist/cjs/development/index.cjs.map +0 -7
  6. package/dist/cjs/production/index.cjs +0 -3
  7. package/dist/esm/development/index.mjs +0 -4209
  8. package/dist/esm/development/index.mjs.map +0 -7
  9. package/dist/esm/production/index.mjs +0 -3
  10. package/dist/types/core/Serializer.d.ts +0 -28
  11. package/dist/types/core/Serializer.d.ts.map +0 -1
  12. package/dist/types/core/base-primitives.d.ts +0 -28
  13. package/dist/types/core/base-primitives.d.ts.map +0 -1
  14. package/dist/types/core/compat.d.ts +0 -15
  15. package/dist/types/core/compat.d.ts.map +0 -1
  16. package/dist/types/core/constants.d.ts +0 -105
  17. package/dist/types/core/constants.d.ts.map +0 -1
  18. package/dist/types/core/constructors.d.ts +0 -50
  19. package/dist/types/core/constructors.d.ts.map +0 -1
  20. package/dist/types/core/context/async-parser.d.ts +0 -20
  21. package/dist/types/core/context/async-parser.d.ts.map +0 -1
  22. package/dist/types/core/context/deserializer.d.ts +0 -52
  23. package/dist/types/core/context/deserializer.d.ts.map +0 -1
  24. package/dist/types/core/context/parser.d.ts +0 -62
  25. package/dist/types/core/context/parser.d.ts.map +0 -1
  26. package/dist/types/core/context/serializer.d.ts +0 -103
  27. package/dist/types/core/context/serializer.d.ts.map +0 -1
  28. package/dist/types/core/context/sync-parser.d.ts +0 -61
  29. package/dist/types/core/context/sync-parser.d.ts.map +0 -1
  30. package/dist/types/core/cross/index.d.ts +0 -24
  31. package/dist/types/core/cross/index.d.ts.map +0 -1
  32. package/dist/types/core/errors.d.ts +0 -47
  33. package/dist/types/core/errors.d.ts.map +0 -1
  34. package/dist/types/core/function-string.d.ts +0 -3
  35. package/dist/types/core/function-string.d.ts.map +0 -1
  36. package/dist/types/core/keys.d.ts +0 -4
  37. package/dist/types/core/keys.d.ts.map +0 -1
  38. package/dist/types/core/literals.d.ts +0 -10
  39. package/dist/types/core/literals.d.ts.map +0 -1
  40. package/dist/types/core/node.d.ts +0 -8
  41. package/dist/types/core/node.d.ts.map +0 -1
  42. package/dist/types/core/opaque-reference.d.ts +0 -9
  43. package/dist/types/core/opaque-reference.d.ts.map +0 -1
  44. package/dist/types/core/plugin.d.ts +0 -52
  45. package/dist/types/core/plugin.d.ts.map +0 -1
  46. package/dist/types/core/reference.d.ts +0 -6
  47. package/dist/types/core/reference.d.ts.map +0 -1
  48. package/dist/types/core/sequence.d.ts +0 -11
  49. package/dist/types/core/sequence.d.ts.map +0 -1
  50. package/dist/types/core/special-reference.d.ts +0 -16
  51. package/dist/types/core/special-reference.d.ts.map +0 -1
  52. package/dist/types/core/stream.d.ts +0 -17
  53. package/dist/types/core/stream.d.ts.map +0 -1
  54. package/dist/types/core/string.d.ts +0 -4
  55. package/dist/types/core/string.d.ts.map +0 -1
  56. package/dist/types/core/symbols.d.ts +0 -14
  57. package/dist/types/core/symbols.d.ts.map +0 -1
  58. package/dist/types/core/tree/index.d.ts +0 -21
  59. package/dist/types/core/tree/index.d.ts.map +0 -1
  60. package/dist/types/core/types.d.ts +0 -233
  61. package/dist/types/core/types.d.ts.map +0 -1
  62. package/dist/types/core/utils/error.d.ts +0 -6
  63. package/dist/types/core/utils/error.d.ts.map +0 -1
  64. package/dist/types/core/utils/get-identifier.d.ts +0 -2
  65. package/dist/types/core/utils/get-identifier.d.ts.map +0 -1
  66. package/dist/types/core/utils/get-object-flag.d.ts +0 -3
  67. package/dist/types/core/utils/get-object-flag.d.ts.map +0 -1
  68. package/dist/types/core/utils/is-valid-identifier.d.ts +0 -2
  69. package/dist/types/core/utils/is-valid-identifier.d.ts.map +0 -1
  70. package/dist/types/core/utils/promise-to-result.d.ts +0 -2
  71. package/dist/types/core/utils/promise-to-result.d.ts.map +0 -1
  72. package/dist/types/core/utils/typed-array.d.ts +0 -6
  73. package/dist/types/core/utils/typed-array.d.ts.map +0 -1
  74. package/dist/types/core/utils/valid-properties.d.ts +0 -3
  75. package/dist/types/core/utils/valid-properties.d.ts.map +0 -1
  76. package/dist/types/index.d.ts +0 -18
  77. package/dist/types/index.d.ts.map +0 -1
  78. package/src/core/Serializer.ts +0 -119
  79. package/src/core/base-primitives.ts +0 -537
  80. package/src/core/compat.ts +0 -23
  81. package/src/core/constants.ts +0 -202
  82. package/src/core/constructors.ts +0 -265
  83. package/src/core/context/async-parser.ts +0 -666
  84. package/src/core/context/deserializer.ts +0 -832
  85. package/src/core/context/parser.ts +0 -335
  86. package/src/core/context/serializer.ts +0 -1524
  87. package/src/core/context/sync-parser.ts +0 -943
  88. package/src/core/cross/index.ts +0 -190
  89. package/src/core/errors.ts +0 -196
  90. package/src/core/function-string.ts +0 -31
  91. package/src/core/keys.ts +0 -17
  92. package/src/core/literals.ts +0 -43
  93. package/src/core/node.ts +0 -40
  94. package/src/core/opaque-reference.ts +0 -9
  95. package/src/core/plugin.ts +0 -105
  96. package/src/core/reference.ts +0 -66
  97. package/src/core/sequence.ts +0 -59
  98. package/src/core/special-reference.ts +0 -42
  99. package/src/core/stream.ts +0 -71
  100. package/src/core/string.ts +0 -86
  101. package/src/core/symbols.ts +0 -20
  102. package/src/core/tree/index.ts +0 -134
  103. package/src/core/types.ts +0 -377
  104. package/src/core/utils/error.ts +0 -73
  105. package/src/core/utils/get-identifier.ts +0 -18
  106. package/src/core/utils/get-object-flag.ts +0 -14
  107. package/src/core/utils/is-valid-identifier.ts +0 -12
  108. package/src/core/utils/promise-to-result.ts +0 -9
  109. package/src/core/utils/typed-array.ts +0 -56
  110. package/src/core/utils/valid-properties.ts +0 -34
  111. package/src/index.ts +0 -36
@@ -1,59 +0,0 @@
1
- import { ITERATOR_CONSTRUCTOR } from './constructors';
2
- import { SYM_ITERATOR } from './symbols';
3
-
4
- export interface Sequence {
5
- __SEROVAL_SEQUENCE__: true;
6
-
7
- v: unknown[];
8
- t: number;
9
- d: number;
10
- }
11
-
12
- export function isSequence(value: object): value is Sequence {
13
- return '__SEROVAL_SEQUENCE__' in value;
14
- }
15
-
16
- export function createSequence(
17
- values: unknown[],
18
- throwAt: number,
19
- doneAt: number,
20
- ): Sequence {
21
- return {
22
- __SEROVAL_SEQUENCE__: true,
23
-
24
- v: values,
25
- t: throwAt,
26
- d: doneAt,
27
- };
28
- }
29
-
30
- export function createSequenceFromIterable<T>(source: Iterable<T>): Sequence {
31
- const values: unknown[] = [];
32
- let throwsAt = -1;
33
- let doneAt = -1;
34
- const iterator = source[SYM_ITERATOR]();
35
-
36
- while (true) {
37
- try {
38
- const value = iterator.next();
39
- values.push(value.value);
40
- if (value.done) {
41
- doneAt = values.length - 1;
42
- break;
43
- }
44
- } catch (error) {
45
- throwsAt = values.length;
46
- values.push(error);
47
- }
48
- }
49
-
50
- return createSequence(values, throwsAt, doneAt);
51
- }
52
-
53
- const createIterator = ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
54
-
55
- export function sequenceToIterator<T>(
56
- sequence: Sequence,
57
- ): () => IterableIterator<T> {
58
- return createIterator(sequence) as unknown as () => IterableIterator<T>;
59
- }
@@ -1,42 +0,0 @@
1
- import {
2
- SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
3
- SERIALIZED_PROMISE_CONSTRUCTOR,
4
- SERIALIZED_PROMISE_FAILURE,
5
- SERIALIZED_PROMISE_SUCCESS,
6
- SERIALIZED_STREAM_CONSTRUCTOR,
7
- } from './constructors';
8
-
9
- export const ITERATOR = {};
10
-
11
- export const ASYNC_ITERATOR = {};
12
-
13
- export const enum SpecialReference {
14
- MapSentinel = 0,
15
- PromiseConstructor = 1,
16
- PromiseSuccess = 2,
17
- PromiseFailure = 3,
18
- StreamConstructor = 4,
19
- ArrayBufferConstructor = 5,
20
- }
21
-
22
- /**
23
- * Placeholder references
24
- */
25
- export const SPECIAL_REFS: Record<SpecialReference, unknown> = {
26
- [SpecialReference.MapSentinel]: {},
27
- [SpecialReference.PromiseConstructor]: {},
28
- [SpecialReference.PromiseSuccess]: {},
29
- [SpecialReference.PromiseFailure]: {},
30
- [SpecialReference.StreamConstructor]: {},
31
- [SpecialReference.ArrayBufferConstructor]: {},
32
- };
33
-
34
- export const SPECIAL_REF_STRING: Record<SpecialReference, string> = {
35
- [SpecialReference.MapSentinel]: '[]',
36
- [SpecialReference.PromiseConstructor]: SERIALIZED_PROMISE_CONSTRUCTOR,
37
- [SpecialReference.PromiseSuccess]: SERIALIZED_PROMISE_SUCCESS,
38
- [SpecialReference.PromiseFailure]: SERIALIZED_PROMISE_FAILURE,
39
- [SpecialReference.StreamConstructor]: SERIALIZED_STREAM_CONSTRUCTOR,
40
- [SpecialReference.ArrayBufferConstructor]:
41
- SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
42
- };
@@ -1,71 +0,0 @@
1
- import {
2
- ASYNC_ITERATOR_CONSTRUCTOR,
3
- PROMISE_CONSTRUCTOR,
4
- STREAM_CONSTRUCTOR,
5
- } from './constructors';
6
- import { SYM_ASYNC_ITERATOR } from './symbols';
7
-
8
- export interface StreamListener<T> {
9
- next(value: T): void;
10
- throw(value: unknown): void;
11
- return(value: T): void;
12
- }
13
-
14
- export interface Stream<T> {
15
- __SEROVAL_STREAM__: true;
16
-
17
- on(listener: StreamListener<T>): () => void;
18
-
19
- next(value: T): void;
20
- throw(value: unknown): void;
21
- return(value: T): void;
22
- }
23
-
24
- export function isStream<T>(value: object): value is Stream<T> {
25
- return '__SEROVAL_STREAM__' in value;
26
- }
27
-
28
- export function createStream<T>(): Stream<T> {
29
- return STREAM_CONSTRUCTOR() as unknown as Stream<T>;
30
- }
31
-
32
- export function createStreamFromAsyncIterable<T>(
33
- iterable: AsyncIterable<T>,
34
- ): Stream<T> {
35
- const stream = createStream<T>();
36
-
37
- const iterator = iterable[SYM_ASYNC_ITERATOR]();
38
-
39
- async function push(): Promise<void> {
40
- try {
41
- const value = await iterator.next();
42
- if (value.done) {
43
- stream.return(value.value as T);
44
- } else {
45
- stream.next(value.value);
46
- await push();
47
- }
48
- } catch (error) {
49
- stream.throw(error);
50
- }
51
- }
52
-
53
- push().catch(() => {
54
- // no-op
55
- });
56
-
57
- return stream;
58
- }
59
-
60
- const createAsyncIterable = ASYNC_ITERATOR_CONSTRUCTOR(
61
- SYM_ASYNC_ITERATOR,
62
- PROMISE_CONSTRUCTOR,
63
- );
64
-
65
- export function streamToAsyncIterable<T>(
66
- stream: Stream<T>,
67
- ): () => AsyncIterableIterator<T> {
68
- return createAsyncIterable(
69
- stream,
70
- ) as unknown as () => AsyncIterableIterator<T>;
71
- }
@@ -1,86 +0,0 @@
1
- import { NIL } from './constants';
2
-
3
- export function serializeChar(str: string): string | undefined {
4
- switch (str) {
5
- case '"':
6
- return '\\"';
7
- case '\\':
8
- return '\\\\';
9
- case '\n':
10
- return '\\n';
11
- case '\r':
12
- return '\\r';
13
- case '\b':
14
- return '\\b';
15
- case '\t':
16
- return '\\t';
17
- case '\f':
18
- return '\\f';
19
- case '<':
20
- return '\\x3C';
21
- case '\u2028':
22
- return '\\u2028';
23
- case '\u2029':
24
- return '\\u2029';
25
- default:
26
- return NIL;
27
- }
28
- }
29
-
30
- // Written by https://github.com/DylanPiercey and is distributed under the MIT license.
31
- // Creates a JavaScript double quoted string and escapes all characters
32
- // not listed as DoubleStringCharacters on
33
- // Also includes "<" to escape "</script>" and "\" to avoid invalid escapes in the output.
34
- // http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
35
- export function serializeString(str: string): string {
36
- let result = '';
37
- let lastPos = 0;
38
- let replacement: string | undefined;
39
- for (let i = 0, len = str.length; i < len; i++) {
40
- replacement = serializeChar(str[i]);
41
- if (replacement) {
42
- result += str.slice(lastPos, i) + replacement;
43
- lastPos = i + 1;
44
- }
45
- }
46
- if (lastPos === 0) {
47
- result = str;
48
- } else {
49
- result += str.slice(lastPos);
50
- }
51
- return result;
52
- }
53
-
54
- function deserializeReplacer(str: string): string {
55
- switch (str) {
56
- case '\\\\':
57
- return '\\';
58
- case '\\"':
59
- return '"';
60
- case '\\n':
61
- return '\n';
62
- case '\\r':
63
- return '\r';
64
- case '\\b':
65
- return '\b';
66
- case '\\t':
67
- return '\t';
68
- case '\\f':
69
- return '\f';
70
- case '\\x3C':
71
- return '\x3C';
72
- case '\\u2028':
73
- return '\u2028';
74
- case '\\u2029':
75
- return '\u2029';
76
- default:
77
- return str;
78
- }
79
- }
80
-
81
- export function deserializeString(str: string): string {
82
- return str.replace(
83
- /(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g,
84
- deserializeReplacer,
85
- );
86
- }
@@ -1,20 +0,0 @@
1
- export const SYM_ASYNC_ITERATOR: typeof Symbol.asyncIterator =
2
- Symbol.asyncIterator;
3
- export const SYM_HAS_INSTANCE: typeof Symbol.hasInstance = Symbol.hasInstance;
4
- export const SYM_IS_CONCAT_SPREADABLE: typeof Symbol.isConcatSpreadable =
5
- Symbol.isConcatSpreadable;
6
- export const SYM_ITERATOR: typeof Symbol.iterator = Symbol.iterator;
7
- export const SYM_MATCH: typeof Symbol.match = Symbol.match;
8
- export const SYM_MATCH_ALL: typeof Symbol.matchAll = Symbol.matchAll;
9
- export const SYM_REPLACE: typeof Symbol.replace = Symbol.replace;
10
- export const SYM_SEARCH: typeof Symbol.search = Symbol.search;
11
- export const SYM_SPECIES: typeof Symbol.species = Symbol.species;
12
- export const SYM_SPLIT: typeof Symbol.split = Symbol.split;
13
- export const SYM_TO_PRIMITIVE: typeof Symbol.toPrimitive = Symbol.toPrimitive;
14
- export const SYM_TO_STRING_TAG: typeof Symbol.toStringTag = Symbol.toStringTag;
15
- export const SYM_UNSCOPABLES: typeof Symbol.unscopables = Symbol.unscopables;
16
-
17
- // For the future
18
-
19
- // export const SYM_DISPOSE = Symbol.dispose;
20
- // export const SYM_ASYNC_DISPOSE = Symbol.asyncDispose;
@@ -1,134 +0,0 @@
1
- import {
2
- createAsyncParserContext,
3
- parseTopAsync,
4
- } from '../context/async-parser';
5
- import {
6
- createVanillaDeserializerContext,
7
- deserializeTop,
8
- } from '../context/deserializer';
9
- import type { BaseParserContextOptions } from '../context/parser';
10
- import {
11
- createVanillaSerializerContext,
12
- serializeTopVanilla,
13
- } from '../context/serializer';
14
- import { createSyncParserContext, parseTop } from '../context/sync-parser';
15
- import {
16
- type PluginAccessOptions,
17
- resolvePlugins,
18
- SerovalMode,
19
- } from '../plugin';
20
- import type { SerovalNode } from '../types';
21
- import { ALL_ENABLED } from '../compat';
22
- export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
23
- export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
24
-
25
- export function serialize<T>(
26
- source: T,
27
- options: SyncParserContextOptions = {},
28
- ): string {
29
- const plugins = resolvePlugins(options.plugins);
30
- const ctx = createSyncParserContext(SerovalMode.Vanilla, {
31
- plugins,
32
- disabledFeatures: options.disabledFeatures,
33
- });
34
- const tree = parseTop(ctx, source);
35
- const serial = createVanillaSerializerContext({
36
- plugins,
37
- features: ctx.base.features,
38
- markedRefs: ctx.base.marked,
39
- });
40
- return serializeTopVanilla(serial, tree);
41
- }
42
-
43
- export async function serializeAsync<T>(
44
- source: T,
45
- options: AsyncParserContextOptions = {},
46
- ): Promise<string> {
47
- const plugins = resolvePlugins(options.plugins);
48
- const ctx = createAsyncParserContext(SerovalMode.Vanilla, {
49
- plugins,
50
- disabledFeatures: options.disabledFeatures,
51
- });
52
- const tree = await parseTopAsync(ctx, source);
53
- const serial = createVanillaSerializerContext({
54
- plugins,
55
- features: ctx.base.features,
56
- markedRefs: ctx.base.marked,
57
- });
58
- return serializeTopVanilla(serial, tree);
59
- }
60
-
61
- export function deserialize<T>(source: string): T {
62
- return (0, eval)(source) as T;
63
- }
64
-
65
- export interface SerovalJSON {
66
- t: SerovalNode;
67
- f: number;
68
- m: number[];
69
- }
70
-
71
- export interface FromJSONOptions extends PluginAccessOptions {
72
- disabledFeatures?: number;
73
- }
74
-
75
- export function toJSON<T>(
76
- source: T,
77
- options: SyncParserContextOptions = {},
78
- ): SerovalJSON {
79
- const plugins = resolvePlugins(options.plugins);
80
- const ctx = createSyncParserContext(SerovalMode.Vanilla, {
81
- plugins,
82
- disabledFeatures: options.disabledFeatures,
83
- });
84
- return {
85
- t: parseTop(ctx, source),
86
- f: ctx.base.features,
87
- m: Array.from(ctx.base.marked),
88
- };
89
- }
90
-
91
- export async function toJSONAsync<T>(
92
- source: T,
93
- options: AsyncParserContextOptions = {},
94
- ): Promise<SerovalJSON> {
95
- const plugins = resolvePlugins(options.plugins);
96
- const ctx = createAsyncParserContext(SerovalMode.Vanilla, {
97
- plugins,
98
- disabledFeatures: options.disabledFeatures,
99
- });
100
- return {
101
- t: await parseTopAsync(ctx, source),
102
- f: ctx.base.features,
103
- m: Array.from(ctx.base.marked),
104
- };
105
- }
106
-
107
- export function compileJSON(
108
- source: SerovalJSON,
109
- options: PluginAccessOptions = {},
110
- ): string {
111
- const plugins = resolvePlugins(options.plugins);
112
- const ctx = createVanillaSerializerContext({
113
- plugins,
114
- features: source.f,
115
- markedRefs: source.m,
116
- });
117
- return serializeTopVanilla(ctx, source.t);
118
- }
119
-
120
- export function fromJSON<T>(
121
- source: SerovalJSON,
122
- options: FromJSONOptions = {},
123
- ): T {
124
- const plugins = resolvePlugins(options.plugins);
125
- const disabledFeatures = options.disabledFeatures || 0;
126
- const sourceFeatures = source.f ?? ALL_ENABLED;
127
- const ctx = createVanillaDeserializerContext({
128
- plugins,
129
- markedRefs: source.m,
130
- features: sourceFeatures & ~disabledFeatures,
131
- disabledFeatures,
132
- });
133
- return deserializeTop(ctx, source.t) as T;
134
- }