Reference

Compatibility

Supported types in each mode, feature flags for older runtimes, and package resolution.

Supported types

The modes are:

  • Sync: serialize, toJSON, crossSerialize and toCrossJSON.
  • Async: serializeAsync, toJSONAsync, crossSerializeAsync and toCrossJSONAsync.
  • Streaming: crossSerializeStream, toCrossJSONStream and Serializer.
TypeSyncAsyncStreaming
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✅✅✅
RegExp✅ 1✅ 1✅ 1
Map and Set✅✅✅
ArrayBuffer and DataView✅✅✅
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array✅✅✅
BigInt64Array and BigUint64Array✅ 2✅ 2✅ 2
Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError✅ 3✅ 3✅ 3
AggregateError✅ 4✅ 4✅ 4
Temporal types✅ 5✅ 5✅ 5
Promise❌ 6✅✅
Iterables (Symbol.iterator)✅✅✅
Async iterables (Symbol.asyncIterator)❌ 7✅✅
createStream and createLiveStream❌ 8✅✅
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 |:

JavaScript
import { Feature, serialize } from 'seroval';

serialize(value, {
  disabledFeatures: Feature.AggregateError | Feature.BigIntTypedArray,
});
FlagWhen disabled
Feature.AggregateErrorAggregateError is sent as an Error.
Feature.ErrorPrototypeStackThe stack property of errors is not sent.
Feature.ObjectAssignOutput assigns properties one by one instead of calling Object.assign.
Feature.BigIntTypedArrayBigInt64Array and BigUint64Array throw.
Feature.RegExpRegExp throws when serialized, and is rejected when decoded.
Feature.TemporalTemporal values throw when serialized, and are rejected when decoded.
Feature.ArrowFunctionDeprecated. It has no effect.

All features are on by default. For example, with ObjectAssign disabled:

JavaScript
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))()

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 development condition selects the build with full error messages.
  • The default selects the production build, which reports errors by code. See Errors.
  • The browser condition selects the same build as the default, so there is never a second copy of seroval's classes or reference registry.

Resolvers that ignore export conditions, such as older bundlers and some React Native setups, can use the main and module fields. Binary encoding uses Buffer when it exists, and portable browser APIs otherwise. Both produce the same output.

Footnotes

  1. Requires Feature.RegExp, which is on by default. When it is off, a RegExp throws SerovalUnsupportedTypeError. ↩ ↩2 ↩3

  2. Requires Feature.BigIntTypedArray, which is on by default. When it is off, these arrays throw SerovalUnsupportedTypeError. ↩ ↩2 ↩3

  3. The stack property is sent only while Feature.ErrorPrototypeStack is on. ↩ ↩2 ↩3

  4. Sent as an Error when Feature.AggregateError is off. ↩ ↩2 ↩3

  5. Requires Feature.Temporal, and a runtime that has the Temporal global on both sides. ↩ ↩2 ↩3

  6. Sync functions cannot wait for promises, so a promise throws a SerovalParserError whose cause is a SerovalUnsupportedTypeError. ↩

  7. Sync functions cannot wait for values, so an async iterable throws a SerovalParserError whose cause is a SerovalUnsupportedTypeError. ↩

  8. Sync functions cannot wait for values, so these are sent as an empty stream that never ends. Reading it waits forever. ↩