seroval 1.5.6 → 1.6.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 (117) hide show
  1. package/dist/dev/index.cjs +2954 -0
  2. package/dist/dev/index.d.cts +761 -0
  3. package/dist/dev/index.d.ts +761 -0
  4. package/dist/dev/index.js +2917 -0
  5. package/dist/index.cjs +2947 -0
  6. package/dist/index.d.cts +761 -0
  7. package/dist/index.d.ts +761 -0
  8. package/dist/index.js +2910 -0
  9. package/package.json +23 -21
  10. package/dist/cjs/development/index.cjs +0 -4232
  11. package/dist/cjs/development/index.cjs.map +0 -7
  12. package/dist/cjs/production/index.cjs +0 -3
  13. package/dist/esm/development/index.mjs +0 -4209
  14. package/dist/esm/development/index.mjs.map +0 -7
  15. package/dist/esm/production/index.mjs +0 -3
  16. package/dist/types/core/Serializer.d.ts +0 -28
  17. package/dist/types/core/Serializer.d.ts.map +0 -1
  18. package/dist/types/core/base-primitives.d.ts +0 -28
  19. package/dist/types/core/base-primitives.d.ts.map +0 -1
  20. package/dist/types/core/compat.d.ts +0 -15
  21. package/dist/types/core/compat.d.ts.map +0 -1
  22. package/dist/types/core/constants.d.ts +0 -105
  23. package/dist/types/core/constants.d.ts.map +0 -1
  24. package/dist/types/core/constructors.d.ts +0 -50
  25. package/dist/types/core/constructors.d.ts.map +0 -1
  26. package/dist/types/core/context/async-parser.d.ts +0 -20
  27. package/dist/types/core/context/async-parser.d.ts.map +0 -1
  28. package/dist/types/core/context/deserializer.d.ts +0 -52
  29. package/dist/types/core/context/deserializer.d.ts.map +0 -1
  30. package/dist/types/core/context/parser.d.ts +0 -62
  31. package/dist/types/core/context/parser.d.ts.map +0 -1
  32. package/dist/types/core/context/serializer.d.ts +0 -103
  33. package/dist/types/core/context/serializer.d.ts.map +0 -1
  34. package/dist/types/core/context/sync-parser.d.ts +0 -61
  35. package/dist/types/core/context/sync-parser.d.ts.map +0 -1
  36. package/dist/types/core/cross/index.d.ts +0 -24
  37. package/dist/types/core/cross/index.d.ts.map +0 -1
  38. package/dist/types/core/errors.d.ts +0 -47
  39. package/dist/types/core/errors.d.ts.map +0 -1
  40. package/dist/types/core/function-string.d.ts +0 -3
  41. package/dist/types/core/function-string.d.ts.map +0 -1
  42. package/dist/types/core/keys.d.ts +0 -4
  43. package/dist/types/core/keys.d.ts.map +0 -1
  44. package/dist/types/core/literals.d.ts +0 -10
  45. package/dist/types/core/literals.d.ts.map +0 -1
  46. package/dist/types/core/node.d.ts +0 -8
  47. package/dist/types/core/node.d.ts.map +0 -1
  48. package/dist/types/core/opaque-reference.d.ts +0 -9
  49. package/dist/types/core/opaque-reference.d.ts.map +0 -1
  50. package/dist/types/core/plugin.d.ts +0 -52
  51. package/dist/types/core/plugin.d.ts.map +0 -1
  52. package/dist/types/core/reference.d.ts +0 -6
  53. package/dist/types/core/reference.d.ts.map +0 -1
  54. package/dist/types/core/sequence.d.ts +0 -11
  55. package/dist/types/core/sequence.d.ts.map +0 -1
  56. package/dist/types/core/special-reference.d.ts +0 -16
  57. package/dist/types/core/special-reference.d.ts.map +0 -1
  58. package/dist/types/core/stream.d.ts +0 -17
  59. package/dist/types/core/stream.d.ts.map +0 -1
  60. package/dist/types/core/string.d.ts +0 -4
  61. package/dist/types/core/string.d.ts.map +0 -1
  62. package/dist/types/core/symbols.d.ts +0 -14
  63. package/dist/types/core/symbols.d.ts.map +0 -1
  64. package/dist/types/core/tree/index.d.ts +0 -21
  65. package/dist/types/core/tree/index.d.ts.map +0 -1
  66. package/dist/types/core/types.d.ts +0 -233
  67. package/dist/types/core/types.d.ts.map +0 -1
  68. package/dist/types/core/utils/error.d.ts +0 -6
  69. package/dist/types/core/utils/error.d.ts.map +0 -1
  70. package/dist/types/core/utils/get-identifier.d.ts +0 -2
  71. package/dist/types/core/utils/get-identifier.d.ts.map +0 -1
  72. package/dist/types/core/utils/get-object-flag.d.ts +0 -3
  73. package/dist/types/core/utils/get-object-flag.d.ts.map +0 -1
  74. package/dist/types/core/utils/is-valid-identifier.d.ts +0 -2
  75. package/dist/types/core/utils/is-valid-identifier.d.ts.map +0 -1
  76. package/dist/types/core/utils/promise-to-result.d.ts +0 -2
  77. package/dist/types/core/utils/promise-to-result.d.ts.map +0 -1
  78. package/dist/types/core/utils/typed-array.d.ts +0 -6
  79. package/dist/types/core/utils/typed-array.d.ts.map +0 -1
  80. package/dist/types/core/utils/valid-properties.d.ts +0 -3
  81. package/dist/types/core/utils/valid-properties.d.ts.map +0 -1
  82. package/dist/types/index.d.ts +0 -18
  83. package/dist/types/index.d.ts.map +0 -1
  84. package/src/core/Serializer.ts +0 -119
  85. package/src/core/base-primitives.ts +0 -537
  86. package/src/core/compat.ts +0 -23
  87. package/src/core/constants.ts +0 -202
  88. package/src/core/constructors.ts +0 -265
  89. package/src/core/context/async-parser.ts +0 -666
  90. package/src/core/context/deserializer.ts +0 -832
  91. package/src/core/context/parser.ts +0 -335
  92. package/src/core/context/serializer.ts +0 -1524
  93. package/src/core/context/sync-parser.ts +0 -943
  94. package/src/core/cross/index.ts +0 -190
  95. package/src/core/errors.ts +0 -196
  96. package/src/core/function-string.ts +0 -31
  97. package/src/core/keys.ts +0 -17
  98. package/src/core/literals.ts +0 -43
  99. package/src/core/node.ts +0 -40
  100. package/src/core/opaque-reference.ts +0 -9
  101. package/src/core/plugin.ts +0 -105
  102. package/src/core/reference.ts +0 -66
  103. package/src/core/sequence.ts +0 -59
  104. package/src/core/special-reference.ts +0 -42
  105. package/src/core/stream.ts +0 -71
  106. package/src/core/string.ts +0 -86
  107. package/src/core/symbols.ts +0 -20
  108. package/src/core/tree/index.ts +0 -134
  109. package/src/core/types.ts +0 -377
  110. package/src/core/utils/error.ts +0 -73
  111. package/src/core/utils/get-identifier.ts +0 -18
  112. package/src/core/utils/get-object-flag.ts +0 -14
  113. package/src/core/utils/is-valid-identifier.ts +0 -12
  114. package/src/core/utils/promise-to-result.ts +0 -9
  115. package/src/core/utils/typed-array.ts +0 -56
  116. package/src/core/utils/valid-properties.ts +0 -34
  117. package/src/index.ts +0 -36
