@unikvs/v8-serde
How to use @unikvs/v8-serde, Node.js-only middleware that serializes values with node:v8.
Overview
Middleware that serializes values with the node:v8 serializer. Middleware Node.js only
Single reads/writes serialize a value directly, while streams convert values to concatenated frames of a 4-byte big-endian length plus payload.
nameis"V8Serde".isOpenbecomestrueafteropen()completes.open()fails in environments withoutnode:v8.
Install with:
npm install @unikvs/v8-serdepnpm add @unikvs/v8-serdeyarn add @unikvs/v8-serdebun add @unikvs/v8-serdenub add @unikvs/v8-serdeaube add @unikvs/v8-serdeUsage
V8Serde implements IMiddleware. Pass it to UniKvs with use().
import { V8Serde } from "@unikvs/v8-serde";
The following example serializes an object for storage and restores its original shape on read.
import { V8Serde } from "@unikvs/v8-serde";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ item: unikvs.Value<{ readonly id: number }> }>()
.use(new V8Serde())
.persist(new Memory())
.build();
await kvs.open();
await kvs.set("item", { id: 1 });
const item = await kvs.get("item");
await kvs.close();
Data types
| Method | Input | Output |
|---|---|---|
encode |
unknown |
Uint8Array<ArrayBuffer> |
decode |
Uint8Array<ArrayBuffer> |
unknown |
encodeStream |
unknown |
Uint8Array<ArrayBuffer> |
decodeStream |
Uint8Array<ArrayBuffer> |
unknown |
encodeserializes a value withv8.serialize. Callopen()first.decoderestores bytes to a value withv8.deserialize. Callopen()first.encodeStreamemits one length-prefixed frame per input item (4-byte big-endian length plus payload), concatenated in order.decodeStreamaccumulates received bytes and emits values as frames complete.- Truncated frames wait for the next chunk; leftover bytes at the end throw an error.
- Empty chunks do not affect decoding, so this middleware ignores them.
Errors
Errors thrown by this package:
V8SerdeEncodeError
Meaning
Thrown when v8.serialize encoding of a value fails. The cause is preserved in cause.
How to fix
Check cause and pass a value supported by v8.serialize.
V8SerdeDecodeError
Meaning
Thrown when v8.deserialize decoding fails. This covers corrupted input and incomplete frames left at the end of a stream. The cause is preserved in cause.
How to fix
Make sure the input was produced by v8.serialize and the stream was not cut off midway.
UnsupportedRuntimeError
Meaning
Thrown by open() in environments without node:v8. This is a shared error from @unikvs/core.
How to fix
Use it in a Node.js environment.