Upgrade to 1.9

What changed in seroval 1.9, and the steps to upgrade from 1.8.

Seroval 1.9 removes the v export, gives every error a code, and changes how typed arrays are written in JSON. This page lists each breaking change and the steps to upgrade from 1.8. Binary mode is new in 1.9: see Binary mode.

Upgrade

  1. Install the new versions together:

    npm install seroval@^1.9.0 seroval-plugins@latest @seroval/transport@latest
    pnpm add seroval@^1.9.0 seroval-plugins@latest @seroval/transport@latest
    yarn add seroval@^1.9.0 seroval-plugins@latest @seroval/transport@latest
    bun add seroval@^1.9.0 seroval-plugins@latest @seroval/transport@latest
  2. Import the validators from seroval/validator. See The v export is removed.

  3. Replace checks on error messages with checks on code. See Errors have codes.

  4. Replace cause instanceof RangeError after an oversized buffer. See Oversized buffers.

  5. Upgrade the sender and the receiver of JSON together, and write stored JSON that holds typed arrays again. See Typed arrays in JSON.

Breaking changes

The v export is removed

seroval, seroval/read, seroval/write and seroval/shared no longer export v. The validators are only in seroval/validator.

Before:

JavaScript
import { v } from 'seroval';

After:

JavaScript
import * as v from 'seroval/validator';

@seroval/transport 0.1.3 and earlier read v from seroval/read. Update it to 0.1.4 or later together with seroval.

Errors have codes

Every error that seroval throws because of the data now extends SerovalError, and has a code such as SRV008. In 1.8, only the three step wrappers extended SerovalError. Existing instanceof checks still work.

1.81.9
name'Error'The class name, such as 'SerovalMalformedNodeError'
code and stepnoneA code from the codes table, and the step that failed
Production messageA step or error number, such as Seroval Error (step: 3)The code, the step and a link, such as Seroval SRV008 (deserialize): https://seroval.pages.dev/docs/errors/SRV008
Development messageA message per errorA message per error, rewritten, and for parse errors the path to the value

Every message changed, so code that matches message text breaks. Check the code instead:

JavaScript
import { SerovalError, fromJSON } from 'seroval';

try {
  fromJSON(body);
} catch (error) {
  if (error instanceof SerovalError && error.code === 'SRV010') {
    // The depth limit was reached.
  }
}

Errors describes the fields and the production format.

Oversized buffers

A buffer larger than maxBase64Length still throws a SerovalDeserializationError. Its cause was a RangeError and is now a SerovalBase64LimitError with the code SRV020.

Before:

JavaScript
if (error.cause instanceof RangeError) {
  // The buffer was too large.
}

After:

JavaScript
if (error.code === 'SRV020') {
  // The buffer was too large.
}

An invalid maxBase64Length option is still a RangeError, now with the code SRV019.

Typed arrays in JSON

A typed array node in JSON names its constructor with a numeric tag in s instead of a name in c. BigInt64Array and BigUint64Array nodes change the same way. This changes the output of toJSON, toJSONAsync, toCrossJSON and the other JSON functions.

1.8:

JSON
{ "t": 15, "i": 0, "c": "Uint8Array", "f": { "...": "..." }, "b": 0, "l": 2 }

1.9:

JSON
{ "t": 15, "i": 0, "s": 4, "f": { "...": "..." }, "b": 0, "l": 2 }

JSON with a typed array cannot cross versions. Seroval 1.9 rejects such JSON from 1.8 with a SerovalMalformedNodeError (SRV008), and 1.8 cannot read it from 1.9. JSON without typed arrays is not affected.

  • Deploy the sender and the receiver with the same version.
  • Decode stored JSON that holds typed arrays with 1.8, then write it again with 1.9.

Errored async iterables

When the stream behind a deserialized async iterable has already ended with an error, next() now returns a rejected promise. In 1.8 it threw synchronously. Code that awaits next() or uses for await sees no change.

seroval-plugins

Two errors in seroval-plugins were plain Error objects and now have codes:

  • A value shared between two plugin values throws SerovalPluginValidationError (SRV013), with the plugin tag and the field.
  • Decoding the node of the AbortController factory directly throws SerovalMalformedNodeError (SRV008).

The code field needs seroval 1.9. With seroval 1.8, the same errors are thrown without a code.