Functions
Each function has two overloads. One accepts schemas that implement both Standard interfaces. The other accepts a Standard Schema plus { structure }.
encode
Section titled “encode”encode<S extends EncodableStandardSchema>(schema: S, value: InferOutput<S>): Uint8Array;encode<S extends StandardSchemaV1>(schema: S, value: InferOutput<S>, options: CompileOptions): Uint8Array;Validates, then writes bytes. Throws EncodeError if validation fails or the schema cannot be encoded.
The returned Uint8Array is an exact-size copy, not a view into a reused buffer. It is safe to retain. The wire plan is cached by schema identity.
decode
Section titled “decode”decode<S extends EncodableStandardSchema>(schema: S, bytes: Uint8Array): InferOutput<S>;decode<S extends StandardSchemaV1>(schema: S, bytes: Uint8Array, options: CompileOptions): InferOutput<S>;Reads the structure, then validates. Throws DecodeError for malformed bytes and for validation failures on the way out.
Trailing bytes cause an error. An input that is not a Uint8Array produces a DecodeError rather than a raw TypeError. Cross-realm arrays from node:vm, an iframe, or jsdom are accepted through a tag check when instanceof fails.
safeEncode / safeDecode
Section titled “safeEncode / safeDecode”safeEncode(schema, value, options?): SafeResult<Uint8Array>;safeDecode(schema, bytes, options?): SafeResult<InferOutput<S>>;
type SafeResult<T> = { success: true; data: T } | { success: false; error: Error };Same behavior without throwing. Non-Error throws are wrapped, so result.error is always an Error.
encodeAsync / decodeAsync
Section titled “encodeAsync / decodeAsync”encodeAsync(schema, value, options?): Promise<Uint8Array>;decodeAsync(schema, bytes, options?): Promise<InferOutput<S>>;Use these functions for schemas with asynchronous refinements. Both accept a schema, not a codec, so async validation does not compose with fingerprinted(). Carry codec.fingerprintHex separately if you need both. There are no safe async variants. See Validation.
Calling encode/decode on an async schema throws:
This Standard Schema validates asynchronously; use encodeAsync/decodeAsync with the Standard Schema. Neither accepts a compiled or fingerprinted codec.
compile
Section titled “compile”compile<S extends EncodableStandardSchema>(schema: S): Schema<InferOutput<S>>;compile<S extends StandardSchemaV1>(schema: S, options: CompileOptions): Schema<InferOutput<S>>;Returns the cached wire plan as a codec with .encode() and .decode(). Aliases: codec, fromStandard.
No build step. compile builds a tree of Schema objects in memory and writes nothing to disk. Repeated calls with the same schema and structure object return the same cached instance. An object schema with no optional fields also builds a decoder with new Function, falling back to the interpreted path where a Content Security Policy forbids it — see Compilation and Caching.
fingerprinted
Section titled “fingerprinted”fingerprinted<T>(codec: Schema<T>, options?: FingerprintOptions): FingerprintedSchema<T>;Prefixes payloads with a short FNV-1a digest of the schema’s canonical wire signature.
const codec = fingerprinted(compile(Person));codec.encode(person); // 3 + payload bytescodec.fingerprint; // Uint8Array — a fresh copy every readcodec.fingerprintHex; // "7236d1" — the Map key for dispatchThrows EncodeError for a codec without a signature:
fingerprinted() needs a codec built from a Standard JSON Schema; compile() returns one, the low-level m API does not
It also throws if bytes is outside 1–4. Performance is effectively the same at every width, so use the default 3 bytes unless you have a specific protocol constraint. See Fingerprinting.
FingerprintedSchema
Section titled “FingerprintedSchema”| Member | Type | Notes |
|---|---|---|
fingerprint | Uint8Array | Fresh copy every read; cannot key a Map |
fingerprintHex | string | Lowercase hex, immutable, the dispatch key |
fingerprint returns a copy so callers cannot mutate the codec’s internal bytes. Otherwise, an accidental write could make the codec non-canonical while it still round-trips against itself.
Schema<T>
Section titled “Schema<T>”abstract class Schema<T> { encode(value: T): Uint8Array; decode(value: Uint8Array): T; optional(): OptionalSchema<T>; nullable(): NullableSchema<T>; readonly signature?: string; // only on codecs built from a JSON Schema}signature is type-only on the base class, so users who do not import fingerprinted() pay no runtime cost for it. _encode, _decode, and _minWidth are internal and may change in a minor release. See Low-Level m API.