Plugins teach seroval new types. Pass them in the plugins option of any function, on both the sending and the receiving side.
import { serializeAsync } from 'seroval';
import { BlobPlugin } from 'seroval-plugins/web';
const blob = new Blob(['Hello'], { type: 'text/plain' });
await serializeAsync(blob, { plugins: [BlobPlugin] });Every function takes plugins, including fromJSON, fromCrossJSON, createCrossDeserializer and Serializer. The receiver needs a plugin with the same tag as the one the sender used, or decoding throws SerovalMissingPluginError.
When several plugins accept the same value, the first one in the list wins. A plugin can depend on other plugins, which are then added for you.
seroval-plugins
The seroval-plugins package has plugins for common web platform types.
npm install seroval-pluginsimport {
FormDataPlugin,
HeadersPlugin,
RequestPlugin,
URLPlugin,
} from 'seroval-plugins/web';Some types can only be read asynchronously. A Blob's bytes, for example, are only available through a promise. Those plugins work only with the async and streaming functions.
| Plugin | Type | Sync | Async | Streaming |
|---|---|---|---|---|
URLPlugin | URL | ✅ | ✅ | ✅ |
URLSearchParamsPlugin | URLSearchParams | ✅ | ✅ | ✅ |
HeadersPlugin | Headers | ✅ | ✅ | ✅ |
FormDataPlugin | FormData | ✅ 1 | ✅ | ✅ 1 |
BlobPlugin | Blob | ❌ | ✅ | ❌ 2 |
FilePlugin | File | ❌ | ✅ | ❌ 2 |
ReadableStreamPlugin | ReadableStream | ❌ | ✅ | ✅ |
RequestPlugin | Request | ❌ | ✅ | ✅ |
ResponsePlugin | Response | ❌ | ✅ | ✅ |
EventPlugin | Event | ✅ | ✅ | ✅ |
CustomEventPlugin | CustomEvent | ✅ | ✅ | ✅ |
DOMExceptionPlugin | DOMException | ✅ | ✅ | ✅ |
ImageDataPlugin | ImageData | ✅ | ✅ | ✅ |
AbortSignalPlugin | AbortSignal | ✅ | ✅ | ✅ |
To use every web plugin at once:
import * as web from 'seroval-plugins/web';
const plugins = Object.values(web);Writing your own
See Writing plugins. If your plugin decodes data from a client, also read Plugin security.