Skip to content

Commit 1a5b39b

Browse files
committed
Add cleanups
1 parent 67caa5a commit 1a5b39b

File tree

10 files changed

+158
-90
lines changed

10 files changed

+158
-90
lines changed

packages/seroval/src/core/base-primitives.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import type { WellKnownSymbols } from './constants';
2-
import { INV_SYMBOL_REF, NIL, SerovalNodeType } from './constants';
1+
import type {
2+
BigIntTypedArrayValue,
3+
TypedArrayValue,
4+
WellKnownSymbols,
5+
} from './constants';
6+
import {
7+
getBigIntTypedArrayTag,
8+
getTypedArrayTag,
9+
INV_SYMBOL_REF,
10+
NIL,
11+
SerovalNodeType,
12+
} from './constants';
313
import {
414
INFINITY_NODE,
515
NAN_NODE,
@@ -41,10 +51,6 @@ import type {
4151
} from './types';
4252
import { getErrorConstructor } from './utils/error';
4353
import { getObjectFlag } from './utils/get-object-flag';
44-
import type {
45-
BigIntTypedArrayValue,
46-
TypedArrayValue,
47-
} from './utils/typed-array';
4854

4955
export function createNumberNode(
5056
value: number,
@@ -276,8 +282,8 @@ export function createTypedArrayNode(
276282
return createSerovalNode(
277283
SerovalNodeType.TypedArray,
278284
id,
285+
getTypedArrayTag(current),
279286
NIL,
280-
current.constructor.name,
281287
NIL,
282288
NIL,
283289
NIL,
@@ -297,8 +303,8 @@ export function createBigIntTypedArrayNode(
297303
return createSerovalNode(
298304
SerovalNodeType.BigIntTypedArray,
299305
id,
306+
getBigIntTypedArrayTag(current),
300307
NIL,
301-
current.constructor.name,
302308
NIL,
303309
NIL,
304310
NIL,

packages/seroval/src/core/constants.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SerovalUnknownTypedArrayError } from './errors';
12
import {
23
SYM_ASYNC_ITERATOR,
34
SYM_HAS_INSTANCE,
@@ -200,3 +201,120 @@ export const ERROR_CONSTRUCTOR: Record<ErrorConstructorTag, ErrorConstructors> =
200201
[ErrorConstructorTag.TypeError]: TypeError,
201202
[ErrorConstructorTag.URIError]: URIError,
202203
};
204+
205+
export function isWellKnownSymbol(value: symbol): value is WellKnownSymbols {
206+
return value in INV_SYMBOL_REF;
207+
}
208+
209+
export const DEFAULT_DEPTH_LIMIT = 64;
210+
211+
export const enum TypedArrayTag {
212+
Int8Array = 1,
213+
Int16Array = 2,
214+
Int32Array = 3,
215+
Uint8Array = 4,
216+
Uint16Array = 5,
217+
Uint32Array = 6,
218+
Uint8ClampedArray = 7,
219+
Float32Array = 8,
220+
Float64Array = 9,
221+
}
222+
223+
export const enum BigIntTypedArrayTag {
224+
BigInt64Array = 1,
225+
BigUint64Array = 2,
226+
}
227+
228+
type TypedArrayConstructor =
229+
| Int8ArrayConstructor
230+
| Int16ArrayConstructor
231+
| Int32ArrayConstructor
232+
| Uint8ArrayConstructor
233+
| Uint16ArrayConstructor
234+
| Uint32ArrayConstructor
235+
| Uint8ClampedArrayConstructor
236+
| Float32ArrayConstructor
237+
| Float64ArrayConstructor;
238+
239+
type BigIntTypedArrayConstructor =
240+
| BigInt64ArrayConstructor
241+
| BigUint64ArrayConstructor;
242+
243+
export const TYPED_ARRAY_CONSTRUCTOR: Record<
244+
TypedArrayTag,
245+
TypedArrayConstructor
246+
> = {
247+
[TypedArrayTag.Float32Array]: Float32Array,
248+
[TypedArrayTag.Float64Array]: Float64Array,
249+
[TypedArrayTag.Int16Array]: Int16Array,
250+
[TypedArrayTag.Int32Array]: Int32Array,
251+
[TypedArrayTag.Int8Array]: Int8Array,
252+
[TypedArrayTag.Uint16Array]: Uint16Array,
253+
[TypedArrayTag.Uint32Array]: Uint32Array,
254+
[TypedArrayTag.Uint8Array]: Uint8Array,
255+
[TypedArrayTag.Uint8ClampedArray]: Uint8ClampedArray,
256+
};
257+
258+
export const BIG_INT_TYPED_ARRAY_CONSTRUCTOR: Record<
259+
BigIntTypedArrayTag,
260+
BigIntTypedArrayConstructor
261+
> = {
262+
[BigIntTypedArrayTag.BigInt64Array]: BigInt64Array,
263+
[BigIntTypedArrayTag.BigUint64Array]: BigUint64Array,
264+
};
265+
266+
export type TypedArrayValue =
267+
| Int8Array
268+
| Int16Array
269+
| Int32Array
270+
| Uint8Array
271+
| Uint16Array
272+
| Uint32Array
273+
| Uint8ClampedArray
274+
| Float32Array
275+
| Float64Array;
276+
277+
export type BigIntTypedArrayValue = BigInt64Array | BigUint64Array;
278+
279+
export function getTypedArrayTag(value: TypedArrayValue): TypedArrayTag {
280+
if (value instanceof Int8Array) {
281+
return TypedArrayTag.Int8Array;
282+
}
283+
if (value instanceof Int16Array) {
284+
return TypedArrayTag.Int16Array;
285+
}
286+
if (value instanceof Int32Array) {
287+
return TypedArrayTag.Int32Array;
288+
}
289+
if (value instanceof Uint8Array) {
290+
return TypedArrayTag.Uint8Array;
291+
}
292+
if (value instanceof Uint16Array) {
293+
return TypedArrayTag.Uint16Array;
294+
}
295+
if (value instanceof Uint32Array) {
296+
return TypedArrayTag.Uint32Array;
297+
}
298+
if (value instanceof Uint8ClampedArray) {
299+
return TypedArrayTag.Uint8ClampedArray;
300+
}
301+
if (value instanceof Float32Array) {
302+
return TypedArrayTag.Float32Array;
303+
}
304+
if (value instanceof Float64Array) {
305+
return TypedArrayTag.Float64Array;
306+
}
307+
throw new SerovalUnknownTypedArrayError();
308+
}
309+
310+
export function getBigIntTypedArrayTag(
311+
value: BigIntTypedArrayValue,
312+
): BigIntTypedArrayTag {
313+
if (value instanceof BigInt64Array) {
314+
return BigIntTypedArrayTag.BigInt64Array;
315+
}
316+
if (value instanceof BigUint64Array) {
317+
return BigIntTypedArrayTag.BigUint64Array;
318+
}
319+
throw new SerovalUnknownTypedArrayError();
320+
}

packages/seroval/src/core/constructors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Sequence } from './sequence';
1+
import type { Sequence } from './sequence';
22
import type { Stream } from './stream';
33

44
type SpecialPromise = Promise<unknown> & { s?: 1 | 2; v?: unknown };

packages/seroval/src/core/context/async-parser.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
createTypedArrayNode,
2323
} from '../base-primitives';
2424
import { Feature } from '../compat';
25+
import type { BigIntTypedArrayValue, TypedArrayValue } from '../constants';
2526
import { NIL, SerovalNodeType } from '../constants';
2627
import { SerovalParserError, SerovalUnsupportedTypeError } from '../errors';
2728
import { FALSE_NODE, NULL_NODE, TRUE_NODE, UNDEFINED_NODE } from '../literals';
@@ -66,10 +67,6 @@ import type {
6667
} from '../types';
6768
import { getErrorOptions } from '../utils/error';
6869
import promiseToResult from '../utils/promise-to-result';
69-
import type {
70-
BigIntTypedArrayValue,
71-
TypedArrayValue,
72-
} from '../utils/typed-array';
7370
import type { BaseParserContext, BaseParserContextOptions } from './parser';
7471
import {
7572
createArrayBufferNode,

packages/seroval/src/core/context/deserializer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { ALL_ENABLED, Feature } from '../compat';
2+
import type { BigIntTypedArrayValue, TypedArrayValue } from '../constants';
23
import {
4+
BIG_INT_TYPED_ARRAY_CONSTRUCTOR,
35
CONSTANT_VAL,
6+
DEFAULT_DEPTH_LIMIT,
47
ERROR_CONSTRUCTOR,
58
NIL,
69
SerovalNodeType,
710
SerovalObjectFlags,
811
SYMBOL_REF,
12+
TYPED_ARRAY_CONSTRUCTOR,
913
} from '../constants';
1014
import {
1115
ARRAY_BUFFER_CONSTRUCTOR,
@@ -66,11 +70,6 @@ import type {
6670
SerovalStreamThrowNode,
6771
SerovalTypedArrayNode,
6872
} from '../types';
69-
import type {
70-
BigIntTypedArrayValue,
71-
TypedArrayValue,
72-
} from '../utils/typed-array';
73-
import { getTypedArrayConstructor } from '../utils/typed-array';
7473

7574
const MAX_BASE64_LENGTH = 1_000_000; // ~0.75MB decoded
7675
const MAX_BIGINT_LENGTH = 10_000;
@@ -109,8 +108,6 @@ export interface BaseDeserializerContext extends PluginAccessOptions {
109108
depthLimit: number;
110109
}
111110

112-
const DEFAULT_DEPTH_LIMIT = 1000;
113-
114111
export function createBaseDeserializerContext(
115112
mode: SerovalMode,
116113
options: BaseDeserializerContextOptions,
@@ -450,7 +447,10 @@ function deserializeTypedArray(
450447
depth: number,
451448
node: SerovalTypedArrayNode | SerovalBigIntTypedArrayNode,
452449
): TypedArrayValue | BigIntTypedArrayValue {
453-
const construct = getTypedArrayConstructor(node.c) as Int8ArrayConstructor;
450+
const construct =
451+
node.t === SerovalNodeType.BigIntTypedArray
452+
? deserializeKnownValue(node, BIG_INT_TYPED_ARRAY_CONSTRUCTOR, node.s)
453+
: deserializeKnownValue(node, TYPED_ARRAY_CONSTRUCTOR, node.s);
454454
const source = deserialize(ctx, depth, node.f) as ArrayBuffer;
455455
const offset = node.b ?? 0;
456456
if (offset < 0 || offset > source.byteLength) {

packages/seroval/src/core/context/parser.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import {
44
createWKSymbolNode,
55
} from '../base-primitives';
66
import { ALL_ENABLED } from '../compat';
7-
import type { WellKnownSymbols } from '../constants';
8-
import { INV_SYMBOL_REF, NIL, SerovalNodeType } from '../constants';
7+
import {
8+
DEFAULT_DEPTH_LIMIT,
9+
isWellKnownSymbol,
10+
NIL,
11+
SerovalNodeType,
12+
} from '../constants';
913
import { SerovalUnsupportedTypeError } from '../errors';
1014
import { createSerovalNode } from '../node';
1115
import type { PluginAccessOptions, SerovalMode } from '../plugin';
@@ -86,7 +90,7 @@ export function createBaseParserContext(
8690
marked: new Set(),
8791
features: ALL_ENABLED ^ (options.disabledFeatures || 0),
8892
refs: options.refs || new Map(),
89-
depthLimit: options.depthLimit || 1000,
93+
depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
9094
};
9195
}
9296

@@ -164,8 +168,8 @@ export function parseWellKnownSymbol(
164168
if (ref.type !== ParserNodeType.Fresh) {
165169
return ref.value;
166170
}
167-
if (current in INV_SYMBOL_REF) {
168-
return createWKSymbolNode(ref.value, current as WellKnownSymbols);
171+
if (isWellKnownSymbol(current)) {
172+
return createWKSymbolNode(ref.value, current);
169173
}
170174
throw new SerovalUnsupportedTypeError(current);
171175
}

packages/seroval/src/core/context/sync-parser.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
createTypedArrayNode,
2323
} from '../base-primitives';
2424
import { Feature } from '../compat';
25+
import type { BigIntTypedArrayValue, TypedArrayValue } from '../constants';
2526
import { NIL, SerovalNodeType } from '../constants';
2627
import {
2728
SerovalDepthLimitError,
@@ -72,10 +73,6 @@ import type {
7273
SerovalTypedArrayNode,
7374
} from '../types';
7475
import { getErrorOptions } from '../utils/error';
75-
import type {
76-
BigIntTypedArrayValue,
77-
TypedArrayValue,
78-
} from '../utils/typed-array';
7976
import type { BaseParserContext, BaseParserContextOptions } from './parser';
8077
import {
8178
createArrayBufferNode,

packages/seroval/src/core/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ export class SerovalMissingReferenceForIdError extends Error {
156156
}
157157

158158
export class SerovalUnknownTypedArrayError extends Error {
159-
constructor(name: string) {
159+
constructor() {
160160
super(
161161
import.meta.env.PROD
162162
? getSpecificErrorMessage(SpecificErrorCodes.UnknownTypedArray)
163-
: 'Unknown TypedArray "' + name + '"',
163+
: 'Unknown TypedArray detected',
164164
);
165165
}
166166
}

packages/seroval/src/core/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type {
2+
BigIntTypedArrayTag,
23
ErrorConstructorTag,
34
SerovalConstant,
45
SerovalNodeType,
56
SerovalObjectFlags,
67
Symbols,
8+
TypedArrayTag,
79
} from './constants';
810
import type { SpecialReference } from './special-reference';
911

@@ -111,7 +113,7 @@ export interface SerovalTypedArrayNode extends SerovalBaseNode {
111113
// id
112114
i: number;
113115
// TypedArray Constructor
114-
c: string;
116+
s: TypedArrayTag;
115117
// ArrayBuffer reference
116118
f: SerovalNode;
117119
// Byte Offset
@@ -124,7 +126,7 @@ export interface SerovalBigIntTypedArrayNode extends SerovalBaseNode {
124126
t: SerovalNodeType.BigIntTypedArray;
125127
i: number;
126128
// TypedArray Constructor
127-
c: string;
129+
s: BigIntTypedArrayTag;
128130
// ArrayBuffer reference
129131
f: SerovalNode;
130132
// Byte Offset

0 commit comments

Comments
 (0)