seroval 1.4.0 → 1.4.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 (68) hide show
  1. package/dist/cjs/development/index.cjs +524 -364
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +524 -364
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +2 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +2 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +2 -2
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +1 -1
  14. package/dist/types/core/constructors.d.ts.map +1 -1
  15. package/dist/types/core/context/async-parser.d.ts +5 -4
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -1
  17. package/dist/types/core/context/deserializer.d.ts +7 -2
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +3 -1
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/context/sync-parser.d.ts +5 -5
  23. package/dist/types/core/context/sync-parser.d.ts.map +1 -1
  24. package/dist/types/core/cross/index.d.ts.map +1 -1
  25. package/dist/types/core/errors.d.ts +9 -0
  26. package/dist/types/core/errors.d.ts.map +1 -1
  27. package/dist/types/core/literals.d.ts.map +1 -1
  28. package/dist/types/core/node.d.ts +1 -1
  29. package/dist/types/core/node.d.ts.map +1 -1
  30. package/dist/types/core/string.d.ts.map +1 -1
  31. package/dist/types/core/tree/index.d.ts +4 -1
  32. package/dist/types/core/tree/index.d.ts.map +1 -1
  33. package/dist/types/core/types.d.ts +2 -11
  34. package/dist/types/core/types.d.ts.map +1 -1
  35. package/package.json +2 -3
  36. package/src/core/Serializer.ts +0 -119
  37. package/src/core/base-primitives.ts +0 -515
  38. package/src/core/compat.ts +0 -21
  39. package/src/core/constants.ts +0 -187
  40. package/src/core/constructors.ts +0 -269
  41. package/src/core/context/async-parser.ts +0 -586
  42. package/src/core/context/deserializer.ts +0 -646
  43. package/src/core/context/parser.ts +0 -333
  44. package/src/core/context/serializer.ts +0 -1411
  45. package/src/core/context/sync-parser.ts +0 -833
  46. package/src/core/cross/index.ts +0 -186
  47. package/src/core/errors.ts +0 -163
  48. package/src/core/function-string.ts +0 -32
  49. package/src/core/keys.ts +0 -17
  50. package/src/core/literals.ts +0 -43
  51. package/src/core/node.ts +0 -40
  52. package/src/core/opaque-reference.ts +0 -9
  53. package/src/core/plugin.ts +0 -100
  54. package/src/core/reference.ts +0 -66
  55. package/src/core/special-reference.ts +0 -42
  56. package/src/core/stream.ts +0 -71
  57. package/src/core/string.ts +0 -84
  58. package/src/core/symbols.ts +0 -18
  59. package/src/core/tree/index.ts +0 -125
  60. package/src/core/types.ts +0 -375
  61. package/src/core/utils/error.ts +0 -73
  62. package/src/core/utils/get-identifier.ts +0 -18
  63. package/src/core/utils/get-object-flag.ts +0 -14
  64. package/src/core/utils/is-valid-identifier.ts +0 -12
  65. package/src/core/utils/iterator-to-sequence.ts +0 -44
  66. package/src/core/utils/promise-to-result.ts +0 -9
  67. package/src/core/utils/typed-array.ts +0 -56
  68. package/src/index.ts +0 -36
