Thrown when a caller passes an argument or object that violates a public
contract: an out-of-range value, a malformed id, or an object not produced
by the package's own factories.
Abstract so every concrete instance belongs to the throwing package's own
subclass, which fixes Code to that package's error-code union and gives
.name a package-specific value.
InvalidInputError,
classParseError<Codeextendsstring>
Thrown when decoding a serialized payload that is malformed, internally
inconsistent, or fails element validation.
@throws ― {WireInvalidInputError} With INVALID_ARGUMENT when the magic is not four bytes, the payload version
or a tag is not a byte, or two kinds share a tag.
artifactFormat } from'knotkit/wire'
class
classExampleParseError
ExampleParseErrorextends
classParseError<Codeextendsstring>
Thrown when decoding a serialized payload that is malformed, internally
inconsistent, or fails element validation.
Abstract for the same reason as
InvalidInputError
.
ParseError<string> {
readonly
ExampleParseError.moduleName: "knotkit/example"
moduleName='knotkit/example'
}
class
classExampleInvalidInputError
ExampleInvalidInputErrorextends
classInvalidInputError<Codeextendsstring>
Thrown when a caller passes an argument or object that violates a public
contract: an out-of-range value, a malformed id, or an object not produced
by the package's own factories.
Abstract so every concrete instance belongs to the throwing package's own
subclass, which fixes Code to that package's error-code union and gives
.name a package-specific value.
@throws ― {WireInvalidInputError} With INVALID_ARGUMENT when the magic is not four bytes, the payload version
or a tag is not a byte, or two kinds share a tag.
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Run build and frame its payload under this format's magic and payload version.
@throws ― {KnotkitError} The module's invalid-input error for every wire caller-contract failure raised by
build or by the framing; any other error is rethrown untouched.
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
ArtifactFormat<"dense"|"sparse", ExampleParseError>.kindTag: (kind:"dense"|"sparse") => number
The byte kind is written as.
kindTag('dense')),
ArtifactPayload.sections: readonly WireSection[]
sections: [{
WireSection.tag: number
tag: 1,
WireSection.bytes: Uint8Array<ArrayBufferLike>
bytes:
var Uint8Array:Uint8ArrayConstructor
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Validate untrusted input as this format's envelope and return the module-owned bytes.
@throws ― {KnotkitError} The module's parse error: wrong-format for a value that is not a Uint8Array, an
envelope reason for malformed bytes, and unsupported-payload-version for another payload version.
Use knotkit/wire for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/wire'.
Exports:
- `artifactFormat` → `ArtifactFormat<Kind, Failure>`: Declare a module's magic, payload version, kind tags, and section tags once; frame its payloads and run its decoder prelude.
- `serializeEnvelope` → `Uint8Array`: Frame package bytes into an aligned, sectioned envelope with an optional CRC-32 integrity section.
- `deserializeEnvelope` → `WireEnvelope`: Validate envelope geometry and hand the owning package its sections.
- `inspectEnvelope` → `WireEnvelopeParts`: Take a valid envelope apart into owned copies of the parts serializeEnvelope takes back, whichever module wrote it.
- `tamperEnvelope` → `Uint8Array`: Copy a valid artifact and damage the copy so decoding fails for exactly one named reason.
- `crc32` → `number`: Checksum the bytes an integrity section covers.
- `chooseIntegerWidth` → `IntegerWidth`: Pick the smallest declared little-endian width that holds a maximum, so an artifact spends no bytes on headroom.
- `integerWidthMaximum` → `number`: The largest value a declared integer width can hold.
- `requireSection` → `WireSection`: Return the only section carrying a tag, or fail with the package's geometry error.
- `rejectSection` → `void`: Fail when an artifact carries a section tag the package's layout forbids.
- `stateBridge` → `StateBridge<State>`: Package-scoped bridge a factory attaches its closure state through, readable by the package's serializer only.
- `mapWireSerializationError` → `T`: Translate wire caller-contract failures into a package's own closed invalid-input codes.
Inputs and their sources:
- generic value encoding: `ElementCodec<T>` from `knotkit/wire`
- the package's own error codes a wire failure translates to: `WireSerializationCodeMap<Code>` from `knotkit/wire`
Use something else when:
- persist a Knotkit data structure directly → the structure package's serialize function
- produce a human-readable persistence artifact → the structure package's Json serializer
- compress a binary artifact → CompressionStream
Errors are WireInvalidInputError with a `code` property. Branch on `error.code`, never on the message. Save and load through `undefined` (serializeEnvelope, deserializeEnvelope).
Full page: https://knotkit.jkomyno.dev/modules/wire.md
Types: node_modules/knotkit/wire/ and https://knotkit.jkomyno.dev/reference/knotkit/wire/