seroval 0.4.0-alpha.8 → 0.4.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.
@@ -1,10 +1,9 @@
1
- import assert from '../assert';
2
- import { constructorCheck } from '../checks';
3
1
  import { Feature } from '../compat';
4
- import { SerializationContext, ParserContext, markRef } from '../context';
5
- import quote from '../quote';
6
- import { ErrorValue, PrimitiveValue } from '../types';
7
- import { SerovalNode, SerovalReferenceNode, SerovalNodeType } from './types';
2
+ import { ParserContext } from '../context';
3
+ import {
4
+ AsyncServerValue,
5
+ ErrorValue,
6
+ } from '../types';
8
7
 
9
8
  export function getErrorConstructor(error: ErrorValue) {
10
9
  if (error instanceof EvalError) {
@@ -29,6 +28,7 @@ export function getErrorConstructor(error: ErrorValue) {
29
28
  }
30
29
 
31
30
  export function getErrorOptions(
31
+ ctx: ParserContext,
32
32
  error: Error,
33
33
  ) {
34
34
  let options: Record<string, any> | undefined;
@@ -44,8 +44,15 @@ export function getErrorOptions(
44
44
  const names = Object.getOwnPropertyNames(error);
45
45
  for (const name of names) {
46
46
  if (name !== 'name' && name !== 'message') {
47
- options = options || {};
48
- options[name] = error[name as keyof Error];
47
+ if (name === 'stack') {
48
+ if (ctx.features & Feature.ErrorPrototypeStack) {
49
+ options = options || {};
50
+ options[name] = error[name as keyof Error];
51
+ }
52
+ } else {
53
+ options = options || {};
54
+ options[name] = error[name as keyof Error];
55
+ }
49
56
  }
50
57
  }
51
58
  return options;
@@ -63,115 +70,32 @@ export function getIterableOptions(obj: Iterable<any>) {
63
70
  return undefined;
64
71
  }
65
72
 
66
- export function isReferenceInStack(
67
- ctx: SerializationContext,
68
- node: SerovalNode,
69
- ): node is SerovalReferenceNode {
70
- return node[0] === SerovalNodeType.Reference && ctx.stack.includes(node[1]);
71
- }
72
-
73
- export function generateRef(
74
- ctx: ParserContext,
75
- current: unknown,
76
- ): number | SerovalReferenceNode {
77
- // Check if reference number already exists
78
- const ref = ctx.refs.has(current);
79
- if (ref) {
80
- // Exists, means this value is currently
81
- // being referenced
82
- const refID = ctx.refs.get(current) || 0;
83
- // Mark reference
84
- markRef(ctx, refID);
85
- return [SerovalNodeType.Reference, refID];
86
- }
87
- // Create a new reference ID
88
- const id = ctx.refs.size;
89
- ctx.refs.set(current, id);
90
- return id;
91
- }
92
-
93
- export function generateSemiPrimitiveValue(
94
- ctx: ParserContext,
95
- current: unknown,
96
- id: number,
97
- ): SerovalNode | undefined {
98
- if (typeof current === 'bigint') {
99
- assert(ctx.features & Feature.BigInt, 'Unsupported type "BigInt"');
100
- return [SerovalNodeType.BigInt, `${current}n`];
101
- }
102
- if (constructorCheck<Date>(current, Date)) {
103
- return [SerovalNodeType.Date, current.toISOString(), id];
104
- }
105
- if (constructorCheck<RegExp>(current, RegExp)) {
106
- return [SerovalNodeType.RegExp, String(current), id];
107
- }
108
- if (
109
- constructorCheck<Int8Array>(current, Int8Array)
110
- || constructorCheck<Int16Array>(current, Int16Array)
111
- || constructorCheck<Int32Array>(current, Int32Array)
112
- || constructorCheck<Uint8Array>(current, Uint8Array)
113
- || constructorCheck<Uint16Array>(current, Uint16Array)
114
- || constructorCheck<Uint32Array>(current, Uint32Array)
115
- || constructorCheck<Uint8ClampedArray>(current, Uint8ClampedArray)
116
- || constructorCheck<Float32Array>(current, Float32Array)
117
- || constructorCheck<Float64Array>(current, Float64Array)
118
- ) {
119
- const constructor = current.constructor.name;
120
- assert(ctx.features & Feature.TypedArray, `Unsupported value type "${constructor}"`);
121
- return [SerovalNodeType.TypedArray, [constructor, current.toString(), current.byteOffset], id];
122
- }
123
- if (
124
- constructorCheck<BigInt64Array>(current, BigInt64Array)
125
- || constructorCheck<BigUint64Array>(current, BigUint64Array)
126
- ) {
127
- const constructor = current.constructor.name;
128
- assert(
129
- ctx.features & (Feature.BigIntTypedArray),
130
- `Unsupported value type "${constructor}"`,
131
- );
132
- let result = '';
133
- const cap = current.length - 1;
134
- for (let i = 0; i < cap; i++) {
135
- result += `${current[i]}n,`;
136
- }
137
- result += `"${current[cap]}"`;
138
- return [
139
- SerovalNodeType.BigIntTypedArray,
140
- [constructor, result, current.byteOffset],
141
- id,
142
- ];
143
- }
144
- return undefined;
145
- }
146
-
147
- export function serializePrimitive(
148
- value: PrimitiveValue,
149
- ): string | number | null {
150
- // Shortened forms
151
- if (value === true) {
152
- return '!0';
153
- }
154
- if (value === false) {
155
- return '!1';
156
- }
157
- if (value === undefined) {
158
- return 'void 0';
159
- }
160
- if (value === null) {
161
- return null;
162
- }
163
- if (typeof value === 'string') {
164
- return quote(value);
165
- }
166
- // negative 0 isn't the same as 0
167
- if (Object.is(value, -0)) {
168
- return '-0';
169
- }
170
- if (Object.is(value, Infinity)) {
171
- return '1/0';
172
- }
173
- if (Object.is(value, -Infinity)) {
174
- return '-1/0';
175
- }
176
- return value;
73
+ export function isIterable(
74
+ value: unknown,
75
+ ): value is Iterable<AsyncServerValue> {
76
+ if (!value || typeof value !== 'object') {
77
+ return false;
78
+ }
79
+ if (Array.isArray(value)) {
80
+ return false;
81
+ }
82
+ switch (value.constructor) {
83
+ case Map:
84
+ case Set:
85
+ case Int8Array:
86
+ case Int16Array:
87
+ case Int32Array:
88
+ case Uint8Array:
89
+ case Uint16Array:
90
+ case Uint32Array:
91
+ case Uint8ClampedArray:
92
+ case Float32Array:
93
+ case Float64Array:
94
+ case BigInt64Array:
95
+ case BigUint64Array:
96
+ return false;
97
+ default:
98
+ break;
99
+ }
100
+ return Symbol.iterator in value;
177
101
  }