@@ -1,190 +0,0 @@
1
- import type { AsyncParserContextOptions } from '../context/async-parser';
2
- import {
3
- createAsyncParserContext,
4
- parseTopAsync,
5
- } from '../context/async-parser';
6
- import type { CrossDeserializerContextOptions } from '../context/deserializer';
7
- import {
8
- createCrossDeserializerContext,
9
- deserializeTop,
10
- } from '../context/deserializer';
11
- import type { CrossContextOptions } from '../context/serializer';
12
- import {
13
- createCrossSerializerContext,
14
- serializeTopCross,
15
- } from '../context/serializer';
16
- import type {
17
- StreamParserContextOptions,
18
- SyncParserContextOptions,
19
- } from '../context/sync-parser';
20
- import {
21
- createStreamParserContext,
22
- createSyncParserContext,
23
- destroyStreamParse,
24
- parseTop,
25
- startStreamParse,
26
- } from '../context/sync-parser';
27
- import { resolvePlugins, SerovalMode } from '../plugin';
28
- import type { SerovalNode } from '../types';
29
-
30
- export interface CrossSerializeOptions
31
- extends SyncParserContextOptions,
32
- CrossContextOptions {}
33
-
34
- export function crossSerialize<T>(
35
- source: T,
36
- options: CrossSerializeOptions = {},
37
- ): string {
38
- const plugins = resolvePlugins(options.plugins);
39
- const ctx = createSyncParserContext(SerovalMode.Cross, {
40
- plugins,
41
- disabledFeatures: options.disabledFeatures,
42
- refs: options.refs,
43
- });
44
- const tree = parseTop(ctx, source);
45
- const serial = createCrossSerializerContext({
46
- plugins,
47
- features: ctx.base.features,
48
- scopeId: options.scopeId,
49
- markedRefs: ctx.base.marked,
50
- });
51
- return serializeTopCross(serial, tree);
52
- }
53
-
54
- export interface CrossSerializeAsyncOptions
55
- extends AsyncParserContextOptions,
56
- CrossContextOptions {}
57
-
58
- export async function crossSerializeAsync<T>(
59
- source: T,
60
- options: CrossSerializeAsyncOptions = {},
61
- ): Promise<string> {
62
- const plugins = resolvePlugins(options.plugins);
63
- const ctx = createAsyncParserContext(SerovalMode.Cross, {
64
- plugins,
65
- disabledFeatures: options.disabledFeatures,
66
- refs: options.refs,
67
- });
68
- const tree = await parseTopAsync(ctx, source);
69
- const serial = createCrossSerializerContext({
70
- plugins,
71
- features: ctx.base.features,
72
- scopeId: options.scopeId,
73
- markedRefs: ctx.base.marked,
74
- });
75
- return serializeTopCross(serial, tree);
76
- }
77
-
78
- export type ToCrossJSONOptions = SyncParserContextOptions;
79
-
80
- export function toCrossJSON<T>(
81
- source: T,
82
- options: ToCrossJSONOptions = {},
83
- ): SerovalNode {
84
- const plugins = resolvePlugins(options.plugins);
85
- const ctx = createSyncParserContext(SerovalMode.Cross, {
86
- plugins,
87
- disabledFeatures: options.disabledFeatures,
88
- refs: options.refs,
89
- });
90
- return parseTop(ctx, source);
91
- }
92
-
93
- export type ToCrossJSONAsyncOptions = AsyncParserContextOptions;
94
-
95
- export async function toCrossJSONAsync<T>(
96
- source: T,
97
- options: ToCrossJSONAsyncOptions = {},
98
- ): Promise<SerovalNode> {
99
- const plugins = resolvePlugins(options.plugins);
100
- const ctx = createAsyncParserContext(SerovalMode.Cross, {
101
- plugins,
102
- disabledFeatures: options.disabledFeatures,
103
- refs: options.refs,
104
- });
105
- return await parseTopAsync(ctx, source);
106
- }
107
-
108
- export interface CrossSerializeStreamOptions
109
- extends Omit<StreamParserContextOptions, 'onParse'>,
110
- CrossContextOptions {
111
- onSerialize: (data: string, initial: boolean) => void;
112
- }
113
-
114
- export function crossSerializeStream<T>(
115
- source: T,
116
- options: CrossSerializeStreamOptions,
117
- ): () => void {
118
- const plugins = resolvePlugins(options.plugins);
119
- const ctx = createStreamParserContext({
120
- plugins,
121
- refs: options.refs,
122
- disabledFeatures: options.disabledFeatures,
123
- onParse(node, initial): void {
124
- const serial = createCrossSerializerContext({
125
- plugins,
126
- features: ctx.base.features,
127
- scopeId: options.scopeId,
128
- markedRefs: ctx.base.marked,
129
- });
130
-
131
- let serialized: string;
132
-
133
- try {
134
- serialized = serializeTopCross(serial, node);
135
- } catch (err) {
136
- if (options.onError) {
137
- options.onError(err);
138
- }
139
- return;
140
- }
141
-
142
- options.onSerialize(serialized, initial);
143
- },
144
- onError: options.onError,
145
- onDone: options.onDone,
146
- });
147
-
148
- startStreamParse(ctx, source);
149
-
150
- return destroyStreamParse.bind(null, ctx);
151
- }
152
-
153
- export type ToCrossJSONStreamOptions = StreamParserContextOptions;
154
-
155
- export function toCrossJSONStream<T>(
156
- source: T,
157
- options: ToCrossJSONStreamOptions,
158
- ): () => void {
159
- const plugins = resolvePlugins(options.plugins);
160
- const ctx = createStreamParserContext({
161
- plugins,
162
- refs: options.refs,
163
- disabledFeatures: options.disabledFeatures,
164
- depthLimit: options.depthLimit,
165
- onParse: options.onParse,
166
- onError: options.onError,
167
- onDone: options.onDone,
168
- });
169
-
170
- startStreamParse(ctx, source);
171
-
172
- return destroyStreamParse.bind(null, ctx);
173
- }
174
-
175
- export type FromCrossJSONOptions = CrossDeserializerContextOptions;
176
-
177
- export function fromCrossJSON<T>(
178
- source: SerovalNode,
179
- options: FromCrossJSONOptions,
180
- ): T {
181
- const plugins = resolvePlugins(options.plugins);
182
- const ctx = createCrossDeserializerContext({
183
- plugins,
184
- refs: options.refs,
185
- features: options.features,
186
- disabledFeatures: options.disabledFeatures,
187
- depthLimit: options.depthLimit,
188
- });
189
- return deserializeTop(ctx, source) as T;
190
- }
@@ -1,196 +0,0 @@
1
- /// <reference types="pridepack/env" />
2
-
3
- import { serializeString } from './string';
4
- import type { SerovalNode } from './types';
5
-
6
- const { toString: objectToString } = /* @__PURE__ */ Object.prototype;
7
-
8
- const enum StepErrorCodes {
9
- Parse = 1,
10
- Serialize = 2,
11
- Deserialize = 3,
12
- }
13
-
14
- function getErrorMessageDev(type: string, cause: unknown): string {
15
- if (cause instanceof Error) {
16
- return `Seroval caught an error during the ${type} process.
17
-
18
- ${cause.name}
19
- ${cause.message}
20
-
21
- - For more information, please check the "cause" property of this error.
22
- - If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
23
- }
24
- return `Seroval caught an error during the ${type} process.
25
-
26
- "${objectToString.call(cause)}"
27
-
28
- For more information, please check the "cause" property of this error.`;
29
- }
30
-
31
- const STEP_ERROR_CODES: Record<string, StepErrorCodes> = {
32
- parsing: StepErrorCodes.Parse,
33
- serialization: StepErrorCodes.Serialize,
34
- deserialization: StepErrorCodes.Deserialize,
35
- };
36
-
37
- function getErrorMessageProd(type: string): string {
38
- return `Seroval Error (step: ${STEP_ERROR_CODES[type]})`;
39
- }
40
-
41
- const getErrorMessage = (type: string, cause: any) =>
42
- import.meta.env.PROD
43
- ? getErrorMessageProd(type)
44
- : getErrorMessageDev(type, cause);
45
-
46
- export class SerovalError extends Error {
47
- constructor(
48
- type: string,
49
- public cause: unknown,
50
- ) {
51
- super(getErrorMessage(type, cause));
52
- }
53
- }
54
-
55
- export class SerovalParserError extends SerovalError {
56
- constructor(cause: unknown) {
57
- super('parsing', cause);
58
- }
59
- }
60
-
61
- export class SerovalSerializationError extends SerovalError {
62
- constructor(cause: unknown) {
63
- super('serialization', cause);
64
- }
65
- }
66
-
67
- export class SerovalDeserializationError extends SerovalError {
68
- constructor(cause: unknown) {
69
- super('deserialization', cause);
70
- }
71
- }
72
-
73
- const enum SpecificErrorCodes {
74
- UnsupportedType = 1,
75
- UnsupportedNode = 2,
76
- MissingPlugin = 3,
77
- MissingInstance = 4,
78
- MissingReference = 5,
79
- MissingReferenceForId = 6,
80
- UnknownTypedArray = 7,
81
- MalformedNode = 8,
82
- ConflictedNodeId = 9,
83
- DepthLimit = 10,
84
- }
85
-
86
- function getSpecificErrorMessage(code: SpecificErrorCodes): string {
87
- return `Seroval Error (specific: ${code})`;
88
- }
89
-
90
- export class SerovalUnsupportedTypeError extends Error {
91
- constructor(public value: unknown) {
92
- super(
93
- import.meta.env.PROD
94
- ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedType)
95
- : `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
96
-
97
- There are few workarounds for this problem:
98
- - Transform the value in a way that it can be serialized.
99
- - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`,
100
- );
101
- }
102
- }
103
-
104
- export class SerovalUnsupportedNodeError extends Error {
105
- constructor(node: SerovalNode) {
106
- super(
107
- import.meta.env.PROD
108
- ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedNode)
109
- : 'Unsupported node type "' + node.t + '".',
110
- );
111
- }
112
- }
113
-
114
- export class SerovalMissingPluginError extends Error {
115
- constructor(tag: string) {
116
- super(
117
- import.meta.env.PROD
118
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingPlugin)
119
- : 'Missing plugin for tag "' + tag + '".',
120
- );
121
- }
122
- }
123
-
124
- export class SerovalMissingInstanceError extends Error {
125
- constructor(tag: string) {
126
- super(
127
- import.meta.env.PROD
128
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingInstance)
129
- : 'Missing "' + tag + '" instance.',
130
- );
131
- }
132
- }
133
-
134
- export class SerovalMissingReferenceError extends Error {
135
- constructor(public value: unknown) {
136
- super(
137
- import.meta.env.PROD
138
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingReference)
139
- : 'Missing reference for the value "' +
140
- objectToString.call(value) +
141
- '" of type "' +
142
- typeof value +
143
- '"',
144
- );
145
- }
146
- }
147
-
148
- export class SerovalMissingReferenceForIdError extends Error {
149
- constructor(id: string) {
150
- super(
151
- import.meta.env.PROD
152
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingReferenceForId)
153
- : 'Missing reference for id "' + serializeString(id) + '"',
154
- );
155
- }
156
- }
157
-
158
- export class SerovalUnknownTypedArrayError extends Error {
159
- constructor(name: string) {
160
- super(
161
- import.meta.env.PROD
162
- ? getSpecificErrorMessage(SpecificErrorCodes.UnknownTypedArray)
163
- : 'Unknown TypedArray "' + name + '"',
164
- );
165
- }
166
- }
167
-
168
- export class SerovalMalformedNodeError extends Error {
169
- constructor(node: SerovalNode) {
170
- super(
171
- import.meta.env.PROD
172
- ? getSpecificErrorMessage(SpecificErrorCodes.MalformedNode)
173
- : 'Malformed node type "' + node.t + '".',
174
- );
175
- }
176
- }
177
-
178
- export class SerovalConflictedNodeIdError extends Error {
179
- constructor(node: SerovalNode) {
180
- super(
181
- import.meta.env.PROD
182
- ? getSpecificErrorMessage(SpecificErrorCodes.ConflictedNodeId)
183
- : 'Conflicted node id "' + node.i + '".',
184
- );
185
- }
186
- }
187
-
188
- export class SerovalDepthLimitError extends Error {
189
- constructor(limit: number) {
190
- super(
191
- import.meta.env.PROD
192
- ? getSpecificErrorMessage(SpecificErrorCodes.ConflictedNodeId)
193
- : 'Depth limit of ' + limit + ' reached',
194
- );
195
- }
196
- }
@@ -1,31 +0,0 @@
1
- declare const T: unknown;
2
-
3
- const RETURN = () => T;
4
- const SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
5
-
6
- const IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
7
-
8
- export function createFunction(parameters: string[], body: string): string {
9
- if (IS_MODERN) {
10
- const joined =
11
- parameters.length === 1
12
- ? parameters[0]
13
- : '(' + parameters.join(',') + ')';
14
- return joined + '=>' + (body.startsWith('{') ? '(' + body + ')' : body);
15
- }
16
- return 'function(' + parameters.join(',') + '){return ' + body + '}';
17
- }
18
-
19
- export function createEffectfulFunction(
20
- parameters: string[],
21
- body: string,
22
- ): string {
23
- if (IS_MODERN) {
24
- const joined =
25
- parameters.length === 1
26
- ? parameters[0]
27
- : '(' + parameters.join(',') + ')';
28
- return joined + '=>{' + body + '}';
29
- }
30
- return 'function(' + parameters.join(',') + '){' + body + '}';
31
- }
package/src/core/keys.ts DELETED
@@ -1,17 +0,0 @@
1
- import { serializeString } from './string';
2
-
3
- // Used for mapping isomorphic references
4
- export const REFERENCES_KEY = '__SEROVAL_REFS__';
5
-
6
- export const GLOBAL_CONTEXT_REFERENCES = '$R';
7
-
8
- const GLOBAL_CONTEXT_R = `self.${GLOBAL_CONTEXT_REFERENCES}`;
9
-
10
- export function getCrossReferenceHeader(id?: string): string {
11
- if (id == null) {
12
- return `${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||[]`;
13
- }
14
- return `(${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||{})["${serializeString(
15
- id,
16
- )}"]=[]`;
17
- }
@@ -1,43 +0,0 @@
1
- import { NIL, SerovalConstant, SerovalNodeType } from './constants';
2
- import { createSerovalNode } from './node';
3
- import type { SerovalConstantNode } from './types';
4
-
5
- function createConstantNode(value: SerovalConstant): SerovalConstantNode {
6
- return createSerovalNode(
7
- SerovalNodeType.Constant,
8
- NIL,
9
- value,
10
- NIL,
11
- NIL,
12
- NIL,
13
- NIL,
14
- NIL,
15
- NIL,
16
- NIL,
17
- NIL,
18
- NIL,
19
- );
20
- }
21
-
22
- export const TRUE_NODE = /* @__PURE__ */ createConstantNode(
23
- SerovalConstant.True,
24
- );
25
- export const FALSE_NODE = /* @__PURE__ */ createConstantNode(
26
- SerovalConstant.False,
27
- );
28
- export const UNDEFINED_NODE = /* @__PURE__ */ createConstantNode(
29
- SerovalConstant.Undefined,
30
- );
31
- export const NULL_NODE = /* @__PURE__ */ createConstantNode(
32
- SerovalConstant.Null,
33
- );
34
- export const NEG_ZERO_NODE = /* @__PURE__ */ createConstantNode(
35
- SerovalConstant.NegZero,
36
- );
37
- export const INFINITY_NODE = /* @__PURE__ */ createConstantNode(
38
- SerovalConstant.Inf,
39
- );
40
- export const NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(
41
- SerovalConstant.NegInf,
42
- );
43
- export const NAN_NODE = /* @__PURE__ */ createConstantNode(SerovalConstant.Nan);
package/src/core/node.ts DELETED
@@ -1,40 +0,0 @@
1
- import type { SerovalNodeType } from './constants';
2
- import type { SerovalNode } from './types';
3
-
4
- type ExtractedNodeType<T extends SerovalNodeType> = Extract<
5
- SerovalNode,
6
- { t: T }
7
- >;
8
-
9
- export function createSerovalNode<
10
- T extends SerovalNodeType,
11
- N extends ExtractedNodeType<T>,
12
- >(
13
- t: T,
14
- i: N['i'],
15
- s: N['s'],
16
- c: N['c'],
17
- m: N['m'],
18
- p: N['p'],
19
- e: N['e'],
20
- a: N['a'],
21
- f: N['f'],
22
- b: N['b'],
23
- o: N['o'],
24
- l: N['l'],
25
- ): N {
26
- return {
27
- t,
28
- i,
29
- s,
30
- c,
31
- m,
32
- p,
33
- e,
34
- a,
35
- f,
36
- b,
37
- o,
38
- l,
39
- } as N;
40
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * An opaque reference allows hiding values from the serializer.
3
- */
4
- export class OpaqueReference<V, R = undefined> {
5
- constructor(
6
- public readonly value: V,
7
- public readonly replacement?: R,
8
- ) {}
9
- }
@@ -1,105 +0,0 @@
1
- import type { AsyncParsePluginContext } from './context/async-parser';
2
- import type { DeserializePluginContext } from './context/deserializer';
3
- import type { SerializePluginContext } from './context/serializer';
4
- import type {
5
- StreamParsePluginContext,
6
- SyncParsePluginContext,
7
- } from './context/sync-parser';
8
- import type { SerovalNode } from './types';
9
-
10
- export const enum SerovalMode {
11
- Vanilla = 1,
12
- Cross = 2,
13
- }
14
-
15
- export interface PluginData {
16
- id: number;
17
- }
18
-
19
- export type PluginInfo = {
20
- [key: string]: SerovalNode;
21
- };
22
-
23
- export interface Plugin<Value, Info extends PluginInfo> {
24
- /**
25
- * A unique string that helps idenfity the plugin
26
- */
27
- tag: string;
28
- /**
29
- * List of dependency plugins
30
- */
31
- extends?: Plugin<any, any>[];
32
- /**
33
- * Method to test if a value is an expected value of the plugin
34
- * @param value
35
- */
36
- test(value: unknown): boolean;
37
- /**
38
- * Parsing modes
39
- */
40
- parse: {
41
- sync?: (
42
- value: Value,
43
- ctx: SyncParsePluginContext,
44
- data: PluginData,
45
- ) => Info;
46
- async?: (
47
- value: Value,
48
- ctx: AsyncParsePluginContext,
49
- data: PluginData,
50
- ) => Promise<Info>;
51
- stream?: (
52
- value: Value,
53
- ctx: StreamParsePluginContext,
54
- data: PluginData,
55
- ) => Info;
56
- };
57
- /**
58
- * Convert the parsed node into a JS string
59
- */
60
- serialize(node: Info, ctx: SerializePluginContext, data: PluginData): string;
61
- /**
62
- * Convert the parsed node into its runtime equivalent.
63
- */
64
- deserialize(
65
- node: Info,
66
- ctx: DeserializePluginContext,
67
- data: PluginData,
68
- ): Value;
69
- }
70
-
71
- export function createPlugin<Value, Info extends PluginInfo>(
72
- plugin: Plugin<Value, Info>,
73
- ): Plugin<Value, Info> {
74
- return plugin;
75
- }
76
-
77
- export interface PluginAccessOptions {
78
- plugins?: Plugin<any, any>[];
79
- }
80
-
81
- function dedupePlugins(
82
- deduped: Set<Plugin<any, any>>,
83
- plugins: Plugin<any, any>[],
84
- ): void {
85
- for (let i = 0, len = plugins.length; i < len; i++) {
86
- const current = plugins[i];
87
- if (!deduped.has(current)) {
88
- deduped.add(current);
89
- if (current.extends) {
90
- dedupePlugins(deduped, current.extends);
91
- }
92
- }
93
- }
94
- }
95
-
96
- export function resolvePlugins(
97
- plugins?: Plugin<any, any>[],
98
- ): Plugin<any, any>[] | undefined {
99
- if (plugins) {
100
- const deduped = new Set<Plugin<any, any>>();
101
- dedupePlugins(deduped, plugins);
102
- return [...deduped];
103
- }
104
- return undefined;
105
- }
@@ -1,66 +0,0 @@
1
- import {
2
- SerovalMissingReferenceError,
3
- SerovalMissingReferenceForIdError,
4
- } from '..';
5
- import { REFERENCES_KEY } from './keys';
6
-
7
- const REFERENCE = new Map<unknown, string>();
8
- const INV_REFERENCE = new Map<string, unknown>();
9
-
10
- export function createReference<T>(id: string, value: T): T {
11
- REFERENCE.set(value, id);
12
- INV_REFERENCE.set(id, value);
13
- return value;
14
- }
15
-
16
- export function hasReferenceID<T>(value: T): boolean {
17
- return REFERENCE.has(value);
18
- }
19
-
20
- export function hasReference(id: string): boolean {
21
- return INV_REFERENCE.has(id);
22
- }
23
-
24
- export function getReferenceID<T>(value: T): string {
25
- if (hasReferenceID(value)) {
26
- return REFERENCE.get(value)!;
27
- }
28
- throw new SerovalMissingReferenceError(value);
29
- }
30
-
31
- export function getReference<T>(id: string): T {
32
- if (hasReference(id)) {
33
- return INV_REFERENCE.get(id) as T;
34
- }
35
- throw new SerovalMissingReferenceForIdError(id);
36
- }
37
-
38
- if (typeof globalThis !== 'undefined') {
39
- Object.defineProperty(globalThis, REFERENCES_KEY, {
40
- value: INV_REFERENCE,
41
- configurable: true,
42
- writable: false,
43
- enumerable: false,
44
- });
45
- } else if (typeof window !== 'undefined') {
46
- Object.defineProperty(window, REFERENCES_KEY, {
47
- value: INV_REFERENCE,
48
- configurable: true,
49
- writable: false,
50
- enumerable: false,
51
- });
52
- } else if (typeof self !== 'undefined') {
53
- Object.defineProperty(self, REFERENCES_KEY, {
54
- value: INV_REFERENCE,
55
- configurable: true,
56
- writable: false,
57
- enumerable: false,
58
- });
59
- } else if (typeof global !== 'undefined') {
60
- Object.defineProperty(global, REFERENCES_KEY, {
61
- value: INV_REFERENCE,
62
- configurable: true,
63
- writable: false,
64
- enumerable: false,
65
- });
66
- }