Entry points
seroval exports everything on this page. Code that only reads or only writes can import a smaller entry point instead.
| Entry point | Exports |
|---|---|
seroval | Everything. |
seroval/read | fromJSON, fromCrossJSON, createCrossDeserializer, deserialize, and everything in seroval/shared. |
seroval/write | serialize, serializeAsync, toJSON, toJSONAsync, compileJSON, the cross-reference serializers, getCrossReferenceHeader, Serializer, and everything in seroval/shared. |
seroval/shared | What both sides use: streams, references, createPlugin, Feature, the errors and the error catalog. |
seroval/binary | The binary mode serialize, deserialize, createDeserializer and createReferenceMap. seroval does not export these. |
seroval/validator | The validators for a plugin's validate field (see Plugin security). seroval does not export them. |
All entry points load the same files, so a value imported from one is the same value in the others. For example, a framework can read page state in one chunk and serialize server function calls in another:
// Loaded on every page.
import { fromCrossJSON } from 'seroval/read';
// Loaded only when a server function runs.
import { fromJSON } from 'seroval/read';
import { toJSONAsync } from 'seroval/write';The bundler keeps the serializers out of the first chunk. Import the subpaths to get this in every bundler. Some bundlers, such as rolldown, also split the code when both chunks import seroval, because its build is split into the same files. Others, such as esbuild, keep the serializers in the first chunk in that case. seroval/read makes sure a chunk never pulls in a serializer by mistake.
Serialize to JavaScript
| Function | Returns | Guide |
|---|---|---|
serialize(value, options?) | string | Serialize to JavaScript |
serializeAsync(value, options?) | Promise<string> | Serialize to JavaScript |
deserialize(source) | the value | Serialize to JavaScript |
Options: plugins, disabledFeatures, depthLimit, compactArrayBufferViews.
JSON
| Function | Returns | Guide |
|---|---|---|
toJSON(value, options?) | SerovalJSON | JSON |
toJSONAsync(value, options?) | Promise<SerovalJSON> | JSON |
fromJSON(tree, options?) | the value | JSON |
compileJSON(tree, options?) | string | JSON |
fromJSON options: plugins, disabledFeatures, depthLimit, maxBase64Length. compileJSON options: plugins.
Cross-reference
| Function | Returns | Guide |
|---|---|---|
crossSerialize(value, options?) | string | Cross-reference |
crossSerializeAsync(value, options?) | Promise<string> | Cross-reference |
crossSerializeStream(value, options) | stop function | Streaming |
toCrossJSON(value, options?) | SerovalNode | Cross-reference |
toCrossJSONAsync(value, options?) | Promise<SerovalNode> | Cross-reference |
toCrossJSONStream(value, options) | stop function | Streaming |
fromCrossJSON(node, options) | the value | Cross-reference |
createCrossDeserializer(options) | CrossDeserializer | Streaming |
getCrossReferenceHeader(scopeId?) | string | Cross-reference |
new Serializer(options) | Serializer | Streaming |
Binary mode
Exported from seroval/binary. See Binary mode.
import * as binary from 'seroval/binary';| Function | Returns | Guide |
|---|---|---|
binary.serialize(value, options) | stop function | Serializing |
binary.deserialize(options) | Promise<{ value, pending }> | Deserializing |
binary.createDeserializer(options) | session | Sharing references |
binary.createReferenceMap() | ReferenceMap | Sharing references |
binary.serialize options:
| Option | Type | Default | Description |
|---|---|---|---|
refs | Map | required | Values already sent. Share one Map between payloads to send references. |
onSerialize(bytes) | function | required | Receives each Uint8Array chunk. May return a promise, which holds back the live stream event behind the chunk. |
onDone() | function | required | Runs once every promise and stream has settled. |
onError(error) | function | required | Runs when a value fails to serialize. |
plugins | Plugin[] | none | Plugins with a binary section. |
disabledFeatures | number | 0 | Feature flags to turn off. |
features | number | all features | Features to turn on. Overrides disabledFeatures. |
depthLimit | number | 64 | Maximum nesting depth. |
compactArrayBufferViews | boolean | false | Send only the bytes each typed array or DataView covers, instead of its whole buffer. |
The stop function cancels every source that is still running and runs plugin cleanups. It does not call onDone.
binary.deserialize options:
| Option | Type | Default | Description |
|---|---|---|---|
read() | function | required | Returns a promise for the next Uint8Array chunk, or for undefined at the end of the input. |
onError(error) | function | required | Runs when the input is invalid or a value cannot be rebuilt. |
refs | ReferenceMap | a new map | From binary.createReferenceMap(). Share one between payloads to resolve references. |
plugins | Plugin[] | none | Plugins with a binary section. |
disabledFeatures | number | 0 | Feature flags to reject. |
features | number | all features | Features to accept. Overrides disabledFeatures. |
maxArrayBufferLength | number | 1000000 | Largest ArrayBuffer, in bytes, that the input may hold. |
maxArrayLength | number | 1000000 | Largest array length that the input may hold. |
signal | AbortSignal | none | Stops decoding when it aborts, and rejects the values still pending with its reason. |
The promise resolves with { value, pending } once the root value is decoded. pending counts the promises and streams in the value that have not settled or ended. Promises and streams inside it may still be pending. It rejects if the input is invalid, ends early or read() rejects before the root value is ready.
Streams
| Export | Description | Guide |
|---|---|---|
createStream() | Creates a stream that keeps every value. | Streams |
isStream(value) | Whether a value is a seroval stream. | |
createLiveStream(options?) | Creates a single-reader stream with backpressure. Returns { stream, producer }. | Live streams |
isLiveStream(value) | Whether a value is a live stream. |
References and plugins
| Export | Description | Guide |
|---|---|---|
createReference(id, value) | Sends value by its key. | Isomorphic references |
new OpaqueReference(value, replacement?) | Sends replacement instead of value. | OpaqueReference |
createPlugin(plugin) | Defines a plugin, with type inference. Add a binary section for binary mode. | Writing plugins |
Feature | Feature flags for disabledFeatures. | Compatibility |
Sequence | The recorded values of an iterable, built by the parsers. Exported only so bundlers can remove it when it is unused. Do not create one, and do not read its fields, which may change in any release. | Internal |
Errors
| Export | Description | Guide |
|---|---|---|
SerovalError | The base class of every error seroval throws because of the data. | Errors |
SerovalParserError, SerovalSerializationError, SerovalDeserializationError | The wrappers for each step. | Errors |
SerovalUnsupportedTypeError and the other error classes | One class per code. | Error codes |
ERROR_CODES, DOCS_ERRORS_URL, getProdMessage, getCatalogEntry, getDevMessage | The error catalog. | Reading the catalog |
Options
All options in one place. Each function lists the ones it accepts above. Binary mode has its own options.
| Option | Type | Default | Description |
|---|---|---|---|
plugins | Plugin[] | none | Plugins for extra types. |
disabledFeatures | number | 0 | Feature flags to turn off, combined with |. |
depthLimit | number | 64 | Maximum nesting depth. |
compactArrayBufferViews | boolean | false | Send only the bytes each typed array covers. |
refs | Map | a new Map | Values already sent, shared between cross-reference calls. |
scopeId | string | none | Writes to $R[scopeId] instead of $R. |
assignSharedRefsOnly | boolean | false | crossSerialize and crossSerializeAsync only. Stores only the values the output reads more than once in $R. |
maxBase64Length | number | 1000000 | Largest encoded ArrayBuffer that JSON decoding accepts. |
onSerialize(chunk, initial) | function | required | Receives each script chunk of a stream. |
onParse(node, initial) | function | required | Receives each JSON node of a stream. |
onDone() | function | none | Runs when a stream finishes. |
onError(error) | function | none | Runs when a stream fails. |
Types
seroval also exports TypeScript types for the values above, including SerovalJSON, SerovalNode, Plugin, SerializeOptions, SerializeAsyncOptions, ToJSONOptions, ToJSONAsyncOptions, CrossSerializeOptions, Stream, StreamListener, LiveStream, LiveStreamProducer, LiveStreamConsumer, CrossDeserializer, PluginWithBinaryMode, BinaryPlugin, ErrorCode, SerovalErrorShape, CatalogEntry and the options type of each function. seroval/binary exports the types SerializeOptions, DeserializeOptions and ReferenceMap.