@@ -1,186 +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
- onParse: options.onParse,
165
- onError: options.onError,
166
- onDone: options.onDone,
167
- });
168
-
169
- startStreamParse(ctx, source);
170
-
171
- return destroyStreamParse.bind(null, ctx);
172
- }
173
-
174
- export type FromCrossJSONOptions = CrossDeserializerContextOptions;
175
-
176
- export function fromCrossJSON<T>(
177
- source: SerovalNode,
178
- options: FromCrossJSONOptions,
179
- ): T {
180
- const plugins = resolvePlugins(options.plugins);
181
- const ctx = createCrossDeserializerContext({
182
- plugins,
183
- refs: options.refs,
184
- });
185
- return deserializeTop(ctx, source) as T;
186
- }
@@ -1,163 +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: any): 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: any,
50
- ) {
51
- super(getErrorMessage(type, cause));
52
- }
53
- }
54
-
55
- export class SerovalParserError extends SerovalError {
56
- constructor(cause: any) {
57
- super('parsing', cause);
58
- }
59
- }
60
-
61
- export class SerovalSerializationError extends SerovalError {
62
- constructor(cause: any) {
63
- super('serialization', cause);
64
- }
65
- }
66
-
67
- export class SerovalDeserializationError extends SerovalError {
68
- constructor(cause: any) {
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
- }
82
-
83
- function getSpecificErrorMessage(code: SpecificErrorCodes): string {
84
- return `Seroval Error (specific: ${code})`;
85
- }
86
-
87
- export class SerovalUnsupportedTypeError extends Error {
88
- constructor(public value: unknown) {
89
- super(
90
- import.meta.env.PROD
91
- ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedType)
92
- : `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
93
-
94
- There are few workarounds for this problem:
95
- - Transform the value in a way that it can be serialized.
96
- - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`,
97
- );
98
- }
99
- }
100
-
101
- export class SerovalUnsupportedNodeError extends Error {
102
- constructor(node: SerovalNode) {
103
- super(
104
- import.meta.env.PROD
105
- ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedNode)
106
- : 'Unsupported node type "' + node.t + '".',
107
- );
108
- }
109
- }
110
-
111
- export class SerovalMissingPluginError extends Error {
112
- constructor(tag: string) {
113
- super(
114
- import.meta.env.PROD
115
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingPlugin)
116
- : 'Missing plugin for tag "' + tag + '".',
117
- );
118
- }
119
- }
120
-
121
- export class SerovalMissingInstanceError extends Error {
122
- constructor(tag: string) {
123
- super(
124
- import.meta.env.PROD
125
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingInstance)
126
- : 'Missing "' + tag + '" instance.',
127
- );
128
- }
129
- }
130
-
131
- export class SerovalMissingReferenceError extends Error {
132
- constructor(public value: unknown) {
133
- super(
134
- import.meta.env.PROD
135
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingReference)
136
- : 'Missing reference for the value "' +
137
- objectToString.call(value) +
138
- '" of type "' +
139
- typeof value +
140
- '"',
141
- );
142
- }
143
- }
144
-
145
- export class SerovalMissingReferenceForIdError extends Error {
146
- constructor(id: string) {
147
- super(
148
- import.meta.env.PROD
149
- ? getSpecificErrorMessage(SpecificErrorCodes.MissingReferenceForId)
150
- : 'Missing reference for id "' + serializeString(id) + '"',
151
- );
152
- }
153
- }
154
-
155
- export class SerovalUnknownTypedArrayError extends Error {
156
- constructor(name: string) {
157
- super(
158
- import.meta.env.PROD
159
- ? getSpecificErrorMessage(SpecificErrorCodes.UnknownTypedArray)
160
- : 'Unknown TypedArray "' + name + '"',
161
- );
162
- }
163
- }
@@ -1,32 +0,0 @@
1
-
2
- declare const T: unknown;
3
-
4
- const RETURN = () => T;
5
- const SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
6
-
7
- const IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
8
-
9
- export function createFunction(parameters: string[], body: string): string {
10
- if (IS_MODERN) {
11
- const joined =
12
- parameters.length === 1
13
- ? parameters[0]
14
- : '(' + parameters.join(',') + ')';
15
- return joined + '=>' + (body.startsWith('{') ? '(' + body + ')' : body);
16
- }
17
- return 'function(' + parameters.join(',') + '){return ' + body + '}';
18
- }
19
-
20
- export function createEffectfulFunction(
21
- parameters: string[],
22
- body: string,
23
- ): string {
24
- if (IS_MODERN) {
25
- const joined =
26
- parameters.length === 1
27
- ? parameters[0]
28
- : '(' + parameters.join(',') + ')';
29
- return joined + '=>{' + body + '}';
30
- }
31
- return 'function(' + parameters.join(',') + '){' + body + '}';
32
- }
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
- l: N['l'],
17
- c: N['c'],
18
- m: N['m'],
19
- p: N['p'],
20
- e: N['e'],
21
- a: N['a'],
22
- f: N['f'],
23
- b: N['b'],
24
- o: N['o'],
25
- ): N {
26
- return {
27
- t,
28
- i,
29
- s,
30
- l,
31
- c,
32
- m,
33
- p,
34
- e,
35
- a,
36
- f,
37
- b,
38
- o,
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,100 +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
-
9
- export const enum SerovalMode {
10
- Vanilla = 1,
11
- Cross = 2,
12
- }
13
-
14
- export interface PluginData {
15
- id: number;
16
- }
17
-
18
- export interface Plugin<Value, Node> {
19
- /**
20
- * A unique string that helps idenfity the plugin
21
- */
22
- tag: string;
23
- /**
24
- * List of dependency plugins
25
- */
26
- extends?: Plugin<any, any>[];
27
- /**
28
- * Method to test if a value is an expected value of the plugin
29
- * @param value
30
- */
31
- test(value: unknown): boolean;
32
- /**
33
- * Parsing modes
34
- */
35
- parse: {
36
- sync?: (
37
- value: Value,
38
- ctx: SyncParsePluginContext,
39
- data: PluginData,
40
- ) => Node;
41
- async?: (
42
- value: Value,
43
- ctx: AsyncParsePluginContext,
44
- data: PluginData,
45
- ) => Promise<Node>;
46
- stream?: (
47
- value: Value,
48
- ctx: StreamParsePluginContext,
49
- data: PluginData,
50
- ) => Node;
51
- };
52
- /**
53
- * Convert the parsed node into a JS string
54
- */
55
- serialize(node: Node, ctx: SerializePluginContext, data: PluginData): string;
56
- /**
57
- * Convert the parsed node into its runtime equivalent.
58
- */
59
- deserialize(
60
- node: Node,
61
- ctx: DeserializePluginContext,
62
- data: PluginData,
63
- ): Value;
64
- }
65
-
66
- export function createPlugin<Value, Node>(
67
- plugin: Plugin<Value, Node>,
68
- ): Plugin<Value, Node> {
69
- return plugin;
70
- }
71
-
72
- export interface PluginAccessOptions {
73
- plugins?: Plugin<any, any>[];
74
- }
75
-
76
- function dedupePlugins(
77
- deduped: Set<Plugin<any, any>>,
78
- plugins: Plugin<any, any>[],
79
- ): void {
80
- for (let i = 0, len = plugins.length; i < len; i++) {
81
- const current = plugins[i];
82
- if (!deduped.has(current)) {
83
- deduped.add(current);
84
- if (current.extends) {
85
- dedupePlugins(deduped, current.extends);
86
- }
87
- }
88
- }
89
- }
90
-
91
- export function resolvePlugins(
92
- plugins?: Plugin<any, any>[],
93
- ): Plugin<any, any>[] | undefined {
94
- if (plugins) {
95
- const deduped = new Set<Plugin<any, any>>();
96
- dedupePlugins(deduped, plugins);
97
- return [...deduped];
98
- }
99
- return undefined;
100
- }
@@ -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
- }
@@ -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
- };