Protocol contract

Two keys define the boundary.

A loadable message is a non-array object with its own type and data properties. Serialization always emits those two keys and no others.

The envelope

{
    "type": "inventory.changed",
    "data": {
        "sku": "A-104",
        "available": 8
    }
}

type chooses the route or event. data carries the payload. js-message does not impose a vocabulary or schema inside either value; applications can layer validation above this transport envelope.

What load() validates

InputOutcome
JSON string for an object with own type and dataLoaded
Buffer, boxed string, typed view, or other JSON-coercible textLoaded after native text coercion and parsing.
Existing object with own type and dataLoaded
Malformed JSONError envelope with a SyntaxError.
null, primitive, or arrayError envelope with a TypeError.
Object missing either required own keyError envelope with a TypeError.
A valid JSON value is not always a valid message.

[], 0, null, and {} parse successfully, but none satisfy the envelope contract.

Normalization rules

  • Extra keys in loaded input are ignored when the message is serialized.
  • Object input keeps the original data reference; js-message does not clone payloads.
  • Direct assignment to type and data is unrestricted for compatibility.
  • JSON uses native JSON.stringify behavior, including omission, conversion, and errors.
  • A later valid load() replaces an earlier error state, so one instance can recover.
Keep payloads wire-safe.

If a message must cross JSON, prefer strings, numbers, booleans, null, arrays, and plain objects. Cycles and BigInt make native serialization throw.