This page lists the types each mode supports, the feature flags for older runtimes, and how the packages resolve.
Supported types
The modes are:
- Sync:
serialize,toJSON,crossSerializeandtoCrossJSON. - Async:
serializeAsync,toJSONAsync,crossSerializeAsyncandtoCrossJSONAsync. - Streaming:
crossSerializeStream,toCrossJSONStreamandSerializer. - Binary:
binary.serialize. See Binary mode.
| Type | Sync | Async | Streaming | Binary |
|---|---|---|---|---|
string, boolean, null, undefined | ✅ | ✅ | ✅ | ✅ |
number, including NaN, Infinity, -Infinity and -0 | ✅ | ✅ | ✅ | ✅ |
bigint | ✅ | ✅ | ✅ | ✅ |
Array, including sparse arrays | ✅ | ✅ | ✅ | ✅ |
Object and Object.create(null) | ✅ | ✅ | ✅ | ✅ |
| Frozen, sealed and non-extensible objects | ✅ | ✅ | ✅ | ✅ |
Date | ✅ | ✅ | ✅ | ✅ |
Boxed primitives, such as Object(1) and Object('text') | ✅ | ✅ | ✅ | ✅ |
RegExp | ✅ 1 | ✅ 1 | ✅ 1 | ✅ 1 |
Map and Set | ✅ | ✅ | ✅ | ✅ |
ArrayBuffer and DataView | ✅ | ✅ | ✅ | ✅ 2 |
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array | ✅ | ✅ | ✅ | ✅ 2 |
BigInt64Array and BigUint64Array | ✅ 3 | ✅ 3 | ✅ 3 | ✅ 3 2 |
Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError | ✅ 4 | ✅ 4 | ✅ 4 | ✅ 4 |
AggregateError | ✅ 5 | ✅ 5 | ✅ 5 | ✅ 5 |
Temporal types | ✅ 6 | ✅ 6 | ✅ 6 | ✅ 6 |
Promise | ❌ 7 | ✅ | ✅ | ✅ |
Iterables (Symbol.iterator) | ✅ | ✅ | ✅ | ✅ |
Async iterables (Symbol.asyncIterator) | ❌ 8 | ✅ | ✅ | ✅ |
createStream | ✅ 9 | ✅ | ✅ | ✅ |
createLiveStream | ❌ 10 | ✅ | ✅ | ✅ |
| Well-known symbols | ✅ | ✅ | ✅ | ✅ |
| Shared and cyclic references | ✅ | ✅ | ✅ | ✅ |
| Isomorphic references | ✅ | ✅ | ✅ | ✅ |
Web types such as URL, Request and FormData are supported through plugins.
Feature flags
Every function takes a disabledFeatures option. It turns features off, so the output runs in older runtimes, or so a receiver rejects types it does not want.
disabledFeatures is a bit mask. Combine flags with |:
import { Feature, serialize } from 'seroval';
serialize(value, {
disabledFeatures: Feature.AggregateError | Feature.BigIntTypedArray,
});| Flag | When disabled |
|---|---|
Feature.AggregateError | AggregateError is sent as an Error. |
Feature.ErrorPrototypeStack | The stack property of errors is not sent. |
Feature.ObjectAssign | Output assigns properties one by one instead of calling Object.assign. |
Feature.BigIntTypedArray | BigInt64Array and BigUint64Array are sent as iterable plain objects. |
Feature.RegExp | RegExp throws when serialized, and is rejected when decoded. |
Feature.Temporal | Temporal values throw when serialized, and are rejected when decoded. |
Feature.ArrowFunction | Deprecated. It has no effect. |
All features are on by default. For example, with ObjectAssign disabled:
const value = Object.assign(Object.create(null), { x: 1 });
serialize(value);
// Object.assign(Object.create(null),{x:1})
serialize(value, { disabledFeatures: Feature.ObjectAssign });
// (h=>h=(h=Object.create(null),h.x=1,h))()Versions
seroval does not keep payloads compatible across versions. A minor release can change how a node is encoded in JavaScript, JSON and binary output. Serialize and deserialize with the same seroval version. When a server and a client are deployed separately, deploy them with the same version.
Runtimes
seroval runs anywhere modern JavaScript runs: Node.js 20 or later, Deno, Bun, browsers and edge runtimes. It has no dependencies.
Package resolution
The seroval package has ESM and CommonJS builds, and a development build with full error messages. Bundlers pick the right one through the package's export conditions:
- The
developmentcondition selects the build with full error messages. - The default selects the production build, which reports errors by code. See Errors.
- The
browsercondition selects the same build as the default, so there is never a second copy of seroval's classes or reference registry.
The seroval/read, seroval/write, seroval/shared and seroval/binary entry points use the same conditions. They load the same files as seroval, so mixing entry points does not create a second copy either. TypeScript resolves their types with the node10, node16, nodenext and bundler settings of moduleResolution.
Resolvers that ignore export conditions, such as older bundlers and some React Native setups, can use the main and module fields. The base64 encoding of buffers uses Buffer when it exists, and portable browser APIs otherwise. Both produce the same output. Binary mode does not use base64.
Related
Footnotes
-
Requires
Feature.RegExp, which is on by default. When it is off, aRegExpthrowsSerovalUnsupportedTypeError. ↩ ↩2 ↩3 ↩4 -
Binary mode rejects typed arrays longer than 1,000,000 elements and a
DataViewlonger than 1,000,000 bytes. AnArrayBufferis limited to 1,000,000 bytes by default. SetmaxArrayBufferLengthonbinary.deserializeto change it. ↩ ↩2 ↩3 -
Requires
Feature.BigIntTypedArray, which is on by default. When it is off, these arrays are sent as iterable plain objects, not as typed arrays. ↩ ↩2 ↩3 ↩4 -
The
stackproperty is sent only whileFeature.ErrorPrototypeStackis on. ↩ ↩2 ↩3 ↩4 -
Sent as an
ErrorwhenFeature.AggregateErroris off. ↩ ↩2 ↩3 ↩4 -
Requires
Feature.Temporal, and a runtime that has theTemporalglobal on both sides. ↩ ↩2 ↩3 ↩4 -
Sync functions cannot wait for promises, so a promise throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩ -
Sync functions cannot wait for values, so an async iterable throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩ -
Sync functions send a stream that has ended with all its events. A stream that is still open throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩ -
Sync functions cannot read a live stream, so it throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